Multi-target support
[deliverable/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 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1810 && TYPE_CODE (vtype) != 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 (get_objfile_arch (objfile), 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 struct program_space *pspace;
3228
3229 scoped_restore_current_program_space restore_pspace;
3230
3231 ALL_PSPACES (pspace)
3232 {
3233 set_current_program_space (pspace);
3234
3235 for (objfile *objfile : current_program_space->objfiles ())
3236 {
3237 int i;
3238 struct gdbarch *gdbarch;
3239 struct breakpoint_objfile_data *bp_objfile_data;
3240
3241 gdbarch = get_objfile_arch (objfile);
3242
3243 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3244
3245 if (!bp_objfile_data->longjmp_searched)
3246 {
3247 std::vector<probe *> ret
3248 = find_probes_in_objfile (objfile, "libc", "longjmp");
3249
3250 if (!ret.empty ())
3251 {
3252 /* We are only interested in checking one element. */
3253 probe *p = ret[0];
3254
3255 if (!p->can_evaluate_arguments ())
3256 {
3257 /* We cannot use the probe interface here, because it does
3258 not know how to evaluate arguments. */
3259 ret.clear ();
3260 }
3261 }
3262 bp_objfile_data->longjmp_probes = ret;
3263 bp_objfile_data->longjmp_searched = 1;
3264 }
3265
3266 if (!bp_objfile_data->longjmp_probes.empty ())
3267 {
3268 for (probe *p : bp_objfile_data->longjmp_probes)
3269 {
3270 struct breakpoint *b;
3271
3272 b = create_internal_breakpoint (gdbarch,
3273 p->get_relocated_address (objfile),
3274 bp_longjmp_master,
3275 &internal_breakpoint_ops);
3276 b->location = new_probe_location ("-probe-stap libc:longjmp");
3277 b->enable_state = bp_disabled;
3278 }
3279
3280 continue;
3281 }
3282
3283 if (!gdbarch_get_longjmp_target_p (gdbarch))
3284 continue;
3285
3286 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3287 {
3288 struct breakpoint *b;
3289 const char *func_name;
3290 CORE_ADDR addr;
3291 struct explicit_location explicit_loc;
3292
3293 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3294 continue;
3295
3296 func_name = longjmp_names[i];
3297 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3298 {
3299 struct bound_minimal_symbol m;
3300
3301 m = lookup_minimal_symbol_text (func_name, objfile);
3302 if (m.minsym == NULL)
3303 {
3304 /* Prevent future lookups in this objfile. */
3305 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3306 continue;
3307 }
3308 bp_objfile_data->longjmp_msym[i] = m;
3309 }
3310
3311 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3312 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3313 &internal_breakpoint_ops);
3314 initialize_explicit_location (&explicit_loc);
3315 explicit_loc.function_name = ASTRDUP (func_name);
3316 b->location = new_explicit_location (&explicit_loc);
3317 b->enable_state = bp_disabled;
3318 }
3319 }
3320 }
3321 }
3322
3323 /* Create a master std::terminate breakpoint. */
3324 static void
3325 create_std_terminate_master_breakpoint (void)
3326 {
3327 struct program_space *pspace;
3328 const char *const func_name = "std::terminate()";
3329
3330 scoped_restore_current_program_space restore_pspace;
3331
3332 ALL_PSPACES (pspace)
3333 {
3334 CORE_ADDR addr;
3335
3336 set_current_program_space (pspace);
3337
3338 for (objfile *objfile : current_program_space->objfiles ())
3339 {
3340 struct breakpoint *b;
3341 struct breakpoint_objfile_data *bp_objfile_data;
3342 struct explicit_location explicit_loc;
3343
3344 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3345
3346 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3347 continue;
3348
3349 if (bp_objfile_data->terminate_msym.minsym == NULL)
3350 {
3351 struct bound_minimal_symbol m;
3352
3353 m = lookup_minimal_symbol (func_name, NULL, objfile);
3354 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3355 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3356 {
3357 /* Prevent future lookups in this objfile. */
3358 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3359 continue;
3360 }
3361 bp_objfile_data->terminate_msym = m;
3362 }
3363
3364 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3365 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3366 bp_std_terminate_master,
3367 &internal_breakpoint_ops);
3368 initialize_explicit_location (&explicit_loc);
3369 explicit_loc.function_name = ASTRDUP (func_name);
3370 b->location = new_explicit_location (&explicit_loc);
3371 b->enable_state = bp_disabled;
3372 }
3373 }
3374 }
3375
3376 /* Install a master breakpoint on the unwinder's debug hook. */
3377
3378 static void
3379 create_exception_master_breakpoint (void)
3380 {
3381 const char *const func_name = "_Unwind_DebugHook";
3382
3383 for (objfile *objfile : current_program_space->objfiles ())
3384 {
3385 struct breakpoint *b;
3386 struct gdbarch *gdbarch;
3387 struct breakpoint_objfile_data *bp_objfile_data;
3388 CORE_ADDR addr;
3389 struct explicit_location explicit_loc;
3390
3391 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3392
3393 /* We prefer the SystemTap probe point if it exists. */
3394 if (!bp_objfile_data->exception_searched)
3395 {
3396 std::vector<probe *> ret
3397 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3398
3399 if (!ret.empty ())
3400 {
3401 /* We are only interested in checking one element. */
3402 probe *p = ret[0];
3403
3404 if (!p->can_evaluate_arguments ())
3405 {
3406 /* We cannot use the probe interface here, because it does
3407 not know how to evaluate arguments. */
3408 ret.clear ();
3409 }
3410 }
3411 bp_objfile_data->exception_probes = ret;
3412 bp_objfile_data->exception_searched = 1;
3413 }
3414
3415 if (!bp_objfile_data->exception_probes.empty ())
3416 {
3417 gdbarch = get_objfile_arch (objfile);
3418
3419 for (probe *p : bp_objfile_data->exception_probes)
3420 {
3421 b = create_internal_breakpoint (gdbarch,
3422 p->get_relocated_address (objfile),
3423 bp_exception_master,
3424 &internal_breakpoint_ops);
3425 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3426 b->enable_state = bp_disabled;
3427 }
3428
3429 continue;
3430 }
3431
3432 /* Otherwise, try the hook function. */
3433
3434 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3435 continue;
3436
3437 gdbarch = get_objfile_arch (objfile);
3438
3439 if (bp_objfile_data->exception_msym.minsym == NULL)
3440 {
3441 struct bound_minimal_symbol debug_hook;
3442
3443 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3444 if (debug_hook.minsym == NULL)
3445 {
3446 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3447 continue;
3448 }
3449
3450 bp_objfile_data->exception_msym = debug_hook;
3451 }
3452
3453 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3454 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3455 current_top_target ());
3456 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3457 &internal_breakpoint_ops);
3458 initialize_explicit_location (&explicit_loc);
3459 explicit_loc.function_name = ASTRDUP (func_name);
3460 b->location = new_explicit_location (&explicit_loc);
3461 b->enable_state = bp_disabled;
3462 }
3463 }
3464
3465 /* Does B have a location spec? */
3466
3467 static int
3468 breakpoint_event_location_empty_p (const struct breakpoint *b)
3469 {
3470 return b->location != NULL && event_location_empty_p (b->location.get ());
3471 }
3472
3473 void
3474 update_breakpoints_after_exec (void)
3475 {
3476 struct breakpoint *b, *b_tmp;
3477 struct bp_location *bploc, **bplocp_tmp;
3478
3479 /* We're about to delete breakpoints from GDB's lists. If the
3480 INSERTED flag is true, GDB will try to lift the breakpoints by
3481 writing the breakpoints' "shadow contents" back into memory. The
3482 "shadow contents" are NOT valid after an exec, so GDB should not
3483 do that. Instead, the target is responsible from marking
3484 breakpoints out as soon as it detects an exec. We don't do that
3485 here instead, because there may be other attempts to delete
3486 breakpoints after detecting an exec and before reaching here. */
3487 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3488 if (bploc->pspace == current_program_space)
3489 gdb_assert (!bploc->inserted);
3490
3491 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3492 {
3493 if (b->pspace != current_program_space)
3494 continue;
3495
3496 /* Solib breakpoints must be explicitly reset after an exec(). */
3497 if (b->type == bp_shlib_event)
3498 {
3499 delete_breakpoint (b);
3500 continue;
3501 }
3502
3503 /* JIT breakpoints must be explicitly reset after an exec(). */
3504 if (b->type == bp_jit_event)
3505 {
3506 delete_breakpoint (b);
3507 continue;
3508 }
3509
3510 /* Thread event breakpoints must be set anew after an exec(),
3511 as must overlay event and longjmp master breakpoints. */
3512 if (b->type == bp_thread_event || b->type == bp_overlay_event
3513 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3514 || b->type == bp_exception_master)
3515 {
3516 delete_breakpoint (b);
3517 continue;
3518 }
3519
3520 /* Step-resume breakpoints are meaningless after an exec(). */
3521 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3522 {
3523 delete_breakpoint (b);
3524 continue;
3525 }
3526
3527 /* Just like single-step breakpoints. */
3528 if (b->type == bp_single_step)
3529 {
3530 delete_breakpoint (b);
3531 continue;
3532 }
3533
3534 /* Longjmp and longjmp-resume breakpoints are also meaningless
3535 after an exec. */
3536 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3537 || b->type == bp_longjmp_call_dummy
3538 || b->type == bp_exception || b->type == bp_exception_resume)
3539 {
3540 delete_breakpoint (b);
3541 continue;
3542 }
3543
3544 if (b->type == bp_catchpoint)
3545 {
3546 /* For now, none of the bp_catchpoint breakpoints need to
3547 do anything at this point. In the future, if some of
3548 the catchpoints need to something, we will need to add
3549 a new method, and call this method from here. */
3550 continue;
3551 }
3552
3553 /* bp_finish is a special case. The only way we ought to be able
3554 to see one of these when an exec() has happened, is if the user
3555 caught a vfork, and then said "finish". Ordinarily a finish just
3556 carries them to the call-site of the current callee, by setting
3557 a temporary bp there and resuming. But in this case, the finish
3558 will carry them entirely through the vfork & exec.
3559
3560 We don't want to allow a bp_finish to remain inserted now. But
3561 we can't safely delete it, 'cause finish_command has a handle to
3562 the bp on a bpstat, and will later want to delete it. There's a
3563 chance (and I've seen it happen) that if we delete the bp_finish
3564 here, that its storage will get reused by the time finish_command
3565 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3566 We really must allow finish_command to delete a bp_finish.
3567
3568 In the absence of a general solution for the "how do we know
3569 it's safe to delete something others may have handles to?"
3570 problem, what we'll do here is just uninsert the bp_finish, and
3571 let finish_command delete it.
3572
3573 (We know the bp_finish is "doomed" in the sense that it's
3574 momentary, and will be deleted as soon as finish_command sees
3575 the inferior stopped. So it doesn't matter that the bp's
3576 address is probably bogus in the new a.out, unlike e.g., the
3577 solib breakpoints.) */
3578
3579 if (b->type == bp_finish)
3580 {
3581 continue;
3582 }
3583
3584 /* Without a symbolic address, we have little hope of the
3585 pre-exec() address meaning the same thing in the post-exec()
3586 a.out. */
3587 if (breakpoint_event_location_empty_p (b))
3588 {
3589 delete_breakpoint (b);
3590 continue;
3591 }
3592 }
3593 }
3594
3595 int
3596 detach_breakpoints (ptid_t ptid)
3597 {
3598 struct bp_location *bl, **blp_tmp;
3599 int val = 0;
3600 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3601 struct inferior *inf = current_inferior ();
3602
3603 if (ptid.pid () == inferior_ptid.pid ())
3604 error (_("Cannot detach breakpoints of inferior_ptid"));
3605
3606 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3607 inferior_ptid = ptid;
3608 ALL_BP_LOCATIONS (bl, blp_tmp)
3609 {
3610 if (bl->pspace != inf->pspace)
3611 continue;
3612
3613 /* This function must physically remove breakpoints locations
3614 from the specified ptid, without modifying the breakpoint
3615 package's state. Locations of type bp_loc_other are only
3616 maintained at GDB side. So, there is no need to remove
3617 these bp_loc_other locations. Moreover, removing these
3618 would modify the breakpoint package's state. */
3619 if (bl->loc_type == bp_loc_other)
3620 continue;
3621
3622 if (bl->inserted)
3623 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3624 }
3625
3626 return val;
3627 }
3628
3629 /* Remove the breakpoint location BL from the current address space.
3630 Note that this is used to detach breakpoints from a child fork.
3631 When we get here, the child isn't in the inferior list, and neither
3632 do we have objects to represent its address space --- we should
3633 *not* look at bl->pspace->aspace here. */
3634
3635 static int
3636 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3637 {
3638 int val;
3639
3640 /* BL is never in moribund_locations by our callers. */
3641 gdb_assert (bl->owner != NULL);
3642
3643 /* The type of none suggests that owner is actually deleted.
3644 This should not ever happen. */
3645 gdb_assert (bl->owner->type != bp_none);
3646
3647 if (bl->loc_type == bp_loc_software_breakpoint
3648 || bl->loc_type == bp_loc_hardware_breakpoint)
3649 {
3650 /* "Normal" instruction breakpoint: either the standard
3651 trap-instruction bp (bp_breakpoint), or a
3652 bp_hardware_breakpoint. */
3653
3654 /* First check to see if we have to handle an overlay. */
3655 if (overlay_debugging == ovly_off
3656 || bl->section == NULL
3657 || !(section_is_overlay (bl->section)))
3658 {
3659 /* No overlay handling: just remove the breakpoint. */
3660
3661 /* If we're trying to uninsert a memory breakpoint that we
3662 know is set in a dynamic object that is marked
3663 shlib_disabled, then either the dynamic object was
3664 removed with "remove-symbol-file" or with
3665 "nosharedlibrary". In the former case, we don't know
3666 whether another dynamic object might have loaded over the
3667 breakpoint's address -- the user might well let us know
3668 about it next with add-symbol-file (the whole point of
3669 add-symbol-file is letting the user manually maintain a
3670 list of dynamically loaded objects). If we have the
3671 breakpoint's shadow memory, that is, this is a software
3672 breakpoint managed by GDB, check whether the breakpoint
3673 is still inserted in memory, to avoid overwriting wrong
3674 code with stale saved shadow contents. Note that HW
3675 breakpoints don't have shadow memory, as they're
3676 implemented using a mechanism that is not dependent on
3677 being able to modify the target's memory, and as such
3678 they should always be removed. */
3679 if (bl->shlib_disabled
3680 && bl->target_info.shadow_len != 0
3681 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3682 val = 0;
3683 else
3684 val = bl->owner->ops->remove_location (bl, reason);
3685 }
3686 else
3687 {
3688 /* This breakpoint is in an overlay section.
3689 Did we set a breakpoint at the LMA? */
3690 if (!overlay_events_enabled)
3691 {
3692 /* Yes -- overlay event support is not active, so we
3693 should have set a breakpoint at the LMA. Remove it.
3694 */
3695 /* Ignore any failures: if the LMA is in ROM, we will
3696 have already warned when we failed to insert it. */
3697 if (bl->loc_type == bp_loc_hardware_breakpoint)
3698 target_remove_hw_breakpoint (bl->gdbarch,
3699 &bl->overlay_target_info);
3700 else
3701 target_remove_breakpoint (bl->gdbarch,
3702 &bl->overlay_target_info,
3703 reason);
3704 }
3705 /* Did we set a breakpoint at the VMA?
3706 If so, we will have marked the breakpoint 'inserted'. */
3707 if (bl->inserted)
3708 {
3709 /* Yes -- remove it. Previously we did not bother to
3710 remove the breakpoint if the section had been
3711 unmapped, but let's not rely on that being safe. We
3712 don't know what the overlay manager might do. */
3713
3714 /* However, we should remove *software* breakpoints only
3715 if the section is still mapped, or else we overwrite
3716 wrong code with the saved shadow contents. */
3717 if (bl->loc_type == bp_loc_hardware_breakpoint
3718 || section_is_mapped (bl->section))
3719 val = bl->owner->ops->remove_location (bl, reason);
3720 else
3721 val = 0;
3722 }
3723 else
3724 {
3725 /* No -- not inserted, so no need to remove. No error. */
3726 val = 0;
3727 }
3728 }
3729
3730 /* In some cases, we might not be able to remove a breakpoint in
3731 a shared library that has already been removed, but we have
3732 not yet processed the shlib unload event. Similarly for an
3733 unloaded add-symbol-file object - the user might not yet have
3734 had the chance to remove-symbol-file it. shlib_disabled will
3735 be set if the library/object has already been removed, but
3736 the breakpoint hasn't been uninserted yet, e.g., after
3737 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3738 always-inserted mode. */
3739 if (val
3740 && (bl->loc_type == bp_loc_software_breakpoint
3741 && (bl->shlib_disabled
3742 || solib_name_from_address (bl->pspace, bl->address)
3743 || shared_objfile_contains_address_p (bl->pspace,
3744 bl->address))))
3745 val = 0;
3746
3747 if (val)
3748 return val;
3749 bl->inserted = (reason == DETACH_BREAKPOINT);
3750 }
3751 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3752 {
3753 gdb_assert (bl->owner->ops != NULL
3754 && bl->owner->ops->remove_location != NULL);
3755
3756 bl->inserted = (reason == DETACH_BREAKPOINT);
3757 bl->owner->ops->remove_location (bl, reason);
3758
3759 /* Failure to remove any of the hardware watchpoints comes here. */
3760 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3761 warning (_("Could not remove hardware watchpoint %d."),
3762 bl->owner->number);
3763 }
3764 else if (bl->owner->type == bp_catchpoint
3765 && breakpoint_enabled (bl->owner)
3766 && !bl->duplicate)
3767 {
3768 gdb_assert (bl->owner->ops != NULL
3769 && bl->owner->ops->remove_location != NULL);
3770
3771 val = bl->owner->ops->remove_location (bl, reason);
3772 if (val)
3773 return val;
3774
3775 bl->inserted = (reason == DETACH_BREAKPOINT);
3776 }
3777
3778 return 0;
3779 }
3780
3781 static int
3782 remove_breakpoint (struct bp_location *bl)
3783 {
3784 /* BL is never in moribund_locations by our callers. */
3785 gdb_assert (bl->owner != NULL);
3786
3787 /* The type of none suggests that owner is actually deleted.
3788 This should not ever happen. */
3789 gdb_assert (bl->owner->type != bp_none);
3790
3791 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3792
3793 switch_to_program_space_and_thread (bl->pspace);
3794
3795 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3796 }
3797
3798 /* Clear the "inserted" flag in all breakpoints. */
3799
3800 void
3801 mark_breakpoints_out (void)
3802 {
3803 struct bp_location *bl, **blp_tmp;
3804
3805 ALL_BP_LOCATIONS (bl, blp_tmp)
3806 if (bl->pspace == current_program_space)
3807 bl->inserted = 0;
3808 }
3809
3810 /* Clear the "inserted" flag in all breakpoints and delete any
3811 breakpoints which should go away between runs of the program.
3812
3813 Plus other such housekeeping that has to be done for breakpoints
3814 between runs.
3815
3816 Note: this function gets called at the end of a run (by
3817 generic_mourn_inferior) and when a run begins (by
3818 init_wait_for_inferior). */
3819
3820
3821
3822 void
3823 breakpoint_init_inferior (enum inf_context context)
3824 {
3825 struct breakpoint *b, *b_tmp;
3826 struct program_space *pspace = current_program_space;
3827
3828 /* If breakpoint locations are shared across processes, then there's
3829 nothing to do. */
3830 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3831 return;
3832
3833 mark_breakpoints_out ();
3834
3835 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3836 {
3837 if (b->loc && b->loc->pspace != pspace)
3838 continue;
3839
3840 switch (b->type)
3841 {
3842 case bp_call_dummy:
3843 case bp_longjmp_call_dummy:
3844
3845 /* If the call dummy breakpoint is at the entry point it will
3846 cause problems when the inferior is rerun, so we better get
3847 rid of it. */
3848
3849 case bp_watchpoint_scope:
3850
3851 /* Also get rid of scope breakpoints. */
3852
3853 case bp_shlib_event:
3854
3855 /* Also remove solib event breakpoints. Their addresses may
3856 have changed since the last time we ran the program.
3857 Actually we may now be debugging against different target;
3858 and so the solib backend that installed this breakpoint may
3859 not be used in by the target. E.g.,
3860
3861 (gdb) file prog-linux
3862 (gdb) run # native linux target
3863 ...
3864 (gdb) kill
3865 (gdb) file prog-win.exe
3866 (gdb) tar rem :9999 # remote Windows gdbserver.
3867 */
3868
3869 case bp_step_resume:
3870
3871 /* Also remove step-resume breakpoints. */
3872
3873 case bp_single_step:
3874
3875 /* Also remove single-step breakpoints. */
3876
3877 delete_breakpoint (b);
3878 break;
3879
3880 case bp_watchpoint:
3881 case bp_hardware_watchpoint:
3882 case bp_read_watchpoint:
3883 case bp_access_watchpoint:
3884 {
3885 struct watchpoint *w = (struct watchpoint *) b;
3886
3887 /* Likewise for watchpoints on local expressions. */
3888 if (w->exp_valid_block != NULL)
3889 delete_breakpoint (b);
3890 else
3891 {
3892 /* Get rid of existing locations, which are no longer
3893 valid. New ones will be created in
3894 update_watchpoint, when the inferior is restarted.
3895 The next update_global_location_list call will
3896 garbage collect them. */
3897 b->loc = NULL;
3898
3899 if (context == inf_starting)
3900 {
3901 /* Reset val field to force reread of starting value in
3902 insert_breakpoints. */
3903 w->val.reset (nullptr);
3904 w->val_valid = false;
3905 }
3906 }
3907 }
3908 break;
3909 default:
3910 break;
3911 }
3912 }
3913
3914 /* Get rid of the moribund locations. */
3915 for (bp_location *bl : moribund_locations)
3916 decref_bp_location (&bl);
3917 moribund_locations.clear ();
3918 }
3919
3920 /* These functions concern about actual breakpoints inserted in the
3921 target --- to e.g. check if we need to do decr_pc adjustment or if
3922 we need to hop over the bkpt --- so we check for address space
3923 match, not program space. */
3924
3925 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3926 exists at PC. It returns ordinary_breakpoint_here if it's an
3927 ordinary breakpoint, or permanent_breakpoint_here if it's a
3928 permanent breakpoint.
3929 - When continuing from a location with an ordinary breakpoint, we
3930 actually single step once before calling insert_breakpoints.
3931 - When continuing from a location with a permanent breakpoint, we
3932 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3933 the target, to advance the PC past the breakpoint. */
3934
3935 enum breakpoint_here
3936 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3937 {
3938 struct bp_location *bl, **blp_tmp;
3939 int any_breakpoint_here = 0;
3940
3941 ALL_BP_LOCATIONS (bl, blp_tmp)
3942 {
3943 if (bl->loc_type != bp_loc_software_breakpoint
3944 && bl->loc_type != bp_loc_hardware_breakpoint)
3945 continue;
3946
3947 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3948 if ((breakpoint_enabled (bl->owner)
3949 || bl->permanent)
3950 && breakpoint_location_address_match (bl, aspace, pc))
3951 {
3952 if (overlay_debugging
3953 && section_is_overlay (bl->section)
3954 && !section_is_mapped (bl->section))
3955 continue; /* unmapped overlay -- can't be a match */
3956 else if (bl->permanent)
3957 return permanent_breakpoint_here;
3958 else
3959 any_breakpoint_here = 1;
3960 }
3961 }
3962
3963 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
3964 }
3965
3966 /* See breakpoint.h. */
3967
3968 int
3969 breakpoint_in_range_p (const address_space *aspace,
3970 CORE_ADDR addr, ULONGEST len)
3971 {
3972 struct bp_location *bl, **blp_tmp;
3973
3974 ALL_BP_LOCATIONS (bl, blp_tmp)
3975 {
3976 if (bl->loc_type != bp_loc_software_breakpoint
3977 && bl->loc_type != bp_loc_hardware_breakpoint)
3978 continue;
3979
3980 if ((breakpoint_enabled (bl->owner)
3981 || bl->permanent)
3982 && breakpoint_location_address_range_overlap (bl, aspace,
3983 addr, len))
3984 {
3985 if (overlay_debugging
3986 && section_is_overlay (bl->section)
3987 && !section_is_mapped (bl->section))
3988 {
3989 /* Unmapped overlay -- can't be a match. */
3990 continue;
3991 }
3992
3993 return 1;
3994 }
3995 }
3996
3997 return 0;
3998 }
3999
4000 /* Return true if there's a moribund breakpoint at PC. */
4001
4002 int
4003 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4004 {
4005 for (bp_location *loc : moribund_locations)
4006 if (breakpoint_location_address_match (loc, aspace, pc))
4007 return 1;
4008
4009 return 0;
4010 }
4011
4012 /* Returns non-zero iff BL is inserted at PC, in address space
4013 ASPACE. */
4014
4015 static int
4016 bp_location_inserted_here_p (struct bp_location *bl,
4017 const address_space *aspace, CORE_ADDR pc)
4018 {
4019 if (bl->inserted
4020 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4021 aspace, pc))
4022 {
4023 if (overlay_debugging
4024 && section_is_overlay (bl->section)
4025 && !section_is_mapped (bl->section))
4026 return 0; /* unmapped overlay -- can't be a match */
4027 else
4028 return 1;
4029 }
4030 return 0;
4031 }
4032
4033 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4034
4035 int
4036 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4037 {
4038 struct bp_location **blp, **blp_tmp = NULL;
4039
4040 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4041 {
4042 struct bp_location *bl = *blp;
4043
4044 if (bl->loc_type != bp_loc_software_breakpoint
4045 && bl->loc_type != bp_loc_hardware_breakpoint)
4046 continue;
4047
4048 if (bp_location_inserted_here_p (bl, aspace, pc))
4049 return 1;
4050 }
4051 return 0;
4052 }
4053
4054 /* This function returns non-zero iff there is a software breakpoint
4055 inserted at PC. */
4056
4057 int
4058 software_breakpoint_inserted_here_p (const address_space *aspace,
4059 CORE_ADDR pc)
4060 {
4061 struct bp_location **blp, **blp_tmp = NULL;
4062
4063 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4064 {
4065 struct bp_location *bl = *blp;
4066
4067 if (bl->loc_type != bp_loc_software_breakpoint)
4068 continue;
4069
4070 if (bp_location_inserted_here_p (bl, aspace, pc))
4071 return 1;
4072 }
4073
4074 return 0;
4075 }
4076
4077 /* See breakpoint.h. */
4078
4079 int
4080 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4081 CORE_ADDR pc)
4082 {
4083 struct bp_location **blp, **blp_tmp = NULL;
4084
4085 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4086 {
4087 struct bp_location *bl = *blp;
4088
4089 if (bl->loc_type != bp_loc_hardware_breakpoint)
4090 continue;
4091
4092 if (bp_location_inserted_here_p (bl, aspace, pc))
4093 return 1;
4094 }
4095
4096 return 0;
4097 }
4098
4099 int
4100 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4101 CORE_ADDR addr, ULONGEST len)
4102 {
4103 struct breakpoint *bpt;
4104
4105 ALL_BREAKPOINTS (bpt)
4106 {
4107 struct bp_location *loc;
4108
4109 if (bpt->type != bp_hardware_watchpoint
4110 && bpt->type != bp_access_watchpoint)
4111 continue;
4112
4113 if (!breakpoint_enabled (bpt))
4114 continue;
4115
4116 for (loc = bpt->loc; loc; loc = loc->next)
4117 if (loc->pspace->aspace == aspace && loc->inserted)
4118 {
4119 CORE_ADDR l, h;
4120
4121 /* Check for intersection. */
4122 l = std::max<CORE_ADDR> (loc->address, addr);
4123 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4124 if (l < h)
4125 return 1;
4126 }
4127 }
4128 return 0;
4129 }
4130
4131 /* See breakpoint.h. */
4132
4133 bool
4134 is_catchpoint (struct breakpoint *b)
4135 {
4136 return (b->type == bp_catchpoint);
4137 }
4138
4139 /* Frees any storage that is part of a bpstat. Does not walk the
4140 'next' chain. */
4141
4142 bpstats::~bpstats ()
4143 {
4144 if (bp_location_at != NULL)
4145 decref_bp_location (&bp_location_at);
4146 }
4147
4148 /* Clear a bpstat so that it says we are not at any breakpoint.
4149 Also free any storage that is part of a bpstat. */
4150
4151 void
4152 bpstat_clear (bpstat *bsp)
4153 {
4154 bpstat p;
4155 bpstat q;
4156
4157 if (bsp == 0)
4158 return;
4159 p = *bsp;
4160 while (p != NULL)
4161 {
4162 q = p->next;
4163 delete p;
4164 p = q;
4165 }
4166 *bsp = NULL;
4167 }
4168
4169 bpstats::bpstats (const bpstats &other)
4170 : next (NULL),
4171 bp_location_at (other.bp_location_at),
4172 breakpoint_at (other.breakpoint_at),
4173 commands (other.commands),
4174 print (other.print),
4175 stop (other.stop),
4176 print_it (other.print_it)
4177 {
4178 if (other.old_val != NULL)
4179 old_val = release_value (value_copy (other.old_val.get ()));
4180 incref_bp_location (bp_location_at);
4181 }
4182
4183 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4184 is part of the bpstat is copied as well. */
4185
4186 bpstat
4187 bpstat_copy (bpstat bs)
4188 {
4189 bpstat p = NULL;
4190 bpstat tmp;
4191 bpstat retval = NULL;
4192
4193 if (bs == NULL)
4194 return bs;
4195
4196 for (; bs != NULL; bs = bs->next)
4197 {
4198 tmp = new bpstats (*bs);
4199
4200 if (p == NULL)
4201 /* This is the first thing in the chain. */
4202 retval = tmp;
4203 else
4204 p->next = tmp;
4205 p = tmp;
4206 }
4207 p->next = NULL;
4208 return retval;
4209 }
4210
4211 /* Find the bpstat associated with this breakpoint. */
4212
4213 bpstat
4214 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4215 {
4216 if (bsp == NULL)
4217 return NULL;
4218
4219 for (; bsp != NULL; bsp = bsp->next)
4220 {
4221 if (bsp->breakpoint_at == breakpoint)
4222 return bsp;
4223 }
4224 return NULL;
4225 }
4226
4227 /* See breakpoint.h. */
4228
4229 bool
4230 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4231 {
4232 for (; bsp != NULL; bsp = bsp->next)
4233 {
4234 if (bsp->breakpoint_at == NULL)
4235 {
4236 /* A moribund location can never explain a signal other than
4237 GDB_SIGNAL_TRAP. */
4238 if (sig == GDB_SIGNAL_TRAP)
4239 return true;
4240 }
4241 else
4242 {
4243 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4244 sig))
4245 return true;
4246 }
4247 }
4248
4249 return false;
4250 }
4251
4252 /* Put in *NUM the breakpoint number of the first breakpoint we are
4253 stopped at. *BSP upon return is a bpstat which points to the
4254 remaining breakpoints stopped at (but which is not guaranteed to be
4255 good for anything but further calls to bpstat_num).
4256
4257 Return 0 if passed a bpstat which does not indicate any breakpoints.
4258 Return -1 if stopped at a breakpoint that has been deleted since
4259 we set it.
4260 Return 1 otherwise. */
4261
4262 int
4263 bpstat_num (bpstat *bsp, int *num)
4264 {
4265 struct breakpoint *b;
4266
4267 if ((*bsp) == NULL)
4268 return 0; /* No more breakpoint values */
4269
4270 /* We assume we'll never have several bpstats that correspond to a
4271 single breakpoint -- otherwise, this function might return the
4272 same number more than once and this will look ugly. */
4273 b = (*bsp)->breakpoint_at;
4274 *bsp = (*bsp)->next;
4275 if (b == NULL)
4276 return -1; /* breakpoint that's been deleted since */
4277
4278 *num = b->number; /* We have its number */
4279 return 1;
4280 }
4281
4282 /* See breakpoint.h. */
4283
4284 void
4285 bpstat_clear_actions (void)
4286 {
4287 bpstat bs;
4288
4289 if (inferior_ptid == null_ptid)
4290 return;
4291
4292 thread_info *tp = inferior_thread ();
4293 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4294 {
4295 bs->commands = NULL;
4296 bs->old_val.reset (nullptr);
4297 }
4298 }
4299
4300 /* Called when a command is about to proceed the inferior. */
4301
4302 static void
4303 breakpoint_about_to_proceed (void)
4304 {
4305 if (inferior_ptid != null_ptid)
4306 {
4307 struct thread_info *tp = inferior_thread ();
4308
4309 /* Allow inferior function calls in breakpoint commands to not
4310 interrupt the command list. When the call finishes
4311 successfully, the inferior will be standing at the same
4312 breakpoint as if nothing happened. */
4313 if (tp->control.in_infcall)
4314 return;
4315 }
4316
4317 breakpoint_proceeded = 1;
4318 }
4319
4320 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4321 or its equivalent. */
4322
4323 static int
4324 command_line_is_silent (struct command_line *cmd)
4325 {
4326 return cmd && (strcmp ("silent", cmd->line) == 0);
4327 }
4328
4329 /* Execute all the commands associated with all the breakpoints at
4330 this location. Any of these commands could cause the process to
4331 proceed beyond this point, etc. We look out for such changes by
4332 checking the global "breakpoint_proceeded" after each command.
4333
4334 Returns true if a breakpoint command resumed the inferior. In that
4335 case, it is the caller's responsibility to recall it again with the
4336 bpstat of the current thread. */
4337
4338 static int
4339 bpstat_do_actions_1 (bpstat *bsp)
4340 {
4341 bpstat bs;
4342 int again = 0;
4343
4344 /* Avoid endless recursion if a `source' command is contained
4345 in bs->commands. */
4346 if (executing_breakpoint_commands)
4347 return 0;
4348
4349 scoped_restore save_executing
4350 = make_scoped_restore (&executing_breakpoint_commands, 1);
4351
4352 scoped_restore preventer = prevent_dont_repeat ();
4353
4354 /* This pointer will iterate over the list of bpstat's. */
4355 bs = *bsp;
4356
4357 breakpoint_proceeded = 0;
4358 for (; bs != NULL; bs = bs->next)
4359 {
4360 struct command_line *cmd = NULL;
4361
4362 /* Take ownership of the BSP's command tree, if it has one.
4363
4364 The command tree could legitimately contain commands like
4365 'step' and 'next', which call clear_proceed_status, which
4366 frees stop_bpstat's command tree. To make sure this doesn't
4367 free the tree we're executing out from under us, we need to
4368 take ownership of the tree ourselves. Since a given bpstat's
4369 commands are only executed once, we don't need to copy it; we
4370 can clear the pointer in the bpstat, and make sure we free
4371 the tree when we're done. */
4372 counted_command_line ccmd = bs->commands;
4373 bs->commands = NULL;
4374 if (ccmd != NULL)
4375 cmd = ccmd.get ();
4376 if (command_line_is_silent (cmd))
4377 {
4378 /* The action has been already done by bpstat_stop_status. */
4379 cmd = cmd->next;
4380 }
4381
4382 while (cmd != NULL)
4383 {
4384 execute_control_command (cmd);
4385
4386 if (breakpoint_proceeded)
4387 break;
4388 else
4389 cmd = cmd->next;
4390 }
4391
4392 if (breakpoint_proceeded)
4393 {
4394 if (current_ui->async)
4395 /* If we are in async mode, then the target might be still
4396 running, not stopped at any breakpoint, so nothing for
4397 us to do here -- just return to the event loop. */
4398 ;
4399 else
4400 /* In sync mode, when execute_control_command returns
4401 we're already standing on the next breakpoint.
4402 Breakpoint commands for that stop were not run, since
4403 execute_command does not run breakpoint commands --
4404 only command_line_handler does, but that one is not
4405 involved in execution of breakpoint commands. So, we
4406 can now execute breakpoint commands. It should be
4407 noted that making execute_command do bpstat actions is
4408 not an option -- in this case we'll have recursive
4409 invocation of bpstat for each breakpoint with a
4410 command, and can easily blow up GDB stack. Instead, we
4411 return true, which will trigger the caller to recall us
4412 with the new stop_bpstat. */
4413 again = 1;
4414 break;
4415 }
4416 }
4417 return again;
4418 }
4419
4420 /* Helper for bpstat_do_actions. Get the current thread, if there's
4421 one, is alive and has execution. Return NULL otherwise. */
4422
4423 static thread_info *
4424 get_bpstat_thread ()
4425 {
4426 if (inferior_ptid == null_ptid || !target_has_execution)
4427 return NULL;
4428
4429 thread_info *tp = inferior_thread ();
4430 if (tp->state == THREAD_EXITED || tp->executing)
4431 return NULL;
4432 return tp;
4433 }
4434
4435 void
4436 bpstat_do_actions (void)
4437 {
4438 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4439 thread_info *tp;
4440
4441 /* Do any commands attached to breakpoint we are stopped at. */
4442 while ((tp = get_bpstat_thread ()) != NULL)
4443 {
4444 /* Since in sync mode, bpstat_do_actions may resume the
4445 inferior, and only return when it is stopped at the next
4446 breakpoint, we keep doing breakpoint actions until it returns
4447 false to indicate the inferior was not resumed. */
4448 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4449 break;
4450 }
4451
4452 cleanup_if_error.release ();
4453 }
4454
4455 /* Print out the (old or new) value associated with a watchpoint. */
4456
4457 static void
4458 watchpoint_value_print (struct value *val, struct ui_file *stream)
4459 {
4460 if (val == NULL)
4461 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4462 else
4463 {
4464 struct value_print_options opts;
4465 get_user_print_options (&opts);
4466 value_print (val, stream, &opts);
4467 }
4468 }
4469
4470 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4471 debugging multiple threads. */
4472
4473 void
4474 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4475 {
4476 if (uiout->is_mi_like_p ())
4477 return;
4478
4479 uiout->text ("\n");
4480
4481 if (show_thread_that_caused_stop ())
4482 {
4483 const char *name;
4484 struct thread_info *thr = inferior_thread ();
4485
4486 uiout->text ("Thread ");
4487 uiout->field_string ("thread-id", print_thread_id (thr));
4488
4489 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4490 if (name != NULL)
4491 {
4492 uiout->text (" \"");
4493 uiout->field_string ("name", name);
4494 uiout->text ("\"");
4495 }
4496
4497 uiout->text (" hit ");
4498 }
4499 }
4500
4501 /* Generic routine for printing messages indicating why we
4502 stopped. The behavior of this function depends on the value
4503 'print_it' in the bpstat structure. Under some circumstances we
4504 may decide not to print anything here and delegate the task to
4505 normal_stop(). */
4506
4507 static enum print_stop_action
4508 print_bp_stop_message (bpstat bs)
4509 {
4510 switch (bs->print_it)
4511 {
4512 case print_it_noop:
4513 /* Nothing should be printed for this bpstat entry. */
4514 return PRINT_UNKNOWN;
4515 break;
4516
4517 case print_it_done:
4518 /* We still want to print the frame, but we already printed the
4519 relevant messages. */
4520 return PRINT_SRC_AND_LOC;
4521 break;
4522
4523 case print_it_normal:
4524 {
4525 struct breakpoint *b = bs->breakpoint_at;
4526
4527 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4528 which has since been deleted. */
4529 if (b == NULL)
4530 return PRINT_UNKNOWN;
4531
4532 /* Normal case. Call the breakpoint's print_it method. */
4533 return b->ops->print_it (bs);
4534 }
4535 break;
4536
4537 default:
4538 internal_error (__FILE__, __LINE__,
4539 _("print_bp_stop_message: unrecognized enum value"));
4540 break;
4541 }
4542 }
4543
4544 /* A helper function that prints a shared library stopped event. */
4545
4546 static void
4547 print_solib_event (int is_catchpoint)
4548 {
4549 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4550 bool any_added = !current_program_space->added_solibs.empty ();
4551
4552 if (!is_catchpoint)
4553 {
4554 if (any_added || any_deleted)
4555 current_uiout->text (_("Stopped due to shared library event:\n"));
4556 else
4557 current_uiout->text (_("Stopped due to shared library event (no "
4558 "libraries added or removed)\n"));
4559 }
4560
4561 if (current_uiout->is_mi_like_p ())
4562 current_uiout->field_string ("reason",
4563 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4564
4565 if (any_deleted)
4566 {
4567 current_uiout->text (_(" Inferior unloaded "));
4568 ui_out_emit_list list_emitter (current_uiout, "removed");
4569 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4570 {
4571 const std::string &name = current_program_space->deleted_solibs[ix];
4572
4573 if (ix > 0)
4574 current_uiout->text (" ");
4575 current_uiout->field_string ("library", name);
4576 current_uiout->text ("\n");
4577 }
4578 }
4579
4580 if (any_added)
4581 {
4582 current_uiout->text (_(" Inferior loaded "));
4583 ui_out_emit_list list_emitter (current_uiout, "added");
4584 bool first = true;
4585 for (so_list *iter : current_program_space->added_solibs)
4586 {
4587 if (!first)
4588 current_uiout->text (" ");
4589 first = false;
4590 current_uiout->field_string ("library", iter->so_name);
4591 current_uiout->text ("\n");
4592 }
4593 }
4594 }
4595
4596 /* Print a message indicating what happened. This is called from
4597 normal_stop(). The input to this routine is the head of the bpstat
4598 list - a list of the eventpoints that caused this stop. KIND is
4599 the target_waitkind for the stopping event. This
4600 routine calls the generic print routine for printing a message
4601 about reasons for stopping. This will print (for example) the
4602 "Breakpoint n," part of the output. The return value of this
4603 routine is one of:
4604
4605 PRINT_UNKNOWN: Means we printed nothing.
4606 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4607 code to print the location. An example is
4608 "Breakpoint 1, " which should be followed by
4609 the location.
4610 PRINT_SRC_ONLY: Means we printed something, but there is no need
4611 to also print the location part of the message.
4612 An example is the catch/throw messages, which
4613 don't require a location appended to the end.
4614 PRINT_NOTHING: We have done some printing and we don't need any
4615 further info to be printed. */
4616
4617 enum print_stop_action
4618 bpstat_print (bpstat bs, int kind)
4619 {
4620 enum print_stop_action val;
4621
4622 /* Maybe another breakpoint in the chain caused us to stop.
4623 (Currently all watchpoints go on the bpstat whether hit or not.
4624 That probably could (should) be changed, provided care is taken
4625 with respect to bpstat_explains_signal). */
4626 for (; bs; bs = bs->next)
4627 {
4628 val = print_bp_stop_message (bs);
4629 if (val == PRINT_SRC_ONLY
4630 || val == PRINT_SRC_AND_LOC
4631 || val == PRINT_NOTHING)
4632 return val;
4633 }
4634
4635 /* If we had hit a shared library event breakpoint,
4636 print_bp_stop_message would print out this message. If we hit an
4637 OS-level shared library event, do the same thing. */
4638 if (kind == TARGET_WAITKIND_LOADED)
4639 {
4640 print_solib_event (0);
4641 return PRINT_NOTHING;
4642 }
4643
4644 /* We reached the end of the chain, or we got a null BS to start
4645 with and nothing was printed. */
4646 return PRINT_UNKNOWN;
4647 }
4648
4649 /* Evaluate the boolean expression EXP and return the result. */
4650
4651 static bool
4652 breakpoint_cond_eval (expression *exp)
4653 {
4654 struct value *mark = value_mark ();
4655 bool res = value_true (evaluate_expression (exp));
4656
4657 value_free_to_mark (mark);
4658 return res;
4659 }
4660
4661 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4662
4663 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4664 : next (NULL),
4665 bp_location_at (bl),
4666 breakpoint_at (bl->owner),
4667 commands (NULL),
4668 print (0),
4669 stop (0),
4670 print_it (print_it_normal)
4671 {
4672 incref_bp_location (bl);
4673 **bs_link_pointer = this;
4674 *bs_link_pointer = &next;
4675 }
4676
4677 bpstats::bpstats ()
4678 : next (NULL),
4679 bp_location_at (NULL),
4680 breakpoint_at (NULL),
4681 commands (NULL),
4682 print (0),
4683 stop (0),
4684 print_it (print_it_normal)
4685 {
4686 }
4687 \f
4688 /* The target has stopped with waitstatus WS. Check if any hardware
4689 watchpoints have triggered, according to the target. */
4690
4691 int
4692 watchpoints_triggered (struct target_waitstatus *ws)
4693 {
4694 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4695 CORE_ADDR addr;
4696 struct breakpoint *b;
4697
4698 if (!stopped_by_watchpoint)
4699 {
4700 /* We were not stopped by a watchpoint. Mark all watchpoints
4701 as not triggered. */
4702 ALL_BREAKPOINTS (b)
4703 if (is_hardware_watchpoint (b))
4704 {
4705 struct watchpoint *w = (struct watchpoint *) b;
4706
4707 w->watchpoint_triggered = watch_triggered_no;
4708 }
4709
4710 return 0;
4711 }
4712
4713 if (!target_stopped_data_address (current_top_target (), &addr))
4714 {
4715 /* We were stopped by a watchpoint, but we don't know where.
4716 Mark all watchpoints as unknown. */
4717 ALL_BREAKPOINTS (b)
4718 if (is_hardware_watchpoint (b))
4719 {
4720 struct watchpoint *w = (struct watchpoint *) b;
4721
4722 w->watchpoint_triggered = watch_triggered_unknown;
4723 }
4724
4725 return 1;
4726 }
4727
4728 /* The target could report the data address. Mark watchpoints
4729 affected by this data address as triggered, and all others as not
4730 triggered. */
4731
4732 ALL_BREAKPOINTS (b)
4733 if (is_hardware_watchpoint (b))
4734 {
4735 struct watchpoint *w = (struct watchpoint *) b;
4736 struct bp_location *loc;
4737
4738 w->watchpoint_triggered = watch_triggered_no;
4739 for (loc = b->loc; loc; loc = loc->next)
4740 {
4741 if (is_masked_watchpoint (b))
4742 {
4743 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4744 CORE_ADDR start = loc->address & w->hw_wp_mask;
4745
4746 if (newaddr == start)
4747 {
4748 w->watchpoint_triggered = watch_triggered_yes;
4749 break;
4750 }
4751 }
4752 /* Exact match not required. Within range is sufficient. */
4753 else if (target_watchpoint_addr_within_range (current_top_target (),
4754 addr, loc->address,
4755 loc->length))
4756 {
4757 w->watchpoint_triggered = watch_triggered_yes;
4758 break;
4759 }
4760 }
4761 }
4762
4763 return 1;
4764 }
4765
4766 /* Possible return values for watchpoint_check. */
4767 enum wp_check_result
4768 {
4769 /* The watchpoint has been deleted. */
4770 WP_DELETED = 1,
4771
4772 /* The value has changed. */
4773 WP_VALUE_CHANGED = 2,
4774
4775 /* The value has not changed. */
4776 WP_VALUE_NOT_CHANGED = 3,
4777
4778 /* Ignore this watchpoint, no matter if the value changed or not. */
4779 WP_IGNORE = 4,
4780 };
4781
4782 #define BP_TEMPFLAG 1
4783 #define BP_HARDWAREFLAG 2
4784
4785 /* Evaluate watchpoint condition expression and check if its value
4786 changed. */
4787
4788 static wp_check_result
4789 watchpoint_check (bpstat bs)
4790 {
4791 struct watchpoint *b;
4792 struct frame_info *fr;
4793 int within_current_scope;
4794
4795 /* BS is built from an existing struct breakpoint. */
4796 gdb_assert (bs->breakpoint_at != NULL);
4797 b = (struct watchpoint *) bs->breakpoint_at;
4798
4799 /* If this is a local watchpoint, we only want to check if the
4800 watchpoint frame is in scope if the current thread is the thread
4801 that was used to create the watchpoint. */
4802 if (!watchpoint_in_thread_scope (b))
4803 return WP_IGNORE;
4804
4805 if (b->exp_valid_block == NULL)
4806 within_current_scope = 1;
4807 else
4808 {
4809 struct frame_info *frame = get_current_frame ();
4810 struct gdbarch *frame_arch = get_frame_arch (frame);
4811 CORE_ADDR frame_pc = get_frame_pc (frame);
4812
4813 /* stack_frame_destroyed_p() returns a non-zero value if we're
4814 still in the function but the stack frame has already been
4815 invalidated. Since we can't rely on the values of local
4816 variables after the stack has been destroyed, we are treating
4817 the watchpoint in that state as `not changed' without further
4818 checking. Don't mark watchpoints as changed if the current
4819 frame is in an epilogue - even if they are in some other
4820 frame, our view of the stack is likely to be wrong and
4821 frame_find_by_id could error out. */
4822 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4823 return WP_IGNORE;
4824
4825 fr = frame_find_by_id (b->watchpoint_frame);
4826 within_current_scope = (fr != NULL);
4827
4828 /* If we've gotten confused in the unwinder, we might have
4829 returned a frame that can't describe this variable. */
4830 if (within_current_scope)
4831 {
4832 struct symbol *function;
4833
4834 function = get_frame_function (fr);
4835 if (function == NULL
4836 || !contained_in (b->exp_valid_block,
4837 SYMBOL_BLOCK_VALUE (function)))
4838 within_current_scope = 0;
4839 }
4840
4841 if (within_current_scope)
4842 /* If we end up stopping, the current frame will get selected
4843 in normal_stop. So this call to select_frame won't affect
4844 the user. */
4845 select_frame (fr);
4846 }
4847
4848 if (within_current_scope)
4849 {
4850 /* We use value_{,free_to_}mark because it could be a *long*
4851 time before we return to the command level and call
4852 free_all_values. We can't call free_all_values because we
4853 might be in the middle of evaluating a function call. */
4854
4855 int pc = 0;
4856 struct value *mark;
4857 struct value *new_val;
4858
4859 if (is_masked_watchpoint (b))
4860 /* Since we don't know the exact trigger address (from
4861 stopped_data_address), just tell the user we've triggered
4862 a mask watchpoint. */
4863 return WP_VALUE_CHANGED;
4864
4865 mark = value_mark ();
4866 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4867
4868 if (b->val_bitsize != 0)
4869 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4870
4871 /* We use value_equal_contents instead of value_equal because
4872 the latter coerces an array to a pointer, thus comparing just
4873 the address of the array instead of its contents. This is
4874 not what we want. */
4875 if ((b->val != NULL) != (new_val != NULL)
4876 || (b->val != NULL && !value_equal_contents (b->val.get (),
4877 new_val)))
4878 {
4879 bs->old_val = b->val;
4880 b->val = release_value (new_val);
4881 b->val_valid = true;
4882 if (new_val != NULL)
4883 value_free_to_mark (mark);
4884 return WP_VALUE_CHANGED;
4885 }
4886 else
4887 {
4888 /* Nothing changed. */
4889 value_free_to_mark (mark);
4890 return WP_VALUE_NOT_CHANGED;
4891 }
4892 }
4893 else
4894 {
4895 /* This seems like the only logical thing to do because
4896 if we temporarily ignored the watchpoint, then when
4897 we reenter the block in which it is valid it contains
4898 garbage (in the case of a function, it may have two
4899 garbage values, one before and one after the prologue).
4900 So we can't even detect the first assignment to it and
4901 watch after that (since the garbage may or may not equal
4902 the first value assigned). */
4903 /* We print all the stop information in
4904 breakpoint_ops->print_it, but in this case, by the time we
4905 call breakpoint_ops->print_it this bp will be deleted
4906 already. So we have no choice but print the information
4907 here. */
4908
4909 SWITCH_THRU_ALL_UIS ()
4910 {
4911 struct ui_out *uiout = current_uiout;
4912
4913 if (uiout->is_mi_like_p ())
4914 uiout->field_string
4915 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4916 uiout->message ("\nWatchpoint %pF deleted because the program has "
4917 "left the block in\n"
4918 "which its expression is valid.\n",
4919 signed_field ("wpnum", b->number));
4920 }
4921
4922 /* Make sure the watchpoint's commands aren't executed. */
4923 b->commands = NULL;
4924 watchpoint_del_at_next_stop (b);
4925
4926 return WP_DELETED;
4927 }
4928 }
4929
4930 /* Return true if it looks like target has stopped due to hitting
4931 breakpoint location BL. This function does not check if we should
4932 stop, only if BL explains the stop. */
4933
4934 static int
4935 bpstat_check_location (const struct bp_location *bl,
4936 const address_space *aspace, CORE_ADDR bp_addr,
4937 const struct target_waitstatus *ws)
4938 {
4939 struct breakpoint *b = bl->owner;
4940
4941 /* BL is from an existing breakpoint. */
4942 gdb_assert (b != NULL);
4943
4944 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4945 }
4946
4947 /* Determine if the watched values have actually changed, and we
4948 should stop. If not, set BS->stop to 0. */
4949
4950 static void
4951 bpstat_check_watchpoint (bpstat bs)
4952 {
4953 const struct bp_location *bl;
4954 struct watchpoint *b;
4955
4956 /* BS is built for existing struct breakpoint. */
4957 bl = bs->bp_location_at;
4958 gdb_assert (bl != NULL);
4959 b = (struct watchpoint *) bs->breakpoint_at;
4960 gdb_assert (b != NULL);
4961
4962 {
4963 int must_check_value = 0;
4964
4965 if (b->type == bp_watchpoint)
4966 /* For a software watchpoint, we must always check the
4967 watched value. */
4968 must_check_value = 1;
4969 else if (b->watchpoint_triggered == watch_triggered_yes)
4970 /* We have a hardware watchpoint (read, write, or access)
4971 and the target earlier reported an address watched by
4972 this watchpoint. */
4973 must_check_value = 1;
4974 else if (b->watchpoint_triggered == watch_triggered_unknown
4975 && b->type == bp_hardware_watchpoint)
4976 /* We were stopped by a hardware watchpoint, but the target could
4977 not report the data address. We must check the watchpoint's
4978 value. Access and read watchpoints are out of luck; without
4979 a data address, we can't figure it out. */
4980 must_check_value = 1;
4981
4982 if (must_check_value)
4983 {
4984 wp_check_result e;
4985
4986 try
4987 {
4988 e = watchpoint_check (bs);
4989 }
4990 catch (const gdb_exception &ex)
4991 {
4992 exception_fprintf (gdb_stderr, ex,
4993 "Error evaluating expression "
4994 "for watchpoint %d\n",
4995 b->number);
4996
4997 SWITCH_THRU_ALL_UIS ()
4998 {
4999 printf_filtered (_("Watchpoint %d deleted.\n"),
5000 b->number);
5001 }
5002 watchpoint_del_at_next_stop (b);
5003 e = WP_DELETED;
5004 }
5005
5006 switch (e)
5007 {
5008 case WP_DELETED:
5009 /* We've already printed what needs to be printed. */
5010 bs->print_it = print_it_done;
5011 /* Stop. */
5012 break;
5013 case WP_IGNORE:
5014 bs->print_it = print_it_noop;
5015 bs->stop = 0;
5016 break;
5017 case WP_VALUE_CHANGED:
5018 if (b->type == bp_read_watchpoint)
5019 {
5020 /* There are two cases to consider here:
5021
5022 1. We're watching the triggered memory for reads.
5023 In that case, trust the target, and always report
5024 the watchpoint hit to the user. Even though
5025 reads don't cause value changes, the value may
5026 have changed since the last time it was read, and
5027 since we're not trapping writes, we will not see
5028 those, and as such we should ignore our notion of
5029 old value.
5030
5031 2. We're watching the triggered memory for both
5032 reads and writes. There are two ways this may
5033 happen:
5034
5035 2.1. This is a target that can't break on data
5036 reads only, but can break on accesses (reads or
5037 writes), such as e.g., x86. We detect this case
5038 at the time we try to insert read watchpoints.
5039
5040 2.2. Otherwise, the target supports read
5041 watchpoints, but, the user set an access or write
5042 watchpoint watching the same memory as this read
5043 watchpoint.
5044
5045 If we're watching memory writes as well as reads,
5046 ignore watchpoint hits when we find that the
5047 value hasn't changed, as reads don't cause
5048 changes. This still gives false positives when
5049 the program writes the same value to memory as
5050 what there was already in memory (we will confuse
5051 it for a read), but it's much better than
5052 nothing. */
5053
5054 int other_write_watchpoint = 0;
5055
5056 if (bl->watchpoint_type == hw_read)
5057 {
5058 struct breakpoint *other_b;
5059
5060 ALL_BREAKPOINTS (other_b)
5061 if (other_b->type == bp_hardware_watchpoint
5062 || other_b->type == bp_access_watchpoint)
5063 {
5064 struct watchpoint *other_w =
5065 (struct watchpoint *) other_b;
5066
5067 if (other_w->watchpoint_triggered
5068 == watch_triggered_yes)
5069 {
5070 other_write_watchpoint = 1;
5071 break;
5072 }
5073 }
5074 }
5075
5076 if (other_write_watchpoint
5077 || bl->watchpoint_type == hw_access)
5078 {
5079 /* We're watching the same memory for writes,
5080 and the value changed since the last time we
5081 updated it, so this trap must be for a write.
5082 Ignore it. */
5083 bs->print_it = print_it_noop;
5084 bs->stop = 0;
5085 }
5086 }
5087 break;
5088 case WP_VALUE_NOT_CHANGED:
5089 if (b->type == bp_hardware_watchpoint
5090 || b->type == bp_watchpoint)
5091 {
5092 /* Don't stop: write watchpoints shouldn't fire if
5093 the value hasn't changed. */
5094 bs->print_it = print_it_noop;
5095 bs->stop = 0;
5096 }
5097 /* Stop. */
5098 break;
5099 default:
5100 /* Can't happen. */
5101 break;
5102 }
5103 }
5104 else /* must_check_value == 0 */
5105 {
5106 /* This is a case where some watchpoint(s) triggered, but
5107 not at the address of this watchpoint, or else no
5108 watchpoint triggered after all. So don't print
5109 anything for this watchpoint. */
5110 bs->print_it = print_it_noop;
5111 bs->stop = 0;
5112 }
5113 }
5114 }
5115
5116 /* For breakpoints that are currently marked as telling gdb to stop,
5117 check conditions (condition proper, frame, thread and ignore count)
5118 of breakpoint referred to by BS. If we should not stop for this
5119 breakpoint, set BS->stop to 0. */
5120
5121 static void
5122 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5123 {
5124 const struct bp_location *bl;
5125 struct breakpoint *b;
5126 /* Assume stop. */
5127 bool condition_result = true;
5128 struct expression *cond;
5129
5130 gdb_assert (bs->stop);
5131
5132 /* BS is built for existing struct breakpoint. */
5133 bl = bs->bp_location_at;
5134 gdb_assert (bl != NULL);
5135 b = bs->breakpoint_at;
5136 gdb_assert (b != NULL);
5137
5138 /* Even if the target evaluated the condition on its end and notified GDB, we
5139 need to do so again since GDB does not know if we stopped due to a
5140 breakpoint or a single step breakpoint. */
5141
5142 if (frame_id_p (b->frame_id)
5143 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5144 {
5145 bs->stop = 0;
5146 return;
5147 }
5148
5149 /* If this is a thread/task-specific breakpoint, don't waste cpu
5150 evaluating the condition if this isn't the specified
5151 thread/task. */
5152 if ((b->thread != -1 && b->thread != thread->global_num)
5153 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5154 {
5155 bs->stop = 0;
5156 return;
5157 }
5158
5159 /* Evaluate extension language breakpoints that have a "stop" method
5160 implemented. */
5161 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5162
5163 if (is_watchpoint (b))
5164 {
5165 struct watchpoint *w = (struct watchpoint *) b;
5166
5167 cond = w->cond_exp.get ();
5168 }
5169 else
5170 cond = bl->cond.get ();
5171
5172 if (cond && b->disposition != disp_del_at_next_stop)
5173 {
5174 int within_current_scope = 1;
5175 struct watchpoint * w;
5176
5177 /* We use value_mark and value_free_to_mark because it could
5178 be a long time before we return to the command level and
5179 call free_all_values. We can't call free_all_values
5180 because we might be in the middle of evaluating a
5181 function call. */
5182 struct value *mark = value_mark ();
5183
5184 if (is_watchpoint (b))
5185 w = (struct watchpoint *) b;
5186 else
5187 w = NULL;
5188
5189 /* Need to select the frame, with all that implies so that
5190 the conditions will have the right context. Because we
5191 use the frame, we will not see an inlined function's
5192 variables when we arrive at a breakpoint at the start
5193 of the inlined function; the current frame will be the
5194 call site. */
5195 if (w == NULL || w->cond_exp_valid_block == NULL)
5196 select_frame (get_current_frame ());
5197 else
5198 {
5199 struct frame_info *frame;
5200
5201 /* For local watchpoint expressions, which particular
5202 instance of a local is being watched matters, so we
5203 keep track of the frame to evaluate the expression
5204 in. To evaluate the condition however, it doesn't
5205 really matter which instantiation of the function
5206 where the condition makes sense triggers the
5207 watchpoint. This allows an expression like "watch
5208 global if q > 10" set in `func', catch writes to
5209 global on all threads that call `func', or catch
5210 writes on all recursive calls of `func' by a single
5211 thread. We simply always evaluate the condition in
5212 the innermost frame that's executing where it makes
5213 sense to evaluate the condition. It seems
5214 intuitive. */
5215 frame = block_innermost_frame (w->cond_exp_valid_block);
5216 if (frame != NULL)
5217 select_frame (frame);
5218 else
5219 within_current_scope = 0;
5220 }
5221 if (within_current_scope)
5222 {
5223 try
5224 {
5225 condition_result = breakpoint_cond_eval (cond);
5226 }
5227 catch (const gdb_exception &ex)
5228 {
5229 exception_fprintf (gdb_stderr, ex,
5230 "Error in testing breakpoint condition:\n");
5231 }
5232 }
5233 else
5234 {
5235 warning (_("Watchpoint condition cannot be tested "
5236 "in the current scope"));
5237 /* If we failed to set the right context for this
5238 watchpoint, unconditionally report it. */
5239 }
5240 /* FIXME-someday, should give breakpoint #. */
5241 value_free_to_mark (mark);
5242 }
5243
5244 if (cond && !condition_result)
5245 {
5246 bs->stop = 0;
5247 }
5248 else if (b->ignore_count > 0)
5249 {
5250 b->ignore_count--;
5251 bs->stop = 0;
5252 /* Increase the hit count even though we don't stop. */
5253 ++(b->hit_count);
5254 gdb::observers::breakpoint_modified.notify (b);
5255 }
5256 }
5257
5258 /* Returns true if we need to track moribund locations of LOC's type
5259 on the current target. */
5260
5261 static int
5262 need_moribund_for_location_type (struct bp_location *loc)
5263 {
5264 return ((loc->loc_type == bp_loc_software_breakpoint
5265 && !target_supports_stopped_by_sw_breakpoint ())
5266 || (loc->loc_type == bp_loc_hardware_breakpoint
5267 && !target_supports_stopped_by_hw_breakpoint ()));
5268 }
5269
5270 /* See breakpoint.h. */
5271
5272 bpstat
5273 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5274 const struct target_waitstatus *ws)
5275 {
5276 struct breakpoint *b;
5277 bpstat bs_head = NULL, *bs_link = &bs_head;
5278
5279 ALL_BREAKPOINTS (b)
5280 {
5281 if (!breakpoint_enabled (b))
5282 continue;
5283
5284 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5285 {
5286 /* For hardware watchpoints, we look only at the first
5287 location. The watchpoint_check function will work on the
5288 entire expression, not the individual locations. For
5289 read watchpoints, the watchpoints_triggered function has
5290 checked all locations already. */
5291 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5292 break;
5293
5294 if (!bl->enabled || bl->shlib_disabled)
5295 continue;
5296
5297 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5298 continue;
5299
5300 /* Come here if it's a watchpoint, or if the break address
5301 matches. */
5302
5303 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5304 explain stop. */
5305
5306 /* Assume we stop. Should we find a watchpoint that is not
5307 actually triggered, or if the condition of the breakpoint
5308 evaluates as false, we'll reset 'stop' to 0. */
5309 bs->stop = 1;
5310 bs->print = 1;
5311
5312 /* If this is a scope breakpoint, mark the associated
5313 watchpoint as triggered so that we will handle the
5314 out-of-scope event. We'll get to the watchpoint next
5315 iteration. */
5316 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5317 {
5318 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5319
5320 w->watchpoint_triggered = watch_triggered_yes;
5321 }
5322 }
5323 }
5324
5325 /* Check if a moribund breakpoint explains the stop. */
5326 if (!target_supports_stopped_by_sw_breakpoint ()
5327 || !target_supports_stopped_by_hw_breakpoint ())
5328 {
5329 for (bp_location *loc : moribund_locations)
5330 {
5331 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5332 && need_moribund_for_location_type (loc))
5333 {
5334 bpstat bs = new bpstats (loc, &bs_link);
5335 /* For hits of moribund locations, we should just proceed. */
5336 bs->stop = 0;
5337 bs->print = 0;
5338 bs->print_it = print_it_noop;
5339 }
5340 }
5341 }
5342
5343 return bs_head;
5344 }
5345
5346 /* See breakpoint.h. */
5347
5348 bpstat
5349 bpstat_stop_status (const address_space *aspace,
5350 CORE_ADDR bp_addr, thread_info *thread,
5351 const struct target_waitstatus *ws,
5352 bpstat stop_chain)
5353 {
5354 struct breakpoint *b = NULL;
5355 /* First item of allocated bpstat's. */
5356 bpstat bs_head = stop_chain;
5357 bpstat bs;
5358 int need_remove_insert;
5359 int removed_any;
5360
5361 /* First, build the bpstat chain with locations that explain a
5362 target stop, while being careful to not set the target running,
5363 as that may invalidate locations (in particular watchpoint
5364 locations are recreated). Resuming will happen here with
5365 breakpoint conditions or watchpoint expressions that include
5366 inferior function calls. */
5367 if (bs_head == NULL)
5368 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5369
5370 /* A bit of special processing for shlib breakpoints. We need to
5371 process solib loading here, so that the lists of loaded and
5372 unloaded libraries are correct before we handle "catch load" and
5373 "catch unload". */
5374 for (bs = bs_head; bs != NULL; bs = bs->next)
5375 {
5376 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5377 {
5378 handle_solib_event ();
5379 break;
5380 }
5381 }
5382
5383 /* Now go through the locations that caused the target to stop, and
5384 check whether we're interested in reporting this stop to higher
5385 layers, or whether we should resume the target transparently. */
5386
5387 removed_any = 0;
5388
5389 for (bs = bs_head; bs != NULL; bs = bs->next)
5390 {
5391 if (!bs->stop)
5392 continue;
5393
5394 b = bs->breakpoint_at;
5395 b->ops->check_status (bs);
5396 if (bs->stop)
5397 {
5398 bpstat_check_breakpoint_conditions (bs, thread);
5399
5400 if (bs->stop)
5401 {
5402 ++(b->hit_count);
5403 gdb::observers::breakpoint_modified.notify (b);
5404
5405 /* We will stop here. */
5406 if (b->disposition == disp_disable)
5407 {
5408 --(b->enable_count);
5409 if (b->enable_count <= 0)
5410 b->enable_state = bp_disabled;
5411 removed_any = 1;
5412 }
5413 if (b->silent)
5414 bs->print = 0;
5415 bs->commands = b->commands;
5416 if (command_line_is_silent (bs->commands
5417 ? bs->commands.get () : NULL))
5418 bs->print = 0;
5419
5420 b->ops->after_condition_true (bs);
5421 }
5422
5423 }
5424
5425 /* Print nothing for this entry if we don't stop or don't
5426 print. */
5427 if (!bs->stop || !bs->print)
5428 bs->print_it = print_it_noop;
5429 }
5430
5431 /* If we aren't stopping, the value of some hardware watchpoint may
5432 not have changed, but the intermediate memory locations we are
5433 watching may have. Don't bother if we're stopping; this will get
5434 done later. */
5435 need_remove_insert = 0;
5436 if (! bpstat_causes_stop (bs_head))
5437 for (bs = bs_head; bs != NULL; bs = bs->next)
5438 if (!bs->stop
5439 && bs->breakpoint_at
5440 && is_hardware_watchpoint (bs->breakpoint_at))
5441 {
5442 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5443
5444 update_watchpoint (w, 0 /* don't reparse. */);
5445 need_remove_insert = 1;
5446 }
5447
5448 if (need_remove_insert)
5449 update_global_location_list (UGLL_MAY_INSERT);
5450 else if (removed_any)
5451 update_global_location_list (UGLL_DONT_INSERT);
5452
5453 return bs_head;
5454 }
5455
5456 static void
5457 handle_jit_event (void)
5458 {
5459 struct frame_info *frame;
5460 struct gdbarch *gdbarch;
5461
5462 if (debug_infrun)
5463 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5464
5465 /* Switch terminal for any messages produced by
5466 breakpoint_re_set. */
5467 target_terminal::ours_for_output ();
5468
5469 frame = get_current_frame ();
5470 gdbarch = get_frame_arch (frame);
5471
5472 jit_event_handler (gdbarch);
5473
5474 target_terminal::inferior ();
5475 }
5476
5477 /* Prepare WHAT final decision for infrun. */
5478
5479 /* Decide what infrun needs to do with this bpstat. */
5480
5481 struct bpstat_what
5482 bpstat_what (bpstat bs_head)
5483 {
5484 struct bpstat_what retval;
5485 bpstat bs;
5486
5487 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5488 retval.call_dummy = STOP_NONE;
5489 retval.is_longjmp = false;
5490
5491 for (bs = bs_head; bs != NULL; bs = bs->next)
5492 {
5493 /* Extract this BS's action. After processing each BS, we check
5494 if its action overrides all we've seem so far. */
5495 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5496 enum bptype bptype;
5497
5498 if (bs->breakpoint_at == NULL)
5499 {
5500 /* I suspect this can happen if it was a momentary
5501 breakpoint which has since been deleted. */
5502 bptype = bp_none;
5503 }
5504 else
5505 bptype = bs->breakpoint_at->type;
5506
5507 switch (bptype)
5508 {
5509 case bp_none:
5510 break;
5511 case bp_breakpoint:
5512 case bp_hardware_breakpoint:
5513 case bp_single_step:
5514 case bp_until:
5515 case bp_finish:
5516 case bp_shlib_event:
5517 if (bs->stop)
5518 {
5519 if (bs->print)
5520 this_action = BPSTAT_WHAT_STOP_NOISY;
5521 else
5522 this_action = BPSTAT_WHAT_STOP_SILENT;
5523 }
5524 else
5525 this_action = BPSTAT_WHAT_SINGLE;
5526 break;
5527 case bp_watchpoint:
5528 case bp_hardware_watchpoint:
5529 case bp_read_watchpoint:
5530 case bp_access_watchpoint:
5531 if (bs->stop)
5532 {
5533 if (bs->print)
5534 this_action = BPSTAT_WHAT_STOP_NOISY;
5535 else
5536 this_action = BPSTAT_WHAT_STOP_SILENT;
5537 }
5538 else
5539 {
5540 /* There was a watchpoint, but we're not stopping.
5541 This requires no further action. */
5542 }
5543 break;
5544 case bp_longjmp:
5545 case bp_longjmp_call_dummy:
5546 case bp_exception:
5547 if (bs->stop)
5548 {
5549 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5550 retval.is_longjmp = bptype != bp_exception;
5551 }
5552 else
5553 this_action = BPSTAT_WHAT_SINGLE;
5554 break;
5555 case bp_longjmp_resume:
5556 case bp_exception_resume:
5557 if (bs->stop)
5558 {
5559 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5560 retval.is_longjmp = bptype == bp_longjmp_resume;
5561 }
5562 else
5563 this_action = BPSTAT_WHAT_SINGLE;
5564 break;
5565 case bp_step_resume:
5566 if (bs->stop)
5567 this_action = BPSTAT_WHAT_STEP_RESUME;
5568 else
5569 {
5570 /* It is for the wrong frame. */
5571 this_action = BPSTAT_WHAT_SINGLE;
5572 }
5573 break;
5574 case bp_hp_step_resume:
5575 if (bs->stop)
5576 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5577 else
5578 {
5579 /* It is for the wrong frame. */
5580 this_action = BPSTAT_WHAT_SINGLE;
5581 }
5582 break;
5583 case bp_watchpoint_scope:
5584 case bp_thread_event:
5585 case bp_overlay_event:
5586 case bp_longjmp_master:
5587 case bp_std_terminate_master:
5588 case bp_exception_master:
5589 this_action = BPSTAT_WHAT_SINGLE;
5590 break;
5591 case bp_catchpoint:
5592 if (bs->stop)
5593 {
5594 if (bs->print)
5595 this_action = BPSTAT_WHAT_STOP_NOISY;
5596 else
5597 this_action = BPSTAT_WHAT_STOP_SILENT;
5598 }
5599 else
5600 {
5601 /* Some catchpoints are implemented with breakpoints.
5602 For those, we need to step over the breakpoint. */
5603 if (bs->bp_location_at->loc_type != bp_loc_other)
5604 this_action = BPSTAT_WHAT_SINGLE;
5605 }
5606 break;
5607 case bp_jit_event:
5608 this_action = BPSTAT_WHAT_SINGLE;
5609 break;
5610 case bp_call_dummy:
5611 /* Make sure the action is stop (silent or noisy),
5612 so infrun.c pops the dummy frame. */
5613 retval.call_dummy = STOP_STACK_DUMMY;
5614 this_action = BPSTAT_WHAT_STOP_SILENT;
5615 break;
5616 case bp_std_terminate:
5617 /* Make sure the action is stop (silent or noisy),
5618 so infrun.c pops the dummy frame. */
5619 retval.call_dummy = STOP_STD_TERMINATE;
5620 this_action = BPSTAT_WHAT_STOP_SILENT;
5621 break;
5622 case bp_tracepoint:
5623 case bp_fast_tracepoint:
5624 case bp_static_tracepoint:
5625 /* Tracepoint hits should not be reported back to GDB, and
5626 if one got through somehow, it should have been filtered
5627 out already. */
5628 internal_error (__FILE__, __LINE__,
5629 _("bpstat_what: tracepoint encountered"));
5630 break;
5631 case bp_gnu_ifunc_resolver:
5632 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5633 this_action = BPSTAT_WHAT_SINGLE;
5634 break;
5635 case bp_gnu_ifunc_resolver_return:
5636 /* The breakpoint will be removed, execution will restart from the
5637 PC of the former breakpoint. */
5638 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5639 break;
5640
5641 case bp_dprintf:
5642 if (bs->stop)
5643 this_action = BPSTAT_WHAT_STOP_SILENT;
5644 else
5645 this_action = BPSTAT_WHAT_SINGLE;
5646 break;
5647
5648 default:
5649 internal_error (__FILE__, __LINE__,
5650 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5651 }
5652
5653 retval.main_action = std::max (retval.main_action, this_action);
5654 }
5655
5656 return retval;
5657 }
5658
5659 void
5660 bpstat_run_callbacks (bpstat bs_head)
5661 {
5662 bpstat bs;
5663
5664 for (bs = bs_head; bs != NULL; bs = bs->next)
5665 {
5666 struct breakpoint *b = bs->breakpoint_at;
5667
5668 if (b == NULL)
5669 continue;
5670 switch (b->type)
5671 {
5672 case bp_jit_event:
5673 handle_jit_event ();
5674 break;
5675 case bp_gnu_ifunc_resolver:
5676 gnu_ifunc_resolver_stop (b);
5677 break;
5678 case bp_gnu_ifunc_resolver_return:
5679 gnu_ifunc_resolver_return_stop (b);
5680 break;
5681 }
5682 }
5683 }
5684
5685 /* See breakpoint.h. */
5686
5687 bool
5688 bpstat_should_step ()
5689 {
5690 struct breakpoint *b;
5691
5692 ALL_BREAKPOINTS (b)
5693 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5694 return true;
5695 return false;
5696 }
5697
5698 /* See breakpoint.h. */
5699
5700 bool
5701 bpstat_causes_stop (bpstat bs)
5702 {
5703 for (; bs != NULL; bs = bs->next)
5704 if (bs->stop)
5705 return true;
5706
5707 return false;
5708 }
5709
5710 \f
5711
5712 /* Compute a string of spaces suitable to indent the next line
5713 so it starts at the position corresponding to the table column
5714 named COL_NAME in the currently active table of UIOUT. */
5715
5716 static char *
5717 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5718 {
5719 static char wrap_indent[80];
5720 int i, total_width, width, align;
5721 const char *text;
5722
5723 total_width = 0;
5724 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5725 {
5726 if (strcmp (text, col_name) == 0)
5727 {
5728 gdb_assert (total_width < sizeof wrap_indent);
5729 memset (wrap_indent, ' ', total_width);
5730 wrap_indent[total_width] = 0;
5731
5732 return wrap_indent;
5733 }
5734
5735 total_width += width + 1;
5736 }
5737
5738 return NULL;
5739 }
5740
5741 /* Determine if the locations of this breakpoint will have their conditions
5742 evaluated by the target, host or a mix of both. Returns the following:
5743
5744 "host": Host evals condition.
5745 "host or target": Host or Target evals condition.
5746 "target": Target evals condition.
5747 */
5748
5749 static const char *
5750 bp_condition_evaluator (struct breakpoint *b)
5751 {
5752 struct bp_location *bl;
5753 char host_evals = 0;
5754 char target_evals = 0;
5755
5756 if (!b)
5757 return NULL;
5758
5759 if (!is_breakpoint (b))
5760 return NULL;
5761
5762 if (gdb_evaluates_breakpoint_condition_p ()
5763 || !target_supports_evaluation_of_breakpoint_conditions ())
5764 return condition_evaluation_host;
5765
5766 for (bl = b->loc; bl; bl = bl->next)
5767 {
5768 if (bl->cond_bytecode)
5769 target_evals++;
5770 else
5771 host_evals++;
5772 }
5773
5774 if (host_evals && target_evals)
5775 return condition_evaluation_both;
5776 else if (target_evals)
5777 return condition_evaluation_target;
5778 else
5779 return condition_evaluation_host;
5780 }
5781
5782 /* Determine the breakpoint location's condition evaluator. This is
5783 similar to bp_condition_evaluator, but for locations. */
5784
5785 static const char *
5786 bp_location_condition_evaluator (struct bp_location *bl)
5787 {
5788 if (bl && !is_breakpoint (bl->owner))
5789 return NULL;
5790
5791 if (gdb_evaluates_breakpoint_condition_p ()
5792 || !target_supports_evaluation_of_breakpoint_conditions ())
5793 return condition_evaluation_host;
5794
5795 if (bl && bl->cond_bytecode)
5796 return condition_evaluation_target;
5797 else
5798 return condition_evaluation_host;
5799 }
5800
5801 /* Print the LOC location out of the list of B->LOC locations. */
5802
5803 static void
5804 print_breakpoint_location (struct breakpoint *b,
5805 struct bp_location *loc)
5806 {
5807 struct ui_out *uiout = current_uiout;
5808
5809 scoped_restore_current_program_space restore_pspace;
5810
5811 if (loc != NULL && loc->shlib_disabled)
5812 loc = NULL;
5813
5814 if (loc != NULL)
5815 set_current_program_space (loc->pspace);
5816
5817 if (b->display_canonical)
5818 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5819 else if (loc && loc->symtab)
5820 {
5821 const struct symbol *sym = loc->symbol;
5822
5823 if (sym)
5824 {
5825 uiout->text ("in ");
5826 uiout->field_string ("func", sym->print_name (),
5827 function_name_style.style ());
5828 uiout->text (" ");
5829 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5830 uiout->text ("at ");
5831 }
5832 uiout->field_string ("file",
5833 symtab_to_filename_for_display (loc->symtab),
5834 file_name_style.style ());
5835 uiout->text (":");
5836
5837 if (uiout->is_mi_like_p ())
5838 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5839
5840 uiout->field_signed ("line", loc->line_number);
5841 }
5842 else if (loc)
5843 {
5844 string_file stb;
5845
5846 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5847 demangle, "");
5848 uiout->field_stream ("at", stb);
5849 }
5850 else
5851 {
5852 uiout->field_string ("pending",
5853 event_location_to_string (b->location.get ()));
5854 /* If extra_string is available, it could be holding a condition
5855 or dprintf arguments. In either case, make sure it is printed,
5856 too, but only for non-MI streams. */
5857 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5858 {
5859 if (b->type == bp_dprintf)
5860 uiout->text (",");
5861 else
5862 uiout->text (" ");
5863 uiout->text (b->extra_string);
5864 }
5865 }
5866
5867 if (loc && is_breakpoint (b)
5868 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5869 && bp_condition_evaluator (b) == condition_evaluation_both)
5870 {
5871 uiout->text (" (");
5872 uiout->field_string ("evaluated-by",
5873 bp_location_condition_evaluator (loc));
5874 uiout->text (")");
5875 }
5876 }
5877
5878 static const char *
5879 bptype_string (enum bptype type)
5880 {
5881 struct ep_type_description
5882 {
5883 enum bptype type;
5884 const char *description;
5885 };
5886 static struct ep_type_description bptypes[] =
5887 {
5888 {bp_none, "?deleted?"},
5889 {bp_breakpoint, "breakpoint"},
5890 {bp_hardware_breakpoint, "hw breakpoint"},
5891 {bp_single_step, "sw single-step"},
5892 {bp_until, "until"},
5893 {bp_finish, "finish"},
5894 {bp_watchpoint, "watchpoint"},
5895 {bp_hardware_watchpoint, "hw watchpoint"},
5896 {bp_read_watchpoint, "read watchpoint"},
5897 {bp_access_watchpoint, "acc watchpoint"},
5898 {bp_longjmp, "longjmp"},
5899 {bp_longjmp_resume, "longjmp resume"},
5900 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5901 {bp_exception, "exception"},
5902 {bp_exception_resume, "exception resume"},
5903 {bp_step_resume, "step resume"},
5904 {bp_hp_step_resume, "high-priority step resume"},
5905 {bp_watchpoint_scope, "watchpoint scope"},
5906 {bp_call_dummy, "call dummy"},
5907 {bp_std_terminate, "std::terminate"},
5908 {bp_shlib_event, "shlib events"},
5909 {bp_thread_event, "thread events"},
5910 {bp_overlay_event, "overlay events"},
5911 {bp_longjmp_master, "longjmp master"},
5912 {bp_std_terminate_master, "std::terminate master"},
5913 {bp_exception_master, "exception master"},
5914 {bp_catchpoint, "catchpoint"},
5915 {bp_tracepoint, "tracepoint"},
5916 {bp_fast_tracepoint, "fast tracepoint"},
5917 {bp_static_tracepoint, "static tracepoint"},
5918 {bp_dprintf, "dprintf"},
5919 {bp_jit_event, "jit events"},
5920 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5921 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5922 };
5923
5924 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5925 || ((int) type != bptypes[(int) type].type))
5926 internal_error (__FILE__, __LINE__,
5927 _("bptypes table does not describe type #%d."),
5928 (int) type);
5929
5930 return bptypes[(int) type].description;
5931 }
5932
5933 /* For MI, output a field named 'thread-groups' with a list as the value.
5934 For CLI, prefix the list with the string 'inf'. */
5935
5936 static void
5937 output_thread_groups (struct ui_out *uiout,
5938 const char *field_name,
5939 const std::vector<int> &inf_nums,
5940 int mi_only)
5941 {
5942 int is_mi = uiout->is_mi_like_p ();
5943
5944 /* For backward compatibility, don't display inferiors in CLI unless
5945 there are several. Always display them for MI. */
5946 if (!is_mi && mi_only)
5947 return;
5948
5949 ui_out_emit_list list_emitter (uiout, field_name);
5950
5951 for (size_t i = 0; i < inf_nums.size (); i++)
5952 {
5953 if (is_mi)
5954 {
5955 char mi_group[10];
5956
5957 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
5958 uiout->field_string (NULL, mi_group);
5959 }
5960 else
5961 {
5962 if (i == 0)
5963 uiout->text (" inf ");
5964 else
5965 uiout->text (", ");
5966
5967 uiout->text (plongest (inf_nums[i]));
5968 }
5969 }
5970 }
5971
5972 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5973 instead of going via breakpoint_ops::print_one. This makes "maint
5974 info breakpoints" show the software breakpoint locations of
5975 catchpoints, which are considered internal implementation
5976 detail. */
5977
5978 static void
5979 print_one_breakpoint_location (struct breakpoint *b,
5980 struct bp_location *loc,
5981 int loc_number,
5982 struct bp_location **last_loc,
5983 int allflag, bool raw_loc)
5984 {
5985 struct command_line *l;
5986 static char bpenables[] = "nynny";
5987
5988 struct ui_out *uiout = current_uiout;
5989 int header_of_multiple = 0;
5990 int part_of_multiple = (loc != NULL);
5991 struct value_print_options opts;
5992
5993 get_user_print_options (&opts);
5994
5995 gdb_assert (!loc || loc_number != 0);
5996 /* See comment in print_one_breakpoint concerning treatment of
5997 breakpoints with single disabled location. */
5998 if (loc == NULL
5999 && (b->loc != NULL
6000 && (b->loc->next != NULL || !b->loc->enabled)))
6001 header_of_multiple = 1;
6002 if (loc == NULL)
6003 loc = b->loc;
6004
6005 annotate_record ();
6006
6007 /* 1 */
6008 annotate_field (0);
6009 if (part_of_multiple)
6010 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6011 else
6012 uiout->field_signed ("number", b->number);
6013
6014 /* 2 */
6015 annotate_field (1);
6016 if (part_of_multiple)
6017 uiout->field_skip ("type");
6018 else
6019 uiout->field_string ("type", bptype_string (b->type));
6020
6021 /* 3 */
6022 annotate_field (2);
6023 if (part_of_multiple)
6024 uiout->field_skip ("disp");
6025 else
6026 uiout->field_string ("disp", bpdisp_text (b->disposition));
6027
6028 /* 4 */
6029 annotate_field (3);
6030 if (part_of_multiple)
6031 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6032 else
6033 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6034
6035 /* 5 and 6 */
6036 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6037 b->ops->print_one (b, last_loc);
6038 else
6039 {
6040 if (is_watchpoint (b))
6041 {
6042 struct watchpoint *w = (struct watchpoint *) b;
6043
6044 /* Field 4, the address, is omitted (which makes the columns
6045 not line up too nicely with the headers, but the effect
6046 is relatively readable). */
6047 if (opts.addressprint)
6048 uiout->field_skip ("addr");
6049 annotate_field (5);
6050 uiout->field_string ("what", w->exp_string);
6051 }
6052 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6053 || is_ada_exception_catchpoint (b))
6054 {
6055 if (opts.addressprint)
6056 {
6057 annotate_field (4);
6058 if (header_of_multiple)
6059 uiout->field_string ("addr", "<MULTIPLE>",
6060 metadata_style.style ());
6061 else if (b->loc == NULL || loc->shlib_disabled)
6062 uiout->field_string ("addr", "<PENDING>",
6063 metadata_style.style ());
6064 else
6065 uiout->field_core_addr ("addr",
6066 loc->gdbarch, loc->address);
6067 }
6068 annotate_field (5);
6069 if (!header_of_multiple)
6070 print_breakpoint_location (b, loc);
6071 if (b->loc)
6072 *last_loc = b->loc;
6073 }
6074 }
6075
6076 if (loc != NULL && !header_of_multiple)
6077 {
6078 std::vector<int> inf_nums;
6079 int mi_only = 1;
6080
6081 for (inferior *inf : all_inferiors ())
6082 {
6083 if (inf->pspace == loc->pspace)
6084 inf_nums.push_back (inf->num);
6085 }
6086
6087 /* For backward compatibility, don't display inferiors in CLI unless
6088 there are several. Always display for MI. */
6089 if (allflag
6090 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6091 && (number_of_program_spaces () > 1
6092 || number_of_inferiors () > 1)
6093 /* LOC is for existing B, it cannot be in
6094 moribund_locations and thus having NULL OWNER. */
6095 && loc->owner->type != bp_catchpoint))
6096 mi_only = 0;
6097 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6098 }
6099
6100 if (!part_of_multiple)
6101 {
6102 if (b->thread != -1)
6103 {
6104 /* FIXME: This seems to be redundant and lost here; see the
6105 "stop only in" line a little further down. */
6106 uiout->text (" thread ");
6107 uiout->field_signed ("thread", b->thread);
6108 }
6109 else if (b->task != 0)
6110 {
6111 uiout->text (" task ");
6112 uiout->field_signed ("task", b->task);
6113 }
6114 }
6115
6116 uiout->text ("\n");
6117
6118 if (!part_of_multiple)
6119 b->ops->print_one_detail (b, uiout);
6120
6121 if (part_of_multiple && frame_id_p (b->frame_id))
6122 {
6123 annotate_field (6);
6124 uiout->text ("\tstop only in stack frame at ");
6125 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6126 the frame ID. */
6127 uiout->field_core_addr ("frame",
6128 b->gdbarch, b->frame_id.stack_addr);
6129 uiout->text ("\n");
6130 }
6131
6132 if (!part_of_multiple && b->cond_string)
6133 {
6134 annotate_field (7);
6135 if (is_tracepoint (b))
6136 uiout->text ("\ttrace only if ");
6137 else
6138 uiout->text ("\tstop only if ");
6139 uiout->field_string ("cond", b->cond_string);
6140
6141 /* Print whether the target is doing the breakpoint's condition
6142 evaluation. If GDB is doing the evaluation, don't print anything. */
6143 if (is_breakpoint (b)
6144 && breakpoint_condition_evaluation_mode ()
6145 == condition_evaluation_target)
6146 {
6147 uiout->message (" (%pF evals)",
6148 string_field ("evaluated-by",
6149 bp_condition_evaluator (b)));
6150 }
6151 uiout->text ("\n");
6152 }
6153
6154 if (!part_of_multiple && b->thread != -1)
6155 {
6156 /* FIXME should make an annotation for this. */
6157 uiout->text ("\tstop only in thread ");
6158 if (uiout->is_mi_like_p ())
6159 uiout->field_signed ("thread", b->thread);
6160 else
6161 {
6162 struct thread_info *thr = find_thread_global_id (b->thread);
6163
6164 uiout->field_string ("thread", print_thread_id (thr));
6165 }
6166 uiout->text ("\n");
6167 }
6168
6169 if (!part_of_multiple)
6170 {
6171 if (b->hit_count)
6172 {
6173 /* FIXME should make an annotation for this. */
6174 if (is_catchpoint (b))
6175 uiout->text ("\tcatchpoint");
6176 else if (is_tracepoint (b))
6177 uiout->text ("\ttracepoint");
6178 else
6179 uiout->text ("\tbreakpoint");
6180 uiout->text (" already hit ");
6181 uiout->field_signed ("times", b->hit_count);
6182 if (b->hit_count == 1)
6183 uiout->text (" time\n");
6184 else
6185 uiout->text (" times\n");
6186 }
6187 else
6188 {
6189 /* Output the count also if it is zero, but only if this is mi. */
6190 if (uiout->is_mi_like_p ())
6191 uiout->field_signed ("times", b->hit_count);
6192 }
6193 }
6194
6195 if (!part_of_multiple && b->ignore_count)
6196 {
6197 annotate_field (8);
6198 uiout->message ("\tignore next %pF hits\n",
6199 signed_field ("ignore", b->ignore_count));
6200 }
6201
6202 /* Note that an enable count of 1 corresponds to "enable once"
6203 behavior, which is reported by the combination of enablement and
6204 disposition, so we don't need to mention it here. */
6205 if (!part_of_multiple && b->enable_count > 1)
6206 {
6207 annotate_field (8);
6208 uiout->text ("\tdisable after ");
6209 /* Tweak the wording to clarify that ignore and enable counts
6210 are distinct, and have additive effect. */
6211 if (b->ignore_count)
6212 uiout->text ("additional ");
6213 else
6214 uiout->text ("next ");
6215 uiout->field_signed ("enable", b->enable_count);
6216 uiout->text (" hits\n");
6217 }
6218
6219 if (!part_of_multiple && is_tracepoint (b))
6220 {
6221 struct tracepoint *tp = (struct tracepoint *) b;
6222
6223 if (tp->traceframe_usage)
6224 {
6225 uiout->text ("\ttrace buffer usage ");
6226 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6227 uiout->text (" bytes\n");
6228 }
6229 }
6230
6231 l = b->commands ? b->commands.get () : NULL;
6232 if (!part_of_multiple && l)
6233 {
6234 annotate_field (9);
6235 ui_out_emit_tuple tuple_emitter (uiout, "script");
6236 print_command_lines (uiout, l, 4);
6237 }
6238
6239 if (is_tracepoint (b))
6240 {
6241 struct tracepoint *t = (struct tracepoint *) b;
6242
6243 if (!part_of_multiple && t->pass_count)
6244 {
6245 annotate_field (10);
6246 uiout->text ("\tpass count ");
6247 uiout->field_signed ("pass", t->pass_count);
6248 uiout->text (" \n");
6249 }
6250
6251 /* Don't display it when tracepoint or tracepoint location is
6252 pending. */
6253 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6254 {
6255 annotate_field (11);
6256
6257 if (uiout->is_mi_like_p ())
6258 uiout->field_string ("installed",
6259 loc->inserted ? "y" : "n");
6260 else
6261 {
6262 if (loc->inserted)
6263 uiout->text ("\t");
6264 else
6265 uiout->text ("\tnot ");
6266 uiout->text ("installed on target\n");
6267 }
6268 }
6269 }
6270
6271 if (uiout->is_mi_like_p () && !part_of_multiple)
6272 {
6273 if (is_watchpoint (b))
6274 {
6275 struct watchpoint *w = (struct watchpoint *) b;
6276
6277 uiout->field_string ("original-location", w->exp_string);
6278 }
6279 else if (b->location != NULL
6280 && event_location_to_string (b->location.get ()) != NULL)
6281 uiout->field_string ("original-location",
6282 event_location_to_string (b->location.get ()));
6283 }
6284 }
6285
6286 /* See breakpoint.h. */
6287
6288 bool fix_multi_location_breakpoint_output_globally = false;
6289
6290 static void
6291 print_one_breakpoint (struct breakpoint *b,
6292 struct bp_location **last_loc,
6293 int allflag)
6294 {
6295 struct ui_out *uiout = current_uiout;
6296 bool use_fixed_output
6297 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6298 || fix_multi_location_breakpoint_output_globally);
6299
6300 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6301 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6302
6303 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6304 are outside. */
6305 if (!use_fixed_output)
6306 bkpt_tuple_emitter.reset ();
6307
6308 /* If this breakpoint has custom print function,
6309 it's already printed. Otherwise, print individual
6310 locations, if any. */
6311 if (b->ops == NULL
6312 || b->ops->print_one == NULL
6313 || allflag)
6314 {
6315 /* If breakpoint has a single location that is disabled, we
6316 print it as if it had several locations, since otherwise it's
6317 hard to represent "breakpoint enabled, location disabled"
6318 situation.
6319
6320 Note that while hardware watchpoints have several locations
6321 internally, that's not a property exposed to users.
6322
6323 Likewise, while catchpoints may be implemented with
6324 breakpoints (e.g., catch throw), that's not a property
6325 exposed to users. We do however display the internal
6326 breakpoint locations with "maint info breakpoints". */
6327 if (!is_hardware_watchpoint (b)
6328 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6329 || is_ada_exception_catchpoint (b))
6330 && (allflag
6331 || (b->loc && (b->loc->next || !b->loc->enabled))))
6332 {
6333 gdb::optional<ui_out_emit_list> locations_list;
6334
6335 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6336 MI record. For later versions, place breakpoint locations in a
6337 list. */
6338 if (uiout->is_mi_like_p () && use_fixed_output)
6339 locations_list.emplace (uiout, "locations");
6340
6341 int n = 1;
6342 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6343 {
6344 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6345 print_one_breakpoint_location (b, loc, n, last_loc,
6346 allflag, allflag);
6347 }
6348 }
6349 }
6350 }
6351
6352 static int
6353 breakpoint_address_bits (struct breakpoint *b)
6354 {
6355 int print_address_bits = 0;
6356 struct bp_location *loc;
6357
6358 /* Software watchpoints that aren't watching memory don't have an
6359 address to print. */
6360 if (is_no_memory_software_watchpoint (b))
6361 return 0;
6362
6363 for (loc = b->loc; loc; loc = loc->next)
6364 {
6365 int addr_bit;
6366
6367 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6368 if (addr_bit > print_address_bits)
6369 print_address_bits = addr_bit;
6370 }
6371
6372 return print_address_bits;
6373 }
6374
6375 /* See breakpoint.h. */
6376
6377 void
6378 print_breakpoint (breakpoint *b)
6379 {
6380 struct bp_location *dummy_loc = NULL;
6381 print_one_breakpoint (b, &dummy_loc, 0);
6382 }
6383
6384 /* Return true if this breakpoint was set by the user, false if it is
6385 internal or momentary. */
6386
6387 int
6388 user_breakpoint_p (struct breakpoint *b)
6389 {
6390 return b->number > 0;
6391 }
6392
6393 /* See breakpoint.h. */
6394
6395 int
6396 pending_breakpoint_p (struct breakpoint *b)
6397 {
6398 return b->loc == NULL;
6399 }
6400
6401 /* Print information on breakpoints (including watchpoints and tracepoints).
6402
6403 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6404 understood by number_or_range_parser. Only breakpoints included in this
6405 list are then printed.
6406
6407 If SHOW_INTERNAL is true, print internal breakpoints.
6408
6409 If FILTER is non-NULL, call it on each breakpoint and only include the
6410 ones for which it returns true.
6411
6412 Return the total number of breakpoints listed. */
6413
6414 static int
6415 breakpoint_1 (const char *bp_num_list, bool show_internal,
6416 bool (*filter) (const struct breakpoint *))
6417 {
6418 struct breakpoint *b;
6419 struct bp_location *last_loc = NULL;
6420 int nr_printable_breakpoints;
6421 struct value_print_options opts;
6422 int print_address_bits = 0;
6423 int print_type_col_width = 14;
6424 struct ui_out *uiout = current_uiout;
6425
6426 get_user_print_options (&opts);
6427
6428 /* Compute the number of rows in the table, as well as the size
6429 required for address fields. */
6430 nr_printable_breakpoints = 0;
6431 ALL_BREAKPOINTS (b)
6432 {
6433 /* If we have a filter, only list the breakpoints it accepts. */
6434 if (filter && !filter (b))
6435 continue;
6436
6437 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6438 accept. Skip the others. */
6439 if (bp_num_list != NULL && *bp_num_list != '\0')
6440 {
6441 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6442 continue;
6443 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6444 continue;
6445 }
6446
6447 if (show_internal || user_breakpoint_p (b))
6448 {
6449 int addr_bit, type_len;
6450
6451 addr_bit = breakpoint_address_bits (b);
6452 if (addr_bit > print_address_bits)
6453 print_address_bits = addr_bit;
6454
6455 type_len = strlen (bptype_string (b->type));
6456 if (type_len > print_type_col_width)
6457 print_type_col_width = type_len;
6458
6459 nr_printable_breakpoints++;
6460 }
6461 }
6462
6463 {
6464 ui_out_emit_table table_emitter (uiout,
6465 opts.addressprint ? 6 : 5,
6466 nr_printable_breakpoints,
6467 "BreakpointTable");
6468
6469 if (nr_printable_breakpoints > 0)
6470 annotate_breakpoints_headers ();
6471 if (nr_printable_breakpoints > 0)
6472 annotate_field (0);
6473 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6474 if (nr_printable_breakpoints > 0)
6475 annotate_field (1);
6476 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6477 if (nr_printable_breakpoints > 0)
6478 annotate_field (2);
6479 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6480 if (nr_printable_breakpoints > 0)
6481 annotate_field (3);
6482 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6483 if (opts.addressprint)
6484 {
6485 if (nr_printable_breakpoints > 0)
6486 annotate_field (4);
6487 if (print_address_bits <= 32)
6488 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6489 else
6490 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6491 }
6492 if (nr_printable_breakpoints > 0)
6493 annotate_field (5);
6494 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6495 uiout->table_body ();
6496 if (nr_printable_breakpoints > 0)
6497 annotate_breakpoints_table ();
6498
6499 ALL_BREAKPOINTS (b)
6500 {
6501 QUIT;
6502 /* If we have a filter, only list the breakpoints it accepts. */
6503 if (filter && !filter (b))
6504 continue;
6505
6506 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6507 accept. Skip the others. */
6508
6509 if (bp_num_list != NULL && *bp_num_list != '\0')
6510 {
6511 if (show_internal) /* maintenance info breakpoint */
6512 {
6513 if (parse_and_eval_long (bp_num_list) != b->number)
6514 continue;
6515 }
6516 else /* all others */
6517 {
6518 if (!number_is_in_list (bp_num_list, b->number))
6519 continue;
6520 }
6521 }
6522 /* We only print out user settable breakpoints unless the
6523 show_internal is set. */
6524 if (show_internal || user_breakpoint_p (b))
6525 print_one_breakpoint (b, &last_loc, show_internal);
6526 }
6527 }
6528
6529 if (nr_printable_breakpoints == 0)
6530 {
6531 /* If there's a filter, let the caller decide how to report
6532 empty list. */
6533 if (!filter)
6534 {
6535 if (bp_num_list == NULL || *bp_num_list == '\0')
6536 uiout->message ("No breakpoints or watchpoints.\n");
6537 else
6538 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6539 bp_num_list);
6540 }
6541 }
6542 else
6543 {
6544 if (last_loc && !server_command)
6545 set_next_address (last_loc->gdbarch, last_loc->address);
6546 }
6547
6548 /* FIXME? Should this be moved up so that it is only called when
6549 there have been breakpoints? */
6550 annotate_breakpoints_table_end ();
6551
6552 return nr_printable_breakpoints;
6553 }
6554
6555 /* Display the value of default-collect in a way that is generally
6556 compatible with the breakpoint list. */
6557
6558 static void
6559 default_collect_info (void)
6560 {
6561 struct ui_out *uiout = current_uiout;
6562
6563 /* If it has no value (which is frequently the case), say nothing; a
6564 message like "No default-collect." gets in user's face when it's
6565 not wanted. */
6566 if (!*default_collect)
6567 return;
6568
6569 /* The following phrase lines up nicely with per-tracepoint collect
6570 actions. */
6571 uiout->text ("default collect ");
6572 uiout->field_string ("default-collect", default_collect);
6573 uiout->text (" \n");
6574 }
6575
6576 static void
6577 info_breakpoints_command (const char *args, int from_tty)
6578 {
6579 breakpoint_1 (args, false, NULL);
6580
6581 default_collect_info ();
6582 }
6583
6584 static void
6585 info_watchpoints_command (const char *args, int from_tty)
6586 {
6587 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6588 struct ui_out *uiout = current_uiout;
6589
6590 if (num_printed == 0)
6591 {
6592 if (args == NULL || *args == '\0')
6593 uiout->message ("No watchpoints.\n");
6594 else
6595 uiout->message ("No watchpoint matching '%s'.\n", args);
6596 }
6597 }
6598
6599 static void
6600 maintenance_info_breakpoints (const char *args, int from_tty)
6601 {
6602 breakpoint_1 (args, true, NULL);
6603
6604 default_collect_info ();
6605 }
6606
6607 static int
6608 breakpoint_has_pc (struct breakpoint *b,
6609 struct program_space *pspace,
6610 CORE_ADDR pc, struct obj_section *section)
6611 {
6612 struct bp_location *bl = b->loc;
6613
6614 for (; bl; bl = bl->next)
6615 {
6616 if (bl->pspace == pspace
6617 && bl->address == pc
6618 && (!overlay_debugging || bl->section == section))
6619 return 1;
6620 }
6621 return 0;
6622 }
6623
6624 /* Print a message describing any user-breakpoints set at PC. This
6625 concerns with logical breakpoints, so we match program spaces, not
6626 address spaces. */
6627
6628 static void
6629 describe_other_breakpoints (struct gdbarch *gdbarch,
6630 struct program_space *pspace, CORE_ADDR pc,
6631 struct obj_section *section, int thread)
6632 {
6633 int others = 0;
6634 struct breakpoint *b;
6635
6636 ALL_BREAKPOINTS (b)
6637 others += (user_breakpoint_p (b)
6638 && breakpoint_has_pc (b, pspace, pc, section));
6639 if (others > 0)
6640 {
6641 if (others == 1)
6642 printf_filtered (_("Note: breakpoint "));
6643 else /* if (others == ???) */
6644 printf_filtered (_("Note: breakpoints "));
6645 ALL_BREAKPOINTS (b)
6646 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6647 {
6648 others--;
6649 printf_filtered ("%d", b->number);
6650 if (b->thread == -1 && thread != -1)
6651 printf_filtered (" (all threads)");
6652 else if (b->thread != -1)
6653 printf_filtered (" (thread %d)", b->thread);
6654 printf_filtered ("%s%s ",
6655 ((b->enable_state == bp_disabled
6656 || b->enable_state == bp_call_disabled)
6657 ? " (disabled)"
6658 : ""),
6659 (others > 1) ? ","
6660 : ((others == 1) ? " and" : ""));
6661 }
6662 current_uiout->message (_("also set at pc %ps.\n"),
6663 styled_string (address_style.style (),
6664 paddress (gdbarch, pc)));
6665 }
6666 }
6667 \f
6668
6669 /* Return true iff it is meaningful to use the address member of LOC.
6670 For some breakpoint types, the locations' address members are
6671 irrelevant and it makes no sense to attempt to compare them to
6672 other addresses (or use them for any other purpose either).
6673
6674 More specifically, software watchpoints and catchpoints that are
6675 not backed by breakpoints always have a zero valued location
6676 address and we don't want to mark breakpoints of any of these types
6677 to be a duplicate of an actual breakpoint location at address
6678 zero. */
6679
6680 static bool
6681 bl_address_is_meaningful (bp_location *loc)
6682 {
6683 return loc->loc_type != bp_loc_other;
6684 }
6685
6686 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6687 true if LOC1 and LOC2 represent the same watchpoint location. */
6688
6689 static int
6690 watchpoint_locations_match (struct bp_location *loc1,
6691 struct bp_location *loc2)
6692 {
6693 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6694 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6695
6696 /* Both of them must exist. */
6697 gdb_assert (w1 != NULL);
6698 gdb_assert (w2 != NULL);
6699
6700 /* If the target can evaluate the condition expression in hardware,
6701 then we we need to insert both watchpoints even if they are at
6702 the same place. Otherwise the watchpoint will only trigger when
6703 the condition of whichever watchpoint was inserted evaluates to
6704 true, not giving a chance for GDB to check the condition of the
6705 other watchpoint. */
6706 if ((w1->cond_exp
6707 && target_can_accel_watchpoint_condition (loc1->address,
6708 loc1->length,
6709 loc1->watchpoint_type,
6710 w1->cond_exp.get ()))
6711 || (w2->cond_exp
6712 && target_can_accel_watchpoint_condition (loc2->address,
6713 loc2->length,
6714 loc2->watchpoint_type,
6715 w2->cond_exp.get ())))
6716 return 0;
6717
6718 /* Note that this checks the owner's type, not the location's. In
6719 case the target does not support read watchpoints, but does
6720 support access watchpoints, we'll have bp_read_watchpoint
6721 watchpoints with hw_access locations. Those should be considered
6722 duplicates of hw_read locations. The hw_read locations will
6723 become hw_access locations later. */
6724 return (loc1->owner->type == loc2->owner->type
6725 && loc1->pspace->aspace == loc2->pspace->aspace
6726 && loc1->address == loc2->address
6727 && loc1->length == loc2->length);
6728 }
6729
6730 /* See breakpoint.h. */
6731
6732 int
6733 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6734 const address_space *aspace2, CORE_ADDR addr2)
6735 {
6736 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6737 || aspace1 == aspace2)
6738 && addr1 == addr2);
6739 }
6740
6741 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6742 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6743 matches ASPACE2. On targets that have global breakpoints, the address
6744 space doesn't really matter. */
6745
6746 static int
6747 breakpoint_address_match_range (const address_space *aspace1,
6748 CORE_ADDR addr1,
6749 int len1, const address_space *aspace2,
6750 CORE_ADDR addr2)
6751 {
6752 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6753 || aspace1 == aspace2)
6754 && addr2 >= addr1 && addr2 < addr1 + len1);
6755 }
6756
6757 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6758 a ranged breakpoint. In most targets, a match happens only if ASPACE
6759 matches the breakpoint's address space. On targets that have global
6760 breakpoints, the address space doesn't really matter. */
6761
6762 static int
6763 breakpoint_location_address_match (struct bp_location *bl,
6764 const address_space *aspace,
6765 CORE_ADDR addr)
6766 {
6767 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6768 aspace, addr)
6769 || (bl->length
6770 && breakpoint_address_match_range (bl->pspace->aspace,
6771 bl->address, bl->length,
6772 aspace, addr)));
6773 }
6774
6775 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6776 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6777 match happens only if ASPACE matches the breakpoint's address
6778 space. On targets that have global breakpoints, the address space
6779 doesn't really matter. */
6780
6781 static int
6782 breakpoint_location_address_range_overlap (struct bp_location *bl,
6783 const address_space *aspace,
6784 CORE_ADDR addr, int len)
6785 {
6786 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6787 || bl->pspace->aspace == aspace)
6788 {
6789 int bl_len = bl->length != 0 ? bl->length : 1;
6790
6791 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6792 return 1;
6793 }
6794 return 0;
6795 }
6796
6797 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6798 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6799 true, otherwise returns false. */
6800
6801 static int
6802 tracepoint_locations_match (struct bp_location *loc1,
6803 struct bp_location *loc2)
6804 {
6805 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6806 /* Since tracepoint locations are never duplicated with others', tracepoint
6807 locations at the same address of different tracepoints are regarded as
6808 different locations. */
6809 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6810 else
6811 return 0;
6812 }
6813
6814 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6815 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6816 the same location. */
6817
6818 static int
6819 breakpoint_locations_match (struct bp_location *loc1,
6820 struct bp_location *loc2)
6821 {
6822 int hw_point1, hw_point2;
6823
6824 /* Both of them must not be in moribund_locations. */
6825 gdb_assert (loc1->owner != NULL);
6826 gdb_assert (loc2->owner != NULL);
6827
6828 hw_point1 = is_hardware_watchpoint (loc1->owner);
6829 hw_point2 = is_hardware_watchpoint (loc2->owner);
6830
6831 if (hw_point1 != hw_point2)
6832 return 0;
6833 else if (hw_point1)
6834 return watchpoint_locations_match (loc1, loc2);
6835 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6836 return tracepoint_locations_match (loc1, loc2);
6837 else
6838 /* We compare bp_location.length in order to cover ranged breakpoints. */
6839 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6840 loc2->pspace->aspace, loc2->address)
6841 && loc1->length == loc2->length);
6842 }
6843
6844 static void
6845 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6846 int bnum, int have_bnum)
6847 {
6848 /* The longest string possibly returned by hex_string_custom
6849 is 50 chars. These must be at least that big for safety. */
6850 char astr1[64];
6851 char astr2[64];
6852
6853 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6854 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6855 if (have_bnum)
6856 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6857 bnum, astr1, astr2);
6858 else
6859 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6860 }
6861
6862 /* Adjust a breakpoint's address to account for architectural
6863 constraints on breakpoint placement. Return the adjusted address.
6864 Note: Very few targets require this kind of adjustment. For most
6865 targets, this function is simply the identity function. */
6866
6867 static CORE_ADDR
6868 adjust_breakpoint_address (struct gdbarch *gdbarch,
6869 CORE_ADDR bpaddr, enum bptype bptype)
6870 {
6871 if (bptype == bp_watchpoint
6872 || bptype == bp_hardware_watchpoint
6873 || bptype == bp_read_watchpoint
6874 || bptype == bp_access_watchpoint
6875 || bptype == bp_catchpoint)
6876 {
6877 /* Watchpoints and the various bp_catch_* eventpoints should not
6878 have their addresses modified. */
6879 return bpaddr;
6880 }
6881 else if (bptype == bp_single_step)
6882 {
6883 /* Single-step breakpoints should not have their addresses
6884 modified. If there's any architectural constrain that
6885 applies to this address, then it should have already been
6886 taken into account when the breakpoint was created in the
6887 first place. If we didn't do this, stepping through e.g.,
6888 Thumb-2 IT blocks would break. */
6889 return bpaddr;
6890 }
6891 else
6892 {
6893 CORE_ADDR adjusted_bpaddr = bpaddr;
6894
6895 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6896 {
6897 /* Some targets have architectural constraints on the placement
6898 of breakpoint instructions. Obtain the adjusted address. */
6899 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6900 }
6901
6902 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
6903
6904 /* An adjusted breakpoint address can significantly alter
6905 a user's expectations. Print a warning if an adjustment
6906 is required. */
6907 if (adjusted_bpaddr != bpaddr)
6908 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6909
6910 return adjusted_bpaddr;
6911 }
6912 }
6913
6914 static bp_loc_type
6915 bp_location_from_bp_type (bptype type)
6916 {
6917 switch (type)
6918 {
6919 case bp_breakpoint:
6920 case bp_single_step:
6921 case bp_until:
6922 case bp_finish:
6923 case bp_longjmp:
6924 case bp_longjmp_resume:
6925 case bp_longjmp_call_dummy:
6926 case bp_exception:
6927 case bp_exception_resume:
6928 case bp_step_resume:
6929 case bp_hp_step_resume:
6930 case bp_watchpoint_scope:
6931 case bp_call_dummy:
6932 case bp_std_terminate:
6933 case bp_shlib_event:
6934 case bp_thread_event:
6935 case bp_overlay_event:
6936 case bp_jit_event:
6937 case bp_longjmp_master:
6938 case bp_std_terminate_master:
6939 case bp_exception_master:
6940 case bp_gnu_ifunc_resolver:
6941 case bp_gnu_ifunc_resolver_return:
6942 case bp_dprintf:
6943 return bp_loc_software_breakpoint;
6944 case bp_hardware_breakpoint:
6945 return bp_loc_hardware_breakpoint;
6946 case bp_hardware_watchpoint:
6947 case bp_read_watchpoint:
6948 case bp_access_watchpoint:
6949 return bp_loc_hardware_watchpoint;
6950 case bp_watchpoint:
6951 case bp_catchpoint:
6952 case bp_tracepoint:
6953 case bp_fast_tracepoint:
6954 case bp_static_tracepoint:
6955 return bp_loc_other;
6956 default:
6957 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6958 }
6959 }
6960
6961 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6962 {
6963 this->owner = owner;
6964 this->cond_bytecode = NULL;
6965 this->shlib_disabled = 0;
6966 this->enabled = 1;
6967
6968 this->loc_type = type;
6969
6970 if (this->loc_type == bp_loc_software_breakpoint
6971 || this->loc_type == bp_loc_hardware_breakpoint)
6972 mark_breakpoint_location_modified (this);
6973
6974 this->refc = 1;
6975 }
6976
6977 bp_location::bp_location (breakpoint *owner)
6978 : bp_location::bp_location (owner,
6979 bp_location_from_bp_type (owner->type))
6980 {
6981 }
6982
6983 /* Allocate a struct bp_location. */
6984
6985 static struct bp_location *
6986 allocate_bp_location (struct breakpoint *bpt)
6987 {
6988 return bpt->ops->allocate_location (bpt);
6989 }
6990
6991 static void
6992 free_bp_location (struct bp_location *loc)
6993 {
6994 delete loc;
6995 }
6996
6997 /* Increment reference count. */
6998
6999 static void
7000 incref_bp_location (struct bp_location *bl)
7001 {
7002 ++bl->refc;
7003 }
7004
7005 /* Decrement reference count. If the reference count reaches 0,
7006 destroy the bp_location. Sets *BLP to NULL. */
7007
7008 static void
7009 decref_bp_location (struct bp_location **blp)
7010 {
7011 gdb_assert ((*blp)->refc > 0);
7012
7013 if (--(*blp)->refc == 0)
7014 free_bp_location (*blp);
7015 *blp = NULL;
7016 }
7017
7018 /* Add breakpoint B at the end of the global breakpoint chain. */
7019
7020 static breakpoint *
7021 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7022 {
7023 struct breakpoint *b1;
7024 struct breakpoint *result = b.get ();
7025
7026 /* Add this breakpoint to the end of the chain so that a list of
7027 breakpoints will come out in order of increasing numbers. */
7028
7029 b1 = breakpoint_chain;
7030 if (b1 == 0)
7031 breakpoint_chain = b.release ();
7032 else
7033 {
7034 while (b1->next)
7035 b1 = b1->next;
7036 b1->next = b.release ();
7037 }
7038
7039 return result;
7040 }
7041
7042 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7043
7044 static void
7045 init_raw_breakpoint_without_location (struct breakpoint *b,
7046 struct gdbarch *gdbarch,
7047 enum bptype bptype,
7048 const struct breakpoint_ops *ops)
7049 {
7050 gdb_assert (ops != NULL);
7051
7052 b->ops = ops;
7053 b->type = bptype;
7054 b->gdbarch = gdbarch;
7055 b->language = current_language->la_language;
7056 b->input_radix = input_radix;
7057 b->related_breakpoint = b;
7058 }
7059
7060 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7061 that has type BPTYPE and has no locations as yet. */
7062
7063 static struct breakpoint *
7064 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7065 enum bptype bptype,
7066 const struct breakpoint_ops *ops)
7067 {
7068 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7069
7070 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7071 return add_to_breakpoint_chain (std::move (b));
7072 }
7073
7074 /* Initialize loc->function_name. */
7075
7076 static void
7077 set_breakpoint_location_function (struct bp_location *loc)
7078 {
7079 gdb_assert (loc->owner != NULL);
7080
7081 if (loc->owner->type == bp_breakpoint
7082 || loc->owner->type == bp_hardware_breakpoint
7083 || is_tracepoint (loc->owner))
7084 {
7085 const char *function_name;
7086
7087 if (loc->msymbol != NULL
7088 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7089 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7090 {
7091 struct breakpoint *b = loc->owner;
7092
7093 function_name = loc->msymbol->linkage_name ();
7094
7095 if (b->type == bp_breakpoint && b->loc == loc
7096 && loc->next == NULL && b->related_breakpoint == b)
7097 {
7098 /* Create only the whole new breakpoint of this type but do not
7099 mess more complicated breakpoints with multiple locations. */
7100 b->type = bp_gnu_ifunc_resolver;
7101 /* Remember the resolver's address for use by the return
7102 breakpoint. */
7103 loc->related_address = loc->address;
7104 }
7105 }
7106 else
7107 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7108
7109 if (function_name)
7110 loc->function_name = xstrdup (function_name);
7111 }
7112 }
7113
7114 /* Attempt to determine architecture of location identified by SAL. */
7115 struct gdbarch *
7116 get_sal_arch (struct symtab_and_line sal)
7117 {
7118 if (sal.section)
7119 return get_objfile_arch (sal.section->objfile);
7120 if (sal.symtab)
7121 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7122
7123 return NULL;
7124 }
7125
7126 /* Low level routine for partially initializing a breakpoint of type
7127 BPTYPE. The newly created breakpoint's address, section, source
7128 file name, and line number are provided by SAL.
7129
7130 It is expected that the caller will complete the initialization of
7131 the newly created breakpoint struct as well as output any status
7132 information regarding the creation of a new breakpoint. */
7133
7134 static void
7135 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7136 struct symtab_and_line sal, enum bptype bptype,
7137 const struct breakpoint_ops *ops)
7138 {
7139 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7140
7141 add_location_to_breakpoint (b, &sal);
7142
7143 if (bptype != bp_catchpoint)
7144 gdb_assert (sal.pspace != NULL);
7145
7146 /* Store the program space that was used to set the breakpoint,
7147 except for ordinary breakpoints, which are independent of the
7148 program space. */
7149 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7150 b->pspace = sal.pspace;
7151 }
7152
7153 /* set_raw_breakpoint is a low level routine for allocating and
7154 partially initializing a breakpoint of type BPTYPE. The newly
7155 created breakpoint's address, section, source file name, and line
7156 number are provided by SAL. The newly created and partially
7157 initialized breakpoint is added to the breakpoint chain and
7158 is also returned as the value of this function.
7159
7160 It is expected that the caller will complete the initialization of
7161 the newly created breakpoint struct as well as output any status
7162 information regarding the creation of a new breakpoint. In
7163 particular, set_raw_breakpoint does NOT set the breakpoint
7164 number! Care should be taken to not allow an error to occur
7165 prior to completing the initialization of the breakpoint. If this
7166 should happen, a bogus breakpoint will be left on the chain. */
7167
7168 struct breakpoint *
7169 set_raw_breakpoint (struct gdbarch *gdbarch,
7170 struct symtab_and_line sal, enum bptype bptype,
7171 const struct breakpoint_ops *ops)
7172 {
7173 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7174
7175 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7176 return add_to_breakpoint_chain (std::move (b));
7177 }
7178
7179 /* Call this routine when stepping and nexting to enable a breakpoint
7180 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7181 initiated the operation. */
7182
7183 void
7184 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7185 {
7186 struct breakpoint *b, *b_tmp;
7187 int thread = tp->global_num;
7188
7189 /* To avoid having to rescan all objfile symbols at every step,
7190 we maintain a list of continually-inserted but always disabled
7191 longjmp "master" breakpoints. Here, we simply create momentary
7192 clones of those and enable them for the requested thread. */
7193 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7194 if (b->pspace == current_program_space
7195 && (b->type == bp_longjmp_master
7196 || b->type == bp_exception_master))
7197 {
7198 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7199 struct breakpoint *clone;
7200
7201 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7202 after their removal. */
7203 clone = momentary_breakpoint_from_master (b, type,
7204 &momentary_breakpoint_ops, 1);
7205 clone->thread = thread;
7206 }
7207
7208 tp->initiating_frame = frame;
7209 }
7210
7211 /* Delete all longjmp breakpoints from THREAD. */
7212 void
7213 delete_longjmp_breakpoint (int thread)
7214 {
7215 struct breakpoint *b, *b_tmp;
7216
7217 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7218 if (b->type == bp_longjmp || b->type == bp_exception)
7219 {
7220 if (b->thread == thread)
7221 delete_breakpoint (b);
7222 }
7223 }
7224
7225 void
7226 delete_longjmp_breakpoint_at_next_stop (int thread)
7227 {
7228 struct breakpoint *b, *b_tmp;
7229
7230 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7231 if (b->type == bp_longjmp || b->type == bp_exception)
7232 {
7233 if (b->thread == thread)
7234 b->disposition = disp_del_at_next_stop;
7235 }
7236 }
7237
7238 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7239 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7240 pointer to any of them. Return NULL if this system cannot place longjmp
7241 breakpoints. */
7242
7243 struct breakpoint *
7244 set_longjmp_breakpoint_for_call_dummy (void)
7245 {
7246 struct breakpoint *b, *retval = NULL;
7247
7248 ALL_BREAKPOINTS (b)
7249 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7250 {
7251 struct breakpoint *new_b;
7252
7253 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7254 &momentary_breakpoint_ops,
7255 1);
7256 new_b->thread = inferior_thread ()->global_num;
7257
7258 /* Link NEW_B into the chain of RETVAL breakpoints. */
7259
7260 gdb_assert (new_b->related_breakpoint == new_b);
7261 if (retval == NULL)
7262 retval = new_b;
7263 new_b->related_breakpoint = retval;
7264 while (retval->related_breakpoint != new_b->related_breakpoint)
7265 retval = retval->related_breakpoint;
7266 retval->related_breakpoint = new_b;
7267 }
7268
7269 return retval;
7270 }
7271
7272 /* Verify all existing dummy frames and their associated breakpoints for
7273 TP. Remove those which can no longer be found in the current frame
7274 stack.
7275
7276 You should call this function only at places where it is safe to currently
7277 unwind the whole stack. Failed stack unwind would discard live dummy
7278 frames. */
7279
7280 void
7281 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7282 {
7283 struct breakpoint *b, *b_tmp;
7284
7285 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7286 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7287 {
7288 struct breakpoint *dummy_b = b->related_breakpoint;
7289
7290 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7291 dummy_b = dummy_b->related_breakpoint;
7292 if (dummy_b->type != bp_call_dummy
7293 || frame_find_by_id (dummy_b->frame_id) != NULL)
7294 continue;
7295
7296 dummy_frame_discard (dummy_b->frame_id, tp);
7297
7298 while (b->related_breakpoint != b)
7299 {
7300 if (b_tmp == b->related_breakpoint)
7301 b_tmp = b->related_breakpoint->next;
7302 delete_breakpoint (b->related_breakpoint);
7303 }
7304 delete_breakpoint (b);
7305 }
7306 }
7307
7308 void
7309 enable_overlay_breakpoints (void)
7310 {
7311 struct breakpoint *b;
7312
7313 ALL_BREAKPOINTS (b)
7314 if (b->type == bp_overlay_event)
7315 {
7316 b->enable_state = bp_enabled;
7317 update_global_location_list (UGLL_MAY_INSERT);
7318 overlay_events_enabled = 1;
7319 }
7320 }
7321
7322 void
7323 disable_overlay_breakpoints (void)
7324 {
7325 struct breakpoint *b;
7326
7327 ALL_BREAKPOINTS (b)
7328 if (b->type == bp_overlay_event)
7329 {
7330 b->enable_state = bp_disabled;
7331 update_global_location_list (UGLL_DONT_INSERT);
7332 overlay_events_enabled = 0;
7333 }
7334 }
7335
7336 /* Set an active std::terminate breakpoint for each std::terminate
7337 master breakpoint. */
7338 void
7339 set_std_terminate_breakpoint (void)
7340 {
7341 struct breakpoint *b, *b_tmp;
7342
7343 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7344 if (b->pspace == current_program_space
7345 && b->type == bp_std_terminate_master)
7346 {
7347 momentary_breakpoint_from_master (b, bp_std_terminate,
7348 &momentary_breakpoint_ops, 1);
7349 }
7350 }
7351
7352 /* Delete all the std::terminate breakpoints. */
7353 void
7354 delete_std_terminate_breakpoint (void)
7355 {
7356 struct breakpoint *b, *b_tmp;
7357
7358 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7359 if (b->type == bp_std_terminate)
7360 delete_breakpoint (b);
7361 }
7362
7363 struct breakpoint *
7364 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7365 {
7366 struct breakpoint *b;
7367
7368 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7369 &internal_breakpoint_ops);
7370
7371 b->enable_state = bp_enabled;
7372 /* location has to be used or breakpoint_re_set will delete me. */
7373 b->location = new_address_location (b->loc->address, NULL, 0);
7374
7375 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7376
7377 return b;
7378 }
7379
7380 struct lang_and_radix
7381 {
7382 enum language lang;
7383 int radix;
7384 };
7385
7386 /* Create a breakpoint for JIT code registration and unregistration. */
7387
7388 struct breakpoint *
7389 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7390 {
7391 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7392 &internal_breakpoint_ops);
7393 }
7394
7395 /* Remove JIT code registration and unregistration breakpoint(s). */
7396
7397 void
7398 remove_jit_event_breakpoints (void)
7399 {
7400 struct breakpoint *b, *b_tmp;
7401
7402 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7403 if (b->type == bp_jit_event
7404 && b->loc->pspace == current_program_space)
7405 delete_breakpoint (b);
7406 }
7407
7408 void
7409 remove_solib_event_breakpoints (void)
7410 {
7411 struct breakpoint *b, *b_tmp;
7412
7413 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7414 if (b->type == bp_shlib_event
7415 && b->loc->pspace == current_program_space)
7416 delete_breakpoint (b);
7417 }
7418
7419 /* See breakpoint.h. */
7420
7421 void
7422 remove_solib_event_breakpoints_at_next_stop (void)
7423 {
7424 struct breakpoint *b, *b_tmp;
7425
7426 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7427 if (b->type == bp_shlib_event
7428 && b->loc->pspace == current_program_space)
7429 b->disposition = disp_del_at_next_stop;
7430 }
7431
7432 /* Helper for create_solib_event_breakpoint /
7433 create_and_insert_solib_event_breakpoint. Allows specifying which
7434 INSERT_MODE to pass through to update_global_location_list. */
7435
7436 static struct breakpoint *
7437 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7438 enum ugll_insert_mode insert_mode)
7439 {
7440 struct breakpoint *b;
7441
7442 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7443 &internal_breakpoint_ops);
7444 update_global_location_list_nothrow (insert_mode);
7445 return b;
7446 }
7447
7448 struct breakpoint *
7449 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7450 {
7451 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7452 }
7453
7454 /* See breakpoint.h. */
7455
7456 struct breakpoint *
7457 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7458 {
7459 struct breakpoint *b;
7460
7461 /* Explicitly tell update_global_location_list to insert
7462 locations. */
7463 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7464 if (!b->loc->inserted)
7465 {
7466 delete_breakpoint (b);
7467 return NULL;
7468 }
7469 return b;
7470 }
7471
7472 /* Disable any breakpoints that are on code in shared libraries. Only
7473 apply to enabled breakpoints, disabled ones can just stay disabled. */
7474
7475 void
7476 disable_breakpoints_in_shlibs (void)
7477 {
7478 struct bp_location *loc, **locp_tmp;
7479
7480 ALL_BP_LOCATIONS (loc, locp_tmp)
7481 {
7482 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7483 struct breakpoint *b = loc->owner;
7484
7485 /* We apply the check to all breakpoints, including disabled for
7486 those with loc->duplicate set. This is so that when breakpoint
7487 becomes enabled, or the duplicate is removed, gdb will try to
7488 insert all breakpoints. If we don't set shlib_disabled here,
7489 we'll try to insert those breakpoints and fail. */
7490 if (((b->type == bp_breakpoint)
7491 || (b->type == bp_jit_event)
7492 || (b->type == bp_hardware_breakpoint)
7493 || (is_tracepoint (b)))
7494 && loc->pspace == current_program_space
7495 && !loc->shlib_disabled
7496 && solib_name_from_address (loc->pspace, loc->address)
7497 )
7498 {
7499 loc->shlib_disabled = 1;
7500 }
7501 }
7502 }
7503
7504 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7505 notification of unloaded_shlib. Only apply to enabled breakpoints,
7506 disabled ones can just stay disabled. */
7507
7508 static void
7509 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7510 {
7511 struct bp_location *loc, **locp_tmp;
7512 int disabled_shlib_breaks = 0;
7513
7514 ALL_BP_LOCATIONS (loc, locp_tmp)
7515 {
7516 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7517 struct breakpoint *b = loc->owner;
7518
7519 if (solib->pspace == loc->pspace
7520 && !loc->shlib_disabled
7521 && (((b->type == bp_breakpoint
7522 || b->type == bp_jit_event
7523 || b->type == bp_hardware_breakpoint)
7524 && (loc->loc_type == bp_loc_hardware_breakpoint
7525 || loc->loc_type == bp_loc_software_breakpoint))
7526 || is_tracepoint (b))
7527 && solib_contains_address_p (solib, loc->address))
7528 {
7529 loc->shlib_disabled = 1;
7530 /* At this point, we cannot rely on remove_breakpoint
7531 succeeding so we must mark the breakpoint as not inserted
7532 to prevent future errors occurring in remove_breakpoints. */
7533 loc->inserted = 0;
7534
7535 /* This may cause duplicate notifications for the same breakpoint. */
7536 gdb::observers::breakpoint_modified.notify (b);
7537
7538 if (!disabled_shlib_breaks)
7539 {
7540 target_terminal::ours_for_output ();
7541 warning (_("Temporarily disabling breakpoints "
7542 "for unloaded shared library \"%s\""),
7543 solib->so_name);
7544 }
7545 disabled_shlib_breaks = 1;
7546 }
7547 }
7548 }
7549
7550 /* Disable any breakpoints and tracepoints in OBJFILE upon
7551 notification of free_objfile. Only apply to enabled breakpoints,
7552 disabled ones can just stay disabled. */
7553
7554 static void
7555 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7556 {
7557 struct breakpoint *b;
7558
7559 if (objfile == NULL)
7560 return;
7561
7562 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7563 managed by the user with add-symbol-file/remove-symbol-file.
7564 Similarly to how breakpoints in shared libraries are handled in
7565 response to "nosharedlibrary", mark breakpoints in such modules
7566 shlib_disabled so they end up uninserted on the next global
7567 location list update. Shared libraries not loaded by the user
7568 aren't handled here -- they're already handled in
7569 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7570 solib_unloaded observer. We skip objfiles that are not
7571 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7572 main objfile). */
7573 if ((objfile->flags & OBJF_SHARED) == 0
7574 || (objfile->flags & OBJF_USERLOADED) == 0)
7575 return;
7576
7577 ALL_BREAKPOINTS (b)
7578 {
7579 struct bp_location *loc;
7580 int bp_modified = 0;
7581
7582 if (!is_breakpoint (b) && !is_tracepoint (b))
7583 continue;
7584
7585 for (loc = b->loc; loc != NULL; loc = loc->next)
7586 {
7587 CORE_ADDR loc_addr = loc->address;
7588
7589 if (loc->loc_type != bp_loc_hardware_breakpoint
7590 && loc->loc_type != bp_loc_software_breakpoint)
7591 continue;
7592
7593 if (loc->shlib_disabled != 0)
7594 continue;
7595
7596 if (objfile->pspace != loc->pspace)
7597 continue;
7598
7599 if (loc->loc_type != bp_loc_hardware_breakpoint
7600 && loc->loc_type != bp_loc_software_breakpoint)
7601 continue;
7602
7603 if (is_addr_in_objfile (loc_addr, objfile))
7604 {
7605 loc->shlib_disabled = 1;
7606 /* At this point, we don't know whether the object was
7607 unmapped from the inferior or not, so leave the
7608 inserted flag alone. We'll handle failure to
7609 uninsert quietly, in case the object was indeed
7610 unmapped. */
7611
7612 mark_breakpoint_location_modified (loc);
7613
7614 bp_modified = 1;
7615 }
7616 }
7617
7618 if (bp_modified)
7619 gdb::observers::breakpoint_modified.notify (b);
7620 }
7621 }
7622
7623 /* FORK & VFORK catchpoints. */
7624
7625 /* An instance of this type is used to represent a fork or vfork
7626 catchpoint. A breakpoint is really of this type iff its ops pointer points
7627 to CATCH_FORK_BREAKPOINT_OPS. */
7628
7629 struct fork_catchpoint : public breakpoint
7630 {
7631 /* Process id of a child process whose forking triggered this
7632 catchpoint. This field is only valid immediately after this
7633 catchpoint has triggered. */
7634 ptid_t forked_inferior_pid;
7635 };
7636
7637 /* Implement the "insert" breakpoint_ops method for fork
7638 catchpoints. */
7639
7640 static int
7641 insert_catch_fork (struct bp_location *bl)
7642 {
7643 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7644 }
7645
7646 /* Implement the "remove" breakpoint_ops method for fork
7647 catchpoints. */
7648
7649 static int
7650 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7651 {
7652 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7653 }
7654
7655 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7656 catchpoints. */
7657
7658 static int
7659 breakpoint_hit_catch_fork (const struct bp_location *bl,
7660 const address_space *aspace, CORE_ADDR bp_addr,
7661 const struct target_waitstatus *ws)
7662 {
7663 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7664
7665 if (ws->kind != TARGET_WAITKIND_FORKED)
7666 return 0;
7667
7668 c->forked_inferior_pid = ws->value.related_pid;
7669 return 1;
7670 }
7671
7672 /* Implement the "print_it" breakpoint_ops method for fork
7673 catchpoints. */
7674
7675 static enum print_stop_action
7676 print_it_catch_fork (bpstat bs)
7677 {
7678 struct ui_out *uiout = current_uiout;
7679 struct breakpoint *b = bs->breakpoint_at;
7680 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7681
7682 annotate_catchpoint (b->number);
7683 maybe_print_thread_hit_breakpoint (uiout);
7684 if (b->disposition == disp_del)
7685 uiout->text ("Temporary catchpoint ");
7686 else
7687 uiout->text ("Catchpoint ");
7688 if (uiout->is_mi_like_p ())
7689 {
7690 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7691 uiout->field_string ("disp", bpdisp_text (b->disposition));
7692 }
7693 uiout->field_signed ("bkptno", b->number);
7694 uiout->text (" (forked process ");
7695 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7696 uiout->text ("), ");
7697 return PRINT_SRC_AND_LOC;
7698 }
7699
7700 /* Implement the "print_one" breakpoint_ops method for fork
7701 catchpoints. */
7702
7703 static void
7704 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7705 {
7706 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7707 struct value_print_options opts;
7708 struct ui_out *uiout = current_uiout;
7709
7710 get_user_print_options (&opts);
7711
7712 /* Field 4, the address, is omitted (which makes the columns not
7713 line up too nicely with the headers, but the effect is relatively
7714 readable). */
7715 if (opts.addressprint)
7716 uiout->field_skip ("addr");
7717 annotate_field (5);
7718 uiout->text ("fork");
7719 if (c->forked_inferior_pid != null_ptid)
7720 {
7721 uiout->text (", process ");
7722 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7723 uiout->spaces (1);
7724 }
7725
7726 if (uiout->is_mi_like_p ())
7727 uiout->field_string ("catch-type", "fork");
7728 }
7729
7730 /* Implement the "print_mention" breakpoint_ops method for fork
7731 catchpoints. */
7732
7733 static void
7734 print_mention_catch_fork (struct breakpoint *b)
7735 {
7736 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7737 }
7738
7739 /* Implement the "print_recreate" breakpoint_ops method for fork
7740 catchpoints. */
7741
7742 static void
7743 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7744 {
7745 fprintf_unfiltered (fp, "catch fork");
7746 print_recreate_thread (b, fp);
7747 }
7748
7749 /* The breakpoint_ops structure to be used in fork catchpoints. */
7750
7751 static struct breakpoint_ops catch_fork_breakpoint_ops;
7752
7753 /* Implement the "insert" breakpoint_ops method for vfork
7754 catchpoints. */
7755
7756 static int
7757 insert_catch_vfork (struct bp_location *bl)
7758 {
7759 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7760 }
7761
7762 /* Implement the "remove" breakpoint_ops method for vfork
7763 catchpoints. */
7764
7765 static int
7766 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7767 {
7768 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7769 }
7770
7771 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7772 catchpoints. */
7773
7774 static int
7775 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7776 const address_space *aspace, CORE_ADDR bp_addr,
7777 const struct target_waitstatus *ws)
7778 {
7779 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7780
7781 if (ws->kind != TARGET_WAITKIND_VFORKED)
7782 return 0;
7783
7784 c->forked_inferior_pid = ws->value.related_pid;
7785 return 1;
7786 }
7787
7788 /* Implement the "print_it" breakpoint_ops method for vfork
7789 catchpoints. */
7790
7791 static enum print_stop_action
7792 print_it_catch_vfork (bpstat bs)
7793 {
7794 struct ui_out *uiout = current_uiout;
7795 struct breakpoint *b = bs->breakpoint_at;
7796 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7797
7798 annotate_catchpoint (b->number);
7799 maybe_print_thread_hit_breakpoint (uiout);
7800 if (b->disposition == disp_del)
7801 uiout->text ("Temporary catchpoint ");
7802 else
7803 uiout->text ("Catchpoint ");
7804 if (uiout->is_mi_like_p ())
7805 {
7806 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7807 uiout->field_string ("disp", bpdisp_text (b->disposition));
7808 }
7809 uiout->field_signed ("bkptno", b->number);
7810 uiout->text (" (vforked process ");
7811 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7812 uiout->text ("), ");
7813 return PRINT_SRC_AND_LOC;
7814 }
7815
7816 /* Implement the "print_one" breakpoint_ops method for vfork
7817 catchpoints. */
7818
7819 static void
7820 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7821 {
7822 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7823 struct value_print_options opts;
7824 struct ui_out *uiout = current_uiout;
7825
7826 get_user_print_options (&opts);
7827 /* Field 4, the address, is omitted (which makes the columns not
7828 line up too nicely with the headers, but the effect is relatively
7829 readable). */
7830 if (opts.addressprint)
7831 uiout->field_skip ("addr");
7832 annotate_field (5);
7833 uiout->text ("vfork");
7834 if (c->forked_inferior_pid != null_ptid)
7835 {
7836 uiout->text (", process ");
7837 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7838 uiout->spaces (1);
7839 }
7840
7841 if (uiout->is_mi_like_p ())
7842 uiout->field_string ("catch-type", "vfork");
7843 }
7844
7845 /* Implement the "print_mention" breakpoint_ops method for vfork
7846 catchpoints. */
7847
7848 static void
7849 print_mention_catch_vfork (struct breakpoint *b)
7850 {
7851 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7852 }
7853
7854 /* Implement the "print_recreate" breakpoint_ops method for vfork
7855 catchpoints. */
7856
7857 static void
7858 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7859 {
7860 fprintf_unfiltered (fp, "catch vfork");
7861 print_recreate_thread (b, fp);
7862 }
7863
7864 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7865
7866 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7867
7868 /* An instance of this type is used to represent an solib catchpoint.
7869 A breakpoint is really of this type iff its ops pointer points to
7870 CATCH_SOLIB_BREAKPOINT_OPS. */
7871
7872 struct solib_catchpoint : public breakpoint
7873 {
7874 ~solib_catchpoint () override;
7875
7876 /* True for "catch load", false for "catch unload". */
7877 unsigned char is_load;
7878
7879 /* Regular expression to match, if any. COMPILED is only valid when
7880 REGEX is non-NULL. */
7881 char *regex;
7882 std::unique_ptr<compiled_regex> compiled;
7883 };
7884
7885 solib_catchpoint::~solib_catchpoint ()
7886 {
7887 xfree (this->regex);
7888 }
7889
7890 static int
7891 insert_catch_solib (struct bp_location *ignore)
7892 {
7893 return 0;
7894 }
7895
7896 static int
7897 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7898 {
7899 return 0;
7900 }
7901
7902 static int
7903 breakpoint_hit_catch_solib (const struct bp_location *bl,
7904 const address_space *aspace,
7905 CORE_ADDR bp_addr,
7906 const struct target_waitstatus *ws)
7907 {
7908 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7909 struct breakpoint *other;
7910
7911 if (ws->kind == TARGET_WAITKIND_LOADED)
7912 return 1;
7913
7914 ALL_BREAKPOINTS (other)
7915 {
7916 struct bp_location *other_bl;
7917
7918 if (other == bl->owner)
7919 continue;
7920
7921 if (other->type != bp_shlib_event)
7922 continue;
7923
7924 if (self->pspace != NULL && other->pspace != self->pspace)
7925 continue;
7926
7927 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7928 {
7929 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7930 return 1;
7931 }
7932 }
7933
7934 return 0;
7935 }
7936
7937 static void
7938 check_status_catch_solib (struct bpstats *bs)
7939 {
7940 struct solib_catchpoint *self
7941 = (struct solib_catchpoint *) bs->breakpoint_at;
7942
7943 if (self->is_load)
7944 {
7945 for (so_list *iter : current_program_space->added_solibs)
7946 {
7947 if (!self->regex
7948 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
7949 return;
7950 }
7951 }
7952 else
7953 {
7954 for (const std::string &iter : current_program_space->deleted_solibs)
7955 {
7956 if (!self->regex
7957 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
7958 return;
7959 }
7960 }
7961
7962 bs->stop = 0;
7963 bs->print_it = print_it_noop;
7964 }
7965
7966 static enum print_stop_action
7967 print_it_catch_solib (bpstat bs)
7968 {
7969 struct breakpoint *b = bs->breakpoint_at;
7970 struct ui_out *uiout = current_uiout;
7971
7972 annotate_catchpoint (b->number);
7973 maybe_print_thread_hit_breakpoint (uiout);
7974 if (b->disposition == disp_del)
7975 uiout->text ("Temporary catchpoint ");
7976 else
7977 uiout->text ("Catchpoint ");
7978 uiout->field_signed ("bkptno", b->number);
7979 uiout->text ("\n");
7980 if (uiout->is_mi_like_p ())
7981 uiout->field_string ("disp", bpdisp_text (b->disposition));
7982 print_solib_event (1);
7983 return PRINT_SRC_AND_LOC;
7984 }
7985
7986 static void
7987 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7988 {
7989 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7990 struct value_print_options opts;
7991 struct ui_out *uiout = current_uiout;
7992
7993 get_user_print_options (&opts);
7994 /* Field 4, the address, is omitted (which makes the columns not
7995 line up too nicely with the headers, but the effect is relatively
7996 readable). */
7997 if (opts.addressprint)
7998 {
7999 annotate_field (4);
8000 uiout->field_skip ("addr");
8001 }
8002
8003 std::string msg;
8004 annotate_field (5);
8005 if (self->is_load)
8006 {
8007 if (self->regex)
8008 msg = string_printf (_("load of library matching %s"), self->regex);
8009 else
8010 msg = _("load of library");
8011 }
8012 else
8013 {
8014 if (self->regex)
8015 msg = string_printf (_("unload of library matching %s"), self->regex);
8016 else
8017 msg = _("unload of library");
8018 }
8019 uiout->field_string ("what", msg);
8020
8021 if (uiout->is_mi_like_p ())
8022 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8023 }
8024
8025 static void
8026 print_mention_catch_solib (struct breakpoint *b)
8027 {
8028 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8029
8030 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8031 self->is_load ? "load" : "unload");
8032 }
8033
8034 static void
8035 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8036 {
8037 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8038
8039 fprintf_unfiltered (fp, "%s %s",
8040 b->disposition == disp_del ? "tcatch" : "catch",
8041 self->is_load ? "load" : "unload");
8042 if (self->regex)
8043 fprintf_unfiltered (fp, " %s", self->regex);
8044 fprintf_unfiltered (fp, "\n");
8045 }
8046
8047 static struct breakpoint_ops catch_solib_breakpoint_ops;
8048
8049 /* Shared helper function (MI and CLI) for creating and installing
8050 a shared object event catchpoint. If IS_LOAD is non-zero then
8051 the events to be caught are load events, otherwise they are
8052 unload events. If IS_TEMP is non-zero the catchpoint is a
8053 temporary one. If ENABLED is non-zero the catchpoint is
8054 created in an enabled state. */
8055
8056 void
8057 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8058 {
8059 struct gdbarch *gdbarch = get_current_arch ();
8060
8061 if (!arg)
8062 arg = "";
8063 arg = skip_spaces (arg);
8064
8065 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8066
8067 if (*arg != '\0')
8068 {
8069 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8070 _("Invalid regexp")));
8071 c->regex = xstrdup (arg);
8072 }
8073
8074 c->is_load = is_load;
8075 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8076 &catch_solib_breakpoint_ops);
8077
8078 c->enable_state = enabled ? bp_enabled : bp_disabled;
8079
8080 install_breakpoint (0, std::move (c), 1);
8081 }
8082
8083 /* A helper function that does all the work for "catch load" and
8084 "catch unload". */
8085
8086 static void
8087 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8088 struct cmd_list_element *command)
8089 {
8090 int tempflag;
8091 const int enabled = 1;
8092
8093 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8094
8095 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8096 }
8097
8098 static void
8099 catch_load_command_1 (const char *arg, int from_tty,
8100 struct cmd_list_element *command)
8101 {
8102 catch_load_or_unload (arg, from_tty, 1, command);
8103 }
8104
8105 static void
8106 catch_unload_command_1 (const char *arg, int from_tty,
8107 struct cmd_list_element *command)
8108 {
8109 catch_load_or_unload (arg, from_tty, 0, command);
8110 }
8111
8112 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8113 is non-zero, then make the breakpoint temporary. If COND_STRING is
8114 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8115 the breakpoint_ops structure associated to the catchpoint. */
8116
8117 void
8118 init_catchpoint (struct breakpoint *b,
8119 struct gdbarch *gdbarch, int tempflag,
8120 const char *cond_string,
8121 const struct breakpoint_ops *ops)
8122 {
8123 symtab_and_line sal;
8124 sal.pspace = current_program_space;
8125
8126 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8127
8128 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8129 b->disposition = tempflag ? disp_del : disp_donttouch;
8130 }
8131
8132 void
8133 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8134 {
8135 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8136 set_breakpoint_number (internal, b);
8137 if (is_tracepoint (b))
8138 set_tracepoint_count (breakpoint_count);
8139 if (!internal)
8140 mention (b);
8141 gdb::observers::breakpoint_created.notify (b);
8142
8143 if (update_gll)
8144 update_global_location_list (UGLL_MAY_INSERT);
8145 }
8146
8147 static void
8148 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8149 int tempflag, const char *cond_string,
8150 const struct breakpoint_ops *ops)
8151 {
8152 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8153
8154 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8155
8156 c->forked_inferior_pid = null_ptid;
8157
8158 install_breakpoint (0, std::move (c), 1);
8159 }
8160
8161 /* Exec catchpoints. */
8162
8163 /* An instance of this type is used to represent an exec catchpoint.
8164 A breakpoint is really of this type iff its ops pointer points to
8165 CATCH_EXEC_BREAKPOINT_OPS. */
8166
8167 struct exec_catchpoint : public breakpoint
8168 {
8169 ~exec_catchpoint () override;
8170
8171 /* Filename of a program whose exec triggered this catchpoint.
8172 This field is only valid immediately after this catchpoint has
8173 triggered. */
8174 char *exec_pathname;
8175 };
8176
8177 /* Exec catchpoint destructor. */
8178
8179 exec_catchpoint::~exec_catchpoint ()
8180 {
8181 xfree (this->exec_pathname);
8182 }
8183
8184 static int
8185 insert_catch_exec (struct bp_location *bl)
8186 {
8187 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8188 }
8189
8190 static int
8191 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8192 {
8193 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8194 }
8195
8196 static int
8197 breakpoint_hit_catch_exec (const struct bp_location *bl,
8198 const address_space *aspace, CORE_ADDR bp_addr,
8199 const struct target_waitstatus *ws)
8200 {
8201 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8202
8203 if (ws->kind != TARGET_WAITKIND_EXECD)
8204 return 0;
8205
8206 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8207 return 1;
8208 }
8209
8210 static enum print_stop_action
8211 print_it_catch_exec (bpstat bs)
8212 {
8213 struct ui_out *uiout = current_uiout;
8214 struct breakpoint *b = bs->breakpoint_at;
8215 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8216
8217 annotate_catchpoint (b->number);
8218 maybe_print_thread_hit_breakpoint (uiout);
8219 if (b->disposition == disp_del)
8220 uiout->text ("Temporary catchpoint ");
8221 else
8222 uiout->text ("Catchpoint ");
8223 if (uiout->is_mi_like_p ())
8224 {
8225 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8226 uiout->field_string ("disp", bpdisp_text (b->disposition));
8227 }
8228 uiout->field_signed ("bkptno", b->number);
8229 uiout->text (" (exec'd ");
8230 uiout->field_string ("new-exec", c->exec_pathname);
8231 uiout->text ("), ");
8232
8233 return PRINT_SRC_AND_LOC;
8234 }
8235
8236 static void
8237 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8238 {
8239 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8240 struct value_print_options opts;
8241 struct ui_out *uiout = current_uiout;
8242
8243 get_user_print_options (&opts);
8244
8245 /* Field 4, the address, is omitted (which makes the columns
8246 not line up too nicely with the headers, but the effect
8247 is relatively readable). */
8248 if (opts.addressprint)
8249 uiout->field_skip ("addr");
8250 annotate_field (5);
8251 uiout->text ("exec");
8252 if (c->exec_pathname != NULL)
8253 {
8254 uiout->text (", program \"");
8255 uiout->field_string ("what", c->exec_pathname);
8256 uiout->text ("\" ");
8257 }
8258
8259 if (uiout->is_mi_like_p ())
8260 uiout->field_string ("catch-type", "exec");
8261 }
8262
8263 static void
8264 print_mention_catch_exec (struct breakpoint *b)
8265 {
8266 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8267 }
8268
8269 /* Implement the "print_recreate" breakpoint_ops method for exec
8270 catchpoints. */
8271
8272 static void
8273 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8274 {
8275 fprintf_unfiltered (fp, "catch exec");
8276 print_recreate_thread (b, fp);
8277 }
8278
8279 static struct breakpoint_ops catch_exec_breakpoint_ops;
8280
8281 static int
8282 hw_breakpoint_used_count (void)
8283 {
8284 int i = 0;
8285 struct breakpoint *b;
8286 struct bp_location *bl;
8287
8288 ALL_BREAKPOINTS (b)
8289 {
8290 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8291 for (bl = b->loc; bl; bl = bl->next)
8292 {
8293 /* Special types of hardware breakpoints may use more than
8294 one register. */
8295 i += b->ops->resources_needed (bl);
8296 }
8297 }
8298
8299 return i;
8300 }
8301
8302 /* Returns the resources B would use if it were a hardware
8303 watchpoint. */
8304
8305 static int
8306 hw_watchpoint_use_count (struct breakpoint *b)
8307 {
8308 int i = 0;
8309 struct bp_location *bl;
8310
8311 if (!breakpoint_enabled (b))
8312 return 0;
8313
8314 for (bl = b->loc; bl; bl = bl->next)
8315 {
8316 /* Special types of hardware watchpoints may use more than
8317 one register. */
8318 i += b->ops->resources_needed (bl);
8319 }
8320
8321 return i;
8322 }
8323
8324 /* Returns the sum the used resources of all hardware watchpoints of
8325 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8326 the sum of the used resources of all hardware watchpoints of other
8327 types _not_ TYPE. */
8328
8329 static int
8330 hw_watchpoint_used_count_others (struct breakpoint *except,
8331 enum bptype type, int *other_type_used)
8332 {
8333 int i = 0;
8334 struct breakpoint *b;
8335
8336 *other_type_used = 0;
8337 ALL_BREAKPOINTS (b)
8338 {
8339 if (b == except)
8340 continue;
8341 if (!breakpoint_enabled (b))
8342 continue;
8343
8344 if (b->type == type)
8345 i += hw_watchpoint_use_count (b);
8346 else if (is_hardware_watchpoint (b))
8347 *other_type_used = 1;
8348 }
8349
8350 return i;
8351 }
8352
8353 void
8354 disable_watchpoints_before_interactive_call_start (void)
8355 {
8356 struct breakpoint *b;
8357
8358 ALL_BREAKPOINTS (b)
8359 {
8360 if (is_watchpoint (b) && breakpoint_enabled (b))
8361 {
8362 b->enable_state = bp_call_disabled;
8363 update_global_location_list (UGLL_DONT_INSERT);
8364 }
8365 }
8366 }
8367
8368 void
8369 enable_watchpoints_after_interactive_call_stop (void)
8370 {
8371 struct breakpoint *b;
8372
8373 ALL_BREAKPOINTS (b)
8374 {
8375 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8376 {
8377 b->enable_state = bp_enabled;
8378 update_global_location_list (UGLL_MAY_INSERT);
8379 }
8380 }
8381 }
8382
8383 void
8384 disable_breakpoints_before_startup (void)
8385 {
8386 current_program_space->executing_startup = 1;
8387 update_global_location_list (UGLL_DONT_INSERT);
8388 }
8389
8390 void
8391 enable_breakpoints_after_startup (void)
8392 {
8393 current_program_space->executing_startup = 0;
8394 breakpoint_re_set ();
8395 }
8396
8397 /* Create a new single-step breakpoint for thread THREAD, with no
8398 locations. */
8399
8400 static struct breakpoint *
8401 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8402 {
8403 std::unique_ptr<breakpoint> b (new breakpoint ());
8404
8405 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8406 &momentary_breakpoint_ops);
8407
8408 b->disposition = disp_donttouch;
8409 b->frame_id = null_frame_id;
8410
8411 b->thread = thread;
8412 gdb_assert (b->thread != 0);
8413
8414 return add_to_breakpoint_chain (std::move (b));
8415 }
8416
8417 /* Set a momentary breakpoint of type TYPE at address specified by
8418 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8419 frame. */
8420
8421 breakpoint_up
8422 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8423 struct frame_id frame_id, enum bptype type)
8424 {
8425 struct breakpoint *b;
8426
8427 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8428 tail-called one. */
8429 gdb_assert (!frame_id_artificial_p (frame_id));
8430
8431 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8432 b->enable_state = bp_enabled;
8433 b->disposition = disp_donttouch;
8434 b->frame_id = frame_id;
8435
8436 b->thread = inferior_thread ()->global_num;
8437
8438 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8439
8440 return breakpoint_up (b);
8441 }
8442
8443 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8444 The new breakpoint will have type TYPE, use OPS as its
8445 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8446
8447 static struct breakpoint *
8448 momentary_breakpoint_from_master (struct breakpoint *orig,
8449 enum bptype type,
8450 const struct breakpoint_ops *ops,
8451 int loc_enabled)
8452 {
8453 struct breakpoint *copy;
8454
8455 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8456 copy->loc = allocate_bp_location (copy);
8457 set_breakpoint_location_function (copy->loc);
8458
8459 copy->loc->gdbarch = orig->loc->gdbarch;
8460 copy->loc->requested_address = orig->loc->requested_address;
8461 copy->loc->address = orig->loc->address;
8462 copy->loc->section = orig->loc->section;
8463 copy->loc->pspace = orig->loc->pspace;
8464 copy->loc->probe = orig->loc->probe;
8465 copy->loc->line_number = orig->loc->line_number;
8466 copy->loc->symtab = orig->loc->symtab;
8467 copy->loc->enabled = loc_enabled;
8468 copy->frame_id = orig->frame_id;
8469 copy->thread = orig->thread;
8470 copy->pspace = orig->pspace;
8471
8472 copy->enable_state = bp_enabled;
8473 copy->disposition = disp_donttouch;
8474 copy->number = internal_breakpoint_number--;
8475
8476 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8477 return copy;
8478 }
8479
8480 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8481 ORIG is NULL. */
8482
8483 struct breakpoint *
8484 clone_momentary_breakpoint (struct breakpoint *orig)
8485 {
8486 /* If there's nothing to clone, then return nothing. */
8487 if (orig == NULL)
8488 return NULL;
8489
8490 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8491 }
8492
8493 breakpoint_up
8494 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8495 enum bptype type)
8496 {
8497 struct symtab_and_line sal;
8498
8499 sal = find_pc_line (pc, 0);
8500 sal.pc = pc;
8501 sal.section = find_pc_overlay (pc);
8502 sal.explicit_pc = 1;
8503
8504 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8505 }
8506 \f
8507
8508 /* Tell the user we have just set a breakpoint B. */
8509
8510 static void
8511 mention (struct breakpoint *b)
8512 {
8513 b->ops->print_mention (b);
8514 current_uiout->text ("\n");
8515 }
8516 \f
8517
8518 static int bp_loc_is_permanent (struct bp_location *loc);
8519
8520 static struct bp_location *
8521 add_location_to_breakpoint (struct breakpoint *b,
8522 const struct symtab_and_line *sal)
8523 {
8524 struct bp_location *loc, **tmp;
8525 CORE_ADDR adjusted_address;
8526 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8527
8528 if (loc_gdbarch == NULL)
8529 loc_gdbarch = b->gdbarch;
8530
8531 /* Adjust the breakpoint's address prior to allocating a location.
8532 Once we call allocate_bp_location(), that mostly uninitialized
8533 location will be placed on the location chain. Adjustment of the
8534 breakpoint may cause target_read_memory() to be called and we do
8535 not want its scan of the location chain to find a breakpoint and
8536 location that's only been partially initialized. */
8537 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8538 sal->pc, b->type);
8539
8540 /* Sort the locations by their ADDRESS. */
8541 loc = allocate_bp_location (b);
8542 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8543 tmp = &((*tmp)->next))
8544 ;
8545 loc->next = *tmp;
8546 *tmp = loc;
8547
8548 loc->requested_address = sal->pc;
8549 loc->address = adjusted_address;
8550 loc->pspace = sal->pspace;
8551 loc->probe.prob = sal->prob;
8552 loc->probe.objfile = sal->objfile;
8553 gdb_assert (loc->pspace != NULL);
8554 loc->section = sal->section;
8555 loc->gdbarch = loc_gdbarch;
8556 loc->line_number = sal->line;
8557 loc->symtab = sal->symtab;
8558 loc->symbol = sal->symbol;
8559 loc->msymbol = sal->msymbol;
8560 loc->objfile = sal->objfile;
8561
8562 set_breakpoint_location_function (loc);
8563
8564 /* While by definition, permanent breakpoints are already present in the
8565 code, we don't mark the location as inserted. Normally one would expect
8566 that GDB could rely on that breakpoint instruction to stop the program,
8567 thus removing the need to insert its own breakpoint, except that executing
8568 the breakpoint instruction can kill the target instead of reporting a
8569 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8570 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8571 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8572 breakpoint be inserted normally results in QEMU knowing about the GDB
8573 breakpoint, and thus trap before the breakpoint instruction is executed.
8574 (If GDB later needs to continue execution past the permanent breakpoint,
8575 it manually increments the PC, thus avoiding executing the breakpoint
8576 instruction.) */
8577 if (bp_loc_is_permanent (loc))
8578 loc->permanent = 1;
8579
8580 return loc;
8581 }
8582 \f
8583
8584 /* See breakpoint.h. */
8585
8586 int
8587 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8588 {
8589 int len;
8590 CORE_ADDR addr;
8591 const gdb_byte *bpoint;
8592 gdb_byte *target_mem;
8593
8594 addr = address;
8595 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8596
8597 /* Software breakpoints unsupported? */
8598 if (bpoint == NULL)
8599 return 0;
8600
8601 target_mem = (gdb_byte *) alloca (len);
8602
8603 /* Enable the automatic memory restoration from breakpoints while
8604 we read the memory. Otherwise we could say about our temporary
8605 breakpoints they are permanent. */
8606 scoped_restore restore_memory
8607 = make_scoped_restore_show_memory_breakpoints (0);
8608
8609 if (target_read_memory (address, target_mem, len) == 0
8610 && memcmp (target_mem, bpoint, len) == 0)
8611 return 1;
8612
8613 return 0;
8614 }
8615
8616 /* Return 1 if LOC is pointing to a permanent breakpoint,
8617 return 0 otherwise. */
8618
8619 static int
8620 bp_loc_is_permanent (struct bp_location *loc)
8621 {
8622 gdb_assert (loc != NULL);
8623
8624 /* If we have a non-breakpoint-backed catchpoint or a software
8625 watchpoint, just return 0. We should not attempt to read from
8626 the addresses the locations of these breakpoint types point to.
8627 program_breakpoint_here_p, below, will attempt to read
8628 memory. */
8629 if (!bl_address_is_meaningful (loc))
8630 return 0;
8631
8632 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8633 switch_to_program_space_and_thread (loc->pspace);
8634 return program_breakpoint_here_p (loc->gdbarch, loc->address);
8635 }
8636
8637 /* Build a command list for the dprintf corresponding to the current
8638 settings of the dprintf style options. */
8639
8640 static void
8641 update_dprintf_command_list (struct breakpoint *b)
8642 {
8643 char *dprintf_args = b->extra_string;
8644 char *printf_line = NULL;
8645
8646 if (!dprintf_args)
8647 return;
8648
8649 dprintf_args = skip_spaces (dprintf_args);
8650
8651 /* Allow a comma, as it may have terminated a location, but don't
8652 insist on it. */
8653 if (*dprintf_args == ',')
8654 ++dprintf_args;
8655 dprintf_args = skip_spaces (dprintf_args);
8656
8657 if (*dprintf_args != '"')
8658 error (_("Bad format string, missing '\"'."));
8659
8660 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8661 printf_line = xstrprintf ("printf %s", dprintf_args);
8662 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8663 {
8664 if (!dprintf_function)
8665 error (_("No function supplied for dprintf call"));
8666
8667 if (dprintf_channel && strlen (dprintf_channel) > 0)
8668 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8669 dprintf_function,
8670 dprintf_channel,
8671 dprintf_args);
8672 else
8673 printf_line = xstrprintf ("call (void) %s (%s)",
8674 dprintf_function,
8675 dprintf_args);
8676 }
8677 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8678 {
8679 if (target_can_run_breakpoint_commands ())
8680 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8681 else
8682 {
8683 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8684 printf_line = xstrprintf ("printf %s", dprintf_args);
8685 }
8686 }
8687 else
8688 internal_error (__FILE__, __LINE__,
8689 _("Invalid dprintf style."));
8690
8691 gdb_assert (printf_line != NULL);
8692
8693 /* Manufacture a printf sequence. */
8694 struct command_line *printf_cmd_line
8695 = new struct command_line (simple_control, printf_line);
8696 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8697 command_lines_deleter ()));
8698 }
8699
8700 /* Update all dprintf commands, making their command lists reflect
8701 current style settings. */
8702
8703 static void
8704 update_dprintf_commands (const char *args, int from_tty,
8705 struct cmd_list_element *c)
8706 {
8707 struct breakpoint *b;
8708
8709 ALL_BREAKPOINTS (b)
8710 {
8711 if (b->type == bp_dprintf)
8712 update_dprintf_command_list (b);
8713 }
8714 }
8715
8716 /* Create a breakpoint with SAL as location. Use LOCATION
8717 as a description of the location, and COND_STRING
8718 as condition expression. If LOCATION is NULL then create an
8719 "address location" from the address in the SAL. */
8720
8721 static void
8722 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8723 gdb::array_view<const symtab_and_line> sals,
8724 event_location_up &&location,
8725 gdb::unique_xmalloc_ptr<char> filter,
8726 gdb::unique_xmalloc_ptr<char> cond_string,
8727 gdb::unique_xmalloc_ptr<char> extra_string,
8728 enum bptype type, enum bpdisp disposition,
8729 int thread, int task, int ignore_count,
8730 const struct breakpoint_ops *ops, int from_tty,
8731 int enabled, int internal, unsigned flags,
8732 int display_canonical)
8733 {
8734 int i;
8735
8736 if (type == bp_hardware_breakpoint)
8737 {
8738 int target_resources_ok;
8739
8740 i = hw_breakpoint_used_count ();
8741 target_resources_ok =
8742 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8743 i + 1, 0);
8744 if (target_resources_ok == 0)
8745 error (_("No hardware breakpoint support in the target."));
8746 else if (target_resources_ok < 0)
8747 error (_("Hardware breakpoints used exceeds limit."));
8748 }
8749
8750 gdb_assert (!sals.empty ());
8751
8752 for (const auto &sal : sals)
8753 {
8754 struct bp_location *loc;
8755
8756 if (from_tty)
8757 {
8758 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8759 if (!loc_gdbarch)
8760 loc_gdbarch = gdbarch;
8761
8762 describe_other_breakpoints (loc_gdbarch,
8763 sal.pspace, sal.pc, sal.section, thread);
8764 }
8765
8766 if (&sal == &sals[0])
8767 {
8768 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8769 b->thread = thread;
8770 b->task = task;
8771
8772 b->cond_string = cond_string.release ();
8773 b->extra_string = extra_string.release ();
8774 b->ignore_count = ignore_count;
8775 b->enable_state = enabled ? bp_enabled : bp_disabled;
8776 b->disposition = disposition;
8777
8778 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8779 b->loc->inserted = 1;
8780
8781 if (type == bp_static_tracepoint)
8782 {
8783 struct tracepoint *t = (struct tracepoint *) b;
8784 struct static_tracepoint_marker marker;
8785
8786 if (strace_marker_p (b))
8787 {
8788 /* We already know the marker exists, otherwise, we
8789 wouldn't see a sal for it. */
8790 const char *p
8791 = &event_location_to_string (b->location.get ())[3];
8792 const char *endp;
8793
8794 p = skip_spaces (p);
8795
8796 endp = skip_to_space (p);
8797
8798 t->static_trace_marker_id.assign (p, endp - p);
8799
8800 printf_filtered (_("Probed static tracepoint "
8801 "marker \"%s\"\n"),
8802 t->static_trace_marker_id.c_str ());
8803 }
8804 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8805 {
8806 t->static_trace_marker_id = std::move (marker.str_id);
8807
8808 printf_filtered (_("Probed static tracepoint "
8809 "marker \"%s\"\n"),
8810 t->static_trace_marker_id.c_str ());
8811 }
8812 else
8813 warning (_("Couldn't determine the static "
8814 "tracepoint marker to probe"));
8815 }
8816
8817 loc = b->loc;
8818 }
8819 else
8820 {
8821 loc = add_location_to_breakpoint (b, &sal);
8822 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8823 loc->inserted = 1;
8824 }
8825
8826 if (b->cond_string)
8827 {
8828 const char *arg = b->cond_string;
8829
8830 loc->cond = parse_exp_1 (&arg, loc->address,
8831 block_for_pc (loc->address), 0);
8832 if (*arg)
8833 error (_("Garbage '%s' follows condition"), arg);
8834 }
8835
8836 /* Dynamic printf requires and uses additional arguments on the
8837 command line, otherwise it's an error. */
8838 if (type == bp_dprintf)
8839 {
8840 if (b->extra_string)
8841 update_dprintf_command_list (b);
8842 else
8843 error (_("Format string required"));
8844 }
8845 else if (b->extra_string)
8846 error (_("Garbage '%s' at end of command"), b->extra_string);
8847 }
8848
8849 b->display_canonical = display_canonical;
8850 if (location != NULL)
8851 b->location = std::move (location);
8852 else
8853 b->location = new_address_location (b->loc->address, NULL, 0);
8854 b->filter = std::move (filter);
8855 }
8856
8857 static void
8858 create_breakpoint_sal (struct gdbarch *gdbarch,
8859 gdb::array_view<const symtab_and_line> sals,
8860 event_location_up &&location,
8861 gdb::unique_xmalloc_ptr<char> filter,
8862 gdb::unique_xmalloc_ptr<char> cond_string,
8863 gdb::unique_xmalloc_ptr<char> extra_string,
8864 enum bptype type, enum bpdisp disposition,
8865 int thread, int task, int ignore_count,
8866 const struct breakpoint_ops *ops, int from_tty,
8867 int enabled, int internal, unsigned flags,
8868 int display_canonical)
8869 {
8870 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8871
8872 init_breakpoint_sal (b.get (), gdbarch,
8873 sals, std::move (location),
8874 std::move (filter),
8875 std::move (cond_string),
8876 std::move (extra_string),
8877 type, disposition,
8878 thread, task, ignore_count,
8879 ops, from_tty,
8880 enabled, internal, flags,
8881 display_canonical);
8882
8883 install_breakpoint (internal, std::move (b), 0);
8884 }
8885
8886 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8887 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8888 value. COND_STRING, if not NULL, specified the condition to be
8889 used for all breakpoints. Essentially the only case where
8890 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8891 function. In that case, it's still not possible to specify
8892 separate conditions for different overloaded functions, so
8893 we take just a single condition string.
8894
8895 NOTE: If the function succeeds, the caller is expected to cleanup
8896 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8897 array contents). If the function fails (error() is called), the
8898 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8899 COND and SALS arrays and each of those arrays contents. */
8900
8901 static void
8902 create_breakpoints_sal (struct gdbarch *gdbarch,
8903 struct linespec_result *canonical,
8904 gdb::unique_xmalloc_ptr<char> cond_string,
8905 gdb::unique_xmalloc_ptr<char> extra_string,
8906 enum bptype type, enum bpdisp disposition,
8907 int thread, int task, int ignore_count,
8908 const struct breakpoint_ops *ops, int from_tty,
8909 int enabled, int internal, unsigned flags)
8910 {
8911 if (canonical->pre_expanded)
8912 gdb_assert (canonical->lsals.size () == 1);
8913
8914 for (const auto &lsal : canonical->lsals)
8915 {
8916 /* Note that 'location' can be NULL in the case of a plain
8917 'break', without arguments. */
8918 event_location_up location
8919 = (canonical->location != NULL
8920 ? copy_event_location (canonical->location.get ()) : NULL);
8921 gdb::unique_xmalloc_ptr<char> filter_string
8922 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8923
8924 create_breakpoint_sal (gdbarch, lsal.sals,
8925 std::move (location),
8926 std::move (filter_string),
8927 std::move (cond_string),
8928 std::move (extra_string),
8929 type, disposition,
8930 thread, task, ignore_count, ops,
8931 from_tty, enabled, internal, flags,
8932 canonical->special_display);
8933 }
8934 }
8935
8936 /* Parse LOCATION which is assumed to be a SAL specification possibly
8937 followed by conditionals. On return, SALS contains an array of SAL
8938 addresses found. LOCATION points to the end of the SAL (for
8939 linespec locations).
8940
8941 The array and the line spec strings are allocated on the heap, it is
8942 the caller's responsibility to free them. */
8943
8944 static void
8945 parse_breakpoint_sals (const struct event_location *location,
8946 struct linespec_result *canonical)
8947 {
8948 struct symtab_and_line cursal;
8949
8950 if (event_location_type (location) == LINESPEC_LOCATION)
8951 {
8952 const char *spec = get_linespec_location (location)->spec_string;
8953
8954 if (spec == NULL)
8955 {
8956 /* The last displayed codepoint, if it's valid, is our default
8957 breakpoint address. */
8958 if (last_displayed_sal_is_valid ())
8959 {
8960 /* Set sal's pspace, pc, symtab, and line to the values
8961 corresponding to the last call to print_frame_info.
8962 Be sure to reinitialize LINE with NOTCURRENT == 0
8963 as the breakpoint line number is inappropriate otherwise.
8964 find_pc_line would adjust PC, re-set it back. */
8965 symtab_and_line sal = get_last_displayed_sal ();
8966 CORE_ADDR pc = sal.pc;
8967
8968 sal = find_pc_line (pc, 0);
8969
8970 /* "break" without arguments is equivalent to "break *PC"
8971 where PC is the last displayed codepoint's address. So
8972 make sure to set sal.explicit_pc to prevent GDB from
8973 trying to expand the list of sals to include all other
8974 instances with the same symtab and line. */
8975 sal.pc = pc;
8976 sal.explicit_pc = 1;
8977
8978 struct linespec_sals lsal;
8979 lsal.sals = {sal};
8980 lsal.canonical = NULL;
8981
8982 canonical->lsals.push_back (std::move (lsal));
8983 return;
8984 }
8985 else
8986 error (_("No default breakpoint address now."));
8987 }
8988 }
8989
8990 /* Force almost all breakpoints to be in terms of the
8991 current_source_symtab (which is decode_line_1's default).
8992 This should produce the results we want almost all of the
8993 time while leaving default_breakpoint_* alone.
8994
8995 ObjC: However, don't match an Objective-C method name which
8996 may have a '+' or '-' succeeded by a '['. */
8997 cursal = get_current_source_symtab_and_line ();
8998 if (last_displayed_sal_is_valid ())
8999 {
9000 const char *spec = NULL;
9001
9002 if (event_location_type (location) == LINESPEC_LOCATION)
9003 spec = get_linespec_location (location)->spec_string;
9004
9005 if (!cursal.symtab
9006 || (spec != NULL
9007 && strchr ("+-", spec[0]) != NULL
9008 && spec[1] != '['))
9009 {
9010 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9011 get_last_displayed_symtab (),
9012 get_last_displayed_line (),
9013 canonical, NULL, NULL);
9014 return;
9015 }
9016 }
9017
9018 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9019 cursal.symtab, cursal.line, canonical, NULL, NULL);
9020 }
9021
9022
9023 /* Convert each SAL into a real PC. Verify that the PC can be
9024 inserted as a breakpoint. If it can't throw an error. */
9025
9026 static void
9027 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9028 {
9029 for (auto &sal : sals)
9030 resolve_sal_pc (&sal);
9031 }
9032
9033 /* Fast tracepoints may have restrictions on valid locations. For
9034 instance, a fast tracepoint using a jump instead of a trap will
9035 likely have to overwrite more bytes than a trap would, and so can
9036 only be placed where the instruction is longer than the jump, or a
9037 multi-instruction sequence does not have a jump into the middle of
9038 it, etc. */
9039
9040 static void
9041 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9042 gdb::array_view<const symtab_and_line> sals)
9043 {
9044 for (const auto &sal : sals)
9045 {
9046 struct gdbarch *sarch;
9047
9048 sarch = get_sal_arch (sal);
9049 /* We fall back to GDBARCH if there is no architecture
9050 associated with SAL. */
9051 if (sarch == NULL)
9052 sarch = gdbarch;
9053 std::string msg;
9054 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9055 error (_("May not have a fast tracepoint at %s%s"),
9056 paddress (sarch, sal.pc), msg.c_str ());
9057 }
9058 }
9059
9060 /* Given TOK, a string specification of condition and thread, as
9061 accepted by the 'break' command, extract the condition
9062 string and thread number and set *COND_STRING and *THREAD.
9063 PC identifies the context at which the condition should be parsed.
9064 If no condition is found, *COND_STRING is set to NULL.
9065 If no thread is found, *THREAD is set to -1. */
9066
9067 static void
9068 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9069 char **cond_string, int *thread, int *task,
9070 char **rest)
9071 {
9072 *cond_string = NULL;
9073 *thread = -1;
9074 *task = 0;
9075 *rest = NULL;
9076
9077 while (tok && *tok)
9078 {
9079 const char *end_tok;
9080 int toklen;
9081 const char *cond_start = NULL;
9082 const char *cond_end = NULL;
9083
9084 tok = skip_spaces (tok);
9085
9086 if ((*tok == '"' || *tok == ',') && rest)
9087 {
9088 *rest = savestring (tok, strlen (tok));
9089 return;
9090 }
9091
9092 end_tok = skip_to_space (tok);
9093
9094 toklen = end_tok - tok;
9095
9096 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9097 {
9098 tok = cond_start = end_tok + 1;
9099 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9100 cond_end = tok;
9101 *cond_string = savestring (cond_start, cond_end - cond_start);
9102 }
9103 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9104 {
9105 const char *tmptok;
9106 struct thread_info *thr;
9107
9108 tok = end_tok + 1;
9109 thr = parse_thread_id (tok, &tmptok);
9110 if (tok == tmptok)
9111 error (_("Junk after thread keyword."));
9112 *thread = thr->global_num;
9113 tok = tmptok;
9114 }
9115 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9116 {
9117 char *tmptok;
9118
9119 tok = end_tok + 1;
9120 *task = strtol (tok, &tmptok, 0);
9121 if (tok == tmptok)
9122 error (_("Junk after task keyword."));
9123 if (!valid_task_id (*task))
9124 error (_("Unknown task %d."), *task);
9125 tok = tmptok;
9126 }
9127 else if (rest)
9128 {
9129 *rest = savestring (tok, strlen (tok));
9130 return;
9131 }
9132 else
9133 error (_("Junk at end of arguments."));
9134 }
9135 }
9136
9137 /* Decode a static tracepoint marker spec. */
9138
9139 static std::vector<symtab_and_line>
9140 decode_static_tracepoint_spec (const char **arg_p)
9141 {
9142 const char *p = &(*arg_p)[3];
9143 const char *endp;
9144
9145 p = skip_spaces (p);
9146
9147 endp = skip_to_space (p);
9148
9149 std::string marker_str (p, endp - p);
9150
9151 std::vector<static_tracepoint_marker> markers
9152 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9153 if (markers.empty ())
9154 error (_("No known static tracepoint marker named %s"),
9155 marker_str.c_str ());
9156
9157 std::vector<symtab_and_line> sals;
9158 sals.reserve (markers.size ());
9159
9160 for (const static_tracepoint_marker &marker : markers)
9161 {
9162 symtab_and_line sal = find_pc_line (marker.address, 0);
9163 sal.pc = marker.address;
9164 sals.push_back (sal);
9165 }
9166
9167 *arg_p = endp;
9168 return sals;
9169 }
9170
9171 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9172 according to IS_TRACEPOINT. */
9173
9174 static const struct breakpoint_ops *
9175 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9176 bool is_tracepoint)
9177 {
9178 if (is_tracepoint)
9179 {
9180 if (location_type == PROBE_LOCATION)
9181 return &tracepoint_probe_breakpoint_ops;
9182 else
9183 return &tracepoint_breakpoint_ops;
9184 }
9185 else
9186 {
9187 if (location_type == PROBE_LOCATION)
9188 return &bkpt_probe_breakpoint_ops;
9189 else
9190 return &bkpt_breakpoint_ops;
9191 }
9192 }
9193
9194 /* See breakpoint.h. */
9195
9196 const struct breakpoint_ops *
9197 breakpoint_ops_for_event_location (const struct event_location *location,
9198 bool is_tracepoint)
9199 {
9200 if (location != nullptr)
9201 return breakpoint_ops_for_event_location_type
9202 (event_location_type (location), is_tracepoint);
9203 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9204 }
9205
9206 /* See breakpoint.h. */
9207
9208 int
9209 create_breakpoint (struct gdbarch *gdbarch,
9210 const struct event_location *location,
9211 const char *cond_string,
9212 int thread, const char *extra_string,
9213 int parse_extra,
9214 int tempflag, enum bptype type_wanted,
9215 int ignore_count,
9216 enum auto_boolean pending_break_support,
9217 const struct breakpoint_ops *ops,
9218 int from_tty, int enabled, int internal,
9219 unsigned flags)
9220 {
9221 struct linespec_result canonical;
9222 int pending = 0;
9223 int task = 0;
9224 int prev_bkpt_count = breakpoint_count;
9225
9226 gdb_assert (ops != NULL);
9227
9228 /* If extra_string isn't useful, set it to NULL. */
9229 if (extra_string != NULL && *extra_string == '\0')
9230 extra_string = NULL;
9231
9232 try
9233 {
9234 ops->create_sals_from_location (location, &canonical, type_wanted);
9235 }
9236 catch (const gdb_exception_error &e)
9237 {
9238 /* If caller is interested in rc value from parse, set
9239 value. */
9240 if (e.error == NOT_FOUND_ERROR)
9241 {
9242 /* If pending breakpoint support is turned off, throw
9243 error. */
9244
9245 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9246 throw;
9247
9248 exception_print (gdb_stderr, e);
9249
9250 /* If pending breakpoint support is auto query and the user
9251 selects no, then simply return the error code. */
9252 if (pending_break_support == AUTO_BOOLEAN_AUTO
9253 && !nquery (_("Make %s pending on future shared library load? "),
9254 bptype_string (type_wanted)))
9255 return 0;
9256
9257 /* At this point, either the user was queried about setting
9258 a pending breakpoint and selected yes, or pending
9259 breakpoint behavior is on and thus a pending breakpoint
9260 is defaulted on behalf of the user. */
9261 pending = 1;
9262 }
9263 else
9264 throw;
9265 }
9266
9267 if (!pending && canonical.lsals.empty ())
9268 return 0;
9269
9270 /* Resolve all line numbers to PC's and verify that the addresses
9271 are ok for the target. */
9272 if (!pending)
9273 {
9274 for (auto &lsal : canonical.lsals)
9275 breakpoint_sals_to_pc (lsal.sals);
9276 }
9277
9278 /* Fast tracepoints may have additional restrictions on location. */
9279 if (!pending && type_wanted == bp_fast_tracepoint)
9280 {
9281 for (const auto &lsal : canonical.lsals)
9282 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9283 }
9284
9285 /* Verify that condition can be parsed, before setting any
9286 breakpoints. Allocate a separate condition expression for each
9287 breakpoint. */
9288 if (!pending)
9289 {
9290 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9291 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9292
9293 if (parse_extra)
9294 {
9295 char *rest;
9296 char *cond;
9297
9298 const linespec_sals &lsal = canonical.lsals[0];
9299
9300 /* Here we only parse 'arg' to separate condition
9301 from thread number, so parsing in context of first
9302 sal is OK. When setting the breakpoint we'll
9303 re-parse it in context of each sal. */
9304
9305 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9306 &cond, &thread, &task, &rest);
9307 cond_string_copy.reset (cond);
9308 extra_string_copy.reset (rest);
9309 }
9310 else
9311 {
9312 if (type_wanted != bp_dprintf
9313 && extra_string != NULL && *extra_string != '\0')
9314 error (_("Garbage '%s' at end of location"), extra_string);
9315
9316 /* Create a private copy of condition string. */
9317 if (cond_string)
9318 cond_string_copy.reset (xstrdup (cond_string));
9319 /* Create a private copy of any extra string. */
9320 if (extra_string)
9321 extra_string_copy.reset (xstrdup (extra_string));
9322 }
9323
9324 ops->create_breakpoints_sal (gdbarch, &canonical,
9325 std::move (cond_string_copy),
9326 std::move (extra_string_copy),
9327 type_wanted,
9328 tempflag ? disp_del : disp_donttouch,
9329 thread, task, ignore_count, ops,
9330 from_tty, enabled, internal, flags);
9331 }
9332 else
9333 {
9334 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9335
9336 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9337 b->location = copy_event_location (location);
9338
9339 if (parse_extra)
9340 b->cond_string = NULL;
9341 else
9342 {
9343 /* Create a private copy of condition string. */
9344 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9345 b->thread = thread;
9346 }
9347
9348 /* Create a private copy of any extra string. */
9349 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9350 b->ignore_count = ignore_count;
9351 b->disposition = tempflag ? disp_del : disp_donttouch;
9352 b->condition_not_parsed = 1;
9353 b->enable_state = enabled ? bp_enabled : bp_disabled;
9354 if ((type_wanted != bp_breakpoint
9355 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9356 b->pspace = current_program_space;
9357
9358 install_breakpoint (internal, std::move (b), 0);
9359 }
9360
9361 if (canonical.lsals.size () > 1)
9362 {
9363 warning (_("Multiple breakpoints were set.\nUse the "
9364 "\"delete\" command to delete unwanted breakpoints."));
9365 prev_breakpoint_count = prev_bkpt_count;
9366 }
9367
9368 update_global_location_list (UGLL_MAY_INSERT);
9369
9370 return 1;
9371 }
9372
9373 /* Set a breakpoint.
9374 ARG is a string describing breakpoint address,
9375 condition, and thread.
9376 FLAG specifies if a breakpoint is hardware on,
9377 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9378 and BP_TEMPFLAG. */
9379
9380 static void
9381 break_command_1 (const char *arg, int flag, int from_tty)
9382 {
9383 int tempflag = flag & BP_TEMPFLAG;
9384 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9385 ? bp_hardware_breakpoint
9386 : bp_breakpoint);
9387
9388 event_location_up location = string_to_event_location (&arg, current_language);
9389 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9390 (location.get (), false /* is_tracepoint */);
9391
9392 create_breakpoint (get_current_arch (),
9393 location.get (),
9394 NULL, 0, arg, 1 /* parse arg */,
9395 tempflag, type_wanted,
9396 0 /* Ignore count */,
9397 pending_break_support,
9398 ops,
9399 from_tty,
9400 1 /* enabled */,
9401 0 /* internal */,
9402 0);
9403 }
9404
9405 /* Helper function for break_command_1 and disassemble_command. */
9406
9407 void
9408 resolve_sal_pc (struct symtab_and_line *sal)
9409 {
9410 CORE_ADDR pc;
9411
9412 if (sal->pc == 0 && sal->symtab != NULL)
9413 {
9414 if (!find_line_pc (sal->symtab, sal->line, &pc))
9415 error (_("No line %d in file \"%s\"."),
9416 sal->line, symtab_to_filename_for_display (sal->symtab));
9417 sal->pc = pc;
9418
9419 /* If this SAL corresponds to a breakpoint inserted using a line
9420 number, then skip the function prologue if necessary. */
9421 if (sal->explicit_line)
9422 skip_prologue_sal (sal);
9423 }
9424
9425 if (sal->section == 0 && sal->symtab != NULL)
9426 {
9427 const struct blockvector *bv;
9428 const struct block *b;
9429 struct symbol *sym;
9430
9431 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9432 SYMTAB_COMPUNIT (sal->symtab));
9433 if (bv != NULL)
9434 {
9435 sym = block_linkage_function (b);
9436 if (sym != NULL)
9437 {
9438 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9439 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9440 sym);
9441 }
9442 else
9443 {
9444 /* It really is worthwhile to have the section, so we'll
9445 just have to look harder. This case can be executed
9446 if we have line numbers but no functions (as can
9447 happen in assembly source). */
9448
9449 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9450 switch_to_program_space_and_thread (sal->pspace);
9451
9452 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9453 if (msym.minsym)
9454 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9455 }
9456 }
9457 }
9458 }
9459
9460 void
9461 break_command (const char *arg, int from_tty)
9462 {
9463 break_command_1 (arg, 0, from_tty);
9464 }
9465
9466 void
9467 tbreak_command (const char *arg, int from_tty)
9468 {
9469 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9470 }
9471
9472 static void
9473 hbreak_command (const char *arg, int from_tty)
9474 {
9475 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9476 }
9477
9478 static void
9479 thbreak_command (const char *arg, int from_tty)
9480 {
9481 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9482 }
9483
9484 static void
9485 stop_command (const char *arg, int from_tty)
9486 {
9487 printf_filtered (_("Specify the type of breakpoint to set.\n\
9488 Usage: stop in <function | address>\n\
9489 stop at <line>\n"));
9490 }
9491
9492 static void
9493 stopin_command (const char *arg, int from_tty)
9494 {
9495 int badInput = 0;
9496
9497 if (arg == NULL)
9498 badInput = 1;
9499 else if (*arg != '*')
9500 {
9501 const char *argptr = arg;
9502 int hasColon = 0;
9503
9504 /* Look for a ':'. If this is a line number specification, then
9505 say it is bad, otherwise, it should be an address or
9506 function/method name. */
9507 while (*argptr && !hasColon)
9508 {
9509 hasColon = (*argptr == ':');
9510 argptr++;
9511 }
9512
9513 if (hasColon)
9514 badInput = (*argptr != ':'); /* Not a class::method */
9515 else
9516 badInput = isdigit (*arg); /* a simple line number */
9517 }
9518
9519 if (badInput)
9520 printf_filtered (_("Usage: stop in <function | address>\n"));
9521 else
9522 break_command_1 (arg, 0, from_tty);
9523 }
9524
9525 static void
9526 stopat_command (const char *arg, int from_tty)
9527 {
9528 int badInput = 0;
9529
9530 if (arg == NULL || *arg == '*') /* no line number */
9531 badInput = 1;
9532 else
9533 {
9534 const char *argptr = arg;
9535 int hasColon = 0;
9536
9537 /* Look for a ':'. If there is a '::' then get out, otherwise
9538 it is probably a line number. */
9539 while (*argptr && !hasColon)
9540 {
9541 hasColon = (*argptr == ':');
9542 argptr++;
9543 }
9544
9545 if (hasColon)
9546 badInput = (*argptr == ':'); /* we have class::method */
9547 else
9548 badInput = !isdigit (*arg); /* not a line number */
9549 }
9550
9551 if (badInput)
9552 printf_filtered (_("Usage: stop at LINE\n"));
9553 else
9554 break_command_1 (arg, 0, from_tty);
9555 }
9556
9557 /* The dynamic printf command is mostly like a regular breakpoint, but
9558 with a prewired command list consisting of a single output command,
9559 built from extra arguments supplied on the dprintf command
9560 line. */
9561
9562 static void
9563 dprintf_command (const char *arg, int from_tty)
9564 {
9565 event_location_up location = string_to_event_location (&arg, current_language);
9566
9567 /* If non-NULL, ARG should have been advanced past the location;
9568 the next character must be ','. */
9569 if (arg != NULL)
9570 {
9571 if (arg[0] != ',' || arg[1] == '\0')
9572 error (_("Format string required"));
9573 else
9574 {
9575 /* Skip the comma. */
9576 ++arg;
9577 }
9578 }
9579
9580 create_breakpoint (get_current_arch (),
9581 location.get (),
9582 NULL, 0, arg, 1 /* parse arg */,
9583 0, bp_dprintf,
9584 0 /* Ignore count */,
9585 pending_break_support,
9586 &dprintf_breakpoint_ops,
9587 from_tty,
9588 1 /* enabled */,
9589 0 /* internal */,
9590 0);
9591 }
9592
9593 static void
9594 agent_printf_command (const char *arg, int from_tty)
9595 {
9596 error (_("May only run agent-printf on the target"));
9597 }
9598
9599 /* Implement the "breakpoint_hit" breakpoint_ops method for
9600 ranged breakpoints. */
9601
9602 static int
9603 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9604 const address_space *aspace,
9605 CORE_ADDR bp_addr,
9606 const struct target_waitstatus *ws)
9607 {
9608 if (ws->kind != TARGET_WAITKIND_STOPPED
9609 || ws->value.sig != GDB_SIGNAL_TRAP)
9610 return 0;
9611
9612 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9613 bl->length, aspace, bp_addr);
9614 }
9615
9616 /* Implement the "resources_needed" breakpoint_ops method for
9617 ranged breakpoints. */
9618
9619 static int
9620 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9621 {
9622 return target_ranged_break_num_registers ();
9623 }
9624
9625 /* Implement the "print_it" breakpoint_ops method for
9626 ranged breakpoints. */
9627
9628 static enum print_stop_action
9629 print_it_ranged_breakpoint (bpstat bs)
9630 {
9631 struct breakpoint *b = bs->breakpoint_at;
9632 struct bp_location *bl = b->loc;
9633 struct ui_out *uiout = current_uiout;
9634
9635 gdb_assert (b->type == bp_hardware_breakpoint);
9636
9637 /* Ranged breakpoints have only one location. */
9638 gdb_assert (bl && bl->next == NULL);
9639
9640 annotate_breakpoint (b->number);
9641
9642 maybe_print_thread_hit_breakpoint (uiout);
9643
9644 if (b->disposition == disp_del)
9645 uiout->text ("Temporary ranged breakpoint ");
9646 else
9647 uiout->text ("Ranged breakpoint ");
9648 if (uiout->is_mi_like_p ())
9649 {
9650 uiout->field_string ("reason",
9651 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9652 uiout->field_string ("disp", bpdisp_text (b->disposition));
9653 }
9654 uiout->field_signed ("bkptno", b->number);
9655 uiout->text (", ");
9656
9657 return PRINT_SRC_AND_LOC;
9658 }
9659
9660 /* Implement the "print_one" breakpoint_ops method for
9661 ranged breakpoints. */
9662
9663 static void
9664 print_one_ranged_breakpoint (struct breakpoint *b,
9665 struct bp_location **last_loc)
9666 {
9667 struct bp_location *bl = b->loc;
9668 struct value_print_options opts;
9669 struct ui_out *uiout = current_uiout;
9670
9671 /* Ranged breakpoints have only one location. */
9672 gdb_assert (bl && bl->next == NULL);
9673
9674 get_user_print_options (&opts);
9675
9676 if (opts.addressprint)
9677 /* We don't print the address range here, it will be printed later
9678 by print_one_detail_ranged_breakpoint. */
9679 uiout->field_skip ("addr");
9680 annotate_field (5);
9681 print_breakpoint_location (b, bl);
9682 *last_loc = bl;
9683 }
9684
9685 /* Implement the "print_one_detail" breakpoint_ops method for
9686 ranged breakpoints. */
9687
9688 static void
9689 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9690 struct ui_out *uiout)
9691 {
9692 CORE_ADDR address_start, address_end;
9693 struct bp_location *bl = b->loc;
9694 string_file stb;
9695
9696 gdb_assert (bl);
9697
9698 address_start = bl->address;
9699 address_end = address_start + bl->length - 1;
9700
9701 uiout->text ("\taddress range: ");
9702 stb.printf ("[%s, %s]",
9703 print_core_address (bl->gdbarch, address_start),
9704 print_core_address (bl->gdbarch, address_end));
9705 uiout->field_stream ("addr", stb);
9706 uiout->text ("\n");
9707 }
9708
9709 /* Implement the "print_mention" breakpoint_ops method for
9710 ranged breakpoints. */
9711
9712 static void
9713 print_mention_ranged_breakpoint (struct breakpoint *b)
9714 {
9715 struct bp_location *bl = b->loc;
9716 struct ui_out *uiout = current_uiout;
9717
9718 gdb_assert (bl);
9719 gdb_assert (b->type == bp_hardware_breakpoint);
9720
9721 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9722 b->number, paddress (bl->gdbarch, bl->address),
9723 paddress (bl->gdbarch, bl->address + bl->length - 1));
9724 }
9725
9726 /* Implement the "print_recreate" breakpoint_ops method for
9727 ranged breakpoints. */
9728
9729 static void
9730 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9731 {
9732 fprintf_unfiltered (fp, "break-range %s, %s",
9733 event_location_to_string (b->location.get ()),
9734 event_location_to_string (b->location_range_end.get ()));
9735 print_recreate_thread (b, fp);
9736 }
9737
9738 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9739
9740 static struct breakpoint_ops ranged_breakpoint_ops;
9741
9742 /* Find the address where the end of the breakpoint range should be
9743 placed, given the SAL of the end of the range. This is so that if
9744 the user provides a line number, the end of the range is set to the
9745 last instruction of the given line. */
9746
9747 static CORE_ADDR
9748 find_breakpoint_range_end (struct symtab_and_line sal)
9749 {
9750 CORE_ADDR end;
9751
9752 /* If the user provided a PC value, use it. Otherwise,
9753 find the address of the end of the given location. */
9754 if (sal.explicit_pc)
9755 end = sal.pc;
9756 else
9757 {
9758 int ret;
9759 CORE_ADDR start;
9760
9761 ret = find_line_pc_range (sal, &start, &end);
9762 if (!ret)
9763 error (_("Could not find location of the end of the range."));
9764
9765 /* find_line_pc_range returns the start of the next line. */
9766 end--;
9767 }
9768
9769 return end;
9770 }
9771
9772 /* Implement the "break-range" CLI command. */
9773
9774 static void
9775 break_range_command (const char *arg, int from_tty)
9776 {
9777 const char *arg_start;
9778 struct linespec_result canonical_start, canonical_end;
9779 int bp_count, can_use_bp, length;
9780 CORE_ADDR end;
9781 struct breakpoint *b;
9782
9783 /* We don't support software ranged breakpoints. */
9784 if (target_ranged_break_num_registers () < 0)
9785 error (_("This target does not support hardware ranged breakpoints."));
9786
9787 bp_count = hw_breakpoint_used_count ();
9788 bp_count += target_ranged_break_num_registers ();
9789 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9790 bp_count, 0);
9791 if (can_use_bp < 0)
9792 error (_("Hardware breakpoints used exceeds limit."));
9793
9794 arg = skip_spaces (arg);
9795 if (arg == NULL || arg[0] == '\0')
9796 error(_("No address range specified."));
9797
9798 arg_start = arg;
9799 event_location_up start_location = string_to_event_location (&arg,
9800 current_language);
9801 parse_breakpoint_sals (start_location.get (), &canonical_start);
9802
9803 if (arg[0] != ',')
9804 error (_("Too few arguments."));
9805 else if (canonical_start.lsals.empty ())
9806 error (_("Could not find location of the beginning of the range."));
9807
9808 const linespec_sals &lsal_start = canonical_start.lsals[0];
9809
9810 if (canonical_start.lsals.size () > 1
9811 || lsal_start.sals.size () != 1)
9812 error (_("Cannot create a ranged breakpoint with multiple locations."));
9813
9814 const symtab_and_line &sal_start = lsal_start.sals[0];
9815 std::string addr_string_start (arg_start, arg - arg_start);
9816
9817 arg++; /* Skip the comma. */
9818 arg = skip_spaces (arg);
9819
9820 /* Parse the end location. */
9821
9822 arg_start = arg;
9823
9824 /* We call decode_line_full directly here instead of using
9825 parse_breakpoint_sals because we need to specify the start location's
9826 symtab and line as the default symtab and line for the end of the
9827 range. This makes it possible to have ranges like "foo.c:27, +14",
9828 where +14 means 14 lines from the start location. */
9829 event_location_up end_location = string_to_event_location (&arg,
9830 current_language);
9831 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9832 sal_start.symtab, sal_start.line,
9833 &canonical_end, NULL, NULL);
9834
9835 if (canonical_end.lsals.empty ())
9836 error (_("Could not find location of the end of the range."));
9837
9838 const linespec_sals &lsal_end = canonical_end.lsals[0];
9839 if (canonical_end.lsals.size () > 1
9840 || lsal_end.sals.size () != 1)
9841 error (_("Cannot create a ranged breakpoint with multiple locations."));
9842
9843 const symtab_and_line &sal_end = lsal_end.sals[0];
9844
9845 end = find_breakpoint_range_end (sal_end);
9846 if (sal_start.pc > end)
9847 error (_("Invalid address range, end precedes start."));
9848
9849 length = end - sal_start.pc + 1;
9850 if (length < 0)
9851 /* Length overflowed. */
9852 error (_("Address range too large."));
9853 else if (length == 1)
9854 {
9855 /* This range is simple enough to be handled by
9856 the `hbreak' command. */
9857 hbreak_command (&addr_string_start[0], 1);
9858
9859 return;
9860 }
9861
9862 /* Now set up the breakpoint. */
9863 b = set_raw_breakpoint (get_current_arch (), sal_start,
9864 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9865 set_breakpoint_count (breakpoint_count + 1);
9866 b->number = breakpoint_count;
9867 b->disposition = disp_donttouch;
9868 b->location = std::move (start_location);
9869 b->location_range_end = std::move (end_location);
9870 b->loc->length = length;
9871
9872 mention (b);
9873 gdb::observers::breakpoint_created.notify (b);
9874 update_global_location_list (UGLL_MAY_INSERT);
9875 }
9876
9877 /* Return non-zero if EXP is verified as constant. Returned zero
9878 means EXP is variable. Also the constant detection may fail for
9879 some constant expressions and in such case still falsely return
9880 zero. */
9881
9882 static int
9883 watchpoint_exp_is_const (const struct expression *exp)
9884 {
9885 int i = exp->nelts;
9886
9887 while (i > 0)
9888 {
9889 int oplenp, argsp;
9890
9891 /* We are only interested in the descriptor of each element. */
9892 operator_length (exp, i, &oplenp, &argsp);
9893 i -= oplenp;
9894
9895 switch (exp->elts[i].opcode)
9896 {
9897 case BINOP_ADD:
9898 case BINOP_SUB:
9899 case BINOP_MUL:
9900 case BINOP_DIV:
9901 case BINOP_REM:
9902 case BINOP_MOD:
9903 case BINOP_LSH:
9904 case BINOP_RSH:
9905 case BINOP_LOGICAL_AND:
9906 case BINOP_LOGICAL_OR:
9907 case BINOP_BITWISE_AND:
9908 case BINOP_BITWISE_IOR:
9909 case BINOP_BITWISE_XOR:
9910 case BINOP_EQUAL:
9911 case BINOP_NOTEQUAL:
9912 case BINOP_LESS:
9913 case BINOP_GTR:
9914 case BINOP_LEQ:
9915 case BINOP_GEQ:
9916 case BINOP_REPEAT:
9917 case BINOP_COMMA:
9918 case BINOP_EXP:
9919 case BINOP_MIN:
9920 case BINOP_MAX:
9921 case BINOP_INTDIV:
9922 case BINOP_CONCAT:
9923 case TERNOP_COND:
9924 case TERNOP_SLICE:
9925
9926 case OP_LONG:
9927 case OP_FLOAT:
9928 case OP_LAST:
9929 case OP_COMPLEX:
9930 case OP_STRING:
9931 case OP_ARRAY:
9932 case OP_TYPE:
9933 case OP_TYPEOF:
9934 case OP_DECLTYPE:
9935 case OP_TYPEID:
9936 case OP_NAME:
9937 case OP_OBJC_NSSTRING:
9938
9939 case UNOP_NEG:
9940 case UNOP_LOGICAL_NOT:
9941 case UNOP_COMPLEMENT:
9942 case UNOP_ADDR:
9943 case UNOP_HIGH:
9944 case UNOP_CAST:
9945
9946 case UNOP_CAST_TYPE:
9947 case UNOP_REINTERPRET_CAST:
9948 case UNOP_DYNAMIC_CAST:
9949 /* Unary, binary and ternary operators: We have to check
9950 their operands. If they are constant, then so is the
9951 result of that operation. For instance, if A and B are
9952 determined to be constants, then so is "A + B".
9953
9954 UNOP_IND is one exception to the rule above, because the
9955 value of *ADDR is not necessarily a constant, even when
9956 ADDR is. */
9957 break;
9958
9959 case OP_VAR_VALUE:
9960 /* Check whether the associated symbol is a constant.
9961
9962 We use SYMBOL_CLASS rather than TYPE_CONST because it's
9963 possible that a buggy compiler could mark a variable as
9964 constant even when it is not, and TYPE_CONST would return
9965 true in this case, while SYMBOL_CLASS wouldn't.
9966
9967 We also have to check for function symbols because they
9968 are always constant. */
9969 {
9970 struct symbol *s = exp->elts[i + 2].symbol;
9971
9972 if (SYMBOL_CLASS (s) != LOC_BLOCK
9973 && SYMBOL_CLASS (s) != LOC_CONST
9974 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9975 return 0;
9976 break;
9977 }
9978
9979 /* The default action is to return 0 because we are using
9980 the optimistic approach here: If we don't know something,
9981 then it is not a constant. */
9982 default:
9983 return 0;
9984 }
9985 }
9986
9987 return 1;
9988 }
9989
9990 /* Watchpoint destructor. */
9991
9992 watchpoint::~watchpoint ()
9993 {
9994 xfree (this->exp_string);
9995 xfree (this->exp_string_reparse);
9996 }
9997
9998 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
9999
10000 static void
10001 re_set_watchpoint (struct breakpoint *b)
10002 {
10003 struct watchpoint *w = (struct watchpoint *) b;
10004
10005 /* Watchpoint can be either on expression using entirely global
10006 variables, or it can be on local variables.
10007
10008 Watchpoints of the first kind are never auto-deleted, and even
10009 persist across program restarts. Since they can use variables
10010 from shared libraries, we need to reparse expression as libraries
10011 are loaded and unloaded.
10012
10013 Watchpoints on local variables can also change meaning as result
10014 of solib event. For example, if a watchpoint uses both a local
10015 and a global variables in expression, it's a local watchpoint,
10016 but unloading of a shared library will make the expression
10017 invalid. This is not a very common use case, but we still
10018 re-evaluate expression, to avoid surprises to the user.
10019
10020 Note that for local watchpoints, we re-evaluate it only if
10021 watchpoints frame id is still valid. If it's not, it means the
10022 watchpoint is out of scope and will be deleted soon. In fact,
10023 I'm not sure we'll ever be called in this case.
10024
10025 If a local watchpoint's frame id is still valid, then
10026 w->exp_valid_block is likewise valid, and we can safely use it.
10027
10028 Don't do anything about disabled watchpoints, since they will be
10029 reevaluated again when enabled. */
10030 update_watchpoint (w, 1 /* reparse */);
10031 }
10032
10033 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10034
10035 static int
10036 insert_watchpoint (struct bp_location *bl)
10037 {
10038 struct watchpoint *w = (struct watchpoint *) bl->owner;
10039 int length = w->exact ? 1 : bl->length;
10040
10041 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10042 w->cond_exp.get ());
10043 }
10044
10045 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10046
10047 static int
10048 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10049 {
10050 struct watchpoint *w = (struct watchpoint *) bl->owner;
10051 int length = w->exact ? 1 : bl->length;
10052
10053 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10054 w->cond_exp.get ());
10055 }
10056
10057 static int
10058 breakpoint_hit_watchpoint (const struct bp_location *bl,
10059 const address_space *aspace, CORE_ADDR bp_addr,
10060 const struct target_waitstatus *ws)
10061 {
10062 struct breakpoint *b = bl->owner;
10063 struct watchpoint *w = (struct watchpoint *) b;
10064
10065 /* Continuable hardware watchpoints are treated as non-existent if the
10066 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10067 some data address). Otherwise gdb won't stop on a break instruction
10068 in the code (not from a breakpoint) when a hardware watchpoint has
10069 been defined. Also skip watchpoints which we know did not trigger
10070 (did not match the data address). */
10071 if (is_hardware_watchpoint (b)
10072 && w->watchpoint_triggered == watch_triggered_no)
10073 return 0;
10074
10075 return 1;
10076 }
10077
10078 static void
10079 check_status_watchpoint (bpstat bs)
10080 {
10081 gdb_assert (is_watchpoint (bs->breakpoint_at));
10082
10083 bpstat_check_watchpoint (bs);
10084 }
10085
10086 /* Implement the "resources_needed" breakpoint_ops method for
10087 hardware watchpoints. */
10088
10089 static int
10090 resources_needed_watchpoint (const struct bp_location *bl)
10091 {
10092 struct watchpoint *w = (struct watchpoint *) bl->owner;
10093 int length = w->exact? 1 : bl->length;
10094
10095 return target_region_ok_for_hw_watchpoint (bl->address, length);
10096 }
10097
10098 /* Implement the "works_in_software_mode" breakpoint_ops method for
10099 hardware watchpoints. */
10100
10101 static int
10102 works_in_software_mode_watchpoint (const struct breakpoint *b)
10103 {
10104 /* Read and access watchpoints only work with hardware support. */
10105 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10106 }
10107
10108 static enum print_stop_action
10109 print_it_watchpoint (bpstat bs)
10110 {
10111 struct breakpoint *b;
10112 enum print_stop_action result;
10113 struct watchpoint *w;
10114 struct ui_out *uiout = current_uiout;
10115
10116 gdb_assert (bs->bp_location_at != NULL);
10117
10118 b = bs->breakpoint_at;
10119 w = (struct watchpoint *) b;
10120
10121 annotate_watchpoint (b->number);
10122 maybe_print_thread_hit_breakpoint (uiout);
10123
10124 string_file stb;
10125
10126 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10127 switch (b->type)
10128 {
10129 case bp_watchpoint:
10130 case bp_hardware_watchpoint:
10131 if (uiout->is_mi_like_p ())
10132 uiout->field_string
10133 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10134 mention (b);
10135 tuple_emitter.emplace (uiout, "value");
10136 uiout->text ("\nOld value = ");
10137 watchpoint_value_print (bs->old_val.get (), &stb);
10138 uiout->field_stream ("old", stb);
10139 uiout->text ("\nNew value = ");
10140 watchpoint_value_print (w->val.get (), &stb);
10141 uiout->field_stream ("new", stb);
10142 uiout->text ("\n");
10143 /* More than one watchpoint may have been triggered. */
10144 result = PRINT_UNKNOWN;
10145 break;
10146
10147 case bp_read_watchpoint:
10148 if (uiout->is_mi_like_p ())
10149 uiout->field_string
10150 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10151 mention (b);
10152 tuple_emitter.emplace (uiout, "value");
10153 uiout->text ("\nValue = ");
10154 watchpoint_value_print (w->val.get (), &stb);
10155 uiout->field_stream ("value", stb);
10156 uiout->text ("\n");
10157 result = PRINT_UNKNOWN;
10158 break;
10159
10160 case bp_access_watchpoint:
10161 if (bs->old_val != NULL)
10162 {
10163 if (uiout->is_mi_like_p ())
10164 uiout->field_string
10165 ("reason",
10166 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10167 mention (b);
10168 tuple_emitter.emplace (uiout, "value");
10169 uiout->text ("\nOld value = ");
10170 watchpoint_value_print (bs->old_val.get (), &stb);
10171 uiout->field_stream ("old", stb);
10172 uiout->text ("\nNew value = ");
10173 }
10174 else
10175 {
10176 mention (b);
10177 if (uiout->is_mi_like_p ())
10178 uiout->field_string
10179 ("reason",
10180 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10181 tuple_emitter.emplace (uiout, "value");
10182 uiout->text ("\nValue = ");
10183 }
10184 watchpoint_value_print (w->val.get (), &stb);
10185 uiout->field_stream ("new", stb);
10186 uiout->text ("\n");
10187 result = PRINT_UNKNOWN;
10188 break;
10189 default:
10190 result = PRINT_UNKNOWN;
10191 }
10192
10193 return result;
10194 }
10195
10196 /* Implement the "print_mention" breakpoint_ops method for hardware
10197 watchpoints. */
10198
10199 static void
10200 print_mention_watchpoint (struct breakpoint *b)
10201 {
10202 struct watchpoint *w = (struct watchpoint *) b;
10203 struct ui_out *uiout = current_uiout;
10204 const char *tuple_name;
10205
10206 switch (b->type)
10207 {
10208 case bp_watchpoint:
10209 uiout->text ("Watchpoint ");
10210 tuple_name = "wpt";
10211 break;
10212 case bp_hardware_watchpoint:
10213 uiout->text ("Hardware watchpoint ");
10214 tuple_name = "wpt";
10215 break;
10216 case bp_read_watchpoint:
10217 uiout->text ("Hardware read watchpoint ");
10218 tuple_name = "hw-rwpt";
10219 break;
10220 case bp_access_watchpoint:
10221 uiout->text ("Hardware access (read/write) watchpoint ");
10222 tuple_name = "hw-awpt";
10223 break;
10224 default:
10225 internal_error (__FILE__, __LINE__,
10226 _("Invalid hardware watchpoint type."));
10227 }
10228
10229 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10230 uiout->field_signed ("number", b->number);
10231 uiout->text (": ");
10232 uiout->field_string ("exp", w->exp_string);
10233 }
10234
10235 /* Implement the "print_recreate" breakpoint_ops method for
10236 watchpoints. */
10237
10238 static void
10239 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10240 {
10241 struct watchpoint *w = (struct watchpoint *) b;
10242
10243 switch (b->type)
10244 {
10245 case bp_watchpoint:
10246 case bp_hardware_watchpoint:
10247 fprintf_unfiltered (fp, "watch");
10248 break;
10249 case bp_read_watchpoint:
10250 fprintf_unfiltered (fp, "rwatch");
10251 break;
10252 case bp_access_watchpoint:
10253 fprintf_unfiltered (fp, "awatch");
10254 break;
10255 default:
10256 internal_error (__FILE__, __LINE__,
10257 _("Invalid watchpoint type."));
10258 }
10259
10260 fprintf_unfiltered (fp, " %s", w->exp_string);
10261 print_recreate_thread (b, fp);
10262 }
10263
10264 /* Implement the "explains_signal" breakpoint_ops method for
10265 watchpoints. */
10266
10267 static int
10268 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10269 {
10270 /* A software watchpoint cannot cause a signal other than
10271 GDB_SIGNAL_TRAP. */
10272 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10273 return 0;
10274
10275 return 1;
10276 }
10277
10278 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10279
10280 static struct breakpoint_ops watchpoint_breakpoint_ops;
10281
10282 /* Implement the "insert" breakpoint_ops method for
10283 masked hardware watchpoints. */
10284
10285 static int
10286 insert_masked_watchpoint (struct bp_location *bl)
10287 {
10288 struct watchpoint *w = (struct watchpoint *) bl->owner;
10289
10290 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10291 bl->watchpoint_type);
10292 }
10293
10294 /* Implement the "remove" breakpoint_ops method for
10295 masked hardware watchpoints. */
10296
10297 static int
10298 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10299 {
10300 struct watchpoint *w = (struct watchpoint *) bl->owner;
10301
10302 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10303 bl->watchpoint_type);
10304 }
10305
10306 /* Implement the "resources_needed" breakpoint_ops method for
10307 masked hardware watchpoints. */
10308
10309 static int
10310 resources_needed_masked_watchpoint (const struct bp_location *bl)
10311 {
10312 struct watchpoint *w = (struct watchpoint *) bl->owner;
10313
10314 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10315 }
10316
10317 /* Implement the "works_in_software_mode" breakpoint_ops method for
10318 masked hardware watchpoints. */
10319
10320 static int
10321 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10322 {
10323 return 0;
10324 }
10325
10326 /* Implement the "print_it" breakpoint_ops method for
10327 masked hardware watchpoints. */
10328
10329 static enum print_stop_action
10330 print_it_masked_watchpoint (bpstat bs)
10331 {
10332 struct breakpoint *b = bs->breakpoint_at;
10333 struct ui_out *uiout = current_uiout;
10334
10335 /* Masked watchpoints have only one location. */
10336 gdb_assert (b->loc && b->loc->next == NULL);
10337
10338 annotate_watchpoint (b->number);
10339 maybe_print_thread_hit_breakpoint (uiout);
10340
10341 switch (b->type)
10342 {
10343 case bp_hardware_watchpoint:
10344 if (uiout->is_mi_like_p ())
10345 uiout->field_string
10346 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10347 break;
10348
10349 case bp_read_watchpoint:
10350 if (uiout->is_mi_like_p ())
10351 uiout->field_string
10352 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10353 break;
10354
10355 case bp_access_watchpoint:
10356 if (uiout->is_mi_like_p ())
10357 uiout->field_string
10358 ("reason",
10359 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10360 break;
10361 default:
10362 internal_error (__FILE__, __LINE__,
10363 _("Invalid hardware watchpoint type."));
10364 }
10365
10366 mention (b);
10367 uiout->text (_("\n\
10368 Check the underlying instruction at PC for the memory\n\
10369 address and value which triggered this watchpoint.\n"));
10370 uiout->text ("\n");
10371
10372 /* More than one watchpoint may have been triggered. */
10373 return PRINT_UNKNOWN;
10374 }
10375
10376 /* Implement the "print_one_detail" breakpoint_ops method for
10377 masked hardware watchpoints. */
10378
10379 static void
10380 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10381 struct ui_out *uiout)
10382 {
10383 struct watchpoint *w = (struct watchpoint *) b;
10384
10385 /* Masked watchpoints have only one location. */
10386 gdb_assert (b->loc && b->loc->next == NULL);
10387
10388 uiout->text ("\tmask ");
10389 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10390 uiout->text ("\n");
10391 }
10392
10393 /* Implement the "print_mention" breakpoint_ops method for
10394 masked hardware watchpoints. */
10395
10396 static void
10397 print_mention_masked_watchpoint (struct breakpoint *b)
10398 {
10399 struct watchpoint *w = (struct watchpoint *) b;
10400 struct ui_out *uiout = current_uiout;
10401 const char *tuple_name;
10402
10403 switch (b->type)
10404 {
10405 case bp_hardware_watchpoint:
10406 uiout->text ("Masked hardware watchpoint ");
10407 tuple_name = "wpt";
10408 break;
10409 case bp_read_watchpoint:
10410 uiout->text ("Masked hardware read watchpoint ");
10411 tuple_name = "hw-rwpt";
10412 break;
10413 case bp_access_watchpoint:
10414 uiout->text ("Masked hardware access (read/write) watchpoint ");
10415 tuple_name = "hw-awpt";
10416 break;
10417 default:
10418 internal_error (__FILE__, __LINE__,
10419 _("Invalid hardware watchpoint type."));
10420 }
10421
10422 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10423 uiout->field_signed ("number", b->number);
10424 uiout->text (": ");
10425 uiout->field_string ("exp", w->exp_string);
10426 }
10427
10428 /* Implement the "print_recreate" breakpoint_ops method for
10429 masked hardware watchpoints. */
10430
10431 static void
10432 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10433 {
10434 struct watchpoint *w = (struct watchpoint *) b;
10435 char tmp[40];
10436
10437 switch (b->type)
10438 {
10439 case bp_hardware_watchpoint:
10440 fprintf_unfiltered (fp, "watch");
10441 break;
10442 case bp_read_watchpoint:
10443 fprintf_unfiltered (fp, "rwatch");
10444 break;
10445 case bp_access_watchpoint:
10446 fprintf_unfiltered (fp, "awatch");
10447 break;
10448 default:
10449 internal_error (__FILE__, __LINE__,
10450 _("Invalid hardware watchpoint type."));
10451 }
10452
10453 sprintf_vma (tmp, w->hw_wp_mask);
10454 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10455 print_recreate_thread (b, fp);
10456 }
10457
10458 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10459
10460 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10461
10462 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10463
10464 static bool
10465 is_masked_watchpoint (const struct breakpoint *b)
10466 {
10467 return b->ops == &masked_watchpoint_breakpoint_ops;
10468 }
10469
10470 /* accessflag: hw_write: watch write,
10471 hw_read: watch read,
10472 hw_access: watch access (read or write) */
10473 static void
10474 watch_command_1 (const char *arg, int accessflag, int from_tty,
10475 int just_location, int internal)
10476 {
10477 struct breakpoint *scope_breakpoint = NULL;
10478 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10479 struct value *result;
10480 int saved_bitpos = 0, saved_bitsize = 0;
10481 const char *exp_start = NULL;
10482 const char *exp_end = NULL;
10483 const char *tok, *end_tok;
10484 int toklen = -1;
10485 const char *cond_start = NULL;
10486 const char *cond_end = NULL;
10487 enum bptype bp_type;
10488 int thread = -1;
10489 int pc = 0;
10490 /* Flag to indicate whether we are going to use masks for
10491 the hardware watchpoint. */
10492 int use_mask = 0;
10493 CORE_ADDR mask = 0;
10494
10495 /* Make sure that we actually have parameters to parse. */
10496 if (arg != NULL && arg[0] != '\0')
10497 {
10498 const char *value_start;
10499
10500 exp_end = arg + strlen (arg);
10501
10502 /* Look for "parameter value" pairs at the end
10503 of the arguments string. */
10504 for (tok = exp_end - 1; tok > arg; tok--)
10505 {
10506 /* Skip whitespace at the end of the argument list. */
10507 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10508 tok--;
10509
10510 /* Find the beginning of the last token.
10511 This is the value of the parameter. */
10512 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10513 tok--;
10514 value_start = tok + 1;
10515
10516 /* Skip whitespace. */
10517 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10518 tok--;
10519
10520 end_tok = tok;
10521
10522 /* Find the beginning of the second to last token.
10523 This is the parameter itself. */
10524 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10525 tok--;
10526 tok++;
10527 toklen = end_tok - tok + 1;
10528
10529 if (toklen == 6 && startswith (tok, "thread"))
10530 {
10531 struct thread_info *thr;
10532 /* At this point we've found a "thread" token, which means
10533 the user is trying to set a watchpoint that triggers
10534 only in a specific thread. */
10535 const char *endp;
10536
10537 if (thread != -1)
10538 error(_("You can specify only one thread."));
10539
10540 /* Extract the thread ID from the next token. */
10541 thr = parse_thread_id (value_start, &endp);
10542
10543 /* Check if the user provided a valid thread ID. */
10544 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10545 invalid_thread_id_error (value_start);
10546
10547 thread = thr->global_num;
10548 }
10549 else if (toklen == 4 && startswith (tok, "mask"))
10550 {
10551 /* We've found a "mask" token, which means the user wants to
10552 create a hardware watchpoint that is going to have the mask
10553 facility. */
10554 struct value *mask_value, *mark;
10555
10556 if (use_mask)
10557 error(_("You can specify only one mask."));
10558
10559 use_mask = just_location = 1;
10560
10561 mark = value_mark ();
10562 mask_value = parse_to_comma_and_eval (&value_start);
10563 mask = value_as_address (mask_value);
10564 value_free_to_mark (mark);
10565 }
10566 else
10567 /* We didn't recognize what we found. We should stop here. */
10568 break;
10569
10570 /* Truncate the string and get rid of the "parameter value" pair before
10571 the arguments string is parsed by the parse_exp_1 function. */
10572 exp_end = tok;
10573 }
10574 }
10575 else
10576 exp_end = arg;
10577
10578 /* Parse the rest of the arguments. From here on out, everything
10579 is in terms of a newly allocated string instead of the original
10580 ARG. */
10581 std::string expression (arg, exp_end - arg);
10582 exp_start = arg = expression.c_str ();
10583 innermost_block_tracker tracker;
10584 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10585 exp_end = arg;
10586 /* Remove trailing whitespace from the expression before saving it.
10587 This makes the eventual display of the expression string a bit
10588 prettier. */
10589 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10590 --exp_end;
10591
10592 /* Checking if the expression is not constant. */
10593 if (watchpoint_exp_is_const (exp.get ()))
10594 {
10595 int len;
10596
10597 len = exp_end - exp_start;
10598 while (len > 0 && isspace (exp_start[len - 1]))
10599 len--;
10600 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10601 }
10602
10603 exp_valid_block = tracker.block ();
10604 struct value *mark = value_mark ();
10605 struct value *val_as_value = nullptr;
10606 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10607 just_location);
10608
10609 if (val_as_value != NULL && just_location)
10610 {
10611 saved_bitpos = value_bitpos (val_as_value);
10612 saved_bitsize = value_bitsize (val_as_value);
10613 }
10614
10615 value_ref_ptr val;
10616 if (just_location)
10617 {
10618 int ret;
10619
10620 exp_valid_block = NULL;
10621 val = release_value (value_addr (result));
10622 value_free_to_mark (mark);
10623
10624 if (use_mask)
10625 {
10626 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10627 mask);
10628 if (ret == -1)
10629 error (_("This target does not support masked watchpoints."));
10630 else if (ret == -2)
10631 error (_("Invalid mask or memory region."));
10632 }
10633 }
10634 else if (val_as_value != NULL)
10635 val = release_value (val_as_value);
10636
10637 tok = skip_spaces (arg);
10638 end_tok = skip_to_space (tok);
10639
10640 toklen = end_tok - tok;
10641 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10642 {
10643 tok = cond_start = end_tok + 1;
10644 innermost_block_tracker if_tracker;
10645 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10646
10647 /* The watchpoint expression may not be local, but the condition
10648 may still be. E.g.: `watch global if local > 0'. */
10649 cond_exp_valid_block = if_tracker.block ();
10650
10651 cond_end = tok;
10652 }
10653 if (*tok)
10654 error (_("Junk at end of command."));
10655
10656 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10657
10658 /* Save this because create_internal_breakpoint below invalidates
10659 'wp_frame'. */
10660 frame_id watchpoint_frame = get_frame_id (wp_frame);
10661
10662 /* If the expression is "local", then set up a "watchpoint scope"
10663 breakpoint at the point where we've left the scope of the watchpoint
10664 expression. Create the scope breakpoint before the watchpoint, so
10665 that we will encounter it first in bpstat_stop_status. */
10666 if (exp_valid_block != NULL && wp_frame != NULL)
10667 {
10668 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10669
10670 if (frame_id_p (caller_frame_id))
10671 {
10672 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10673 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10674
10675 scope_breakpoint
10676 = create_internal_breakpoint (caller_arch, caller_pc,
10677 bp_watchpoint_scope,
10678 &momentary_breakpoint_ops);
10679
10680 /* create_internal_breakpoint could invalidate WP_FRAME. */
10681 wp_frame = NULL;
10682
10683 scope_breakpoint->enable_state = bp_enabled;
10684
10685 /* Automatically delete the breakpoint when it hits. */
10686 scope_breakpoint->disposition = disp_del;
10687
10688 /* Only break in the proper frame (help with recursion). */
10689 scope_breakpoint->frame_id = caller_frame_id;
10690
10691 /* Set the address at which we will stop. */
10692 scope_breakpoint->loc->gdbarch = caller_arch;
10693 scope_breakpoint->loc->requested_address = caller_pc;
10694 scope_breakpoint->loc->address
10695 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10696 scope_breakpoint->loc->requested_address,
10697 scope_breakpoint->type);
10698 }
10699 }
10700
10701 /* Now set up the breakpoint. We create all watchpoints as hardware
10702 watchpoints here even if hardware watchpoints are turned off, a call
10703 to update_watchpoint later in this function will cause the type to
10704 drop back to bp_watchpoint (software watchpoint) if required. */
10705
10706 if (accessflag == hw_read)
10707 bp_type = bp_read_watchpoint;
10708 else if (accessflag == hw_access)
10709 bp_type = bp_access_watchpoint;
10710 else
10711 bp_type = bp_hardware_watchpoint;
10712
10713 std::unique_ptr<watchpoint> w (new watchpoint ());
10714
10715 if (use_mask)
10716 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10717 &masked_watchpoint_breakpoint_ops);
10718 else
10719 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10720 &watchpoint_breakpoint_ops);
10721 w->thread = thread;
10722 w->disposition = disp_donttouch;
10723 w->pspace = current_program_space;
10724 w->exp = std::move (exp);
10725 w->exp_valid_block = exp_valid_block;
10726 w->cond_exp_valid_block = cond_exp_valid_block;
10727 if (just_location)
10728 {
10729 struct type *t = value_type (val.get ());
10730 CORE_ADDR addr = value_as_address (val.get ());
10731
10732 w->exp_string_reparse
10733 = current_language->la_watch_location_expression (t, addr).release ();
10734
10735 w->exp_string = xstrprintf ("-location %.*s",
10736 (int) (exp_end - exp_start), exp_start);
10737 }
10738 else
10739 w->exp_string = savestring (exp_start, exp_end - exp_start);
10740
10741 if (use_mask)
10742 {
10743 w->hw_wp_mask = mask;
10744 }
10745 else
10746 {
10747 w->val = val;
10748 w->val_bitpos = saved_bitpos;
10749 w->val_bitsize = saved_bitsize;
10750 w->val_valid = true;
10751 }
10752
10753 if (cond_start)
10754 w->cond_string = savestring (cond_start, cond_end - cond_start);
10755 else
10756 w->cond_string = 0;
10757
10758 if (frame_id_p (watchpoint_frame))
10759 {
10760 w->watchpoint_frame = watchpoint_frame;
10761 w->watchpoint_thread = inferior_ptid;
10762 }
10763 else
10764 {
10765 w->watchpoint_frame = null_frame_id;
10766 w->watchpoint_thread = null_ptid;
10767 }
10768
10769 if (scope_breakpoint != NULL)
10770 {
10771 /* The scope breakpoint is related to the watchpoint. We will
10772 need to act on them together. */
10773 w->related_breakpoint = scope_breakpoint;
10774 scope_breakpoint->related_breakpoint = w.get ();
10775 }
10776
10777 if (!just_location)
10778 value_free_to_mark (mark);
10779
10780 /* Finally update the new watchpoint. This creates the locations
10781 that should be inserted. */
10782 update_watchpoint (w.get (), 1);
10783
10784 install_breakpoint (internal, std::move (w), 1);
10785 }
10786
10787 /* Return count of debug registers needed to watch the given expression.
10788 If the watchpoint cannot be handled in hardware return zero. */
10789
10790 static int
10791 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10792 {
10793 int found_memory_cnt = 0;
10794
10795 /* Did the user specifically forbid us to use hardware watchpoints? */
10796 if (!can_use_hw_watchpoints)
10797 return 0;
10798
10799 gdb_assert (!vals.empty ());
10800 struct value *head = vals[0].get ();
10801
10802 /* Make sure that the value of the expression depends only upon
10803 memory contents, and values computed from them within GDB. If we
10804 find any register references or function calls, we can't use a
10805 hardware watchpoint.
10806
10807 The idea here is that evaluating an expression generates a series
10808 of values, one holding the value of every subexpression. (The
10809 expression a*b+c has five subexpressions: a, b, a*b, c, and
10810 a*b+c.) GDB's values hold almost enough information to establish
10811 the criteria given above --- they identify memory lvalues,
10812 register lvalues, computed values, etcetera. So we can evaluate
10813 the expression, and then scan the chain of values that leaves
10814 behind to decide whether we can detect any possible change to the
10815 expression's final value using only hardware watchpoints.
10816
10817 However, I don't think that the values returned by inferior
10818 function calls are special in any way. So this function may not
10819 notice that an expression involving an inferior function call
10820 can't be watched with hardware watchpoints. FIXME. */
10821 for (const value_ref_ptr &iter : vals)
10822 {
10823 struct value *v = iter.get ();
10824
10825 if (VALUE_LVAL (v) == lval_memory)
10826 {
10827 if (v != head && value_lazy (v))
10828 /* A lazy memory lvalue in the chain is one that GDB never
10829 needed to fetch; we either just used its address (e.g.,
10830 `a' in `a.b') or we never needed it at all (e.g., `a'
10831 in `a,b'). This doesn't apply to HEAD; if that is
10832 lazy then it was not readable, but watch it anyway. */
10833 ;
10834 else
10835 {
10836 /* Ahh, memory we actually used! Check if we can cover
10837 it with hardware watchpoints. */
10838 struct type *vtype = check_typedef (value_type (v));
10839
10840 /* We only watch structs and arrays if user asked for it
10841 explicitly, never if they just happen to appear in a
10842 middle of some value chain. */
10843 if (v == head
10844 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10845 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10846 {
10847 CORE_ADDR vaddr = value_address (v);
10848 int len;
10849 int num_regs;
10850
10851 len = (target_exact_watchpoints
10852 && is_scalar_type_recursive (vtype))?
10853 1 : TYPE_LENGTH (value_type (v));
10854
10855 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10856 if (!num_regs)
10857 return 0;
10858 else
10859 found_memory_cnt += num_regs;
10860 }
10861 }
10862 }
10863 else if (VALUE_LVAL (v) != not_lval
10864 && deprecated_value_modifiable (v) == 0)
10865 return 0; /* These are values from the history (e.g., $1). */
10866 else if (VALUE_LVAL (v) == lval_register)
10867 return 0; /* Cannot watch a register with a HW watchpoint. */
10868 }
10869
10870 /* The expression itself looks suitable for using a hardware
10871 watchpoint, but give the target machine a chance to reject it. */
10872 return found_memory_cnt;
10873 }
10874
10875 void
10876 watch_command_wrapper (const char *arg, int from_tty, int internal)
10877 {
10878 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10879 }
10880
10881 /* A helper function that looks for the "-location" argument and then
10882 calls watch_command_1. */
10883
10884 static void
10885 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10886 {
10887 int just_location = 0;
10888
10889 if (arg
10890 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10891 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10892 just_location = 1;
10893
10894 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10895 }
10896
10897 static void
10898 watch_command (const char *arg, int from_tty)
10899 {
10900 watch_maybe_just_location (arg, hw_write, from_tty);
10901 }
10902
10903 void
10904 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
10905 {
10906 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10907 }
10908
10909 static void
10910 rwatch_command (const char *arg, int from_tty)
10911 {
10912 watch_maybe_just_location (arg, hw_read, from_tty);
10913 }
10914
10915 void
10916 awatch_command_wrapper (const char *arg, int from_tty, int internal)
10917 {
10918 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10919 }
10920
10921 static void
10922 awatch_command (const char *arg, int from_tty)
10923 {
10924 watch_maybe_just_location (arg, hw_access, from_tty);
10925 }
10926 \f
10927
10928 /* Data for the FSM that manages the until(location)/advance commands
10929 in infcmd.c. Here because it uses the mechanisms of
10930 breakpoints. */
10931
10932 struct until_break_fsm : public thread_fsm
10933 {
10934 /* The thread that was current when the command was executed. */
10935 int thread;
10936
10937 /* The breakpoint set at the destination location. */
10938 breakpoint_up location_breakpoint;
10939
10940 /* Breakpoint set at the return address in the caller frame. May be
10941 NULL. */
10942 breakpoint_up caller_breakpoint;
10943
10944 until_break_fsm (struct interp *cmd_interp, int thread,
10945 breakpoint_up &&location_breakpoint,
10946 breakpoint_up &&caller_breakpoint)
10947 : thread_fsm (cmd_interp),
10948 thread (thread),
10949 location_breakpoint (std::move (location_breakpoint)),
10950 caller_breakpoint (std::move (caller_breakpoint))
10951 {
10952 }
10953
10954 void clean_up (struct thread_info *thread) override;
10955 bool should_stop (struct thread_info *thread) override;
10956 enum async_reply_reason do_async_reply_reason () override;
10957 };
10958
10959 /* Implementation of the 'should_stop' FSM method for the
10960 until(location)/advance commands. */
10961
10962 bool
10963 until_break_fsm::should_stop (struct thread_info *tp)
10964 {
10965 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10966 location_breakpoint.get ()) != NULL
10967 || (caller_breakpoint != NULL
10968 && bpstat_find_breakpoint (tp->control.stop_bpstat,
10969 caller_breakpoint.get ()) != NULL))
10970 set_finished ();
10971
10972 return true;
10973 }
10974
10975 /* Implementation of the 'clean_up' FSM method for the
10976 until(location)/advance commands. */
10977
10978 void
10979 until_break_fsm::clean_up (struct thread_info *)
10980 {
10981 /* Clean up our temporary breakpoints. */
10982 location_breakpoint.reset ();
10983 caller_breakpoint.reset ();
10984 delete_longjmp_breakpoint (thread);
10985 }
10986
10987 /* Implementation of the 'async_reply_reason' FSM method for the
10988 until(location)/advance commands. */
10989
10990 enum async_reply_reason
10991 until_break_fsm::do_async_reply_reason ()
10992 {
10993 return EXEC_ASYNC_LOCATION_REACHED;
10994 }
10995
10996 void
10997 until_break_command (const char *arg, int from_tty, int anywhere)
10998 {
10999 struct frame_info *frame;
11000 struct gdbarch *frame_gdbarch;
11001 struct frame_id stack_frame_id;
11002 struct frame_id caller_frame_id;
11003 int thread;
11004 struct thread_info *tp;
11005
11006 clear_proceed_status (0);
11007
11008 /* Set a breakpoint where the user wants it and at return from
11009 this function. */
11010
11011 event_location_up location = string_to_event_location (&arg, current_language);
11012
11013 std::vector<symtab_and_line> sals
11014 = (last_displayed_sal_is_valid ()
11015 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11016 get_last_displayed_symtab (),
11017 get_last_displayed_line ())
11018 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11019 NULL, NULL, 0));
11020
11021 if (sals.size () != 1)
11022 error (_("Couldn't get information on specified line."));
11023
11024 symtab_and_line &sal = sals[0];
11025
11026 if (*arg)
11027 error (_("Junk at end of arguments."));
11028
11029 resolve_sal_pc (&sal);
11030
11031 tp = inferior_thread ();
11032 thread = tp->global_num;
11033
11034 /* Note linespec handling above invalidates the frame chain.
11035 Installing a breakpoint also invalidates the frame chain (as it
11036 may need to switch threads), so do any frame handling before
11037 that. */
11038
11039 frame = get_selected_frame (NULL);
11040 frame_gdbarch = get_frame_arch (frame);
11041 stack_frame_id = get_stack_frame_id (frame);
11042 caller_frame_id = frame_unwind_caller_id (frame);
11043
11044 /* Keep within the current frame, or in frames called by the current
11045 one. */
11046
11047 breakpoint_up caller_breakpoint;
11048
11049 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11050
11051 if (frame_id_p (caller_frame_id))
11052 {
11053 struct symtab_and_line sal2;
11054 struct gdbarch *caller_gdbarch;
11055
11056 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11057 sal2.pc = frame_unwind_caller_pc (frame);
11058 caller_gdbarch = frame_unwind_caller_arch (frame);
11059 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11060 sal2,
11061 caller_frame_id,
11062 bp_until);
11063
11064 set_longjmp_breakpoint (tp, caller_frame_id);
11065 lj_deleter.emplace (thread);
11066 }
11067
11068 /* set_momentary_breakpoint could invalidate FRAME. */
11069 frame = NULL;
11070
11071 breakpoint_up location_breakpoint;
11072 if (anywhere)
11073 /* If the user told us to continue until a specified location,
11074 we don't specify a frame at which we need to stop. */
11075 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11076 null_frame_id, bp_until);
11077 else
11078 /* Otherwise, specify the selected frame, because we want to stop
11079 only at the very same frame. */
11080 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11081 stack_frame_id, bp_until);
11082
11083 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11084 std::move (location_breakpoint),
11085 std::move (caller_breakpoint));
11086
11087 if (lj_deleter)
11088 lj_deleter->release ();
11089
11090 proceed (-1, GDB_SIGNAL_DEFAULT);
11091 }
11092
11093 /* This function attempts to parse an optional "if <cond>" clause
11094 from the arg string. If one is not found, it returns NULL.
11095
11096 Else, it returns a pointer to the condition string. (It does not
11097 attempt to evaluate the string against a particular block.) And,
11098 it updates arg to point to the first character following the parsed
11099 if clause in the arg string. */
11100
11101 const char *
11102 ep_parse_optional_if_clause (const char **arg)
11103 {
11104 const char *cond_string;
11105
11106 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11107 return NULL;
11108
11109 /* Skip the "if" keyword. */
11110 (*arg) += 2;
11111
11112 /* Skip any extra leading whitespace, and record the start of the
11113 condition string. */
11114 *arg = skip_spaces (*arg);
11115 cond_string = *arg;
11116
11117 /* Assume that the condition occupies the remainder of the arg
11118 string. */
11119 (*arg) += strlen (cond_string);
11120
11121 return cond_string;
11122 }
11123
11124 /* Commands to deal with catching events, such as signals, exceptions,
11125 process start/exit, etc. */
11126
11127 typedef enum
11128 {
11129 catch_fork_temporary, catch_vfork_temporary,
11130 catch_fork_permanent, catch_vfork_permanent
11131 }
11132 catch_fork_kind;
11133
11134 static void
11135 catch_fork_command_1 (const char *arg, int from_tty,
11136 struct cmd_list_element *command)
11137 {
11138 struct gdbarch *gdbarch = get_current_arch ();
11139 const char *cond_string = NULL;
11140 catch_fork_kind fork_kind;
11141 int tempflag;
11142
11143 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11144 tempflag = (fork_kind == catch_fork_temporary
11145 || fork_kind == catch_vfork_temporary);
11146
11147 if (!arg)
11148 arg = "";
11149 arg = skip_spaces (arg);
11150
11151 /* The allowed syntax is:
11152 catch [v]fork
11153 catch [v]fork if <cond>
11154
11155 First, check if there's an if clause. */
11156 cond_string = ep_parse_optional_if_clause (&arg);
11157
11158 if ((*arg != '\0') && !isspace (*arg))
11159 error (_("Junk at end of arguments."));
11160
11161 /* If this target supports it, create a fork or vfork catchpoint
11162 and enable reporting of such events. */
11163 switch (fork_kind)
11164 {
11165 case catch_fork_temporary:
11166 case catch_fork_permanent:
11167 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11168 &catch_fork_breakpoint_ops);
11169 break;
11170 case catch_vfork_temporary:
11171 case catch_vfork_permanent:
11172 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11173 &catch_vfork_breakpoint_ops);
11174 break;
11175 default:
11176 error (_("unsupported or unknown fork kind; cannot catch it"));
11177 break;
11178 }
11179 }
11180
11181 static void
11182 catch_exec_command_1 (const char *arg, int from_tty,
11183 struct cmd_list_element *command)
11184 {
11185 struct gdbarch *gdbarch = get_current_arch ();
11186 int tempflag;
11187 const char *cond_string = NULL;
11188
11189 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11190
11191 if (!arg)
11192 arg = "";
11193 arg = skip_spaces (arg);
11194
11195 /* The allowed syntax is:
11196 catch exec
11197 catch exec if <cond>
11198
11199 First, check if there's an if clause. */
11200 cond_string = ep_parse_optional_if_clause (&arg);
11201
11202 if ((*arg != '\0') && !isspace (*arg))
11203 error (_("Junk at end of arguments."));
11204
11205 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11206 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11207 &catch_exec_breakpoint_ops);
11208 c->exec_pathname = NULL;
11209
11210 install_breakpoint (0, std::move (c), 1);
11211 }
11212
11213 void
11214 init_ada_exception_breakpoint (struct breakpoint *b,
11215 struct gdbarch *gdbarch,
11216 struct symtab_and_line sal,
11217 const char *addr_string,
11218 const struct breakpoint_ops *ops,
11219 int tempflag,
11220 int enabled,
11221 int from_tty)
11222 {
11223 if (from_tty)
11224 {
11225 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11226 if (!loc_gdbarch)
11227 loc_gdbarch = gdbarch;
11228
11229 describe_other_breakpoints (loc_gdbarch,
11230 sal.pspace, sal.pc, sal.section, -1);
11231 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11232 version for exception catchpoints, because two catchpoints
11233 used for different exception names will use the same address.
11234 In this case, a "breakpoint ... also set at..." warning is
11235 unproductive. Besides, the warning phrasing is also a bit
11236 inappropriate, we should use the word catchpoint, and tell
11237 the user what type of catchpoint it is. The above is good
11238 enough for now, though. */
11239 }
11240
11241 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11242
11243 b->enable_state = enabled ? bp_enabled : bp_disabled;
11244 b->disposition = tempflag ? disp_del : disp_donttouch;
11245 b->location = string_to_event_location (&addr_string,
11246 language_def (language_ada));
11247 b->language = language_ada;
11248 }
11249
11250 static void
11251 catch_command (const char *arg, int from_tty)
11252 {
11253 error (_("Catch requires an event name."));
11254 }
11255 \f
11256
11257 static void
11258 tcatch_command (const char *arg, int from_tty)
11259 {
11260 error (_("Catch requires an event name."));
11261 }
11262
11263 /* Compare two breakpoints and return a strcmp-like result. */
11264
11265 static int
11266 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11267 {
11268 uintptr_t ua = (uintptr_t) a;
11269 uintptr_t ub = (uintptr_t) b;
11270
11271 if (a->number < b->number)
11272 return -1;
11273 else if (a->number > b->number)
11274 return 1;
11275
11276 /* Now sort by address, in case we see, e..g, two breakpoints with
11277 the number 0. */
11278 if (ua < ub)
11279 return -1;
11280 return ua > ub ? 1 : 0;
11281 }
11282
11283 /* Delete breakpoints by address or line. */
11284
11285 static void
11286 clear_command (const char *arg, int from_tty)
11287 {
11288 struct breakpoint *b;
11289 int default_match;
11290
11291 std::vector<symtab_and_line> decoded_sals;
11292 symtab_and_line last_sal;
11293 gdb::array_view<symtab_and_line> sals;
11294 if (arg)
11295 {
11296 decoded_sals
11297 = decode_line_with_current_source (arg,
11298 (DECODE_LINE_FUNFIRSTLINE
11299 | DECODE_LINE_LIST_MODE));
11300 default_match = 0;
11301 sals = decoded_sals;
11302 }
11303 else
11304 {
11305 /* Set sal's line, symtab, pc, and pspace to the values
11306 corresponding to the last call to print_frame_info. If the
11307 codepoint is not valid, this will set all the fields to 0. */
11308 last_sal = get_last_displayed_sal ();
11309 if (last_sal.symtab == 0)
11310 error (_("No source file specified."));
11311
11312 default_match = 1;
11313 sals = last_sal;
11314 }
11315
11316 /* We don't call resolve_sal_pc here. That's not as bad as it
11317 seems, because all existing breakpoints typically have both
11318 file/line and pc set. So, if clear is given file/line, we can
11319 match this to existing breakpoint without obtaining pc at all.
11320
11321 We only support clearing given the address explicitly
11322 present in breakpoint table. Say, we've set breakpoint
11323 at file:line. There were several PC values for that file:line,
11324 due to optimization, all in one block.
11325
11326 We've picked one PC value. If "clear" is issued with another
11327 PC corresponding to the same file:line, the breakpoint won't
11328 be cleared. We probably can still clear the breakpoint, but
11329 since the other PC value is never presented to user, user
11330 can only find it by guessing, and it does not seem important
11331 to support that. */
11332
11333 /* For each line spec given, delete bps which correspond to it. Do
11334 it in two passes, solely to preserve the current behavior that
11335 from_tty is forced true if we delete more than one
11336 breakpoint. */
11337
11338 std::vector<struct breakpoint *> found;
11339 for (const auto &sal : sals)
11340 {
11341 const char *sal_fullname;
11342
11343 /* If exact pc given, clear bpts at that pc.
11344 If line given (pc == 0), clear all bpts on specified line.
11345 If defaulting, clear all bpts on default line
11346 or at default pc.
11347
11348 defaulting sal.pc != 0 tests to do
11349
11350 0 1 pc
11351 1 1 pc _and_ line
11352 0 0 line
11353 1 0 <can't happen> */
11354
11355 sal_fullname = (sal.symtab == NULL
11356 ? NULL : symtab_to_fullname (sal.symtab));
11357
11358 /* Find all matching breakpoints and add them to 'found'. */
11359 ALL_BREAKPOINTS (b)
11360 {
11361 int match = 0;
11362 /* Are we going to delete b? */
11363 if (b->type != bp_none && !is_watchpoint (b))
11364 {
11365 struct bp_location *loc = b->loc;
11366 for (; loc; loc = loc->next)
11367 {
11368 /* If the user specified file:line, don't allow a PC
11369 match. This matches historical gdb behavior. */
11370 int pc_match = (!sal.explicit_line
11371 && sal.pc
11372 && (loc->pspace == sal.pspace)
11373 && (loc->address == sal.pc)
11374 && (!section_is_overlay (loc->section)
11375 || loc->section == sal.section));
11376 int line_match = 0;
11377
11378 if ((default_match || sal.explicit_line)
11379 && loc->symtab != NULL
11380 && sal_fullname != NULL
11381 && sal.pspace == loc->pspace
11382 && loc->line_number == sal.line
11383 && filename_cmp (symtab_to_fullname (loc->symtab),
11384 sal_fullname) == 0)
11385 line_match = 1;
11386
11387 if (pc_match || line_match)
11388 {
11389 match = 1;
11390 break;
11391 }
11392 }
11393 }
11394
11395 if (match)
11396 found.push_back (b);
11397 }
11398 }
11399
11400 /* Now go thru the 'found' chain and delete them. */
11401 if (found.empty ())
11402 {
11403 if (arg)
11404 error (_("No breakpoint at %s."), arg);
11405 else
11406 error (_("No breakpoint at this line."));
11407 }
11408
11409 /* Remove duplicates from the vec. */
11410 std::sort (found.begin (), found.end (),
11411 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11412 {
11413 return compare_breakpoints (bp_a, bp_b) < 0;
11414 });
11415 found.erase (std::unique (found.begin (), found.end (),
11416 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11417 {
11418 return compare_breakpoints (bp_a, bp_b) == 0;
11419 }),
11420 found.end ());
11421
11422 if (found.size () > 1)
11423 from_tty = 1; /* Always report if deleted more than one. */
11424 if (from_tty)
11425 {
11426 if (found.size () == 1)
11427 printf_unfiltered (_("Deleted breakpoint "));
11428 else
11429 printf_unfiltered (_("Deleted breakpoints "));
11430 }
11431
11432 for (breakpoint *iter : found)
11433 {
11434 if (from_tty)
11435 printf_unfiltered ("%d ", iter->number);
11436 delete_breakpoint (iter);
11437 }
11438 if (from_tty)
11439 putchar_unfiltered ('\n');
11440 }
11441 \f
11442 /* Delete breakpoint in BS if they are `delete' breakpoints and
11443 all breakpoints that are marked for deletion, whether hit or not.
11444 This is called after any breakpoint is hit, or after errors. */
11445
11446 void
11447 breakpoint_auto_delete (bpstat bs)
11448 {
11449 struct breakpoint *b, *b_tmp;
11450
11451 for (; bs; bs = bs->next)
11452 if (bs->breakpoint_at
11453 && bs->breakpoint_at->disposition == disp_del
11454 && bs->stop)
11455 delete_breakpoint (bs->breakpoint_at);
11456
11457 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11458 {
11459 if (b->disposition == disp_del_at_next_stop)
11460 delete_breakpoint (b);
11461 }
11462 }
11463
11464 /* A comparison function for bp_location AP and BP being interfaced to
11465 std::sort. Sort elements primarily by their ADDRESS (no matter what
11466 bl_address_is_meaningful says), secondarily by ordering first
11467 permanent elements and terciarily just ensuring the array is sorted
11468 stable way despite std::sort being an unstable algorithm. */
11469
11470 static int
11471 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11472 {
11473 if (a->address != b->address)
11474 return a->address < b->address;
11475
11476 /* Sort locations at the same address by their pspace number, keeping
11477 locations of the same inferior (in a multi-inferior environment)
11478 grouped. */
11479
11480 if (a->pspace->num != b->pspace->num)
11481 return a->pspace->num < b->pspace->num;
11482
11483 /* Sort permanent breakpoints first. */
11484 if (a->permanent != b->permanent)
11485 return a->permanent > b->permanent;
11486
11487 /* Make the internal GDB representation stable across GDB runs
11488 where A and B memory inside GDB can differ. Breakpoint locations of
11489 the same type at the same address can be sorted in arbitrary order. */
11490
11491 if (a->owner->number != b->owner->number)
11492 return a->owner->number < b->owner->number;
11493
11494 return a < b;
11495 }
11496
11497 /* Set bp_locations_placed_address_before_address_max and
11498 bp_locations_shadow_len_after_address_max according to the current
11499 content of the bp_locations array. */
11500
11501 static void
11502 bp_locations_target_extensions_update (void)
11503 {
11504 struct bp_location *bl, **blp_tmp;
11505
11506 bp_locations_placed_address_before_address_max = 0;
11507 bp_locations_shadow_len_after_address_max = 0;
11508
11509 ALL_BP_LOCATIONS (bl, blp_tmp)
11510 {
11511 CORE_ADDR start, end, addr;
11512
11513 if (!bp_location_has_shadow (bl))
11514 continue;
11515
11516 start = bl->target_info.placed_address;
11517 end = start + bl->target_info.shadow_len;
11518
11519 gdb_assert (bl->address >= start);
11520 addr = bl->address - start;
11521 if (addr > bp_locations_placed_address_before_address_max)
11522 bp_locations_placed_address_before_address_max = addr;
11523
11524 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11525
11526 gdb_assert (bl->address < end);
11527 addr = end - bl->address;
11528 if (addr > bp_locations_shadow_len_after_address_max)
11529 bp_locations_shadow_len_after_address_max = addr;
11530 }
11531 }
11532
11533 /* Download tracepoint locations if they haven't been. */
11534
11535 static void
11536 download_tracepoint_locations (void)
11537 {
11538 struct breakpoint *b;
11539 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11540
11541 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11542
11543 ALL_TRACEPOINTS (b)
11544 {
11545 struct bp_location *bl;
11546 struct tracepoint *t;
11547 int bp_location_downloaded = 0;
11548
11549 if ((b->type == bp_fast_tracepoint
11550 ? !may_insert_fast_tracepoints
11551 : !may_insert_tracepoints))
11552 continue;
11553
11554 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11555 {
11556 if (target_can_download_tracepoint ())
11557 can_download_tracepoint = TRIBOOL_TRUE;
11558 else
11559 can_download_tracepoint = TRIBOOL_FALSE;
11560 }
11561
11562 if (can_download_tracepoint == TRIBOOL_FALSE)
11563 break;
11564
11565 for (bl = b->loc; bl; bl = bl->next)
11566 {
11567 /* In tracepoint, locations are _never_ duplicated, so
11568 should_be_inserted is equivalent to
11569 unduplicated_should_be_inserted. */
11570 if (!should_be_inserted (bl) || bl->inserted)
11571 continue;
11572
11573 switch_to_program_space_and_thread (bl->pspace);
11574
11575 target_download_tracepoint (bl);
11576
11577 bl->inserted = 1;
11578 bp_location_downloaded = 1;
11579 }
11580 t = (struct tracepoint *) b;
11581 t->number_on_target = b->number;
11582 if (bp_location_downloaded)
11583 gdb::observers::breakpoint_modified.notify (b);
11584 }
11585 }
11586
11587 /* Swap the insertion/duplication state between two locations. */
11588
11589 static void
11590 swap_insertion (struct bp_location *left, struct bp_location *right)
11591 {
11592 const int left_inserted = left->inserted;
11593 const int left_duplicate = left->duplicate;
11594 const int left_needs_update = left->needs_update;
11595 const struct bp_target_info left_target_info = left->target_info;
11596
11597 /* Locations of tracepoints can never be duplicated. */
11598 if (is_tracepoint (left->owner))
11599 gdb_assert (!left->duplicate);
11600 if (is_tracepoint (right->owner))
11601 gdb_assert (!right->duplicate);
11602
11603 left->inserted = right->inserted;
11604 left->duplicate = right->duplicate;
11605 left->needs_update = right->needs_update;
11606 left->target_info = right->target_info;
11607 right->inserted = left_inserted;
11608 right->duplicate = left_duplicate;
11609 right->needs_update = left_needs_update;
11610 right->target_info = left_target_info;
11611 }
11612
11613 /* Force the re-insertion of the locations at ADDRESS. This is called
11614 once a new/deleted/modified duplicate location is found and we are evaluating
11615 conditions on the target's side. Such conditions need to be updated on
11616 the target. */
11617
11618 static void
11619 force_breakpoint_reinsertion (struct bp_location *bl)
11620 {
11621 struct bp_location **locp = NULL, **loc2p;
11622 struct bp_location *loc;
11623 CORE_ADDR address = 0;
11624 int pspace_num;
11625
11626 address = bl->address;
11627 pspace_num = bl->pspace->num;
11628
11629 /* This is only meaningful if the target is
11630 evaluating conditions and if the user has
11631 opted for condition evaluation on the target's
11632 side. */
11633 if (gdb_evaluates_breakpoint_condition_p ()
11634 || !target_supports_evaluation_of_breakpoint_conditions ())
11635 return;
11636
11637 /* Flag all breakpoint locations with this address and
11638 the same program space as the location
11639 as "its condition has changed". We need to
11640 update the conditions on the target's side. */
11641 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11642 {
11643 loc = *loc2p;
11644
11645 if (!is_breakpoint (loc->owner)
11646 || pspace_num != loc->pspace->num)
11647 continue;
11648
11649 /* Flag the location appropriately. We use a different state to
11650 let everyone know that we already updated the set of locations
11651 with addr bl->address and program space bl->pspace. This is so
11652 we don't have to keep calling these functions just to mark locations
11653 that have already been marked. */
11654 loc->condition_changed = condition_updated;
11655
11656 /* Free the agent expression bytecode as well. We will compute
11657 it later on. */
11658 loc->cond_bytecode.reset ();
11659 }
11660 }
11661 /* Called whether new breakpoints are created, or existing breakpoints
11662 deleted, to update the global location list and recompute which
11663 locations are duplicate of which.
11664
11665 The INSERT_MODE flag determines whether locations may not, may, or
11666 shall be inserted now. See 'enum ugll_insert_mode' for more
11667 info. */
11668
11669 static void
11670 update_global_location_list (enum ugll_insert_mode insert_mode)
11671 {
11672 struct breakpoint *b;
11673 struct bp_location **locp, *loc;
11674 /* Last breakpoint location address that was marked for update. */
11675 CORE_ADDR last_addr = 0;
11676 /* Last breakpoint location program space that was marked for update. */
11677 int last_pspace_num = -1;
11678
11679 /* Used in the duplicates detection below. When iterating over all
11680 bp_locations, points to the first bp_location of a given address.
11681 Breakpoints and watchpoints of different types are never
11682 duplicates of each other. Keep one pointer for each type of
11683 breakpoint/watchpoint, so we only need to loop over all locations
11684 once. */
11685 struct bp_location *bp_loc_first; /* breakpoint */
11686 struct bp_location *wp_loc_first; /* hardware watchpoint */
11687 struct bp_location *awp_loc_first; /* access watchpoint */
11688 struct bp_location *rwp_loc_first; /* read watchpoint */
11689
11690 /* Saved former bp_locations array which we compare against the newly
11691 built bp_locations from the current state of ALL_BREAKPOINTS. */
11692 struct bp_location **old_locp;
11693 unsigned old_locations_count;
11694 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11695
11696 old_locations_count = bp_locations_count;
11697 bp_locations = NULL;
11698 bp_locations_count = 0;
11699
11700 ALL_BREAKPOINTS (b)
11701 for (loc = b->loc; loc; loc = loc->next)
11702 bp_locations_count++;
11703
11704 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11705 locp = bp_locations;
11706 ALL_BREAKPOINTS (b)
11707 for (loc = b->loc; loc; loc = loc->next)
11708 *locp++ = loc;
11709 std::sort (bp_locations, bp_locations + bp_locations_count,
11710 bp_location_is_less_than);
11711
11712 bp_locations_target_extensions_update ();
11713
11714 /* Identify bp_location instances that are no longer present in the
11715 new list, and therefore should be freed. Note that it's not
11716 necessary that those locations should be removed from inferior --
11717 if there's another location at the same address (previously
11718 marked as duplicate), we don't need to remove/insert the
11719 location.
11720
11721 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11722 and former bp_location array state respectively. */
11723
11724 locp = bp_locations;
11725 for (old_locp = old_locations.get ();
11726 old_locp < old_locations.get () + old_locations_count;
11727 old_locp++)
11728 {
11729 struct bp_location *old_loc = *old_locp;
11730 struct bp_location **loc2p;
11731
11732 /* Tells if 'old_loc' is found among the new locations. If
11733 not, we have to free it. */
11734 int found_object = 0;
11735 /* Tells if the location should remain inserted in the target. */
11736 int keep_in_target = 0;
11737 int removed = 0;
11738
11739 /* Skip LOCP entries which will definitely never be needed.
11740 Stop either at or being the one matching OLD_LOC. */
11741 while (locp < bp_locations + bp_locations_count
11742 && (*locp)->address < old_loc->address)
11743 locp++;
11744
11745 for (loc2p = locp;
11746 (loc2p < bp_locations + bp_locations_count
11747 && (*loc2p)->address == old_loc->address);
11748 loc2p++)
11749 {
11750 /* Check if this is a new/duplicated location or a duplicated
11751 location that had its condition modified. If so, we want to send
11752 its condition to the target if evaluation of conditions is taking
11753 place there. */
11754 if ((*loc2p)->condition_changed == condition_modified
11755 && (last_addr != old_loc->address
11756 || last_pspace_num != old_loc->pspace->num))
11757 {
11758 force_breakpoint_reinsertion (*loc2p);
11759 last_pspace_num = old_loc->pspace->num;
11760 }
11761
11762 if (*loc2p == old_loc)
11763 found_object = 1;
11764 }
11765
11766 /* We have already handled this address, update it so that we don't
11767 have to go through updates again. */
11768 last_addr = old_loc->address;
11769
11770 /* Target-side condition evaluation: Handle deleted locations. */
11771 if (!found_object)
11772 force_breakpoint_reinsertion (old_loc);
11773
11774 /* If this location is no longer present, and inserted, look if
11775 there's maybe a new location at the same address. If so,
11776 mark that one inserted, and don't remove this one. This is
11777 needed so that we don't have a time window where a breakpoint
11778 at certain location is not inserted. */
11779
11780 if (old_loc->inserted)
11781 {
11782 /* If the location is inserted now, we might have to remove
11783 it. */
11784
11785 if (found_object && should_be_inserted (old_loc))
11786 {
11787 /* The location is still present in the location list,
11788 and still should be inserted. Don't do anything. */
11789 keep_in_target = 1;
11790 }
11791 else
11792 {
11793 /* This location still exists, but it won't be kept in the
11794 target since it may have been disabled. We proceed to
11795 remove its target-side condition. */
11796
11797 /* The location is either no longer present, or got
11798 disabled. See if there's another location at the
11799 same address, in which case we don't need to remove
11800 this one from the target. */
11801
11802 /* OLD_LOC comes from existing struct breakpoint. */
11803 if (bl_address_is_meaningful (old_loc))
11804 {
11805 for (loc2p = locp;
11806 (loc2p < bp_locations + bp_locations_count
11807 && (*loc2p)->address == old_loc->address);
11808 loc2p++)
11809 {
11810 struct bp_location *loc2 = *loc2p;
11811
11812 if (breakpoint_locations_match (loc2, old_loc))
11813 {
11814 /* Read watchpoint locations are switched to
11815 access watchpoints, if the former are not
11816 supported, but the latter are. */
11817 if (is_hardware_watchpoint (old_loc->owner))
11818 {
11819 gdb_assert (is_hardware_watchpoint (loc2->owner));
11820 loc2->watchpoint_type = old_loc->watchpoint_type;
11821 }
11822
11823 /* loc2 is a duplicated location. We need to check
11824 if it should be inserted in case it will be
11825 unduplicated. */
11826 if (loc2 != old_loc
11827 && unduplicated_should_be_inserted (loc2))
11828 {
11829 swap_insertion (old_loc, loc2);
11830 keep_in_target = 1;
11831 break;
11832 }
11833 }
11834 }
11835 }
11836 }
11837
11838 if (!keep_in_target)
11839 {
11840 if (remove_breakpoint (old_loc))
11841 {
11842 /* This is just about all we can do. We could keep
11843 this location on the global list, and try to
11844 remove it next time, but there's no particular
11845 reason why we will succeed next time.
11846
11847 Note that at this point, old_loc->owner is still
11848 valid, as delete_breakpoint frees the breakpoint
11849 only after calling us. */
11850 printf_filtered (_("warning: Error removing "
11851 "breakpoint %d\n"),
11852 old_loc->owner->number);
11853 }
11854 removed = 1;
11855 }
11856 }
11857
11858 if (!found_object)
11859 {
11860 if (removed && target_is_non_stop_p ()
11861 && need_moribund_for_location_type (old_loc))
11862 {
11863 /* This location was removed from the target. In
11864 non-stop mode, a race condition is possible where
11865 we've removed a breakpoint, but stop events for that
11866 breakpoint are already queued and will arrive later.
11867 We apply an heuristic to be able to distinguish such
11868 SIGTRAPs from other random SIGTRAPs: we keep this
11869 breakpoint location for a bit, and will retire it
11870 after we see some number of events. The theory here
11871 is that reporting of events should, "on the average",
11872 be fair, so after a while we'll see events from all
11873 threads that have anything of interest, and no longer
11874 need to keep this breakpoint location around. We
11875 don't hold locations forever so to reduce chances of
11876 mistaking a non-breakpoint SIGTRAP for a breakpoint
11877 SIGTRAP.
11878
11879 The heuristic failing can be disastrous on
11880 decr_pc_after_break targets.
11881
11882 On decr_pc_after_break targets, like e.g., x86-linux,
11883 if we fail to recognize a late breakpoint SIGTRAP,
11884 because events_till_retirement has reached 0 too
11885 soon, we'll fail to do the PC adjustment, and report
11886 a random SIGTRAP to the user. When the user resumes
11887 the inferior, it will most likely immediately crash
11888 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11889 corrupted, because of being resumed e.g., in the
11890 middle of a multi-byte instruction, or skipped a
11891 one-byte instruction. This was actually seen happen
11892 on native x86-linux, and should be less rare on
11893 targets that do not support new thread events, like
11894 remote, due to the heuristic depending on
11895 thread_count.
11896
11897 Mistaking a random SIGTRAP for a breakpoint trap
11898 causes similar symptoms (PC adjustment applied when
11899 it shouldn't), but then again, playing with SIGTRAPs
11900 behind the debugger's back is asking for trouble.
11901
11902 Since hardware watchpoint traps are always
11903 distinguishable from other traps, so we don't need to
11904 apply keep hardware watchpoint moribund locations
11905 around. We simply always ignore hardware watchpoint
11906 traps we can no longer explain. */
11907
11908 process_stratum_target *proc_target = nullptr;
11909 for (inferior *inf : all_inferiors ())
11910 if (inf->pspace == old_loc->pspace)
11911 {
11912 proc_target = inf->process_target ();
11913 break;
11914 }
11915 if (proc_target != nullptr)
11916 old_loc->events_till_retirement
11917 = 3 * (thread_count (proc_target) + 1);
11918 else
11919 old_loc->events_till_retirement = 1;
11920 old_loc->owner = NULL;
11921
11922 moribund_locations.push_back (old_loc);
11923 }
11924 else
11925 {
11926 old_loc->owner = NULL;
11927 decref_bp_location (&old_loc);
11928 }
11929 }
11930 }
11931
11932 /* Rescan breakpoints at the same address and section, marking the
11933 first one as "first" and any others as "duplicates". This is so
11934 that the bpt instruction is only inserted once. If we have a
11935 permanent breakpoint at the same place as BPT, make that one the
11936 official one, and the rest as duplicates. Permanent breakpoints
11937 are sorted first for the same address.
11938
11939 Do the same for hardware watchpoints, but also considering the
11940 watchpoint's type (regular/access/read) and length. */
11941
11942 bp_loc_first = NULL;
11943 wp_loc_first = NULL;
11944 awp_loc_first = NULL;
11945 rwp_loc_first = NULL;
11946 ALL_BP_LOCATIONS (loc, locp)
11947 {
11948 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11949 non-NULL. */
11950 struct bp_location **loc_first_p;
11951 b = loc->owner;
11952
11953 if (!unduplicated_should_be_inserted (loc)
11954 || !bl_address_is_meaningful (loc)
11955 /* Don't detect duplicate for tracepoint locations because they are
11956 never duplicated. See the comments in field `duplicate' of
11957 `struct bp_location'. */
11958 || is_tracepoint (b))
11959 {
11960 /* Clear the condition modification flag. */
11961 loc->condition_changed = condition_unchanged;
11962 continue;
11963 }
11964
11965 if (b->type == bp_hardware_watchpoint)
11966 loc_first_p = &wp_loc_first;
11967 else if (b->type == bp_read_watchpoint)
11968 loc_first_p = &rwp_loc_first;
11969 else if (b->type == bp_access_watchpoint)
11970 loc_first_p = &awp_loc_first;
11971 else
11972 loc_first_p = &bp_loc_first;
11973
11974 if (*loc_first_p == NULL
11975 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11976 || !breakpoint_locations_match (loc, *loc_first_p))
11977 {
11978 *loc_first_p = loc;
11979 loc->duplicate = 0;
11980
11981 if (is_breakpoint (loc->owner) && loc->condition_changed)
11982 {
11983 loc->needs_update = 1;
11984 /* Clear the condition modification flag. */
11985 loc->condition_changed = condition_unchanged;
11986 }
11987 continue;
11988 }
11989
11990
11991 /* This and the above ensure the invariant that the first location
11992 is not duplicated, and is the inserted one.
11993 All following are marked as duplicated, and are not inserted. */
11994 if (loc->inserted)
11995 swap_insertion (loc, *loc_first_p);
11996 loc->duplicate = 1;
11997
11998 /* Clear the condition modification flag. */
11999 loc->condition_changed = condition_unchanged;
12000 }
12001
12002 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12003 {
12004 if (insert_mode != UGLL_DONT_INSERT)
12005 insert_breakpoint_locations ();
12006 else
12007 {
12008 /* Even though the caller told us to not insert new
12009 locations, we may still need to update conditions on the
12010 target's side of breakpoints that were already inserted
12011 if the target is evaluating breakpoint conditions. We
12012 only update conditions for locations that are marked
12013 "needs_update". */
12014 update_inserted_breakpoint_locations ();
12015 }
12016 }
12017
12018 if (insert_mode != UGLL_DONT_INSERT)
12019 download_tracepoint_locations ();
12020 }
12021
12022 void
12023 breakpoint_retire_moribund (void)
12024 {
12025 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12026 {
12027 struct bp_location *loc = moribund_locations[ix];
12028 if (--(loc->events_till_retirement) == 0)
12029 {
12030 decref_bp_location (&loc);
12031 unordered_remove (moribund_locations, ix);
12032 --ix;
12033 }
12034 }
12035 }
12036
12037 static void
12038 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12039 {
12040
12041 try
12042 {
12043 update_global_location_list (insert_mode);
12044 }
12045 catch (const gdb_exception_error &e)
12046 {
12047 }
12048 }
12049
12050 /* Clear BKP from a BPS. */
12051
12052 static void
12053 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12054 {
12055 bpstat bs;
12056
12057 for (bs = bps; bs; bs = bs->next)
12058 if (bs->breakpoint_at == bpt)
12059 {
12060 bs->breakpoint_at = NULL;
12061 bs->old_val = NULL;
12062 /* bs->commands will be freed later. */
12063 }
12064 }
12065
12066 /* Callback for iterate_over_threads. */
12067 static int
12068 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12069 {
12070 struct breakpoint *bpt = (struct breakpoint *) data;
12071
12072 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12073 return 0;
12074 }
12075
12076 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12077 callbacks. */
12078
12079 static void
12080 say_where (struct breakpoint *b)
12081 {
12082 struct value_print_options opts;
12083
12084 get_user_print_options (&opts);
12085
12086 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12087 single string. */
12088 if (b->loc == NULL)
12089 {
12090 /* For pending locations, the output differs slightly based
12091 on b->extra_string. If this is non-NULL, it contains either
12092 a condition or dprintf arguments. */
12093 if (b->extra_string == NULL)
12094 {
12095 printf_filtered (_(" (%s) pending."),
12096 event_location_to_string (b->location.get ()));
12097 }
12098 else if (b->type == bp_dprintf)
12099 {
12100 printf_filtered (_(" (%s,%s) pending."),
12101 event_location_to_string (b->location.get ()),
12102 b->extra_string);
12103 }
12104 else
12105 {
12106 printf_filtered (_(" (%s %s) pending."),
12107 event_location_to_string (b->location.get ()),
12108 b->extra_string);
12109 }
12110 }
12111 else
12112 {
12113 if (opts.addressprint || b->loc->symtab == NULL)
12114 printf_filtered (" at %ps",
12115 styled_string (address_style.style (),
12116 paddress (b->loc->gdbarch,
12117 b->loc->address)));
12118 if (b->loc->symtab != NULL)
12119 {
12120 /* If there is a single location, we can print the location
12121 more nicely. */
12122 if (b->loc->next == NULL)
12123 {
12124 const char *filename
12125 = symtab_to_filename_for_display (b->loc->symtab);
12126 printf_filtered (": file %ps, line %d.",
12127 styled_string (file_name_style.style (),
12128 filename),
12129 b->loc->line_number);
12130 }
12131 else
12132 /* This is not ideal, but each location may have a
12133 different file name, and this at least reflects the
12134 real situation somewhat. */
12135 printf_filtered (": %s.",
12136 event_location_to_string (b->location.get ()));
12137 }
12138
12139 if (b->loc->next)
12140 {
12141 struct bp_location *loc = b->loc;
12142 int n = 0;
12143 for (; loc; loc = loc->next)
12144 ++n;
12145 printf_filtered (" (%d locations)", n);
12146 }
12147 }
12148 }
12149
12150 bp_location::~bp_location ()
12151 {
12152 xfree (function_name);
12153 }
12154
12155 /* Destructor for the breakpoint base class. */
12156
12157 breakpoint::~breakpoint ()
12158 {
12159 xfree (this->cond_string);
12160 xfree (this->extra_string);
12161 }
12162
12163 static struct bp_location *
12164 base_breakpoint_allocate_location (struct breakpoint *self)
12165 {
12166 return new bp_location (self);
12167 }
12168
12169 static void
12170 base_breakpoint_re_set (struct breakpoint *b)
12171 {
12172 /* Nothing to re-set. */
12173 }
12174
12175 #define internal_error_pure_virtual_called() \
12176 gdb_assert_not_reached ("pure virtual function called")
12177
12178 static int
12179 base_breakpoint_insert_location (struct bp_location *bl)
12180 {
12181 internal_error_pure_virtual_called ();
12182 }
12183
12184 static int
12185 base_breakpoint_remove_location (struct bp_location *bl,
12186 enum remove_bp_reason reason)
12187 {
12188 internal_error_pure_virtual_called ();
12189 }
12190
12191 static int
12192 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12193 const address_space *aspace,
12194 CORE_ADDR bp_addr,
12195 const struct target_waitstatus *ws)
12196 {
12197 internal_error_pure_virtual_called ();
12198 }
12199
12200 static void
12201 base_breakpoint_check_status (bpstat bs)
12202 {
12203 /* Always stop. */
12204 }
12205
12206 /* A "works_in_software_mode" breakpoint_ops method that just internal
12207 errors. */
12208
12209 static int
12210 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12211 {
12212 internal_error_pure_virtual_called ();
12213 }
12214
12215 /* A "resources_needed" breakpoint_ops method that just internal
12216 errors. */
12217
12218 static int
12219 base_breakpoint_resources_needed (const struct bp_location *bl)
12220 {
12221 internal_error_pure_virtual_called ();
12222 }
12223
12224 static enum print_stop_action
12225 base_breakpoint_print_it (bpstat bs)
12226 {
12227 internal_error_pure_virtual_called ();
12228 }
12229
12230 static void
12231 base_breakpoint_print_one_detail (const struct breakpoint *self,
12232 struct ui_out *uiout)
12233 {
12234 /* nothing */
12235 }
12236
12237 static void
12238 base_breakpoint_print_mention (struct breakpoint *b)
12239 {
12240 internal_error_pure_virtual_called ();
12241 }
12242
12243 static void
12244 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12245 {
12246 internal_error_pure_virtual_called ();
12247 }
12248
12249 static void
12250 base_breakpoint_create_sals_from_location
12251 (const struct event_location *location,
12252 struct linespec_result *canonical,
12253 enum bptype type_wanted)
12254 {
12255 internal_error_pure_virtual_called ();
12256 }
12257
12258 static void
12259 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12260 struct linespec_result *c,
12261 gdb::unique_xmalloc_ptr<char> cond_string,
12262 gdb::unique_xmalloc_ptr<char> extra_string,
12263 enum bptype type_wanted,
12264 enum bpdisp disposition,
12265 int thread,
12266 int task, int ignore_count,
12267 const struct breakpoint_ops *o,
12268 int from_tty, int enabled,
12269 int internal, unsigned flags)
12270 {
12271 internal_error_pure_virtual_called ();
12272 }
12273
12274 static std::vector<symtab_and_line>
12275 base_breakpoint_decode_location (struct breakpoint *b,
12276 const struct event_location *location,
12277 struct program_space *search_pspace)
12278 {
12279 internal_error_pure_virtual_called ();
12280 }
12281
12282 /* The default 'explains_signal' method. */
12283
12284 static int
12285 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12286 {
12287 return 1;
12288 }
12289
12290 /* The default "after_condition_true" method. */
12291
12292 static void
12293 base_breakpoint_after_condition_true (struct bpstats *bs)
12294 {
12295 /* Nothing to do. */
12296 }
12297
12298 struct breakpoint_ops base_breakpoint_ops =
12299 {
12300 base_breakpoint_allocate_location,
12301 base_breakpoint_re_set,
12302 base_breakpoint_insert_location,
12303 base_breakpoint_remove_location,
12304 base_breakpoint_breakpoint_hit,
12305 base_breakpoint_check_status,
12306 base_breakpoint_resources_needed,
12307 base_breakpoint_works_in_software_mode,
12308 base_breakpoint_print_it,
12309 NULL,
12310 base_breakpoint_print_one_detail,
12311 base_breakpoint_print_mention,
12312 base_breakpoint_print_recreate,
12313 base_breakpoint_create_sals_from_location,
12314 base_breakpoint_create_breakpoints_sal,
12315 base_breakpoint_decode_location,
12316 base_breakpoint_explains_signal,
12317 base_breakpoint_after_condition_true,
12318 };
12319
12320 /* Default breakpoint_ops methods. */
12321
12322 static void
12323 bkpt_re_set (struct breakpoint *b)
12324 {
12325 /* FIXME: is this still reachable? */
12326 if (breakpoint_event_location_empty_p (b))
12327 {
12328 /* Anything without a location can't be re-set. */
12329 delete_breakpoint (b);
12330 return;
12331 }
12332
12333 breakpoint_re_set_default (b);
12334 }
12335
12336 static int
12337 bkpt_insert_location (struct bp_location *bl)
12338 {
12339 CORE_ADDR addr = bl->target_info.reqstd_address;
12340
12341 bl->target_info.kind = breakpoint_kind (bl, &addr);
12342 bl->target_info.placed_address = addr;
12343
12344 if (bl->loc_type == bp_loc_hardware_breakpoint)
12345 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12346 else
12347 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12348 }
12349
12350 static int
12351 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12352 {
12353 if (bl->loc_type == bp_loc_hardware_breakpoint)
12354 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12355 else
12356 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12357 }
12358
12359 static int
12360 bkpt_breakpoint_hit (const struct bp_location *bl,
12361 const address_space *aspace, CORE_ADDR bp_addr,
12362 const struct target_waitstatus *ws)
12363 {
12364 if (ws->kind != TARGET_WAITKIND_STOPPED
12365 || ws->value.sig != GDB_SIGNAL_TRAP)
12366 return 0;
12367
12368 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12369 aspace, bp_addr))
12370 return 0;
12371
12372 if (overlay_debugging /* unmapped overlay section */
12373 && section_is_overlay (bl->section)
12374 && !section_is_mapped (bl->section))
12375 return 0;
12376
12377 return 1;
12378 }
12379
12380 static int
12381 dprintf_breakpoint_hit (const struct bp_location *bl,
12382 const address_space *aspace, CORE_ADDR bp_addr,
12383 const struct target_waitstatus *ws)
12384 {
12385 if (dprintf_style == dprintf_style_agent
12386 && target_can_run_breakpoint_commands ())
12387 {
12388 /* An agent-style dprintf never causes a stop. If we see a trap
12389 for this address it must be for a breakpoint that happens to
12390 be set at the same address. */
12391 return 0;
12392 }
12393
12394 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12395 }
12396
12397 static int
12398 bkpt_resources_needed (const struct bp_location *bl)
12399 {
12400 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12401
12402 return 1;
12403 }
12404
12405 static enum print_stop_action
12406 bkpt_print_it (bpstat bs)
12407 {
12408 struct breakpoint *b;
12409 const struct bp_location *bl;
12410 int bp_temp;
12411 struct ui_out *uiout = current_uiout;
12412
12413 gdb_assert (bs->bp_location_at != NULL);
12414
12415 bl = bs->bp_location_at;
12416 b = bs->breakpoint_at;
12417
12418 bp_temp = b->disposition == disp_del;
12419 if (bl->address != bl->requested_address)
12420 breakpoint_adjustment_warning (bl->requested_address,
12421 bl->address,
12422 b->number, 1);
12423 annotate_breakpoint (b->number);
12424 maybe_print_thread_hit_breakpoint (uiout);
12425
12426 if (uiout->is_mi_like_p ())
12427 {
12428 uiout->field_string ("reason",
12429 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12430 uiout->field_string ("disp", bpdisp_text (b->disposition));
12431 }
12432 if (bp_temp)
12433 uiout->message ("Temporary breakpoint %pF, ",
12434 signed_field ("bkptno", b->number));
12435 else
12436 uiout->message ("Breakpoint %pF, ",
12437 signed_field ("bkptno", b->number));
12438
12439 return PRINT_SRC_AND_LOC;
12440 }
12441
12442 static void
12443 bkpt_print_mention (struct breakpoint *b)
12444 {
12445 if (current_uiout->is_mi_like_p ())
12446 return;
12447
12448 switch (b->type)
12449 {
12450 case bp_breakpoint:
12451 case bp_gnu_ifunc_resolver:
12452 if (b->disposition == disp_del)
12453 printf_filtered (_("Temporary breakpoint"));
12454 else
12455 printf_filtered (_("Breakpoint"));
12456 printf_filtered (_(" %d"), b->number);
12457 if (b->type == bp_gnu_ifunc_resolver)
12458 printf_filtered (_(" at gnu-indirect-function resolver"));
12459 break;
12460 case bp_hardware_breakpoint:
12461 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12462 break;
12463 case bp_dprintf:
12464 printf_filtered (_("Dprintf %d"), b->number);
12465 break;
12466 }
12467
12468 say_where (b);
12469 }
12470
12471 static void
12472 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12473 {
12474 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12475 fprintf_unfiltered (fp, "tbreak");
12476 else if (tp->type == bp_breakpoint)
12477 fprintf_unfiltered (fp, "break");
12478 else if (tp->type == bp_hardware_breakpoint
12479 && tp->disposition == disp_del)
12480 fprintf_unfiltered (fp, "thbreak");
12481 else if (tp->type == bp_hardware_breakpoint)
12482 fprintf_unfiltered (fp, "hbreak");
12483 else
12484 internal_error (__FILE__, __LINE__,
12485 _("unhandled breakpoint type %d"), (int) tp->type);
12486
12487 fprintf_unfiltered (fp, " %s",
12488 event_location_to_string (tp->location.get ()));
12489
12490 /* Print out extra_string if this breakpoint is pending. It might
12491 contain, for example, conditions that were set by the user. */
12492 if (tp->loc == NULL && tp->extra_string != NULL)
12493 fprintf_unfiltered (fp, " %s", tp->extra_string);
12494
12495 print_recreate_thread (tp, fp);
12496 }
12497
12498 static void
12499 bkpt_create_sals_from_location (const struct event_location *location,
12500 struct linespec_result *canonical,
12501 enum bptype type_wanted)
12502 {
12503 create_sals_from_location_default (location, canonical, type_wanted);
12504 }
12505
12506 static void
12507 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12508 struct linespec_result *canonical,
12509 gdb::unique_xmalloc_ptr<char> cond_string,
12510 gdb::unique_xmalloc_ptr<char> extra_string,
12511 enum bptype type_wanted,
12512 enum bpdisp disposition,
12513 int thread,
12514 int task, int ignore_count,
12515 const struct breakpoint_ops *ops,
12516 int from_tty, int enabled,
12517 int internal, unsigned flags)
12518 {
12519 create_breakpoints_sal_default (gdbarch, canonical,
12520 std::move (cond_string),
12521 std::move (extra_string),
12522 type_wanted,
12523 disposition, thread, task,
12524 ignore_count, ops, from_tty,
12525 enabled, internal, flags);
12526 }
12527
12528 static std::vector<symtab_and_line>
12529 bkpt_decode_location (struct breakpoint *b,
12530 const struct event_location *location,
12531 struct program_space *search_pspace)
12532 {
12533 return decode_location_default (b, location, search_pspace);
12534 }
12535
12536 /* Virtual table for internal breakpoints. */
12537
12538 static void
12539 internal_bkpt_re_set (struct breakpoint *b)
12540 {
12541 switch (b->type)
12542 {
12543 /* Delete overlay event and longjmp master breakpoints; they
12544 will be reset later by breakpoint_re_set. */
12545 case bp_overlay_event:
12546 case bp_longjmp_master:
12547 case bp_std_terminate_master:
12548 case bp_exception_master:
12549 delete_breakpoint (b);
12550 break;
12551
12552 /* This breakpoint is special, it's set up when the inferior
12553 starts and we really don't want to touch it. */
12554 case bp_shlib_event:
12555
12556 /* Like bp_shlib_event, this breakpoint type is special. Once
12557 it is set up, we do not want to touch it. */
12558 case bp_thread_event:
12559 break;
12560 }
12561 }
12562
12563 static void
12564 internal_bkpt_check_status (bpstat bs)
12565 {
12566 if (bs->breakpoint_at->type == bp_shlib_event)
12567 {
12568 /* If requested, stop when the dynamic linker notifies GDB of
12569 events. This allows the user to get control and place
12570 breakpoints in initializer routines for dynamically loaded
12571 objects (among other things). */
12572 bs->stop = stop_on_solib_events;
12573 bs->print = stop_on_solib_events;
12574 }
12575 else
12576 bs->stop = 0;
12577 }
12578
12579 static enum print_stop_action
12580 internal_bkpt_print_it (bpstat bs)
12581 {
12582 struct breakpoint *b;
12583
12584 b = bs->breakpoint_at;
12585
12586 switch (b->type)
12587 {
12588 case bp_shlib_event:
12589 /* Did we stop because the user set the stop_on_solib_events
12590 variable? (If so, we report this as a generic, "Stopped due
12591 to shlib event" message.) */
12592 print_solib_event (0);
12593 break;
12594
12595 case bp_thread_event:
12596 /* Not sure how we will get here.
12597 GDB should not stop for these breakpoints. */
12598 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12599 break;
12600
12601 case bp_overlay_event:
12602 /* By analogy with the thread event, GDB should not stop for these. */
12603 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12604 break;
12605
12606 case bp_longjmp_master:
12607 /* These should never be enabled. */
12608 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12609 break;
12610
12611 case bp_std_terminate_master:
12612 /* These should never be enabled. */
12613 printf_filtered (_("std::terminate Master Breakpoint: "
12614 "gdb should not stop!\n"));
12615 break;
12616
12617 case bp_exception_master:
12618 /* These should never be enabled. */
12619 printf_filtered (_("Exception Master Breakpoint: "
12620 "gdb should not stop!\n"));
12621 break;
12622 }
12623
12624 return PRINT_NOTHING;
12625 }
12626
12627 static void
12628 internal_bkpt_print_mention (struct breakpoint *b)
12629 {
12630 /* Nothing to mention. These breakpoints are internal. */
12631 }
12632
12633 /* Virtual table for momentary breakpoints */
12634
12635 static void
12636 momentary_bkpt_re_set (struct breakpoint *b)
12637 {
12638 /* Keep temporary breakpoints, which can be encountered when we step
12639 over a dlopen call and solib_add is resetting the breakpoints.
12640 Otherwise these should have been blown away via the cleanup chain
12641 or by breakpoint_init_inferior when we rerun the executable. */
12642 }
12643
12644 static void
12645 momentary_bkpt_check_status (bpstat bs)
12646 {
12647 /* Nothing. The point of these breakpoints is causing a stop. */
12648 }
12649
12650 static enum print_stop_action
12651 momentary_bkpt_print_it (bpstat bs)
12652 {
12653 return PRINT_UNKNOWN;
12654 }
12655
12656 static void
12657 momentary_bkpt_print_mention (struct breakpoint *b)
12658 {
12659 /* Nothing to mention. These breakpoints are internal. */
12660 }
12661
12662 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12663
12664 It gets cleared already on the removal of the first one of such placed
12665 breakpoints. This is OK as they get all removed altogether. */
12666
12667 longjmp_breakpoint::~longjmp_breakpoint ()
12668 {
12669 thread_info *tp = find_thread_global_id (this->thread);
12670
12671 if (tp != NULL)
12672 tp->initiating_frame = null_frame_id;
12673 }
12674
12675 /* Specific methods for probe breakpoints. */
12676
12677 static int
12678 bkpt_probe_insert_location (struct bp_location *bl)
12679 {
12680 int v = bkpt_insert_location (bl);
12681
12682 if (v == 0)
12683 {
12684 /* The insertion was successful, now let's set the probe's semaphore
12685 if needed. */
12686 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12687 }
12688
12689 return v;
12690 }
12691
12692 static int
12693 bkpt_probe_remove_location (struct bp_location *bl,
12694 enum remove_bp_reason reason)
12695 {
12696 /* Let's clear the semaphore before removing the location. */
12697 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12698
12699 return bkpt_remove_location (bl, reason);
12700 }
12701
12702 static void
12703 bkpt_probe_create_sals_from_location (const struct event_location *location,
12704 struct linespec_result *canonical,
12705 enum bptype type_wanted)
12706 {
12707 struct linespec_sals lsal;
12708
12709 lsal.sals = parse_probes (location, NULL, canonical);
12710 lsal.canonical
12711 = xstrdup (event_location_to_string (canonical->location.get ()));
12712 canonical->lsals.push_back (std::move (lsal));
12713 }
12714
12715 static std::vector<symtab_and_line>
12716 bkpt_probe_decode_location (struct breakpoint *b,
12717 const struct event_location *location,
12718 struct program_space *search_pspace)
12719 {
12720 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12721 if (sals.empty ())
12722 error (_("probe not found"));
12723 return sals;
12724 }
12725
12726 /* The breakpoint_ops structure to be used in tracepoints. */
12727
12728 static void
12729 tracepoint_re_set (struct breakpoint *b)
12730 {
12731 breakpoint_re_set_default (b);
12732 }
12733
12734 static int
12735 tracepoint_breakpoint_hit (const struct bp_location *bl,
12736 const address_space *aspace, CORE_ADDR bp_addr,
12737 const struct target_waitstatus *ws)
12738 {
12739 /* By definition, the inferior does not report stops at
12740 tracepoints. */
12741 return 0;
12742 }
12743
12744 static void
12745 tracepoint_print_one_detail (const struct breakpoint *self,
12746 struct ui_out *uiout)
12747 {
12748 struct tracepoint *tp = (struct tracepoint *) self;
12749 if (!tp->static_trace_marker_id.empty ())
12750 {
12751 gdb_assert (self->type == bp_static_tracepoint);
12752
12753 uiout->message ("\tmarker id is %pF\n",
12754 string_field ("static-tracepoint-marker-string-id",
12755 tp->static_trace_marker_id.c_str ()));
12756 }
12757 }
12758
12759 static void
12760 tracepoint_print_mention (struct breakpoint *b)
12761 {
12762 if (current_uiout->is_mi_like_p ())
12763 return;
12764
12765 switch (b->type)
12766 {
12767 case bp_tracepoint:
12768 printf_filtered (_("Tracepoint"));
12769 printf_filtered (_(" %d"), b->number);
12770 break;
12771 case bp_fast_tracepoint:
12772 printf_filtered (_("Fast tracepoint"));
12773 printf_filtered (_(" %d"), b->number);
12774 break;
12775 case bp_static_tracepoint:
12776 printf_filtered (_("Static tracepoint"));
12777 printf_filtered (_(" %d"), b->number);
12778 break;
12779 default:
12780 internal_error (__FILE__, __LINE__,
12781 _("unhandled tracepoint type %d"), (int) b->type);
12782 }
12783
12784 say_where (b);
12785 }
12786
12787 static void
12788 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12789 {
12790 struct tracepoint *tp = (struct tracepoint *) self;
12791
12792 if (self->type == bp_fast_tracepoint)
12793 fprintf_unfiltered (fp, "ftrace");
12794 else if (self->type == bp_static_tracepoint)
12795 fprintf_unfiltered (fp, "strace");
12796 else if (self->type == bp_tracepoint)
12797 fprintf_unfiltered (fp, "trace");
12798 else
12799 internal_error (__FILE__, __LINE__,
12800 _("unhandled tracepoint type %d"), (int) self->type);
12801
12802 fprintf_unfiltered (fp, " %s",
12803 event_location_to_string (self->location.get ()));
12804 print_recreate_thread (self, fp);
12805
12806 if (tp->pass_count)
12807 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12808 }
12809
12810 static void
12811 tracepoint_create_sals_from_location (const struct event_location *location,
12812 struct linespec_result *canonical,
12813 enum bptype type_wanted)
12814 {
12815 create_sals_from_location_default (location, canonical, type_wanted);
12816 }
12817
12818 static void
12819 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12820 struct linespec_result *canonical,
12821 gdb::unique_xmalloc_ptr<char> cond_string,
12822 gdb::unique_xmalloc_ptr<char> extra_string,
12823 enum bptype type_wanted,
12824 enum bpdisp disposition,
12825 int thread,
12826 int task, int ignore_count,
12827 const struct breakpoint_ops *ops,
12828 int from_tty, int enabled,
12829 int internal, unsigned flags)
12830 {
12831 create_breakpoints_sal_default (gdbarch, canonical,
12832 std::move (cond_string),
12833 std::move (extra_string),
12834 type_wanted,
12835 disposition, thread, task,
12836 ignore_count, ops, from_tty,
12837 enabled, internal, flags);
12838 }
12839
12840 static std::vector<symtab_and_line>
12841 tracepoint_decode_location (struct breakpoint *b,
12842 const struct event_location *location,
12843 struct program_space *search_pspace)
12844 {
12845 return decode_location_default (b, location, search_pspace);
12846 }
12847
12848 struct breakpoint_ops tracepoint_breakpoint_ops;
12849
12850 /* Virtual table for tracepoints on static probes. */
12851
12852 static void
12853 tracepoint_probe_create_sals_from_location
12854 (const struct event_location *location,
12855 struct linespec_result *canonical,
12856 enum bptype type_wanted)
12857 {
12858 /* We use the same method for breakpoint on probes. */
12859 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12860 }
12861
12862 static std::vector<symtab_and_line>
12863 tracepoint_probe_decode_location (struct breakpoint *b,
12864 const struct event_location *location,
12865 struct program_space *search_pspace)
12866 {
12867 /* We use the same method for breakpoint on probes. */
12868 return bkpt_probe_decode_location (b, location, search_pspace);
12869 }
12870
12871 /* Dprintf breakpoint_ops methods. */
12872
12873 static void
12874 dprintf_re_set (struct breakpoint *b)
12875 {
12876 breakpoint_re_set_default (b);
12877
12878 /* extra_string should never be non-NULL for dprintf. */
12879 gdb_assert (b->extra_string != NULL);
12880
12881 /* 1 - connect to target 1, that can run breakpoint commands.
12882 2 - create a dprintf, which resolves fine.
12883 3 - disconnect from target 1
12884 4 - connect to target 2, that can NOT run breakpoint commands.
12885
12886 After steps #3/#4, you'll want the dprintf command list to
12887 be updated, because target 1 and 2 may well return different
12888 answers for target_can_run_breakpoint_commands().
12889 Given absence of finer grained resetting, we get to do
12890 it all the time. */
12891 if (b->extra_string != NULL)
12892 update_dprintf_command_list (b);
12893 }
12894
12895 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12896
12897 static void
12898 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12899 {
12900 fprintf_unfiltered (fp, "dprintf %s,%s",
12901 event_location_to_string (tp->location.get ()),
12902 tp->extra_string);
12903 print_recreate_thread (tp, fp);
12904 }
12905
12906 /* Implement the "after_condition_true" breakpoint_ops method for
12907 dprintf.
12908
12909 dprintf's are implemented with regular commands in their command
12910 list, but we run the commands here instead of before presenting the
12911 stop to the user, as dprintf's don't actually cause a stop. This
12912 also makes it so that the commands of multiple dprintfs at the same
12913 address are all handled. */
12914
12915 static void
12916 dprintf_after_condition_true (struct bpstats *bs)
12917 {
12918 struct bpstats tmp_bs;
12919 struct bpstats *tmp_bs_p = &tmp_bs;
12920
12921 /* dprintf's never cause a stop. This wasn't set in the
12922 check_status hook instead because that would make the dprintf's
12923 condition not be evaluated. */
12924 bs->stop = 0;
12925
12926 /* Run the command list here. Take ownership of it instead of
12927 copying. We never want these commands to run later in
12928 bpstat_do_actions, if a breakpoint that causes a stop happens to
12929 be set at same address as this dprintf, or even if running the
12930 commands here throws. */
12931 tmp_bs.commands = bs->commands;
12932 bs->commands = NULL;
12933
12934 bpstat_do_actions_1 (&tmp_bs_p);
12935
12936 /* 'tmp_bs.commands' will usually be NULL by now, but
12937 bpstat_do_actions_1 may return early without processing the whole
12938 list. */
12939 }
12940
12941 /* The breakpoint_ops structure to be used on static tracepoints with
12942 markers (`-m'). */
12943
12944 static void
12945 strace_marker_create_sals_from_location (const struct event_location *location,
12946 struct linespec_result *canonical,
12947 enum bptype type_wanted)
12948 {
12949 struct linespec_sals lsal;
12950 const char *arg_start, *arg;
12951
12952 arg = arg_start = get_linespec_location (location)->spec_string;
12953 lsal.sals = decode_static_tracepoint_spec (&arg);
12954
12955 std::string str (arg_start, arg - arg_start);
12956 const char *ptr = str.c_str ();
12957 canonical->location
12958 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12959
12960 lsal.canonical
12961 = xstrdup (event_location_to_string (canonical->location.get ()));
12962 canonical->lsals.push_back (std::move (lsal));
12963 }
12964
12965 static void
12966 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12967 struct linespec_result *canonical,
12968 gdb::unique_xmalloc_ptr<char> cond_string,
12969 gdb::unique_xmalloc_ptr<char> extra_string,
12970 enum bptype type_wanted,
12971 enum bpdisp disposition,
12972 int thread,
12973 int task, int ignore_count,
12974 const struct breakpoint_ops *ops,
12975 int from_tty, int enabled,
12976 int internal, unsigned flags)
12977 {
12978 const linespec_sals &lsal = canonical->lsals[0];
12979
12980 /* If the user is creating a static tracepoint by marker id
12981 (strace -m MARKER_ID), then store the sals index, so that
12982 breakpoint_re_set can try to match up which of the newly
12983 found markers corresponds to this one, and, don't try to
12984 expand multiple locations for each sal, given than SALS
12985 already should contain all sals for MARKER_ID. */
12986
12987 for (size_t i = 0; i < lsal.sals.size (); i++)
12988 {
12989 event_location_up location
12990 = copy_event_location (canonical->location.get ());
12991
12992 std::unique_ptr<tracepoint> tp (new tracepoint ());
12993 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
12994 std::move (location), NULL,
12995 std::move (cond_string),
12996 std::move (extra_string),
12997 type_wanted, disposition,
12998 thread, task, ignore_count, ops,
12999 from_tty, enabled, internal, flags,
13000 canonical->special_display);
13001 /* Given that its possible to have multiple markers with
13002 the same string id, if the user is creating a static
13003 tracepoint by marker id ("strace -m MARKER_ID"), then
13004 store the sals index, so that breakpoint_re_set can
13005 try to match up which of the newly found markers
13006 corresponds to this one */
13007 tp->static_trace_marker_id_idx = i;
13008
13009 install_breakpoint (internal, std::move (tp), 0);
13010 }
13011 }
13012
13013 static std::vector<symtab_and_line>
13014 strace_marker_decode_location (struct breakpoint *b,
13015 const struct event_location *location,
13016 struct program_space *search_pspace)
13017 {
13018 struct tracepoint *tp = (struct tracepoint *) b;
13019 const char *s = get_linespec_location (location)->spec_string;
13020
13021 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13022 if (sals.size () > tp->static_trace_marker_id_idx)
13023 {
13024 sals[0] = sals[tp->static_trace_marker_id_idx];
13025 sals.resize (1);
13026 return sals;
13027 }
13028 else
13029 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13030 }
13031
13032 static struct breakpoint_ops strace_marker_breakpoint_ops;
13033
13034 static int
13035 strace_marker_p (struct breakpoint *b)
13036 {
13037 return b->ops == &strace_marker_breakpoint_ops;
13038 }
13039
13040 /* Delete a breakpoint and clean up all traces of it in the data
13041 structures. */
13042
13043 void
13044 delete_breakpoint (struct breakpoint *bpt)
13045 {
13046 struct breakpoint *b;
13047
13048 gdb_assert (bpt != NULL);
13049
13050 /* Has this bp already been deleted? This can happen because
13051 multiple lists can hold pointers to bp's. bpstat lists are
13052 especial culprits.
13053
13054 One example of this happening is a watchpoint's scope bp. When
13055 the scope bp triggers, we notice that the watchpoint is out of
13056 scope, and delete it. We also delete its scope bp. But the
13057 scope bp is marked "auto-deleting", and is already on a bpstat.
13058 That bpstat is then checked for auto-deleting bp's, which are
13059 deleted.
13060
13061 A real solution to this problem might involve reference counts in
13062 bp's, and/or giving them pointers back to their referencing
13063 bpstat's, and teaching delete_breakpoint to only free a bp's
13064 storage when no more references were extent. A cheaper bandaid
13065 was chosen. */
13066 if (bpt->type == bp_none)
13067 return;
13068
13069 /* At least avoid this stale reference until the reference counting
13070 of breakpoints gets resolved. */
13071 if (bpt->related_breakpoint != bpt)
13072 {
13073 struct breakpoint *related;
13074 struct watchpoint *w;
13075
13076 if (bpt->type == bp_watchpoint_scope)
13077 w = (struct watchpoint *) bpt->related_breakpoint;
13078 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13079 w = (struct watchpoint *) bpt;
13080 else
13081 w = NULL;
13082 if (w != NULL)
13083 watchpoint_del_at_next_stop (w);
13084
13085 /* Unlink bpt from the bpt->related_breakpoint ring. */
13086 for (related = bpt; related->related_breakpoint != bpt;
13087 related = related->related_breakpoint);
13088 related->related_breakpoint = bpt->related_breakpoint;
13089 bpt->related_breakpoint = bpt;
13090 }
13091
13092 /* watch_command_1 creates a watchpoint but only sets its number if
13093 update_watchpoint succeeds in creating its bp_locations. If there's
13094 a problem in that process, we'll be asked to delete the half-created
13095 watchpoint. In that case, don't announce the deletion. */
13096 if (bpt->number)
13097 gdb::observers::breakpoint_deleted.notify (bpt);
13098
13099 if (breakpoint_chain == bpt)
13100 breakpoint_chain = bpt->next;
13101
13102 ALL_BREAKPOINTS (b)
13103 if (b->next == bpt)
13104 {
13105 b->next = bpt->next;
13106 break;
13107 }
13108
13109 /* Be sure no bpstat's are pointing at the breakpoint after it's
13110 been freed. */
13111 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13112 in all threads for now. Note that we cannot just remove bpstats
13113 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13114 commands are associated with the bpstat; if we remove it here,
13115 then the later call to bpstat_do_actions (&stop_bpstat); in
13116 event-top.c won't do anything, and temporary breakpoints with
13117 commands won't work. */
13118
13119 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13120
13121 /* Now that breakpoint is removed from breakpoint list, update the
13122 global location list. This will remove locations that used to
13123 belong to this breakpoint. Do this before freeing the breakpoint
13124 itself, since remove_breakpoint looks at location's owner. It
13125 might be better design to have location completely
13126 self-contained, but it's not the case now. */
13127 update_global_location_list (UGLL_DONT_INSERT);
13128
13129 /* On the chance that someone will soon try again to delete this
13130 same bp, we mark it as deleted before freeing its storage. */
13131 bpt->type = bp_none;
13132 delete bpt;
13133 }
13134
13135 /* Iterator function to call a user-provided callback function once
13136 for each of B and its related breakpoints. */
13137
13138 static void
13139 iterate_over_related_breakpoints (struct breakpoint *b,
13140 gdb::function_view<void (breakpoint *)> function)
13141 {
13142 struct breakpoint *related;
13143
13144 related = b;
13145 do
13146 {
13147 struct breakpoint *next;
13148
13149 /* FUNCTION may delete RELATED. */
13150 next = related->related_breakpoint;
13151
13152 if (next == related)
13153 {
13154 /* RELATED is the last ring entry. */
13155 function (related);
13156
13157 /* FUNCTION may have deleted it, so we'd never reach back to
13158 B. There's nothing left to do anyway, so just break
13159 out. */
13160 break;
13161 }
13162 else
13163 function (related);
13164
13165 related = next;
13166 }
13167 while (related != b);
13168 }
13169
13170 static void
13171 delete_command (const char *arg, int from_tty)
13172 {
13173 struct breakpoint *b, *b_tmp;
13174
13175 dont_repeat ();
13176
13177 if (arg == 0)
13178 {
13179 int breaks_to_delete = 0;
13180
13181 /* Delete all breakpoints if no argument. Do not delete
13182 internal breakpoints, these have to be deleted with an
13183 explicit breakpoint number argument. */
13184 ALL_BREAKPOINTS (b)
13185 if (user_breakpoint_p (b))
13186 {
13187 breaks_to_delete = 1;
13188 break;
13189 }
13190
13191 /* Ask user only if there are some breakpoints to delete. */
13192 if (!from_tty
13193 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13194 {
13195 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13196 if (user_breakpoint_p (b))
13197 delete_breakpoint (b);
13198 }
13199 }
13200 else
13201 map_breakpoint_numbers
13202 (arg, [&] (breakpoint *br)
13203 {
13204 iterate_over_related_breakpoints (br, delete_breakpoint);
13205 });
13206 }
13207
13208 /* Return true if all locations of B bound to PSPACE are pending. If
13209 PSPACE is NULL, all locations of all program spaces are
13210 considered. */
13211
13212 static int
13213 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13214 {
13215 struct bp_location *loc;
13216
13217 for (loc = b->loc; loc != NULL; loc = loc->next)
13218 if ((pspace == NULL
13219 || loc->pspace == pspace)
13220 && !loc->shlib_disabled
13221 && !loc->pspace->executing_startup)
13222 return 0;
13223 return 1;
13224 }
13225
13226 /* Subroutine of update_breakpoint_locations to simplify it.
13227 Return non-zero if multiple fns in list LOC have the same name.
13228 Null names are ignored. */
13229
13230 static int
13231 ambiguous_names_p (struct bp_location *loc)
13232 {
13233 struct bp_location *l;
13234 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13235 xcalloc, xfree);
13236
13237 for (l = loc; l != NULL; l = l->next)
13238 {
13239 const char **slot;
13240 const char *name = l->function_name;
13241
13242 /* Allow for some names to be NULL, ignore them. */
13243 if (name == NULL)
13244 continue;
13245
13246 slot = (const char **) htab_find_slot (htab, (const void *) name,
13247 INSERT);
13248 /* NOTE: We can assume slot != NULL here because xcalloc never
13249 returns NULL. */
13250 if (*slot != NULL)
13251 {
13252 htab_delete (htab);
13253 return 1;
13254 }
13255 *slot = name;
13256 }
13257
13258 htab_delete (htab);
13259 return 0;
13260 }
13261
13262 /* When symbols change, it probably means the sources changed as well,
13263 and it might mean the static tracepoint markers are no longer at
13264 the same address or line numbers they used to be at last we
13265 checked. Losing your static tracepoints whenever you rebuild is
13266 undesirable. This function tries to resync/rematch gdb static
13267 tracepoints with the markers on the target, for static tracepoints
13268 that have not been set by marker id. Static tracepoint that have
13269 been set by marker id are reset by marker id in breakpoint_re_set.
13270 The heuristic is:
13271
13272 1) For a tracepoint set at a specific address, look for a marker at
13273 the old PC. If one is found there, assume to be the same marker.
13274 If the name / string id of the marker found is different from the
13275 previous known name, assume that means the user renamed the marker
13276 in the sources, and output a warning.
13277
13278 2) For a tracepoint set at a given line number, look for a marker
13279 at the new address of the old line number. If one is found there,
13280 assume to be the same marker. If the name / string id of the
13281 marker found is different from the previous known name, assume that
13282 means the user renamed the marker in the sources, and output a
13283 warning.
13284
13285 3) If a marker is no longer found at the same address or line, it
13286 may mean the marker no longer exists. But it may also just mean
13287 the code changed a bit. Maybe the user added a few lines of code
13288 that made the marker move up or down (in line number terms). Ask
13289 the target for info about the marker with the string id as we knew
13290 it. If found, update line number and address in the matching
13291 static tracepoint. This will get confused if there's more than one
13292 marker with the same ID (possible in UST, although unadvised
13293 precisely because it confuses tools). */
13294
13295 static struct symtab_and_line
13296 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13297 {
13298 struct tracepoint *tp = (struct tracepoint *) b;
13299 struct static_tracepoint_marker marker;
13300 CORE_ADDR pc;
13301
13302 pc = sal.pc;
13303 if (sal.line)
13304 find_line_pc (sal.symtab, sal.line, &pc);
13305
13306 if (target_static_tracepoint_marker_at (pc, &marker))
13307 {
13308 if (tp->static_trace_marker_id != marker.str_id)
13309 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13310 b->number, tp->static_trace_marker_id.c_str (),
13311 marker.str_id.c_str ());
13312
13313 tp->static_trace_marker_id = std::move (marker.str_id);
13314
13315 return sal;
13316 }
13317
13318 /* Old marker wasn't found on target at lineno. Try looking it up
13319 by string ID. */
13320 if (!sal.explicit_pc
13321 && sal.line != 0
13322 && sal.symtab != NULL
13323 && !tp->static_trace_marker_id.empty ())
13324 {
13325 std::vector<static_tracepoint_marker> markers
13326 = target_static_tracepoint_markers_by_strid
13327 (tp->static_trace_marker_id.c_str ());
13328
13329 if (!markers.empty ())
13330 {
13331 struct symbol *sym;
13332 struct static_tracepoint_marker *tpmarker;
13333 struct ui_out *uiout = current_uiout;
13334 struct explicit_location explicit_loc;
13335
13336 tpmarker = &markers[0];
13337
13338 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13339
13340 warning (_("marker for static tracepoint %d (%s) not "
13341 "found at previous line number"),
13342 b->number, tp->static_trace_marker_id.c_str ());
13343
13344 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13345 sym = find_pc_sect_function (tpmarker->address, NULL);
13346 uiout->text ("Now in ");
13347 if (sym)
13348 {
13349 uiout->field_string ("func", sym->print_name (),
13350 function_name_style.style ());
13351 uiout->text (" at ");
13352 }
13353 uiout->field_string ("file",
13354 symtab_to_filename_for_display (sal2.symtab),
13355 file_name_style.style ());
13356 uiout->text (":");
13357
13358 if (uiout->is_mi_like_p ())
13359 {
13360 const char *fullname = symtab_to_fullname (sal2.symtab);
13361
13362 uiout->field_string ("fullname", fullname);
13363 }
13364
13365 uiout->field_signed ("line", sal2.line);
13366 uiout->text ("\n");
13367
13368 b->loc->line_number = sal2.line;
13369 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13370
13371 b->location.reset (NULL);
13372 initialize_explicit_location (&explicit_loc);
13373 explicit_loc.source_filename
13374 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13375 explicit_loc.line_offset.offset = b->loc->line_number;
13376 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13377 b->location = new_explicit_location (&explicit_loc);
13378
13379 /* Might be nice to check if function changed, and warn if
13380 so. */
13381 }
13382 }
13383 return sal;
13384 }
13385
13386 /* Returns 1 iff locations A and B are sufficiently same that
13387 we don't need to report breakpoint as changed. */
13388
13389 static int
13390 locations_are_equal (struct bp_location *a, struct bp_location *b)
13391 {
13392 while (a && b)
13393 {
13394 if (a->address != b->address)
13395 return 0;
13396
13397 if (a->shlib_disabled != b->shlib_disabled)
13398 return 0;
13399
13400 if (a->enabled != b->enabled)
13401 return 0;
13402
13403 a = a->next;
13404 b = b->next;
13405 }
13406
13407 if ((a == NULL) != (b == NULL))
13408 return 0;
13409
13410 return 1;
13411 }
13412
13413 /* Split all locations of B that are bound to PSPACE out of B's
13414 location list to a separate list and return that list's head. If
13415 PSPACE is NULL, hoist out all locations of B. */
13416
13417 static struct bp_location *
13418 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13419 {
13420 struct bp_location head;
13421 struct bp_location *i = b->loc;
13422 struct bp_location **i_link = &b->loc;
13423 struct bp_location *hoisted = &head;
13424
13425 if (pspace == NULL)
13426 {
13427 i = b->loc;
13428 b->loc = NULL;
13429 return i;
13430 }
13431
13432 head.next = NULL;
13433
13434 while (i != NULL)
13435 {
13436 if (i->pspace == pspace)
13437 {
13438 *i_link = i->next;
13439 i->next = NULL;
13440 hoisted->next = i;
13441 hoisted = i;
13442 }
13443 else
13444 i_link = &i->next;
13445 i = *i_link;
13446 }
13447
13448 return head.next;
13449 }
13450
13451 /* Create new breakpoint locations for B (a hardware or software
13452 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13453 zero, then B is a ranged breakpoint. Only recreates locations for
13454 FILTER_PSPACE. Locations of other program spaces are left
13455 untouched. */
13456
13457 void
13458 update_breakpoint_locations (struct breakpoint *b,
13459 struct program_space *filter_pspace,
13460 gdb::array_view<const symtab_and_line> sals,
13461 gdb::array_view<const symtab_and_line> sals_end)
13462 {
13463 struct bp_location *existing_locations;
13464
13465 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13466 {
13467 /* Ranged breakpoints have only one start location and one end
13468 location. */
13469 b->enable_state = bp_disabled;
13470 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13471 "multiple locations found\n"),
13472 b->number);
13473 return;
13474 }
13475
13476 /* If there's no new locations, and all existing locations are
13477 pending, don't do anything. This optimizes the common case where
13478 all locations are in the same shared library, that was unloaded.
13479 We'd like to retain the location, so that when the library is
13480 loaded again, we don't loose the enabled/disabled status of the
13481 individual locations. */
13482 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13483 return;
13484
13485 existing_locations = hoist_existing_locations (b, filter_pspace);
13486
13487 for (const auto &sal : sals)
13488 {
13489 struct bp_location *new_loc;
13490
13491 switch_to_program_space_and_thread (sal.pspace);
13492
13493 new_loc = add_location_to_breakpoint (b, &sal);
13494
13495 /* Reparse conditions, they might contain references to the
13496 old symtab. */
13497 if (b->cond_string != NULL)
13498 {
13499 const char *s;
13500
13501 s = b->cond_string;
13502 try
13503 {
13504 new_loc->cond = parse_exp_1 (&s, sal.pc,
13505 block_for_pc (sal.pc),
13506 0);
13507 }
13508 catch (const gdb_exception_error &e)
13509 {
13510 warning (_("failed to reevaluate condition "
13511 "for breakpoint %d: %s"),
13512 b->number, e.what ());
13513 new_loc->enabled = 0;
13514 }
13515 }
13516
13517 if (!sals_end.empty ())
13518 {
13519 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13520
13521 new_loc->length = end - sals[0].pc + 1;
13522 }
13523 }
13524
13525 /* If possible, carry over 'disable' status from existing
13526 breakpoints. */
13527 {
13528 struct bp_location *e = existing_locations;
13529 /* If there are multiple breakpoints with the same function name,
13530 e.g. for inline functions, comparing function names won't work.
13531 Instead compare pc addresses; this is just a heuristic as things
13532 may have moved, but in practice it gives the correct answer
13533 often enough until a better solution is found. */
13534 int have_ambiguous_names = ambiguous_names_p (b->loc);
13535
13536 for (; e; e = e->next)
13537 {
13538 if (!e->enabled && e->function_name)
13539 {
13540 struct bp_location *l = b->loc;
13541 if (have_ambiguous_names)
13542 {
13543 for (; l; l = l->next)
13544 if (breakpoint_locations_match (e, l))
13545 {
13546 l->enabled = 0;
13547 break;
13548 }
13549 }
13550 else
13551 {
13552 for (; l; l = l->next)
13553 if (l->function_name
13554 && strcmp (e->function_name, l->function_name) == 0)
13555 {
13556 l->enabled = 0;
13557 break;
13558 }
13559 }
13560 }
13561 }
13562 }
13563
13564 if (!locations_are_equal (existing_locations, b->loc))
13565 gdb::observers::breakpoint_modified.notify (b);
13566 }
13567
13568 /* Find the SaL locations corresponding to the given LOCATION.
13569 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13570
13571 static std::vector<symtab_and_line>
13572 location_to_sals (struct breakpoint *b, struct event_location *location,
13573 struct program_space *search_pspace, int *found)
13574 {
13575 struct gdb_exception exception;
13576
13577 gdb_assert (b->ops != NULL);
13578
13579 std::vector<symtab_and_line> sals;
13580
13581 try
13582 {
13583 sals = b->ops->decode_location (b, location, search_pspace);
13584 }
13585 catch (gdb_exception_error &e)
13586 {
13587 int not_found_and_ok = 0;
13588
13589 /* For pending breakpoints, it's expected that parsing will
13590 fail until the right shared library is loaded. User has
13591 already told to create pending breakpoints and don't need
13592 extra messages. If breakpoint is in bp_shlib_disabled
13593 state, then user already saw the message about that
13594 breakpoint being disabled, and don't want to see more
13595 errors. */
13596 if (e.error == NOT_FOUND_ERROR
13597 && (b->condition_not_parsed
13598 || (b->loc != NULL
13599 && search_pspace != NULL
13600 && b->loc->pspace != search_pspace)
13601 || (b->loc && b->loc->shlib_disabled)
13602 || (b->loc && b->loc->pspace->executing_startup)
13603 || b->enable_state == bp_disabled))
13604 not_found_and_ok = 1;
13605
13606 if (!not_found_and_ok)
13607 {
13608 /* We surely don't want to warn about the same breakpoint
13609 10 times. One solution, implemented here, is disable
13610 the breakpoint on error. Another solution would be to
13611 have separate 'warning emitted' flag. Since this
13612 happens only when a binary has changed, I don't know
13613 which approach is better. */
13614 b->enable_state = bp_disabled;
13615 throw;
13616 }
13617
13618 exception = std::move (e);
13619 }
13620
13621 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13622 {
13623 for (auto &sal : sals)
13624 resolve_sal_pc (&sal);
13625 if (b->condition_not_parsed && b->extra_string != NULL)
13626 {
13627 char *cond_string, *extra_string;
13628 int thread, task;
13629
13630 find_condition_and_thread (b->extra_string, sals[0].pc,
13631 &cond_string, &thread, &task,
13632 &extra_string);
13633 gdb_assert (b->cond_string == NULL);
13634 if (cond_string)
13635 b->cond_string = cond_string;
13636 b->thread = thread;
13637 b->task = task;
13638 if (extra_string)
13639 {
13640 xfree (b->extra_string);
13641 b->extra_string = extra_string;
13642 }
13643 b->condition_not_parsed = 0;
13644 }
13645
13646 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13647 sals[0] = update_static_tracepoint (b, sals[0]);
13648
13649 *found = 1;
13650 }
13651 else
13652 *found = 0;
13653
13654 return sals;
13655 }
13656
13657 /* The default re_set method, for typical hardware or software
13658 breakpoints. Reevaluate the breakpoint and recreate its
13659 locations. */
13660
13661 static void
13662 breakpoint_re_set_default (struct breakpoint *b)
13663 {
13664 struct program_space *filter_pspace = current_program_space;
13665 std::vector<symtab_and_line> expanded, expanded_end;
13666
13667 int found;
13668 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13669 filter_pspace, &found);
13670 if (found)
13671 expanded = std::move (sals);
13672
13673 if (b->location_range_end != NULL)
13674 {
13675 std::vector<symtab_and_line> sals_end
13676 = location_to_sals (b, b->location_range_end.get (),
13677 filter_pspace, &found);
13678 if (found)
13679 expanded_end = std::move (sals_end);
13680 }
13681
13682 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13683 }
13684
13685 /* Default method for creating SALs from an address string. It basically
13686 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13687
13688 static void
13689 create_sals_from_location_default (const struct event_location *location,
13690 struct linespec_result *canonical,
13691 enum bptype type_wanted)
13692 {
13693 parse_breakpoint_sals (location, canonical);
13694 }
13695
13696 /* Call create_breakpoints_sal for the given arguments. This is the default
13697 function for the `create_breakpoints_sal' method of
13698 breakpoint_ops. */
13699
13700 static void
13701 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13702 struct linespec_result *canonical,
13703 gdb::unique_xmalloc_ptr<char> cond_string,
13704 gdb::unique_xmalloc_ptr<char> extra_string,
13705 enum bptype type_wanted,
13706 enum bpdisp disposition,
13707 int thread,
13708 int task, int ignore_count,
13709 const struct breakpoint_ops *ops,
13710 int from_tty, int enabled,
13711 int internal, unsigned flags)
13712 {
13713 create_breakpoints_sal (gdbarch, canonical,
13714 std::move (cond_string),
13715 std::move (extra_string),
13716 type_wanted, disposition,
13717 thread, task, ignore_count, ops, from_tty,
13718 enabled, internal, flags);
13719 }
13720
13721 /* Decode the line represented by S by calling decode_line_full. This is the
13722 default function for the `decode_location' method of breakpoint_ops. */
13723
13724 static std::vector<symtab_and_line>
13725 decode_location_default (struct breakpoint *b,
13726 const struct event_location *location,
13727 struct program_space *search_pspace)
13728 {
13729 struct linespec_result canonical;
13730
13731 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13732 NULL, 0, &canonical, multiple_symbols_all,
13733 b->filter.get ());
13734
13735 /* We should get 0 or 1 resulting SALs. */
13736 gdb_assert (canonical.lsals.size () < 2);
13737
13738 if (!canonical.lsals.empty ())
13739 {
13740 const linespec_sals &lsal = canonical.lsals[0];
13741 return std::move (lsal.sals);
13742 }
13743 return {};
13744 }
13745
13746 /* Reset a breakpoint. */
13747
13748 static void
13749 breakpoint_re_set_one (breakpoint *b)
13750 {
13751 input_radix = b->input_radix;
13752 set_language (b->language);
13753
13754 b->ops->re_set (b);
13755 }
13756
13757 /* Re-set breakpoint locations for the current program space.
13758 Locations bound to other program spaces are left untouched. */
13759
13760 void
13761 breakpoint_re_set (void)
13762 {
13763 struct breakpoint *b, *b_tmp;
13764
13765 {
13766 scoped_restore_current_language save_language;
13767 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13768 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13769
13770 /* breakpoint_re_set_one sets the current_language to the language
13771 of the breakpoint it is resetting (see prepare_re_set_context)
13772 before re-evaluating the breakpoint's location. This change can
13773 unfortunately get undone by accident if the language_mode is set
13774 to auto, and we either switch frames, or more likely in this context,
13775 we select the current frame.
13776
13777 We prevent this by temporarily turning the language_mode to
13778 language_mode_manual. We restore it once all breakpoints
13779 have been reset. */
13780 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13781 language_mode = language_mode_manual;
13782
13783 /* Note: we must not try to insert locations until after all
13784 breakpoints have been re-set. Otherwise, e.g., when re-setting
13785 breakpoint 1, we'd insert the locations of breakpoint 2, which
13786 hadn't been re-set yet, and thus may have stale locations. */
13787
13788 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13789 {
13790 try
13791 {
13792 breakpoint_re_set_one (b);
13793 }
13794 catch (const gdb_exception &ex)
13795 {
13796 exception_fprintf (gdb_stderr, ex,
13797 "Error in re-setting breakpoint %d: ",
13798 b->number);
13799 }
13800 }
13801
13802 jit_breakpoint_re_set ();
13803 }
13804
13805 create_overlay_event_breakpoint ();
13806 create_longjmp_master_breakpoint ();
13807 create_std_terminate_master_breakpoint ();
13808 create_exception_master_breakpoint ();
13809
13810 /* Now we can insert. */
13811 update_global_location_list (UGLL_MAY_INSERT);
13812 }
13813 \f
13814 /* Reset the thread number of this breakpoint:
13815
13816 - If the breakpoint is for all threads, leave it as-is.
13817 - Else, reset it to the current thread for inferior_ptid. */
13818 void
13819 breakpoint_re_set_thread (struct breakpoint *b)
13820 {
13821 if (b->thread != -1)
13822 {
13823 b->thread = inferior_thread ()->global_num;
13824
13825 /* We're being called after following a fork. The new fork is
13826 selected as current, and unless this was a vfork will have a
13827 different program space from the original thread. Reset that
13828 as well. */
13829 b->loc->pspace = current_program_space;
13830 }
13831 }
13832
13833 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13834 If from_tty is nonzero, it prints a message to that effect,
13835 which ends with a period (no newline). */
13836
13837 void
13838 set_ignore_count (int bptnum, int count, int from_tty)
13839 {
13840 struct breakpoint *b;
13841
13842 if (count < 0)
13843 count = 0;
13844
13845 ALL_BREAKPOINTS (b)
13846 if (b->number == bptnum)
13847 {
13848 if (is_tracepoint (b))
13849 {
13850 if (from_tty && count != 0)
13851 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13852 bptnum);
13853 return;
13854 }
13855
13856 b->ignore_count = count;
13857 if (from_tty)
13858 {
13859 if (count == 0)
13860 printf_filtered (_("Will stop next time "
13861 "breakpoint %d is reached."),
13862 bptnum);
13863 else if (count == 1)
13864 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13865 bptnum);
13866 else
13867 printf_filtered (_("Will ignore next %d "
13868 "crossings of breakpoint %d."),
13869 count, bptnum);
13870 }
13871 gdb::observers::breakpoint_modified.notify (b);
13872 return;
13873 }
13874
13875 error (_("No breakpoint number %d."), bptnum);
13876 }
13877
13878 /* Command to set ignore-count of breakpoint N to COUNT. */
13879
13880 static void
13881 ignore_command (const char *args, int from_tty)
13882 {
13883 const char *p = args;
13884 int num;
13885
13886 if (p == 0)
13887 error_no_arg (_("a breakpoint number"));
13888
13889 num = get_number (&p);
13890 if (num == 0)
13891 error (_("bad breakpoint number: '%s'"), args);
13892 if (*p == 0)
13893 error (_("Second argument (specified ignore-count) is missing."));
13894
13895 set_ignore_count (num,
13896 longest_to_int (value_as_long (parse_and_eval (p))),
13897 from_tty);
13898 if (from_tty)
13899 printf_filtered ("\n");
13900 }
13901 \f
13902
13903 /* Call FUNCTION on each of the breakpoints with numbers in the range
13904 defined by BP_NUM_RANGE (an inclusive range). */
13905
13906 static void
13907 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13908 gdb::function_view<void (breakpoint *)> function)
13909 {
13910 if (bp_num_range.first == 0)
13911 {
13912 warning (_("bad breakpoint number at or near '%d'"),
13913 bp_num_range.first);
13914 }
13915 else
13916 {
13917 struct breakpoint *b, *tmp;
13918
13919 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13920 {
13921 bool match = false;
13922
13923 ALL_BREAKPOINTS_SAFE (b, tmp)
13924 if (b->number == i)
13925 {
13926 match = true;
13927 function (b);
13928 break;
13929 }
13930 if (!match)
13931 printf_unfiltered (_("No breakpoint number %d.\n"), i);
13932 }
13933 }
13934 }
13935
13936 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13937 ARGS. */
13938
13939 static void
13940 map_breakpoint_numbers (const char *args,
13941 gdb::function_view<void (breakpoint *)> function)
13942 {
13943 if (args == NULL || *args == '\0')
13944 error_no_arg (_("one or more breakpoint numbers"));
13945
13946 number_or_range_parser parser (args);
13947
13948 while (!parser.finished ())
13949 {
13950 int num = parser.get_number ();
13951 map_breakpoint_number_range (std::make_pair (num, num), function);
13952 }
13953 }
13954
13955 /* Return the breakpoint location structure corresponding to the
13956 BP_NUM and LOC_NUM values. */
13957
13958 static struct bp_location *
13959 find_location_by_number (int bp_num, int loc_num)
13960 {
13961 struct breakpoint *b;
13962
13963 ALL_BREAKPOINTS (b)
13964 if (b->number == bp_num)
13965 {
13966 break;
13967 }
13968
13969 if (!b || b->number != bp_num)
13970 error (_("Bad breakpoint number '%d'"), bp_num);
13971
13972 if (loc_num == 0)
13973 error (_("Bad breakpoint location number '%d'"), loc_num);
13974
13975 int n = 0;
13976 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
13977 if (++n == loc_num)
13978 return loc;
13979
13980 error (_("Bad breakpoint location number '%d'"), loc_num);
13981 }
13982
13983 /* Modes of operation for extract_bp_num. */
13984 enum class extract_bp_kind
13985 {
13986 /* Extracting a breakpoint number. */
13987 bp,
13988
13989 /* Extracting a location number. */
13990 loc,
13991 };
13992
13993 /* Extract a breakpoint or location number (as determined by KIND)
13994 from the string starting at START. TRAILER is a character which
13995 can be found after the number. If you don't want a trailer, use
13996 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13997 string. This always returns a positive integer. */
13998
13999 static int
14000 extract_bp_num (extract_bp_kind kind, const char *start,
14001 int trailer, const char **end_out = NULL)
14002 {
14003 const char *end = start;
14004 int num = get_number_trailer (&end, trailer);
14005 if (num < 0)
14006 error (kind == extract_bp_kind::bp
14007 ? _("Negative breakpoint number '%.*s'")
14008 : _("Negative breakpoint location number '%.*s'"),
14009 int (end - start), start);
14010 if (num == 0)
14011 error (kind == extract_bp_kind::bp
14012 ? _("Bad breakpoint number '%.*s'")
14013 : _("Bad breakpoint location number '%.*s'"),
14014 int (end - start), start);
14015
14016 if (end_out != NULL)
14017 *end_out = end;
14018 return num;
14019 }
14020
14021 /* Extract a breakpoint or location range (as determined by KIND) in
14022 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14023 representing the (inclusive) range. The returned pair's elements
14024 are always positive integers. */
14025
14026 static std::pair<int, int>
14027 extract_bp_or_bp_range (extract_bp_kind kind,
14028 const std::string &arg,
14029 std::string::size_type arg_offset)
14030 {
14031 std::pair<int, int> range;
14032 const char *bp_loc = &arg[arg_offset];
14033 std::string::size_type dash = arg.find ('-', arg_offset);
14034 if (dash != std::string::npos)
14035 {
14036 /* bp_loc is a range (x-z). */
14037 if (arg.length () == dash + 1)
14038 error (kind == extract_bp_kind::bp
14039 ? _("Bad breakpoint number at or near: '%s'")
14040 : _("Bad breakpoint location number at or near: '%s'"),
14041 bp_loc);
14042
14043 const char *end;
14044 const char *start_first = bp_loc;
14045 const char *start_second = &arg[dash + 1];
14046 range.first = extract_bp_num (kind, start_first, '-');
14047 range.second = extract_bp_num (kind, start_second, '\0', &end);
14048
14049 if (range.first > range.second)
14050 error (kind == extract_bp_kind::bp
14051 ? _("Inverted breakpoint range at '%.*s'")
14052 : _("Inverted breakpoint location range at '%.*s'"),
14053 int (end - start_first), start_first);
14054 }
14055 else
14056 {
14057 /* bp_loc is a single value. */
14058 range.first = extract_bp_num (kind, bp_loc, '\0');
14059 range.second = range.first;
14060 }
14061 return range;
14062 }
14063
14064 /* Extract the breakpoint/location range specified by ARG. Returns
14065 the breakpoint range in BP_NUM_RANGE, and the location range in
14066 BP_LOC_RANGE.
14067
14068 ARG may be in any of the following forms:
14069
14070 x where 'x' is a breakpoint number.
14071 x-y where 'x' and 'y' specify a breakpoint numbers range.
14072 x.y where 'x' is a breakpoint number and 'y' a location number.
14073 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14074 location number range.
14075 */
14076
14077 static void
14078 extract_bp_number_and_location (const std::string &arg,
14079 std::pair<int, int> &bp_num_range,
14080 std::pair<int, int> &bp_loc_range)
14081 {
14082 std::string::size_type dot = arg.find ('.');
14083
14084 if (dot != std::string::npos)
14085 {
14086 /* Handle 'x.y' and 'x.y-z' cases. */
14087
14088 if (arg.length () == dot + 1 || dot == 0)
14089 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14090
14091 bp_num_range.first
14092 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14093 bp_num_range.second = bp_num_range.first;
14094
14095 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14096 arg, dot + 1);
14097 }
14098 else
14099 {
14100 /* Handle x and x-y cases. */
14101
14102 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14103 bp_loc_range.first = 0;
14104 bp_loc_range.second = 0;
14105 }
14106 }
14107
14108 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14109 specifies whether to enable or disable. */
14110
14111 static void
14112 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14113 {
14114 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14115 if (loc != NULL)
14116 {
14117 if (loc->enabled != enable)
14118 {
14119 loc->enabled = enable;
14120 mark_breakpoint_location_modified (loc);
14121 }
14122 if (target_supports_enable_disable_tracepoint ()
14123 && current_trace_status ()->running && loc->owner
14124 && is_tracepoint (loc->owner))
14125 target_disable_tracepoint (loc);
14126 }
14127 update_global_location_list (UGLL_DONT_INSERT);
14128
14129 gdb::observers::breakpoint_modified.notify (loc->owner);
14130 }
14131
14132 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14133 number of the breakpoint, and BP_LOC_RANGE specifies the
14134 (inclusive) range of location numbers of that breakpoint to
14135 enable/disable. ENABLE specifies whether to enable or disable the
14136 location. */
14137
14138 static void
14139 enable_disable_breakpoint_location_range (int bp_num,
14140 std::pair<int, int> &bp_loc_range,
14141 bool enable)
14142 {
14143 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14144 enable_disable_bp_num_loc (bp_num, i, enable);
14145 }
14146
14147 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14148 If from_tty is nonzero, it prints a message to that effect,
14149 which ends with a period (no newline). */
14150
14151 void
14152 disable_breakpoint (struct breakpoint *bpt)
14153 {
14154 /* Never disable a watchpoint scope breakpoint; we want to
14155 hit them when we leave scope so we can delete both the
14156 watchpoint and its scope breakpoint at that time. */
14157 if (bpt->type == bp_watchpoint_scope)
14158 return;
14159
14160 bpt->enable_state = bp_disabled;
14161
14162 /* Mark breakpoint locations modified. */
14163 mark_breakpoint_modified (bpt);
14164
14165 if (target_supports_enable_disable_tracepoint ()
14166 && current_trace_status ()->running && is_tracepoint (bpt))
14167 {
14168 struct bp_location *location;
14169
14170 for (location = bpt->loc; location; location = location->next)
14171 target_disable_tracepoint (location);
14172 }
14173
14174 update_global_location_list (UGLL_DONT_INSERT);
14175
14176 gdb::observers::breakpoint_modified.notify (bpt);
14177 }
14178
14179 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14180 specified in ARGS. ARGS may be in any of the formats handled by
14181 extract_bp_number_and_location. ENABLE specifies whether to enable
14182 or disable the breakpoints/locations. */
14183
14184 static void
14185 enable_disable_command (const char *args, int from_tty, bool enable)
14186 {
14187 if (args == 0)
14188 {
14189 struct breakpoint *bpt;
14190
14191 ALL_BREAKPOINTS (bpt)
14192 if (user_breakpoint_p (bpt))
14193 {
14194 if (enable)
14195 enable_breakpoint (bpt);
14196 else
14197 disable_breakpoint (bpt);
14198 }
14199 }
14200 else
14201 {
14202 std::string num = extract_arg (&args);
14203
14204 while (!num.empty ())
14205 {
14206 std::pair<int, int> bp_num_range, bp_loc_range;
14207
14208 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14209
14210 if (bp_loc_range.first == bp_loc_range.second
14211 && bp_loc_range.first == 0)
14212 {
14213 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14214 map_breakpoint_number_range (bp_num_range,
14215 enable
14216 ? enable_breakpoint
14217 : disable_breakpoint);
14218 }
14219 else
14220 {
14221 /* Handle breakpoint ids with formats 'x.y' or
14222 'x.y-z'. */
14223 enable_disable_breakpoint_location_range
14224 (bp_num_range.first, bp_loc_range, enable);
14225 }
14226 num = extract_arg (&args);
14227 }
14228 }
14229 }
14230
14231 /* The disable command disables the specified breakpoints/locations
14232 (or all defined breakpoints) so they're no longer effective in
14233 stopping the inferior. ARGS may be in any of the forms defined in
14234 extract_bp_number_and_location. */
14235
14236 static void
14237 disable_command (const char *args, int from_tty)
14238 {
14239 enable_disable_command (args, from_tty, false);
14240 }
14241
14242 static void
14243 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14244 int count)
14245 {
14246 int target_resources_ok;
14247
14248 if (bpt->type == bp_hardware_breakpoint)
14249 {
14250 int i;
14251 i = hw_breakpoint_used_count ();
14252 target_resources_ok =
14253 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14254 i + 1, 0);
14255 if (target_resources_ok == 0)
14256 error (_("No hardware breakpoint support in the target."));
14257 else if (target_resources_ok < 0)
14258 error (_("Hardware breakpoints used exceeds limit."));
14259 }
14260
14261 if (is_watchpoint (bpt))
14262 {
14263 /* Initialize it just to avoid a GCC false warning. */
14264 enum enable_state orig_enable_state = bp_disabled;
14265
14266 try
14267 {
14268 struct watchpoint *w = (struct watchpoint *) bpt;
14269
14270 orig_enable_state = bpt->enable_state;
14271 bpt->enable_state = bp_enabled;
14272 update_watchpoint (w, 1 /* reparse */);
14273 }
14274 catch (const gdb_exception &e)
14275 {
14276 bpt->enable_state = orig_enable_state;
14277 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14278 bpt->number);
14279 return;
14280 }
14281 }
14282
14283 bpt->enable_state = bp_enabled;
14284
14285 /* Mark breakpoint locations modified. */
14286 mark_breakpoint_modified (bpt);
14287
14288 if (target_supports_enable_disable_tracepoint ()
14289 && current_trace_status ()->running && is_tracepoint (bpt))
14290 {
14291 struct bp_location *location;
14292
14293 for (location = bpt->loc; location; location = location->next)
14294 target_enable_tracepoint (location);
14295 }
14296
14297 bpt->disposition = disposition;
14298 bpt->enable_count = count;
14299 update_global_location_list (UGLL_MAY_INSERT);
14300
14301 gdb::observers::breakpoint_modified.notify (bpt);
14302 }
14303
14304
14305 void
14306 enable_breakpoint (struct breakpoint *bpt)
14307 {
14308 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14309 }
14310
14311 /* The enable command enables the specified breakpoints/locations (or
14312 all defined breakpoints) so they once again become (or continue to
14313 be) effective in stopping the inferior. ARGS may be in any of the
14314 forms defined in extract_bp_number_and_location. */
14315
14316 static void
14317 enable_command (const char *args, int from_tty)
14318 {
14319 enable_disable_command (args, from_tty, true);
14320 }
14321
14322 static void
14323 enable_once_command (const char *args, int from_tty)
14324 {
14325 map_breakpoint_numbers
14326 (args, [&] (breakpoint *b)
14327 {
14328 iterate_over_related_breakpoints
14329 (b, [&] (breakpoint *bpt)
14330 {
14331 enable_breakpoint_disp (bpt, disp_disable, 1);
14332 });
14333 });
14334 }
14335
14336 static void
14337 enable_count_command (const char *args, int from_tty)
14338 {
14339 int count;
14340
14341 if (args == NULL)
14342 error_no_arg (_("hit count"));
14343
14344 count = get_number (&args);
14345
14346 map_breakpoint_numbers
14347 (args, [&] (breakpoint *b)
14348 {
14349 iterate_over_related_breakpoints
14350 (b, [&] (breakpoint *bpt)
14351 {
14352 enable_breakpoint_disp (bpt, disp_disable, count);
14353 });
14354 });
14355 }
14356
14357 static void
14358 enable_delete_command (const char *args, int from_tty)
14359 {
14360 map_breakpoint_numbers
14361 (args, [&] (breakpoint *b)
14362 {
14363 iterate_over_related_breakpoints
14364 (b, [&] (breakpoint *bpt)
14365 {
14366 enable_breakpoint_disp (bpt, disp_del, 1);
14367 });
14368 });
14369 }
14370 \f
14371 static void
14372 set_breakpoint_cmd (const char *args, int from_tty)
14373 {
14374 }
14375
14376 static void
14377 show_breakpoint_cmd (const char *args, int from_tty)
14378 {
14379 }
14380
14381 /* Invalidate last known value of any hardware watchpoint if
14382 the memory which that value represents has been written to by
14383 GDB itself. */
14384
14385 static void
14386 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14387 CORE_ADDR addr, ssize_t len,
14388 const bfd_byte *data)
14389 {
14390 struct breakpoint *bp;
14391
14392 ALL_BREAKPOINTS (bp)
14393 if (bp->enable_state == bp_enabled
14394 && bp->type == bp_hardware_watchpoint)
14395 {
14396 struct watchpoint *wp = (struct watchpoint *) bp;
14397
14398 if (wp->val_valid && wp->val != nullptr)
14399 {
14400 struct bp_location *loc;
14401
14402 for (loc = bp->loc; loc != NULL; loc = loc->next)
14403 if (loc->loc_type == bp_loc_hardware_watchpoint
14404 && loc->address + loc->length > addr
14405 && addr + len > loc->address)
14406 {
14407 wp->val = NULL;
14408 wp->val_valid = false;
14409 }
14410 }
14411 }
14412 }
14413
14414 /* Create and insert a breakpoint for software single step. */
14415
14416 void
14417 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14418 const address_space *aspace,
14419 CORE_ADDR next_pc)
14420 {
14421 struct thread_info *tp = inferior_thread ();
14422 struct symtab_and_line sal;
14423 CORE_ADDR pc = next_pc;
14424
14425 if (tp->control.single_step_breakpoints == NULL)
14426 {
14427 tp->control.single_step_breakpoints
14428 = new_single_step_breakpoint (tp->global_num, gdbarch);
14429 }
14430
14431 sal = find_pc_line (pc, 0);
14432 sal.pc = pc;
14433 sal.section = find_pc_overlay (pc);
14434 sal.explicit_pc = 1;
14435 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14436
14437 update_global_location_list (UGLL_INSERT);
14438 }
14439
14440 /* Insert single step breakpoints according to the current state. */
14441
14442 int
14443 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14444 {
14445 struct regcache *regcache = get_current_regcache ();
14446 std::vector<CORE_ADDR> next_pcs;
14447
14448 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14449
14450 if (!next_pcs.empty ())
14451 {
14452 struct frame_info *frame = get_current_frame ();
14453 const address_space *aspace = get_frame_address_space (frame);
14454
14455 for (CORE_ADDR pc : next_pcs)
14456 insert_single_step_breakpoint (gdbarch, aspace, pc);
14457
14458 return 1;
14459 }
14460 else
14461 return 0;
14462 }
14463
14464 /* See breakpoint.h. */
14465
14466 int
14467 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14468 const address_space *aspace,
14469 CORE_ADDR pc)
14470 {
14471 struct bp_location *loc;
14472
14473 for (loc = bp->loc; loc != NULL; loc = loc->next)
14474 if (loc->inserted
14475 && breakpoint_location_address_match (loc, aspace, pc))
14476 return 1;
14477
14478 return 0;
14479 }
14480
14481 /* Check whether a software single-step breakpoint is inserted at
14482 PC. */
14483
14484 int
14485 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14486 CORE_ADDR pc)
14487 {
14488 struct breakpoint *bpt;
14489
14490 ALL_BREAKPOINTS (bpt)
14491 {
14492 if (bpt->type == bp_single_step
14493 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14494 return 1;
14495 }
14496 return 0;
14497 }
14498
14499 /* Tracepoint-specific operations. */
14500
14501 /* Set tracepoint count to NUM. */
14502 static void
14503 set_tracepoint_count (int num)
14504 {
14505 tracepoint_count = num;
14506 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14507 }
14508
14509 static void
14510 trace_command (const char *arg, int from_tty)
14511 {
14512 event_location_up location = string_to_event_location (&arg,
14513 current_language);
14514 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14515 (location.get (), true /* is_tracepoint */);
14516
14517 create_breakpoint (get_current_arch (),
14518 location.get (),
14519 NULL, 0, arg, 1 /* parse arg */,
14520 0 /* tempflag */,
14521 bp_tracepoint /* type_wanted */,
14522 0 /* Ignore count */,
14523 pending_break_support,
14524 ops,
14525 from_tty,
14526 1 /* enabled */,
14527 0 /* internal */, 0);
14528 }
14529
14530 static void
14531 ftrace_command (const char *arg, int from_tty)
14532 {
14533 event_location_up location = string_to_event_location (&arg,
14534 current_language);
14535 create_breakpoint (get_current_arch (),
14536 location.get (),
14537 NULL, 0, arg, 1 /* parse arg */,
14538 0 /* tempflag */,
14539 bp_fast_tracepoint /* type_wanted */,
14540 0 /* Ignore count */,
14541 pending_break_support,
14542 &tracepoint_breakpoint_ops,
14543 from_tty,
14544 1 /* enabled */,
14545 0 /* internal */, 0);
14546 }
14547
14548 /* strace command implementation. Creates a static tracepoint. */
14549
14550 static void
14551 strace_command (const char *arg, int from_tty)
14552 {
14553 struct breakpoint_ops *ops;
14554 event_location_up location;
14555
14556 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14557 or with a normal static tracepoint. */
14558 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14559 {
14560 ops = &strace_marker_breakpoint_ops;
14561 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14562 }
14563 else
14564 {
14565 ops = &tracepoint_breakpoint_ops;
14566 location = string_to_event_location (&arg, current_language);
14567 }
14568
14569 create_breakpoint (get_current_arch (),
14570 location.get (),
14571 NULL, 0, arg, 1 /* parse arg */,
14572 0 /* tempflag */,
14573 bp_static_tracepoint /* type_wanted */,
14574 0 /* Ignore count */,
14575 pending_break_support,
14576 ops,
14577 from_tty,
14578 1 /* enabled */,
14579 0 /* internal */, 0);
14580 }
14581
14582 /* Set up a fake reader function that gets command lines from a linked
14583 list that was acquired during tracepoint uploading. */
14584
14585 static struct uploaded_tp *this_utp;
14586 static int next_cmd;
14587
14588 static char *
14589 read_uploaded_action (void)
14590 {
14591 char *rslt = nullptr;
14592
14593 if (next_cmd < this_utp->cmd_strings.size ())
14594 {
14595 rslt = this_utp->cmd_strings[next_cmd].get ();
14596 next_cmd++;
14597 }
14598
14599 return rslt;
14600 }
14601
14602 /* Given information about a tracepoint as recorded on a target (which
14603 can be either a live system or a trace file), attempt to create an
14604 equivalent GDB tracepoint. This is not a reliable process, since
14605 the target does not necessarily have all the information used when
14606 the tracepoint was originally defined. */
14607
14608 struct tracepoint *
14609 create_tracepoint_from_upload (struct uploaded_tp *utp)
14610 {
14611 const char *addr_str;
14612 char small_buf[100];
14613 struct tracepoint *tp;
14614
14615 if (utp->at_string)
14616 addr_str = utp->at_string.get ();
14617 else
14618 {
14619 /* In the absence of a source location, fall back to raw
14620 address. Since there is no way to confirm that the address
14621 means the same thing as when the trace was started, warn the
14622 user. */
14623 warning (_("Uploaded tracepoint %d has no "
14624 "source location, using raw address"),
14625 utp->number);
14626 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14627 addr_str = small_buf;
14628 }
14629
14630 /* There's not much we can do with a sequence of bytecodes. */
14631 if (utp->cond && !utp->cond_string)
14632 warning (_("Uploaded tracepoint %d condition "
14633 "has no source form, ignoring it"),
14634 utp->number);
14635
14636 event_location_up location = string_to_event_location (&addr_str,
14637 current_language);
14638 if (!create_breakpoint (get_current_arch (),
14639 location.get (),
14640 utp->cond_string.get (), -1, addr_str,
14641 0 /* parse cond/thread */,
14642 0 /* tempflag */,
14643 utp->type /* type_wanted */,
14644 0 /* Ignore count */,
14645 pending_break_support,
14646 &tracepoint_breakpoint_ops,
14647 0 /* from_tty */,
14648 utp->enabled /* enabled */,
14649 0 /* internal */,
14650 CREATE_BREAKPOINT_FLAGS_INSERTED))
14651 return NULL;
14652
14653 /* Get the tracepoint we just created. */
14654 tp = get_tracepoint (tracepoint_count);
14655 gdb_assert (tp != NULL);
14656
14657 if (utp->pass > 0)
14658 {
14659 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14660 tp->number);
14661
14662 trace_pass_command (small_buf, 0);
14663 }
14664
14665 /* If we have uploaded versions of the original commands, set up a
14666 special-purpose "reader" function and call the usual command line
14667 reader, then pass the result to the breakpoint command-setting
14668 function. */
14669 if (!utp->cmd_strings.empty ())
14670 {
14671 counted_command_line cmd_list;
14672
14673 this_utp = utp;
14674 next_cmd = 0;
14675
14676 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14677
14678 breakpoint_set_commands (tp, std::move (cmd_list));
14679 }
14680 else if (!utp->actions.empty ()
14681 || !utp->step_actions.empty ())
14682 warning (_("Uploaded tracepoint %d actions "
14683 "have no source form, ignoring them"),
14684 utp->number);
14685
14686 /* Copy any status information that might be available. */
14687 tp->hit_count = utp->hit_count;
14688 tp->traceframe_usage = utp->traceframe_usage;
14689
14690 return tp;
14691 }
14692
14693 /* Print information on tracepoint number TPNUM_EXP, or all if
14694 omitted. */
14695
14696 static void
14697 info_tracepoints_command (const char *args, int from_tty)
14698 {
14699 struct ui_out *uiout = current_uiout;
14700 int num_printed;
14701
14702 num_printed = breakpoint_1 (args, false, is_tracepoint);
14703
14704 if (num_printed == 0)
14705 {
14706 if (args == NULL || *args == '\0')
14707 uiout->message ("No tracepoints.\n");
14708 else
14709 uiout->message ("No tracepoint matching '%s'.\n", args);
14710 }
14711
14712 default_collect_info ();
14713 }
14714
14715 /* The 'enable trace' command enables tracepoints.
14716 Not supported by all targets. */
14717 static void
14718 enable_trace_command (const char *args, int from_tty)
14719 {
14720 enable_command (args, from_tty);
14721 }
14722
14723 /* The 'disable trace' command disables tracepoints.
14724 Not supported by all targets. */
14725 static void
14726 disable_trace_command (const char *args, int from_tty)
14727 {
14728 disable_command (args, from_tty);
14729 }
14730
14731 /* Remove a tracepoint (or all if no argument). */
14732 static void
14733 delete_trace_command (const char *arg, int from_tty)
14734 {
14735 struct breakpoint *b, *b_tmp;
14736
14737 dont_repeat ();
14738
14739 if (arg == 0)
14740 {
14741 int breaks_to_delete = 0;
14742
14743 /* Delete all breakpoints if no argument.
14744 Do not delete internal or call-dummy breakpoints, these
14745 have to be deleted with an explicit breakpoint number
14746 argument. */
14747 ALL_TRACEPOINTS (b)
14748 if (is_tracepoint (b) && user_breakpoint_p (b))
14749 {
14750 breaks_to_delete = 1;
14751 break;
14752 }
14753
14754 /* Ask user only if there are some breakpoints to delete. */
14755 if (!from_tty
14756 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14757 {
14758 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14759 if (is_tracepoint (b) && user_breakpoint_p (b))
14760 delete_breakpoint (b);
14761 }
14762 }
14763 else
14764 map_breakpoint_numbers
14765 (arg, [&] (breakpoint *br)
14766 {
14767 iterate_over_related_breakpoints (br, delete_breakpoint);
14768 });
14769 }
14770
14771 /* Helper function for trace_pass_command. */
14772
14773 static void
14774 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14775 {
14776 tp->pass_count = count;
14777 gdb::observers::breakpoint_modified.notify (tp);
14778 if (from_tty)
14779 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14780 tp->number, count);
14781 }
14782
14783 /* Set passcount for tracepoint.
14784
14785 First command argument is passcount, second is tracepoint number.
14786 If tracepoint number omitted, apply to most recently defined.
14787 Also accepts special argument "all". */
14788
14789 static void
14790 trace_pass_command (const char *args, int from_tty)
14791 {
14792 struct tracepoint *t1;
14793 ULONGEST count;
14794
14795 if (args == 0 || *args == 0)
14796 error (_("passcount command requires an "
14797 "argument (count + optional TP num)"));
14798
14799 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14800
14801 args = skip_spaces (args);
14802 if (*args && strncasecmp (args, "all", 3) == 0)
14803 {
14804 struct breakpoint *b;
14805
14806 args += 3; /* Skip special argument "all". */
14807 if (*args)
14808 error (_("Junk at end of arguments."));
14809
14810 ALL_TRACEPOINTS (b)
14811 {
14812 t1 = (struct tracepoint *) b;
14813 trace_pass_set_count (t1, count, from_tty);
14814 }
14815 }
14816 else if (*args == '\0')
14817 {
14818 t1 = get_tracepoint_by_number (&args, NULL);
14819 if (t1)
14820 trace_pass_set_count (t1, count, from_tty);
14821 }
14822 else
14823 {
14824 number_or_range_parser parser (args);
14825 while (!parser.finished ())
14826 {
14827 t1 = get_tracepoint_by_number (&args, &parser);
14828 if (t1)
14829 trace_pass_set_count (t1, count, from_tty);
14830 }
14831 }
14832 }
14833
14834 struct tracepoint *
14835 get_tracepoint (int num)
14836 {
14837 struct breakpoint *t;
14838
14839 ALL_TRACEPOINTS (t)
14840 if (t->number == num)
14841 return (struct tracepoint *) t;
14842
14843 return NULL;
14844 }
14845
14846 /* Find the tracepoint with the given target-side number (which may be
14847 different from the tracepoint number after disconnecting and
14848 reconnecting). */
14849
14850 struct tracepoint *
14851 get_tracepoint_by_number_on_target (int num)
14852 {
14853 struct breakpoint *b;
14854
14855 ALL_TRACEPOINTS (b)
14856 {
14857 struct tracepoint *t = (struct tracepoint *) b;
14858
14859 if (t->number_on_target == num)
14860 return t;
14861 }
14862
14863 return NULL;
14864 }
14865
14866 /* Utility: parse a tracepoint number and look it up in the list.
14867 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14868 If the argument is missing, the most recent tracepoint
14869 (tracepoint_count) is returned. */
14870
14871 struct tracepoint *
14872 get_tracepoint_by_number (const char **arg,
14873 number_or_range_parser *parser)
14874 {
14875 struct breakpoint *t;
14876 int tpnum;
14877 const char *instring = arg == NULL ? NULL : *arg;
14878
14879 if (parser != NULL)
14880 {
14881 gdb_assert (!parser->finished ());
14882 tpnum = parser->get_number ();
14883 }
14884 else if (arg == NULL || *arg == NULL || ! **arg)
14885 tpnum = tracepoint_count;
14886 else
14887 tpnum = get_number (arg);
14888
14889 if (tpnum <= 0)
14890 {
14891 if (instring && *instring)
14892 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14893 instring);
14894 else
14895 printf_filtered (_("No previous tracepoint\n"));
14896 return NULL;
14897 }
14898
14899 ALL_TRACEPOINTS (t)
14900 if (t->number == tpnum)
14901 {
14902 return (struct tracepoint *) t;
14903 }
14904
14905 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14906 return NULL;
14907 }
14908
14909 void
14910 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14911 {
14912 if (b->thread != -1)
14913 fprintf_unfiltered (fp, " thread %d", b->thread);
14914
14915 if (b->task != 0)
14916 fprintf_unfiltered (fp, " task %d", b->task);
14917
14918 fprintf_unfiltered (fp, "\n");
14919 }
14920
14921 /* Save information on user settable breakpoints (watchpoints, etc) to
14922 a new script file named FILENAME. If FILTER is non-NULL, call it
14923 on each breakpoint and only include the ones for which it returns
14924 true. */
14925
14926 static void
14927 save_breakpoints (const char *filename, int from_tty,
14928 bool (*filter) (const struct breakpoint *))
14929 {
14930 struct breakpoint *tp;
14931 int any = 0;
14932 int extra_trace_bits = 0;
14933
14934 if (filename == 0 || *filename == 0)
14935 error (_("Argument required (file name in which to save)"));
14936
14937 /* See if we have anything to save. */
14938 ALL_BREAKPOINTS (tp)
14939 {
14940 /* Skip internal and momentary breakpoints. */
14941 if (!user_breakpoint_p (tp))
14942 continue;
14943
14944 /* If we have a filter, only save the breakpoints it accepts. */
14945 if (filter && !filter (tp))
14946 continue;
14947
14948 any = 1;
14949
14950 if (is_tracepoint (tp))
14951 {
14952 extra_trace_bits = 1;
14953
14954 /* We can stop searching. */
14955 break;
14956 }
14957 }
14958
14959 if (!any)
14960 {
14961 warning (_("Nothing to save."));
14962 return;
14963 }
14964
14965 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14966
14967 stdio_file fp;
14968
14969 if (!fp.open (expanded_filename.get (), "w"))
14970 error (_("Unable to open file '%s' for saving (%s)"),
14971 expanded_filename.get (), safe_strerror (errno));
14972
14973 if (extra_trace_bits)
14974 save_trace_state_variables (&fp);
14975
14976 ALL_BREAKPOINTS (tp)
14977 {
14978 /* Skip internal and momentary breakpoints. */
14979 if (!user_breakpoint_p (tp))
14980 continue;
14981
14982 /* If we have a filter, only save the breakpoints it accepts. */
14983 if (filter && !filter (tp))
14984 continue;
14985
14986 tp->ops->print_recreate (tp, &fp);
14987
14988 /* Note, we can't rely on tp->number for anything, as we can't
14989 assume the recreated breakpoint numbers will match. Use $bpnum
14990 instead. */
14991
14992 if (tp->cond_string)
14993 fp.printf (" condition $bpnum %s\n", tp->cond_string);
14994
14995 if (tp->ignore_count)
14996 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14997
14998 if (tp->type != bp_dprintf && tp->commands)
14999 {
15000 fp.puts (" commands\n");
15001
15002 current_uiout->redirect (&fp);
15003 try
15004 {
15005 print_command_lines (current_uiout, tp->commands.get (), 2);
15006 }
15007 catch (const gdb_exception &ex)
15008 {
15009 current_uiout->redirect (NULL);
15010 throw;
15011 }
15012
15013 current_uiout->redirect (NULL);
15014 fp.puts (" end\n");
15015 }
15016
15017 if (tp->enable_state == bp_disabled)
15018 fp.puts ("disable $bpnum\n");
15019
15020 /* If this is a multi-location breakpoint, check if the locations
15021 should be individually disabled. Watchpoint locations are
15022 special, and not user visible. */
15023 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15024 {
15025 struct bp_location *loc;
15026 int n = 1;
15027
15028 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15029 if (!loc->enabled)
15030 fp.printf ("disable $bpnum.%d\n", n);
15031 }
15032 }
15033
15034 if (extra_trace_bits && *default_collect)
15035 fp.printf ("set default-collect %s\n", default_collect);
15036
15037 if (from_tty)
15038 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15039 }
15040
15041 /* The `save breakpoints' command. */
15042
15043 static void
15044 save_breakpoints_command (const char *args, int from_tty)
15045 {
15046 save_breakpoints (args, from_tty, NULL);
15047 }
15048
15049 /* The `save tracepoints' command. */
15050
15051 static void
15052 save_tracepoints_command (const char *args, int from_tty)
15053 {
15054 save_breakpoints (args, from_tty, is_tracepoint);
15055 }
15056
15057 /* Create a vector of all tracepoints. */
15058
15059 std::vector<breakpoint *>
15060 all_tracepoints (void)
15061 {
15062 std::vector<breakpoint *> tp_vec;
15063 struct breakpoint *tp;
15064
15065 ALL_TRACEPOINTS (tp)
15066 {
15067 tp_vec.push_back (tp);
15068 }
15069
15070 return tp_vec;
15071 }
15072
15073 \f
15074 /* This help string is used to consolidate all the help string for specifying
15075 locations used by several commands. */
15076
15077 #define LOCATION_HELP_STRING \
15078 "Linespecs are colon-separated lists of location parameters, such as\n\
15079 source filename, function name, label name, and line number.\n\
15080 Example: To specify the start of a label named \"the_top\" in the\n\
15081 function \"fact\" in the file \"factorial.c\", use\n\
15082 \"factorial.c:fact:the_top\".\n\
15083 \n\
15084 Address locations begin with \"*\" and specify an exact address in the\n\
15085 program. Example: To specify the fourth byte past the start function\n\
15086 \"main\", use \"*main + 4\".\n\
15087 \n\
15088 Explicit locations are similar to linespecs but use an option/argument\n\
15089 syntax to specify location parameters.\n\
15090 Example: To specify the start of the label named \"the_top\" in the\n\
15091 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15092 -function fact -label the_top\".\n\
15093 \n\
15094 By default, a specified function is matched against the program's\n\
15095 functions in all scopes. For C++, this means in all namespaces and\n\
15096 classes. For Ada, this means in all packages. E.g., in C++,\n\
15097 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15098 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15099 specified name as a complete fully-qualified name instead."
15100
15101 /* This help string is used for the break, hbreak, tbreak and thbreak
15102 commands. It is defined as a macro to prevent duplication.
15103 COMMAND should be a string constant containing the name of the
15104 command. */
15105
15106 #define BREAK_ARGS_HELP(command) \
15107 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15108 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15109 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15110 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15111 `-probe-dtrace' (for a DTrace probe).\n\
15112 LOCATION may be a linespec, address, or explicit location as described\n\
15113 below.\n\
15114 \n\
15115 With no LOCATION, uses current execution address of the selected\n\
15116 stack frame. This is useful for breaking on return to a stack frame.\n\
15117 \n\
15118 THREADNUM is the number from \"info threads\".\n\
15119 CONDITION is a boolean expression.\n\
15120 \n" LOCATION_HELP_STRING "\n\n\
15121 Multiple breakpoints at one place are permitted, and useful if their\n\
15122 conditions are different.\n\
15123 \n\
15124 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15125
15126 /* List of subcommands for "catch". */
15127 static struct cmd_list_element *catch_cmdlist;
15128
15129 /* List of subcommands for "tcatch". */
15130 static struct cmd_list_element *tcatch_cmdlist;
15131
15132 void
15133 add_catch_command (const char *name, const char *docstring,
15134 cmd_const_sfunc_ftype *sfunc,
15135 completer_ftype *completer,
15136 void *user_data_catch,
15137 void *user_data_tcatch)
15138 {
15139 struct cmd_list_element *command;
15140
15141 command = add_cmd (name, class_breakpoint, docstring,
15142 &catch_cmdlist);
15143 set_cmd_sfunc (command, sfunc);
15144 set_cmd_context (command, user_data_catch);
15145 set_cmd_completer (command, completer);
15146
15147 command = add_cmd (name, class_breakpoint, docstring,
15148 &tcatch_cmdlist);
15149 set_cmd_sfunc (command, sfunc);
15150 set_cmd_context (command, user_data_tcatch);
15151 set_cmd_completer (command, completer);
15152 }
15153
15154 static void
15155 save_command (const char *arg, int from_tty)
15156 {
15157 printf_unfiltered (_("\"save\" must be followed by "
15158 "the name of a save subcommand.\n"));
15159 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15160 }
15161
15162 struct breakpoint *
15163 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15164 {
15165 struct breakpoint *b, *b_tmp;
15166
15167 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15168 {
15169 if (callback (b))
15170 return b;
15171 }
15172
15173 return NULL;
15174 }
15175
15176 /* Zero if any of the breakpoint's locations could be a location where
15177 functions have been inlined, nonzero otherwise. */
15178
15179 static int
15180 is_non_inline_function (struct breakpoint *b)
15181 {
15182 /* The shared library event breakpoint is set on the address of a
15183 non-inline function. */
15184 if (b->type == bp_shlib_event)
15185 return 1;
15186
15187 return 0;
15188 }
15189
15190 /* Nonzero if the specified PC cannot be a location where functions
15191 have been inlined. */
15192
15193 int
15194 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15195 const struct target_waitstatus *ws)
15196 {
15197 struct breakpoint *b;
15198 struct bp_location *bl;
15199
15200 ALL_BREAKPOINTS (b)
15201 {
15202 if (!is_non_inline_function (b))
15203 continue;
15204
15205 for (bl = b->loc; bl != NULL; bl = bl->next)
15206 {
15207 if (!bl->shlib_disabled
15208 && bpstat_check_location (bl, aspace, pc, ws))
15209 return 1;
15210 }
15211 }
15212
15213 return 0;
15214 }
15215
15216 /* Remove any references to OBJFILE which is going to be freed. */
15217
15218 void
15219 breakpoint_free_objfile (struct objfile *objfile)
15220 {
15221 struct bp_location **locp, *loc;
15222
15223 ALL_BP_LOCATIONS (loc, locp)
15224 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15225 loc->symtab = NULL;
15226 }
15227
15228 void
15229 initialize_breakpoint_ops (void)
15230 {
15231 static int initialized = 0;
15232
15233 struct breakpoint_ops *ops;
15234
15235 if (initialized)
15236 return;
15237 initialized = 1;
15238
15239 /* The breakpoint_ops structure to be inherit by all kinds of
15240 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15241 internal and momentary breakpoints, etc.). */
15242 ops = &bkpt_base_breakpoint_ops;
15243 *ops = base_breakpoint_ops;
15244 ops->re_set = bkpt_re_set;
15245 ops->insert_location = bkpt_insert_location;
15246 ops->remove_location = bkpt_remove_location;
15247 ops->breakpoint_hit = bkpt_breakpoint_hit;
15248 ops->create_sals_from_location = bkpt_create_sals_from_location;
15249 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15250 ops->decode_location = bkpt_decode_location;
15251
15252 /* The breakpoint_ops structure to be used in regular breakpoints. */
15253 ops = &bkpt_breakpoint_ops;
15254 *ops = bkpt_base_breakpoint_ops;
15255 ops->re_set = bkpt_re_set;
15256 ops->resources_needed = bkpt_resources_needed;
15257 ops->print_it = bkpt_print_it;
15258 ops->print_mention = bkpt_print_mention;
15259 ops->print_recreate = bkpt_print_recreate;
15260
15261 /* Ranged breakpoints. */
15262 ops = &ranged_breakpoint_ops;
15263 *ops = bkpt_breakpoint_ops;
15264 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15265 ops->resources_needed = resources_needed_ranged_breakpoint;
15266 ops->print_it = print_it_ranged_breakpoint;
15267 ops->print_one = print_one_ranged_breakpoint;
15268 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15269 ops->print_mention = print_mention_ranged_breakpoint;
15270 ops->print_recreate = print_recreate_ranged_breakpoint;
15271
15272 /* Internal breakpoints. */
15273 ops = &internal_breakpoint_ops;
15274 *ops = bkpt_base_breakpoint_ops;
15275 ops->re_set = internal_bkpt_re_set;
15276 ops->check_status = internal_bkpt_check_status;
15277 ops->print_it = internal_bkpt_print_it;
15278 ops->print_mention = internal_bkpt_print_mention;
15279
15280 /* Momentary breakpoints. */
15281 ops = &momentary_breakpoint_ops;
15282 *ops = bkpt_base_breakpoint_ops;
15283 ops->re_set = momentary_bkpt_re_set;
15284 ops->check_status = momentary_bkpt_check_status;
15285 ops->print_it = momentary_bkpt_print_it;
15286 ops->print_mention = momentary_bkpt_print_mention;
15287
15288 /* Probe breakpoints. */
15289 ops = &bkpt_probe_breakpoint_ops;
15290 *ops = bkpt_breakpoint_ops;
15291 ops->insert_location = bkpt_probe_insert_location;
15292 ops->remove_location = bkpt_probe_remove_location;
15293 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15294 ops->decode_location = bkpt_probe_decode_location;
15295
15296 /* Watchpoints. */
15297 ops = &watchpoint_breakpoint_ops;
15298 *ops = base_breakpoint_ops;
15299 ops->re_set = re_set_watchpoint;
15300 ops->insert_location = insert_watchpoint;
15301 ops->remove_location = remove_watchpoint;
15302 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15303 ops->check_status = check_status_watchpoint;
15304 ops->resources_needed = resources_needed_watchpoint;
15305 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15306 ops->print_it = print_it_watchpoint;
15307 ops->print_mention = print_mention_watchpoint;
15308 ops->print_recreate = print_recreate_watchpoint;
15309 ops->explains_signal = explains_signal_watchpoint;
15310
15311 /* Masked watchpoints. */
15312 ops = &masked_watchpoint_breakpoint_ops;
15313 *ops = watchpoint_breakpoint_ops;
15314 ops->insert_location = insert_masked_watchpoint;
15315 ops->remove_location = remove_masked_watchpoint;
15316 ops->resources_needed = resources_needed_masked_watchpoint;
15317 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15318 ops->print_it = print_it_masked_watchpoint;
15319 ops->print_one_detail = print_one_detail_masked_watchpoint;
15320 ops->print_mention = print_mention_masked_watchpoint;
15321 ops->print_recreate = print_recreate_masked_watchpoint;
15322
15323 /* Tracepoints. */
15324 ops = &tracepoint_breakpoint_ops;
15325 *ops = base_breakpoint_ops;
15326 ops->re_set = tracepoint_re_set;
15327 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15328 ops->print_one_detail = tracepoint_print_one_detail;
15329 ops->print_mention = tracepoint_print_mention;
15330 ops->print_recreate = tracepoint_print_recreate;
15331 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15332 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15333 ops->decode_location = tracepoint_decode_location;
15334
15335 /* Probe tracepoints. */
15336 ops = &tracepoint_probe_breakpoint_ops;
15337 *ops = tracepoint_breakpoint_ops;
15338 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15339 ops->decode_location = tracepoint_probe_decode_location;
15340
15341 /* Static tracepoints with marker (`-m'). */
15342 ops = &strace_marker_breakpoint_ops;
15343 *ops = tracepoint_breakpoint_ops;
15344 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15345 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15346 ops->decode_location = strace_marker_decode_location;
15347
15348 /* Fork catchpoints. */
15349 ops = &catch_fork_breakpoint_ops;
15350 *ops = base_breakpoint_ops;
15351 ops->insert_location = insert_catch_fork;
15352 ops->remove_location = remove_catch_fork;
15353 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15354 ops->print_it = print_it_catch_fork;
15355 ops->print_one = print_one_catch_fork;
15356 ops->print_mention = print_mention_catch_fork;
15357 ops->print_recreate = print_recreate_catch_fork;
15358
15359 /* Vfork catchpoints. */
15360 ops = &catch_vfork_breakpoint_ops;
15361 *ops = base_breakpoint_ops;
15362 ops->insert_location = insert_catch_vfork;
15363 ops->remove_location = remove_catch_vfork;
15364 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15365 ops->print_it = print_it_catch_vfork;
15366 ops->print_one = print_one_catch_vfork;
15367 ops->print_mention = print_mention_catch_vfork;
15368 ops->print_recreate = print_recreate_catch_vfork;
15369
15370 /* Exec catchpoints. */
15371 ops = &catch_exec_breakpoint_ops;
15372 *ops = base_breakpoint_ops;
15373 ops->insert_location = insert_catch_exec;
15374 ops->remove_location = remove_catch_exec;
15375 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15376 ops->print_it = print_it_catch_exec;
15377 ops->print_one = print_one_catch_exec;
15378 ops->print_mention = print_mention_catch_exec;
15379 ops->print_recreate = print_recreate_catch_exec;
15380
15381 /* Solib-related catchpoints. */
15382 ops = &catch_solib_breakpoint_ops;
15383 *ops = base_breakpoint_ops;
15384 ops->insert_location = insert_catch_solib;
15385 ops->remove_location = remove_catch_solib;
15386 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15387 ops->check_status = check_status_catch_solib;
15388 ops->print_it = print_it_catch_solib;
15389 ops->print_one = print_one_catch_solib;
15390 ops->print_mention = print_mention_catch_solib;
15391 ops->print_recreate = print_recreate_catch_solib;
15392
15393 ops = &dprintf_breakpoint_ops;
15394 *ops = bkpt_base_breakpoint_ops;
15395 ops->re_set = dprintf_re_set;
15396 ops->resources_needed = bkpt_resources_needed;
15397 ops->print_it = bkpt_print_it;
15398 ops->print_mention = bkpt_print_mention;
15399 ops->print_recreate = dprintf_print_recreate;
15400 ops->after_condition_true = dprintf_after_condition_true;
15401 ops->breakpoint_hit = dprintf_breakpoint_hit;
15402 }
15403
15404 /* Chain containing all defined "enable breakpoint" subcommands. */
15405
15406 static struct cmd_list_element *enablebreaklist = NULL;
15407
15408 /* See breakpoint.h. */
15409
15410 cmd_list_element *commands_cmd_element = nullptr;
15411
15412 void
15413 _initialize_breakpoint (void)
15414 {
15415 struct cmd_list_element *c;
15416
15417 initialize_breakpoint_ops ();
15418
15419 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15420 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15421 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15422
15423 breakpoint_chain = 0;
15424 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15425 before a breakpoint is set. */
15426 breakpoint_count = 0;
15427
15428 tracepoint_count = 0;
15429
15430 add_com ("ignore", class_breakpoint, ignore_command, _("\
15431 Set ignore-count of breakpoint number N to COUNT.\n\
15432 Usage is `ignore N COUNT'."));
15433
15434 commands_cmd_element = add_com ("commands", class_breakpoint,
15435 commands_command, _("\
15436 Set commands to be executed when the given breakpoints are hit.\n\
15437 Give a space-separated breakpoint list as argument after \"commands\".\n\
15438 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15439 (e.g. `5-7').\n\
15440 With no argument, the targeted breakpoint is the last one set.\n\
15441 The commands themselves follow starting on the next line.\n\
15442 Type a line containing \"end\" to indicate the end of them.\n\
15443 Give \"silent\" as the first line to make the breakpoint silent;\n\
15444 then no output is printed when it is hit, except what the commands print."));
15445
15446 c = add_com ("condition", class_breakpoint, condition_command, _("\
15447 Specify breakpoint number N to break only if COND is true.\n\
15448 Usage is `condition N COND', where N is an integer and COND is an\n\
15449 expression to be evaluated whenever breakpoint N is reached."));
15450 set_cmd_completer (c, condition_completer);
15451
15452 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15453 Set a temporary breakpoint.\n\
15454 Like \"break\" except the breakpoint is only temporary,\n\
15455 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15456 by using \"enable delete\" on the breakpoint number.\n\
15457 \n"
15458 BREAK_ARGS_HELP ("tbreak")));
15459 set_cmd_completer (c, location_completer);
15460
15461 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15462 Set a hardware assisted breakpoint.\n\
15463 Like \"break\" except the breakpoint requires hardware support,\n\
15464 some target hardware may not have this support.\n\
15465 \n"
15466 BREAK_ARGS_HELP ("hbreak")));
15467 set_cmd_completer (c, location_completer);
15468
15469 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15470 Set a temporary hardware assisted breakpoint.\n\
15471 Like \"hbreak\" except the breakpoint is only temporary,\n\
15472 so it will be deleted when hit.\n\
15473 \n"
15474 BREAK_ARGS_HELP ("thbreak")));
15475 set_cmd_completer (c, location_completer);
15476
15477 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15478 Enable all or some breakpoints.\n\
15479 Usage: enable [BREAKPOINTNUM]...\n\
15480 Give breakpoint numbers (separated by spaces) as arguments.\n\
15481 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15482 This is used to cancel the effect of the \"disable\" command.\n\
15483 With a subcommand you can enable temporarily."),
15484 &enablelist, "enable ", 1, &cmdlist);
15485
15486 add_com_alias ("en", "enable", class_breakpoint, 1);
15487
15488 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15489 Enable all or some breakpoints.\n\
15490 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15491 Give breakpoint numbers (separated by spaces) as arguments.\n\
15492 This is used to cancel the effect of the \"disable\" command.\n\
15493 May be abbreviated to simply \"enable\"."),
15494 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15495
15496 add_cmd ("once", no_class, enable_once_command, _("\
15497 Enable some breakpoints for one hit.\n\
15498 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15499 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15500 &enablebreaklist);
15501
15502 add_cmd ("delete", no_class, enable_delete_command, _("\
15503 Enable some breakpoints and delete when hit.\n\
15504 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15505 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15506 &enablebreaklist);
15507
15508 add_cmd ("count", no_class, enable_count_command, _("\
15509 Enable some breakpoints for COUNT hits.\n\
15510 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15511 If a breakpoint is hit while enabled in this fashion,\n\
15512 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15513 &enablebreaklist);
15514
15515 add_cmd ("delete", no_class, enable_delete_command, _("\
15516 Enable some breakpoints and delete when hit.\n\
15517 Usage: enable delete BREAKPOINTNUM...\n\
15518 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15519 &enablelist);
15520
15521 add_cmd ("once", no_class, enable_once_command, _("\
15522 Enable some breakpoints for one hit.\n\
15523 Usage: enable once BREAKPOINTNUM...\n\
15524 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15525 &enablelist);
15526
15527 add_cmd ("count", no_class, enable_count_command, _("\
15528 Enable some breakpoints for COUNT hits.\n\
15529 Usage: enable count COUNT BREAKPOINTNUM...\n\
15530 If a breakpoint is hit while enabled in this fashion,\n\
15531 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15532 &enablelist);
15533
15534 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15535 Disable all or some breakpoints.\n\
15536 Usage: disable [BREAKPOINTNUM]...\n\
15537 Arguments are breakpoint numbers with spaces in between.\n\
15538 To disable all breakpoints, give no argument.\n\
15539 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15540 &disablelist, "disable ", 1, &cmdlist);
15541 add_com_alias ("dis", "disable", class_breakpoint, 1);
15542 add_com_alias ("disa", "disable", class_breakpoint, 1);
15543
15544 add_cmd ("breakpoints", class_alias, disable_command, _("\
15545 Disable all or some breakpoints.\n\
15546 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15547 Arguments are breakpoint numbers with spaces in between.\n\
15548 To disable all breakpoints, give no argument.\n\
15549 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15550 This command may be abbreviated \"disable\"."),
15551 &disablelist);
15552
15553 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15554 Delete all or some breakpoints.\n\
15555 Usage: delete [BREAKPOINTNUM]...\n\
15556 Arguments are breakpoint numbers with spaces in between.\n\
15557 To delete all breakpoints, give no argument.\n\
15558 \n\
15559 Also a prefix command for deletion of other GDB objects."),
15560 &deletelist, "delete ", 1, &cmdlist);
15561 add_com_alias ("d", "delete", class_breakpoint, 1);
15562 add_com_alias ("del", "delete", class_breakpoint, 1);
15563
15564 add_cmd ("breakpoints", class_alias, delete_command, _("\
15565 Delete all or some breakpoints or auto-display expressions.\n\
15566 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15567 Arguments are breakpoint numbers with spaces in between.\n\
15568 To delete all breakpoints, give no argument.\n\
15569 This command may be abbreviated \"delete\"."),
15570 &deletelist);
15571
15572 add_com ("clear", class_breakpoint, clear_command, _("\
15573 Clear breakpoint at specified location.\n\
15574 Argument may be a linespec, explicit, or address location as described below.\n\
15575 \n\
15576 With no argument, clears all breakpoints in the line that the selected frame\n\
15577 is executing in.\n"
15578 "\n" LOCATION_HELP_STRING "\n\n\
15579 See also the \"delete\" command which clears breakpoints by number."));
15580 add_com_alias ("cl", "clear", class_breakpoint, 1);
15581
15582 c = add_com ("break", class_breakpoint, break_command, _("\
15583 Set breakpoint at specified location.\n"
15584 BREAK_ARGS_HELP ("break")));
15585 set_cmd_completer (c, location_completer);
15586
15587 add_com_alias ("b", "break", class_run, 1);
15588 add_com_alias ("br", "break", class_run, 1);
15589 add_com_alias ("bre", "break", class_run, 1);
15590 add_com_alias ("brea", "break", class_run, 1);
15591
15592 if (dbx_commands)
15593 {
15594 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15595 Break in function/address or break at a line in the current file."),
15596 &stoplist, "stop ", 1, &cmdlist);
15597 add_cmd ("in", class_breakpoint, stopin_command,
15598 _("Break in function or address."), &stoplist);
15599 add_cmd ("at", class_breakpoint, stopat_command,
15600 _("Break at a line in the current file."), &stoplist);
15601 add_com ("status", class_info, info_breakpoints_command, _("\
15602 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15603 The \"Type\" column indicates one of:\n\
15604 \tbreakpoint - normal breakpoint\n\
15605 \twatchpoint - watchpoint\n\
15606 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15607 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15608 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15609 address and file/line number respectively.\n\
15610 \n\
15611 Convenience variable \"$_\" and default examine address for \"x\"\n\
15612 are set to the address of the last breakpoint listed unless the command\n\
15613 is prefixed with \"server \".\n\n\
15614 Convenience variable \"$bpnum\" contains the number of the last\n\
15615 breakpoint set."));
15616 }
15617
15618 add_info ("breakpoints", info_breakpoints_command, _("\
15619 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15620 The \"Type\" column indicates one of:\n\
15621 \tbreakpoint - normal breakpoint\n\
15622 \twatchpoint - watchpoint\n\
15623 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15624 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15625 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15626 address and file/line number respectively.\n\
15627 \n\
15628 Convenience variable \"$_\" and default examine address for \"x\"\n\
15629 are set to the address of the last breakpoint listed unless the command\n\
15630 is prefixed with \"server \".\n\n\
15631 Convenience variable \"$bpnum\" contains the number of the last\n\
15632 breakpoint set."));
15633
15634 add_info_alias ("b", "breakpoints", 1);
15635
15636 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15637 Status of all breakpoints, or breakpoint number NUMBER.\n\
15638 The \"Type\" column indicates one of:\n\
15639 \tbreakpoint - normal breakpoint\n\
15640 \twatchpoint - watchpoint\n\
15641 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15642 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15643 \tuntil - internal breakpoint used by the \"until\" command\n\
15644 \tfinish - internal breakpoint used by the \"finish\" command\n\
15645 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15646 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15647 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15648 address and file/line number respectively.\n\
15649 \n\
15650 Convenience variable \"$_\" and default examine address for \"x\"\n\
15651 are set to the address of the last breakpoint listed unless the command\n\
15652 is prefixed with \"server \".\n\n\
15653 Convenience variable \"$bpnum\" contains the number of the last\n\
15654 breakpoint set."),
15655 &maintenanceinfolist);
15656
15657 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15658 Set catchpoints to catch events."),
15659 &catch_cmdlist, "catch ",
15660 0/*allow-unknown*/, &cmdlist);
15661
15662 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15663 Set temporary catchpoints to catch events."),
15664 &tcatch_cmdlist, "tcatch ",
15665 0/*allow-unknown*/, &cmdlist);
15666
15667 add_catch_command ("fork", _("Catch calls to fork."),
15668 catch_fork_command_1,
15669 NULL,
15670 (void *) (uintptr_t) catch_fork_permanent,
15671 (void *) (uintptr_t) catch_fork_temporary);
15672 add_catch_command ("vfork", _("Catch calls to vfork."),
15673 catch_fork_command_1,
15674 NULL,
15675 (void *) (uintptr_t) catch_vfork_permanent,
15676 (void *) (uintptr_t) catch_vfork_temporary);
15677 add_catch_command ("exec", _("Catch calls to exec."),
15678 catch_exec_command_1,
15679 NULL,
15680 CATCH_PERMANENT,
15681 CATCH_TEMPORARY);
15682 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15683 Usage: catch load [REGEX]\n\
15684 If REGEX is given, only stop for libraries matching the regular expression."),
15685 catch_load_command_1,
15686 NULL,
15687 CATCH_PERMANENT,
15688 CATCH_TEMPORARY);
15689 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15690 Usage: catch unload [REGEX]\n\
15691 If REGEX is given, only stop for libraries matching the regular expression."),
15692 catch_unload_command_1,
15693 NULL,
15694 CATCH_PERMANENT,
15695 CATCH_TEMPORARY);
15696
15697 c = add_com ("watch", class_breakpoint, watch_command, _("\
15698 Set a watchpoint for an expression.\n\
15699 Usage: watch [-l|-location] EXPRESSION\n\
15700 A watchpoint stops execution of your program whenever the value of\n\
15701 an expression changes.\n\
15702 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15703 the memory to which it refers."));
15704 set_cmd_completer (c, expression_completer);
15705
15706 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15707 Set a read watchpoint for an expression.\n\
15708 Usage: rwatch [-l|-location] EXPRESSION\n\
15709 A watchpoint stops execution of your program whenever the value of\n\
15710 an expression is read.\n\
15711 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15712 the memory to which it refers."));
15713 set_cmd_completer (c, expression_completer);
15714
15715 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15716 Set a watchpoint for an expression.\n\
15717 Usage: awatch [-l|-location] EXPRESSION\n\
15718 A watchpoint stops execution of your program whenever the value of\n\
15719 an expression is either read or written.\n\
15720 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15721 the memory to which it refers."));
15722 set_cmd_completer (c, expression_completer);
15723
15724 add_info ("watchpoints", info_watchpoints_command, _("\
15725 Status of specified watchpoints (all watchpoints if no argument)."));
15726
15727 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15728 respond to changes - contrary to the description. */
15729 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15730 &can_use_hw_watchpoints, _("\
15731 Set debugger's willingness to use watchpoint hardware."), _("\
15732 Show debugger's willingness to use watchpoint hardware."), _("\
15733 If zero, gdb will not use hardware for new watchpoints, even if\n\
15734 such is available. (However, any hardware watchpoints that were\n\
15735 created before setting this to nonzero, will continue to use watchpoint\n\
15736 hardware.)"),
15737 NULL,
15738 show_can_use_hw_watchpoints,
15739 &setlist, &showlist);
15740
15741 can_use_hw_watchpoints = 1;
15742
15743 /* Tracepoint manipulation commands. */
15744
15745 c = add_com ("trace", class_breakpoint, trace_command, _("\
15746 Set a tracepoint at specified location.\n\
15747 \n"
15748 BREAK_ARGS_HELP ("trace") "\n\
15749 Do \"help tracepoints\" for info on other tracepoint commands."));
15750 set_cmd_completer (c, location_completer);
15751
15752 add_com_alias ("tp", "trace", class_alias, 0);
15753 add_com_alias ("tr", "trace", class_alias, 1);
15754 add_com_alias ("tra", "trace", class_alias, 1);
15755 add_com_alias ("trac", "trace", class_alias, 1);
15756
15757 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15758 Set a fast tracepoint at specified location.\n\
15759 \n"
15760 BREAK_ARGS_HELP ("ftrace") "\n\
15761 Do \"help tracepoints\" for info on other tracepoint commands."));
15762 set_cmd_completer (c, location_completer);
15763
15764 c = add_com ("strace", class_breakpoint, strace_command, _("\
15765 Set a static tracepoint at location or marker.\n\
15766 \n\
15767 strace [LOCATION] [if CONDITION]\n\
15768 LOCATION may be a linespec, explicit, or address location (described below) \n\
15769 or -m MARKER_ID.\n\n\
15770 If a marker id is specified, probe the marker with that name. With\n\
15771 no LOCATION, uses current execution address of the selected stack frame.\n\
15772 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15773 This collects arbitrary user data passed in the probe point call to the\n\
15774 tracing library. You can inspect it when analyzing the trace buffer,\n\
15775 by printing the $_sdata variable like any other convenience variable.\n\
15776 \n\
15777 CONDITION is a boolean expression.\n\
15778 \n" LOCATION_HELP_STRING "\n\n\
15779 Multiple tracepoints at one place are permitted, and useful if their\n\
15780 conditions are different.\n\
15781 \n\
15782 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15783 Do \"help tracepoints\" for info on other tracepoint commands."));
15784 set_cmd_completer (c, location_completer);
15785
15786 add_info ("tracepoints", info_tracepoints_command, _("\
15787 Status of specified tracepoints (all tracepoints if no argument).\n\
15788 Convenience variable \"$tpnum\" contains the number of the\n\
15789 last tracepoint set."));
15790
15791 add_info_alias ("tp", "tracepoints", 1);
15792
15793 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15794 Delete specified tracepoints.\n\
15795 Arguments are tracepoint numbers, separated by spaces.\n\
15796 No argument means delete all tracepoints."),
15797 &deletelist);
15798 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15799
15800 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15801 Disable specified tracepoints.\n\
15802 Arguments are tracepoint numbers, separated by spaces.\n\
15803 No argument means disable all tracepoints."),
15804 &disablelist);
15805 deprecate_cmd (c, "disable");
15806
15807 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15808 Enable specified tracepoints.\n\
15809 Arguments are tracepoint numbers, separated by spaces.\n\
15810 No argument means enable all tracepoints."),
15811 &enablelist);
15812 deprecate_cmd (c, "enable");
15813
15814 add_com ("passcount", class_trace, trace_pass_command, _("\
15815 Set the passcount for a tracepoint.\n\
15816 The trace will end when the tracepoint has been passed 'count' times.\n\
15817 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15818 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15819
15820 add_prefix_cmd ("save", class_breakpoint, save_command,
15821 _("Save breakpoint definitions as a script."),
15822 &save_cmdlist, "save ",
15823 0/*allow-unknown*/, &cmdlist);
15824
15825 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15826 Save current breakpoint definitions as a script.\n\
15827 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15828 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15829 session to restore them."),
15830 &save_cmdlist);
15831 set_cmd_completer (c, filename_completer);
15832
15833 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15834 Save current tracepoint definitions as a script.\n\
15835 Use the 'source' command in another debug session to restore them."),
15836 &save_cmdlist);
15837 set_cmd_completer (c, filename_completer);
15838
15839 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15840 deprecate_cmd (c, "save tracepoints");
15841
15842 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
15843 Breakpoint specific settings.\n\
15844 Configure various breakpoint-specific variables such as\n\
15845 pending breakpoint behavior."),
15846 &breakpoint_set_cmdlist, "set breakpoint ",
15847 0/*allow-unknown*/, &setlist);
15848 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
15849 Breakpoint specific settings.\n\
15850 Configure various breakpoint-specific variables such as\n\
15851 pending breakpoint behavior."),
15852 &breakpoint_show_cmdlist, "show breakpoint ",
15853 0/*allow-unknown*/, &showlist);
15854
15855 add_setshow_auto_boolean_cmd ("pending", no_class,
15856 &pending_break_support, _("\
15857 Set debugger's behavior regarding pending breakpoints."), _("\
15858 Show debugger's behavior regarding pending breakpoints."), _("\
15859 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15860 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15861 an error. If auto, an unrecognized breakpoint location results in a\n\
15862 user-query to see if a pending breakpoint should be created."),
15863 NULL,
15864 show_pending_break_support,
15865 &breakpoint_set_cmdlist,
15866 &breakpoint_show_cmdlist);
15867
15868 pending_break_support = AUTO_BOOLEAN_AUTO;
15869
15870 add_setshow_boolean_cmd ("auto-hw", no_class,
15871 &automatic_hardware_breakpoints, _("\
15872 Set automatic usage of hardware breakpoints."), _("\
15873 Show automatic usage of hardware breakpoints."), _("\
15874 If set, the debugger will automatically use hardware breakpoints for\n\
15875 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15876 a warning will be emitted for such breakpoints."),
15877 NULL,
15878 show_automatic_hardware_breakpoints,
15879 &breakpoint_set_cmdlist,
15880 &breakpoint_show_cmdlist);
15881
15882 add_setshow_boolean_cmd ("always-inserted", class_support,
15883 &always_inserted_mode, _("\
15884 Set mode for inserting breakpoints."), _("\
15885 Show mode for inserting breakpoints."), _("\
15886 When this mode is on, breakpoints are inserted immediately as soon as\n\
15887 they're created, kept inserted even when execution stops, and removed\n\
15888 only when the user deletes them. When this mode is off (the default),\n\
15889 breakpoints are inserted only when execution continues, and removed\n\
15890 when execution stops."),
15891 NULL,
15892 &show_always_inserted_mode,
15893 &breakpoint_set_cmdlist,
15894 &breakpoint_show_cmdlist);
15895
15896 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15897 condition_evaluation_enums,
15898 &condition_evaluation_mode_1, _("\
15899 Set mode of breakpoint condition evaluation."), _("\
15900 Show mode of breakpoint condition evaluation."), _("\
15901 When this is set to \"host\", breakpoint conditions will be\n\
15902 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15903 breakpoint conditions will be downloaded to the target (if the target\n\
15904 supports such feature) and conditions will be evaluated on the target's side.\n\
15905 If this is set to \"auto\" (default), this will be automatically set to\n\
15906 \"target\" if it supports condition evaluation, otherwise it will\n\
15907 be set to \"gdb\""),
15908 &set_condition_evaluation_mode,
15909 &show_condition_evaluation_mode,
15910 &breakpoint_set_cmdlist,
15911 &breakpoint_show_cmdlist);
15912
15913 add_com ("break-range", class_breakpoint, break_range_command, _("\
15914 Set a breakpoint for an address range.\n\
15915 break-range START-LOCATION, END-LOCATION\n\
15916 where START-LOCATION and END-LOCATION can be one of the following:\n\
15917 LINENUM, for that line in the current file,\n\
15918 FILE:LINENUM, for that line in that file,\n\
15919 +OFFSET, for that number of lines after the current line\n\
15920 or the start of the range\n\
15921 FUNCTION, for the first line in that function,\n\
15922 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15923 *ADDRESS, for the instruction at that address.\n\
15924 \n\
15925 The breakpoint will stop execution of the inferior whenever it executes\n\
15926 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15927 range (including START-LOCATION and END-LOCATION)."));
15928
15929 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15930 Set a dynamic printf at specified location.\n\
15931 dprintf location,format string,arg1,arg2,...\n\
15932 location may be a linespec, explicit, or address location.\n"
15933 "\n" LOCATION_HELP_STRING));
15934 set_cmd_completer (c, location_completer);
15935
15936 add_setshow_enum_cmd ("dprintf-style", class_support,
15937 dprintf_style_enums, &dprintf_style, _("\
15938 Set the style of usage for dynamic printf."), _("\
15939 Show the style of usage for dynamic printf."), _("\
15940 This setting chooses how GDB will do a dynamic printf.\n\
15941 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15942 console, as with the \"printf\" command.\n\
15943 If the value is \"call\", the print is done by calling a function in your\n\
15944 program; by default printf(), but you can choose a different function or\n\
15945 output stream by setting dprintf-function and dprintf-channel."),
15946 update_dprintf_commands, NULL,
15947 &setlist, &showlist);
15948
15949 dprintf_function = xstrdup ("printf");
15950 add_setshow_string_cmd ("dprintf-function", class_support,
15951 &dprintf_function, _("\
15952 Set the function to use for dynamic printf."), _("\
15953 Show the function to use for dynamic printf."), NULL,
15954 update_dprintf_commands, NULL,
15955 &setlist, &showlist);
15956
15957 dprintf_channel = xstrdup ("");
15958 add_setshow_string_cmd ("dprintf-channel", class_support,
15959 &dprintf_channel, _("\
15960 Set the channel to use for dynamic printf."), _("\
15961 Show the channel to use for dynamic printf."), NULL,
15962 update_dprintf_commands, NULL,
15963 &setlist, &showlist);
15964
15965 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15966 &disconnected_dprintf, _("\
15967 Set whether dprintf continues after GDB disconnects."), _("\
15968 Show whether dprintf continues after GDB disconnects."), _("\
15969 Use this to let dprintf commands continue to hit and produce output\n\
15970 even if GDB disconnects or detaches from the target."),
15971 NULL,
15972 NULL,
15973 &setlist, &showlist);
15974
15975 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15976 Target agent only formatted printing, like the C \"printf\" function.\n\
15977 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15978 This supports most C printf format specifications, like %s, %d, etc.\n\
15979 This is useful for formatted output in user-defined commands."));
15980
15981 automatic_hardware_breakpoints = true;
15982
15983 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
15984 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
15985 }
This page took 0.393465 seconds and 4 git commands to generate.