Replace some more qsort calls with std::sort
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70
71 /* readline include files */
72 #include "readline/tilde.h"
73
74 /* readline defines this. */
75 #undef savestring
76
77 #include "mi/mi-common.h"
78 #include "extension.h"
79 #include <algorithm>
80 #include "progspace-and-thread.h"
81 #include "gdbsupport/array-view.h"
82 #include "gdbsupport/gdb_optional.h"
83
84 /* Prototypes for local functions. */
85
86 static void map_breakpoint_numbers (const char *,
87 gdb::function_view<void (breakpoint *)>);
88
89 static void breakpoint_re_set_default (struct breakpoint *);
90
91 static void
92 create_sals_from_location_default (const struct event_location *location,
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
95
96 static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
104 int, int, int, unsigned);
105
106 static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, const struct event_location *location,
108 struct program_space *search_pspace);
109
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
112
113 static void mention (struct breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
117 const struct breakpoint_ops *);
118 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
121 /* This function is used in gdbtk sources and thus can not be made
122 static. */
123 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
124 struct symtab_and_line,
125 enum bptype,
126 const struct breakpoint_ops *);
127
128 static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
133
134 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
136 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
138 enum bptype bptype);
139
140 static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
142 struct obj_section *, int);
143
144 static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
147 static int breakpoint_location_address_match (struct bp_location *bl,
148 const struct address_space *aspace,
149 CORE_ADDR addr);
150
151 static int breakpoint_location_address_range_overlap (struct bp_location *,
152 const address_space *,
153 CORE_ADDR, int);
154
155 static int remove_breakpoint (struct bp_location *);
156 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
157
158 static enum print_stop_action print_bp_stop_message (bpstat bs);
159
160 static int hw_breakpoint_used_count (void);
161
162 static int hw_watchpoint_use_count (struct breakpoint *);
163
164 static int hw_watchpoint_used_count_others (struct breakpoint *except,
165 enum bptype type,
166 int *other_type_used);
167
168 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
169 int count);
170
171 static void free_bp_location (struct bp_location *loc);
172 static void incref_bp_location (struct bp_location *loc);
173 static void decref_bp_location (struct bp_location **loc);
174
175 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
176
177 /* update_global_location_list's modes of operation wrt to whether to
178 insert locations now. */
179 enum ugll_insert_mode
180 {
181 /* Don't insert any breakpoint locations into the inferior, only
182 remove already-inserted locations that no longer should be
183 inserted. Functions that delete a breakpoint or breakpoints
184 should specify this mode, so that deleting a breakpoint doesn't
185 have the side effect of inserting the locations of other
186 breakpoints that are marked not-inserted, but should_be_inserted
187 returns true on them.
188
189 This behavior is useful is situations close to tear-down -- e.g.,
190 after an exec, while the target still has execution, but
191 breakpoint shadows of the previous executable image should *NOT*
192 be restored to the new image; or before detaching, where the
193 target still has execution and wants to delete breakpoints from
194 GDB's lists, and all breakpoints had already been removed from
195 the inferior. */
196 UGLL_DONT_INSERT,
197
198 /* May insert breakpoints iff breakpoints_should_be_inserted_now
199 claims breakpoints should be inserted now. */
200 UGLL_MAY_INSERT,
201
202 /* Insert locations now, irrespective of
203 breakpoints_should_be_inserted_now. E.g., say all threads are
204 stopped right now, and the user did "continue". We need to
205 insert breakpoints _before_ resuming the target, but
206 UGLL_MAY_INSERT wouldn't insert them, because
207 breakpoints_should_be_inserted_now returns false at that point,
208 as no thread is running yet. */
209 UGLL_INSERT
210 };
211
212 static void update_global_location_list (enum ugll_insert_mode);
213
214 static void update_global_location_list_nothrow (enum ugll_insert_mode);
215
216 static void insert_breakpoint_locations (void);
217
218 static void trace_pass_command (const char *, int);
219
220 static void set_tracepoint_count (int num);
221
222 static bool is_masked_watchpoint (const struct breakpoint *b);
223
224 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
225
226 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
227 otherwise. */
228
229 static int strace_marker_p (struct breakpoint *b);
230
231 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
232 that are implemented on top of software or hardware breakpoints
233 (user breakpoints, internal and momentary breakpoints, etc.). */
234 static struct breakpoint_ops bkpt_base_breakpoint_ops;
235
236 /* Internal breakpoints class type. */
237 static struct breakpoint_ops internal_breakpoint_ops;
238
239 /* Momentary breakpoints class type. */
240 static struct breakpoint_ops momentary_breakpoint_ops;
241
242 /* The breakpoint_ops structure to be used in regular user created
243 breakpoints. */
244 struct breakpoint_ops bkpt_breakpoint_ops;
245
246 /* Breakpoints set on probes. */
247 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
248
249 /* Dynamic printf class type. */
250 struct breakpoint_ops dprintf_breakpoint_ops;
251
252 /* The style in which to perform a dynamic printf. This is a user
253 option because different output options have different tradeoffs;
254 if GDB does the printing, there is better error handling if there
255 is a problem with any of the arguments, but using an inferior
256 function lets you have special-purpose printers and sending of
257 output to the same place as compiled-in print functions. */
258
259 static const char dprintf_style_gdb[] = "gdb";
260 static const char dprintf_style_call[] = "call";
261 static const char dprintf_style_agent[] = "agent";
262 static const char *const dprintf_style_enums[] = {
263 dprintf_style_gdb,
264 dprintf_style_call,
265 dprintf_style_agent,
266 NULL
267 };
268 static const char *dprintf_style = dprintf_style_gdb;
269
270 /* The function to use for dynamic printf if the preferred style is to
271 call into the inferior. The value is simply a string that is
272 copied into the command, so it can be anything that GDB can
273 evaluate to a callable address, not necessarily a function name. */
274
275 static char *dprintf_function;
276
277 /* The channel to use for dynamic printf if the preferred style is to
278 call into the inferior; if a nonempty string, it will be passed to
279 the call as the first argument, with the format string as the
280 second. As with the dprintf function, this can be anything that
281 GDB knows how to evaluate, so in addition to common choices like
282 "stderr", this could be an app-specific expression like
283 "mystreams[curlogger]". */
284
285 static char *dprintf_channel;
286
287 /* True if dprintf commands should continue to operate even if GDB
288 has disconnected. */
289 static bool disconnected_dprintf = true;
290
291 struct command_line *
292 breakpoint_commands (struct breakpoint *b)
293 {
294 return b->commands ? b->commands.get () : NULL;
295 }
296
297 /* Flag indicating that a command has proceeded the inferior past the
298 current breakpoint. */
299
300 static bool breakpoint_proceeded;
301
302 const char *
303 bpdisp_text (enum bpdisp disp)
304 {
305 /* NOTE: the following values are a part of MI protocol and
306 represent values of 'disp' field returned when inferior stops at
307 a breakpoint. */
308 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
309
310 return bpdisps[(int) disp];
311 }
312
313 /* Prototypes for exported functions. */
314 /* If FALSE, gdb will not use hardware support for watchpoints, even
315 if such is available. */
316 static int can_use_hw_watchpoints;
317
318 static void
319 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
320 struct cmd_list_element *c,
321 const char *value)
322 {
323 fprintf_filtered (file,
324 _("Debugger's willingness to use "
325 "watchpoint hardware is %s.\n"),
326 value);
327 }
328
329 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
330 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
331 for unrecognized breakpoint locations.
332 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
333 static enum auto_boolean pending_break_support;
334 static void
335 show_pending_break_support (struct ui_file *file, int from_tty,
336 struct cmd_list_element *c,
337 const char *value)
338 {
339 fprintf_filtered (file,
340 _("Debugger's behavior regarding "
341 "pending breakpoints is %s.\n"),
342 value);
343 }
344
345 /* If true, gdb will automatically use hardware breakpoints for breakpoints
346 set with "break" but falling in read-only memory.
347 If false, gdb will warn about such breakpoints, but won't automatically
348 use hardware breakpoints. */
349 static bool automatic_hardware_breakpoints;
350 static void
351 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
352 struct cmd_list_element *c,
353 const char *value)
354 {
355 fprintf_filtered (file,
356 _("Automatic usage of hardware breakpoints is %s.\n"),
357 value);
358 }
359
360 /* If on, GDB keeps breakpoints inserted even if the inferior is
361 stopped, and immediately inserts any new breakpoints as soon as
362 they're created. If off (default), GDB keeps breakpoints off of
363 the target as long as possible. That is, it delays inserting
364 breakpoints until the next resume, and removes them again when the
365 target fully stops. This is a bit safer in case GDB crashes while
366 processing user input. */
367 static bool always_inserted_mode = false;
368
369 static void
370 show_always_inserted_mode (struct ui_file *file, int from_tty,
371 struct cmd_list_element *c, const char *value)
372 {
373 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
374 value);
375 }
376
377 /* See breakpoint.h. */
378
379 int
380 breakpoints_should_be_inserted_now (void)
381 {
382 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
383 {
384 /* If breakpoints are global, they should be inserted even if no
385 thread under gdb's control is running, or even if there are
386 no threads under GDB's control yet. */
387 return 1;
388 }
389 else if (target_has_execution)
390 {
391 if (always_inserted_mode)
392 {
393 /* The user wants breakpoints inserted even if all threads
394 are stopped. */
395 return 1;
396 }
397
398 if (threads_are_executing ())
399 return 1;
400
401 /* Don't remove breakpoints yet if, even though all threads are
402 stopped, we still have events to process. */
403 for (thread_info *tp : all_non_exited_threads ())
404 if (tp->resumed
405 && tp->suspend.waitstatus_pending_p)
406 return 1;
407 }
408 return 0;
409 }
410
411 static const char condition_evaluation_both[] = "host or target";
412
413 /* Modes for breakpoint condition evaluation. */
414 static const char condition_evaluation_auto[] = "auto";
415 static const char condition_evaluation_host[] = "host";
416 static const char condition_evaluation_target[] = "target";
417 static const char *const condition_evaluation_enums[] = {
418 condition_evaluation_auto,
419 condition_evaluation_host,
420 condition_evaluation_target,
421 NULL
422 };
423
424 /* Global that holds the current mode for breakpoint condition evaluation. */
425 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
426
427 /* Global that we use to display information to the user (gets its value from
428 condition_evaluation_mode_1. */
429 static const char *condition_evaluation_mode = condition_evaluation_auto;
430
431 /* Translate a condition evaluation mode MODE into either "host"
432 or "target". This is used mostly to translate from "auto" to the
433 real setting that is being used. It returns the translated
434 evaluation mode. */
435
436 static const char *
437 translate_condition_evaluation_mode (const char *mode)
438 {
439 if (mode == condition_evaluation_auto)
440 {
441 if (target_supports_evaluation_of_breakpoint_conditions ())
442 return condition_evaluation_target;
443 else
444 return condition_evaluation_host;
445 }
446 else
447 return mode;
448 }
449
450 /* Discovers what condition_evaluation_auto translates to. */
451
452 static const char *
453 breakpoint_condition_evaluation_mode (void)
454 {
455 return translate_condition_evaluation_mode (condition_evaluation_mode);
456 }
457
458 /* Return true if GDB should evaluate breakpoint conditions or false
459 otherwise. */
460
461 static int
462 gdb_evaluates_breakpoint_condition_p (void)
463 {
464 const char *mode = breakpoint_condition_evaluation_mode ();
465
466 return (mode == condition_evaluation_host);
467 }
468
469 /* Are we executing breakpoint commands? */
470 static int executing_breakpoint_commands;
471
472 /* Are overlay event breakpoints enabled? */
473 static int overlay_events_enabled;
474
475 /* See description in breakpoint.h. */
476 bool target_exact_watchpoints = false;
477
478 /* Walk the following statement or block through all breakpoints.
479 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
480 current breakpoint. */
481
482 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
483
484 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
485 for (B = breakpoint_chain; \
486 B ? (TMP=B->next, 1): 0; \
487 B = TMP)
488
489 /* Similar iterator for the low-level breakpoints. SAFE variant is
490 not provided so update_global_location_list must not be called
491 while executing the block of ALL_BP_LOCATIONS. */
492
493 #define ALL_BP_LOCATIONS(B,BP_TMP) \
494 for (BP_TMP = bp_locations; \
495 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
496 BP_TMP++)
497
498 /* Iterates through locations with address ADDRESS for the currently selected
499 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
500 to where the loop should start from.
501 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
502 appropriate location to start with. */
503
504 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
505 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
506 BP_LOCP_TMP = BP_LOCP_START; \
507 BP_LOCP_START \
508 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
509 && (*BP_LOCP_TMP)->address == ADDRESS); \
510 BP_LOCP_TMP++)
511
512 /* Iterator for tracepoints only. */
513
514 #define ALL_TRACEPOINTS(B) \
515 for (B = breakpoint_chain; B; B = B->next) \
516 if (is_tracepoint (B))
517
518 /* Chains of all breakpoints defined. */
519
520 static struct breakpoint *breakpoint_chain;
521
522 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
523
524 static struct bp_location **bp_locations;
525
526 /* Number of elements of BP_LOCATIONS. */
527
528 static unsigned bp_locations_count;
529
530 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
531 ADDRESS for the current elements of BP_LOCATIONS which get a valid
532 result from bp_location_has_shadow. You can use it for roughly
533 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
534 an address you need to read. */
535
536 static CORE_ADDR bp_locations_placed_address_before_address_max;
537
538 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
539 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
540 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
541 You can use it for roughly limiting the subrange of BP_LOCATIONS to
542 scan for shadow bytes for an address you need to read. */
543
544 static CORE_ADDR bp_locations_shadow_len_after_address_max;
545
546 /* The locations that no longer correspond to any breakpoint, unlinked
547 from the bp_locations array, but for which a hit may still be
548 reported by a target. */
549 static std::vector<bp_location *> moribund_locations;
550
551 /* Number of last breakpoint made. */
552
553 static int breakpoint_count;
554
555 /* The value of `breakpoint_count' before the last command that
556 created breakpoints. If the last (break-like) command created more
557 than one breakpoint, then the difference between BREAKPOINT_COUNT
558 and PREV_BREAKPOINT_COUNT is more than one. */
559 static int prev_breakpoint_count;
560
561 /* Number of last tracepoint made. */
562
563 static int tracepoint_count;
564
565 static struct cmd_list_element *breakpoint_set_cmdlist;
566 static struct cmd_list_element *breakpoint_show_cmdlist;
567 struct cmd_list_element *save_cmdlist;
568
569 /* See declaration at breakpoint.h. */
570
571 struct breakpoint *
572 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
573 void *user_data)
574 {
575 struct breakpoint *b = NULL;
576
577 ALL_BREAKPOINTS (b)
578 {
579 if (func (b, user_data) != 0)
580 break;
581 }
582
583 return b;
584 }
585
586 /* Return whether a breakpoint is an active enabled breakpoint. */
587 static int
588 breakpoint_enabled (struct breakpoint *b)
589 {
590 return (b->enable_state == bp_enabled);
591 }
592
593 /* Set breakpoint count to NUM. */
594
595 static void
596 set_breakpoint_count (int num)
597 {
598 prev_breakpoint_count = breakpoint_count;
599 breakpoint_count = num;
600 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
601 }
602
603 /* Used by `start_rbreak_breakpoints' below, to record the current
604 breakpoint count before "rbreak" creates any breakpoint. */
605 static int rbreak_start_breakpoint_count;
606
607 /* Called at the start an "rbreak" command to record the first
608 breakpoint made. */
609
610 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
611 {
612 rbreak_start_breakpoint_count = breakpoint_count;
613 }
614
615 /* Called at the end of an "rbreak" command to record the last
616 breakpoint made. */
617
618 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
619 {
620 prev_breakpoint_count = rbreak_start_breakpoint_count;
621 }
622
623 /* Used in run_command to zero the hit count when a new run starts. */
624
625 void
626 clear_breakpoint_hit_counts (void)
627 {
628 struct breakpoint *b;
629
630 ALL_BREAKPOINTS (b)
631 b->hit_count = 0;
632 }
633
634 \f
635 /* Return the breakpoint with the specified number, or NULL
636 if the number does not refer to an existing breakpoint. */
637
638 struct breakpoint *
639 get_breakpoint (int num)
640 {
641 struct breakpoint *b;
642
643 ALL_BREAKPOINTS (b)
644 if (b->number == num)
645 return b;
646
647 return NULL;
648 }
649
650 \f
651
652 /* Mark locations as "conditions have changed" in case the target supports
653 evaluating conditions on its side. */
654
655 static void
656 mark_breakpoint_modified (struct breakpoint *b)
657 {
658 struct bp_location *loc;
659
660 /* This is only meaningful if the target is
661 evaluating conditions and if the user has
662 opted for condition evaluation on the target's
663 side. */
664 if (gdb_evaluates_breakpoint_condition_p ()
665 || !target_supports_evaluation_of_breakpoint_conditions ())
666 return;
667
668 if (!is_breakpoint (b))
669 return;
670
671 for (loc = b->loc; loc; loc = loc->next)
672 loc->condition_changed = condition_modified;
673 }
674
675 /* Mark location as "conditions have changed" in case the target supports
676 evaluating conditions on its side. */
677
678 static void
679 mark_breakpoint_location_modified (struct bp_location *loc)
680 {
681 /* This is only meaningful if the target is
682 evaluating conditions and if the user has
683 opted for condition evaluation on the target's
684 side. */
685 if (gdb_evaluates_breakpoint_condition_p ()
686 || !target_supports_evaluation_of_breakpoint_conditions ())
687
688 return;
689
690 if (!is_breakpoint (loc->owner))
691 return;
692
693 loc->condition_changed = condition_modified;
694 }
695
696 /* Sets the condition-evaluation mode using the static global
697 condition_evaluation_mode. */
698
699 static void
700 set_condition_evaluation_mode (const char *args, int from_tty,
701 struct cmd_list_element *c)
702 {
703 const char *old_mode, *new_mode;
704
705 if ((condition_evaluation_mode_1 == condition_evaluation_target)
706 && !target_supports_evaluation_of_breakpoint_conditions ())
707 {
708 condition_evaluation_mode_1 = condition_evaluation_mode;
709 warning (_("Target does not support breakpoint condition evaluation.\n"
710 "Using host evaluation mode instead."));
711 return;
712 }
713
714 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
715 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
716
717 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
718 settings was "auto". */
719 condition_evaluation_mode = condition_evaluation_mode_1;
720
721 /* Only update the mode if the user picked a different one. */
722 if (new_mode != old_mode)
723 {
724 struct bp_location *loc, **loc_tmp;
725 /* If the user switched to a different evaluation mode, we
726 need to synch the changes with the target as follows:
727
728 "host" -> "target": Send all (valid) conditions to the target.
729 "target" -> "host": Remove all the conditions from the target.
730 */
731
732 if (new_mode == condition_evaluation_target)
733 {
734 /* Mark everything modified and synch conditions with the
735 target. */
736 ALL_BP_LOCATIONS (loc, loc_tmp)
737 mark_breakpoint_location_modified (loc);
738 }
739 else
740 {
741 /* Manually mark non-duplicate locations to synch conditions
742 with the target. We do this to remove all the conditions the
743 target knows about. */
744 ALL_BP_LOCATIONS (loc, loc_tmp)
745 if (is_breakpoint (loc->owner) && loc->inserted)
746 loc->needs_update = 1;
747 }
748
749 /* Do the update. */
750 update_global_location_list (UGLL_MAY_INSERT);
751 }
752
753 return;
754 }
755
756 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
757 what "auto" is translating to. */
758
759 static void
760 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
761 struct cmd_list_element *c, const char *value)
762 {
763 if (condition_evaluation_mode == condition_evaluation_auto)
764 fprintf_filtered (file,
765 _("Breakpoint condition evaluation "
766 "mode is %s (currently %s).\n"),
767 value,
768 breakpoint_condition_evaluation_mode ());
769 else
770 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
771 value);
772 }
773
774 /* A comparison function for bp_location AP and BP that is used by
775 bsearch. This comparison function only cares about addresses, unlike
776 the more general bp_location_is_less_than function. */
777
778 static int
779 bp_locations_compare_addrs (const void *ap, const void *bp)
780 {
781 const struct bp_location *a = *(const struct bp_location **) ap;
782 const struct bp_location *b = *(const struct bp_location **) bp;
783
784 if (a->address == b->address)
785 return 0;
786 else
787 return ((a->address > b->address) - (a->address < b->address));
788 }
789
790 /* Helper function to skip all bp_locations with addresses
791 less than ADDRESS. It returns the first bp_location that
792 is greater than or equal to ADDRESS. If none is found, just
793 return NULL. */
794
795 static struct bp_location **
796 get_first_locp_gte_addr (CORE_ADDR address)
797 {
798 struct bp_location dummy_loc;
799 struct bp_location *dummy_locp = &dummy_loc;
800 struct bp_location **locp_found = NULL;
801
802 /* Initialize the dummy location's address field. */
803 dummy_loc.address = address;
804
805 /* Find a close match to the first location at ADDRESS. */
806 locp_found = ((struct bp_location **)
807 bsearch (&dummy_locp, bp_locations, bp_locations_count,
808 sizeof (struct bp_location **),
809 bp_locations_compare_addrs));
810
811 /* Nothing was found, nothing left to do. */
812 if (locp_found == NULL)
813 return NULL;
814
815 /* We may have found a location that is at ADDRESS but is not the first in the
816 location's list. Go backwards (if possible) and locate the first one. */
817 while ((locp_found - 1) >= bp_locations
818 && (*(locp_found - 1))->address == address)
819 locp_found--;
820
821 return locp_found;
822 }
823
824 void
825 set_breakpoint_condition (struct breakpoint *b, const char *exp,
826 int from_tty)
827 {
828 xfree (b->cond_string);
829 b->cond_string = NULL;
830
831 if (is_watchpoint (b))
832 {
833 struct watchpoint *w = (struct watchpoint *) b;
834
835 w->cond_exp.reset ();
836 }
837 else
838 {
839 struct bp_location *loc;
840
841 for (loc = b->loc; loc; loc = loc->next)
842 {
843 loc->cond.reset ();
844
845 /* No need to free the condition agent expression
846 bytecode (if we have one). We will handle this
847 when we go through update_global_location_list. */
848 }
849 }
850
851 if (*exp == 0)
852 {
853 if (from_tty)
854 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
855 }
856 else
857 {
858 const char *arg = exp;
859
860 /* I don't know if it matters whether this is the string the user
861 typed in or the decompiled expression. */
862 b->cond_string = xstrdup (arg);
863 b->condition_not_parsed = 0;
864
865 if (is_watchpoint (b))
866 {
867 struct watchpoint *w = (struct watchpoint *) b;
868
869 innermost_block_tracker tracker;
870 arg = exp;
871 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
872 if (*arg)
873 error (_("Junk at end of expression"));
874 w->cond_exp_valid_block = tracker.block ();
875 }
876 else
877 {
878 struct bp_location *loc;
879
880 for (loc = b->loc; loc; loc = loc->next)
881 {
882 arg = exp;
883 loc->cond =
884 parse_exp_1 (&arg, loc->address,
885 block_for_pc (loc->address), 0);
886 if (*arg)
887 error (_("Junk at end of expression"));
888 }
889 }
890 }
891 mark_breakpoint_modified (b);
892
893 gdb::observers::breakpoint_modified.notify (b);
894 }
895
896 /* Completion for the "condition" command. */
897
898 static void
899 condition_completer (struct cmd_list_element *cmd,
900 completion_tracker &tracker,
901 const char *text, const char *word)
902 {
903 const char *space;
904
905 text = skip_spaces (text);
906 space = skip_to_space (text);
907 if (*space == '\0')
908 {
909 int len;
910 struct breakpoint *b;
911
912 if (text[0] == '$')
913 {
914 /* We don't support completion of history indices. */
915 if (!isdigit (text[1]))
916 complete_internalvar (tracker, &text[1]);
917 return;
918 }
919
920 /* We're completing the breakpoint number. */
921 len = strlen (text);
922
923 ALL_BREAKPOINTS (b)
924 {
925 char number[50];
926
927 xsnprintf (number, sizeof (number), "%d", b->number);
928
929 if (strncmp (number, text, len) == 0)
930 tracker.add_completion (make_unique_xstrdup (number));
931 }
932
933 return;
934 }
935
936 /* We're completing the expression part. */
937 text = skip_spaces (space);
938 expression_completer (cmd, tracker, text, word);
939 }
940
941 /* condition N EXP -- set break condition of breakpoint N to EXP. */
942
943 static void
944 condition_command (const char *arg, int from_tty)
945 {
946 struct breakpoint *b;
947 const char *p;
948 int bnum;
949
950 if (arg == 0)
951 error_no_arg (_("breakpoint number"));
952
953 p = arg;
954 bnum = get_number (&p);
955 if (bnum == 0)
956 error (_("Bad breakpoint argument: '%s'"), arg);
957
958 ALL_BREAKPOINTS (b)
959 if (b->number == bnum)
960 {
961 /* Check if this breakpoint has a "stop" method implemented in an
962 extension language. This method and conditions entered into GDB
963 from the CLI are mutually exclusive. */
964 const struct extension_language_defn *extlang
965 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
966
967 if (extlang != NULL)
968 {
969 error (_("Only one stop condition allowed. There is currently"
970 " a %s stop condition defined for this breakpoint."),
971 ext_lang_capitalized_name (extlang));
972 }
973 set_breakpoint_condition (b, p, from_tty);
974
975 if (is_breakpoint (b))
976 update_global_location_list (UGLL_MAY_INSERT);
977
978 return;
979 }
980
981 error (_("No breakpoint number %d."), bnum);
982 }
983
984 /* Check that COMMAND do not contain commands that are suitable
985 only for tracepoints and not suitable for ordinary breakpoints.
986 Throw if any such commands is found. */
987
988 static void
989 check_no_tracepoint_commands (struct command_line *commands)
990 {
991 struct command_line *c;
992
993 for (c = commands; c; c = c->next)
994 {
995 if (c->control_type == while_stepping_control)
996 error (_("The 'while-stepping' command can "
997 "only be used for tracepoints"));
998
999 check_no_tracepoint_commands (c->body_list_0.get ());
1000 check_no_tracepoint_commands (c->body_list_1.get ());
1001
1002 /* Not that command parsing removes leading whitespace and comment
1003 lines and also empty lines. So, we only need to check for
1004 command directly. */
1005 if (strstr (c->line, "collect ") == c->line)
1006 error (_("The 'collect' command can only be used for tracepoints"));
1007
1008 if (strstr (c->line, "teval ") == c->line)
1009 error (_("The 'teval' command can only be used for tracepoints"));
1010 }
1011 }
1012
1013 struct longjmp_breakpoint : public breakpoint
1014 {
1015 ~longjmp_breakpoint () override;
1016 };
1017
1018 /* Encapsulate tests for different types of tracepoints. */
1019
1020 static bool
1021 is_tracepoint_type (bptype type)
1022 {
1023 return (type == bp_tracepoint
1024 || type == bp_fast_tracepoint
1025 || type == bp_static_tracepoint);
1026 }
1027
1028 static bool
1029 is_longjmp_type (bptype type)
1030 {
1031 return type == bp_longjmp || type == bp_exception;
1032 }
1033
1034 /* See breakpoint.h. */
1035
1036 bool
1037 is_tracepoint (const struct breakpoint *b)
1038 {
1039 return is_tracepoint_type (b->type);
1040 }
1041
1042 /* Factory function to create an appropriate instance of breakpoint given
1043 TYPE. */
1044
1045 static std::unique_ptr<breakpoint>
1046 new_breakpoint_from_type (bptype type)
1047 {
1048 breakpoint *b;
1049
1050 if (is_tracepoint_type (type))
1051 b = new tracepoint ();
1052 else if (is_longjmp_type (type))
1053 b = new longjmp_breakpoint ();
1054 else
1055 b = new breakpoint ();
1056
1057 return std::unique_ptr<breakpoint> (b);
1058 }
1059
1060 /* A helper function that validates that COMMANDS are valid for a
1061 breakpoint. This function will throw an exception if a problem is
1062 found. */
1063
1064 static void
1065 validate_commands_for_breakpoint (struct breakpoint *b,
1066 struct command_line *commands)
1067 {
1068 if (is_tracepoint (b))
1069 {
1070 struct tracepoint *t = (struct tracepoint *) b;
1071 struct command_line *c;
1072 struct command_line *while_stepping = 0;
1073
1074 /* Reset the while-stepping step count. The previous commands
1075 might have included a while-stepping action, while the new
1076 ones might not. */
1077 t->step_count = 0;
1078
1079 /* We need to verify that each top-level element of commands is
1080 valid for tracepoints, that there's at most one
1081 while-stepping element, and that the while-stepping's body
1082 has valid tracing commands excluding nested while-stepping.
1083 We also need to validate the tracepoint action line in the
1084 context of the tracepoint --- validate_actionline actually
1085 has side effects, like setting the tracepoint's
1086 while-stepping STEP_COUNT, in addition to checking if the
1087 collect/teval actions parse and make sense in the
1088 tracepoint's context. */
1089 for (c = commands; c; c = c->next)
1090 {
1091 if (c->control_type == while_stepping_control)
1092 {
1093 if (b->type == bp_fast_tracepoint)
1094 error (_("The 'while-stepping' command "
1095 "cannot be used for fast tracepoint"));
1096 else if (b->type == bp_static_tracepoint)
1097 error (_("The 'while-stepping' command "
1098 "cannot be used for static tracepoint"));
1099
1100 if (while_stepping)
1101 error (_("The 'while-stepping' command "
1102 "can be used only once"));
1103 else
1104 while_stepping = c;
1105 }
1106
1107 validate_actionline (c->line, b);
1108 }
1109 if (while_stepping)
1110 {
1111 struct command_line *c2;
1112
1113 gdb_assert (while_stepping->body_list_1 == nullptr);
1114 c2 = while_stepping->body_list_0.get ();
1115 for (; c2; c2 = c2->next)
1116 {
1117 if (c2->control_type == while_stepping_control)
1118 error (_("The 'while-stepping' command cannot be nested"));
1119 }
1120 }
1121 }
1122 else
1123 {
1124 check_no_tracepoint_commands (commands);
1125 }
1126 }
1127
1128 /* Return a vector of all the static tracepoints set at ADDR. The
1129 caller is responsible for releasing the vector. */
1130
1131 std::vector<breakpoint *>
1132 static_tracepoints_here (CORE_ADDR addr)
1133 {
1134 struct breakpoint *b;
1135 std::vector<breakpoint *> found;
1136 struct bp_location *loc;
1137
1138 ALL_BREAKPOINTS (b)
1139 if (b->type == bp_static_tracepoint)
1140 {
1141 for (loc = b->loc; loc; loc = loc->next)
1142 if (loc->address == addr)
1143 found.push_back (b);
1144 }
1145
1146 return found;
1147 }
1148
1149 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1150 validate that only allowed commands are included. */
1151
1152 void
1153 breakpoint_set_commands (struct breakpoint *b,
1154 counted_command_line &&commands)
1155 {
1156 validate_commands_for_breakpoint (b, commands.get ());
1157
1158 b->commands = std::move (commands);
1159 gdb::observers::breakpoint_modified.notify (b);
1160 }
1161
1162 /* Set the internal `silent' flag on the breakpoint. Note that this
1163 is not the same as the "silent" that may appear in the breakpoint's
1164 commands. */
1165
1166 void
1167 breakpoint_set_silent (struct breakpoint *b, int silent)
1168 {
1169 int old_silent = b->silent;
1170
1171 b->silent = silent;
1172 if (old_silent != silent)
1173 gdb::observers::breakpoint_modified.notify (b);
1174 }
1175
1176 /* Set the thread for this breakpoint. If THREAD is -1, make the
1177 breakpoint work for any thread. */
1178
1179 void
1180 breakpoint_set_thread (struct breakpoint *b, int thread)
1181 {
1182 int old_thread = b->thread;
1183
1184 b->thread = thread;
1185 if (old_thread != thread)
1186 gdb::observers::breakpoint_modified.notify (b);
1187 }
1188
1189 /* Set the task for this breakpoint. If TASK is 0, make the
1190 breakpoint work for any task. */
1191
1192 void
1193 breakpoint_set_task (struct breakpoint *b, int task)
1194 {
1195 int old_task = b->task;
1196
1197 b->task = task;
1198 if (old_task != task)
1199 gdb::observers::breakpoint_modified.notify (b);
1200 }
1201
1202 static void
1203 commands_command_1 (const char *arg, int from_tty,
1204 struct command_line *control)
1205 {
1206 counted_command_line cmd;
1207 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1208 NULL after the call to read_command_lines if the user provides an empty
1209 list of command by just typing "end". */
1210 bool cmd_read = false;
1211
1212 std::string new_arg;
1213
1214 if (arg == NULL || !*arg)
1215 {
1216 if (breakpoint_count - prev_breakpoint_count > 1)
1217 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1218 breakpoint_count);
1219 else if (breakpoint_count > 0)
1220 new_arg = string_printf ("%d", breakpoint_count);
1221 arg = new_arg.c_str ();
1222 }
1223
1224 map_breakpoint_numbers
1225 (arg, [&] (breakpoint *b)
1226 {
1227 if (!cmd_read)
1228 {
1229 gdb_assert (cmd == NULL);
1230 if (control != NULL)
1231 cmd = control->body_list_0;
1232 else
1233 {
1234 std::string str
1235 = string_printf (_("Type commands for breakpoint(s) "
1236 "%s, one per line."),
1237 arg);
1238
1239 auto do_validate = [=] (const char *line)
1240 {
1241 validate_actionline (line, b);
1242 };
1243 gdb::function_view<void (const char *)> validator;
1244 if (is_tracepoint (b))
1245 validator = do_validate;
1246
1247 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1248 }
1249 cmd_read = true;
1250 }
1251
1252 /* If a breakpoint was on the list more than once, we don't need to
1253 do anything. */
1254 if (b->commands != cmd)
1255 {
1256 validate_commands_for_breakpoint (b, cmd.get ());
1257 b->commands = cmd;
1258 gdb::observers::breakpoint_modified.notify (b);
1259 }
1260 });
1261 }
1262
1263 static void
1264 commands_command (const char *arg, int from_tty)
1265 {
1266 commands_command_1 (arg, from_tty, NULL);
1267 }
1268
1269 /* Like commands_command, but instead of reading the commands from
1270 input stream, takes them from an already parsed command structure.
1271
1272 This is used by cli-script.c to DTRT with breakpoint commands
1273 that are part of if and while bodies. */
1274 enum command_control_type
1275 commands_from_control_command (const char *arg, struct command_line *cmd)
1276 {
1277 commands_command_1 (arg, 0, cmd);
1278 return simple_control;
1279 }
1280
1281 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1282
1283 static int
1284 bp_location_has_shadow (struct bp_location *bl)
1285 {
1286 if (bl->loc_type != bp_loc_software_breakpoint)
1287 return 0;
1288 if (!bl->inserted)
1289 return 0;
1290 if (bl->target_info.shadow_len == 0)
1291 /* BL isn't valid, or doesn't shadow memory. */
1292 return 0;
1293 return 1;
1294 }
1295
1296 /* Update BUF, which is LEN bytes read from the target address
1297 MEMADDR, by replacing a memory breakpoint with its shadowed
1298 contents.
1299
1300 If READBUF is not NULL, this buffer must not overlap with the of
1301 the breakpoint location's shadow_contents buffer. Otherwise, a
1302 failed assertion internal error will be raised. */
1303
1304 static void
1305 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1306 const gdb_byte *writebuf_org,
1307 ULONGEST memaddr, LONGEST len,
1308 struct bp_target_info *target_info,
1309 struct gdbarch *gdbarch)
1310 {
1311 /* Now do full processing of the found relevant range of elements. */
1312 CORE_ADDR bp_addr = 0;
1313 int bp_size = 0;
1314 int bptoffset = 0;
1315
1316 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1317 current_program_space->aspace, 0))
1318 {
1319 /* The breakpoint is inserted in a different address space. */
1320 return;
1321 }
1322
1323 /* Addresses and length of the part of the breakpoint that
1324 we need to copy. */
1325 bp_addr = target_info->placed_address;
1326 bp_size = target_info->shadow_len;
1327
1328 if (bp_addr + bp_size <= memaddr)
1329 {
1330 /* The breakpoint is entirely before the chunk of memory we are
1331 reading. */
1332 return;
1333 }
1334
1335 if (bp_addr >= memaddr + len)
1336 {
1337 /* The breakpoint is entirely after the chunk of memory we are
1338 reading. */
1339 return;
1340 }
1341
1342 /* Offset within shadow_contents. */
1343 if (bp_addr < memaddr)
1344 {
1345 /* Only copy the second part of the breakpoint. */
1346 bp_size -= memaddr - bp_addr;
1347 bptoffset = memaddr - bp_addr;
1348 bp_addr = memaddr;
1349 }
1350
1351 if (bp_addr + bp_size > memaddr + len)
1352 {
1353 /* Only copy the first part of the breakpoint. */
1354 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1355 }
1356
1357 if (readbuf != NULL)
1358 {
1359 /* Verify that the readbuf buffer does not overlap with the
1360 shadow_contents buffer. */
1361 gdb_assert (target_info->shadow_contents >= readbuf + len
1362 || readbuf >= (target_info->shadow_contents
1363 + target_info->shadow_len));
1364
1365 /* Update the read buffer with this inserted breakpoint's
1366 shadow. */
1367 memcpy (readbuf + bp_addr - memaddr,
1368 target_info->shadow_contents + bptoffset, bp_size);
1369 }
1370 else
1371 {
1372 const unsigned char *bp;
1373 CORE_ADDR addr = target_info->reqstd_address;
1374 int placed_size;
1375
1376 /* Update the shadow with what we want to write to memory. */
1377 memcpy (target_info->shadow_contents + bptoffset,
1378 writebuf_org + bp_addr - memaddr, bp_size);
1379
1380 /* Determine appropriate breakpoint contents and size for this
1381 address. */
1382 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1383
1384 /* Update the final write buffer with this inserted
1385 breakpoint's INSN. */
1386 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1387 }
1388 }
1389
1390 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1391 by replacing any memory breakpoints with their shadowed contents.
1392
1393 If READBUF is not NULL, this buffer must not overlap with any of
1394 the breakpoint location's shadow_contents buffers. Otherwise,
1395 a failed assertion internal error will be raised.
1396
1397 The range of shadowed area by each bp_location is:
1398 bl->address - bp_locations_placed_address_before_address_max
1399 up to bl->address + bp_locations_shadow_len_after_address_max
1400 The range we were requested to resolve shadows for is:
1401 memaddr ... memaddr + len
1402 Thus the safe cutoff boundaries for performance optimization are
1403 memaddr + len <= (bl->address
1404 - bp_locations_placed_address_before_address_max)
1405 and:
1406 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1407
1408 void
1409 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1410 const gdb_byte *writebuf_org,
1411 ULONGEST memaddr, LONGEST len)
1412 {
1413 /* Left boundary, right boundary and median element of our binary
1414 search. */
1415 unsigned bc_l, bc_r, bc;
1416
1417 /* Find BC_L which is a leftmost element which may affect BUF
1418 content. It is safe to report lower value but a failure to
1419 report higher one. */
1420
1421 bc_l = 0;
1422 bc_r = bp_locations_count;
1423 while (bc_l + 1 < bc_r)
1424 {
1425 struct bp_location *bl;
1426
1427 bc = (bc_l + bc_r) / 2;
1428 bl = bp_locations[bc];
1429
1430 /* Check first BL->ADDRESS will not overflow due to the added
1431 constant. Then advance the left boundary only if we are sure
1432 the BC element can in no way affect the BUF content (MEMADDR
1433 to MEMADDR + LEN range).
1434
1435 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1436 offset so that we cannot miss a breakpoint with its shadow
1437 range tail still reaching MEMADDR. */
1438
1439 if ((bl->address + bp_locations_shadow_len_after_address_max
1440 >= bl->address)
1441 && (bl->address + bp_locations_shadow_len_after_address_max
1442 <= memaddr))
1443 bc_l = bc;
1444 else
1445 bc_r = bc;
1446 }
1447
1448 /* Due to the binary search above, we need to make sure we pick the
1449 first location that's at BC_L's address. E.g., if there are
1450 multiple locations at the same address, BC_L may end up pointing
1451 at a duplicate location, and miss the "master"/"inserted"
1452 location. Say, given locations L1, L2 and L3 at addresses A and
1453 B:
1454
1455 L1@A, L2@A, L3@B, ...
1456
1457 BC_L could end up pointing at location L2, while the "master"
1458 location could be L1. Since the `loc->inserted' flag is only set
1459 on "master" locations, we'd forget to restore the shadow of L1
1460 and L2. */
1461 while (bc_l > 0
1462 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1463 bc_l--;
1464
1465 /* Now do full processing of the found relevant range of elements. */
1466
1467 for (bc = bc_l; bc < bp_locations_count; bc++)
1468 {
1469 struct bp_location *bl = bp_locations[bc];
1470
1471 /* bp_location array has BL->OWNER always non-NULL. */
1472 if (bl->owner->type == bp_none)
1473 warning (_("reading through apparently deleted breakpoint #%d?"),
1474 bl->owner->number);
1475
1476 /* Performance optimization: any further element can no longer affect BUF
1477 content. */
1478
1479 if (bl->address >= bp_locations_placed_address_before_address_max
1480 && memaddr + len <= (bl->address
1481 - bp_locations_placed_address_before_address_max))
1482 break;
1483
1484 if (!bp_location_has_shadow (bl))
1485 continue;
1486
1487 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1488 memaddr, len, &bl->target_info, bl->gdbarch);
1489 }
1490 }
1491
1492 /* See breakpoint.h. */
1493
1494 bool
1495 is_breakpoint (const struct breakpoint *bpt)
1496 {
1497 return (bpt->type == bp_breakpoint
1498 || bpt->type == bp_hardware_breakpoint
1499 || bpt->type == bp_dprintf);
1500 }
1501
1502 /* Return true if BPT is of any hardware watchpoint kind. */
1503
1504 static bool
1505 is_hardware_watchpoint (const struct breakpoint *bpt)
1506 {
1507 return (bpt->type == bp_hardware_watchpoint
1508 || bpt->type == bp_read_watchpoint
1509 || bpt->type == bp_access_watchpoint);
1510 }
1511
1512 /* See breakpoint.h. */
1513
1514 bool
1515 is_watchpoint (const struct breakpoint *bpt)
1516 {
1517 return (is_hardware_watchpoint (bpt)
1518 || bpt->type == bp_watchpoint);
1519 }
1520
1521 /* Returns true if the current thread and its running state are safe
1522 to evaluate or update watchpoint B. Watchpoints on local
1523 expressions need to be evaluated in the context of the thread that
1524 was current when the watchpoint was created, and, that thread needs
1525 to be stopped to be able to select the correct frame context.
1526 Watchpoints on global expressions can be evaluated on any thread,
1527 and in any state. It is presently left to the target allowing
1528 memory accesses when threads are running. */
1529
1530 static int
1531 watchpoint_in_thread_scope (struct watchpoint *b)
1532 {
1533 return (b->pspace == current_program_space
1534 && (b->watchpoint_thread == null_ptid
1535 || (inferior_ptid == b->watchpoint_thread
1536 && !inferior_thread ()->executing)));
1537 }
1538
1539 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1540 associated bp_watchpoint_scope breakpoint. */
1541
1542 static void
1543 watchpoint_del_at_next_stop (struct watchpoint *w)
1544 {
1545 if (w->related_breakpoint != w)
1546 {
1547 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1548 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1549 w->related_breakpoint->disposition = disp_del_at_next_stop;
1550 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1551 w->related_breakpoint = w;
1552 }
1553 w->disposition = disp_del_at_next_stop;
1554 }
1555
1556 /* Extract a bitfield value from value VAL using the bit parameters contained in
1557 watchpoint W. */
1558
1559 static struct value *
1560 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1561 {
1562 struct value *bit_val;
1563
1564 if (val == NULL)
1565 return NULL;
1566
1567 bit_val = allocate_value (value_type (val));
1568
1569 unpack_value_bitfield (bit_val,
1570 w->val_bitpos,
1571 w->val_bitsize,
1572 value_contents_for_printing (val),
1573 value_offset (val),
1574 val);
1575
1576 return bit_val;
1577 }
1578
1579 /* Allocate a dummy location and add it to B, which must be a software
1580 watchpoint. This is required because even if a software watchpoint
1581 is not watching any memory, bpstat_stop_status requires a location
1582 to be able to report stops. */
1583
1584 static void
1585 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1586 struct program_space *pspace)
1587 {
1588 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1589
1590 b->loc = allocate_bp_location (b);
1591 b->loc->pspace = pspace;
1592 b->loc->address = -1;
1593 b->loc->length = -1;
1594 }
1595
1596 /* Returns true if B is a software watchpoint that is not watching any
1597 memory (e.g., "watch $pc"). */
1598
1599 static bool
1600 is_no_memory_software_watchpoint (struct breakpoint *b)
1601 {
1602 return (b->type == bp_watchpoint
1603 && b->loc != NULL
1604 && b->loc->next == NULL
1605 && b->loc->address == -1
1606 && b->loc->length == -1);
1607 }
1608
1609 /* Assuming that B is a watchpoint:
1610 - Reparse watchpoint expression, if REPARSE is non-zero
1611 - Evaluate expression and store the result in B->val
1612 - Evaluate the condition if there is one, and store the result
1613 in b->loc->cond.
1614 - Update the list of values that must be watched in B->loc.
1615
1616 If the watchpoint disposition is disp_del_at_next_stop, then do
1617 nothing. If this is local watchpoint that is out of scope, delete
1618 it.
1619
1620 Even with `set breakpoint always-inserted on' the watchpoints are
1621 removed + inserted on each stop here. Normal breakpoints must
1622 never be removed because they might be missed by a running thread
1623 when debugging in non-stop mode. On the other hand, hardware
1624 watchpoints (is_hardware_watchpoint; processed here) are specific
1625 to each LWP since they are stored in each LWP's hardware debug
1626 registers. Therefore, such LWP must be stopped first in order to
1627 be able to modify its hardware watchpoints.
1628
1629 Hardware watchpoints must be reset exactly once after being
1630 presented to the user. It cannot be done sooner, because it would
1631 reset the data used to present the watchpoint hit to the user. And
1632 it must not be done later because it could display the same single
1633 watchpoint hit during multiple GDB stops. Note that the latter is
1634 relevant only to the hardware watchpoint types bp_read_watchpoint
1635 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1636 not user-visible - its hit is suppressed if the memory content has
1637 not changed.
1638
1639 The following constraints influence the location where we can reset
1640 hardware watchpoints:
1641
1642 * target_stopped_by_watchpoint and target_stopped_data_address are
1643 called several times when GDB stops.
1644
1645 [linux]
1646 * Multiple hardware watchpoints can be hit at the same time,
1647 causing GDB to stop. GDB only presents one hardware watchpoint
1648 hit at a time as the reason for stopping, and all the other hits
1649 are presented later, one after the other, each time the user
1650 requests the execution to be resumed. Execution is not resumed
1651 for the threads still having pending hit event stored in
1652 LWP_INFO->STATUS. While the watchpoint is already removed from
1653 the inferior on the first stop the thread hit event is kept being
1654 reported from its cached value by linux_nat_stopped_data_address
1655 until the real thread resume happens after the watchpoint gets
1656 presented and thus its LWP_INFO->STATUS gets reset.
1657
1658 Therefore the hardware watchpoint hit can get safely reset on the
1659 watchpoint removal from inferior. */
1660
1661 static void
1662 update_watchpoint (struct watchpoint *b, int reparse)
1663 {
1664 int within_current_scope;
1665 struct frame_id saved_frame_id;
1666 int frame_saved;
1667
1668 /* If this is a local watchpoint, we only want to check if the
1669 watchpoint frame is in scope if the current thread is the thread
1670 that was used to create the watchpoint. */
1671 if (!watchpoint_in_thread_scope (b))
1672 return;
1673
1674 if (b->disposition == disp_del_at_next_stop)
1675 return;
1676
1677 frame_saved = 0;
1678
1679 /* Determine if the watchpoint is within scope. */
1680 if (b->exp_valid_block == NULL)
1681 within_current_scope = 1;
1682 else
1683 {
1684 struct frame_info *fi = get_current_frame ();
1685 struct gdbarch *frame_arch = get_frame_arch (fi);
1686 CORE_ADDR frame_pc = get_frame_pc (fi);
1687
1688 /* If we're at a point where the stack has been destroyed
1689 (e.g. in a function epilogue), unwinding may not work
1690 properly. Do not attempt to recreate locations at this
1691 point. See similar comments in watchpoint_check. */
1692 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1693 return;
1694
1695 /* Save the current frame's ID so we can restore it after
1696 evaluating the watchpoint expression on its own frame. */
1697 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1698 took a frame parameter, so that we didn't have to change the
1699 selected frame. */
1700 frame_saved = 1;
1701 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1702
1703 fi = frame_find_by_id (b->watchpoint_frame);
1704 within_current_scope = (fi != NULL);
1705 if (within_current_scope)
1706 select_frame (fi);
1707 }
1708
1709 /* We don't free locations. They are stored in the bp_location array
1710 and update_global_location_list will eventually delete them and
1711 remove breakpoints if needed. */
1712 b->loc = NULL;
1713
1714 if (within_current_scope && reparse)
1715 {
1716 const char *s;
1717
1718 b->exp.reset ();
1719 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1720 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1721 /* If the meaning of expression itself changed, the old value is
1722 no longer relevant. We don't want to report a watchpoint hit
1723 to the user when the old value and the new value may actually
1724 be completely different objects. */
1725 b->val = NULL;
1726 b->val_valid = false;
1727
1728 /* Note that unlike with breakpoints, the watchpoint's condition
1729 expression is stored in the breakpoint object, not in the
1730 locations (re)created below. */
1731 if (b->cond_string != NULL)
1732 {
1733 b->cond_exp.reset ();
1734
1735 s = b->cond_string;
1736 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1737 }
1738 }
1739
1740 /* If we failed to parse the expression, for example because
1741 it refers to a global variable in a not-yet-loaded shared library,
1742 don't try to insert watchpoint. We don't automatically delete
1743 such watchpoint, though, since failure to parse expression
1744 is different from out-of-scope watchpoint. */
1745 if (!target_has_execution)
1746 {
1747 /* Without execution, memory can't change. No use to try and
1748 set watchpoint locations. The watchpoint will be reset when
1749 the target gains execution, through breakpoint_re_set. */
1750 if (!can_use_hw_watchpoints)
1751 {
1752 if (b->ops->works_in_software_mode (b))
1753 b->type = bp_watchpoint;
1754 else
1755 error (_("Can't set read/access watchpoint when "
1756 "hardware watchpoints are disabled."));
1757 }
1758 }
1759 else if (within_current_scope && b->exp)
1760 {
1761 int pc = 0;
1762 std::vector<value_ref_ptr> val_chain;
1763 struct value *v, *result;
1764 struct program_space *frame_pspace;
1765
1766 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1767
1768 /* Avoid setting b->val if it's already set. The meaning of
1769 b->val is 'the last value' user saw, and we should update
1770 it only if we reported that last value to user. As it
1771 happens, the code that reports it updates b->val directly.
1772 We don't keep track of the memory value for masked
1773 watchpoints. */
1774 if (!b->val_valid && !is_masked_watchpoint (b))
1775 {
1776 if (b->val_bitsize != 0)
1777 v = extract_bitfield_from_watchpoint_value (b, v);
1778 b->val = release_value (v);
1779 b->val_valid = true;
1780 }
1781
1782 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1783
1784 /* Look at each value on the value chain. */
1785 gdb_assert (!val_chain.empty ());
1786 for (const value_ref_ptr &iter : val_chain)
1787 {
1788 v = iter.get ();
1789
1790 /* If it's a memory location, and GDB actually needed
1791 its contents to evaluate the expression, then we
1792 must watch it. If the first value returned is
1793 still lazy, that means an error occurred reading it;
1794 watch it anyway in case it becomes readable. */
1795 if (VALUE_LVAL (v) == lval_memory
1796 && (v == val_chain[0] || ! value_lazy (v)))
1797 {
1798 struct type *vtype = check_typedef (value_type (v));
1799
1800 /* We only watch structs and arrays if user asked
1801 for it explicitly, never if they just happen to
1802 appear in the middle of some value chain. */
1803 if (v == result
1804 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1805 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1806 {
1807 CORE_ADDR addr;
1808 enum target_hw_bp_type type;
1809 struct bp_location *loc, **tmp;
1810 int bitpos = 0, bitsize = 0;
1811
1812 if (value_bitsize (v) != 0)
1813 {
1814 /* Extract the bit parameters out from the bitfield
1815 sub-expression. */
1816 bitpos = value_bitpos (v);
1817 bitsize = value_bitsize (v);
1818 }
1819 else if (v == result && b->val_bitsize != 0)
1820 {
1821 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1822 lvalue whose bit parameters are saved in the fields
1823 VAL_BITPOS and VAL_BITSIZE. */
1824 bitpos = b->val_bitpos;
1825 bitsize = b->val_bitsize;
1826 }
1827
1828 addr = value_address (v);
1829 if (bitsize != 0)
1830 {
1831 /* Skip the bytes that don't contain the bitfield. */
1832 addr += bitpos / 8;
1833 }
1834
1835 type = hw_write;
1836 if (b->type == bp_read_watchpoint)
1837 type = hw_read;
1838 else if (b->type == bp_access_watchpoint)
1839 type = hw_access;
1840
1841 loc = allocate_bp_location (b);
1842 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1843 ;
1844 *tmp = loc;
1845 loc->gdbarch = get_type_arch (value_type (v));
1846
1847 loc->pspace = frame_pspace;
1848 loc->address = address_significant (loc->gdbarch, addr);
1849
1850 if (bitsize != 0)
1851 {
1852 /* Just cover the bytes that make up the bitfield. */
1853 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1854 }
1855 else
1856 loc->length = TYPE_LENGTH (value_type (v));
1857
1858 loc->watchpoint_type = type;
1859 }
1860 }
1861 }
1862
1863 /* Change the type of breakpoint between hardware assisted or
1864 an ordinary watchpoint depending on the hardware support
1865 and free hardware slots. REPARSE is set when the inferior
1866 is started. */
1867 if (reparse)
1868 {
1869 int reg_cnt;
1870 enum bp_loc_type loc_type;
1871 struct bp_location *bl;
1872
1873 reg_cnt = can_use_hardware_watchpoint (val_chain);
1874
1875 if (reg_cnt)
1876 {
1877 int i, target_resources_ok, other_type_used;
1878 enum bptype type;
1879
1880 /* Use an exact watchpoint when there's only one memory region to be
1881 watched, and only one debug register is needed to watch it. */
1882 b->exact = target_exact_watchpoints && reg_cnt == 1;
1883
1884 /* We need to determine how many resources are already
1885 used for all other hardware watchpoints plus this one
1886 to see if we still have enough resources to also fit
1887 this watchpoint in as well. */
1888
1889 /* If this is a software watchpoint, we try to turn it
1890 to a hardware one -- count resources as if B was of
1891 hardware watchpoint type. */
1892 type = b->type;
1893 if (type == bp_watchpoint)
1894 type = bp_hardware_watchpoint;
1895
1896 /* This watchpoint may or may not have been placed on
1897 the list yet at this point (it won't be in the list
1898 if we're trying to create it for the first time,
1899 through watch_command), so always account for it
1900 manually. */
1901
1902 /* Count resources used by all watchpoints except B. */
1903 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1904
1905 /* Add in the resources needed for B. */
1906 i += hw_watchpoint_use_count (b);
1907
1908 target_resources_ok
1909 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1910 if (target_resources_ok <= 0)
1911 {
1912 int sw_mode = b->ops->works_in_software_mode (b);
1913
1914 if (target_resources_ok == 0 && !sw_mode)
1915 error (_("Target does not support this type of "
1916 "hardware watchpoint."));
1917 else if (target_resources_ok < 0 && !sw_mode)
1918 error (_("There are not enough available hardware "
1919 "resources for this watchpoint."));
1920
1921 /* Downgrade to software watchpoint. */
1922 b->type = bp_watchpoint;
1923 }
1924 else
1925 {
1926 /* If this was a software watchpoint, we've just
1927 found we have enough resources to turn it to a
1928 hardware watchpoint. Otherwise, this is a
1929 nop. */
1930 b->type = type;
1931 }
1932 }
1933 else if (!b->ops->works_in_software_mode (b))
1934 {
1935 if (!can_use_hw_watchpoints)
1936 error (_("Can't set read/access watchpoint when "
1937 "hardware watchpoints are disabled."));
1938 else
1939 error (_("Expression cannot be implemented with "
1940 "read/access watchpoint."));
1941 }
1942 else
1943 b->type = bp_watchpoint;
1944
1945 loc_type = (b->type == bp_watchpoint? bp_loc_other
1946 : bp_loc_hardware_watchpoint);
1947 for (bl = b->loc; bl; bl = bl->next)
1948 bl->loc_type = loc_type;
1949 }
1950
1951 /* If a software watchpoint is not watching any memory, then the
1952 above left it without any location set up. But,
1953 bpstat_stop_status requires a location to be able to report
1954 stops, so make sure there's at least a dummy one. */
1955 if (b->type == bp_watchpoint && b->loc == NULL)
1956 software_watchpoint_add_no_memory_location (b, frame_pspace);
1957 }
1958 else if (!within_current_scope)
1959 {
1960 printf_filtered (_("\
1961 Watchpoint %d deleted because the program has left the block\n\
1962 in which its expression is valid.\n"),
1963 b->number);
1964 watchpoint_del_at_next_stop (b);
1965 }
1966
1967 /* Restore the selected frame. */
1968 if (frame_saved)
1969 select_frame (frame_find_by_id (saved_frame_id));
1970 }
1971
1972
1973 /* Returns 1 iff breakpoint location should be
1974 inserted in the inferior. We don't differentiate the type of BL's owner
1975 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1976 breakpoint_ops is not defined, because in insert_bp_location,
1977 tracepoint's insert_location will not be called. */
1978 static int
1979 should_be_inserted (struct bp_location *bl)
1980 {
1981 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1982 return 0;
1983
1984 if (bl->owner->disposition == disp_del_at_next_stop)
1985 return 0;
1986
1987 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1988 return 0;
1989
1990 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1991 return 0;
1992
1993 /* This is set for example, when we're attached to the parent of a
1994 vfork, and have detached from the child. The child is running
1995 free, and we expect it to do an exec or exit, at which point the
1996 OS makes the parent schedulable again (and the target reports
1997 that the vfork is done). Until the child is done with the shared
1998 memory region, do not insert breakpoints in the parent, otherwise
1999 the child could still trip on the parent's breakpoints. Since
2000 the parent is blocked anyway, it won't miss any breakpoint. */
2001 if (bl->pspace->breakpoints_not_allowed)
2002 return 0;
2003
2004 /* Don't insert a breakpoint if we're trying to step past its
2005 location, except if the breakpoint is a single-step breakpoint,
2006 and the breakpoint's thread is the thread which is stepping past
2007 a breakpoint. */
2008 if ((bl->loc_type == bp_loc_software_breakpoint
2009 || bl->loc_type == bp_loc_hardware_breakpoint)
2010 && stepping_past_instruction_at (bl->pspace->aspace,
2011 bl->address)
2012 /* The single-step breakpoint may be inserted at the location
2013 we're trying to step if the instruction branches to itself.
2014 However, the instruction won't be executed at all and it may
2015 break the semantics of the instruction, for example, the
2016 instruction is a conditional branch or updates some flags.
2017 We can't fix it unless GDB is able to emulate the instruction
2018 or switch to displaced stepping. */
2019 && !(bl->owner->type == bp_single_step
2020 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2021 {
2022 if (debug_infrun)
2023 {
2024 fprintf_unfiltered (gdb_stdlog,
2025 "infrun: skipping breakpoint: "
2026 "stepping past insn at: %s\n",
2027 paddress (bl->gdbarch, bl->address));
2028 }
2029 return 0;
2030 }
2031
2032 /* Don't insert watchpoints if we're trying to step past the
2033 instruction that triggered one. */
2034 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2035 && stepping_past_nonsteppable_watchpoint ())
2036 {
2037 if (debug_infrun)
2038 {
2039 fprintf_unfiltered (gdb_stdlog,
2040 "infrun: stepping past non-steppable watchpoint. "
2041 "skipping watchpoint at %s:%d\n",
2042 paddress (bl->gdbarch, bl->address),
2043 bl->length);
2044 }
2045 return 0;
2046 }
2047
2048 return 1;
2049 }
2050
2051 /* Same as should_be_inserted but does the check assuming
2052 that the location is not duplicated. */
2053
2054 static int
2055 unduplicated_should_be_inserted (struct bp_location *bl)
2056 {
2057 int result;
2058 const int save_duplicate = bl->duplicate;
2059
2060 bl->duplicate = 0;
2061 result = should_be_inserted (bl);
2062 bl->duplicate = save_duplicate;
2063 return result;
2064 }
2065
2066 /* Parses a conditional described by an expression COND into an
2067 agent expression bytecode suitable for evaluation
2068 by the bytecode interpreter. Return NULL if there was
2069 any error during parsing. */
2070
2071 static agent_expr_up
2072 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2073 {
2074 if (cond == NULL)
2075 return NULL;
2076
2077 agent_expr_up aexpr;
2078
2079 /* We don't want to stop processing, so catch any errors
2080 that may show up. */
2081 try
2082 {
2083 aexpr = gen_eval_for_expr (scope, cond);
2084 }
2085
2086 catch (const gdb_exception_error &ex)
2087 {
2088 /* If we got here, it means the condition could not be parsed to a valid
2089 bytecode expression and thus can't be evaluated on the target's side.
2090 It's no use iterating through the conditions. */
2091 }
2092
2093 /* We have a valid agent expression. */
2094 return aexpr;
2095 }
2096
2097 /* Based on location BL, create a list of breakpoint conditions to be
2098 passed on to the target. If we have duplicated locations with different
2099 conditions, we will add such conditions to the list. The idea is that the
2100 target will evaluate the list of conditions and will only notify GDB when
2101 one of them is true. */
2102
2103 static void
2104 build_target_condition_list (struct bp_location *bl)
2105 {
2106 struct bp_location **locp = NULL, **loc2p;
2107 int null_condition_or_parse_error = 0;
2108 int modified = bl->needs_update;
2109 struct bp_location *loc;
2110
2111 /* Release conditions left over from a previous insert. */
2112 bl->target_info.conditions.clear ();
2113
2114 /* This is only meaningful if the target is
2115 evaluating conditions and if the user has
2116 opted for condition evaluation on the target's
2117 side. */
2118 if (gdb_evaluates_breakpoint_condition_p ()
2119 || !target_supports_evaluation_of_breakpoint_conditions ())
2120 return;
2121
2122 /* Do a first pass to check for locations with no assigned
2123 conditions or conditions that fail to parse to a valid agent expression
2124 bytecode. If any of these happen, then it's no use to send conditions
2125 to the target since this location will always trigger and generate a
2126 response back to GDB. */
2127 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2128 {
2129 loc = (*loc2p);
2130 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2131 {
2132 if (modified)
2133 {
2134 /* Re-parse the conditions since something changed. In that
2135 case we already freed the condition bytecodes (see
2136 force_breakpoint_reinsertion). We just
2137 need to parse the condition to bytecodes again. */
2138 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2139 loc->cond.get ());
2140 }
2141
2142 /* If we have a NULL bytecode expression, it means something
2143 went wrong or we have a null condition expression. */
2144 if (!loc->cond_bytecode)
2145 {
2146 null_condition_or_parse_error = 1;
2147 break;
2148 }
2149 }
2150 }
2151
2152 /* If any of these happened, it means we will have to evaluate the conditions
2153 for the location's address on gdb's side. It is no use keeping bytecodes
2154 for all the other duplicate locations, thus we free all of them here.
2155
2156 This is so we have a finer control over which locations' conditions are
2157 being evaluated by GDB or the remote stub. */
2158 if (null_condition_or_parse_error)
2159 {
2160 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2161 {
2162 loc = (*loc2p);
2163 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2164 {
2165 /* Only go as far as the first NULL bytecode is
2166 located. */
2167 if (!loc->cond_bytecode)
2168 return;
2169
2170 loc->cond_bytecode.reset ();
2171 }
2172 }
2173 }
2174
2175 /* No NULL conditions or failed bytecode generation. Build a condition list
2176 for this location's address. */
2177 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2178 {
2179 loc = (*loc2p);
2180 if (loc->cond
2181 && is_breakpoint (loc->owner)
2182 && loc->pspace->num == bl->pspace->num
2183 && loc->owner->enable_state == bp_enabled
2184 && loc->enabled)
2185 {
2186 /* Add the condition to the vector. This will be used later
2187 to send the conditions to the target. */
2188 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2189 }
2190 }
2191
2192 return;
2193 }
2194
2195 /* Parses a command described by string CMD into an agent expression
2196 bytecode suitable for evaluation by the bytecode interpreter.
2197 Return NULL if there was any error during parsing. */
2198
2199 static agent_expr_up
2200 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2201 {
2202 const char *cmdrest;
2203 const char *format_start, *format_end;
2204 struct gdbarch *gdbarch = get_current_arch ();
2205
2206 if (cmd == NULL)
2207 return NULL;
2208
2209 cmdrest = cmd;
2210
2211 if (*cmdrest == ',')
2212 ++cmdrest;
2213 cmdrest = skip_spaces (cmdrest);
2214
2215 if (*cmdrest++ != '"')
2216 error (_("No format string following the location"));
2217
2218 format_start = cmdrest;
2219
2220 format_pieces fpieces (&cmdrest);
2221
2222 format_end = cmdrest;
2223
2224 if (*cmdrest++ != '"')
2225 error (_("Bad format string, non-terminated '\"'."));
2226
2227 cmdrest = skip_spaces (cmdrest);
2228
2229 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2230 error (_("Invalid argument syntax"));
2231
2232 if (*cmdrest == ',')
2233 cmdrest++;
2234 cmdrest = skip_spaces (cmdrest);
2235
2236 /* For each argument, make an expression. */
2237
2238 std::vector<struct expression *> argvec;
2239 while (*cmdrest != '\0')
2240 {
2241 const char *cmd1;
2242
2243 cmd1 = cmdrest;
2244 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2245 argvec.push_back (expr.release ());
2246 cmdrest = cmd1;
2247 if (*cmdrest == ',')
2248 ++cmdrest;
2249 }
2250
2251 agent_expr_up aexpr;
2252
2253 /* We don't want to stop processing, so catch any errors
2254 that may show up. */
2255 try
2256 {
2257 aexpr = gen_printf (scope, gdbarch, 0, 0,
2258 format_start, format_end - format_start,
2259 argvec.size (), argvec.data ());
2260 }
2261 catch (const gdb_exception_error &ex)
2262 {
2263 /* If we got here, it means the command could not be parsed to a valid
2264 bytecode expression and thus can't be evaluated on the target's side.
2265 It's no use iterating through the other commands. */
2266 }
2267
2268 /* We have a valid agent expression, return it. */
2269 return aexpr;
2270 }
2271
2272 /* Based on location BL, create a list of breakpoint commands to be
2273 passed on to the target. If we have duplicated locations with
2274 different commands, we will add any such to the list. */
2275
2276 static void
2277 build_target_command_list (struct bp_location *bl)
2278 {
2279 struct bp_location **locp = NULL, **loc2p;
2280 int null_command_or_parse_error = 0;
2281 int modified = bl->needs_update;
2282 struct bp_location *loc;
2283
2284 /* Clear commands left over from a previous insert. */
2285 bl->target_info.tcommands.clear ();
2286
2287 if (!target_can_run_breakpoint_commands ())
2288 return;
2289
2290 /* For now, limit to agent-style dprintf breakpoints. */
2291 if (dprintf_style != dprintf_style_agent)
2292 return;
2293
2294 /* For now, if we have any duplicate location that isn't a dprintf,
2295 don't install the target-side commands, as that would make the
2296 breakpoint not be reported to the core, and we'd lose
2297 control. */
2298 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2299 {
2300 loc = (*loc2p);
2301 if (is_breakpoint (loc->owner)
2302 && loc->pspace->num == bl->pspace->num
2303 && loc->owner->type != bp_dprintf)
2304 return;
2305 }
2306
2307 /* Do a first pass to check for locations with no assigned
2308 conditions or conditions that fail to parse to a valid agent expression
2309 bytecode. If any of these happen, then it's no use to send conditions
2310 to the target since this location will always trigger and generate a
2311 response back to GDB. */
2312 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2313 {
2314 loc = (*loc2p);
2315 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2316 {
2317 if (modified)
2318 {
2319 /* Re-parse the commands since something changed. In that
2320 case we already freed the command bytecodes (see
2321 force_breakpoint_reinsertion). We just
2322 need to parse the command to bytecodes again. */
2323 loc->cmd_bytecode
2324 = parse_cmd_to_aexpr (bl->address,
2325 loc->owner->extra_string);
2326 }
2327
2328 /* If we have a NULL bytecode expression, it means something
2329 went wrong or we have a null command expression. */
2330 if (!loc->cmd_bytecode)
2331 {
2332 null_command_or_parse_error = 1;
2333 break;
2334 }
2335 }
2336 }
2337
2338 /* If anything failed, then we're not doing target-side commands,
2339 and so clean up. */
2340 if (null_command_or_parse_error)
2341 {
2342 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2343 {
2344 loc = (*loc2p);
2345 if (is_breakpoint (loc->owner)
2346 && loc->pspace->num == bl->pspace->num)
2347 {
2348 /* Only go as far as the first NULL bytecode is
2349 located. */
2350 if (loc->cmd_bytecode == NULL)
2351 return;
2352
2353 loc->cmd_bytecode.reset ();
2354 }
2355 }
2356 }
2357
2358 /* No NULL commands or failed bytecode generation. Build a command list
2359 for this location's address. */
2360 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2361 {
2362 loc = (*loc2p);
2363 if (loc->owner->extra_string
2364 && is_breakpoint (loc->owner)
2365 && loc->pspace->num == bl->pspace->num
2366 && loc->owner->enable_state == bp_enabled
2367 && loc->enabled)
2368 {
2369 /* Add the command to the vector. This will be used later
2370 to send the commands to the target. */
2371 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2372 }
2373 }
2374
2375 bl->target_info.persist = 0;
2376 /* Maybe flag this location as persistent. */
2377 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2378 bl->target_info.persist = 1;
2379 }
2380
2381 /* Return the kind of breakpoint on address *ADDR. Get the kind
2382 of breakpoint according to ADDR except single-step breakpoint.
2383 Get the kind of single-step breakpoint according to the current
2384 registers state. */
2385
2386 static int
2387 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2388 {
2389 if (bl->owner->type == bp_single_step)
2390 {
2391 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2392 struct regcache *regcache;
2393
2394 regcache = get_thread_regcache (thr);
2395
2396 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2397 regcache, addr);
2398 }
2399 else
2400 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2401 }
2402
2403 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2404 location. Any error messages are printed to TMP_ERROR_STREAM; and
2405 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2406 Returns 0 for success, 1 if the bp_location type is not supported or
2407 -1 for failure.
2408
2409 NOTE drow/2003-09-09: This routine could be broken down to an
2410 object-style method for each breakpoint or catchpoint type. */
2411 static int
2412 insert_bp_location (struct bp_location *bl,
2413 struct ui_file *tmp_error_stream,
2414 int *disabled_breaks,
2415 int *hw_breakpoint_error,
2416 int *hw_bp_error_explained_already)
2417 {
2418 gdb_exception bp_excpt;
2419
2420 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2421 return 0;
2422
2423 /* Note we don't initialize bl->target_info, as that wipes out
2424 the breakpoint location's shadow_contents if the breakpoint
2425 is still inserted at that location. This in turn breaks
2426 target_read_memory which depends on these buffers when
2427 a memory read is requested at the breakpoint location:
2428 Once the target_info has been wiped, we fail to see that
2429 we have a breakpoint inserted at that address and thus
2430 read the breakpoint instead of returning the data saved in
2431 the breakpoint location's shadow contents. */
2432 bl->target_info.reqstd_address = bl->address;
2433 bl->target_info.placed_address_space = bl->pspace->aspace;
2434 bl->target_info.length = bl->length;
2435
2436 /* When working with target-side conditions, we must pass all the conditions
2437 for the same breakpoint address down to the target since GDB will not
2438 insert those locations. With a list of breakpoint conditions, the target
2439 can decide when to stop and notify GDB. */
2440
2441 if (is_breakpoint (bl->owner))
2442 {
2443 build_target_condition_list (bl);
2444 build_target_command_list (bl);
2445 /* Reset the modification marker. */
2446 bl->needs_update = 0;
2447 }
2448
2449 if (bl->loc_type == bp_loc_software_breakpoint
2450 || bl->loc_type == bp_loc_hardware_breakpoint)
2451 {
2452 if (bl->owner->type != bp_hardware_breakpoint)
2453 {
2454 /* If the explicitly specified breakpoint type
2455 is not hardware breakpoint, check the memory map to see
2456 if the breakpoint address is in read only memory or not.
2457
2458 Two important cases are:
2459 - location type is not hardware breakpoint, memory
2460 is readonly. We change the type of the location to
2461 hardware breakpoint.
2462 - location type is hardware breakpoint, memory is
2463 read-write. This means we've previously made the
2464 location hardware one, but then the memory map changed,
2465 so we undo.
2466
2467 When breakpoints are removed, remove_breakpoints will use
2468 location types we've just set here, the only possible
2469 problem is that memory map has changed during running
2470 program, but it's not going to work anyway with current
2471 gdb. */
2472 struct mem_region *mr
2473 = lookup_mem_region (bl->target_info.reqstd_address);
2474
2475 if (mr)
2476 {
2477 if (automatic_hardware_breakpoints)
2478 {
2479 enum bp_loc_type new_type;
2480
2481 if (mr->attrib.mode != MEM_RW)
2482 new_type = bp_loc_hardware_breakpoint;
2483 else
2484 new_type = bp_loc_software_breakpoint;
2485
2486 if (new_type != bl->loc_type)
2487 {
2488 static int said = 0;
2489
2490 bl->loc_type = new_type;
2491 if (!said)
2492 {
2493 fprintf_filtered (gdb_stdout,
2494 _("Note: automatically using "
2495 "hardware breakpoints for "
2496 "read-only addresses.\n"));
2497 said = 1;
2498 }
2499 }
2500 }
2501 else if (bl->loc_type == bp_loc_software_breakpoint
2502 && mr->attrib.mode != MEM_RW)
2503 {
2504 fprintf_unfiltered (tmp_error_stream,
2505 _("Cannot insert breakpoint %d.\n"
2506 "Cannot set software breakpoint "
2507 "at read-only address %s\n"),
2508 bl->owner->number,
2509 paddress (bl->gdbarch, bl->address));
2510 return 1;
2511 }
2512 }
2513 }
2514
2515 /* First check to see if we have to handle an overlay. */
2516 if (overlay_debugging == ovly_off
2517 || bl->section == NULL
2518 || !(section_is_overlay (bl->section)))
2519 {
2520 /* No overlay handling: just set the breakpoint. */
2521 try
2522 {
2523 int val;
2524
2525 val = bl->owner->ops->insert_location (bl);
2526 if (val)
2527 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2528 }
2529 catch (gdb_exception &e)
2530 {
2531 bp_excpt = std::move (e);
2532 }
2533 }
2534 else
2535 {
2536 /* This breakpoint is in an overlay section.
2537 Shall we set a breakpoint at the LMA? */
2538 if (!overlay_events_enabled)
2539 {
2540 /* Yes -- overlay event support is not active,
2541 so we must try to set a breakpoint at the LMA.
2542 This will not work for a hardware breakpoint. */
2543 if (bl->loc_type == bp_loc_hardware_breakpoint)
2544 warning (_("hardware breakpoint %d not supported in overlay!"),
2545 bl->owner->number);
2546 else
2547 {
2548 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2549 bl->section);
2550 /* Set a software (trap) breakpoint at the LMA. */
2551 bl->overlay_target_info = bl->target_info;
2552 bl->overlay_target_info.reqstd_address = addr;
2553
2554 /* No overlay handling: just set the breakpoint. */
2555 try
2556 {
2557 int val;
2558
2559 bl->overlay_target_info.kind
2560 = breakpoint_kind (bl, &addr);
2561 bl->overlay_target_info.placed_address = addr;
2562 val = target_insert_breakpoint (bl->gdbarch,
2563 &bl->overlay_target_info);
2564 if (val)
2565 bp_excpt
2566 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2567 }
2568 catch (gdb_exception &e)
2569 {
2570 bp_excpt = std::move (e);
2571 }
2572
2573 if (bp_excpt.reason != 0)
2574 fprintf_unfiltered (tmp_error_stream,
2575 "Overlay breakpoint %d "
2576 "failed: in ROM?\n",
2577 bl->owner->number);
2578 }
2579 }
2580 /* Shall we set a breakpoint at the VMA? */
2581 if (section_is_mapped (bl->section))
2582 {
2583 /* Yes. This overlay section is mapped into memory. */
2584 try
2585 {
2586 int val;
2587
2588 val = bl->owner->ops->insert_location (bl);
2589 if (val)
2590 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2591 }
2592 catch (gdb_exception &e)
2593 {
2594 bp_excpt = std::move (e);
2595 }
2596 }
2597 else
2598 {
2599 /* No. This breakpoint will not be inserted.
2600 No error, but do not mark the bp as 'inserted'. */
2601 return 0;
2602 }
2603 }
2604
2605 if (bp_excpt.reason != 0)
2606 {
2607 /* Can't set the breakpoint. */
2608
2609 /* In some cases, we might not be able to insert a
2610 breakpoint in a shared library that has already been
2611 removed, but we have not yet processed the shlib unload
2612 event. Unfortunately, some targets that implement
2613 breakpoint insertion themselves can't tell why the
2614 breakpoint insertion failed (e.g., the remote target
2615 doesn't define error codes), so we must treat generic
2616 errors as memory errors. */
2617 if (bp_excpt.reason == RETURN_ERROR
2618 && (bp_excpt.error == GENERIC_ERROR
2619 || bp_excpt.error == MEMORY_ERROR)
2620 && bl->loc_type == bp_loc_software_breakpoint
2621 && (solib_name_from_address (bl->pspace, bl->address)
2622 || shared_objfile_contains_address_p (bl->pspace,
2623 bl->address)))
2624 {
2625 /* See also: disable_breakpoints_in_shlibs. */
2626 bl->shlib_disabled = 1;
2627 gdb::observers::breakpoint_modified.notify (bl->owner);
2628 if (!*disabled_breaks)
2629 {
2630 fprintf_unfiltered (tmp_error_stream,
2631 "Cannot insert breakpoint %d.\n",
2632 bl->owner->number);
2633 fprintf_unfiltered (tmp_error_stream,
2634 "Temporarily disabling shared "
2635 "library breakpoints:\n");
2636 }
2637 *disabled_breaks = 1;
2638 fprintf_unfiltered (tmp_error_stream,
2639 "breakpoint #%d\n", bl->owner->number);
2640 return 0;
2641 }
2642 else
2643 {
2644 if (bl->loc_type == bp_loc_hardware_breakpoint)
2645 {
2646 *hw_breakpoint_error = 1;
2647 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2648 fprintf_unfiltered (tmp_error_stream,
2649 "Cannot insert hardware breakpoint %d%s",
2650 bl->owner->number,
2651 bp_excpt.message ? ":" : ".\n");
2652 if (bp_excpt.message != NULL)
2653 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2654 bp_excpt.what ());
2655 }
2656 else
2657 {
2658 if (bp_excpt.message == NULL)
2659 {
2660 std::string message
2661 = memory_error_message (TARGET_XFER_E_IO,
2662 bl->gdbarch, bl->address);
2663
2664 fprintf_unfiltered (tmp_error_stream,
2665 "Cannot insert breakpoint %d.\n"
2666 "%s\n",
2667 bl->owner->number, message.c_str ());
2668 }
2669 else
2670 {
2671 fprintf_unfiltered (tmp_error_stream,
2672 "Cannot insert breakpoint %d: %s\n",
2673 bl->owner->number,
2674 bp_excpt.what ());
2675 }
2676 }
2677 return 1;
2678
2679 }
2680 }
2681 else
2682 bl->inserted = 1;
2683
2684 return 0;
2685 }
2686
2687 else if (bl->loc_type == bp_loc_hardware_watchpoint
2688 /* NOTE drow/2003-09-08: This state only exists for removing
2689 watchpoints. It's not clear that it's necessary... */
2690 && bl->owner->disposition != disp_del_at_next_stop)
2691 {
2692 int val;
2693
2694 gdb_assert (bl->owner->ops != NULL
2695 && bl->owner->ops->insert_location != NULL);
2696
2697 val = bl->owner->ops->insert_location (bl);
2698
2699 /* If trying to set a read-watchpoint, and it turns out it's not
2700 supported, try emulating one with an access watchpoint. */
2701 if (val == 1 && bl->watchpoint_type == hw_read)
2702 {
2703 struct bp_location *loc, **loc_temp;
2704
2705 /* But don't try to insert it, if there's already another
2706 hw_access location that would be considered a duplicate
2707 of this one. */
2708 ALL_BP_LOCATIONS (loc, loc_temp)
2709 if (loc != bl
2710 && loc->watchpoint_type == hw_access
2711 && watchpoint_locations_match (bl, loc))
2712 {
2713 bl->duplicate = 1;
2714 bl->inserted = 1;
2715 bl->target_info = loc->target_info;
2716 bl->watchpoint_type = hw_access;
2717 val = 0;
2718 break;
2719 }
2720
2721 if (val == 1)
2722 {
2723 bl->watchpoint_type = hw_access;
2724 val = bl->owner->ops->insert_location (bl);
2725
2726 if (val)
2727 /* Back to the original value. */
2728 bl->watchpoint_type = hw_read;
2729 }
2730 }
2731
2732 bl->inserted = (val == 0);
2733 }
2734
2735 else if (bl->owner->type == bp_catchpoint)
2736 {
2737 int val;
2738
2739 gdb_assert (bl->owner->ops != NULL
2740 && bl->owner->ops->insert_location != NULL);
2741
2742 val = bl->owner->ops->insert_location (bl);
2743 if (val)
2744 {
2745 bl->owner->enable_state = bp_disabled;
2746
2747 if (val == 1)
2748 warning (_("\
2749 Error inserting catchpoint %d: Your system does not support this type\n\
2750 of catchpoint."), bl->owner->number);
2751 else
2752 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2753 }
2754
2755 bl->inserted = (val == 0);
2756
2757 /* We've already printed an error message if there was a problem
2758 inserting this catchpoint, and we've disabled the catchpoint,
2759 so just return success. */
2760 return 0;
2761 }
2762
2763 return 0;
2764 }
2765
2766 /* This function is called when program space PSPACE is about to be
2767 deleted. It takes care of updating breakpoints to not reference
2768 PSPACE anymore. */
2769
2770 void
2771 breakpoint_program_space_exit (struct program_space *pspace)
2772 {
2773 struct breakpoint *b, *b_temp;
2774 struct bp_location *loc, **loc_temp;
2775
2776 /* Remove any breakpoint that was set through this program space. */
2777 ALL_BREAKPOINTS_SAFE (b, b_temp)
2778 {
2779 if (b->pspace == pspace)
2780 delete_breakpoint (b);
2781 }
2782
2783 /* Breakpoints set through other program spaces could have locations
2784 bound to PSPACE as well. Remove those. */
2785 ALL_BP_LOCATIONS (loc, loc_temp)
2786 {
2787 struct bp_location *tmp;
2788
2789 if (loc->pspace == pspace)
2790 {
2791 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2792 if (loc->owner->loc == loc)
2793 loc->owner->loc = loc->next;
2794 else
2795 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2796 if (tmp->next == loc)
2797 {
2798 tmp->next = loc->next;
2799 break;
2800 }
2801 }
2802 }
2803
2804 /* Now update the global location list to permanently delete the
2805 removed locations above. */
2806 update_global_location_list (UGLL_DONT_INSERT);
2807 }
2808
2809 /* Make sure all breakpoints are inserted in inferior.
2810 Throws exception on any error.
2811 A breakpoint that is already inserted won't be inserted
2812 again, so calling this function twice is safe. */
2813 void
2814 insert_breakpoints (void)
2815 {
2816 struct breakpoint *bpt;
2817
2818 ALL_BREAKPOINTS (bpt)
2819 if (is_hardware_watchpoint (bpt))
2820 {
2821 struct watchpoint *w = (struct watchpoint *) bpt;
2822
2823 update_watchpoint (w, 0 /* don't reparse. */);
2824 }
2825
2826 /* Updating watchpoints creates new locations, so update the global
2827 location list. Explicitly tell ugll to insert locations and
2828 ignore breakpoints_always_inserted_mode. */
2829 update_global_location_list (UGLL_INSERT);
2830 }
2831
2832 /* Invoke CALLBACK for each of bp_location. */
2833
2834 void
2835 iterate_over_bp_locations (walk_bp_location_callback callback)
2836 {
2837 struct bp_location *loc, **loc_tmp;
2838
2839 ALL_BP_LOCATIONS (loc, loc_tmp)
2840 {
2841 callback (loc, NULL);
2842 }
2843 }
2844
2845 /* This is used when we need to synch breakpoint conditions between GDB and the
2846 target. It is the case with deleting and disabling of breakpoints when using
2847 always-inserted mode. */
2848
2849 static void
2850 update_inserted_breakpoint_locations (void)
2851 {
2852 struct bp_location *bl, **blp_tmp;
2853 int error_flag = 0;
2854 int val = 0;
2855 int disabled_breaks = 0;
2856 int hw_breakpoint_error = 0;
2857 int hw_bp_details_reported = 0;
2858
2859 string_file tmp_error_stream;
2860
2861 /* Explicitly mark the warning -- this will only be printed if
2862 there was an error. */
2863 tmp_error_stream.puts ("Warning:\n");
2864
2865 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2866
2867 ALL_BP_LOCATIONS (bl, blp_tmp)
2868 {
2869 /* We only want to update software breakpoints and hardware
2870 breakpoints. */
2871 if (!is_breakpoint (bl->owner))
2872 continue;
2873
2874 /* We only want to update locations that are already inserted
2875 and need updating. This is to avoid unwanted insertion during
2876 deletion of breakpoints. */
2877 if (!bl->inserted || !bl->needs_update)
2878 continue;
2879
2880 switch_to_program_space_and_thread (bl->pspace);
2881
2882 /* For targets that support global breakpoints, there's no need
2883 to select an inferior to insert breakpoint to. In fact, even
2884 if we aren't attached to any process yet, we should still
2885 insert breakpoints. */
2886 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2887 && inferior_ptid == null_ptid)
2888 continue;
2889
2890 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2891 &hw_breakpoint_error, &hw_bp_details_reported);
2892 if (val)
2893 error_flag = val;
2894 }
2895
2896 if (error_flag)
2897 {
2898 target_terminal::ours_for_output ();
2899 error_stream (tmp_error_stream);
2900 }
2901 }
2902
2903 /* Used when starting or continuing the program. */
2904
2905 static void
2906 insert_breakpoint_locations (void)
2907 {
2908 struct breakpoint *bpt;
2909 struct bp_location *bl, **blp_tmp;
2910 int error_flag = 0;
2911 int val = 0;
2912 int disabled_breaks = 0;
2913 int hw_breakpoint_error = 0;
2914 int hw_bp_error_explained_already = 0;
2915
2916 string_file tmp_error_stream;
2917
2918 /* Explicitly mark the warning -- this will only be printed if
2919 there was an error. */
2920 tmp_error_stream.puts ("Warning:\n");
2921
2922 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2923
2924 ALL_BP_LOCATIONS (bl, blp_tmp)
2925 {
2926 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2927 continue;
2928
2929 /* There is no point inserting thread-specific breakpoints if
2930 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2931 has BL->OWNER always non-NULL. */
2932 if (bl->owner->thread != -1
2933 && !valid_global_thread_id (bl->owner->thread))
2934 continue;
2935
2936 switch_to_program_space_and_thread (bl->pspace);
2937
2938 /* For targets that support global breakpoints, there's no need
2939 to select an inferior to insert breakpoint to. In fact, even
2940 if we aren't attached to any process yet, we should still
2941 insert breakpoints. */
2942 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2943 && inferior_ptid == null_ptid)
2944 continue;
2945
2946 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2947 &hw_breakpoint_error, &hw_bp_error_explained_already);
2948 if (val)
2949 error_flag = val;
2950 }
2951
2952 /* If we failed to insert all locations of a watchpoint, remove
2953 them, as half-inserted watchpoint is of limited use. */
2954 ALL_BREAKPOINTS (bpt)
2955 {
2956 int some_failed = 0;
2957 struct bp_location *loc;
2958
2959 if (!is_hardware_watchpoint (bpt))
2960 continue;
2961
2962 if (!breakpoint_enabled (bpt))
2963 continue;
2964
2965 if (bpt->disposition == disp_del_at_next_stop)
2966 continue;
2967
2968 for (loc = bpt->loc; loc; loc = loc->next)
2969 if (!loc->inserted && should_be_inserted (loc))
2970 {
2971 some_failed = 1;
2972 break;
2973 }
2974 if (some_failed)
2975 {
2976 for (loc = bpt->loc; loc; loc = loc->next)
2977 if (loc->inserted)
2978 remove_breakpoint (loc);
2979
2980 hw_breakpoint_error = 1;
2981 tmp_error_stream.printf ("Could not insert "
2982 "hardware watchpoint %d.\n",
2983 bpt->number);
2984 error_flag = -1;
2985 }
2986 }
2987
2988 if (error_flag)
2989 {
2990 /* If a hardware breakpoint or watchpoint was inserted, add a
2991 message about possibly exhausted resources. */
2992 if (hw_breakpoint_error && !hw_bp_error_explained_already)
2993 {
2994 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
2995 You may have requested too many hardware breakpoints/watchpoints.\n");
2996 }
2997 target_terminal::ours_for_output ();
2998 error_stream (tmp_error_stream);
2999 }
3000 }
3001
3002 /* Used when the program stops.
3003 Returns zero if successful, or non-zero if there was a problem
3004 removing a breakpoint location. */
3005
3006 int
3007 remove_breakpoints (void)
3008 {
3009 struct bp_location *bl, **blp_tmp;
3010 int val = 0;
3011
3012 ALL_BP_LOCATIONS (bl, blp_tmp)
3013 {
3014 if (bl->inserted && !is_tracepoint (bl->owner))
3015 val |= remove_breakpoint (bl);
3016 }
3017 return val;
3018 }
3019
3020 /* When a thread exits, remove breakpoints that are related to
3021 that thread. */
3022
3023 static void
3024 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3025 {
3026 struct breakpoint *b, *b_tmp;
3027
3028 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3029 {
3030 if (b->thread == tp->global_num && user_breakpoint_p (b))
3031 {
3032 b->disposition = disp_del_at_next_stop;
3033
3034 printf_filtered (_("\
3035 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3036 b->number, print_thread_id (tp));
3037
3038 /* Hide it from the user. */
3039 b->number = 0;
3040 }
3041 }
3042 }
3043
3044 /* See breakpoint.h. */
3045
3046 void
3047 remove_breakpoints_inf (inferior *inf)
3048 {
3049 struct bp_location *bl, **blp_tmp;
3050 int val;
3051
3052 ALL_BP_LOCATIONS (bl, blp_tmp)
3053 {
3054 if (bl->pspace != inf->pspace)
3055 continue;
3056
3057 if (bl->inserted && !bl->target_info.persist)
3058 {
3059 val = remove_breakpoint (bl);
3060 if (val != 0)
3061 return;
3062 }
3063 }
3064 }
3065
3066 static int internal_breakpoint_number = -1;
3067
3068 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3069 If INTERNAL is non-zero, the breakpoint number will be populated
3070 from internal_breakpoint_number and that variable decremented.
3071 Otherwise the breakpoint number will be populated from
3072 breakpoint_count and that value incremented. Internal breakpoints
3073 do not set the internal var bpnum. */
3074 static void
3075 set_breakpoint_number (int internal, struct breakpoint *b)
3076 {
3077 if (internal)
3078 b->number = internal_breakpoint_number--;
3079 else
3080 {
3081 set_breakpoint_count (breakpoint_count + 1);
3082 b->number = breakpoint_count;
3083 }
3084 }
3085
3086 static struct breakpoint *
3087 create_internal_breakpoint (struct gdbarch *gdbarch,
3088 CORE_ADDR address, enum bptype type,
3089 const struct breakpoint_ops *ops)
3090 {
3091 symtab_and_line sal;
3092 sal.pc = address;
3093 sal.section = find_pc_overlay (sal.pc);
3094 sal.pspace = current_program_space;
3095
3096 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3097 b->number = internal_breakpoint_number--;
3098 b->disposition = disp_donttouch;
3099
3100 return b;
3101 }
3102
3103 static const char *const longjmp_names[] =
3104 {
3105 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3106 };
3107 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3108
3109 /* Per-objfile data private to breakpoint.c. */
3110 struct breakpoint_objfile_data
3111 {
3112 /* Minimal symbol for "_ovly_debug_event" (if any). */
3113 struct bound_minimal_symbol overlay_msym {};
3114
3115 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3116 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3117
3118 /* True if we have looked for longjmp probes. */
3119 int longjmp_searched = 0;
3120
3121 /* SystemTap probe points for longjmp (if any). These are non-owning
3122 references. */
3123 std::vector<probe *> longjmp_probes;
3124
3125 /* Minimal symbol for "std::terminate()" (if any). */
3126 struct bound_minimal_symbol terminate_msym {};
3127
3128 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3129 struct bound_minimal_symbol exception_msym {};
3130
3131 /* True if we have looked for exception probes. */
3132 int exception_searched = 0;
3133
3134 /* SystemTap probe points for unwinding (if any). These are non-owning
3135 references. */
3136 std::vector<probe *> exception_probes;
3137 };
3138
3139 static const struct objfile_key<breakpoint_objfile_data>
3140 breakpoint_objfile_key;
3141
3142 /* Minimal symbol not found sentinel. */
3143 static struct minimal_symbol msym_not_found;
3144
3145 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3146
3147 static int
3148 msym_not_found_p (const struct minimal_symbol *msym)
3149 {
3150 return msym == &msym_not_found;
3151 }
3152
3153 /* Return per-objfile data needed by breakpoint.c.
3154 Allocate the data if necessary. */
3155
3156 static struct breakpoint_objfile_data *
3157 get_breakpoint_objfile_data (struct objfile *objfile)
3158 {
3159 struct breakpoint_objfile_data *bp_objfile_data;
3160
3161 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3162 if (bp_objfile_data == NULL)
3163 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3164 return bp_objfile_data;
3165 }
3166
3167 static void
3168 create_overlay_event_breakpoint (void)
3169 {
3170 const char *const func_name = "_ovly_debug_event";
3171
3172 for (objfile *objfile : current_program_space->objfiles ())
3173 {
3174 struct breakpoint *b;
3175 struct breakpoint_objfile_data *bp_objfile_data;
3176 CORE_ADDR addr;
3177 struct explicit_location explicit_loc;
3178
3179 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3180
3181 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3182 continue;
3183
3184 if (bp_objfile_data->overlay_msym.minsym == NULL)
3185 {
3186 struct bound_minimal_symbol m;
3187
3188 m = lookup_minimal_symbol_text (func_name, objfile);
3189 if (m.minsym == NULL)
3190 {
3191 /* Avoid future lookups in this objfile. */
3192 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3193 continue;
3194 }
3195 bp_objfile_data->overlay_msym = m;
3196 }
3197
3198 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3199 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3200 bp_overlay_event,
3201 &internal_breakpoint_ops);
3202 initialize_explicit_location (&explicit_loc);
3203 explicit_loc.function_name = ASTRDUP (func_name);
3204 b->location = new_explicit_location (&explicit_loc);
3205
3206 if (overlay_debugging == ovly_auto)
3207 {
3208 b->enable_state = bp_enabled;
3209 overlay_events_enabled = 1;
3210 }
3211 else
3212 {
3213 b->enable_state = bp_disabled;
3214 overlay_events_enabled = 0;
3215 }
3216 }
3217 }
3218
3219 static void
3220 create_longjmp_master_breakpoint (void)
3221 {
3222 struct program_space *pspace;
3223
3224 scoped_restore_current_program_space restore_pspace;
3225
3226 ALL_PSPACES (pspace)
3227 {
3228 set_current_program_space (pspace);
3229
3230 for (objfile *objfile : current_program_space->objfiles ())
3231 {
3232 int i;
3233 struct gdbarch *gdbarch;
3234 struct breakpoint_objfile_data *bp_objfile_data;
3235
3236 gdbarch = get_objfile_arch (objfile);
3237
3238 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3239
3240 if (!bp_objfile_data->longjmp_searched)
3241 {
3242 std::vector<probe *> ret
3243 = find_probes_in_objfile (objfile, "libc", "longjmp");
3244
3245 if (!ret.empty ())
3246 {
3247 /* We are only interested in checking one element. */
3248 probe *p = ret[0];
3249
3250 if (!p->can_evaluate_arguments ())
3251 {
3252 /* We cannot use the probe interface here, because it does
3253 not know how to evaluate arguments. */
3254 ret.clear ();
3255 }
3256 }
3257 bp_objfile_data->longjmp_probes = ret;
3258 bp_objfile_data->longjmp_searched = 1;
3259 }
3260
3261 if (!bp_objfile_data->longjmp_probes.empty ())
3262 {
3263 for (probe *p : bp_objfile_data->longjmp_probes)
3264 {
3265 struct breakpoint *b;
3266
3267 b = create_internal_breakpoint (gdbarch,
3268 p->get_relocated_address (objfile),
3269 bp_longjmp_master,
3270 &internal_breakpoint_ops);
3271 b->location = new_probe_location ("-probe-stap libc:longjmp");
3272 b->enable_state = bp_disabled;
3273 }
3274
3275 continue;
3276 }
3277
3278 if (!gdbarch_get_longjmp_target_p (gdbarch))
3279 continue;
3280
3281 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3282 {
3283 struct breakpoint *b;
3284 const char *func_name;
3285 CORE_ADDR addr;
3286 struct explicit_location explicit_loc;
3287
3288 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3289 continue;
3290
3291 func_name = longjmp_names[i];
3292 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3293 {
3294 struct bound_minimal_symbol m;
3295
3296 m = lookup_minimal_symbol_text (func_name, objfile);
3297 if (m.minsym == NULL)
3298 {
3299 /* Prevent future lookups in this objfile. */
3300 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3301 continue;
3302 }
3303 bp_objfile_data->longjmp_msym[i] = m;
3304 }
3305
3306 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3307 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3308 &internal_breakpoint_ops);
3309 initialize_explicit_location (&explicit_loc);
3310 explicit_loc.function_name = ASTRDUP (func_name);
3311 b->location = new_explicit_location (&explicit_loc);
3312 b->enable_state = bp_disabled;
3313 }
3314 }
3315 }
3316 }
3317
3318 /* Create a master std::terminate breakpoint. */
3319 static void
3320 create_std_terminate_master_breakpoint (void)
3321 {
3322 struct program_space *pspace;
3323 const char *const func_name = "std::terminate()";
3324
3325 scoped_restore_current_program_space restore_pspace;
3326
3327 ALL_PSPACES (pspace)
3328 {
3329 CORE_ADDR addr;
3330
3331 set_current_program_space (pspace);
3332
3333 for (objfile *objfile : current_program_space->objfiles ())
3334 {
3335 struct breakpoint *b;
3336 struct breakpoint_objfile_data *bp_objfile_data;
3337 struct explicit_location explicit_loc;
3338
3339 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3340
3341 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3342 continue;
3343
3344 if (bp_objfile_data->terminate_msym.minsym == NULL)
3345 {
3346 struct bound_minimal_symbol m;
3347
3348 m = lookup_minimal_symbol (func_name, NULL, objfile);
3349 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3350 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3351 {
3352 /* Prevent future lookups in this objfile. */
3353 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3354 continue;
3355 }
3356 bp_objfile_data->terminate_msym = m;
3357 }
3358
3359 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3360 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3361 bp_std_terminate_master,
3362 &internal_breakpoint_ops);
3363 initialize_explicit_location (&explicit_loc);
3364 explicit_loc.function_name = ASTRDUP (func_name);
3365 b->location = new_explicit_location (&explicit_loc);
3366 b->enable_state = bp_disabled;
3367 }
3368 }
3369 }
3370
3371 /* Install a master breakpoint on the unwinder's debug hook. */
3372
3373 static void
3374 create_exception_master_breakpoint (void)
3375 {
3376 const char *const func_name = "_Unwind_DebugHook";
3377
3378 for (objfile *objfile : current_program_space->objfiles ())
3379 {
3380 struct breakpoint *b;
3381 struct gdbarch *gdbarch;
3382 struct breakpoint_objfile_data *bp_objfile_data;
3383 CORE_ADDR addr;
3384 struct explicit_location explicit_loc;
3385
3386 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3387
3388 /* We prefer the SystemTap probe point if it exists. */
3389 if (!bp_objfile_data->exception_searched)
3390 {
3391 std::vector<probe *> ret
3392 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3393
3394 if (!ret.empty ())
3395 {
3396 /* We are only interested in checking one element. */
3397 probe *p = ret[0];
3398
3399 if (!p->can_evaluate_arguments ())
3400 {
3401 /* We cannot use the probe interface here, because it does
3402 not know how to evaluate arguments. */
3403 ret.clear ();
3404 }
3405 }
3406 bp_objfile_data->exception_probes = ret;
3407 bp_objfile_data->exception_searched = 1;
3408 }
3409
3410 if (!bp_objfile_data->exception_probes.empty ())
3411 {
3412 gdbarch = get_objfile_arch (objfile);
3413
3414 for (probe *p : bp_objfile_data->exception_probes)
3415 {
3416 b = create_internal_breakpoint (gdbarch,
3417 p->get_relocated_address (objfile),
3418 bp_exception_master,
3419 &internal_breakpoint_ops);
3420 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3421 b->enable_state = bp_disabled;
3422 }
3423
3424 continue;
3425 }
3426
3427 /* Otherwise, try the hook function. */
3428
3429 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3430 continue;
3431
3432 gdbarch = get_objfile_arch (objfile);
3433
3434 if (bp_objfile_data->exception_msym.minsym == NULL)
3435 {
3436 struct bound_minimal_symbol debug_hook;
3437
3438 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3439 if (debug_hook.minsym == NULL)
3440 {
3441 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3442 continue;
3443 }
3444
3445 bp_objfile_data->exception_msym = debug_hook;
3446 }
3447
3448 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3449 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3450 current_top_target ());
3451 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3452 &internal_breakpoint_ops);
3453 initialize_explicit_location (&explicit_loc);
3454 explicit_loc.function_name = ASTRDUP (func_name);
3455 b->location = new_explicit_location (&explicit_loc);
3456 b->enable_state = bp_disabled;
3457 }
3458 }
3459
3460 /* Does B have a location spec? */
3461
3462 static int
3463 breakpoint_event_location_empty_p (const struct breakpoint *b)
3464 {
3465 return b->location != NULL && event_location_empty_p (b->location.get ());
3466 }
3467
3468 void
3469 update_breakpoints_after_exec (void)
3470 {
3471 struct breakpoint *b, *b_tmp;
3472 struct bp_location *bploc, **bplocp_tmp;
3473
3474 /* We're about to delete breakpoints from GDB's lists. If the
3475 INSERTED flag is true, GDB will try to lift the breakpoints by
3476 writing the breakpoints' "shadow contents" back into memory. The
3477 "shadow contents" are NOT valid after an exec, so GDB should not
3478 do that. Instead, the target is responsible from marking
3479 breakpoints out as soon as it detects an exec. We don't do that
3480 here instead, because there may be other attempts to delete
3481 breakpoints after detecting an exec and before reaching here. */
3482 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3483 if (bploc->pspace == current_program_space)
3484 gdb_assert (!bploc->inserted);
3485
3486 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3487 {
3488 if (b->pspace != current_program_space)
3489 continue;
3490
3491 /* Solib breakpoints must be explicitly reset after an exec(). */
3492 if (b->type == bp_shlib_event)
3493 {
3494 delete_breakpoint (b);
3495 continue;
3496 }
3497
3498 /* JIT breakpoints must be explicitly reset after an exec(). */
3499 if (b->type == bp_jit_event)
3500 {
3501 delete_breakpoint (b);
3502 continue;
3503 }
3504
3505 /* Thread event breakpoints must be set anew after an exec(),
3506 as must overlay event and longjmp master breakpoints. */
3507 if (b->type == bp_thread_event || b->type == bp_overlay_event
3508 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3509 || b->type == bp_exception_master)
3510 {
3511 delete_breakpoint (b);
3512 continue;
3513 }
3514
3515 /* Step-resume breakpoints are meaningless after an exec(). */
3516 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3517 {
3518 delete_breakpoint (b);
3519 continue;
3520 }
3521
3522 /* Just like single-step breakpoints. */
3523 if (b->type == bp_single_step)
3524 {
3525 delete_breakpoint (b);
3526 continue;
3527 }
3528
3529 /* Longjmp and longjmp-resume breakpoints are also meaningless
3530 after an exec. */
3531 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3532 || b->type == bp_longjmp_call_dummy
3533 || b->type == bp_exception || b->type == bp_exception_resume)
3534 {
3535 delete_breakpoint (b);
3536 continue;
3537 }
3538
3539 if (b->type == bp_catchpoint)
3540 {
3541 /* For now, none of the bp_catchpoint breakpoints need to
3542 do anything at this point. In the future, if some of
3543 the catchpoints need to something, we will need to add
3544 a new method, and call this method from here. */
3545 continue;
3546 }
3547
3548 /* bp_finish is a special case. The only way we ought to be able
3549 to see one of these when an exec() has happened, is if the user
3550 caught a vfork, and then said "finish". Ordinarily a finish just
3551 carries them to the call-site of the current callee, by setting
3552 a temporary bp there and resuming. But in this case, the finish
3553 will carry them entirely through the vfork & exec.
3554
3555 We don't want to allow a bp_finish to remain inserted now. But
3556 we can't safely delete it, 'cause finish_command has a handle to
3557 the bp on a bpstat, and will later want to delete it. There's a
3558 chance (and I've seen it happen) that if we delete the bp_finish
3559 here, that its storage will get reused by the time finish_command
3560 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3561 We really must allow finish_command to delete a bp_finish.
3562
3563 In the absence of a general solution for the "how do we know
3564 it's safe to delete something others may have handles to?"
3565 problem, what we'll do here is just uninsert the bp_finish, and
3566 let finish_command delete it.
3567
3568 (We know the bp_finish is "doomed" in the sense that it's
3569 momentary, and will be deleted as soon as finish_command sees
3570 the inferior stopped. So it doesn't matter that the bp's
3571 address is probably bogus in the new a.out, unlike e.g., the
3572 solib breakpoints.) */
3573
3574 if (b->type == bp_finish)
3575 {
3576 continue;
3577 }
3578
3579 /* Without a symbolic address, we have little hope of the
3580 pre-exec() address meaning the same thing in the post-exec()
3581 a.out. */
3582 if (breakpoint_event_location_empty_p (b))
3583 {
3584 delete_breakpoint (b);
3585 continue;
3586 }
3587 }
3588 }
3589
3590 int
3591 detach_breakpoints (ptid_t ptid)
3592 {
3593 struct bp_location *bl, **blp_tmp;
3594 int val = 0;
3595 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3596 struct inferior *inf = current_inferior ();
3597
3598 if (ptid.pid () == inferior_ptid.pid ())
3599 error (_("Cannot detach breakpoints of inferior_ptid"));
3600
3601 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3602 inferior_ptid = ptid;
3603 ALL_BP_LOCATIONS (bl, blp_tmp)
3604 {
3605 if (bl->pspace != inf->pspace)
3606 continue;
3607
3608 /* This function must physically remove breakpoints locations
3609 from the specified ptid, without modifying the breakpoint
3610 package's state. Locations of type bp_loc_other are only
3611 maintained at GDB side. So, there is no need to remove
3612 these bp_loc_other locations. Moreover, removing these
3613 would modify the breakpoint package's state. */
3614 if (bl->loc_type == bp_loc_other)
3615 continue;
3616
3617 if (bl->inserted)
3618 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3619 }
3620
3621 return val;
3622 }
3623
3624 /* Remove the breakpoint location BL from the current address space.
3625 Note that this is used to detach breakpoints from a child fork.
3626 When we get here, the child isn't in the inferior list, and neither
3627 do we have objects to represent its address space --- we should
3628 *not* look at bl->pspace->aspace here. */
3629
3630 static int
3631 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3632 {
3633 int val;
3634
3635 /* BL is never in moribund_locations by our callers. */
3636 gdb_assert (bl->owner != NULL);
3637
3638 /* The type of none suggests that owner is actually deleted.
3639 This should not ever happen. */
3640 gdb_assert (bl->owner->type != bp_none);
3641
3642 if (bl->loc_type == bp_loc_software_breakpoint
3643 || bl->loc_type == bp_loc_hardware_breakpoint)
3644 {
3645 /* "Normal" instruction breakpoint: either the standard
3646 trap-instruction bp (bp_breakpoint), or a
3647 bp_hardware_breakpoint. */
3648
3649 /* First check to see if we have to handle an overlay. */
3650 if (overlay_debugging == ovly_off
3651 || bl->section == NULL
3652 || !(section_is_overlay (bl->section)))
3653 {
3654 /* No overlay handling: just remove the breakpoint. */
3655
3656 /* If we're trying to uninsert a memory breakpoint that we
3657 know is set in a dynamic object that is marked
3658 shlib_disabled, then either the dynamic object was
3659 removed with "remove-symbol-file" or with
3660 "nosharedlibrary". In the former case, we don't know
3661 whether another dynamic object might have loaded over the
3662 breakpoint's address -- the user might well let us know
3663 about it next with add-symbol-file (the whole point of
3664 add-symbol-file is letting the user manually maintain a
3665 list of dynamically loaded objects). If we have the
3666 breakpoint's shadow memory, that is, this is a software
3667 breakpoint managed by GDB, check whether the breakpoint
3668 is still inserted in memory, to avoid overwriting wrong
3669 code with stale saved shadow contents. Note that HW
3670 breakpoints don't have shadow memory, as they're
3671 implemented using a mechanism that is not dependent on
3672 being able to modify the target's memory, and as such
3673 they should always be removed. */
3674 if (bl->shlib_disabled
3675 && bl->target_info.shadow_len != 0
3676 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3677 val = 0;
3678 else
3679 val = bl->owner->ops->remove_location (bl, reason);
3680 }
3681 else
3682 {
3683 /* This breakpoint is in an overlay section.
3684 Did we set a breakpoint at the LMA? */
3685 if (!overlay_events_enabled)
3686 {
3687 /* Yes -- overlay event support is not active, so we
3688 should have set a breakpoint at the LMA. Remove it.
3689 */
3690 /* Ignore any failures: if the LMA is in ROM, we will
3691 have already warned when we failed to insert it. */
3692 if (bl->loc_type == bp_loc_hardware_breakpoint)
3693 target_remove_hw_breakpoint (bl->gdbarch,
3694 &bl->overlay_target_info);
3695 else
3696 target_remove_breakpoint (bl->gdbarch,
3697 &bl->overlay_target_info,
3698 reason);
3699 }
3700 /* Did we set a breakpoint at the VMA?
3701 If so, we will have marked the breakpoint 'inserted'. */
3702 if (bl->inserted)
3703 {
3704 /* Yes -- remove it. Previously we did not bother to
3705 remove the breakpoint if the section had been
3706 unmapped, but let's not rely on that being safe. We
3707 don't know what the overlay manager might do. */
3708
3709 /* However, we should remove *software* breakpoints only
3710 if the section is still mapped, or else we overwrite
3711 wrong code with the saved shadow contents. */
3712 if (bl->loc_type == bp_loc_hardware_breakpoint
3713 || section_is_mapped (bl->section))
3714 val = bl->owner->ops->remove_location (bl, reason);
3715 else
3716 val = 0;
3717 }
3718 else
3719 {
3720 /* No -- not inserted, so no need to remove. No error. */
3721 val = 0;
3722 }
3723 }
3724
3725 /* In some cases, we might not be able to remove a breakpoint in
3726 a shared library that has already been removed, but we have
3727 not yet processed the shlib unload event. Similarly for an
3728 unloaded add-symbol-file object - the user might not yet have
3729 had the chance to remove-symbol-file it. shlib_disabled will
3730 be set if the library/object has already been removed, but
3731 the breakpoint hasn't been uninserted yet, e.g., after
3732 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3733 always-inserted mode. */
3734 if (val
3735 && (bl->loc_type == bp_loc_software_breakpoint
3736 && (bl->shlib_disabled
3737 || solib_name_from_address (bl->pspace, bl->address)
3738 || shared_objfile_contains_address_p (bl->pspace,
3739 bl->address))))
3740 val = 0;
3741
3742 if (val)
3743 return val;
3744 bl->inserted = (reason == DETACH_BREAKPOINT);
3745 }
3746 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3747 {
3748 gdb_assert (bl->owner->ops != NULL
3749 && bl->owner->ops->remove_location != NULL);
3750
3751 bl->inserted = (reason == DETACH_BREAKPOINT);
3752 bl->owner->ops->remove_location (bl, reason);
3753
3754 /* Failure to remove any of the hardware watchpoints comes here. */
3755 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3756 warning (_("Could not remove hardware watchpoint %d."),
3757 bl->owner->number);
3758 }
3759 else if (bl->owner->type == bp_catchpoint
3760 && breakpoint_enabled (bl->owner)
3761 && !bl->duplicate)
3762 {
3763 gdb_assert (bl->owner->ops != NULL
3764 && bl->owner->ops->remove_location != NULL);
3765
3766 val = bl->owner->ops->remove_location (bl, reason);
3767 if (val)
3768 return val;
3769
3770 bl->inserted = (reason == DETACH_BREAKPOINT);
3771 }
3772
3773 return 0;
3774 }
3775
3776 static int
3777 remove_breakpoint (struct bp_location *bl)
3778 {
3779 /* BL is never in moribund_locations by our callers. */
3780 gdb_assert (bl->owner != NULL);
3781
3782 /* The type of none suggests that owner is actually deleted.
3783 This should not ever happen. */
3784 gdb_assert (bl->owner->type != bp_none);
3785
3786 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3787
3788 switch_to_program_space_and_thread (bl->pspace);
3789
3790 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3791 }
3792
3793 /* Clear the "inserted" flag in all breakpoints. */
3794
3795 void
3796 mark_breakpoints_out (void)
3797 {
3798 struct bp_location *bl, **blp_tmp;
3799
3800 ALL_BP_LOCATIONS (bl, blp_tmp)
3801 if (bl->pspace == current_program_space)
3802 bl->inserted = 0;
3803 }
3804
3805 /* Clear the "inserted" flag in all breakpoints and delete any
3806 breakpoints which should go away between runs of the program.
3807
3808 Plus other such housekeeping that has to be done for breakpoints
3809 between runs.
3810
3811 Note: this function gets called at the end of a run (by
3812 generic_mourn_inferior) and when a run begins (by
3813 init_wait_for_inferior). */
3814
3815
3816
3817 void
3818 breakpoint_init_inferior (enum inf_context context)
3819 {
3820 struct breakpoint *b, *b_tmp;
3821 struct program_space *pspace = current_program_space;
3822
3823 /* If breakpoint locations are shared across processes, then there's
3824 nothing to do. */
3825 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3826 return;
3827
3828 mark_breakpoints_out ();
3829
3830 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3831 {
3832 if (b->loc && b->loc->pspace != pspace)
3833 continue;
3834
3835 switch (b->type)
3836 {
3837 case bp_call_dummy:
3838 case bp_longjmp_call_dummy:
3839
3840 /* If the call dummy breakpoint is at the entry point it will
3841 cause problems when the inferior is rerun, so we better get
3842 rid of it. */
3843
3844 case bp_watchpoint_scope:
3845
3846 /* Also get rid of scope breakpoints. */
3847
3848 case bp_shlib_event:
3849
3850 /* Also remove solib event breakpoints. Their addresses may
3851 have changed since the last time we ran the program.
3852 Actually we may now be debugging against different target;
3853 and so the solib backend that installed this breakpoint may
3854 not be used in by the target. E.g.,
3855
3856 (gdb) file prog-linux
3857 (gdb) run # native linux target
3858 ...
3859 (gdb) kill
3860 (gdb) file prog-win.exe
3861 (gdb) tar rem :9999 # remote Windows gdbserver.
3862 */
3863
3864 case bp_step_resume:
3865
3866 /* Also remove step-resume breakpoints. */
3867
3868 case bp_single_step:
3869
3870 /* Also remove single-step breakpoints. */
3871
3872 delete_breakpoint (b);
3873 break;
3874
3875 case bp_watchpoint:
3876 case bp_hardware_watchpoint:
3877 case bp_read_watchpoint:
3878 case bp_access_watchpoint:
3879 {
3880 struct watchpoint *w = (struct watchpoint *) b;
3881
3882 /* Likewise for watchpoints on local expressions. */
3883 if (w->exp_valid_block != NULL)
3884 delete_breakpoint (b);
3885 else
3886 {
3887 /* Get rid of existing locations, which are no longer
3888 valid. New ones will be created in
3889 update_watchpoint, when the inferior is restarted.
3890 The next update_global_location_list call will
3891 garbage collect them. */
3892 b->loc = NULL;
3893
3894 if (context == inf_starting)
3895 {
3896 /* Reset val field to force reread of starting value in
3897 insert_breakpoints. */
3898 w->val.reset (nullptr);
3899 w->val_valid = false;
3900 }
3901 }
3902 }
3903 break;
3904 default:
3905 break;
3906 }
3907 }
3908
3909 /* Get rid of the moribund locations. */
3910 for (bp_location *bl : moribund_locations)
3911 decref_bp_location (&bl);
3912 moribund_locations.clear ();
3913 }
3914
3915 /* These functions concern about actual breakpoints inserted in the
3916 target --- to e.g. check if we need to do decr_pc adjustment or if
3917 we need to hop over the bkpt --- so we check for address space
3918 match, not program space. */
3919
3920 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3921 exists at PC. It returns ordinary_breakpoint_here if it's an
3922 ordinary breakpoint, or permanent_breakpoint_here if it's a
3923 permanent breakpoint.
3924 - When continuing from a location with an ordinary breakpoint, we
3925 actually single step once before calling insert_breakpoints.
3926 - When continuing from a location with a permanent breakpoint, we
3927 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3928 the target, to advance the PC past the breakpoint. */
3929
3930 enum breakpoint_here
3931 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3932 {
3933 struct bp_location *bl, **blp_tmp;
3934 int any_breakpoint_here = 0;
3935
3936 ALL_BP_LOCATIONS (bl, blp_tmp)
3937 {
3938 if (bl->loc_type != bp_loc_software_breakpoint
3939 && bl->loc_type != bp_loc_hardware_breakpoint)
3940 continue;
3941
3942 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3943 if ((breakpoint_enabled (bl->owner)
3944 || bl->permanent)
3945 && breakpoint_location_address_match (bl, aspace, pc))
3946 {
3947 if (overlay_debugging
3948 && section_is_overlay (bl->section)
3949 && !section_is_mapped (bl->section))
3950 continue; /* unmapped overlay -- can't be a match */
3951 else if (bl->permanent)
3952 return permanent_breakpoint_here;
3953 else
3954 any_breakpoint_here = 1;
3955 }
3956 }
3957
3958 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
3959 }
3960
3961 /* See breakpoint.h. */
3962
3963 int
3964 breakpoint_in_range_p (const address_space *aspace,
3965 CORE_ADDR addr, ULONGEST len)
3966 {
3967 struct bp_location *bl, **blp_tmp;
3968
3969 ALL_BP_LOCATIONS (bl, blp_tmp)
3970 {
3971 if (bl->loc_type != bp_loc_software_breakpoint
3972 && bl->loc_type != bp_loc_hardware_breakpoint)
3973 continue;
3974
3975 if ((breakpoint_enabled (bl->owner)
3976 || bl->permanent)
3977 && breakpoint_location_address_range_overlap (bl, aspace,
3978 addr, len))
3979 {
3980 if (overlay_debugging
3981 && section_is_overlay (bl->section)
3982 && !section_is_mapped (bl->section))
3983 {
3984 /* Unmapped overlay -- can't be a match. */
3985 continue;
3986 }
3987
3988 return 1;
3989 }
3990 }
3991
3992 return 0;
3993 }
3994
3995 /* Return true if there's a moribund breakpoint at PC. */
3996
3997 int
3998 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3999 {
4000 for (bp_location *loc : moribund_locations)
4001 if (breakpoint_location_address_match (loc, aspace, pc))
4002 return 1;
4003
4004 return 0;
4005 }
4006
4007 /* Returns non-zero iff BL is inserted at PC, in address space
4008 ASPACE. */
4009
4010 static int
4011 bp_location_inserted_here_p (struct bp_location *bl,
4012 const address_space *aspace, CORE_ADDR pc)
4013 {
4014 if (bl->inserted
4015 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4016 aspace, pc))
4017 {
4018 if (overlay_debugging
4019 && section_is_overlay (bl->section)
4020 && !section_is_mapped (bl->section))
4021 return 0; /* unmapped overlay -- can't be a match */
4022 else
4023 return 1;
4024 }
4025 return 0;
4026 }
4027
4028 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4029
4030 int
4031 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4032 {
4033 struct bp_location **blp, **blp_tmp = NULL;
4034
4035 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4036 {
4037 struct bp_location *bl = *blp;
4038
4039 if (bl->loc_type != bp_loc_software_breakpoint
4040 && bl->loc_type != bp_loc_hardware_breakpoint)
4041 continue;
4042
4043 if (bp_location_inserted_here_p (bl, aspace, pc))
4044 return 1;
4045 }
4046 return 0;
4047 }
4048
4049 /* This function returns non-zero iff there is a software breakpoint
4050 inserted at PC. */
4051
4052 int
4053 software_breakpoint_inserted_here_p (const address_space *aspace,
4054 CORE_ADDR pc)
4055 {
4056 struct bp_location **blp, **blp_tmp = NULL;
4057
4058 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4059 {
4060 struct bp_location *bl = *blp;
4061
4062 if (bl->loc_type != bp_loc_software_breakpoint)
4063 continue;
4064
4065 if (bp_location_inserted_here_p (bl, aspace, pc))
4066 return 1;
4067 }
4068
4069 return 0;
4070 }
4071
4072 /* See breakpoint.h. */
4073
4074 int
4075 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4076 CORE_ADDR pc)
4077 {
4078 struct bp_location **blp, **blp_tmp = NULL;
4079
4080 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4081 {
4082 struct bp_location *bl = *blp;
4083
4084 if (bl->loc_type != bp_loc_hardware_breakpoint)
4085 continue;
4086
4087 if (bp_location_inserted_here_p (bl, aspace, pc))
4088 return 1;
4089 }
4090
4091 return 0;
4092 }
4093
4094 int
4095 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4096 CORE_ADDR addr, ULONGEST len)
4097 {
4098 struct breakpoint *bpt;
4099
4100 ALL_BREAKPOINTS (bpt)
4101 {
4102 struct bp_location *loc;
4103
4104 if (bpt->type != bp_hardware_watchpoint
4105 && bpt->type != bp_access_watchpoint)
4106 continue;
4107
4108 if (!breakpoint_enabled (bpt))
4109 continue;
4110
4111 for (loc = bpt->loc; loc; loc = loc->next)
4112 if (loc->pspace->aspace == aspace && loc->inserted)
4113 {
4114 CORE_ADDR l, h;
4115
4116 /* Check for intersection. */
4117 l = std::max<CORE_ADDR> (loc->address, addr);
4118 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4119 if (l < h)
4120 return 1;
4121 }
4122 }
4123 return 0;
4124 }
4125
4126 /* See breakpoint.h. */
4127
4128 bool
4129 is_catchpoint (struct breakpoint *b)
4130 {
4131 return (b->type == bp_catchpoint);
4132 }
4133
4134 /* Frees any storage that is part of a bpstat. Does not walk the
4135 'next' chain. */
4136
4137 bpstats::~bpstats ()
4138 {
4139 if (bp_location_at != NULL)
4140 decref_bp_location (&bp_location_at);
4141 }
4142
4143 /* Clear a bpstat so that it says we are not at any breakpoint.
4144 Also free any storage that is part of a bpstat. */
4145
4146 void
4147 bpstat_clear (bpstat *bsp)
4148 {
4149 bpstat p;
4150 bpstat q;
4151
4152 if (bsp == 0)
4153 return;
4154 p = *bsp;
4155 while (p != NULL)
4156 {
4157 q = p->next;
4158 delete p;
4159 p = q;
4160 }
4161 *bsp = NULL;
4162 }
4163
4164 bpstats::bpstats (const bpstats &other)
4165 : next (NULL),
4166 bp_location_at (other.bp_location_at),
4167 breakpoint_at (other.breakpoint_at),
4168 commands (other.commands),
4169 print (other.print),
4170 stop (other.stop),
4171 print_it (other.print_it)
4172 {
4173 if (other.old_val != NULL)
4174 old_val = release_value (value_copy (other.old_val.get ()));
4175 incref_bp_location (bp_location_at);
4176 }
4177
4178 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4179 is part of the bpstat is copied as well. */
4180
4181 bpstat
4182 bpstat_copy (bpstat bs)
4183 {
4184 bpstat p = NULL;
4185 bpstat tmp;
4186 bpstat retval = NULL;
4187
4188 if (bs == NULL)
4189 return bs;
4190
4191 for (; bs != NULL; bs = bs->next)
4192 {
4193 tmp = new bpstats (*bs);
4194
4195 if (p == NULL)
4196 /* This is the first thing in the chain. */
4197 retval = tmp;
4198 else
4199 p->next = tmp;
4200 p = tmp;
4201 }
4202 p->next = NULL;
4203 return retval;
4204 }
4205
4206 /* Find the bpstat associated with this breakpoint. */
4207
4208 bpstat
4209 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4210 {
4211 if (bsp == NULL)
4212 return NULL;
4213
4214 for (; bsp != NULL; bsp = bsp->next)
4215 {
4216 if (bsp->breakpoint_at == breakpoint)
4217 return bsp;
4218 }
4219 return NULL;
4220 }
4221
4222 /* See breakpoint.h. */
4223
4224 bool
4225 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4226 {
4227 for (; bsp != NULL; bsp = bsp->next)
4228 {
4229 if (bsp->breakpoint_at == NULL)
4230 {
4231 /* A moribund location can never explain a signal other than
4232 GDB_SIGNAL_TRAP. */
4233 if (sig == GDB_SIGNAL_TRAP)
4234 return true;
4235 }
4236 else
4237 {
4238 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4239 sig))
4240 return true;
4241 }
4242 }
4243
4244 return false;
4245 }
4246
4247 /* Put in *NUM the breakpoint number of the first breakpoint we are
4248 stopped at. *BSP upon return is a bpstat which points to the
4249 remaining breakpoints stopped at (but which is not guaranteed to be
4250 good for anything but further calls to bpstat_num).
4251
4252 Return 0 if passed a bpstat which does not indicate any breakpoints.
4253 Return -1 if stopped at a breakpoint that has been deleted since
4254 we set it.
4255 Return 1 otherwise. */
4256
4257 int
4258 bpstat_num (bpstat *bsp, int *num)
4259 {
4260 struct breakpoint *b;
4261
4262 if ((*bsp) == NULL)
4263 return 0; /* No more breakpoint values */
4264
4265 /* We assume we'll never have several bpstats that correspond to a
4266 single breakpoint -- otherwise, this function might return the
4267 same number more than once and this will look ugly. */
4268 b = (*bsp)->breakpoint_at;
4269 *bsp = (*bsp)->next;
4270 if (b == NULL)
4271 return -1; /* breakpoint that's been deleted since */
4272
4273 *num = b->number; /* We have its number */
4274 return 1;
4275 }
4276
4277 /* See breakpoint.h. */
4278
4279 void
4280 bpstat_clear_actions (void)
4281 {
4282 bpstat bs;
4283
4284 if (inferior_ptid == null_ptid)
4285 return;
4286
4287 thread_info *tp = inferior_thread ();
4288 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4289 {
4290 bs->commands = NULL;
4291 bs->old_val.reset (nullptr);
4292 }
4293 }
4294
4295 /* Called when a command is about to proceed the inferior. */
4296
4297 static void
4298 breakpoint_about_to_proceed (void)
4299 {
4300 if (inferior_ptid != null_ptid)
4301 {
4302 struct thread_info *tp = inferior_thread ();
4303
4304 /* Allow inferior function calls in breakpoint commands to not
4305 interrupt the command list. When the call finishes
4306 successfully, the inferior will be standing at the same
4307 breakpoint as if nothing happened. */
4308 if (tp->control.in_infcall)
4309 return;
4310 }
4311
4312 breakpoint_proceeded = 1;
4313 }
4314
4315 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4316 or its equivalent. */
4317
4318 static int
4319 command_line_is_silent (struct command_line *cmd)
4320 {
4321 return cmd && (strcmp ("silent", cmd->line) == 0);
4322 }
4323
4324 /* Execute all the commands associated with all the breakpoints at
4325 this location. Any of these commands could cause the process to
4326 proceed beyond this point, etc. We look out for such changes by
4327 checking the global "breakpoint_proceeded" after each command.
4328
4329 Returns true if a breakpoint command resumed the inferior. In that
4330 case, it is the caller's responsibility to recall it again with the
4331 bpstat of the current thread. */
4332
4333 static int
4334 bpstat_do_actions_1 (bpstat *bsp)
4335 {
4336 bpstat bs;
4337 int again = 0;
4338
4339 /* Avoid endless recursion if a `source' command is contained
4340 in bs->commands. */
4341 if (executing_breakpoint_commands)
4342 return 0;
4343
4344 scoped_restore save_executing
4345 = make_scoped_restore (&executing_breakpoint_commands, 1);
4346
4347 scoped_restore preventer = prevent_dont_repeat ();
4348
4349 /* This pointer will iterate over the list of bpstat's. */
4350 bs = *bsp;
4351
4352 breakpoint_proceeded = 0;
4353 for (; bs != NULL; bs = bs->next)
4354 {
4355 struct command_line *cmd = NULL;
4356
4357 /* Take ownership of the BSP's command tree, if it has one.
4358
4359 The command tree could legitimately contain commands like
4360 'step' and 'next', which call clear_proceed_status, which
4361 frees stop_bpstat's command tree. To make sure this doesn't
4362 free the tree we're executing out from under us, we need to
4363 take ownership of the tree ourselves. Since a given bpstat's
4364 commands are only executed once, we don't need to copy it; we
4365 can clear the pointer in the bpstat, and make sure we free
4366 the tree when we're done. */
4367 counted_command_line ccmd = bs->commands;
4368 bs->commands = NULL;
4369 if (ccmd != NULL)
4370 cmd = ccmd.get ();
4371 if (command_line_is_silent (cmd))
4372 {
4373 /* The action has been already done by bpstat_stop_status. */
4374 cmd = cmd->next;
4375 }
4376
4377 while (cmd != NULL)
4378 {
4379 execute_control_command (cmd);
4380
4381 if (breakpoint_proceeded)
4382 break;
4383 else
4384 cmd = cmd->next;
4385 }
4386
4387 if (breakpoint_proceeded)
4388 {
4389 if (current_ui->async)
4390 /* If we are in async mode, then the target might be still
4391 running, not stopped at any breakpoint, so nothing for
4392 us to do here -- just return to the event loop. */
4393 ;
4394 else
4395 /* In sync mode, when execute_control_command returns
4396 we're already standing on the next breakpoint.
4397 Breakpoint commands for that stop were not run, since
4398 execute_command does not run breakpoint commands --
4399 only command_line_handler does, but that one is not
4400 involved in execution of breakpoint commands. So, we
4401 can now execute breakpoint commands. It should be
4402 noted that making execute_command do bpstat actions is
4403 not an option -- in this case we'll have recursive
4404 invocation of bpstat for each breakpoint with a
4405 command, and can easily blow up GDB stack. Instead, we
4406 return true, which will trigger the caller to recall us
4407 with the new stop_bpstat. */
4408 again = 1;
4409 break;
4410 }
4411 }
4412 return again;
4413 }
4414
4415 /* Helper for bpstat_do_actions. Get the current thread, if there's
4416 one, is alive and has execution. Return NULL otherwise. */
4417
4418 static thread_info *
4419 get_bpstat_thread ()
4420 {
4421 if (inferior_ptid == null_ptid || !target_has_execution)
4422 return NULL;
4423
4424 thread_info *tp = inferior_thread ();
4425 if (tp->state == THREAD_EXITED || tp->executing)
4426 return NULL;
4427 return tp;
4428 }
4429
4430 void
4431 bpstat_do_actions (void)
4432 {
4433 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4434 thread_info *tp;
4435
4436 /* Do any commands attached to breakpoint we are stopped at. */
4437 while ((tp = get_bpstat_thread ()) != NULL)
4438 {
4439 /* Since in sync mode, bpstat_do_actions may resume the
4440 inferior, and only return when it is stopped at the next
4441 breakpoint, we keep doing breakpoint actions until it returns
4442 false to indicate the inferior was not resumed. */
4443 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4444 break;
4445 }
4446
4447 cleanup_if_error.release ();
4448 }
4449
4450 /* Print out the (old or new) value associated with a watchpoint. */
4451
4452 static void
4453 watchpoint_value_print (struct value *val, struct ui_file *stream)
4454 {
4455 if (val == NULL)
4456 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4457 else
4458 {
4459 struct value_print_options opts;
4460 get_user_print_options (&opts);
4461 value_print (val, stream, &opts);
4462 }
4463 }
4464
4465 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4466 debugging multiple threads. */
4467
4468 void
4469 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4470 {
4471 if (uiout->is_mi_like_p ())
4472 return;
4473
4474 uiout->text ("\n");
4475
4476 if (show_thread_that_caused_stop ())
4477 {
4478 const char *name;
4479 struct thread_info *thr = inferior_thread ();
4480
4481 uiout->text ("Thread ");
4482 uiout->field_string ("thread-id", print_thread_id (thr));
4483
4484 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4485 if (name != NULL)
4486 {
4487 uiout->text (" \"");
4488 uiout->field_string ("name", name);
4489 uiout->text ("\"");
4490 }
4491
4492 uiout->text (" hit ");
4493 }
4494 }
4495
4496 /* Generic routine for printing messages indicating why we
4497 stopped. The behavior of this function depends on the value
4498 'print_it' in the bpstat structure. Under some circumstances we
4499 may decide not to print anything here and delegate the task to
4500 normal_stop(). */
4501
4502 static enum print_stop_action
4503 print_bp_stop_message (bpstat bs)
4504 {
4505 switch (bs->print_it)
4506 {
4507 case print_it_noop:
4508 /* Nothing should be printed for this bpstat entry. */
4509 return PRINT_UNKNOWN;
4510 break;
4511
4512 case print_it_done:
4513 /* We still want to print the frame, but we already printed the
4514 relevant messages. */
4515 return PRINT_SRC_AND_LOC;
4516 break;
4517
4518 case print_it_normal:
4519 {
4520 struct breakpoint *b = bs->breakpoint_at;
4521
4522 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4523 which has since been deleted. */
4524 if (b == NULL)
4525 return PRINT_UNKNOWN;
4526
4527 /* Normal case. Call the breakpoint's print_it method. */
4528 return b->ops->print_it (bs);
4529 }
4530 break;
4531
4532 default:
4533 internal_error (__FILE__, __LINE__,
4534 _("print_bp_stop_message: unrecognized enum value"));
4535 break;
4536 }
4537 }
4538
4539 /* A helper function that prints a shared library stopped event. */
4540
4541 static void
4542 print_solib_event (int is_catchpoint)
4543 {
4544 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4545 bool any_added = !current_program_space->added_solibs.empty ();
4546
4547 if (!is_catchpoint)
4548 {
4549 if (any_added || any_deleted)
4550 current_uiout->text (_("Stopped due to shared library event:\n"));
4551 else
4552 current_uiout->text (_("Stopped due to shared library event (no "
4553 "libraries added or removed)\n"));
4554 }
4555
4556 if (current_uiout->is_mi_like_p ())
4557 current_uiout->field_string ("reason",
4558 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4559
4560 if (any_deleted)
4561 {
4562 current_uiout->text (_(" Inferior unloaded "));
4563 ui_out_emit_list list_emitter (current_uiout, "removed");
4564 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4565 {
4566 const std::string &name = current_program_space->deleted_solibs[ix];
4567
4568 if (ix > 0)
4569 current_uiout->text (" ");
4570 current_uiout->field_string ("library", name);
4571 current_uiout->text ("\n");
4572 }
4573 }
4574
4575 if (any_added)
4576 {
4577 current_uiout->text (_(" Inferior loaded "));
4578 ui_out_emit_list list_emitter (current_uiout, "added");
4579 bool first = true;
4580 for (so_list *iter : current_program_space->added_solibs)
4581 {
4582 if (!first)
4583 current_uiout->text (" ");
4584 first = false;
4585 current_uiout->field_string ("library", iter->so_name);
4586 current_uiout->text ("\n");
4587 }
4588 }
4589 }
4590
4591 /* Print a message indicating what happened. This is called from
4592 normal_stop(). The input to this routine is the head of the bpstat
4593 list - a list of the eventpoints that caused this stop. KIND is
4594 the target_waitkind for the stopping event. This
4595 routine calls the generic print routine for printing a message
4596 about reasons for stopping. This will print (for example) the
4597 "Breakpoint n," part of the output. The return value of this
4598 routine is one of:
4599
4600 PRINT_UNKNOWN: Means we printed nothing.
4601 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4602 code to print the location. An example is
4603 "Breakpoint 1, " which should be followed by
4604 the location.
4605 PRINT_SRC_ONLY: Means we printed something, but there is no need
4606 to also print the location part of the message.
4607 An example is the catch/throw messages, which
4608 don't require a location appended to the end.
4609 PRINT_NOTHING: We have done some printing and we don't need any
4610 further info to be printed. */
4611
4612 enum print_stop_action
4613 bpstat_print (bpstat bs, int kind)
4614 {
4615 enum print_stop_action val;
4616
4617 /* Maybe another breakpoint in the chain caused us to stop.
4618 (Currently all watchpoints go on the bpstat whether hit or not.
4619 That probably could (should) be changed, provided care is taken
4620 with respect to bpstat_explains_signal). */
4621 for (; bs; bs = bs->next)
4622 {
4623 val = print_bp_stop_message (bs);
4624 if (val == PRINT_SRC_ONLY
4625 || val == PRINT_SRC_AND_LOC
4626 || val == PRINT_NOTHING)
4627 return val;
4628 }
4629
4630 /* If we had hit a shared library event breakpoint,
4631 print_bp_stop_message would print out this message. If we hit an
4632 OS-level shared library event, do the same thing. */
4633 if (kind == TARGET_WAITKIND_LOADED)
4634 {
4635 print_solib_event (0);
4636 return PRINT_NOTHING;
4637 }
4638
4639 /* We reached the end of the chain, or we got a null BS to start
4640 with and nothing was printed. */
4641 return PRINT_UNKNOWN;
4642 }
4643
4644 /* Evaluate the boolean expression EXP and return the result. */
4645
4646 static bool
4647 breakpoint_cond_eval (expression *exp)
4648 {
4649 struct value *mark = value_mark ();
4650 bool res = value_true (evaluate_expression (exp));
4651
4652 value_free_to_mark (mark);
4653 return res;
4654 }
4655
4656 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4657
4658 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4659 : next (NULL),
4660 bp_location_at (bl),
4661 breakpoint_at (bl->owner),
4662 commands (NULL),
4663 print (0),
4664 stop (0),
4665 print_it (print_it_normal)
4666 {
4667 incref_bp_location (bl);
4668 **bs_link_pointer = this;
4669 *bs_link_pointer = &next;
4670 }
4671
4672 bpstats::bpstats ()
4673 : next (NULL),
4674 bp_location_at (NULL),
4675 breakpoint_at (NULL),
4676 commands (NULL),
4677 print (0),
4678 stop (0),
4679 print_it (print_it_normal)
4680 {
4681 }
4682 \f
4683 /* The target has stopped with waitstatus WS. Check if any hardware
4684 watchpoints have triggered, according to the target. */
4685
4686 int
4687 watchpoints_triggered (struct target_waitstatus *ws)
4688 {
4689 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4690 CORE_ADDR addr;
4691 struct breakpoint *b;
4692
4693 if (!stopped_by_watchpoint)
4694 {
4695 /* We were not stopped by a watchpoint. Mark all watchpoints
4696 as not triggered. */
4697 ALL_BREAKPOINTS (b)
4698 if (is_hardware_watchpoint (b))
4699 {
4700 struct watchpoint *w = (struct watchpoint *) b;
4701
4702 w->watchpoint_triggered = watch_triggered_no;
4703 }
4704
4705 return 0;
4706 }
4707
4708 if (!target_stopped_data_address (current_top_target (), &addr))
4709 {
4710 /* We were stopped by a watchpoint, but we don't know where.
4711 Mark all watchpoints as unknown. */
4712 ALL_BREAKPOINTS (b)
4713 if (is_hardware_watchpoint (b))
4714 {
4715 struct watchpoint *w = (struct watchpoint *) b;
4716
4717 w->watchpoint_triggered = watch_triggered_unknown;
4718 }
4719
4720 return 1;
4721 }
4722
4723 /* The target could report the data address. Mark watchpoints
4724 affected by this data address as triggered, and all others as not
4725 triggered. */
4726
4727 ALL_BREAKPOINTS (b)
4728 if (is_hardware_watchpoint (b))
4729 {
4730 struct watchpoint *w = (struct watchpoint *) b;
4731 struct bp_location *loc;
4732
4733 w->watchpoint_triggered = watch_triggered_no;
4734 for (loc = b->loc; loc; loc = loc->next)
4735 {
4736 if (is_masked_watchpoint (b))
4737 {
4738 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4739 CORE_ADDR start = loc->address & w->hw_wp_mask;
4740
4741 if (newaddr == start)
4742 {
4743 w->watchpoint_triggered = watch_triggered_yes;
4744 break;
4745 }
4746 }
4747 /* Exact match not required. Within range is sufficient. */
4748 else if (target_watchpoint_addr_within_range (current_top_target (),
4749 addr, loc->address,
4750 loc->length))
4751 {
4752 w->watchpoint_triggered = watch_triggered_yes;
4753 break;
4754 }
4755 }
4756 }
4757
4758 return 1;
4759 }
4760
4761 /* Possible return values for watchpoint_check. */
4762 enum wp_check_result
4763 {
4764 /* The watchpoint has been deleted. */
4765 WP_DELETED = 1,
4766
4767 /* The value has changed. */
4768 WP_VALUE_CHANGED = 2,
4769
4770 /* The value has not changed. */
4771 WP_VALUE_NOT_CHANGED = 3,
4772
4773 /* Ignore this watchpoint, no matter if the value changed or not. */
4774 WP_IGNORE = 4,
4775 };
4776
4777 #define BP_TEMPFLAG 1
4778 #define BP_HARDWAREFLAG 2
4779
4780 /* Evaluate watchpoint condition expression and check if its value
4781 changed. */
4782
4783 static wp_check_result
4784 watchpoint_check (bpstat bs)
4785 {
4786 struct watchpoint *b;
4787 struct frame_info *fr;
4788 int within_current_scope;
4789
4790 /* BS is built from an existing struct breakpoint. */
4791 gdb_assert (bs->breakpoint_at != NULL);
4792 b = (struct watchpoint *) bs->breakpoint_at;
4793
4794 /* If this is a local watchpoint, we only want to check if the
4795 watchpoint frame is in scope if the current thread is the thread
4796 that was used to create the watchpoint. */
4797 if (!watchpoint_in_thread_scope (b))
4798 return WP_IGNORE;
4799
4800 if (b->exp_valid_block == NULL)
4801 within_current_scope = 1;
4802 else
4803 {
4804 struct frame_info *frame = get_current_frame ();
4805 struct gdbarch *frame_arch = get_frame_arch (frame);
4806 CORE_ADDR frame_pc = get_frame_pc (frame);
4807
4808 /* stack_frame_destroyed_p() returns a non-zero value if we're
4809 still in the function but the stack frame has already been
4810 invalidated. Since we can't rely on the values of local
4811 variables after the stack has been destroyed, we are treating
4812 the watchpoint in that state as `not changed' without further
4813 checking. Don't mark watchpoints as changed if the current
4814 frame is in an epilogue - even if they are in some other
4815 frame, our view of the stack is likely to be wrong and
4816 frame_find_by_id could error out. */
4817 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4818 return WP_IGNORE;
4819
4820 fr = frame_find_by_id (b->watchpoint_frame);
4821 within_current_scope = (fr != NULL);
4822
4823 /* If we've gotten confused in the unwinder, we might have
4824 returned a frame that can't describe this variable. */
4825 if (within_current_scope)
4826 {
4827 struct symbol *function;
4828
4829 function = get_frame_function (fr);
4830 if (function == NULL
4831 || !contained_in (b->exp_valid_block,
4832 SYMBOL_BLOCK_VALUE (function)))
4833 within_current_scope = 0;
4834 }
4835
4836 if (within_current_scope)
4837 /* If we end up stopping, the current frame will get selected
4838 in normal_stop. So this call to select_frame won't affect
4839 the user. */
4840 select_frame (fr);
4841 }
4842
4843 if (within_current_scope)
4844 {
4845 /* We use value_{,free_to_}mark because it could be a *long*
4846 time before we return to the command level and call
4847 free_all_values. We can't call free_all_values because we
4848 might be in the middle of evaluating a function call. */
4849
4850 int pc = 0;
4851 struct value *mark;
4852 struct value *new_val;
4853
4854 if (is_masked_watchpoint (b))
4855 /* Since we don't know the exact trigger address (from
4856 stopped_data_address), just tell the user we've triggered
4857 a mask watchpoint. */
4858 return WP_VALUE_CHANGED;
4859
4860 mark = value_mark ();
4861 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4862
4863 if (b->val_bitsize != 0)
4864 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4865
4866 /* We use value_equal_contents instead of value_equal because
4867 the latter coerces an array to a pointer, thus comparing just
4868 the address of the array instead of its contents. This is
4869 not what we want. */
4870 if ((b->val != NULL) != (new_val != NULL)
4871 || (b->val != NULL && !value_equal_contents (b->val.get (),
4872 new_val)))
4873 {
4874 bs->old_val = b->val;
4875 b->val = release_value (new_val);
4876 b->val_valid = true;
4877 if (new_val != NULL)
4878 value_free_to_mark (mark);
4879 return WP_VALUE_CHANGED;
4880 }
4881 else
4882 {
4883 /* Nothing changed. */
4884 value_free_to_mark (mark);
4885 return WP_VALUE_NOT_CHANGED;
4886 }
4887 }
4888 else
4889 {
4890 /* This seems like the only logical thing to do because
4891 if we temporarily ignored the watchpoint, then when
4892 we reenter the block in which it is valid it contains
4893 garbage (in the case of a function, it may have two
4894 garbage values, one before and one after the prologue).
4895 So we can't even detect the first assignment to it and
4896 watch after that (since the garbage may or may not equal
4897 the first value assigned). */
4898 /* We print all the stop information in
4899 breakpoint_ops->print_it, but in this case, by the time we
4900 call breakpoint_ops->print_it this bp will be deleted
4901 already. So we have no choice but print the information
4902 here. */
4903
4904 SWITCH_THRU_ALL_UIS ()
4905 {
4906 struct ui_out *uiout = current_uiout;
4907
4908 if (uiout->is_mi_like_p ())
4909 uiout->field_string
4910 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4911 uiout->message ("\nWatchpoint %pF deleted because the program has "
4912 "left the block in\n"
4913 "which its expression is valid.\n",
4914 signed_field ("wpnum", b->number));
4915 }
4916
4917 /* Make sure the watchpoint's commands aren't executed. */
4918 b->commands = NULL;
4919 watchpoint_del_at_next_stop (b);
4920
4921 return WP_DELETED;
4922 }
4923 }
4924
4925 /* Return true if it looks like target has stopped due to hitting
4926 breakpoint location BL. This function does not check if we should
4927 stop, only if BL explains the stop. */
4928
4929 static int
4930 bpstat_check_location (const struct bp_location *bl,
4931 const address_space *aspace, CORE_ADDR bp_addr,
4932 const struct target_waitstatus *ws)
4933 {
4934 struct breakpoint *b = bl->owner;
4935
4936 /* BL is from an existing breakpoint. */
4937 gdb_assert (b != NULL);
4938
4939 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4940 }
4941
4942 /* Determine if the watched values have actually changed, and we
4943 should stop. If not, set BS->stop to 0. */
4944
4945 static void
4946 bpstat_check_watchpoint (bpstat bs)
4947 {
4948 const struct bp_location *bl;
4949 struct watchpoint *b;
4950
4951 /* BS is built for existing struct breakpoint. */
4952 bl = bs->bp_location_at;
4953 gdb_assert (bl != NULL);
4954 b = (struct watchpoint *) bs->breakpoint_at;
4955 gdb_assert (b != NULL);
4956
4957 {
4958 int must_check_value = 0;
4959
4960 if (b->type == bp_watchpoint)
4961 /* For a software watchpoint, we must always check the
4962 watched value. */
4963 must_check_value = 1;
4964 else if (b->watchpoint_triggered == watch_triggered_yes)
4965 /* We have a hardware watchpoint (read, write, or access)
4966 and the target earlier reported an address watched by
4967 this watchpoint. */
4968 must_check_value = 1;
4969 else if (b->watchpoint_triggered == watch_triggered_unknown
4970 && b->type == bp_hardware_watchpoint)
4971 /* We were stopped by a hardware watchpoint, but the target could
4972 not report the data address. We must check the watchpoint's
4973 value. Access and read watchpoints are out of luck; without
4974 a data address, we can't figure it out. */
4975 must_check_value = 1;
4976
4977 if (must_check_value)
4978 {
4979 wp_check_result e;
4980
4981 try
4982 {
4983 e = watchpoint_check (bs);
4984 }
4985 catch (const gdb_exception &ex)
4986 {
4987 exception_fprintf (gdb_stderr, ex,
4988 "Error evaluating expression "
4989 "for watchpoint %d\n",
4990 b->number);
4991
4992 SWITCH_THRU_ALL_UIS ()
4993 {
4994 printf_filtered (_("Watchpoint %d deleted.\n"),
4995 b->number);
4996 }
4997 watchpoint_del_at_next_stop (b);
4998 e = WP_DELETED;
4999 }
5000
5001 switch (e)
5002 {
5003 case WP_DELETED:
5004 /* We've already printed what needs to be printed. */
5005 bs->print_it = print_it_done;
5006 /* Stop. */
5007 break;
5008 case WP_IGNORE:
5009 bs->print_it = print_it_noop;
5010 bs->stop = 0;
5011 break;
5012 case WP_VALUE_CHANGED:
5013 if (b->type == bp_read_watchpoint)
5014 {
5015 /* There are two cases to consider here:
5016
5017 1. We're watching the triggered memory for reads.
5018 In that case, trust the target, and always report
5019 the watchpoint hit to the user. Even though
5020 reads don't cause value changes, the value may
5021 have changed since the last time it was read, and
5022 since we're not trapping writes, we will not see
5023 those, and as such we should ignore our notion of
5024 old value.
5025
5026 2. We're watching the triggered memory for both
5027 reads and writes. There are two ways this may
5028 happen:
5029
5030 2.1. This is a target that can't break on data
5031 reads only, but can break on accesses (reads or
5032 writes), such as e.g., x86. We detect this case
5033 at the time we try to insert read watchpoints.
5034
5035 2.2. Otherwise, the target supports read
5036 watchpoints, but, the user set an access or write
5037 watchpoint watching the same memory as this read
5038 watchpoint.
5039
5040 If we're watching memory writes as well as reads,
5041 ignore watchpoint hits when we find that the
5042 value hasn't changed, as reads don't cause
5043 changes. This still gives false positives when
5044 the program writes the same value to memory as
5045 what there was already in memory (we will confuse
5046 it for a read), but it's much better than
5047 nothing. */
5048
5049 int other_write_watchpoint = 0;
5050
5051 if (bl->watchpoint_type == hw_read)
5052 {
5053 struct breakpoint *other_b;
5054
5055 ALL_BREAKPOINTS (other_b)
5056 if (other_b->type == bp_hardware_watchpoint
5057 || other_b->type == bp_access_watchpoint)
5058 {
5059 struct watchpoint *other_w =
5060 (struct watchpoint *) other_b;
5061
5062 if (other_w->watchpoint_triggered
5063 == watch_triggered_yes)
5064 {
5065 other_write_watchpoint = 1;
5066 break;
5067 }
5068 }
5069 }
5070
5071 if (other_write_watchpoint
5072 || bl->watchpoint_type == hw_access)
5073 {
5074 /* We're watching the same memory for writes,
5075 and the value changed since the last time we
5076 updated it, so this trap must be for a write.
5077 Ignore it. */
5078 bs->print_it = print_it_noop;
5079 bs->stop = 0;
5080 }
5081 }
5082 break;
5083 case WP_VALUE_NOT_CHANGED:
5084 if (b->type == bp_hardware_watchpoint
5085 || b->type == bp_watchpoint)
5086 {
5087 /* Don't stop: write watchpoints shouldn't fire if
5088 the value hasn't changed. */
5089 bs->print_it = print_it_noop;
5090 bs->stop = 0;
5091 }
5092 /* Stop. */
5093 break;
5094 default:
5095 /* Can't happen. */
5096 break;
5097 }
5098 }
5099 else /* must_check_value == 0 */
5100 {
5101 /* This is a case where some watchpoint(s) triggered, but
5102 not at the address of this watchpoint, or else no
5103 watchpoint triggered after all. So don't print
5104 anything for this watchpoint. */
5105 bs->print_it = print_it_noop;
5106 bs->stop = 0;
5107 }
5108 }
5109 }
5110
5111 /* For breakpoints that are currently marked as telling gdb to stop,
5112 check conditions (condition proper, frame, thread and ignore count)
5113 of breakpoint referred to by BS. If we should not stop for this
5114 breakpoint, set BS->stop to 0. */
5115
5116 static void
5117 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5118 {
5119 const struct bp_location *bl;
5120 struct breakpoint *b;
5121 /* Assume stop. */
5122 bool condition_result = true;
5123 struct expression *cond;
5124
5125 gdb_assert (bs->stop);
5126
5127 /* BS is built for existing struct breakpoint. */
5128 bl = bs->bp_location_at;
5129 gdb_assert (bl != NULL);
5130 b = bs->breakpoint_at;
5131 gdb_assert (b != NULL);
5132
5133 /* Even if the target evaluated the condition on its end and notified GDB, we
5134 need to do so again since GDB does not know if we stopped due to a
5135 breakpoint or a single step breakpoint. */
5136
5137 if (frame_id_p (b->frame_id)
5138 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5139 {
5140 bs->stop = 0;
5141 return;
5142 }
5143
5144 /* If this is a thread/task-specific breakpoint, don't waste cpu
5145 evaluating the condition if this isn't the specified
5146 thread/task. */
5147 if ((b->thread != -1 && b->thread != thread->global_num)
5148 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5149 {
5150 bs->stop = 0;
5151 return;
5152 }
5153
5154 /* Evaluate extension language breakpoints that have a "stop" method
5155 implemented. */
5156 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5157
5158 if (is_watchpoint (b))
5159 {
5160 struct watchpoint *w = (struct watchpoint *) b;
5161
5162 cond = w->cond_exp.get ();
5163 }
5164 else
5165 cond = bl->cond.get ();
5166
5167 if (cond && b->disposition != disp_del_at_next_stop)
5168 {
5169 int within_current_scope = 1;
5170 struct watchpoint * w;
5171
5172 /* We use value_mark and value_free_to_mark because it could
5173 be a long time before we return to the command level and
5174 call free_all_values. We can't call free_all_values
5175 because we might be in the middle of evaluating a
5176 function call. */
5177 struct value *mark = value_mark ();
5178
5179 if (is_watchpoint (b))
5180 w = (struct watchpoint *) b;
5181 else
5182 w = NULL;
5183
5184 /* Need to select the frame, with all that implies so that
5185 the conditions will have the right context. Because we
5186 use the frame, we will not see an inlined function's
5187 variables when we arrive at a breakpoint at the start
5188 of the inlined function; the current frame will be the
5189 call site. */
5190 if (w == NULL || w->cond_exp_valid_block == NULL)
5191 select_frame (get_current_frame ());
5192 else
5193 {
5194 struct frame_info *frame;
5195
5196 /* For local watchpoint expressions, which particular
5197 instance of a local is being watched matters, so we
5198 keep track of the frame to evaluate the expression
5199 in. To evaluate the condition however, it doesn't
5200 really matter which instantiation of the function
5201 where the condition makes sense triggers the
5202 watchpoint. This allows an expression like "watch
5203 global if q > 10" set in `func', catch writes to
5204 global on all threads that call `func', or catch
5205 writes on all recursive calls of `func' by a single
5206 thread. We simply always evaluate the condition in
5207 the innermost frame that's executing where it makes
5208 sense to evaluate the condition. It seems
5209 intuitive. */
5210 frame = block_innermost_frame (w->cond_exp_valid_block);
5211 if (frame != NULL)
5212 select_frame (frame);
5213 else
5214 within_current_scope = 0;
5215 }
5216 if (within_current_scope)
5217 {
5218 try
5219 {
5220 condition_result = breakpoint_cond_eval (cond);
5221 }
5222 catch (const gdb_exception &ex)
5223 {
5224 exception_fprintf (gdb_stderr, ex,
5225 "Error in testing breakpoint condition:\n");
5226 }
5227 }
5228 else
5229 {
5230 warning (_("Watchpoint condition cannot be tested "
5231 "in the current scope"));
5232 /* If we failed to set the right context for this
5233 watchpoint, unconditionally report it. */
5234 }
5235 /* FIXME-someday, should give breakpoint #. */
5236 value_free_to_mark (mark);
5237 }
5238
5239 if (cond && !condition_result)
5240 {
5241 bs->stop = 0;
5242 }
5243 else if (b->ignore_count > 0)
5244 {
5245 b->ignore_count--;
5246 bs->stop = 0;
5247 /* Increase the hit count even though we don't stop. */
5248 ++(b->hit_count);
5249 gdb::observers::breakpoint_modified.notify (b);
5250 }
5251 }
5252
5253 /* Returns true if we need to track moribund locations of LOC's type
5254 on the current target. */
5255
5256 static int
5257 need_moribund_for_location_type (struct bp_location *loc)
5258 {
5259 return ((loc->loc_type == bp_loc_software_breakpoint
5260 && !target_supports_stopped_by_sw_breakpoint ())
5261 || (loc->loc_type == bp_loc_hardware_breakpoint
5262 && !target_supports_stopped_by_hw_breakpoint ()));
5263 }
5264
5265 /* See breakpoint.h. */
5266
5267 bpstat
5268 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5269 const struct target_waitstatus *ws)
5270 {
5271 struct breakpoint *b;
5272 bpstat bs_head = NULL, *bs_link = &bs_head;
5273
5274 ALL_BREAKPOINTS (b)
5275 {
5276 if (!breakpoint_enabled (b))
5277 continue;
5278
5279 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5280 {
5281 /* For hardware watchpoints, we look only at the first
5282 location. The watchpoint_check function will work on the
5283 entire expression, not the individual locations. For
5284 read watchpoints, the watchpoints_triggered function has
5285 checked all locations already. */
5286 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5287 break;
5288
5289 if (!bl->enabled || bl->shlib_disabled)
5290 continue;
5291
5292 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5293 continue;
5294
5295 /* Come here if it's a watchpoint, or if the break address
5296 matches. */
5297
5298 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5299 explain stop. */
5300
5301 /* Assume we stop. Should we find a watchpoint that is not
5302 actually triggered, or if the condition of the breakpoint
5303 evaluates as false, we'll reset 'stop' to 0. */
5304 bs->stop = 1;
5305 bs->print = 1;
5306
5307 /* If this is a scope breakpoint, mark the associated
5308 watchpoint as triggered so that we will handle the
5309 out-of-scope event. We'll get to the watchpoint next
5310 iteration. */
5311 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5312 {
5313 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5314
5315 w->watchpoint_triggered = watch_triggered_yes;
5316 }
5317 }
5318 }
5319
5320 /* Check if a moribund breakpoint explains the stop. */
5321 if (!target_supports_stopped_by_sw_breakpoint ()
5322 || !target_supports_stopped_by_hw_breakpoint ())
5323 {
5324 for (bp_location *loc : moribund_locations)
5325 {
5326 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5327 && need_moribund_for_location_type (loc))
5328 {
5329 bpstat bs = new bpstats (loc, &bs_link);
5330 /* For hits of moribund locations, we should just proceed. */
5331 bs->stop = 0;
5332 bs->print = 0;
5333 bs->print_it = print_it_noop;
5334 }
5335 }
5336 }
5337
5338 return bs_head;
5339 }
5340
5341 /* See breakpoint.h. */
5342
5343 bpstat
5344 bpstat_stop_status (const address_space *aspace,
5345 CORE_ADDR bp_addr, thread_info *thread,
5346 const struct target_waitstatus *ws,
5347 bpstat stop_chain)
5348 {
5349 struct breakpoint *b = NULL;
5350 /* First item of allocated bpstat's. */
5351 bpstat bs_head = stop_chain;
5352 bpstat bs;
5353 int need_remove_insert;
5354 int removed_any;
5355
5356 /* First, build the bpstat chain with locations that explain a
5357 target stop, while being careful to not set the target running,
5358 as that may invalidate locations (in particular watchpoint
5359 locations are recreated). Resuming will happen here with
5360 breakpoint conditions or watchpoint expressions that include
5361 inferior function calls. */
5362 if (bs_head == NULL)
5363 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5364
5365 /* A bit of special processing for shlib breakpoints. We need to
5366 process solib loading here, so that the lists of loaded and
5367 unloaded libraries are correct before we handle "catch load" and
5368 "catch unload". */
5369 for (bs = bs_head; bs != NULL; bs = bs->next)
5370 {
5371 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5372 {
5373 handle_solib_event ();
5374 break;
5375 }
5376 }
5377
5378 /* Now go through the locations that caused the target to stop, and
5379 check whether we're interested in reporting this stop to higher
5380 layers, or whether we should resume the target transparently. */
5381
5382 removed_any = 0;
5383
5384 for (bs = bs_head; bs != NULL; bs = bs->next)
5385 {
5386 if (!bs->stop)
5387 continue;
5388
5389 b = bs->breakpoint_at;
5390 b->ops->check_status (bs);
5391 if (bs->stop)
5392 {
5393 bpstat_check_breakpoint_conditions (bs, thread);
5394
5395 if (bs->stop)
5396 {
5397 ++(b->hit_count);
5398 gdb::observers::breakpoint_modified.notify (b);
5399
5400 /* We will stop here. */
5401 if (b->disposition == disp_disable)
5402 {
5403 --(b->enable_count);
5404 if (b->enable_count <= 0)
5405 b->enable_state = bp_disabled;
5406 removed_any = 1;
5407 }
5408 if (b->silent)
5409 bs->print = 0;
5410 bs->commands = b->commands;
5411 if (command_line_is_silent (bs->commands
5412 ? bs->commands.get () : NULL))
5413 bs->print = 0;
5414
5415 b->ops->after_condition_true (bs);
5416 }
5417
5418 }
5419
5420 /* Print nothing for this entry if we don't stop or don't
5421 print. */
5422 if (!bs->stop || !bs->print)
5423 bs->print_it = print_it_noop;
5424 }
5425
5426 /* If we aren't stopping, the value of some hardware watchpoint may
5427 not have changed, but the intermediate memory locations we are
5428 watching may have. Don't bother if we're stopping; this will get
5429 done later. */
5430 need_remove_insert = 0;
5431 if (! bpstat_causes_stop (bs_head))
5432 for (bs = bs_head; bs != NULL; bs = bs->next)
5433 if (!bs->stop
5434 && bs->breakpoint_at
5435 && is_hardware_watchpoint (bs->breakpoint_at))
5436 {
5437 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5438
5439 update_watchpoint (w, 0 /* don't reparse. */);
5440 need_remove_insert = 1;
5441 }
5442
5443 if (need_remove_insert)
5444 update_global_location_list (UGLL_MAY_INSERT);
5445 else if (removed_any)
5446 update_global_location_list (UGLL_DONT_INSERT);
5447
5448 return bs_head;
5449 }
5450
5451 static void
5452 handle_jit_event (void)
5453 {
5454 struct frame_info *frame;
5455 struct gdbarch *gdbarch;
5456
5457 if (debug_infrun)
5458 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5459
5460 /* Switch terminal for any messages produced by
5461 breakpoint_re_set. */
5462 target_terminal::ours_for_output ();
5463
5464 frame = get_current_frame ();
5465 gdbarch = get_frame_arch (frame);
5466
5467 jit_event_handler (gdbarch);
5468
5469 target_terminal::inferior ();
5470 }
5471
5472 /* Prepare WHAT final decision for infrun. */
5473
5474 /* Decide what infrun needs to do with this bpstat. */
5475
5476 struct bpstat_what
5477 bpstat_what (bpstat bs_head)
5478 {
5479 struct bpstat_what retval;
5480 bpstat bs;
5481
5482 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5483 retval.call_dummy = STOP_NONE;
5484 retval.is_longjmp = false;
5485
5486 for (bs = bs_head; bs != NULL; bs = bs->next)
5487 {
5488 /* Extract this BS's action. After processing each BS, we check
5489 if its action overrides all we've seem so far. */
5490 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5491 enum bptype bptype;
5492
5493 if (bs->breakpoint_at == NULL)
5494 {
5495 /* I suspect this can happen if it was a momentary
5496 breakpoint which has since been deleted. */
5497 bptype = bp_none;
5498 }
5499 else
5500 bptype = bs->breakpoint_at->type;
5501
5502 switch (bptype)
5503 {
5504 case bp_none:
5505 break;
5506 case bp_breakpoint:
5507 case bp_hardware_breakpoint:
5508 case bp_single_step:
5509 case bp_until:
5510 case bp_finish:
5511 case bp_shlib_event:
5512 if (bs->stop)
5513 {
5514 if (bs->print)
5515 this_action = BPSTAT_WHAT_STOP_NOISY;
5516 else
5517 this_action = BPSTAT_WHAT_STOP_SILENT;
5518 }
5519 else
5520 this_action = BPSTAT_WHAT_SINGLE;
5521 break;
5522 case bp_watchpoint:
5523 case bp_hardware_watchpoint:
5524 case bp_read_watchpoint:
5525 case bp_access_watchpoint:
5526 if (bs->stop)
5527 {
5528 if (bs->print)
5529 this_action = BPSTAT_WHAT_STOP_NOISY;
5530 else
5531 this_action = BPSTAT_WHAT_STOP_SILENT;
5532 }
5533 else
5534 {
5535 /* There was a watchpoint, but we're not stopping.
5536 This requires no further action. */
5537 }
5538 break;
5539 case bp_longjmp:
5540 case bp_longjmp_call_dummy:
5541 case bp_exception:
5542 if (bs->stop)
5543 {
5544 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5545 retval.is_longjmp = bptype != bp_exception;
5546 }
5547 else
5548 this_action = BPSTAT_WHAT_SINGLE;
5549 break;
5550 case bp_longjmp_resume:
5551 case bp_exception_resume:
5552 if (bs->stop)
5553 {
5554 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5555 retval.is_longjmp = bptype == bp_longjmp_resume;
5556 }
5557 else
5558 this_action = BPSTAT_WHAT_SINGLE;
5559 break;
5560 case bp_step_resume:
5561 if (bs->stop)
5562 this_action = BPSTAT_WHAT_STEP_RESUME;
5563 else
5564 {
5565 /* It is for the wrong frame. */
5566 this_action = BPSTAT_WHAT_SINGLE;
5567 }
5568 break;
5569 case bp_hp_step_resume:
5570 if (bs->stop)
5571 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5572 else
5573 {
5574 /* It is for the wrong frame. */
5575 this_action = BPSTAT_WHAT_SINGLE;
5576 }
5577 break;
5578 case bp_watchpoint_scope:
5579 case bp_thread_event:
5580 case bp_overlay_event:
5581 case bp_longjmp_master:
5582 case bp_std_terminate_master:
5583 case bp_exception_master:
5584 this_action = BPSTAT_WHAT_SINGLE;
5585 break;
5586 case bp_catchpoint:
5587 if (bs->stop)
5588 {
5589 if (bs->print)
5590 this_action = BPSTAT_WHAT_STOP_NOISY;
5591 else
5592 this_action = BPSTAT_WHAT_STOP_SILENT;
5593 }
5594 else
5595 {
5596 /* Some catchpoints are implemented with breakpoints.
5597 For those, we need to step over the breakpoint. */
5598 if (bs->bp_location_at->loc_type != bp_loc_other)
5599 this_action = BPSTAT_WHAT_SINGLE;
5600 }
5601 break;
5602 case bp_jit_event:
5603 this_action = BPSTAT_WHAT_SINGLE;
5604 break;
5605 case bp_call_dummy:
5606 /* Make sure the action is stop (silent or noisy),
5607 so infrun.c pops the dummy frame. */
5608 retval.call_dummy = STOP_STACK_DUMMY;
5609 this_action = BPSTAT_WHAT_STOP_SILENT;
5610 break;
5611 case bp_std_terminate:
5612 /* Make sure the action is stop (silent or noisy),
5613 so infrun.c pops the dummy frame. */
5614 retval.call_dummy = STOP_STD_TERMINATE;
5615 this_action = BPSTAT_WHAT_STOP_SILENT;
5616 break;
5617 case bp_tracepoint:
5618 case bp_fast_tracepoint:
5619 case bp_static_tracepoint:
5620 /* Tracepoint hits should not be reported back to GDB, and
5621 if one got through somehow, it should have been filtered
5622 out already. */
5623 internal_error (__FILE__, __LINE__,
5624 _("bpstat_what: tracepoint encountered"));
5625 break;
5626 case bp_gnu_ifunc_resolver:
5627 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5628 this_action = BPSTAT_WHAT_SINGLE;
5629 break;
5630 case bp_gnu_ifunc_resolver_return:
5631 /* The breakpoint will be removed, execution will restart from the
5632 PC of the former breakpoint. */
5633 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5634 break;
5635
5636 case bp_dprintf:
5637 if (bs->stop)
5638 this_action = BPSTAT_WHAT_STOP_SILENT;
5639 else
5640 this_action = BPSTAT_WHAT_SINGLE;
5641 break;
5642
5643 default:
5644 internal_error (__FILE__, __LINE__,
5645 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5646 }
5647
5648 retval.main_action = std::max (retval.main_action, this_action);
5649 }
5650
5651 return retval;
5652 }
5653
5654 void
5655 bpstat_run_callbacks (bpstat bs_head)
5656 {
5657 bpstat bs;
5658
5659 for (bs = bs_head; bs != NULL; bs = bs->next)
5660 {
5661 struct breakpoint *b = bs->breakpoint_at;
5662
5663 if (b == NULL)
5664 continue;
5665 switch (b->type)
5666 {
5667 case bp_jit_event:
5668 handle_jit_event ();
5669 break;
5670 case bp_gnu_ifunc_resolver:
5671 gnu_ifunc_resolver_stop (b);
5672 break;
5673 case bp_gnu_ifunc_resolver_return:
5674 gnu_ifunc_resolver_return_stop (b);
5675 break;
5676 }
5677 }
5678 }
5679
5680 /* See breakpoint.h. */
5681
5682 bool
5683 bpstat_should_step ()
5684 {
5685 struct breakpoint *b;
5686
5687 ALL_BREAKPOINTS (b)
5688 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5689 return true;
5690 return false;
5691 }
5692
5693 /* See breakpoint.h. */
5694
5695 bool
5696 bpstat_causes_stop (bpstat bs)
5697 {
5698 for (; bs != NULL; bs = bs->next)
5699 if (bs->stop)
5700 return true;
5701
5702 return false;
5703 }
5704
5705 \f
5706
5707 /* Compute a string of spaces suitable to indent the next line
5708 so it starts at the position corresponding to the table column
5709 named COL_NAME in the currently active table of UIOUT. */
5710
5711 static char *
5712 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5713 {
5714 static char wrap_indent[80];
5715 int i, total_width, width, align;
5716 const char *text;
5717
5718 total_width = 0;
5719 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5720 {
5721 if (strcmp (text, col_name) == 0)
5722 {
5723 gdb_assert (total_width < sizeof wrap_indent);
5724 memset (wrap_indent, ' ', total_width);
5725 wrap_indent[total_width] = 0;
5726
5727 return wrap_indent;
5728 }
5729
5730 total_width += width + 1;
5731 }
5732
5733 return NULL;
5734 }
5735
5736 /* Determine if the locations of this breakpoint will have their conditions
5737 evaluated by the target, host or a mix of both. Returns the following:
5738
5739 "host": Host evals condition.
5740 "host or target": Host or Target evals condition.
5741 "target": Target evals condition.
5742 */
5743
5744 static const char *
5745 bp_condition_evaluator (struct breakpoint *b)
5746 {
5747 struct bp_location *bl;
5748 char host_evals = 0;
5749 char target_evals = 0;
5750
5751 if (!b)
5752 return NULL;
5753
5754 if (!is_breakpoint (b))
5755 return NULL;
5756
5757 if (gdb_evaluates_breakpoint_condition_p ()
5758 || !target_supports_evaluation_of_breakpoint_conditions ())
5759 return condition_evaluation_host;
5760
5761 for (bl = b->loc; bl; bl = bl->next)
5762 {
5763 if (bl->cond_bytecode)
5764 target_evals++;
5765 else
5766 host_evals++;
5767 }
5768
5769 if (host_evals && target_evals)
5770 return condition_evaluation_both;
5771 else if (target_evals)
5772 return condition_evaluation_target;
5773 else
5774 return condition_evaluation_host;
5775 }
5776
5777 /* Determine the breakpoint location's condition evaluator. This is
5778 similar to bp_condition_evaluator, but for locations. */
5779
5780 static const char *
5781 bp_location_condition_evaluator (struct bp_location *bl)
5782 {
5783 if (bl && !is_breakpoint (bl->owner))
5784 return NULL;
5785
5786 if (gdb_evaluates_breakpoint_condition_p ()
5787 || !target_supports_evaluation_of_breakpoint_conditions ())
5788 return condition_evaluation_host;
5789
5790 if (bl && bl->cond_bytecode)
5791 return condition_evaluation_target;
5792 else
5793 return condition_evaluation_host;
5794 }
5795
5796 /* Print the LOC location out of the list of B->LOC locations. */
5797
5798 static void
5799 print_breakpoint_location (struct breakpoint *b,
5800 struct bp_location *loc)
5801 {
5802 struct ui_out *uiout = current_uiout;
5803
5804 scoped_restore_current_program_space restore_pspace;
5805
5806 if (loc != NULL && loc->shlib_disabled)
5807 loc = NULL;
5808
5809 if (loc != NULL)
5810 set_current_program_space (loc->pspace);
5811
5812 if (b->display_canonical)
5813 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5814 else if (loc && loc->symtab)
5815 {
5816 const struct symbol *sym = loc->symbol;
5817
5818 if (sym)
5819 {
5820 uiout->text ("in ");
5821 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
5822 function_name_style.style ());
5823 uiout->text (" ");
5824 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5825 uiout->text ("at ");
5826 }
5827 uiout->field_string ("file",
5828 symtab_to_filename_for_display (loc->symtab),
5829 file_name_style.style ());
5830 uiout->text (":");
5831
5832 if (uiout->is_mi_like_p ())
5833 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5834
5835 uiout->field_signed ("line", loc->line_number);
5836 }
5837 else if (loc)
5838 {
5839 string_file stb;
5840
5841 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5842 demangle, "");
5843 uiout->field_stream ("at", stb);
5844 }
5845 else
5846 {
5847 uiout->field_string ("pending",
5848 event_location_to_string (b->location.get ()));
5849 /* If extra_string is available, it could be holding a condition
5850 or dprintf arguments. In either case, make sure it is printed,
5851 too, but only for non-MI streams. */
5852 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5853 {
5854 if (b->type == bp_dprintf)
5855 uiout->text (",");
5856 else
5857 uiout->text (" ");
5858 uiout->text (b->extra_string);
5859 }
5860 }
5861
5862 if (loc && is_breakpoint (b)
5863 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5864 && bp_condition_evaluator (b) == condition_evaluation_both)
5865 {
5866 uiout->text (" (");
5867 uiout->field_string ("evaluated-by",
5868 bp_location_condition_evaluator (loc));
5869 uiout->text (")");
5870 }
5871 }
5872
5873 static const char *
5874 bptype_string (enum bptype type)
5875 {
5876 struct ep_type_description
5877 {
5878 enum bptype type;
5879 const char *description;
5880 };
5881 static struct ep_type_description bptypes[] =
5882 {
5883 {bp_none, "?deleted?"},
5884 {bp_breakpoint, "breakpoint"},
5885 {bp_hardware_breakpoint, "hw breakpoint"},
5886 {bp_single_step, "sw single-step"},
5887 {bp_until, "until"},
5888 {bp_finish, "finish"},
5889 {bp_watchpoint, "watchpoint"},
5890 {bp_hardware_watchpoint, "hw watchpoint"},
5891 {bp_read_watchpoint, "read watchpoint"},
5892 {bp_access_watchpoint, "acc watchpoint"},
5893 {bp_longjmp, "longjmp"},
5894 {bp_longjmp_resume, "longjmp resume"},
5895 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5896 {bp_exception, "exception"},
5897 {bp_exception_resume, "exception resume"},
5898 {bp_step_resume, "step resume"},
5899 {bp_hp_step_resume, "high-priority step resume"},
5900 {bp_watchpoint_scope, "watchpoint scope"},
5901 {bp_call_dummy, "call dummy"},
5902 {bp_std_terminate, "std::terminate"},
5903 {bp_shlib_event, "shlib events"},
5904 {bp_thread_event, "thread events"},
5905 {bp_overlay_event, "overlay events"},
5906 {bp_longjmp_master, "longjmp master"},
5907 {bp_std_terminate_master, "std::terminate master"},
5908 {bp_exception_master, "exception master"},
5909 {bp_catchpoint, "catchpoint"},
5910 {bp_tracepoint, "tracepoint"},
5911 {bp_fast_tracepoint, "fast tracepoint"},
5912 {bp_static_tracepoint, "static tracepoint"},
5913 {bp_dprintf, "dprintf"},
5914 {bp_jit_event, "jit events"},
5915 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5916 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5917 };
5918
5919 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5920 || ((int) type != bptypes[(int) type].type))
5921 internal_error (__FILE__, __LINE__,
5922 _("bptypes table does not describe type #%d."),
5923 (int) type);
5924
5925 return bptypes[(int) type].description;
5926 }
5927
5928 /* For MI, output a field named 'thread-groups' with a list as the value.
5929 For CLI, prefix the list with the string 'inf'. */
5930
5931 static void
5932 output_thread_groups (struct ui_out *uiout,
5933 const char *field_name,
5934 const std::vector<int> &inf_nums,
5935 int mi_only)
5936 {
5937 int is_mi = uiout->is_mi_like_p ();
5938
5939 /* For backward compatibility, don't display inferiors in CLI unless
5940 there are several. Always display them for MI. */
5941 if (!is_mi && mi_only)
5942 return;
5943
5944 ui_out_emit_list list_emitter (uiout, field_name);
5945
5946 for (size_t i = 0; i < inf_nums.size (); i++)
5947 {
5948 if (is_mi)
5949 {
5950 char mi_group[10];
5951
5952 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
5953 uiout->field_string (NULL, mi_group);
5954 }
5955 else
5956 {
5957 if (i == 0)
5958 uiout->text (" inf ");
5959 else
5960 uiout->text (", ");
5961
5962 uiout->text (plongest (inf_nums[i]));
5963 }
5964 }
5965 }
5966
5967 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5968 instead of going via breakpoint_ops::print_one. This makes "maint
5969 info breakpoints" show the software breakpoint locations of
5970 catchpoints, which are considered internal implementation
5971 detail. */
5972
5973 static void
5974 print_one_breakpoint_location (struct breakpoint *b,
5975 struct bp_location *loc,
5976 int loc_number,
5977 struct bp_location **last_loc,
5978 int allflag, bool raw_loc)
5979 {
5980 struct command_line *l;
5981 static char bpenables[] = "nynny";
5982
5983 struct ui_out *uiout = current_uiout;
5984 int header_of_multiple = 0;
5985 int part_of_multiple = (loc != NULL);
5986 struct value_print_options opts;
5987
5988 get_user_print_options (&opts);
5989
5990 gdb_assert (!loc || loc_number != 0);
5991 /* See comment in print_one_breakpoint concerning treatment of
5992 breakpoints with single disabled location. */
5993 if (loc == NULL
5994 && (b->loc != NULL
5995 && (b->loc->next != NULL || !b->loc->enabled)))
5996 header_of_multiple = 1;
5997 if (loc == NULL)
5998 loc = b->loc;
5999
6000 annotate_record ();
6001
6002 /* 1 */
6003 annotate_field (0);
6004 if (part_of_multiple)
6005 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6006 else
6007 uiout->field_signed ("number", b->number);
6008
6009 /* 2 */
6010 annotate_field (1);
6011 if (part_of_multiple)
6012 uiout->field_skip ("type");
6013 else
6014 uiout->field_string ("type", bptype_string (b->type));
6015
6016 /* 3 */
6017 annotate_field (2);
6018 if (part_of_multiple)
6019 uiout->field_skip ("disp");
6020 else
6021 uiout->field_string ("disp", bpdisp_text (b->disposition));
6022
6023 /* 4 */
6024 annotate_field (3);
6025 if (part_of_multiple)
6026 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6027 else
6028 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6029
6030 /* 5 and 6 */
6031 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6032 b->ops->print_one (b, last_loc);
6033 else
6034 {
6035 if (is_watchpoint (b))
6036 {
6037 struct watchpoint *w = (struct watchpoint *) b;
6038
6039 /* Field 4, the address, is omitted (which makes the columns
6040 not line up too nicely with the headers, but the effect
6041 is relatively readable). */
6042 if (opts.addressprint)
6043 uiout->field_skip ("addr");
6044 annotate_field (5);
6045 uiout->field_string ("what", w->exp_string);
6046 }
6047 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6048 || is_ada_exception_catchpoint (b))
6049 {
6050 if (opts.addressprint)
6051 {
6052 annotate_field (4);
6053 if (header_of_multiple)
6054 uiout->field_string ("addr", "<MULTIPLE>",
6055 metadata_style.style ());
6056 else if (b->loc == NULL || loc->shlib_disabled)
6057 uiout->field_string ("addr", "<PENDING>",
6058 metadata_style.style ());
6059 else
6060 uiout->field_core_addr ("addr",
6061 loc->gdbarch, loc->address);
6062 }
6063 annotate_field (5);
6064 if (!header_of_multiple)
6065 print_breakpoint_location (b, loc);
6066 if (b->loc)
6067 *last_loc = b->loc;
6068 }
6069 }
6070
6071 if (loc != NULL && !header_of_multiple)
6072 {
6073 std::vector<int> inf_nums;
6074 int mi_only = 1;
6075
6076 for (inferior *inf : all_inferiors ())
6077 {
6078 if (inf->pspace == loc->pspace)
6079 inf_nums.push_back (inf->num);
6080 }
6081
6082 /* For backward compatibility, don't display inferiors in CLI unless
6083 there are several. Always display for MI. */
6084 if (allflag
6085 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6086 && (number_of_program_spaces () > 1
6087 || number_of_inferiors () > 1)
6088 /* LOC is for existing B, it cannot be in
6089 moribund_locations and thus having NULL OWNER. */
6090 && loc->owner->type != bp_catchpoint))
6091 mi_only = 0;
6092 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6093 }
6094
6095 if (!part_of_multiple)
6096 {
6097 if (b->thread != -1)
6098 {
6099 /* FIXME: This seems to be redundant and lost here; see the
6100 "stop only in" line a little further down. */
6101 uiout->text (" thread ");
6102 uiout->field_signed ("thread", b->thread);
6103 }
6104 else if (b->task != 0)
6105 {
6106 uiout->text (" task ");
6107 uiout->field_signed ("task", b->task);
6108 }
6109 }
6110
6111 uiout->text ("\n");
6112
6113 if (!part_of_multiple)
6114 b->ops->print_one_detail (b, uiout);
6115
6116 if (part_of_multiple && frame_id_p (b->frame_id))
6117 {
6118 annotate_field (6);
6119 uiout->text ("\tstop only in stack frame at ");
6120 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6121 the frame ID. */
6122 uiout->field_core_addr ("frame",
6123 b->gdbarch, b->frame_id.stack_addr);
6124 uiout->text ("\n");
6125 }
6126
6127 if (!part_of_multiple && b->cond_string)
6128 {
6129 annotate_field (7);
6130 if (is_tracepoint (b))
6131 uiout->text ("\ttrace only if ");
6132 else
6133 uiout->text ("\tstop only if ");
6134 uiout->field_string ("cond", b->cond_string);
6135
6136 /* Print whether the target is doing the breakpoint's condition
6137 evaluation. If GDB is doing the evaluation, don't print anything. */
6138 if (is_breakpoint (b)
6139 && breakpoint_condition_evaluation_mode ()
6140 == condition_evaluation_target)
6141 {
6142 uiout->message (" (%pF evals)",
6143 string_field ("evaluated-by",
6144 bp_condition_evaluator (b)));
6145 }
6146 uiout->text ("\n");
6147 }
6148
6149 if (!part_of_multiple && b->thread != -1)
6150 {
6151 /* FIXME should make an annotation for this. */
6152 uiout->text ("\tstop only in thread ");
6153 if (uiout->is_mi_like_p ())
6154 uiout->field_signed ("thread", b->thread);
6155 else
6156 {
6157 struct thread_info *thr = find_thread_global_id (b->thread);
6158
6159 uiout->field_string ("thread", print_thread_id (thr));
6160 }
6161 uiout->text ("\n");
6162 }
6163
6164 if (!part_of_multiple)
6165 {
6166 if (b->hit_count)
6167 {
6168 /* FIXME should make an annotation for this. */
6169 if (is_catchpoint (b))
6170 uiout->text ("\tcatchpoint");
6171 else if (is_tracepoint (b))
6172 uiout->text ("\ttracepoint");
6173 else
6174 uiout->text ("\tbreakpoint");
6175 uiout->text (" already hit ");
6176 uiout->field_signed ("times", b->hit_count);
6177 if (b->hit_count == 1)
6178 uiout->text (" time\n");
6179 else
6180 uiout->text (" times\n");
6181 }
6182 else
6183 {
6184 /* Output the count also if it is zero, but only if this is mi. */
6185 if (uiout->is_mi_like_p ())
6186 uiout->field_signed ("times", b->hit_count);
6187 }
6188 }
6189
6190 if (!part_of_multiple && b->ignore_count)
6191 {
6192 annotate_field (8);
6193 uiout->message ("\tignore next %pF hits\n",
6194 signed_field ("ignore", b->ignore_count));
6195 }
6196
6197 /* Note that an enable count of 1 corresponds to "enable once"
6198 behavior, which is reported by the combination of enablement and
6199 disposition, so we don't need to mention it here. */
6200 if (!part_of_multiple && b->enable_count > 1)
6201 {
6202 annotate_field (8);
6203 uiout->text ("\tdisable after ");
6204 /* Tweak the wording to clarify that ignore and enable counts
6205 are distinct, and have additive effect. */
6206 if (b->ignore_count)
6207 uiout->text ("additional ");
6208 else
6209 uiout->text ("next ");
6210 uiout->field_signed ("enable", b->enable_count);
6211 uiout->text (" hits\n");
6212 }
6213
6214 if (!part_of_multiple && is_tracepoint (b))
6215 {
6216 struct tracepoint *tp = (struct tracepoint *) b;
6217
6218 if (tp->traceframe_usage)
6219 {
6220 uiout->text ("\ttrace buffer usage ");
6221 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6222 uiout->text (" bytes\n");
6223 }
6224 }
6225
6226 l = b->commands ? b->commands.get () : NULL;
6227 if (!part_of_multiple && l)
6228 {
6229 annotate_field (9);
6230 ui_out_emit_tuple tuple_emitter (uiout, "script");
6231 print_command_lines (uiout, l, 4);
6232 }
6233
6234 if (is_tracepoint (b))
6235 {
6236 struct tracepoint *t = (struct tracepoint *) b;
6237
6238 if (!part_of_multiple && t->pass_count)
6239 {
6240 annotate_field (10);
6241 uiout->text ("\tpass count ");
6242 uiout->field_signed ("pass", t->pass_count);
6243 uiout->text (" \n");
6244 }
6245
6246 /* Don't display it when tracepoint or tracepoint location is
6247 pending. */
6248 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6249 {
6250 annotate_field (11);
6251
6252 if (uiout->is_mi_like_p ())
6253 uiout->field_string ("installed",
6254 loc->inserted ? "y" : "n");
6255 else
6256 {
6257 if (loc->inserted)
6258 uiout->text ("\t");
6259 else
6260 uiout->text ("\tnot ");
6261 uiout->text ("installed on target\n");
6262 }
6263 }
6264 }
6265
6266 if (uiout->is_mi_like_p () && !part_of_multiple)
6267 {
6268 if (is_watchpoint (b))
6269 {
6270 struct watchpoint *w = (struct watchpoint *) b;
6271
6272 uiout->field_string ("original-location", w->exp_string);
6273 }
6274 else if (b->location != NULL
6275 && event_location_to_string (b->location.get ()) != NULL)
6276 uiout->field_string ("original-location",
6277 event_location_to_string (b->location.get ()));
6278 }
6279 }
6280
6281 /* See breakpoint.h. */
6282
6283 bool fix_multi_location_breakpoint_output_globally = false;
6284
6285 static void
6286 print_one_breakpoint (struct breakpoint *b,
6287 struct bp_location **last_loc,
6288 int allflag)
6289 {
6290 struct ui_out *uiout = current_uiout;
6291 bool use_fixed_output
6292 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6293 || fix_multi_location_breakpoint_output_globally);
6294
6295 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6296 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6297
6298 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6299 are outside. */
6300 if (!use_fixed_output)
6301 bkpt_tuple_emitter.reset ();
6302
6303 /* If this breakpoint has custom print function,
6304 it's already printed. Otherwise, print individual
6305 locations, if any. */
6306 if (b->ops == NULL
6307 || b->ops->print_one == NULL
6308 || allflag)
6309 {
6310 /* If breakpoint has a single location that is disabled, we
6311 print it as if it had several locations, since otherwise it's
6312 hard to represent "breakpoint enabled, location disabled"
6313 situation.
6314
6315 Note that while hardware watchpoints have several locations
6316 internally, that's not a property exposed to users.
6317
6318 Likewise, while catchpoints may be implemented with
6319 breakpoints (e.g., catch throw), that's not a property
6320 exposed to users. We do however display the internal
6321 breakpoint locations with "maint info breakpoints". */
6322 if (!is_hardware_watchpoint (b)
6323 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6324 || is_ada_exception_catchpoint (b))
6325 && (allflag
6326 || (b->loc && (b->loc->next || !b->loc->enabled))))
6327 {
6328 gdb::optional<ui_out_emit_list> locations_list;
6329
6330 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6331 MI record. For later versions, place breakpoint locations in a
6332 list. */
6333 if (uiout->is_mi_like_p () && use_fixed_output)
6334 locations_list.emplace (uiout, "locations");
6335
6336 int n = 1;
6337 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6338 {
6339 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6340 print_one_breakpoint_location (b, loc, n, last_loc,
6341 allflag, allflag);
6342 }
6343 }
6344 }
6345 }
6346
6347 static int
6348 breakpoint_address_bits (struct breakpoint *b)
6349 {
6350 int print_address_bits = 0;
6351 struct bp_location *loc;
6352
6353 /* Software watchpoints that aren't watching memory don't have an
6354 address to print. */
6355 if (is_no_memory_software_watchpoint (b))
6356 return 0;
6357
6358 for (loc = b->loc; loc; loc = loc->next)
6359 {
6360 int addr_bit;
6361
6362 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6363 if (addr_bit > print_address_bits)
6364 print_address_bits = addr_bit;
6365 }
6366
6367 return print_address_bits;
6368 }
6369
6370 /* See breakpoint.h. */
6371
6372 void
6373 print_breakpoint (breakpoint *b)
6374 {
6375 struct bp_location *dummy_loc = NULL;
6376 print_one_breakpoint (b, &dummy_loc, 0);
6377 }
6378
6379 /* Return true if this breakpoint was set by the user, false if it is
6380 internal or momentary. */
6381
6382 int
6383 user_breakpoint_p (struct breakpoint *b)
6384 {
6385 return b->number > 0;
6386 }
6387
6388 /* See breakpoint.h. */
6389
6390 int
6391 pending_breakpoint_p (struct breakpoint *b)
6392 {
6393 return b->loc == NULL;
6394 }
6395
6396 /* Print information on breakpoints (including watchpoints and tracepoints).
6397
6398 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6399 understood by number_or_range_parser. Only breakpoints included in this
6400 list are then printed.
6401
6402 If SHOW_INTERNAL is true, print internal breakpoints.
6403
6404 If FILTER is non-NULL, call it on each breakpoint and only include the
6405 ones for which it returns true.
6406
6407 Return the total number of breakpoints listed. */
6408
6409 static int
6410 breakpoint_1 (const char *bp_num_list, bool show_internal,
6411 bool (*filter) (const struct breakpoint *))
6412 {
6413 struct breakpoint *b;
6414 struct bp_location *last_loc = NULL;
6415 int nr_printable_breakpoints;
6416 struct value_print_options opts;
6417 int print_address_bits = 0;
6418 int print_type_col_width = 14;
6419 struct ui_out *uiout = current_uiout;
6420
6421 get_user_print_options (&opts);
6422
6423 /* Compute the number of rows in the table, as well as the size
6424 required for address fields. */
6425 nr_printable_breakpoints = 0;
6426 ALL_BREAKPOINTS (b)
6427 {
6428 /* If we have a filter, only list the breakpoints it accepts. */
6429 if (filter && !filter (b))
6430 continue;
6431
6432 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6433 accept. Skip the others. */
6434 if (bp_num_list != NULL && *bp_num_list != '\0')
6435 {
6436 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6437 continue;
6438 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6439 continue;
6440 }
6441
6442 if (show_internal || user_breakpoint_p (b))
6443 {
6444 int addr_bit, type_len;
6445
6446 addr_bit = breakpoint_address_bits (b);
6447 if (addr_bit > print_address_bits)
6448 print_address_bits = addr_bit;
6449
6450 type_len = strlen (bptype_string (b->type));
6451 if (type_len > print_type_col_width)
6452 print_type_col_width = type_len;
6453
6454 nr_printable_breakpoints++;
6455 }
6456 }
6457
6458 {
6459 ui_out_emit_table table_emitter (uiout,
6460 opts.addressprint ? 6 : 5,
6461 nr_printable_breakpoints,
6462 "BreakpointTable");
6463
6464 if (nr_printable_breakpoints > 0)
6465 annotate_breakpoints_headers ();
6466 if (nr_printable_breakpoints > 0)
6467 annotate_field (0);
6468 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6469 if (nr_printable_breakpoints > 0)
6470 annotate_field (1);
6471 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6472 if (nr_printable_breakpoints > 0)
6473 annotate_field (2);
6474 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6475 if (nr_printable_breakpoints > 0)
6476 annotate_field (3);
6477 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6478 if (opts.addressprint)
6479 {
6480 if (nr_printable_breakpoints > 0)
6481 annotate_field (4);
6482 if (print_address_bits <= 32)
6483 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6484 else
6485 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6486 }
6487 if (nr_printable_breakpoints > 0)
6488 annotate_field (5);
6489 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6490 uiout->table_body ();
6491 if (nr_printable_breakpoints > 0)
6492 annotate_breakpoints_table ();
6493
6494 ALL_BREAKPOINTS (b)
6495 {
6496 QUIT;
6497 /* If we have a filter, only list the breakpoints it accepts. */
6498 if (filter && !filter (b))
6499 continue;
6500
6501 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6502 accept. Skip the others. */
6503
6504 if (bp_num_list != NULL && *bp_num_list != '\0')
6505 {
6506 if (show_internal) /* maintenance info breakpoint */
6507 {
6508 if (parse_and_eval_long (bp_num_list) != b->number)
6509 continue;
6510 }
6511 else /* all others */
6512 {
6513 if (!number_is_in_list (bp_num_list, b->number))
6514 continue;
6515 }
6516 }
6517 /* We only print out user settable breakpoints unless the
6518 show_internal is set. */
6519 if (show_internal || user_breakpoint_p (b))
6520 print_one_breakpoint (b, &last_loc, show_internal);
6521 }
6522 }
6523
6524 if (nr_printable_breakpoints == 0)
6525 {
6526 /* If there's a filter, let the caller decide how to report
6527 empty list. */
6528 if (!filter)
6529 {
6530 if (bp_num_list == NULL || *bp_num_list == '\0')
6531 uiout->message ("No breakpoints or watchpoints.\n");
6532 else
6533 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6534 bp_num_list);
6535 }
6536 }
6537 else
6538 {
6539 if (last_loc && !server_command)
6540 set_next_address (last_loc->gdbarch, last_loc->address);
6541 }
6542
6543 /* FIXME? Should this be moved up so that it is only called when
6544 there have been breakpoints? */
6545 annotate_breakpoints_table_end ();
6546
6547 return nr_printable_breakpoints;
6548 }
6549
6550 /* Display the value of default-collect in a way that is generally
6551 compatible with the breakpoint list. */
6552
6553 static void
6554 default_collect_info (void)
6555 {
6556 struct ui_out *uiout = current_uiout;
6557
6558 /* If it has no value (which is frequently the case), say nothing; a
6559 message like "No default-collect." gets in user's face when it's
6560 not wanted. */
6561 if (!*default_collect)
6562 return;
6563
6564 /* The following phrase lines up nicely with per-tracepoint collect
6565 actions. */
6566 uiout->text ("default collect ");
6567 uiout->field_string ("default-collect", default_collect);
6568 uiout->text (" \n");
6569 }
6570
6571 static void
6572 info_breakpoints_command (const char *args, int from_tty)
6573 {
6574 breakpoint_1 (args, false, NULL);
6575
6576 default_collect_info ();
6577 }
6578
6579 static void
6580 info_watchpoints_command (const char *args, int from_tty)
6581 {
6582 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6583 struct ui_out *uiout = current_uiout;
6584
6585 if (num_printed == 0)
6586 {
6587 if (args == NULL || *args == '\0')
6588 uiout->message ("No watchpoints.\n");
6589 else
6590 uiout->message ("No watchpoint matching '%s'.\n", args);
6591 }
6592 }
6593
6594 static void
6595 maintenance_info_breakpoints (const char *args, int from_tty)
6596 {
6597 breakpoint_1 (args, true, NULL);
6598
6599 default_collect_info ();
6600 }
6601
6602 static int
6603 breakpoint_has_pc (struct breakpoint *b,
6604 struct program_space *pspace,
6605 CORE_ADDR pc, struct obj_section *section)
6606 {
6607 struct bp_location *bl = b->loc;
6608
6609 for (; bl; bl = bl->next)
6610 {
6611 if (bl->pspace == pspace
6612 && bl->address == pc
6613 && (!overlay_debugging || bl->section == section))
6614 return 1;
6615 }
6616 return 0;
6617 }
6618
6619 /* Print a message describing any user-breakpoints set at PC. This
6620 concerns with logical breakpoints, so we match program spaces, not
6621 address spaces. */
6622
6623 static void
6624 describe_other_breakpoints (struct gdbarch *gdbarch,
6625 struct program_space *pspace, CORE_ADDR pc,
6626 struct obj_section *section, int thread)
6627 {
6628 int others = 0;
6629 struct breakpoint *b;
6630
6631 ALL_BREAKPOINTS (b)
6632 others += (user_breakpoint_p (b)
6633 && breakpoint_has_pc (b, pspace, pc, section));
6634 if (others > 0)
6635 {
6636 if (others == 1)
6637 printf_filtered (_("Note: breakpoint "));
6638 else /* if (others == ???) */
6639 printf_filtered (_("Note: breakpoints "));
6640 ALL_BREAKPOINTS (b)
6641 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6642 {
6643 others--;
6644 printf_filtered ("%d", b->number);
6645 if (b->thread == -1 && thread != -1)
6646 printf_filtered (" (all threads)");
6647 else if (b->thread != -1)
6648 printf_filtered (" (thread %d)", b->thread);
6649 printf_filtered ("%s%s ",
6650 ((b->enable_state == bp_disabled
6651 || b->enable_state == bp_call_disabled)
6652 ? " (disabled)"
6653 : ""),
6654 (others > 1) ? ","
6655 : ((others == 1) ? " and" : ""));
6656 }
6657 current_uiout->message (_("also set at pc %ps.\n"),
6658 styled_string (address_style.style (),
6659 paddress (gdbarch, pc)));
6660 }
6661 }
6662 \f
6663
6664 /* Return true iff it is meaningful to use the address member of LOC.
6665 For some breakpoint types, the locations' address members are
6666 irrelevant and it makes no sense to attempt to compare them to
6667 other addresses (or use them for any other purpose either).
6668
6669 More specifically, software watchpoints and catchpoints that are
6670 not backed by breakpoints always have a zero valued location
6671 address and we don't want to mark breakpoints of any of these types
6672 to be a duplicate of an actual breakpoint location at address
6673 zero. */
6674
6675 static bool
6676 bl_address_is_meaningful (bp_location *loc)
6677 {
6678 return loc->loc_type != bp_loc_other;
6679 }
6680
6681 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6682 true if LOC1 and LOC2 represent the same watchpoint location. */
6683
6684 static int
6685 watchpoint_locations_match (struct bp_location *loc1,
6686 struct bp_location *loc2)
6687 {
6688 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6689 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6690
6691 /* Both of them must exist. */
6692 gdb_assert (w1 != NULL);
6693 gdb_assert (w2 != NULL);
6694
6695 /* If the target can evaluate the condition expression in hardware,
6696 then we we need to insert both watchpoints even if they are at
6697 the same place. Otherwise the watchpoint will only trigger when
6698 the condition of whichever watchpoint was inserted evaluates to
6699 true, not giving a chance for GDB to check the condition of the
6700 other watchpoint. */
6701 if ((w1->cond_exp
6702 && target_can_accel_watchpoint_condition (loc1->address,
6703 loc1->length,
6704 loc1->watchpoint_type,
6705 w1->cond_exp.get ()))
6706 || (w2->cond_exp
6707 && target_can_accel_watchpoint_condition (loc2->address,
6708 loc2->length,
6709 loc2->watchpoint_type,
6710 w2->cond_exp.get ())))
6711 return 0;
6712
6713 /* Note that this checks the owner's type, not the location's. In
6714 case the target does not support read watchpoints, but does
6715 support access watchpoints, we'll have bp_read_watchpoint
6716 watchpoints with hw_access locations. Those should be considered
6717 duplicates of hw_read locations. The hw_read locations will
6718 become hw_access locations later. */
6719 return (loc1->owner->type == loc2->owner->type
6720 && loc1->pspace->aspace == loc2->pspace->aspace
6721 && loc1->address == loc2->address
6722 && loc1->length == loc2->length);
6723 }
6724
6725 /* See breakpoint.h. */
6726
6727 int
6728 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6729 const address_space *aspace2, CORE_ADDR addr2)
6730 {
6731 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6732 || aspace1 == aspace2)
6733 && addr1 == addr2);
6734 }
6735
6736 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6737 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6738 matches ASPACE2. On targets that have global breakpoints, the address
6739 space doesn't really matter. */
6740
6741 static int
6742 breakpoint_address_match_range (const address_space *aspace1,
6743 CORE_ADDR addr1,
6744 int len1, const address_space *aspace2,
6745 CORE_ADDR addr2)
6746 {
6747 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6748 || aspace1 == aspace2)
6749 && addr2 >= addr1 && addr2 < addr1 + len1);
6750 }
6751
6752 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6753 a ranged breakpoint. In most targets, a match happens only if ASPACE
6754 matches the breakpoint's address space. On targets that have global
6755 breakpoints, the address space doesn't really matter. */
6756
6757 static int
6758 breakpoint_location_address_match (struct bp_location *bl,
6759 const address_space *aspace,
6760 CORE_ADDR addr)
6761 {
6762 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6763 aspace, addr)
6764 || (bl->length
6765 && breakpoint_address_match_range (bl->pspace->aspace,
6766 bl->address, bl->length,
6767 aspace, addr)));
6768 }
6769
6770 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6771 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6772 match happens only if ASPACE matches the breakpoint's address
6773 space. On targets that have global breakpoints, the address space
6774 doesn't really matter. */
6775
6776 static int
6777 breakpoint_location_address_range_overlap (struct bp_location *bl,
6778 const address_space *aspace,
6779 CORE_ADDR addr, int len)
6780 {
6781 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6782 || bl->pspace->aspace == aspace)
6783 {
6784 int bl_len = bl->length != 0 ? bl->length : 1;
6785
6786 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6787 return 1;
6788 }
6789 return 0;
6790 }
6791
6792 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6793 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6794 true, otherwise returns false. */
6795
6796 static int
6797 tracepoint_locations_match (struct bp_location *loc1,
6798 struct bp_location *loc2)
6799 {
6800 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6801 /* Since tracepoint locations are never duplicated with others', tracepoint
6802 locations at the same address of different tracepoints are regarded as
6803 different locations. */
6804 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6805 else
6806 return 0;
6807 }
6808
6809 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6810 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6811 the same location. */
6812
6813 static int
6814 breakpoint_locations_match (struct bp_location *loc1,
6815 struct bp_location *loc2)
6816 {
6817 int hw_point1, hw_point2;
6818
6819 /* Both of them must not be in moribund_locations. */
6820 gdb_assert (loc1->owner != NULL);
6821 gdb_assert (loc2->owner != NULL);
6822
6823 hw_point1 = is_hardware_watchpoint (loc1->owner);
6824 hw_point2 = is_hardware_watchpoint (loc2->owner);
6825
6826 if (hw_point1 != hw_point2)
6827 return 0;
6828 else if (hw_point1)
6829 return watchpoint_locations_match (loc1, loc2);
6830 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6831 return tracepoint_locations_match (loc1, loc2);
6832 else
6833 /* We compare bp_location.length in order to cover ranged breakpoints. */
6834 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6835 loc2->pspace->aspace, loc2->address)
6836 && loc1->length == loc2->length);
6837 }
6838
6839 static void
6840 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6841 int bnum, int have_bnum)
6842 {
6843 /* The longest string possibly returned by hex_string_custom
6844 is 50 chars. These must be at least that big for safety. */
6845 char astr1[64];
6846 char astr2[64];
6847
6848 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6849 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6850 if (have_bnum)
6851 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6852 bnum, astr1, astr2);
6853 else
6854 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6855 }
6856
6857 /* Adjust a breakpoint's address to account for architectural
6858 constraints on breakpoint placement. Return the adjusted address.
6859 Note: Very few targets require this kind of adjustment. For most
6860 targets, this function is simply the identity function. */
6861
6862 static CORE_ADDR
6863 adjust_breakpoint_address (struct gdbarch *gdbarch,
6864 CORE_ADDR bpaddr, enum bptype bptype)
6865 {
6866 if (bptype == bp_watchpoint
6867 || bptype == bp_hardware_watchpoint
6868 || bptype == bp_read_watchpoint
6869 || bptype == bp_access_watchpoint
6870 || bptype == bp_catchpoint)
6871 {
6872 /* Watchpoints and the various bp_catch_* eventpoints should not
6873 have their addresses modified. */
6874 return bpaddr;
6875 }
6876 else if (bptype == bp_single_step)
6877 {
6878 /* Single-step breakpoints should not have their addresses
6879 modified. If there's any architectural constrain that
6880 applies to this address, then it should have already been
6881 taken into account when the breakpoint was created in the
6882 first place. If we didn't do this, stepping through e.g.,
6883 Thumb-2 IT blocks would break. */
6884 return bpaddr;
6885 }
6886 else
6887 {
6888 CORE_ADDR adjusted_bpaddr = bpaddr;
6889
6890 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6891 {
6892 /* Some targets have architectural constraints on the placement
6893 of breakpoint instructions. Obtain the adjusted address. */
6894 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6895 }
6896
6897 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
6898
6899 /* An adjusted breakpoint address can significantly alter
6900 a user's expectations. Print a warning if an adjustment
6901 is required. */
6902 if (adjusted_bpaddr != bpaddr)
6903 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6904
6905 return adjusted_bpaddr;
6906 }
6907 }
6908
6909 static bp_loc_type
6910 bp_location_from_bp_type (bptype type)
6911 {
6912 switch (type)
6913 {
6914 case bp_breakpoint:
6915 case bp_single_step:
6916 case bp_until:
6917 case bp_finish:
6918 case bp_longjmp:
6919 case bp_longjmp_resume:
6920 case bp_longjmp_call_dummy:
6921 case bp_exception:
6922 case bp_exception_resume:
6923 case bp_step_resume:
6924 case bp_hp_step_resume:
6925 case bp_watchpoint_scope:
6926 case bp_call_dummy:
6927 case bp_std_terminate:
6928 case bp_shlib_event:
6929 case bp_thread_event:
6930 case bp_overlay_event:
6931 case bp_jit_event:
6932 case bp_longjmp_master:
6933 case bp_std_terminate_master:
6934 case bp_exception_master:
6935 case bp_gnu_ifunc_resolver:
6936 case bp_gnu_ifunc_resolver_return:
6937 case bp_dprintf:
6938 return bp_loc_software_breakpoint;
6939 case bp_hardware_breakpoint:
6940 return bp_loc_hardware_breakpoint;
6941 case bp_hardware_watchpoint:
6942 case bp_read_watchpoint:
6943 case bp_access_watchpoint:
6944 return bp_loc_hardware_watchpoint;
6945 case bp_watchpoint:
6946 case bp_catchpoint:
6947 case bp_tracepoint:
6948 case bp_fast_tracepoint:
6949 case bp_static_tracepoint:
6950 return bp_loc_other;
6951 default:
6952 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6953 }
6954 }
6955
6956 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6957 {
6958 this->owner = owner;
6959 this->cond_bytecode = NULL;
6960 this->shlib_disabled = 0;
6961 this->enabled = 1;
6962
6963 this->loc_type = type;
6964
6965 if (this->loc_type == bp_loc_software_breakpoint
6966 || this->loc_type == bp_loc_hardware_breakpoint)
6967 mark_breakpoint_location_modified (this);
6968
6969 this->refc = 1;
6970 }
6971
6972 bp_location::bp_location (breakpoint *owner)
6973 : bp_location::bp_location (owner,
6974 bp_location_from_bp_type (owner->type))
6975 {
6976 }
6977
6978 /* Allocate a struct bp_location. */
6979
6980 static struct bp_location *
6981 allocate_bp_location (struct breakpoint *bpt)
6982 {
6983 return bpt->ops->allocate_location (bpt);
6984 }
6985
6986 static void
6987 free_bp_location (struct bp_location *loc)
6988 {
6989 delete loc;
6990 }
6991
6992 /* Increment reference count. */
6993
6994 static void
6995 incref_bp_location (struct bp_location *bl)
6996 {
6997 ++bl->refc;
6998 }
6999
7000 /* Decrement reference count. If the reference count reaches 0,
7001 destroy the bp_location. Sets *BLP to NULL. */
7002
7003 static void
7004 decref_bp_location (struct bp_location **blp)
7005 {
7006 gdb_assert ((*blp)->refc > 0);
7007
7008 if (--(*blp)->refc == 0)
7009 free_bp_location (*blp);
7010 *blp = NULL;
7011 }
7012
7013 /* Add breakpoint B at the end of the global breakpoint chain. */
7014
7015 static breakpoint *
7016 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7017 {
7018 struct breakpoint *b1;
7019 struct breakpoint *result = b.get ();
7020
7021 /* Add this breakpoint to the end of the chain so that a list of
7022 breakpoints will come out in order of increasing numbers. */
7023
7024 b1 = breakpoint_chain;
7025 if (b1 == 0)
7026 breakpoint_chain = b.release ();
7027 else
7028 {
7029 while (b1->next)
7030 b1 = b1->next;
7031 b1->next = b.release ();
7032 }
7033
7034 return result;
7035 }
7036
7037 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7038
7039 static void
7040 init_raw_breakpoint_without_location (struct breakpoint *b,
7041 struct gdbarch *gdbarch,
7042 enum bptype bptype,
7043 const struct breakpoint_ops *ops)
7044 {
7045 gdb_assert (ops != NULL);
7046
7047 b->ops = ops;
7048 b->type = bptype;
7049 b->gdbarch = gdbarch;
7050 b->language = current_language->la_language;
7051 b->input_radix = input_radix;
7052 b->related_breakpoint = b;
7053 }
7054
7055 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7056 that has type BPTYPE and has no locations as yet. */
7057
7058 static struct breakpoint *
7059 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7060 enum bptype bptype,
7061 const struct breakpoint_ops *ops)
7062 {
7063 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7064
7065 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7066 return add_to_breakpoint_chain (std::move (b));
7067 }
7068
7069 /* Initialize loc->function_name. */
7070
7071 static void
7072 set_breakpoint_location_function (struct bp_location *loc)
7073 {
7074 gdb_assert (loc->owner != NULL);
7075
7076 if (loc->owner->type == bp_breakpoint
7077 || loc->owner->type == bp_hardware_breakpoint
7078 || is_tracepoint (loc->owner))
7079 {
7080 const char *function_name;
7081
7082 if (loc->msymbol != NULL
7083 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7084 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7085 {
7086 struct breakpoint *b = loc->owner;
7087
7088 function_name = MSYMBOL_LINKAGE_NAME (loc->msymbol);
7089
7090 if (b->type == bp_breakpoint && b->loc == loc
7091 && loc->next == NULL && b->related_breakpoint == b)
7092 {
7093 /* Create only the whole new breakpoint of this type but do not
7094 mess more complicated breakpoints with multiple locations. */
7095 b->type = bp_gnu_ifunc_resolver;
7096 /* Remember the resolver's address for use by the return
7097 breakpoint. */
7098 loc->related_address = loc->address;
7099 }
7100 }
7101 else
7102 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7103
7104 if (function_name)
7105 loc->function_name = xstrdup (function_name);
7106 }
7107 }
7108
7109 /* Attempt to determine architecture of location identified by SAL. */
7110 struct gdbarch *
7111 get_sal_arch (struct symtab_and_line sal)
7112 {
7113 if (sal.section)
7114 return get_objfile_arch (sal.section->objfile);
7115 if (sal.symtab)
7116 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7117
7118 return NULL;
7119 }
7120
7121 /* Low level routine for partially initializing a breakpoint of type
7122 BPTYPE. The newly created breakpoint's address, section, source
7123 file name, and line number are provided by SAL.
7124
7125 It is expected that the caller will complete the initialization of
7126 the newly created breakpoint struct as well as output any status
7127 information regarding the creation of a new breakpoint. */
7128
7129 static void
7130 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7131 struct symtab_and_line sal, enum bptype bptype,
7132 const struct breakpoint_ops *ops)
7133 {
7134 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7135
7136 add_location_to_breakpoint (b, &sal);
7137
7138 if (bptype != bp_catchpoint)
7139 gdb_assert (sal.pspace != NULL);
7140
7141 /* Store the program space that was used to set the breakpoint,
7142 except for ordinary breakpoints, which are independent of the
7143 program space. */
7144 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7145 b->pspace = sal.pspace;
7146 }
7147
7148 /* set_raw_breakpoint is a low level routine for allocating and
7149 partially initializing a breakpoint of type BPTYPE. The newly
7150 created breakpoint's address, section, source file name, and line
7151 number are provided by SAL. The newly created and partially
7152 initialized breakpoint is added to the breakpoint chain and
7153 is also returned as the value of this function.
7154
7155 It is expected that the caller will complete the initialization of
7156 the newly created breakpoint struct as well as output any status
7157 information regarding the creation of a new breakpoint. In
7158 particular, set_raw_breakpoint does NOT set the breakpoint
7159 number! Care should be taken to not allow an error to occur
7160 prior to completing the initialization of the breakpoint. If this
7161 should happen, a bogus breakpoint will be left on the chain. */
7162
7163 struct breakpoint *
7164 set_raw_breakpoint (struct gdbarch *gdbarch,
7165 struct symtab_and_line sal, enum bptype bptype,
7166 const struct breakpoint_ops *ops)
7167 {
7168 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7169
7170 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7171 return add_to_breakpoint_chain (std::move (b));
7172 }
7173
7174 /* Call this routine when stepping and nexting to enable a breakpoint
7175 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7176 initiated the operation. */
7177
7178 void
7179 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7180 {
7181 struct breakpoint *b, *b_tmp;
7182 int thread = tp->global_num;
7183
7184 /* To avoid having to rescan all objfile symbols at every step,
7185 we maintain a list of continually-inserted but always disabled
7186 longjmp "master" breakpoints. Here, we simply create momentary
7187 clones of those and enable them for the requested thread. */
7188 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7189 if (b->pspace == current_program_space
7190 && (b->type == bp_longjmp_master
7191 || b->type == bp_exception_master))
7192 {
7193 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7194 struct breakpoint *clone;
7195
7196 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7197 after their removal. */
7198 clone = momentary_breakpoint_from_master (b, type,
7199 &momentary_breakpoint_ops, 1);
7200 clone->thread = thread;
7201 }
7202
7203 tp->initiating_frame = frame;
7204 }
7205
7206 /* Delete all longjmp breakpoints from THREAD. */
7207 void
7208 delete_longjmp_breakpoint (int thread)
7209 {
7210 struct breakpoint *b, *b_tmp;
7211
7212 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7213 if (b->type == bp_longjmp || b->type == bp_exception)
7214 {
7215 if (b->thread == thread)
7216 delete_breakpoint (b);
7217 }
7218 }
7219
7220 void
7221 delete_longjmp_breakpoint_at_next_stop (int thread)
7222 {
7223 struct breakpoint *b, *b_tmp;
7224
7225 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7226 if (b->type == bp_longjmp || b->type == bp_exception)
7227 {
7228 if (b->thread == thread)
7229 b->disposition = disp_del_at_next_stop;
7230 }
7231 }
7232
7233 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7234 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7235 pointer to any of them. Return NULL if this system cannot place longjmp
7236 breakpoints. */
7237
7238 struct breakpoint *
7239 set_longjmp_breakpoint_for_call_dummy (void)
7240 {
7241 struct breakpoint *b, *retval = NULL;
7242
7243 ALL_BREAKPOINTS (b)
7244 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7245 {
7246 struct breakpoint *new_b;
7247
7248 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7249 &momentary_breakpoint_ops,
7250 1);
7251 new_b->thread = inferior_thread ()->global_num;
7252
7253 /* Link NEW_B into the chain of RETVAL breakpoints. */
7254
7255 gdb_assert (new_b->related_breakpoint == new_b);
7256 if (retval == NULL)
7257 retval = new_b;
7258 new_b->related_breakpoint = retval;
7259 while (retval->related_breakpoint != new_b->related_breakpoint)
7260 retval = retval->related_breakpoint;
7261 retval->related_breakpoint = new_b;
7262 }
7263
7264 return retval;
7265 }
7266
7267 /* Verify all existing dummy frames and their associated breakpoints for
7268 TP. Remove those which can no longer be found in the current frame
7269 stack.
7270
7271 You should call this function only at places where it is safe to currently
7272 unwind the whole stack. Failed stack unwind would discard live dummy
7273 frames. */
7274
7275 void
7276 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7277 {
7278 struct breakpoint *b, *b_tmp;
7279
7280 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7281 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7282 {
7283 struct breakpoint *dummy_b = b->related_breakpoint;
7284
7285 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7286 dummy_b = dummy_b->related_breakpoint;
7287 if (dummy_b->type != bp_call_dummy
7288 || frame_find_by_id (dummy_b->frame_id) != NULL)
7289 continue;
7290
7291 dummy_frame_discard (dummy_b->frame_id, tp);
7292
7293 while (b->related_breakpoint != b)
7294 {
7295 if (b_tmp == b->related_breakpoint)
7296 b_tmp = b->related_breakpoint->next;
7297 delete_breakpoint (b->related_breakpoint);
7298 }
7299 delete_breakpoint (b);
7300 }
7301 }
7302
7303 void
7304 enable_overlay_breakpoints (void)
7305 {
7306 struct breakpoint *b;
7307
7308 ALL_BREAKPOINTS (b)
7309 if (b->type == bp_overlay_event)
7310 {
7311 b->enable_state = bp_enabled;
7312 update_global_location_list (UGLL_MAY_INSERT);
7313 overlay_events_enabled = 1;
7314 }
7315 }
7316
7317 void
7318 disable_overlay_breakpoints (void)
7319 {
7320 struct breakpoint *b;
7321
7322 ALL_BREAKPOINTS (b)
7323 if (b->type == bp_overlay_event)
7324 {
7325 b->enable_state = bp_disabled;
7326 update_global_location_list (UGLL_DONT_INSERT);
7327 overlay_events_enabled = 0;
7328 }
7329 }
7330
7331 /* Set an active std::terminate breakpoint for each std::terminate
7332 master breakpoint. */
7333 void
7334 set_std_terminate_breakpoint (void)
7335 {
7336 struct breakpoint *b, *b_tmp;
7337
7338 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7339 if (b->pspace == current_program_space
7340 && b->type == bp_std_terminate_master)
7341 {
7342 momentary_breakpoint_from_master (b, bp_std_terminate,
7343 &momentary_breakpoint_ops, 1);
7344 }
7345 }
7346
7347 /* Delete all the std::terminate breakpoints. */
7348 void
7349 delete_std_terminate_breakpoint (void)
7350 {
7351 struct breakpoint *b, *b_tmp;
7352
7353 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7354 if (b->type == bp_std_terminate)
7355 delete_breakpoint (b);
7356 }
7357
7358 struct breakpoint *
7359 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7360 {
7361 struct breakpoint *b;
7362
7363 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7364 &internal_breakpoint_ops);
7365
7366 b->enable_state = bp_enabled;
7367 /* location has to be used or breakpoint_re_set will delete me. */
7368 b->location = new_address_location (b->loc->address, NULL, 0);
7369
7370 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7371
7372 return b;
7373 }
7374
7375 struct lang_and_radix
7376 {
7377 enum language lang;
7378 int radix;
7379 };
7380
7381 /* Create a breakpoint for JIT code registration and unregistration. */
7382
7383 struct breakpoint *
7384 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7385 {
7386 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7387 &internal_breakpoint_ops);
7388 }
7389
7390 /* Remove JIT code registration and unregistration breakpoint(s). */
7391
7392 void
7393 remove_jit_event_breakpoints (void)
7394 {
7395 struct breakpoint *b, *b_tmp;
7396
7397 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7398 if (b->type == bp_jit_event
7399 && b->loc->pspace == current_program_space)
7400 delete_breakpoint (b);
7401 }
7402
7403 void
7404 remove_solib_event_breakpoints (void)
7405 {
7406 struct breakpoint *b, *b_tmp;
7407
7408 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7409 if (b->type == bp_shlib_event
7410 && b->loc->pspace == current_program_space)
7411 delete_breakpoint (b);
7412 }
7413
7414 /* See breakpoint.h. */
7415
7416 void
7417 remove_solib_event_breakpoints_at_next_stop (void)
7418 {
7419 struct breakpoint *b, *b_tmp;
7420
7421 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7422 if (b->type == bp_shlib_event
7423 && b->loc->pspace == current_program_space)
7424 b->disposition = disp_del_at_next_stop;
7425 }
7426
7427 /* Helper for create_solib_event_breakpoint /
7428 create_and_insert_solib_event_breakpoint. Allows specifying which
7429 INSERT_MODE to pass through to update_global_location_list. */
7430
7431 static struct breakpoint *
7432 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7433 enum ugll_insert_mode insert_mode)
7434 {
7435 struct breakpoint *b;
7436
7437 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7438 &internal_breakpoint_ops);
7439 update_global_location_list_nothrow (insert_mode);
7440 return b;
7441 }
7442
7443 struct breakpoint *
7444 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7445 {
7446 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7447 }
7448
7449 /* See breakpoint.h. */
7450
7451 struct breakpoint *
7452 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7453 {
7454 struct breakpoint *b;
7455
7456 /* Explicitly tell update_global_location_list to insert
7457 locations. */
7458 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7459 if (!b->loc->inserted)
7460 {
7461 delete_breakpoint (b);
7462 return NULL;
7463 }
7464 return b;
7465 }
7466
7467 /* Disable any breakpoints that are on code in shared libraries. Only
7468 apply to enabled breakpoints, disabled ones can just stay disabled. */
7469
7470 void
7471 disable_breakpoints_in_shlibs (void)
7472 {
7473 struct bp_location *loc, **locp_tmp;
7474
7475 ALL_BP_LOCATIONS (loc, locp_tmp)
7476 {
7477 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7478 struct breakpoint *b = loc->owner;
7479
7480 /* We apply the check to all breakpoints, including disabled for
7481 those with loc->duplicate set. This is so that when breakpoint
7482 becomes enabled, or the duplicate is removed, gdb will try to
7483 insert all breakpoints. If we don't set shlib_disabled here,
7484 we'll try to insert those breakpoints and fail. */
7485 if (((b->type == bp_breakpoint)
7486 || (b->type == bp_jit_event)
7487 || (b->type == bp_hardware_breakpoint)
7488 || (is_tracepoint (b)))
7489 && loc->pspace == current_program_space
7490 && !loc->shlib_disabled
7491 && solib_name_from_address (loc->pspace, loc->address)
7492 )
7493 {
7494 loc->shlib_disabled = 1;
7495 }
7496 }
7497 }
7498
7499 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7500 notification of unloaded_shlib. Only apply to enabled breakpoints,
7501 disabled ones can just stay disabled. */
7502
7503 static void
7504 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7505 {
7506 struct bp_location *loc, **locp_tmp;
7507 int disabled_shlib_breaks = 0;
7508
7509 ALL_BP_LOCATIONS (loc, locp_tmp)
7510 {
7511 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7512 struct breakpoint *b = loc->owner;
7513
7514 if (solib->pspace == loc->pspace
7515 && !loc->shlib_disabled
7516 && (((b->type == bp_breakpoint
7517 || b->type == bp_jit_event
7518 || b->type == bp_hardware_breakpoint)
7519 && (loc->loc_type == bp_loc_hardware_breakpoint
7520 || loc->loc_type == bp_loc_software_breakpoint))
7521 || is_tracepoint (b))
7522 && solib_contains_address_p (solib, loc->address))
7523 {
7524 loc->shlib_disabled = 1;
7525 /* At this point, we cannot rely on remove_breakpoint
7526 succeeding so we must mark the breakpoint as not inserted
7527 to prevent future errors occurring in remove_breakpoints. */
7528 loc->inserted = 0;
7529
7530 /* This may cause duplicate notifications for the same breakpoint. */
7531 gdb::observers::breakpoint_modified.notify (b);
7532
7533 if (!disabled_shlib_breaks)
7534 {
7535 target_terminal::ours_for_output ();
7536 warning (_("Temporarily disabling breakpoints "
7537 "for unloaded shared library \"%s\""),
7538 solib->so_name);
7539 }
7540 disabled_shlib_breaks = 1;
7541 }
7542 }
7543 }
7544
7545 /* Disable any breakpoints and tracepoints in OBJFILE upon
7546 notification of free_objfile. Only apply to enabled breakpoints,
7547 disabled ones can just stay disabled. */
7548
7549 static void
7550 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7551 {
7552 struct breakpoint *b;
7553
7554 if (objfile == NULL)
7555 return;
7556
7557 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7558 managed by the user with add-symbol-file/remove-symbol-file.
7559 Similarly to how breakpoints in shared libraries are handled in
7560 response to "nosharedlibrary", mark breakpoints in such modules
7561 shlib_disabled so they end up uninserted on the next global
7562 location list update. Shared libraries not loaded by the user
7563 aren't handled here -- they're already handled in
7564 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7565 solib_unloaded observer. We skip objfiles that are not
7566 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7567 main objfile). */
7568 if ((objfile->flags & OBJF_SHARED) == 0
7569 || (objfile->flags & OBJF_USERLOADED) == 0)
7570 return;
7571
7572 ALL_BREAKPOINTS (b)
7573 {
7574 struct bp_location *loc;
7575 int bp_modified = 0;
7576
7577 if (!is_breakpoint (b) && !is_tracepoint (b))
7578 continue;
7579
7580 for (loc = b->loc; loc != NULL; loc = loc->next)
7581 {
7582 CORE_ADDR loc_addr = loc->address;
7583
7584 if (loc->loc_type != bp_loc_hardware_breakpoint
7585 && loc->loc_type != bp_loc_software_breakpoint)
7586 continue;
7587
7588 if (loc->shlib_disabled != 0)
7589 continue;
7590
7591 if (objfile->pspace != loc->pspace)
7592 continue;
7593
7594 if (loc->loc_type != bp_loc_hardware_breakpoint
7595 && loc->loc_type != bp_loc_software_breakpoint)
7596 continue;
7597
7598 if (is_addr_in_objfile (loc_addr, objfile))
7599 {
7600 loc->shlib_disabled = 1;
7601 /* At this point, we don't know whether the object was
7602 unmapped from the inferior or not, so leave the
7603 inserted flag alone. We'll handle failure to
7604 uninsert quietly, in case the object was indeed
7605 unmapped. */
7606
7607 mark_breakpoint_location_modified (loc);
7608
7609 bp_modified = 1;
7610 }
7611 }
7612
7613 if (bp_modified)
7614 gdb::observers::breakpoint_modified.notify (b);
7615 }
7616 }
7617
7618 /* FORK & VFORK catchpoints. */
7619
7620 /* An instance of this type is used to represent a fork or vfork
7621 catchpoint. A breakpoint is really of this type iff its ops pointer points
7622 to CATCH_FORK_BREAKPOINT_OPS. */
7623
7624 struct fork_catchpoint : public breakpoint
7625 {
7626 /* Process id of a child process whose forking triggered this
7627 catchpoint. This field is only valid immediately after this
7628 catchpoint has triggered. */
7629 ptid_t forked_inferior_pid;
7630 };
7631
7632 /* Implement the "insert" breakpoint_ops method for fork
7633 catchpoints. */
7634
7635 static int
7636 insert_catch_fork (struct bp_location *bl)
7637 {
7638 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7639 }
7640
7641 /* Implement the "remove" breakpoint_ops method for fork
7642 catchpoints. */
7643
7644 static int
7645 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7646 {
7647 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7648 }
7649
7650 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7651 catchpoints. */
7652
7653 static int
7654 breakpoint_hit_catch_fork (const struct bp_location *bl,
7655 const address_space *aspace, CORE_ADDR bp_addr,
7656 const struct target_waitstatus *ws)
7657 {
7658 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7659
7660 if (ws->kind != TARGET_WAITKIND_FORKED)
7661 return 0;
7662
7663 c->forked_inferior_pid = ws->value.related_pid;
7664 return 1;
7665 }
7666
7667 /* Implement the "print_it" breakpoint_ops method for fork
7668 catchpoints. */
7669
7670 static enum print_stop_action
7671 print_it_catch_fork (bpstat bs)
7672 {
7673 struct ui_out *uiout = current_uiout;
7674 struct breakpoint *b = bs->breakpoint_at;
7675 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7676
7677 annotate_catchpoint (b->number);
7678 maybe_print_thread_hit_breakpoint (uiout);
7679 if (b->disposition == disp_del)
7680 uiout->text ("Temporary catchpoint ");
7681 else
7682 uiout->text ("Catchpoint ");
7683 if (uiout->is_mi_like_p ())
7684 {
7685 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7686 uiout->field_string ("disp", bpdisp_text (b->disposition));
7687 }
7688 uiout->field_signed ("bkptno", b->number);
7689 uiout->text (" (forked process ");
7690 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7691 uiout->text ("), ");
7692 return PRINT_SRC_AND_LOC;
7693 }
7694
7695 /* Implement the "print_one" breakpoint_ops method for fork
7696 catchpoints. */
7697
7698 static void
7699 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7700 {
7701 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7702 struct value_print_options opts;
7703 struct ui_out *uiout = current_uiout;
7704
7705 get_user_print_options (&opts);
7706
7707 /* Field 4, the address, is omitted (which makes the columns not
7708 line up too nicely with the headers, but the effect is relatively
7709 readable). */
7710 if (opts.addressprint)
7711 uiout->field_skip ("addr");
7712 annotate_field (5);
7713 uiout->text ("fork");
7714 if (c->forked_inferior_pid != null_ptid)
7715 {
7716 uiout->text (", process ");
7717 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7718 uiout->spaces (1);
7719 }
7720
7721 if (uiout->is_mi_like_p ())
7722 uiout->field_string ("catch-type", "fork");
7723 }
7724
7725 /* Implement the "print_mention" breakpoint_ops method for fork
7726 catchpoints. */
7727
7728 static void
7729 print_mention_catch_fork (struct breakpoint *b)
7730 {
7731 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7732 }
7733
7734 /* Implement the "print_recreate" breakpoint_ops method for fork
7735 catchpoints. */
7736
7737 static void
7738 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7739 {
7740 fprintf_unfiltered (fp, "catch fork");
7741 print_recreate_thread (b, fp);
7742 }
7743
7744 /* The breakpoint_ops structure to be used in fork catchpoints. */
7745
7746 static struct breakpoint_ops catch_fork_breakpoint_ops;
7747
7748 /* Implement the "insert" breakpoint_ops method for vfork
7749 catchpoints. */
7750
7751 static int
7752 insert_catch_vfork (struct bp_location *bl)
7753 {
7754 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7755 }
7756
7757 /* Implement the "remove" breakpoint_ops method for vfork
7758 catchpoints. */
7759
7760 static int
7761 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7762 {
7763 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7764 }
7765
7766 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7767 catchpoints. */
7768
7769 static int
7770 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7771 const address_space *aspace, CORE_ADDR bp_addr,
7772 const struct target_waitstatus *ws)
7773 {
7774 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7775
7776 if (ws->kind != TARGET_WAITKIND_VFORKED)
7777 return 0;
7778
7779 c->forked_inferior_pid = ws->value.related_pid;
7780 return 1;
7781 }
7782
7783 /* Implement the "print_it" breakpoint_ops method for vfork
7784 catchpoints. */
7785
7786 static enum print_stop_action
7787 print_it_catch_vfork (bpstat bs)
7788 {
7789 struct ui_out *uiout = current_uiout;
7790 struct breakpoint *b = bs->breakpoint_at;
7791 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7792
7793 annotate_catchpoint (b->number);
7794 maybe_print_thread_hit_breakpoint (uiout);
7795 if (b->disposition == disp_del)
7796 uiout->text ("Temporary catchpoint ");
7797 else
7798 uiout->text ("Catchpoint ");
7799 if (uiout->is_mi_like_p ())
7800 {
7801 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7802 uiout->field_string ("disp", bpdisp_text (b->disposition));
7803 }
7804 uiout->field_signed ("bkptno", b->number);
7805 uiout->text (" (vforked process ");
7806 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7807 uiout->text ("), ");
7808 return PRINT_SRC_AND_LOC;
7809 }
7810
7811 /* Implement the "print_one" breakpoint_ops method for vfork
7812 catchpoints. */
7813
7814 static void
7815 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7816 {
7817 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7818 struct value_print_options opts;
7819 struct ui_out *uiout = current_uiout;
7820
7821 get_user_print_options (&opts);
7822 /* Field 4, the address, is omitted (which makes the columns not
7823 line up too nicely with the headers, but the effect is relatively
7824 readable). */
7825 if (opts.addressprint)
7826 uiout->field_skip ("addr");
7827 annotate_field (5);
7828 uiout->text ("vfork");
7829 if (c->forked_inferior_pid != null_ptid)
7830 {
7831 uiout->text (", process ");
7832 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7833 uiout->spaces (1);
7834 }
7835
7836 if (uiout->is_mi_like_p ())
7837 uiout->field_string ("catch-type", "vfork");
7838 }
7839
7840 /* Implement the "print_mention" breakpoint_ops method for vfork
7841 catchpoints. */
7842
7843 static void
7844 print_mention_catch_vfork (struct breakpoint *b)
7845 {
7846 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7847 }
7848
7849 /* Implement the "print_recreate" breakpoint_ops method for vfork
7850 catchpoints. */
7851
7852 static void
7853 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7854 {
7855 fprintf_unfiltered (fp, "catch vfork");
7856 print_recreate_thread (b, fp);
7857 }
7858
7859 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7860
7861 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7862
7863 /* An instance of this type is used to represent an solib catchpoint.
7864 A breakpoint is really of this type iff its ops pointer points to
7865 CATCH_SOLIB_BREAKPOINT_OPS. */
7866
7867 struct solib_catchpoint : public breakpoint
7868 {
7869 ~solib_catchpoint () override;
7870
7871 /* True for "catch load", false for "catch unload". */
7872 unsigned char is_load;
7873
7874 /* Regular expression to match, if any. COMPILED is only valid when
7875 REGEX is non-NULL. */
7876 char *regex;
7877 std::unique_ptr<compiled_regex> compiled;
7878 };
7879
7880 solib_catchpoint::~solib_catchpoint ()
7881 {
7882 xfree (this->regex);
7883 }
7884
7885 static int
7886 insert_catch_solib (struct bp_location *ignore)
7887 {
7888 return 0;
7889 }
7890
7891 static int
7892 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7893 {
7894 return 0;
7895 }
7896
7897 static int
7898 breakpoint_hit_catch_solib (const struct bp_location *bl,
7899 const address_space *aspace,
7900 CORE_ADDR bp_addr,
7901 const struct target_waitstatus *ws)
7902 {
7903 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7904 struct breakpoint *other;
7905
7906 if (ws->kind == TARGET_WAITKIND_LOADED)
7907 return 1;
7908
7909 ALL_BREAKPOINTS (other)
7910 {
7911 struct bp_location *other_bl;
7912
7913 if (other == bl->owner)
7914 continue;
7915
7916 if (other->type != bp_shlib_event)
7917 continue;
7918
7919 if (self->pspace != NULL && other->pspace != self->pspace)
7920 continue;
7921
7922 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7923 {
7924 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7925 return 1;
7926 }
7927 }
7928
7929 return 0;
7930 }
7931
7932 static void
7933 check_status_catch_solib (struct bpstats *bs)
7934 {
7935 struct solib_catchpoint *self
7936 = (struct solib_catchpoint *) bs->breakpoint_at;
7937
7938 if (self->is_load)
7939 {
7940 for (so_list *iter : current_program_space->added_solibs)
7941 {
7942 if (!self->regex
7943 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
7944 return;
7945 }
7946 }
7947 else
7948 {
7949 for (const std::string &iter : current_program_space->deleted_solibs)
7950 {
7951 if (!self->regex
7952 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
7953 return;
7954 }
7955 }
7956
7957 bs->stop = 0;
7958 bs->print_it = print_it_noop;
7959 }
7960
7961 static enum print_stop_action
7962 print_it_catch_solib (bpstat bs)
7963 {
7964 struct breakpoint *b = bs->breakpoint_at;
7965 struct ui_out *uiout = current_uiout;
7966
7967 annotate_catchpoint (b->number);
7968 maybe_print_thread_hit_breakpoint (uiout);
7969 if (b->disposition == disp_del)
7970 uiout->text ("Temporary catchpoint ");
7971 else
7972 uiout->text ("Catchpoint ");
7973 uiout->field_signed ("bkptno", b->number);
7974 uiout->text ("\n");
7975 if (uiout->is_mi_like_p ())
7976 uiout->field_string ("disp", bpdisp_text (b->disposition));
7977 print_solib_event (1);
7978 return PRINT_SRC_AND_LOC;
7979 }
7980
7981 static void
7982 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7983 {
7984 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7985 struct value_print_options opts;
7986 struct ui_out *uiout = current_uiout;
7987
7988 get_user_print_options (&opts);
7989 /* Field 4, the address, is omitted (which makes the columns not
7990 line up too nicely with the headers, but the effect is relatively
7991 readable). */
7992 if (opts.addressprint)
7993 {
7994 annotate_field (4);
7995 uiout->field_skip ("addr");
7996 }
7997
7998 std::string msg;
7999 annotate_field (5);
8000 if (self->is_load)
8001 {
8002 if (self->regex)
8003 msg = string_printf (_("load of library matching %s"), self->regex);
8004 else
8005 msg = _("load of library");
8006 }
8007 else
8008 {
8009 if (self->regex)
8010 msg = string_printf (_("unload of library matching %s"), self->regex);
8011 else
8012 msg = _("unload of library");
8013 }
8014 uiout->field_string ("what", msg);
8015
8016 if (uiout->is_mi_like_p ())
8017 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8018 }
8019
8020 static void
8021 print_mention_catch_solib (struct breakpoint *b)
8022 {
8023 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8024
8025 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8026 self->is_load ? "load" : "unload");
8027 }
8028
8029 static void
8030 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8031 {
8032 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8033
8034 fprintf_unfiltered (fp, "%s %s",
8035 b->disposition == disp_del ? "tcatch" : "catch",
8036 self->is_load ? "load" : "unload");
8037 if (self->regex)
8038 fprintf_unfiltered (fp, " %s", self->regex);
8039 fprintf_unfiltered (fp, "\n");
8040 }
8041
8042 static struct breakpoint_ops catch_solib_breakpoint_ops;
8043
8044 /* Shared helper function (MI and CLI) for creating and installing
8045 a shared object event catchpoint. If IS_LOAD is non-zero then
8046 the events to be caught are load events, otherwise they are
8047 unload events. If IS_TEMP is non-zero the catchpoint is a
8048 temporary one. If ENABLED is non-zero the catchpoint is
8049 created in an enabled state. */
8050
8051 void
8052 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8053 {
8054 struct gdbarch *gdbarch = get_current_arch ();
8055
8056 if (!arg)
8057 arg = "";
8058 arg = skip_spaces (arg);
8059
8060 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8061
8062 if (*arg != '\0')
8063 {
8064 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8065 _("Invalid regexp")));
8066 c->regex = xstrdup (arg);
8067 }
8068
8069 c->is_load = is_load;
8070 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8071 &catch_solib_breakpoint_ops);
8072
8073 c->enable_state = enabled ? bp_enabled : bp_disabled;
8074
8075 install_breakpoint (0, std::move (c), 1);
8076 }
8077
8078 /* A helper function that does all the work for "catch load" and
8079 "catch unload". */
8080
8081 static void
8082 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8083 struct cmd_list_element *command)
8084 {
8085 int tempflag;
8086 const int enabled = 1;
8087
8088 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8089
8090 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8091 }
8092
8093 static void
8094 catch_load_command_1 (const char *arg, int from_tty,
8095 struct cmd_list_element *command)
8096 {
8097 catch_load_or_unload (arg, from_tty, 1, command);
8098 }
8099
8100 static void
8101 catch_unload_command_1 (const char *arg, int from_tty,
8102 struct cmd_list_element *command)
8103 {
8104 catch_load_or_unload (arg, from_tty, 0, command);
8105 }
8106
8107 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8108 is non-zero, then make the breakpoint temporary. If COND_STRING is
8109 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8110 the breakpoint_ops structure associated to the catchpoint. */
8111
8112 void
8113 init_catchpoint (struct breakpoint *b,
8114 struct gdbarch *gdbarch, int tempflag,
8115 const char *cond_string,
8116 const struct breakpoint_ops *ops)
8117 {
8118 symtab_and_line sal;
8119 sal.pspace = current_program_space;
8120
8121 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8122
8123 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8124 b->disposition = tempflag ? disp_del : disp_donttouch;
8125 }
8126
8127 void
8128 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8129 {
8130 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8131 set_breakpoint_number (internal, b);
8132 if (is_tracepoint (b))
8133 set_tracepoint_count (breakpoint_count);
8134 if (!internal)
8135 mention (b);
8136 gdb::observers::breakpoint_created.notify (b);
8137
8138 if (update_gll)
8139 update_global_location_list (UGLL_MAY_INSERT);
8140 }
8141
8142 static void
8143 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8144 int tempflag, const char *cond_string,
8145 const struct breakpoint_ops *ops)
8146 {
8147 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8148
8149 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8150
8151 c->forked_inferior_pid = null_ptid;
8152
8153 install_breakpoint (0, std::move (c), 1);
8154 }
8155
8156 /* Exec catchpoints. */
8157
8158 /* An instance of this type is used to represent an exec catchpoint.
8159 A breakpoint is really of this type iff its ops pointer points to
8160 CATCH_EXEC_BREAKPOINT_OPS. */
8161
8162 struct exec_catchpoint : public breakpoint
8163 {
8164 ~exec_catchpoint () override;
8165
8166 /* Filename of a program whose exec triggered this catchpoint.
8167 This field is only valid immediately after this catchpoint has
8168 triggered. */
8169 char *exec_pathname;
8170 };
8171
8172 /* Exec catchpoint destructor. */
8173
8174 exec_catchpoint::~exec_catchpoint ()
8175 {
8176 xfree (this->exec_pathname);
8177 }
8178
8179 static int
8180 insert_catch_exec (struct bp_location *bl)
8181 {
8182 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8183 }
8184
8185 static int
8186 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8187 {
8188 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8189 }
8190
8191 static int
8192 breakpoint_hit_catch_exec (const struct bp_location *bl,
8193 const address_space *aspace, CORE_ADDR bp_addr,
8194 const struct target_waitstatus *ws)
8195 {
8196 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8197
8198 if (ws->kind != TARGET_WAITKIND_EXECD)
8199 return 0;
8200
8201 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8202 return 1;
8203 }
8204
8205 static enum print_stop_action
8206 print_it_catch_exec (bpstat bs)
8207 {
8208 struct ui_out *uiout = current_uiout;
8209 struct breakpoint *b = bs->breakpoint_at;
8210 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8211
8212 annotate_catchpoint (b->number);
8213 maybe_print_thread_hit_breakpoint (uiout);
8214 if (b->disposition == disp_del)
8215 uiout->text ("Temporary catchpoint ");
8216 else
8217 uiout->text ("Catchpoint ");
8218 if (uiout->is_mi_like_p ())
8219 {
8220 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8221 uiout->field_string ("disp", bpdisp_text (b->disposition));
8222 }
8223 uiout->field_signed ("bkptno", b->number);
8224 uiout->text (" (exec'd ");
8225 uiout->field_string ("new-exec", c->exec_pathname);
8226 uiout->text ("), ");
8227
8228 return PRINT_SRC_AND_LOC;
8229 }
8230
8231 static void
8232 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8233 {
8234 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8235 struct value_print_options opts;
8236 struct ui_out *uiout = current_uiout;
8237
8238 get_user_print_options (&opts);
8239
8240 /* Field 4, the address, is omitted (which makes the columns
8241 not line up too nicely with the headers, but the effect
8242 is relatively readable). */
8243 if (opts.addressprint)
8244 uiout->field_skip ("addr");
8245 annotate_field (5);
8246 uiout->text ("exec");
8247 if (c->exec_pathname != NULL)
8248 {
8249 uiout->text (", program \"");
8250 uiout->field_string ("what", c->exec_pathname);
8251 uiout->text ("\" ");
8252 }
8253
8254 if (uiout->is_mi_like_p ())
8255 uiout->field_string ("catch-type", "exec");
8256 }
8257
8258 static void
8259 print_mention_catch_exec (struct breakpoint *b)
8260 {
8261 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8262 }
8263
8264 /* Implement the "print_recreate" breakpoint_ops method for exec
8265 catchpoints. */
8266
8267 static void
8268 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8269 {
8270 fprintf_unfiltered (fp, "catch exec");
8271 print_recreate_thread (b, fp);
8272 }
8273
8274 static struct breakpoint_ops catch_exec_breakpoint_ops;
8275
8276 static int
8277 hw_breakpoint_used_count (void)
8278 {
8279 int i = 0;
8280 struct breakpoint *b;
8281 struct bp_location *bl;
8282
8283 ALL_BREAKPOINTS (b)
8284 {
8285 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8286 for (bl = b->loc; bl; bl = bl->next)
8287 {
8288 /* Special types of hardware breakpoints may use more than
8289 one register. */
8290 i += b->ops->resources_needed (bl);
8291 }
8292 }
8293
8294 return i;
8295 }
8296
8297 /* Returns the resources B would use if it were a hardware
8298 watchpoint. */
8299
8300 static int
8301 hw_watchpoint_use_count (struct breakpoint *b)
8302 {
8303 int i = 0;
8304 struct bp_location *bl;
8305
8306 if (!breakpoint_enabled (b))
8307 return 0;
8308
8309 for (bl = b->loc; bl; bl = bl->next)
8310 {
8311 /* Special types of hardware watchpoints may use more than
8312 one register. */
8313 i += b->ops->resources_needed (bl);
8314 }
8315
8316 return i;
8317 }
8318
8319 /* Returns the sum the used resources of all hardware watchpoints of
8320 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8321 the sum of the used resources of all hardware watchpoints of other
8322 types _not_ TYPE. */
8323
8324 static int
8325 hw_watchpoint_used_count_others (struct breakpoint *except,
8326 enum bptype type, int *other_type_used)
8327 {
8328 int i = 0;
8329 struct breakpoint *b;
8330
8331 *other_type_used = 0;
8332 ALL_BREAKPOINTS (b)
8333 {
8334 if (b == except)
8335 continue;
8336 if (!breakpoint_enabled (b))
8337 continue;
8338
8339 if (b->type == type)
8340 i += hw_watchpoint_use_count (b);
8341 else if (is_hardware_watchpoint (b))
8342 *other_type_used = 1;
8343 }
8344
8345 return i;
8346 }
8347
8348 void
8349 disable_watchpoints_before_interactive_call_start (void)
8350 {
8351 struct breakpoint *b;
8352
8353 ALL_BREAKPOINTS (b)
8354 {
8355 if (is_watchpoint (b) && breakpoint_enabled (b))
8356 {
8357 b->enable_state = bp_call_disabled;
8358 update_global_location_list (UGLL_DONT_INSERT);
8359 }
8360 }
8361 }
8362
8363 void
8364 enable_watchpoints_after_interactive_call_stop (void)
8365 {
8366 struct breakpoint *b;
8367
8368 ALL_BREAKPOINTS (b)
8369 {
8370 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8371 {
8372 b->enable_state = bp_enabled;
8373 update_global_location_list (UGLL_MAY_INSERT);
8374 }
8375 }
8376 }
8377
8378 void
8379 disable_breakpoints_before_startup (void)
8380 {
8381 current_program_space->executing_startup = 1;
8382 update_global_location_list (UGLL_DONT_INSERT);
8383 }
8384
8385 void
8386 enable_breakpoints_after_startup (void)
8387 {
8388 current_program_space->executing_startup = 0;
8389 breakpoint_re_set ();
8390 }
8391
8392 /* Create a new single-step breakpoint for thread THREAD, with no
8393 locations. */
8394
8395 static struct breakpoint *
8396 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8397 {
8398 std::unique_ptr<breakpoint> b (new breakpoint ());
8399
8400 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8401 &momentary_breakpoint_ops);
8402
8403 b->disposition = disp_donttouch;
8404 b->frame_id = null_frame_id;
8405
8406 b->thread = thread;
8407 gdb_assert (b->thread != 0);
8408
8409 return add_to_breakpoint_chain (std::move (b));
8410 }
8411
8412 /* Set a momentary breakpoint of type TYPE at address specified by
8413 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8414 frame. */
8415
8416 breakpoint_up
8417 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8418 struct frame_id frame_id, enum bptype type)
8419 {
8420 struct breakpoint *b;
8421
8422 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8423 tail-called one. */
8424 gdb_assert (!frame_id_artificial_p (frame_id));
8425
8426 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8427 b->enable_state = bp_enabled;
8428 b->disposition = disp_donttouch;
8429 b->frame_id = frame_id;
8430
8431 b->thread = inferior_thread ()->global_num;
8432
8433 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8434
8435 return breakpoint_up (b);
8436 }
8437
8438 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8439 The new breakpoint will have type TYPE, use OPS as its
8440 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8441
8442 static struct breakpoint *
8443 momentary_breakpoint_from_master (struct breakpoint *orig,
8444 enum bptype type,
8445 const struct breakpoint_ops *ops,
8446 int loc_enabled)
8447 {
8448 struct breakpoint *copy;
8449
8450 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8451 copy->loc = allocate_bp_location (copy);
8452 set_breakpoint_location_function (copy->loc);
8453
8454 copy->loc->gdbarch = orig->loc->gdbarch;
8455 copy->loc->requested_address = orig->loc->requested_address;
8456 copy->loc->address = orig->loc->address;
8457 copy->loc->section = orig->loc->section;
8458 copy->loc->pspace = orig->loc->pspace;
8459 copy->loc->probe = orig->loc->probe;
8460 copy->loc->line_number = orig->loc->line_number;
8461 copy->loc->symtab = orig->loc->symtab;
8462 copy->loc->enabled = loc_enabled;
8463 copy->frame_id = orig->frame_id;
8464 copy->thread = orig->thread;
8465 copy->pspace = orig->pspace;
8466
8467 copy->enable_state = bp_enabled;
8468 copy->disposition = disp_donttouch;
8469 copy->number = internal_breakpoint_number--;
8470
8471 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8472 return copy;
8473 }
8474
8475 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8476 ORIG is NULL. */
8477
8478 struct breakpoint *
8479 clone_momentary_breakpoint (struct breakpoint *orig)
8480 {
8481 /* If there's nothing to clone, then return nothing. */
8482 if (orig == NULL)
8483 return NULL;
8484
8485 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8486 }
8487
8488 breakpoint_up
8489 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8490 enum bptype type)
8491 {
8492 struct symtab_and_line sal;
8493
8494 sal = find_pc_line (pc, 0);
8495 sal.pc = pc;
8496 sal.section = find_pc_overlay (pc);
8497 sal.explicit_pc = 1;
8498
8499 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8500 }
8501 \f
8502
8503 /* Tell the user we have just set a breakpoint B. */
8504
8505 static void
8506 mention (struct breakpoint *b)
8507 {
8508 b->ops->print_mention (b);
8509 current_uiout->text ("\n");
8510 }
8511 \f
8512
8513 static int bp_loc_is_permanent (struct bp_location *loc);
8514
8515 static struct bp_location *
8516 add_location_to_breakpoint (struct breakpoint *b,
8517 const struct symtab_and_line *sal)
8518 {
8519 struct bp_location *loc, **tmp;
8520 CORE_ADDR adjusted_address;
8521 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8522
8523 if (loc_gdbarch == NULL)
8524 loc_gdbarch = b->gdbarch;
8525
8526 /* Adjust the breakpoint's address prior to allocating a location.
8527 Once we call allocate_bp_location(), that mostly uninitialized
8528 location will be placed on the location chain. Adjustment of the
8529 breakpoint may cause target_read_memory() to be called and we do
8530 not want its scan of the location chain to find a breakpoint and
8531 location that's only been partially initialized. */
8532 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8533 sal->pc, b->type);
8534
8535 /* Sort the locations by their ADDRESS. */
8536 loc = allocate_bp_location (b);
8537 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8538 tmp = &((*tmp)->next))
8539 ;
8540 loc->next = *tmp;
8541 *tmp = loc;
8542
8543 loc->requested_address = sal->pc;
8544 loc->address = adjusted_address;
8545 loc->pspace = sal->pspace;
8546 loc->probe.prob = sal->prob;
8547 loc->probe.objfile = sal->objfile;
8548 gdb_assert (loc->pspace != NULL);
8549 loc->section = sal->section;
8550 loc->gdbarch = loc_gdbarch;
8551 loc->line_number = sal->line;
8552 loc->symtab = sal->symtab;
8553 loc->symbol = sal->symbol;
8554 loc->msymbol = sal->msymbol;
8555 loc->objfile = sal->objfile;
8556
8557 set_breakpoint_location_function (loc);
8558
8559 /* While by definition, permanent breakpoints are already present in the
8560 code, we don't mark the location as inserted. Normally one would expect
8561 that GDB could rely on that breakpoint instruction to stop the program,
8562 thus removing the need to insert its own breakpoint, except that executing
8563 the breakpoint instruction can kill the target instead of reporting a
8564 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8565 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8566 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8567 breakpoint be inserted normally results in QEMU knowing about the GDB
8568 breakpoint, and thus trap before the breakpoint instruction is executed.
8569 (If GDB later needs to continue execution past the permanent breakpoint,
8570 it manually increments the PC, thus avoiding executing the breakpoint
8571 instruction.) */
8572 if (bp_loc_is_permanent (loc))
8573 loc->permanent = 1;
8574
8575 return loc;
8576 }
8577 \f
8578
8579 /* See breakpoint.h. */
8580
8581 int
8582 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8583 {
8584 int len;
8585 CORE_ADDR addr;
8586 const gdb_byte *bpoint;
8587 gdb_byte *target_mem;
8588
8589 addr = address;
8590 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8591
8592 /* Software breakpoints unsupported? */
8593 if (bpoint == NULL)
8594 return 0;
8595
8596 target_mem = (gdb_byte *) alloca (len);
8597
8598 /* Enable the automatic memory restoration from breakpoints while
8599 we read the memory. Otherwise we could say about our temporary
8600 breakpoints they are permanent. */
8601 scoped_restore restore_memory
8602 = make_scoped_restore_show_memory_breakpoints (0);
8603
8604 if (target_read_memory (address, target_mem, len) == 0
8605 && memcmp (target_mem, bpoint, len) == 0)
8606 return 1;
8607
8608 return 0;
8609 }
8610
8611 /* Return 1 if LOC is pointing to a permanent breakpoint,
8612 return 0 otherwise. */
8613
8614 static int
8615 bp_loc_is_permanent (struct bp_location *loc)
8616 {
8617 gdb_assert (loc != NULL);
8618
8619 /* If we have a non-breakpoint-backed catchpoint or a software
8620 watchpoint, just return 0. We should not attempt to read from
8621 the addresses the locations of these breakpoint types point to.
8622 program_breakpoint_here_p, below, will attempt to read
8623 memory. */
8624 if (!bl_address_is_meaningful (loc))
8625 return 0;
8626
8627 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8628 switch_to_program_space_and_thread (loc->pspace);
8629 return program_breakpoint_here_p (loc->gdbarch, loc->address);
8630 }
8631
8632 /* Build a command list for the dprintf corresponding to the current
8633 settings of the dprintf style options. */
8634
8635 static void
8636 update_dprintf_command_list (struct breakpoint *b)
8637 {
8638 char *dprintf_args = b->extra_string;
8639 char *printf_line = NULL;
8640
8641 if (!dprintf_args)
8642 return;
8643
8644 dprintf_args = skip_spaces (dprintf_args);
8645
8646 /* Allow a comma, as it may have terminated a location, but don't
8647 insist on it. */
8648 if (*dprintf_args == ',')
8649 ++dprintf_args;
8650 dprintf_args = skip_spaces (dprintf_args);
8651
8652 if (*dprintf_args != '"')
8653 error (_("Bad format string, missing '\"'."));
8654
8655 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8656 printf_line = xstrprintf ("printf %s", dprintf_args);
8657 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8658 {
8659 if (!dprintf_function)
8660 error (_("No function supplied for dprintf call"));
8661
8662 if (dprintf_channel && strlen (dprintf_channel) > 0)
8663 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8664 dprintf_function,
8665 dprintf_channel,
8666 dprintf_args);
8667 else
8668 printf_line = xstrprintf ("call (void) %s (%s)",
8669 dprintf_function,
8670 dprintf_args);
8671 }
8672 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8673 {
8674 if (target_can_run_breakpoint_commands ())
8675 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8676 else
8677 {
8678 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8679 printf_line = xstrprintf ("printf %s", dprintf_args);
8680 }
8681 }
8682 else
8683 internal_error (__FILE__, __LINE__,
8684 _("Invalid dprintf style."));
8685
8686 gdb_assert (printf_line != NULL);
8687
8688 /* Manufacture a printf sequence. */
8689 struct command_line *printf_cmd_line
8690 = new struct command_line (simple_control, printf_line);
8691 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8692 command_lines_deleter ()));
8693 }
8694
8695 /* Update all dprintf commands, making their command lists reflect
8696 current style settings. */
8697
8698 static void
8699 update_dprintf_commands (const char *args, int from_tty,
8700 struct cmd_list_element *c)
8701 {
8702 struct breakpoint *b;
8703
8704 ALL_BREAKPOINTS (b)
8705 {
8706 if (b->type == bp_dprintf)
8707 update_dprintf_command_list (b);
8708 }
8709 }
8710
8711 /* Create a breakpoint with SAL as location. Use LOCATION
8712 as a description of the location, and COND_STRING
8713 as condition expression. If LOCATION is NULL then create an
8714 "address location" from the address in the SAL. */
8715
8716 static void
8717 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8718 gdb::array_view<const symtab_and_line> sals,
8719 event_location_up &&location,
8720 gdb::unique_xmalloc_ptr<char> filter,
8721 gdb::unique_xmalloc_ptr<char> cond_string,
8722 gdb::unique_xmalloc_ptr<char> extra_string,
8723 enum bptype type, enum bpdisp disposition,
8724 int thread, int task, int ignore_count,
8725 const struct breakpoint_ops *ops, int from_tty,
8726 int enabled, int internal, unsigned flags,
8727 int display_canonical)
8728 {
8729 int i;
8730
8731 if (type == bp_hardware_breakpoint)
8732 {
8733 int target_resources_ok;
8734
8735 i = hw_breakpoint_used_count ();
8736 target_resources_ok =
8737 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8738 i + 1, 0);
8739 if (target_resources_ok == 0)
8740 error (_("No hardware breakpoint support in the target."));
8741 else if (target_resources_ok < 0)
8742 error (_("Hardware breakpoints used exceeds limit."));
8743 }
8744
8745 gdb_assert (!sals.empty ());
8746
8747 for (const auto &sal : sals)
8748 {
8749 struct bp_location *loc;
8750
8751 if (from_tty)
8752 {
8753 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8754 if (!loc_gdbarch)
8755 loc_gdbarch = gdbarch;
8756
8757 describe_other_breakpoints (loc_gdbarch,
8758 sal.pspace, sal.pc, sal.section, thread);
8759 }
8760
8761 if (&sal == &sals[0])
8762 {
8763 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8764 b->thread = thread;
8765 b->task = task;
8766
8767 b->cond_string = cond_string.release ();
8768 b->extra_string = extra_string.release ();
8769 b->ignore_count = ignore_count;
8770 b->enable_state = enabled ? bp_enabled : bp_disabled;
8771 b->disposition = disposition;
8772
8773 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8774 b->loc->inserted = 1;
8775
8776 if (type == bp_static_tracepoint)
8777 {
8778 struct tracepoint *t = (struct tracepoint *) b;
8779 struct static_tracepoint_marker marker;
8780
8781 if (strace_marker_p (b))
8782 {
8783 /* We already know the marker exists, otherwise, we
8784 wouldn't see a sal for it. */
8785 const char *p
8786 = &event_location_to_string (b->location.get ())[3];
8787 const char *endp;
8788
8789 p = skip_spaces (p);
8790
8791 endp = skip_to_space (p);
8792
8793 t->static_trace_marker_id.assign (p, endp - p);
8794
8795 printf_filtered (_("Probed static tracepoint "
8796 "marker \"%s\"\n"),
8797 t->static_trace_marker_id.c_str ());
8798 }
8799 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8800 {
8801 t->static_trace_marker_id = std::move (marker.str_id);
8802
8803 printf_filtered (_("Probed static tracepoint "
8804 "marker \"%s\"\n"),
8805 t->static_trace_marker_id.c_str ());
8806 }
8807 else
8808 warning (_("Couldn't determine the static "
8809 "tracepoint marker to probe"));
8810 }
8811
8812 loc = b->loc;
8813 }
8814 else
8815 {
8816 loc = add_location_to_breakpoint (b, &sal);
8817 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8818 loc->inserted = 1;
8819 }
8820
8821 if (b->cond_string)
8822 {
8823 const char *arg = b->cond_string;
8824
8825 loc->cond = parse_exp_1 (&arg, loc->address,
8826 block_for_pc (loc->address), 0);
8827 if (*arg)
8828 error (_("Garbage '%s' follows condition"), arg);
8829 }
8830
8831 /* Dynamic printf requires and uses additional arguments on the
8832 command line, otherwise it's an error. */
8833 if (type == bp_dprintf)
8834 {
8835 if (b->extra_string)
8836 update_dprintf_command_list (b);
8837 else
8838 error (_("Format string required"));
8839 }
8840 else if (b->extra_string)
8841 error (_("Garbage '%s' at end of command"), b->extra_string);
8842 }
8843
8844 b->display_canonical = display_canonical;
8845 if (location != NULL)
8846 b->location = std::move (location);
8847 else
8848 b->location = new_address_location (b->loc->address, NULL, 0);
8849 b->filter = std::move (filter);
8850 }
8851
8852 static void
8853 create_breakpoint_sal (struct gdbarch *gdbarch,
8854 gdb::array_view<const symtab_and_line> sals,
8855 event_location_up &&location,
8856 gdb::unique_xmalloc_ptr<char> filter,
8857 gdb::unique_xmalloc_ptr<char> cond_string,
8858 gdb::unique_xmalloc_ptr<char> extra_string,
8859 enum bptype type, enum bpdisp disposition,
8860 int thread, int task, int ignore_count,
8861 const struct breakpoint_ops *ops, int from_tty,
8862 int enabled, int internal, unsigned flags,
8863 int display_canonical)
8864 {
8865 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8866
8867 init_breakpoint_sal (b.get (), gdbarch,
8868 sals, std::move (location),
8869 std::move (filter),
8870 std::move (cond_string),
8871 std::move (extra_string),
8872 type, disposition,
8873 thread, task, ignore_count,
8874 ops, from_tty,
8875 enabled, internal, flags,
8876 display_canonical);
8877
8878 install_breakpoint (internal, std::move (b), 0);
8879 }
8880
8881 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8882 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8883 value. COND_STRING, if not NULL, specified the condition to be
8884 used for all breakpoints. Essentially the only case where
8885 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8886 function. In that case, it's still not possible to specify
8887 separate conditions for different overloaded functions, so
8888 we take just a single condition string.
8889
8890 NOTE: If the function succeeds, the caller is expected to cleanup
8891 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8892 array contents). If the function fails (error() is called), the
8893 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8894 COND and SALS arrays and each of those arrays contents. */
8895
8896 static void
8897 create_breakpoints_sal (struct gdbarch *gdbarch,
8898 struct linespec_result *canonical,
8899 gdb::unique_xmalloc_ptr<char> cond_string,
8900 gdb::unique_xmalloc_ptr<char> extra_string,
8901 enum bptype type, enum bpdisp disposition,
8902 int thread, int task, int ignore_count,
8903 const struct breakpoint_ops *ops, int from_tty,
8904 int enabled, int internal, unsigned flags)
8905 {
8906 if (canonical->pre_expanded)
8907 gdb_assert (canonical->lsals.size () == 1);
8908
8909 for (const auto &lsal : canonical->lsals)
8910 {
8911 /* Note that 'location' can be NULL in the case of a plain
8912 'break', without arguments. */
8913 event_location_up location
8914 = (canonical->location != NULL
8915 ? copy_event_location (canonical->location.get ()) : NULL);
8916 gdb::unique_xmalloc_ptr<char> filter_string
8917 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8918
8919 create_breakpoint_sal (gdbarch, lsal.sals,
8920 std::move (location),
8921 std::move (filter_string),
8922 std::move (cond_string),
8923 std::move (extra_string),
8924 type, disposition,
8925 thread, task, ignore_count, ops,
8926 from_tty, enabled, internal, flags,
8927 canonical->special_display);
8928 }
8929 }
8930
8931 /* Parse LOCATION which is assumed to be a SAL specification possibly
8932 followed by conditionals. On return, SALS contains an array of SAL
8933 addresses found. LOCATION points to the end of the SAL (for
8934 linespec locations).
8935
8936 The array and the line spec strings are allocated on the heap, it is
8937 the caller's responsibility to free them. */
8938
8939 static void
8940 parse_breakpoint_sals (const struct event_location *location,
8941 struct linespec_result *canonical)
8942 {
8943 struct symtab_and_line cursal;
8944
8945 if (event_location_type (location) == LINESPEC_LOCATION)
8946 {
8947 const char *spec = get_linespec_location (location)->spec_string;
8948
8949 if (spec == NULL)
8950 {
8951 /* The last displayed codepoint, if it's valid, is our default
8952 breakpoint address. */
8953 if (last_displayed_sal_is_valid ())
8954 {
8955 /* Set sal's pspace, pc, symtab, and line to the values
8956 corresponding to the last call to print_frame_info.
8957 Be sure to reinitialize LINE with NOTCURRENT == 0
8958 as the breakpoint line number is inappropriate otherwise.
8959 find_pc_line would adjust PC, re-set it back. */
8960 symtab_and_line sal = get_last_displayed_sal ();
8961 CORE_ADDR pc = sal.pc;
8962
8963 sal = find_pc_line (pc, 0);
8964
8965 /* "break" without arguments is equivalent to "break *PC"
8966 where PC is the last displayed codepoint's address. So
8967 make sure to set sal.explicit_pc to prevent GDB from
8968 trying to expand the list of sals to include all other
8969 instances with the same symtab and line. */
8970 sal.pc = pc;
8971 sal.explicit_pc = 1;
8972
8973 struct linespec_sals lsal;
8974 lsal.sals = {sal};
8975 lsal.canonical = NULL;
8976
8977 canonical->lsals.push_back (std::move (lsal));
8978 return;
8979 }
8980 else
8981 error (_("No default breakpoint address now."));
8982 }
8983 }
8984
8985 /* Force almost all breakpoints to be in terms of the
8986 current_source_symtab (which is decode_line_1's default).
8987 This should produce the results we want almost all of the
8988 time while leaving default_breakpoint_* alone.
8989
8990 ObjC: However, don't match an Objective-C method name which
8991 may have a '+' or '-' succeeded by a '['. */
8992 cursal = get_current_source_symtab_and_line ();
8993 if (last_displayed_sal_is_valid ())
8994 {
8995 const char *spec = NULL;
8996
8997 if (event_location_type (location) == LINESPEC_LOCATION)
8998 spec = get_linespec_location (location)->spec_string;
8999
9000 if (!cursal.symtab
9001 || (spec != NULL
9002 && strchr ("+-", spec[0]) != NULL
9003 && spec[1] != '['))
9004 {
9005 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9006 get_last_displayed_symtab (),
9007 get_last_displayed_line (),
9008 canonical, NULL, NULL);
9009 return;
9010 }
9011 }
9012
9013 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9014 cursal.symtab, cursal.line, canonical, NULL, NULL);
9015 }
9016
9017
9018 /* Convert each SAL into a real PC. Verify that the PC can be
9019 inserted as a breakpoint. If it can't throw an error. */
9020
9021 static void
9022 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9023 {
9024 for (auto &sal : sals)
9025 resolve_sal_pc (&sal);
9026 }
9027
9028 /* Fast tracepoints may have restrictions on valid locations. For
9029 instance, a fast tracepoint using a jump instead of a trap will
9030 likely have to overwrite more bytes than a trap would, and so can
9031 only be placed where the instruction is longer than the jump, or a
9032 multi-instruction sequence does not have a jump into the middle of
9033 it, etc. */
9034
9035 static void
9036 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9037 gdb::array_view<const symtab_and_line> sals)
9038 {
9039 for (const auto &sal : sals)
9040 {
9041 struct gdbarch *sarch;
9042
9043 sarch = get_sal_arch (sal);
9044 /* We fall back to GDBARCH if there is no architecture
9045 associated with SAL. */
9046 if (sarch == NULL)
9047 sarch = gdbarch;
9048 std::string msg;
9049 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9050 error (_("May not have a fast tracepoint at %s%s"),
9051 paddress (sarch, sal.pc), msg.c_str ());
9052 }
9053 }
9054
9055 /* Given TOK, a string specification of condition and thread, as
9056 accepted by the 'break' command, extract the condition
9057 string and thread number and set *COND_STRING and *THREAD.
9058 PC identifies the context at which the condition should be parsed.
9059 If no condition is found, *COND_STRING is set to NULL.
9060 If no thread is found, *THREAD is set to -1. */
9061
9062 static void
9063 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9064 char **cond_string, int *thread, int *task,
9065 char **rest)
9066 {
9067 *cond_string = NULL;
9068 *thread = -1;
9069 *task = 0;
9070 *rest = NULL;
9071
9072 while (tok && *tok)
9073 {
9074 const char *end_tok;
9075 int toklen;
9076 const char *cond_start = NULL;
9077 const char *cond_end = NULL;
9078
9079 tok = skip_spaces (tok);
9080
9081 if ((*tok == '"' || *tok == ',') && rest)
9082 {
9083 *rest = savestring (tok, strlen (tok));
9084 return;
9085 }
9086
9087 end_tok = skip_to_space (tok);
9088
9089 toklen = end_tok - tok;
9090
9091 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9092 {
9093 tok = cond_start = end_tok + 1;
9094 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9095 cond_end = tok;
9096 *cond_string = savestring (cond_start, cond_end - cond_start);
9097 }
9098 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9099 {
9100 const char *tmptok;
9101 struct thread_info *thr;
9102
9103 tok = end_tok + 1;
9104 thr = parse_thread_id (tok, &tmptok);
9105 if (tok == tmptok)
9106 error (_("Junk after thread keyword."));
9107 *thread = thr->global_num;
9108 tok = tmptok;
9109 }
9110 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9111 {
9112 char *tmptok;
9113
9114 tok = end_tok + 1;
9115 *task = strtol (tok, &tmptok, 0);
9116 if (tok == tmptok)
9117 error (_("Junk after task keyword."));
9118 if (!valid_task_id (*task))
9119 error (_("Unknown task %d."), *task);
9120 tok = tmptok;
9121 }
9122 else if (rest)
9123 {
9124 *rest = savestring (tok, strlen (tok));
9125 return;
9126 }
9127 else
9128 error (_("Junk at end of arguments."));
9129 }
9130 }
9131
9132 /* Decode a static tracepoint marker spec. */
9133
9134 static std::vector<symtab_and_line>
9135 decode_static_tracepoint_spec (const char **arg_p)
9136 {
9137 const char *p = &(*arg_p)[3];
9138 const char *endp;
9139
9140 p = skip_spaces (p);
9141
9142 endp = skip_to_space (p);
9143
9144 std::string marker_str (p, endp - p);
9145
9146 std::vector<static_tracepoint_marker> markers
9147 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9148 if (markers.empty ())
9149 error (_("No known static tracepoint marker named %s"),
9150 marker_str.c_str ());
9151
9152 std::vector<symtab_and_line> sals;
9153 sals.reserve (markers.size ());
9154
9155 for (const static_tracepoint_marker &marker : markers)
9156 {
9157 symtab_and_line sal = find_pc_line (marker.address, 0);
9158 sal.pc = marker.address;
9159 sals.push_back (sal);
9160 }
9161
9162 *arg_p = endp;
9163 return sals;
9164 }
9165
9166 /* See breakpoint.h. */
9167
9168 int
9169 create_breakpoint (struct gdbarch *gdbarch,
9170 const struct event_location *location,
9171 const char *cond_string,
9172 int thread, const char *extra_string,
9173 int parse_extra,
9174 int tempflag, enum bptype type_wanted,
9175 int ignore_count,
9176 enum auto_boolean pending_break_support,
9177 const struct breakpoint_ops *ops,
9178 int from_tty, int enabled, int internal,
9179 unsigned flags)
9180 {
9181 struct linespec_result canonical;
9182 int pending = 0;
9183 int task = 0;
9184 int prev_bkpt_count = breakpoint_count;
9185
9186 gdb_assert (ops != NULL);
9187
9188 /* If extra_string isn't useful, set it to NULL. */
9189 if (extra_string != NULL && *extra_string == '\0')
9190 extra_string = NULL;
9191
9192 try
9193 {
9194 ops->create_sals_from_location (location, &canonical, type_wanted);
9195 }
9196 catch (const gdb_exception_error &e)
9197 {
9198 /* If caller is interested in rc value from parse, set
9199 value. */
9200 if (e.error == NOT_FOUND_ERROR)
9201 {
9202 /* If pending breakpoint support is turned off, throw
9203 error. */
9204
9205 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9206 throw;
9207
9208 exception_print (gdb_stderr, e);
9209
9210 /* If pending breakpoint support is auto query and the user
9211 selects no, then simply return the error code. */
9212 if (pending_break_support == AUTO_BOOLEAN_AUTO
9213 && !nquery (_("Make %s pending on future shared library load? "),
9214 bptype_string (type_wanted)))
9215 return 0;
9216
9217 /* At this point, either the user was queried about setting
9218 a pending breakpoint and selected yes, or pending
9219 breakpoint behavior is on and thus a pending breakpoint
9220 is defaulted on behalf of the user. */
9221 pending = 1;
9222 }
9223 else
9224 throw;
9225 }
9226
9227 if (!pending && canonical.lsals.empty ())
9228 return 0;
9229
9230 /* Resolve all line numbers to PC's and verify that the addresses
9231 are ok for the target. */
9232 if (!pending)
9233 {
9234 for (auto &lsal : canonical.lsals)
9235 breakpoint_sals_to_pc (lsal.sals);
9236 }
9237
9238 /* Fast tracepoints may have additional restrictions on location. */
9239 if (!pending && type_wanted == bp_fast_tracepoint)
9240 {
9241 for (const auto &lsal : canonical.lsals)
9242 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9243 }
9244
9245 /* Verify that condition can be parsed, before setting any
9246 breakpoints. Allocate a separate condition expression for each
9247 breakpoint. */
9248 if (!pending)
9249 {
9250 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9251 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9252
9253 if (parse_extra)
9254 {
9255 char *rest;
9256 char *cond;
9257
9258 const linespec_sals &lsal = canonical.lsals[0];
9259
9260 /* Here we only parse 'arg' to separate condition
9261 from thread number, so parsing in context of first
9262 sal is OK. When setting the breakpoint we'll
9263 re-parse it in context of each sal. */
9264
9265 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9266 &cond, &thread, &task, &rest);
9267 cond_string_copy.reset (cond);
9268 extra_string_copy.reset (rest);
9269 }
9270 else
9271 {
9272 if (type_wanted != bp_dprintf
9273 && extra_string != NULL && *extra_string != '\0')
9274 error (_("Garbage '%s' at end of location"), extra_string);
9275
9276 /* Create a private copy of condition string. */
9277 if (cond_string)
9278 cond_string_copy.reset (xstrdup (cond_string));
9279 /* Create a private copy of any extra string. */
9280 if (extra_string)
9281 extra_string_copy.reset (xstrdup (extra_string));
9282 }
9283
9284 ops->create_breakpoints_sal (gdbarch, &canonical,
9285 std::move (cond_string_copy),
9286 std::move (extra_string_copy),
9287 type_wanted,
9288 tempflag ? disp_del : disp_donttouch,
9289 thread, task, ignore_count, ops,
9290 from_tty, enabled, internal, flags);
9291 }
9292 else
9293 {
9294 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9295
9296 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9297 b->location = copy_event_location (location);
9298
9299 if (parse_extra)
9300 b->cond_string = NULL;
9301 else
9302 {
9303 /* Create a private copy of condition string. */
9304 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9305 b->thread = thread;
9306 }
9307
9308 /* Create a private copy of any extra string. */
9309 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9310 b->ignore_count = ignore_count;
9311 b->disposition = tempflag ? disp_del : disp_donttouch;
9312 b->condition_not_parsed = 1;
9313 b->enable_state = enabled ? bp_enabled : bp_disabled;
9314 if ((type_wanted != bp_breakpoint
9315 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9316 b->pspace = current_program_space;
9317
9318 install_breakpoint (internal, std::move (b), 0);
9319 }
9320
9321 if (canonical.lsals.size () > 1)
9322 {
9323 warning (_("Multiple breakpoints were set.\nUse the "
9324 "\"delete\" command to delete unwanted breakpoints."));
9325 prev_breakpoint_count = prev_bkpt_count;
9326 }
9327
9328 update_global_location_list (UGLL_MAY_INSERT);
9329
9330 return 1;
9331 }
9332
9333 /* Set a breakpoint.
9334 ARG is a string describing breakpoint address,
9335 condition, and thread.
9336 FLAG specifies if a breakpoint is hardware on,
9337 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9338 and BP_TEMPFLAG. */
9339
9340 static void
9341 break_command_1 (const char *arg, int flag, int from_tty)
9342 {
9343 int tempflag = flag & BP_TEMPFLAG;
9344 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9345 ? bp_hardware_breakpoint
9346 : bp_breakpoint);
9347 struct breakpoint_ops *ops;
9348
9349 event_location_up location = string_to_event_location (&arg, current_language);
9350
9351 /* Matching breakpoints on probes. */
9352 if (location != NULL
9353 && event_location_type (location.get ()) == PROBE_LOCATION)
9354 ops = &bkpt_probe_breakpoint_ops;
9355 else
9356 ops = &bkpt_breakpoint_ops;
9357
9358 create_breakpoint (get_current_arch (),
9359 location.get (),
9360 NULL, 0, arg, 1 /* parse arg */,
9361 tempflag, type_wanted,
9362 0 /* Ignore count */,
9363 pending_break_support,
9364 ops,
9365 from_tty,
9366 1 /* enabled */,
9367 0 /* internal */,
9368 0);
9369 }
9370
9371 /* Helper function for break_command_1 and disassemble_command. */
9372
9373 void
9374 resolve_sal_pc (struct symtab_and_line *sal)
9375 {
9376 CORE_ADDR pc;
9377
9378 if (sal->pc == 0 && sal->symtab != NULL)
9379 {
9380 if (!find_line_pc (sal->symtab, sal->line, &pc))
9381 error (_("No line %d in file \"%s\"."),
9382 sal->line, symtab_to_filename_for_display (sal->symtab));
9383 sal->pc = pc;
9384
9385 /* If this SAL corresponds to a breakpoint inserted using a line
9386 number, then skip the function prologue if necessary. */
9387 if (sal->explicit_line)
9388 skip_prologue_sal (sal);
9389 }
9390
9391 if (sal->section == 0 && sal->symtab != NULL)
9392 {
9393 const struct blockvector *bv;
9394 const struct block *b;
9395 struct symbol *sym;
9396
9397 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9398 SYMTAB_COMPUNIT (sal->symtab));
9399 if (bv != NULL)
9400 {
9401 sym = block_linkage_function (b);
9402 if (sym != NULL)
9403 {
9404 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9405 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9406 sym);
9407 }
9408 else
9409 {
9410 /* It really is worthwhile to have the section, so we'll
9411 just have to look harder. This case can be executed
9412 if we have line numbers but no functions (as can
9413 happen in assembly source). */
9414
9415 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9416 switch_to_program_space_and_thread (sal->pspace);
9417
9418 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9419 if (msym.minsym)
9420 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9421 }
9422 }
9423 }
9424 }
9425
9426 void
9427 break_command (const char *arg, int from_tty)
9428 {
9429 break_command_1 (arg, 0, from_tty);
9430 }
9431
9432 void
9433 tbreak_command (const char *arg, int from_tty)
9434 {
9435 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9436 }
9437
9438 static void
9439 hbreak_command (const char *arg, int from_tty)
9440 {
9441 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9442 }
9443
9444 static void
9445 thbreak_command (const char *arg, int from_tty)
9446 {
9447 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9448 }
9449
9450 static void
9451 stop_command (const char *arg, int from_tty)
9452 {
9453 printf_filtered (_("Specify the type of breakpoint to set.\n\
9454 Usage: stop in <function | address>\n\
9455 stop at <line>\n"));
9456 }
9457
9458 static void
9459 stopin_command (const char *arg, int from_tty)
9460 {
9461 int badInput = 0;
9462
9463 if (arg == NULL)
9464 badInput = 1;
9465 else if (*arg != '*')
9466 {
9467 const char *argptr = arg;
9468 int hasColon = 0;
9469
9470 /* Look for a ':'. If this is a line number specification, then
9471 say it is bad, otherwise, it should be an address or
9472 function/method name. */
9473 while (*argptr && !hasColon)
9474 {
9475 hasColon = (*argptr == ':');
9476 argptr++;
9477 }
9478
9479 if (hasColon)
9480 badInput = (*argptr != ':'); /* Not a class::method */
9481 else
9482 badInput = isdigit (*arg); /* a simple line number */
9483 }
9484
9485 if (badInput)
9486 printf_filtered (_("Usage: stop in <function | address>\n"));
9487 else
9488 break_command_1 (arg, 0, from_tty);
9489 }
9490
9491 static void
9492 stopat_command (const char *arg, int from_tty)
9493 {
9494 int badInput = 0;
9495
9496 if (arg == NULL || *arg == '*') /* no line number */
9497 badInput = 1;
9498 else
9499 {
9500 const char *argptr = arg;
9501 int hasColon = 0;
9502
9503 /* Look for a ':'. If there is a '::' then get out, otherwise
9504 it is probably a line number. */
9505 while (*argptr && !hasColon)
9506 {
9507 hasColon = (*argptr == ':');
9508 argptr++;
9509 }
9510
9511 if (hasColon)
9512 badInput = (*argptr == ':'); /* we have class::method */
9513 else
9514 badInput = !isdigit (*arg); /* not a line number */
9515 }
9516
9517 if (badInput)
9518 printf_filtered (_("Usage: stop at LINE\n"));
9519 else
9520 break_command_1 (arg, 0, from_tty);
9521 }
9522
9523 /* The dynamic printf command is mostly like a regular breakpoint, but
9524 with a prewired command list consisting of a single output command,
9525 built from extra arguments supplied on the dprintf command
9526 line. */
9527
9528 static void
9529 dprintf_command (const char *arg, int from_tty)
9530 {
9531 event_location_up location = string_to_event_location (&arg, current_language);
9532
9533 /* If non-NULL, ARG should have been advanced past the location;
9534 the next character must be ','. */
9535 if (arg != NULL)
9536 {
9537 if (arg[0] != ',' || arg[1] == '\0')
9538 error (_("Format string required"));
9539 else
9540 {
9541 /* Skip the comma. */
9542 ++arg;
9543 }
9544 }
9545
9546 create_breakpoint (get_current_arch (),
9547 location.get (),
9548 NULL, 0, arg, 1 /* parse arg */,
9549 0, bp_dprintf,
9550 0 /* Ignore count */,
9551 pending_break_support,
9552 &dprintf_breakpoint_ops,
9553 from_tty,
9554 1 /* enabled */,
9555 0 /* internal */,
9556 0);
9557 }
9558
9559 static void
9560 agent_printf_command (const char *arg, int from_tty)
9561 {
9562 error (_("May only run agent-printf on the target"));
9563 }
9564
9565 /* Implement the "breakpoint_hit" breakpoint_ops method for
9566 ranged breakpoints. */
9567
9568 static int
9569 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9570 const address_space *aspace,
9571 CORE_ADDR bp_addr,
9572 const struct target_waitstatus *ws)
9573 {
9574 if (ws->kind != TARGET_WAITKIND_STOPPED
9575 || ws->value.sig != GDB_SIGNAL_TRAP)
9576 return 0;
9577
9578 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9579 bl->length, aspace, bp_addr);
9580 }
9581
9582 /* Implement the "resources_needed" breakpoint_ops method for
9583 ranged breakpoints. */
9584
9585 static int
9586 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9587 {
9588 return target_ranged_break_num_registers ();
9589 }
9590
9591 /* Implement the "print_it" breakpoint_ops method for
9592 ranged breakpoints. */
9593
9594 static enum print_stop_action
9595 print_it_ranged_breakpoint (bpstat bs)
9596 {
9597 struct breakpoint *b = bs->breakpoint_at;
9598 struct bp_location *bl = b->loc;
9599 struct ui_out *uiout = current_uiout;
9600
9601 gdb_assert (b->type == bp_hardware_breakpoint);
9602
9603 /* Ranged breakpoints have only one location. */
9604 gdb_assert (bl && bl->next == NULL);
9605
9606 annotate_breakpoint (b->number);
9607
9608 maybe_print_thread_hit_breakpoint (uiout);
9609
9610 if (b->disposition == disp_del)
9611 uiout->text ("Temporary ranged breakpoint ");
9612 else
9613 uiout->text ("Ranged breakpoint ");
9614 if (uiout->is_mi_like_p ())
9615 {
9616 uiout->field_string ("reason",
9617 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9618 uiout->field_string ("disp", bpdisp_text (b->disposition));
9619 }
9620 uiout->field_signed ("bkptno", b->number);
9621 uiout->text (", ");
9622
9623 return PRINT_SRC_AND_LOC;
9624 }
9625
9626 /* Implement the "print_one" breakpoint_ops method for
9627 ranged breakpoints. */
9628
9629 static void
9630 print_one_ranged_breakpoint (struct breakpoint *b,
9631 struct bp_location **last_loc)
9632 {
9633 struct bp_location *bl = b->loc;
9634 struct value_print_options opts;
9635 struct ui_out *uiout = current_uiout;
9636
9637 /* Ranged breakpoints have only one location. */
9638 gdb_assert (bl && bl->next == NULL);
9639
9640 get_user_print_options (&opts);
9641
9642 if (opts.addressprint)
9643 /* We don't print the address range here, it will be printed later
9644 by print_one_detail_ranged_breakpoint. */
9645 uiout->field_skip ("addr");
9646 annotate_field (5);
9647 print_breakpoint_location (b, bl);
9648 *last_loc = bl;
9649 }
9650
9651 /* Implement the "print_one_detail" breakpoint_ops method for
9652 ranged breakpoints. */
9653
9654 static void
9655 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9656 struct ui_out *uiout)
9657 {
9658 CORE_ADDR address_start, address_end;
9659 struct bp_location *bl = b->loc;
9660 string_file stb;
9661
9662 gdb_assert (bl);
9663
9664 address_start = bl->address;
9665 address_end = address_start + bl->length - 1;
9666
9667 uiout->text ("\taddress range: ");
9668 stb.printf ("[%s, %s]",
9669 print_core_address (bl->gdbarch, address_start),
9670 print_core_address (bl->gdbarch, address_end));
9671 uiout->field_stream ("addr", stb);
9672 uiout->text ("\n");
9673 }
9674
9675 /* Implement the "print_mention" breakpoint_ops method for
9676 ranged breakpoints. */
9677
9678 static void
9679 print_mention_ranged_breakpoint (struct breakpoint *b)
9680 {
9681 struct bp_location *bl = b->loc;
9682 struct ui_out *uiout = current_uiout;
9683
9684 gdb_assert (bl);
9685 gdb_assert (b->type == bp_hardware_breakpoint);
9686
9687 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9688 b->number, paddress (bl->gdbarch, bl->address),
9689 paddress (bl->gdbarch, bl->address + bl->length - 1));
9690 }
9691
9692 /* Implement the "print_recreate" breakpoint_ops method for
9693 ranged breakpoints. */
9694
9695 static void
9696 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9697 {
9698 fprintf_unfiltered (fp, "break-range %s, %s",
9699 event_location_to_string (b->location.get ()),
9700 event_location_to_string (b->location_range_end.get ()));
9701 print_recreate_thread (b, fp);
9702 }
9703
9704 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9705
9706 static struct breakpoint_ops ranged_breakpoint_ops;
9707
9708 /* Find the address where the end of the breakpoint range should be
9709 placed, given the SAL of the end of the range. This is so that if
9710 the user provides a line number, the end of the range is set to the
9711 last instruction of the given line. */
9712
9713 static CORE_ADDR
9714 find_breakpoint_range_end (struct symtab_and_line sal)
9715 {
9716 CORE_ADDR end;
9717
9718 /* If the user provided a PC value, use it. Otherwise,
9719 find the address of the end of the given location. */
9720 if (sal.explicit_pc)
9721 end = sal.pc;
9722 else
9723 {
9724 int ret;
9725 CORE_ADDR start;
9726
9727 ret = find_line_pc_range (sal, &start, &end);
9728 if (!ret)
9729 error (_("Could not find location of the end of the range."));
9730
9731 /* find_line_pc_range returns the start of the next line. */
9732 end--;
9733 }
9734
9735 return end;
9736 }
9737
9738 /* Implement the "break-range" CLI command. */
9739
9740 static void
9741 break_range_command (const char *arg, int from_tty)
9742 {
9743 const char *arg_start;
9744 struct linespec_result canonical_start, canonical_end;
9745 int bp_count, can_use_bp, length;
9746 CORE_ADDR end;
9747 struct breakpoint *b;
9748
9749 /* We don't support software ranged breakpoints. */
9750 if (target_ranged_break_num_registers () < 0)
9751 error (_("This target does not support hardware ranged breakpoints."));
9752
9753 bp_count = hw_breakpoint_used_count ();
9754 bp_count += target_ranged_break_num_registers ();
9755 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9756 bp_count, 0);
9757 if (can_use_bp < 0)
9758 error (_("Hardware breakpoints used exceeds limit."));
9759
9760 arg = skip_spaces (arg);
9761 if (arg == NULL || arg[0] == '\0')
9762 error(_("No address range specified."));
9763
9764 arg_start = arg;
9765 event_location_up start_location = string_to_event_location (&arg,
9766 current_language);
9767 parse_breakpoint_sals (start_location.get (), &canonical_start);
9768
9769 if (arg[0] != ',')
9770 error (_("Too few arguments."));
9771 else if (canonical_start.lsals.empty ())
9772 error (_("Could not find location of the beginning of the range."));
9773
9774 const linespec_sals &lsal_start = canonical_start.lsals[0];
9775
9776 if (canonical_start.lsals.size () > 1
9777 || lsal_start.sals.size () != 1)
9778 error (_("Cannot create a ranged breakpoint with multiple locations."));
9779
9780 const symtab_and_line &sal_start = lsal_start.sals[0];
9781 std::string addr_string_start (arg_start, arg - arg_start);
9782
9783 arg++; /* Skip the comma. */
9784 arg = skip_spaces (arg);
9785
9786 /* Parse the end location. */
9787
9788 arg_start = arg;
9789
9790 /* We call decode_line_full directly here instead of using
9791 parse_breakpoint_sals because we need to specify the start location's
9792 symtab and line as the default symtab and line for the end of the
9793 range. This makes it possible to have ranges like "foo.c:27, +14",
9794 where +14 means 14 lines from the start location. */
9795 event_location_up end_location = string_to_event_location (&arg,
9796 current_language);
9797 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9798 sal_start.symtab, sal_start.line,
9799 &canonical_end, NULL, NULL);
9800
9801 if (canonical_end.lsals.empty ())
9802 error (_("Could not find location of the end of the range."));
9803
9804 const linespec_sals &lsal_end = canonical_end.lsals[0];
9805 if (canonical_end.lsals.size () > 1
9806 || lsal_end.sals.size () != 1)
9807 error (_("Cannot create a ranged breakpoint with multiple locations."));
9808
9809 const symtab_and_line &sal_end = lsal_end.sals[0];
9810
9811 end = find_breakpoint_range_end (sal_end);
9812 if (sal_start.pc > end)
9813 error (_("Invalid address range, end precedes start."));
9814
9815 length = end - sal_start.pc + 1;
9816 if (length < 0)
9817 /* Length overflowed. */
9818 error (_("Address range too large."));
9819 else if (length == 1)
9820 {
9821 /* This range is simple enough to be handled by
9822 the `hbreak' command. */
9823 hbreak_command (&addr_string_start[0], 1);
9824
9825 return;
9826 }
9827
9828 /* Now set up the breakpoint. */
9829 b = set_raw_breakpoint (get_current_arch (), sal_start,
9830 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9831 set_breakpoint_count (breakpoint_count + 1);
9832 b->number = breakpoint_count;
9833 b->disposition = disp_donttouch;
9834 b->location = std::move (start_location);
9835 b->location_range_end = std::move (end_location);
9836 b->loc->length = length;
9837
9838 mention (b);
9839 gdb::observers::breakpoint_created.notify (b);
9840 update_global_location_list (UGLL_MAY_INSERT);
9841 }
9842
9843 /* Return non-zero if EXP is verified as constant. Returned zero
9844 means EXP is variable. Also the constant detection may fail for
9845 some constant expressions and in such case still falsely return
9846 zero. */
9847
9848 static int
9849 watchpoint_exp_is_const (const struct expression *exp)
9850 {
9851 int i = exp->nelts;
9852
9853 while (i > 0)
9854 {
9855 int oplenp, argsp;
9856
9857 /* We are only interested in the descriptor of each element. */
9858 operator_length (exp, i, &oplenp, &argsp);
9859 i -= oplenp;
9860
9861 switch (exp->elts[i].opcode)
9862 {
9863 case BINOP_ADD:
9864 case BINOP_SUB:
9865 case BINOP_MUL:
9866 case BINOP_DIV:
9867 case BINOP_REM:
9868 case BINOP_MOD:
9869 case BINOP_LSH:
9870 case BINOP_RSH:
9871 case BINOP_LOGICAL_AND:
9872 case BINOP_LOGICAL_OR:
9873 case BINOP_BITWISE_AND:
9874 case BINOP_BITWISE_IOR:
9875 case BINOP_BITWISE_XOR:
9876 case BINOP_EQUAL:
9877 case BINOP_NOTEQUAL:
9878 case BINOP_LESS:
9879 case BINOP_GTR:
9880 case BINOP_LEQ:
9881 case BINOP_GEQ:
9882 case BINOP_REPEAT:
9883 case BINOP_COMMA:
9884 case BINOP_EXP:
9885 case BINOP_MIN:
9886 case BINOP_MAX:
9887 case BINOP_INTDIV:
9888 case BINOP_CONCAT:
9889 case TERNOP_COND:
9890 case TERNOP_SLICE:
9891
9892 case OP_LONG:
9893 case OP_FLOAT:
9894 case OP_LAST:
9895 case OP_COMPLEX:
9896 case OP_STRING:
9897 case OP_ARRAY:
9898 case OP_TYPE:
9899 case OP_TYPEOF:
9900 case OP_DECLTYPE:
9901 case OP_TYPEID:
9902 case OP_NAME:
9903 case OP_OBJC_NSSTRING:
9904
9905 case UNOP_NEG:
9906 case UNOP_LOGICAL_NOT:
9907 case UNOP_COMPLEMENT:
9908 case UNOP_ADDR:
9909 case UNOP_HIGH:
9910 case UNOP_CAST:
9911
9912 case UNOP_CAST_TYPE:
9913 case UNOP_REINTERPRET_CAST:
9914 case UNOP_DYNAMIC_CAST:
9915 /* Unary, binary and ternary operators: We have to check
9916 their operands. If they are constant, then so is the
9917 result of that operation. For instance, if A and B are
9918 determined to be constants, then so is "A + B".
9919
9920 UNOP_IND is one exception to the rule above, because the
9921 value of *ADDR is not necessarily a constant, even when
9922 ADDR is. */
9923 break;
9924
9925 case OP_VAR_VALUE:
9926 /* Check whether the associated symbol is a constant.
9927
9928 We use SYMBOL_CLASS rather than TYPE_CONST because it's
9929 possible that a buggy compiler could mark a variable as
9930 constant even when it is not, and TYPE_CONST would return
9931 true in this case, while SYMBOL_CLASS wouldn't.
9932
9933 We also have to check for function symbols because they
9934 are always constant. */
9935 {
9936 struct symbol *s = exp->elts[i + 2].symbol;
9937
9938 if (SYMBOL_CLASS (s) != LOC_BLOCK
9939 && SYMBOL_CLASS (s) != LOC_CONST
9940 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9941 return 0;
9942 break;
9943 }
9944
9945 /* The default action is to return 0 because we are using
9946 the optimistic approach here: If we don't know something,
9947 then it is not a constant. */
9948 default:
9949 return 0;
9950 }
9951 }
9952
9953 return 1;
9954 }
9955
9956 /* Watchpoint destructor. */
9957
9958 watchpoint::~watchpoint ()
9959 {
9960 xfree (this->exp_string);
9961 xfree (this->exp_string_reparse);
9962 }
9963
9964 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
9965
9966 static void
9967 re_set_watchpoint (struct breakpoint *b)
9968 {
9969 struct watchpoint *w = (struct watchpoint *) b;
9970
9971 /* Watchpoint can be either on expression using entirely global
9972 variables, or it can be on local variables.
9973
9974 Watchpoints of the first kind are never auto-deleted, and even
9975 persist across program restarts. Since they can use variables
9976 from shared libraries, we need to reparse expression as libraries
9977 are loaded and unloaded.
9978
9979 Watchpoints on local variables can also change meaning as result
9980 of solib event. For example, if a watchpoint uses both a local
9981 and a global variables in expression, it's a local watchpoint,
9982 but unloading of a shared library will make the expression
9983 invalid. This is not a very common use case, but we still
9984 re-evaluate expression, to avoid surprises to the user.
9985
9986 Note that for local watchpoints, we re-evaluate it only if
9987 watchpoints frame id is still valid. If it's not, it means the
9988 watchpoint is out of scope and will be deleted soon. In fact,
9989 I'm not sure we'll ever be called in this case.
9990
9991 If a local watchpoint's frame id is still valid, then
9992 w->exp_valid_block is likewise valid, and we can safely use it.
9993
9994 Don't do anything about disabled watchpoints, since they will be
9995 reevaluated again when enabled. */
9996 update_watchpoint (w, 1 /* reparse */);
9997 }
9998
9999 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10000
10001 static int
10002 insert_watchpoint (struct bp_location *bl)
10003 {
10004 struct watchpoint *w = (struct watchpoint *) bl->owner;
10005 int length = w->exact ? 1 : bl->length;
10006
10007 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10008 w->cond_exp.get ());
10009 }
10010
10011 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10012
10013 static int
10014 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10015 {
10016 struct watchpoint *w = (struct watchpoint *) bl->owner;
10017 int length = w->exact ? 1 : bl->length;
10018
10019 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10020 w->cond_exp.get ());
10021 }
10022
10023 static int
10024 breakpoint_hit_watchpoint (const struct bp_location *bl,
10025 const address_space *aspace, CORE_ADDR bp_addr,
10026 const struct target_waitstatus *ws)
10027 {
10028 struct breakpoint *b = bl->owner;
10029 struct watchpoint *w = (struct watchpoint *) b;
10030
10031 /* Continuable hardware watchpoints are treated as non-existent if the
10032 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10033 some data address). Otherwise gdb won't stop on a break instruction
10034 in the code (not from a breakpoint) when a hardware watchpoint has
10035 been defined. Also skip watchpoints which we know did not trigger
10036 (did not match the data address). */
10037 if (is_hardware_watchpoint (b)
10038 && w->watchpoint_triggered == watch_triggered_no)
10039 return 0;
10040
10041 return 1;
10042 }
10043
10044 static void
10045 check_status_watchpoint (bpstat bs)
10046 {
10047 gdb_assert (is_watchpoint (bs->breakpoint_at));
10048
10049 bpstat_check_watchpoint (bs);
10050 }
10051
10052 /* Implement the "resources_needed" breakpoint_ops method for
10053 hardware watchpoints. */
10054
10055 static int
10056 resources_needed_watchpoint (const struct bp_location *bl)
10057 {
10058 struct watchpoint *w = (struct watchpoint *) bl->owner;
10059 int length = w->exact? 1 : bl->length;
10060
10061 return target_region_ok_for_hw_watchpoint (bl->address, length);
10062 }
10063
10064 /* Implement the "works_in_software_mode" breakpoint_ops method for
10065 hardware watchpoints. */
10066
10067 static int
10068 works_in_software_mode_watchpoint (const struct breakpoint *b)
10069 {
10070 /* Read and access watchpoints only work with hardware support. */
10071 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10072 }
10073
10074 static enum print_stop_action
10075 print_it_watchpoint (bpstat bs)
10076 {
10077 struct breakpoint *b;
10078 enum print_stop_action result;
10079 struct watchpoint *w;
10080 struct ui_out *uiout = current_uiout;
10081
10082 gdb_assert (bs->bp_location_at != NULL);
10083
10084 b = bs->breakpoint_at;
10085 w = (struct watchpoint *) b;
10086
10087 annotate_watchpoint (b->number);
10088 maybe_print_thread_hit_breakpoint (uiout);
10089
10090 string_file stb;
10091
10092 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10093 switch (b->type)
10094 {
10095 case bp_watchpoint:
10096 case bp_hardware_watchpoint:
10097 if (uiout->is_mi_like_p ())
10098 uiout->field_string
10099 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10100 mention (b);
10101 tuple_emitter.emplace (uiout, "value");
10102 uiout->text ("\nOld value = ");
10103 watchpoint_value_print (bs->old_val.get (), &stb);
10104 uiout->field_stream ("old", stb);
10105 uiout->text ("\nNew value = ");
10106 watchpoint_value_print (w->val.get (), &stb);
10107 uiout->field_stream ("new", stb);
10108 uiout->text ("\n");
10109 /* More than one watchpoint may have been triggered. */
10110 result = PRINT_UNKNOWN;
10111 break;
10112
10113 case bp_read_watchpoint:
10114 if (uiout->is_mi_like_p ())
10115 uiout->field_string
10116 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10117 mention (b);
10118 tuple_emitter.emplace (uiout, "value");
10119 uiout->text ("\nValue = ");
10120 watchpoint_value_print (w->val.get (), &stb);
10121 uiout->field_stream ("value", stb);
10122 uiout->text ("\n");
10123 result = PRINT_UNKNOWN;
10124 break;
10125
10126 case bp_access_watchpoint:
10127 if (bs->old_val != NULL)
10128 {
10129 if (uiout->is_mi_like_p ())
10130 uiout->field_string
10131 ("reason",
10132 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10133 mention (b);
10134 tuple_emitter.emplace (uiout, "value");
10135 uiout->text ("\nOld value = ");
10136 watchpoint_value_print (bs->old_val.get (), &stb);
10137 uiout->field_stream ("old", stb);
10138 uiout->text ("\nNew value = ");
10139 }
10140 else
10141 {
10142 mention (b);
10143 if (uiout->is_mi_like_p ())
10144 uiout->field_string
10145 ("reason",
10146 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10147 tuple_emitter.emplace (uiout, "value");
10148 uiout->text ("\nValue = ");
10149 }
10150 watchpoint_value_print (w->val.get (), &stb);
10151 uiout->field_stream ("new", stb);
10152 uiout->text ("\n");
10153 result = PRINT_UNKNOWN;
10154 break;
10155 default:
10156 result = PRINT_UNKNOWN;
10157 }
10158
10159 return result;
10160 }
10161
10162 /* Implement the "print_mention" breakpoint_ops method for hardware
10163 watchpoints. */
10164
10165 static void
10166 print_mention_watchpoint (struct breakpoint *b)
10167 {
10168 struct watchpoint *w = (struct watchpoint *) b;
10169 struct ui_out *uiout = current_uiout;
10170 const char *tuple_name;
10171
10172 switch (b->type)
10173 {
10174 case bp_watchpoint:
10175 uiout->text ("Watchpoint ");
10176 tuple_name = "wpt";
10177 break;
10178 case bp_hardware_watchpoint:
10179 uiout->text ("Hardware watchpoint ");
10180 tuple_name = "wpt";
10181 break;
10182 case bp_read_watchpoint:
10183 uiout->text ("Hardware read watchpoint ");
10184 tuple_name = "hw-rwpt";
10185 break;
10186 case bp_access_watchpoint:
10187 uiout->text ("Hardware access (read/write) watchpoint ");
10188 tuple_name = "hw-awpt";
10189 break;
10190 default:
10191 internal_error (__FILE__, __LINE__,
10192 _("Invalid hardware watchpoint type."));
10193 }
10194
10195 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10196 uiout->field_signed ("number", b->number);
10197 uiout->text (": ");
10198 uiout->field_string ("exp", w->exp_string);
10199 }
10200
10201 /* Implement the "print_recreate" breakpoint_ops method for
10202 watchpoints. */
10203
10204 static void
10205 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10206 {
10207 struct watchpoint *w = (struct watchpoint *) b;
10208
10209 switch (b->type)
10210 {
10211 case bp_watchpoint:
10212 case bp_hardware_watchpoint:
10213 fprintf_unfiltered (fp, "watch");
10214 break;
10215 case bp_read_watchpoint:
10216 fprintf_unfiltered (fp, "rwatch");
10217 break;
10218 case bp_access_watchpoint:
10219 fprintf_unfiltered (fp, "awatch");
10220 break;
10221 default:
10222 internal_error (__FILE__, __LINE__,
10223 _("Invalid watchpoint type."));
10224 }
10225
10226 fprintf_unfiltered (fp, " %s", w->exp_string);
10227 print_recreate_thread (b, fp);
10228 }
10229
10230 /* Implement the "explains_signal" breakpoint_ops method for
10231 watchpoints. */
10232
10233 static int
10234 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10235 {
10236 /* A software watchpoint cannot cause a signal other than
10237 GDB_SIGNAL_TRAP. */
10238 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10239 return 0;
10240
10241 return 1;
10242 }
10243
10244 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10245
10246 static struct breakpoint_ops watchpoint_breakpoint_ops;
10247
10248 /* Implement the "insert" breakpoint_ops method for
10249 masked hardware watchpoints. */
10250
10251 static int
10252 insert_masked_watchpoint (struct bp_location *bl)
10253 {
10254 struct watchpoint *w = (struct watchpoint *) bl->owner;
10255
10256 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10257 bl->watchpoint_type);
10258 }
10259
10260 /* Implement the "remove" breakpoint_ops method for
10261 masked hardware watchpoints. */
10262
10263 static int
10264 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10265 {
10266 struct watchpoint *w = (struct watchpoint *) bl->owner;
10267
10268 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10269 bl->watchpoint_type);
10270 }
10271
10272 /* Implement the "resources_needed" breakpoint_ops method for
10273 masked hardware watchpoints. */
10274
10275 static int
10276 resources_needed_masked_watchpoint (const struct bp_location *bl)
10277 {
10278 struct watchpoint *w = (struct watchpoint *) bl->owner;
10279
10280 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10281 }
10282
10283 /* Implement the "works_in_software_mode" breakpoint_ops method for
10284 masked hardware watchpoints. */
10285
10286 static int
10287 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10288 {
10289 return 0;
10290 }
10291
10292 /* Implement the "print_it" breakpoint_ops method for
10293 masked hardware watchpoints. */
10294
10295 static enum print_stop_action
10296 print_it_masked_watchpoint (bpstat bs)
10297 {
10298 struct breakpoint *b = bs->breakpoint_at;
10299 struct ui_out *uiout = current_uiout;
10300
10301 /* Masked watchpoints have only one location. */
10302 gdb_assert (b->loc && b->loc->next == NULL);
10303
10304 annotate_watchpoint (b->number);
10305 maybe_print_thread_hit_breakpoint (uiout);
10306
10307 switch (b->type)
10308 {
10309 case bp_hardware_watchpoint:
10310 if (uiout->is_mi_like_p ())
10311 uiout->field_string
10312 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10313 break;
10314
10315 case bp_read_watchpoint:
10316 if (uiout->is_mi_like_p ())
10317 uiout->field_string
10318 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10319 break;
10320
10321 case bp_access_watchpoint:
10322 if (uiout->is_mi_like_p ())
10323 uiout->field_string
10324 ("reason",
10325 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10326 break;
10327 default:
10328 internal_error (__FILE__, __LINE__,
10329 _("Invalid hardware watchpoint type."));
10330 }
10331
10332 mention (b);
10333 uiout->text (_("\n\
10334 Check the underlying instruction at PC for the memory\n\
10335 address and value which triggered this watchpoint.\n"));
10336 uiout->text ("\n");
10337
10338 /* More than one watchpoint may have been triggered. */
10339 return PRINT_UNKNOWN;
10340 }
10341
10342 /* Implement the "print_one_detail" breakpoint_ops method for
10343 masked hardware watchpoints. */
10344
10345 static void
10346 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10347 struct ui_out *uiout)
10348 {
10349 struct watchpoint *w = (struct watchpoint *) b;
10350
10351 /* Masked watchpoints have only one location. */
10352 gdb_assert (b->loc && b->loc->next == NULL);
10353
10354 uiout->text ("\tmask ");
10355 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10356 uiout->text ("\n");
10357 }
10358
10359 /* Implement the "print_mention" breakpoint_ops method for
10360 masked hardware watchpoints. */
10361
10362 static void
10363 print_mention_masked_watchpoint (struct breakpoint *b)
10364 {
10365 struct watchpoint *w = (struct watchpoint *) b;
10366 struct ui_out *uiout = current_uiout;
10367 const char *tuple_name;
10368
10369 switch (b->type)
10370 {
10371 case bp_hardware_watchpoint:
10372 uiout->text ("Masked hardware watchpoint ");
10373 tuple_name = "wpt";
10374 break;
10375 case bp_read_watchpoint:
10376 uiout->text ("Masked hardware read watchpoint ");
10377 tuple_name = "hw-rwpt";
10378 break;
10379 case bp_access_watchpoint:
10380 uiout->text ("Masked hardware access (read/write) watchpoint ");
10381 tuple_name = "hw-awpt";
10382 break;
10383 default:
10384 internal_error (__FILE__, __LINE__,
10385 _("Invalid hardware watchpoint type."));
10386 }
10387
10388 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10389 uiout->field_signed ("number", b->number);
10390 uiout->text (": ");
10391 uiout->field_string ("exp", w->exp_string);
10392 }
10393
10394 /* Implement the "print_recreate" breakpoint_ops method for
10395 masked hardware watchpoints. */
10396
10397 static void
10398 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10399 {
10400 struct watchpoint *w = (struct watchpoint *) b;
10401 char tmp[40];
10402
10403 switch (b->type)
10404 {
10405 case bp_hardware_watchpoint:
10406 fprintf_unfiltered (fp, "watch");
10407 break;
10408 case bp_read_watchpoint:
10409 fprintf_unfiltered (fp, "rwatch");
10410 break;
10411 case bp_access_watchpoint:
10412 fprintf_unfiltered (fp, "awatch");
10413 break;
10414 default:
10415 internal_error (__FILE__, __LINE__,
10416 _("Invalid hardware watchpoint type."));
10417 }
10418
10419 sprintf_vma (tmp, w->hw_wp_mask);
10420 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10421 print_recreate_thread (b, fp);
10422 }
10423
10424 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10425
10426 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10427
10428 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10429
10430 static bool
10431 is_masked_watchpoint (const struct breakpoint *b)
10432 {
10433 return b->ops == &masked_watchpoint_breakpoint_ops;
10434 }
10435
10436 /* accessflag: hw_write: watch write,
10437 hw_read: watch read,
10438 hw_access: watch access (read or write) */
10439 static void
10440 watch_command_1 (const char *arg, int accessflag, int from_tty,
10441 int just_location, int internal)
10442 {
10443 struct breakpoint *scope_breakpoint = NULL;
10444 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10445 struct value *result;
10446 int saved_bitpos = 0, saved_bitsize = 0;
10447 const char *exp_start = NULL;
10448 const char *exp_end = NULL;
10449 const char *tok, *end_tok;
10450 int toklen = -1;
10451 const char *cond_start = NULL;
10452 const char *cond_end = NULL;
10453 enum bptype bp_type;
10454 int thread = -1;
10455 int pc = 0;
10456 /* Flag to indicate whether we are going to use masks for
10457 the hardware watchpoint. */
10458 int use_mask = 0;
10459 CORE_ADDR mask = 0;
10460
10461 /* Make sure that we actually have parameters to parse. */
10462 if (arg != NULL && arg[0] != '\0')
10463 {
10464 const char *value_start;
10465
10466 exp_end = arg + strlen (arg);
10467
10468 /* Look for "parameter value" pairs at the end
10469 of the arguments string. */
10470 for (tok = exp_end - 1; tok > arg; tok--)
10471 {
10472 /* Skip whitespace at the end of the argument list. */
10473 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10474 tok--;
10475
10476 /* Find the beginning of the last token.
10477 This is the value of the parameter. */
10478 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10479 tok--;
10480 value_start = tok + 1;
10481
10482 /* Skip whitespace. */
10483 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10484 tok--;
10485
10486 end_tok = tok;
10487
10488 /* Find the beginning of the second to last token.
10489 This is the parameter itself. */
10490 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10491 tok--;
10492 tok++;
10493 toklen = end_tok - tok + 1;
10494
10495 if (toklen == 6 && startswith (tok, "thread"))
10496 {
10497 struct thread_info *thr;
10498 /* At this point we've found a "thread" token, which means
10499 the user is trying to set a watchpoint that triggers
10500 only in a specific thread. */
10501 const char *endp;
10502
10503 if (thread != -1)
10504 error(_("You can specify only one thread."));
10505
10506 /* Extract the thread ID from the next token. */
10507 thr = parse_thread_id (value_start, &endp);
10508
10509 /* Check if the user provided a valid thread ID. */
10510 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10511 invalid_thread_id_error (value_start);
10512
10513 thread = thr->global_num;
10514 }
10515 else if (toklen == 4 && startswith (tok, "mask"))
10516 {
10517 /* We've found a "mask" token, which means the user wants to
10518 create a hardware watchpoint that is going to have the mask
10519 facility. */
10520 struct value *mask_value, *mark;
10521
10522 if (use_mask)
10523 error(_("You can specify only one mask."));
10524
10525 use_mask = just_location = 1;
10526
10527 mark = value_mark ();
10528 mask_value = parse_to_comma_and_eval (&value_start);
10529 mask = value_as_address (mask_value);
10530 value_free_to_mark (mark);
10531 }
10532 else
10533 /* We didn't recognize what we found. We should stop here. */
10534 break;
10535
10536 /* Truncate the string and get rid of the "parameter value" pair before
10537 the arguments string is parsed by the parse_exp_1 function. */
10538 exp_end = tok;
10539 }
10540 }
10541 else
10542 exp_end = arg;
10543
10544 /* Parse the rest of the arguments. From here on out, everything
10545 is in terms of a newly allocated string instead of the original
10546 ARG. */
10547 std::string expression (arg, exp_end - arg);
10548 exp_start = arg = expression.c_str ();
10549 innermost_block_tracker tracker;
10550 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10551 exp_end = arg;
10552 /* Remove trailing whitespace from the expression before saving it.
10553 This makes the eventual display of the expression string a bit
10554 prettier. */
10555 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10556 --exp_end;
10557
10558 /* Checking if the expression is not constant. */
10559 if (watchpoint_exp_is_const (exp.get ()))
10560 {
10561 int len;
10562
10563 len = exp_end - exp_start;
10564 while (len > 0 && isspace (exp_start[len - 1]))
10565 len--;
10566 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10567 }
10568
10569 exp_valid_block = tracker.block ();
10570 struct value *mark = value_mark ();
10571 struct value *val_as_value = nullptr;
10572 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10573 just_location);
10574
10575 if (val_as_value != NULL && just_location)
10576 {
10577 saved_bitpos = value_bitpos (val_as_value);
10578 saved_bitsize = value_bitsize (val_as_value);
10579 }
10580
10581 value_ref_ptr val;
10582 if (just_location)
10583 {
10584 int ret;
10585
10586 exp_valid_block = NULL;
10587 val = release_value (value_addr (result));
10588 value_free_to_mark (mark);
10589
10590 if (use_mask)
10591 {
10592 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10593 mask);
10594 if (ret == -1)
10595 error (_("This target does not support masked watchpoints."));
10596 else if (ret == -2)
10597 error (_("Invalid mask or memory region."));
10598 }
10599 }
10600 else if (val_as_value != NULL)
10601 val = release_value (val_as_value);
10602
10603 tok = skip_spaces (arg);
10604 end_tok = skip_to_space (tok);
10605
10606 toklen = end_tok - tok;
10607 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10608 {
10609 tok = cond_start = end_tok + 1;
10610 innermost_block_tracker if_tracker;
10611 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10612
10613 /* The watchpoint expression may not be local, but the condition
10614 may still be. E.g.: `watch global if local > 0'. */
10615 cond_exp_valid_block = if_tracker.block ();
10616
10617 cond_end = tok;
10618 }
10619 if (*tok)
10620 error (_("Junk at end of command."));
10621
10622 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10623
10624 /* Save this because create_internal_breakpoint below invalidates
10625 'wp_frame'. */
10626 frame_id watchpoint_frame = get_frame_id (wp_frame);
10627
10628 /* If the expression is "local", then set up a "watchpoint scope"
10629 breakpoint at the point where we've left the scope of the watchpoint
10630 expression. Create the scope breakpoint before the watchpoint, so
10631 that we will encounter it first in bpstat_stop_status. */
10632 if (exp_valid_block != NULL && wp_frame != NULL)
10633 {
10634 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10635
10636 if (frame_id_p (caller_frame_id))
10637 {
10638 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10639 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10640
10641 scope_breakpoint
10642 = create_internal_breakpoint (caller_arch, caller_pc,
10643 bp_watchpoint_scope,
10644 &momentary_breakpoint_ops);
10645
10646 /* create_internal_breakpoint could invalidate WP_FRAME. */
10647 wp_frame = NULL;
10648
10649 scope_breakpoint->enable_state = bp_enabled;
10650
10651 /* Automatically delete the breakpoint when it hits. */
10652 scope_breakpoint->disposition = disp_del;
10653
10654 /* Only break in the proper frame (help with recursion). */
10655 scope_breakpoint->frame_id = caller_frame_id;
10656
10657 /* Set the address at which we will stop. */
10658 scope_breakpoint->loc->gdbarch = caller_arch;
10659 scope_breakpoint->loc->requested_address = caller_pc;
10660 scope_breakpoint->loc->address
10661 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10662 scope_breakpoint->loc->requested_address,
10663 scope_breakpoint->type);
10664 }
10665 }
10666
10667 /* Now set up the breakpoint. We create all watchpoints as hardware
10668 watchpoints here even if hardware watchpoints are turned off, a call
10669 to update_watchpoint later in this function will cause the type to
10670 drop back to bp_watchpoint (software watchpoint) if required. */
10671
10672 if (accessflag == hw_read)
10673 bp_type = bp_read_watchpoint;
10674 else if (accessflag == hw_access)
10675 bp_type = bp_access_watchpoint;
10676 else
10677 bp_type = bp_hardware_watchpoint;
10678
10679 std::unique_ptr<watchpoint> w (new watchpoint ());
10680
10681 if (use_mask)
10682 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10683 &masked_watchpoint_breakpoint_ops);
10684 else
10685 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10686 &watchpoint_breakpoint_ops);
10687 w->thread = thread;
10688 w->disposition = disp_donttouch;
10689 w->pspace = current_program_space;
10690 w->exp = std::move (exp);
10691 w->exp_valid_block = exp_valid_block;
10692 w->cond_exp_valid_block = cond_exp_valid_block;
10693 if (just_location)
10694 {
10695 struct type *t = value_type (val.get ());
10696 CORE_ADDR addr = value_as_address (val.get ());
10697
10698 w->exp_string_reparse
10699 = current_language->la_watch_location_expression (t, addr).release ();
10700
10701 w->exp_string = xstrprintf ("-location %.*s",
10702 (int) (exp_end - exp_start), exp_start);
10703 }
10704 else
10705 w->exp_string = savestring (exp_start, exp_end - exp_start);
10706
10707 if (use_mask)
10708 {
10709 w->hw_wp_mask = mask;
10710 }
10711 else
10712 {
10713 w->val = val;
10714 w->val_bitpos = saved_bitpos;
10715 w->val_bitsize = saved_bitsize;
10716 w->val_valid = true;
10717 }
10718
10719 if (cond_start)
10720 w->cond_string = savestring (cond_start, cond_end - cond_start);
10721 else
10722 w->cond_string = 0;
10723
10724 if (frame_id_p (watchpoint_frame))
10725 {
10726 w->watchpoint_frame = watchpoint_frame;
10727 w->watchpoint_thread = inferior_ptid;
10728 }
10729 else
10730 {
10731 w->watchpoint_frame = null_frame_id;
10732 w->watchpoint_thread = null_ptid;
10733 }
10734
10735 if (scope_breakpoint != NULL)
10736 {
10737 /* The scope breakpoint is related to the watchpoint. We will
10738 need to act on them together. */
10739 w->related_breakpoint = scope_breakpoint;
10740 scope_breakpoint->related_breakpoint = w.get ();
10741 }
10742
10743 if (!just_location)
10744 value_free_to_mark (mark);
10745
10746 /* Finally update the new watchpoint. This creates the locations
10747 that should be inserted. */
10748 update_watchpoint (w.get (), 1);
10749
10750 install_breakpoint (internal, std::move (w), 1);
10751 }
10752
10753 /* Return count of debug registers needed to watch the given expression.
10754 If the watchpoint cannot be handled in hardware return zero. */
10755
10756 static int
10757 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10758 {
10759 int found_memory_cnt = 0;
10760
10761 /* Did the user specifically forbid us to use hardware watchpoints? */
10762 if (!can_use_hw_watchpoints)
10763 return 0;
10764
10765 gdb_assert (!vals.empty ());
10766 struct value *head = vals[0].get ();
10767
10768 /* Make sure that the value of the expression depends only upon
10769 memory contents, and values computed from them within GDB. If we
10770 find any register references or function calls, we can't use a
10771 hardware watchpoint.
10772
10773 The idea here is that evaluating an expression generates a series
10774 of values, one holding the value of every subexpression. (The
10775 expression a*b+c has five subexpressions: a, b, a*b, c, and
10776 a*b+c.) GDB's values hold almost enough information to establish
10777 the criteria given above --- they identify memory lvalues,
10778 register lvalues, computed values, etcetera. So we can evaluate
10779 the expression, and then scan the chain of values that leaves
10780 behind to decide whether we can detect any possible change to the
10781 expression's final value using only hardware watchpoints.
10782
10783 However, I don't think that the values returned by inferior
10784 function calls are special in any way. So this function may not
10785 notice that an expression involving an inferior function call
10786 can't be watched with hardware watchpoints. FIXME. */
10787 for (const value_ref_ptr &iter : vals)
10788 {
10789 struct value *v = iter.get ();
10790
10791 if (VALUE_LVAL (v) == lval_memory)
10792 {
10793 if (v != head && value_lazy (v))
10794 /* A lazy memory lvalue in the chain is one that GDB never
10795 needed to fetch; we either just used its address (e.g.,
10796 `a' in `a.b') or we never needed it at all (e.g., `a'
10797 in `a,b'). This doesn't apply to HEAD; if that is
10798 lazy then it was not readable, but watch it anyway. */
10799 ;
10800 else
10801 {
10802 /* Ahh, memory we actually used! Check if we can cover
10803 it with hardware watchpoints. */
10804 struct type *vtype = check_typedef (value_type (v));
10805
10806 /* We only watch structs and arrays if user asked for it
10807 explicitly, never if they just happen to appear in a
10808 middle of some value chain. */
10809 if (v == head
10810 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10811 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10812 {
10813 CORE_ADDR vaddr = value_address (v);
10814 int len;
10815 int num_regs;
10816
10817 len = (target_exact_watchpoints
10818 && is_scalar_type_recursive (vtype))?
10819 1 : TYPE_LENGTH (value_type (v));
10820
10821 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10822 if (!num_regs)
10823 return 0;
10824 else
10825 found_memory_cnt += num_regs;
10826 }
10827 }
10828 }
10829 else if (VALUE_LVAL (v) != not_lval
10830 && deprecated_value_modifiable (v) == 0)
10831 return 0; /* These are values from the history (e.g., $1). */
10832 else if (VALUE_LVAL (v) == lval_register)
10833 return 0; /* Cannot watch a register with a HW watchpoint. */
10834 }
10835
10836 /* The expression itself looks suitable for using a hardware
10837 watchpoint, but give the target machine a chance to reject it. */
10838 return found_memory_cnt;
10839 }
10840
10841 void
10842 watch_command_wrapper (const char *arg, int from_tty, int internal)
10843 {
10844 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10845 }
10846
10847 /* A helper function that looks for the "-location" argument and then
10848 calls watch_command_1. */
10849
10850 static void
10851 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10852 {
10853 int just_location = 0;
10854
10855 if (arg
10856 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10857 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10858 just_location = 1;
10859
10860 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10861 }
10862
10863 static void
10864 watch_command (const char *arg, int from_tty)
10865 {
10866 watch_maybe_just_location (arg, hw_write, from_tty);
10867 }
10868
10869 void
10870 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
10871 {
10872 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10873 }
10874
10875 static void
10876 rwatch_command (const char *arg, int from_tty)
10877 {
10878 watch_maybe_just_location (arg, hw_read, from_tty);
10879 }
10880
10881 void
10882 awatch_command_wrapper (const char *arg, int from_tty, int internal)
10883 {
10884 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10885 }
10886
10887 static void
10888 awatch_command (const char *arg, int from_tty)
10889 {
10890 watch_maybe_just_location (arg, hw_access, from_tty);
10891 }
10892 \f
10893
10894 /* Data for the FSM that manages the until(location)/advance commands
10895 in infcmd.c. Here because it uses the mechanisms of
10896 breakpoints. */
10897
10898 struct until_break_fsm : public thread_fsm
10899 {
10900 /* The thread that was current when the command was executed. */
10901 int thread;
10902
10903 /* The breakpoint set at the destination location. */
10904 breakpoint_up location_breakpoint;
10905
10906 /* Breakpoint set at the return address in the caller frame. May be
10907 NULL. */
10908 breakpoint_up caller_breakpoint;
10909
10910 until_break_fsm (struct interp *cmd_interp, int thread,
10911 breakpoint_up &&location_breakpoint,
10912 breakpoint_up &&caller_breakpoint)
10913 : thread_fsm (cmd_interp),
10914 thread (thread),
10915 location_breakpoint (std::move (location_breakpoint)),
10916 caller_breakpoint (std::move (caller_breakpoint))
10917 {
10918 }
10919
10920 void clean_up (struct thread_info *thread) override;
10921 bool should_stop (struct thread_info *thread) override;
10922 enum async_reply_reason do_async_reply_reason () override;
10923 };
10924
10925 /* Implementation of the 'should_stop' FSM method for the
10926 until(location)/advance commands. */
10927
10928 bool
10929 until_break_fsm::should_stop (struct thread_info *tp)
10930 {
10931 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10932 location_breakpoint.get ()) != NULL
10933 || (caller_breakpoint != NULL
10934 && bpstat_find_breakpoint (tp->control.stop_bpstat,
10935 caller_breakpoint.get ()) != NULL))
10936 set_finished ();
10937
10938 return true;
10939 }
10940
10941 /* Implementation of the 'clean_up' FSM method for the
10942 until(location)/advance commands. */
10943
10944 void
10945 until_break_fsm::clean_up (struct thread_info *)
10946 {
10947 /* Clean up our temporary breakpoints. */
10948 location_breakpoint.reset ();
10949 caller_breakpoint.reset ();
10950 delete_longjmp_breakpoint (thread);
10951 }
10952
10953 /* Implementation of the 'async_reply_reason' FSM method for the
10954 until(location)/advance commands. */
10955
10956 enum async_reply_reason
10957 until_break_fsm::do_async_reply_reason ()
10958 {
10959 return EXEC_ASYNC_LOCATION_REACHED;
10960 }
10961
10962 void
10963 until_break_command (const char *arg, int from_tty, int anywhere)
10964 {
10965 struct frame_info *frame;
10966 struct gdbarch *frame_gdbarch;
10967 struct frame_id stack_frame_id;
10968 struct frame_id caller_frame_id;
10969 int thread;
10970 struct thread_info *tp;
10971
10972 clear_proceed_status (0);
10973
10974 /* Set a breakpoint where the user wants it and at return from
10975 this function. */
10976
10977 event_location_up location = string_to_event_location (&arg, current_language);
10978
10979 std::vector<symtab_and_line> sals
10980 = (last_displayed_sal_is_valid ()
10981 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10982 get_last_displayed_symtab (),
10983 get_last_displayed_line ())
10984 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
10985 NULL, NULL, 0));
10986
10987 if (sals.size () != 1)
10988 error (_("Couldn't get information on specified line."));
10989
10990 symtab_and_line &sal = sals[0];
10991
10992 if (*arg)
10993 error (_("Junk at end of arguments."));
10994
10995 resolve_sal_pc (&sal);
10996
10997 tp = inferior_thread ();
10998 thread = tp->global_num;
10999
11000 /* Note linespec handling above invalidates the frame chain.
11001 Installing a breakpoint also invalidates the frame chain (as it
11002 may need to switch threads), so do any frame handling before
11003 that. */
11004
11005 frame = get_selected_frame (NULL);
11006 frame_gdbarch = get_frame_arch (frame);
11007 stack_frame_id = get_stack_frame_id (frame);
11008 caller_frame_id = frame_unwind_caller_id (frame);
11009
11010 /* Keep within the current frame, or in frames called by the current
11011 one. */
11012
11013 breakpoint_up caller_breakpoint;
11014
11015 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11016
11017 if (frame_id_p (caller_frame_id))
11018 {
11019 struct symtab_and_line sal2;
11020 struct gdbarch *caller_gdbarch;
11021
11022 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11023 sal2.pc = frame_unwind_caller_pc (frame);
11024 caller_gdbarch = frame_unwind_caller_arch (frame);
11025 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11026 sal2,
11027 caller_frame_id,
11028 bp_until);
11029
11030 set_longjmp_breakpoint (tp, caller_frame_id);
11031 lj_deleter.emplace (thread);
11032 }
11033
11034 /* set_momentary_breakpoint could invalidate FRAME. */
11035 frame = NULL;
11036
11037 breakpoint_up location_breakpoint;
11038 if (anywhere)
11039 /* If the user told us to continue until a specified location,
11040 we don't specify a frame at which we need to stop. */
11041 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11042 null_frame_id, bp_until);
11043 else
11044 /* Otherwise, specify the selected frame, because we want to stop
11045 only at the very same frame. */
11046 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11047 stack_frame_id, bp_until);
11048
11049 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11050 std::move (location_breakpoint),
11051 std::move (caller_breakpoint));
11052
11053 if (lj_deleter)
11054 lj_deleter->release ();
11055
11056 proceed (-1, GDB_SIGNAL_DEFAULT);
11057 }
11058
11059 /* This function attempts to parse an optional "if <cond>" clause
11060 from the arg string. If one is not found, it returns NULL.
11061
11062 Else, it returns a pointer to the condition string. (It does not
11063 attempt to evaluate the string against a particular block.) And,
11064 it updates arg to point to the first character following the parsed
11065 if clause in the arg string. */
11066
11067 const char *
11068 ep_parse_optional_if_clause (const char **arg)
11069 {
11070 const char *cond_string;
11071
11072 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11073 return NULL;
11074
11075 /* Skip the "if" keyword. */
11076 (*arg) += 2;
11077
11078 /* Skip any extra leading whitespace, and record the start of the
11079 condition string. */
11080 *arg = skip_spaces (*arg);
11081 cond_string = *arg;
11082
11083 /* Assume that the condition occupies the remainder of the arg
11084 string. */
11085 (*arg) += strlen (cond_string);
11086
11087 return cond_string;
11088 }
11089
11090 /* Commands to deal with catching events, such as signals, exceptions,
11091 process start/exit, etc. */
11092
11093 typedef enum
11094 {
11095 catch_fork_temporary, catch_vfork_temporary,
11096 catch_fork_permanent, catch_vfork_permanent
11097 }
11098 catch_fork_kind;
11099
11100 static void
11101 catch_fork_command_1 (const char *arg, int from_tty,
11102 struct cmd_list_element *command)
11103 {
11104 struct gdbarch *gdbarch = get_current_arch ();
11105 const char *cond_string = NULL;
11106 catch_fork_kind fork_kind;
11107 int tempflag;
11108
11109 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11110 tempflag = (fork_kind == catch_fork_temporary
11111 || fork_kind == catch_vfork_temporary);
11112
11113 if (!arg)
11114 arg = "";
11115 arg = skip_spaces (arg);
11116
11117 /* The allowed syntax is:
11118 catch [v]fork
11119 catch [v]fork if <cond>
11120
11121 First, check if there's an if clause. */
11122 cond_string = ep_parse_optional_if_clause (&arg);
11123
11124 if ((*arg != '\0') && !isspace (*arg))
11125 error (_("Junk at end of arguments."));
11126
11127 /* If this target supports it, create a fork or vfork catchpoint
11128 and enable reporting of such events. */
11129 switch (fork_kind)
11130 {
11131 case catch_fork_temporary:
11132 case catch_fork_permanent:
11133 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11134 &catch_fork_breakpoint_ops);
11135 break;
11136 case catch_vfork_temporary:
11137 case catch_vfork_permanent:
11138 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11139 &catch_vfork_breakpoint_ops);
11140 break;
11141 default:
11142 error (_("unsupported or unknown fork kind; cannot catch it"));
11143 break;
11144 }
11145 }
11146
11147 static void
11148 catch_exec_command_1 (const char *arg, int from_tty,
11149 struct cmd_list_element *command)
11150 {
11151 struct gdbarch *gdbarch = get_current_arch ();
11152 int tempflag;
11153 const char *cond_string = NULL;
11154
11155 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11156
11157 if (!arg)
11158 arg = "";
11159 arg = skip_spaces (arg);
11160
11161 /* The allowed syntax is:
11162 catch exec
11163 catch exec if <cond>
11164
11165 First, check if there's an if clause. */
11166 cond_string = ep_parse_optional_if_clause (&arg);
11167
11168 if ((*arg != '\0') && !isspace (*arg))
11169 error (_("Junk at end of arguments."));
11170
11171 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11172 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11173 &catch_exec_breakpoint_ops);
11174 c->exec_pathname = NULL;
11175
11176 install_breakpoint (0, std::move (c), 1);
11177 }
11178
11179 void
11180 init_ada_exception_breakpoint (struct breakpoint *b,
11181 struct gdbarch *gdbarch,
11182 struct symtab_and_line sal,
11183 const char *addr_string,
11184 const struct breakpoint_ops *ops,
11185 int tempflag,
11186 int enabled,
11187 int from_tty)
11188 {
11189 if (from_tty)
11190 {
11191 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11192 if (!loc_gdbarch)
11193 loc_gdbarch = gdbarch;
11194
11195 describe_other_breakpoints (loc_gdbarch,
11196 sal.pspace, sal.pc, sal.section, -1);
11197 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11198 version for exception catchpoints, because two catchpoints
11199 used for different exception names will use the same address.
11200 In this case, a "breakpoint ... also set at..." warning is
11201 unproductive. Besides, the warning phrasing is also a bit
11202 inappropriate, we should use the word catchpoint, and tell
11203 the user what type of catchpoint it is. The above is good
11204 enough for now, though. */
11205 }
11206
11207 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11208
11209 b->enable_state = enabled ? bp_enabled : bp_disabled;
11210 b->disposition = tempflag ? disp_del : disp_donttouch;
11211 b->location = string_to_event_location (&addr_string,
11212 language_def (language_ada));
11213 b->language = language_ada;
11214 }
11215
11216 static void
11217 catch_command (const char *arg, int from_tty)
11218 {
11219 error (_("Catch requires an event name."));
11220 }
11221 \f
11222
11223 static void
11224 tcatch_command (const char *arg, int from_tty)
11225 {
11226 error (_("Catch requires an event name."));
11227 }
11228
11229 /* Compare two breakpoints and return a strcmp-like result. */
11230
11231 static int
11232 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11233 {
11234 uintptr_t ua = (uintptr_t) a;
11235 uintptr_t ub = (uintptr_t) b;
11236
11237 if (a->number < b->number)
11238 return -1;
11239 else if (a->number > b->number)
11240 return 1;
11241
11242 /* Now sort by address, in case we see, e..g, two breakpoints with
11243 the number 0. */
11244 if (ua < ub)
11245 return -1;
11246 return ua > ub ? 1 : 0;
11247 }
11248
11249 /* Delete breakpoints by address or line. */
11250
11251 static void
11252 clear_command (const char *arg, int from_tty)
11253 {
11254 struct breakpoint *b;
11255 int default_match;
11256
11257 std::vector<symtab_and_line> decoded_sals;
11258 symtab_and_line last_sal;
11259 gdb::array_view<symtab_and_line> sals;
11260 if (arg)
11261 {
11262 decoded_sals
11263 = decode_line_with_current_source (arg,
11264 (DECODE_LINE_FUNFIRSTLINE
11265 | DECODE_LINE_LIST_MODE));
11266 default_match = 0;
11267 sals = decoded_sals;
11268 }
11269 else
11270 {
11271 /* Set sal's line, symtab, pc, and pspace to the values
11272 corresponding to the last call to print_frame_info. If the
11273 codepoint is not valid, this will set all the fields to 0. */
11274 last_sal = get_last_displayed_sal ();
11275 if (last_sal.symtab == 0)
11276 error (_("No source file specified."));
11277
11278 default_match = 1;
11279 sals = last_sal;
11280 }
11281
11282 /* We don't call resolve_sal_pc here. That's not as bad as it
11283 seems, because all existing breakpoints typically have both
11284 file/line and pc set. So, if clear is given file/line, we can
11285 match this to existing breakpoint without obtaining pc at all.
11286
11287 We only support clearing given the address explicitly
11288 present in breakpoint table. Say, we've set breakpoint
11289 at file:line. There were several PC values for that file:line,
11290 due to optimization, all in one block.
11291
11292 We've picked one PC value. If "clear" is issued with another
11293 PC corresponding to the same file:line, the breakpoint won't
11294 be cleared. We probably can still clear the breakpoint, but
11295 since the other PC value is never presented to user, user
11296 can only find it by guessing, and it does not seem important
11297 to support that. */
11298
11299 /* For each line spec given, delete bps which correspond to it. Do
11300 it in two passes, solely to preserve the current behavior that
11301 from_tty is forced true if we delete more than one
11302 breakpoint. */
11303
11304 std::vector<struct breakpoint *> found;
11305 for (const auto &sal : sals)
11306 {
11307 const char *sal_fullname;
11308
11309 /* If exact pc given, clear bpts at that pc.
11310 If line given (pc == 0), clear all bpts on specified line.
11311 If defaulting, clear all bpts on default line
11312 or at default pc.
11313
11314 defaulting sal.pc != 0 tests to do
11315
11316 0 1 pc
11317 1 1 pc _and_ line
11318 0 0 line
11319 1 0 <can't happen> */
11320
11321 sal_fullname = (sal.symtab == NULL
11322 ? NULL : symtab_to_fullname (sal.symtab));
11323
11324 /* Find all matching breakpoints and add them to 'found'. */
11325 ALL_BREAKPOINTS (b)
11326 {
11327 int match = 0;
11328 /* Are we going to delete b? */
11329 if (b->type != bp_none && !is_watchpoint (b))
11330 {
11331 struct bp_location *loc = b->loc;
11332 for (; loc; loc = loc->next)
11333 {
11334 /* If the user specified file:line, don't allow a PC
11335 match. This matches historical gdb behavior. */
11336 int pc_match = (!sal.explicit_line
11337 && sal.pc
11338 && (loc->pspace == sal.pspace)
11339 && (loc->address == sal.pc)
11340 && (!section_is_overlay (loc->section)
11341 || loc->section == sal.section));
11342 int line_match = 0;
11343
11344 if ((default_match || sal.explicit_line)
11345 && loc->symtab != NULL
11346 && sal_fullname != NULL
11347 && sal.pspace == loc->pspace
11348 && loc->line_number == sal.line
11349 && filename_cmp (symtab_to_fullname (loc->symtab),
11350 sal_fullname) == 0)
11351 line_match = 1;
11352
11353 if (pc_match || line_match)
11354 {
11355 match = 1;
11356 break;
11357 }
11358 }
11359 }
11360
11361 if (match)
11362 found.push_back (b);
11363 }
11364 }
11365
11366 /* Now go thru the 'found' chain and delete them. */
11367 if (found.empty ())
11368 {
11369 if (arg)
11370 error (_("No breakpoint at %s."), arg);
11371 else
11372 error (_("No breakpoint at this line."));
11373 }
11374
11375 /* Remove duplicates from the vec. */
11376 std::sort (found.begin (), found.end (),
11377 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11378 {
11379 return compare_breakpoints (bp_a, bp_b) < 0;
11380 });
11381 found.erase (std::unique (found.begin (), found.end (),
11382 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11383 {
11384 return compare_breakpoints (bp_a, bp_b) == 0;
11385 }),
11386 found.end ());
11387
11388 if (found.size () > 1)
11389 from_tty = 1; /* Always report if deleted more than one. */
11390 if (from_tty)
11391 {
11392 if (found.size () == 1)
11393 printf_unfiltered (_("Deleted breakpoint "));
11394 else
11395 printf_unfiltered (_("Deleted breakpoints "));
11396 }
11397
11398 for (breakpoint *iter : found)
11399 {
11400 if (from_tty)
11401 printf_unfiltered ("%d ", iter->number);
11402 delete_breakpoint (iter);
11403 }
11404 if (from_tty)
11405 putchar_unfiltered ('\n');
11406 }
11407 \f
11408 /* Delete breakpoint in BS if they are `delete' breakpoints and
11409 all breakpoints that are marked for deletion, whether hit or not.
11410 This is called after any breakpoint is hit, or after errors. */
11411
11412 void
11413 breakpoint_auto_delete (bpstat bs)
11414 {
11415 struct breakpoint *b, *b_tmp;
11416
11417 for (; bs; bs = bs->next)
11418 if (bs->breakpoint_at
11419 && bs->breakpoint_at->disposition == disp_del
11420 && bs->stop)
11421 delete_breakpoint (bs->breakpoint_at);
11422
11423 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11424 {
11425 if (b->disposition == disp_del_at_next_stop)
11426 delete_breakpoint (b);
11427 }
11428 }
11429
11430 /* A comparison function for bp_location AP and BP being interfaced to
11431 std::sort. Sort elements primarily by their ADDRESS (no matter what
11432 bl_address_is_meaningful says), secondarily by ordering first
11433 permanent elements and terciarily just ensuring the array is sorted
11434 stable way despite std::sort being an unstable algorithm. */
11435
11436 static int
11437 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11438 {
11439 if (a->address != b->address)
11440 return a->address < b->address;
11441
11442 /* Sort locations at the same address by their pspace number, keeping
11443 locations of the same inferior (in a multi-inferior environment)
11444 grouped. */
11445
11446 if (a->pspace->num != b->pspace->num)
11447 return a->pspace->num < b->pspace->num;
11448
11449 /* Sort permanent breakpoints first. */
11450 if (a->permanent != b->permanent)
11451 return a->permanent > b->permanent;
11452
11453 /* Make the internal GDB representation stable across GDB runs
11454 where A and B memory inside GDB can differ. Breakpoint locations of
11455 the same type at the same address can be sorted in arbitrary order. */
11456
11457 if (a->owner->number != b->owner->number)
11458 return a->owner->number < b->owner->number;
11459
11460 return a < b;
11461 }
11462
11463 /* Set bp_locations_placed_address_before_address_max and
11464 bp_locations_shadow_len_after_address_max according to the current
11465 content of the bp_locations array. */
11466
11467 static void
11468 bp_locations_target_extensions_update (void)
11469 {
11470 struct bp_location *bl, **blp_tmp;
11471
11472 bp_locations_placed_address_before_address_max = 0;
11473 bp_locations_shadow_len_after_address_max = 0;
11474
11475 ALL_BP_LOCATIONS (bl, blp_tmp)
11476 {
11477 CORE_ADDR start, end, addr;
11478
11479 if (!bp_location_has_shadow (bl))
11480 continue;
11481
11482 start = bl->target_info.placed_address;
11483 end = start + bl->target_info.shadow_len;
11484
11485 gdb_assert (bl->address >= start);
11486 addr = bl->address - start;
11487 if (addr > bp_locations_placed_address_before_address_max)
11488 bp_locations_placed_address_before_address_max = addr;
11489
11490 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11491
11492 gdb_assert (bl->address < end);
11493 addr = end - bl->address;
11494 if (addr > bp_locations_shadow_len_after_address_max)
11495 bp_locations_shadow_len_after_address_max = addr;
11496 }
11497 }
11498
11499 /* Download tracepoint locations if they haven't been. */
11500
11501 static void
11502 download_tracepoint_locations (void)
11503 {
11504 struct breakpoint *b;
11505 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11506
11507 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11508
11509 ALL_TRACEPOINTS (b)
11510 {
11511 struct bp_location *bl;
11512 struct tracepoint *t;
11513 int bp_location_downloaded = 0;
11514
11515 if ((b->type == bp_fast_tracepoint
11516 ? !may_insert_fast_tracepoints
11517 : !may_insert_tracepoints))
11518 continue;
11519
11520 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11521 {
11522 if (target_can_download_tracepoint ())
11523 can_download_tracepoint = TRIBOOL_TRUE;
11524 else
11525 can_download_tracepoint = TRIBOOL_FALSE;
11526 }
11527
11528 if (can_download_tracepoint == TRIBOOL_FALSE)
11529 break;
11530
11531 for (bl = b->loc; bl; bl = bl->next)
11532 {
11533 /* In tracepoint, locations are _never_ duplicated, so
11534 should_be_inserted is equivalent to
11535 unduplicated_should_be_inserted. */
11536 if (!should_be_inserted (bl) || bl->inserted)
11537 continue;
11538
11539 switch_to_program_space_and_thread (bl->pspace);
11540
11541 target_download_tracepoint (bl);
11542
11543 bl->inserted = 1;
11544 bp_location_downloaded = 1;
11545 }
11546 t = (struct tracepoint *) b;
11547 t->number_on_target = b->number;
11548 if (bp_location_downloaded)
11549 gdb::observers::breakpoint_modified.notify (b);
11550 }
11551 }
11552
11553 /* Swap the insertion/duplication state between two locations. */
11554
11555 static void
11556 swap_insertion (struct bp_location *left, struct bp_location *right)
11557 {
11558 const int left_inserted = left->inserted;
11559 const int left_duplicate = left->duplicate;
11560 const int left_needs_update = left->needs_update;
11561 const struct bp_target_info left_target_info = left->target_info;
11562
11563 /* Locations of tracepoints can never be duplicated. */
11564 if (is_tracepoint (left->owner))
11565 gdb_assert (!left->duplicate);
11566 if (is_tracepoint (right->owner))
11567 gdb_assert (!right->duplicate);
11568
11569 left->inserted = right->inserted;
11570 left->duplicate = right->duplicate;
11571 left->needs_update = right->needs_update;
11572 left->target_info = right->target_info;
11573 right->inserted = left_inserted;
11574 right->duplicate = left_duplicate;
11575 right->needs_update = left_needs_update;
11576 right->target_info = left_target_info;
11577 }
11578
11579 /* Force the re-insertion of the locations at ADDRESS. This is called
11580 once a new/deleted/modified duplicate location is found and we are evaluating
11581 conditions on the target's side. Such conditions need to be updated on
11582 the target. */
11583
11584 static void
11585 force_breakpoint_reinsertion (struct bp_location *bl)
11586 {
11587 struct bp_location **locp = NULL, **loc2p;
11588 struct bp_location *loc;
11589 CORE_ADDR address = 0;
11590 int pspace_num;
11591
11592 address = bl->address;
11593 pspace_num = bl->pspace->num;
11594
11595 /* This is only meaningful if the target is
11596 evaluating conditions and if the user has
11597 opted for condition evaluation on the target's
11598 side. */
11599 if (gdb_evaluates_breakpoint_condition_p ()
11600 || !target_supports_evaluation_of_breakpoint_conditions ())
11601 return;
11602
11603 /* Flag all breakpoint locations with this address and
11604 the same program space as the location
11605 as "its condition has changed". We need to
11606 update the conditions on the target's side. */
11607 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11608 {
11609 loc = *loc2p;
11610
11611 if (!is_breakpoint (loc->owner)
11612 || pspace_num != loc->pspace->num)
11613 continue;
11614
11615 /* Flag the location appropriately. We use a different state to
11616 let everyone know that we already updated the set of locations
11617 with addr bl->address and program space bl->pspace. This is so
11618 we don't have to keep calling these functions just to mark locations
11619 that have already been marked. */
11620 loc->condition_changed = condition_updated;
11621
11622 /* Free the agent expression bytecode as well. We will compute
11623 it later on. */
11624 loc->cond_bytecode.reset ();
11625 }
11626 }
11627 /* Called whether new breakpoints are created, or existing breakpoints
11628 deleted, to update the global location list and recompute which
11629 locations are duplicate of which.
11630
11631 The INSERT_MODE flag determines whether locations may not, may, or
11632 shall be inserted now. See 'enum ugll_insert_mode' for more
11633 info. */
11634
11635 static void
11636 update_global_location_list (enum ugll_insert_mode insert_mode)
11637 {
11638 struct breakpoint *b;
11639 struct bp_location **locp, *loc;
11640 /* Last breakpoint location address that was marked for update. */
11641 CORE_ADDR last_addr = 0;
11642 /* Last breakpoint location program space that was marked for update. */
11643 int last_pspace_num = -1;
11644
11645 /* Used in the duplicates detection below. When iterating over all
11646 bp_locations, points to the first bp_location of a given address.
11647 Breakpoints and watchpoints of different types are never
11648 duplicates of each other. Keep one pointer for each type of
11649 breakpoint/watchpoint, so we only need to loop over all locations
11650 once. */
11651 struct bp_location *bp_loc_first; /* breakpoint */
11652 struct bp_location *wp_loc_first; /* hardware watchpoint */
11653 struct bp_location *awp_loc_first; /* access watchpoint */
11654 struct bp_location *rwp_loc_first; /* read watchpoint */
11655
11656 /* Saved former bp_locations array which we compare against the newly
11657 built bp_locations from the current state of ALL_BREAKPOINTS. */
11658 struct bp_location **old_locp;
11659 unsigned old_locations_count;
11660 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11661
11662 old_locations_count = bp_locations_count;
11663 bp_locations = NULL;
11664 bp_locations_count = 0;
11665
11666 ALL_BREAKPOINTS (b)
11667 for (loc = b->loc; loc; loc = loc->next)
11668 bp_locations_count++;
11669
11670 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11671 locp = bp_locations;
11672 ALL_BREAKPOINTS (b)
11673 for (loc = b->loc; loc; loc = loc->next)
11674 *locp++ = loc;
11675 std::sort (bp_locations, bp_locations + bp_locations_count,
11676 bp_location_is_less_than);
11677
11678 bp_locations_target_extensions_update ();
11679
11680 /* Identify bp_location instances that are no longer present in the
11681 new list, and therefore should be freed. Note that it's not
11682 necessary that those locations should be removed from inferior --
11683 if there's another location at the same address (previously
11684 marked as duplicate), we don't need to remove/insert the
11685 location.
11686
11687 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11688 and former bp_location array state respectively. */
11689
11690 locp = bp_locations;
11691 for (old_locp = old_locations.get ();
11692 old_locp < old_locations.get () + old_locations_count;
11693 old_locp++)
11694 {
11695 struct bp_location *old_loc = *old_locp;
11696 struct bp_location **loc2p;
11697
11698 /* Tells if 'old_loc' is found among the new locations. If
11699 not, we have to free it. */
11700 int found_object = 0;
11701 /* Tells if the location should remain inserted in the target. */
11702 int keep_in_target = 0;
11703 int removed = 0;
11704
11705 /* Skip LOCP entries which will definitely never be needed.
11706 Stop either at or being the one matching OLD_LOC. */
11707 while (locp < bp_locations + bp_locations_count
11708 && (*locp)->address < old_loc->address)
11709 locp++;
11710
11711 for (loc2p = locp;
11712 (loc2p < bp_locations + bp_locations_count
11713 && (*loc2p)->address == old_loc->address);
11714 loc2p++)
11715 {
11716 /* Check if this is a new/duplicated location or a duplicated
11717 location that had its condition modified. If so, we want to send
11718 its condition to the target if evaluation of conditions is taking
11719 place there. */
11720 if ((*loc2p)->condition_changed == condition_modified
11721 && (last_addr != old_loc->address
11722 || last_pspace_num != old_loc->pspace->num))
11723 {
11724 force_breakpoint_reinsertion (*loc2p);
11725 last_pspace_num = old_loc->pspace->num;
11726 }
11727
11728 if (*loc2p == old_loc)
11729 found_object = 1;
11730 }
11731
11732 /* We have already handled this address, update it so that we don't
11733 have to go through updates again. */
11734 last_addr = old_loc->address;
11735
11736 /* Target-side condition evaluation: Handle deleted locations. */
11737 if (!found_object)
11738 force_breakpoint_reinsertion (old_loc);
11739
11740 /* If this location is no longer present, and inserted, look if
11741 there's maybe a new location at the same address. If so,
11742 mark that one inserted, and don't remove this one. This is
11743 needed so that we don't have a time window where a breakpoint
11744 at certain location is not inserted. */
11745
11746 if (old_loc->inserted)
11747 {
11748 /* If the location is inserted now, we might have to remove
11749 it. */
11750
11751 if (found_object && should_be_inserted (old_loc))
11752 {
11753 /* The location is still present in the location list,
11754 and still should be inserted. Don't do anything. */
11755 keep_in_target = 1;
11756 }
11757 else
11758 {
11759 /* This location still exists, but it won't be kept in the
11760 target since it may have been disabled. We proceed to
11761 remove its target-side condition. */
11762
11763 /* The location is either no longer present, or got
11764 disabled. See if there's another location at the
11765 same address, in which case we don't need to remove
11766 this one from the target. */
11767
11768 /* OLD_LOC comes from existing struct breakpoint. */
11769 if (bl_address_is_meaningful (old_loc))
11770 {
11771 for (loc2p = locp;
11772 (loc2p < bp_locations + bp_locations_count
11773 && (*loc2p)->address == old_loc->address);
11774 loc2p++)
11775 {
11776 struct bp_location *loc2 = *loc2p;
11777
11778 if (breakpoint_locations_match (loc2, old_loc))
11779 {
11780 /* Read watchpoint locations are switched to
11781 access watchpoints, if the former are not
11782 supported, but the latter are. */
11783 if (is_hardware_watchpoint (old_loc->owner))
11784 {
11785 gdb_assert (is_hardware_watchpoint (loc2->owner));
11786 loc2->watchpoint_type = old_loc->watchpoint_type;
11787 }
11788
11789 /* loc2 is a duplicated location. We need to check
11790 if it should be inserted in case it will be
11791 unduplicated. */
11792 if (loc2 != old_loc
11793 && unduplicated_should_be_inserted (loc2))
11794 {
11795 swap_insertion (old_loc, loc2);
11796 keep_in_target = 1;
11797 break;
11798 }
11799 }
11800 }
11801 }
11802 }
11803
11804 if (!keep_in_target)
11805 {
11806 if (remove_breakpoint (old_loc))
11807 {
11808 /* This is just about all we can do. We could keep
11809 this location on the global list, and try to
11810 remove it next time, but there's no particular
11811 reason why we will succeed next time.
11812
11813 Note that at this point, old_loc->owner is still
11814 valid, as delete_breakpoint frees the breakpoint
11815 only after calling us. */
11816 printf_filtered (_("warning: Error removing "
11817 "breakpoint %d\n"),
11818 old_loc->owner->number);
11819 }
11820 removed = 1;
11821 }
11822 }
11823
11824 if (!found_object)
11825 {
11826 if (removed && target_is_non_stop_p ()
11827 && need_moribund_for_location_type (old_loc))
11828 {
11829 /* This location was removed from the target. In
11830 non-stop mode, a race condition is possible where
11831 we've removed a breakpoint, but stop events for that
11832 breakpoint are already queued and will arrive later.
11833 We apply an heuristic to be able to distinguish such
11834 SIGTRAPs from other random SIGTRAPs: we keep this
11835 breakpoint location for a bit, and will retire it
11836 after we see some number of events. The theory here
11837 is that reporting of events should, "on the average",
11838 be fair, so after a while we'll see events from all
11839 threads that have anything of interest, and no longer
11840 need to keep this breakpoint location around. We
11841 don't hold locations forever so to reduce chances of
11842 mistaking a non-breakpoint SIGTRAP for a breakpoint
11843 SIGTRAP.
11844
11845 The heuristic failing can be disastrous on
11846 decr_pc_after_break targets.
11847
11848 On decr_pc_after_break targets, like e.g., x86-linux,
11849 if we fail to recognize a late breakpoint SIGTRAP,
11850 because events_till_retirement has reached 0 too
11851 soon, we'll fail to do the PC adjustment, and report
11852 a random SIGTRAP to the user. When the user resumes
11853 the inferior, it will most likely immediately crash
11854 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11855 corrupted, because of being resumed e.g., in the
11856 middle of a multi-byte instruction, or skipped a
11857 one-byte instruction. This was actually seen happen
11858 on native x86-linux, and should be less rare on
11859 targets that do not support new thread events, like
11860 remote, due to the heuristic depending on
11861 thread_count.
11862
11863 Mistaking a random SIGTRAP for a breakpoint trap
11864 causes similar symptoms (PC adjustment applied when
11865 it shouldn't), but then again, playing with SIGTRAPs
11866 behind the debugger's back is asking for trouble.
11867
11868 Since hardware watchpoint traps are always
11869 distinguishable from other traps, so we don't need to
11870 apply keep hardware watchpoint moribund locations
11871 around. We simply always ignore hardware watchpoint
11872 traps we can no longer explain. */
11873
11874 old_loc->events_till_retirement = 3 * (thread_count () + 1);
11875 old_loc->owner = NULL;
11876
11877 moribund_locations.push_back (old_loc);
11878 }
11879 else
11880 {
11881 old_loc->owner = NULL;
11882 decref_bp_location (&old_loc);
11883 }
11884 }
11885 }
11886
11887 /* Rescan breakpoints at the same address and section, marking the
11888 first one as "first" and any others as "duplicates". This is so
11889 that the bpt instruction is only inserted once. If we have a
11890 permanent breakpoint at the same place as BPT, make that one the
11891 official one, and the rest as duplicates. Permanent breakpoints
11892 are sorted first for the same address.
11893
11894 Do the same for hardware watchpoints, but also considering the
11895 watchpoint's type (regular/access/read) and length. */
11896
11897 bp_loc_first = NULL;
11898 wp_loc_first = NULL;
11899 awp_loc_first = NULL;
11900 rwp_loc_first = NULL;
11901 ALL_BP_LOCATIONS (loc, locp)
11902 {
11903 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11904 non-NULL. */
11905 struct bp_location **loc_first_p;
11906 b = loc->owner;
11907
11908 if (!unduplicated_should_be_inserted (loc)
11909 || !bl_address_is_meaningful (loc)
11910 /* Don't detect duplicate for tracepoint locations because they are
11911 never duplicated. See the comments in field `duplicate' of
11912 `struct bp_location'. */
11913 || is_tracepoint (b))
11914 {
11915 /* Clear the condition modification flag. */
11916 loc->condition_changed = condition_unchanged;
11917 continue;
11918 }
11919
11920 if (b->type == bp_hardware_watchpoint)
11921 loc_first_p = &wp_loc_first;
11922 else if (b->type == bp_read_watchpoint)
11923 loc_first_p = &rwp_loc_first;
11924 else if (b->type == bp_access_watchpoint)
11925 loc_first_p = &awp_loc_first;
11926 else
11927 loc_first_p = &bp_loc_first;
11928
11929 if (*loc_first_p == NULL
11930 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11931 || !breakpoint_locations_match (loc, *loc_first_p))
11932 {
11933 *loc_first_p = loc;
11934 loc->duplicate = 0;
11935
11936 if (is_breakpoint (loc->owner) && loc->condition_changed)
11937 {
11938 loc->needs_update = 1;
11939 /* Clear the condition modification flag. */
11940 loc->condition_changed = condition_unchanged;
11941 }
11942 continue;
11943 }
11944
11945
11946 /* This and the above ensure the invariant that the first location
11947 is not duplicated, and is the inserted one.
11948 All following are marked as duplicated, and are not inserted. */
11949 if (loc->inserted)
11950 swap_insertion (loc, *loc_first_p);
11951 loc->duplicate = 1;
11952
11953 /* Clear the condition modification flag. */
11954 loc->condition_changed = condition_unchanged;
11955 }
11956
11957 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11958 {
11959 if (insert_mode != UGLL_DONT_INSERT)
11960 insert_breakpoint_locations ();
11961 else
11962 {
11963 /* Even though the caller told us to not insert new
11964 locations, we may still need to update conditions on the
11965 target's side of breakpoints that were already inserted
11966 if the target is evaluating breakpoint conditions. We
11967 only update conditions for locations that are marked
11968 "needs_update". */
11969 update_inserted_breakpoint_locations ();
11970 }
11971 }
11972
11973 if (insert_mode != UGLL_DONT_INSERT)
11974 download_tracepoint_locations ();
11975 }
11976
11977 void
11978 breakpoint_retire_moribund (void)
11979 {
11980 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11981 {
11982 struct bp_location *loc = moribund_locations[ix];
11983 if (--(loc->events_till_retirement) == 0)
11984 {
11985 decref_bp_location (&loc);
11986 unordered_remove (moribund_locations, ix);
11987 --ix;
11988 }
11989 }
11990 }
11991
11992 static void
11993 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11994 {
11995
11996 try
11997 {
11998 update_global_location_list (insert_mode);
11999 }
12000 catch (const gdb_exception_error &e)
12001 {
12002 }
12003 }
12004
12005 /* Clear BKP from a BPS. */
12006
12007 static void
12008 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12009 {
12010 bpstat bs;
12011
12012 for (bs = bps; bs; bs = bs->next)
12013 if (bs->breakpoint_at == bpt)
12014 {
12015 bs->breakpoint_at = NULL;
12016 bs->old_val = NULL;
12017 /* bs->commands will be freed later. */
12018 }
12019 }
12020
12021 /* Callback for iterate_over_threads. */
12022 static int
12023 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12024 {
12025 struct breakpoint *bpt = (struct breakpoint *) data;
12026
12027 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12028 return 0;
12029 }
12030
12031 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12032 callbacks. */
12033
12034 static void
12035 say_where (struct breakpoint *b)
12036 {
12037 struct value_print_options opts;
12038
12039 get_user_print_options (&opts);
12040
12041 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12042 single string. */
12043 if (b->loc == NULL)
12044 {
12045 /* For pending locations, the output differs slightly based
12046 on b->extra_string. If this is non-NULL, it contains either
12047 a condition or dprintf arguments. */
12048 if (b->extra_string == NULL)
12049 {
12050 printf_filtered (_(" (%s) pending."),
12051 event_location_to_string (b->location.get ()));
12052 }
12053 else if (b->type == bp_dprintf)
12054 {
12055 printf_filtered (_(" (%s,%s) pending."),
12056 event_location_to_string (b->location.get ()),
12057 b->extra_string);
12058 }
12059 else
12060 {
12061 printf_filtered (_(" (%s %s) pending."),
12062 event_location_to_string (b->location.get ()),
12063 b->extra_string);
12064 }
12065 }
12066 else
12067 {
12068 if (opts.addressprint || b->loc->symtab == NULL)
12069 printf_filtered (" at %ps",
12070 styled_string (address_style.style (),
12071 paddress (b->loc->gdbarch,
12072 b->loc->address)));
12073 if (b->loc->symtab != NULL)
12074 {
12075 /* If there is a single location, we can print the location
12076 more nicely. */
12077 if (b->loc->next == NULL)
12078 {
12079 const char *filename
12080 = symtab_to_filename_for_display (b->loc->symtab);
12081 printf_filtered (": file %ps, line %d.",
12082 styled_string (file_name_style.style (),
12083 filename),
12084 b->loc->line_number);
12085 }
12086 else
12087 /* This is not ideal, but each location may have a
12088 different file name, and this at least reflects the
12089 real situation somewhat. */
12090 printf_filtered (": %s.",
12091 event_location_to_string (b->location.get ()));
12092 }
12093
12094 if (b->loc->next)
12095 {
12096 struct bp_location *loc = b->loc;
12097 int n = 0;
12098 for (; loc; loc = loc->next)
12099 ++n;
12100 printf_filtered (" (%d locations)", n);
12101 }
12102 }
12103 }
12104
12105 bp_location::~bp_location ()
12106 {
12107 xfree (function_name);
12108 }
12109
12110 /* Destructor for the breakpoint base class. */
12111
12112 breakpoint::~breakpoint ()
12113 {
12114 xfree (this->cond_string);
12115 xfree (this->extra_string);
12116 }
12117
12118 static struct bp_location *
12119 base_breakpoint_allocate_location (struct breakpoint *self)
12120 {
12121 return new bp_location (self);
12122 }
12123
12124 static void
12125 base_breakpoint_re_set (struct breakpoint *b)
12126 {
12127 /* Nothing to re-set. */
12128 }
12129
12130 #define internal_error_pure_virtual_called() \
12131 gdb_assert_not_reached ("pure virtual function called")
12132
12133 static int
12134 base_breakpoint_insert_location (struct bp_location *bl)
12135 {
12136 internal_error_pure_virtual_called ();
12137 }
12138
12139 static int
12140 base_breakpoint_remove_location (struct bp_location *bl,
12141 enum remove_bp_reason reason)
12142 {
12143 internal_error_pure_virtual_called ();
12144 }
12145
12146 static int
12147 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12148 const address_space *aspace,
12149 CORE_ADDR bp_addr,
12150 const struct target_waitstatus *ws)
12151 {
12152 internal_error_pure_virtual_called ();
12153 }
12154
12155 static void
12156 base_breakpoint_check_status (bpstat bs)
12157 {
12158 /* Always stop. */
12159 }
12160
12161 /* A "works_in_software_mode" breakpoint_ops method that just internal
12162 errors. */
12163
12164 static int
12165 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12166 {
12167 internal_error_pure_virtual_called ();
12168 }
12169
12170 /* A "resources_needed" breakpoint_ops method that just internal
12171 errors. */
12172
12173 static int
12174 base_breakpoint_resources_needed (const struct bp_location *bl)
12175 {
12176 internal_error_pure_virtual_called ();
12177 }
12178
12179 static enum print_stop_action
12180 base_breakpoint_print_it (bpstat bs)
12181 {
12182 internal_error_pure_virtual_called ();
12183 }
12184
12185 static void
12186 base_breakpoint_print_one_detail (const struct breakpoint *self,
12187 struct ui_out *uiout)
12188 {
12189 /* nothing */
12190 }
12191
12192 static void
12193 base_breakpoint_print_mention (struct breakpoint *b)
12194 {
12195 internal_error_pure_virtual_called ();
12196 }
12197
12198 static void
12199 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12200 {
12201 internal_error_pure_virtual_called ();
12202 }
12203
12204 static void
12205 base_breakpoint_create_sals_from_location
12206 (const struct event_location *location,
12207 struct linespec_result *canonical,
12208 enum bptype type_wanted)
12209 {
12210 internal_error_pure_virtual_called ();
12211 }
12212
12213 static void
12214 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12215 struct linespec_result *c,
12216 gdb::unique_xmalloc_ptr<char> cond_string,
12217 gdb::unique_xmalloc_ptr<char> extra_string,
12218 enum bptype type_wanted,
12219 enum bpdisp disposition,
12220 int thread,
12221 int task, int ignore_count,
12222 const struct breakpoint_ops *o,
12223 int from_tty, int enabled,
12224 int internal, unsigned flags)
12225 {
12226 internal_error_pure_virtual_called ();
12227 }
12228
12229 static std::vector<symtab_and_line>
12230 base_breakpoint_decode_location (struct breakpoint *b,
12231 const struct event_location *location,
12232 struct program_space *search_pspace)
12233 {
12234 internal_error_pure_virtual_called ();
12235 }
12236
12237 /* The default 'explains_signal' method. */
12238
12239 static int
12240 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12241 {
12242 return 1;
12243 }
12244
12245 /* The default "after_condition_true" method. */
12246
12247 static void
12248 base_breakpoint_after_condition_true (struct bpstats *bs)
12249 {
12250 /* Nothing to do. */
12251 }
12252
12253 struct breakpoint_ops base_breakpoint_ops =
12254 {
12255 base_breakpoint_allocate_location,
12256 base_breakpoint_re_set,
12257 base_breakpoint_insert_location,
12258 base_breakpoint_remove_location,
12259 base_breakpoint_breakpoint_hit,
12260 base_breakpoint_check_status,
12261 base_breakpoint_resources_needed,
12262 base_breakpoint_works_in_software_mode,
12263 base_breakpoint_print_it,
12264 NULL,
12265 base_breakpoint_print_one_detail,
12266 base_breakpoint_print_mention,
12267 base_breakpoint_print_recreate,
12268 base_breakpoint_create_sals_from_location,
12269 base_breakpoint_create_breakpoints_sal,
12270 base_breakpoint_decode_location,
12271 base_breakpoint_explains_signal,
12272 base_breakpoint_after_condition_true,
12273 };
12274
12275 /* Default breakpoint_ops methods. */
12276
12277 static void
12278 bkpt_re_set (struct breakpoint *b)
12279 {
12280 /* FIXME: is this still reachable? */
12281 if (breakpoint_event_location_empty_p (b))
12282 {
12283 /* Anything without a location can't be re-set. */
12284 delete_breakpoint (b);
12285 return;
12286 }
12287
12288 breakpoint_re_set_default (b);
12289 }
12290
12291 static int
12292 bkpt_insert_location (struct bp_location *bl)
12293 {
12294 CORE_ADDR addr = bl->target_info.reqstd_address;
12295
12296 bl->target_info.kind = breakpoint_kind (bl, &addr);
12297 bl->target_info.placed_address = addr;
12298
12299 if (bl->loc_type == bp_loc_hardware_breakpoint)
12300 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12301 else
12302 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12303 }
12304
12305 static int
12306 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12307 {
12308 if (bl->loc_type == bp_loc_hardware_breakpoint)
12309 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12310 else
12311 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12312 }
12313
12314 static int
12315 bkpt_breakpoint_hit (const struct bp_location *bl,
12316 const address_space *aspace, CORE_ADDR bp_addr,
12317 const struct target_waitstatus *ws)
12318 {
12319 if (ws->kind != TARGET_WAITKIND_STOPPED
12320 || ws->value.sig != GDB_SIGNAL_TRAP)
12321 return 0;
12322
12323 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12324 aspace, bp_addr))
12325 return 0;
12326
12327 if (overlay_debugging /* unmapped overlay section */
12328 && section_is_overlay (bl->section)
12329 && !section_is_mapped (bl->section))
12330 return 0;
12331
12332 return 1;
12333 }
12334
12335 static int
12336 dprintf_breakpoint_hit (const struct bp_location *bl,
12337 const address_space *aspace, CORE_ADDR bp_addr,
12338 const struct target_waitstatus *ws)
12339 {
12340 if (dprintf_style == dprintf_style_agent
12341 && target_can_run_breakpoint_commands ())
12342 {
12343 /* An agent-style dprintf never causes a stop. If we see a trap
12344 for this address it must be for a breakpoint that happens to
12345 be set at the same address. */
12346 return 0;
12347 }
12348
12349 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12350 }
12351
12352 static int
12353 bkpt_resources_needed (const struct bp_location *bl)
12354 {
12355 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12356
12357 return 1;
12358 }
12359
12360 static enum print_stop_action
12361 bkpt_print_it (bpstat bs)
12362 {
12363 struct breakpoint *b;
12364 const struct bp_location *bl;
12365 int bp_temp;
12366 struct ui_out *uiout = current_uiout;
12367
12368 gdb_assert (bs->bp_location_at != NULL);
12369
12370 bl = bs->bp_location_at;
12371 b = bs->breakpoint_at;
12372
12373 bp_temp = b->disposition == disp_del;
12374 if (bl->address != bl->requested_address)
12375 breakpoint_adjustment_warning (bl->requested_address,
12376 bl->address,
12377 b->number, 1);
12378 annotate_breakpoint (b->number);
12379 maybe_print_thread_hit_breakpoint (uiout);
12380
12381 if (uiout->is_mi_like_p ())
12382 {
12383 uiout->field_string ("reason",
12384 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12385 uiout->field_string ("disp", bpdisp_text (b->disposition));
12386 }
12387 if (bp_temp)
12388 uiout->message ("Temporary breakpoint %pF, ",
12389 signed_field ("bkptno", b->number));
12390 else
12391 uiout->message ("Breakpoint %pF, ",
12392 signed_field ("bkptno", b->number));
12393
12394 return PRINT_SRC_AND_LOC;
12395 }
12396
12397 static void
12398 bkpt_print_mention (struct breakpoint *b)
12399 {
12400 if (current_uiout->is_mi_like_p ())
12401 return;
12402
12403 switch (b->type)
12404 {
12405 case bp_breakpoint:
12406 case bp_gnu_ifunc_resolver:
12407 if (b->disposition == disp_del)
12408 printf_filtered (_("Temporary breakpoint"));
12409 else
12410 printf_filtered (_("Breakpoint"));
12411 printf_filtered (_(" %d"), b->number);
12412 if (b->type == bp_gnu_ifunc_resolver)
12413 printf_filtered (_(" at gnu-indirect-function resolver"));
12414 break;
12415 case bp_hardware_breakpoint:
12416 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12417 break;
12418 case bp_dprintf:
12419 printf_filtered (_("Dprintf %d"), b->number);
12420 break;
12421 }
12422
12423 say_where (b);
12424 }
12425
12426 static void
12427 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12428 {
12429 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12430 fprintf_unfiltered (fp, "tbreak");
12431 else if (tp->type == bp_breakpoint)
12432 fprintf_unfiltered (fp, "break");
12433 else if (tp->type == bp_hardware_breakpoint
12434 && tp->disposition == disp_del)
12435 fprintf_unfiltered (fp, "thbreak");
12436 else if (tp->type == bp_hardware_breakpoint)
12437 fprintf_unfiltered (fp, "hbreak");
12438 else
12439 internal_error (__FILE__, __LINE__,
12440 _("unhandled breakpoint type %d"), (int) tp->type);
12441
12442 fprintf_unfiltered (fp, " %s",
12443 event_location_to_string (tp->location.get ()));
12444
12445 /* Print out extra_string if this breakpoint is pending. It might
12446 contain, for example, conditions that were set by the user. */
12447 if (tp->loc == NULL && tp->extra_string != NULL)
12448 fprintf_unfiltered (fp, " %s", tp->extra_string);
12449
12450 print_recreate_thread (tp, fp);
12451 }
12452
12453 static void
12454 bkpt_create_sals_from_location (const struct event_location *location,
12455 struct linespec_result *canonical,
12456 enum bptype type_wanted)
12457 {
12458 create_sals_from_location_default (location, canonical, type_wanted);
12459 }
12460
12461 static void
12462 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12463 struct linespec_result *canonical,
12464 gdb::unique_xmalloc_ptr<char> cond_string,
12465 gdb::unique_xmalloc_ptr<char> extra_string,
12466 enum bptype type_wanted,
12467 enum bpdisp disposition,
12468 int thread,
12469 int task, int ignore_count,
12470 const struct breakpoint_ops *ops,
12471 int from_tty, int enabled,
12472 int internal, unsigned flags)
12473 {
12474 create_breakpoints_sal_default (gdbarch, canonical,
12475 std::move (cond_string),
12476 std::move (extra_string),
12477 type_wanted,
12478 disposition, thread, task,
12479 ignore_count, ops, from_tty,
12480 enabled, internal, flags);
12481 }
12482
12483 static std::vector<symtab_and_line>
12484 bkpt_decode_location (struct breakpoint *b,
12485 const struct event_location *location,
12486 struct program_space *search_pspace)
12487 {
12488 return decode_location_default (b, location, search_pspace);
12489 }
12490
12491 /* Virtual table for internal breakpoints. */
12492
12493 static void
12494 internal_bkpt_re_set (struct breakpoint *b)
12495 {
12496 switch (b->type)
12497 {
12498 /* Delete overlay event and longjmp master breakpoints; they
12499 will be reset later by breakpoint_re_set. */
12500 case bp_overlay_event:
12501 case bp_longjmp_master:
12502 case bp_std_terminate_master:
12503 case bp_exception_master:
12504 delete_breakpoint (b);
12505 break;
12506
12507 /* This breakpoint is special, it's set up when the inferior
12508 starts and we really don't want to touch it. */
12509 case bp_shlib_event:
12510
12511 /* Like bp_shlib_event, this breakpoint type is special. Once
12512 it is set up, we do not want to touch it. */
12513 case bp_thread_event:
12514 break;
12515 }
12516 }
12517
12518 static void
12519 internal_bkpt_check_status (bpstat bs)
12520 {
12521 if (bs->breakpoint_at->type == bp_shlib_event)
12522 {
12523 /* If requested, stop when the dynamic linker notifies GDB of
12524 events. This allows the user to get control and place
12525 breakpoints in initializer routines for dynamically loaded
12526 objects (among other things). */
12527 bs->stop = stop_on_solib_events;
12528 bs->print = stop_on_solib_events;
12529 }
12530 else
12531 bs->stop = 0;
12532 }
12533
12534 static enum print_stop_action
12535 internal_bkpt_print_it (bpstat bs)
12536 {
12537 struct breakpoint *b;
12538
12539 b = bs->breakpoint_at;
12540
12541 switch (b->type)
12542 {
12543 case bp_shlib_event:
12544 /* Did we stop because the user set the stop_on_solib_events
12545 variable? (If so, we report this as a generic, "Stopped due
12546 to shlib event" message.) */
12547 print_solib_event (0);
12548 break;
12549
12550 case bp_thread_event:
12551 /* Not sure how we will get here.
12552 GDB should not stop for these breakpoints. */
12553 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12554 break;
12555
12556 case bp_overlay_event:
12557 /* By analogy with the thread event, GDB should not stop for these. */
12558 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12559 break;
12560
12561 case bp_longjmp_master:
12562 /* These should never be enabled. */
12563 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12564 break;
12565
12566 case bp_std_terminate_master:
12567 /* These should never be enabled. */
12568 printf_filtered (_("std::terminate Master Breakpoint: "
12569 "gdb should not stop!\n"));
12570 break;
12571
12572 case bp_exception_master:
12573 /* These should never be enabled. */
12574 printf_filtered (_("Exception Master Breakpoint: "
12575 "gdb should not stop!\n"));
12576 break;
12577 }
12578
12579 return PRINT_NOTHING;
12580 }
12581
12582 static void
12583 internal_bkpt_print_mention (struct breakpoint *b)
12584 {
12585 /* Nothing to mention. These breakpoints are internal. */
12586 }
12587
12588 /* Virtual table for momentary breakpoints */
12589
12590 static void
12591 momentary_bkpt_re_set (struct breakpoint *b)
12592 {
12593 /* Keep temporary breakpoints, which can be encountered when we step
12594 over a dlopen call and solib_add is resetting the breakpoints.
12595 Otherwise these should have been blown away via the cleanup chain
12596 or by breakpoint_init_inferior when we rerun the executable. */
12597 }
12598
12599 static void
12600 momentary_bkpt_check_status (bpstat bs)
12601 {
12602 /* Nothing. The point of these breakpoints is causing a stop. */
12603 }
12604
12605 static enum print_stop_action
12606 momentary_bkpt_print_it (bpstat bs)
12607 {
12608 return PRINT_UNKNOWN;
12609 }
12610
12611 static void
12612 momentary_bkpt_print_mention (struct breakpoint *b)
12613 {
12614 /* Nothing to mention. These breakpoints are internal. */
12615 }
12616
12617 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12618
12619 It gets cleared already on the removal of the first one of such placed
12620 breakpoints. This is OK as they get all removed altogether. */
12621
12622 longjmp_breakpoint::~longjmp_breakpoint ()
12623 {
12624 thread_info *tp = find_thread_global_id (this->thread);
12625
12626 if (tp != NULL)
12627 tp->initiating_frame = null_frame_id;
12628 }
12629
12630 /* Specific methods for probe breakpoints. */
12631
12632 static int
12633 bkpt_probe_insert_location (struct bp_location *bl)
12634 {
12635 int v = bkpt_insert_location (bl);
12636
12637 if (v == 0)
12638 {
12639 /* The insertion was successful, now let's set the probe's semaphore
12640 if needed. */
12641 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12642 }
12643
12644 return v;
12645 }
12646
12647 static int
12648 bkpt_probe_remove_location (struct bp_location *bl,
12649 enum remove_bp_reason reason)
12650 {
12651 /* Let's clear the semaphore before removing the location. */
12652 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12653
12654 return bkpt_remove_location (bl, reason);
12655 }
12656
12657 static void
12658 bkpt_probe_create_sals_from_location (const struct event_location *location,
12659 struct linespec_result *canonical,
12660 enum bptype type_wanted)
12661 {
12662 struct linespec_sals lsal;
12663
12664 lsal.sals = parse_probes (location, NULL, canonical);
12665 lsal.canonical
12666 = xstrdup (event_location_to_string (canonical->location.get ()));
12667 canonical->lsals.push_back (std::move (lsal));
12668 }
12669
12670 static std::vector<symtab_and_line>
12671 bkpt_probe_decode_location (struct breakpoint *b,
12672 const struct event_location *location,
12673 struct program_space *search_pspace)
12674 {
12675 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12676 if (sals.empty ())
12677 error (_("probe not found"));
12678 return sals;
12679 }
12680
12681 /* The breakpoint_ops structure to be used in tracepoints. */
12682
12683 static void
12684 tracepoint_re_set (struct breakpoint *b)
12685 {
12686 breakpoint_re_set_default (b);
12687 }
12688
12689 static int
12690 tracepoint_breakpoint_hit (const struct bp_location *bl,
12691 const address_space *aspace, CORE_ADDR bp_addr,
12692 const struct target_waitstatus *ws)
12693 {
12694 /* By definition, the inferior does not report stops at
12695 tracepoints. */
12696 return 0;
12697 }
12698
12699 static void
12700 tracepoint_print_one_detail (const struct breakpoint *self,
12701 struct ui_out *uiout)
12702 {
12703 struct tracepoint *tp = (struct tracepoint *) self;
12704 if (!tp->static_trace_marker_id.empty ())
12705 {
12706 gdb_assert (self->type == bp_static_tracepoint);
12707
12708 uiout->message ("\tmarker id is %pF\n",
12709 string_field ("static-tracepoint-marker-string-id",
12710 tp->static_trace_marker_id.c_str ()));
12711 }
12712 }
12713
12714 static void
12715 tracepoint_print_mention (struct breakpoint *b)
12716 {
12717 if (current_uiout->is_mi_like_p ())
12718 return;
12719
12720 switch (b->type)
12721 {
12722 case bp_tracepoint:
12723 printf_filtered (_("Tracepoint"));
12724 printf_filtered (_(" %d"), b->number);
12725 break;
12726 case bp_fast_tracepoint:
12727 printf_filtered (_("Fast tracepoint"));
12728 printf_filtered (_(" %d"), b->number);
12729 break;
12730 case bp_static_tracepoint:
12731 printf_filtered (_("Static tracepoint"));
12732 printf_filtered (_(" %d"), b->number);
12733 break;
12734 default:
12735 internal_error (__FILE__, __LINE__,
12736 _("unhandled tracepoint type %d"), (int) b->type);
12737 }
12738
12739 say_where (b);
12740 }
12741
12742 static void
12743 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12744 {
12745 struct tracepoint *tp = (struct tracepoint *) self;
12746
12747 if (self->type == bp_fast_tracepoint)
12748 fprintf_unfiltered (fp, "ftrace");
12749 else if (self->type == bp_static_tracepoint)
12750 fprintf_unfiltered (fp, "strace");
12751 else if (self->type == bp_tracepoint)
12752 fprintf_unfiltered (fp, "trace");
12753 else
12754 internal_error (__FILE__, __LINE__,
12755 _("unhandled tracepoint type %d"), (int) self->type);
12756
12757 fprintf_unfiltered (fp, " %s",
12758 event_location_to_string (self->location.get ()));
12759 print_recreate_thread (self, fp);
12760
12761 if (tp->pass_count)
12762 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12763 }
12764
12765 static void
12766 tracepoint_create_sals_from_location (const struct event_location *location,
12767 struct linespec_result *canonical,
12768 enum bptype type_wanted)
12769 {
12770 create_sals_from_location_default (location, canonical, type_wanted);
12771 }
12772
12773 static void
12774 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12775 struct linespec_result *canonical,
12776 gdb::unique_xmalloc_ptr<char> cond_string,
12777 gdb::unique_xmalloc_ptr<char> extra_string,
12778 enum bptype type_wanted,
12779 enum bpdisp disposition,
12780 int thread,
12781 int task, int ignore_count,
12782 const struct breakpoint_ops *ops,
12783 int from_tty, int enabled,
12784 int internal, unsigned flags)
12785 {
12786 create_breakpoints_sal_default (gdbarch, canonical,
12787 std::move (cond_string),
12788 std::move (extra_string),
12789 type_wanted,
12790 disposition, thread, task,
12791 ignore_count, ops, from_tty,
12792 enabled, internal, flags);
12793 }
12794
12795 static std::vector<symtab_and_line>
12796 tracepoint_decode_location (struct breakpoint *b,
12797 const struct event_location *location,
12798 struct program_space *search_pspace)
12799 {
12800 return decode_location_default (b, location, search_pspace);
12801 }
12802
12803 struct breakpoint_ops tracepoint_breakpoint_ops;
12804
12805 /* The breakpoint_ops structure to be use on tracepoints placed in a
12806 static probe. */
12807
12808 static void
12809 tracepoint_probe_create_sals_from_location
12810 (const struct event_location *location,
12811 struct linespec_result *canonical,
12812 enum bptype type_wanted)
12813 {
12814 /* We use the same method for breakpoint on probes. */
12815 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12816 }
12817
12818 static std::vector<symtab_and_line>
12819 tracepoint_probe_decode_location (struct breakpoint *b,
12820 const struct event_location *location,
12821 struct program_space *search_pspace)
12822 {
12823 /* We use the same method for breakpoint on probes. */
12824 return bkpt_probe_decode_location (b, location, search_pspace);
12825 }
12826
12827 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
12828
12829 /* Dprintf breakpoint_ops methods. */
12830
12831 static void
12832 dprintf_re_set (struct breakpoint *b)
12833 {
12834 breakpoint_re_set_default (b);
12835
12836 /* extra_string should never be non-NULL for dprintf. */
12837 gdb_assert (b->extra_string != NULL);
12838
12839 /* 1 - connect to target 1, that can run breakpoint commands.
12840 2 - create a dprintf, which resolves fine.
12841 3 - disconnect from target 1
12842 4 - connect to target 2, that can NOT run breakpoint commands.
12843
12844 After steps #3/#4, you'll want the dprintf command list to
12845 be updated, because target 1 and 2 may well return different
12846 answers for target_can_run_breakpoint_commands().
12847 Given absence of finer grained resetting, we get to do
12848 it all the time. */
12849 if (b->extra_string != NULL)
12850 update_dprintf_command_list (b);
12851 }
12852
12853 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12854
12855 static void
12856 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12857 {
12858 fprintf_unfiltered (fp, "dprintf %s,%s",
12859 event_location_to_string (tp->location.get ()),
12860 tp->extra_string);
12861 print_recreate_thread (tp, fp);
12862 }
12863
12864 /* Implement the "after_condition_true" breakpoint_ops method for
12865 dprintf.
12866
12867 dprintf's are implemented with regular commands in their command
12868 list, but we run the commands here instead of before presenting the
12869 stop to the user, as dprintf's don't actually cause a stop. This
12870 also makes it so that the commands of multiple dprintfs at the same
12871 address are all handled. */
12872
12873 static void
12874 dprintf_after_condition_true (struct bpstats *bs)
12875 {
12876 struct bpstats tmp_bs;
12877 struct bpstats *tmp_bs_p = &tmp_bs;
12878
12879 /* dprintf's never cause a stop. This wasn't set in the
12880 check_status hook instead because that would make the dprintf's
12881 condition not be evaluated. */
12882 bs->stop = 0;
12883
12884 /* Run the command list here. Take ownership of it instead of
12885 copying. We never want these commands to run later in
12886 bpstat_do_actions, if a breakpoint that causes a stop happens to
12887 be set at same address as this dprintf, or even if running the
12888 commands here throws. */
12889 tmp_bs.commands = bs->commands;
12890 bs->commands = NULL;
12891
12892 bpstat_do_actions_1 (&tmp_bs_p);
12893
12894 /* 'tmp_bs.commands' will usually be NULL by now, but
12895 bpstat_do_actions_1 may return early without processing the whole
12896 list. */
12897 }
12898
12899 /* The breakpoint_ops structure to be used on static tracepoints with
12900 markers (`-m'). */
12901
12902 static void
12903 strace_marker_create_sals_from_location (const struct event_location *location,
12904 struct linespec_result *canonical,
12905 enum bptype type_wanted)
12906 {
12907 struct linespec_sals lsal;
12908 const char *arg_start, *arg;
12909
12910 arg = arg_start = get_linespec_location (location)->spec_string;
12911 lsal.sals = decode_static_tracepoint_spec (&arg);
12912
12913 std::string str (arg_start, arg - arg_start);
12914 const char *ptr = str.c_str ();
12915 canonical->location
12916 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12917
12918 lsal.canonical
12919 = xstrdup (event_location_to_string (canonical->location.get ()));
12920 canonical->lsals.push_back (std::move (lsal));
12921 }
12922
12923 static void
12924 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12925 struct linespec_result *canonical,
12926 gdb::unique_xmalloc_ptr<char> cond_string,
12927 gdb::unique_xmalloc_ptr<char> extra_string,
12928 enum bptype type_wanted,
12929 enum bpdisp disposition,
12930 int thread,
12931 int task, int ignore_count,
12932 const struct breakpoint_ops *ops,
12933 int from_tty, int enabled,
12934 int internal, unsigned flags)
12935 {
12936 const linespec_sals &lsal = canonical->lsals[0];
12937
12938 /* If the user is creating a static tracepoint by marker id
12939 (strace -m MARKER_ID), then store the sals index, so that
12940 breakpoint_re_set can try to match up which of the newly
12941 found markers corresponds to this one, and, don't try to
12942 expand multiple locations for each sal, given than SALS
12943 already should contain all sals for MARKER_ID. */
12944
12945 for (size_t i = 0; i < lsal.sals.size (); i++)
12946 {
12947 event_location_up location
12948 = copy_event_location (canonical->location.get ());
12949
12950 std::unique_ptr<tracepoint> tp (new tracepoint ());
12951 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
12952 std::move (location), NULL,
12953 std::move (cond_string),
12954 std::move (extra_string),
12955 type_wanted, disposition,
12956 thread, task, ignore_count, ops,
12957 from_tty, enabled, internal, flags,
12958 canonical->special_display);
12959 /* Given that its possible to have multiple markers with
12960 the same string id, if the user is creating a static
12961 tracepoint by marker id ("strace -m MARKER_ID"), then
12962 store the sals index, so that breakpoint_re_set can
12963 try to match up which of the newly found markers
12964 corresponds to this one */
12965 tp->static_trace_marker_id_idx = i;
12966
12967 install_breakpoint (internal, std::move (tp), 0);
12968 }
12969 }
12970
12971 static std::vector<symtab_and_line>
12972 strace_marker_decode_location (struct breakpoint *b,
12973 const struct event_location *location,
12974 struct program_space *search_pspace)
12975 {
12976 struct tracepoint *tp = (struct tracepoint *) b;
12977 const char *s = get_linespec_location (location)->spec_string;
12978
12979 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12980 if (sals.size () > tp->static_trace_marker_id_idx)
12981 {
12982 sals[0] = sals[tp->static_trace_marker_id_idx];
12983 sals.resize (1);
12984 return sals;
12985 }
12986 else
12987 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
12988 }
12989
12990 static struct breakpoint_ops strace_marker_breakpoint_ops;
12991
12992 static int
12993 strace_marker_p (struct breakpoint *b)
12994 {
12995 return b->ops == &strace_marker_breakpoint_ops;
12996 }
12997
12998 /* Delete a breakpoint and clean up all traces of it in the data
12999 structures. */
13000
13001 void
13002 delete_breakpoint (struct breakpoint *bpt)
13003 {
13004 struct breakpoint *b;
13005
13006 gdb_assert (bpt != NULL);
13007
13008 /* Has this bp already been deleted? This can happen because
13009 multiple lists can hold pointers to bp's. bpstat lists are
13010 especial culprits.
13011
13012 One example of this happening is a watchpoint's scope bp. When
13013 the scope bp triggers, we notice that the watchpoint is out of
13014 scope, and delete it. We also delete its scope bp. But the
13015 scope bp is marked "auto-deleting", and is already on a bpstat.
13016 That bpstat is then checked for auto-deleting bp's, which are
13017 deleted.
13018
13019 A real solution to this problem might involve reference counts in
13020 bp's, and/or giving them pointers back to their referencing
13021 bpstat's, and teaching delete_breakpoint to only free a bp's
13022 storage when no more references were extent. A cheaper bandaid
13023 was chosen. */
13024 if (bpt->type == bp_none)
13025 return;
13026
13027 /* At least avoid this stale reference until the reference counting
13028 of breakpoints gets resolved. */
13029 if (bpt->related_breakpoint != bpt)
13030 {
13031 struct breakpoint *related;
13032 struct watchpoint *w;
13033
13034 if (bpt->type == bp_watchpoint_scope)
13035 w = (struct watchpoint *) bpt->related_breakpoint;
13036 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13037 w = (struct watchpoint *) bpt;
13038 else
13039 w = NULL;
13040 if (w != NULL)
13041 watchpoint_del_at_next_stop (w);
13042
13043 /* Unlink bpt from the bpt->related_breakpoint ring. */
13044 for (related = bpt; related->related_breakpoint != bpt;
13045 related = related->related_breakpoint);
13046 related->related_breakpoint = bpt->related_breakpoint;
13047 bpt->related_breakpoint = bpt;
13048 }
13049
13050 /* watch_command_1 creates a watchpoint but only sets its number if
13051 update_watchpoint succeeds in creating its bp_locations. If there's
13052 a problem in that process, we'll be asked to delete the half-created
13053 watchpoint. In that case, don't announce the deletion. */
13054 if (bpt->number)
13055 gdb::observers::breakpoint_deleted.notify (bpt);
13056
13057 if (breakpoint_chain == bpt)
13058 breakpoint_chain = bpt->next;
13059
13060 ALL_BREAKPOINTS (b)
13061 if (b->next == bpt)
13062 {
13063 b->next = bpt->next;
13064 break;
13065 }
13066
13067 /* Be sure no bpstat's are pointing at the breakpoint after it's
13068 been freed. */
13069 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13070 in all threads for now. Note that we cannot just remove bpstats
13071 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13072 commands are associated with the bpstat; if we remove it here,
13073 then the later call to bpstat_do_actions (&stop_bpstat); in
13074 event-top.c won't do anything, and temporary breakpoints with
13075 commands won't work. */
13076
13077 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13078
13079 /* Now that breakpoint is removed from breakpoint list, update the
13080 global location list. This will remove locations that used to
13081 belong to this breakpoint. Do this before freeing the breakpoint
13082 itself, since remove_breakpoint looks at location's owner. It
13083 might be better design to have location completely
13084 self-contained, but it's not the case now. */
13085 update_global_location_list (UGLL_DONT_INSERT);
13086
13087 /* On the chance that someone will soon try again to delete this
13088 same bp, we mark it as deleted before freeing its storage. */
13089 bpt->type = bp_none;
13090 delete bpt;
13091 }
13092
13093 /* Iterator function to call a user-provided callback function once
13094 for each of B and its related breakpoints. */
13095
13096 static void
13097 iterate_over_related_breakpoints (struct breakpoint *b,
13098 gdb::function_view<void (breakpoint *)> function)
13099 {
13100 struct breakpoint *related;
13101
13102 related = b;
13103 do
13104 {
13105 struct breakpoint *next;
13106
13107 /* FUNCTION may delete RELATED. */
13108 next = related->related_breakpoint;
13109
13110 if (next == related)
13111 {
13112 /* RELATED is the last ring entry. */
13113 function (related);
13114
13115 /* FUNCTION may have deleted it, so we'd never reach back to
13116 B. There's nothing left to do anyway, so just break
13117 out. */
13118 break;
13119 }
13120 else
13121 function (related);
13122
13123 related = next;
13124 }
13125 while (related != b);
13126 }
13127
13128 static void
13129 delete_command (const char *arg, int from_tty)
13130 {
13131 struct breakpoint *b, *b_tmp;
13132
13133 dont_repeat ();
13134
13135 if (arg == 0)
13136 {
13137 int breaks_to_delete = 0;
13138
13139 /* Delete all breakpoints if no argument. Do not delete
13140 internal breakpoints, these have to be deleted with an
13141 explicit breakpoint number argument. */
13142 ALL_BREAKPOINTS (b)
13143 if (user_breakpoint_p (b))
13144 {
13145 breaks_to_delete = 1;
13146 break;
13147 }
13148
13149 /* Ask user only if there are some breakpoints to delete. */
13150 if (!from_tty
13151 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13152 {
13153 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13154 if (user_breakpoint_p (b))
13155 delete_breakpoint (b);
13156 }
13157 }
13158 else
13159 map_breakpoint_numbers
13160 (arg, [&] (breakpoint *br)
13161 {
13162 iterate_over_related_breakpoints (br, delete_breakpoint);
13163 });
13164 }
13165
13166 /* Return true if all locations of B bound to PSPACE are pending. If
13167 PSPACE is NULL, all locations of all program spaces are
13168 considered. */
13169
13170 static int
13171 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13172 {
13173 struct bp_location *loc;
13174
13175 for (loc = b->loc; loc != NULL; loc = loc->next)
13176 if ((pspace == NULL
13177 || loc->pspace == pspace)
13178 && !loc->shlib_disabled
13179 && !loc->pspace->executing_startup)
13180 return 0;
13181 return 1;
13182 }
13183
13184 /* Subroutine of update_breakpoint_locations to simplify it.
13185 Return non-zero if multiple fns in list LOC have the same name.
13186 Null names are ignored. */
13187
13188 static int
13189 ambiguous_names_p (struct bp_location *loc)
13190 {
13191 struct bp_location *l;
13192 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13193 xcalloc, xfree);
13194
13195 for (l = loc; l != NULL; l = l->next)
13196 {
13197 const char **slot;
13198 const char *name = l->function_name;
13199
13200 /* Allow for some names to be NULL, ignore them. */
13201 if (name == NULL)
13202 continue;
13203
13204 slot = (const char **) htab_find_slot (htab, (const void *) name,
13205 INSERT);
13206 /* NOTE: We can assume slot != NULL here because xcalloc never
13207 returns NULL. */
13208 if (*slot != NULL)
13209 {
13210 htab_delete (htab);
13211 return 1;
13212 }
13213 *slot = name;
13214 }
13215
13216 htab_delete (htab);
13217 return 0;
13218 }
13219
13220 /* When symbols change, it probably means the sources changed as well,
13221 and it might mean the static tracepoint markers are no longer at
13222 the same address or line numbers they used to be at last we
13223 checked. Losing your static tracepoints whenever you rebuild is
13224 undesirable. This function tries to resync/rematch gdb static
13225 tracepoints with the markers on the target, for static tracepoints
13226 that have not been set by marker id. Static tracepoint that have
13227 been set by marker id are reset by marker id in breakpoint_re_set.
13228 The heuristic is:
13229
13230 1) For a tracepoint set at a specific address, look for a marker at
13231 the old PC. If one is found there, assume to be the same marker.
13232 If the name / string id of the marker found is different from the
13233 previous known name, assume that means the user renamed the marker
13234 in the sources, and output a warning.
13235
13236 2) For a tracepoint set at a given line number, look for a marker
13237 at the new address of the old line number. If one is found there,
13238 assume to be the same marker. If the name / string id of the
13239 marker found is different from the previous known name, assume that
13240 means the user renamed the marker in the sources, and output a
13241 warning.
13242
13243 3) If a marker is no longer found at the same address or line, it
13244 may mean the marker no longer exists. But it may also just mean
13245 the code changed a bit. Maybe the user added a few lines of code
13246 that made the marker move up or down (in line number terms). Ask
13247 the target for info about the marker with the string id as we knew
13248 it. If found, update line number and address in the matching
13249 static tracepoint. This will get confused if there's more than one
13250 marker with the same ID (possible in UST, although unadvised
13251 precisely because it confuses tools). */
13252
13253 static struct symtab_and_line
13254 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13255 {
13256 struct tracepoint *tp = (struct tracepoint *) b;
13257 struct static_tracepoint_marker marker;
13258 CORE_ADDR pc;
13259
13260 pc = sal.pc;
13261 if (sal.line)
13262 find_line_pc (sal.symtab, sal.line, &pc);
13263
13264 if (target_static_tracepoint_marker_at (pc, &marker))
13265 {
13266 if (tp->static_trace_marker_id != marker.str_id)
13267 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13268 b->number, tp->static_trace_marker_id.c_str (),
13269 marker.str_id.c_str ());
13270
13271 tp->static_trace_marker_id = std::move (marker.str_id);
13272
13273 return sal;
13274 }
13275
13276 /* Old marker wasn't found on target at lineno. Try looking it up
13277 by string ID. */
13278 if (!sal.explicit_pc
13279 && sal.line != 0
13280 && sal.symtab != NULL
13281 && !tp->static_trace_marker_id.empty ())
13282 {
13283 std::vector<static_tracepoint_marker> markers
13284 = target_static_tracepoint_markers_by_strid
13285 (tp->static_trace_marker_id.c_str ());
13286
13287 if (!markers.empty ())
13288 {
13289 struct symbol *sym;
13290 struct static_tracepoint_marker *tpmarker;
13291 struct ui_out *uiout = current_uiout;
13292 struct explicit_location explicit_loc;
13293
13294 tpmarker = &markers[0];
13295
13296 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13297
13298 warning (_("marker for static tracepoint %d (%s) not "
13299 "found at previous line number"),
13300 b->number, tp->static_trace_marker_id.c_str ());
13301
13302 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13303 sym = find_pc_sect_function (tpmarker->address, NULL);
13304 uiout->text ("Now in ");
13305 if (sym)
13306 {
13307 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
13308 function_name_style.style ());
13309 uiout->text (" at ");
13310 }
13311 uiout->field_string ("file",
13312 symtab_to_filename_for_display (sal2.symtab),
13313 file_name_style.style ());
13314 uiout->text (":");
13315
13316 if (uiout->is_mi_like_p ())
13317 {
13318 const char *fullname = symtab_to_fullname (sal2.symtab);
13319
13320 uiout->field_string ("fullname", fullname);
13321 }
13322
13323 uiout->field_signed ("line", sal2.line);
13324 uiout->text ("\n");
13325
13326 b->loc->line_number = sal2.line;
13327 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13328
13329 b->location.reset (NULL);
13330 initialize_explicit_location (&explicit_loc);
13331 explicit_loc.source_filename
13332 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13333 explicit_loc.line_offset.offset = b->loc->line_number;
13334 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13335 b->location = new_explicit_location (&explicit_loc);
13336
13337 /* Might be nice to check if function changed, and warn if
13338 so. */
13339 }
13340 }
13341 return sal;
13342 }
13343
13344 /* Returns 1 iff locations A and B are sufficiently same that
13345 we don't need to report breakpoint as changed. */
13346
13347 static int
13348 locations_are_equal (struct bp_location *a, struct bp_location *b)
13349 {
13350 while (a && b)
13351 {
13352 if (a->address != b->address)
13353 return 0;
13354
13355 if (a->shlib_disabled != b->shlib_disabled)
13356 return 0;
13357
13358 if (a->enabled != b->enabled)
13359 return 0;
13360
13361 a = a->next;
13362 b = b->next;
13363 }
13364
13365 if ((a == NULL) != (b == NULL))
13366 return 0;
13367
13368 return 1;
13369 }
13370
13371 /* Split all locations of B that are bound to PSPACE out of B's
13372 location list to a separate list and return that list's head. If
13373 PSPACE is NULL, hoist out all locations of B. */
13374
13375 static struct bp_location *
13376 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13377 {
13378 struct bp_location head;
13379 struct bp_location *i = b->loc;
13380 struct bp_location **i_link = &b->loc;
13381 struct bp_location *hoisted = &head;
13382
13383 if (pspace == NULL)
13384 {
13385 i = b->loc;
13386 b->loc = NULL;
13387 return i;
13388 }
13389
13390 head.next = NULL;
13391
13392 while (i != NULL)
13393 {
13394 if (i->pspace == pspace)
13395 {
13396 *i_link = i->next;
13397 i->next = NULL;
13398 hoisted->next = i;
13399 hoisted = i;
13400 }
13401 else
13402 i_link = &i->next;
13403 i = *i_link;
13404 }
13405
13406 return head.next;
13407 }
13408
13409 /* Create new breakpoint locations for B (a hardware or software
13410 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13411 zero, then B is a ranged breakpoint. Only recreates locations for
13412 FILTER_PSPACE. Locations of other program spaces are left
13413 untouched. */
13414
13415 void
13416 update_breakpoint_locations (struct breakpoint *b,
13417 struct program_space *filter_pspace,
13418 gdb::array_view<const symtab_and_line> sals,
13419 gdb::array_view<const symtab_and_line> sals_end)
13420 {
13421 struct bp_location *existing_locations;
13422
13423 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13424 {
13425 /* Ranged breakpoints have only one start location and one end
13426 location. */
13427 b->enable_state = bp_disabled;
13428 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13429 "multiple locations found\n"),
13430 b->number);
13431 return;
13432 }
13433
13434 /* If there's no new locations, and all existing locations are
13435 pending, don't do anything. This optimizes the common case where
13436 all locations are in the same shared library, that was unloaded.
13437 We'd like to retain the location, so that when the library is
13438 loaded again, we don't loose the enabled/disabled status of the
13439 individual locations. */
13440 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13441 return;
13442
13443 existing_locations = hoist_existing_locations (b, filter_pspace);
13444
13445 for (const auto &sal : sals)
13446 {
13447 struct bp_location *new_loc;
13448
13449 switch_to_program_space_and_thread (sal.pspace);
13450
13451 new_loc = add_location_to_breakpoint (b, &sal);
13452
13453 /* Reparse conditions, they might contain references to the
13454 old symtab. */
13455 if (b->cond_string != NULL)
13456 {
13457 const char *s;
13458
13459 s = b->cond_string;
13460 try
13461 {
13462 new_loc->cond = parse_exp_1 (&s, sal.pc,
13463 block_for_pc (sal.pc),
13464 0);
13465 }
13466 catch (const gdb_exception_error &e)
13467 {
13468 warning (_("failed to reevaluate condition "
13469 "for breakpoint %d: %s"),
13470 b->number, e.what ());
13471 new_loc->enabled = 0;
13472 }
13473 }
13474
13475 if (!sals_end.empty ())
13476 {
13477 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13478
13479 new_loc->length = end - sals[0].pc + 1;
13480 }
13481 }
13482
13483 /* If possible, carry over 'disable' status from existing
13484 breakpoints. */
13485 {
13486 struct bp_location *e = existing_locations;
13487 /* If there are multiple breakpoints with the same function name,
13488 e.g. for inline functions, comparing function names won't work.
13489 Instead compare pc addresses; this is just a heuristic as things
13490 may have moved, but in practice it gives the correct answer
13491 often enough until a better solution is found. */
13492 int have_ambiguous_names = ambiguous_names_p (b->loc);
13493
13494 for (; e; e = e->next)
13495 {
13496 if (!e->enabled && e->function_name)
13497 {
13498 struct bp_location *l = b->loc;
13499 if (have_ambiguous_names)
13500 {
13501 for (; l; l = l->next)
13502 if (breakpoint_locations_match (e, l))
13503 {
13504 l->enabled = 0;
13505 break;
13506 }
13507 }
13508 else
13509 {
13510 for (; l; l = l->next)
13511 if (l->function_name
13512 && strcmp (e->function_name, l->function_name) == 0)
13513 {
13514 l->enabled = 0;
13515 break;
13516 }
13517 }
13518 }
13519 }
13520 }
13521
13522 if (!locations_are_equal (existing_locations, b->loc))
13523 gdb::observers::breakpoint_modified.notify (b);
13524 }
13525
13526 /* Find the SaL locations corresponding to the given LOCATION.
13527 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13528
13529 static std::vector<symtab_and_line>
13530 location_to_sals (struct breakpoint *b, struct event_location *location,
13531 struct program_space *search_pspace, int *found)
13532 {
13533 struct gdb_exception exception;
13534
13535 gdb_assert (b->ops != NULL);
13536
13537 std::vector<symtab_and_line> sals;
13538
13539 try
13540 {
13541 sals = b->ops->decode_location (b, location, search_pspace);
13542 }
13543 catch (gdb_exception_error &e)
13544 {
13545 int not_found_and_ok = 0;
13546
13547 /* For pending breakpoints, it's expected that parsing will
13548 fail until the right shared library is loaded. User has
13549 already told to create pending breakpoints and don't need
13550 extra messages. If breakpoint is in bp_shlib_disabled
13551 state, then user already saw the message about that
13552 breakpoint being disabled, and don't want to see more
13553 errors. */
13554 if (e.error == NOT_FOUND_ERROR
13555 && (b->condition_not_parsed
13556 || (b->loc != NULL
13557 && search_pspace != NULL
13558 && b->loc->pspace != search_pspace)
13559 || (b->loc && b->loc->shlib_disabled)
13560 || (b->loc && b->loc->pspace->executing_startup)
13561 || b->enable_state == bp_disabled))
13562 not_found_and_ok = 1;
13563
13564 if (!not_found_and_ok)
13565 {
13566 /* We surely don't want to warn about the same breakpoint
13567 10 times. One solution, implemented here, is disable
13568 the breakpoint on error. Another solution would be to
13569 have separate 'warning emitted' flag. Since this
13570 happens only when a binary has changed, I don't know
13571 which approach is better. */
13572 b->enable_state = bp_disabled;
13573 throw;
13574 }
13575
13576 exception = std::move (e);
13577 }
13578
13579 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13580 {
13581 for (auto &sal : sals)
13582 resolve_sal_pc (&sal);
13583 if (b->condition_not_parsed && b->extra_string != NULL)
13584 {
13585 char *cond_string, *extra_string;
13586 int thread, task;
13587
13588 find_condition_and_thread (b->extra_string, sals[0].pc,
13589 &cond_string, &thread, &task,
13590 &extra_string);
13591 gdb_assert (b->cond_string == NULL);
13592 if (cond_string)
13593 b->cond_string = cond_string;
13594 b->thread = thread;
13595 b->task = task;
13596 if (extra_string)
13597 {
13598 xfree (b->extra_string);
13599 b->extra_string = extra_string;
13600 }
13601 b->condition_not_parsed = 0;
13602 }
13603
13604 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13605 sals[0] = update_static_tracepoint (b, sals[0]);
13606
13607 *found = 1;
13608 }
13609 else
13610 *found = 0;
13611
13612 return sals;
13613 }
13614
13615 /* The default re_set method, for typical hardware or software
13616 breakpoints. Reevaluate the breakpoint and recreate its
13617 locations. */
13618
13619 static void
13620 breakpoint_re_set_default (struct breakpoint *b)
13621 {
13622 struct program_space *filter_pspace = current_program_space;
13623 std::vector<symtab_and_line> expanded, expanded_end;
13624
13625 int found;
13626 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13627 filter_pspace, &found);
13628 if (found)
13629 expanded = std::move (sals);
13630
13631 if (b->location_range_end != NULL)
13632 {
13633 std::vector<symtab_and_line> sals_end
13634 = location_to_sals (b, b->location_range_end.get (),
13635 filter_pspace, &found);
13636 if (found)
13637 expanded_end = std::move (sals_end);
13638 }
13639
13640 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13641 }
13642
13643 /* Default method for creating SALs from an address string. It basically
13644 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13645
13646 static void
13647 create_sals_from_location_default (const struct event_location *location,
13648 struct linespec_result *canonical,
13649 enum bptype type_wanted)
13650 {
13651 parse_breakpoint_sals (location, canonical);
13652 }
13653
13654 /* Call create_breakpoints_sal for the given arguments. This is the default
13655 function for the `create_breakpoints_sal' method of
13656 breakpoint_ops. */
13657
13658 static void
13659 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13660 struct linespec_result *canonical,
13661 gdb::unique_xmalloc_ptr<char> cond_string,
13662 gdb::unique_xmalloc_ptr<char> extra_string,
13663 enum bptype type_wanted,
13664 enum bpdisp disposition,
13665 int thread,
13666 int task, int ignore_count,
13667 const struct breakpoint_ops *ops,
13668 int from_tty, int enabled,
13669 int internal, unsigned flags)
13670 {
13671 create_breakpoints_sal (gdbarch, canonical,
13672 std::move (cond_string),
13673 std::move (extra_string),
13674 type_wanted, disposition,
13675 thread, task, ignore_count, ops, from_tty,
13676 enabled, internal, flags);
13677 }
13678
13679 /* Decode the line represented by S by calling decode_line_full. This is the
13680 default function for the `decode_location' method of breakpoint_ops. */
13681
13682 static std::vector<symtab_and_line>
13683 decode_location_default (struct breakpoint *b,
13684 const struct event_location *location,
13685 struct program_space *search_pspace)
13686 {
13687 struct linespec_result canonical;
13688
13689 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13690 NULL, 0, &canonical, multiple_symbols_all,
13691 b->filter.get ());
13692
13693 /* We should get 0 or 1 resulting SALs. */
13694 gdb_assert (canonical.lsals.size () < 2);
13695
13696 if (!canonical.lsals.empty ())
13697 {
13698 const linespec_sals &lsal = canonical.lsals[0];
13699 return std::move (lsal.sals);
13700 }
13701 return {};
13702 }
13703
13704 /* Reset a breakpoint. */
13705
13706 static void
13707 breakpoint_re_set_one (breakpoint *b)
13708 {
13709 input_radix = b->input_radix;
13710 set_language (b->language);
13711
13712 b->ops->re_set (b);
13713 }
13714
13715 /* Re-set breakpoint locations for the current program space.
13716 Locations bound to other program spaces are left untouched. */
13717
13718 void
13719 breakpoint_re_set (void)
13720 {
13721 struct breakpoint *b, *b_tmp;
13722
13723 {
13724 scoped_restore_current_language save_language;
13725 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13726 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13727
13728 /* breakpoint_re_set_one sets the current_language to the language
13729 of the breakpoint it is resetting (see prepare_re_set_context)
13730 before re-evaluating the breakpoint's location. This change can
13731 unfortunately get undone by accident if the language_mode is set
13732 to auto, and we either switch frames, or more likely in this context,
13733 we select the current frame.
13734
13735 We prevent this by temporarily turning the language_mode to
13736 language_mode_manual. We restore it once all breakpoints
13737 have been reset. */
13738 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13739 language_mode = language_mode_manual;
13740
13741 /* Note: we must not try to insert locations until after all
13742 breakpoints have been re-set. Otherwise, e.g., when re-setting
13743 breakpoint 1, we'd insert the locations of breakpoint 2, which
13744 hadn't been re-set yet, and thus may have stale locations. */
13745
13746 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13747 {
13748 try
13749 {
13750 breakpoint_re_set_one (b);
13751 }
13752 catch (const gdb_exception &ex)
13753 {
13754 exception_fprintf (gdb_stderr, ex,
13755 "Error in re-setting breakpoint %d: ",
13756 b->number);
13757 }
13758 }
13759
13760 jit_breakpoint_re_set ();
13761 }
13762
13763 create_overlay_event_breakpoint ();
13764 create_longjmp_master_breakpoint ();
13765 create_std_terminate_master_breakpoint ();
13766 create_exception_master_breakpoint ();
13767
13768 /* Now we can insert. */
13769 update_global_location_list (UGLL_MAY_INSERT);
13770 }
13771 \f
13772 /* Reset the thread number of this breakpoint:
13773
13774 - If the breakpoint is for all threads, leave it as-is.
13775 - Else, reset it to the current thread for inferior_ptid. */
13776 void
13777 breakpoint_re_set_thread (struct breakpoint *b)
13778 {
13779 if (b->thread != -1)
13780 {
13781 b->thread = inferior_thread ()->global_num;
13782
13783 /* We're being called after following a fork. The new fork is
13784 selected as current, and unless this was a vfork will have a
13785 different program space from the original thread. Reset that
13786 as well. */
13787 b->loc->pspace = current_program_space;
13788 }
13789 }
13790
13791 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13792 If from_tty is nonzero, it prints a message to that effect,
13793 which ends with a period (no newline). */
13794
13795 void
13796 set_ignore_count (int bptnum, int count, int from_tty)
13797 {
13798 struct breakpoint *b;
13799
13800 if (count < 0)
13801 count = 0;
13802
13803 ALL_BREAKPOINTS (b)
13804 if (b->number == bptnum)
13805 {
13806 if (is_tracepoint (b))
13807 {
13808 if (from_tty && count != 0)
13809 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13810 bptnum);
13811 return;
13812 }
13813
13814 b->ignore_count = count;
13815 if (from_tty)
13816 {
13817 if (count == 0)
13818 printf_filtered (_("Will stop next time "
13819 "breakpoint %d is reached."),
13820 bptnum);
13821 else if (count == 1)
13822 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13823 bptnum);
13824 else
13825 printf_filtered (_("Will ignore next %d "
13826 "crossings of breakpoint %d."),
13827 count, bptnum);
13828 }
13829 gdb::observers::breakpoint_modified.notify (b);
13830 return;
13831 }
13832
13833 error (_("No breakpoint number %d."), bptnum);
13834 }
13835
13836 /* Command to set ignore-count of breakpoint N to COUNT. */
13837
13838 static void
13839 ignore_command (const char *args, int from_tty)
13840 {
13841 const char *p = args;
13842 int num;
13843
13844 if (p == 0)
13845 error_no_arg (_("a breakpoint number"));
13846
13847 num = get_number (&p);
13848 if (num == 0)
13849 error (_("bad breakpoint number: '%s'"), args);
13850 if (*p == 0)
13851 error (_("Second argument (specified ignore-count) is missing."));
13852
13853 set_ignore_count (num,
13854 longest_to_int (value_as_long (parse_and_eval (p))),
13855 from_tty);
13856 if (from_tty)
13857 printf_filtered ("\n");
13858 }
13859 \f
13860
13861 /* Call FUNCTION on each of the breakpoints with numbers in the range
13862 defined by BP_NUM_RANGE (an inclusive range). */
13863
13864 static void
13865 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13866 gdb::function_view<void (breakpoint *)> function)
13867 {
13868 if (bp_num_range.first == 0)
13869 {
13870 warning (_("bad breakpoint number at or near '%d'"),
13871 bp_num_range.first);
13872 }
13873 else
13874 {
13875 struct breakpoint *b, *tmp;
13876
13877 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13878 {
13879 bool match = false;
13880
13881 ALL_BREAKPOINTS_SAFE (b, tmp)
13882 if (b->number == i)
13883 {
13884 match = true;
13885 function (b);
13886 break;
13887 }
13888 if (!match)
13889 printf_unfiltered (_("No breakpoint number %d.\n"), i);
13890 }
13891 }
13892 }
13893
13894 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13895 ARGS. */
13896
13897 static void
13898 map_breakpoint_numbers (const char *args,
13899 gdb::function_view<void (breakpoint *)> function)
13900 {
13901 if (args == NULL || *args == '\0')
13902 error_no_arg (_("one or more breakpoint numbers"));
13903
13904 number_or_range_parser parser (args);
13905
13906 while (!parser.finished ())
13907 {
13908 int num = parser.get_number ();
13909 map_breakpoint_number_range (std::make_pair (num, num), function);
13910 }
13911 }
13912
13913 /* Return the breakpoint location structure corresponding to the
13914 BP_NUM and LOC_NUM values. */
13915
13916 static struct bp_location *
13917 find_location_by_number (int bp_num, int loc_num)
13918 {
13919 struct breakpoint *b;
13920
13921 ALL_BREAKPOINTS (b)
13922 if (b->number == bp_num)
13923 {
13924 break;
13925 }
13926
13927 if (!b || b->number != bp_num)
13928 error (_("Bad breakpoint number '%d'"), bp_num);
13929
13930 if (loc_num == 0)
13931 error (_("Bad breakpoint location number '%d'"), loc_num);
13932
13933 int n = 0;
13934 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
13935 if (++n == loc_num)
13936 return loc;
13937
13938 error (_("Bad breakpoint location number '%d'"), loc_num);
13939 }
13940
13941 /* Modes of operation for extract_bp_num. */
13942 enum class extract_bp_kind
13943 {
13944 /* Extracting a breakpoint number. */
13945 bp,
13946
13947 /* Extracting a location number. */
13948 loc,
13949 };
13950
13951 /* Extract a breakpoint or location number (as determined by KIND)
13952 from the string starting at START. TRAILER is a character which
13953 can be found after the number. If you don't want a trailer, use
13954 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13955 string. This always returns a positive integer. */
13956
13957 static int
13958 extract_bp_num (extract_bp_kind kind, const char *start,
13959 int trailer, const char **end_out = NULL)
13960 {
13961 const char *end = start;
13962 int num = get_number_trailer (&end, trailer);
13963 if (num < 0)
13964 error (kind == extract_bp_kind::bp
13965 ? _("Negative breakpoint number '%.*s'")
13966 : _("Negative breakpoint location number '%.*s'"),
13967 int (end - start), start);
13968 if (num == 0)
13969 error (kind == extract_bp_kind::bp
13970 ? _("Bad breakpoint number '%.*s'")
13971 : _("Bad breakpoint location number '%.*s'"),
13972 int (end - start), start);
13973
13974 if (end_out != NULL)
13975 *end_out = end;
13976 return num;
13977 }
13978
13979 /* Extract a breakpoint or location range (as determined by KIND) in
13980 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13981 representing the (inclusive) range. The returned pair's elements
13982 are always positive integers. */
13983
13984 static std::pair<int, int>
13985 extract_bp_or_bp_range (extract_bp_kind kind,
13986 const std::string &arg,
13987 std::string::size_type arg_offset)
13988 {
13989 std::pair<int, int> range;
13990 const char *bp_loc = &arg[arg_offset];
13991 std::string::size_type dash = arg.find ('-', arg_offset);
13992 if (dash != std::string::npos)
13993 {
13994 /* bp_loc is a range (x-z). */
13995 if (arg.length () == dash + 1)
13996 error (kind == extract_bp_kind::bp
13997 ? _("Bad breakpoint number at or near: '%s'")
13998 : _("Bad breakpoint location number at or near: '%s'"),
13999 bp_loc);
14000
14001 const char *end;
14002 const char *start_first = bp_loc;
14003 const char *start_second = &arg[dash + 1];
14004 range.first = extract_bp_num (kind, start_first, '-');
14005 range.second = extract_bp_num (kind, start_second, '\0', &end);
14006
14007 if (range.first > range.second)
14008 error (kind == extract_bp_kind::bp
14009 ? _("Inverted breakpoint range at '%.*s'")
14010 : _("Inverted breakpoint location range at '%.*s'"),
14011 int (end - start_first), start_first);
14012 }
14013 else
14014 {
14015 /* bp_loc is a single value. */
14016 range.first = extract_bp_num (kind, bp_loc, '\0');
14017 range.second = range.first;
14018 }
14019 return range;
14020 }
14021
14022 /* Extract the breakpoint/location range specified by ARG. Returns
14023 the breakpoint range in BP_NUM_RANGE, and the location range in
14024 BP_LOC_RANGE.
14025
14026 ARG may be in any of the following forms:
14027
14028 x where 'x' is a breakpoint number.
14029 x-y where 'x' and 'y' specify a breakpoint numbers range.
14030 x.y where 'x' is a breakpoint number and 'y' a location number.
14031 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14032 location number range.
14033 */
14034
14035 static void
14036 extract_bp_number_and_location (const std::string &arg,
14037 std::pair<int, int> &bp_num_range,
14038 std::pair<int, int> &bp_loc_range)
14039 {
14040 std::string::size_type dot = arg.find ('.');
14041
14042 if (dot != std::string::npos)
14043 {
14044 /* Handle 'x.y' and 'x.y-z' cases. */
14045
14046 if (arg.length () == dot + 1 || dot == 0)
14047 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14048
14049 bp_num_range.first
14050 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14051 bp_num_range.second = bp_num_range.first;
14052
14053 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14054 arg, dot + 1);
14055 }
14056 else
14057 {
14058 /* Handle x and x-y cases. */
14059
14060 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14061 bp_loc_range.first = 0;
14062 bp_loc_range.second = 0;
14063 }
14064 }
14065
14066 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14067 specifies whether to enable or disable. */
14068
14069 static void
14070 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14071 {
14072 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14073 if (loc != NULL)
14074 {
14075 if (loc->enabled != enable)
14076 {
14077 loc->enabled = enable;
14078 mark_breakpoint_location_modified (loc);
14079 }
14080 if (target_supports_enable_disable_tracepoint ()
14081 && current_trace_status ()->running && loc->owner
14082 && is_tracepoint (loc->owner))
14083 target_disable_tracepoint (loc);
14084 }
14085 update_global_location_list (UGLL_DONT_INSERT);
14086
14087 gdb::observers::breakpoint_modified.notify (loc->owner);
14088 }
14089
14090 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14091 number of the breakpoint, and BP_LOC_RANGE specifies the
14092 (inclusive) range of location numbers of that breakpoint to
14093 enable/disable. ENABLE specifies whether to enable or disable the
14094 location. */
14095
14096 static void
14097 enable_disable_breakpoint_location_range (int bp_num,
14098 std::pair<int, int> &bp_loc_range,
14099 bool enable)
14100 {
14101 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14102 enable_disable_bp_num_loc (bp_num, i, enable);
14103 }
14104
14105 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14106 If from_tty is nonzero, it prints a message to that effect,
14107 which ends with a period (no newline). */
14108
14109 void
14110 disable_breakpoint (struct breakpoint *bpt)
14111 {
14112 /* Never disable a watchpoint scope breakpoint; we want to
14113 hit them when we leave scope so we can delete both the
14114 watchpoint and its scope breakpoint at that time. */
14115 if (bpt->type == bp_watchpoint_scope)
14116 return;
14117
14118 bpt->enable_state = bp_disabled;
14119
14120 /* Mark breakpoint locations modified. */
14121 mark_breakpoint_modified (bpt);
14122
14123 if (target_supports_enable_disable_tracepoint ()
14124 && current_trace_status ()->running && is_tracepoint (bpt))
14125 {
14126 struct bp_location *location;
14127
14128 for (location = bpt->loc; location; location = location->next)
14129 target_disable_tracepoint (location);
14130 }
14131
14132 update_global_location_list (UGLL_DONT_INSERT);
14133
14134 gdb::observers::breakpoint_modified.notify (bpt);
14135 }
14136
14137 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14138 specified in ARGS. ARGS may be in any of the formats handled by
14139 extract_bp_number_and_location. ENABLE specifies whether to enable
14140 or disable the breakpoints/locations. */
14141
14142 static void
14143 enable_disable_command (const char *args, int from_tty, bool enable)
14144 {
14145 if (args == 0)
14146 {
14147 struct breakpoint *bpt;
14148
14149 ALL_BREAKPOINTS (bpt)
14150 if (user_breakpoint_p (bpt))
14151 {
14152 if (enable)
14153 enable_breakpoint (bpt);
14154 else
14155 disable_breakpoint (bpt);
14156 }
14157 }
14158 else
14159 {
14160 std::string num = extract_arg (&args);
14161
14162 while (!num.empty ())
14163 {
14164 std::pair<int, int> bp_num_range, bp_loc_range;
14165
14166 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14167
14168 if (bp_loc_range.first == bp_loc_range.second
14169 && bp_loc_range.first == 0)
14170 {
14171 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14172 map_breakpoint_number_range (bp_num_range,
14173 enable
14174 ? enable_breakpoint
14175 : disable_breakpoint);
14176 }
14177 else
14178 {
14179 /* Handle breakpoint ids with formats 'x.y' or
14180 'x.y-z'. */
14181 enable_disable_breakpoint_location_range
14182 (bp_num_range.first, bp_loc_range, enable);
14183 }
14184 num = extract_arg (&args);
14185 }
14186 }
14187 }
14188
14189 /* The disable command disables the specified breakpoints/locations
14190 (or all defined breakpoints) so they're no longer effective in
14191 stopping the inferior. ARGS may be in any of the forms defined in
14192 extract_bp_number_and_location. */
14193
14194 static void
14195 disable_command (const char *args, int from_tty)
14196 {
14197 enable_disable_command (args, from_tty, false);
14198 }
14199
14200 static void
14201 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14202 int count)
14203 {
14204 int target_resources_ok;
14205
14206 if (bpt->type == bp_hardware_breakpoint)
14207 {
14208 int i;
14209 i = hw_breakpoint_used_count ();
14210 target_resources_ok =
14211 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14212 i + 1, 0);
14213 if (target_resources_ok == 0)
14214 error (_("No hardware breakpoint support in the target."));
14215 else if (target_resources_ok < 0)
14216 error (_("Hardware breakpoints used exceeds limit."));
14217 }
14218
14219 if (is_watchpoint (bpt))
14220 {
14221 /* Initialize it just to avoid a GCC false warning. */
14222 enum enable_state orig_enable_state = bp_disabled;
14223
14224 try
14225 {
14226 struct watchpoint *w = (struct watchpoint *) bpt;
14227
14228 orig_enable_state = bpt->enable_state;
14229 bpt->enable_state = bp_enabled;
14230 update_watchpoint (w, 1 /* reparse */);
14231 }
14232 catch (const gdb_exception &e)
14233 {
14234 bpt->enable_state = orig_enable_state;
14235 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14236 bpt->number);
14237 return;
14238 }
14239 }
14240
14241 bpt->enable_state = bp_enabled;
14242
14243 /* Mark breakpoint locations modified. */
14244 mark_breakpoint_modified (bpt);
14245
14246 if (target_supports_enable_disable_tracepoint ()
14247 && current_trace_status ()->running && is_tracepoint (bpt))
14248 {
14249 struct bp_location *location;
14250
14251 for (location = bpt->loc; location; location = location->next)
14252 target_enable_tracepoint (location);
14253 }
14254
14255 bpt->disposition = disposition;
14256 bpt->enable_count = count;
14257 update_global_location_list (UGLL_MAY_INSERT);
14258
14259 gdb::observers::breakpoint_modified.notify (bpt);
14260 }
14261
14262
14263 void
14264 enable_breakpoint (struct breakpoint *bpt)
14265 {
14266 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14267 }
14268
14269 /* The enable command enables the specified breakpoints/locations (or
14270 all defined breakpoints) so they once again become (or continue to
14271 be) effective in stopping the inferior. ARGS may be in any of the
14272 forms defined in extract_bp_number_and_location. */
14273
14274 static void
14275 enable_command (const char *args, int from_tty)
14276 {
14277 enable_disable_command (args, from_tty, true);
14278 }
14279
14280 static void
14281 enable_once_command (const char *args, int from_tty)
14282 {
14283 map_breakpoint_numbers
14284 (args, [&] (breakpoint *b)
14285 {
14286 iterate_over_related_breakpoints
14287 (b, [&] (breakpoint *bpt)
14288 {
14289 enable_breakpoint_disp (bpt, disp_disable, 1);
14290 });
14291 });
14292 }
14293
14294 static void
14295 enable_count_command (const char *args, int from_tty)
14296 {
14297 int count;
14298
14299 if (args == NULL)
14300 error_no_arg (_("hit count"));
14301
14302 count = get_number (&args);
14303
14304 map_breakpoint_numbers
14305 (args, [&] (breakpoint *b)
14306 {
14307 iterate_over_related_breakpoints
14308 (b, [&] (breakpoint *bpt)
14309 {
14310 enable_breakpoint_disp (bpt, disp_disable, count);
14311 });
14312 });
14313 }
14314
14315 static void
14316 enable_delete_command (const char *args, int from_tty)
14317 {
14318 map_breakpoint_numbers
14319 (args, [&] (breakpoint *b)
14320 {
14321 iterate_over_related_breakpoints
14322 (b, [&] (breakpoint *bpt)
14323 {
14324 enable_breakpoint_disp (bpt, disp_del, 1);
14325 });
14326 });
14327 }
14328 \f
14329 static void
14330 set_breakpoint_cmd (const char *args, int from_tty)
14331 {
14332 }
14333
14334 static void
14335 show_breakpoint_cmd (const char *args, int from_tty)
14336 {
14337 }
14338
14339 /* Invalidate last known value of any hardware watchpoint if
14340 the memory which that value represents has been written to by
14341 GDB itself. */
14342
14343 static void
14344 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14345 CORE_ADDR addr, ssize_t len,
14346 const bfd_byte *data)
14347 {
14348 struct breakpoint *bp;
14349
14350 ALL_BREAKPOINTS (bp)
14351 if (bp->enable_state == bp_enabled
14352 && bp->type == bp_hardware_watchpoint)
14353 {
14354 struct watchpoint *wp = (struct watchpoint *) bp;
14355
14356 if (wp->val_valid && wp->val != nullptr)
14357 {
14358 struct bp_location *loc;
14359
14360 for (loc = bp->loc; loc != NULL; loc = loc->next)
14361 if (loc->loc_type == bp_loc_hardware_watchpoint
14362 && loc->address + loc->length > addr
14363 && addr + len > loc->address)
14364 {
14365 wp->val = NULL;
14366 wp->val_valid = false;
14367 }
14368 }
14369 }
14370 }
14371
14372 /* Create and insert a breakpoint for software single step. */
14373
14374 void
14375 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14376 const address_space *aspace,
14377 CORE_ADDR next_pc)
14378 {
14379 struct thread_info *tp = inferior_thread ();
14380 struct symtab_and_line sal;
14381 CORE_ADDR pc = next_pc;
14382
14383 if (tp->control.single_step_breakpoints == NULL)
14384 {
14385 tp->control.single_step_breakpoints
14386 = new_single_step_breakpoint (tp->global_num, gdbarch);
14387 }
14388
14389 sal = find_pc_line (pc, 0);
14390 sal.pc = pc;
14391 sal.section = find_pc_overlay (pc);
14392 sal.explicit_pc = 1;
14393 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14394
14395 update_global_location_list (UGLL_INSERT);
14396 }
14397
14398 /* Insert single step breakpoints according to the current state. */
14399
14400 int
14401 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14402 {
14403 struct regcache *regcache = get_current_regcache ();
14404 std::vector<CORE_ADDR> next_pcs;
14405
14406 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14407
14408 if (!next_pcs.empty ())
14409 {
14410 struct frame_info *frame = get_current_frame ();
14411 const address_space *aspace = get_frame_address_space (frame);
14412
14413 for (CORE_ADDR pc : next_pcs)
14414 insert_single_step_breakpoint (gdbarch, aspace, pc);
14415
14416 return 1;
14417 }
14418 else
14419 return 0;
14420 }
14421
14422 /* See breakpoint.h. */
14423
14424 int
14425 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14426 const address_space *aspace,
14427 CORE_ADDR pc)
14428 {
14429 struct bp_location *loc;
14430
14431 for (loc = bp->loc; loc != NULL; loc = loc->next)
14432 if (loc->inserted
14433 && breakpoint_location_address_match (loc, aspace, pc))
14434 return 1;
14435
14436 return 0;
14437 }
14438
14439 /* Check whether a software single-step breakpoint is inserted at
14440 PC. */
14441
14442 int
14443 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14444 CORE_ADDR pc)
14445 {
14446 struct breakpoint *bpt;
14447
14448 ALL_BREAKPOINTS (bpt)
14449 {
14450 if (bpt->type == bp_single_step
14451 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14452 return 1;
14453 }
14454 return 0;
14455 }
14456
14457 /* Tracepoint-specific operations. */
14458
14459 /* Set tracepoint count to NUM. */
14460 static void
14461 set_tracepoint_count (int num)
14462 {
14463 tracepoint_count = num;
14464 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14465 }
14466
14467 static void
14468 trace_command (const char *arg, int from_tty)
14469 {
14470 struct breakpoint_ops *ops;
14471
14472 event_location_up location = string_to_event_location (&arg,
14473 current_language);
14474 if (location != NULL
14475 && event_location_type (location.get ()) == PROBE_LOCATION)
14476 ops = &tracepoint_probe_breakpoint_ops;
14477 else
14478 ops = &tracepoint_breakpoint_ops;
14479
14480 create_breakpoint (get_current_arch (),
14481 location.get (),
14482 NULL, 0, arg, 1 /* parse arg */,
14483 0 /* tempflag */,
14484 bp_tracepoint /* type_wanted */,
14485 0 /* Ignore count */,
14486 pending_break_support,
14487 ops,
14488 from_tty,
14489 1 /* enabled */,
14490 0 /* internal */, 0);
14491 }
14492
14493 static void
14494 ftrace_command (const char *arg, int from_tty)
14495 {
14496 event_location_up location = string_to_event_location (&arg,
14497 current_language);
14498 create_breakpoint (get_current_arch (),
14499 location.get (),
14500 NULL, 0, arg, 1 /* parse arg */,
14501 0 /* tempflag */,
14502 bp_fast_tracepoint /* type_wanted */,
14503 0 /* Ignore count */,
14504 pending_break_support,
14505 &tracepoint_breakpoint_ops,
14506 from_tty,
14507 1 /* enabled */,
14508 0 /* internal */, 0);
14509 }
14510
14511 /* strace command implementation. Creates a static tracepoint. */
14512
14513 static void
14514 strace_command (const char *arg, int from_tty)
14515 {
14516 struct breakpoint_ops *ops;
14517 event_location_up location;
14518
14519 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14520 or with a normal static tracepoint. */
14521 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14522 {
14523 ops = &strace_marker_breakpoint_ops;
14524 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14525 }
14526 else
14527 {
14528 ops = &tracepoint_breakpoint_ops;
14529 location = string_to_event_location (&arg, current_language);
14530 }
14531
14532 create_breakpoint (get_current_arch (),
14533 location.get (),
14534 NULL, 0, arg, 1 /* parse arg */,
14535 0 /* tempflag */,
14536 bp_static_tracepoint /* type_wanted */,
14537 0 /* Ignore count */,
14538 pending_break_support,
14539 ops,
14540 from_tty,
14541 1 /* enabled */,
14542 0 /* internal */, 0);
14543 }
14544
14545 /* Set up a fake reader function that gets command lines from a linked
14546 list that was acquired during tracepoint uploading. */
14547
14548 static struct uploaded_tp *this_utp;
14549 static int next_cmd;
14550
14551 static char *
14552 read_uploaded_action (void)
14553 {
14554 char *rslt = nullptr;
14555
14556 if (next_cmd < this_utp->cmd_strings.size ())
14557 {
14558 rslt = this_utp->cmd_strings[next_cmd].get ();
14559 next_cmd++;
14560 }
14561
14562 return rslt;
14563 }
14564
14565 /* Given information about a tracepoint as recorded on a target (which
14566 can be either a live system or a trace file), attempt to create an
14567 equivalent GDB tracepoint. This is not a reliable process, since
14568 the target does not necessarily have all the information used when
14569 the tracepoint was originally defined. */
14570
14571 struct tracepoint *
14572 create_tracepoint_from_upload (struct uploaded_tp *utp)
14573 {
14574 const char *addr_str;
14575 char small_buf[100];
14576 struct tracepoint *tp;
14577
14578 if (utp->at_string)
14579 addr_str = utp->at_string.get ();
14580 else
14581 {
14582 /* In the absence of a source location, fall back to raw
14583 address. Since there is no way to confirm that the address
14584 means the same thing as when the trace was started, warn the
14585 user. */
14586 warning (_("Uploaded tracepoint %d has no "
14587 "source location, using raw address"),
14588 utp->number);
14589 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14590 addr_str = small_buf;
14591 }
14592
14593 /* There's not much we can do with a sequence of bytecodes. */
14594 if (utp->cond && !utp->cond_string)
14595 warning (_("Uploaded tracepoint %d condition "
14596 "has no source form, ignoring it"),
14597 utp->number);
14598
14599 event_location_up location = string_to_event_location (&addr_str,
14600 current_language);
14601 if (!create_breakpoint (get_current_arch (),
14602 location.get (),
14603 utp->cond_string.get (), -1, addr_str,
14604 0 /* parse cond/thread */,
14605 0 /* tempflag */,
14606 utp->type /* type_wanted */,
14607 0 /* Ignore count */,
14608 pending_break_support,
14609 &tracepoint_breakpoint_ops,
14610 0 /* from_tty */,
14611 utp->enabled /* enabled */,
14612 0 /* internal */,
14613 CREATE_BREAKPOINT_FLAGS_INSERTED))
14614 return NULL;
14615
14616 /* Get the tracepoint we just created. */
14617 tp = get_tracepoint (tracepoint_count);
14618 gdb_assert (tp != NULL);
14619
14620 if (utp->pass > 0)
14621 {
14622 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14623 tp->number);
14624
14625 trace_pass_command (small_buf, 0);
14626 }
14627
14628 /* If we have uploaded versions of the original commands, set up a
14629 special-purpose "reader" function and call the usual command line
14630 reader, then pass the result to the breakpoint command-setting
14631 function. */
14632 if (!utp->cmd_strings.empty ())
14633 {
14634 counted_command_line cmd_list;
14635
14636 this_utp = utp;
14637 next_cmd = 0;
14638
14639 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14640
14641 breakpoint_set_commands (tp, std::move (cmd_list));
14642 }
14643 else if (!utp->actions.empty ()
14644 || !utp->step_actions.empty ())
14645 warning (_("Uploaded tracepoint %d actions "
14646 "have no source form, ignoring them"),
14647 utp->number);
14648
14649 /* Copy any status information that might be available. */
14650 tp->hit_count = utp->hit_count;
14651 tp->traceframe_usage = utp->traceframe_usage;
14652
14653 return tp;
14654 }
14655
14656 /* Print information on tracepoint number TPNUM_EXP, or all if
14657 omitted. */
14658
14659 static void
14660 info_tracepoints_command (const char *args, int from_tty)
14661 {
14662 struct ui_out *uiout = current_uiout;
14663 int num_printed;
14664
14665 num_printed = breakpoint_1 (args, false, is_tracepoint);
14666
14667 if (num_printed == 0)
14668 {
14669 if (args == NULL || *args == '\0')
14670 uiout->message ("No tracepoints.\n");
14671 else
14672 uiout->message ("No tracepoint matching '%s'.\n", args);
14673 }
14674
14675 default_collect_info ();
14676 }
14677
14678 /* The 'enable trace' command enables tracepoints.
14679 Not supported by all targets. */
14680 static void
14681 enable_trace_command (const char *args, int from_tty)
14682 {
14683 enable_command (args, from_tty);
14684 }
14685
14686 /* The 'disable trace' command disables tracepoints.
14687 Not supported by all targets. */
14688 static void
14689 disable_trace_command (const char *args, int from_tty)
14690 {
14691 disable_command (args, from_tty);
14692 }
14693
14694 /* Remove a tracepoint (or all if no argument). */
14695 static void
14696 delete_trace_command (const char *arg, int from_tty)
14697 {
14698 struct breakpoint *b, *b_tmp;
14699
14700 dont_repeat ();
14701
14702 if (arg == 0)
14703 {
14704 int breaks_to_delete = 0;
14705
14706 /* Delete all breakpoints if no argument.
14707 Do not delete internal or call-dummy breakpoints, these
14708 have to be deleted with an explicit breakpoint number
14709 argument. */
14710 ALL_TRACEPOINTS (b)
14711 if (is_tracepoint (b) && user_breakpoint_p (b))
14712 {
14713 breaks_to_delete = 1;
14714 break;
14715 }
14716
14717 /* Ask user only if there are some breakpoints to delete. */
14718 if (!from_tty
14719 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14720 {
14721 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14722 if (is_tracepoint (b) && user_breakpoint_p (b))
14723 delete_breakpoint (b);
14724 }
14725 }
14726 else
14727 map_breakpoint_numbers
14728 (arg, [&] (breakpoint *br)
14729 {
14730 iterate_over_related_breakpoints (br, delete_breakpoint);
14731 });
14732 }
14733
14734 /* Helper function for trace_pass_command. */
14735
14736 static void
14737 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14738 {
14739 tp->pass_count = count;
14740 gdb::observers::breakpoint_modified.notify (tp);
14741 if (from_tty)
14742 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14743 tp->number, count);
14744 }
14745
14746 /* Set passcount for tracepoint.
14747
14748 First command argument is passcount, second is tracepoint number.
14749 If tracepoint number omitted, apply to most recently defined.
14750 Also accepts special argument "all". */
14751
14752 static void
14753 trace_pass_command (const char *args, int from_tty)
14754 {
14755 struct tracepoint *t1;
14756 ULONGEST count;
14757
14758 if (args == 0 || *args == 0)
14759 error (_("passcount command requires an "
14760 "argument (count + optional TP num)"));
14761
14762 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14763
14764 args = skip_spaces (args);
14765 if (*args && strncasecmp (args, "all", 3) == 0)
14766 {
14767 struct breakpoint *b;
14768
14769 args += 3; /* Skip special argument "all". */
14770 if (*args)
14771 error (_("Junk at end of arguments."));
14772
14773 ALL_TRACEPOINTS (b)
14774 {
14775 t1 = (struct tracepoint *) b;
14776 trace_pass_set_count (t1, count, from_tty);
14777 }
14778 }
14779 else if (*args == '\0')
14780 {
14781 t1 = get_tracepoint_by_number (&args, NULL);
14782 if (t1)
14783 trace_pass_set_count (t1, count, from_tty);
14784 }
14785 else
14786 {
14787 number_or_range_parser parser (args);
14788 while (!parser.finished ())
14789 {
14790 t1 = get_tracepoint_by_number (&args, &parser);
14791 if (t1)
14792 trace_pass_set_count (t1, count, from_tty);
14793 }
14794 }
14795 }
14796
14797 struct tracepoint *
14798 get_tracepoint (int num)
14799 {
14800 struct breakpoint *t;
14801
14802 ALL_TRACEPOINTS (t)
14803 if (t->number == num)
14804 return (struct tracepoint *) t;
14805
14806 return NULL;
14807 }
14808
14809 /* Find the tracepoint with the given target-side number (which may be
14810 different from the tracepoint number after disconnecting and
14811 reconnecting). */
14812
14813 struct tracepoint *
14814 get_tracepoint_by_number_on_target (int num)
14815 {
14816 struct breakpoint *b;
14817
14818 ALL_TRACEPOINTS (b)
14819 {
14820 struct tracepoint *t = (struct tracepoint *) b;
14821
14822 if (t->number_on_target == num)
14823 return t;
14824 }
14825
14826 return NULL;
14827 }
14828
14829 /* Utility: parse a tracepoint number and look it up in the list.
14830 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14831 If the argument is missing, the most recent tracepoint
14832 (tracepoint_count) is returned. */
14833
14834 struct tracepoint *
14835 get_tracepoint_by_number (const char **arg,
14836 number_or_range_parser *parser)
14837 {
14838 struct breakpoint *t;
14839 int tpnum;
14840 const char *instring = arg == NULL ? NULL : *arg;
14841
14842 if (parser != NULL)
14843 {
14844 gdb_assert (!parser->finished ());
14845 tpnum = parser->get_number ();
14846 }
14847 else if (arg == NULL || *arg == NULL || ! **arg)
14848 tpnum = tracepoint_count;
14849 else
14850 tpnum = get_number (arg);
14851
14852 if (tpnum <= 0)
14853 {
14854 if (instring && *instring)
14855 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14856 instring);
14857 else
14858 printf_filtered (_("No previous tracepoint\n"));
14859 return NULL;
14860 }
14861
14862 ALL_TRACEPOINTS (t)
14863 if (t->number == tpnum)
14864 {
14865 return (struct tracepoint *) t;
14866 }
14867
14868 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14869 return NULL;
14870 }
14871
14872 void
14873 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14874 {
14875 if (b->thread != -1)
14876 fprintf_unfiltered (fp, " thread %d", b->thread);
14877
14878 if (b->task != 0)
14879 fprintf_unfiltered (fp, " task %d", b->task);
14880
14881 fprintf_unfiltered (fp, "\n");
14882 }
14883
14884 /* Save information on user settable breakpoints (watchpoints, etc) to
14885 a new script file named FILENAME. If FILTER is non-NULL, call it
14886 on each breakpoint and only include the ones for which it returns
14887 true. */
14888
14889 static void
14890 save_breakpoints (const char *filename, int from_tty,
14891 bool (*filter) (const struct breakpoint *))
14892 {
14893 struct breakpoint *tp;
14894 int any = 0;
14895 int extra_trace_bits = 0;
14896
14897 if (filename == 0 || *filename == 0)
14898 error (_("Argument required (file name in which to save)"));
14899
14900 /* See if we have anything to save. */
14901 ALL_BREAKPOINTS (tp)
14902 {
14903 /* Skip internal and momentary breakpoints. */
14904 if (!user_breakpoint_p (tp))
14905 continue;
14906
14907 /* If we have a filter, only save the breakpoints it accepts. */
14908 if (filter && !filter (tp))
14909 continue;
14910
14911 any = 1;
14912
14913 if (is_tracepoint (tp))
14914 {
14915 extra_trace_bits = 1;
14916
14917 /* We can stop searching. */
14918 break;
14919 }
14920 }
14921
14922 if (!any)
14923 {
14924 warning (_("Nothing to save."));
14925 return;
14926 }
14927
14928 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14929
14930 stdio_file fp;
14931
14932 if (!fp.open (expanded_filename.get (), "w"))
14933 error (_("Unable to open file '%s' for saving (%s)"),
14934 expanded_filename.get (), safe_strerror (errno));
14935
14936 if (extra_trace_bits)
14937 save_trace_state_variables (&fp);
14938
14939 ALL_BREAKPOINTS (tp)
14940 {
14941 /* Skip internal and momentary breakpoints. */
14942 if (!user_breakpoint_p (tp))
14943 continue;
14944
14945 /* If we have a filter, only save the breakpoints it accepts. */
14946 if (filter && !filter (tp))
14947 continue;
14948
14949 tp->ops->print_recreate (tp, &fp);
14950
14951 /* Note, we can't rely on tp->number for anything, as we can't
14952 assume the recreated breakpoint numbers will match. Use $bpnum
14953 instead. */
14954
14955 if (tp->cond_string)
14956 fp.printf (" condition $bpnum %s\n", tp->cond_string);
14957
14958 if (tp->ignore_count)
14959 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14960
14961 if (tp->type != bp_dprintf && tp->commands)
14962 {
14963 fp.puts (" commands\n");
14964
14965 current_uiout->redirect (&fp);
14966 try
14967 {
14968 print_command_lines (current_uiout, tp->commands.get (), 2);
14969 }
14970 catch (const gdb_exception &ex)
14971 {
14972 current_uiout->redirect (NULL);
14973 throw;
14974 }
14975
14976 current_uiout->redirect (NULL);
14977 fp.puts (" end\n");
14978 }
14979
14980 if (tp->enable_state == bp_disabled)
14981 fp.puts ("disable $bpnum\n");
14982
14983 /* If this is a multi-location breakpoint, check if the locations
14984 should be individually disabled. Watchpoint locations are
14985 special, and not user visible. */
14986 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14987 {
14988 struct bp_location *loc;
14989 int n = 1;
14990
14991 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
14992 if (!loc->enabled)
14993 fp.printf ("disable $bpnum.%d\n", n);
14994 }
14995 }
14996
14997 if (extra_trace_bits && *default_collect)
14998 fp.printf ("set default-collect %s\n", default_collect);
14999
15000 if (from_tty)
15001 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15002 }
15003
15004 /* The `save breakpoints' command. */
15005
15006 static void
15007 save_breakpoints_command (const char *args, int from_tty)
15008 {
15009 save_breakpoints (args, from_tty, NULL);
15010 }
15011
15012 /* The `save tracepoints' command. */
15013
15014 static void
15015 save_tracepoints_command (const char *args, int from_tty)
15016 {
15017 save_breakpoints (args, from_tty, is_tracepoint);
15018 }
15019
15020 /* Create a vector of all tracepoints. */
15021
15022 std::vector<breakpoint *>
15023 all_tracepoints (void)
15024 {
15025 std::vector<breakpoint *> tp_vec;
15026 struct breakpoint *tp;
15027
15028 ALL_TRACEPOINTS (tp)
15029 {
15030 tp_vec.push_back (tp);
15031 }
15032
15033 return tp_vec;
15034 }
15035
15036 \f
15037 /* This help string is used to consolidate all the help string for specifying
15038 locations used by several commands. */
15039
15040 #define LOCATION_HELP_STRING \
15041 "Linespecs are colon-separated lists of location parameters, such as\n\
15042 source filename, function name, label name, and line number.\n\
15043 Example: To specify the start of a label named \"the_top\" in the\n\
15044 function \"fact\" in the file \"factorial.c\", use\n\
15045 \"factorial.c:fact:the_top\".\n\
15046 \n\
15047 Address locations begin with \"*\" and specify an exact address in the\n\
15048 program. Example: To specify the fourth byte past the start function\n\
15049 \"main\", use \"*main + 4\".\n\
15050 \n\
15051 Explicit locations are similar to linespecs but use an option/argument\n\
15052 syntax to specify location parameters.\n\
15053 Example: To specify the start of the label named \"the_top\" in the\n\
15054 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15055 -function fact -label the_top\".\n\
15056 \n\
15057 By default, a specified function is matched against the program's\n\
15058 functions in all scopes. For C++, this means in all namespaces and\n\
15059 classes. For Ada, this means in all packages. E.g., in C++,\n\
15060 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15061 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15062 specified name as a complete fully-qualified name instead."
15063
15064 /* This help string is used for the break, hbreak, tbreak and thbreak
15065 commands. It is defined as a macro to prevent duplication.
15066 COMMAND should be a string constant containing the name of the
15067 command. */
15068
15069 #define BREAK_ARGS_HELP(command) \
15070 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15071 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15072 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15073 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15074 `-probe-dtrace' (for a DTrace probe).\n\
15075 LOCATION may be a linespec, address, or explicit location as described\n\
15076 below.\n\
15077 \n\
15078 With no LOCATION, uses current execution address of the selected\n\
15079 stack frame. This is useful for breaking on return to a stack frame.\n\
15080 \n\
15081 THREADNUM is the number from \"info threads\".\n\
15082 CONDITION is a boolean expression.\n\
15083 \n" LOCATION_HELP_STRING "\n\n\
15084 Multiple breakpoints at one place are permitted, and useful if their\n\
15085 conditions are different.\n\
15086 \n\
15087 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15088
15089 /* List of subcommands for "catch". */
15090 static struct cmd_list_element *catch_cmdlist;
15091
15092 /* List of subcommands for "tcatch". */
15093 static struct cmd_list_element *tcatch_cmdlist;
15094
15095 void
15096 add_catch_command (const char *name, const char *docstring,
15097 cmd_const_sfunc_ftype *sfunc,
15098 completer_ftype *completer,
15099 void *user_data_catch,
15100 void *user_data_tcatch)
15101 {
15102 struct cmd_list_element *command;
15103
15104 command = add_cmd (name, class_breakpoint, docstring,
15105 &catch_cmdlist);
15106 set_cmd_sfunc (command, sfunc);
15107 set_cmd_context (command, user_data_catch);
15108 set_cmd_completer (command, completer);
15109
15110 command = add_cmd (name, class_breakpoint, docstring,
15111 &tcatch_cmdlist);
15112 set_cmd_sfunc (command, sfunc);
15113 set_cmd_context (command, user_data_tcatch);
15114 set_cmd_completer (command, completer);
15115 }
15116
15117 static void
15118 save_command (const char *arg, int from_tty)
15119 {
15120 printf_unfiltered (_("\"save\" must be followed by "
15121 "the name of a save subcommand.\n"));
15122 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15123 }
15124
15125 struct breakpoint *
15126 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15127 {
15128 struct breakpoint *b, *b_tmp;
15129
15130 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15131 {
15132 if (callback (b))
15133 return b;
15134 }
15135
15136 return NULL;
15137 }
15138
15139 /* Zero if any of the breakpoint's locations could be a location where
15140 functions have been inlined, nonzero otherwise. */
15141
15142 static int
15143 is_non_inline_function (struct breakpoint *b)
15144 {
15145 /* The shared library event breakpoint is set on the address of a
15146 non-inline function. */
15147 if (b->type == bp_shlib_event)
15148 return 1;
15149
15150 return 0;
15151 }
15152
15153 /* Nonzero if the specified PC cannot be a location where functions
15154 have been inlined. */
15155
15156 int
15157 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15158 const struct target_waitstatus *ws)
15159 {
15160 struct breakpoint *b;
15161 struct bp_location *bl;
15162
15163 ALL_BREAKPOINTS (b)
15164 {
15165 if (!is_non_inline_function (b))
15166 continue;
15167
15168 for (bl = b->loc; bl != NULL; bl = bl->next)
15169 {
15170 if (!bl->shlib_disabled
15171 && bpstat_check_location (bl, aspace, pc, ws))
15172 return 1;
15173 }
15174 }
15175
15176 return 0;
15177 }
15178
15179 /* Remove any references to OBJFILE which is going to be freed. */
15180
15181 void
15182 breakpoint_free_objfile (struct objfile *objfile)
15183 {
15184 struct bp_location **locp, *loc;
15185
15186 ALL_BP_LOCATIONS (loc, locp)
15187 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15188 loc->symtab = NULL;
15189 }
15190
15191 void
15192 initialize_breakpoint_ops (void)
15193 {
15194 static int initialized = 0;
15195
15196 struct breakpoint_ops *ops;
15197
15198 if (initialized)
15199 return;
15200 initialized = 1;
15201
15202 /* The breakpoint_ops structure to be inherit by all kinds of
15203 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15204 internal and momentary breakpoints, etc.). */
15205 ops = &bkpt_base_breakpoint_ops;
15206 *ops = base_breakpoint_ops;
15207 ops->re_set = bkpt_re_set;
15208 ops->insert_location = bkpt_insert_location;
15209 ops->remove_location = bkpt_remove_location;
15210 ops->breakpoint_hit = bkpt_breakpoint_hit;
15211 ops->create_sals_from_location = bkpt_create_sals_from_location;
15212 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15213 ops->decode_location = bkpt_decode_location;
15214
15215 /* The breakpoint_ops structure to be used in regular breakpoints. */
15216 ops = &bkpt_breakpoint_ops;
15217 *ops = bkpt_base_breakpoint_ops;
15218 ops->re_set = bkpt_re_set;
15219 ops->resources_needed = bkpt_resources_needed;
15220 ops->print_it = bkpt_print_it;
15221 ops->print_mention = bkpt_print_mention;
15222 ops->print_recreate = bkpt_print_recreate;
15223
15224 /* Ranged breakpoints. */
15225 ops = &ranged_breakpoint_ops;
15226 *ops = bkpt_breakpoint_ops;
15227 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15228 ops->resources_needed = resources_needed_ranged_breakpoint;
15229 ops->print_it = print_it_ranged_breakpoint;
15230 ops->print_one = print_one_ranged_breakpoint;
15231 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15232 ops->print_mention = print_mention_ranged_breakpoint;
15233 ops->print_recreate = print_recreate_ranged_breakpoint;
15234
15235 /* Internal breakpoints. */
15236 ops = &internal_breakpoint_ops;
15237 *ops = bkpt_base_breakpoint_ops;
15238 ops->re_set = internal_bkpt_re_set;
15239 ops->check_status = internal_bkpt_check_status;
15240 ops->print_it = internal_bkpt_print_it;
15241 ops->print_mention = internal_bkpt_print_mention;
15242
15243 /* Momentary breakpoints. */
15244 ops = &momentary_breakpoint_ops;
15245 *ops = bkpt_base_breakpoint_ops;
15246 ops->re_set = momentary_bkpt_re_set;
15247 ops->check_status = momentary_bkpt_check_status;
15248 ops->print_it = momentary_bkpt_print_it;
15249 ops->print_mention = momentary_bkpt_print_mention;
15250
15251 /* Probe breakpoints. */
15252 ops = &bkpt_probe_breakpoint_ops;
15253 *ops = bkpt_breakpoint_ops;
15254 ops->insert_location = bkpt_probe_insert_location;
15255 ops->remove_location = bkpt_probe_remove_location;
15256 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15257 ops->decode_location = bkpt_probe_decode_location;
15258
15259 /* Watchpoints. */
15260 ops = &watchpoint_breakpoint_ops;
15261 *ops = base_breakpoint_ops;
15262 ops->re_set = re_set_watchpoint;
15263 ops->insert_location = insert_watchpoint;
15264 ops->remove_location = remove_watchpoint;
15265 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15266 ops->check_status = check_status_watchpoint;
15267 ops->resources_needed = resources_needed_watchpoint;
15268 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15269 ops->print_it = print_it_watchpoint;
15270 ops->print_mention = print_mention_watchpoint;
15271 ops->print_recreate = print_recreate_watchpoint;
15272 ops->explains_signal = explains_signal_watchpoint;
15273
15274 /* Masked watchpoints. */
15275 ops = &masked_watchpoint_breakpoint_ops;
15276 *ops = watchpoint_breakpoint_ops;
15277 ops->insert_location = insert_masked_watchpoint;
15278 ops->remove_location = remove_masked_watchpoint;
15279 ops->resources_needed = resources_needed_masked_watchpoint;
15280 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15281 ops->print_it = print_it_masked_watchpoint;
15282 ops->print_one_detail = print_one_detail_masked_watchpoint;
15283 ops->print_mention = print_mention_masked_watchpoint;
15284 ops->print_recreate = print_recreate_masked_watchpoint;
15285
15286 /* Tracepoints. */
15287 ops = &tracepoint_breakpoint_ops;
15288 *ops = base_breakpoint_ops;
15289 ops->re_set = tracepoint_re_set;
15290 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15291 ops->print_one_detail = tracepoint_print_one_detail;
15292 ops->print_mention = tracepoint_print_mention;
15293 ops->print_recreate = tracepoint_print_recreate;
15294 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15295 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15296 ops->decode_location = tracepoint_decode_location;
15297
15298 /* Probe tracepoints. */
15299 ops = &tracepoint_probe_breakpoint_ops;
15300 *ops = tracepoint_breakpoint_ops;
15301 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15302 ops->decode_location = tracepoint_probe_decode_location;
15303
15304 /* Static tracepoints with marker (`-m'). */
15305 ops = &strace_marker_breakpoint_ops;
15306 *ops = tracepoint_breakpoint_ops;
15307 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15308 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15309 ops->decode_location = strace_marker_decode_location;
15310
15311 /* Fork catchpoints. */
15312 ops = &catch_fork_breakpoint_ops;
15313 *ops = base_breakpoint_ops;
15314 ops->insert_location = insert_catch_fork;
15315 ops->remove_location = remove_catch_fork;
15316 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15317 ops->print_it = print_it_catch_fork;
15318 ops->print_one = print_one_catch_fork;
15319 ops->print_mention = print_mention_catch_fork;
15320 ops->print_recreate = print_recreate_catch_fork;
15321
15322 /* Vfork catchpoints. */
15323 ops = &catch_vfork_breakpoint_ops;
15324 *ops = base_breakpoint_ops;
15325 ops->insert_location = insert_catch_vfork;
15326 ops->remove_location = remove_catch_vfork;
15327 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15328 ops->print_it = print_it_catch_vfork;
15329 ops->print_one = print_one_catch_vfork;
15330 ops->print_mention = print_mention_catch_vfork;
15331 ops->print_recreate = print_recreate_catch_vfork;
15332
15333 /* Exec catchpoints. */
15334 ops = &catch_exec_breakpoint_ops;
15335 *ops = base_breakpoint_ops;
15336 ops->insert_location = insert_catch_exec;
15337 ops->remove_location = remove_catch_exec;
15338 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15339 ops->print_it = print_it_catch_exec;
15340 ops->print_one = print_one_catch_exec;
15341 ops->print_mention = print_mention_catch_exec;
15342 ops->print_recreate = print_recreate_catch_exec;
15343
15344 /* Solib-related catchpoints. */
15345 ops = &catch_solib_breakpoint_ops;
15346 *ops = base_breakpoint_ops;
15347 ops->insert_location = insert_catch_solib;
15348 ops->remove_location = remove_catch_solib;
15349 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15350 ops->check_status = check_status_catch_solib;
15351 ops->print_it = print_it_catch_solib;
15352 ops->print_one = print_one_catch_solib;
15353 ops->print_mention = print_mention_catch_solib;
15354 ops->print_recreate = print_recreate_catch_solib;
15355
15356 ops = &dprintf_breakpoint_ops;
15357 *ops = bkpt_base_breakpoint_ops;
15358 ops->re_set = dprintf_re_set;
15359 ops->resources_needed = bkpt_resources_needed;
15360 ops->print_it = bkpt_print_it;
15361 ops->print_mention = bkpt_print_mention;
15362 ops->print_recreate = dprintf_print_recreate;
15363 ops->after_condition_true = dprintf_after_condition_true;
15364 ops->breakpoint_hit = dprintf_breakpoint_hit;
15365 }
15366
15367 /* Chain containing all defined "enable breakpoint" subcommands. */
15368
15369 static struct cmd_list_element *enablebreaklist = NULL;
15370
15371 /* See breakpoint.h. */
15372
15373 cmd_list_element *commands_cmd_element = nullptr;
15374
15375 void
15376 _initialize_breakpoint (void)
15377 {
15378 struct cmd_list_element *c;
15379
15380 initialize_breakpoint_ops ();
15381
15382 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15383 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15384 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15385
15386 breakpoint_chain = 0;
15387 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15388 before a breakpoint is set. */
15389 breakpoint_count = 0;
15390
15391 tracepoint_count = 0;
15392
15393 add_com ("ignore", class_breakpoint, ignore_command, _("\
15394 Set ignore-count of breakpoint number N to COUNT.\n\
15395 Usage is `ignore N COUNT'."));
15396
15397 commands_cmd_element = add_com ("commands", class_breakpoint,
15398 commands_command, _("\
15399 Set commands to be executed when the given breakpoints are hit.\n\
15400 Give a space-separated breakpoint list as argument after \"commands\".\n\
15401 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15402 (e.g. `5-7').\n\
15403 With no argument, the targeted breakpoint is the last one set.\n\
15404 The commands themselves follow starting on the next line.\n\
15405 Type a line containing \"end\" to indicate the end of them.\n\
15406 Give \"silent\" as the first line to make the breakpoint silent;\n\
15407 then no output is printed when it is hit, except what the commands print."));
15408
15409 c = add_com ("condition", class_breakpoint, condition_command, _("\
15410 Specify breakpoint number N to break only if COND is true.\n\
15411 Usage is `condition N COND', where N is an integer and COND is an\n\
15412 expression to be evaluated whenever breakpoint N is reached."));
15413 set_cmd_completer (c, condition_completer);
15414
15415 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15416 Set a temporary breakpoint.\n\
15417 Like \"break\" except the breakpoint is only temporary,\n\
15418 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15419 by using \"enable delete\" on the breakpoint number.\n\
15420 \n"
15421 BREAK_ARGS_HELP ("tbreak")));
15422 set_cmd_completer (c, location_completer);
15423
15424 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15425 Set a hardware assisted breakpoint.\n\
15426 Like \"break\" except the breakpoint requires hardware support,\n\
15427 some target hardware may not have this support.\n\
15428 \n"
15429 BREAK_ARGS_HELP ("hbreak")));
15430 set_cmd_completer (c, location_completer);
15431
15432 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15433 Set a temporary hardware assisted breakpoint.\n\
15434 Like \"hbreak\" except the breakpoint is only temporary,\n\
15435 so it will be deleted when hit.\n\
15436 \n"
15437 BREAK_ARGS_HELP ("thbreak")));
15438 set_cmd_completer (c, location_completer);
15439
15440 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15441 Enable all or some breakpoints.\n\
15442 Usage: enable [BREAKPOINTNUM]...\n\
15443 Give breakpoint numbers (separated by spaces) as arguments.\n\
15444 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15445 This is used to cancel the effect of the \"disable\" command.\n\
15446 With a subcommand you can enable temporarily."),
15447 &enablelist, "enable ", 1, &cmdlist);
15448
15449 add_com_alias ("en", "enable", class_breakpoint, 1);
15450
15451 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15452 Enable all or some breakpoints.\n\
15453 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15454 Give breakpoint numbers (separated by spaces) as arguments.\n\
15455 This is used to cancel the effect of the \"disable\" command.\n\
15456 May be abbreviated to simply \"enable\"."),
15457 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15458
15459 add_cmd ("once", no_class, enable_once_command, _("\
15460 Enable some breakpoints for one hit.\n\
15461 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15462 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15463 &enablebreaklist);
15464
15465 add_cmd ("delete", no_class, enable_delete_command, _("\
15466 Enable some breakpoints and delete when hit.\n\
15467 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15468 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15469 &enablebreaklist);
15470
15471 add_cmd ("count", no_class, enable_count_command, _("\
15472 Enable some breakpoints for COUNT hits.\n\
15473 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15474 If a breakpoint is hit while enabled in this fashion,\n\
15475 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15476 &enablebreaklist);
15477
15478 add_cmd ("delete", no_class, enable_delete_command, _("\
15479 Enable some breakpoints and delete when hit.\n\
15480 Usage: enable delete BREAKPOINTNUM...\n\
15481 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15482 &enablelist);
15483
15484 add_cmd ("once", no_class, enable_once_command, _("\
15485 Enable some breakpoints for one hit.\n\
15486 Usage: enable once BREAKPOINTNUM...\n\
15487 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15488 &enablelist);
15489
15490 add_cmd ("count", no_class, enable_count_command, _("\
15491 Enable some breakpoints for COUNT hits.\n\
15492 Usage: enable count COUNT BREAKPOINTNUM...\n\
15493 If a breakpoint is hit while enabled in this fashion,\n\
15494 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15495 &enablelist);
15496
15497 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15498 Disable all or some breakpoints.\n\
15499 Usage: disable [BREAKPOINTNUM]...\n\
15500 Arguments are breakpoint numbers with spaces in between.\n\
15501 To disable all breakpoints, give no argument.\n\
15502 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15503 &disablelist, "disable ", 1, &cmdlist);
15504 add_com_alias ("dis", "disable", class_breakpoint, 1);
15505 add_com_alias ("disa", "disable", class_breakpoint, 1);
15506
15507 add_cmd ("breakpoints", class_alias, disable_command, _("\
15508 Disable all or some breakpoints.\n\
15509 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15510 Arguments are breakpoint numbers with spaces in between.\n\
15511 To disable all breakpoints, give no argument.\n\
15512 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15513 This command may be abbreviated \"disable\"."),
15514 &disablelist);
15515
15516 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15517 Delete all or some breakpoints.\n\
15518 Usage: delete [BREAKPOINTNUM]...\n\
15519 Arguments are breakpoint numbers with spaces in between.\n\
15520 To delete all breakpoints, give no argument.\n\
15521 \n\
15522 Also a prefix command for deletion of other GDB objects."),
15523 &deletelist, "delete ", 1, &cmdlist);
15524 add_com_alias ("d", "delete", class_breakpoint, 1);
15525 add_com_alias ("del", "delete", class_breakpoint, 1);
15526
15527 add_cmd ("breakpoints", class_alias, delete_command, _("\
15528 Delete all or some breakpoints or auto-display expressions.\n\
15529 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15530 Arguments are breakpoint numbers with spaces in between.\n\
15531 To delete all breakpoints, give no argument.\n\
15532 This command may be abbreviated \"delete\"."),
15533 &deletelist);
15534
15535 add_com ("clear", class_breakpoint, clear_command, _("\
15536 Clear breakpoint at specified location.\n\
15537 Argument may be a linespec, explicit, or address location as described below.\n\
15538 \n\
15539 With no argument, clears all breakpoints in the line that the selected frame\n\
15540 is executing in.\n"
15541 "\n" LOCATION_HELP_STRING "\n\n\
15542 See also the \"delete\" command which clears breakpoints by number."));
15543 add_com_alias ("cl", "clear", class_breakpoint, 1);
15544
15545 c = add_com ("break", class_breakpoint, break_command, _("\
15546 Set breakpoint at specified location.\n"
15547 BREAK_ARGS_HELP ("break")));
15548 set_cmd_completer (c, location_completer);
15549
15550 add_com_alias ("b", "break", class_run, 1);
15551 add_com_alias ("br", "break", class_run, 1);
15552 add_com_alias ("bre", "break", class_run, 1);
15553 add_com_alias ("brea", "break", class_run, 1);
15554
15555 if (dbx_commands)
15556 {
15557 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15558 Break in function/address or break at a line in the current file."),
15559 &stoplist, "stop ", 1, &cmdlist);
15560 add_cmd ("in", class_breakpoint, stopin_command,
15561 _("Break in function or address."), &stoplist);
15562 add_cmd ("at", class_breakpoint, stopat_command,
15563 _("Break at a line in the current file."), &stoplist);
15564 add_com ("status", class_info, info_breakpoints_command, _("\
15565 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15566 The \"Type\" column indicates one of:\n\
15567 \tbreakpoint - normal breakpoint\n\
15568 \twatchpoint - watchpoint\n\
15569 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15570 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15571 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15572 address and file/line number respectively.\n\
15573 \n\
15574 Convenience variable \"$_\" and default examine address for \"x\"\n\
15575 are set to the address of the last breakpoint listed unless the command\n\
15576 is prefixed with \"server \".\n\n\
15577 Convenience variable \"$bpnum\" contains the number of the last\n\
15578 breakpoint set."));
15579 }
15580
15581 add_info ("breakpoints", info_breakpoints_command, _("\
15582 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15583 The \"Type\" column indicates one of:\n\
15584 \tbreakpoint - normal breakpoint\n\
15585 \twatchpoint - watchpoint\n\
15586 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15587 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15588 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15589 address and file/line number respectively.\n\
15590 \n\
15591 Convenience variable \"$_\" and default examine address for \"x\"\n\
15592 are set to the address of the last breakpoint listed unless the command\n\
15593 is prefixed with \"server \".\n\n\
15594 Convenience variable \"$bpnum\" contains the number of the last\n\
15595 breakpoint set."));
15596
15597 add_info_alias ("b", "breakpoints", 1);
15598
15599 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15600 Status of all breakpoints, or breakpoint number NUMBER.\n\
15601 The \"Type\" column indicates one of:\n\
15602 \tbreakpoint - normal breakpoint\n\
15603 \twatchpoint - watchpoint\n\
15604 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15605 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15606 \tuntil - internal breakpoint used by the \"until\" command\n\
15607 \tfinish - internal breakpoint used by the \"finish\" command\n\
15608 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15609 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15610 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15611 address and file/line number respectively.\n\
15612 \n\
15613 Convenience variable \"$_\" and default examine address for \"x\"\n\
15614 are set to the address of the last breakpoint listed unless the command\n\
15615 is prefixed with \"server \".\n\n\
15616 Convenience variable \"$bpnum\" contains the number of the last\n\
15617 breakpoint set."),
15618 &maintenanceinfolist);
15619
15620 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15621 Set catchpoints to catch events."),
15622 &catch_cmdlist, "catch ",
15623 0/*allow-unknown*/, &cmdlist);
15624
15625 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15626 Set temporary catchpoints to catch events."),
15627 &tcatch_cmdlist, "tcatch ",
15628 0/*allow-unknown*/, &cmdlist);
15629
15630 add_catch_command ("fork", _("Catch calls to fork."),
15631 catch_fork_command_1,
15632 NULL,
15633 (void *) (uintptr_t) catch_fork_permanent,
15634 (void *) (uintptr_t) catch_fork_temporary);
15635 add_catch_command ("vfork", _("Catch calls to vfork."),
15636 catch_fork_command_1,
15637 NULL,
15638 (void *) (uintptr_t) catch_vfork_permanent,
15639 (void *) (uintptr_t) catch_vfork_temporary);
15640 add_catch_command ("exec", _("Catch calls to exec."),
15641 catch_exec_command_1,
15642 NULL,
15643 CATCH_PERMANENT,
15644 CATCH_TEMPORARY);
15645 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15646 Usage: catch load [REGEX]\n\
15647 If REGEX is given, only stop for libraries matching the regular expression."),
15648 catch_load_command_1,
15649 NULL,
15650 CATCH_PERMANENT,
15651 CATCH_TEMPORARY);
15652 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15653 Usage: catch unload [REGEX]\n\
15654 If REGEX is given, only stop for libraries matching the regular expression."),
15655 catch_unload_command_1,
15656 NULL,
15657 CATCH_PERMANENT,
15658 CATCH_TEMPORARY);
15659
15660 c = add_com ("watch", class_breakpoint, watch_command, _("\
15661 Set a watchpoint for an expression.\n\
15662 Usage: watch [-l|-location] EXPRESSION\n\
15663 A watchpoint stops execution of your program whenever the value of\n\
15664 an expression changes.\n\
15665 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15666 the memory to which it refers."));
15667 set_cmd_completer (c, expression_completer);
15668
15669 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15670 Set a read watchpoint for an expression.\n\
15671 Usage: rwatch [-l|-location] EXPRESSION\n\
15672 A watchpoint stops execution of your program whenever the value of\n\
15673 an expression is read.\n\
15674 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15675 the memory to which it refers."));
15676 set_cmd_completer (c, expression_completer);
15677
15678 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15679 Set a watchpoint for an expression.\n\
15680 Usage: awatch [-l|-location] EXPRESSION\n\
15681 A watchpoint stops execution of your program whenever the value of\n\
15682 an expression is either read or written.\n\
15683 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15684 the memory to which it refers."));
15685 set_cmd_completer (c, expression_completer);
15686
15687 add_info ("watchpoints", info_watchpoints_command, _("\
15688 Status of specified watchpoints (all watchpoints if no argument)."));
15689
15690 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15691 respond to changes - contrary to the description. */
15692 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15693 &can_use_hw_watchpoints, _("\
15694 Set debugger's willingness to use watchpoint hardware."), _("\
15695 Show debugger's willingness to use watchpoint hardware."), _("\
15696 If zero, gdb will not use hardware for new watchpoints, even if\n\
15697 such is available. (However, any hardware watchpoints that were\n\
15698 created before setting this to nonzero, will continue to use watchpoint\n\
15699 hardware.)"),
15700 NULL,
15701 show_can_use_hw_watchpoints,
15702 &setlist, &showlist);
15703
15704 can_use_hw_watchpoints = 1;
15705
15706 /* Tracepoint manipulation commands. */
15707
15708 c = add_com ("trace", class_breakpoint, trace_command, _("\
15709 Set a tracepoint at specified location.\n\
15710 \n"
15711 BREAK_ARGS_HELP ("trace") "\n\
15712 Do \"help tracepoints\" for info on other tracepoint commands."));
15713 set_cmd_completer (c, location_completer);
15714
15715 add_com_alias ("tp", "trace", class_alias, 0);
15716 add_com_alias ("tr", "trace", class_alias, 1);
15717 add_com_alias ("tra", "trace", class_alias, 1);
15718 add_com_alias ("trac", "trace", class_alias, 1);
15719
15720 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15721 Set a fast tracepoint at specified location.\n\
15722 \n"
15723 BREAK_ARGS_HELP ("ftrace") "\n\
15724 Do \"help tracepoints\" for info on other tracepoint commands."));
15725 set_cmd_completer (c, location_completer);
15726
15727 c = add_com ("strace", class_breakpoint, strace_command, _("\
15728 Set a static tracepoint at location or marker.\n\
15729 \n\
15730 strace [LOCATION] [if CONDITION]\n\
15731 LOCATION may be a linespec, explicit, or address location (described below) \n\
15732 or -m MARKER_ID.\n\n\
15733 If a marker id is specified, probe the marker with that name. With\n\
15734 no LOCATION, uses current execution address of the selected stack frame.\n\
15735 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15736 This collects arbitrary user data passed in the probe point call to the\n\
15737 tracing library. You can inspect it when analyzing the trace buffer,\n\
15738 by printing the $_sdata variable like any other convenience variable.\n\
15739 \n\
15740 CONDITION is a boolean expression.\n\
15741 \n" LOCATION_HELP_STRING "\n\n\
15742 Multiple tracepoints at one place are permitted, and useful if their\n\
15743 conditions are different.\n\
15744 \n\
15745 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15746 Do \"help tracepoints\" for info on other tracepoint commands."));
15747 set_cmd_completer (c, location_completer);
15748
15749 add_info ("tracepoints", info_tracepoints_command, _("\
15750 Status of specified tracepoints (all tracepoints if no argument).\n\
15751 Convenience variable \"$tpnum\" contains the number of the\n\
15752 last tracepoint set."));
15753
15754 add_info_alias ("tp", "tracepoints", 1);
15755
15756 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15757 Delete specified tracepoints.\n\
15758 Arguments are tracepoint numbers, separated by spaces.\n\
15759 No argument means delete all tracepoints."),
15760 &deletelist);
15761 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15762
15763 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15764 Disable specified tracepoints.\n\
15765 Arguments are tracepoint numbers, separated by spaces.\n\
15766 No argument means disable all tracepoints."),
15767 &disablelist);
15768 deprecate_cmd (c, "disable");
15769
15770 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15771 Enable specified tracepoints.\n\
15772 Arguments are tracepoint numbers, separated by spaces.\n\
15773 No argument means enable all tracepoints."),
15774 &enablelist);
15775 deprecate_cmd (c, "enable");
15776
15777 add_com ("passcount", class_trace, trace_pass_command, _("\
15778 Set the passcount for a tracepoint.\n\
15779 The trace will end when the tracepoint has been passed 'count' times.\n\
15780 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15781 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15782
15783 add_prefix_cmd ("save", class_breakpoint, save_command,
15784 _("Save breakpoint definitions as a script."),
15785 &save_cmdlist, "save ",
15786 0/*allow-unknown*/, &cmdlist);
15787
15788 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15789 Save current breakpoint definitions as a script.\n\
15790 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15791 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15792 session to restore them."),
15793 &save_cmdlist);
15794 set_cmd_completer (c, filename_completer);
15795
15796 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15797 Save current tracepoint definitions as a script.\n\
15798 Use the 'source' command in another debug session to restore them."),
15799 &save_cmdlist);
15800 set_cmd_completer (c, filename_completer);
15801
15802 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15803 deprecate_cmd (c, "save tracepoints");
15804
15805 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
15806 Breakpoint specific settings.\n\
15807 Configure various breakpoint-specific variables such as\n\
15808 pending breakpoint behavior."),
15809 &breakpoint_set_cmdlist, "set breakpoint ",
15810 0/*allow-unknown*/, &setlist);
15811 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
15812 Breakpoint specific settings.\n\
15813 Configure various breakpoint-specific variables such as\n\
15814 pending breakpoint behavior."),
15815 &breakpoint_show_cmdlist, "show breakpoint ",
15816 0/*allow-unknown*/, &showlist);
15817
15818 add_setshow_auto_boolean_cmd ("pending", no_class,
15819 &pending_break_support, _("\
15820 Set debugger's behavior regarding pending breakpoints."), _("\
15821 Show debugger's behavior regarding pending breakpoints."), _("\
15822 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15823 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15824 an error. If auto, an unrecognized breakpoint location results in a\n\
15825 user-query to see if a pending breakpoint should be created."),
15826 NULL,
15827 show_pending_break_support,
15828 &breakpoint_set_cmdlist,
15829 &breakpoint_show_cmdlist);
15830
15831 pending_break_support = AUTO_BOOLEAN_AUTO;
15832
15833 add_setshow_boolean_cmd ("auto-hw", no_class,
15834 &automatic_hardware_breakpoints, _("\
15835 Set automatic usage of hardware breakpoints."), _("\
15836 Show automatic usage of hardware breakpoints."), _("\
15837 If set, the debugger will automatically use hardware breakpoints for\n\
15838 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15839 a warning will be emitted for such breakpoints."),
15840 NULL,
15841 show_automatic_hardware_breakpoints,
15842 &breakpoint_set_cmdlist,
15843 &breakpoint_show_cmdlist);
15844
15845 add_setshow_boolean_cmd ("always-inserted", class_support,
15846 &always_inserted_mode, _("\
15847 Set mode for inserting breakpoints."), _("\
15848 Show mode for inserting breakpoints."), _("\
15849 When this mode is on, breakpoints are inserted immediately as soon as\n\
15850 they're created, kept inserted even when execution stops, and removed\n\
15851 only when the user deletes them. When this mode is off (the default),\n\
15852 breakpoints are inserted only when execution continues, and removed\n\
15853 when execution stops."),
15854 NULL,
15855 &show_always_inserted_mode,
15856 &breakpoint_set_cmdlist,
15857 &breakpoint_show_cmdlist);
15858
15859 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15860 condition_evaluation_enums,
15861 &condition_evaluation_mode_1, _("\
15862 Set mode of breakpoint condition evaluation."), _("\
15863 Show mode of breakpoint condition evaluation."), _("\
15864 When this is set to \"host\", breakpoint conditions will be\n\
15865 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15866 breakpoint conditions will be downloaded to the target (if the target\n\
15867 supports such feature) and conditions will be evaluated on the target's side.\n\
15868 If this is set to \"auto\" (default), this will be automatically set to\n\
15869 \"target\" if it supports condition evaluation, otherwise it will\n\
15870 be set to \"gdb\""),
15871 &set_condition_evaluation_mode,
15872 &show_condition_evaluation_mode,
15873 &breakpoint_set_cmdlist,
15874 &breakpoint_show_cmdlist);
15875
15876 add_com ("break-range", class_breakpoint, break_range_command, _("\
15877 Set a breakpoint for an address range.\n\
15878 break-range START-LOCATION, END-LOCATION\n\
15879 where START-LOCATION and END-LOCATION can be one of the following:\n\
15880 LINENUM, for that line in the current file,\n\
15881 FILE:LINENUM, for that line in that file,\n\
15882 +OFFSET, for that number of lines after the current line\n\
15883 or the start of the range\n\
15884 FUNCTION, for the first line in that function,\n\
15885 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15886 *ADDRESS, for the instruction at that address.\n\
15887 \n\
15888 The breakpoint will stop execution of the inferior whenever it executes\n\
15889 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15890 range (including START-LOCATION and END-LOCATION)."));
15891
15892 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15893 Set a dynamic printf at specified location.\n\
15894 dprintf location,format string,arg1,arg2,...\n\
15895 location may be a linespec, explicit, or address location.\n"
15896 "\n" LOCATION_HELP_STRING));
15897 set_cmd_completer (c, location_completer);
15898
15899 add_setshow_enum_cmd ("dprintf-style", class_support,
15900 dprintf_style_enums, &dprintf_style, _("\
15901 Set the style of usage for dynamic printf."), _("\
15902 Show the style of usage for dynamic printf."), _("\
15903 This setting chooses how GDB will do a dynamic printf.\n\
15904 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15905 console, as with the \"printf\" command.\n\
15906 If the value is \"call\", the print is done by calling a function in your\n\
15907 program; by default printf(), but you can choose a different function or\n\
15908 output stream by setting dprintf-function and dprintf-channel."),
15909 update_dprintf_commands, NULL,
15910 &setlist, &showlist);
15911
15912 dprintf_function = xstrdup ("printf");
15913 add_setshow_string_cmd ("dprintf-function", class_support,
15914 &dprintf_function, _("\
15915 Set the function to use for dynamic printf."), _("\
15916 Show the function to use for dynamic printf."), NULL,
15917 update_dprintf_commands, NULL,
15918 &setlist, &showlist);
15919
15920 dprintf_channel = xstrdup ("");
15921 add_setshow_string_cmd ("dprintf-channel", class_support,
15922 &dprintf_channel, _("\
15923 Set the channel to use for dynamic printf."), _("\
15924 Show the channel to use for dynamic printf."), NULL,
15925 update_dprintf_commands, NULL,
15926 &setlist, &showlist);
15927
15928 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15929 &disconnected_dprintf, _("\
15930 Set whether dprintf continues after GDB disconnects."), _("\
15931 Show whether dprintf continues after GDB disconnects."), _("\
15932 Use this to let dprintf commands continue to hit and produce output\n\
15933 even if GDB disconnects or detaches from the target."),
15934 NULL,
15935 NULL,
15936 &setlist, &showlist);
15937
15938 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15939 Target agent only formatted printing, like the C \"printf\" function.\n\
15940 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15941 This supports most C printf format specifications, like %s, %d, etc.\n\
15942 This is useful for formatted output in user-defined commands."));
15943
15944 automatic_hardware_breakpoints = true;
15945
15946 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
15947 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
15948 }
This page took 0.333375 seconds and 5 git commands to generate.