]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/breakpoint.c
Ensure class_alias is only used for user-defined aliases.
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70
71 /* readline include files */
72 #include "readline/tilde.h"
73
74 /* readline defines this. */
75 #undef savestring
76
77 #include "mi/mi-common.h"
78 #include "extension.h"
79 #include <algorithm>
80 #include "progspace-and-thread.h"
81 #include "gdbsupport/array-view.h"
82 #include "gdbsupport/gdb_optional.h"
83
84 /* Prototypes for local functions. */
85
86 static void map_breakpoint_numbers (const char *,
87 gdb::function_view<void (breakpoint *)>);
88
89 static void breakpoint_re_set_default (struct breakpoint *);
90
91 static void
92 create_sals_from_location_default (const struct event_location *location,
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
95
96 static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
104 int, int, int, unsigned);
105
106 static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, const struct event_location *location,
108 struct program_space *search_pspace);
109
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
112
113 static void mention (struct breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
117 const struct breakpoint_ops *);
118 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
121 /* This function is used in gdbtk sources and thus can not be made
122 static. */
123 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
124 struct symtab_and_line,
125 enum bptype,
126 const struct breakpoint_ops *);
127
128 static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
133
134 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
136 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
138 enum bptype bptype);
139
140 static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
142 struct obj_section *, int);
143
144 static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
147 static int breakpoint_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 /* Tracepoints set on probes. */
250 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
251
252 /* Dynamic printf class type. */
253 struct breakpoint_ops dprintf_breakpoint_ops;
254
255 /* The style in which to perform a dynamic printf. This is a user
256 option because different output options have different tradeoffs;
257 if GDB does the printing, there is better error handling if there
258 is a problem with any of the arguments, but using an inferior
259 function lets you have special-purpose printers and sending of
260 output to the same place as compiled-in print functions. */
261
262 static const char dprintf_style_gdb[] = "gdb";
263 static const char dprintf_style_call[] = "call";
264 static const char dprintf_style_agent[] = "agent";
265 static const char *const dprintf_style_enums[] = {
266 dprintf_style_gdb,
267 dprintf_style_call,
268 dprintf_style_agent,
269 NULL
270 };
271 static const char *dprintf_style = dprintf_style_gdb;
272
273 /* The function to use for dynamic printf if the preferred style is to
274 call into the inferior. The value is simply a string that is
275 copied into the command, so it can be anything that GDB can
276 evaluate to a callable address, not necessarily a function name. */
277
278 static char *dprintf_function;
279
280 /* The channel to use for dynamic printf if the preferred style is to
281 call into the inferior; if a nonempty string, it will be passed to
282 the call as the first argument, with the format string as the
283 second. As with the dprintf function, this can be anything that
284 GDB knows how to evaluate, so in addition to common choices like
285 "stderr", this could be an app-specific expression like
286 "mystreams[curlogger]". */
287
288 static char *dprintf_channel;
289
290 /* True if dprintf commands should continue to operate even if GDB
291 has disconnected. */
292 static bool disconnected_dprintf = true;
293
294 struct command_line *
295 breakpoint_commands (struct breakpoint *b)
296 {
297 return b->commands ? b->commands.get () : NULL;
298 }
299
300 /* Flag indicating that a command has proceeded the inferior past the
301 current breakpoint. */
302
303 static bool breakpoint_proceeded;
304
305 const char *
306 bpdisp_text (enum bpdisp disp)
307 {
308 /* NOTE: the following values are a part of MI protocol and
309 represent values of 'disp' field returned when inferior stops at
310 a breakpoint. */
311 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
312
313 return bpdisps[(int) disp];
314 }
315
316 /* Prototypes for exported functions. */
317 /* If FALSE, gdb will not use hardware support for watchpoints, even
318 if such is available. */
319 static int can_use_hw_watchpoints;
320
321 static void
322 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
323 struct cmd_list_element *c,
324 const char *value)
325 {
326 fprintf_filtered (file,
327 _("Debugger's willingness to use "
328 "watchpoint hardware is %s.\n"),
329 value);
330 }
331
332 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
333 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
334 for unrecognized breakpoint locations.
335 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
336 static enum auto_boolean pending_break_support;
337 static void
338 show_pending_break_support (struct ui_file *file, int from_tty,
339 struct cmd_list_element *c,
340 const char *value)
341 {
342 fprintf_filtered (file,
343 _("Debugger's behavior regarding "
344 "pending breakpoints is %s.\n"),
345 value);
346 }
347
348 /* If true, gdb will automatically use hardware breakpoints for breakpoints
349 set with "break" but falling in read-only memory.
350 If false, gdb will warn about such breakpoints, but won't automatically
351 use hardware breakpoints. */
352 static bool automatic_hardware_breakpoints;
353 static void
354 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
355 struct cmd_list_element *c,
356 const char *value)
357 {
358 fprintf_filtered (file,
359 _("Automatic usage of hardware breakpoints is %s.\n"),
360 value);
361 }
362
363 /* If on, GDB keeps breakpoints inserted even if the inferior is
364 stopped, and immediately inserts any new breakpoints as soon as
365 they're created. If off (default), GDB keeps breakpoints off of
366 the target as long as possible. That is, it delays inserting
367 breakpoints until the next resume, and removes them again when the
368 target fully stops. This is a bit safer in case GDB crashes while
369 processing user input. */
370 static bool always_inserted_mode = false;
371
372 static void
373 show_always_inserted_mode (struct ui_file *file, int from_tty,
374 struct cmd_list_element *c, const char *value)
375 {
376 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
377 value);
378 }
379
380 /* See breakpoint.h. */
381
382 int
383 breakpoints_should_be_inserted_now (void)
384 {
385 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
386 {
387 /* If breakpoints are global, they should be inserted even if no
388 thread under gdb's control is running, or even if there are
389 no threads under GDB's control yet. */
390 return 1;
391 }
392 else
393 {
394 if (always_inserted_mode)
395 {
396 /* The user wants breakpoints inserted even if all threads
397 are stopped. */
398 return 1;
399 }
400
401 for (inferior *inf : all_inferiors ())
402 if (inf->has_execution ()
403 && threads_are_executing (inf->process_target ()))
404 return 1;
405
406 /* Don't remove breakpoints yet if, even though all threads are
407 stopped, we still have events to process. */
408 for (thread_info *tp : all_non_exited_threads ())
409 if (tp->resumed
410 && tp->suspend.waitstatus_pending_p)
411 return 1;
412 }
413 return 0;
414 }
415
416 static const char condition_evaluation_both[] = "host or target";
417
418 /* Modes for breakpoint condition evaluation. */
419 static const char condition_evaluation_auto[] = "auto";
420 static const char condition_evaluation_host[] = "host";
421 static const char condition_evaluation_target[] = "target";
422 static const char *const condition_evaluation_enums[] = {
423 condition_evaluation_auto,
424 condition_evaluation_host,
425 condition_evaluation_target,
426 NULL
427 };
428
429 /* Global that holds the current mode for breakpoint condition evaluation. */
430 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
431
432 /* Global that we use to display information to the user (gets its value from
433 condition_evaluation_mode_1. */
434 static const char *condition_evaluation_mode = condition_evaluation_auto;
435
436 /* Translate a condition evaluation mode MODE into either "host"
437 or "target". This is used mostly to translate from "auto" to the
438 real setting that is being used. It returns the translated
439 evaluation mode. */
440
441 static const char *
442 translate_condition_evaluation_mode (const char *mode)
443 {
444 if (mode == condition_evaluation_auto)
445 {
446 if (target_supports_evaluation_of_breakpoint_conditions ())
447 return condition_evaluation_target;
448 else
449 return condition_evaluation_host;
450 }
451 else
452 return mode;
453 }
454
455 /* Discovers what condition_evaluation_auto translates to. */
456
457 static const char *
458 breakpoint_condition_evaluation_mode (void)
459 {
460 return translate_condition_evaluation_mode (condition_evaluation_mode);
461 }
462
463 /* Return true if GDB should evaluate breakpoint conditions or false
464 otherwise. */
465
466 static int
467 gdb_evaluates_breakpoint_condition_p (void)
468 {
469 const char *mode = breakpoint_condition_evaluation_mode ();
470
471 return (mode == condition_evaluation_host);
472 }
473
474 /* Are we executing breakpoint commands? */
475 static int executing_breakpoint_commands;
476
477 /* Are overlay event breakpoints enabled? */
478 static int overlay_events_enabled;
479
480 /* See description in breakpoint.h. */
481 bool target_exact_watchpoints = false;
482
483 /* Walk the following statement or block through all breakpoints.
484 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
485 current breakpoint. */
486
487 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
488
489 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
490 for (B = breakpoint_chain; \
491 B ? (TMP=B->next, 1): 0; \
492 B = TMP)
493
494 /* Similar iterator for the low-level breakpoints. SAFE variant is
495 not provided so update_global_location_list must not be called
496 while executing the block of ALL_BP_LOCATIONS. */
497
498 #define ALL_BP_LOCATIONS(B,BP_TMP) \
499 for (BP_TMP = bp_locations; \
500 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
501 BP_TMP++)
502
503 /* Iterates through locations with address ADDRESS for the currently selected
504 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
505 to where the loop should start from.
506 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
507 appropriate location to start with. */
508
509 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
510 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
511 BP_LOCP_TMP = BP_LOCP_START; \
512 BP_LOCP_START \
513 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
514 && (*BP_LOCP_TMP)->address == ADDRESS); \
515 BP_LOCP_TMP++)
516
517 /* Iterator for tracepoints only. */
518
519 #define ALL_TRACEPOINTS(B) \
520 for (B = breakpoint_chain; B; B = B->next) \
521 if (is_tracepoint (B))
522
523 /* Chains of all breakpoints defined. */
524
525 static struct breakpoint *breakpoint_chain;
526
527 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
528
529 static struct bp_location **bp_locations;
530
531 /* Number of elements of BP_LOCATIONS. */
532
533 static unsigned bp_locations_count;
534
535 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
536 ADDRESS for the current elements of BP_LOCATIONS which get a valid
537 result from bp_location_has_shadow. You can use it for roughly
538 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
539 an address you need to read. */
540
541 static CORE_ADDR bp_locations_placed_address_before_address_max;
542
543 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
544 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
545 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
546 You can use it for roughly limiting the subrange of BP_LOCATIONS to
547 scan for shadow bytes for an address you need to read. */
548
549 static CORE_ADDR bp_locations_shadow_len_after_address_max;
550
551 /* The locations that no longer correspond to any breakpoint, unlinked
552 from the bp_locations array, but for which a hit may still be
553 reported by a target. */
554 static std::vector<bp_location *> moribund_locations;
555
556 /* Number of last breakpoint made. */
557
558 static int breakpoint_count;
559
560 /* The value of `breakpoint_count' before the last command that
561 created breakpoints. If the last (break-like) command created more
562 than one breakpoint, then the difference between BREAKPOINT_COUNT
563 and PREV_BREAKPOINT_COUNT is more than one. */
564 static int prev_breakpoint_count;
565
566 /* Number of last tracepoint made. */
567
568 static int tracepoint_count;
569
570 static struct cmd_list_element *breakpoint_set_cmdlist;
571 static struct cmd_list_element *breakpoint_show_cmdlist;
572 struct cmd_list_element *save_cmdlist;
573
574 /* See declaration at breakpoint.h. */
575
576 struct breakpoint *
577 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
578 void *user_data)
579 {
580 struct breakpoint *b = NULL;
581
582 ALL_BREAKPOINTS (b)
583 {
584 if (func (b, user_data) != 0)
585 break;
586 }
587
588 return b;
589 }
590
591 /* Return whether a breakpoint is an active enabled breakpoint. */
592 static int
593 breakpoint_enabled (struct breakpoint *b)
594 {
595 return (b->enable_state == bp_enabled);
596 }
597
598 /* Set breakpoint count to NUM. */
599
600 static void
601 set_breakpoint_count (int num)
602 {
603 prev_breakpoint_count = breakpoint_count;
604 breakpoint_count = num;
605 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
606 }
607
608 /* Used by `start_rbreak_breakpoints' below, to record the current
609 breakpoint count before "rbreak" creates any breakpoint. */
610 static int rbreak_start_breakpoint_count;
611
612 /* Called at the start an "rbreak" command to record the first
613 breakpoint made. */
614
615 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
616 {
617 rbreak_start_breakpoint_count = breakpoint_count;
618 }
619
620 /* Called at the end of an "rbreak" command to record the last
621 breakpoint made. */
622
623 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
624 {
625 prev_breakpoint_count = rbreak_start_breakpoint_count;
626 }
627
628 /* Used in run_command to zero the hit count when a new run starts. */
629
630 void
631 clear_breakpoint_hit_counts (void)
632 {
633 struct breakpoint *b;
634
635 ALL_BREAKPOINTS (b)
636 b->hit_count = 0;
637 }
638
639 \f
640 /* Return the breakpoint with the specified number, or NULL
641 if the number does not refer to an existing breakpoint. */
642
643 struct breakpoint *
644 get_breakpoint (int num)
645 {
646 struct breakpoint *b;
647
648 ALL_BREAKPOINTS (b)
649 if (b->number == num)
650 return b;
651
652 return NULL;
653 }
654
655 \f
656
657 /* Mark locations as "conditions have changed" in case the target supports
658 evaluating conditions on its side. */
659
660 static void
661 mark_breakpoint_modified (struct breakpoint *b)
662 {
663 struct bp_location *loc;
664
665 /* This is only meaningful if the target is
666 evaluating conditions and if the user has
667 opted for condition evaluation on the target's
668 side. */
669 if (gdb_evaluates_breakpoint_condition_p ()
670 || !target_supports_evaluation_of_breakpoint_conditions ())
671 return;
672
673 if (!is_breakpoint (b))
674 return;
675
676 for (loc = b->loc; loc; loc = loc->next)
677 loc->condition_changed = condition_modified;
678 }
679
680 /* Mark location as "conditions have changed" in case the target supports
681 evaluating conditions on its side. */
682
683 static void
684 mark_breakpoint_location_modified (struct bp_location *loc)
685 {
686 /* This is only meaningful if the target is
687 evaluating conditions and if the user has
688 opted for condition evaluation on the target's
689 side. */
690 if (gdb_evaluates_breakpoint_condition_p ()
691 || !target_supports_evaluation_of_breakpoint_conditions ())
692
693 return;
694
695 if (!is_breakpoint (loc->owner))
696 return;
697
698 loc->condition_changed = condition_modified;
699 }
700
701 /* Sets the condition-evaluation mode using the static global
702 condition_evaluation_mode. */
703
704 static void
705 set_condition_evaluation_mode (const char *args, int from_tty,
706 struct cmd_list_element *c)
707 {
708 const char *old_mode, *new_mode;
709
710 if ((condition_evaluation_mode_1 == condition_evaluation_target)
711 && !target_supports_evaluation_of_breakpoint_conditions ())
712 {
713 condition_evaluation_mode_1 = condition_evaluation_mode;
714 warning (_("Target does not support breakpoint condition evaluation.\n"
715 "Using host evaluation mode instead."));
716 return;
717 }
718
719 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
720 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
721
722 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
723 settings was "auto". */
724 condition_evaluation_mode = condition_evaluation_mode_1;
725
726 /* Only update the mode if the user picked a different one. */
727 if (new_mode != old_mode)
728 {
729 struct bp_location *loc, **loc_tmp;
730 /* If the user switched to a different evaluation mode, we
731 need to synch the changes with the target as follows:
732
733 "host" -> "target": Send all (valid) conditions to the target.
734 "target" -> "host": Remove all the conditions from the target.
735 */
736
737 if (new_mode == condition_evaluation_target)
738 {
739 /* Mark everything modified and synch conditions with the
740 target. */
741 ALL_BP_LOCATIONS (loc, loc_tmp)
742 mark_breakpoint_location_modified (loc);
743 }
744 else
745 {
746 /* Manually mark non-duplicate locations to synch conditions
747 with the target. We do this to remove all the conditions the
748 target knows about. */
749 ALL_BP_LOCATIONS (loc, loc_tmp)
750 if (is_breakpoint (loc->owner) && loc->inserted)
751 loc->needs_update = 1;
752 }
753
754 /* Do the update. */
755 update_global_location_list (UGLL_MAY_INSERT);
756 }
757
758 return;
759 }
760
761 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
762 what "auto" is translating to. */
763
764 static void
765 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
766 struct cmd_list_element *c, const char *value)
767 {
768 if (condition_evaluation_mode == condition_evaluation_auto)
769 fprintf_filtered (file,
770 _("Breakpoint condition evaluation "
771 "mode is %s (currently %s).\n"),
772 value,
773 breakpoint_condition_evaluation_mode ());
774 else
775 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
776 value);
777 }
778
779 /* A comparison function for bp_location AP and BP that is used by
780 bsearch. This comparison function only cares about addresses, unlike
781 the more general bp_location_is_less_than function. */
782
783 static int
784 bp_locations_compare_addrs (const void *ap, const void *bp)
785 {
786 const struct bp_location *a = *(const struct bp_location **) ap;
787 const struct bp_location *b = *(const struct bp_location **) bp;
788
789 if (a->address == b->address)
790 return 0;
791 else
792 return ((a->address > b->address) - (a->address < b->address));
793 }
794
795 /* Helper function to skip all bp_locations with addresses
796 less than ADDRESS. It returns the first bp_location that
797 is greater than or equal to ADDRESS. If none is found, just
798 return NULL. */
799
800 static struct bp_location **
801 get_first_locp_gte_addr (CORE_ADDR address)
802 {
803 struct bp_location dummy_loc;
804 struct bp_location *dummy_locp = &dummy_loc;
805 struct bp_location **locp_found = NULL;
806
807 /* Initialize the dummy location's address field. */
808 dummy_loc.address = address;
809
810 /* Find a close match to the first location at ADDRESS. */
811 locp_found = ((struct bp_location **)
812 bsearch (&dummy_locp, bp_locations, bp_locations_count,
813 sizeof (struct bp_location **),
814 bp_locations_compare_addrs));
815
816 /* Nothing was found, nothing left to do. */
817 if (locp_found == NULL)
818 return NULL;
819
820 /* We may have found a location that is at ADDRESS but is not the first in the
821 location's list. Go backwards (if possible) and locate the first one. */
822 while ((locp_found - 1) >= bp_locations
823 && (*(locp_found - 1))->address == address)
824 locp_found--;
825
826 return locp_found;
827 }
828
829 void
830 set_breakpoint_condition (struct breakpoint *b, const char *exp,
831 int from_tty)
832 {
833 xfree (b->cond_string);
834 b->cond_string = NULL;
835
836 if (is_watchpoint (b))
837 {
838 struct watchpoint *w = (struct watchpoint *) b;
839
840 w->cond_exp.reset ();
841 }
842 else
843 {
844 struct bp_location *loc;
845
846 for (loc = b->loc; loc; loc = loc->next)
847 {
848 loc->cond.reset ();
849
850 /* No need to free the condition agent expression
851 bytecode (if we have one). We will handle this
852 when we go through update_global_location_list. */
853 }
854 }
855
856 if (*exp == 0)
857 {
858 if (from_tty)
859 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
860 }
861 else
862 {
863 const char *arg = exp;
864
865 /* I don't know if it matters whether this is the string the user
866 typed in or the decompiled expression. */
867 b->cond_string = xstrdup (arg);
868 b->condition_not_parsed = 0;
869
870 if (is_watchpoint (b))
871 {
872 struct watchpoint *w = (struct watchpoint *) b;
873
874 innermost_block_tracker tracker;
875 arg = exp;
876 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
877 if (*arg)
878 error (_("Junk at end of expression"));
879 w->cond_exp_valid_block = tracker.block ();
880 }
881 else
882 {
883 struct bp_location *loc;
884
885 for (loc = b->loc; loc; loc = loc->next)
886 {
887 arg = exp;
888 loc->cond =
889 parse_exp_1 (&arg, loc->address,
890 block_for_pc (loc->address), 0);
891 if (*arg)
892 error (_("Junk at end of expression"));
893 }
894 }
895 }
896 mark_breakpoint_modified (b);
897
898 gdb::observers::breakpoint_modified.notify (b);
899 }
900
901 /* Completion for the "condition" command. */
902
903 static void
904 condition_completer (struct cmd_list_element *cmd,
905 completion_tracker &tracker,
906 const char *text, const char *word)
907 {
908 const char *space;
909
910 text = skip_spaces (text);
911 space = skip_to_space (text);
912 if (*space == '\0')
913 {
914 int len;
915 struct breakpoint *b;
916
917 if (text[0] == '$')
918 {
919 /* We don't support completion of history indices. */
920 if (!isdigit (text[1]))
921 complete_internalvar (tracker, &text[1]);
922 return;
923 }
924
925 /* We're completing the breakpoint number. */
926 len = strlen (text);
927
928 ALL_BREAKPOINTS (b)
929 {
930 char number[50];
931
932 xsnprintf (number, sizeof (number), "%d", b->number);
933
934 if (strncmp (number, text, len) == 0)
935 tracker.add_completion (make_unique_xstrdup (number));
936 }
937
938 return;
939 }
940
941 /* We're completing the expression part. */
942 text = skip_spaces (space);
943 expression_completer (cmd, tracker, text, word);
944 }
945
946 /* condition N EXP -- set break condition of breakpoint N to EXP. */
947
948 static void
949 condition_command (const char *arg, int from_tty)
950 {
951 struct breakpoint *b;
952 const char *p;
953 int bnum;
954
955 if (arg == 0)
956 error_no_arg (_("breakpoint number"));
957
958 p = arg;
959 bnum = get_number (&p);
960 if (bnum == 0)
961 error (_("Bad breakpoint argument: '%s'"), arg);
962
963 ALL_BREAKPOINTS (b)
964 if (b->number == bnum)
965 {
966 /* Check if this breakpoint has a "stop" method implemented in an
967 extension language. This method and conditions entered into GDB
968 from the CLI are mutually exclusive. */
969 const struct extension_language_defn *extlang
970 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
971
972 if (extlang != NULL)
973 {
974 error (_("Only one stop condition allowed. There is currently"
975 " a %s stop condition defined for this breakpoint."),
976 ext_lang_capitalized_name (extlang));
977 }
978 set_breakpoint_condition (b, p, from_tty);
979
980 if (is_breakpoint (b))
981 update_global_location_list (UGLL_MAY_INSERT);
982
983 return;
984 }
985
986 error (_("No breakpoint number %d."), bnum);
987 }
988
989 /* Check that COMMAND do not contain commands that are suitable
990 only for tracepoints and not suitable for ordinary breakpoints.
991 Throw if any such commands is found. */
992
993 static void
994 check_no_tracepoint_commands (struct command_line *commands)
995 {
996 struct command_line *c;
997
998 for (c = commands; c; c = c->next)
999 {
1000 if (c->control_type == while_stepping_control)
1001 error (_("The 'while-stepping' command can "
1002 "only be used for tracepoints"));
1003
1004 check_no_tracepoint_commands (c->body_list_0.get ());
1005 check_no_tracepoint_commands (c->body_list_1.get ());
1006
1007 /* Not that command parsing removes leading whitespace and comment
1008 lines and also empty lines. So, we only need to check for
1009 command directly. */
1010 if (strstr (c->line, "collect ") == c->line)
1011 error (_("The 'collect' command can only be used for tracepoints"));
1012
1013 if (strstr (c->line, "teval ") == c->line)
1014 error (_("The 'teval' command can only be used for tracepoints"));
1015 }
1016 }
1017
1018 struct longjmp_breakpoint : public breakpoint
1019 {
1020 ~longjmp_breakpoint () override;
1021 };
1022
1023 /* Encapsulate tests for different types of tracepoints. */
1024
1025 static bool
1026 is_tracepoint_type (bptype type)
1027 {
1028 return (type == bp_tracepoint
1029 || type == bp_fast_tracepoint
1030 || type == bp_static_tracepoint);
1031 }
1032
1033 static bool
1034 is_longjmp_type (bptype type)
1035 {
1036 return type == bp_longjmp || type == bp_exception;
1037 }
1038
1039 /* See breakpoint.h. */
1040
1041 bool
1042 is_tracepoint (const struct breakpoint *b)
1043 {
1044 return is_tracepoint_type (b->type);
1045 }
1046
1047 /* Factory function to create an appropriate instance of breakpoint given
1048 TYPE. */
1049
1050 static std::unique_ptr<breakpoint>
1051 new_breakpoint_from_type (bptype type)
1052 {
1053 breakpoint *b;
1054
1055 if (is_tracepoint_type (type))
1056 b = new tracepoint ();
1057 else if (is_longjmp_type (type))
1058 b = new longjmp_breakpoint ();
1059 else
1060 b = new breakpoint ();
1061
1062 return std::unique_ptr<breakpoint> (b);
1063 }
1064
1065 /* A helper function that validates that COMMANDS are valid for a
1066 breakpoint. This function will throw an exception if a problem is
1067 found. */
1068
1069 static void
1070 validate_commands_for_breakpoint (struct breakpoint *b,
1071 struct command_line *commands)
1072 {
1073 if (is_tracepoint (b))
1074 {
1075 struct tracepoint *t = (struct tracepoint *) b;
1076 struct command_line *c;
1077 struct command_line *while_stepping = 0;
1078
1079 /* Reset the while-stepping step count. The previous commands
1080 might have included a while-stepping action, while the new
1081 ones might not. */
1082 t->step_count = 0;
1083
1084 /* We need to verify that each top-level element of commands is
1085 valid for tracepoints, that there's at most one
1086 while-stepping element, and that the while-stepping's body
1087 has valid tracing commands excluding nested while-stepping.
1088 We also need to validate the tracepoint action line in the
1089 context of the tracepoint --- validate_actionline actually
1090 has side effects, like setting the tracepoint's
1091 while-stepping STEP_COUNT, in addition to checking if the
1092 collect/teval actions parse and make sense in the
1093 tracepoint's context. */
1094 for (c = commands; c; c = c->next)
1095 {
1096 if (c->control_type == while_stepping_control)
1097 {
1098 if (b->type == bp_fast_tracepoint)
1099 error (_("The 'while-stepping' command "
1100 "cannot be used for fast tracepoint"));
1101 else if (b->type == bp_static_tracepoint)
1102 error (_("The 'while-stepping' command "
1103 "cannot be used for static tracepoint"));
1104
1105 if (while_stepping)
1106 error (_("The 'while-stepping' command "
1107 "can be used only once"));
1108 else
1109 while_stepping = c;
1110 }
1111
1112 validate_actionline (c->line, b);
1113 }
1114 if (while_stepping)
1115 {
1116 struct command_line *c2;
1117
1118 gdb_assert (while_stepping->body_list_1 == nullptr);
1119 c2 = while_stepping->body_list_0.get ();
1120 for (; c2; c2 = c2->next)
1121 {
1122 if (c2->control_type == while_stepping_control)
1123 error (_("The 'while-stepping' command cannot be nested"));
1124 }
1125 }
1126 }
1127 else
1128 {
1129 check_no_tracepoint_commands (commands);
1130 }
1131 }
1132
1133 /* Return a vector of all the static tracepoints set at ADDR. The
1134 caller is responsible for releasing the vector. */
1135
1136 std::vector<breakpoint *>
1137 static_tracepoints_here (CORE_ADDR addr)
1138 {
1139 struct breakpoint *b;
1140 std::vector<breakpoint *> found;
1141 struct bp_location *loc;
1142
1143 ALL_BREAKPOINTS (b)
1144 if (b->type == bp_static_tracepoint)
1145 {
1146 for (loc = b->loc; loc; loc = loc->next)
1147 if (loc->address == addr)
1148 found.push_back (b);
1149 }
1150
1151 return found;
1152 }
1153
1154 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1155 validate that only allowed commands are included. */
1156
1157 void
1158 breakpoint_set_commands (struct breakpoint *b,
1159 counted_command_line &&commands)
1160 {
1161 validate_commands_for_breakpoint (b, commands.get ());
1162
1163 b->commands = std::move (commands);
1164 gdb::observers::breakpoint_modified.notify (b);
1165 }
1166
1167 /* Set the internal `silent' flag on the breakpoint. Note that this
1168 is not the same as the "silent" that may appear in the breakpoint's
1169 commands. */
1170
1171 void
1172 breakpoint_set_silent (struct breakpoint *b, int silent)
1173 {
1174 int old_silent = b->silent;
1175
1176 b->silent = silent;
1177 if (old_silent != silent)
1178 gdb::observers::breakpoint_modified.notify (b);
1179 }
1180
1181 /* Set the thread for this breakpoint. If THREAD is -1, make the
1182 breakpoint work for any thread. */
1183
1184 void
1185 breakpoint_set_thread (struct breakpoint *b, int thread)
1186 {
1187 int old_thread = b->thread;
1188
1189 b->thread = thread;
1190 if (old_thread != thread)
1191 gdb::observers::breakpoint_modified.notify (b);
1192 }
1193
1194 /* Set the task for this breakpoint. If TASK is 0, make the
1195 breakpoint work for any task. */
1196
1197 void
1198 breakpoint_set_task (struct breakpoint *b, int task)
1199 {
1200 int old_task = b->task;
1201
1202 b->task = task;
1203 if (old_task != task)
1204 gdb::observers::breakpoint_modified.notify (b);
1205 }
1206
1207 static void
1208 commands_command_1 (const char *arg, int from_tty,
1209 struct command_line *control)
1210 {
1211 counted_command_line cmd;
1212 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1213 NULL after the call to read_command_lines if the user provides an empty
1214 list of command by just typing "end". */
1215 bool cmd_read = false;
1216
1217 std::string new_arg;
1218
1219 if (arg == NULL || !*arg)
1220 {
1221 if (breakpoint_count - prev_breakpoint_count > 1)
1222 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1223 breakpoint_count);
1224 else if (breakpoint_count > 0)
1225 new_arg = string_printf ("%d", breakpoint_count);
1226 arg = new_arg.c_str ();
1227 }
1228
1229 map_breakpoint_numbers
1230 (arg, [&] (breakpoint *b)
1231 {
1232 if (!cmd_read)
1233 {
1234 gdb_assert (cmd == NULL);
1235 if (control != NULL)
1236 cmd = control->body_list_0;
1237 else
1238 {
1239 std::string str
1240 = string_printf (_("Type commands for breakpoint(s) "
1241 "%s, one per line."),
1242 arg);
1243
1244 auto do_validate = [=] (const char *line)
1245 {
1246 validate_actionline (line, b);
1247 };
1248 gdb::function_view<void (const char *)> validator;
1249 if (is_tracepoint (b))
1250 validator = do_validate;
1251
1252 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1253 }
1254 cmd_read = true;
1255 }
1256
1257 /* If a breakpoint was on the list more than once, we don't need to
1258 do anything. */
1259 if (b->commands != cmd)
1260 {
1261 validate_commands_for_breakpoint (b, cmd.get ());
1262 b->commands = cmd;
1263 gdb::observers::breakpoint_modified.notify (b);
1264 }
1265 });
1266 }
1267
1268 static void
1269 commands_command (const char *arg, int from_tty)
1270 {
1271 commands_command_1 (arg, from_tty, NULL);
1272 }
1273
1274 /* Like commands_command, but instead of reading the commands from
1275 input stream, takes them from an already parsed command structure.
1276
1277 This is used by cli-script.c to DTRT with breakpoint commands
1278 that are part of if and while bodies. */
1279 enum command_control_type
1280 commands_from_control_command (const char *arg, struct command_line *cmd)
1281 {
1282 commands_command_1 (arg, 0, cmd);
1283 return simple_control;
1284 }
1285
1286 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1287
1288 static int
1289 bp_location_has_shadow (struct bp_location *bl)
1290 {
1291 if (bl->loc_type != bp_loc_software_breakpoint)
1292 return 0;
1293 if (!bl->inserted)
1294 return 0;
1295 if (bl->target_info.shadow_len == 0)
1296 /* BL isn't valid, or doesn't shadow memory. */
1297 return 0;
1298 return 1;
1299 }
1300
1301 /* Update BUF, which is LEN bytes read from the target address
1302 MEMADDR, by replacing a memory breakpoint with its shadowed
1303 contents.
1304
1305 If READBUF is not NULL, this buffer must not overlap with the of
1306 the breakpoint location's shadow_contents buffer. Otherwise, a
1307 failed assertion internal error will be raised. */
1308
1309 static void
1310 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1311 const gdb_byte *writebuf_org,
1312 ULONGEST memaddr, LONGEST len,
1313 struct bp_target_info *target_info,
1314 struct gdbarch *gdbarch)
1315 {
1316 /* Now do full processing of the found relevant range of elements. */
1317 CORE_ADDR bp_addr = 0;
1318 int bp_size = 0;
1319 int bptoffset = 0;
1320
1321 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1322 current_program_space->aspace, 0))
1323 {
1324 /* The breakpoint is inserted in a different address space. */
1325 return;
1326 }
1327
1328 /* Addresses and length of the part of the breakpoint that
1329 we need to copy. */
1330 bp_addr = target_info->placed_address;
1331 bp_size = target_info->shadow_len;
1332
1333 if (bp_addr + bp_size <= memaddr)
1334 {
1335 /* The breakpoint is entirely before the chunk of memory we are
1336 reading. */
1337 return;
1338 }
1339
1340 if (bp_addr >= memaddr + len)
1341 {
1342 /* The breakpoint is entirely after the chunk of memory we are
1343 reading. */
1344 return;
1345 }
1346
1347 /* Offset within shadow_contents. */
1348 if (bp_addr < memaddr)
1349 {
1350 /* Only copy the second part of the breakpoint. */
1351 bp_size -= memaddr - bp_addr;
1352 bptoffset = memaddr - bp_addr;
1353 bp_addr = memaddr;
1354 }
1355
1356 if (bp_addr + bp_size > memaddr + len)
1357 {
1358 /* Only copy the first part of the breakpoint. */
1359 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1360 }
1361
1362 if (readbuf != NULL)
1363 {
1364 /* Verify that the readbuf buffer does not overlap with the
1365 shadow_contents buffer. */
1366 gdb_assert (target_info->shadow_contents >= readbuf + len
1367 || readbuf >= (target_info->shadow_contents
1368 + target_info->shadow_len));
1369
1370 /* Update the read buffer with this inserted breakpoint's
1371 shadow. */
1372 memcpy (readbuf + bp_addr - memaddr,
1373 target_info->shadow_contents + bptoffset, bp_size);
1374 }
1375 else
1376 {
1377 const unsigned char *bp;
1378 CORE_ADDR addr = target_info->reqstd_address;
1379 int placed_size;
1380
1381 /* Update the shadow with what we want to write to memory. */
1382 memcpy (target_info->shadow_contents + bptoffset,
1383 writebuf_org + bp_addr - memaddr, bp_size);
1384
1385 /* Determine appropriate breakpoint contents and size for this
1386 address. */
1387 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1388
1389 /* Update the final write buffer with this inserted
1390 breakpoint's INSN. */
1391 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1392 }
1393 }
1394
1395 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1396 by replacing any memory breakpoints with their shadowed contents.
1397
1398 If READBUF is not NULL, this buffer must not overlap with any of
1399 the breakpoint location's shadow_contents buffers. Otherwise,
1400 a failed assertion internal error will be raised.
1401
1402 The range of shadowed area by each bp_location is:
1403 bl->address - bp_locations_placed_address_before_address_max
1404 up to bl->address + bp_locations_shadow_len_after_address_max
1405 The range we were requested to resolve shadows for is:
1406 memaddr ... memaddr + len
1407 Thus the safe cutoff boundaries for performance optimization are
1408 memaddr + len <= (bl->address
1409 - bp_locations_placed_address_before_address_max)
1410 and:
1411 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1412
1413 void
1414 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1415 const gdb_byte *writebuf_org,
1416 ULONGEST memaddr, LONGEST len)
1417 {
1418 /* Left boundary, right boundary and median element of our binary
1419 search. */
1420 unsigned bc_l, bc_r, bc;
1421
1422 /* Find BC_L which is a leftmost element which may affect BUF
1423 content. It is safe to report lower value but a failure to
1424 report higher one. */
1425
1426 bc_l = 0;
1427 bc_r = bp_locations_count;
1428 while (bc_l + 1 < bc_r)
1429 {
1430 struct bp_location *bl;
1431
1432 bc = (bc_l + bc_r) / 2;
1433 bl = bp_locations[bc];
1434
1435 /* Check first BL->ADDRESS will not overflow due to the added
1436 constant. Then advance the left boundary only if we are sure
1437 the BC element can in no way affect the BUF content (MEMADDR
1438 to MEMADDR + LEN range).
1439
1440 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1441 offset so that we cannot miss a breakpoint with its shadow
1442 range tail still reaching MEMADDR. */
1443
1444 if ((bl->address + bp_locations_shadow_len_after_address_max
1445 >= bl->address)
1446 && (bl->address + bp_locations_shadow_len_after_address_max
1447 <= memaddr))
1448 bc_l = bc;
1449 else
1450 bc_r = bc;
1451 }
1452
1453 /* Due to the binary search above, we need to make sure we pick the
1454 first location that's at BC_L's address. E.g., if there are
1455 multiple locations at the same address, BC_L may end up pointing
1456 at a duplicate location, and miss the "master"/"inserted"
1457 location. Say, given locations L1, L2 and L3 at addresses A and
1458 B:
1459
1460 L1@A, L2@A, L3@B, ...
1461
1462 BC_L could end up pointing at location L2, while the "master"
1463 location could be L1. Since the `loc->inserted' flag is only set
1464 on "master" locations, we'd forget to restore the shadow of L1
1465 and L2. */
1466 while (bc_l > 0
1467 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1468 bc_l--;
1469
1470 /* Now do full processing of the found relevant range of elements. */
1471
1472 for (bc = bc_l; bc < bp_locations_count; bc++)
1473 {
1474 struct bp_location *bl = bp_locations[bc];
1475
1476 /* bp_location array has BL->OWNER always non-NULL. */
1477 if (bl->owner->type == bp_none)
1478 warning (_("reading through apparently deleted breakpoint #%d?"),
1479 bl->owner->number);
1480
1481 /* Performance optimization: any further element can no longer affect BUF
1482 content. */
1483
1484 if (bl->address >= bp_locations_placed_address_before_address_max
1485 && memaddr + len <= (bl->address
1486 - bp_locations_placed_address_before_address_max))
1487 break;
1488
1489 if (!bp_location_has_shadow (bl))
1490 continue;
1491
1492 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1493 memaddr, len, &bl->target_info, bl->gdbarch);
1494 }
1495 }
1496
1497 /* See breakpoint.h. */
1498
1499 bool
1500 is_breakpoint (const struct breakpoint *bpt)
1501 {
1502 return (bpt->type == bp_breakpoint
1503 || bpt->type == bp_hardware_breakpoint
1504 || bpt->type == bp_dprintf);
1505 }
1506
1507 /* Return true if BPT is of any hardware watchpoint kind. */
1508
1509 static bool
1510 is_hardware_watchpoint (const struct breakpoint *bpt)
1511 {
1512 return (bpt->type == bp_hardware_watchpoint
1513 || bpt->type == bp_read_watchpoint
1514 || bpt->type == bp_access_watchpoint);
1515 }
1516
1517 /* See breakpoint.h. */
1518
1519 bool
1520 is_watchpoint (const struct breakpoint *bpt)
1521 {
1522 return (is_hardware_watchpoint (bpt)
1523 || bpt->type == bp_watchpoint);
1524 }
1525
1526 /* Returns true if the current thread and its running state are safe
1527 to evaluate or update watchpoint B. Watchpoints on local
1528 expressions need to be evaluated in the context of the thread that
1529 was current when the watchpoint was created, and, that thread needs
1530 to be stopped to be able to select the correct frame context.
1531 Watchpoints on global expressions can be evaluated on any thread,
1532 and in any state. It is presently left to the target allowing
1533 memory accesses when threads are running. */
1534
1535 static int
1536 watchpoint_in_thread_scope (struct watchpoint *b)
1537 {
1538 return (b->pspace == current_program_space
1539 && (b->watchpoint_thread == null_ptid
1540 || (inferior_ptid == b->watchpoint_thread
1541 && !inferior_thread ()->executing)));
1542 }
1543
1544 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1545 associated bp_watchpoint_scope breakpoint. */
1546
1547 static void
1548 watchpoint_del_at_next_stop (struct watchpoint *w)
1549 {
1550 if (w->related_breakpoint != w)
1551 {
1552 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1553 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1554 w->related_breakpoint->disposition = disp_del_at_next_stop;
1555 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1556 w->related_breakpoint = w;
1557 }
1558 w->disposition = disp_del_at_next_stop;
1559 }
1560
1561 /* Extract a bitfield value from value VAL using the bit parameters contained in
1562 watchpoint W. */
1563
1564 static struct value *
1565 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1566 {
1567 struct value *bit_val;
1568
1569 if (val == NULL)
1570 return NULL;
1571
1572 bit_val = allocate_value (value_type (val));
1573
1574 unpack_value_bitfield (bit_val,
1575 w->val_bitpos,
1576 w->val_bitsize,
1577 value_contents_for_printing (val),
1578 value_offset (val),
1579 val);
1580
1581 return bit_val;
1582 }
1583
1584 /* Allocate a dummy location and add it to B, which must be a software
1585 watchpoint. This is required because even if a software watchpoint
1586 is not watching any memory, bpstat_stop_status requires a location
1587 to be able to report stops. */
1588
1589 static void
1590 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1591 struct program_space *pspace)
1592 {
1593 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1594
1595 b->loc = allocate_bp_location (b);
1596 b->loc->pspace = pspace;
1597 b->loc->address = -1;
1598 b->loc->length = -1;
1599 }
1600
1601 /* Returns true if B is a software watchpoint that is not watching any
1602 memory (e.g., "watch $pc"). */
1603
1604 static bool
1605 is_no_memory_software_watchpoint (struct breakpoint *b)
1606 {
1607 return (b->type == bp_watchpoint
1608 && b->loc != NULL
1609 && b->loc->next == NULL
1610 && b->loc->address == -1
1611 && b->loc->length == -1);
1612 }
1613
1614 /* Assuming that B is a watchpoint:
1615 - Reparse watchpoint expression, if REPARSE is non-zero
1616 - Evaluate expression and store the result in B->val
1617 - Evaluate the condition if there is one, and store the result
1618 in b->loc->cond.
1619 - Update the list of values that must be watched in B->loc.
1620
1621 If the watchpoint disposition is disp_del_at_next_stop, then do
1622 nothing. If this is local watchpoint that is out of scope, delete
1623 it.
1624
1625 Even with `set breakpoint always-inserted on' the watchpoints are
1626 removed + inserted on each stop here. Normal breakpoints must
1627 never be removed because they might be missed by a running thread
1628 when debugging in non-stop mode. On the other hand, hardware
1629 watchpoints (is_hardware_watchpoint; processed here) are specific
1630 to each LWP since they are stored in each LWP's hardware debug
1631 registers. Therefore, such LWP must be stopped first in order to
1632 be able to modify its hardware watchpoints.
1633
1634 Hardware watchpoints must be reset exactly once after being
1635 presented to the user. It cannot be done sooner, because it would
1636 reset the data used to present the watchpoint hit to the user. And
1637 it must not be done later because it could display the same single
1638 watchpoint hit during multiple GDB stops. Note that the latter is
1639 relevant only to the hardware watchpoint types bp_read_watchpoint
1640 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1641 not user-visible - its hit is suppressed if the memory content has
1642 not changed.
1643
1644 The following constraints influence the location where we can reset
1645 hardware watchpoints:
1646
1647 * target_stopped_by_watchpoint and target_stopped_data_address are
1648 called several times when GDB stops.
1649
1650 [linux]
1651 * Multiple hardware watchpoints can be hit at the same time,
1652 causing GDB to stop. GDB only presents one hardware watchpoint
1653 hit at a time as the reason for stopping, and all the other hits
1654 are presented later, one after the other, each time the user
1655 requests the execution to be resumed. Execution is not resumed
1656 for the threads still having pending hit event stored in
1657 LWP_INFO->STATUS. While the watchpoint is already removed from
1658 the inferior on the first stop the thread hit event is kept being
1659 reported from its cached value by linux_nat_stopped_data_address
1660 until the real thread resume happens after the watchpoint gets
1661 presented and thus its LWP_INFO->STATUS gets reset.
1662
1663 Therefore the hardware watchpoint hit can get safely reset on the
1664 watchpoint removal from inferior. */
1665
1666 static void
1667 update_watchpoint (struct watchpoint *b, int reparse)
1668 {
1669 int within_current_scope;
1670 struct frame_id saved_frame_id;
1671 int frame_saved;
1672
1673 /* If this is a local watchpoint, we only want to check if the
1674 watchpoint frame is in scope if the current thread is the thread
1675 that was used to create the watchpoint. */
1676 if (!watchpoint_in_thread_scope (b))
1677 return;
1678
1679 if (b->disposition == disp_del_at_next_stop)
1680 return;
1681
1682 frame_saved = 0;
1683
1684 /* Determine if the watchpoint is within scope. */
1685 if (b->exp_valid_block == NULL)
1686 within_current_scope = 1;
1687 else
1688 {
1689 struct frame_info *fi = get_current_frame ();
1690 struct gdbarch *frame_arch = get_frame_arch (fi);
1691 CORE_ADDR frame_pc = get_frame_pc (fi);
1692
1693 /* If we're at a point where the stack has been destroyed
1694 (e.g. in a function epilogue), unwinding may not work
1695 properly. Do not attempt to recreate locations at this
1696 point. See similar comments in watchpoint_check. */
1697 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1698 return;
1699
1700 /* Save the current frame's ID so we can restore it after
1701 evaluating the watchpoint expression on its own frame. */
1702 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1703 took a frame parameter, so that we didn't have to change the
1704 selected frame. */
1705 frame_saved = 1;
1706 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1707
1708 fi = frame_find_by_id (b->watchpoint_frame);
1709 within_current_scope = (fi != NULL);
1710 if (within_current_scope)
1711 select_frame (fi);
1712 }
1713
1714 /* We don't free locations. They are stored in the bp_location array
1715 and update_global_location_list will eventually delete them and
1716 remove breakpoints if needed. */
1717 b->loc = NULL;
1718
1719 if (within_current_scope && reparse)
1720 {
1721 const char *s;
1722
1723 b->exp.reset ();
1724 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1725 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1726 /* If the meaning of expression itself changed, the old value is
1727 no longer relevant. We don't want to report a watchpoint hit
1728 to the user when the old value and the new value may actually
1729 be completely different objects. */
1730 b->val = NULL;
1731 b->val_valid = false;
1732
1733 /* Note that unlike with breakpoints, the watchpoint's condition
1734 expression is stored in the breakpoint object, not in the
1735 locations (re)created below. */
1736 if (b->cond_string != NULL)
1737 {
1738 b->cond_exp.reset ();
1739
1740 s = b->cond_string;
1741 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1742 }
1743 }
1744
1745 /* If we failed to parse the expression, for example because
1746 it refers to a global variable in a not-yet-loaded shared library,
1747 don't try to insert watchpoint. We don't automatically delete
1748 such watchpoint, though, since failure to parse expression
1749 is different from out-of-scope watchpoint. */
1750 if (!target_has_execution)
1751 {
1752 /* Without execution, memory can't change. No use to try and
1753 set watchpoint locations. The watchpoint will be reset when
1754 the target gains execution, through breakpoint_re_set. */
1755 if (!can_use_hw_watchpoints)
1756 {
1757 if (b->ops->works_in_software_mode (b))
1758 b->type = bp_watchpoint;
1759 else
1760 error (_("Can't set read/access watchpoint when "
1761 "hardware watchpoints are disabled."));
1762 }
1763 }
1764 else if (within_current_scope && b->exp)
1765 {
1766 int pc = 0;
1767 std::vector<value_ref_ptr> val_chain;
1768 struct value *v, *result;
1769 struct program_space *frame_pspace;
1770
1771 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1772
1773 /* Avoid setting b->val if it's already set. The meaning of
1774 b->val is 'the last value' user saw, and we should update
1775 it only if we reported that last value to user. As it
1776 happens, the code that reports it updates b->val directly.
1777 We don't keep track of the memory value for masked
1778 watchpoints. */
1779 if (!b->val_valid && !is_masked_watchpoint (b))
1780 {
1781 if (b->val_bitsize != 0)
1782 v = extract_bitfield_from_watchpoint_value (b, v);
1783 b->val = release_value (v);
1784 b->val_valid = true;
1785 }
1786
1787 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1788
1789 /* Look at each value on the value chain. */
1790 gdb_assert (!val_chain.empty ());
1791 for (const value_ref_ptr &iter : val_chain)
1792 {
1793 v = iter.get ();
1794
1795 /* If it's a memory location, and GDB actually needed
1796 its contents to evaluate the expression, then we
1797 must watch it. If the first value returned is
1798 still lazy, that means an error occurred reading it;
1799 watch it anyway in case it becomes readable. */
1800 if (VALUE_LVAL (v) == lval_memory
1801 && (v == val_chain[0] || ! value_lazy (v)))
1802 {
1803 struct type *vtype = check_typedef (value_type (v));
1804
1805 /* We only watch structs and arrays if user asked
1806 for it explicitly, never if they just happen to
1807 appear in the middle of some value chain. */
1808 if (v == result
1809 || (vtype->code () != TYPE_CODE_STRUCT
1810 && vtype->code () != TYPE_CODE_ARRAY))
1811 {
1812 CORE_ADDR addr;
1813 enum target_hw_bp_type type;
1814 struct bp_location *loc, **tmp;
1815 int bitpos = 0, bitsize = 0;
1816
1817 if (value_bitsize (v) != 0)
1818 {
1819 /* Extract the bit parameters out from the bitfield
1820 sub-expression. */
1821 bitpos = value_bitpos (v);
1822 bitsize = value_bitsize (v);
1823 }
1824 else if (v == result && b->val_bitsize != 0)
1825 {
1826 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1827 lvalue whose bit parameters are saved in the fields
1828 VAL_BITPOS and VAL_BITSIZE. */
1829 bitpos = b->val_bitpos;
1830 bitsize = b->val_bitsize;
1831 }
1832
1833 addr = value_address (v);
1834 if (bitsize != 0)
1835 {
1836 /* Skip the bytes that don't contain the bitfield. */
1837 addr += bitpos / 8;
1838 }
1839
1840 type = hw_write;
1841 if (b->type == bp_read_watchpoint)
1842 type = hw_read;
1843 else if (b->type == bp_access_watchpoint)
1844 type = hw_access;
1845
1846 loc = allocate_bp_location (b);
1847 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1848 ;
1849 *tmp = loc;
1850 loc->gdbarch = get_type_arch (value_type (v));
1851
1852 loc->pspace = frame_pspace;
1853 loc->address = address_significant (loc->gdbarch, addr);
1854
1855 if (bitsize != 0)
1856 {
1857 /* Just cover the bytes that make up the bitfield. */
1858 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1859 }
1860 else
1861 loc->length = TYPE_LENGTH (value_type (v));
1862
1863 loc->watchpoint_type = type;
1864 }
1865 }
1866 }
1867
1868 /* Change the type of breakpoint between hardware assisted or
1869 an ordinary watchpoint depending on the hardware support
1870 and free hardware slots. REPARSE is set when the inferior
1871 is started. */
1872 if (reparse)
1873 {
1874 int reg_cnt;
1875 enum bp_loc_type loc_type;
1876 struct bp_location *bl;
1877
1878 reg_cnt = can_use_hardware_watchpoint (val_chain);
1879
1880 if (reg_cnt)
1881 {
1882 int i, target_resources_ok, other_type_used;
1883 enum bptype type;
1884
1885 /* Use an exact watchpoint when there's only one memory region to be
1886 watched, and only one debug register is needed to watch it. */
1887 b->exact = target_exact_watchpoints && reg_cnt == 1;
1888
1889 /* We need to determine how many resources are already
1890 used for all other hardware watchpoints plus this one
1891 to see if we still have enough resources to also fit
1892 this watchpoint in as well. */
1893
1894 /* If this is a software watchpoint, we try to turn it
1895 to a hardware one -- count resources as if B was of
1896 hardware watchpoint type. */
1897 type = b->type;
1898 if (type == bp_watchpoint)
1899 type = bp_hardware_watchpoint;
1900
1901 /* This watchpoint may or may not have been placed on
1902 the list yet at this point (it won't be in the list
1903 if we're trying to create it for the first time,
1904 through watch_command), so always account for it
1905 manually. */
1906
1907 /* Count resources used by all watchpoints except B. */
1908 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1909
1910 /* Add in the resources needed for B. */
1911 i += hw_watchpoint_use_count (b);
1912
1913 target_resources_ok
1914 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1915 if (target_resources_ok <= 0)
1916 {
1917 int sw_mode = b->ops->works_in_software_mode (b);
1918
1919 if (target_resources_ok == 0 && !sw_mode)
1920 error (_("Target does not support this type of "
1921 "hardware watchpoint."));
1922 else if (target_resources_ok < 0 && !sw_mode)
1923 error (_("There are not enough available hardware "
1924 "resources for this watchpoint."));
1925
1926 /* Downgrade to software watchpoint. */
1927 b->type = bp_watchpoint;
1928 }
1929 else
1930 {
1931 /* If this was a software watchpoint, we've just
1932 found we have enough resources to turn it to a
1933 hardware watchpoint. Otherwise, this is a
1934 nop. */
1935 b->type = type;
1936 }
1937 }
1938 else if (!b->ops->works_in_software_mode (b))
1939 {
1940 if (!can_use_hw_watchpoints)
1941 error (_("Can't set read/access watchpoint when "
1942 "hardware watchpoints are disabled."));
1943 else
1944 error (_("Expression cannot be implemented with "
1945 "read/access watchpoint."));
1946 }
1947 else
1948 b->type = bp_watchpoint;
1949
1950 loc_type = (b->type == bp_watchpoint? bp_loc_other
1951 : bp_loc_hardware_watchpoint);
1952 for (bl = b->loc; bl; bl = bl->next)
1953 bl->loc_type = loc_type;
1954 }
1955
1956 /* If a software watchpoint is not watching any memory, then the
1957 above left it without any location set up. But,
1958 bpstat_stop_status requires a location to be able to report
1959 stops, so make sure there's at least a dummy one. */
1960 if (b->type == bp_watchpoint && b->loc == NULL)
1961 software_watchpoint_add_no_memory_location (b, frame_pspace);
1962 }
1963 else if (!within_current_scope)
1964 {
1965 printf_filtered (_("\
1966 Watchpoint %d deleted because the program has left the block\n\
1967 in which its expression is valid.\n"),
1968 b->number);
1969 watchpoint_del_at_next_stop (b);
1970 }
1971
1972 /* Restore the selected frame. */
1973 if (frame_saved)
1974 select_frame (frame_find_by_id (saved_frame_id));
1975 }
1976
1977
1978 /* Returns 1 iff breakpoint location should be
1979 inserted in the inferior. We don't differentiate the type of BL's owner
1980 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1981 breakpoint_ops is not defined, because in insert_bp_location,
1982 tracepoint's insert_location will not be called. */
1983 static int
1984 should_be_inserted (struct bp_location *bl)
1985 {
1986 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1987 return 0;
1988
1989 if (bl->owner->disposition == disp_del_at_next_stop)
1990 return 0;
1991
1992 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1993 return 0;
1994
1995 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1996 return 0;
1997
1998 /* This is set for example, when we're attached to the parent of a
1999 vfork, and have detached from the child. The child is running
2000 free, and we expect it to do an exec or exit, at which point the
2001 OS makes the parent schedulable again (and the target reports
2002 that the vfork is done). Until the child is done with the shared
2003 memory region, do not insert breakpoints in the parent, otherwise
2004 the child could still trip on the parent's breakpoints. Since
2005 the parent is blocked anyway, it won't miss any breakpoint. */
2006 if (bl->pspace->breakpoints_not_allowed)
2007 return 0;
2008
2009 /* Don't insert a breakpoint if we're trying to step past its
2010 location, except if the breakpoint is a single-step breakpoint,
2011 and the breakpoint's thread is the thread which is stepping past
2012 a breakpoint. */
2013 if ((bl->loc_type == bp_loc_software_breakpoint
2014 || bl->loc_type == bp_loc_hardware_breakpoint)
2015 && stepping_past_instruction_at (bl->pspace->aspace,
2016 bl->address)
2017 /* The single-step breakpoint may be inserted at the location
2018 we're trying to step if the instruction branches to itself.
2019 However, the instruction won't be executed at all and it may
2020 break the semantics of the instruction, for example, the
2021 instruction is a conditional branch or updates some flags.
2022 We can't fix it unless GDB is able to emulate the instruction
2023 or switch to displaced stepping. */
2024 && !(bl->owner->type == bp_single_step
2025 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2026 {
2027 if (debug_infrun)
2028 {
2029 fprintf_unfiltered (gdb_stdlog,
2030 "infrun: skipping breakpoint: "
2031 "stepping past insn at: %s\n",
2032 paddress (bl->gdbarch, bl->address));
2033 }
2034 return 0;
2035 }
2036
2037 /* Don't insert watchpoints if we're trying to step past the
2038 instruction that triggered one. */
2039 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2040 && stepping_past_nonsteppable_watchpoint ())
2041 {
2042 if (debug_infrun)
2043 {
2044 fprintf_unfiltered (gdb_stdlog,
2045 "infrun: stepping past non-steppable watchpoint. "
2046 "skipping watchpoint at %s:%d\n",
2047 paddress (bl->gdbarch, bl->address),
2048 bl->length);
2049 }
2050 return 0;
2051 }
2052
2053 return 1;
2054 }
2055
2056 /* Same as should_be_inserted but does the check assuming
2057 that the location is not duplicated. */
2058
2059 static int
2060 unduplicated_should_be_inserted (struct bp_location *bl)
2061 {
2062 int result;
2063 const int save_duplicate = bl->duplicate;
2064
2065 bl->duplicate = 0;
2066 result = should_be_inserted (bl);
2067 bl->duplicate = save_duplicate;
2068 return result;
2069 }
2070
2071 /* Parses a conditional described by an expression COND into an
2072 agent expression bytecode suitable for evaluation
2073 by the bytecode interpreter. Return NULL if there was
2074 any error during parsing. */
2075
2076 static agent_expr_up
2077 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2078 {
2079 if (cond == NULL)
2080 return NULL;
2081
2082 agent_expr_up aexpr;
2083
2084 /* We don't want to stop processing, so catch any errors
2085 that may show up. */
2086 try
2087 {
2088 aexpr = gen_eval_for_expr (scope, cond);
2089 }
2090
2091 catch (const gdb_exception_error &ex)
2092 {
2093 /* If we got here, it means the condition could not be parsed to a valid
2094 bytecode expression and thus can't be evaluated on the target's side.
2095 It's no use iterating through the conditions. */
2096 }
2097
2098 /* We have a valid agent expression. */
2099 return aexpr;
2100 }
2101
2102 /* Based on location BL, create a list of breakpoint conditions to be
2103 passed on to the target. If we have duplicated locations with different
2104 conditions, we will add such conditions to the list. The idea is that the
2105 target will evaluate the list of conditions and will only notify GDB when
2106 one of them is true. */
2107
2108 static void
2109 build_target_condition_list (struct bp_location *bl)
2110 {
2111 struct bp_location **locp = NULL, **loc2p;
2112 int null_condition_or_parse_error = 0;
2113 int modified = bl->needs_update;
2114 struct bp_location *loc;
2115
2116 /* Release conditions left over from a previous insert. */
2117 bl->target_info.conditions.clear ();
2118
2119 /* This is only meaningful if the target is
2120 evaluating conditions and if the user has
2121 opted for condition evaluation on the target's
2122 side. */
2123 if (gdb_evaluates_breakpoint_condition_p ()
2124 || !target_supports_evaluation_of_breakpoint_conditions ())
2125 return;
2126
2127 /* Do a first pass to check for locations with no assigned
2128 conditions or conditions that fail to parse to a valid agent expression
2129 bytecode. If any of these happen, then it's no use to send conditions
2130 to the target since this location will always trigger and generate a
2131 response back to GDB. */
2132 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2133 {
2134 loc = (*loc2p);
2135 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2136 {
2137 if (modified)
2138 {
2139 /* Re-parse the conditions since something changed. In that
2140 case we already freed the condition bytecodes (see
2141 force_breakpoint_reinsertion). We just
2142 need to parse the condition to bytecodes again. */
2143 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2144 loc->cond.get ());
2145 }
2146
2147 /* If we have a NULL bytecode expression, it means something
2148 went wrong or we have a null condition expression. */
2149 if (!loc->cond_bytecode)
2150 {
2151 null_condition_or_parse_error = 1;
2152 break;
2153 }
2154 }
2155 }
2156
2157 /* If any of these happened, it means we will have to evaluate the conditions
2158 for the location's address on gdb's side. It is no use keeping bytecodes
2159 for all the other duplicate locations, thus we free all of them here.
2160
2161 This is so we have a finer control over which locations' conditions are
2162 being evaluated by GDB or the remote stub. */
2163 if (null_condition_or_parse_error)
2164 {
2165 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2166 {
2167 loc = (*loc2p);
2168 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2169 {
2170 /* Only go as far as the first NULL bytecode is
2171 located. */
2172 if (!loc->cond_bytecode)
2173 return;
2174
2175 loc->cond_bytecode.reset ();
2176 }
2177 }
2178 }
2179
2180 /* No NULL conditions or failed bytecode generation. Build a condition list
2181 for this location's address. */
2182 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2183 {
2184 loc = (*loc2p);
2185 if (loc->cond
2186 && is_breakpoint (loc->owner)
2187 && loc->pspace->num == bl->pspace->num
2188 && loc->owner->enable_state == bp_enabled
2189 && loc->enabled)
2190 {
2191 /* Add the condition to the vector. This will be used later
2192 to send the conditions to the target. */
2193 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2194 }
2195 }
2196
2197 return;
2198 }
2199
2200 /* Parses a command described by string CMD into an agent expression
2201 bytecode suitable for evaluation by the bytecode interpreter.
2202 Return NULL if there was any error during parsing. */
2203
2204 static agent_expr_up
2205 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2206 {
2207 const char *cmdrest;
2208 const char *format_start, *format_end;
2209 struct gdbarch *gdbarch = get_current_arch ();
2210
2211 if (cmd == NULL)
2212 return NULL;
2213
2214 cmdrest = cmd;
2215
2216 if (*cmdrest == ',')
2217 ++cmdrest;
2218 cmdrest = skip_spaces (cmdrest);
2219
2220 if (*cmdrest++ != '"')
2221 error (_("No format string following the location"));
2222
2223 format_start = cmdrest;
2224
2225 format_pieces fpieces (&cmdrest);
2226
2227 format_end = cmdrest;
2228
2229 if (*cmdrest++ != '"')
2230 error (_("Bad format string, non-terminated '\"'."));
2231
2232 cmdrest = skip_spaces (cmdrest);
2233
2234 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2235 error (_("Invalid argument syntax"));
2236
2237 if (*cmdrest == ',')
2238 cmdrest++;
2239 cmdrest = skip_spaces (cmdrest);
2240
2241 /* For each argument, make an expression. */
2242
2243 std::vector<struct expression *> argvec;
2244 while (*cmdrest != '\0')
2245 {
2246 const char *cmd1;
2247
2248 cmd1 = cmdrest;
2249 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2250 argvec.push_back (expr.release ());
2251 cmdrest = cmd1;
2252 if (*cmdrest == ',')
2253 ++cmdrest;
2254 }
2255
2256 agent_expr_up aexpr;
2257
2258 /* We don't want to stop processing, so catch any errors
2259 that may show up. */
2260 try
2261 {
2262 aexpr = gen_printf (scope, gdbarch, 0, 0,
2263 format_start, format_end - format_start,
2264 argvec.size (), argvec.data ());
2265 }
2266 catch (const gdb_exception_error &ex)
2267 {
2268 /* If we got here, it means the command could not be parsed to a valid
2269 bytecode expression and thus can't be evaluated on the target's side.
2270 It's no use iterating through the other commands. */
2271 }
2272
2273 /* We have a valid agent expression, return it. */
2274 return aexpr;
2275 }
2276
2277 /* Based on location BL, create a list of breakpoint commands to be
2278 passed on to the target. If we have duplicated locations with
2279 different commands, we will add any such to the list. */
2280
2281 static void
2282 build_target_command_list (struct bp_location *bl)
2283 {
2284 struct bp_location **locp = NULL, **loc2p;
2285 int null_command_or_parse_error = 0;
2286 int modified = bl->needs_update;
2287 struct bp_location *loc;
2288
2289 /* Clear commands left over from a previous insert. */
2290 bl->target_info.tcommands.clear ();
2291
2292 if (!target_can_run_breakpoint_commands ())
2293 return;
2294
2295 /* For now, limit to agent-style dprintf breakpoints. */
2296 if (dprintf_style != dprintf_style_agent)
2297 return;
2298
2299 /* For now, if we have any duplicate location that isn't a dprintf,
2300 don't install the target-side commands, as that would make the
2301 breakpoint not be reported to the core, and we'd lose
2302 control. */
2303 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2304 {
2305 loc = (*loc2p);
2306 if (is_breakpoint (loc->owner)
2307 && loc->pspace->num == bl->pspace->num
2308 && loc->owner->type != bp_dprintf)
2309 return;
2310 }
2311
2312 /* Do a first pass to check for locations with no assigned
2313 conditions or conditions that fail to parse to a valid agent expression
2314 bytecode. If any of these happen, then it's no use to send conditions
2315 to the target since this location will always trigger and generate a
2316 response back to GDB. */
2317 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2318 {
2319 loc = (*loc2p);
2320 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2321 {
2322 if (modified)
2323 {
2324 /* Re-parse the commands since something changed. In that
2325 case we already freed the command bytecodes (see
2326 force_breakpoint_reinsertion). We just
2327 need to parse the command to bytecodes again. */
2328 loc->cmd_bytecode
2329 = parse_cmd_to_aexpr (bl->address,
2330 loc->owner->extra_string);
2331 }
2332
2333 /* If we have a NULL bytecode expression, it means something
2334 went wrong or we have a null command expression. */
2335 if (!loc->cmd_bytecode)
2336 {
2337 null_command_or_parse_error = 1;
2338 break;
2339 }
2340 }
2341 }
2342
2343 /* If anything failed, then we're not doing target-side commands,
2344 and so clean up. */
2345 if (null_command_or_parse_error)
2346 {
2347 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2348 {
2349 loc = (*loc2p);
2350 if (is_breakpoint (loc->owner)
2351 && loc->pspace->num == bl->pspace->num)
2352 {
2353 /* Only go as far as the first NULL bytecode is
2354 located. */
2355 if (loc->cmd_bytecode == NULL)
2356 return;
2357
2358 loc->cmd_bytecode.reset ();
2359 }
2360 }
2361 }
2362
2363 /* No NULL commands or failed bytecode generation. Build a command list
2364 for this location's address. */
2365 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2366 {
2367 loc = (*loc2p);
2368 if (loc->owner->extra_string
2369 && is_breakpoint (loc->owner)
2370 && loc->pspace->num == bl->pspace->num
2371 && loc->owner->enable_state == bp_enabled
2372 && loc->enabled)
2373 {
2374 /* Add the command to the vector. This will be used later
2375 to send the commands to the target. */
2376 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2377 }
2378 }
2379
2380 bl->target_info.persist = 0;
2381 /* Maybe flag this location as persistent. */
2382 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2383 bl->target_info.persist = 1;
2384 }
2385
2386 /* Return the kind of breakpoint on address *ADDR. Get the kind
2387 of breakpoint according to ADDR except single-step breakpoint.
2388 Get the kind of single-step breakpoint according to the current
2389 registers state. */
2390
2391 static int
2392 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2393 {
2394 if (bl->owner->type == bp_single_step)
2395 {
2396 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2397 struct regcache *regcache;
2398
2399 regcache = get_thread_regcache (thr);
2400
2401 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2402 regcache, addr);
2403 }
2404 else
2405 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2406 }
2407
2408 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2409 location. Any error messages are printed to TMP_ERROR_STREAM; and
2410 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2411 Returns 0 for success, 1 if the bp_location type is not supported or
2412 -1 for failure.
2413
2414 NOTE drow/2003-09-09: This routine could be broken down to an
2415 object-style method for each breakpoint or catchpoint type. */
2416 static int
2417 insert_bp_location (struct bp_location *bl,
2418 struct ui_file *tmp_error_stream,
2419 int *disabled_breaks,
2420 int *hw_breakpoint_error,
2421 int *hw_bp_error_explained_already)
2422 {
2423 gdb_exception bp_excpt;
2424
2425 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2426 return 0;
2427
2428 /* Note we don't initialize bl->target_info, as that wipes out
2429 the breakpoint location's shadow_contents if the breakpoint
2430 is still inserted at that location. This in turn breaks
2431 target_read_memory which depends on these buffers when
2432 a memory read is requested at the breakpoint location:
2433 Once the target_info has been wiped, we fail to see that
2434 we have a breakpoint inserted at that address and thus
2435 read the breakpoint instead of returning the data saved in
2436 the breakpoint location's shadow contents. */
2437 bl->target_info.reqstd_address = bl->address;
2438 bl->target_info.placed_address_space = bl->pspace->aspace;
2439 bl->target_info.length = bl->length;
2440
2441 /* When working with target-side conditions, we must pass all the conditions
2442 for the same breakpoint address down to the target since GDB will not
2443 insert those locations. With a list of breakpoint conditions, the target
2444 can decide when to stop and notify GDB. */
2445
2446 if (is_breakpoint (bl->owner))
2447 {
2448 build_target_condition_list (bl);
2449 build_target_command_list (bl);
2450 /* Reset the modification marker. */
2451 bl->needs_update = 0;
2452 }
2453
2454 if (bl->loc_type == bp_loc_software_breakpoint
2455 || bl->loc_type == bp_loc_hardware_breakpoint)
2456 {
2457 if (bl->owner->type != bp_hardware_breakpoint)
2458 {
2459 /* If the explicitly specified breakpoint type
2460 is not hardware breakpoint, check the memory map to see
2461 if the breakpoint address is in read only memory or not.
2462
2463 Two important cases are:
2464 - location type is not hardware breakpoint, memory
2465 is readonly. We change the type of the location to
2466 hardware breakpoint.
2467 - location type is hardware breakpoint, memory is
2468 read-write. This means we've previously made the
2469 location hardware one, but then the memory map changed,
2470 so we undo.
2471
2472 When breakpoints are removed, remove_breakpoints will use
2473 location types we've just set here, the only possible
2474 problem is that memory map has changed during running
2475 program, but it's not going to work anyway with current
2476 gdb. */
2477 struct mem_region *mr
2478 = lookup_mem_region (bl->target_info.reqstd_address);
2479
2480 if (mr)
2481 {
2482 if (automatic_hardware_breakpoints)
2483 {
2484 enum bp_loc_type new_type;
2485
2486 if (mr->attrib.mode != MEM_RW)
2487 new_type = bp_loc_hardware_breakpoint;
2488 else
2489 new_type = bp_loc_software_breakpoint;
2490
2491 if (new_type != bl->loc_type)
2492 {
2493 static int said = 0;
2494
2495 bl->loc_type = new_type;
2496 if (!said)
2497 {
2498 fprintf_filtered (gdb_stdout,
2499 _("Note: automatically using "
2500 "hardware breakpoints for "
2501 "read-only addresses.\n"));
2502 said = 1;
2503 }
2504 }
2505 }
2506 else if (bl->loc_type == bp_loc_software_breakpoint
2507 && mr->attrib.mode != MEM_RW)
2508 {
2509 fprintf_unfiltered (tmp_error_stream,
2510 _("Cannot insert breakpoint %d.\n"
2511 "Cannot set software breakpoint "
2512 "at read-only address %s\n"),
2513 bl->owner->number,
2514 paddress (bl->gdbarch, bl->address));
2515 return 1;
2516 }
2517 }
2518 }
2519
2520 /* First check to see if we have to handle an overlay. */
2521 if (overlay_debugging == ovly_off
2522 || bl->section == NULL
2523 || !(section_is_overlay (bl->section)))
2524 {
2525 /* No overlay handling: just set the breakpoint. */
2526 try
2527 {
2528 int val;
2529
2530 val = bl->owner->ops->insert_location (bl);
2531 if (val)
2532 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2533 }
2534 catch (gdb_exception &e)
2535 {
2536 bp_excpt = std::move (e);
2537 }
2538 }
2539 else
2540 {
2541 /* This breakpoint is in an overlay section.
2542 Shall we set a breakpoint at the LMA? */
2543 if (!overlay_events_enabled)
2544 {
2545 /* Yes -- overlay event support is not active,
2546 so we must try to set a breakpoint at the LMA.
2547 This will not work for a hardware breakpoint. */
2548 if (bl->loc_type == bp_loc_hardware_breakpoint)
2549 warning (_("hardware breakpoint %d not supported in overlay!"),
2550 bl->owner->number);
2551 else
2552 {
2553 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2554 bl->section);
2555 /* Set a software (trap) breakpoint at the LMA. */
2556 bl->overlay_target_info = bl->target_info;
2557 bl->overlay_target_info.reqstd_address = addr;
2558
2559 /* No overlay handling: just set the breakpoint. */
2560 try
2561 {
2562 int val;
2563
2564 bl->overlay_target_info.kind
2565 = breakpoint_kind (bl, &addr);
2566 bl->overlay_target_info.placed_address = addr;
2567 val = target_insert_breakpoint (bl->gdbarch,
2568 &bl->overlay_target_info);
2569 if (val)
2570 bp_excpt
2571 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2572 }
2573 catch (gdb_exception &e)
2574 {
2575 bp_excpt = std::move (e);
2576 }
2577
2578 if (bp_excpt.reason != 0)
2579 fprintf_unfiltered (tmp_error_stream,
2580 "Overlay breakpoint %d "
2581 "failed: in ROM?\n",
2582 bl->owner->number);
2583 }
2584 }
2585 /* Shall we set a breakpoint at the VMA? */
2586 if (section_is_mapped (bl->section))
2587 {
2588 /* Yes. This overlay section is mapped into memory. */
2589 try
2590 {
2591 int val;
2592
2593 val = bl->owner->ops->insert_location (bl);
2594 if (val)
2595 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2596 }
2597 catch (gdb_exception &e)
2598 {
2599 bp_excpt = std::move (e);
2600 }
2601 }
2602 else
2603 {
2604 /* No. This breakpoint will not be inserted.
2605 No error, but do not mark the bp as 'inserted'. */
2606 return 0;
2607 }
2608 }
2609
2610 if (bp_excpt.reason != 0)
2611 {
2612 /* Can't set the breakpoint. */
2613
2614 /* In some cases, we might not be able to insert a
2615 breakpoint in a shared library that has already been
2616 removed, but we have not yet processed the shlib unload
2617 event. Unfortunately, some targets that implement
2618 breakpoint insertion themselves can't tell why the
2619 breakpoint insertion failed (e.g., the remote target
2620 doesn't define error codes), so we must treat generic
2621 errors as memory errors. */
2622 if (bp_excpt.reason == RETURN_ERROR
2623 && (bp_excpt.error == GENERIC_ERROR
2624 || bp_excpt.error == MEMORY_ERROR)
2625 && bl->loc_type == bp_loc_software_breakpoint
2626 && (solib_name_from_address (bl->pspace, bl->address)
2627 || shared_objfile_contains_address_p (bl->pspace,
2628 bl->address)))
2629 {
2630 /* See also: disable_breakpoints_in_shlibs. */
2631 bl->shlib_disabled = 1;
2632 gdb::observers::breakpoint_modified.notify (bl->owner);
2633 if (!*disabled_breaks)
2634 {
2635 fprintf_unfiltered (tmp_error_stream,
2636 "Cannot insert breakpoint %d.\n",
2637 bl->owner->number);
2638 fprintf_unfiltered (tmp_error_stream,
2639 "Temporarily disabling shared "
2640 "library breakpoints:\n");
2641 }
2642 *disabled_breaks = 1;
2643 fprintf_unfiltered (tmp_error_stream,
2644 "breakpoint #%d\n", bl->owner->number);
2645 return 0;
2646 }
2647 else
2648 {
2649 if (bl->loc_type == bp_loc_hardware_breakpoint)
2650 {
2651 *hw_breakpoint_error = 1;
2652 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2653 fprintf_unfiltered (tmp_error_stream,
2654 "Cannot insert hardware breakpoint %d%s",
2655 bl->owner->number,
2656 bp_excpt.message ? ":" : ".\n");
2657 if (bp_excpt.message != NULL)
2658 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2659 bp_excpt.what ());
2660 }
2661 else
2662 {
2663 if (bp_excpt.message == NULL)
2664 {
2665 std::string message
2666 = memory_error_message (TARGET_XFER_E_IO,
2667 bl->gdbarch, bl->address);
2668
2669 fprintf_unfiltered (tmp_error_stream,
2670 "Cannot insert breakpoint %d.\n"
2671 "%s\n",
2672 bl->owner->number, message.c_str ());
2673 }
2674 else
2675 {
2676 fprintf_unfiltered (tmp_error_stream,
2677 "Cannot insert breakpoint %d: %s\n",
2678 bl->owner->number,
2679 bp_excpt.what ());
2680 }
2681 }
2682 return 1;
2683
2684 }
2685 }
2686 else
2687 bl->inserted = 1;
2688
2689 return 0;
2690 }
2691
2692 else if (bl->loc_type == bp_loc_hardware_watchpoint
2693 /* NOTE drow/2003-09-08: This state only exists for removing
2694 watchpoints. It's not clear that it's necessary... */
2695 && bl->owner->disposition != disp_del_at_next_stop)
2696 {
2697 int val;
2698
2699 gdb_assert (bl->owner->ops != NULL
2700 && bl->owner->ops->insert_location != NULL);
2701
2702 val = bl->owner->ops->insert_location (bl);
2703
2704 /* If trying to set a read-watchpoint, and it turns out it's not
2705 supported, try emulating one with an access watchpoint. */
2706 if (val == 1 && bl->watchpoint_type == hw_read)
2707 {
2708 struct bp_location *loc, **loc_temp;
2709
2710 /* But don't try to insert it, if there's already another
2711 hw_access location that would be considered a duplicate
2712 of this one. */
2713 ALL_BP_LOCATIONS (loc, loc_temp)
2714 if (loc != bl
2715 && loc->watchpoint_type == hw_access
2716 && watchpoint_locations_match (bl, loc))
2717 {
2718 bl->duplicate = 1;
2719 bl->inserted = 1;
2720 bl->target_info = loc->target_info;
2721 bl->watchpoint_type = hw_access;
2722 val = 0;
2723 break;
2724 }
2725
2726 if (val == 1)
2727 {
2728 bl->watchpoint_type = hw_access;
2729 val = bl->owner->ops->insert_location (bl);
2730
2731 if (val)
2732 /* Back to the original value. */
2733 bl->watchpoint_type = hw_read;
2734 }
2735 }
2736
2737 bl->inserted = (val == 0);
2738 }
2739
2740 else if (bl->owner->type == bp_catchpoint)
2741 {
2742 int val;
2743
2744 gdb_assert (bl->owner->ops != NULL
2745 && bl->owner->ops->insert_location != NULL);
2746
2747 val = bl->owner->ops->insert_location (bl);
2748 if (val)
2749 {
2750 bl->owner->enable_state = bp_disabled;
2751
2752 if (val == 1)
2753 warning (_("\
2754 Error inserting catchpoint %d: Your system does not support this type\n\
2755 of catchpoint."), bl->owner->number);
2756 else
2757 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2758 }
2759
2760 bl->inserted = (val == 0);
2761
2762 /* We've already printed an error message if there was a problem
2763 inserting this catchpoint, and we've disabled the catchpoint,
2764 so just return success. */
2765 return 0;
2766 }
2767
2768 return 0;
2769 }
2770
2771 /* This function is called when program space PSPACE is about to be
2772 deleted. It takes care of updating breakpoints to not reference
2773 PSPACE anymore. */
2774
2775 void
2776 breakpoint_program_space_exit (struct program_space *pspace)
2777 {
2778 struct breakpoint *b, *b_temp;
2779 struct bp_location *loc, **loc_temp;
2780
2781 /* Remove any breakpoint that was set through this program space. */
2782 ALL_BREAKPOINTS_SAFE (b, b_temp)
2783 {
2784 if (b->pspace == pspace)
2785 delete_breakpoint (b);
2786 }
2787
2788 /* Breakpoints set through other program spaces could have locations
2789 bound to PSPACE as well. Remove those. */
2790 ALL_BP_LOCATIONS (loc, loc_temp)
2791 {
2792 struct bp_location *tmp;
2793
2794 if (loc->pspace == pspace)
2795 {
2796 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2797 if (loc->owner->loc == loc)
2798 loc->owner->loc = loc->next;
2799 else
2800 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2801 if (tmp->next == loc)
2802 {
2803 tmp->next = loc->next;
2804 break;
2805 }
2806 }
2807 }
2808
2809 /* Now update the global location list to permanently delete the
2810 removed locations above. */
2811 update_global_location_list (UGLL_DONT_INSERT);
2812 }
2813
2814 /* Make sure all breakpoints are inserted in inferior.
2815 Throws exception on any error.
2816 A breakpoint that is already inserted won't be inserted
2817 again, so calling this function twice is safe. */
2818 void
2819 insert_breakpoints (void)
2820 {
2821 struct breakpoint *bpt;
2822
2823 ALL_BREAKPOINTS (bpt)
2824 if (is_hardware_watchpoint (bpt))
2825 {
2826 struct watchpoint *w = (struct watchpoint *) bpt;
2827
2828 update_watchpoint (w, 0 /* don't reparse. */);
2829 }
2830
2831 /* Updating watchpoints creates new locations, so update the global
2832 location list. Explicitly tell ugll to insert locations and
2833 ignore breakpoints_always_inserted_mode. */
2834 update_global_location_list (UGLL_INSERT);
2835 }
2836
2837 /* Invoke CALLBACK for each of bp_location. */
2838
2839 void
2840 iterate_over_bp_locations (walk_bp_location_callback callback)
2841 {
2842 struct bp_location *loc, **loc_tmp;
2843
2844 ALL_BP_LOCATIONS (loc, loc_tmp)
2845 {
2846 callback (loc, NULL);
2847 }
2848 }
2849
2850 /* This is used when we need to synch breakpoint conditions between GDB and the
2851 target. It is the case with deleting and disabling of breakpoints when using
2852 always-inserted mode. */
2853
2854 static void
2855 update_inserted_breakpoint_locations (void)
2856 {
2857 struct bp_location *bl, **blp_tmp;
2858 int error_flag = 0;
2859 int val = 0;
2860 int disabled_breaks = 0;
2861 int hw_breakpoint_error = 0;
2862 int hw_bp_details_reported = 0;
2863
2864 string_file tmp_error_stream;
2865
2866 /* Explicitly mark the warning -- this will only be printed if
2867 there was an error. */
2868 tmp_error_stream.puts ("Warning:\n");
2869
2870 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2871
2872 ALL_BP_LOCATIONS (bl, blp_tmp)
2873 {
2874 /* We only want to update software breakpoints and hardware
2875 breakpoints. */
2876 if (!is_breakpoint (bl->owner))
2877 continue;
2878
2879 /* We only want to update locations that are already inserted
2880 and need updating. This is to avoid unwanted insertion during
2881 deletion of breakpoints. */
2882 if (!bl->inserted || !bl->needs_update)
2883 continue;
2884
2885 switch_to_program_space_and_thread (bl->pspace);
2886
2887 /* For targets that support global breakpoints, there's no need
2888 to select an inferior to insert breakpoint to. In fact, even
2889 if we aren't attached to any process yet, we should still
2890 insert breakpoints. */
2891 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2892 && (inferior_ptid == null_ptid || !target_has_execution))
2893 continue;
2894
2895 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2896 &hw_breakpoint_error, &hw_bp_details_reported);
2897 if (val)
2898 error_flag = val;
2899 }
2900
2901 if (error_flag)
2902 {
2903 target_terminal::ours_for_output ();
2904 error_stream (tmp_error_stream);
2905 }
2906 }
2907
2908 /* Used when starting or continuing the program. */
2909
2910 static void
2911 insert_breakpoint_locations (void)
2912 {
2913 struct breakpoint *bpt;
2914 struct bp_location *bl, **blp_tmp;
2915 int error_flag = 0;
2916 int val = 0;
2917 int disabled_breaks = 0;
2918 int hw_breakpoint_error = 0;
2919 int hw_bp_error_explained_already = 0;
2920
2921 string_file tmp_error_stream;
2922
2923 /* Explicitly mark the warning -- this will only be printed if
2924 there was an error. */
2925 tmp_error_stream.puts ("Warning:\n");
2926
2927 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2928
2929 ALL_BP_LOCATIONS (bl, blp_tmp)
2930 {
2931 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2932 continue;
2933
2934 /* There is no point inserting thread-specific breakpoints if
2935 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2936 has BL->OWNER always non-NULL. */
2937 if (bl->owner->thread != -1
2938 && !valid_global_thread_id (bl->owner->thread))
2939 continue;
2940
2941 switch_to_program_space_and_thread (bl->pspace);
2942
2943 /* For targets that support global breakpoints, there's no need
2944 to select an inferior to insert breakpoint to. In fact, even
2945 if we aren't attached to any process yet, we should still
2946 insert breakpoints. */
2947 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2948 && (inferior_ptid == null_ptid || !target_has_execution))
2949 continue;
2950
2951 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2952 &hw_breakpoint_error, &hw_bp_error_explained_already);
2953 if (val)
2954 error_flag = val;
2955 }
2956
2957 /* If we failed to insert all locations of a watchpoint, remove
2958 them, as half-inserted watchpoint is of limited use. */
2959 ALL_BREAKPOINTS (bpt)
2960 {
2961 int some_failed = 0;
2962 struct bp_location *loc;
2963
2964 if (!is_hardware_watchpoint (bpt))
2965 continue;
2966
2967 if (!breakpoint_enabled (bpt))
2968 continue;
2969
2970 if (bpt->disposition == disp_del_at_next_stop)
2971 continue;
2972
2973 for (loc = bpt->loc; loc; loc = loc->next)
2974 if (!loc->inserted && should_be_inserted (loc))
2975 {
2976 some_failed = 1;
2977 break;
2978 }
2979 if (some_failed)
2980 {
2981 for (loc = bpt->loc; loc; loc = loc->next)
2982 if (loc->inserted)
2983 remove_breakpoint (loc);
2984
2985 hw_breakpoint_error = 1;
2986 tmp_error_stream.printf ("Could not insert "
2987 "hardware watchpoint %d.\n",
2988 bpt->number);
2989 error_flag = -1;
2990 }
2991 }
2992
2993 if (error_flag)
2994 {
2995 /* If a hardware breakpoint or watchpoint was inserted, add a
2996 message about possibly exhausted resources. */
2997 if (hw_breakpoint_error && !hw_bp_error_explained_already)
2998 {
2999 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3000 You may have requested too many hardware breakpoints/watchpoints.\n");
3001 }
3002 target_terminal::ours_for_output ();
3003 error_stream (tmp_error_stream);
3004 }
3005 }
3006
3007 /* Used when the program stops.
3008 Returns zero if successful, or non-zero if there was a problem
3009 removing a breakpoint location. */
3010
3011 int
3012 remove_breakpoints (void)
3013 {
3014 struct bp_location *bl, **blp_tmp;
3015 int val = 0;
3016
3017 ALL_BP_LOCATIONS (bl, blp_tmp)
3018 {
3019 if (bl->inserted && !is_tracepoint (bl->owner))
3020 val |= remove_breakpoint (bl);
3021 }
3022 return val;
3023 }
3024
3025 /* When a thread exits, remove breakpoints that are related to
3026 that thread. */
3027
3028 static void
3029 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3030 {
3031 struct breakpoint *b, *b_tmp;
3032
3033 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3034 {
3035 if (b->thread == tp->global_num && user_breakpoint_p (b))
3036 {
3037 b->disposition = disp_del_at_next_stop;
3038
3039 printf_filtered (_("\
3040 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3041 b->number, print_thread_id (tp));
3042
3043 /* Hide it from the user. */
3044 b->number = 0;
3045 }
3046 }
3047 }
3048
3049 /* See breakpoint.h. */
3050
3051 void
3052 remove_breakpoints_inf (inferior *inf)
3053 {
3054 struct bp_location *bl, **blp_tmp;
3055 int val;
3056
3057 ALL_BP_LOCATIONS (bl, blp_tmp)
3058 {
3059 if (bl->pspace != inf->pspace)
3060 continue;
3061
3062 if (bl->inserted && !bl->target_info.persist)
3063 {
3064 val = remove_breakpoint (bl);
3065 if (val != 0)
3066 return;
3067 }
3068 }
3069 }
3070
3071 static int internal_breakpoint_number = -1;
3072
3073 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3074 If INTERNAL is non-zero, the breakpoint number will be populated
3075 from internal_breakpoint_number and that variable decremented.
3076 Otherwise the breakpoint number will be populated from
3077 breakpoint_count and that value incremented. Internal breakpoints
3078 do not set the internal var bpnum. */
3079 static void
3080 set_breakpoint_number (int internal, struct breakpoint *b)
3081 {
3082 if (internal)
3083 b->number = internal_breakpoint_number--;
3084 else
3085 {
3086 set_breakpoint_count (breakpoint_count + 1);
3087 b->number = breakpoint_count;
3088 }
3089 }
3090
3091 static struct breakpoint *
3092 create_internal_breakpoint (struct gdbarch *gdbarch,
3093 CORE_ADDR address, enum bptype type,
3094 const struct breakpoint_ops *ops)
3095 {
3096 symtab_and_line sal;
3097 sal.pc = address;
3098 sal.section = find_pc_overlay (sal.pc);
3099 sal.pspace = current_program_space;
3100
3101 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3102 b->number = internal_breakpoint_number--;
3103 b->disposition = disp_donttouch;
3104
3105 return b;
3106 }
3107
3108 static const char *const longjmp_names[] =
3109 {
3110 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3111 };
3112 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3113
3114 /* Per-objfile data private to breakpoint.c. */
3115 struct breakpoint_objfile_data
3116 {
3117 /* Minimal symbol for "_ovly_debug_event" (if any). */
3118 struct bound_minimal_symbol overlay_msym {};
3119
3120 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3121 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3122
3123 /* True if we have looked for longjmp probes. */
3124 int longjmp_searched = 0;
3125
3126 /* SystemTap probe points for longjmp (if any). These are non-owning
3127 references. */
3128 std::vector<probe *> longjmp_probes;
3129
3130 /* Minimal symbol for "std::terminate()" (if any). */
3131 struct bound_minimal_symbol terminate_msym {};
3132
3133 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3134 struct bound_minimal_symbol exception_msym {};
3135
3136 /* True if we have looked for exception probes. */
3137 int exception_searched = 0;
3138
3139 /* SystemTap probe points for unwinding (if any). These are non-owning
3140 references. */
3141 std::vector<probe *> exception_probes;
3142 };
3143
3144 static const struct objfile_key<breakpoint_objfile_data>
3145 breakpoint_objfile_key;
3146
3147 /* Minimal symbol not found sentinel. */
3148 static struct minimal_symbol msym_not_found;
3149
3150 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3151
3152 static int
3153 msym_not_found_p (const struct minimal_symbol *msym)
3154 {
3155 return msym == &msym_not_found;
3156 }
3157
3158 /* Return per-objfile data needed by breakpoint.c.
3159 Allocate the data if necessary. */
3160
3161 static struct breakpoint_objfile_data *
3162 get_breakpoint_objfile_data (struct objfile *objfile)
3163 {
3164 struct breakpoint_objfile_data *bp_objfile_data;
3165
3166 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3167 if (bp_objfile_data == NULL)
3168 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3169 return bp_objfile_data;
3170 }
3171
3172 static void
3173 create_overlay_event_breakpoint (void)
3174 {
3175 const char *const func_name = "_ovly_debug_event";
3176
3177 for (objfile *objfile : current_program_space->objfiles ())
3178 {
3179 struct breakpoint *b;
3180 struct breakpoint_objfile_data *bp_objfile_data;
3181 CORE_ADDR addr;
3182 struct explicit_location explicit_loc;
3183
3184 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3185
3186 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3187 continue;
3188
3189 if (bp_objfile_data->overlay_msym.minsym == NULL)
3190 {
3191 struct bound_minimal_symbol m;
3192
3193 m = lookup_minimal_symbol_text (func_name, objfile);
3194 if (m.minsym == NULL)
3195 {
3196 /* Avoid future lookups in this objfile. */
3197 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3198 continue;
3199 }
3200 bp_objfile_data->overlay_msym = m;
3201 }
3202
3203 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3204 b = create_internal_breakpoint (objfile->arch (), addr,
3205 bp_overlay_event,
3206 &internal_breakpoint_ops);
3207 initialize_explicit_location (&explicit_loc);
3208 explicit_loc.function_name = ASTRDUP (func_name);
3209 b->location = new_explicit_location (&explicit_loc);
3210
3211 if (overlay_debugging == ovly_auto)
3212 {
3213 b->enable_state = bp_enabled;
3214 overlay_events_enabled = 1;
3215 }
3216 else
3217 {
3218 b->enable_state = bp_disabled;
3219 overlay_events_enabled = 0;
3220 }
3221 }
3222 }
3223
3224 static void
3225 create_longjmp_master_breakpoint (void)
3226 {
3227 scoped_restore_current_program_space restore_pspace;
3228
3229 for (struct program_space *pspace : program_spaces)
3230 {
3231 set_current_program_space (pspace);
3232
3233 for (objfile *objfile : current_program_space->objfiles ())
3234 {
3235 int i;
3236 struct gdbarch *gdbarch;
3237 struct breakpoint_objfile_data *bp_objfile_data;
3238
3239 gdbarch = objfile->arch ();
3240
3241 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3242
3243 if (!bp_objfile_data->longjmp_searched)
3244 {
3245 std::vector<probe *> ret
3246 = find_probes_in_objfile (objfile, "libc", "longjmp");
3247
3248 if (!ret.empty ())
3249 {
3250 /* We are only interested in checking one element. */
3251 probe *p = ret[0];
3252
3253 if (!p->can_evaluate_arguments ())
3254 {
3255 /* We cannot use the probe interface here,
3256 because it does not know how to evaluate
3257 arguments. */
3258 ret.clear ();
3259 }
3260 }
3261 bp_objfile_data->longjmp_probes = ret;
3262 bp_objfile_data->longjmp_searched = 1;
3263 }
3264
3265 if (!bp_objfile_data->longjmp_probes.empty ())
3266 {
3267 for (probe *p : bp_objfile_data->longjmp_probes)
3268 {
3269 struct breakpoint *b;
3270
3271 b = create_internal_breakpoint (gdbarch,
3272 p->get_relocated_address (objfile),
3273 bp_longjmp_master,
3274 &internal_breakpoint_ops);
3275 b->location = new_probe_location ("-probe-stap libc:longjmp");
3276 b->enable_state = bp_disabled;
3277 }
3278
3279 continue;
3280 }
3281
3282 if (!gdbarch_get_longjmp_target_p (gdbarch))
3283 continue;
3284
3285 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3286 {
3287 struct breakpoint *b;
3288 const char *func_name;
3289 CORE_ADDR addr;
3290 struct explicit_location explicit_loc;
3291
3292 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3293 continue;
3294
3295 func_name = longjmp_names[i];
3296 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3297 {
3298 struct bound_minimal_symbol m;
3299
3300 m = lookup_minimal_symbol_text (func_name, objfile);
3301 if (m.minsym == NULL)
3302 {
3303 /* Prevent future lookups in this objfile. */
3304 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3305 continue;
3306 }
3307 bp_objfile_data->longjmp_msym[i] = m;
3308 }
3309
3310 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3311 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3312 &internal_breakpoint_ops);
3313 initialize_explicit_location (&explicit_loc);
3314 explicit_loc.function_name = ASTRDUP (func_name);
3315 b->location = new_explicit_location (&explicit_loc);
3316 b->enable_state = bp_disabled;
3317 }
3318 }
3319 }
3320 }
3321
3322 /* Create a master std::terminate breakpoint. */
3323 static void
3324 create_std_terminate_master_breakpoint (void)
3325 {
3326 const char *const func_name = "std::terminate()";
3327
3328 scoped_restore_current_program_space restore_pspace;
3329
3330 for (struct program_space *pspace : program_spaces)
3331 {
3332 CORE_ADDR addr;
3333
3334 set_current_program_space (pspace);
3335
3336 for (objfile *objfile : current_program_space->objfiles ())
3337 {
3338 struct breakpoint *b;
3339 struct breakpoint_objfile_data *bp_objfile_data;
3340 struct explicit_location explicit_loc;
3341
3342 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3343
3344 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3345 continue;
3346
3347 if (bp_objfile_data->terminate_msym.minsym == NULL)
3348 {
3349 struct bound_minimal_symbol m;
3350
3351 m = lookup_minimal_symbol (func_name, NULL, objfile);
3352 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3353 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3354 {
3355 /* Prevent future lookups in this objfile. */
3356 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3357 continue;
3358 }
3359 bp_objfile_data->terminate_msym = m;
3360 }
3361
3362 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3363 b = create_internal_breakpoint (objfile->arch (), addr,
3364 bp_std_terminate_master,
3365 &internal_breakpoint_ops);
3366 initialize_explicit_location (&explicit_loc);
3367 explicit_loc.function_name = ASTRDUP (func_name);
3368 b->location = new_explicit_location (&explicit_loc);
3369 b->enable_state = bp_disabled;
3370 }
3371 }
3372 }
3373
3374 /* Install a master breakpoint on the unwinder's debug hook. */
3375
3376 static void
3377 create_exception_master_breakpoint (void)
3378 {
3379 const char *const func_name = "_Unwind_DebugHook";
3380
3381 for (objfile *objfile : current_program_space->objfiles ())
3382 {
3383 struct breakpoint *b;
3384 struct gdbarch *gdbarch;
3385 struct breakpoint_objfile_data *bp_objfile_data;
3386 CORE_ADDR addr;
3387 struct explicit_location explicit_loc;
3388
3389 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3390
3391 /* We prefer the SystemTap probe point if it exists. */
3392 if (!bp_objfile_data->exception_searched)
3393 {
3394 std::vector<probe *> ret
3395 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3396
3397 if (!ret.empty ())
3398 {
3399 /* We are only interested in checking one element. */
3400 probe *p = ret[0];
3401
3402 if (!p->can_evaluate_arguments ())
3403 {
3404 /* We cannot use the probe interface here, because it does
3405 not know how to evaluate arguments. */
3406 ret.clear ();
3407 }
3408 }
3409 bp_objfile_data->exception_probes = ret;
3410 bp_objfile_data->exception_searched = 1;
3411 }
3412
3413 if (!bp_objfile_data->exception_probes.empty ())
3414 {
3415 gdbarch = objfile->arch ();
3416
3417 for (probe *p : bp_objfile_data->exception_probes)
3418 {
3419 b = create_internal_breakpoint (gdbarch,
3420 p->get_relocated_address (objfile),
3421 bp_exception_master,
3422 &internal_breakpoint_ops);
3423 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3424 b->enable_state = bp_disabled;
3425 }
3426
3427 continue;
3428 }
3429
3430 /* Otherwise, try the hook function. */
3431
3432 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3433 continue;
3434
3435 gdbarch = objfile->arch ();
3436
3437 if (bp_objfile_data->exception_msym.minsym == NULL)
3438 {
3439 struct bound_minimal_symbol debug_hook;
3440
3441 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3442 if (debug_hook.minsym == NULL)
3443 {
3444 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3445 continue;
3446 }
3447
3448 bp_objfile_data->exception_msym = debug_hook;
3449 }
3450
3451 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3452 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3453 current_top_target ());
3454 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3455 &internal_breakpoint_ops);
3456 initialize_explicit_location (&explicit_loc);
3457 explicit_loc.function_name = ASTRDUP (func_name);
3458 b->location = new_explicit_location (&explicit_loc);
3459 b->enable_state = bp_disabled;
3460 }
3461 }
3462
3463 /* Does B have a location spec? */
3464
3465 static int
3466 breakpoint_event_location_empty_p (const struct breakpoint *b)
3467 {
3468 return b->location != NULL && event_location_empty_p (b->location.get ());
3469 }
3470
3471 void
3472 update_breakpoints_after_exec (void)
3473 {
3474 struct breakpoint *b, *b_tmp;
3475 struct bp_location *bploc, **bplocp_tmp;
3476
3477 /* We're about to delete breakpoints from GDB's lists. If the
3478 INSERTED flag is true, GDB will try to lift the breakpoints by
3479 writing the breakpoints' "shadow contents" back into memory. The
3480 "shadow contents" are NOT valid after an exec, so GDB should not
3481 do that. Instead, the target is responsible from marking
3482 breakpoints out as soon as it detects an exec. We don't do that
3483 here instead, because there may be other attempts to delete
3484 breakpoints after detecting an exec and before reaching here. */
3485 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3486 if (bploc->pspace == current_program_space)
3487 gdb_assert (!bploc->inserted);
3488
3489 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3490 {
3491 if (b->pspace != current_program_space)
3492 continue;
3493
3494 /* Solib breakpoints must be explicitly reset after an exec(). */
3495 if (b->type == bp_shlib_event)
3496 {
3497 delete_breakpoint (b);
3498 continue;
3499 }
3500
3501 /* JIT breakpoints must be explicitly reset after an exec(). */
3502 if (b->type == bp_jit_event)
3503 {
3504 delete_breakpoint (b);
3505 continue;
3506 }
3507
3508 /* Thread event breakpoints must be set anew after an exec(),
3509 as must overlay event and longjmp master breakpoints. */
3510 if (b->type == bp_thread_event || b->type == bp_overlay_event
3511 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3512 || b->type == bp_exception_master)
3513 {
3514 delete_breakpoint (b);
3515 continue;
3516 }
3517
3518 /* Step-resume breakpoints are meaningless after an exec(). */
3519 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3520 {
3521 delete_breakpoint (b);
3522 continue;
3523 }
3524
3525 /* Just like single-step breakpoints. */
3526 if (b->type == bp_single_step)
3527 {
3528 delete_breakpoint (b);
3529 continue;
3530 }
3531
3532 /* Longjmp and longjmp-resume breakpoints are also meaningless
3533 after an exec. */
3534 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3535 || b->type == bp_longjmp_call_dummy
3536 || b->type == bp_exception || b->type == bp_exception_resume)
3537 {
3538 delete_breakpoint (b);
3539 continue;
3540 }
3541
3542 if (b->type == bp_catchpoint)
3543 {
3544 /* For now, none of the bp_catchpoint breakpoints need to
3545 do anything at this point. In the future, if some of
3546 the catchpoints need to something, we will need to add
3547 a new method, and call this method from here. */
3548 continue;
3549 }
3550
3551 /* bp_finish is a special case. The only way we ought to be able
3552 to see one of these when an exec() has happened, is if the user
3553 caught a vfork, and then said "finish". Ordinarily a finish just
3554 carries them to the call-site of the current callee, by setting
3555 a temporary bp there and resuming. But in this case, the finish
3556 will carry them entirely through the vfork & exec.
3557
3558 We don't want to allow a bp_finish to remain inserted now. But
3559 we can't safely delete it, 'cause finish_command has a handle to
3560 the bp on a bpstat, and will later want to delete it. There's a
3561 chance (and I've seen it happen) that if we delete the bp_finish
3562 here, that its storage will get reused by the time finish_command
3563 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3564 We really must allow finish_command to delete a bp_finish.
3565
3566 In the absence of a general solution for the "how do we know
3567 it's safe to delete something others may have handles to?"
3568 problem, what we'll do here is just uninsert the bp_finish, and
3569 let finish_command delete it.
3570
3571 (We know the bp_finish is "doomed" in the sense that it's
3572 momentary, and will be deleted as soon as finish_command sees
3573 the inferior stopped. So it doesn't matter that the bp's
3574 address is probably bogus in the new a.out, unlike e.g., the
3575 solib breakpoints.) */
3576
3577 if (b->type == bp_finish)
3578 {
3579 continue;
3580 }
3581
3582 /* Without a symbolic address, we have little hope of the
3583 pre-exec() address meaning the same thing in the post-exec()
3584 a.out. */
3585 if (breakpoint_event_location_empty_p (b))
3586 {
3587 delete_breakpoint (b);
3588 continue;
3589 }
3590 }
3591 }
3592
3593 int
3594 detach_breakpoints (ptid_t ptid)
3595 {
3596 struct bp_location *bl, **blp_tmp;
3597 int val = 0;
3598 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3599 struct inferior *inf = current_inferior ();
3600
3601 if (ptid.pid () == inferior_ptid.pid ())
3602 error (_("Cannot detach breakpoints of inferior_ptid"));
3603
3604 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3605 inferior_ptid = ptid;
3606 ALL_BP_LOCATIONS (bl, blp_tmp)
3607 {
3608 if (bl->pspace != inf->pspace)
3609 continue;
3610
3611 /* This function must physically remove breakpoints locations
3612 from the specified ptid, without modifying the breakpoint
3613 package's state. Locations of type bp_loc_other are only
3614 maintained at GDB side. So, there is no need to remove
3615 these bp_loc_other locations. Moreover, removing these
3616 would modify the breakpoint package's state. */
3617 if (bl->loc_type == bp_loc_other)
3618 continue;
3619
3620 if (bl->inserted)
3621 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3622 }
3623
3624 return val;
3625 }
3626
3627 /* Remove the breakpoint location BL from the current address space.
3628 Note that this is used to detach breakpoints from a child fork.
3629 When we get here, the child isn't in the inferior list, and neither
3630 do we have objects to represent its address space --- we should
3631 *not* look at bl->pspace->aspace here. */
3632
3633 static int
3634 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3635 {
3636 int val;
3637
3638 /* BL is never in moribund_locations by our callers. */
3639 gdb_assert (bl->owner != NULL);
3640
3641 /* The type of none suggests that owner is actually deleted.
3642 This should not ever happen. */
3643 gdb_assert (bl->owner->type != bp_none);
3644
3645 if (bl->loc_type == bp_loc_software_breakpoint
3646 || bl->loc_type == bp_loc_hardware_breakpoint)
3647 {
3648 /* "Normal" instruction breakpoint: either the standard
3649 trap-instruction bp (bp_breakpoint), or a
3650 bp_hardware_breakpoint. */
3651
3652 /* First check to see if we have to handle an overlay. */
3653 if (overlay_debugging == ovly_off
3654 || bl->section == NULL
3655 || !(section_is_overlay (bl->section)))
3656 {
3657 /* No overlay handling: just remove the breakpoint. */
3658
3659 /* If we're trying to uninsert a memory breakpoint that we
3660 know is set in a dynamic object that is marked
3661 shlib_disabled, then either the dynamic object was
3662 removed with "remove-symbol-file" or with
3663 "nosharedlibrary". In the former case, we don't know
3664 whether another dynamic object might have loaded over the
3665 breakpoint's address -- the user might well let us know
3666 about it next with add-symbol-file (the whole point of
3667 add-symbol-file is letting the user manually maintain a
3668 list of dynamically loaded objects). If we have the
3669 breakpoint's shadow memory, that is, this is a software
3670 breakpoint managed by GDB, check whether the breakpoint
3671 is still inserted in memory, to avoid overwriting wrong
3672 code with stale saved shadow contents. Note that HW
3673 breakpoints don't have shadow memory, as they're
3674 implemented using a mechanism that is not dependent on
3675 being able to modify the target's memory, and as such
3676 they should always be removed. */
3677 if (bl->shlib_disabled
3678 && bl->target_info.shadow_len != 0
3679 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3680 val = 0;
3681 else
3682 val = bl->owner->ops->remove_location (bl, reason);
3683 }
3684 else
3685 {
3686 /* This breakpoint is in an overlay section.
3687 Did we set a breakpoint at the LMA? */
3688 if (!overlay_events_enabled)
3689 {
3690 /* Yes -- overlay event support is not active, so we
3691 should have set a breakpoint at the LMA. Remove it.
3692 */
3693 /* Ignore any failures: if the LMA is in ROM, we will
3694 have already warned when we failed to insert it. */
3695 if (bl->loc_type == bp_loc_hardware_breakpoint)
3696 target_remove_hw_breakpoint (bl->gdbarch,
3697 &bl->overlay_target_info);
3698 else
3699 target_remove_breakpoint (bl->gdbarch,
3700 &bl->overlay_target_info,
3701 reason);
3702 }
3703 /* Did we set a breakpoint at the VMA?
3704 If so, we will have marked the breakpoint 'inserted'. */
3705 if (bl->inserted)
3706 {
3707 /* Yes -- remove it. Previously we did not bother to
3708 remove the breakpoint if the section had been
3709 unmapped, but let's not rely on that being safe. We
3710 don't know what the overlay manager might do. */
3711
3712 /* However, we should remove *software* breakpoints only
3713 if the section is still mapped, or else we overwrite
3714 wrong code with the saved shadow contents. */
3715 if (bl->loc_type == bp_loc_hardware_breakpoint
3716 || section_is_mapped (bl->section))
3717 val = bl->owner->ops->remove_location (bl, reason);
3718 else
3719 val = 0;
3720 }
3721 else
3722 {
3723 /* No -- not inserted, so no need to remove. No error. */
3724 val = 0;
3725 }
3726 }
3727
3728 /* In some cases, we might not be able to remove a breakpoint in
3729 a shared library that has already been removed, but we have
3730 not yet processed the shlib unload event. Similarly for an
3731 unloaded add-symbol-file object - the user might not yet have
3732 had the chance to remove-symbol-file it. shlib_disabled will
3733 be set if the library/object has already been removed, but
3734 the breakpoint hasn't been uninserted yet, e.g., after
3735 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3736 always-inserted mode. */
3737 if (val
3738 && (bl->loc_type == bp_loc_software_breakpoint
3739 && (bl->shlib_disabled
3740 || solib_name_from_address (bl->pspace, bl->address)
3741 || shared_objfile_contains_address_p (bl->pspace,
3742 bl->address))))
3743 val = 0;
3744
3745 if (val)
3746 return val;
3747 bl->inserted = (reason == DETACH_BREAKPOINT);
3748 }
3749 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3750 {
3751 gdb_assert (bl->owner->ops != NULL
3752 && bl->owner->ops->remove_location != NULL);
3753
3754 bl->inserted = (reason == DETACH_BREAKPOINT);
3755 bl->owner->ops->remove_location (bl, reason);
3756
3757 /* Failure to remove any of the hardware watchpoints comes here. */
3758 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3759 warning (_("Could not remove hardware watchpoint %d."),
3760 bl->owner->number);
3761 }
3762 else if (bl->owner->type == bp_catchpoint
3763 && breakpoint_enabled (bl->owner)
3764 && !bl->duplicate)
3765 {
3766 gdb_assert (bl->owner->ops != NULL
3767 && bl->owner->ops->remove_location != NULL);
3768
3769 val = bl->owner->ops->remove_location (bl, reason);
3770 if (val)
3771 return val;
3772
3773 bl->inserted = (reason == DETACH_BREAKPOINT);
3774 }
3775
3776 return 0;
3777 }
3778
3779 static int
3780 remove_breakpoint (struct bp_location *bl)
3781 {
3782 /* BL is never in moribund_locations by our callers. */
3783 gdb_assert (bl->owner != NULL);
3784
3785 /* The type of none suggests that owner is actually deleted.
3786 This should not ever happen. */
3787 gdb_assert (bl->owner->type != bp_none);
3788
3789 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3790
3791 switch_to_program_space_and_thread (bl->pspace);
3792
3793 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3794 }
3795
3796 /* Clear the "inserted" flag in all breakpoints. */
3797
3798 void
3799 mark_breakpoints_out (void)
3800 {
3801 struct bp_location *bl, **blp_tmp;
3802
3803 ALL_BP_LOCATIONS (bl, blp_tmp)
3804 if (bl->pspace == current_program_space)
3805 bl->inserted = 0;
3806 }
3807
3808 /* Clear the "inserted" flag in all breakpoints and delete any
3809 breakpoints which should go away between runs of the program.
3810
3811 Plus other such housekeeping that has to be done for breakpoints
3812 between runs.
3813
3814 Note: this function gets called at the end of a run (by
3815 generic_mourn_inferior) and when a run begins (by
3816 init_wait_for_inferior). */
3817
3818
3819
3820 void
3821 breakpoint_init_inferior (enum inf_context context)
3822 {
3823 struct breakpoint *b, *b_tmp;
3824 struct program_space *pspace = current_program_space;
3825
3826 /* If breakpoint locations are shared across processes, then there's
3827 nothing to do. */
3828 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3829 return;
3830
3831 mark_breakpoints_out ();
3832
3833 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3834 {
3835 if (b->loc && b->loc->pspace != pspace)
3836 continue;
3837
3838 switch (b->type)
3839 {
3840 case bp_call_dummy:
3841 case bp_longjmp_call_dummy:
3842
3843 /* If the call dummy breakpoint is at the entry point it will
3844 cause problems when the inferior is rerun, so we better get
3845 rid of it. */
3846
3847 case bp_watchpoint_scope:
3848
3849 /* Also get rid of scope breakpoints. */
3850
3851 case bp_shlib_event:
3852
3853 /* Also remove solib event breakpoints. Their addresses may
3854 have changed since the last time we ran the program.
3855 Actually we may now be debugging against different target;
3856 and so the solib backend that installed this breakpoint may
3857 not be used in by the target. E.g.,
3858
3859 (gdb) file prog-linux
3860 (gdb) run # native linux target
3861 ...
3862 (gdb) kill
3863 (gdb) file prog-win.exe
3864 (gdb) tar rem :9999 # remote Windows gdbserver.
3865 */
3866
3867 case bp_step_resume:
3868
3869 /* Also remove step-resume breakpoints. */
3870
3871 case bp_single_step:
3872
3873 /* Also remove single-step breakpoints. */
3874
3875 delete_breakpoint (b);
3876 break;
3877
3878 case bp_watchpoint:
3879 case bp_hardware_watchpoint:
3880 case bp_read_watchpoint:
3881 case bp_access_watchpoint:
3882 {
3883 struct watchpoint *w = (struct watchpoint *) b;
3884
3885 /* Likewise for watchpoints on local expressions. */
3886 if (w->exp_valid_block != NULL)
3887 delete_breakpoint (b);
3888 else
3889 {
3890 /* Get rid of existing locations, which are no longer
3891 valid. New ones will be created in
3892 update_watchpoint, when the inferior is restarted.
3893 The next update_global_location_list call will
3894 garbage collect them. */
3895 b->loc = NULL;
3896
3897 if (context == inf_starting)
3898 {
3899 /* Reset val field to force reread of starting value in
3900 insert_breakpoints. */
3901 w->val.reset (nullptr);
3902 w->val_valid = false;
3903 }
3904 }
3905 }
3906 break;
3907 default:
3908 break;
3909 }
3910 }
3911
3912 /* Get rid of the moribund locations. */
3913 for (bp_location *bl : moribund_locations)
3914 decref_bp_location (&bl);
3915 moribund_locations.clear ();
3916 }
3917
3918 /* These functions concern about actual breakpoints inserted in the
3919 target --- to e.g. check if we need to do decr_pc adjustment or if
3920 we need to hop over the bkpt --- so we check for address space
3921 match, not program space. */
3922
3923 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3924 exists at PC. It returns ordinary_breakpoint_here if it's an
3925 ordinary breakpoint, or permanent_breakpoint_here if it's a
3926 permanent breakpoint.
3927 - When continuing from a location with an ordinary breakpoint, we
3928 actually single step once before calling insert_breakpoints.
3929 - When continuing from a location with a permanent breakpoint, we
3930 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3931 the target, to advance the PC past the breakpoint. */
3932
3933 enum breakpoint_here
3934 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3935 {
3936 struct bp_location *bl, **blp_tmp;
3937 int any_breakpoint_here = 0;
3938
3939 ALL_BP_LOCATIONS (bl, blp_tmp)
3940 {
3941 if (bl->loc_type != bp_loc_software_breakpoint
3942 && bl->loc_type != bp_loc_hardware_breakpoint)
3943 continue;
3944
3945 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3946 if ((breakpoint_enabled (bl->owner)
3947 || bl->permanent)
3948 && breakpoint_location_address_match (bl, aspace, pc))
3949 {
3950 if (overlay_debugging
3951 && section_is_overlay (bl->section)
3952 && !section_is_mapped (bl->section))
3953 continue; /* unmapped overlay -- can't be a match */
3954 else if (bl->permanent)
3955 return permanent_breakpoint_here;
3956 else
3957 any_breakpoint_here = 1;
3958 }
3959 }
3960
3961 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
3962 }
3963
3964 /* See breakpoint.h. */
3965
3966 int
3967 breakpoint_in_range_p (const address_space *aspace,
3968 CORE_ADDR addr, ULONGEST len)
3969 {
3970 struct bp_location *bl, **blp_tmp;
3971
3972 ALL_BP_LOCATIONS (bl, blp_tmp)
3973 {
3974 if (bl->loc_type != bp_loc_software_breakpoint
3975 && bl->loc_type != bp_loc_hardware_breakpoint)
3976 continue;
3977
3978 if ((breakpoint_enabled (bl->owner)
3979 || bl->permanent)
3980 && breakpoint_location_address_range_overlap (bl, aspace,
3981 addr, len))
3982 {
3983 if (overlay_debugging
3984 && section_is_overlay (bl->section)
3985 && !section_is_mapped (bl->section))
3986 {
3987 /* Unmapped overlay -- can't be a match. */
3988 continue;
3989 }
3990
3991 return 1;
3992 }
3993 }
3994
3995 return 0;
3996 }
3997
3998 /* Return true if there's a moribund breakpoint at PC. */
3999
4000 int
4001 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4002 {
4003 for (bp_location *loc : moribund_locations)
4004 if (breakpoint_location_address_match (loc, aspace, pc))
4005 return 1;
4006
4007 return 0;
4008 }
4009
4010 /* Returns non-zero iff BL is inserted at PC, in address space
4011 ASPACE. */
4012
4013 static int
4014 bp_location_inserted_here_p (struct bp_location *bl,
4015 const address_space *aspace, CORE_ADDR pc)
4016 {
4017 if (bl->inserted
4018 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4019 aspace, pc))
4020 {
4021 if (overlay_debugging
4022 && section_is_overlay (bl->section)
4023 && !section_is_mapped (bl->section))
4024 return 0; /* unmapped overlay -- can't be a match */
4025 else
4026 return 1;
4027 }
4028 return 0;
4029 }
4030
4031 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4032
4033 int
4034 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4035 {
4036 struct bp_location **blp, **blp_tmp = NULL;
4037
4038 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4039 {
4040 struct bp_location *bl = *blp;
4041
4042 if (bl->loc_type != bp_loc_software_breakpoint
4043 && bl->loc_type != bp_loc_hardware_breakpoint)
4044 continue;
4045
4046 if (bp_location_inserted_here_p (bl, aspace, pc))
4047 return 1;
4048 }
4049 return 0;
4050 }
4051
4052 /* This function returns non-zero iff there is a software breakpoint
4053 inserted at PC. */
4054
4055 int
4056 software_breakpoint_inserted_here_p (const address_space *aspace,
4057 CORE_ADDR pc)
4058 {
4059 struct bp_location **blp, **blp_tmp = NULL;
4060
4061 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4062 {
4063 struct bp_location *bl = *blp;
4064
4065 if (bl->loc_type != bp_loc_software_breakpoint)
4066 continue;
4067
4068 if (bp_location_inserted_here_p (bl, aspace, pc))
4069 return 1;
4070 }
4071
4072 return 0;
4073 }
4074
4075 /* See breakpoint.h. */
4076
4077 int
4078 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4079 CORE_ADDR pc)
4080 {
4081 struct bp_location **blp, **blp_tmp = NULL;
4082
4083 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4084 {
4085 struct bp_location *bl = *blp;
4086
4087 if (bl->loc_type != bp_loc_hardware_breakpoint)
4088 continue;
4089
4090 if (bp_location_inserted_here_p (bl, aspace, pc))
4091 return 1;
4092 }
4093
4094 return 0;
4095 }
4096
4097 int
4098 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4099 CORE_ADDR addr, ULONGEST len)
4100 {
4101 struct breakpoint *bpt;
4102
4103 ALL_BREAKPOINTS (bpt)
4104 {
4105 struct bp_location *loc;
4106
4107 if (bpt->type != bp_hardware_watchpoint
4108 && bpt->type != bp_access_watchpoint)
4109 continue;
4110
4111 if (!breakpoint_enabled (bpt))
4112 continue;
4113
4114 for (loc = bpt->loc; loc; loc = loc->next)
4115 if (loc->pspace->aspace == aspace && loc->inserted)
4116 {
4117 CORE_ADDR l, h;
4118
4119 /* Check for intersection. */
4120 l = std::max<CORE_ADDR> (loc->address, addr);
4121 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4122 if (l < h)
4123 return 1;
4124 }
4125 }
4126 return 0;
4127 }
4128
4129 /* See breakpoint.h. */
4130
4131 bool
4132 is_catchpoint (struct breakpoint *b)
4133 {
4134 return (b->type == bp_catchpoint);
4135 }
4136
4137 /* Frees any storage that is part of a bpstat. Does not walk the
4138 'next' chain. */
4139
4140 bpstats::~bpstats ()
4141 {
4142 if (bp_location_at != NULL)
4143 decref_bp_location (&bp_location_at);
4144 }
4145
4146 /* Clear a bpstat so that it says we are not at any breakpoint.
4147 Also free any storage that is part of a bpstat. */
4148
4149 void
4150 bpstat_clear (bpstat *bsp)
4151 {
4152 bpstat p;
4153 bpstat q;
4154
4155 if (bsp == 0)
4156 return;
4157 p = *bsp;
4158 while (p != NULL)
4159 {
4160 q = p->next;
4161 delete p;
4162 p = q;
4163 }
4164 *bsp = NULL;
4165 }
4166
4167 bpstats::bpstats (const bpstats &other)
4168 : next (NULL),
4169 bp_location_at (other.bp_location_at),
4170 breakpoint_at (other.breakpoint_at),
4171 commands (other.commands),
4172 print (other.print),
4173 stop (other.stop),
4174 print_it (other.print_it)
4175 {
4176 if (other.old_val != NULL)
4177 old_val = release_value (value_copy (other.old_val.get ()));
4178 incref_bp_location (bp_location_at);
4179 }
4180
4181 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4182 is part of the bpstat is copied as well. */
4183
4184 bpstat
4185 bpstat_copy (bpstat bs)
4186 {
4187 bpstat p = NULL;
4188 bpstat tmp;
4189 bpstat retval = NULL;
4190
4191 if (bs == NULL)
4192 return bs;
4193
4194 for (; bs != NULL; bs = bs->next)
4195 {
4196 tmp = new bpstats (*bs);
4197
4198 if (p == NULL)
4199 /* This is the first thing in the chain. */
4200 retval = tmp;
4201 else
4202 p->next = tmp;
4203 p = tmp;
4204 }
4205 p->next = NULL;
4206 return retval;
4207 }
4208
4209 /* Find the bpstat associated with this breakpoint. */
4210
4211 bpstat
4212 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4213 {
4214 if (bsp == NULL)
4215 return NULL;
4216
4217 for (; bsp != NULL; bsp = bsp->next)
4218 {
4219 if (bsp->breakpoint_at == breakpoint)
4220 return bsp;
4221 }
4222 return NULL;
4223 }
4224
4225 /* See breakpoint.h. */
4226
4227 bool
4228 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4229 {
4230 for (; bsp != NULL; bsp = bsp->next)
4231 {
4232 if (bsp->breakpoint_at == NULL)
4233 {
4234 /* A moribund location can never explain a signal other than
4235 GDB_SIGNAL_TRAP. */
4236 if (sig == GDB_SIGNAL_TRAP)
4237 return true;
4238 }
4239 else
4240 {
4241 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4242 sig))
4243 return true;
4244 }
4245 }
4246
4247 return false;
4248 }
4249
4250 /* Put in *NUM the breakpoint number of the first breakpoint we are
4251 stopped at. *BSP upon return is a bpstat which points to the
4252 remaining breakpoints stopped at (but which is not guaranteed to be
4253 good for anything but further calls to bpstat_num).
4254
4255 Return 0 if passed a bpstat which does not indicate any breakpoints.
4256 Return -1 if stopped at a breakpoint that has been deleted since
4257 we set it.
4258 Return 1 otherwise. */
4259
4260 int
4261 bpstat_num (bpstat *bsp, int *num)
4262 {
4263 struct breakpoint *b;
4264
4265 if ((*bsp) == NULL)
4266 return 0; /* No more breakpoint values */
4267
4268 /* We assume we'll never have several bpstats that correspond to a
4269 single breakpoint -- otherwise, this function might return the
4270 same number more than once and this will look ugly. */
4271 b = (*bsp)->breakpoint_at;
4272 *bsp = (*bsp)->next;
4273 if (b == NULL)
4274 return -1; /* breakpoint that's been deleted since */
4275
4276 *num = b->number; /* We have its number */
4277 return 1;
4278 }
4279
4280 /* See breakpoint.h. */
4281
4282 void
4283 bpstat_clear_actions (void)
4284 {
4285 bpstat bs;
4286
4287 if (inferior_ptid == null_ptid)
4288 return;
4289
4290 thread_info *tp = inferior_thread ();
4291 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4292 {
4293 bs->commands = NULL;
4294 bs->old_val.reset (nullptr);
4295 }
4296 }
4297
4298 /* Called when a command is about to proceed the inferior. */
4299
4300 static void
4301 breakpoint_about_to_proceed (void)
4302 {
4303 if (inferior_ptid != null_ptid)
4304 {
4305 struct thread_info *tp = inferior_thread ();
4306
4307 /* Allow inferior function calls in breakpoint commands to not
4308 interrupt the command list. When the call finishes
4309 successfully, the inferior will be standing at the same
4310 breakpoint as if nothing happened. */
4311 if (tp->control.in_infcall)
4312 return;
4313 }
4314
4315 breakpoint_proceeded = 1;
4316 }
4317
4318 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4319 or its equivalent. */
4320
4321 static int
4322 command_line_is_silent (struct command_line *cmd)
4323 {
4324 return cmd && (strcmp ("silent", cmd->line) == 0);
4325 }
4326
4327 /* Execute all the commands associated with all the breakpoints at
4328 this location. Any of these commands could cause the process to
4329 proceed beyond this point, etc. We look out for such changes by
4330 checking the global "breakpoint_proceeded" after each command.
4331
4332 Returns true if a breakpoint command resumed the inferior. In that
4333 case, it is the caller's responsibility to recall it again with the
4334 bpstat of the current thread. */
4335
4336 static int
4337 bpstat_do_actions_1 (bpstat *bsp)
4338 {
4339 bpstat bs;
4340 int again = 0;
4341
4342 /* Avoid endless recursion if a `source' command is contained
4343 in bs->commands. */
4344 if (executing_breakpoint_commands)
4345 return 0;
4346
4347 scoped_restore save_executing
4348 = make_scoped_restore (&executing_breakpoint_commands, 1);
4349
4350 scoped_restore preventer = prevent_dont_repeat ();
4351
4352 /* This pointer will iterate over the list of bpstat's. */
4353 bs = *bsp;
4354
4355 breakpoint_proceeded = 0;
4356 for (; bs != NULL; bs = bs->next)
4357 {
4358 struct command_line *cmd = NULL;
4359
4360 /* Take ownership of the BSP's command tree, if it has one.
4361
4362 The command tree could legitimately contain commands like
4363 'step' and 'next', which call clear_proceed_status, which
4364 frees stop_bpstat's command tree. To make sure this doesn't
4365 free the tree we're executing out from under us, we need to
4366 take ownership of the tree ourselves. Since a given bpstat's
4367 commands are only executed once, we don't need to copy it; we
4368 can clear the pointer in the bpstat, and make sure we free
4369 the tree when we're done. */
4370 counted_command_line ccmd = bs->commands;
4371 bs->commands = NULL;
4372 if (ccmd != NULL)
4373 cmd = ccmd.get ();
4374 if (command_line_is_silent (cmd))
4375 {
4376 /* The action has been already done by bpstat_stop_status. */
4377 cmd = cmd->next;
4378 }
4379
4380 while (cmd != NULL)
4381 {
4382 execute_control_command (cmd);
4383
4384 if (breakpoint_proceeded)
4385 break;
4386 else
4387 cmd = cmd->next;
4388 }
4389
4390 if (breakpoint_proceeded)
4391 {
4392 if (current_ui->async)
4393 /* If we are in async mode, then the target might be still
4394 running, not stopped at any breakpoint, so nothing for
4395 us to do here -- just return to the event loop. */
4396 ;
4397 else
4398 /* In sync mode, when execute_control_command returns
4399 we're already standing on the next breakpoint.
4400 Breakpoint commands for that stop were not run, since
4401 execute_command does not run breakpoint commands --
4402 only command_line_handler does, but that one is not
4403 involved in execution of breakpoint commands. So, we
4404 can now execute breakpoint commands. It should be
4405 noted that making execute_command do bpstat actions is
4406 not an option -- in this case we'll have recursive
4407 invocation of bpstat for each breakpoint with a
4408 command, and can easily blow up GDB stack. Instead, we
4409 return true, which will trigger the caller to recall us
4410 with the new stop_bpstat. */
4411 again = 1;
4412 break;
4413 }
4414 }
4415 return again;
4416 }
4417
4418 /* Helper for bpstat_do_actions. Get the current thread, if there's
4419 one, is alive and has execution. Return NULL otherwise. */
4420
4421 static thread_info *
4422 get_bpstat_thread ()
4423 {
4424 if (inferior_ptid == null_ptid || !target_has_execution)
4425 return NULL;
4426
4427 thread_info *tp = inferior_thread ();
4428 if (tp->state == THREAD_EXITED || tp->executing)
4429 return NULL;
4430 return tp;
4431 }
4432
4433 void
4434 bpstat_do_actions (void)
4435 {
4436 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4437 thread_info *tp;
4438
4439 /* Do any commands attached to breakpoint we are stopped at. */
4440 while ((tp = get_bpstat_thread ()) != NULL)
4441 {
4442 /* Since in sync mode, bpstat_do_actions may resume the
4443 inferior, and only return when it is stopped at the next
4444 breakpoint, we keep doing breakpoint actions until it returns
4445 false to indicate the inferior was not resumed. */
4446 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4447 break;
4448 }
4449
4450 cleanup_if_error.release ();
4451 }
4452
4453 /* Print out the (old or new) value associated with a watchpoint. */
4454
4455 static void
4456 watchpoint_value_print (struct value *val, struct ui_file *stream)
4457 {
4458 if (val == NULL)
4459 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4460 else
4461 {
4462 struct value_print_options opts;
4463 get_user_print_options (&opts);
4464 value_print (val, stream, &opts);
4465 }
4466 }
4467
4468 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4469 debugging multiple threads. */
4470
4471 void
4472 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4473 {
4474 if (uiout->is_mi_like_p ())
4475 return;
4476
4477 uiout->text ("\n");
4478
4479 if (show_thread_that_caused_stop ())
4480 {
4481 const char *name;
4482 struct thread_info *thr = inferior_thread ();
4483
4484 uiout->text ("Thread ");
4485 uiout->field_string ("thread-id", print_thread_id (thr));
4486
4487 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4488 if (name != NULL)
4489 {
4490 uiout->text (" \"");
4491 uiout->field_string ("name", name);
4492 uiout->text ("\"");
4493 }
4494
4495 uiout->text (" hit ");
4496 }
4497 }
4498
4499 /* Generic routine for printing messages indicating why we
4500 stopped. The behavior of this function depends on the value
4501 'print_it' in the bpstat structure. Under some circumstances we
4502 may decide not to print anything here and delegate the task to
4503 normal_stop(). */
4504
4505 static enum print_stop_action
4506 print_bp_stop_message (bpstat bs)
4507 {
4508 switch (bs->print_it)
4509 {
4510 case print_it_noop:
4511 /* Nothing should be printed for this bpstat entry. */
4512 return PRINT_UNKNOWN;
4513 break;
4514
4515 case print_it_done:
4516 /* We still want to print the frame, but we already printed the
4517 relevant messages. */
4518 return PRINT_SRC_AND_LOC;
4519 break;
4520
4521 case print_it_normal:
4522 {
4523 struct breakpoint *b = bs->breakpoint_at;
4524
4525 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4526 which has since been deleted. */
4527 if (b == NULL)
4528 return PRINT_UNKNOWN;
4529
4530 /* Normal case. Call the breakpoint's print_it method. */
4531 return b->ops->print_it (bs);
4532 }
4533 break;
4534
4535 default:
4536 internal_error (__FILE__, __LINE__,
4537 _("print_bp_stop_message: unrecognized enum value"));
4538 break;
4539 }
4540 }
4541
4542 /* A helper function that prints a shared library stopped event. */
4543
4544 static void
4545 print_solib_event (int is_catchpoint)
4546 {
4547 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4548 bool any_added = !current_program_space->added_solibs.empty ();
4549
4550 if (!is_catchpoint)
4551 {
4552 if (any_added || any_deleted)
4553 current_uiout->text (_("Stopped due to shared library event:\n"));
4554 else
4555 current_uiout->text (_("Stopped due to shared library event (no "
4556 "libraries added or removed)\n"));
4557 }
4558
4559 if (current_uiout->is_mi_like_p ())
4560 current_uiout->field_string ("reason",
4561 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4562
4563 if (any_deleted)
4564 {
4565 current_uiout->text (_(" Inferior unloaded "));
4566 ui_out_emit_list list_emitter (current_uiout, "removed");
4567 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4568 {
4569 const std::string &name = current_program_space->deleted_solibs[ix];
4570
4571 if (ix > 0)
4572 current_uiout->text (" ");
4573 current_uiout->field_string ("library", name);
4574 current_uiout->text ("\n");
4575 }
4576 }
4577
4578 if (any_added)
4579 {
4580 current_uiout->text (_(" Inferior loaded "));
4581 ui_out_emit_list list_emitter (current_uiout, "added");
4582 bool first = true;
4583 for (so_list *iter : current_program_space->added_solibs)
4584 {
4585 if (!first)
4586 current_uiout->text (" ");
4587 first = false;
4588 current_uiout->field_string ("library", iter->so_name);
4589 current_uiout->text ("\n");
4590 }
4591 }
4592 }
4593
4594 /* Print a message indicating what happened. This is called from
4595 normal_stop(). The input to this routine is the head of the bpstat
4596 list - a list of the eventpoints that caused this stop. KIND is
4597 the target_waitkind for the stopping event. This
4598 routine calls the generic print routine for printing a message
4599 about reasons for stopping. This will print (for example) the
4600 "Breakpoint n," part of the output. The return value of this
4601 routine is one of:
4602
4603 PRINT_UNKNOWN: Means we printed nothing.
4604 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4605 code to print the location. An example is
4606 "Breakpoint 1, " which should be followed by
4607 the location.
4608 PRINT_SRC_ONLY: Means we printed something, but there is no need
4609 to also print the location part of the message.
4610 An example is the catch/throw messages, which
4611 don't require a location appended to the end.
4612 PRINT_NOTHING: We have done some printing and we don't need any
4613 further info to be printed. */
4614
4615 enum print_stop_action
4616 bpstat_print (bpstat bs, int kind)
4617 {
4618 enum print_stop_action val;
4619
4620 /* Maybe another breakpoint in the chain caused us to stop.
4621 (Currently all watchpoints go on the bpstat whether hit or not.
4622 That probably could (should) be changed, provided care is taken
4623 with respect to bpstat_explains_signal). */
4624 for (; bs; bs = bs->next)
4625 {
4626 val = print_bp_stop_message (bs);
4627 if (val == PRINT_SRC_ONLY
4628 || val == PRINT_SRC_AND_LOC
4629 || val == PRINT_NOTHING)
4630 return val;
4631 }
4632
4633 /* If we had hit a shared library event breakpoint,
4634 print_bp_stop_message would print out this message. If we hit an
4635 OS-level shared library event, do the same thing. */
4636 if (kind == TARGET_WAITKIND_LOADED)
4637 {
4638 print_solib_event (0);
4639 return PRINT_NOTHING;
4640 }
4641
4642 /* We reached the end of the chain, or we got a null BS to start
4643 with and nothing was printed. */
4644 return PRINT_UNKNOWN;
4645 }
4646
4647 /* Evaluate the boolean expression EXP and return the result. */
4648
4649 static bool
4650 breakpoint_cond_eval (expression *exp)
4651 {
4652 struct value *mark = value_mark ();
4653 bool res = value_true (evaluate_expression (exp));
4654
4655 value_free_to_mark (mark);
4656 return res;
4657 }
4658
4659 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4660
4661 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4662 : next (NULL),
4663 bp_location_at (bl),
4664 breakpoint_at (bl->owner),
4665 commands (NULL),
4666 print (0),
4667 stop (0),
4668 print_it (print_it_normal)
4669 {
4670 incref_bp_location (bl);
4671 **bs_link_pointer = this;
4672 *bs_link_pointer = &next;
4673 }
4674
4675 bpstats::bpstats ()
4676 : next (NULL),
4677 bp_location_at (NULL),
4678 breakpoint_at (NULL),
4679 commands (NULL),
4680 print (0),
4681 stop (0),
4682 print_it (print_it_normal)
4683 {
4684 }
4685 \f
4686 /* The target has stopped with waitstatus WS. Check if any hardware
4687 watchpoints have triggered, according to the target. */
4688
4689 int
4690 watchpoints_triggered (struct target_waitstatus *ws)
4691 {
4692 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4693 CORE_ADDR addr;
4694 struct breakpoint *b;
4695
4696 if (!stopped_by_watchpoint)
4697 {
4698 /* We were not stopped by a watchpoint. Mark all watchpoints
4699 as not triggered. */
4700 ALL_BREAKPOINTS (b)
4701 if (is_hardware_watchpoint (b))
4702 {
4703 struct watchpoint *w = (struct watchpoint *) b;
4704
4705 w->watchpoint_triggered = watch_triggered_no;
4706 }
4707
4708 return 0;
4709 }
4710
4711 if (!target_stopped_data_address (current_top_target (), &addr))
4712 {
4713 /* We were stopped by a watchpoint, but we don't know where.
4714 Mark all watchpoints as unknown. */
4715 ALL_BREAKPOINTS (b)
4716 if (is_hardware_watchpoint (b))
4717 {
4718 struct watchpoint *w = (struct watchpoint *) b;
4719
4720 w->watchpoint_triggered = watch_triggered_unknown;
4721 }
4722
4723 return 1;
4724 }
4725
4726 /* The target could report the data address. Mark watchpoints
4727 affected by this data address as triggered, and all others as not
4728 triggered. */
4729
4730 ALL_BREAKPOINTS (b)
4731 if (is_hardware_watchpoint (b))
4732 {
4733 struct watchpoint *w = (struct watchpoint *) b;
4734 struct bp_location *loc;
4735
4736 w->watchpoint_triggered = watch_triggered_no;
4737 for (loc = b->loc; loc; loc = loc->next)
4738 {
4739 if (is_masked_watchpoint (b))
4740 {
4741 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4742 CORE_ADDR start = loc->address & w->hw_wp_mask;
4743
4744 if (newaddr == start)
4745 {
4746 w->watchpoint_triggered = watch_triggered_yes;
4747 break;
4748 }
4749 }
4750 /* Exact match not required. Within range is sufficient. */
4751 else if (target_watchpoint_addr_within_range (current_top_target (),
4752 addr, loc->address,
4753 loc->length))
4754 {
4755 w->watchpoint_triggered = watch_triggered_yes;
4756 break;
4757 }
4758 }
4759 }
4760
4761 return 1;
4762 }
4763
4764 /* Possible return values for watchpoint_check. */
4765 enum wp_check_result
4766 {
4767 /* The watchpoint has been deleted. */
4768 WP_DELETED = 1,
4769
4770 /* The value has changed. */
4771 WP_VALUE_CHANGED = 2,
4772
4773 /* The value has not changed. */
4774 WP_VALUE_NOT_CHANGED = 3,
4775
4776 /* Ignore this watchpoint, no matter if the value changed or not. */
4777 WP_IGNORE = 4,
4778 };
4779
4780 #define BP_TEMPFLAG 1
4781 #define BP_HARDWAREFLAG 2
4782
4783 /* Evaluate watchpoint condition expression and check if its value
4784 changed. */
4785
4786 static wp_check_result
4787 watchpoint_check (bpstat bs)
4788 {
4789 struct watchpoint *b;
4790 struct frame_info *fr;
4791 int within_current_scope;
4792
4793 /* BS is built from an existing struct breakpoint. */
4794 gdb_assert (bs->breakpoint_at != NULL);
4795 b = (struct watchpoint *) bs->breakpoint_at;
4796
4797 /* If this is a local watchpoint, we only want to check if the
4798 watchpoint frame is in scope if the current thread is the thread
4799 that was used to create the watchpoint. */
4800 if (!watchpoint_in_thread_scope (b))
4801 return WP_IGNORE;
4802
4803 if (b->exp_valid_block == NULL)
4804 within_current_scope = 1;
4805 else
4806 {
4807 struct frame_info *frame = get_current_frame ();
4808 struct gdbarch *frame_arch = get_frame_arch (frame);
4809 CORE_ADDR frame_pc = get_frame_pc (frame);
4810
4811 /* stack_frame_destroyed_p() returns a non-zero value if we're
4812 still in the function but the stack frame has already been
4813 invalidated. Since we can't rely on the values of local
4814 variables after the stack has been destroyed, we are treating
4815 the watchpoint in that state as `not changed' without further
4816 checking. Don't mark watchpoints as changed if the current
4817 frame is in an epilogue - even if they are in some other
4818 frame, our view of the stack is likely to be wrong and
4819 frame_find_by_id could error out. */
4820 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4821 return WP_IGNORE;
4822
4823 fr = frame_find_by_id (b->watchpoint_frame);
4824 within_current_scope = (fr != NULL);
4825
4826 /* If we've gotten confused in the unwinder, we might have
4827 returned a frame that can't describe this variable. */
4828 if (within_current_scope)
4829 {
4830 struct symbol *function;
4831
4832 function = get_frame_function (fr);
4833 if (function == NULL
4834 || !contained_in (b->exp_valid_block,
4835 SYMBOL_BLOCK_VALUE (function)))
4836 within_current_scope = 0;
4837 }
4838
4839 if (within_current_scope)
4840 /* If we end up stopping, the current frame will get selected
4841 in normal_stop. So this call to select_frame won't affect
4842 the user. */
4843 select_frame (fr);
4844 }
4845
4846 if (within_current_scope)
4847 {
4848 /* We use value_{,free_to_}mark because it could be a *long*
4849 time before we return to the command level and call
4850 free_all_values. We can't call free_all_values because we
4851 might be in the middle of evaluating a function call. */
4852
4853 int pc = 0;
4854 struct value *mark;
4855 struct value *new_val;
4856
4857 if (is_masked_watchpoint (b))
4858 /* Since we don't know the exact trigger address (from
4859 stopped_data_address), just tell the user we've triggered
4860 a mask watchpoint. */
4861 return WP_VALUE_CHANGED;
4862
4863 mark = value_mark ();
4864 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4865
4866 if (b->val_bitsize != 0)
4867 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4868
4869 /* We use value_equal_contents instead of value_equal because
4870 the latter coerces an array to a pointer, thus comparing just
4871 the address of the array instead of its contents. This is
4872 not what we want. */
4873 if ((b->val != NULL) != (new_val != NULL)
4874 || (b->val != NULL && !value_equal_contents (b->val.get (),
4875 new_val)))
4876 {
4877 bs->old_val = b->val;
4878 b->val = release_value (new_val);
4879 b->val_valid = true;
4880 if (new_val != NULL)
4881 value_free_to_mark (mark);
4882 return WP_VALUE_CHANGED;
4883 }
4884 else
4885 {
4886 /* Nothing changed. */
4887 value_free_to_mark (mark);
4888 return WP_VALUE_NOT_CHANGED;
4889 }
4890 }
4891 else
4892 {
4893 /* This seems like the only logical thing to do because
4894 if we temporarily ignored the watchpoint, then when
4895 we reenter the block in which it is valid it contains
4896 garbage (in the case of a function, it may have two
4897 garbage values, one before and one after the prologue).
4898 So we can't even detect the first assignment to it and
4899 watch after that (since the garbage may or may not equal
4900 the first value assigned). */
4901 /* We print all the stop information in
4902 breakpoint_ops->print_it, but in this case, by the time we
4903 call breakpoint_ops->print_it this bp will be deleted
4904 already. So we have no choice but print the information
4905 here. */
4906
4907 SWITCH_THRU_ALL_UIS ()
4908 {
4909 struct ui_out *uiout = current_uiout;
4910
4911 if (uiout->is_mi_like_p ())
4912 uiout->field_string
4913 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4914 uiout->message ("\nWatchpoint %pF deleted because the program has "
4915 "left the block in\n"
4916 "which its expression is valid.\n",
4917 signed_field ("wpnum", b->number));
4918 }
4919
4920 /* Make sure the watchpoint's commands aren't executed. */
4921 b->commands = NULL;
4922 watchpoint_del_at_next_stop (b);
4923
4924 return WP_DELETED;
4925 }
4926 }
4927
4928 /* Return true if it looks like target has stopped due to hitting
4929 breakpoint location BL. This function does not check if we should
4930 stop, only if BL explains the stop. */
4931
4932 static int
4933 bpstat_check_location (const struct bp_location *bl,
4934 const address_space *aspace, CORE_ADDR bp_addr,
4935 const struct target_waitstatus *ws)
4936 {
4937 struct breakpoint *b = bl->owner;
4938
4939 /* BL is from an existing breakpoint. */
4940 gdb_assert (b != NULL);
4941
4942 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4943 }
4944
4945 /* Determine if the watched values have actually changed, and we
4946 should stop. If not, set BS->stop to 0. */
4947
4948 static void
4949 bpstat_check_watchpoint (bpstat bs)
4950 {
4951 const struct bp_location *bl;
4952 struct watchpoint *b;
4953
4954 /* BS is built for existing struct breakpoint. */
4955 bl = bs->bp_location_at;
4956 gdb_assert (bl != NULL);
4957 b = (struct watchpoint *) bs->breakpoint_at;
4958 gdb_assert (b != NULL);
4959
4960 {
4961 int must_check_value = 0;
4962
4963 if (b->type == bp_watchpoint)
4964 /* For a software watchpoint, we must always check the
4965 watched value. */
4966 must_check_value = 1;
4967 else if (b->watchpoint_triggered == watch_triggered_yes)
4968 /* We have a hardware watchpoint (read, write, or access)
4969 and the target earlier reported an address watched by
4970 this watchpoint. */
4971 must_check_value = 1;
4972 else if (b->watchpoint_triggered == watch_triggered_unknown
4973 && b->type == bp_hardware_watchpoint)
4974 /* We were stopped by a hardware watchpoint, but the target could
4975 not report the data address. We must check the watchpoint's
4976 value. Access and read watchpoints are out of luck; without
4977 a data address, we can't figure it out. */
4978 must_check_value = 1;
4979
4980 if (must_check_value)
4981 {
4982 wp_check_result e;
4983
4984 try
4985 {
4986 e = watchpoint_check (bs);
4987 }
4988 catch (const gdb_exception &ex)
4989 {
4990 exception_fprintf (gdb_stderr, ex,
4991 "Error evaluating expression "
4992 "for watchpoint %d\n",
4993 b->number);
4994
4995 SWITCH_THRU_ALL_UIS ()
4996 {
4997 printf_filtered (_("Watchpoint %d deleted.\n"),
4998 b->number);
4999 }
5000 watchpoint_del_at_next_stop (b);
5001 e = WP_DELETED;
5002 }
5003
5004 switch (e)
5005 {
5006 case WP_DELETED:
5007 /* We've already printed what needs to be printed. */
5008 bs->print_it = print_it_done;
5009 /* Stop. */
5010 break;
5011 case WP_IGNORE:
5012 bs->print_it = print_it_noop;
5013 bs->stop = 0;
5014 break;
5015 case WP_VALUE_CHANGED:
5016 if (b->type == bp_read_watchpoint)
5017 {
5018 /* There are two cases to consider here:
5019
5020 1. We're watching the triggered memory for reads.
5021 In that case, trust the target, and always report
5022 the watchpoint hit to the user. Even though
5023 reads don't cause value changes, the value may
5024 have changed since the last time it was read, and
5025 since we're not trapping writes, we will not see
5026 those, and as such we should ignore our notion of
5027 old value.
5028
5029 2. We're watching the triggered memory for both
5030 reads and writes. There are two ways this may
5031 happen:
5032
5033 2.1. This is a target that can't break on data
5034 reads only, but can break on accesses (reads or
5035 writes), such as e.g., x86. We detect this case
5036 at the time we try to insert read watchpoints.
5037
5038 2.2. Otherwise, the target supports read
5039 watchpoints, but, the user set an access or write
5040 watchpoint watching the same memory as this read
5041 watchpoint.
5042
5043 If we're watching memory writes as well as reads,
5044 ignore watchpoint hits when we find that the
5045 value hasn't changed, as reads don't cause
5046 changes. This still gives false positives when
5047 the program writes the same value to memory as
5048 what there was already in memory (we will confuse
5049 it for a read), but it's much better than
5050 nothing. */
5051
5052 int other_write_watchpoint = 0;
5053
5054 if (bl->watchpoint_type == hw_read)
5055 {
5056 struct breakpoint *other_b;
5057
5058 ALL_BREAKPOINTS (other_b)
5059 if (other_b->type == bp_hardware_watchpoint
5060 || other_b->type == bp_access_watchpoint)
5061 {
5062 struct watchpoint *other_w =
5063 (struct watchpoint *) other_b;
5064
5065 if (other_w->watchpoint_triggered
5066 == watch_triggered_yes)
5067 {
5068 other_write_watchpoint = 1;
5069 break;
5070 }
5071 }
5072 }
5073
5074 if (other_write_watchpoint
5075 || bl->watchpoint_type == hw_access)
5076 {
5077 /* We're watching the same memory for writes,
5078 and the value changed since the last time we
5079 updated it, so this trap must be for a write.
5080 Ignore it. */
5081 bs->print_it = print_it_noop;
5082 bs->stop = 0;
5083 }
5084 }
5085 break;
5086 case WP_VALUE_NOT_CHANGED:
5087 if (b->type == bp_hardware_watchpoint
5088 || b->type == bp_watchpoint)
5089 {
5090 /* Don't stop: write watchpoints shouldn't fire if
5091 the value hasn't changed. */
5092 bs->print_it = print_it_noop;
5093 bs->stop = 0;
5094 }
5095 /* Stop. */
5096 break;
5097 default:
5098 /* Can't happen. */
5099 break;
5100 }
5101 }
5102 else /* must_check_value == 0 */
5103 {
5104 /* This is a case where some watchpoint(s) triggered, but
5105 not at the address of this watchpoint, or else no
5106 watchpoint triggered after all. So don't print
5107 anything for this watchpoint. */
5108 bs->print_it = print_it_noop;
5109 bs->stop = 0;
5110 }
5111 }
5112 }
5113
5114 /* For breakpoints that are currently marked as telling gdb to stop,
5115 check conditions (condition proper, frame, thread and ignore count)
5116 of breakpoint referred to by BS. If we should not stop for this
5117 breakpoint, set BS->stop to 0. */
5118
5119 static void
5120 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5121 {
5122 const struct bp_location *bl;
5123 struct breakpoint *b;
5124 /* Assume stop. */
5125 bool condition_result = true;
5126 struct expression *cond;
5127
5128 gdb_assert (bs->stop);
5129
5130 /* BS is built for existing struct breakpoint. */
5131 bl = bs->bp_location_at;
5132 gdb_assert (bl != NULL);
5133 b = bs->breakpoint_at;
5134 gdb_assert (b != NULL);
5135
5136 /* Even if the target evaluated the condition on its end and notified GDB, we
5137 need to do so again since GDB does not know if we stopped due to a
5138 breakpoint or a single step breakpoint. */
5139
5140 if (frame_id_p (b->frame_id)
5141 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5142 {
5143 bs->stop = 0;
5144 return;
5145 }
5146
5147 /* If this is a thread/task-specific breakpoint, don't waste cpu
5148 evaluating the condition if this isn't the specified
5149 thread/task. */
5150 if ((b->thread != -1 && b->thread != thread->global_num)
5151 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5152 {
5153 bs->stop = 0;
5154 return;
5155 }
5156
5157 /* Evaluate extension language breakpoints that have a "stop" method
5158 implemented. */
5159 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5160
5161 if (is_watchpoint (b))
5162 {
5163 struct watchpoint *w = (struct watchpoint *) b;
5164
5165 cond = w->cond_exp.get ();
5166 }
5167 else
5168 cond = bl->cond.get ();
5169
5170 if (cond && b->disposition != disp_del_at_next_stop)
5171 {
5172 int within_current_scope = 1;
5173 struct watchpoint * w;
5174
5175 /* We use value_mark and value_free_to_mark because it could
5176 be a long time before we return to the command level and
5177 call free_all_values. We can't call free_all_values
5178 because we might be in the middle of evaluating a
5179 function call. */
5180 struct value *mark = value_mark ();
5181
5182 if (is_watchpoint (b))
5183 w = (struct watchpoint *) b;
5184 else
5185 w = NULL;
5186
5187 /* Need to select the frame, with all that implies so that
5188 the conditions will have the right context. Because we
5189 use the frame, we will not see an inlined function's
5190 variables when we arrive at a breakpoint at the start
5191 of the inlined function; the current frame will be the
5192 call site. */
5193 if (w == NULL || w->cond_exp_valid_block == NULL)
5194 select_frame (get_current_frame ());
5195 else
5196 {
5197 struct frame_info *frame;
5198
5199 /* For local watchpoint expressions, which particular
5200 instance of a local is being watched matters, so we
5201 keep track of the frame to evaluate the expression
5202 in. To evaluate the condition however, it doesn't
5203 really matter which instantiation of the function
5204 where the condition makes sense triggers the
5205 watchpoint. This allows an expression like "watch
5206 global if q > 10" set in `func', catch writes to
5207 global on all threads that call `func', or catch
5208 writes on all recursive calls of `func' by a single
5209 thread. We simply always evaluate the condition in
5210 the innermost frame that's executing where it makes
5211 sense to evaluate the condition. It seems
5212 intuitive. */
5213 frame = block_innermost_frame (w->cond_exp_valid_block);
5214 if (frame != NULL)
5215 select_frame (frame);
5216 else
5217 within_current_scope = 0;
5218 }
5219 if (within_current_scope)
5220 {
5221 try
5222 {
5223 condition_result = breakpoint_cond_eval (cond);
5224 }
5225 catch (const gdb_exception &ex)
5226 {
5227 exception_fprintf (gdb_stderr, ex,
5228 "Error in testing breakpoint condition:\n");
5229 }
5230 }
5231 else
5232 {
5233 warning (_("Watchpoint condition cannot be tested "
5234 "in the current scope"));
5235 /* If we failed to set the right context for this
5236 watchpoint, unconditionally report it. */
5237 }
5238 /* FIXME-someday, should give breakpoint #. */
5239 value_free_to_mark (mark);
5240 }
5241
5242 if (cond && !condition_result)
5243 {
5244 bs->stop = 0;
5245 }
5246 else if (b->ignore_count > 0)
5247 {
5248 b->ignore_count--;
5249 bs->stop = 0;
5250 /* Increase the hit count even though we don't stop. */
5251 ++(b->hit_count);
5252 gdb::observers::breakpoint_modified.notify (b);
5253 }
5254 }
5255
5256 /* Returns true if we need to track moribund locations of LOC's type
5257 on the current target. */
5258
5259 static int
5260 need_moribund_for_location_type (struct bp_location *loc)
5261 {
5262 return ((loc->loc_type == bp_loc_software_breakpoint
5263 && !target_supports_stopped_by_sw_breakpoint ())
5264 || (loc->loc_type == bp_loc_hardware_breakpoint
5265 && !target_supports_stopped_by_hw_breakpoint ()));
5266 }
5267
5268 /* See breakpoint.h. */
5269
5270 bpstat
5271 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5272 const struct target_waitstatus *ws)
5273 {
5274 struct breakpoint *b;
5275 bpstat bs_head = NULL, *bs_link = &bs_head;
5276
5277 ALL_BREAKPOINTS (b)
5278 {
5279 if (!breakpoint_enabled (b))
5280 continue;
5281
5282 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5283 {
5284 /* For hardware watchpoints, we look only at the first
5285 location. The watchpoint_check function will work on the
5286 entire expression, not the individual locations. For
5287 read watchpoints, the watchpoints_triggered function has
5288 checked all locations already. */
5289 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5290 break;
5291
5292 if (!bl->enabled || bl->shlib_disabled)
5293 continue;
5294
5295 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5296 continue;
5297
5298 /* Come here if it's a watchpoint, or if the break address
5299 matches. */
5300
5301 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5302 explain stop. */
5303
5304 /* Assume we stop. Should we find a watchpoint that is not
5305 actually triggered, or if the condition of the breakpoint
5306 evaluates as false, we'll reset 'stop' to 0. */
5307 bs->stop = 1;
5308 bs->print = 1;
5309
5310 /* If this is a scope breakpoint, mark the associated
5311 watchpoint as triggered so that we will handle the
5312 out-of-scope event. We'll get to the watchpoint next
5313 iteration. */
5314 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5315 {
5316 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5317
5318 w->watchpoint_triggered = watch_triggered_yes;
5319 }
5320 }
5321 }
5322
5323 /* Check if a moribund breakpoint explains the stop. */
5324 if (!target_supports_stopped_by_sw_breakpoint ()
5325 || !target_supports_stopped_by_hw_breakpoint ())
5326 {
5327 for (bp_location *loc : moribund_locations)
5328 {
5329 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5330 && need_moribund_for_location_type (loc))
5331 {
5332 bpstat bs = new bpstats (loc, &bs_link);
5333 /* For hits of moribund locations, we should just proceed. */
5334 bs->stop = 0;
5335 bs->print = 0;
5336 bs->print_it = print_it_noop;
5337 }
5338 }
5339 }
5340
5341 return bs_head;
5342 }
5343
5344 /* See breakpoint.h. */
5345
5346 bpstat
5347 bpstat_stop_status (const address_space *aspace,
5348 CORE_ADDR bp_addr, thread_info *thread,
5349 const struct target_waitstatus *ws,
5350 bpstat stop_chain)
5351 {
5352 struct breakpoint *b = NULL;
5353 /* First item of allocated bpstat's. */
5354 bpstat bs_head = stop_chain;
5355 bpstat bs;
5356 int need_remove_insert;
5357 int removed_any;
5358
5359 /* First, build the bpstat chain with locations that explain a
5360 target stop, while being careful to not set the target running,
5361 as that may invalidate locations (in particular watchpoint
5362 locations are recreated). Resuming will happen here with
5363 breakpoint conditions or watchpoint expressions that include
5364 inferior function calls. */
5365 if (bs_head == NULL)
5366 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5367
5368 /* A bit of special processing for shlib breakpoints. We need to
5369 process solib loading here, so that the lists of loaded and
5370 unloaded libraries are correct before we handle "catch load" and
5371 "catch unload". */
5372 for (bs = bs_head; bs != NULL; bs = bs->next)
5373 {
5374 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5375 {
5376 handle_solib_event ();
5377 break;
5378 }
5379 }
5380
5381 /* Now go through the locations that caused the target to stop, and
5382 check whether we're interested in reporting this stop to higher
5383 layers, or whether we should resume the target transparently. */
5384
5385 removed_any = 0;
5386
5387 for (bs = bs_head; bs != NULL; bs = bs->next)
5388 {
5389 if (!bs->stop)
5390 continue;
5391
5392 b = bs->breakpoint_at;
5393 b->ops->check_status (bs);
5394 if (bs->stop)
5395 {
5396 bpstat_check_breakpoint_conditions (bs, thread);
5397
5398 if (bs->stop)
5399 {
5400 ++(b->hit_count);
5401 gdb::observers::breakpoint_modified.notify (b);
5402
5403 /* We will stop here. */
5404 if (b->disposition == disp_disable)
5405 {
5406 --(b->enable_count);
5407 if (b->enable_count <= 0)
5408 b->enable_state = bp_disabled;
5409 removed_any = 1;
5410 }
5411 if (b->silent)
5412 bs->print = 0;
5413 bs->commands = b->commands;
5414 if (command_line_is_silent (bs->commands
5415 ? bs->commands.get () : NULL))
5416 bs->print = 0;
5417
5418 b->ops->after_condition_true (bs);
5419 }
5420
5421 }
5422
5423 /* Print nothing for this entry if we don't stop or don't
5424 print. */
5425 if (!bs->stop || !bs->print)
5426 bs->print_it = print_it_noop;
5427 }
5428
5429 /* If we aren't stopping, the value of some hardware watchpoint may
5430 not have changed, but the intermediate memory locations we are
5431 watching may have. Don't bother if we're stopping; this will get
5432 done later. */
5433 need_remove_insert = 0;
5434 if (! bpstat_causes_stop (bs_head))
5435 for (bs = bs_head; bs != NULL; bs = bs->next)
5436 if (!bs->stop
5437 && bs->breakpoint_at
5438 && is_hardware_watchpoint (bs->breakpoint_at))
5439 {
5440 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5441
5442 update_watchpoint (w, 0 /* don't reparse. */);
5443 need_remove_insert = 1;
5444 }
5445
5446 if (need_remove_insert)
5447 update_global_location_list (UGLL_MAY_INSERT);
5448 else if (removed_any)
5449 update_global_location_list (UGLL_DONT_INSERT);
5450
5451 return bs_head;
5452 }
5453
5454 static void
5455 handle_jit_event (void)
5456 {
5457 struct frame_info *frame;
5458 struct gdbarch *gdbarch;
5459
5460 if (debug_infrun)
5461 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5462
5463 /* Switch terminal for any messages produced by
5464 breakpoint_re_set. */
5465 target_terminal::ours_for_output ();
5466
5467 frame = get_current_frame ();
5468 gdbarch = get_frame_arch (frame);
5469
5470 jit_event_handler (gdbarch);
5471
5472 target_terminal::inferior ();
5473 }
5474
5475 /* Prepare WHAT final decision for infrun. */
5476
5477 /* Decide what infrun needs to do with this bpstat. */
5478
5479 struct bpstat_what
5480 bpstat_what (bpstat bs_head)
5481 {
5482 struct bpstat_what retval;
5483 bpstat bs;
5484
5485 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5486 retval.call_dummy = STOP_NONE;
5487 retval.is_longjmp = false;
5488
5489 for (bs = bs_head; bs != NULL; bs = bs->next)
5490 {
5491 /* Extract this BS's action. After processing each BS, we check
5492 if its action overrides all we've seem so far. */
5493 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5494 enum bptype bptype;
5495
5496 if (bs->breakpoint_at == NULL)
5497 {
5498 /* I suspect this can happen if it was a momentary
5499 breakpoint which has since been deleted. */
5500 bptype = bp_none;
5501 }
5502 else
5503 bptype = bs->breakpoint_at->type;
5504
5505 switch (bptype)
5506 {
5507 case bp_none:
5508 break;
5509 case bp_breakpoint:
5510 case bp_hardware_breakpoint:
5511 case bp_single_step:
5512 case bp_until:
5513 case bp_finish:
5514 case bp_shlib_event:
5515 if (bs->stop)
5516 {
5517 if (bs->print)
5518 this_action = BPSTAT_WHAT_STOP_NOISY;
5519 else
5520 this_action = BPSTAT_WHAT_STOP_SILENT;
5521 }
5522 else
5523 this_action = BPSTAT_WHAT_SINGLE;
5524 break;
5525 case bp_watchpoint:
5526 case bp_hardware_watchpoint:
5527 case bp_read_watchpoint:
5528 case bp_access_watchpoint:
5529 if (bs->stop)
5530 {
5531 if (bs->print)
5532 this_action = BPSTAT_WHAT_STOP_NOISY;
5533 else
5534 this_action = BPSTAT_WHAT_STOP_SILENT;
5535 }
5536 else
5537 {
5538 /* There was a watchpoint, but we're not stopping.
5539 This requires no further action. */
5540 }
5541 break;
5542 case bp_longjmp:
5543 case bp_longjmp_call_dummy:
5544 case bp_exception:
5545 if (bs->stop)
5546 {
5547 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5548 retval.is_longjmp = bptype != bp_exception;
5549 }
5550 else
5551 this_action = BPSTAT_WHAT_SINGLE;
5552 break;
5553 case bp_longjmp_resume:
5554 case bp_exception_resume:
5555 if (bs->stop)
5556 {
5557 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5558 retval.is_longjmp = bptype == bp_longjmp_resume;
5559 }
5560 else
5561 this_action = BPSTAT_WHAT_SINGLE;
5562 break;
5563 case bp_step_resume:
5564 if (bs->stop)
5565 this_action = BPSTAT_WHAT_STEP_RESUME;
5566 else
5567 {
5568 /* It is for the wrong frame. */
5569 this_action = BPSTAT_WHAT_SINGLE;
5570 }
5571 break;
5572 case bp_hp_step_resume:
5573 if (bs->stop)
5574 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5575 else
5576 {
5577 /* It is for the wrong frame. */
5578 this_action = BPSTAT_WHAT_SINGLE;
5579 }
5580 break;
5581 case bp_watchpoint_scope:
5582 case bp_thread_event:
5583 case bp_overlay_event:
5584 case bp_longjmp_master:
5585 case bp_std_terminate_master:
5586 case bp_exception_master:
5587 this_action = BPSTAT_WHAT_SINGLE;
5588 break;
5589 case bp_catchpoint:
5590 if (bs->stop)
5591 {
5592 if (bs->print)
5593 this_action = BPSTAT_WHAT_STOP_NOISY;
5594 else
5595 this_action = BPSTAT_WHAT_STOP_SILENT;
5596 }
5597 else
5598 {
5599 /* Some catchpoints are implemented with breakpoints.
5600 For those, we need to step over the breakpoint. */
5601 if (bs->bp_location_at->loc_type != bp_loc_other)
5602 this_action = BPSTAT_WHAT_SINGLE;
5603 }
5604 break;
5605 case bp_jit_event:
5606 this_action = BPSTAT_WHAT_SINGLE;
5607 break;
5608 case bp_call_dummy:
5609 /* Make sure the action is stop (silent or noisy),
5610 so infrun.c pops the dummy frame. */
5611 retval.call_dummy = STOP_STACK_DUMMY;
5612 this_action = BPSTAT_WHAT_STOP_SILENT;
5613 break;
5614 case bp_std_terminate:
5615 /* Make sure the action is stop (silent or noisy),
5616 so infrun.c pops the dummy frame. */
5617 retval.call_dummy = STOP_STD_TERMINATE;
5618 this_action = BPSTAT_WHAT_STOP_SILENT;
5619 break;
5620 case bp_tracepoint:
5621 case bp_fast_tracepoint:
5622 case bp_static_tracepoint:
5623 /* Tracepoint hits should not be reported back to GDB, and
5624 if one got through somehow, it should have been filtered
5625 out already. */
5626 internal_error (__FILE__, __LINE__,
5627 _("bpstat_what: tracepoint encountered"));
5628 break;
5629 case bp_gnu_ifunc_resolver:
5630 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5631 this_action = BPSTAT_WHAT_SINGLE;
5632 break;
5633 case bp_gnu_ifunc_resolver_return:
5634 /* The breakpoint will be removed, execution will restart from the
5635 PC of the former breakpoint. */
5636 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5637 break;
5638
5639 case bp_dprintf:
5640 if (bs->stop)
5641 this_action = BPSTAT_WHAT_STOP_SILENT;
5642 else
5643 this_action = BPSTAT_WHAT_SINGLE;
5644 break;
5645
5646 default:
5647 internal_error (__FILE__, __LINE__,
5648 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5649 }
5650
5651 retval.main_action = std::max (retval.main_action, this_action);
5652 }
5653
5654 return retval;
5655 }
5656
5657 void
5658 bpstat_run_callbacks (bpstat bs_head)
5659 {
5660 bpstat bs;
5661
5662 for (bs = bs_head; bs != NULL; bs = bs->next)
5663 {
5664 struct breakpoint *b = bs->breakpoint_at;
5665
5666 if (b == NULL)
5667 continue;
5668 switch (b->type)
5669 {
5670 case bp_jit_event:
5671 handle_jit_event ();
5672 break;
5673 case bp_gnu_ifunc_resolver:
5674 gnu_ifunc_resolver_stop (b);
5675 break;
5676 case bp_gnu_ifunc_resolver_return:
5677 gnu_ifunc_resolver_return_stop (b);
5678 break;
5679 }
5680 }
5681 }
5682
5683 /* See breakpoint.h. */
5684
5685 bool
5686 bpstat_should_step ()
5687 {
5688 struct breakpoint *b;
5689
5690 ALL_BREAKPOINTS (b)
5691 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5692 return true;
5693 return false;
5694 }
5695
5696 /* See breakpoint.h. */
5697
5698 bool
5699 bpstat_causes_stop (bpstat bs)
5700 {
5701 for (; bs != NULL; bs = bs->next)
5702 if (bs->stop)
5703 return true;
5704
5705 return false;
5706 }
5707
5708 \f
5709
5710 /* Compute a string of spaces suitable to indent the next line
5711 so it starts at the position corresponding to the table column
5712 named COL_NAME in the currently active table of UIOUT. */
5713
5714 static char *
5715 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5716 {
5717 static char wrap_indent[80];
5718 int i, total_width, width, align;
5719 const char *text;
5720
5721 total_width = 0;
5722 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5723 {
5724 if (strcmp (text, col_name) == 0)
5725 {
5726 gdb_assert (total_width < sizeof wrap_indent);
5727 memset (wrap_indent, ' ', total_width);
5728 wrap_indent[total_width] = 0;
5729
5730 return wrap_indent;
5731 }
5732
5733 total_width += width + 1;
5734 }
5735
5736 return NULL;
5737 }
5738
5739 /* Determine if the locations of this breakpoint will have their conditions
5740 evaluated by the target, host or a mix of both. Returns the following:
5741
5742 "host": Host evals condition.
5743 "host or target": Host or Target evals condition.
5744 "target": Target evals condition.
5745 */
5746
5747 static const char *
5748 bp_condition_evaluator (struct breakpoint *b)
5749 {
5750 struct bp_location *bl;
5751 char host_evals = 0;
5752 char target_evals = 0;
5753
5754 if (!b)
5755 return NULL;
5756
5757 if (!is_breakpoint (b))
5758 return NULL;
5759
5760 if (gdb_evaluates_breakpoint_condition_p ()
5761 || !target_supports_evaluation_of_breakpoint_conditions ())
5762 return condition_evaluation_host;
5763
5764 for (bl = b->loc; bl; bl = bl->next)
5765 {
5766 if (bl->cond_bytecode)
5767 target_evals++;
5768 else
5769 host_evals++;
5770 }
5771
5772 if (host_evals && target_evals)
5773 return condition_evaluation_both;
5774 else if (target_evals)
5775 return condition_evaluation_target;
5776 else
5777 return condition_evaluation_host;
5778 }
5779
5780 /* Determine the breakpoint location's condition evaluator. This is
5781 similar to bp_condition_evaluator, but for locations. */
5782
5783 static const char *
5784 bp_location_condition_evaluator (struct bp_location *bl)
5785 {
5786 if (bl && !is_breakpoint (bl->owner))
5787 return NULL;
5788
5789 if (gdb_evaluates_breakpoint_condition_p ()
5790 || !target_supports_evaluation_of_breakpoint_conditions ())
5791 return condition_evaluation_host;
5792
5793 if (bl && bl->cond_bytecode)
5794 return condition_evaluation_target;
5795 else
5796 return condition_evaluation_host;
5797 }
5798
5799 /* Print the LOC location out of the list of B->LOC locations. */
5800
5801 static void
5802 print_breakpoint_location (struct breakpoint *b,
5803 struct bp_location *loc)
5804 {
5805 struct ui_out *uiout = current_uiout;
5806
5807 scoped_restore_current_program_space restore_pspace;
5808
5809 if (loc != NULL && loc->shlib_disabled)
5810 loc = NULL;
5811
5812 if (loc != NULL)
5813 set_current_program_space (loc->pspace);
5814
5815 if (b->display_canonical)
5816 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5817 else if (loc && loc->symtab)
5818 {
5819 const struct symbol *sym = loc->symbol;
5820
5821 if (sym)
5822 {
5823 uiout->text ("in ");
5824 uiout->field_string ("func", sym->print_name (),
5825 function_name_style.style ());
5826 uiout->text (" ");
5827 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5828 uiout->text ("at ");
5829 }
5830 uiout->field_string ("file",
5831 symtab_to_filename_for_display (loc->symtab),
5832 file_name_style.style ());
5833 uiout->text (":");
5834
5835 if (uiout->is_mi_like_p ())
5836 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5837
5838 uiout->field_signed ("line", loc->line_number);
5839 }
5840 else if (loc)
5841 {
5842 string_file stb;
5843
5844 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5845 demangle, "");
5846 uiout->field_stream ("at", stb);
5847 }
5848 else
5849 {
5850 uiout->field_string ("pending",
5851 event_location_to_string (b->location.get ()));
5852 /* If extra_string is available, it could be holding a condition
5853 or dprintf arguments. In either case, make sure it is printed,
5854 too, but only for non-MI streams. */
5855 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5856 {
5857 if (b->type == bp_dprintf)
5858 uiout->text (",");
5859 else
5860 uiout->text (" ");
5861 uiout->text (b->extra_string);
5862 }
5863 }
5864
5865 if (loc && is_breakpoint (b)
5866 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5867 && bp_condition_evaluator (b) == condition_evaluation_both)
5868 {
5869 uiout->text (" (");
5870 uiout->field_string ("evaluated-by",
5871 bp_location_condition_evaluator (loc));
5872 uiout->text (")");
5873 }
5874 }
5875
5876 static const char *
5877 bptype_string (enum bptype type)
5878 {
5879 struct ep_type_description
5880 {
5881 enum bptype type;
5882 const char *description;
5883 };
5884 static struct ep_type_description bptypes[] =
5885 {
5886 {bp_none, "?deleted?"},
5887 {bp_breakpoint, "breakpoint"},
5888 {bp_hardware_breakpoint, "hw breakpoint"},
5889 {bp_single_step, "sw single-step"},
5890 {bp_until, "until"},
5891 {bp_finish, "finish"},
5892 {bp_watchpoint, "watchpoint"},
5893 {bp_hardware_watchpoint, "hw watchpoint"},
5894 {bp_read_watchpoint, "read watchpoint"},
5895 {bp_access_watchpoint, "acc watchpoint"},
5896 {bp_longjmp, "longjmp"},
5897 {bp_longjmp_resume, "longjmp resume"},
5898 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5899 {bp_exception, "exception"},
5900 {bp_exception_resume, "exception resume"},
5901 {bp_step_resume, "step resume"},
5902 {bp_hp_step_resume, "high-priority step resume"},
5903 {bp_watchpoint_scope, "watchpoint scope"},
5904 {bp_call_dummy, "call dummy"},
5905 {bp_std_terminate, "std::terminate"},
5906 {bp_shlib_event, "shlib events"},
5907 {bp_thread_event, "thread events"},
5908 {bp_overlay_event, "overlay events"},
5909 {bp_longjmp_master, "longjmp master"},
5910 {bp_std_terminate_master, "std::terminate master"},
5911 {bp_exception_master, "exception master"},
5912 {bp_catchpoint, "catchpoint"},
5913 {bp_tracepoint, "tracepoint"},
5914 {bp_fast_tracepoint, "fast tracepoint"},
5915 {bp_static_tracepoint, "static tracepoint"},
5916 {bp_dprintf, "dprintf"},
5917 {bp_jit_event, "jit events"},
5918 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5919 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5920 };
5921
5922 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5923 || ((int) type != bptypes[(int) type].type))
5924 internal_error (__FILE__, __LINE__,
5925 _("bptypes table does not describe type #%d."),
5926 (int) type);
5927
5928 return bptypes[(int) type].description;
5929 }
5930
5931 /* For MI, output a field named 'thread-groups' with a list as the value.
5932 For CLI, prefix the list with the string 'inf'. */
5933
5934 static void
5935 output_thread_groups (struct ui_out *uiout,
5936 const char *field_name,
5937 const std::vector<int> &inf_nums,
5938 int mi_only)
5939 {
5940 int is_mi = uiout->is_mi_like_p ();
5941
5942 /* For backward compatibility, don't display inferiors in CLI unless
5943 there are several. Always display them for MI. */
5944 if (!is_mi && mi_only)
5945 return;
5946
5947 ui_out_emit_list list_emitter (uiout, field_name);
5948
5949 for (size_t i = 0; i < inf_nums.size (); i++)
5950 {
5951 if (is_mi)
5952 {
5953 char mi_group[10];
5954
5955 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
5956 uiout->field_string (NULL, mi_group);
5957 }
5958 else
5959 {
5960 if (i == 0)
5961 uiout->text (" inf ");
5962 else
5963 uiout->text (", ");
5964
5965 uiout->text (plongest (inf_nums[i]));
5966 }
5967 }
5968 }
5969
5970 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5971 instead of going via breakpoint_ops::print_one. This makes "maint
5972 info breakpoints" show the software breakpoint locations of
5973 catchpoints, which are considered internal implementation
5974 detail. */
5975
5976 static void
5977 print_one_breakpoint_location (struct breakpoint *b,
5978 struct bp_location *loc,
5979 int loc_number,
5980 struct bp_location **last_loc,
5981 int allflag, bool raw_loc)
5982 {
5983 struct command_line *l;
5984 static char bpenables[] = "nynny";
5985
5986 struct ui_out *uiout = current_uiout;
5987 int header_of_multiple = 0;
5988 int part_of_multiple = (loc != NULL);
5989 struct value_print_options opts;
5990
5991 get_user_print_options (&opts);
5992
5993 gdb_assert (!loc || loc_number != 0);
5994 /* See comment in print_one_breakpoint concerning treatment of
5995 breakpoints with single disabled location. */
5996 if (loc == NULL
5997 && (b->loc != NULL
5998 && (b->loc->next != NULL || !b->loc->enabled)))
5999 header_of_multiple = 1;
6000 if (loc == NULL)
6001 loc = b->loc;
6002
6003 annotate_record ();
6004
6005 /* 1 */
6006 annotate_field (0);
6007 if (part_of_multiple)
6008 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6009 else
6010 uiout->field_signed ("number", b->number);
6011
6012 /* 2 */
6013 annotate_field (1);
6014 if (part_of_multiple)
6015 uiout->field_skip ("type");
6016 else
6017 uiout->field_string ("type", bptype_string (b->type));
6018
6019 /* 3 */
6020 annotate_field (2);
6021 if (part_of_multiple)
6022 uiout->field_skip ("disp");
6023 else
6024 uiout->field_string ("disp", bpdisp_text (b->disposition));
6025
6026 /* 4 */
6027 annotate_field (3);
6028 if (part_of_multiple)
6029 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6030 else
6031 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6032
6033 /* 5 and 6 */
6034 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6035 b->ops->print_one (b, last_loc);
6036 else
6037 {
6038 if (is_watchpoint (b))
6039 {
6040 struct watchpoint *w = (struct watchpoint *) b;
6041
6042 /* Field 4, the address, is omitted (which makes the columns
6043 not line up too nicely with the headers, but the effect
6044 is relatively readable). */
6045 if (opts.addressprint)
6046 uiout->field_skip ("addr");
6047 annotate_field (5);
6048 uiout->field_string ("what", w->exp_string);
6049 }
6050 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6051 || is_ada_exception_catchpoint (b))
6052 {
6053 if (opts.addressprint)
6054 {
6055 annotate_field (4);
6056 if (header_of_multiple)
6057 uiout->field_string ("addr", "<MULTIPLE>",
6058 metadata_style.style ());
6059 else if (b->loc == NULL || loc->shlib_disabled)
6060 uiout->field_string ("addr", "<PENDING>",
6061 metadata_style.style ());
6062 else
6063 uiout->field_core_addr ("addr",
6064 loc->gdbarch, loc->address);
6065 }
6066 annotate_field (5);
6067 if (!header_of_multiple)
6068 print_breakpoint_location (b, loc);
6069 if (b->loc)
6070 *last_loc = b->loc;
6071 }
6072 }
6073
6074 if (loc != NULL && !header_of_multiple)
6075 {
6076 std::vector<int> inf_nums;
6077 int mi_only = 1;
6078
6079 for (inferior *inf : all_inferiors ())
6080 {
6081 if (inf->pspace == loc->pspace)
6082 inf_nums.push_back (inf->num);
6083 }
6084
6085 /* For backward compatibility, don't display inferiors in CLI unless
6086 there are several. Always display for MI. */
6087 if (allflag
6088 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6089 && (program_spaces.size () > 1
6090 || number_of_inferiors () > 1)
6091 /* LOC is for existing B, it cannot be in
6092 moribund_locations and thus having NULL OWNER. */
6093 && loc->owner->type != bp_catchpoint))
6094 mi_only = 0;
6095 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6096 }
6097
6098 if (!part_of_multiple)
6099 {
6100 if (b->thread != -1)
6101 {
6102 /* FIXME: This seems to be redundant and lost here; see the
6103 "stop only in" line a little further down. */
6104 uiout->text (" thread ");
6105 uiout->field_signed ("thread", b->thread);
6106 }
6107 else if (b->task != 0)
6108 {
6109 uiout->text (" task ");
6110 uiout->field_signed ("task", b->task);
6111 }
6112 }
6113
6114 uiout->text ("\n");
6115
6116 if (!part_of_multiple)
6117 b->ops->print_one_detail (b, uiout);
6118
6119 if (part_of_multiple && frame_id_p (b->frame_id))
6120 {
6121 annotate_field (6);
6122 uiout->text ("\tstop only in stack frame at ");
6123 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6124 the frame ID. */
6125 uiout->field_core_addr ("frame",
6126 b->gdbarch, b->frame_id.stack_addr);
6127 uiout->text ("\n");
6128 }
6129
6130 if (!part_of_multiple && b->cond_string)
6131 {
6132 annotate_field (7);
6133 if (is_tracepoint (b))
6134 uiout->text ("\ttrace only if ");
6135 else
6136 uiout->text ("\tstop only if ");
6137 uiout->field_string ("cond", b->cond_string);
6138
6139 /* Print whether the target is doing the breakpoint's condition
6140 evaluation. If GDB is doing the evaluation, don't print anything. */
6141 if (is_breakpoint (b)
6142 && breakpoint_condition_evaluation_mode ()
6143 == condition_evaluation_target)
6144 {
6145 uiout->message (" (%pF evals)",
6146 string_field ("evaluated-by",
6147 bp_condition_evaluator (b)));
6148 }
6149 uiout->text ("\n");
6150 }
6151
6152 if (!part_of_multiple && b->thread != -1)
6153 {
6154 /* FIXME should make an annotation for this. */
6155 uiout->text ("\tstop only in thread ");
6156 if (uiout->is_mi_like_p ())
6157 uiout->field_signed ("thread", b->thread);
6158 else
6159 {
6160 struct thread_info *thr = find_thread_global_id (b->thread);
6161
6162 uiout->field_string ("thread", print_thread_id (thr));
6163 }
6164 uiout->text ("\n");
6165 }
6166
6167 if (!part_of_multiple)
6168 {
6169 if (b->hit_count)
6170 {
6171 /* FIXME should make an annotation for this. */
6172 if (is_catchpoint (b))
6173 uiout->text ("\tcatchpoint");
6174 else if (is_tracepoint (b))
6175 uiout->text ("\ttracepoint");
6176 else
6177 uiout->text ("\tbreakpoint");
6178 uiout->text (" already hit ");
6179 uiout->field_signed ("times", b->hit_count);
6180 if (b->hit_count == 1)
6181 uiout->text (" time\n");
6182 else
6183 uiout->text (" times\n");
6184 }
6185 else
6186 {
6187 /* Output the count also if it is zero, but only if this is mi. */
6188 if (uiout->is_mi_like_p ())
6189 uiout->field_signed ("times", b->hit_count);
6190 }
6191 }
6192
6193 if (!part_of_multiple && b->ignore_count)
6194 {
6195 annotate_field (8);
6196 uiout->message ("\tignore next %pF hits\n",
6197 signed_field ("ignore", b->ignore_count));
6198 }
6199
6200 /* Note that an enable count of 1 corresponds to "enable once"
6201 behavior, which is reported by the combination of enablement and
6202 disposition, so we don't need to mention it here. */
6203 if (!part_of_multiple && b->enable_count > 1)
6204 {
6205 annotate_field (8);
6206 uiout->text ("\tdisable after ");
6207 /* Tweak the wording to clarify that ignore and enable counts
6208 are distinct, and have additive effect. */
6209 if (b->ignore_count)
6210 uiout->text ("additional ");
6211 else
6212 uiout->text ("next ");
6213 uiout->field_signed ("enable", b->enable_count);
6214 uiout->text (" hits\n");
6215 }
6216
6217 if (!part_of_multiple && is_tracepoint (b))
6218 {
6219 struct tracepoint *tp = (struct tracepoint *) b;
6220
6221 if (tp->traceframe_usage)
6222 {
6223 uiout->text ("\ttrace buffer usage ");
6224 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6225 uiout->text (" bytes\n");
6226 }
6227 }
6228
6229 l = b->commands ? b->commands.get () : NULL;
6230 if (!part_of_multiple && l)
6231 {
6232 annotate_field (9);
6233 ui_out_emit_tuple tuple_emitter (uiout, "script");
6234 print_command_lines (uiout, l, 4);
6235 }
6236
6237 if (is_tracepoint (b))
6238 {
6239 struct tracepoint *t = (struct tracepoint *) b;
6240
6241 if (!part_of_multiple && t->pass_count)
6242 {
6243 annotate_field (10);
6244 uiout->text ("\tpass count ");
6245 uiout->field_signed ("pass", t->pass_count);
6246 uiout->text (" \n");
6247 }
6248
6249 /* Don't display it when tracepoint or tracepoint location is
6250 pending. */
6251 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6252 {
6253 annotate_field (11);
6254
6255 if (uiout->is_mi_like_p ())
6256 uiout->field_string ("installed",
6257 loc->inserted ? "y" : "n");
6258 else
6259 {
6260 if (loc->inserted)
6261 uiout->text ("\t");
6262 else
6263 uiout->text ("\tnot ");
6264 uiout->text ("installed on target\n");
6265 }
6266 }
6267 }
6268
6269 if (uiout->is_mi_like_p () && !part_of_multiple)
6270 {
6271 if (is_watchpoint (b))
6272 {
6273 struct watchpoint *w = (struct watchpoint *) b;
6274
6275 uiout->field_string ("original-location", w->exp_string);
6276 }
6277 else if (b->location != NULL
6278 && event_location_to_string (b->location.get ()) != NULL)
6279 uiout->field_string ("original-location",
6280 event_location_to_string (b->location.get ()));
6281 }
6282 }
6283
6284 /* See breakpoint.h. */
6285
6286 bool fix_multi_location_breakpoint_output_globally = false;
6287
6288 static void
6289 print_one_breakpoint (struct breakpoint *b,
6290 struct bp_location **last_loc,
6291 int allflag)
6292 {
6293 struct ui_out *uiout = current_uiout;
6294 bool use_fixed_output
6295 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6296 || fix_multi_location_breakpoint_output_globally);
6297
6298 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6299 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6300
6301 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6302 are outside. */
6303 if (!use_fixed_output)
6304 bkpt_tuple_emitter.reset ();
6305
6306 /* If this breakpoint has custom print function,
6307 it's already printed. Otherwise, print individual
6308 locations, if any. */
6309 if (b->ops == NULL
6310 || b->ops->print_one == NULL
6311 || allflag)
6312 {
6313 /* If breakpoint has a single location that is disabled, we
6314 print it as if it had several locations, since otherwise it's
6315 hard to represent "breakpoint enabled, location disabled"
6316 situation.
6317
6318 Note that while hardware watchpoints have several locations
6319 internally, that's not a property exposed to users.
6320
6321 Likewise, while catchpoints may be implemented with
6322 breakpoints (e.g., catch throw), that's not a property
6323 exposed to users. We do however display the internal
6324 breakpoint locations with "maint info breakpoints". */
6325 if (!is_hardware_watchpoint (b)
6326 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6327 || is_ada_exception_catchpoint (b))
6328 && (allflag
6329 || (b->loc && (b->loc->next || !b->loc->enabled))))
6330 {
6331 gdb::optional<ui_out_emit_list> locations_list;
6332
6333 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6334 MI record. For later versions, place breakpoint locations in a
6335 list. */
6336 if (uiout->is_mi_like_p () && use_fixed_output)
6337 locations_list.emplace (uiout, "locations");
6338
6339 int n = 1;
6340 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6341 {
6342 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6343 print_one_breakpoint_location (b, loc, n, last_loc,
6344 allflag, allflag);
6345 }
6346 }
6347 }
6348 }
6349
6350 static int
6351 breakpoint_address_bits (struct breakpoint *b)
6352 {
6353 int print_address_bits = 0;
6354 struct bp_location *loc;
6355
6356 /* Software watchpoints that aren't watching memory don't have an
6357 address to print. */
6358 if (is_no_memory_software_watchpoint (b))
6359 return 0;
6360
6361 for (loc = b->loc; loc; loc = loc->next)
6362 {
6363 int addr_bit;
6364
6365 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6366 if (addr_bit > print_address_bits)
6367 print_address_bits = addr_bit;
6368 }
6369
6370 return print_address_bits;
6371 }
6372
6373 /* See breakpoint.h. */
6374
6375 void
6376 print_breakpoint (breakpoint *b)
6377 {
6378 struct bp_location *dummy_loc = NULL;
6379 print_one_breakpoint (b, &dummy_loc, 0);
6380 }
6381
6382 /* Return true if this breakpoint was set by the user, false if it is
6383 internal or momentary. */
6384
6385 int
6386 user_breakpoint_p (struct breakpoint *b)
6387 {
6388 return b->number > 0;
6389 }
6390
6391 /* See breakpoint.h. */
6392
6393 int
6394 pending_breakpoint_p (struct breakpoint *b)
6395 {
6396 return b->loc == NULL;
6397 }
6398
6399 /* Print information on breakpoints (including watchpoints and tracepoints).
6400
6401 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6402 understood by number_or_range_parser. Only breakpoints included in this
6403 list are then printed.
6404
6405 If SHOW_INTERNAL is true, print internal breakpoints.
6406
6407 If FILTER is non-NULL, call it on each breakpoint and only include the
6408 ones for which it returns true.
6409
6410 Return the total number of breakpoints listed. */
6411
6412 static int
6413 breakpoint_1 (const char *bp_num_list, bool show_internal,
6414 bool (*filter) (const struct breakpoint *))
6415 {
6416 struct breakpoint *b;
6417 struct bp_location *last_loc = NULL;
6418 int nr_printable_breakpoints;
6419 struct value_print_options opts;
6420 int print_address_bits = 0;
6421 int print_type_col_width = 14;
6422 struct ui_out *uiout = current_uiout;
6423
6424 get_user_print_options (&opts);
6425
6426 /* Compute the number of rows in the table, as well as the size
6427 required for address fields. */
6428 nr_printable_breakpoints = 0;
6429 ALL_BREAKPOINTS (b)
6430 {
6431 /* If we have a filter, only list the breakpoints it accepts. */
6432 if (filter && !filter (b))
6433 continue;
6434
6435 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6436 accept. Skip the others. */
6437 if (bp_num_list != NULL && *bp_num_list != '\0')
6438 {
6439 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6440 continue;
6441 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6442 continue;
6443 }
6444
6445 if (show_internal || user_breakpoint_p (b))
6446 {
6447 int addr_bit, type_len;
6448
6449 addr_bit = breakpoint_address_bits (b);
6450 if (addr_bit > print_address_bits)
6451 print_address_bits = addr_bit;
6452
6453 type_len = strlen (bptype_string (b->type));
6454 if (type_len > print_type_col_width)
6455 print_type_col_width = type_len;
6456
6457 nr_printable_breakpoints++;
6458 }
6459 }
6460
6461 {
6462 ui_out_emit_table table_emitter (uiout,
6463 opts.addressprint ? 6 : 5,
6464 nr_printable_breakpoints,
6465 "BreakpointTable");
6466
6467 if (nr_printable_breakpoints > 0)
6468 annotate_breakpoints_headers ();
6469 if (nr_printable_breakpoints > 0)
6470 annotate_field (0);
6471 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6472 if (nr_printable_breakpoints > 0)
6473 annotate_field (1);
6474 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6475 if (nr_printable_breakpoints > 0)
6476 annotate_field (2);
6477 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6478 if (nr_printable_breakpoints > 0)
6479 annotate_field (3);
6480 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6481 if (opts.addressprint)
6482 {
6483 if (nr_printable_breakpoints > 0)
6484 annotate_field (4);
6485 if (print_address_bits <= 32)
6486 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6487 else
6488 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6489 }
6490 if (nr_printable_breakpoints > 0)
6491 annotate_field (5);
6492 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6493 uiout->table_body ();
6494 if (nr_printable_breakpoints > 0)
6495 annotate_breakpoints_table ();
6496
6497 ALL_BREAKPOINTS (b)
6498 {
6499 QUIT;
6500 /* If we have a filter, only list the breakpoints it accepts. */
6501 if (filter && !filter (b))
6502 continue;
6503
6504 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6505 accept. Skip the others. */
6506
6507 if (bp_num_list != NULL && *bp_num_list != '\0')
6508 {
6509 if (show_internal) /* maintenance info breakpoint */
6510 {
6511 if (parse_and_eval_long (bp_num_list) != b->number)
6512 continue;
6513 }
6514 else /* all others */
6515 {
6516 if (!number_is_in_list (bp_num_list, b->number))
6517 continue;
6518 }
6519 }
6520 /* We only print out user settable breakpoints unless the
6521 show_internal is set. */
6522 if (show_internal || user_breakpoint_p (b))
6523 print_one_breakpoint (b, &last_loc, show_internal);
6524 }
6525 }
6526
6527 if (nr_printable_breakpoints == 0)
6528 {
6529 /* If there's a filter, let the caller decide how to report
6530 empty list. */
6531 if (!filter)
6532 {
6533 if (bp_num_list == NULL || *bp_num_list == '\0')
6534 uiout->message ("No breakpoints or watchpoints.\n");
6535 else
6536 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6537 bp_num_list);
6538 }
6539 }
6540 else
6541 {
6542 if (last_loc && !server_command)
6543 set_next_address (last_loc->gdbarch, last_loc->address);
6544 }
6545
6546 /* FIXME? Should this be moved up so that it is only called when
6547 there have been breakpoints? */
6548 annotate_breakpoints_table_end ();
6549
6550 return nr_printable_breakpoints;
6551 }
6552
6553 /* Display the value of default-collect in a way that is generally
6554 compatible with the breakpoint list. */
6555
6556 static void
6557 default_collect_info (void)
6558 {
6559 struct ui_out *uiout = current_uiout;
6560
6561 /* If it has no value (which is frequently the case), say nothing; a
6562 message like "No default-collect." gets in user's face when it's
6563 not wanted. */
6564 if (!*default_collect)
6565 return;
6566
6567 /* The following phrase lines up nicely with per-tracepoint collect
6568 actions. */
6569 uiout->text ("default collect ");
6570 uiout->field_string ("default-collect", default_collect);
6571 uiout->text (" \n");
6572 }
6573
6574 static void
6575 info_breakpoints_command (const char *args, int from_tty)
6576 {
6577 breakpoint_1 (args, false, NULL);
6578
6579 default_collect_info ();
6580 }
6581
6582 static void
6583 info_watchpoints_command (const char *args, int from_tty)
6584 {
6585 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6586 struct ui_out *uiout = current_uiout;
6587
6588 if (num_printed == 0)
6589 {
6590 if (args == NULL || *args == '\0')
6591 uiout->message ("No watchpoints.\n");
6592 else
6593 uiout->message ("No watchpoint matching '%s'.\n", args);
6594 }
6595 }
6596
6597 static void
6598 maintenance_info_breakpoints (const char *args, int from_tty)
6599 {
6600 breakpoint_1 (args, true, NULL);
6601
6602 default_collect_info ();
6603 }
6604
6605 static int
6606 breakpoint_has_pc (struct breakpoint *b,
6607 struct program_space *pspace,
6608 CORE_ADDR pc, struct obj_section *section)
6609 {
6610 struct bp_location *bl = b->loc;
6611
6612 for (; bl; bl = bl->next)
6613 {
6614 if (bl->pspace == pspace
6615 && bl->address == pc
6616 && (!overlay_debugging || bl->section == section))
6617 return 1;
6618 }
6619 return 0;
6620 }
6621
6622 /* Print a message describing any user-breakpoints set at PC. This
6623 concerns with logical breakpoints, so we match program spaces, not
6624 address spaces. */
6625
6626 static void
6627 describe_other_breakpoints (struct gdbarch *gdbarch,
6628 struct program_space *pspace, CORE_ADDR pc,
6629 struct obj_section *section, int thread)
6630 {
6631 int others = 0;
6632 struct breakpoint *b;
6633
6634 ALL_BREAKPOINTS (b)
6635 others += (user_breakpoint_p (b)
6636 && breakpoint_has_pc (b, pspace, pc, section));
6637 if (others > 0)
6638 {
6639 if (others == 1)
6640 printf_filtered (_("Note: breakpoint "));
6641 else /* if (others == ???) */
6642 printf_filtered (_("Note: breakpoints "));
6643 ALL_BREAKPOINTS (b)
6644 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6645 {
6646 others--;
6647 printf_filtered ("%d", b->number);
6648 if (b->thread == -1 && thread != -1)
6649 printf_filtered (" (all threads)");
6650 else if (b->thread != -1)
6651 printf_filtered (" (thread %d)", b->thread);
6652 printf_filtered ("%s%s ",
6653 ((b->enable_state == bp_disabled
6654 || b->enable_state == bp_call_disabled)
6655 ? " (disabled)"
6656 : ""),
6657 (others > 1) ? ","
6658 : ((others == 1) ? " and" : ""));
6659 }
6660 current_uiout->message (_("also set at pc %ps.\n"),
6661 styled_string (address_style.style (),
6662 paddress (gdbarch, pc)));
6663 }
6664 }
6665 \f
6666
6667 /* Return true iff it is meaningful to use the address member of LOC.
6668 For some breakpoint types, the locations' address members are
6669 irrelevant and it makes no sense to attempt to compare them to
6670 other addresses (or use them for any other purpose either).
6671
6672 More specifically, software watchpoints and catchpoints that are
6673 not backed by breakpoints always have a zero valued location
6674 address and we don't want to mark breakpoints of any of these types
6675 to be a duplicate of an actual breakpoint location at address
6676 zero. */
6677
6678 static bool
6679 bl_address_is_meaningful (bp_location *loc)
6680 {
6681 return loc->loc_type != bp_loc_other;
6682 }
6683
6684 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6685 true if LOC1 and LOC2 represent the same watchpoint location. */
6686
6687 static int
6688 watchpoint_locations_match (struct bp_location *loc1,
6689 struct bp_location *loc2)
6690 {
6691 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6692 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6693
6694 /* Both of them must exist. */
6695 gdb_assert (w1 != NULL);
6696 gdb_assert (w2 != NULL);
6697
6698 /* If the target can evaluate the condition expression in hardware,
6699 then we we need to insert both watchpoints even if they are at
6700 the same place. Otherwise the watchpoint will only trigger when
6701 the condition of whichever watchpoint was inserted evaluates to
6702 true, not giving a chance for GDB to check the condition of the
6703 other watchpoint. */
6704 if ((w1->cond_exp
6705 && target_can_accel_watchpoint_condition (loc1->address,
6706 loc1->length,
6707 loc1->watchpoint_type,
6708 w1->cond_exp.get ()))
6709 || (w2->cond_exp
6710 && target_can_accel_watchpoint_condition (loc2->address,
6711 loc2->length,
6712 loc2->watchpoint_type,
6713 w2->cond_exp.get ())))
6714 return 0;
6715
6716 /* Note that this checks the owner's type, not the location's. In
6717 case the target does not support read watchpoints, but does
6718 support access watchpoints, we'll have bp_read_watchpoint
6719 watchpoints with hw_access locations. Those should be considered
6720 duplicates of hw_read locations. The hw_read locations will
6721 become hw_access locations later. */
6722 return (loc1->owner->type == loc2->owner->type
6723 && loc1->pspace->aspace == loc2->pspace->aspace
6724 && loc1->address == loc2->address
6725 && loc1->length == loc2->length);
6726 }
6727
6728 /* See breakpoint.h. */
6729
6730 int
6731 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6732 const address_space *aspace2, CORE_ADDR addr2)
6733 {
6734 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6735 || aspace1 == aspace2)
6736 && addr1 == addr2);
6737 }
6738
6739 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6740 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6741 matches ASPACE2. On targets that have global breakpoints, the address
6742 space doesn't really matter. */
6743
6744 static int
6745 breakpoint_address_match_range (const address_space *aspace1,
6746 CORE_ADDR addr1,
6747 int len1, const address_space *aspace2,
6748 CORE_ADDR addr2)
6749 {
6750 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6751 || aspace1 == aspace2)
6752 && addr2 >= addr1 && addr2 < addr1 + len1);
6753 }
6754
6755 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6756 a ranged breakpoint. In most targets, a match happens only if ASPACE
6757 matches the breakpoint's address space. On targets that have global
6758 breakpoints, the address space doesn't really matter. */
6759
6760 static int
6761 breakpoint_location_address_match (struct bp_location *bl,
6762 const address_space *aspace,
6763 CORE_ADDR addr)
6764 {
6765 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6766 aspace, addr)
6767 || (bl->length
6768 && breakpoint_address_match_range (bl->pspace->aspace,
6769 bl->address, bl->length,
6770 aspace, addr)));
6771 }
6772
6773 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6774 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6775 match happens only if ASPACE matches the breakpoint's address
6776 space. On targets that have global breakpoints, the address space
6777 doesn't really matter. */
6778
6779 static int
6780 breakpoint_location_address_range_overlap (struct bp_location *bl,
6781 const address_space *aspace,
6782 CORE_ADDR addr, int len)
6783 {
6784 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6785 || bl->pspace->aspace == aspace)
6786 {
6787 int bl_len = bl->length != 0 ? bl->length : 1;
6788
6789 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6790 return 1;
6791 }
6792 return 0;
6793 }
6794
6795 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6796 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6797 true, otherwise returns false. */
6798
6799 static int
6800 tracepoint_locations_match (struct bp_location *loc1,
6801 struct bp_location *loc2)
6802 {
6803 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6804 /* Since tracepoint locations are never duplicated with others', tracepoint
6805 locations at the same address of different tracepoints are regarded as
6806 different locations. */
6807 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6808 else
6809 return 0;
6810 }
6811
6812 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6813 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6814 the same location. */
6815
6816 static int
6817 breakpoint_locations_match (struct bp_location *loc1,
6818 struct bp_location *loc2)
6819 {
6820 int hw_point1, hw_point2;
6821
6822 /* Both of them must not be in moribund_locations. */
6823 gdb_assert (loc1->owner != NULL);
6824 gdb_assert (loc2->owner != NULL);
6825
6826 hw_point1 = is_hardware_watchpoint (loc1->owner);
6827 hw_point2 = is_hardware_watchpoint (loc2->owner);
6828
6829 if (hw_point1 != hw_point2)
6830 return 0;
6831 else if (hw_point1)
6832 return watchpoint_locations_match (loc1, loc2);
6833 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6834 return tracepoint_locations_match (loc1, loc2);
6835 else
6836 /* We compare bp_location.length in order to cover ranged breakpoints. */
6837 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6838 loc2->pspace->aspace, loc2->address)
6839 && loc1->length == loc2->length);
6840 }
6841
6842 static void
6843 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6844 int bnum, int have_bnum)
6845 {
6846 /* The longest string possibly returned by hex_string_custom
6847 is 50 chars. These must be at least that big for safety. */
6848 char astr1[64];
6849 char astr2[64];
6850
6851 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6852 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6853 if (have_bnum)
6854 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6855 bnum, astr1, astr2);
6856 else
6857 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6858 }
6859
6860 /* Adjust a breakpoint's address to account for architectural
6861 constraints on breakpoint placement. Return the adjusted address.
6862 Note: Very few targets require this kind of adjustment. For most
6863 targets, this function is simply the identity function. */
6864
6865 static CORE_ADDR
6866 adjust_breakpoint_address (struct gdbarch *gdbarch,
6867 CORE_ADDR bpaddr, enum bptype bptype)
6868 {
6869 if (bptype == bp_watchpoint
6870 || bptype == bp_hardware_watchpoint
6871 || bptype == bp_read_watchpoint
6872 || bptype == bp_access_watchpoint
6873 || bptype == bp_catchpoint)
6874 {
6875 /* Watchpoints and the various bp_catch_* eventpoints should not
6876 have their addresses modified. */
6877 return bpaddr;
6878 }
6879 else if (bptype == bp_single_step)
6880 {
6881 /* Single-step breakpoints should not have their addresses
6882 modified. If there's any architectural constrain that
6883 applies to this address, then it should have already been
6884 taken into account when the breakpoint was created in the
6885 first place. If we didn't do this, stepping through e.g.,
6886 Thumb-2 IT blocks would break. */
6887 return bpaddr;
6888 }
6889 else
6890 {
6891 CORE_ADDR adjusted_bpaddr = bpaddr;
6892
6893 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6894 {
6895 /* Some targets have architectural constraints on the placement
6896 of breakpoint instructions. Obtain the adjusted address. */
6897 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6898 }
6899
6900 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
6901
6902 /* An adjusted breakpoint address can significantly alter
6903 a user's expectations. Print a warning if an adjustment
6904 is required. */
6905 if (adjusted_bpaddr != bpaddr)
6906 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6907
6908 return adjusted_bpaddr;
6909 }
6910 }
6911
6912 static bp_loc_type
6913 bp_location_from_bp_type (bptype type)
6914 {
6915 switch (type)
6916 {
6917 case bp_breakpoint:
6918 case bp_single_step:
6919 case bp_until:
6920 case bp_finish:
6921 case bp_longjmp:
6922 case bp_longjmp_resume:
6923 case bp_longjmp_call_dummy:
6924 case bp_exception:
6925 case bp_exception_resume:
6926 case bp_step_resume:
6927 case bp_hp_step_resume:
6928 case bp_watchpoint_scope:
6929 case bp_call_dummy:
6930 case bp_std_terminate:
6931 case bp_shlib_event:
6932 case bp_thread_event:
6933 case bp_overlay_event:
6934 case bp_jit_event:
6935 case bp_longjmp_master:
6936 case bp_std_terminate_master:
6937 case bp_exception_master:
6938 case bp_gnu_ifunc_resolver:
6939 case bp_gnu_ifunc_resolver_return:
6940 case bp_dprintf:
6941 return bp_loc_software_breakpoint;
6942 case bp_hardware_breakpoint:
6943 return bp_loc_hardware_breakpoint;
6944 case bp_hardware_watchpoint:
6945 case bp_read_watchpoint:
6946 case bp_access_watchpoint:
6947 return bp_loc_hardware_watchpoint;
6948 case bp_watchpoint:
6949 case bp_catchpoint:
6950 case bp_tracepoint:
6951 case bp_fast_tracepoint:
6952 case bp_static_tracepoint:
6953 return bp_loc_other;
6954 default:
6955 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6956 }
6957 }
6958
6959 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6960 {
6961 this->owner = owner;
6962 this->cond_bytecode = NULL;
6963 this->shlib_disabled = 0;
6964 this->enabled = 1;
6965
6966 this->loc_type = type;
6967
6968 if (this->loc_type == bp_loc_software_breakpoint
6969 || this->loc_type == bp_loc_hardware_breakpoint)
6970 mark_breakpoint_location_modified (this);
6971
6972 this->refc = 1;
6973 }
6974
6975 bp_location::bp_location (breakpoint *owner)
6976 : bp_location::bp_location (owner,
6977 bp_location_from_bp_type (owner->type))
6978 {
6979 }
6980
6981 /* Allocate a struct bp_location. */
6982
6983 static struct bp_location *
6984 allocate_bp_location (struct breakpoint *bpt)
6985 {
6986 return bpt->ops->allocate_location (bpt);
6987 }
6988
6989 static void
6990 free_bp_location (struct bp_location *loc)
6991 {
6992 delete loc;
6993 }
6994
6995 /* Increment reference count. */
6996
6997 static void
6998 incref_bp_location (struct bp_location *bl)
6999 {
7000 ++bl->refc;
7001 }
7002
7003 /* Decrement reference count. If the reference count reaches 0,
7004 destroy the bp_location. Sets *BLP to NULL. */
7005
7006 static void
7007 decref_bp_location (struct bp_location **blp)
7008 {
7009 gdb_assert ((*blp)->refc > 0);
7010
7011 if (--(*blp)->refc == 0)
7012 free_bp_location (*blp);
7013 *blp = NULL;
7014 }
7015
7016 /* Add breakpoint B at the end of the global breakpoint chain. */
7017
7018 static breakpoint *
7019 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7020 {
7021 struct breakpoint *b1;
7022 struct breakpoint *result = b.get ();
7023
7024 /* Add this breakpoint to the end of the chain so that a list of
7025 breakpoints will come out in order of increasing numbers. */
7026
7027 b1 = breakpoint_chain;
7028 if (b1 == 0)
7029 breakpoint_chain = b.release ();
7030 else
7031 {
7032 while (b1->next)
7033 b1 = b1->next;
7034 b1->next = b.release ();
7035 }
7036
7037 return result;
7038 }
7039
7040 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7041
7042 static void
7043 init_raw_breakpoint_without_location (struct breakpoint *b,
7044 struct gdbarch *gdbarch,
7045 enum bptype bptype,
7046 const struct breakpoint_ops *ops)
7047 {
7048 gdb_assert (ops != NULL);
7049
7050 b->ops = ops;
7051 b->type = bptype;
7052 b->gdbarch = gdbarch;
7053 b->language = current_language->la_language;
7054 b->input_radix = input_radix;
7055 b->related_breakpoint = b;
7056 }
7057
7058 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7059 that has type BPTYPE and has no locations as yet. */
7060
7061 static struct breakpoint *
7062 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7063 enum bptype bptype,
7064 const struct breakpoint_ops *ops)
7065 {
7066 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7067
7068 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7069 return add_to_breakpoint_chain (std::move (b));
7070 }
7071
7072 /* Initialize loc->function_name. */
7073
7074 static void
7075 set_breakpoint_location_function (struct bp_location *loc)
7076 {
7077 gdb_assert (loc->owner != NULL);
7078
7079 if (loc->owner->type == bp_breakpoint
7080 || loc->owner->type == bp_hardware_breakpoint
7081 || is_tracepoint (loc->owner))
7082 {
7083 const char *function_name;
7084
7085 if (loc->msymbol != NULL
7086 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7087 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7088 {
7089 struct breakpoint *b = loc->owner;
7090
7091 function_name = loc->msymbol->linkage_name ();
7092
7093 if (b->type == bp_breakpoint && b->loc == loc
7094 && loc->next == NULL && b->related_breakpoint == b)
7095 {
7096 /* Create only the whole new breakpoint of this type but do not
7097 mess more complicated breakpoints with multiple locations. */
7098 b->type = bp_gnu_ifunc_resolver;
7099 /* Remember the resolver's address for use by the return
7100 breakpoint. */
7101 loc->related_address = loc->address;
7102 }
7103 }
7104 else
7105 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7106
7107 if (function_name)
7108 loc->function_name = xstrdup (function_name);
7109 }
7110 }
7111
7112 /* Attempt to determine architecture of location identified by SAL. */
7113 struct gdbarch *
7114 get_sal_arch (struct symtab_and_line sal)
7115 {
7116 if (sal.section)
7117 return sal.section->objfile->arch ();
7118 if (sal.symtab)
7119 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7120
7121 return NULL;
7122 }
7123
7124 /* Low level routine for partially initializing a breakpoint of type
7125 BPTYPE. The newly created breakpoint's address, section, source
7126 file name, and line number are provided by SAL.
7127
7128 It is expected that the caller will complete the initialization of
7129 the newly created breakpoint struct as well as output any status
7130 information regarding the creation of a new breakpoint. */
7131
7132 static void
7133 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7134 struct symtab_and_line sal, enum bptype bptype,
7135 const struct breakpoint_ops *ops)
7136 {
7137 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7138
7139 add_location_to_breakpoint (b, &sal);
7140
7141 if (bptype != bp_catchpoint)
7142 gdb_assert (sal.pspace != NULL);
7143
7144 /* Store the program space that was used to set the breakpoint,
7145 except for ordinary breakpoints, which are independent of the
7146 program space. */
7147 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7148 b->pspace = sal.pspace;
7149 }
7150
7151 /* set_raw_breakpoint is a low level routine for allocating and
7152 partially initializing a breakpoint of type BPTYPE. The newly
7153 created breakpoint's address, section, source file name, and line
7154 number are provided by SAL. The newly created and partially
7155 initialized breakpoint is added to the breakpoint chain and
7156 is also returned as the value of this function.
7157
7158 It is expected that the caller will complete the initialization of
7159 the newly created breakpoint struct as well as output any status
7160 information regarding the creation of a new breakpoint. In
7161 particular, set_raw_breakpoint does NOT set the breakpoint
7162 number! Care should be taken to not allow an error to occur
7163 prior to completing the initialization of the breakpoint. If this
7164 should happen, a bogus breakpoint will be left on the chain. */
7165
7166 struct breakpoint *
7167 set_raw_breakpoint (struct gdbarch *gdbarch,
7168 struct symtab_and_line sal, enum bptype bptype,
7169 const struct breakpoint_ops *ops)
7170 {
7171 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7172
7173 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7174 return add_to_breakpoint_chain (std::move (b));
7175 }
7176
7177 /* Call this routine when stepping and nexting to enable a breakpoint
7178 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7179 initiated the operation. */
7180
7181 void
7182 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7183 {
7184 struct breakpoint *b, *b_tmp;
7185 int thread = tp->global_num;
7186
7187 /* To avoid having to rescan all objfile symbols at every step,
7188 we maintain a list of continually-inserted but always disabled
7189 longjmp "master" breakpoints. Here, we simply create momentary
7190 clones of those and enable them for the requested thread. */
7191 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7192 if (b->pspace == current_program_space
7193 && (b->type == bp_longjmp_master
7194 || b->type == bp_exception_master))
7195 {
7196 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7197 struct breakpoint *clone;
7198
7199 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7200 after their removal. */
7201 clone = momentary_breakpoint_from_master (b, type,
7202 &momentary_breakpoint_ops, 1);
7203 clone->thread = thread;
7204 }
7205
7206 tp->initiating_frame = frame;
7207 }
7208
7209 /* Delete all longjmp breakpoints from THREAD. */
7210 void
7211 delete_longjmp_breakpoint (int thread)
7212 {
7213 struct breakpoint *b, *b_tmp;
7214
7215 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7216 if (b->type == bp_longjmp || b->type == bp_exception)
7217 {
7218 if (b->thread == thread)
7219 delete_breakpoint (b);
7220 }
7221 }
7222
7223 void
7224 delete_longjmp_breakpoint_at_next_stop (int thread)
7225 {
7226 struct breakpoint *b, *b_tmp;
7227
7228 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7229 if (b->type == bp_longjmp || b->type == bp_exception)
7230 {
7231 if (b->thread == thread)
7232 b->disposition = disp_del_at_next_stop;
7233 }
7234 }
7235
7236 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7237 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7238 pointer to any of them. Return NULL if this system cannot place longjmp
7239 breakpoints. */
7240
7241 struct breakpoint *
7242 set_longjmp_breakpoint_for_call_dummy (void)
7243 {
7244 struct breakpoint *b, *retval = NULL;
7245
7246 ALL_BREAKPOINTS (b)
7247 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7248 {
7249 struct breakpoint *new_b;
7250
7251 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7252 &momentary_breakpoint_ops,
7253 1);
7254 new_b->thread = inferior_thread ()->global_num;
7255
7256 /* Link NEW_B into the chain of RETVAL breakpoints. */
7257
7258 gdb_assert (new_b->related_breakpoint == new_b);
7259 if (retval == NULL)
7260 retval = new_b;
7261 new_b->related_breakpoint = retval;
7262 while (retval->related_breakpoint != new_b->related_breakpoint)
7263 retval = retval->related_breakpoint;
7264 retval->related_breakpoint = new_b;
7265 }
7266
7267 return retval;
7268 }
7269
7270 /* Verify all existing dummy frames and their associated breakpoints for
7271 TP. Remove those which can no longer be found in the current frame
7272 stack.
7273
7274 You should call this function only at places where it is safe to currently
7275 unwind the whole stack. Failed stack unwind would discard live dummy
7276 frames. */
7277
7278 void
7279 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7280 {
7281 struct breakpoint *b, *b_tmp;
7282
7283 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7284 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7285 {
7286 struct breakpoint *dummy_b = b->related_breakpoint;
7287
7288 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7289 dummy_b = dummy_b->related_breakpoint;
7290 if (dummy_b->type != bp_call_dummy
7291 || frame_find_by_id (dummy_b->frame_id) != NULL)
7292 continue;
7293
7294 dummy_frame_discard (dummy_b->frame_id, tp);
7295
7296 while (b->related_breakpoint != b)
7297 {
7298 if (b_tmp == b->related_breakpoint)
7299 b_tmp = b->related_breakpoint->next;
7300 delete_breakpoint (b->related_breakpoint);
7301 }
7302 delete_breakpoint (b);
7303 }
7304 }
7305
7306 void
7307 enable_overlay_breakpoints (void)
7308 {
7309 struct breakpoint *b;
7310
7311 ALL_BREAKPOINTS (b)
7312 if (b->type == bp_overlay_event)
7313 {
7314 b->enable_state = bp_enabled;
7315 update_global_location_list (UGLL_MAY_INSERT);
7316 overlay_events_enabled = 1;
7317 }
7318 }
7319
7320 void
7321 disable_overlay_breakpoints (void)
7322 {
7323 struct breakpoint *b;
7324
7325 ALL_BREAKPOINTS (b)
7326 if (b->type == bp_overlay_event)
7327 {
7328 b->enable_state = bp_disabled;
7329 update_global_location_list (UGLL_DONT_INSERT);
7330 overlay_events_enabled = 0;
7331 }
7332 }
7333
7334 /* Set an active std::terminate breakpoint for each std::terminate
7335 master breakpoint. */
7336 void
7337 set_std_terminate_breakpoint (void)
7338 {
7339 struct breakpoint *b, *b_tmp;
7340
7341 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7342 if (b->pspace == current_program_space
7343 && b->type == bp_std_terminate_master)
7344 {
7345 momentary_breakpoint_from_master (b, bp_std_terminate,
7346 &momentary_breakpoint_ops, 1);
7347 }
7348 }
7349
7350 /* Delete all the std::terminate breakpoints. */
7351 void
7352 delete_std_terminate_breakpoint (void)
7353 {
7354 struct breakpoint *b, *b_tmp;
7355
7356 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7357 if (b->type == bp_std_terminate)
7358 delete_breakpoint (b);
7359 }
7360
7361 struct breakpoint *
7362 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7363 {
7364 struct breakpoint *b;
7365
7366 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7367 &internal_breakpoint_ops);
7368
7369 b->enable_state = bp_enabled;
7370 /* location has to be used or breakpoint_re_set will delete me. */
7371 b->location = new_address_location (b->loc->address, NULL, 0);
7372
7373 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7374
7375 return b;
7376 }
7377
7378 struct lang_and_radix
7379 {
7380 enum language lang;
7381 int radix;
7382 };
7383
7384 /* Create a breakpoint for JIT code registration and unregistration. */
7385
7386 struct breakpoint *
7387 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7388 {
7389 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7390 &internal_breakpoint_ops);
7391 }
7392
7393 /* Remove JIT code registration and unregistration breakpoint(s). */
7394
7395 void
7396 remove_jit_event_breakpoints (void)
7397 {
7398 struct breakpoint *b, *b_tmp;
7399
7400 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7401 if (b->type == bp_jit_event
7402 && b->loc->pspace == current_program_space)
7403 delete_breakpoint (b);
7404 }
7405
7406 void
7407 remove_solib_event_breakpoints (void)
7408 {
7409 struct breakpoint *b, *b_tmp;
7410
7411 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7412 if (b->type == bp_shlib_event
7413 && b->loc->pspace == current_program_space)
7414 delete_breakpoint (b);
7415 }
7416
7417 /* See breakpoint.h. */
7418
7419 void
7420 remove_solib_event_breakpoints_at_next_stop (void)
7421 {
7422 struct breakpoint *b, *b_tmp;
7423
7424 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7425 if (b->type == bp_shlib_event
7426 && b->loc->pspace == current_program_space)
7427 b->disposition = disp_del_at_next_stop;
7428 }
7429
7430 /* Helper for create_solib_event_breakpoint /
7431 create_and_insert_solib_event_breakpoint. Allows specifying which
7432 INSERT_MODE to pass through to update_global_location_list. */
7433
7434 static struct breakpoint *
7435 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7436 enum ugll_insert_mode insert_mode)
7437 {
7438 struct breakpoint *b;
7439
7440 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7441 &internal_breakpoint_ops);
7442 update_global_location_list_nothrow (insert_mode);
7443 return b;
7444 }
7445
7446 struct breakpoint *
7447 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7448 {
7449 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7450 }
7451
7452 /* See breakpoint.h. */
7453
7454 struct breakpoint *
7455 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7456 {
7457 struct breakpoint *b;
7458
7459 /* Explicitly tell update_global_location_list to insert
7460 locations. */
7461 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7462 if (!b->loc->inserted)
7463 {
7464 delete_breakpoint (b);
7465 return NULL;
7466 }
7467 return b;
7468 }
7469
7470 /* Disable any breakpoints that are on code in shared libraries. Only
7471 apply to enabled breakpoints, disabled ones can just stay disabled. */
7472
7473 void
7474 disable_breakpoints_in_shlibs (void)
7475 {
7476 struct bp_location *loc, **locp_tmp;
7477
7478 ALL_BP_LOCATIONS (loc, locp_tmp)
7479 {
7480 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7481 struct breakpoint *b = loc->owner;
7482
7483 /* We apply the check to all breakpoints, including disabled for
7484 those with loc->duplicate set. This is so that when breakpoint
7485 becomes enabled, or the duplicate is removed, gdb will try to
7486 insert all breakpoints. If we don't set shlib_disabled here,
7487 we'll try to insert those breakpoints and fail. */
7488 if (((b->type == bp_breakpoint)
7489 || (b->type == bp_jit_event)
7490 || (b->type == bp_hardware_breakpoint)
7491 || (is_tracepoint (b)))
7492 && loc->pspace == current_program_space
7493 && !loc->shlib_disabled
7494 && solib_name_from_address (loc->pspace, loc->address)
7495 )
7496 {
7497 loc->shlib_disabled = 1;
7498 }
7499 }
7500 }
7501
7502 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7503 notification of unloaded_shlib. Only apply to enabled breakpoints,
7504 disabled ones can just stay disabled. */
7505
7506 static void
7507 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7508 {
7509 struct bp_location *loc, **locp_tmp;
7510 int disabled_shlib_breaks = 0;
7511
7512 ALL_BP_LOCATIONS (loc, locp_tmp)
7513 {
7514 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7515 struct breakpoint *b = loc->owner;
7516
7517 if (solib->pspace == loc->pspace
7518 && !loc->shlib_disabled
7519 && (((b->type == bp_breakpoint
7520 || b->type == bp_jit_event
7521 || b->type == bp_hardware_breakpoint)
7522 && (loc->loc_type == bp_loc_hardware_breakpoint
7523 || loc->loc_type == bp_loc_software_breakpoint))
7524 || is_tracepoint (b))
7525 && solib_contains_address_p (solib, loc->address))
7526 {
7527 loc->shlib_disabled = 1;
7528 /* At this point, we cannot rely on remove_breakpoint
7529 succeeding so we must mark the breakpoint as not inserted
7530 to prevent future errors occurring in remove_breakpoints. */
7531 loc->inserted = 0;
7532
7533 /* This may cause duplicate notifications for the same breakpoint. */
7534 gdb::observers::breakpoint_modified.notify (b);
7535
7536 if (!disabled_shlib_breaks)
7537 {
7538 target_terminal::ours_for_output ();
7539 warning (_("Temporarily disabling breakpoints "
7540 "for unloaded shared library \"%s\""),
7541 solib->so_name);
7542 }
7543 disabled_shlib_breaks = 1;
7544 }
7545 }
7546 }
7547
7548 /* Disable any breakpoints and tracepoints in OBJFILE upon
7549 notification of free_objfile. Only apply to enabled breakpoints,
7550 disabled ones can just stay disabled. */
7551
7552 static void
7553 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7554 {
7555 struct breakpoint *b;
7556
7557 if (objfile == NULL)
7558 return;
7559
7560 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7561 managed by the user with add-symbol-file/remove-symbol-file.
7562 Similarly to how breakpoints in shared libraries are handled in
7563 response to "nosharedlibrary", mark breakpoints in such modules
7564 shlib_disabled so they end up uninserted on the next global
7565 location list update. Shared libraries not loaded by the user
7566 aren't handled here -- they're already handled in
7567 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7568 solib_unloaded observer. We skip objfiles that are not
7569 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7570 main objfile). */
7571 if ((objfile->flags & OBJF_SHARED) == 0
7572 || (objfile->flags & OBJF_USERLOADED) == 0)
7573 return;
7574
7575 ALL_BREAKPOINTS (b)
7576 {
7577 struct bp_location *loc;
7578 int bp_modified = 0;
7579
7580 if (!is_breakpoint (b) && !is_tracepoint (b))
7581 continue;
7582
7583 for (loc = b->loc; loc != NULL; loc = loc->next)
7584 {
7585 CORE_ADDR loc_addr = loc->address;
7586
7587 if (loc->loc_type != bp_loc_hardware_breakpoint
7588 && loc->loc_type != bp_loc_software_breakpoint)
7589 continue;
7590
7591 if (loc->shlib_disabled != 0)
7592 continue;
7593
7594 if (objfile->pspace != loc->pspace)
7595 continue;
7596
7597 if (loc->loc_type != bp_loc_hardware_breakpoint
7598 && loc->loc_type != bp_loc_software_breakpoint)
7599 continue;
7600
7601 if (is_addr_in_objfile (loc_addr, objfile))
7602 {
7603 loc->shlib_disabled = 1;
7604 /* At this point, we don't know whether the object was
7605 unmapped from the inferior or not, so leave the
7606 inserted flag alone. We'll handle failure to
7607 uninsert quietly, in case the object was indeed
7608 unmapped. */
7609
7610 mark_breakpoint_location_modified (loc);
7611
7612 bp_modified = 1;
7613 }
7614 }
7615
7616 if (bp_modified)
7617 gdb::observers::breakpoint_modified.notify (b);
7618 }
7619 }
7620
7621 /* FORK & VFORK catchpoints. */
7622
7623 /* An instance of this type is used to represent a fork or vfork
7624 catchpoint. A breakpoint is really of this type iff its ops pointer points
7625 to CATCH_FORK_BREAKPOINT_OPS. */
7626
7627 struct fork_catchpoint : public breakpoint
7628 {
7629 /* Process id of a child process whose forking triggered this
7630 catchpoint. This field is only valid immediately after this
7631 catchpoint has triggered. */
7632 ptid_t forked_inferior_pid;
7633 };
7634
7635 /* Implement the "insert" breakpoint_ops method for fork
7636 catchpoints. */
7637
7638 static int
7639 insert_catch_fork (struct bp_location *bl)
7640 {
7641 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7642 }
7643
7644 /* Implement the "remove" breakpoint_ops method for fork
7645 catchpoints. */
7646
7647 static int
7648 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7649 {
7650 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7651 }
7652
7653 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7654 catchpoints. */
7655
7656 static int
7657 breakpoint_hit_catch_fork (const struct bp_location *bl,
7658 const address_space *aspace, CORE_ADDR bp_addr,
7659 const struct target_waitstatus *ws)
7660 {
7661 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7662
7663 if (ws->kind != TARGET_WAITKIND_FORKED)
7664 return 0;
7665
7666 c->forked_inferior_pid = ws->value.related_pid;
7667 return 1;
7668 }
7669
7670 /* Implement the "print_it" breakpoint_ops method for fork
7671 catchpoints. */
7672
7673 static enum print_stop_action
7674 print_it_catch_fork (bpstat bs)
7675 {
7676 struct ui_out *uiout = current_uiout;
7677 struct breakpoint *b = bs->breakpoint_at;
7678 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7679
7680 annotate_catchpoint (b->number);
7681 maybe_print_thread_hit_breakpoint (uiout);
7682 if (b->disposition == disp_del)
7683 uiout->text ("Temporary catchpoint ");
7684 else
7685 uiout->text ("Catchpoint ");
7686 if (uiout->is_mi_like_p ())
7687 {
7688 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7689 uiout->field_string ("disp", bpdisp_text (b->disposition));
7690 }
7691 uiout->field_signed ("bkptno", b->number);
7692 uiout->text (" (forked process ");
7693 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7694 uiout->text ("), ");
7695 return PRINT_SRC_AND_LOC;
7696 }
7697
7698 /* Implement the "print_one" breakpoint_ops method for fork
7699 catchpoints. */
7700
7701 static void
7702 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7703 {
7704 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7705 struct value_print_options opts;
7706 struct ui_out *uiout = current_uiout;
7707
7708 get_user_print_options (&opts);
7709
7710 /* Field 4, the address, is omitted (which makes the columns not
7711 line up too nicely with the headers, but the effect is relatively
7712 readable). */
7713 if (opts.addressprint)
7714 uiout->field_skip ("addr");
7715 annotate_field (5);
7716 uiout->text ("fork");
7717 if (c->forked_inferior_pid != null_ptid)
7718 {
7719 uiout->text (", process ");
7720 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7721 uiout->spaces (1);
7722 }
7723
7724 if (uiout->is_mi_like_p ())
7725 uiout->field_string ("catch-type", "fork");
7726 }
7727
7728 /* Implement the "print_mention" breakpoint_ops method for fork
7729 catchpoints. */
7730
7731 static void
7732 print_mention_catch_fork (struct breakpoint *b)
7733 {
7734 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7735 }
7736
7737 /* Implement the "print_recreate" breakpoint_ops method for fork
7738 catchpoints. */
7739
7740 static void
7741 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7742 {
7743 fprintf_unfiltered (fp, "catch fork");
7744 print_recreate_thread (b, fp);
7745 }
7746
7747 /* The breakpoint_ops structure to be used in fork catchpoints. */
7748
7749 static struct breakpoint_ops catch_fork_breakpoint_ops;
7750
7751 /* Implement the "insert" breakpoint_ops method for vfork
7752 catchpoints. */
7753
7754 static int
7755 insert_catch_vfork (struct bp_location *bl)
7756 {
7757 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7758 }
7759
7760 /* Implement the "remove" breakpoint_ops method for vfork
7761 catchpoints. */
7762
7763 static int
7764 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7765 {
7766 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7767 }
7768
7769 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7770 catchpoints. */
7771
7772 static int
7773 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7774 const address_space *aspace, CORE_ADDR bp_addr,
7775 const struct target_waitstatus *ws)
7776 {
7777 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7778
7779 if (ws->kind != TARGET_WAITKIND_VFORKED)
7780 return 0;
7781
7782 c->forked_inferior_pid = ws->value.related_pid;
7783 return 1;
7784 }
7785
7786 /* Implement the "print_it" breakpoint_ops method for vfork
7787 catchpoints. */
7788
7789 static enum print_stop_action
7790 print_it_catch_vfork (bpstat bs)
7791 {
7792 struct ui_out *uiout = current_uiout;
7793 struct breakpoint *b = bs->breakpoint_at;
7794 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7795
7796 annotate_catchpoint (b->number);
7797 maybe_print_thread_hit_breakpoint (uiout);
7798 if (b->disposition == disp_del)
7799 uiout->text ("Temporary catchpoint ");
7800 else
7801 uiout->text ("Catchpoint ");
7802 if (uiout->is_mi_like_p ())
7803 {
7804 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7805 uiout->field_string ("disp", bpdisp_text (b->disposition));
7806 }
7807 uiout->field_signed ("bkptno", b->number);
7808 uiout->text (" (vforked process ");
7809 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7810 uiout->text ("), ");
7811 return PRINT_SRC_AND_LOC;
7812 }
7813
7814 /* Implement the "print_one" breakpoint_ops method for vfork
7815 catchpoints. */
7816
7817 static void
7818 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7819 {
7820 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7821 struct value_print_options opts;
7822 struct ui_out *uiout = current_uiout;
7823
7824 get_user_print_options (&opts);
7825 /* Field 4, the address, is omitted (which makes the columns not
7826 line up too nicely with the headers, but the effect is relatively
7827 readable). */
7828 if (opts.addressprint)
7829 uiout->field_skip ("addr");
7830 annotate_field (5);
7831 uiout->text ("vfork");
7832 if (c->forked_inferior_pid != null_ptid)
7833 {
7834 uiout->text (", process ");
7835 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7836 uiout->spaces (1);
7837 }
7838
7839 if (uiout->is_mi_like_p ())
7840 uiout->field_string ("catch-type", "vfork");
7841 }
7842
7843 /* Implement the "print_mention" breakpoint_ops method for vfork
7844 catchpoints. */
7845
7846 static void
7847 print_mention_catch_vfork (struct breakpoint *b)
7848 {
7849 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7850 }
7851
7852 /* Implement the "print_recreate" breakpoint_ops method for vfork
7853 catchpoints. */
7854
7855 static void
7856 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7857 {
7858 fprintf_unfiltered (fp, "catch vfork");
7859 print_recreate_thread (b, fp);
7860 }
7861
7862 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7863
7864 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7865
7866 /* An instance of this type is used to represent an solib catchpoint.
7867 A breakpoint is really of this type iff its ops pointer points to
7868 CATCH_SOLIB_BREAKPOINT_OPS. */
7869
7870 struct solib_catchpoint : public breakpoint
7871 {
7872 ~solib_catchpoint () override;
7873
7874 /* True for "catch load", false for "catch unload". */
7875 unsigned char is_load;
7876
7877 /* Regular expression to match, if any. COMPILED is only valid when
7878 REGEX is non-NULL. */
7879 char *regex;
7880 std::unique_ptr<compiled_regex> compiled;
7881 };
7882
7883 solib_catchpoint::~solib_catchpoint ()
7884 {
7885 xfree (this->regex);
7886 }
7887
7888 static int
7889 insert_catch_solib (struct bp_location *ignore)
7890 {
7891 return 0;
7892 }
7893
7894 static int
7895 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7896 {
7897 return 0;
7898 }
7899
7900 static int
7901 breakpoint_hit_catch_solib (const struct bp_location *bl,
7902 const address_space *aspace,
7903 CORE_ADDR bp_addr,
7904 const struct target_waitstatus *ws)
7905 {
7906 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7907 struct breakpoint *other;
7908
7909 if (ws->kind == TARGET_WAITKIND_LOADED)
7910 return 1;
7911
7912 ALL_BREAKPOINTS (other)
7913 {
7914 struct bp_location *other_bl;
7915
7916 if (other == bl->owner)
7917 continue;
7918
7919 if (other->type != bp_shlib_event)
7920 continue;
7921
7922 if (self->pspace != NULL && other->pspace != self->pspace)
7923 continue;
7924
7925 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7926 {
7927 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7928 return 1;
7929 }
7930 }
7931
7932 return 0;
7933 }
7934
7935 static void
7936 check_status_catch_solib (struct bpstats *bs)
7937 {
7938 struct solib_catchpoint *self
7939 = (struct solib_catchpoint *) bs->breakpoint_at;
7940
7941 if (self->is_load)
7942 {
7943 for (so_list *iter : current_program_space->added_solibs)
7944 {
7945 if (!self->regex
7946 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
7947 return;
7948 }
7949 }
7950 else
7951 {
7952 for (const std::string &iter : current_program_space->deleted_solibs)
7953 {
7954 if (!self->regex
7955 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
7956 return;
7957 }
7958 }
7959
7960 bs->stop = 0;
7961 bs->print_it = print_it_noop;
7962 }
7963
7964 static enum print_stop_action
7965 print_it_catch_solib (bpstat bs)
7966 {
7967 struct breakpoint *b = bs->breakpoint_at;
7968 struct ui_out *uiout = current_uiout;
7969
7970 annotate_catchpoint (b->number);
7971 maybe_print_thread_hit_breakpoint (uiout);
7972 if (b->disposition == disp_del)
7973 uiout->text ("Temporary catchpoint ");
7974 else
7975 uiout->text ("Catchpoint ");
7976 uiout->field_signed ("bkptno", b->number);
7977 uiout->text ("\n");
7978 if (uiout->is_mi_like_p ())
7979 uiout->field_string ("disp", bpdisp_text (b->disposition));
7980 print_solib_event (1);
7981 return PRINT_SRC_AND_LOC;
7982 }
7983
7984 static void
7985 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7986 {
7987 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7988 struct value_print_options opts;
7989 struct ui_out *uiout = current_uiout;
7990
7991 get_user_print_options (&opts);
7992 /* Field 4, the address, is omitted (which makes the columns not
7993 line up too nicely with the headers, but the effect is relatively
7994 readable). */
7995 if (opts.addressprint)
7996 {
7997 annotate_field (4);
7998 uiout->field_skip ("addr");
7999 }
8000
8001 std::string msg;
8002 annotate_field (5);
8003 if (self->is_load)
8004 {
8005 if (self->regex)
8006 msg = string_printf (_("load of library matching %s"), self->regex);
8007 else
8008 msg = _("load of library");
8009 }
8010 else
8011 {
8012 if (self->regex)
8013 msg = string_printf (_("unload of library matching %s"), self->regex);
8014 else
8015 msg = _("unload of library");
8016 }
8017 uiout->field_string ("what", msg);
8018
8019 if (uiout->is_mi_like_p ())
8020 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8021 }
8022
8023 static void
8024 print_mention_catch_solib (struct breakpoint *b)
8025 {
8026 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8027
8028 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8029 self->is_load ? "load" : "unload");
8030 }
8031
8032 static void
8033 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8034 {
8035 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8036
8037 fprintf_unfiltered (fp, "%s %s",
8038 b->disposition == disp_del ? "tcatch" : "catch",
8039 self->is_load ? "load" : "unload");
8040 if (self->regex)
8041 fprintf_unfiltered (fp, " %s", self->regex);
8042 fprintf_unfiltered (fp, "\n");
8043 }
8044
8045 static struct breakpoint_ops catch_solib_breakpoint_ops;
8046
8047 /* Shared helper function (MI and CLI) for creating and installing
8048 a shared object event catchpoint. If IS_LOAD is non-zero then
8049 the events to be caught are load events, otherwise they are
8050 unload events. If IS_TEMP is non-zero the catchpoint is a
8051 temporary one. If ENABLED is non-zero the catchpoint is
8052 created in an enabled state. */
8053
8054 void
8055 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8056 {
8057 struct gdbarch *gdbarch = get_current_arch ();
8058
8059 if (!arg)
8060 arg = "";
8061 arg = skip_spaces (arg);
8062
8063 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8064
8065 if (*arg != '\0')
8066 {
8067 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8068 _("Invalid regexp")));
8069 c->regex = xstrdup (arg);
8070 }
8071
8072 c->is_load = is_load;
8073 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8074 &catch_solib_breakpoint_ops);
8075
8076 c->enable_state = enabled ? bp_enabled : bp_disabled;
8077
8078 install_breakpoint (0, std::move (c), 1);
8079 }
8080
8081 /* A helper function that does all the work for "catch load" and
8082 "catch unload". */
8083
8084 static void
8085 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8086 struct cmd_list_element *command)
8087 {
8088 int tempflag;
8089 const int enabled = 1;
8090
8091 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8092
8093 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8094 }
8095
8096 static void
8097 catch_load_command_1 (const char *arg, int from_tty,
8098 struct cmd_list_element *command)
8099 {
8100 catch_load_or_unload (arg, from_tty, 1, command);
8101 }
8102
8103 static void
8104 catch_unload_command_1 (const char *arg, int from_tty,
8105 struct cmd_list_element *command)
8106 {
8107 catch_load_or_unload (arg, from_tty, 0, command);
8108 }
8109
8110 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8111 is non-zero, then make the breakpoint temporary. If COND_STRING is
8112 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8113 the breakpoint_ops structure associated to the catchpoint. */
8114
8115 void
8116 init_catchpoint (struct breakpoint *b,
8117 struct gdbarch *gdbarch, int tempflag,
8118 const char *cond_string,
8119 const struct breakpoint_ops *ops)
8120 {
8121 symtab_and_line sal;
8122 sal.pspace = current_program_space;
8123
8124 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8125
8126 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8127 b->disposition = tempflag ? disp_del : disp_donttouch;
8128 }
8129
8130 void
8131 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8132 {
8133 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8134 set_breakpoint_number (internal, b);
8135 if (is_tracepoint (b))
8136 set_tracepoint_count (breakpoint_count);
8137 if (!internal)
8138 mention (b);
8139 gdb::observers::breakpoint_created.notify (b);
8140
8141 if (update_gll)
8142 update_global_location_list (UGLL_MAY_INSERT);
8143 }
8144
8145 static void
8146 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8147 int tempflag, const char *cond_string,
8148 const struct breakpoint_ops *ops)
8149 {
8150 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8151
8152 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8153
8154 c->forked_inferior_pid = null_ptid;
8155
8156 install_breakpoint (0, std::move (c), 1);
8157 }
8158
8159 /* Exec catchpoints. */
8160
8161 /* An instance of this type is used to represent an exec catchpoint.
8162 A breakpoint is really of this type iff its ops pointer points to
8163 CATCH_EXEC_BREAKPOINT_OPS. */
8164
8165 struct exec_catchpoint : public breakpoint
8166 {
8167 ~exec_catchpoint () override;
8168
8169 /* Filename of a program whose exec triggered this catchpoint.
8170 This field is only valid immediately after this catchpoint has
8171 triggered. */
8172 char *exec_pathname;
8173 };
8174
8175 /* Exec catchpoint destructor. */
8176
8177 exec_catchpoint::~exec_catchpoint ()
8178 {
8179 xfree (this->exec_pathname);
8180 }
8181
8182 static int
8183 insert_catch_exec (struct bp_location *bl)
8184 {
8185 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8186 }
8187
8188 static int
8189 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8190 {
8191 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8192 }
8193
8194 static int
8195 breakpoint_hit_catch_exec (const struct bp_location *bl,
8196 const address_space *aspace, CORE_ADDR bp_addr,
8197 const struct target_waitstatus *ws)
8198 {
8199 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8200
8201 if (ws->kind != TARGET_WAITKIND_EXECD)
8202 return 0;
8203
8204 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8205 return 1;
8206 }
8207
8208 static enum print_stop_action
8209 print_it_catch_exec (bpstat bs)
8210 {
8211 struct ui_out *uiout = current_uiout;
8212 struct breakpoint *b = bs->breakpoint_at;
8213 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8214
8215 annotate_catchpoint (b->number);
8216 maybe_print_thread_hit_breakpoint (uiout);
8217 if (b->disposition == disp_del)
8218 uiout->text ("Temporary catchpoint ");
8219 else
8220 uiout->text ("Catchpoint ");
8221 if (uiout->is_mi_like_p ())
8222 {
8223 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8224 uiout->field_string ("disp", bpdisp_text (b->disposition));
8225 }
8226 uiout->field_signed ("bkptno", b->number);
8227 uiout->text (" (exec'd ");
8228 uiout->field_string ("new-exec", c->exec_pathname);
8229 uiout->text ("), ");
8230
8231 return PRINT_SRC_AND_LOC;
8232 }
8233
8234 static void
8235 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8236 {
8237 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8238 struct value_print_options opts;
8239 struct ui_out *uiout = current_uiout;
8240
8241 get_user_print_options (&opts);
8242
8243 /* Field 4, the address, is omitted (which makes the columns
8244 not line up too nicely with the headers, but the effect
8245 is relatively readable). */
8246 if (opts.addressprint)
8247 uiout->field_skip ("addr");
8248 annotate_field (5);
8249 uiout->text ("exec");
8250 if (c->exec_pathname != NULL)
8251 {
8252 uiout->text (", program \"");
8253 uiout->field_string ("what", c->exec_pathname);
8254 uiout->text ("\" ");
8255 }
8256
8257 if (uiout->is_mi_like_p ())
8258 uiout->field_string ("catch-type", "exec");
8259 }
8260
8261 static void
8262 print_mention_catch_exec (struct breakpoint *b)
8263 {
8264 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8265 }
8266
8267 /* Implement the "print_recreate" breakpoint_ops method for exec
8268 catchpoints. */
8269
8270 static void
8271 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8272 {
8273 fprintf_unfiltered (fp, "catch exec");
8274 print_recreate_thread (b, fp);
8275 }
8276
8277 static struct breakpoint_ops catch_exec_breakpoint_ops;
8278
8279 static int
8280 hw_breakpoint_used_count (void)
8281 {
8282 int i = 0;
8283 struct breakpoint *b;
8284 struct bp_location *bl;
8285
8286 ALL_BREAKPOINTS (b)
8287 {
8288 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8289 for (bl = b->loc; bl; bl = bl->next)
8290 {
8291 /* Special types of hardware breakpoints may use more than
8292 one register. */
8293 i += b->ops->resources_needed (bl);
8294 }
8295 }
8296
8297 return i;
8298 }
8299
8300 /* Returns the resources B would use if it were a hardware
8301 watchpoint. */
8302
8303 static int
8304 hw_watchpoint_use_count (struct breakpoint *b)
8305 {
8306 int i = 0;
8307 struct bp_location *bl;
8308
8309 if (!breakpoint_enabled (b))
8310 return 0;
8311
8312 for (bl = b->loc; bl; bl = bl->next)
8313 {
8314 /* Special types of hardware watchpoints may use more than
8315 one register. */
8316 i += b->ops->resources_needed (bl);
8317 }
8318
8319 return i;
8320 }
8321
8322 /* Returns the sum the used resources of all hardware watchpoints of
8323 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8324 the sum of the used resources of all hardware watchpoints of other
8325 types _not_ TYPE. */
8326
8327 static int
8328 hw_watchpoint_used_count_others (struct breakpoint *except,
8329 enum bptype type, int *other_type_used)
8330 {
8331 int i = 0;
8332 struct breakpoint *b;
8333
8334 *other_type_used = 0;
8335 ALL_BREAKPOINTS (b)
8336 {
8337 if (b == except)
8338 continue;
8339 if (!breakpoint_enabled (b))
8340 continue;
8341
8342 if (b->type == type)
8343 i += hw_watchpoint_use_count (b);
8344 else if (is_hardware_watchpoint (b))
8345 *other_type_used = 1;
8346 }
8347
8348 return i;
8349 }
8350
8351 void
8352 disable_watchpoints_before_interactive_call_start (void)
8353 {
8354 struct breakpoint *b;
8355
8356 ALL_BREAKPOINTS (b)
8357 {
8358 if (is_watchpoint (b) && breakpoint_enabled (b))
8359 {
8360 b->enable_state = bp_call_disabled;
8361 update_global_location_list (UGLL_DONT_INSERT);
8362 }
8363 }
8364 }
8365
8366 void
8367 enable_watchpoints_after_interactive_call_stop (void)
8368 {
8369 struct breakpoint *b;
8370
8371 ALL_BREAKPOINTS (b)
8372 {
8373 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8374 {
8375 b->enable_state = bp_enabled;
8376 update_global_location_list (UGLL_MAY_INSERT);
8377 }
8378 }
8379 }
8380
8381 void
8382 disable_breakpoints_before_startup (void)
8383 {
8384 current_program_space->executing_startup = 1;
8385 update_global_location_list (UGLL_DONT_INSERT);
8386 }
8387
8388 void
8389 enable_breakpoints_after_startup (void)
8390 {
8391 current_program_space->executing_startup = 0;
8392 breakpoint_re_set ();
8393 }
8394
8395 /* Create a new single-step breakpoint for thread THREAD, with no
8396 locations. */
8397
8398 static struct breakpoint *
8399 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8400 {
8401 std::unique_ptr<breakpoint> b (new breakpoint ());
8402
8403 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8404 &momentary_breakpoint_ops);
8405
8406 b->disposition = disp_donttouch;
8407 b->frame_id = null_frame_id;
8408
8409 b->thread = thread;
8410 gdb_assert (b->thread != 0);
8411
8412 return add_to_breakpoint_chain (std::move (b));
8413 }
8414
8415 /* Set a momentary breakpoint of type TYPE at address specified by
8416 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8417 frame. */
8418
8419 breakpoint_up
8420 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8421 struct frame_id frame_id, enum bptype type)
8422 {
8423 struct breakpoint *b;
8424
8425 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8426 tail-called one. */
8427 gdb_assert (!frame_id_artificial_p (frame_id));
8428
8429 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8430 b->enable_state = bp_enabled;
8431 b->disposition = disp_donttouch;
8432 b->frame_id = frame_id;
8433
8434 b->thread = inferior_thread ()->global_num;
8435
8436 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8437
8438 return breakpoint_up (b);
8439 }
8440
8441 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8442 The new breakpoint will have type TYPE, use OPS as its
8443 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8444
8445 static struct breakpoint *
8446 momentary_breakpoint_from_master (struct breakpoint *orig,
8447 enum bptype type,
8448 const struct breakpoint_ops *ops,
8449 int loc_enabled)
8450 {
8451 struct breakpoint *copy;
8452
8453 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8454 copy->loc = allocate_bp_location (copy);
8455 set_breakpoint_location_function (copy->loc);
8456
8457 copy->loc->gdbarch = orig->loc->gdbarch;
8458 copy->loc->requested_address = orig->loc->requested_address;
8459 copy->loc->address = orig->loc->address;
8460 copy->loc->section = orig->loc->section;
8461 copy->loc->pspace = orig->loc->pspace;
8462 copy->loc->probe = orig->loc->probe;
8463 copy->loc->line_number = orig->loc->line_number;
8464 copy->loc->symtab = orig->loc->symtab;
8465 copy->loc->enabled = loc_enabled;
8466 copy->frame_id = orig->frame_id;
8467 copy->thread = orig->thread;
8468 copy->pspace = orig->pspace;
8469
8470 copy->enable_state = bp_enabled;
8471 copy->disposition = disp_donttouch;
8472 copy->number = internal_breakpoint_number--;
8473
8474 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8475 return copy;
8476 }
8477
8478 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8479 ORIG is NULL. */
8480
8481 struct breakpoint *
8482 clone_momentary_breakpoint (struct breakpoint *orig)
8483 {
8484 /* If there's nothing to clone, then return nothing. */
8485 if (orig == NULL)
8486 return NULL;
8487
8488 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8489 }
8490
8491 breakpoint_up
8492 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8493 enum bptype type)
8494 {
8495 struct symtab_and_line sal;
8496
8497 sal = find_pc_line (pc, 0);
8498 sal.pc = pc;
8499 sal.section = find_pc_overlay (pc);
8500 sal.explicit_pc = 1;
8501
8502 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8503 }
8504 \f
8505
8506 /* Tell the user we have just set a breakpoint B. */
8507
8508 static void
8509 mention (struct breakpoint *b)
8510 {
8511 b->ops->print_mention (b);
8512 current_uiout->text ("\n");
8513 }
8514 \f
8515
8516 static bool bp_loc_is_permanent (struct bp_location *loc);
8517
8518 static struct bp_location *
8519 add_location_to_breakpoint (struct breakpoint *b,
8520 const struct symtab_and_line *sal)
8521 {
8522 struct bp_location *loc, **tmp;
8523 CORE_ADDR adjusted_address;
8524 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8525
8526 if (loc_gdbarch == NULL)
8527 loc_gdbarch = b->gdbarch;
8528
8529 /* Adjust the breakpoint's address prior to allocating a location.
8530 Once we call allocate_bp_location(), that mostly uninitialized
8531 location will be placed on the location chain. Adjustment of the
8532 breakpoint may cause target_read_memory() to be called and we do
8533 not want its scan of the location chain to find a breakpoint and
8534 location that's only been partially initialized. */
8535 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8536 sal->pc, b->type);
8537
8538 /* Sort the locations by their ADDRESS. */
8539 loc = allocate_bp_location (b);
8540 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8541 tmp = &((*tmp)->next))
8542 ;
8543 loc->next = *tmp;
8544 *tmp = loc;
8545
8546 loc->requested_address = sal->pc;
8547 loc->address = adjusted_address;
8548 loc->pspace = sal->pspace;
8549 loc->probe.prob = sal->prob;
8550 loc->probe.objfile = sal->objfile;
8551 gdb_assert (loc->pspace != NULL);
8552 loc->section = sal->section;
8553 loc->gdbarch = loc_gdbarch;
8554 loc->line_number = sal->line;
8555 loc->symtab = sal->symtab;
8556 loc->symbol = sal->symbol;
8557 loc->msymbol = sal->msymbol;
8558 loc->objfile = sal->objfile;
8559
8560 set_breakpoint_location_function (loc);
8561
8562 /* While by definition, permanent breakpoints are already present in the
8563 code, we don't mark the location as inserted. Normally one would expect
8564 that GDB could rely on that breakpoint instruction to stop the program,
8565 thus removing the need to insert its own breakpoint, except that executing
8566 the breakpoint instruction can kill the target instead of reporting a
8567 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8568 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8569 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8570 breakpoint be inserted normally results in QEMU knowing about the GDB
8571 breakpoint, and thus trap before the breakpoint instruction is executed.
8572 (If GDB later needs to continue execution past the permanent breakpoint,
8573 it manually increments the PC, thus avoiding executing the breakpoint
8574 instruction.) */
8575 if (bp_loc_is_permanent (loc))
8576 loc->permanent = 1;
8577
8578 return loc;
8579 }
8580 \f
8581
8582 /* Return true if LOC is pointing to a permanent breakpoint,
8583 return false otherwise. */
8584
8585 static bool
8586 bp_loc_is_permanent (struct bp_location *loc)
8587 {
8588 gdb_assert (loc != NULL);
8589
8590 /* If we have a non-breakpoint-backed catchpoint or a software
8591 watchpoint, just return 0. We should not attempt to read from
8592 the addresses the locations of these breakpoint types point to.
8593 gdbarch_program_breakpoint_here_p, below, will attempt to read
8594 memory. */
8595 if (!bl_address_is_meaningful (loc))
8596 return false;
8597
8598 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8599 switch_to_program_space_and_thread (loc->pspace);
8600 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8601 }
8602
8603 /* Build a command list for the dprintf corresponding to the current
8604 settings of the dprintf style options. */
8605
8606 static void
8607 update_dprintf_command_list (struct breakpoint *b)
8608 {
8609 char *dprintf_args = b->extra_string;
8610 char *printf_line = NULL;
8611
8612 if (!dprintf_args)
8613 return;
8614
8615 dprintf_args = skip_spaces (dprintf_args);
8616
8617 /* Allow a comma, as it may have terminated a location, but don't
8618 insist on it. */
8619 if (*dprintf_args == ',')
8620 ++dprintf_args;
8621 dprintf_args = skip_spaces (dprintf_args);
8622
8623 if (*dprintf_args != '"')
8624 error (_("Bad format string, missing '\"'."));
8625
8626 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8627 printf_line = xstrprintf ("printf %s", dprintf_args);
8628 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8629 {
8630 if (!dprintf_function)
8631 error (_("No function supplied for dprintf call"));
8632
8633 if (dprintf_channel && strlen (dprintf_channel) > 0)
8634 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8635 dprintf_function,
8636 dprintf_channel,
8637 dprintf_args);
8638 else
8639 printf_line = xstrprintf ("call (void) %s (%s)",
8640 dprintf_function,
8641 dprintf_args);
8642 }
8643 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8644 {
8645 if (target_can_run_breakpoint_commands ())
8646 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8647 else
8648 {
8649 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8650 printf_line = xstrprintf ("printf %s", dprintf_args);
8651 }
8652 }
8653 else
8654 internal_error (__FILE__, __LINE__,
8655 _("Invalid dprintf style."));
8656
8657 gdb_assert (printf_line != NULL);
8658
8659 /* Manufacture a printf sequence. */
8660 struct command_line *printf_cmd_line
8661 = new struct command_line (simple_control, printf_line);
8662 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8663 command_lines_deleter ()));
8664 }
8665
8666 /* Update all dprintf commands, making their command lists reflect
8667 current style settings. */
8668
8669 static void
8670 update_dprintf_commands (const char *args, int from_tty,
8671 struct cmd_list_element *c)
8672 {
8673 struct breakpoint *b;
8674
8675 ALL_BREAKPOINTS (b)
8676 {
8677 if (b->type == bp_dprintf)
8678 update_dprintf_command_list (b);
8679 }
8680 }
8681
8682 /* Create a breakpoint with SAL as location. Use LOCATION
8683 as a description of the location, and COND_STRING
8684 as condition expression. If LOCATION is NULL then create an
8685 "address location" from the address in the SAL. */
8686
8687 static void
8688 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8689 gdb::array_view<const symtab_and_line> sals,
8690 event_location_up &&location,
8691 gdb::unique_xmalloc_ptr<char> filter,
8692 gdb::unique_xmalloc_ptr<char> cond_string,
8693 gdb::unique_xmalloc_ptr<char> extra_string,
8694 enum bptype type, enum bpdisp disposition,
8695 int thread, int task, int ignore_count,
8696 const struct breakpoint_ops *ops, int from_tty,
8697 int enabled, int internal, unsigned flags,
8698 int display_canonical)
8699 {
8700 int i;
8701
8702 if (type == bp_hardware_breakpoint)
8703 {
8704 int target_resources_ok;
8705
8706 i = hw_breakpoint_used_count ();
8707 target_resources_ok =
8708 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8709 i + 1, 0);
8710 if (target_resources_ok == 0)
8711 error (_("No hardware breakpoint support in the target."));
8712 else if (target_resources_ok < 0)
8713 error (_("Hardware breakpoints used exceeds limit."));
8714 }
8715
8716 gdb_assert (!sals.empty ());
8717
8718 for (const auto &sal : sals)
8719 {
8720 struct bp_location *loc;
8721
8722 if (from_tty)
8723 {
8724 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8725 if (!loc_gdbarch)
8726 loc_gdbarch = gdbarch;
8727
8728 describe_other_breakpoints (loc_gdbarch,
8729 sal.pspace, sal.pc, sal.section, thread);
8730 }
8731
8732 if (&sal == &sals[0])
8733 {
8734 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8735 b->thread = thread;
8736 b->task = task;
8737
8738 b->cond_string = cond_string.release ();
8739 b->extra_string = extra_string.release ();
8740 b->ignore_count = ignore_count;
8741 b->enable_state = enabled ? bp_enabled : bp_disabled;
8742 b->disposition = disposition;
8743
8744 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8745 b->loc->inserted = 1;
8746
8747 if (type == bp_static_tracepoint)
8748 {
8749 struct tracepoint *t = (struct tracepoint *) b;
8750 struct static_tracepoint_marker marker;
8751
8752 if (strace_marker_p (b))
8753 {
8754 /* We already know the marker exists, otherwise, we
8755 wouldn't see a sal for it. */
8756 const char *p
8757 = &event_location_to_string (b->location.get ())[3];
8758 const char *endp;
8759
8760 p = skip_spaces (p);
8761
8762 endp = skip_to_space (p);
8763
8764 t->static_trace_marker_id.assign (p, endp - p);
8765
8766 printf_filtered (_("Probed static tracepoint "
8767 "marker \"%s\"\n"),
8768 t->static_trace_marker_id.c_str ());
8769 }
8770 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8771 {
8772 t->static_trace_marker_id = std::move (marker.str_id);
8773
8774 printf_filtered (_("Probed static tracepoint "
8775 "marker \"%s\"\n"),
8776 t->static_trace_marker_id.c_str ());
8777 }
8778 else
8779 warning (_("Couldn't determine the static "
8780 "tracepoint marker to probe"));
8781 }
8782
8783 loc = b->loc;
8784 }
8785 else
8786 {
8787 loc = add_location_to_breakpoint (b, &sal);
8788 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8789 loc->inserted = 1;
8790 }
8791
8792 if (b->cond_string)
8793 {
8794 const char *arg = b->cond_string;
8795
8796 loc->cond = parse_exp_1 (&arg, loc->address,
8797 block_for_pc (loc->address), 0);
8798 if (*arg)
8799 error (_("Garbage '%s' follows condition"), arg);
8800 }
8801
8802 /* Dynamic printf requires and uses additional arguments on the
8803 command line, otherwise it's an error. */
8804 if (type == bp_dprintf)
8805 {
8806 if (b->extra_string)
8807 update_dprintf_command_list (b);
8808 else
8809 error (_("Format string required"));
8810 }
8811 else if (b->extra_string)
8812 error (_("Garbage '%s' at end of command"), b->extra_string);
8813 }
8814
8815 b->display_canonical = display_canonical;
8816 if (location != NULL)
8817 b->location = std::move (location);
8818 else
8819 b->location = new_address_location (b->loc->address, NULL, 0);
8820 b->filter = std::move (filter);
8821 }
8822
8823 static void
8824 create_breakpoint_sal (struct gdbarch *gdbarch,
8825 gdb::array_view<const symtab_and_line> sals,
8826 event_location_up &&location,
8827 gdb::unique_xmalloc_ptr<char> filter,
8828 gdb::unique_xmalloc_ptr<char> cond_string,
8829 gdb::unique_xmalloc_ptr<char> extra_string,
8830 enum bptype type, enum bpdisp disposition,
8831 int thread, int task, int ignore_count,
8832 const struct breakpoint_ops *ops, int from_tty,
8833 int enabled, int internal, unsigned flags,
8834 int display_canonical)
8835 {
8836 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8837
8838 init_breakpoint_sal (b.get (), gdbarch,
8839 sals, std::move (location),
8840 std::move (filter),
8841 std::move (cond_string),
8842 std::move (extra_string),
8843 type, disposition,
8844 thread, task, ignore_count,
8845 ops, from_tty,
8846 enabled, internal, flags,
8847 display_canonical);
8848
8849 install_breakpoint (internal, std::move (b), 0);
8850 }
8851
8852 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8853 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8854 value. COND_STRING, if not NULL, specified the condition to be
8855 used for all breakpoints. Essentially the only case where
8856 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8857 function. In that case, it's still not possible to specify
8858 separate conditions for different overloaded functions, so
8859 we take just a single condition string.
8860
8861 NOTE: If the function succeeds, the caller is expected to cleanup
8862 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8863 array contents). If the function fails (error() is called), the
8864 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8865 COND and SALS arrays and each of those arrays contents. */
8866
8867 static void
8868 create_breakpoints_sal (struct gdbarch *gdbarch,
8869 struct linespec_result *canonical,
8870 gdb::unique_xmalloc_ptr<char> cond_string,
8871 gdb::unique_xmalloc_ptr<char> extra_string,
8872 enum bptype type, enum bpdisp disposition,
8873 int thread, int task, int ignore_count,
8874 const struct breakpoint_ops *ops, int from_tty,
8875 int enabled, int internal, unsigned flags)
8876 {
8877 if (canonical->pre_expanded)
8878 gdb_assert (canonical->lsals.size () == 1);
8879
8880 for (const auto &lsal : canonical->lsals)
8881 {
8882 /* Note that 'location' can be NULL in the case of a plain
8883 'break', without arguments. */
8884 event_location_up location
8885 = (canonical->location != NULL
8886 ? copy_event_location (canonical->location.get ()) : NULL);
8887 gdb::unique_xmalloc_ptr<char> filter_string
8888 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8889
8890 create_breakpoint_sal (gdbarch, lsal.sals,
8891 std::move (location),
8892 std::move (filter_string),
8893 std::move (cond_string),
8894 std::move (extra_string),
8895 type, disposition,
8896 thread, task, ignore_count, ops,
8897 from_tty, enabled, internal, flags,
8898 canonical->special_display);
8899 }
8900 }
8901
8902 /* Parse LOCATION which is assumed to be a SAL specification possibly
8903 followed by conditionals. On return, SALS contains an array of SAL
8904 addresses found. LOCATION points to the end of the SAL (for
8905 linespec locations).
8906
8907 The array and the line spec strings are allocated on the heap, it is
8908 the caller's responsibility to free them. */
8909
8910 static void
8911 parse_breakpoint_sals (const struct event_location *location,
8912 struct linespec_result *canonical)
8913 {
8914 struct symtab_and_line cursal;
8915
8916 if (event_location_type (location) == LINESPEC_LOCATION)
8917 {
8918 const char *spec = get_linespec_location (location)->spec_string;
8919
8920 if (spec == NULL)
8921 {
8922 /* The last displayed codepoint, if it's valid, is our default
8923 breakpoint address. */
8924 if (last_displayed_sal_is_valid ())
8925 {
8926 /* Set sal's pspace, pc, symtab, and line to the values
8927 corresponding to the last call to print_frame_info.
8928 Be sure to reinitialize LINE with NOTCURRENT == 0
8929 as the breakpoint line number is inappropriate otherwise.
8930 find_pc_line would adjust PC, re-set it back. */
8931 symtab_and_line sal = get_last_displayed_sal ();
8932 CORE_ADDR pc = sal.pc;
8933
8934 sal = find_pc_line (pc, 0);
8935
8936 /* "break" without arguments is equivalent to "break *PC"
8937 where PC is the last displayed codepoint's address. So
8938 make sure to set sal.explicit_pc to prevent GDB from
8939 trying to expand the list of sals to include all other
8940 instances with the same symtab and line. */
8941 sal.pc = pc;
8942 sal.explicit_pc = 1;
8943
8944 struct linespec_sals lsal;
8945 lsal.sals = {sal};
8946 lsal.canonical = NULL;
8947
8948 canonical->lsals.push_back (std::move (lsal));
8949 return;
8950 }
8951 else
8952 error (_("No default breakpoint address now."));
8953 }
8954 }
8955
8956 /* Force almost all breakpoints to be in terms of the
8957 current_source_symtab (which is decode_line_1's default).
8958 This should produce the results we want almost all of the
8959 time while leaving default_breakpoint_* alone.
8960
8961 ObjC: However, don't match an Objective-C method name which
8962 may have a '+' or '-' succeeded by a '['. */
8963 cursal = get_current_source_symtab_and_line ();
8964 if (last_displayed_sal_is_valid ())
8965 {
8966 const char *spec = NULL;
8967
8968 if (event_location_type (location) == LINESPEC_LOCATION)
8969 spec = get_linespec_location (location)->spec_string;
8970
8971 if (!cursal.symtab
8972 || (spec != NULL
8973 && strchr ("+-", spec[0]) != NULL
8974 && spec[1] != '['))
8975 {
8976 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8977 get_last_displayed_symtab (),
8978 get_last_displayed_line (),
8979 canonical, NULL, NULL);
8980 return;
8981 }
8982 }
8983
8984 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8985 cursal.symtab, cursal.line, canonical, NULL, NULL);
8986 }
8987
8988
8989 /* Convert each SAL into a real PC. Verify that the PC can be
8990 inserted as a breakpoint. If it can't throw an error. */
8991
8992 static void
8993 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8994 {
8995 for (auto &sal : sals)
8996 resolve_sal_pc (&sal);
8997 }
8998
8999 /* Fast tracepoints may have restrictions on valid locations. For
9000 instance, a fast tracepoint using a jump instead of a trap will
9001 likely have to overwrite more bytes than a trap would, and so can
9002 only be placed where the instruction is longer than the jump, or a
9003 multi-instruction sequence does not have a jump into the middle of
9004 it, etc. */
9005
9006 static void
9007 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9008 gdb::array_view<const symtab_and_line> sals)
9009 {
9010 for (const auto &sal : sals)
9011 {
9012 struct gdbarch *sarch;
9013
9014 sarch = get_sal_arch (sal);
9015 /* We fall back to GDBARCH if there is no architecture
9016 associated with SAL. */
9017 if (sarch == NULL)
9018 sarch = gdbarch;
9019 std::string msg;
9020 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9021 error (_("May not have a fast tracepoint at %s%s"),
9022 paddress (sarch, sal.pc), msg.c_str ());
9023 }
9024 }
9025
9026 /* Given TOK, a string specification of condition and thread, as
9027 accepted by the 'break' command, extract the condition
9028 string and thread number and set *COND_STRING and *THREAD.
9029 PC identifies the context at which the condition should be parsed.
9030 If no condition is found, *COND_STRING is set to NULL.
9031 If no thread is found, *THREAD is set to -1. */
9032
9033 static void
9034 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9035 char **cond_string, int *thread, int *task,
9036 char **rest)
9037 {
9038 *cond_string = NULL;
9039 *thread = -1;
9040 *task = 0;
9041 *rest = NULL;
9042
9043 while (tok && *tok)
9044 {
9045 const char *end_tok;
9046 int toklen;
9047 const char *cond_start = NULL;
9048 const char *cond_end = NULL;
9049
9050 tok = skip_spaces (tok);
9051
9052 if ((*tok == '"' || *tok == ',') && rest)
9053 {
9054 *rest = savestring (tok, strlen (tok));
9055 return;
9056 }
9057
9058 end_tok = skip_to_space (tok);
9059
9060 toklen = end_tok - tok;
9061
9062 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9063 {
9064 tok = cond_start = end_tok + 1;
9065 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9066 cond_end = tok;
9067 *cond_string = savestring (cond_start, cond_end - cond_start);
9068 }
9069 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9070 {
9071 const char *tmptok;
9072 struct thread_info *thr;
9073
9074 tok = end_tok + 1;
9075 thr = parse_thread_id (tok, &tmptok);
9076 if (tok == tmptok)
9077 error (_("Junk after thread keyword."));
9078 *thread = thr->global_num;
9079 tok = tmptok;
9080 }
9081 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9082 {
9083 char *tmptok;
9084
9085 tok = end_tok + 1;
9086 *task = strtol (tok, &tmptok, 0);
9087 if (tok == tmptok)
9088 error (_("Junk after task keyword."));
9089 if (!valid_task_id (*task))
9090 error (_("Unknown task %d."), *task);
9091 tok = tmptok;
9092 }
9093 else if (rest)
9094 {
9095 *rest = savestring (tok, strlen (tok));
9096 return;
9097 }
9098 else
9099 error (_("Junk at end of arguments."));
9100 }
9101 }
9102
9103 /* Decode a static tracepoint marker spec. */
9104
9105 static std::vector<symtab_and_line>
9106 decode_static_tracepoint_spec (const char **arg_p)
9107 {
9108 const char *p = &(*arg_p)[3];
9109 const char *endp;
9110
9111 p = skip_spaces (p);
9112
9113 endp = skip_to_space (p);
9114
9115 std::string marker_str (p, endp - p);
9116
9117 std::vector<static_tracepoint_marker> markers
9118 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9119 if (markers.empty ())
9120 error (_("No known static tracepoint marker named %s"),
9121 marker_str.c_str ());
9122
9123 std::vector<symtab_and_line> sals;
9124 sals.reserve (markers.size ());
9125
9126 for (const static_tracepoint_marker &marker : markers)
9127 {
9128 symtab_and_line sal = find_pc_line (marker.address, 0);
9129 sal.pc = marker.address;
9130 sals.push_back (sal);
9131 }
9132
9133 *arg_p = endp;
9134 return sals;
9135 }
9136
9137 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9138 according to IS_TRACEPOINT. */
9139
9140 static const struct breakpoint_ops *
9141 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9142 bool is_tracepoint)
9143 {
9144 if (is_tracepoint)
9145 {
9146 if (location_type == PROBE_LOCATION)
9147 return &tracepoint_probe_breakpoint_ops;
9148 else
9149 return &tracepoint_breakpoint_ops;
9150 }
9151 else
9152 {
9153 if (location_type == PROBE_LOCATION)
9154 return &bkpt_probe_breakpoint_ops;
9155 else
9156 return &bkpt_breakpoint_ops;
9157 }
9158 }
9159
9160 /* See breakpoint.h. */
9161
9162 const struct breakpoint_ops *
9163 breakpoint_ops_for_event_location (const struct event_location *location,
9164 bool is_tracepoint)
9165 {
9166 if (location != nullptr)
9167 return breakpoint_ops_for_event_location_type
9168 (event_location_type (location), is_tracepoint);
9169 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9170 }
9171
9172 /* See breakpoint.h. */
9173
9174 int
9175 create_breakpoint (struct gdbarch *gdbarch,
9176 const struct event_location *location,
9177 const char *cond_string,
9178 int thread, const char *extra_string,
9179 int parse_extra,
9180 int tempflag, enum bptype type_wanted,
9181 int ignore_count,
9182 enum auto_boolean pending_break_support,
9183 const struct breakpoint_ops *ops,
9184 int from_tty, int enabled, int internal,
9185 unsigned flags)
9186 {
9187 struct linespec_result canonical;
9188 int pending = 0;
9189 int task = 0;
9190 int prev_bkpt_count = breakpoint_count;
9191
9192 gdb_assert (ops != NULL);
9193
9194 /* If extra_string isn't useful, set it to NULL. */
9195 if (extra_string != NULL && *extra_string == '\0')
9196 extra_string = NULL;
9197
9198 try
9199 {
9200 ops->create_sals_from_location (location, &canonical, type_wanted);
9201 }
9202 catch (const gdb_exception_error &e)
9203 {
9204 /* If caller is interested in rc value from parse, set
9205 value. */
9206 if (e.error == NOT_FOUND_ERROR)
9207 {
9208 /* If pending breakpoint support is turned off, throw
9209 error. */
9210
9211 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9212 throw;
9213
9214 exception_print (gdb_stderr, e);
9215
9216 /* If pending breakpoint support is auto query and the user
9217 selects no, then simply return the error code. */
9218 if (pending_break_support == AUTO_BOOLEAN_AUTO
9219 && !nquery (_("Make %s pending on future shared library load? "),
9220 bptype_string (type_wanted)))
9221 return 0;
9222
9223 /* At this point, either the user was queried about setting
9224 a pending breakpoint and selected yes, or pending
9225 breakpoint behavior is on and thus a pending breakpoint
9226 is defaulted on behalf of the user. */
9227 pending = 1;
9228 }
9229 else
9230 throw;
9231 }
9232
9233 if (!pending && canonical.lsals.empty ())
9234 return 0;
9235
9236 /* Resolve all line numbers to PC's and verify that the addresses
9237 are ok for the target. */
9238 if (!pending)
9239 {
9240 for (auto &lsal : canonical.lsals)
9241 breakpoint_sals_to_pc (lsal.sals);
9242 }
9243
9244 /* Fast tracepoints may have additional restrictions on location. */
9245 if (!pending && type_wanted == bp_fast_tracepoint)
9246 {
9247 for (const auto &lsal : canonical.lsals)
9248 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9249 }
9250
9251 /* Verify that condition can be parsed, before setting any
9252 breakpoints. Allocate a separate condition expression for each
9253 breakpoint. */
9254 if (!pending)
9255 {
9256 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9257 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9258
9259 if (parse_extra)
9260 {
9261 char *rest;
9262 char *cond;
9263
9264 const linespec_sals &lsal = canonical.lsals[0];
9265
9266 /* Here we only parse 'arg' to separate condition
9267 from thread number, so parsing in context of first
9268 sal is OK. When setting the breakpoint we'll
9269 re-parse it in context of each sal. */
9270
9271 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9272 &cond, &thread, &task, &rest);
9273 cond_string_copy.reset (cond);
9274 extra_string_copy.reset (rest);
9275 }
9276 else
9277 {
9278 if (type_wanted != bp_dprintf
9279 && extra_string != NULL && *extra_string != '\0')
9280 error (_("Garbage '%s' at end of location"), extra_string);
9281
9282 /* Create a private copy of condition string. */
9283 if (cond_string)
9284 cond_string_copy.reset (xstrdup (cond_string));
9285 /* Create a private copy of any extra string. */
9286 if (extra_string)
9287 extra_string_copy.reset (xstrdup (extra_string));
9288 }
9289
9290 ops->create_breakpoints_sal (gdbarch, &canonical,
9291 std::move (cond_string_copy),
9292 std::move (extra_string_copy),
9293 type_wanted,
9294 tempflag ? disp_del : disp_donttouch,
9295 thread, task, ignore_count, ops,
9296 from_tty, enabled, internal, flags);
9297 }
9298 else
9299 {
9300 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9301
9302 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9303 b->location = copy_event_location (location);
9304
9305 if (parse_extra)
9306 b->cond_string = NULL;
9307 else
9308 {
9309 /* Create a private copy of condition string. */
9310 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9311 b->thread = thread;
9312 }
9313
9314 /* Create a private copy of any extra string. */
9315 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9316 b->ignore_count = ignore_count;
9317 b->disposition = tempflag ? disp_del : disp_donttouch;
9318 b->condition_not_parsed = 1;
9319 b->enable_state = enabled ? bp_enabled : bp_disabled;
9320 if ((type_wanted != bp_breakpoint
9321 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9322 b->pspace = current_program_space;
9323
9324 install_breakpoint (internal, std::move (b), 0);
9325 }
9326
9327 if (canonical.lsals.size () > 1)
9328 {
9329 warning (_("Multiple breakpoints were set.\nUse the "
9330 "\"delete\" command to delete unwanted breakpoints."));
9331 prev_breakpoint_count = prev_bkpt_count;
9332 }
9333
9334 update_global_location_list (UGLL_MAY_INSERT);
9335
9336 return 1;
9337 }
9338
9339 /* Set a breakpoint.
9340 ARG is a string describing breakpoint address,
9341 condition, and thread.
9342 FLAG specifies if a breakpoint is hardware on,
9343 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9344 and BP_TEMPFLAG. */
9345
9346 static void
9347 break_command_1 (const char *arg, int flag, int from_tty)
9348 {
9349 int tempflag = flag & BP_TEMPFLAG;
9350 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9351 ? bp_hardware_breakpoint
9352 : bp_breakpoint);
9353
9354 event_location_up location = string_to_event_location (&arg, current_language);
9355 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9356 (location.get (), false /* is_tracepoint */);
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
10402 switch (b->type)
10403 {
10404 case bp_hardware_watchpoint:
10405 fprintf_unfiltered (fp, "watch");
10406 break;
10407 case bp_read_watchpoint:
10408 fprintf_unfiltered (fp, "rwatch");
10409 break;
10410 case bp_access_watchpoint:
10411 fprintf_unfiltered (fp, "awatch");
10412 break;
10413 default:
10414 internal_error (__FILE__, __LINE__,
10415 _("Invalid hardware watchpoint type."));
10416 }
10417
10418 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10419 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10420 print_recreate_thread (b, fp);
10421 }
10422
10423 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10424
10425 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10426
10427 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10428
10429 static bool
10430 is_masked_watchpoint (const struct breakpoint *b)
10431 {
10432 return b->ops == &masked_watchpoint_breakpoint_ops;
10433 }
10434
10435 /* accessflag: hw_write: watch write,
10436 hw_read: watch read,
10437 hw_access: watch access (read or write) */
10438 static void
10439 watch_command_1 (const char *arg, int accessflag, int from_tty,
10440 int just_location, int internal)
10441 {
10442 struct breakpoint *scope_breakpoint = NULL;
10443 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10444 struct value *result;
10445 int saved_bitpos = 0, saved_bitsize = 0;
10446 const char *exp_start = NULL;
10447 const char *exp_end = NULL;
10448 const char *tok, *end_tok;
10449 int toklen = -1;
10450 const char *cond_start = NULL;
10451 const char *cond_end = NULL;
10452 enum bptype bp_type;
10453 int thread = -1;
10454 int pc = 0;
10455 /* Flag to indicate whether we are going to use masks for
10456 the hardware watchpoint. */
10457 int use_mask = 0;
10458 CORE_ADDR mask = 0;
10459
10460 /* Make sure that we actually have parameters to parse. */
10461 if (arg != NULL && arg[0] != '\0')
10462 {
10463 const char *value_start;
10464
10465 exp_end = arg + strlen (arg);
10466
10467 /* Look for "parameter value" pairs at the end
10468 of the arguments string. */
10469 for (tok = exp_end - 1; tok > arg; tok--)
10470 {
10471 /* Skip whitespace at the end of the argument list. */
10472 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10473 tok--;
10474
10475 /* Find the beginning of the last token.
10476 This is the value of the parameter. */
10477 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10478 tok--;
10479 value_start = tok + 1;
10480
10481 /* Skip whitespace. */
10482 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10483 tok--;
10484
10485 end_tok = tok;
10486
10487 /* Find the beginning of the second to last token.
10488 This is the parameter itself. */
10489 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10490 tok--;
10491 tok++;
10492 toklen = end_tok - tok + 1;
10493
10494 if (toklen == 6 && startswith (tok, "thread"))
10495 {
10496 struct thread_info *thr;
10497 /* At this point we've found a "thread" token, which means
10498 the user is trying to set a watchpoint that triggers
10499 only in a specific thread. */
10500 const char *endp;
10501
10502 if (thread != -1)
10503 error(_("You can specify only one thread."));
10504
10505 /* Extract the thread ID from the next token. */
10506 thr = parse_thread_id (value_start, &endp);
10507
10508 /* Check if the user provided a valid thread ID. */
10509 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10510 invalid_thread_id_error (value_start);
10511
10512 thread = thr->global_num;
10513 }
10514 else if (toklen == 4 && startswith (tok, "mask"))
10515 {
10516 /* We've found a "mask" token, which means the user wants to
10517 create a hardware watchpoint that is going to have the mask
10518 facility. */
10519 struct value *mask_value, *mark;
10520
10521 if (use_mask)
10522 error(_("You can specify only one mask."));
10523
10524 use_mask = just_location = 1;
10525
10526 mark = value_mark ();
10527 mask_value = parse_to_comma_and_eval (&value_start);
10528 mask = value_as_address (mask_value);
10529 value_free_to_mark (mark);
10530 }
10531 else
10532 /* We didn't recognize what we found. We should stop here. */
10533 break;
10534
10535 /* Truncate the string and get rid of the "parameter value" pair before
10536 the arguments string is parsed by the parse_exp_1 function. */
10537 exp_end = tok;
10538 }
10539 }
10540 else
10541 exp_end = arg;
10542
10543 /* Parse the rest of the arguments. From here on out, everything
10544 is in terms of a newly allocated string instead of the original
10545 ARG. */
10546 std::string expression (arg, exp_end - arg);
10547 exp_start = arg = expression.c_str ();
10548 innermost_block_tracker tracker;
10549 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10550 exp_end = arg;
10551 /* Remove trailing whitespace from the expression before saving it.
10552 This makes the eventual display of the expression string a bit
10553 prettier. */
10554 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10555 --exp_end;
10556
10557 /* Checking if the expression is not constant. */
10558 if (watchpoint_exp_is_const (exp.get ()))
10559 {
10560 int len;
10561
10562 len = exp_end - exp_start;
10563 while (len > 0 && isspace (exp_start[len - 1]))
10564 len--;
10565 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10566 }
10567
10568 exp_valid_block = tracker.block ();
10569 struct value *mark = value_mark ();
10570 struct value *val_as_value = nullptr;
10571 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10572 just_location);
10573
10574 if (val_as_value != NULL && just_location)
10575 {
10576 saved_bitpos = value_bitpos (val_as_value);
10577 saved_bitsize = value_bitsize (val_as_value);
10578 }
10579
10580 value_ref_ptr val;
10581 if (just_location)
10582 {
10583 int ret;
10584
10585 exp_valid_block = NULL;
10586 val = release_value (value_addr (result));
10587 value_free_to_mark (mark);
10588
10589 if (use_mask)
10590 {
10591 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10592 mask);
10593 if (ret == -1)
10594 error (_("This target does not support masked watchpoints."));
10595 else if (ret == -2)
10596 error (_("Invalid mask or memory region."));
10597 }
10598 }
10599 else if (val_as_value != NULL)
10600 val = release_value (val_as_value);
10601
10602 tok = skip_spaces (arg);
10603 end_tok = skip_to_space (tok);
10604
10605 toklen = end_tok - tok;
10606 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10607 {
10608 tok = cond_start = end_tok + 1;
10609 innermost_block_tracker if_tracker;
10610 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10611
10612 /* The watchpoint expression may not be local, but the condition
10613 may still be. E.g.: `watch global if local > 0'. */
10614 cond_exp_valid_block = if_tracker.block ();
10615
10616 cond_end = tok;
10617 }
10618 if (*tok)
10619 error (_("Junk at end of command."));
10620
10621 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10622
10623 /* Save this because create_internal_breakpoint below invalidates
10624 'wp_frame'. */
10625 frame_id watchpoint_frame = get_frame_id (wp_frame);
10626
10627 /* If the expression is "local", then set up a "watchpoint scope"
10628 breakpoint at the point where we've left the scope of the watchpoint
10629 expression. Create the scope breakpoint before the watchpoint, so
10630 that we will encounter it first in bpstat_stop_status. */
10631 if (exp_valid_block != NULL && wp_frame != NULL)
10632 {
10633 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10634
10635 if (frame_id_p (caller_frame_id))
10636 {
10637 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10638 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10639
10640 scope_breakpoint
10641 = create_internal_breakpoint (caller_arch, caller_pc,
10642 bp_watchpoint_scope,
10643 &momentary_breakpoint_ops);
10644
10645 /* create_internal_breakpoint could invalidate WP_FRAME. */
10646 wp_frame = NULL;
10647
10648 scope_breakpoint->enable_state = bp_enabled;
10649
10650 /* Automatically delete the breakpoint when it hits. */
10651 scope_breakpoint->disposition = disp_del;
10652
10653 /* Only break in the proper frame (help with recursion). */
10654 scope_breakpoint->frame_id = caller_frame_id;
10655
10656 /* Set the address at which we will stop. */
10657 scope_breakpoint->loc->gdbarch = caller_arch;
10658 scope_breakpoint->loc->requested_address = caller_pc;
10659 scope_breakpoint->loc->address
10660 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10661 scope_breakpoint->loc->requested_address,
10662 scope_breakpoint->type);
10663 }
10664 }
10665
10666 /* Now set up the breakpoint. We create all watchpoints as hardware
10667 watchpoints here even if hardware watchpoints are turned off, a call
10668 to update_watchpoint later in this function will cause the type to
10669 drop back to bp_watchpoint (software watchpoint) if required. */
10670
10671 if (accessflag == hw_read)
10672 bp_type = bp_read_watchpoint;
10673 else if (accessflag == hw_access)
10674 bp_type = bp_access_watchpoint;
10675 else
10676 bp_type = bp_hardware_watchpoint;
10677
10678 std::unique_ptr<watchpoint> w (new watchpoint ());
10679
10680 if (use_mask)
10681 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10682 &masked_watchpoint_breakpoint_ops);
10683 else
10684 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10685 &watchpoint_breakpoint_ops);
10686 w->thread = thread;
10687 w->disposition = disp_donttouch;
10688 w->pspace = current_program_space;
10689 w->exp = std::move (exp);
10690 w->exp_valid_block = exp_valid_block;
10691 w->cond_exp_valid_block = cond_exp_valid_block;
10692 if (just_location)
10693 {
10694 struct type *t = value_type (val.get ());
10695 CORE_ADDR addr = value_as_address (val.get ());
10696
10697 w->exp_string_reparse
10698 = current_language->la_watch_location_expression (t, addr).release ();
10699
10700 w->exp_string = xstrprintf ("-location %.*s",
10701 (int) (exp_end - exp_start), exp_start);
10702 }
10703 else
10704 w->exp_string = savestring (exp_start, exp_end - exp_start);
10705
10706 if (use_mask)
10707 {
10708 w->hw_wp_mask = mask;
10709 }
10710 else
10711 {
10712 w->val = val;
10713 w->val_bitpos = saved_bitpos;
10714 w->val_bitsize = saved_bitsize;
10715 w->val_valid = true;
10716 }
10717
10718 if (cond_start)
10719 w->cond_string = savestring (cond_start, cond_end - cond_start);
10720 else
10721 w->cond_string = 0;
10722
10723 if (frame_id_p (watchpoint_frame))
10724 {
10725 w->watchpoint_frame = watchpoint_frame;
10726 w->watchpoint_thread = inferior_ptid;
10727 }
10728 else
10729 {
10730 w->watchpoint_frame = null_frame_id;
10731 w->watchpoint_thread = null_ptid;
10732 }
10733
10734 if (scope_breakpoint != NULL)
10735 {
10736 /* The scope breakpoint is related to the watchpoint. We will
10737 need to act on them together. */
10738 w->related_breakpoint = scope_breakpoint;
10739 scope_breakpoint->related_breakpoint = w.get ();
10740 }
10741
10742 if (!just_location)
10743 value_free_to_mark (mark);
10744
10745 /* Finally update the new watchpoint. This creates the locations
10746 that should be inserted. */
10747 update_watchpoint (w.get (), 1);
10748
10749 install_breakpoint (internal, std::move (w), 1);
10750 }
10751
10752 /* Return count of debug registers needed to watch the given expression.
10753 If the watchpoint cannot be handled in hardware return zero. */
10754
10755 static int
10756 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10757 {
10758 int found_memory_cnt = 0;
10759
10760 /* Did the user specifically forbid us to use hardware watchpoints? */
10761 if (!can_use_hw_watchpoints)
10762 return 0;
10763
10764 gdb_assert (!vals.empty ());
10765 struct value *head = vals[0].get ();
10766
10767 /* Make sure that the value of the expression depends only upon
10768 memory contents, and values computed from them within GDB. If we
10769 find any register references or function calls, we can't use a
10770 hardware watchpoint.
10771
10772 The idea here is that evaluating an expression generates a series
10773 of values, one holding the value of every subexpression. (The
10774 expression a*b+c has five subexpressions: a, b, a*b, c, and
10775 a*b+c.) GDB's values hold almost enough information to establish
10776 the criteria given above --- they identify memory lvalues,
10777 register lvalues, computed values, etcetera. So we can evaluate
10778 the expression, and then scan the chain of values that leaves
10779 behind to decide whether we can detect any possible change to the
10780 expression's final value using only hardware watchpoints.
10781
10782 However, I don't think that the values returned by inferior
10783 function calls are special in any way. So this function may not
10784 notice that an expression involving an inferior function call
10785 can't be watched with hardware watchpoints. FIXME. */
10786 for (const value_ref_ptr &iter : vals)
10787 {
10788 struct value *v = iter.get ();
10789
10790 if (VALUE_LVAL (v) == lval_memory)
10791 {
10792 if (v != head && value_lazy (v))
10793 /* A lazy memory lvalue in the chain is one that GDB never
10794 needed to fetch; we either just used its address (e.g.,
10795 `a' in `a.b') or we never needed it at all (e.g., `a'
10796 in `a,b'). This doesn't apply to HEAD; if that is
10797 lazy then it was not readable, but watch it anyway. */
10798 ;
10799 else
10800 {
10801 /* Ahh, memory we actually used! Check if we can cover
10802 it with hardware watchpoints. */
10803 struct type *vtype = check_typedef (value_type (v));
10804
10805 /* We only watch structs and arrays if user asked for it
10806 explicitly, never if they just happen to appear in a
10807 middle of some value chain. */
10808 if (v == head
10809 || (vtype->code () != TYPE_CODE_STRUCT
10810 && vtype->code () != TYPE_CODE_ARRAY))
10811 {
10812 CORE_ADDR vaddr = value_address (v);
10813 int len;
10814 int num_regs;
10815
10816 len = (target_exact_watchpoints
10817 && is_scalar_type_recursive (vtype))?
10818 1 : TYPE_LENGTH (value_type (v));
10819
10820 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10821 if (!num_regs)
10822 return 0;
10823 else
10824 found_memory_cnt += num_regs;
10825 }
10826 }
10827 }
10828 else if (VALUE_LVAL (v) != not_lval
10829 && deprecated_value_modifiable (v) == 0)
10830 return 0; /* These are values from the history (e.g., $1). */
10831 else if (VALUE_LVAL (v) == lval_register)
10832 return 0; /* Cannot watch a register with a HW watchpoint. */
10833 }
10834
10835 /* The expression itself looks suitable for using a hardware
10836 watchpoint, but give the target machine a chance to reject it. */
10837 return found_memory_cnt;
10838 }
10839
10840 void
10841 watch_command_wrapper (const char *arg, int from_tty, int internal)
10842 {
10843 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10844 }
10845
10846 /* A helper function that looks for the "-location" argument and then
10847 calls watch_command_1. */
10848
10849 static void
10850 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10851 {
10852 int just_location = 0;
10853
10854 if (arg
10855 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10856 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10857 just_location = 1;
10858
10859 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10860 }
10861
10862 static void
10863 watch_command (const char *arg, int from_tty)
10864 {
10865 watch_maybe_just_location (arg, hw_write, from_tty);
10866 }
10867
10868 void
10869 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
10870 {
10871 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10872 }
10873
10874 static void
10875 rwatch_command (const char *arg, int from_tty)
10876 {
10877 watch_maybe_just_location (arg, hw_read, from_tty);
10878 }
10879
10880 void
10881 awatch_command_wrapper (const char *arg, int from_tty, int internal)
10882 {
10883 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10884 }
10885
10886 static void
10887 awatch_command (const char *arg, int from_tty)
10888 {
10889 watch_maybe_just_location (arg, hw_access, from_tty);
10890 }
10891 \f
10892
10893 /* Data for the FSM that manages the until(location)/advance commands
10894 in infcmd.c. Here because it uses the mechanisms of
10895 breakpoints. */
10896
10897 struct until_break_fsm : public thread_fsm
10898 {
10899 /* The thread that was current when the command was executed. */
10900 int thread;
10901
10902 /* The breakpoint set at the destination location. */
10903 breakpoint_up location_breakpoint;
10904
10905 /* Breakpoint set at the return address in the caller frame. May be
10906 NULL. */
10907 breakpoint_up caller_breakpoint;
10908
10909 until_break_fsm (struct interp *cmd_interp, int thread,
10910 breakpoint_up &&location_breakpoint,
10911 breakpoint_up &&caller_breakpoint)
10912 : thread_fsm (cmd_interp),
10913 thread (thread),
10914 location_breakpoint (std::move (location_breakpoint)),
10915 caller_breakpoint (std::move (caller_breakpoint))
10916 {
10917 }
10918
10919 void clean_up (struct thread_info *thread) override;
10920 bool should_stop (struct thread_info *thread) override;
10921 enum async_reply_reason do_async_reply_reason () override;
10922 };
10923
10924 /* Implementation of the 'should_stop' FSM method for the
10925 until(location)/advance commands. */
10926
10927 bool
10928 until_break_fsm::should_stop (struct thread_info *tp)
10929 {
10930 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10931 location_breakpoint.get ()) != NULL
10932 || (caller_breakpoint != NULL
10933 && bpstat_find_breakpoint (tp->control.stop_bpstat,
10934 caller_breakpoint.get ()) != NULL))
10935 set_finished ();
10936
10937 return true;
10938 }
10939
10940 /* Implementation of the 'clean_up' FSM method for the
10941 until(location)/advance commands. */
10942
10943 void
10944 until_break_fsm::clean_up (struct thread_info *)
10945 {
10946 /* Clean up our temporary breakpoints. */
10947 location_breakpoint.reset ();
10948 caller_breakpoint.reset ();
10949 delete_longjmp_breakpoint (thread);
10950 }
10951
10952 /* Implementation of the 'async_reply_reason' FSM method for the
10953 until(location)/advance commands. */
10954
10955 enum async_reply_reason
10956 until_break_fsm::do_async_reply_reason ()
10957 {
10958 return EXEC_ASYNC_LOCATION_REACHED;
10959 }
10960
10961 void
10962 until_break_command (const char *arg, int from_tty, int anywhere)
10963 {
10964 struct frame_info *frame;
10965 struct gdbarch *frame_gdbarch;
10966 struct frame_id stack_frame_id;
10967 struct frame_id caller_frame_id;
10968 int thread;
10969 struct thread_info *tp;
10970
10971 clear_proceed_status (0);
10972
10973 /* Set a breakpoint where the user wants it and at return from
10974 this function. */
10975
10976 event_location_up location = string_to_event_location (&arg, current_language);
10977
10978 std::vector<symtab_and_line> sals
10979 = (last_displayed_sal_is_valid ()
10980 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10981 get_last_displayed_symtab (),
10982 get_last_displayed_line ())
10983 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
10984 NULL, NULL, 0));
10985
10986 if (sals.size () != 1)
10987 error (_("Couldn't get information on specified line."));
10988
10989 symtab_and_line &sal = sals[0];
10990
10991 if (*arg)
10992 error (_("Junk at end of arguments."));
10993
10994 resolve_sal_pc (&sal);
10995
10996 tp = inferior_thread ();
10997 thread = tp->global_num;
10998
10999 /* Note linespec handling above invalidates the frame chain.
11000 Installing a breakpoint also invalidates the frame chain (as it
11001 may need to switch threads), so do any frame handling before
11002 that. */
11003
11004 frame = get_selected_frame (NULL);
11005 frame_gdbarch = get_frame_arch (frame);
11006 stack_frame_id = get_stack_frame_id (frame);
11007 caller_frame_id = frame_unwind_caller_id (frame);
11008
11009 /* Keep within the current frame, or in frames called by the current
11010 one. */
11011
11012 breakpoint_up caller_breakpoint;
11013
11014 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11015
11016 if (frame_id_p (caller_frame_id))
11017 {
11018 struct symtab_and_line sal2;
11019 struct gdbarch *caller_gdbarch;
11020
11021 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11022 sal2.pc = frame_unwind_caller_pc (frame);
11023 caller_gdbarch = frame_unwind_caller_arch (frame);
11024 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11025 sal2,
11026 caller_frame_id,
11027 bp_until);
11028
11029 set_longjmp_breakpoint (tp, caller_frame_id);
11030 lj_deleter.emplace (thread);
11031 }
11032
11033 /* set_momentary_breakpoint could invalidate FRAME. */
11034 frame = NULL;
11035
11036 breakpoint_up location_breakpoint;
11037 if (anywhere)
11038 /* If the user told us to continue until a specified location,
11039 we don't specify a frame at which we need to stop. */
11040 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11041 null_frame_id, bp_until);
11042 else
11043 /* Otherwise, specify the selected frame, because we want to stop
11044 only at the very same frame. */
11045 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11046 stack_frame_id, bp_until);
11047
11048 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11049 std::move (location_breakpoint),
11050 std::move (caller_breakpoint));
11051
11052 if (lj_deleter)
11053 lj_deleter->release ();
11054
11055 proceed (-1, GDB_SIGNAL_DEFAULT);
11056 }
11057
11058 /* This function attempts to parse an optional "if <cond>" clause
11059 from the arg string. If one is not found, it returns NULL.
11060
11061 Else, it returns a pointer to the condition string. (It does not
11062 attempt to evaluate the string against a particular block.) And,
11063 it updates arg to point to the first character following the parsed
11064 if clause in the arg string. */
11065
11066 const char *
11067 ep_parse_optional_if_clause (const char **arg)
11068 {
11069 const char *cond_string;
11070
11071 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11072 return NULL;
11073
11074 /* Skip the "if" keyword. */
11075 (*arg) += 2;
11076
11077 /* Skip any extra leading whitespace, and record the start of the
11078 condition string. */
11079 *arg = skip_spaces (*arg);
11080 cond_string = *arg;
11081
11082 /* Assume that the condition occupies the remainder of the arg
11083 string. */
11084 (*arg) += strlen (cond_string);
11085
11086 return cond_string;
11087 }
11088
11089 /* Commands to deal with catching events, such as signals, exceptions,
11090 process start/exit, etc. */
11091
11092 typedef enum
11093 {
11094 catch_fork_temporary, catch_vfork_temporary,
11095 catch_fork_permanent, catch_vfork_permanent
11096 }
11097 catch_fork_kind;
11098
11099 static void
11100 catch_fork_command_1 (const char *arg, int from_tty,
11101 struct cmd_list_element *command)
11102 {
11103 struct gdbarch *gdbarch = get_current_arch ();
11104 const char *cond_string = NULL;
11105 catch_fork_kind fork_kind;
11106 int tempflag;
11107
11108 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11109 tempflag = (fork_kind == catch_fork_temporary
11110 || fork_kind == catch_vfork_temporary);
11111
11112 if (!arg)
11113 arg = "";
11114 arg = skip_spaces (arg);
11115
11116 /* The allowed syntax is:
11117 catch [v]fork
11118 catch [v]fork if <cond>
11119
11120 First, check if there's an if clause. */
11121 cond_string = ep_parse_optional_if_clause (&arg);
11122
11123 if ((*arg != '\0') && !isspace (*arg))
11124 error (_("Junk at end of arguments."));
11125
11126 /* If this target supports it, create a fork or vfork catchpoint
11127 and enable reporting of such events. */
11128 switch (fork_kind)
11129 {
11130 case catch_fork_temporary:
11131 case catch_fork_permanent:
11132 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11133 &catch_fork_breakpoint_ops);
11134 break;
11135 case catch_vfork_temporary:
11136 case catch_vfork_permanent:
11137 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11138 &catch_vfork_breakpoint_ops);
11139 break;
11140 default:
11141 error (_("unsupported or unknown fork kind; cannot catch it"));
11142 break;
11143 }
11144 }
11145
11146 static void
11147 catch_exec_command_1 (const char *arg, int from_tty,
11148 struct cmd_list_element *command)
11149 {
11150 struct gdbarch *gdbarch = get_current_arch ();
11151 int tempflag;
11152 const char *cond_string = NULL;
11153
11154 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11155
11156 if (!arg)
11157 arg = "";
11158 arg = skip_spaces (arg);
11159
11160 /* The allowed syntax is:
11161 catch exec
11162 catch exec if <cond>
11163
11164 First, check if there's an if clause. */
11165 cond_string = ep_parse_optional_if_clause (&arg);
11166
11167 if ((*arg != '\0') && !isspace (*arg))
11168 error (_("Junk at end of arguments."));
11169
11170 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11171 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11172 &catch_exec_breakpoint_ops);
11173 c->exec_pathname = NULL;
11174
11175 install_breakpoint (0, std::move (c), 1);
11176 }
11177
11178 void
11179 init_ada_exception_breakpoint (struct breakpoint *b,
11180 struct gdbarch *gdbarch,
11181 struct symtab_and_line sal,
11182 const char *addr_string,
11183 const struct breakpoint_ops *ops,
11184 int tempflag,
11185 int enabled,
11186 int from_tty)
11187 {
11188 if (from_tty)
11189 {
11190 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11191 if (!loc_gdbarch)
11192 loc_gdbarch = gdbarch;
11193
11194 describe_other_breakpoints (loc_gdbarch,
11195 sal.pspace, sal.pc, sal.section, -1);
11196 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11197 version for exception catchpoints, because two catchpoints
11198 used for different exception names will use the same address.
11199 In this case, a "breakpoint ... also set at..." warning is
11200 unproductive. Besides, the warning phrasing is also a bit
11201 inappropriate, we should use the word catchpoint, and tell
11202 the user what type of catchpoint it is. The above is good
11203 enough for now, though. */
11204 }
11205
11206 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11207
11208 b->enable_state = enabled ? bp_enabled : bp_disabled;
11209 b->disposition = tempflag ? disp_del : disp_donttouch;
11210 b->location = string_to_event_location (&addr_string,
11211 language_def (language_ada));
11212 b->language = language_ada;
11213 }
11214
11215 \f
11216
11217 /* Compare two breakpoints and return a strcmp-like result. */
11218
11219 static int
11220 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11221 {
11222 uintptr_t ua = (uintptr_t) a;
11223 uintptr_t ub = (uintptr_t) b;
11224
11225 if (a->number < b->number)
11226 return -1;
11227 else if (a->number > b->number)
11228 return 1;
11229
11230 /* Now sort by address, in case we see, e..g, two breakpoints with
11231 the number 0. */
11232 if (ua < ub)
11233 return -1;
11234 return ua > ub ? 1 : 0;
11235 }
11236
11237 /* Delete breakpoints by address or line. */
11238
11239 static void
11240 clear_command (const char *arg, int from_tty)
11241 {
11242 struct breakpoint *b;
11243 int default_match;
11244
11245 std::vector<symtab_and_line> decoded_sals;
11246 symtab_and_line last_sal;
11247 gdb::array_view<symtab_and_line> sals;
11248 if (arg)
11249 {
11250 decoded_sals
11251 = decode_line_with_current_source (arg,
11252 (DECODE_LINE_FUNFIRSTLINE
11253 | DECODE_LINE_LIST_MODE));
11254 default_match = 0;
11255 sals = decoded_sals;
11256 }
11257 else
11258 {
11259 /* Set sal's line, symtab, pc, and pspace to the values
11260 corresponding to the last call to print_frame_info. If the
11261 codepoint is not valid, this will set all the fields to 0. */
11262 last_sal = get_last_displayed_sal ();
11263 if (last_sal.symtab == 0)
11264 error (_("No source file specified."));
11265
11266 default_match = 1;
11267 sals = last_sal;
11268 }
11269
11270 /* We don't call resolve_sal_pc here. That's not as bad as it
11271 seems, because all existing breakpoints typically have both
11272 file/line and pc set. So, if clear is given file/line, we can
11273 match this to existing breakpoint without obtaining pc at all.
11274
11275 We only support clearing given the address explicitly
11276 present in breakpoint table. Say, we've set breakpoint
11277 at file:line. There were several PC values for that file:line,
11278 due to optimization, all in one block.
11279
11280 We've picked one PC value. If "clear" is issued with another
11281 PC corresponding to the same file:line, the breakpoint won't
11282 be cleared. We probably can still clear the breakpoint, but
11283 since the other PC value is never presented to user, user
11284 can only find it by guessing, and it does not seem important
11285 to support that. */
11286
11287 /* For each line spec given, delete bps which correspond to it. Do
11288 it in two passes, solely to preserve the current behavior that
11289 from_tty is forced true if we delete more than one
11290 breakpoint. */
11291
11292 std::vector<struct breakpoint *> found;
11293 for (const auto &sal : sals)
11294 {
11295 const char *sal_fullname;
11296
11297 /* If exact pc given, clear bpts at that pc.
11298 If line given (pc == 0), clear all bpts on specified line.
11299 If defaulting, clear all bpts on default line
11300 or at default pc.
11301
11302 defaulting sal.pc != 0 tests to do
11303
11304 0 1 pc
11305 1 1 pc _and_ line
11306 0 0 line
11307 1 0 <can't happen> */
11308
11309 sal_fullname = (sal.symtab == NULL
11310 ? NULL : symtab_to_fullname (sal.symtab));
11311
11312 /* Find all matching breakpoints and add them to 'found'. */
11313 ALL_BREAKPOINTS (b)
11314 {
11315 int match = 0;
11316 /* Are we going to delete b? */
11317 if (b->type != bp_none && !is_watchpoint (b))
11318 {
11319 struct bp_location *loc = b->loc;
11320 for (; loc; loc = loc->next)
11321 {
11322 /* If the user specified file:line, don't allow a PC
11323 match. This matches historical gdb behavior. */
11324 int pc_match = (!sal.explicit_line
11325 && sal.pc
11326 && (loc->pspace == sal.pspace)
11327 && (loc->address == sal.pc)
11328 && (!section_is_overlay (loc->section)
11329 || loc->section == sal.section));
11330 int line_match = 0;
11331
11332 if ((default_match || sal.explicit_line)
11333 && loc->symtab != NULL
11334 && sal_fullname != NULL
11335 && sal.pspace == loc->pspace
11336 && loc->line_number == sal.line
11337 && filename_cmp (symtab_to_fullname (loc->symtab),
11338 sal_fullname) == 0)
11339 line_match = 1;
11340
11341 if (pc_match || line_match)
11342 {
11343 match = 1;
11344 break;
11345 }
11346 }
11347 }
11348
11349 if (match)
11350 found.push_back (b);
11351 }
11352 }
11353
11354 /* Now go thru the 'found' chain and delete them. */
11355 if (found.empty ())
11356 {
11357 if (arg)
11358 error (_("No breakpoint at %s."), arg);
11359 else
11360 error (_("No breakpoint at this line."));
11361 }
11362
11363 /* Remove duplicates from the vec. */
11364 std::sort (found.begin (), found.end (),
11365 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11366 {
11367 return compare_breakpoints (bp_a, bp_b) < 0;
11368 });
11369 found.erase (std::unique (found.begin (), found.end (),
11370 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11371 {
11372 return compare_breakpoints (bp_a, bp_b) == 0;
11373 }),
11374 found.end ());
11375
11376 if (found.size () > 1)
11377 from_tty = 1; /* Always report if deleted more than one. */
11378 if (from_tty)
11379 {
11380 if (found.size () == 1)
11381 printf_unfiltered (_("Deleted breakpoint "));
11382 else
11383 printf_unfiltered (_("Deleted breakpoints "));
11384 }
11385
11386 for (breakpoint *iter : found)
11387 {
11388 if (from_tty)
11389 printf_unfiltered ("%d ", iter->number);
11390 delete_breakpoint (iter);
11391 }
11392 if (from_tty)
11393 putchar_unfiltered ('\n');
11394 }
11395 \f
11396 /* Delete breakpoint in BS if they are `delete' breakpoints and
11397 all breakpoints that are marked for deletion, whether hit or not.
11398 This is called after any breakpoint is hit, or after errors. */
11399
11400 void
11401 breakpoint_auto_delete (bpstat bs)
11402 {
11403 struct breakpoint *b, *b_tmp;
11404
11405 for (; bs; bs = bs->next)
11406 if (bs->breakpoint_at
11407 && bs->breakpoint_at->disposition == disp_del
11408 && bs->stop)
11409 delete_breakpoint (bs->breakpoint_at);
11410
11411 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11412 {
11413 if (b->disposition == disp_del_at_next_stop)
11414 delete_breakpoint (b);
11415 }
11416 }
11417
11418 /* A comparison function for bp_location AP and BP being interfaced to
11419 std::sort. Sort elements primarily by their ADDRESS (no matter what
11420 bl_address_is_meaningful says), secondarily by ordering first
11421 permanent elements and terciarily just ensuring the array is sorted
11422 stable way despite std::sort being an unstable algorithm. */
11423
11424 static int
11425 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11426 {
11427 if (a->address != b->address)
11428 return a->address < b->address;
11429
11430 /* Sort locations at the same address by their pspace number, keeping
11431 locations of the same inferior (in a multi-inferior environment)
11432 grouped. */
11433
11434 if (a->pspace->num != b->pspace->num)
11435 return a->pspace->num < b->pspace->num;
11436
11437 /* Sort permanent breakpoints first. */
11438 if (a->permanent != b->permanent)
11439 return a->permanent > b->permanent;
11440
11441 /* Make the internal GDB representation stable across GDB runs
11442 where A and B memory inside GDB can differ. Breakpoint locations of
11443 the same type at the same address can be sorted in arbitrary order. */
11444
11445 if (a->owner->number != b->owner->number)
11446 return a->owner->number < b->owner->number;
11447
11448 return a < b;
11449 }
11450
11451 /* Set bp_locations_placed_address_before_address_max and
11452 bp_locations_shadow_len_after_address_max according to the current
11453 content of the bp_locations array. */
11454
11455 static void
11456 bp_locations_target_extensions_update (void)
11457 {
11458 struct bp_location *bl, **blp_tmp;
11459
11460 bp_locations_placed_address_before_address_max = 0;
11461 bp_locations_shadow_len_after_address_max = 0;
11462
11463 ALL_BP_LOCATIONS (bl, blp_tmp)
11464 {
11465 CORE_ADDR start, end, addr;
11466
11467 if (!bp_location_has_shadow (bl))
11468 continue;
11469
11470 start = bl->target_info.placed_address;
11471 end = start + bl->target_info.shadow_len;
11472
11473 gdb_assert (bl->address >= start);
11474 addr = bl->address - start;
11475 if (addr > bp_locations_placed_address_before_address_max)
11476 bp_locations_placed_address_before_address_max = addr;
11477
11478 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11479
11480 gdb_assert (bl->address < end);
11481 addr = end - bl->address;
11482 if (addr > bp_locations_shadow_len_after_address_max)
11483 bp_locations_shadow_len_after_address_max = addr;
11484 }
11485 }
11486
11487 /* Download tracepoint locations if they haven't been. */
11488
11489 static void
11490 download_tracepoint_locations (void)
11491 {
11492 struct breakpoint *b;
11493 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11494
11495 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11496
11497 ALL_TRACEPOINTS (b)
11498 {
11499 struct bp_location *bl;
11500 struct tracepoint *t;
11501 int bp_location_downloaded = 0;
11502
11503 if ((b->type == bp_fast_tracepoint
11504 ? !may_insert_fast_tracepoints
11505 : !may_insert_tracepoints))
11506 continue;
11507
11508 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11509 {
11510 if (target_can_download_tracepoint ())
11511 can_download_tracepoint = TRIBOOL_TRUE;
11512 else
11513 can_download_tracepoint = TRIBOOL_FALSE;
11514 }
11515
11516 if (can_download_tracepoint == TRIBOOL_FALSE)
11517 break;
11518
11519 for (bl = b->loc; bl; bl = bl->next)
11520 {
11521 /* In tracepoint, locations are _never_ duplicated, so
11522 should_be_inserted is equivalent to
11523 unduplicated_should_be_inserted. */
11524 if (!should_be_inserted (bl) || bl->inserted)
11525 continue;
11526
11527 switch_to_program_space_and_thread (bl->pspace);
11528
11529 target_download_tracepoint (bl);
11530
11531 bl->inserted = 1;
11532 bp_location_downloaded = 1;
11533 }
11534 t = (struct tracepoint *) b;
11535 t->number_on_target = b->number;
11536 if (bp_location_downloaded)
11537 gdb::observers::breakpoint_modified.notify (b);
11538 }
11539 }
11540
11541 /* Swap the insertion/duplication state between two locations. */
11542
11543 static void
11544 swap_insertion (struct bp_location *left, struct bp_location *right)
11545 {
11546 const int left_inserted = left->inserted;
11547 const int left_duplicate = left->duplicate;
11548 const int left_needs_update = left->needs_update;
11549 const struct bp_target_info left_target_info = left->target_info;
11550
11551 /* Locations of tracepoints can never be duplicated. */
11552 if (is_tracepoint (left->owner))
11553 gdb_assert (!left->duplicate);
11554 if (is_tracepoint (right->owner))
11555 gdb_assert (!right->duplicate);
11556
11557 left->inserted = right->inserted;
11558 left->duplicate = right->duplicate;
11559 left->needs_update = right->needs_update;
11560 left->target_info = right->target_info;
11561 right->inserted = left_inserted;
11562 right->duplicate = left_duplicate;
11563 right->needs_update = left_needs_update;
11564 right->target_info = left_target_info;
11565 }
11566
11567 /* Force the re-insertion of the locations at ADDRESS. This is called
11568 once a new/deleted/modified duplicate location is found and we are evaluating
11569 conditions on the target's side. Such conditions need to be updated on
11570 the target. */
11571
11572 static void
11573 force_breakpoint_reinsertion (struct bp_location *bl)
11574 {
11575 struct bp_location **locp = NULL, **loc2p;
11576 struct bp_location *loc;
11577 CORE_ADDR address = 0;
11578 int pspace_num;
11579
11580 address = bl->address;
11581 pspace_num = bl->pspace->num;
11582
11583 /* This is only meaningful if the target is
11584 evaluating conditions and if the user has
11585 opted for condition evaluation on the target's
11586 side. */
11587 if (gdb_evaluates_breakpoint_condition_p ()
11588 || !target_supports_evaluation_of_breakpoint_conditions ())
11589 return;
11590
11591 /* Flag all breakpoint locations with this address and
11592 the same program space as the location
11593 as "its condition has changed". We need to
11594 update the conditions on the target's side. */
11595 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11596 {
11597 loc = *loc2p;
11598
11599 if (!is_breakpoint (loc->owner)
11600 || pspace_num != loc->pspace->num)
11601 continue;
11602
11603 /* Flag the location appropriately. We use a different state to
11604 let everyone know that we already updated the set of locations
11605 with addr bl->address and program space bl->pspace. This is so
11606 we don't have to keep calling these functions just to mark locations
11607 that have already been marked. */
11608 loc->condition_changed = condition_updated;
11609
11610 /* Free the agent expression bytecode as well. We will compute
11611 it later on. */
11612 loc->cond_bytecode.reset ();
11613 }
11614 }
11615 /* Called whether new breakpoints are created, or existing breakpoints
11616 deleted, to update the global location list and recompute which
11617 locations are duplicate of which.
11618
11619 The INSERT_MODE flag determines whether locations may not, may, or
11620 shall be inserted now. See 'enum ugll_insert_mode' for more
11621 info. */
11622
11623 static void
11624 update_global_location_list (enum ugll_insert_mode insert_mode)
11625 {
11626 struct breakpoint *b;
11627 struct bp_location **locp, *loc;
11628 /* Last breakpoint location address that was marked for update. */
11629 CORE_ADDR last_addr = 0;
11630 /* Last breakpoint location program space that was marked for update. */
11631 int last_pspace_num = -1;
11632
11633 /* Used in the duplicates detection below. When iterating over all
11634 bp_locations, points to the first bp_location of a given address.
11635 Breakpoints and watchpoints of different types are never
11636 duplicates of each other. Keep one pointer for each type of
11637 breakpoint/watchpoint, so we only need to loop over all locations
11638 once. */
11639 struct bp_location *bp_loc_first; /* breakpoint */
11640 struct bp_location *wp_loc_first; /* hardware watchpoint */
11641 struct bp_location *awp_loc_first; /* access watchpoint */
11642 struct bp_location *rwp_loc_first; /* read watchpoint */
11643
11644 /* Saved former bp_locations array which we compare against the newly
11645 built bp_locations from the current state of ALL_BREAKPOINTS. */
11646 struct bp_location **old_locp;
11647 unsigned old_locations_count;
11648 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11649
11650 old_locations_count = bp_locations_count;
11651 bp_locations = NULL;
11652 bp_locations_count = 0;
11653
11654 ALL_BREAKPOINTS (b)
11655 for (loc = b->loc; loc; loc = loc->next)
11656 bp_locations_count++;
11657
11658 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11659 locp = bp_locations;
11660 ALL_BREAKPOINTS (b)
11661 for (loc = b->loc; loc; loc = loc->next)
11662 *locp++ = loc;
11663 std::sort (bp_locations, bp_locations + bp_locations_count,
11664 bp_location_is_less_than);
11665
11666 bp_locations_target_extensions_update ();
11667
11668 /* Identify bp_location instances that are no longer present in the
11669 new list, and therefore should be freed. Note that it's not
11670 necessary that those locations should be removed from inferior --
11671 if there's another location at the same address (previously
11672 marked as duplicate), we don't need to remove/insert the
11673 location.
11674
11675 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11676 and former bp_location array state respectively. */
11677
11678 locp = bp_locations;
11679 for (old_locp = old_locations.get ();
11680 old_locp < old_locations.get () + old_locations_count;
11681 old_locp++)
11682 {
11683 struct bp_location *old_loc = *old_locp;
11684 struct bp_location **loc2p;
11685
11686 /* Tells if 'old_loc' is found among the new locations. If
11687 not, we have to free it. */
11688 int found_object = 0;
11689 /* Tells if the location should remain inserted in the target. */
11690 int keep_in_target = 0;
11691 int removed = 0;
11692
11693 /* Skip LOCP entries which will definitely never be needed.
11694 Stop either at or being the one matching OLD_LOC. */
11695 while (locp < bp_locations + bp_locations_count
11696 && (*locp)->address < old_loc->address)
11697 locp++;
11698
11699 for (loc2p = locp;
11700 (loc2p < bp_locations + bp_locations_count
11701 && (*loc2p)->address == old_loc->address);
11702 loc2p++)
11703 {
11704 /* Check if this is a new/duplicated location or a duplicated
11705 location that had its condition modified. If so, we want to send
11706 its condition to the target if evaluation of conditions is taking
11707 place there. */
11708 if ((*loc2p)->condition_changed == condition_modified
11709 && (last_addr != old_loc->address
11710 || last_pspace_num != old_loc->pspace->num))
11711 {
11712 force_breakpoint_reinsertion (*loc2p);
11713 last_pspace_num = old_loc->pspace->num;
11714 }
11715
11716 if (*loc2p == old_loc)
11717 found_object = 1;
11718 }
11719
11720 /* We have already handled this address, update it so that we don't
11721 have to go through updates again. */
11722 last_addr = old_loc->address;
11723
11724 /* Target-side condition evaluation: Handle deleted locations. */
11725 if (!found_object)
11726 force_breakpoint_reinsertion (old_loc);
11727
11728 /* If this location is no longer present, and inserted, look if
11729 there's maybe a new location at the same address. If so,
11730 mark that one inserted, and don't remove this one. This is
11731 needed so that we don't have a time window where a breakpoint
11732 at certain location is not inserted. */
11733
11734 if (old_loc->inserted)
11735 {
11736 /* If the location is inserted now, we might have to remove
11737 it. */
11738
11739 if (found_object && should_be_inserted (old_loc))
11740 {
11741 /* The location is still present in the location list,
11742 and still should be inserted. Don't do anything. */
11743 keep_in_target = 1;
11744 }
11745 else
11746 {
11747 /* This location still exists, but it won't be kept in the
11748 target since it may have been disabled. We proceed to
11749 remove its target-side condition. */
11750
11751 /* The location is either no longer present, or got
11752 disabled. See if there's another location at the
11753 same address, in which case we don't need to remove
11754 this one from the target. */
11755
11756 /* OLD_LOC comes from existing struct breakpoint. */
11757 if (bl_address_is_meaningful (old_loc))
11758 {
11759 for (loc2p = locp;
11760 (loc2p < bp_locations + bp_locations_count
11761 && (*loc2p)->address == old_loc->address);
11762 loc2p++)
11763 {
11764 struct bp_location *loc2 = *loc2p;
11765
11766 if (breakpoint_locations_match (loc2, old_loc))
11767 {
11768 /* Read watchpoint locations are switched to
11769 access watchpoints, if the former are not
11770 supported, but the latter are. */
11771 if (is_hardware_watchpoint (old_loc->owner))
11772 {
11773 gdb_assert (is_hardware_watchpoint (loc2->owner));
11774 loc2->watchpoint_type = old_loc->watchpoint_type;
11775 }
11776
11777 /* loc2 is a duplicated location. We need to check
11778 if it should be inserted in case it will be
11779 unduplicated. */
11780 if (loc2 != old_loc
11781 && unduplicated_should_be_inserted (loc2))
11782 {
11783 swap_insertion (old_loc, loc2);
11784 keep_in_target = 1;
11785 break;
11786 }
11787 }
11788 }
11789 }
11790 }
11791
11792 if (!keep_in_target)
11793 {
11794 if (remove_breakpoint (old_loc))
11795 {
11796 /* This is just about all we can do. We could keep
11797 this location on the global list, and try to
11798 remove it next time, but there's no particular
11799 reason why we will succeed next time.
11800
11801 Note that at this point, old_loc->owner is still
11802 valid, as delete_breakpoint frees the breakpoint
11803 only after calling us. */
11804 printf_filtered (_("warning: Error removing "
11805 "breakpoint %d\n"),
11806 old_loc->owner->number);
11807 }
11808 removed = 1;
11809 }
11810 }
11811
11812 if (!found_object)
11813 {
11814 if (removed && target_is_non_stop_p ()
11815 && need_moribund_for_location_type (old_loc))
11816 {
11817 /* This location was removed from the target. In
11818 non-stop mode, a race condition is possible where
11819 we've removed a breakpoint, but stop events for that
11820 breakpoint are already queued and will arrive later.
11821 We apply an heuristic to be able to distinguish such
11822 SIGTRAPs from other random SIGTRAPs: we keep this
11823 breakpoint location for a bit, and will retire it
11824 after we see some number of events. The theory here
11825 is that reporting of events should, "on the average",
11826 be fair, so after a while we'll see events from all
11827 threads that have anything of interest, and no longer
11828 need to keep this breakpoint location around. We
11829 don't hold locations forever so to reduce chances of
11830 mistaking a non-breakpoint SIGTRAP for a breakpoint
11831 SIGTRAP.
11832
11833 The heuristic failing can be disastrous on
11834 decr_pc_after_break targets.
11835
11836 On decr_pc_after_break targets, like e.g., x86-linux,
11837 if we fail to recognize a late breakpoint SIGTRAP,
11838 because events_till_retirement has reached 0 too
11839 soon, we'll fail to do the PC adjustment, and report
11840 a random SIGTRAP to the user. When the user resumes
11841 the inferior, it will most likely immediately crash
11842 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11843 corrupted, because of being resumed e.g., in the
11844 middle of a multi-byte instruction, or skipped a
11845 one-byte instruction. This was actually seen happen
11846 on native x86-linux, and should be less rare on
11847 targets that do not support new thread events, like
11848 remote, due to the heuristic depending on
11849 thread_count.
11850
11851 Mistaking a random SIGTRAP for a breakpoint trap
11852 causes similar symptoms (PC adjustment applied when
11853 it shouldn't), but then again, playing with SIGTRAPs
11854 behind the debugger's back is asking for trouble.
11855
11856 Since hardware watchpoint traps are always
11857 distinguishable from other traps, so we don't need to
11858 apply keep hardware watchpoint moribund locations
11859 around. We simply always ignore hardware watchpoint
11860 traps we can no longer explain. */
11861
11862 process_stratum_target *proc_target = nullptr;
11863 for (inferior *inf : all_inferiors ())
11864 if (inf->pspace == old_loc->pspace)
11865 {
11866 proc_target = inf->process_target ();
11867 break;
11868 }
11869 if (proc_target != nullptr)
11870 old_loc->events_till_retirement
11871 = 3 * (thread_count (proc_target) + 1);
11872 else
11873 old_loc->events_till_retirement = 1;
11874 old_loc->owner = NULL;
11875
11876 moribund_locations.push_back (old_loc);
11877 }
11878 else
11879 {
11880 old_loc->owner = NULL;
11881 decref_bp_location (&old_loc);
11882 }
11883 }
11884 }
11885
11886 /* Rescan breakpoints at the same address and section, marking the
11887 first one as "first" and any others as "duplicates". This is so
11888 that the bpt instruction is only inserted once. If we have a
11889 permanent breakpoint at the same place as BPT, make that one the
11890 official one, and the rest as duplicates. Permanent breakpoints
11891 are sorted first for the same address.
11892
11893 Do the same for hardware watchpoints, but also considering the
11894 watchpoint's type (regular/access/read) and length. */
11895
11896 bp_loc_first = NULL;
11897 wp_loc_first = NULL;
11898 awp_loc_first = NULL;
11899 rwp_loc_first = NULL;
11900 ALL_BP_LOCATIONS (loc, locp)
11901 {
11902 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11903 non-NULL. */
11904 struct bp_location **loc_first_p;
11905 b = loc->owner;
11906
11907 if (!unduplicated_should_be_inserted (loc)
11908 || !bl_address_is_meaningful (loc)
11909 /* Don't detect duplicate for tracepoint locations because they are
11910 never duplicated. See the comments in field `duplicate' of
11911 `struct bp_location'. */
11912 || is_tracepoint (b))
11913 {
11914 /* Clear the condition modification flag. */
11915 loc->condition_changed = condition_unchanged;
11916 continue;
11917 }
11918
11919 if (b->type == bp_hardware_watchpoint)
11920 loc_first_p = &wp_loc_first;
11921 else if (b->type == bp_read_watchpoint)
11922 loc_first_p = &rwp_loc_first;
11923 else if (b->type == bp_access_watchpoint)
11924 loc_first_p = &awp_loc_first;
11925 else
11926 loc_first_p = &bp_loc_first;
11927
11928 if (*loc_first_p == NULL
11929 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11930 || !breakpoint_locations_match (loc, *loc_first_p))
11931 {
11932 *loc_first_p = loc;
11933 loc->duplicate = 0;
11934
11935 if (is_breakpoint (loc->owner) && loc->condition_changed)
11936 {
11937 loc->needs_update = 1;
11938 /* Clear the condition modification flag. */
11939 loc->condition_changed = condition_unchanged;
11940 }
11941 continue;
11942 }
11943
11944
11945 /* This and the above ensure the invariant that the first location
11946 is not duplicated, and is the inserted one.
11947 All following are marked as duplicated, and are not inserted. */
11948 if (loc->inserted)
11949 swap_insertion (loc, *loc_first_p);
11950 loc->duplicate = 1;
11951
11952 /* Clear the condition modification flag. */
11953 loc->condition_changed = condition_unchanged;
11954 }
11955
11956 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11957 {
11958 if (insert_mode != UGLL_DONT_INSERT)
11959 insert_breakpoint_locations ();
11960 else
11961 {
11962 /* Even though the caller told us to not insert new
11963 locations, we may still need to update conditions on the
11964 target's side of breakpoints that were already inserted
11965 if the target is evaluating breakpoint conditions. We
11966 only update conditions for locations that are marked
11967 "needs_update". */
11968 update_inserted_breakpoint_locations ();
11969 }
11970 }
11971
11972 if (insert_mode != UGLL_DONT_INSERT)
11973 download_tracepoint_locations ();
11974 }
11975
11976 void
11977 breakpoint_retire_moribund (void)
11978 {
11979 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11980 {
11981 struct bp_location *loc = moribund_locations[ix];
11982 if (--(loc->events_till_retirement) == 0)
11983 {
11984 decref_bp_location (&loc);
11985 unordered_remove (moribund_locations, ix);
11986 --ix;
11987 }
11988 }
11989 }
11990
11991 static void
11992 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11993 {
11994
11995 try
11996 {
11997 update_global_location_list (insert_mode);
11998 }
11999 catch (const gdb_exception_error &e)
12000 {
12001 }
12002 }
12003
12004 /* Clear BKP from a BPS. */
12005
12006 static void
12007 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12008 {
12009 bpstat bs;
12010
12011 for (bs = bps; bs; bs = bs->next)
12012 if (bs->breakpoint_at == bpt)
12013 {
12014 bs->breakpoint_at = NULL;
12015 bs->old_val = NULL;
12016 /* bs->commands will be freed later. */
12017 }
12018 }
12019
12020 /* Callback for iterate_over_threads. */
12021 static int
12022 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12023 {
12024 struct breakpoint *bpt = (struct breakpoint *) data;
12025
12026 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12027 return 0;
12028 }
12029
12030 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12031 callbacks. */
12032
12033 static void
12034 say_where (struct breakpoint *b)
12035 {
12036 struct value_print_options opts;
12037
12038 get_user_print_options (&opts);
12039
12040 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12041 single string. */
12042 if (b->loc == NULL)
12043 {
12044 /* For pending locations, the output differs slightly based
12045 on b->extra_string. If this is non-NULL, it contains either
12046 a condition or dprintf arguments. */
12047 if (b->extra_string == NULL)
12048 {
12049 printf_filtered (_(" (%s) pending."),
12050 event_location_to_string (b->location.get ()));
12051 }
12052 else if (b->type == bp_dprintf)
12053 {
12054 printf_filtered (_(" (%s,%s) pending."),
12055 event_location_to_string (b->location.get ()),
12056 b->extra_string);
12057 }
12058 else
12059 {
12060 printf_filtered (_(" (%s %s) pending."),
12061 event_location_to_string (b->location.get ()),
12062 b->extra_string);
12063 }
12064 }
12065 else
12066 {
12067 if (opts.addressprint || b->loc->symtab == NULL)
12068 printf_filtered (" at %ps",
12069 styled_string (address_style.style (),
12070 paddress (b->loc->gdbarch,
12071 b->loc->address)));
12072 if (b->loc->symtab != NULL)
12073 {
12074 /* If there is a single location, we can print the location
12075 more nicely. */
12076 if (b->loc->next == NULL)
12077 {
12078 const char *filename
12079 = symtab_to_filename_for_display (b->loc->symtab);
12080 printf_filtered (": file %ps, line %d.",
12081 styled_string (file_name_style.style (),
12082 filename),
12083 b->loc->line_number);
12084 }
12085 else
12086 /* This is not ideal, but each location may have a
12087 different file name, and this at least reflects the
12088 real situation somewhat. */
12089 printf_filtered (": %s.",
12090 event_location_to_string (b->location.get ()));
12091 }
12092
12093 if (b->loc->next)
12094 {
12095 struct bp_location *loc = b->loc;
12096 int n = 0;
12097 for (; loc; loc = loc->next)
12098 ++n;
12099 printf_filtered (" (%d locations)", n);
12100 }
12101 }
12102 }
12103
12104 bp_location::~bp_location ()
12105 {
12106 xfree (function_name);
12107 }
12108
12109 /* Destructor for the breakpoint base class. */
12110
12111 breakpoint::~breakpoint ()
12112 {
12113 xfree (this->cond_string);
12114 xfree (this->extra_string);
12115 }
12116
12117 static struct bp_location *
12118 base_breakpoint_allocate_location (struct breakpoint *self)
12119 {
12120 return new bp_location (self);
12121 }
12122
12123 static void
12124 base_breakpoint_re_set (struct breakpoint *b)
12125 {
12126 /* Nothing to re-set. */
12127 }
12128
12129 #define internal_error_pure_virtual_called() \
12130 gdb_assert_not_reached ("pure virtual function called")
12131
12132 static int
12133 base_breakpoint_insert_location (struct bp_location *bl)
12134 {
12135 internal_error_pure_virtual_called ();
12136 }
12137
12138 static int
12139 base_breakpoint_remove_location (struct bp_location *bl,
12140 enum remove_bp_reason reason)
12141 {
12142 internal_error_pure_virtual_called ();
12143 }
12144
12145 static int
12146 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12147 const address_space *aspace,
12148 CORE_ADDR bp_addr,
12149 const struct target_waitstatus *ws)
12150 {
12151 internal_error_pure_virtual_called ();
12152 }
12153
12154 static void
12155 base_breakpoint_check_status (bpstat bs)
12156 {
12157 /* Always stop. */
12158 }
12159
12160 /* A "works_in_software_mode" breakpoint_ops method that just internal
12161 errors. */
12162
12163 static int
12164 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12165 {
12166 internal_error_pure_virtual_called ();
12167 }
12168
12169 /* A "resources_needed" breakpoint_ops method that just internal
12170 errors. */
12171
12172 static int
12173 base_breakpoint_resources_needed (const struct bp_location *bl)
12174 {
12175 internal_error_pure_virtual_called ();
12176 }
12177
12178 static enum print_stop_action
12179 base_breakpoint_print_it (bpstat bs)
12180 {
12181 internal_error_pure_virtual_called ();
12182 }
12183
12184 static void
12185 base_breakpoint_print_one_detail (const struct breakpoint *self,
12186 struct ui_out *uiout)
12187 {
12188 /* nothing */
12189 }
12190
12191 static void
12192 base_breakpoint_print_mention (struct breakpoint *b)
12193 {
12194 internal_error_pure_virtual_called ();
12195 }
12196
12197 static void
12198 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12199 {
12200 internal_error_pure_virtual_called ();
12201 }
12202
12203 static void
12204 base_breakpoint_create_sals_from_location
12205 (const struct event_location *location,
12206 struct linespec_result *canonical,
12207 enum bptype type_wanted)
12208 {
12209 internal_error_pure_virtual_called ();
12210 }
12211
12212 static void
12213 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12214 struct linespec_result *c,
12215 gdb::unique_xmalloc_ptr<char> cond_string,
12216 gdb::unique_xmalloc_ptr<char> extra_string,
12217 enum bptype type_wanted,
12218 enum bpdisp disposition,
12219 int thread,
12220 int task, int ignore_count,
12221 const struct breakpoint_ops *o,
12222 int from_tty, int enabled,
12223 int internal, unsigned flags)
12224 {
12225 internal_error_pure_virtual_called ();
12226 }
12227
12228 static std::vector<symtab_and_line>
12229 base_breakpoint_decode_location (struct breakpoint *b,
12230 const struct event_location *location,
12231 struct program_space *search_pspace)
12232 {
12233 internal_error_pure_virtual_called ();
12234 }
12235
12236 /* The default 'explains_signal' method. */
12237
12238 static int
12239 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12240 {
12241 return 1;
12242 }
12243
12244 /* The default "after_condition_true" method. */
12245
12246 static void
12247 base_breakpoint_after_condition_true (struct bpstats *bs)
12248 {
12249 /* Nothing to do. */
12250 }
12251
12252 struct breakpoint_ops base_breakpoint_ops =
12253 {
12254 base_breakpoint_allocate_location,
12255 base_breakpoint_re_set,
12256 base_breakpoint_insert_location,
12257 base_breakpoint_remove_location,
12258 base_breakpoint_breakpoint_hit,
12259 base_breakpoint_check_status,
12260 base_breakpoint_resources_needed,
12261 base_breakpoint_works_in_software_mode,
12262 base_breakpoint_print_it,
12263 NULL,
12264 base_breakpoint_print_one_detail,
12265 base_breakpoint_print_mention,
12266 base_breakpoint_print_recreate,
12267 base_breakpoint_create_sals_from_location,
12268 base_breakpoint_create_breakpoints_sal,
12269 base_breakpoint_decode_location,
12270 base_breakpoint_explains_signal,
12271 base_breakpoint_after_condition_true,
12272 };
12273
12274 /* Default breakpoint_ops methods. */
12275
12276 static void
12277 bkpt_re_set (struct breakpoint *b)
12278 {
12279 /* FIXME: is this still reachable? */
12280 if (breakpoint_event_location_empty_p (b))
12281 {
12282 /* Anything without a location can't be re-set. */
12283 delete_breakpoint (b);
12284 return;
12285 }
12286
12287 breakpoint_re_set_default (b);
12288 }
12289
12290 static int
12291 bkpt_insert_location (struct bp_location *bl)
12292 {
12293 CORE_ADDR addr = bl->target_info.reqstd_address;
12294
12295 bl->target_info.kind = breakpoint_kind (bl, &addr);
12296 bl->target_info.placed_address = addr;
12297
12298 if (bl->loc_type == bp_loc_hardware_breakpoint)
12299 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12300 else
12301 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12302 }
12303
12304 static int
12305 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12306 {
12307 if (bl->loc_type == bp_loc_hardware_breakpoint)
12308 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12309 else
12310 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12311 }
12312
12313 static int
12314 bkpt_breakpoint_hit (const struct bp_location *bl,
12315 const address_space *aspace, CORE_ADDR bp_addr,
12316 const struct target_waitstatus *ws)
12317 {
12318 if (ws->kind != TARGET_WAITKIND_STOPPED
12319 || ws->value.sig != GDB_SIGNAL_TRAP)
12320 return 0;
12321
12322 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12323 aspace, bp_addr))
12324 return 0;
12325
12326 if (overlay_debugging /* unmapped overlay section */
12327 && section_is_overlay (bl->section)
12328 && !section_is_mapped (bl->section))
12329 return 0;
12330
12331 return 1;
12332 }
12333
12334 static int
12335 dprintf_breakpoint_hit (const struct bp_location *bl,
12336 const address_space *aspace, CORE_ADDR bp_addr,
12337 const struct target_waitstatus *ws)
12338 {
12339 if (dprintf_style == dprintf_style_agent
12340 && target_can_run_breakpoint_commands ())
12341 {
12342 /* An agent-style dprintf never causes a stop. If we see a trap
12343 for this address it must be for a breakpoint that happens to
12344 be set at the same address. */
12345 return 0;
12346 }
12347
12348 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12349 }
12350
12351 static int
12352 bkpt_resources_needed (const struct bp_location *bl)
12353 {
12354 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12355
12356 return 1;
12357 }
12358
12359 static enum print_stop_action
12360 bkpt_print_it (bpstat bs)
12361 {
12362 struct breakpoint *b;
12363 const struct bp_location *bl;
12364 int bp_temp;
12365 struct ui_out *uiout = current_uiout;
12366
12367 gdb_assert (bs->bp_location_at != NULL);
12368
12369 bl = bs->bp_location_at;
12370 b = bs->breakpoint_at;
12371
12372 bp_temp = b->disposition == disp_del;
12373 if (bl->address != bl->requested_address)
12374 breakpoint_adjustment_warning (bl->requested_address,
12375 bl->address,
12376 b->number, 1);
12377 annotate_breakpoint (b->number);
12378 maybe_print_thread_hit_breakpoint (uiout);
12379
12380 if (uiout->is_mi_like_p ())
12381 {
12382 uiout->field_string ("reason",
12383 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12384 uiout->field_string ("disp", bpdisp_text (b->disposition));
12385 }
12386 if (bp_temp)
12387 uiout->message ("Temporary breakpoint %pF, ",
12388 signed_field ("bkptno", b->number));
12389 else
12390 uiout->message ("Breakpoint %pF, ",
12391 signed_field ("bkptno", b->number));
12392
12393 return PRINT_SRC_AND_LOC;
12394 }
12395
12396 static void
12397 bkpt_print_mention (struct breakpoint *b)
12398 {
12399 if (current_uiout->is_mi_like_p ())
12400 return;
12401
12402 switch (b->type)
12403 {
12404 case bp_breakpoint:
12405 case bp_gnu_ifunc_resolver:
12406 if (b->disposition == disp_del)
12407 printf_filtered (_("Temporary breakpoint"));
12408 else
12409 printf_filtered (_("Breakpoint"));
12410 printf_filtered (_(" %d"), b->number);
12411 if (b->type == bp_gnu_ifunc_resolver)
12412 printf_filtered (_(" at gnu-indirect-function resolver"));
12413 break;
12414 case bp_hardware_breakpoint:
12415 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12416 break;
12417 case bp_dprintf:
12418 printf_filtered (_("Dprintf %d"), b->number);
12419 break;
12420 }
12421
12422 say_where (b);
12423 }
12424
12425 static void
12426 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12427 {
12428 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12429 fprintf_unfiltered (fp, "tbreak");
12430 else if (tp->type == bp_breakpoint)
12431 fprintf_unfiltered (fp, "break");
12432 else if (tp->type == bp_hardware_breakpoint
12433 && tp->disposition == disp_del)
12434 fprintf_unfiltered (fp, "thbreak");
12435 else if (tp->type == bp_hardware_breakpoint)
12436 fprintf_unfiltered (fp, "hbreak");
12437 else
12438 internal_error (__FILE__, __LINE__,
12439 _("unhandled breakpoint type %d"), (int) tp->type);
12440
12441 fprintf_unfiltered (fp, " %s",
12442 event_location_to_string (tp->location.get ()));
12443
12444 /* Print out extra_string if this breakpoint is pending. It might
12445 contain, for example, conditions that were set by the user. */
12446 if (tp->loc == NULL && tp->extra_string != NULL)
12447 fprintf_unfiltered (fp, " %s", tp->extra_string);
12448
12449 print_recreate_thread (tp, fp);
12450 }
12451
12452 static void
12453 bkpt_create_sals_from_location (const struct event_location *location,
12454 struct linespec_result *canonical,
12455 enum bptype type_wanted)
12456 {
12457 create_sals_from_location_default (location, canonical, type_wanted);
12458 }
12459
12460 static void
12461 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12462 struct linespec_result *canonical,
12463 gdb::unique_xmalloc_ptr<char> cond_string,
12464 gdb::unique_xmalloc_ptr<char> extra_string,
12465 enum bptype type_wanted,
12466 enum bpdisp disposition,
12467 int thread,
12468 int task, int ignore_count,
12469 const struct breakpoint_ops *ops,
12470 int from_tty, int enabled,
12471 int internal, unsigned flags)
12472 {
12473 create_breakpoints_sal_default (gdbarch, canonical,
12474 std::move (cond_string),
12475 std::move (extra_string),
12476 type_wanted,
12477 disposition, thread, task,
12478 ignore_count, ops, from_tty,
12479 enabled, internal, flags);
12480 }
12481
12482 static std::vector<symtab_and_line>
12483 bkpt_decode_location (struct breakpoint *b,
12484 const struct event_location *location,
12485 struct program_space *search_pspace)
12486 {
12487 return decode_location_default (b, location, search_pspace);
12488 }
12489
12490 /* Virtual table for internal breakpoints. */
12491
12492 static void
12493 internal_bkpt_re_set (struct breakpoint *b)
12494 {
12495 switch (b->type)
12496 {
12497 /* Delete overlay event and longjmp master breakpoints; they
12498 will be reset later by breakpoint_re_set. */
12499 case bp_overlay_event:
12500 case bp_longjmp_master:
12501 case bp_std_terminate_master:
12502 case bp_exception_master:
12503 delete_breakpoint (b);
12504 break;
12505
12506 /* This breakpoint is special, it's set up when the inferior
12507 starts and we really don't want to touch it. */
12508 case bp_shlib_event:
12509
12510 /* Like bp_shlib_event, this breakpoint type is special. Once
12511 it is set up, we do not want to touch it. */
12512 case bp_thread_event:
12513 break;
12514 }
12515 }
12516
12517 static void
12518 internal_bkpt_check_status (bpstat bs)
12519 {
12520 if (bs->breakpoint_at->type == bp_shlib_event)
12521 {
12522 /* If requested, stop when the dynamic linker notifies GDB of
12523 events. This allows the user to get control and place
12524 breakpoints in initializer routines for dynamically loaded
12525 objects (among other things). */
12526 bs->stop = stop_on_solib_events;
12527 bs->print = stop_on_solib_events;
12528 }
12529 else
12530 bs->stop = 0;
12531 }
12532
12533 static enum print_stop_action
12534 internal_bkpt_print_it (bpstat bs)
12535 {
12536 struct breakpoint *b;
12537
12538 b = bs->breakpoint_at;
12539
12540 switch (b->type)
12541 {
12542 case bp_shlib_event:
12543 /* Did we stop because the user set the stop_on_solib_events
12544 variable? (If so, we report this as a generic, "Stopped due
12545 to shlib event" message.) */
12546 print_solib_event (0);
12547 break;
12548
12549 case bp_thread_event:
12550 /* Not sure how we will get here.
12551 GDB should not stop for these breakpoints. */
12552 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12553 break;
12554
12555 case bp_overlay_event:
12556 /* By analogy with the thread event, GDB should not stop for these. */
12557 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12558 break;
12559
12560 case bp_longjmp_master:
12561 /* These should never be enabled. */
12562 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12563 break;
12564
12565 case bp_std_terminate_master:
12566 /* These should never be enabled. */
12567 printf_filtered (_("std::terminate Master Breakpoint: "
12568 "gdb should not stop!\n"));
12569 break;
12570
12571 case bp_exception_master:
12572 /* These should never be enabled. */
12573 printf_filtered (_("Exception Master Breakpoint: "
12574 "gdb should not stop!\n"));
12575 break;
12576 }
12577
12578 return PRINT_NOTHING;
12579 }
12580
12581 static void
12582 internal_bkpt_print_mention (struct breakpoint *b)
12583 {
12584 /* Nothing to mention. These breakpoints are internal. */
12585 }
12586
12587 /* Virtual table for momentary breakpoints */
12588
12589 static void
12590 momentary_bkpt_re_set (struct breakpoint *b)
12591 {
12592 /* Keep temporary breakpoints, which can be encountered when we step
12593 over a dlopen call and solib_add is resetting the breakpoints.
12594 Otherwise these should have been blown away via the cleanup chain
12595 or by breakpoint_init_inferior when we rerun the executable. */
12596 }
12597
12598 static void
12599 momentary_bkpt_check_status (bpstat bs)
12600 {
12601 /* Nothing. The point of these breakpoints is causing a stop. */
12602 }
12603
12604 static enum print_stop_action
12605 momentary_bkpt_print_it (bpstat bs)
12606 {
12607 return PRINT_UNKNOWN;
12608 }
12609
12610 static void
12611 momentary_bkpt_print_mention (struct breakpoint *b)
12612 {
12613 /* Nothing to mention. These breakpoints are internal. */
12614 }
12615
12616 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12617
12618 It gets cleared already on the removal of the first one of such placed
12619 breakpoints. This is OK as they get all removed altogether. */
12620
12621 longjmp_breakpoint::~longjmp_breakpoint ()
12622 {
12623 thread_info *tp = find_thread_global_id (this->thread);
12624
12625 if (tp != NULL)
12626 tp->initiating_frame = null_frame_id;
12627 }
12628
12629 /* Specific methods for probe breakpoints. */
12630
12631 static int
12632 bkpt_probe_insert_location (struct bp_location *bl)
12633 {
12634 int v = bkpt_insert_location (bl);
12635
12636 if (v == 0)
12637 {
12638 /* The insertion was successful, now let's set the probe's semaphore
12639 if needed. */
12640 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12641 }
12642
12643 return v;
12644 }
12645
12646 static int
12647 bkpt_probe_remove_location (struct bp_location *bl,
12648 enum remove_bp_reason reason)
12649 {
12650 /* Let's clear the semaphore before removing the location. */
12651 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12652
12653 return bkpt_remove_location (bl, reason);
12654 }
12655
12656 static void
12657 bkpt_probe_create_sals_from_location (const struct event_location *location,
12658 struct linespec_result *canonical,
12659 enum bptype type_wanted)
12660 {
12661 struct linespec_sals lsal;
12662
12663 lsal.sals = parse_probes (location, NULL, canonical);
12664 lsal.canonical
12665 = xstrdup (event_location_to_string (canonical->location.get ()));
12666 canonical->lsals.push_back (std::move (lsal));
12667 }
12668
12669 static std::vector<symtab_and_line>
12670 bkpt_probe_decode_location (struct breakpoint *b,
12671 const struct event_location *location,
12672 struct program_space *search_pspace)
12673 {
12674 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12675 if (sals.empty ())
12676 error (_("probe not found"));
12677 return sals;
12678 }
12679
12680 /* The breakpoint_ops structure to be used in tracepoints. */
12681
12682 static void
12683 tracepoint_re_set (struct breakpoint *b)
12684 {
12685 breakpoint_re_set_default (b);
12686 }
12687
12688 static int
12689 tracepoint_breakpoint_hit (const struct bp_location *bl,
12690 const address_space *aspace, CORE_ADDR bp_addr,
12691 const struct target_waitstatus *ws)
12692 {
12693 /* By definition, the inferior does not report stops at
12694 tracepoints. */
12695 return 0;
12696 }
12697
12698 static void
12699 tracepoint_print_one_detail (const struct breakpoint *self,
12700 struct ui_out *uiout)
12701 {
12702 struct tracepoint *tp = (struct tracepoint *) self;
12703 if (!tp->static_trace_marker_id.empty ())
12704 {
12705 gdb_assert (self->type == bp_static_tracepoint);
12706
12707 uiout->message ("\tmarker id is %pF\n",
12708 string_field ("static-tracepoint-marker-string-id",
12709 tp->static_trace_marker_id.c_str ()));
12710 }
12711 }
12712
12713 static void
12714 tracepoint_print_mention (struct breakpoint *b)
12715 {
12716 if (current_uiout->is_mi_like_p ())
12717 return;
12718
12719 switch (b->type)
12720 {
12721 case bp_tracepoint:
12722 printf_filtered (_("Tracepoint"));
12723 printf_filtered (_(" %d"), b->number);
12724 break;
12725 case bp_fast_tracepoint:
12726 printf_filtered (_("Fast tracepoint"));
12727 printf_filtered (_(" %d"), b->number);
12728 break;
12729 case bp_static_tracepoint:
12730 printf_filtered (_("Static tracepoint"));
12731 printf_filtered (_(" %d"), b->number);
12732 break;
12733 default:
12734 internal_error (__FILE__, __LINE__,
12735 _("unhandled tracepoint type %d"), (int) b->type);
12736 }
12737
12738 say_where (b);
12739 }
12740
12741 static void
12742 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12743 {
12744 struct tracepoint *tp = (struct tracepoint *) self;
12745
12746 if (self->type == bp_fast_tracepoint)
12747 fprintf_unfiltered (fp, "ftrace");
12748 else if (self->type == bp_static_tracepoint)
12749 fprintf_unfiltered (fp, "strace");
12750 else if (self->type == bp_tracepoint)
12751 fprintf_unfiltered (fp, "trace");
12752 else
12753 internal_error (__FILE__, __LINE__,
12754 _("unhandled tracepoint type %d"), (int) self->type);
12755
12756 fprintf_unfiltered (fp, " %s",
12757 event_location_to_string (self->location.get ()));
12758 print_recreate_thread (self, fp);
12759
12760 if (tp->pass_count)
12761 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12762 }
12763
12764 static void
12765 tracepoint_create_sals_from_location (const struct event_location *location,
12766 struct linespec_result *canonical,
12767 enum bptype type_wanted)
12768 {
12769 create_sals_from_location_default (location, canonical, type_wanted);
12770 }
12771
12772 static void
12773 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12774 struct linespec_result *canonical,
12775 gdb::unique_xmalloc_ptr<char> cond_string,
12776 gdb::unique_xmalloc_ptr<char> extra_string,
12777 enum bptype type_wanted,
12778 enum bpdisp disposition,
12779 int thread,
12780 int task, int ignore_count,
12781 const struct breakpoint_ops *ops,
12782 int from_tty, int enabled,
12783 int internal, unsigned flags)
12784 {
12785 create_breakpoints_sal_default (gdbarch, canonical,
12786 std::move (cond_string),
12787 std::move (extra_string),
12788 type_wanted,
12789 disposition, thread, task,
12790 ignore_count, ops, from_tty,
12791 enabled, internal, flags);
12792 }
12793
12794 static std::vector<symtab_and_line>
12795 tracepoint_decode_location (struct breakpoint *b,
12796 const struct event_location *location,
12797 struct program_space *search_pspace)
12798 {
12799 return decode_location_default (b, location, search_pspace);
12800 }
12801
12802 struct breakpoint_ops tracepoint_breakpoint_ops;
12803
12804 /* Virtual table for tracepoints on static probes. */
12805
12806 static void
12807 tracepoint_probe_create_sals_from_location
12808 (const struct event_location *location,
12809 struct linespec_result *canonical,
12810 enum bptype type_wanted)
12811 {
12812 /* We use the same method for breakpoint on probes. */
12813 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12814 }
12815
12816 static std::vector<symtab_and_line>
12817 tracepoint_probe_decode_location (struct breakpoint *b,
12818 const struct event_location *location,
12819 struct program_space *search_pspace)
12820 {
12821 /* We use the same method for breakpoint on probes. */
12822 return bkpt_probe_decode_location (b, location, search_pspace);
12823 }
12824
12825 /* Dprintf breakpoint_ops methods. */
12826
12827 static void
12828 dprintf_re_set (struct breakpoint *b)
12829 {
12830 breakpoint_re_set_default (b);
12831
12832 /* extra_string should never be non-NULL for dprintf. */
12833 gdb_assert (b->extra_string != NULL);
12834
12835 /* 1 - connect to target 1, that can run breakpoint commands.
12836 2 - create a dprintf, which resolves fine.
12837 3 - disconnect from target 1
12838 4 - connect to target 2, that can NOT run breakpoint commands.
12839
12840 After steps #3/#4, you'll want the dprintf command list to
12841 be updated, because target 1 and 2 may well return different
12842 answers for target_can_run_breakpoint_commands().
12843 Given absence of finer grained resetting, we get to do
12844 it all the time. */
12845 if (b->extra_string != NULL)
12846 update_dprintf_command_list (b);
12847 }
12848
12849 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12850
12851 static void
12852 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12853 {
12854 fprintf_unfiltered (fp, "dprintf %s,%s",
12855 event_location_to_string (tp->location.get ()),
12856 tp->extra_string);
12857 print_recreate_thread (tp, fp);
12858 }
12859
12860 /* Implement the "after_condition_true" breakpoint_ops method for
12861 dprintf.
12862
12863 dprintf's are implemented with regular commands in their command
12864 list, but we run the commands here instead of before presenting the
12865 stop to the user, as dprintf's don't actually cause a stop. This
12866 also makes it so that the commands of multiple dprintfs at the same
12867 address are all handled. */
12868
12869 static void
12870 dprintf_after_condition_true (struct bpstats *bs)
12871 {
12872 struct bpstats tmp_bs;
12873 struct bpstats *tmp_bs_p = &tmp_bs;
12874
12875 /* dprintf's never cause a stop. This wasn't set in the
12876 check_status hook instead because that would make the dprintf's
12877 condition not be evaluated. */
12878 bs->stop = 0;
12879
12880 /* Run the command list here. Take ownership of it instead of
12881 copying. We never want these commands to run later in
12882 bpstat_do_actions, if a breakpoint that causes a stop happens to
12883 be set at same address as this dprintf, or even if running the
12884 commands here throws. */
12885 tmp_bs.commands = bs->commands;
12886 bs->commands = NULL;
12887
12888 bpstat_do_actions_1 (&tmp_bs_p);
12889
12890 /* 'tmp_bs.commands' will usually be NULL by now, but
12891 bpstat_do_actions_1 may return early without processing the whole
12892 list. */
12893 }
12894
12895 /* The breakpoint_ops structure to be used on static tracepoints with
12896 markers (`-m'). */
12897
12898 static void
12899 strace_marker_create_sals_from_location (const struct event_location *location,
12900 struct linespec_result *canonical,
12901 enum bptype type_wanted)
12902 {
12903 struct linespec_sals lsal;
12904 const char *arg_start, *arg;
12905
12906 arg = arg_start = get_linespec_location (location)->spec_string;
12907 lsal.sals = decode_static_tracepoint_spec (&arg);
12908
12909 std::string str (arg_start, arg - arg_start);
12910 const char *ptr = str.c_str ();
12911 canonical->location
12912 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12913
12914 lsal.canonical
12915 = xstrdup (event_location_to_string (canonical->location.get ()));
12916 canonical->lsals.push_back (std::move (lsal));
12917 }
12918
12919 static void
12920 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12921 struct linespec_result *canonical,
12922 gdb::unique_xmalloc_ptr<char> cond_string,
12923 gdb::unique_xmalloc_ptr<char> extra_string,
12924 enum bptype type_wanted,
12925 enum bpdisp disposition,
12926 int thread,
12927 int task, int ignore_count,
12928 const struct breakpoint_ops *ops,
12929 int from_tty, int enabled,
12930 int internal, unsigned flags)
12931 {
12932 const linespec_sals &lsal = canonical->lsals[0];
12933
12934 /* If the user is creating a static tracepoint by marker id
12935 (strace -m MARKER_ID), then store the sals index, so that
12936 breakpoint_re_set can try to match up which of the newly
12937 found markers corresponds to this one, and, don't try to
12938 expand multiple locations for each sal, given than SALS
12939 already should contain all sals for MARKER_ID. */
12940
12941 for (size_t i = 0; i < lsal.sals.size (); i++)
12942 {
12943 event_location_up location
12944 = copy_event_location (canonical->location.get ());
12945
12946 std::unique_ptr<tracepoint> tp (new tracepoint ());
12947 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
12948 std::move (location), NULL,
12949 std::move (cond_string),
12950 std::move (extra_string),
12951 type_wanted, disposition,
12952 thread, task, ignore_count, ops,
12953 from_tty, enabled, internal, flags,
12954 canonical->special_display);
12955 /* Given that its possible to have multiple markers with
12956 the same string id, if the user is creating a static
12957 tracepoint by marker id ("strace -m MARKER_ID"), then
12958 store the sals index, so that breakpoint_re_set can
12959 try to match up which of the newly found markers
12960 corresponds to this one */
12961 tp->static_trace_marker_id_idx = i;
12962
12963 install_breakpoint (internal, std::move (tp), 0);
12964 }
12965 }
12966
12967 static std::vector<symtab_and_line>
12968 strace_marker_decode_location (struct breakpoint *b,
12969 const struct event_location *location,
12970 struct program_space *search_pspace)
12971 {
12972 struct tracepoint *tp = (struct tracepoint *) b;
12973 const char *s = get_linespec_location (location)->spec_string;
12974
12975 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12976 if (sals.size () > tp->static_trace_marker_id_idx)
12977 {
12978 sals[0] = sals[tp->static_trace_marker_id_idx];
12979 sals.resize (1);
12980 return sals;
12981 }
12982 else
12983 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
12984 }
12985
12986 static struct breakpoint_ops strace_marker_breakpoint_ops;
12987
12988 static int
12989 strace_marker_p (struct breakpoint *b)
12990 {
12991 return b->ops == &strace_marker_breakpoint_ops;
12992 }
12993
12994 /* Delete a breakpoint and clean up all traces of it in the data
12995 structures. */
12996
12997 void
12998 delete_breakpoint (struct breakpoint *bpt)
12999 {
13000 struct breakpoint *b;
13001
13002 gdb_assert (bpt != NULL);
13003
13004 /* Has this bp already been deleted? This can happen because
13005 multiple lists can hold pointers to bp's. bpstat lists are
13006 especial culprits.
13007
13008 One example of this happening is a watchpoint's scope bp. When
13009 the scope bp triggers, we notice that the watchpoint is out of
13010 scope, and delete it. We also delete its scope bp. But the
13011 scope bp is marked "auto-deleting", and is already on a bpstat.
13012 That bpstat is then checked for auto-deleting bp's, which are
13013 deleted.
13014
13015 A real solution to this problem might involve reference counts in
13016 bp's, and/or giving them pointers back to their referencing
13017 bpstat's, and teaching delete_breakpoint to only free a bp's
13018 storage when no more references were extent. A cheaper bandaid
13019 was chosen. */
13020 if (bpt->type == bp_none)
13021 return;
13022
13023 /* At least avoid this stale reference until the reference counting
13024 of breakpoints gets resolved. */
13025 if (bpt->related_breakpoint != bpt)
13026 {
13027 struct breakpoint *related;
13028 struct watchpoint *w;
13029
13030 if (bpt->type == bp_watchpoint_scope)
13031 w = (struct watchpoint *) bpt->related_breakpoint;
13032 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13033 w = (struct watchpoint *) bpt;
13034 else
13035 w = NULL;
13036 if (w != NULL)
13037 watchpoint_del_at_next_stop (w);
13038
13039 /* Unlink bpt from the bpt->related_breakpoint ring. */
13040 for (related = bpt; related->related_breakpoint != bpt;
13041 related = related->related_breakpoint);
13042 related->related_breakpoint = bpt->related_breakpoint;
13043 bpt->related_breakpoint = bpt;
13044 }
13045
13046 /* watch_command_1 creates a watchpoint but only sets its number if
13047 update_watchpoint succeeds in creating its bp_locations. If there's
13048 a problem in that process, we'll be asked to delete the half-created
13049 watchpoint. In that case, don't announce the deletion. */
13050 if (bpt->number)
13051 gdb::observers::breakpoint_deleted.notify (bpt);
13052
13053 if (breakpoint_chain == bpt)
13054 breakpoint_chain = bpt->next;
13055
13056 ALL_BREAKPOINTS (b)
13057 if (b->next == bpt)
13058 {
13059 b->next = bpt->next;
13060 break;
13061 }
13062
13063 /* Be sure no bpstat's are pointing at the breakpoint after it's
13064 been freed. */
13065 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13066 in all threads for now. Note that we cannot just remove bpstats
13067 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13068 commands are associated with the bpstat; if we remove it here,
13069 then the later call to bpstat_do_actions (&stop_bpstat); in
13070 event-top.c won't do anything, and temporary breakpoints with
13071 commands won't work. */
13072
13073 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13074
13075 /* Now that breakpoint is removed from breakpoint list, update the
13076 global location list. This will remove locations that used to
13077 belong to this breakpoint. Do this before freeing the breakpoint
13078 itself, since remove_breakpoint looks at location's owner. It
13079 might be better design to have location completely
13080 self-contained, but it's not the case now. */
13081 update_global_location_list (UGLL_DONT_INSERT);
13082
13083 /* On the chance that someone will soon try again to delete this
13084 same bp, we mark it as deleted before freeing its storage. */
13085 bpt->type = bp_none;
13086 delete bpt;
13087 }
13088
13089 /* Iterator function to call a user-provided callback function once
13090 for each of B and its related breakpoints. */
13091
13092 static void
13093 iterate_over_related_breakpoints (struct breakpoint *b,
13094 gdb::function_view<void (breakpoint *)> function)
13095 {
13096 struct breakpoint *related;
13097
13098 related = b;
13099 do
13100 {
13101 struct breakpoint *next;
13102
13103 /* FUNCTION may delete RELATED. */
13104 next = related->related_breakpoint;
13105
13106 if (next == related)
13107 {
13108 /* RELATED is the last ring entry. */
13109 function (related);
13110
13111 /* FUNCTION may have deleted it, so we'd never reach back to
13112 B. There's nothing left to do anyway, so just break
13113 out. */
13114 break;
13115 }
13116 else
13117 function (related);
13118
13119 related = next;
13120 }
13121 while (related != b);
13122 }
13123
13124 static void
13125 delete_command (const char *arg, int from_tty)
13126 {
13127 struct breakpoint *b, *b_tmp;
13128
13129 dont_repeat ();
13130
13131 if (arg == 0)
13132 {
13133 int breaks_to_delete = 0;
13134
13135 /* Delete all breakpoints if no argument. Do not delete
13136 internal breakpoints, these have to be deleted with an
13137 explicit breakpoint number argument. */
13138 ALL_BREAKPOINTS (b)
13139 if (user_breakpoint_p (b))
13140 {
13141 breaks_to_delete = 1;
13142 break;
13143 }
13144
13145 /* Ask user only if there are some breakpoints to delete. */
13146 if (!from_tty
13147 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13148 {
13149 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13150 if (user_breakpoint_p (b))
13151 delete_breakpoint (b);
13152 }
13153 }
13154 else
13155 map_breakpoint_numbers
13156 (arg, [&] (breakpoint *br)
13157 {
13158 iterate_over_related_breakpoints (br, delete_breakpoint);
13159 });
13160 }
13161
13162 /* Return true if all locations of B bound to PSPACE are pending. If
13163 PSPACE is NULL, all locations of all program spaces are
13164 considered. */
13165
13166 static int
13167 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13168 {
13169 struct bp_location *loc;
13170
13171 for (loc = b->loc; loc != NULL; loc = loc->next)
13172 if ((pspace == NULL
13173 || loc->pspace == pspace)
13174 && !loc->shlib_disabled
13175 && !loc->pspace->executing_startup)
13176 return 0;
13177 return 1;
13178 }
13179
13180 /* Subroutine of update_breakpoint_locations to simplify it.
13181 Return non-zero if multiple fns in list LOC have the same name.
13182 Null names are ignored. */
13183
13184 static int
13185 ambiguous_names_p (struct bp_location *loc)
13186 {
13187 struct bp_location *l;
13188 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13189 xcalloc, xfree);
13190
13191 for (l = loc; l != NULL; l = l->next)
13192 {
13193 const char **slot;
13194 const char *name = l->function_name;
13195
13196 /* Allow for some names to be NULL, ignore them. */
13197 if (name == NULL)
13198 continue;
13199
13200 slot = (const char **) htab_find_slot (htab, (const void *) name,
13201 INSERT);
13202 /* NOTE: We can assume slot != NULL here because xcalloc never
13203 returns NULL. */
13204 if (*slot != NULL)
13205 {
13206 htab_delete (htab);
13207 return 1;
13208 }
13209 *slot = name;
13210 }
13211
13212 htab_delete (htab);
13213 return 0;
13214 }
13215
13216 /* When symbols change, it probably means the sources changed as well,
13217 and it might mean the static tracepoint markers are no longer at
13218 the same address or line numbers they used to be at last we
13219 checked. Losing your static tracepoints whenever you rebuild is
13220 undesirable. This function tries to resync/rematch gdb static
13221 tracepoints with the markers on the target, for static tracepoints
13222 that have not been set by marker id. Static tracepoint that have
13223 been set by marker id are reset by marker id in breakpoint_re_set.
13224 The heuristic is:
13225
13226 1) For a tracepoint set at a specific address, look for a marker at
13227 the old PC. If one is found there, assume to be the same marker.
13228 If the name / string id of the marker found is different from the
13229 previous known name, assume that means the user renamed the marker
13230 in the sources, and output a warning.
13231
13232 2) For a tracepoint set at a given line number, look for a marker
13233 at the new address of the old line number. If one is found there,
13234 assume to be the same marker. If the name / string id of the
13235 marker found is different from the previous known name, assume that
13236 means the user renamed the marker in the sources, and output a
13237 warning.
13238
13239 3) If a marker is no longer found at the same address or line, it
13240 may mean the marker no longer exists. But it may also just mean
13241 the code changed a bit. Maybe the user added a few lines of code
13242 that made the marker move up or down (in line number terms). Ask
13243 the target for info about the marker with the string id as we knew
13244 it. If found, update line number and address in the matching
13245 static tracepoint. This will get confused if there's more than one
13246 marker with the same ID (possible in UST, although unadvised
13247 precisely because it confuses tools). */
13248
13249 static struct symtab_and_line
13250 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13251 {
13252 struct tracepoint *tp = (struct tracepoint *) b;
13253 struct static_tracepoint_marker marker;
13254 CORE_ADDR pc;
13255
13256 pc = sal.pc;
13257 if (sal.line)
13258 find_line_pc (sal.symtab, sal.line, &pc);
13259
13260 if (target_static_tracepoint_marker_at (pc, &marker))
13261 {
13262 if (tp->static_trace_marker_id != marker.str_id)
13263 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13264 b->number, tp->static_trace_marker_id.c_str (),
13265 marker.str_id.c_str ());
13266
13267 tp->static_trace_marker_id = std::move (marker.str_id);
13268
13269 return sal;
13270 }
13271
13272 /* Old marker wasn't found on target at lineno. Try looking it up
13273 by string ID. */
13274 if (!sal.explicit_pc
13275 && sal.line != 0
13276 && sal.symtab != NULL
13277 && !tp->static_trace_marker_id.empty ())
13278 {
13279 std::vector<static_tracepoint_marker> markers
13280 = target_static_tracepoint_markers_by_strid
13281 (tp->static_trace_marker_id.c_str ());
13282
13283 if (!markers.empty ())
13284 {
13285 struct symbol *sym;
13286 struct static_tracepoint_marker *tpmarker;
13287 struct ui_out *uiout = current_uiout;
13288 struct explicit_location explicit_loc;
13289
13290 tpmarker = &markers[0];
13291
13292 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13293
13294 warning (_("marker for static tracepoint %d (%s) not "
13295 "found at previous line number"),
13296 b->number, tp->static_trace_marker_id.c_str ());
13297
13298 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13299 sym = find_pc_sect_function (tpmarker->address, NULL);
13300 uiout->text ("Now in ");
13301 if (sym)
13302 {
13303 uiout->field_string ("func", sym->print_name (),
13304 function_name_style.style ());
13305 uiout->text (" at ");
13306 }
13307 uiout->field_string ("file",
13308 symtab_to_filename_for_display (sal2.symtab),
13309 file_name_style.style ());
13310 uiout->text (":");
13311
13312 if (uiout->is_mi_like_p ())
13313 {
13314 const char *fullname = symtab_to_fullname (sal2.symtab);
13315
13316 uiout->field_string ("fullname", fullname);
13317 }
13318
13319 uiout->field_signed ("line", sal2.line);
13320 uiout->text ("\n");
13321
13322 b->loc->line_number = sal2.line;
13323 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13324
13325 b->location.reset (NULL);
13326 initialize_explicit_location (&explicit_loc);
13327 explicit_loc.source_filename
13328 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13329 explicit_loc.line_offset.offset = b->loc->line_number;
13330 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13331 b->location = new_explicit_location (&explicit_loc);
13332
13333 /* Might be nice to check if function changed, and warn if
13334 so. */
13335 }
13336 }
13337 return sal;
13338 }
13339
13340 /* Returns 1 iff locations A and B are sufficiently same that
13341 we don't need to report breakpoint as changed. */
13342
13343 static int
13344 locations_are_equal (struct bp_location *a, struct bp_location *b)
13345 {
13346 while (a && b)
13347 {
13348 if (a->address != b->address)
13349 return 0;
13350
13351 if (a->shlib_disabled != b->shlib_disabled)
13352 return 0;
13353
13354 if (a->enabled != b->enabled)
13355 return 0;
13356
13357 a = a->next;
13358 b = b->next;
13359 }
13360
13361 if ((a == NULL) != (b == NULL))
13362 return 0;
13363
13364 return 1;
13365 }
13366
13367 /* Split all locations of B that are bound to PSPACE out of B's
13368 location list to a separate list and return that list's head. If
13369 PSPACE is NULL, hoist out all locations of B. */
13370
13371 static struct bp_location *
13372 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13373 {
13374 struct bp_location head;
13375 struct bp_location *i = b->loc;
13376 struct bp_location **i_link = &b->loc;
13377 struct bp_location *hoisted = &head;
13378
13379 if (pspace == NULL)
13380 {
13381 i = b->loc;
13382 b->loc = NULL;
13383 return i;
13384 }
13385
13386 head.next = NULL;
13387
13388 while (i != NULL)
13389 {
13390 if (i->pspace == pspace)
13391 {
13392 *i_link = i->next;
13393 i->next = NULL;
13394 hoisted->next = i;
13395 hoisted = i;
13396 }
13397 else
13398 i_link = &i->next;
13399 i = *i_link;
13400 }
13401
13402 return head.next;
13403 }
13404
13405 /* Create new breakpoint locations for B (a hardware or software
13406 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13407 zero, then B is a ranged breakpoint. Only recreates locations for
13408 FILTER_PSPACE. Locations of other program spaces are left
13409 untouched. */
13410
13411 void
13412 update_breakpoint_locations (struct breakpoint *b,
13413 struct program_space *filter_pspace,
13414 gdb::array_view<const symtab_and_line> sals,
13415 gdb::array_view<const symtab_and_line> sals_end)
13416 {
13417 struct bp_location *existing_locations;
13418
13419 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13420 {
13421 /* Ranged breakpoints have only one start location and one end
13422 location. */
13423 b->enable_state = bp_disabled;
13424 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13425 "multiple locations found\n"),
13426 b->number);
13427 return;
13428 }
13429
13430 /* If there's no new locations, and all existing locations are
13431 pending, don't do anything. This optimizes the common case where
13432 all locations are in the same shared library, that was unloaded.
13433 We'd like to retain the location, so that when the library is
13434 loaded again, we don't loose the enabled/disabled status of the
13435 individual locations. */
13436 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13437 return;
13438
13439 existing_locations = hoist_existing_locations (b, filter_pspace);
13440
13441 for (const auto &sal : sals)
13442 {
13443 struct bp_location *new_loc;
13444
13445 switch_to_program_space_and_thread (sal.pspace);
13446
13447 new_loc = add_location_to_breakpoint (b, &sal);
13448
13449 /* Reparse conditions, they might contain references to the
13450 old symtab. */
13451 if (b->cond_string != NULL)
13452 {
13453 const char *s;
13454
13455 s = b->cond_string;
13456 try
13457 {
13458 new_loc->cond = parse_exp_1 (&s, sal.pc,
13459 block_for_pc (sal.pc),
13460 0);
13461 }
13462 catch (const gdb_exception_error &e)
13463 {
13464 warning (_("failed to reevaluate condition "
13465 "for breakpoint %d: %s"),
13466 b->number, e.what ());
13467 new_loc->enabled = 0;
13468 }
13469 }
13470
13471 if (!sals_end.empty ())
13472 {
13473 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13474
13475 new_loc->length = end - sals[0].pc + 1;
13476 }
13477 }
13478
13479 /* If possible, carry over 'disable' status from existing
13480 breakpoints. */
13481 {
13482 struct bp_location *e = existing_locations;
13483 /* If there are multiple breakpoints with the same function name,
13484 e.g. for inline functions, comparing function names won't work.
13485 Instead compare pc addresses; this is just a heuristic as things
13486 may have moved, but in practice it gives the correct answer
13487 often enough until a better solution is found. */
13488 int have_ambiguous_names = ambiguous_names_p (b->loc);
13489
13490 for (; e; e = e->next)
13491 {
13492 if (!e->enabled && e->function_name)
13493 {
13494 struct bp_location *l = b->loc;
13495 if (have_ambiguous_names)
13496 {
13497 for (; l; l = l->next)
13498 if (breakpoint_locations_match (e, l))
13499 {
13500 l->enabled = 0;
13501 break;
13502 }
13503 }
13504 else
13505 {
13506 for (; l; l = l->next)
13507 if (l->function_name
13508 && strcmp (e->function_name, l->function_name) == 0)
13509 {
13510 l->enabled = 0;
13511 break;
13512 }
13513 }
13514 }
13515 }
13516 }
13517
13518 if (!locations_are_equal (existing_locations, b->loc))
13519 gdb::observers::breakpoint_modified.notify (b);
13520 }
13521
13522 /* Find the SaL locations corresponding to the given LOCATION.
13523 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13524
13525 static std::vector<symtab_and_line>
13526 location_to_sals (struct breakpoint *b, struct event_location *location,
13527 struct program_space *search_pspace, int *found)
13528 {
13529 struct gdb_exception exception;
13530
13531 gdb_assert (b->ops != NULL);
13532
13533 std::vector<symtab_and_line> sals;
13534
13535 try
13536 {
13537 sals = b->ops->decode_location (b, location, search_pspace);
13538 }
13539 catch (gdb_exception_error &e)
13540 {
13541 int not_found_and_ok = 0;
13542
13543 /* For pending breakpoints, it's expected that parsing will
13544 fail until the right shared library is loaded. User has
13545 already told to create pending breakpoints and don't need
13546 extra messages. If breakpoint is in bp_shlib_disabled
13547 state, then user already saw the message about that
13548 breakpoint being disabled, and don't want to see more
13549 errors. */
13550 if (e.error == NOT_FOUND_ERROR
13551 && (b->condition_not_parsed
13552 || (b->loc != NULL
13553 && search_pspace != NULL
13554 && b->loc->pspace != search_pspace)
13555 || (b->loc && b->loc->shlib_disabled)
13556 || (b->loc && b->loc->pspace->executing_startup)
13557 || b->enable_state == bp_disabled))
13558 not_found_and_ok = 1;
13559
13560 if (!not_found_and_ok)
13561 {
13562 /* We surely don't want to warn about the same breakpoint
13563 10 times. One solution, implemented here, is disable
13564 the breakpoint on error. Another solution would be to
13565 have separate 'warning emitted' flag. Since this
13566 happens only when a binary has changed, I don't know
13567 which approach is better. */
13568 b->enable_state = bp_disabled;
13569 throw;
13570 }
13571
13572 exception = std::move (e);
13573 }
13574
13575 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13576 {
13577 for (auto &sal : sals)
13578 resolve_sal_pc (&sal);
13579 if (b->condition_not_parsed && b->extra_string != NULL)
13580 {
13581 char *cond_string, *extra_string;
13582 int thread, task;
13583
13584 find_condition_and_thread (b->extra_string, sals[0].pc,
13585 &cond_string, &thread, &task,
13586 &extra_string);
13587 gdb_assert (b->cond_string == NULL);
13588 if (cond_string)
13589 b->cond_string = cond_string;
13590 b->thread = thread;
13591 b->task = task;
13592 if (extra_string)
13593 {
13594 xfree (b->extra_string);
13595 b->extra_string = extra_string;
13596 }
13597 b->condition_not_parsed = 0;
13598 }
13599
13600 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13601 sals[0] = update_static_tracepoint (b, sals[0]);
13602
13603 *found = 1;
13604 }
13605 else
13606 *found = 0;
13607
13608 return sals;
13609 }
13610
13611 /* The default re_set method, for typical hardware or software
13612 breakpoints. Reevaluate the breakpoint and recreate its
13613 locations. */
13614
13615 static void
13616 breakpoint_re_set_default (struct breakpoint *b)
13617 {
13618 struct program_space *filter_pspace = current_program_space;
13619 std::vector<symtab_and_line> expanded, expanded_end;
13620
13621 int found;
13622 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13623 filter_pspace, &found);
13624 if (found)
13625 expanded = std::move (sals);
13626
13627 if (b->location_range_end != NULL)
13628 {
13629 std::vector<symtab_and_line> sals_end
13630 = location_to_sals (b, b->location_range_end.get (),
13631 filter_pspace, &found);
13632 if (found)
13633 expanded_end = std::move (sals_end);
13634 }
13635
13636 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13637 }
13638
13639 /* Default method for creating SALs from an address string. It basically
13640 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13641
13642 static void
13643 create_sals_from_location_default (const struct event_location *location,
13644 struct linespec_result *canonical,
13645 enum bptype type_wanted)
13646 {
13647 parse_breakpoint_sals (location, canonical);
13648 }
13649
13650 /* Call create_breakpoints_sal for the given arguments. This is the default
13651 function for the `create_breakpoints_sal' method of
13652 breakpoint_ops. */
13653
13654 static void
13655 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13656 struct linespec_result *canonical,
13657 gdb::unique_xmalloc_ptr<char> cond_string,
13658 gdb::unique_xmalloc_ptr<char> extra_string,
13659 enum bptype type_wanted,
13660 enum bpdisp disposition,
13661 int thread,
13662 int task, int ignore_count,
13663 const struct breakpoint_ops *ops,
13664 int from_tty, int enabled,
13665 int internal, unsigned flags)
13666 {
13667 create_breakpoints_sal (gdbarch, canonical,
13668 std::move (cond_string),
13669 std::move (extra_string),
13670 type_wanted, disposition,
13671 thread, task, ignore_count, ops, from_tty,
13672 enabled, internal, flags);
13673 }
13674
13675 /* Decode the line represented by S by calling decode_line_full. This is the
13676 default function for the `decode_location' method of breakpoint_ops. */
13677
13678 static std::vector<symtab_and_line>
13679 decode_location_default (struct breakpoint *b,
13680 const struct event_location *location,
13681 struct program_space *search_pspace)
13682 {
13683 struct linespec_result canonical;
13684
13685 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13686 NULL, 0, &canonical, multiple_symbols_all,
13687 b->filter.get ());
13688
13689 /* We should get 0 or 1 resulting SALs. */
13690 gdb_assert (canonical.lsals.size () < 2);
13691
13692 if (!canonical.lsals.empty ())
13693 {
13694 const linespec_sals &lsal = canonical.lsals[0];
13695 return std::move (lsal.sals);
13696 }
13697 return {};
13698 }
13699
13700 /* Reset a breakpoint. */
13701
13702 static void
13703 breakpoint_re_set_one (breakpoint *b)
13704 {
13705 input_radix = b->input_radix;
13706 set_language (b->language);
13707
13708 b->ops->re_set (b);
13709 }
13710
13711 /* Re-set breakpoint locations for the current program space.
13712 Locations bound to other program spaces are left untouched. */
13713
13714 void
13715 breakpoint_re_set (void)
13716 {
13717 struct breakpoint *b, *b_tmp;
13718
13719 {
13720 scoped_restore_current_language save_language;
13721 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13722 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13723
13724 /* breakpoint_re_set_one sets the current_language to the language
13725 of the breakpoint it is resetting (see prepare_re_set_context)
13726 before re-evaluating the breakpoint's location. This change can
13727 unfortunately get undone by accident if the language_mode is set
13728 to auto, and we either switch frames, or more likely in this context,
13729 we select the current frame.
13730
13731 We prevent this by temporarily turning the language_mode to
13732 language_mode_manual. We restore it once all breakpoints
13733 have been reset. */
13734 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13735 language_mode = language_mode_manual;
13736
13737 /* Note: we must not try to insert locations until after all
13738 breakpoints have been re-set. Otherwise, e.g., when re-setting
13739 breakpoint 1, we'd insert the locations of breakpoint 2, which
13740 hadn't been re-set yet, and thus may have stale locations. */
13741
13742 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13743 {
13744 try
13745 {
13746 breakpoint_re_set_one (b);
13747 }
13748 catch (const gdb_exception &ex)
13749 {
13750 exception_fprintf (gdb_stderr, ex,
13751 "Error in re-setting breakpoint %d: ",
13752 b->number);
13753 }
13754 }
13755
13756 jit_breakpoint_re_set ();
13757 }
13758
13759 create_overlay_event_breakpoint ();
13760 create_longjmp_master_breakpoint ();
13761 create_std_terminate_master_breakpoint ();
13762 create_exception_master_breakpoint ();
13763
13764 /* Now we can insert. */
13765 update_global_location_list (UGLL_MAY_INSERT);
13766 }
13767 \f
13768 /* Reset the thread number of this breakpoint:
13769
13770 - If the breakpoint is for all threads, leave it as-is.
13771 - Else, reset it to the current thread for inferior_ptid. */
13772 void
13773 breakpoint_re_set_thread (struct breakpoint *b)
13774 {
13775 if (b->thread != -1)
13776 {
13777 b->thread = inferior_thread ()->global_num;
13778
13779 /* We're being called after following a fork. The new fork is
13780 selected as current, and unless this was a vfork will have a
13781 different program space from the original thread. Reset that
13782 as well. */
13783 b->loc->pspace = current_program_space;
13784 }
13785 }
13786
13787 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13788 If from_tty is nonzero, it prints a message to that effect,
13789 which ends with a period (no newline). */
13790
13791 void
13792 set_ignore_count (int bptnum, int count, int from_tty)
13793 {
13794 struct breakpoint *b;
13795
13796 if (count < 0)
13797 count = 0;
13798
13799 ALL_BREAKPOINTS (b)
13800 if (b->number == bptnum)
13801 {
13802 if (is_tracepoint (b))
13803 {
13804 if (from_tty && count != 0)
13805 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13806 bptnum);
13807 return;
13808 }
13809
13810 b->ignore_count = count;
13811 if (from_tty)
13812 {
13813 if (count == 0)
13814 printf_filtered (_("Will stop next time "
13815 "breakpoint %d is reached."),
13816 bptnum);
13817 else if (count == 1)
13818 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13819 bptnum);
13820 else
13821 printf_filtered (_("Will ignore next %d "
13822 "crossings of breakpoint %d."),
13823 count, bptnum);
13824 }
13825 gdb::observers::breakpoint_modified.notify (b);
13826 return;
13827 }
13828
13829 error (_("No breakpoint number %d."), bptnum);
13830 }
13831
13832 /* Command to set ignore-count of breakpoint N to COUNT. */
13833
13834 static void
13835 ignore_command (const char *args, int from_tty)
13836 {
13837 const char *p = args;
13838 int num;
13839
13840 if (p == 0)
13841 error_no_arg (_("a breakpoint number"));
13842
13843 num = get_number (&p);
13844 if (num == 0)
13845 error (_("bad breakpoint number: '%s'"), args);
13846 if (*p == 0)
13847 error (_("Second argument (specified ignore-count) is missing."));
13848
13849 set_ignore_count (num,
13850 longest_to_int (value_as_long (parse_and_eval (p))),
13851 from_tty);
13852 if (from_tty)
13853 printf_filtered ("\n");
13854 }
13855 \f
13856
13857 /* Call FUNCTION on each of the breakpoints with numbers in the range
13858 defined by BP_NUM_RANGE (an inclusive range). */
13859
13860 static void
13861 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13862 gdb::function_view<void (breakpoint *)> function)
13863 {
13864 if (bp_num_range.first == 0)
13865 {
13866 warning (_("bad breakpoint number at or near '%d'"),
13867 bp_num_range.first);
13868 }
13869 else
13870 {
13871 struct breakpoint *b, *tmp;
13872
13873 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13874 {
13875 bool match = false;
13876
13877 ALL_BREAKPOINTS_SAFE (b, tmp)
13878 if (b->number == i)
13879 {
13880 match = true;
13881 function (b);
13882 break;
13883 }
13884 if (!match)
13885 printf_unfiltered (_("No breakpoint number %d.\n"), i);
13886 }
13887 }
13888 }
13889
13890 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13891 ARGS. */
13892
13893 static void
13894 map_breakpoint_numbers (const char *args,
13895 gdb::function_view<void (breakpoint *)> function)
13896 {
13897 if (args == NULL || *args == '\0')
13898 error_no_arg (_("one or more breakpoint numbers"));
13899
13900 number_or_range_parser parser (args);
13901
13902 while (!parser.finished ())
13903 {
13904 int num = parser.get_number ();
13905 map_breakpoint_number_range (std::make_pair (num, num), function);
13906 }
13907 }
13908
13909 /* Return the breakpoint location structure corresponding to the
13910 BP_NUM and LOC_NUM values. */
13911
13912 static struct bp_location *
13913 find_location_by_number (int bp_num, int loc_num)
13914 {
13915 struct breakpoint *b;
13916
13917 ALL_BREAKPOINTS (b)
13918 if (b->number == bp_num)
13919 {
13920 break;
13921 }
13922
13923 if (!b || b->number != bp_num)
13924 error (_("Bad breakpoint number '%d'"), bp_num);
13925
13926 if (loc_num == 0)
13927 error (_("Bad breakpoint location number '%d'"), loc_num);
13928
13929 int n = 0;
13930 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
13931 if (++n == loc_num)
13932 return loc;
13933
13934 error (_("Bad breakpoint location number '%d'"), loc_num);
13935 }
13936
13937 /* Modes of operation for extract_bp_num. */
13938 enum class extract_bp_kind
13939 {
13940 /* Extracting a breakpoint number. */
13941 bp,
13942
13943 /* Extracting a location number. */
13944 loc,
13945 };
13946
13947 /* Extract a breakpoint or location number (as determined by KIND)
13948 from the string starting at START. TRAILER is a character which
13949 can be found after the number. If you don't want a trailer, use
13950 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13951 string. This always returns a positive integer. */
13952
13953 static int
13954 extract_bp_num (extract_bp_kind kind, const char *start,
13955 int trailer, const char **end_out = NULL)
13956 {
13957 const char *end = start;
13958 int num = get_number_trailer (&end, trailer);
13959 if (num < 0)
13960 error (kind == extract_bp_kind::bp
13961 ? _("Negative breakpoint number '%.*s'")
13962 : _("Negative breakpoint location number '%.*s'"),
13963 int (end - start), start);
13964 if (num == 0)
13965 error (kind == extract_bp_kind::bp
13966 ? _("Bad breakpoint number '%.*s'")
13967 : _("Bad breakpoint location number '%.*s'"),
13968 int (end - start), start);
13969
13970 if (end_out != NULL)
13971 *end_out = end;
13972 return num;
13973 }
13974
13975 /* Extract a breakpoint or location range (as determined by KIND) in
13976 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13977 representing the (inclusive) range. The returned pair's elements
13978 are always positive integers. */
13979
13980 static std::pair<int, int>
13981 extract_bp_or_bp_range (extract_bp_kind kind,
13982 const std::string &arg,
13983 std::string::size_type arg_offset)
13984 {
13985 std::pair<int, int> range;
13986 const char *bp_loc = &arg[arg_offset];
13987 std::string::size_type dash = arg.find ('-', arg_offset);
13988 if (dash != std::string::npos)
13989 {
13990 /* bp_loc is a range (x-z). */
13991 if (arg.length () == dash + 1)
13992 error (kind == extract_bp_kind::bp
13993 ? _("Bad breakpoint number at or near: '%s'")
13994 : _("Bad breakpoint location number at or near: '%s'"),
13995 bp_loc);
13996
13997 const char *end;
13998 const char *start_first = bp_loc;
13999 const char *start_second = &arg[dash + 1];
14000 range.first = extract_bp_num (kind, start_first, '-');
14001 range.second = extract_bp_num (kind, start_second, '\0', &end);
14002
14003 if (range.first > range.second)
14004 error (kind == extract_bp_kind::bp
14005 ? _("Inverted breakpoint range at '%.*s'")
14006 : _("Inverted breakpoint location range at '%.*s'"),
14007 int (end - start_first), start_first);
14008 }
14009 else
14010 {
14011 /* bp_loc is a single value. */
14012 range.first = extract_bp_num (kind, bp_loc, '\0');
14013 range.second = range.first;
14014 }
14015 return range;
14016 }
14017
14018 /* Extract the breakpoint/location range specified by ARG. Returns
14019 the breakpoint range in BP_NUM_RANGE, and the location range in
14020 BP_LOC_RANGE.
14021
14022 ARG may be in any of the following forms:
14023
14024 x where 'x' is a breakpoint number.
14025 x-y where 'x' and 'y' specify a breakpoint numbers range.
14026 x.y where 'x' is a breakpoint number and 'y' a location number.
14027 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14028 location number range.
14029 */
14030
14031 static void
14032 extract_bp_number_and_location (const std::string &arg,
14033 std::pair<int, int> &bp_num_range,
14034 std::pair<int, int> &bp_loc_range)
14035 {
14036 std::string::size_type dot = arg.find ('.');
14037
14038 if (dot != std::string::npos)
14039 {
14040 /* Handle 'x.y' and 'x.y-z' cases. */
14041
14042 if (arg.length () == dot + 1 || dot == 0)
14043 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14044
14045 bp_num_range.first
14046 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14047 bp_num_range.second = bp_num_range.first;
14048
14049 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14050 arg, dot + 1);
14051 }
14052 else
14053 {
14054 /* Handle x and x-y cases. */
14055
14056 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14057 bp_loc_range.first = 0;
14058 bp_loc_range.second = 0;
14059 }
14060 }
14061
14062 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14063 specifies whether to enable or disable. */
14064
14065 static void
14066 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14067 {
14068 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14069 if (loc != NULL)
14070 {
14071 if (loc->enabled != enable)
14072 {
14073 loc->enabled = enable;
14074 mark_breakpoint_location_modified (loc);
14075 }
14076 if (target_supports_enable_disable_tracepoint ()
14077 && current_trace_status ()->running && loc->owner
14078 && is_tracepoint (loc->owner))
14079 target_disable_tracepoint (loc);
14080 }
14081 update_global_location_list (UGLL_DONT_INSERT);
14082
14083 gdb::observers::breakpoint_modified.notify (loc->owner);
14084 }
14085
14086 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14087 number of the breakpoint, and BP_LOC_RANGE specifies the
14088 (inclusive) range of location numbers of that breakpoint to
14089 enable/disable. ENABLE specifies whether to enable or disable the
14090 location. */
14091
14092 static void
14093 enable_disable_breakpoint_location_range (int bp_num,
14094 std::pair<int, int> &bp_loc_range,
14095 bool enable)
14096 {
14097 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14098 enable_disable_bp_num_loc (bp_num, i, enable);
14099 }
14100
14101 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14102 If from_tty is nonzero, it prints a message to that effect,
14103 which ends with a period (no newline). */
14104
14105 void
14106 disable_breakpoint (struct breakpoint *bpt)
14107 {
14108 /* Never disable a watchpoint scope breakpoint; we want to
14109 hit them when we leave scope so we can delete both the
14110 watchpoint and its scope breakpoint at that time. */
14111 if (bpt->type == bp_watchpoint_scope)
14112 return;
14113
14114 bpt->enable_state = bp_disabled;
14115
14116 /* Mark breakpoint locations modified. */
14117 mark_breakpoint_modified (bpt);
14118
14119 if (target_supports_enable_disable_tracepoint ()
14120 && current_trace_status ()->running && is_tracepoint (bpt))
14121 {
14122 struct bp_location *location;
14123
14124 for (location = bpt->loc; location; location = location->next)
14125 target_disable_tracepoint (location);
14126 }
14127
14128 update_global_location_list (UGLL_DONT_INSERT);
14129
14130 gdb::observers::breakpoint_modified.notify (bpt);
14131 }
14132
14133 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14134 specified in ARGS. ARGS may be in any of the formats handled by
14135 extract_bp_number_and_location. ENABLE specifies whether to enable
14136 or disable the breakpoints/locations. */
14137
14138 static void
14139 enable_disable_command (const char *args, int from_tty, bool enable)
14140 {
14141 if (args == 0)
14142 {
14143 struct breakpoint *bpt;
14144
14145 ALL_BREAKPOINTS (bpt)
14146 if (user_breakpoint_p (bpt))
14147 {
14148 if (enable)
14149 enable_breakpoint (bpt);
14150 else
14151 disable_breakpoint (bpt);
14152 }
14153 }
14154 else
14155 {
14156 std::string num = extract_arg (&args);
14157
14158 while (!num.empty ())
14159 {
14160 std::pair<int, int> bp_num_range, bp_loc_range;
14161
14162 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14163
14164 if (bp_loc_range.first == bp_loc_range.second
14165 && bp_loc_range.first == 0)
14166 {
14167 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14168 map_breakpoint_number_range (bp_num_range,
14169 enable
14170 ? enable_breakpoint
14171 : disable_breakpoint);
14172 }
14173 else
14174 {
14175 /* Handle breakpoint ids with formats 'x.y' or
14176 'x.y-z'. */
14177 enable_disable_breakpoint_location_range
14178 (bp_num_range.first, bp_loc_range, enable);
14179 }
14180 num = extract_arg (&args);
14181 }
14182 }
14183 }
14184
14185 /* The disable command disables the specified breakpoints/locations
14186 (or all defined breakpoints) so they're no longer effective in
14187 stopping the inferior. ARGS may be in any of the forms defined in
14188 extract_bp_number_and_location. */
14189
14190 static void
14191 disable_command (const char *args, int from_tty)
14192 {
14193 enable_disable_command (args, from_tty, false);
14194 }
14195
14196 static void
14197 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14198 int count)
14199 {
14200 int target_resources_ok;
14201
14202 if (bpt->type == bp_hardware_breakpoint)
14203 {
14204 int i;
14205 i = hw_breakpoint_used_count ();
14206 target_resources_ok =
14207 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14208 i + 1, 0);
14209 if (target_resources_ok == 0)
14210 error (_("No hardware breakpoint support in the target."));
14211 else if (target_resources_ok < 0)
14212 error (_("Hardware breakpoints used exceeds limit."));
14213 }
14214
14215 if (is_watchpoint (bpt))
14216 {
14217 /* Initialize it just to avoid a GCC false warning. */
14218 enum enable_state orig_enable_state = bp_disabled;
14219
14220 try
14221 {
14222 struct watchpoint *w = (struct watchpoint *) bpt;
14223
14224 orig_enable_state = bpt->enable_state;
14225 bpt->enable_state = bp_enabled;
14226 update_watchpoint (w, 1 /* reparse */);
14227 }
14228 catch (const gdb_exception &e)
14229 {
14230 bpt->enable_state = orig_enable_state;
14231 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14232 bpt->number);
14233 return;
14234 }
14235 }
14236
14237 bpt->enable_state = bp_enabled;
14238
14239 /* Mark breakpoint locations modified. */
14240 mark_breakpoint_modified (bpt);
14241
14242 if (target_supports_enable_disable_tracepoint ()
14243 && current_trace_status ()->running && is_tracepoint (bpt))
14244 {
14245 struct bp_location *location;
14246
14247 for (location = bpt->loc; location; location = location->next)
14248 target_enable_tracepoint (location);
14249 }
14250
14251 bpt->disposition = disposition;
14252 bpt->enable_count = count;
14253 update_global_location_list (UGLL_MAY_INSERT);
14254
14255 gdb::observers::breakpoint_modified.notify (bpt);
14256 }
14257
14258
14259 void
14260 enable_breakpoint (struct breakpoint *bpt)
14261 {
14262 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14263 }
14264
14265 /* The enable command enables the specified breakpoints/locations (or
14266 all defined breakpoints) so they once again become (or continue to
14267 be) effective in stopping the inferior. ARGS may be in any of the
14268 forms defined in extract_bp_number_and_location. */
14269
14270 static void
14271 enable_command (const char *args, int from_tty)
14272 {
14273 enable_disable_command (args, from_tty, true);
14274 }
14275
14276 static void
14277 enable_once_command (const char *args, int from_tty)
14278 {
14279 map_breakpoint_numbers
14280 (args, [&] (breakpoint *b)
14281 {
14282 iterate_over_related_breakpoints
14283 (b, [&] (breakpoint *bpt)
14284 {
14285 enable_breakpoint_disp (bpt, disp_disable, 1);
14286 });
14287 });
14288 }
14289
14290 static void
14291 enable_count_command (const char *args, int from_tty)
14292 {
14293 int count;
14294
14295 if (args == NULL)
14296 error_no_arg (_("hit count"));
14297
14298 count = get_number (&args);
14299
14300 map_breakpoint_numbers
14301 (args, [&] (breakpoint *b)
14302 {
14303 iterate_over_related_breakpoints
14304 (b, [&] (breakpoint *bpt)
14305 {
14306 enable_breakpoint_disp (bpt, disp_disable, count);
14307 });
14308 });
14309 }
14310
14311 static void
14312 enable_delete_command (const char *args, int from_tty)
14313 {
14314 map_breakpoint_numbers
14315 (args, [&] (breakpoint *b)
14316 {
14317 iterate_over_related_breakpoints
14318 (b, [&] (breakpoint *bpt)
14319 {
14320 enable_breakpoint_disp (bpt, disp_del, 1);
14321 });
14322 });
14323 }
14324 \f
14325 /* Invalidate last known value of any hardware watchpoint if
14326 the memory which that value represents has been written to by
14327 GDB itself. */
14328
14329 static void
14330 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14331 CORE_ADDR addr, ssize_t len,
14332 const bfd_byte *data)
14333 {
14334 struct breakpoint *bp;
14335
14336 ALL_BREAKPOINTS (bp)
14337 if (bp->enable_state == bp_enabled
14338 && bp->type == bp_hardware_watchpoint)
14339 {
14340 struct watchpoint *wp = (struct watchpoint *) bp;
14341
14342 if (wp->val_valid && wp->val != nullptr)
14343 {
14344 struct bp_location *loc;
14345
14346 for (loc = bp->loc; loc != NULL; loc = loc->next)
14347 if (loc->loc_type == bp_loc_hardware_watchpoint
14348 && loc->address + loc->length > addr
14349 && addr + len > loc->address)
14350 {
14351 wp->val = NULL;
14352 wp->val_valid = false;
14353 }
14354 }
14355 }
14356 }
14357
14358 /* Create and insert a breakpoint for software single step. */
14359
14360 void
14361 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14362 const address_space *aspace,
14363 CORE_ADDR next_pc)
14364 {
14365 struct thread_info *tp = inferior_thread ();
14366 struct symtab_and_line sal;
14367 CORE_ADDR pc = next_pc;
14368
14369 if (tp->control.single_step_breakpoints == NULL)
14370 {
14371 tp->control.single_step_breakpoints
14372 = new_single_step_breakpoint (tp->global_num, gdbarch);
14373 }
14374
14375 sal = find_pc_line (pc, 0);
14376 sal.pc = pc;
14377 sal.section = find_pc_overlay (pc);
14378 sal.explicit_pc = 1;
14379 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14380
14381 update_global_location_list (UGLL_INSERT);
14382 }
14383
14384 /* Insert single step breakpoints according to the current state. */
14385
14386 int
14387 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14388 {
14389 struct regcache *regcache = get_current_regcache ();
14390 std::vector<CORE_ADDR> next_pcs;
14391
14392 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14393
14394 if (!next_pcs.empty ())
14395 {
14396 struct frame_info *frame = get_current_frame ();
14397 const address_space *aspace = get_frame_address_space (frame);
14398
14399 for (CORE_ADDR pc : next_pcs)
14400 insert_single_step_breakpoint (gdbarch, aspace, pc);
14401
14402 return 1;
14403 }
14404 else
14405 return 0;
14406 }
14407
14408 /* See breakpoint.h. */
14409
14410 int
14411 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14412 const address_space *aspace,
14413 CORE_ADDR pc)
14414 {
14415 struct bp_location *loc;
14416
14417 for (loc = bp->loc; loc != NULL; loc = loc->next)
14418 if (loc->inserted
14419 && breakpoint_location_address_match (loc, aspace, pc))
14420 return 1;
14421
14422 return 0;
14423 }
14424
14425 /* Check whether a software single-step breakpoint is inserted at
14426 PC. */
14427
14428 int
14429 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14430 CORE_ADDR pc)
14431 {
14432 struct breakpoint *bpt;
14433
14434 ALL_BREAKPOINTS (bpt)
14435 {
14436 if (bpt->type == bp_single_step
14437 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14438 return 1;
14439 }
14440 return 0;
14441 }
14442
14443 /* Tracepoint-specific operations. */
14444
14445 /* Set tracepoint count to NUM. */
14446 static void
14447 set_tracepoint_count (int num)
14448 {
14449 tracepoint_count = num;
14450 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14451 }
14452
14453 static void
14454 trace_command (const char *arg, int from_tty)
14455 {
14456 event_location_up location = string_to_event_location (&arg,
14457 current_language);
14458 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14459 (location.get (), true /* is_tracepoint */);
14460
14461 create_breakpoint (get_current_arch (),
14462 location.get (),
14463 NULL, 0, arg, 1 /* parse arg */,
14464 0 /* tempflag */,
14465 bp_tracepoint /* type_wanted */,
14466 0 /* Ignore count */,
14467 pending_break_support,
14468 ops,
14469 from_tty,
14470 1 /* enabled */,
14471 0 /* internal */, 0);
14472 }
14473
14474 static void
14475 ftrace_command (const char *arg, int from_tty)
14476 {
14477 event_location_up location = string_to_event_location (&arg,
14478 current_language);
14479 create_breakpoint (get_current_arch (),
14480 location.get (),
14481 NULL, 0, arg, 1 /* parse arg */,
14482 0 /* tempflag */,
14483 bp_fast_tracepoint /* type_wanted */,
14484 0 /* Ignore count */,
14485 pending_break_support,
14486 &tracepoint_breakpoint_ops,
14487 from_tty,
14488 1 /* enabled */,
14489 0 /* internal */, 0);
14490 }
14491
14492 /* strace command implementation. Creates a static tracepoint. */
14493
14494 static void
14495 strace_command (const char *arg, int from_tty)
14496 {
14497 struct breakpoint_ops *ops;
14498 event_location_up location;
14499
14500 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14501 or with a normal static tracepoint. */
14502 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14503 {
14504 ops = &strace_marker_breakpoint_ops;
14505 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14506 }
14507 else
14508 {
14509 ops = &tracepoint_breakpoint_ops;
14510 location = string_to_event_location (&arg, current_language);
14511 }
14512
14513 create_breakpoint (get_current_arch (),
14514 location.get (),
14515 NULL, 0, arg, 1 /* parse arg */,
14516 0 /* tempflag */,
14517 bp_static_tracepoint /* type_wanted */,
14518 0 /* Ignore count */,
14519 pending_break_support,
14520 ops,
14521 from_tty,
14522 1 /* enabled */,
14523 0 /* internal */, 0);
14524 }
14525
14526 /* Set up a fake reader function that gets command lines from a linked
14527 list that was acquired during tracepoint uploading. */
14528
14529 static struct uploaded_tp *this_utp;
14530 static int next_cmd;
14531
14532 static char *
14533 read_uploaded_action (void)
14534 {
14535 char *rslt = nullptr;
14536
14537 if (next_cmd < this_utp->cmd_strings.size ())
14538 {
14539 rslt = this_utp->cmd_strings[next_cmd].get ();
14540 next_cmd++;
14541 }
14542
14543 return rslt;
14544 }
14545
14546 /* Given information about a tracepoint as recorded on a target (which
14547 can be either a live system or a trace file), attempt to create an
14548 equivalent GDB tracepoint. This is not a reliable process, since
14549 the target does not necessarily have all the information used when
14550 the tracepoint was originally defined. */
14551
14552 struct tracepoint *
14553 create_tracepoint_from_upload (struct uploaded_tp *utp)
14554 {
14555 const char *addr_str;
14556 char small_buf[100];
14557 struct tracepoint *tp;
14558
14559 if (utp->at_string)
14560 addr_str = utp->at_string.get ();
14561 else
14562 {
14563 /* In the absence of a source location, fall back to raw
14564 address. Since there is no way to confirm that the address
14565 means the same thing as when the trace was started, warn the
14566 user. */
14567 warning (_("Uploaded tracepoint %d has no "
14568 "source location, using raw address"),
14569 utp->number);
14570 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14571 addr_str = small_buf;
14572 }
14573
14574 /* There's not much we can do with a sequence of bytecodes. */
14575 if (utp->cond && !utp->cond_string)
14576 warning (_("Uploaded tracepoint %d condition "
14577 "has no source form, ignoring it"),
14578 utp->number);
14579
14580 event_location_up location = string_to_event_location (&addr_str,
14581 current_language);
14582 if (!create_breakpoint (get_current_arch (),
14583 location.get (),
14584 utp->cond_string.get (), -1, addr_str,
14585 0 /* parse cond/thread */,
14586 0 /* tempflag */,
14587 utp->type /* type_wanted */,
14588 0 /* Ignore count */,
14589 pending_break_support,
14590 &tracepoint_breakpoint_ops,
14591 0 /* from_tty */,
14592 utp->enabled /* enabled */,
14593 0 /* internal */,
14594 CREATE_BREAKPOINT_FLAGS_INSERTED))
14595 return NULL;
14596
14597 /* Get the tracepoint we just created. */
14598 tp = get_tracepoint (tracepoint_count);
14599 gdb_assert (tp != NULL);
14600
14601 if (utp->pass > 0)
14602 {
14603 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14604 tp->number);
14605
14606 trace_pass_command (small_buf, 0);
14607 }
14608
14609 /* If we have uploaded versions of the original commands, set up a
14610 special-purpose "reader" function and call the usual command line
14611 reader, then pass the result to the breakpoint command-setting
14612 function. */
14613 if (!utp->cmd_strings.empty ())
14614 {
14615 counted_command_line cmd_list;
14616
14617 this_utp = utp;
14618 next_cmd = 0;
14619
14620 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14621
14622 breakpoint_set_commands (tp, std::move (cmd_list));
14623 }
14624 else if (!utp->actions.empty ()
14625 || !utp->step_actions.empty ())
14626 warning (_("Uploaded tracepoint %d actions "
14627 "have no source form, ignoring them"),
14628 utp->number);
14629
14630 /* Copy any status information that might be available. */
14631 tp->hit_count = utp->hit_count;
14632 tp->traceframe_usage = utp->traceframe_usage;
14633
14634 return tp;
14635 }
14636
14637 /* Print information on tracepoint number TPNUM_EXP, or all if
14638 omitted. */
14639
14640 static void
14641 info_tracepoints_command (const char *args, int from_tty)
14642 {
14643 struct ui_out *uiout = current_uiout;
14644 int num_printed;
14645
14646 num_printed = breakpoint_1 (args, false, is_tracepoint);
14647
14648 if (num_printed == 0)
14649 {
14650 if (args == NULL || *args == '\0')
14651 uiout->message ("No tracepoints.\n");
14652 else
14653 uiout->message ("No tracepoint matching '%s'.\n", args);
14654 }
14655
14656 default_collect_info ();
14657 }
14658
14659 /* The 'enable trace' command enables tracepoints.
14660 Not supported by all targets. */
14661 static void
14662 enable_trace_command (const char *args, int from_tty)
14663 {
14664 enable_command (args, from_tty);
14665 }
14666
14667 /* The 'disable trace' command disables tracepoints.
14668 Not supported by all targets. */
14669 static void
14670 disable_trace_command (const char *args, int from_tty)
14671 {
14672 disable_command (args, from_tty);
14673 }
14674
14675 /* Remove a tracepoint (or all if no argument). */
14676 static void
14677 delete_trace_command (const char *arg, int from_tty)
14678 {
14679 struct breakpoint *b, *b_tmp;
14680
14681 dont_repeat ();
14682
14683 if (arg == 0)
14684 {
14685 int breaks_to_delete = 0;
14686
14687 /* Delete all breakpoints if no argument.
14688 Do not delete internal or call-dummy breakpoints, these
14689 have to be deleted with an explicit breakpoint number
14690 argument. */
14691 ALL_TRACEPOINTS (b)
14692 if (is_tracepoint (b) && user_breakpoint_p (b))
14693 {
14694 breaks_to_delete = 1;
14695 break;
14696 }
14697
14698 /* Ask user only if there are some breakpoints to delete. */
14699 if (!from_tty
14700 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14701 {
14702 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14703 if (is_tracepoint (b) && user_breakpoint_p (b))
14704 delete_breakpoint (b);
14705 }
14706 }
14707 else
14708 map_breakpoint_numbers
14709 (arg, [&] (breakpoint *br)
14710 {
14711 iterate_over_related_breakpoints (br, delete_breakpoint);
14712 });
14713 }
14714
14715 /* Helper function for trace_pass_command. */
14716
14717 static void
14718 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14719 {
14720 tp->pass_count = count;
14721 gdb::observers::breakpoint_modified.notify (tp);
14722 if (from_tty)
14723 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14724 tp->number, count);
14725 }
14726
14727 /* Set passcount for tracepoint.
14728
14729 First command argument is passcount, second is tracepoint number.
14730 If tracepoint number omitted, apply to most recently defined.
14731 Also accepts special argument "all". */
14732
14733 static void
14734 trace_pass_command (const char *args, int from_tty)
14735 {
14736 struct tracepoint *t1;
14737 ULONGEST count;
14738
14739 if (args == 0 || *args == 0)
14740 error (_("passcount command requires an "
14741 "argument (count + optional TP num)"));
14742
14743 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14744
14745 args = skip_spaces (args);
14746 if (*args && strncasecmp (args, "all", 3) == 0)
14747 {
14748 struct breakpoint *b;
14749
14750 args += 3; /* Skip special argument "all". */
14751 if (*args)
14752 error (_("Junk at end of arguments."));
14753
14754 ALL_TRACEPOINTS (b)
14755 {
14756 t1 = (struct tracepoint *) b;
14757 trace_pass_set_count (t1, count, from_tty);
14758 }
14759 }
14760 else if (*args == '\0')
14761 {
14762 t1 = get_tracepoint_by_number (&args, NULL);
14763 if (t1)
14764 trace_pass_set_count (t1, count, from_tty);
14765 }
14766 else
14767 {
14768 number_or_range_parser parser (args);
14769 while (!parser.finished ())
14770 {
14771 t1 = get_tracepoint_by_number (&args, &parser);
14772 if (t1)
14773 trace_pass_set_count (t1, count, from_tty);
14774 }
14775 }
14776 }
14777
14778 struct tracepoint *
14779 get_tracepoint (int num)
14780 {
14781 struct breakpoint *t;
14782
14783 ALL_TRACEPOINTS (t)
14784 if (t->number == num)
14785 return (struct tracepoint *) t;
14786
14787 return NULL;
14788 }
14789
14790 /* Find the tracepoint with the given target-side number (which may be
14791 different from the tracepoint number after disconnecting and
14792 reconnecting). */
14793
14794 struct tracepoint *
14795 get_tracepoint_by_number_on_target (int num)
14796 {
14797 struct breakpoint *b;
14798
14799 ALL_TRACEPOINTS (b)
14800 {
14801 struct tracepoint *t = (struct tracepoint *) b;
14802
14803 if (t->number_on_target == num)
14804 return t;
14805 }
14806
14807 return NULL;
14808 }
14809
14810 /* Utility: parse a tracepoint number and look it up in the list.
14811 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14812 If the argument is missing, the most recent tracepoint
14813 (tracepoint_count) is returned. */
14814
14815 struct tracepoint *
14816 get_tracepoint_by_number (const char **arg,
14817 number_or_range_parser *parser)
14818 {
14819 struct breakpoint *t;
14820 int tpnum;
14821 const char *instring = arg == NULL ? NULL : *arg;
14822
14823 if (parser != NULL)
14824 {
14825 gdb_assert (!parser->finished ());
14826 tpnum = parser->get_number ();
14827 }
14828 else if (arg == NULL || *arg == NULL || ! **arg)
14829 tpnum = tracepoint_count;
14830 else
14831 tpnum = get_number (arg);
14832
14833 if (tpnum <= 0)
14834 {
14835 if (instring && *instring)
14836 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14837 instring);
14838 else
14839 printf_filtered (_("No previous tracepoint\n"));
14840 return NULL;
14841 }
14842
14843 ALL_TRACEPOINTS (t)
14844 if (t->number == tpnum)
14845 {
14846 return (struct tracepoint *) t;
14847 }
14848
14849 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14850 return NULL;
14851 }
14852
14853 void
14854 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14855 {
14856 if (b->thread != -1)
14857 fprintf_unfiltered (fp, " thread %d", b->thread);
14858
14859 if (b->task != 0)
14860 fprintf_unfiltered (fp, " task %d", b->task);
14861
14862 fprintf_unfiltered (fp, "\n");
14863 }
14864
14865 /* Save information on user settable breakpoints (watchpoints, etc) to
14866 a new script file named FILENAME. If FILTER is non-NULL, call it
14867 on each breakpoint and only include the ones for which it returns
14868 true. */
14869
14870 static void
14871 save_breakpoints (const char *filename, int from_tty,
14872 bool (*filter) (const struct breakpoint *))
14873 {
14874 struct breakpoint *tp;
14875 int any = 0;
14876 int extra_trace_bits = 0;
14877
14878 if (filename == 0 || *filename == 0)
14879 error (_("Argument required (file name in which to save)"));
14880
14881 /* See if we have anything to save. */
14882 ALL_BREAKPOINTS (tp)
14883 {
14884 /* Skip internal and momentary breakpoints. */
14885 if (!user_breakpoint_p (tp))
14886 continue;
14887
14888 /* If we have a filter, only save the breakpoints it accepts. */
14889 if (filter && !filter (tp))
14890 continue;
14891
14892 any = 1;
14893
14894 if (is_tracepoint (tp))
14895 {
14896 extra_trace_bits = 1;
14897
14898 /* We can stop searching. */
14899 break;
14900 }
14901 }
14902
14903 if (!any)
14904 {
14905 warning (_("Nothing to save."));
14906 return;
14907 }
14908
14909 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14910
14911 stdio_file fp;
14912
14913 if (!fp.open (expanded_filename.get (), "w"))
14914 error (_("Unable to open file '%s' for saving (%s)"),
14915 expanded_filename.get (), safe_strerror (errno));
14916
14917 if (extra_trace_bits)
14918 save_trace_state_variables (&fp);
14919
14920 ALL_BREAKPOINTS (tp)
14921 {
14922 /* Skip internal and momentary breakpoints. */
14923 if (!user_breakpoint_p (tp))
14924 continue;
14925
14926 /* If we have a filter, only save the breakpoints it accepts. */
14927 if (filter && !filter (tp))
14928 continue;
14929
14930 tp->ops->print_recreate (tp, &fp);
14931
14932 /* Note, we can't rely on tp->number for anything, as we can't
14933 assume the recreated breakpoint numbers will match. Use $bpnum
14934 instead. */
14935
14936 if (tp->cond_string)
14937 fp.printf (" condition $bpnum %s\n", tp->cond_string);
14938
14939 if (tp->ignore_count)
14940 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14941
14942 if (tp->type != bp_dprintf && tp->commands)
14943 {
14944 fp.puts (" commands\n");
14945
14946 current_uiout->redirect (&fp);
14947 try
14948 {
14949 print_command_lines (current_uiout, tp->commands.get (), 2);
14950 }
14951 catch (const gdb_exception &ex)
14952 {
14953 current_uiout->redirect (NULL);
14954 throw;
14955 }
14956
14957 current_uiout->redirect (NULL);
14958 fp.puts (" end\n");
14959 }
14960
14961 if (tp->enable_state == bp_disabled)
14962 fp.puts ("disable $bpnum\n");
14963
14964 /* If this is a multi-location breakpoint, check if the locations
14965 should be individually disabled. Watchpoint locations are
14966 special, and not user visible. */
14967 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14968 {
14969 struct bp_location *loc;
14970 int n = 1;
14971
14972 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
14973 if (!loc->enabled)
14974 fp.printf ("disable $bpnum.%d\n", n);
14975 }
14976 }
14977
14978 if (extra_trace_bits && *default_collect)
14979 fp.printf ("set default-collect %s\n", default_collect);
14980
14981 if (from_tty)
14982 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
14983 }
14984
14985 /* The `save breakpoints' command. */
14986
14987 static void
14988 save_breakpoints_command (const char *args, int from_tty)
14989 {
14990 save_breakpoints (args, from_tty, NULL);
14991 }
14992
14993 /* The `save tracepoints' command. */
14994
14995 static void
14996 save_tracepoints_command (const char *args, int from_tty)
14997 {
14998 save_breakpoints (args, from_tty, is_tracepoint);
14999 }
15000
15001 /* Create a vector of all tracepoints. */
15002
15003 std::vector<breakpoint *>
15004 all_tracepoints (void)
15005 {
15006 std::vector<breakpoint *> tp_vec;
15007 struct breakpoint *tp;
15008
15009 ALL_TRACEPOINTS (tp)
15010 {
15011 tp_vec.push_back (tp);
15012 }
15013
15014 return tp_vec;
15015 }
15016
15017 \f
15018 /* This help string is used to consolidate all the help string for specifying
15019 locations used by several commands. */
15020
15021 #define LOCATION_HELP_STRING \
15022 "Linespecs are colon-separated lists of location parameters, such as\n\
15023 source filename, function name, label name, and line number.\n\
15024 Example: To specify the start of a label named \"the_top\" in the\n\
15025 function \"fact\" in the file \"factorial.c\", use\n\
15026 \"factorial.c:fact:the_top\".\n\
15027 \n\
15028 Address locations begin with \"*\" and specify an exact address in the\n\
15029 program. Example: To specify the fourth byte past the start function\n\
15030 \"main\", use \"*main + 4\".\n\
15031 \n\
15032 Explicit locations are similar to linespecs but use an option/argument\n\
15033 syntax to specify location parameters.\n\
15034 Example: To specify the start of the label named \"the_top\" in the\n\
15035 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15036 -function fact -label the_top\".\n\
15037 \n\
15038 By default, a specified function is matched against the program's\n\
15039 functions in all scopes. For C++, this means in all namespaces and\n\
15040 classes. For Ada, this means in all packages. E.g., in C++,\n\
15041 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15042 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15043 specified name as a complete fully-qualified name instead."
15044
15045 /* This help string is used for the break, hbreak, tbreak and thbreak
15046 commands. It is defined as a macro to prevent duplication.
15047 COMMAND should be a string constant containing the name of the
15048 command. */
15049
15050 #define BREAK_ARGS_HELP(command) \
15051 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15052 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15053 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15054 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15055 `-probe-dtrace' (for a DTrace probe).\n\
15056 LOCATION may be a linespec, address, or explicit location as described\n\
15057 below.\n\
15058 \n\
15059 With no LOCATION, uses current execution address of the selected\n\
15060 stack frame. This is useful for breaking on return to a stack frame.\n\
15061 \n\
15062 THREADNUM is the number from \"info threads\".\n\
15063 CONDITION is a boolean expression.\n\
15064 \n" LOCATION_HELP_STRING "\n\n\
15065 Multiple breakpoints at one place are permitted, and useful if their\n\
15066 conditions are different.\n\
15067 \n\
15068 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15069
15070 /* List of subcommands for "catch". */
15071 static struct cmd_list_element *catch_cmdlist;
15072
15073 /* List of subcommands for "tcatch". */
15074 static struct cmd_list_element *tcatch_cmdlist;
15075
15076 void
15077 add_catch_command (const char *name, const char *docstring,
15078 cmd_const_sfunc_ftype *sfunc,
15079 completer_ftype *completer,
15080 void *user_data_catch,
15081 void *user_data_tcatch)
15082 {
15083 struct cmd_list_element *command;
15084
15085 command = add_cmd (name, class_breakpoint, docstring,
15086 &catch_cmdlist);
15087 set_cmd_sfunc (command, sfunc);
15088 set_cmd_context (command, user_data_catch);
15089 set_cmd_completer (command, completer);
15090
15091 command = add_cmd (name, class_breakpoint, docstring,
15092 &tcatch_cmdlist);
15093 set_cmd_sfunc (command, sfunc);
15094 set_cmd_context (command, user_data_tcatch);
15095 set_cmd_completer (command, completer);
15096 }
15097
15098 struct breakpoint *
15099 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15100 {
15101 struct breakpoint *b, *b_tmp;
15102
15103 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15104 {
15105 if (callback (b))
15106 return b;
15107 }
15108
15109 return NULL;
15110 }
15111
15112 /* Zero if any of the breakpoint's locations could be a location where
15113 functions have been inlined, nonzero otherwise. */
15114
15115 static int
15116 is_non_inline_function (struct breakpoint *b)
15117 {
15118 /* The shared library event breakpoint is set on the address of a
15119 non-inline function. */
15120 if (b->type == bp_shlib_event)
15121 return 1;
15122
15123 return 0;
15124 }
15125
15126 /* Nonzero if the specified PC cannot be a location where functions
15127 have been inlined. */
15128
15129 int
15130 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15131 const struct target_waitstatus *ws)
15132 {
15133 struct breakpoint *b;
15134 struct bp_location *bl;
15135
15136 ALL_BREAKPOINTS (b)
15137 {
15138 if (!is_non_inline_function (b))
15139 continue;
15140
15141 for (bl = b->loc; bl != NULL; bl = bl->next)
15142 {
15143 if (!bl->shlib_disabled
15144 && bpstat_check_location (bl, aspace, pc, ws))
15145 return 1;
15146 }
15147 }
15148
15149 return 0;
15150 }
15151
15152 /* Remove any references to OBJFILE which is going to be freed. */
15153
15154 void
15155 breakpoint_free_objfile (struct objfile *objfile)
15156 {
15157 struct bp_location **locp, *loc;
15158
15159 ALL_BP_LOCATIONS (loc, locp)
15160 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15161 loc->symtab = NULL;
15162 }
15163
15164 void
15165 initialize_breakpoint_ops (void)
15166 {
15167 static int initialized = 0;
15168
15169 struct breakpoint_ops *ops;
15170
15171 if (initialized)
15172 return;
15173 initialized = 1;
15174
15175 /* The breakpoint_ops structure to be inherit by all kinds of
15176 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15177 internal and momentary breakpoints, etc.). */
15178 ops = &bkpt_base_breakpoint_ops;
15179 *ops = base_breakpoint_ops;
15180 ops->re_set = bkpt_re_set;
15181 ops->insert_location = bkpt_insert_location;
15182 ops->remove_location = bkpt_remove_location;
15183 ops->breakpoint_hit = bkpt_breakpoint_hit;
15184 ops->create_sals_from_location = bkpt_create_sals_from_location;
15185 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15186 ops->decode_location = bkpt_decode_location;
15187
15188 /* The breakpoint_ops structure to be used in regular breakpoints. */
15189 ops = &bkpt_breakpoint_ops;
15190 *ops = bkpt_base_breakpoint_ops;
15191 ops->re_set = bkpt_re_set;
15192 ops->resources_needed = bkpt_resources_needed;
15193 ops->print_it = bkpt_print_it;
15194 ops->print_mention = bkpt_print_mention;
15195 ops->print_recreate = bkpt_print_recreate;
15196
15197 /* Ranged breakpoints. */
15198 ops = &ranged_breakpoint_ops;
15199 *ops = bkpt_breakpoint_ops;
15200 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15201 ops->resources_needed = resources_needed_ranged_breakpoint;
15202 ops->print_it = print_it_ranged_breakpoint;
15203 ops->print_one = print_one_ranged_breakpoint;
15204 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15205 ops->print_mention = print_mention_ranged_breakpoint;
15206 ops->print_recreate = print_recreate_ranged_breakpoint;
15207
15208 /* Internal breakpoints. */
15209 ops = &internal_breakpoint_ops;
15210 *ops = bkpt_base_breakpoint_ops;
15211 ops->re_set = internal_bkpt_re_set;
15212 ops->check_status = internal_bkpt_check_status;
15213 ops->print_it = internal_bkpt_print_it;
15214 ops->print_mention = internal_bkpt_print_mention;
15215
15216 /* Momentary breakpoints. */
15217 ops = &momentary_breakpoint_ops;
15218 *ops = bkpt_base_breakpoint_ops;
15219 ops->re_set = momentary_bkpt_re_set;
15220 ops->check_status = momentary_bkpt_check_status;
15221 ops->print_it = momentary_bkpt_print_it;
15222 ops->print_mention = momentary_bkpt_print_mention;
15223
15224 /* Probe breakpoints. */
15225 ops = &bkpt_probe_breakpoint_ops;
15226 *ops = bkpt_breakpoint_ops;
15227 ops->insert_location = bkpt_probe_insert_location;
15228 ops->remove_location = bkpt_probe_remove_location;
15229 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15230 ops->decode_location = bkpt_probe_decode_location;
15231
15232 /* Watchpoints. */
15233 ops = &watchpoint_breakpoint_ops;
15234 *ops = base_breakpoint_ops;
15235 ops->re_set = re_set_watchpoint;
15236 ops->insert_location = insert_watchpoint;
15237 ops->remove_location = remove_watchpoint;
15238 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15239 ops->check_status = check_status_watchpoint;
15240 ops->resources_needed = resources_needed_watchpoint;
15241 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15242 ops->print_it = print_it_watchpoint;
15243 ops->print_mention = print_mention_watchpoint;
15244 ops->print_recreate = print_recreate_watchpoint;
15245 ops->explains_signal = explains_signal_watchpoint;
15246
15247 /* Masked watchpoints. */
15248 ops = &masked_watchpoint_breakpoint_ops;
15249 *ops = watchpoint_breakpoint_ops;
15250 ops->insert_location = insert_masked_watchpoint;
15251 ops->remove_location = remove_masked_watchpoint;
15252 ops->resources_needed = resources_needed_masked_watchpoint;
15253 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15254 ops->print_it = print_it_masked_watchpoint;
15255 ops->print_one_detail = print_one_detail_masked_watchpoint;
15256 ops->print_mention = print_mention_masked_watchpoint;
15257 ops->print_recreate = print_recreate_masked_watchpoint;
15258
15259 /* Tracepoints. */
15260 ops = &tracepoint_breakpoint_ops;
15261 *ops = base_breakpoint_ops;
15262 ops->re_set = tracepoint_re_set;
15263 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15264 ops->print_one_detail = tracepoint_print_one_detail;
15265 ops->print_mention = tracepoint_print_mention;
15266 ops->print_recreate = tracepoint_print_recreate;
15267 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15268 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15269 ops->decode_location = tracepoint_decode_location;
15270
15271 /* Probe tracepoints. */
15272 ops = &tracepoint_probe_breakpoint_ops;
15273 *ops = tracepoint_breakpoint_ops;
15274 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15275 ops->decode_location = tracepoint_probe_decode_location;
15276
15277 /* Static tracepoints with marker (`-m'). */
15278 ops = &strace_marker_breakpoint_ops;
15279 *ops = tracepoint_breakpoint_ops;
15280 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15281 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15282 ops->decode_location = strace_marker_decode_location;
15283
15284 /* Fork catchpoints. */
15285 ops = &catch_fork_breakpoint_ops;
15286 *ops = base_breakpoint_ops;
15287 ops->insert_location = insert_catch_fork;
15288 ops->remove_location = remove_catch_fork;
15289 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15290 ops->print_it = print_it_catch_fork;
15291 ops->print_one = print_one_catch_fork;
15292 ops->print_mention = print_mention_catch_fork;
15293 ops->print_recreate = print_recreate_catch_fork;
15294
15295 /* Vfork catchpoints. */
15296 ops = &catch_vfork_breakpoint_ops;
15297 *ops = base_breakpoint_ops;
15298 ops->insert_location = insert_catch_vfork;
15299 ops->remove_location = remove_catch_vfork;
15300 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15301 ops->print_it = print_it_catch_vfork;
15302 ops->print_one = print_one_catch_vfork;
15303 ops->print_mention = print_mention_catch_vfork;
15304 ops->print_recreate = print_recreate_catch_vfork;
15305
15306 /* Exec catchpoints. */
15307 ops = &catch_exec_breakpoint_ops;
15308 *ops = base_breakpoint_ops;
15309 ops->insert_location = insert_catch_exec;
15310 ops->remove_location = remove_catch_exec;
15311 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15312 ops->print_it = print_it_catch_exec;
15313 ops->print_one = print_one_catch_exec;
15314 ops->print_mention = print_mention_catch_exec;
15315 ops->print_recreate = print_recreate_catch_exec;
15316
15317 /* Solib-related catchpoints. */
15318 ops = &catch_solib_breakpoint_ops;
15319 *ops = base_breakpoint_ops;
15320 ops->insert_location = insert_catch_solib;
15321 ops->remove_location = remove_catch_solib;
15322 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15323 ops->check_status = check_status_catch_solib;
15324 ops->print_it = print_it_catch_solib;
15325 ops->print_one = print_one_catch_solib;
15326 ops->print_mention = print_mention_catch_solib;
15327 ops->print_recreate = print_recreate_catch_solib;
15328
15329 ops = &dprintf_breakpoint_ops;
15330 *ops = bkpt_base_breakpoint_ops;
15331 ops->re_set = dprintf_re_set;
15332 ops->resources_needed = bkpt_resources_needed;
15333 ops->print_it = bkpt_print_it;
15334 ops->print_mention = bkpt_print_mention;
15335 ops->print_recreate = dprintf_print_recreate;
15336 ops->after_condition_true = dprintf_after_condition_true;
15337 ops->breakpoint_hit = dprintf_breakpoint_hit;
15338 }
15339
15340 /* Chain containing all defined "enable breakpoint" subcommands. */
15341
15342 static struct cmd_list_element *enablebreaklist = NULL;
15343
15344 /* See breakpoint.h. */
15345
15346 cmd_list_element *commands_cmd_element = nullptr;
15347
15348 void _initialize_breakpoint ();
15349 void
15350 _initialize_breakpoint ()
15351 {
15352 struct cmd_list_element *c;
15353
15354 initialize_breakpoint_ops ();
15355
15356 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15357 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15358 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15359
15360 breakpoint_chain = 0;
15361 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15362 before a breakpoint is set. */
15363 breakpoint_count = 0;
15364
15365 tracepoint_count = 0;
15366
15367 add_com ("ignore", class_breakpoint, ignore_command, _("\
15368 Set ignore-count of breakpoint number N to COUNT.\n\
15369 Usage is `ignore N COUNT'."));
15370
15371 commands_cmd_element = add_com ("commands", class_breakpoint,
15372 commands_command, _("\
15373 Set commands to be executed when the given breakpoints are hit.\n\
15374 Give a space-separated breakpoint list as argument after \"commands\".\n\
15375 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15376 (e.g. `5-7').\n\
15377 With no argument, the targeted breakpoint is the last one set.\n\
15378 The commands themselves follow starting on the next line.\n\
15379 Type a line containing \"end\" to indicate the end of them.\n\
15380 Give \"silent\" as the first line to make the breakpoint silent;\n\
15381 then no output is printed when it is hit, except what the commands print."));
15382
15383 c = add_com ("condition", class_breakpoint, condition_command, _("\
15384 Specify breakpoint number N to break only if COND is true.\n\
15385 Usage is `condition N COND', where N is an integer and COND is an\n\
15386 expression to be evaluated whenever breakpoint N is reached."));
15387 set_cmd_completer (c, condition_completer);
15388
15389 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15390 Set a temporary breakpoint.\n\
15391 Like \"break\" except the breakpoint is only temporary,\n\
15392 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15393 by using \"enable delete\" on the breakpoint number.\n\
15394 \n"
15395 BREAK_ARGS_HELP ("tbreak")));
15396 set_cmd_completer (c, location_completer);
15397
15398 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15399 Set a hardware assisted breakpoint.\n\
15400 Like \"break\" except the breakpoint requires hardware support,\n\
15401 some target hardware may not have this support.\n\
15402 \n"
15403 BREAK_ARGS_HELP ("hbreak")));
15404 set_cmd_completer (c, location_completer);
15405
15406 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15407 Set a temporary hardware assisted breakpoint.\n\
15408 Like \"hbreak\" except the breakpoint is only temporary,\n\
15409 so it will be deleted when hit.\n\
15410 \n"
15411 BREAK_ARGS_HELP ("thbreak")));
15412 set_cmd_completer (c, location_completer);
15413
15414 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15415 Enable all or some breakpoints.\n\
15416 Usage: enable [BREAKPOINTNUM]...\n\
15417 Give breakpoint numbers (separated by spaces) as arguments.\n\
15418 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15419 This is used to cancel the effect of the \"disable\" command.\n\
15420 With a subcommand you can enable temporarily."),
15421 &enablelist, "enable ", 1, &cmdlist);
15422
15423 add_com_alias ("en", "enable", class_breakpoint, 1);
15424
15425 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15426 Enable all or some breakpoints.\n\
15427 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15428 Give breakpoint numbers (separated by spaces) as arguments.\n\
15429 This is used to cancel the effect of the \"disable\" command.\n\
15430 May be abbreviated to simply \"enable\"."),
15431 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15432
15433 add_cmd ("once", no_class, enable_once_command, _("\
15434 Enable some breakpoints for one hit.\n\
15435 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15436 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15437 &enablebreaklist);
15438
15439 add_cmd ("delete", no_class, enable_delete_command, _("\
15440 Enable some breakpoints and delete when hit.\n\
15441 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15442 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15443 &enablebreaklist);
15444
15445 add_cmd ("count", no_class, enable_count_command, _("\
15446 Enable some breakpoints for COUNT hits.\n\
15447 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15448 If a breakpoint is hit while enabled in this fashion,\n\
15449 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15450 &enablebreaklist);
15451
15452 add_cmd ("delete", no_class, enable_delete_command, _("\
15453 Enable some breakpoints and delete when hit.\n\
15454 Usage: enable delete BREAKPOINTNUM...\n\
15455 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15456 &enablelist);
15457
15458 add_cmd ("once", no_class, enable_once_command, _("\
15459 Enable some breakpoints for one hit.\n\
15460 Usage: enable once BREAKPOINTNUM...\n\
15461 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15462 &enablelist);
15463
15464 add_cmd ("count", no_class, enable_count_command, _("\
15465 Enable some breakpoints for COUNT hits.\n\
15466 Usage: enable count COUNT BREAKPOINTNUM...\n\
15467 If a breakpoint is hit while enabled in this fashion,\n\
15468 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15469 &enablelist);
15470
15471 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15472 Disable all or some breakpoints.\n\
15473 Usage: disable [BREAKPOINTNUM]...\n\
15474 Arguments are breakpoint numbers with spaces in between.\n\
15475 To disable all breakpoints, give no argument.\n\
15476 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15477 &disablelist, "disable ", 1, &cmdlist);
15478 add_com_alias ("dis", "disable", class_breakpoint, 1);
15479 add_com_alias ("disa", "disable", class_breakpoint, 1);
15480
15481 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15482 Disable all or some breakpoints.\n\
15483 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15484 Arguments are breakpoint numbers with spaces in between.\n\
15485 To disable all breakpoints, give no argument.\n\
15486 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15487 This command may be abbreviated \"disable\"."),
15488 &disablelist);
15489
15490 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15491 Delete all or some breakpoints.\n\
15492 Usage: delete [BREAKPOINTNUM]...\n\
15493 Arguments are breakpoint numbers with spaces in between.\n\
15494 To delete all breakpoints, give no argument.\n\
15495 \n\
15496 Also a prefix command for deletion of other GDB objects."),
15497 &deletelist, "delete ", 1, &cmdlist);
15498 add_com_alias ("d", "delete", class_breakpoint, 1);
15499 add_com_alias ("del", "delete", class_breakpoint, 1);
15500
15501 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15502 Delete all or some breakpoints or auto-display expressions.\n\
15503 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15504 Arguments are breakpoint numbers with spaces in between.\n\
15505 To delete all breakpoints, give no argument.\n\
15506 This command may be abbreviated \"delete\"."),
15507 &deletelist);
15508
15509 add_com ("clear", class_breakpoint, clear_command, _("\
15510 Clear breakpoint at specified location.\n\
15511 Argument may be a linespec, explicit, or address location as described below.\n\
15512 \n\
15513 With no argument, clears all breakpoints in the line that the selected frame\n\
15514 is executing in.\n"
15515 "\n" LOCATION_HELP_STRING "\n\n\
15516 See also the \"delete\" command which clears breakpoints by number."));
15517 add_com_alias ("cl", "clear", class_breakpoint, 1);
15518
15519 c = add_com ("break", class_breakpoint, break_command, _("\
15520 Set breakpoint at specified location.\n"
15521 BREAK_ARGS_HELP ("break")));
15522 set_cmd_completer (c, location_completer);
15523
15524 add_com_alias ("b", "break", class_run, 1);
15525 add_com_alias ("br", "break", class_run, 1);
15526 add_com_alias ("bre", "break", class_run, 1);
15527 add_com_alias ("brea", "break", class_run, 1);
15528
15529 if (dbx_commands)
15530 {
15531 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15532 Break in function/address or break at a line in the current file."),
15533 &stoplist, "stop ", 1, &cmdlist);
15534 add_cmd ("in", class_breakpoint, stopin_command,
15535 _("Break in function or address."), &stoplist);
15536 add_cmd ("at", class_breakpoint, stopat_command,
15537 _("Break at a line in the current file."), &stoplist);
15538 add_com ("status", class_info, info_breakpoints_command, _("\
15539 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15540 The \"Type\" column indicates one of:\n\
15541 \tbreakpoint - normal breakpoint\n\
15542 \twatchpoint - watchpoint\n\
15543 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15544 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15545 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15546 address and file/line number respectively.\n\
15547 \n\
15548 Convenience variable \"$_\" and default examine address for \"x\"\n\
15549 are set to the address of the last breakpoint listed unless the command\n\
15550 is prefixed with \"server \".\n\n\
15551 Convenience variable \"$bpnum\" contains the number of the last\n\
15552 breakpoint set."));
15553 }
15554
15555 add_info ("breakpoints", info_breakpoints_command, _("\
15556 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15557 The \"Type\" column indicates one of:\n\
15558 \tbreakpoint - normal breakpoint\n\
15559 \twatchpoint - watchpoint\n\
15560 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15561 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15562 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15563 address and file/line number respectively.\n\
15564 \n\
15565 Convenience variable \"$_\" and default examine address for \"x\"\n\
15566 are set to the address of the last breakpoint listed unless the command\n\
15567 is prefixed with \"server \".\n\n\
15568 Convenience variable \"$bpnum\" contains the number of the last\n\
15569 breakpoint set."));
15570
15571 add_info_alias ("b", "breakpoints", 1);
15572
15573 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15574 Status of all breakpoints, or breakpoint number NUMBER.\n\
15575 The \"Type\" column indicates one of:\n\
15576 \tbreakpoint - normal breakpoint\n\
15577 \twatchpoint - watchpoint\n\
15578 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15579 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15580 \tuntil - internal breakpoint used by the \"until\" command\n\
15581 \tfinish - internal breakpoint used by the \"finish\" command\n\
15582 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15583 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15584 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15585 address and file/line number respectively.\n\
15586 \n\
15587 Convenience variable \"$_\" and default examine address for \"x\"\n\
15588 are set to the address of the last breakpoint listed unless the command\n\
15589 is prefixed with \"server \".\n\n\
15590 Convenience variable \"$bpnum\" contains the number of the last\n\
15591 breakpoint set."),
15592 &maintenanceinfolist);
15593
15594 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15595 Set catchpoints to catch events."),
15596 &catch_cmdlist, "catch ",
15597 0/*allow-unknown*/, &cmdlist);
15598
15599 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15600 Set temporary catchpoints to catch events."),
15601 &tcatch_cmdlist, "tcatch ",
15602 0/*allow-unknown*/, &cmdlist);
15603
15604 add_catch_command ("fork", _("Catch calls to fork."),
15605 catch_fork_command_1,
15606 NULL,
15607 (void *) (uintptr_t) catch_fork_permanent,
15608 (void *) (uintptr_t) catch_fork_temporary);
15609 add_catch_command ("vfork", _("Catch calls to vfork."),
15610 catch_fork_command_1,
15611 NULL,
15612 (void *) (uintptr_t) catch_vfork_permanent,
15613 (void *) (uintptr_t) catch_vfork_temporary);
15614 add_catch_command ("exec", _("Catch calls to exec."),
15615 catch_exec_command_1,
15616 NULL,
15617 CATCH_PERMANENT,
15618 CATCH_TEMPORARY);
15619 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15620 Usage: catch load [REGEX]\n\
15621 If REGEX is given, only stop for libraries matching the regular expression."),
15622 catch_load_command_1,
15623 NULL,
15624 CATCH_PERMANENT,
15625 CATCH_TEMPORARY);
15626 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15627 Usage: catch unload [REGEX]\n\
15628 If REGEX is given, only stop for libraries matching the regular expression."),
15629 catch_unload_command_1,
15630 NULL,
15631 CATCH_PERMANENT,
15632 CATCH_TEMPORARY);
15633
15634 c = add_com ("watch", class_breakpoint, watch_command, _("\
15635 Set a watchpoint for an expression.\n\
15636 Usage: watch [-l|-location] EXPRESSION\n\
15637 A watchpoint stops execution of your program whenever the value of\n\
15638 an expression changes.\n\
15639 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15640 the memory to which it refers."));
15641 set_cmd_completer (c, expression_completer);
15642
15643 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15644 Set a read watchpoint for an expression.\n\
15645 Usage: rwatch [-l|-location] EXPRESSION\n\
15646 A watchpoint stops execution of your program whenever the value of\n\
15647 an expression is read.\n\
15648 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15649 the memory to which it refers."));
15650 set_cmd_completer (c, expression_completer);
15651
15652 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15653 Set a watchpoint for an expression.\n\
15654 Usage: awatch [-l|-location] EXPRESSION\n\
15655 A watchpoint stops execution of your program whenever the value of\n\
15656 an expression is either read or written.\n\
15657 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15658 the memory to which it refers."));
15659 set_cmd_completer (c, expression_completer);
15660
15661 add_info ("watchpoints", info_watchpoints_command, _("\
15662 Status of specified watchpoints (all watchpoints if no argument)."));
15663
15664 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15665 respond to changes - contrary to the description. */
15666 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15667 &can_use_hw_watchpoints, _("\
15668 Set debugger's willingness to use watchpoint hardware."), _("\
15669 Show debugger's willingness to use watchpoint hardware."), _("\
15670 If zero, gdb will not use hardware for new watchpoints, even if\n\
15671 such is available. (However, any hardware watchpoints that were\n\
15672 created before setting this to nonzero, will continue to use watchpoint\n\
15673 hardware.)"),
15674 NULL,
15675 show_can_use_hw_watchpoints,
15676 &setlist, &showlist);
15677
15678 can_use_hw_watchpoints = 1;
15679
15680 /* Tracepoint manipulation commands. */
15681
15682 c = add_com ("trace", class_breakpoint, trace_command, _("\
15683 Set a tracepoint at specified location.\n\
15684 \n"
15685 BREAK_ARGS_HELP ("trace") "\n\
15686 Do \"help tracepoints\" for info on other tracepoint commands."));
15687 set_cmd_completer (c, location_completer);
15688
15689 add_com_alias ("tp", "trace", class_breakpoint, 0);
15690 add_com_alias ("tr", "trace", class_breakpoint, 1);
15691 add_com_alias ("tra", "trace", class_breakpoint, 1);
15692 add_com_alias ("trac", "trace", class_breakpoint, 1);
15693
15694 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15695 Set a fast tracepoint at specified location.\n\
15696 \n"
15697 BREAK_ARGS_HELP ("ftrace") "\n\
15698 Do \"help tracepoints\" for info on other tracepoint commands."));
15699 set_cmd_completer (c, location_completer);
15700
15701 c = add_com ("strace", class_breakpoint, strace_command, _("\
15702 Set a static tracepoint at location or marker.\n\
15703 \n\
15704 strace [LOCATION] [if CONDITION]\n\
15705 LOCATION may be a linespec, explicit, or address location (described below) \n\
15706 or -m MARKER_ID.\n\n\
15707 If a marker id is specified, probe the marker with that name. With\n\
15708 no LOCATION, uses current execution address of the selected stack frame.\n\
15709 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15710 This collects arbitrary user data passed in the probe point call to the\n\
15711 tracing library. You can inspect it when analyzing the trace buffer,\n\
15712 by printing the $_sdata variable like any other convenience variable.\n\
15713 \n\
15714 CONDITION is a boolean expression.\n\
15715 \n" LOCATION_HELP_STRING "\n\n\
15716 Multiple tracepoints at one place are permitted, and useful if their\n\
15717 conditions are different.\n\
15718 \n\
15719 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15720 Do \"help tracepoints\" for info on other tracepoint commands."));
15721 set_cmd_completer (c, location_completer);
15722
15723 add_info ("tracepoints", info_tracepoints_command, _("\
15724 Status of specified tracepoints (all tracepoints if no argument).\n\
15725 Convenience variable \"$tpnum\" contains the number of the\n\
15726 last tracepoint set."));
15727
15728 add_info_alias ("tp", "tracepoints", 1);
15729
15730 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15731 Delete specified tracepoints.\n\
15732 Arguments are tracepoint numbers, separated by spaces.\n\
15733 No argument means delete all tracepoints."),
15734 &deletelist);
15735 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15736
15737 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15738 Disable specified tracepoints.\n\
15739 Arguments are tracepoint numbers, separated by spaces.\n\
15740 No argument means disable all tracepoints."),
15741 &disablelist);
15742 deprecate_cmd (c, "disable");
15743
15744 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15745 Enable specified tracepoints.\n\
15746 Arguments are tracepoint numbers, separated by spaces.\n\
15747 No argument means enable all tracepoints."),
15748 &enablelist);
15749 deprecate_cmd (c, "enable");
15750
15751 add_com ("passcount", class_trace, trace_pass_command, _("\
15752 Set the passcount for a tracepoint.\n\
15753 The trace will end when the tracepoint has been passed 'count' times.\n\
15754 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15755 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15756
15757 add_basic_prefix_cmd ("save", class_breakpoint,
15758 _("Save breakpoint definitions as a script."),
15759 &save_cmdlist, "save ",
15760 0/*allow-unknown*/, &cmdlist);
15761
15762 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15763 Save current breakpoint definitions as a script.\n\
15764 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15765 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15766 session to restore them."),
15767 &save_cmdlist);
15768 set_cmd_completer (c, filename_completer);
15769
15770 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15771 Save current tracepoint definitions as a script.\n\
15772 Use the 'source' command in another debug session to restore them."),
15773 &save_cmdlist);
15774 set_cmd_completer (c, filename_completer);
15775
15776 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15777 deprecate_cmd (c, "save tracepoints");
15778
15779 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
15780 Breakpoint specific settings.\n\
15781 Configure various breakpoint-specific variables such as\n\
15782 pending breakpoint behavior."),
15783 &breakpoint_set_cmdlist, "set breakpoint ",
15784 0/*allow-unknown*/, &setlist);
15785 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
15786 Breakpoint specific settings.\n\
15787 Configure various breakpoint-specific variables such as\n\
15788 pending breakpoint behavior."),
15789 &breakpoint_show_cmdlist, "show breakpoint ",
15790 0/*allow-unknown*/, &showlist);
15791
15792 add_setshow_auto_boolean_cmd ("pending", no_class,
15793 &pending_break_support, _("\
15794 Set debugger's behavior regarding pending breakpoints."), _("\
15795 Show debugger's behavior regarding pending breakpoints."), _("\
15796 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15797 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15798 an error. If auto, an unrecognized breakpoint location results in a\n\
15799 user-query to see if a pending breakpoint should be created."),
15800 NULL,
15801 show_pending_break_support,
15802 &breakpoint_set_cmdlist,
15803 &breakpoint_show_cmdlist);
15804
15805 pending_break_support = AUTO_BOOLEAN_AUTO;
15806
15807 add_setshow_boolean_cmd ("auto-hw", no_class,
15808 &automatic_hardware_breakpoints, _("\
15809 Set automatic usage of hardware breakpoints."), _("\
15810 Show automatic usage of hardware breakpoints."), _("\
15811 If set, the debugger will automatically use hardware breakpoints for\n\
15812 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15813 a warning will be emitted for such breakpoints."),
15814 NULL,
15815 show_automatic_hardware_breakpoints,
15816 &breakpoint_set_cmdlist,
15817 &breakpoint_show_cmdlist);
15818
15819 add_setshow_boolean_cmd ("always-inserted", class_support,
15820 &always_inserted_mode, _("\
15821 Set mode for inserting breakpoints."), _("\
15822 Show mode for inserting breakpoints."), _("\
15823 When this mode is on, breakpoints are inserted immediately as soon as\n\
15824 they're created, kept inserted even when execution stops, and removed\n\
15825 only when the user deletes them. When this mode is off (the default),\n\
15826 breakpoints are inserted only when execution continues, and removed\n\
15827 when execution stops."),
15828 NULL,
15829 &show_always_inserted_mode,
15830 &breakpoint_set_cmdlist,
15831 &breakpoint_show_cmdlist);
15832
15833 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15834 condition_evaluation_enums,
15835 &condition_evaluation_mode_1, _("\
15836 Set mode of breakpoint condition evaluation."), _("\
15837 Show mode of breakpoint condition evaluation."), _("\
15838 When this is set to \"host\", breakpoint conditions will be\n\
15839 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15840 breakpoint conditions will be downloaded to the target (if the target\n\
15841 supports such feature) and conditions will be evaluated on the target's side.\n\
15842 If this is set to \"auto\" (default), this will be automatically set to\n\
15843 \"target\" if it supports condition evaluation, otherwise it will\n\
15844 be set to \"gdb\""),
15845 &set_condition_evaluation_mode,
15846 &show_condition_evaluation_mode,
15847 &breakpoint_set_cmdlist,
15848 &breakpoint_show_cmdlist);
15849
15850 add_com ("break-range", class_breakpoint, break_range_command, _("\
15851 Set a breakpoint for an address range.\n\
15852 break-range START-LOCATION, END-LOCATION\n\
15853 where START-LOCATION and END-LOCATION can be one of the following:\n\
15854 LINENUM, for that line in the current file,\n\
15855 FILE:LINENUM, for that line in that file,\n\
15856 +OFFSET, for that number of lines after the current line\n\
15857 or the start of the range\n\
15858 FUNCTION, for the first line in that function,\n\
15859 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15860 *ADDRESS, for the instruction at that address.\n\
15861 \n\
15862 The breakpoint will stop execution of the inferior whenever it executes\n\
15863 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15864 range (including START-LOCATION and END-LOCATION)."));
15865
15866 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15867 Set a dynamic printf at specified location.\n\
15868 dprintf location,format string,arg1,arg2,...\n\
15869 location may be a linespec, explicit, or address location.\n"
15870 "\n" LOCATION_HELP_STRING));
15871 set_cmd_completer (c, location_completer);
15872
15873 add_setshow_enum_cmd ("dprintf-style", class_support,
15874 dprintf_style_enums, &dprintf_style, _("\
15875 Set the style of usage for dynamic printf."), _("\
15876 Show the style of usage for dynamic printf."), _("\
15877 This setting chooses how GDB will do a dynamic printf.\n\
15878 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15879 console, as with the \"printf\" command.\n\
15880 If the value is \"call\", the print is done by calling a function in your\n\
15881 program; by default printf(), but you can choose a different function or\n\
15882 output stream by setting dprintf-function and dprintf-channel."),
15883 update_dprintf_commands, NULL,
15884 &setlist, &showlist);
15885
15886 dprintf_function = xstrdup ("printf");
15887 add_setshow_string_cmd ("dprintf-function", class_support,
15888 &dprintf_function, _("\
15889 Set the function to use for dynamic printf."), _("\
15890 Show the function to use for dynamic printf."), NULL,
15891 update_dprintf_commands, NULL,
15892 &setlist, &showlist);
15893
15894 dprintf_channel = xstrdup ("");
15895 add_setshow_string_cmd ("dprintf-channel", class_support,
15896 &dprintf_channel, _("\
15897 Set the channel to use for dynamic printf."), _("\
15898 Show the channel to use for dynamic printf."), NULL,
15899 update_dprintf_commands, NULL,
15900 &setlist, &showlist);
15901
15902 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15903 &disconnected_dprintf, _("\
15904 Set whether dprintf continues after GDB disconnects."), _("\
15905 Show whether dprintf continues after GDB disconnects."), _("\
15906 Use this to let dprintf commands continue to hit and produce output\n\
15907 even if GDB disconnects or detaches from the target."),
15908 NULL,
15909 NULL,
15910 &setlist, &showlist);
15911
15912 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15913 Target agent only formatted printing, like the C \"printf\" function.\n\
15914 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15915 This supports most C printf format specifications, like %s, %d, etc.\n\
15916 This is useful for formatted output in user-defined commands."));
15917
15918 automatic_hardware_breakpoints = true;
15919
15920 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
15921 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
15922 }