Use new %p format suffixes in gdb
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "continuations.h"
63 #include "stack.h"
64 #include "skip.h"
65 #include "ax-gdb.h"
66 #include "dummy-frame.h"
67 #include "interps.h"
68 #include "gdbsupport/format.h"
69 #include "thread-fsm.h"
70 #include "tid-parse.h"
71 #include "cli/cli-style.h"
72 #include "mi/mi-main.h"
73
74 /* readline include files */
75 #include "readline/readline.h"
76 #include "readline/history.h"
77
78 /* readline defines this. */
79 #undef savestring
80
81 #include "mi/mi-common.h"
82 #include "extension.h"
83 #include <algorithm>
84 #include "progspace-and-thread.h"
85 #include "gdbsupport/array-view.h"
86 #include "gdbsupport/gdb_optional.h"
87
88 /* Prototypes for local functions. */
89
90 static void map_breakpoint_numbers (const char *,
91 gdb::function_view<void (breakpoint *)>);
92
93 static void breakpoint_re_set_default (struct breakpoint *);
94
95 static void
96 create_sals_from_location_default (const struct event_location *location,
97 struct linespec_result *canonical,
98 enum bptype type_wanted);
99
100 static void create_breakpoints_sal_default (struct gdbarch *,
101 struct linespec_result *,
102 gdb::unique_xmalloc_ptr<char>,
103 gdb::unique_xmalloc_ptr<char>,
104 enum bptype,
105 enum bpdisp, int, int,
106 int,
107 const struct breakpoint_ops *,
108 int, int, int, unsigned);
109
110 static std::vector<symtab_and_line> decode_location_default
111 (struct breakpoint *b, const struct event_location *location,
112 struct program_space *search_pspace);
113
114 static int can_use_hardware_watchpoint
115 (const std::vector<value_ref_ptr> &vals);
116
117 static void mention (struct breakpoint *);
118
119 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
120 enum bptype,
121 const struct breakpoint_ops *);
122 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
123 const struct symtab_and_line *);
124
125 /* This function is used in gdbtk sources and thus can not be made
126 static. */
127 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
128 struct symtab_and_line,
129 enum bptype,
130 const struct breakpoint_ops *);
131
132 static struct breakpoint *
133 momentary_breakpoint_from_master (struct breakpoint *orig,
134 enum bptype type,
135 const struct breakpoint_ops *ops,
136 int loc_enabled);
137
138 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
139
140 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
141 CORE_ADDR bpaddr,
142 enum bptype bptype);
143
144 static void describe_other_breakpoints (struct gdbarch *,
145 struct program_space *, CORE_ADDR,
146 struct obj_section *, int);
147
148 static int watchpoint_locations_match (struct bp_location *loc1,
149 struct bp_location *loc2);
150
151 static int breakpoint_location_address_match (struct bp_location *bl,
152 const struct address_space *aspace,
153 CORE_ADDR addr);
154
155 static int breakpoint_location_address_range_overlap (struct bp_location *,
156 const address_space *,
157 CORE_ADDR, int);
158
159 static int remove_breakpoint (struct bp_location *);
160 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
161
162 static enum print_stop_action print_bp_stop_message (bpstat bs);
163
164 static int hw_breakpoint_used_count (void);
165
166 static int hw_watchpoint_use_count (struct breakpoint *);
167
168 static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
171
172 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
174
175 static void free_bp_location (struct bp_location *loc);
176 static void incref_bp_location (struct bp_location *loc);
177 static void decref_bp_location (struct bp_location **loc);
178
179 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
180
181 /* update_global_location_list's modes of operation wrt to whether to
182 insert locations now. */
183 enum ugll_insert_mode
184 {
185 /* Don't insert any breakpoint locations into the inferior, only
186 remove already-inserted locations that no longer should be
187 inserted. Functions that delete a breakpoint or breakpoints
188 should specify this mode, so that deleting a breakpoint doesn't
189 have the side effect of inserting the locations of other
190 breakpoints that are marked not-inserted, but should_be_inserted
191 returns true on them.
192
193 This behavior is useful is situations close to tear-down -- e.g.,
194 after an exec, while the target still has execution, but
195 breakpoint shadows of the previous executable image should *NOT*
196 be restored to the new image; or before detaching, where the
197 target still has execution and wants to delete breakpoints from
198 GDB's lists, and all breakpoints had already been removed from
199 the inferior. */
200 UGLL_DONT_INSERT,
201
202 /* May insert breakpoints iff breakpoints_should_be_inserted_now
203 claims breakpoints should be inserted now. */
204 UGLL_MAY_INSERT,
205
206 /* Insert locations now, irrespective of
207 breakpoints_should_be_inserted_now. E.g., say all threads are
208 stopped right now, and the user did "continue". We need to
209 insert breakpoints _before_ resuming the target, but
210 UGLL_MAY_INSERT wouldn't insert them, because
211 breakpoints_should_be_inserted_now returns false at that point,
212 as no thread is running yet. */
213 UGLL_INSERT
214 };
215
216 static void update_global_location_list (enum ugll_insert_mode);
217
218 static void update_global_location_list_nothrow (enum ugll_insert_mode);
219
220 static void insert_breakpoint_locations (void);
221
222 static void trace_pass_command (const char *, int);
223
224 static void set_tracepoint_count (int num);
225
226 static bool is_masked_watchpoint (const struct breakpoint *b);
227
228 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
229
230 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
231 otherwise. */
232
233 static int strace_marker_p (struct breakpoint *b);
234
235 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
236 that are implemented on top of software or hardware breakpoints
237 (user breakpoints, internal and momentary breakpoints, etc.). */
238 static struct breakpoint_ops bkpt_base_breakpoint_ops;
239
240 /* Internal breakpoints class type. */
241 static struct breakpoint_ops internal_breakpoint_ops;
242
243 /* Momentary breakpoints class type. */
244 static struct breakpoint_ops momentary_breakpoint_ops;
245
246 /* The breakpoint_ops structure to be used in regular user created
247 breakpoints. */
248 struct breakpoint_ops bkpt_breakpoint_ops;
249
250 /* Breakpoints set on probes. */
251 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
252
253 /* Dynamic printf class type. */
254 struct breakpoint_ops dprintf_breakpoint_ops;
255
256 /* The style in which to perform a dynamic printf. This is a user
257 option because different output options have different tradeoffs;
258 if GDB does the printing, there is better error handling if there
259 is a problem with any of the arguments, but using an inferior
260 function lets you have special-purpose printers and sending of
261 output to the same place as compiled-in print functions. */
262
263 static const char dprintf_style_gdb[] = "gdb";
264 static const char dprintf_style_call[] = "call";
265 static const char dprintf_style_agent[] = "agent";
266 static const char *const dprintf_style_enums[] = {
267 dprintf_style_gdb,
268 dprintf_style_call,
269 dprintf_style_agent,
270 NULL
271 };
272 static const char *dprintf_style = dprintf_style_gdb;
273
274 /* The function to use for dynamic printf if the preferred style is to
275 call into the inferior. The value is simply a string that is
276 copied into the command, so it can be anything that GDB can
277 evaluate to a callable address, not necessarily a function name. */
278
279 static char *dprintf_function;
280
281 /* The channel to use for dynamic printf if the preferred style is to
282 call into the inferior; if a nonempty string, it will be passed to
283 the call as the first argument, with the format string as the
284 second. As with the dprintf function, this can be anything that
285 GDB knows how to evaluate, so in addition to common choices like
286 "stderr", this could be an app-specific expression like
287 "mystreams[curlogger]". */
288
289 static char *dprintf_channel;
290
291 /* True if dprintf commands should continue to operate even if GDB
292 has disconnected. */
293 static bool disconnected_dprintf = true;
294
295 struct command_line *
296 breakpoint_commands (struct breakpoint *b)
297 {
298 return b->commands ? b->commands.get () : NULL;
299 }
300
301 /* Flag indicating that a command has proceeded the inferior past the
302 current breakpoint. */
303
304 static bool breakpoint_proceeded;
305
306 const char *
307 bpdisp_text (enum bpdisp disp)
308 {
309 /* NOTE: the following values are a part of MI protocol and
310 represent values of 'disp' field returned when inferior stops at
311 a breakpoint. */
312 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
313
314 return bpdisps[(int) disp];
315 }
316
317 /* Prototypes for exported functions. */
318 /* If FALSE, gdb will not use hardware support for watchpoints, even
319 if such is available. */
320 static int can_use_hw_watchpoints;
321
322 static void
323 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
324 struct cmd_list_element *c,
325 const char *value)
326 {
327 fprintf_filtered (file,
328 _("Debugger's willingness to use "
329 "watchpoint hardware is %s.\n"),
330 value);
331 }
332
333 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
334 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
335 for unrecognized breakpoint locations.
336 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
337 static enum auto_boolean pending_break_support;
338 static void
339 show_pending_break_support (struct ui_file *file, int from_tty,
340 struct cmd_list_element *c,
341 const char *value)
342 {
343 fprintf_filtered (file,
344 _("Debugger's behavior regarding "
345 "pending breakpoints is %s.\n"),
346 value);
347 }
348
349 /* If true, gdb will automatically use hardware breakpoints for breakpoints
350 set with "break" but falling in read-only memory.
351 If false, gdb will warn about such breakpoints, but won't automatically
352 use hardware breakpoints. */
353 static bool automatic_hardware_breakpoints;
354 static void
355 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
356 struct cmd_list_element *c,
357 const char *value)
358 {
359 fprintf_filtered (file,
360 _("Automatic usage of hardware breakpoints is %s.\n"),
361 value);
362 }
363
364 /* If on, GDB keeps breakpoints inserted even if the inferior is
365 stopped, and immediately inserts any new breakpoints as soon as
366 they're created. If off (default), GDB keeps breakpoints off of
367 the target as long as possible. That is, it delays inserting
368 breakpoints until the next resume, and removes them again when the
369 target fully stops. This is a bit safer in case GDB crashes while
370 processing user input. */
371 static bool always_inserted_mode = false;
372
373 static void
374 show_always_inserted_mode (struct ui_file *file, int from_tty,
375 struct cmd_list_element *c, const char *value)
376 {
377 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
378 value);
379 }
380
381 /* See breakpoint.h. */
382
383 int
384 breakpoints_should_be_inserted_now (void)
385 {
386 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
387 {
388 /* If breakpoints are global, they should be inserted even if no
389 thread under gdb's control is running, or even if there are
390 no threads under GDB's control yet. */
391 return 1;
392 }
393 else if (target_has_execution)
394 {
395 if (always_inserted_mode)
396 {
397 /* The user wants breakpoints inserted even if all threads
398 are stopped. */
399 return 1;
400 }
401
402 if (threads_are_executing ())
403 return 1;
404
405 /* Don't remove breakpoints yet if, even though all threads are
406 stopped, we still have events to process. */
407 for (thread_info *tp : all_non_exited_threads ())
408 if (tp->resumed
409 && tp->suspend.waitstatus_pending_p)
410 return 1;
411 }
412 return 0;
413 }
414
415 static const char condition_evaluation_both[] = "host or target";
416
417 /* Modes for breakpoint condition evaluation. */
418 static const char condition_evaluation_auto[] = "auto";
419 static const char condition_evaluation_host[] = "host";
420 static const char condition_evaluation_target[] = "target";
421 static const char *const condition_evaluation_enums[] = {
422 condition_evaluation_auto,
423 condition_evaluation_host,
424 condition_evaluation_target,
425 NULL
426 };
427
428 /* Global that holds the current mode for breakpoint condition evaluation. */
429 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
430
431 /* Global that we use to display information to the user (gets its value from
432 condition_evaluation_mode_1. */
433 static const char *condition_evaluation_mode = condition_evaluation_auto;
434
435 /* Translate a condition evaluation mode MODE into either "host"
436 or "target". This is used mostly to translate from "auto" to the
437 real setting that is being used. It returns the translated
438 evaluation mode. */
439
440 static const char *
441 translate_condition_evaluation_mode (const char *mode)
442 {
443 if (mode == condition_evaluation_auto)
444 {
445 if (target_supports_evaluation_of_breakpoint_conditions ())
446 return condition_evaluation_target;
447 else
448 return condition_evaluation_host;
449 }
450 else
451 return mode;
452 }
453
454 /* Discovers what condition_evaluation_auto translates to. */
455
456 static const char *
457 breakpoint_condition_evaluation_mode (void)
458 {
459 return translate_condition_evaluation_mode (condition_evaluation_mode);
460 }
461
462 /* Return true if GDB should evaluate breakpoint conditions or false
463 otherwise. */
464
465 static int
466 gdb_evaluates_breakpoint_condition_p (void)
467 {
468 const char *mode = breakpoint_condition_evaluation_mode ();
469
470 return (mode == condition_evaluation_host);
471 }
472
473 /* Are we executing breakpoint commands? */
474 static int executing_breakpoint_commands;
475
476 /* Are overlay event breakpoints enabled? */
477 static int overlay_events_enabled;
478
479 /* See description in breakpoint.h. */
480 bool target_exact_watchpoints = false;
481
482 /* Walk the following statement or block through all breakpoints.
483 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
484 current breakpoint. */
485
486 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
487
488 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
489 for (B = breakpoint_chain; \
490 B ? (TMP=B->next, 1): 0; \
491 B = TMP)
492
493 /* Similar iterator for the low-level breakpoints. SAFE variant is
494 not provided so update_global_location_list must not be called
495 while executing the block of ALL_BP_LOCATIONS. */
496
497 #define ALL_BP_LOCATIONS(B,BP_TMP) \
498 for (BP_TMP = bp_locations; \
499 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
500 BP_TMP++)
501
502 /* Iterates through locations with address ADDRESS for the currently selected
503 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
504 to where the loop should start from.
505 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
506 appropriate location to start with. */
507
508 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
509 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
510 BP_LOCP_TMP = BP_LOCP_START; \
511 BP_LOCP_START \
512 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
513 && (*BP_LOCP_TMP)->address == ADDRESS); \
514 BP_LOCP_TMP++)
515
516 /* Iterator for tracepoints only. */
517
518 #define ALL_TRACEPOINTS(B) \
519 for (B = breakpoint_chain; B; B = B->next) \
520 if (is_tracepoint (B))
521
522 /* Chains of all breakpoints defined. */
523
524 struct breakpoint *breakpoint_chain;
525
526 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
527
528 static struct bp_location **bp_locations;
529
530 /* Number of elements of BP_LOCATIONS. */
531
532 static unsigned bp_locations_count;
533
534 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
535 ADDRESS for the current elements of BP_LOCATIONS which get a valid
536 result from bp_location_has_shadow. You can use it for roughly
537 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
538 an address you need to read. */
539
540 static CORE_ADDR bp_locations_placed_address_before_address_max;
541
542 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
543 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
544 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
545 You can use it for roughly limiting the subrange of BP_LOCATIONS to
546 scan for shadow bytes for an address you need to read. */
547
548 static CORE_ADDR bp_locations_shadow_len_after_address_max;
549
550 /* The locations that no longer correspond to any breakpoint, unlinked
551 from the bp_locations array, but for which a hit may still be
552 reported by a target. */
553 static std::vector<bp_location *> moribund_locations;
554
555 /* Number of last breakpoint made. */
556
557 static int breakpoint_count;
558
559 /* The value of `breakpoint_count' before the last command that
560 created breakpoints. If the last (break-like) command created more
561 than one breakpoint, then the difference between BREAKPOINT_COUNT
562 and PREV_BREAKPOINT_COUNT is more than one. */
563 static int prev_breakpoint_count;
564
565 /* Number of last tracepoint made. */
566
567 static int tracepoint_count;
568
569 static struct cmd_list_element *breakpoint_set_cmdlist;
570 static struct cmd_list_element *breakpoint_show_cmdlist;
571 struct cmd_list_element *save_cmdlist;
572
573 /* See declaration at breakpoint.h. */
574
575 struct breakpoint *
576 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
577 void *user_data)
578 {
579 struct breakpoint *b = NULL;
580
581 ALL_BREAKPOINTS (b)
582 {
583 if (func (b, user_data) != 0)
584 break;
585 }
586
587 return b;
588 }
589
590 /* Return whether a breakpoint is an active enabled breakpoint. */
591 static int
592 breakpoint_enabled (struct breakpoint *b)
593 {
594 return (b->enable_state == bp_enabled);
595 }
596
597 /* Set breakpoint count to NUM. */
598
599 static void
600 set_breakpoint_count (int num)
601 {
602 prev_breakpoint_count = breakpoint_count;
603 breakpoint_count = num;
604 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
605 }
606
607 /* Used by `start_rbreak_breakpoints' below, to record the current
608 breakpoint count before "rbreak" creates any breakpoint. */
609 static int rbreak_start_breakpoint_count;
610
611 /* Called at the start an "rbreak" command to record the first
612 breakpoint made. */
613
614 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
615 {
616 rbreak_start_breakpoint_count = breakpoint_count;
617 }
618
619 /* Called at the end of an "rbreak" command to record the last
620 breakpoint made. */
621
622 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
623 {
624 prev_breakpoint_count = rbreak_start_breakpoint_count;
625 }
626
627 /* Used in run_command to zero the hit count when a new run starts. */
628
629 void
630 clear_breakpoint_hit_counts (void)
631 {
632 struct breakpoint *b;
633
634 ALL_BREAKPOINTS (b)
635 b->hit_count = 0;
636 }
637
638 \f
639 /* Return the breakpoint with the specified number, or NULL
640 if the number does not refer to an existing breakpoint. */
641
642 struct breakpoint *
643 get_breakpoint (int num)
644 {
645 struct breakpoint *b;
646
647 ALL_BREAKPOINTS (b)
648 if (b->number == num)
649 return b;
650
651 return NULL;
652 }
653
654 \f
655
656 /* Mark locations as "conditions have changed" in case the target supports
657 evaluating conditions on its side. */
658
659 static void
660 mark_breakpoint_modified (struct breakpoint *b)
661 {
662 struct bp_location *loc;
663
664 /* This is only meaningful if the target is
665 evaluating conditions and if the user has
666 opted for condition evaluation on the target's
667 side. */
668 if (gdb_evaluates_breakpoint_condition_p ()
669 || !target_supports_evaluation_of_breakpoint_conditions ())
670 return;
671
672 if (!is_breakpoint (b))
673 return;
674
675 for (loc = b->loc; loc; loc = loc->next)
676 loc->condition_changed = condition_modified;
677 }
678
679 /* Mark location as "conditions have changed" in case the target supports
680 evaluating conditions on its side. */
681
682 static void
683 mark_breakpoint_location_modified (struct bp_location *loc)
684 {
685 /* This is only meaningful if the target is
686 evaluating conditions and if the user has
687 opted for condition evaluation on the target's
688 side. */
689 if (gdb_evaluates_breakpoint_condition_p ()
690 || !target_supports_evaluation_of_breakpoint_conditions ())
691
692 return;
693
694 if (!is_breakpoint (loc->owner))
695 return;
696
697 loc->condition_changed = condition_modified;
698 }
699
700 /* Sets the condition-evaluation mode using the static global
701 condition_evaluation_mode. */
702
703 static void
704 set_condition_evaluation_mode (const char *args, int from_tty,
705 struct cmd_list_element *c)
706 {
707 const char *old_mode, *new_mode;
708
709 if ((condition_evaluation_mode_1 == condition_evaluation_target)
710 && !target_supports_evaluation_of_breakpoint_conditions ())
711 {
712 condition_evaluation_mode_1 = condition_evaluation_mode;
713 warning (_("Target does not support breakpoint condition evaluation.\n"
714 "Using host evaluation mode instead."));
715 return;
716 }
717
718 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
719 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
720
721 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
722 settings was "auto". */
723 condition_evaluation_mode = condition_evaluation_mode_1;
724
725 /* Only update the mode if the user picked a different one. */
726 if (new_mode != old_mode)
727 {
728 struct bp_location *loc, **loc_tmp;
729 /* If the user switched to a different evaluation mode, we
730 need to synch the changes with the target as follows:
731
732 "host" -> "target": Send all (valid) conditions to the target.
733 "target" -> "host": Remove all the conditions from the target.
734 */
735
736 if (new_mode == condition_evaluation_target)
737 {
738 /* Mark everything modified and synch conditions with the
739 target. */
740 ALL_BP_LOCATIONS (loc, loc_tmp)
741 mark_breakpoint_location_modified (loc);
742 }
743 else
744 {
745 /* Manually mark non-duplicate locations to synch conditions
746 with the target. We do this to remove all the conditions the
747 target knows about. */
748 ALL_BP_LOCATIONS (loc, loc_tmp)
749 if (is_breakpoint (loc->owner) && loc->inserted)
750 loc->needs_update = 1;
751 }
752
753 /* Do the update. */
754 update_global_location_list (UGLL_MAY_INSERT);
755 }
756
757 return;
758 }
759
760 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
761 what "auto" is translating to. */
762
763 static void
764 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
765 struct cmd_list_element *c, const char *value)
766 {
767 if (condition_evaluation_mode == condition_evaluation_auto)
768 fprintf_filtered (file,
769 _("Breakpoint condition evaluation "
770 "mode is %s (currently %s).\n"),
771 value,
772 breakpoint_condition_evaluation_mode ());
773 else
774 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
775 value);
776 }
777
778 /* A comparison function for bp_location AP and BP that is used by
779 bsearch. This comparison function only cares about addresses, unlike
780 the more general bp_locations_compare function. */
781
782 static int
783 bp_locations_compare_addrs (const void *ap, const void *bp)
784 {
785 const struct bp_location *a = *(const struct bp_location **) ap;
786 const struct bp_location *b = *(const struct bp_location **) bp;
787
788 if (a->address == b->address)
789 return 0;
790 else
791 return ((a->address > b->address) - (a->address < b->address));
792 }
793
794 /* Helper function to skip all bp_locations with addresses
795 less than ADDRESS. It returns the first bp_location that
796 is greater than or equal to ADDRESS. If none is found, just
797 return NULL. */
798
799 static struct bp_location **
800 get_first_locp_gte_addr (CORE_ADDR address)
801 {
802 struct bp_location dummy_loc;
803 struct bp_location *dummy_locp = &dummy_loc;
804 struct bp_location **locp_found = NULL;
805
806 /* Initialize the dummy location's address field. */
807 dummy_loc.address = address;
808
809 /* Find a close match to the first location at ADDRESS. */
810 locp_found = ((struct bp_location **)
811 bsearch (&dummy_locp, bp_locations, bp_locations_count,
812 sizeof (struct bp_location **),
813 bp_locations_compare_addrs));
814
815 /* Nothing was found, nothing left to do. */
816 if (locp_found == NULL)
817 return NULL;
818
819 /* We may have found a location that is at ADDRESS but is not the first in the
820 location's list. Go backwards (if possible) and locate the first one. */
821 while ((locp_found - 1) >= bp_locations
822 && (*(locp_found - 1))->address == address)
823 locp_found--;
824
825 return locp_found;
826 }
827
828 void
829 set_breakpoint_condition (struct breakpoint *b, const char *exp,
830 int from_tty)
831 {
832 xfree (b->cond_string);
833 b->cond_string = NULL;
834
835 if (is_watchpoint (b))
836 {
837 struct watchpoint *w = (struct watchpoint *) b;
838
839 w->cond_exp.reset ();
840 }
841 else
842 {
843 struct bp_location *loc;
844
845 for (loc = b->loc; loc; loc = loc->next)
846 {
847 loc->cond.reset ();
848
849 /* No need to free the condition agent expression
850 bytecode (if we have one). We will handle this
851 when we go through update_global_location_list. */
852 }
853 }
854
855 if (*exp == 0)
856 {
857 if (from_tty)
858 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
859 }
860 else
861 {
862 const char *arg = exp;
863
864 /* I don't know if it matters whether this is the string the user
865 typed in or the decompiled expression. */
866 b->cond_string = xstrdup (arg);
867 b->condition_not_parsed = 0;
868
869 if (is_watchpoint (b))
870 {
871 struct watchpoint *w = (struct watchpoint *) b;
872
873 innermost_block_tracker tracker;
874 arg = exp;
875 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
876 if (*arg)
877 error (_("Junk at end of expression"));
878 w->cond_exp_valid_block = tracker.block ();
879 }
880 else
881 {
882 struct bp_location *loc;
883
884 for (loc = b->loc; loc; loc = loc->next)
885 {
886 arg = exp;
887 loc->cond =
888 parse_exp_1 (&arg, loc->address,
889 block_for_pc (loc->address), 0);
890 if (*arg)
891 error (_("Junk at end of expression"));
892 }
893 }
894 }
895 mark_breakpoint_modified (b);
896
897 gdb::observers::breakpoint_modified.notify (b);
898 }
899
900 /* Completion for the "condition" command. */
901
902 static void
903 condition_completer (struct cmd_list_element *cmd,
904 completion_tracker &tracker,
905 const char *text, const char *word)
906 {
907 const char *space;
908
909 text = skip_spaces (text);
910 space = skip_to_space (text);
911 if (*space == '\0')
912 {
913 int len;
914 struct breakpoint *b;
915
916 if (text[0] == '$')
917 {
918 /* We don't support completion of history indices. */
919 if (!isdigit (text[1]))
920 complete_internalvar (tracker, &text[1]);
921 return;
922 }
923
924 /* We're completing the breakpoint number. */
925 len = strlen (text);
926
927 ALL_BREAKPOINTS (b)
928 {
929 char number[50];
930
931 xsnprintf (number, sizeof (number), "%d", b->number);
932
933 if (strncmp (number, text, len) == 0)
934 tracker.add_completion (make_unique_xstrdup (number));
935 }
936
937 return;
938 }
939
940 /* We're completing the expression part. */
941 text = skip_spaces (space);
942 expression_completer (cmd, tracker, text, word);
943 }
944
945 /* condition N EXP -- set break condition of breakpoint N to EXP. */
946
947 static void
948 condition_command (const char *arg, int from_tty)
949 {
950 struct breakpoint *b;
951 const char *p;
952 int bnum;
953
954 if (arg == 0)
955 error_no_arg (_("breakpoint number"));
956
957 p = arg;
958 bnum = get_number (&p);
959 if (bnum == 0)
960 error (_("Bad breakpoint argument: '%s'"), arg);
961
962 ALL_BREAKPOINTS (b)
963 if (b->number == bnum)
964 {
965 /* Check if this breakpoint has a "stop" method implemented in an
966 extension language. This method and conditions entered into GDB
967 from the CLI are mutually exclusive. */
968 const struct extension_language_defn *extlang
969 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
970
971 if (extlang != NULL)
972 {
973 error (_("Only one stop condition allowed. There is currently"
974 " a %s stop condition defined for this breakpoint."),
975 ext_lang_capitalized_name (extlang));
976 }
977 set_breakpoint_condition (b, p, from_tty);
978
979 if (is_breakpoint (b))
980 update_global_location_list (UGLL_MAY_INSERT);
981
982 return;
983 }
984
985 error (_("No breakpoint number %d."), bnum);
986 }
987
988 /* Check that COMMAND do not contain commands that are suitable
989 only for tracepoints and not suitable for ordinary breakpoints.
990 Throw if any such commands is found. */
991
992 static void
993 check_no_tracepoint_commands (struct command_line *commands)
994 {
995 struct command_line *c;
996
997 for (c = commands; c; c = c->next)
998 {
999 if (c->control_type == while_stepping_control)
1000 error (_("The 'while-stepping' command can "
1001 "only be used for tracepoints"));
1002
1003 check_no_tracepoint_commands (c->body_list_0.get ());
1004 check_no_tracepoint_commands (c->body_list_1.get ());
1005
1006 /* Not that command parsing removes leading whitespace and comment
1007 lines and also empty lines. So, we only need to check for
1008 command directly. */
1009 if (strstr (c->line, "collect ") == c->line)
1010 error (_("The 'collect' command can only be used for tracepoints"));
1011
1012 if (strstr (c->line, "teval ") == c->line)
1013 error (_("The 'teval' command can only be used for tracepoints"));
1014 }
1015 }
1016
1017 struct longjmp_breakpoint : public breakpoint
1018 {
1019 ~longjmp_breakpoint () override;
1020 };
1021
1022 /* Encapsulate tests for different types of tracepoints. */
1023
1024 static bool
1025 is_tracepoint_type (bptype type)
1026 {
1027 return (type == bp_tracepoint
1028 || type == bp_fast_tracepoint
1029 || type == bp_static_tracepoint);
1030 }
1031
1032 static bool
1033 is_longjmp_type (bptype type)
1034 {
1035 return type == bp_longjmp || type == bp_exception;
1036 }
1037
1038 /* See breakpoint.h. */
1039
1040 bool
1041 is_tracepoint (const struct breakpoint *b)
1042 {
1043 return is_tracepoint_type (b->type);
1044 }
1045
1046 /* Factory function to create an appropriate instance of breakpoint given
1047 TYPE. */
1048
1049 static std::unique_ptr<breakpoint>
1050 new_breakpoint_from_type (bptype type)
1051 {
1052 breakpoint *b;
1053
1054 if (is_tracepoint_type (type))
1055 b = new tracepoint ();
1056 else if (is_longjmp_type (type))
1057 b = new longjmp_breakpoint ();
1058 else
1059 b = new breakpoint ();
1060
1061 return std::unique_ptr<breakpoint> (b);
1062 }
1063
1064 /* A helper function that validates that COMMANDS are valid for a
1065 breakpoint. This function will throw an exception if a problem is
1066 found. */
1067
1068 static void
1069 validate_commands_for_breakpoint (struct breakpoint *b,
1070 struct command_line *commands)
1071 {
1072 if (is_tracepoint (b))
1073 {
1074 struct tracepoint *t = (struct tracepoint *) b;
1075 struct command_line *c;
1076 struct command_line *while_stepping = 0;
1077
1078 /* Reset the while-stepping step count. The previous commands
1079 might have included a while-stepping action, while the new
1080 ones might not. */
1081 t->step_count = 0;
1082
1083 /* We need to verify that each top-level element of commands is
1084 valid for tracepoints, that there's at most one
1085 while-stepping element, and that the while-stepping's body
1086 has valid tracing commands excluding nested while-stepping.
1087 We also need to validate the tracepoint action line in the
1088 context of the tracepoint --- validate_actionline actually
1089 has side effects, like setting the tracepoint's
1090 while-stepping STEP_COUNT, in addition to checking if the
1091 collect/teval actions parse and make sense in the
1092 tracepoint's context. */
1093 for (c = commands; c; c = c->next)
1094 {
1095 if (c->control_type == while_stepping_control)
1096 {
1097 if (b->type == bp_fast_tracepoint)
1098 error (_("The 'while-stepping' command "
1099 "cannot be used for fast tracepoint"));
1100 else if (b->type == bp_static_tracepoint)
1101 error (_("The 'while-stepping' command "
1102 "cannot be used for static tracepoint"));
1103
1104 if (while_stepping)
1105 error (_("The 'while-stepping' command "
1106 "can be used only once"));
1107 else
1108 while_stepping = c;
1109 }
1110
1111 validate_actionline (c->line, b);
1112 }
1113 if (while_stepping)
1114 {
1115 struct command_line *c2;
1116
1117 gdb_assert (while_stepping->body_list_1 == nullptr);
1118 c2 = while_stepping->body_list_0.get ();
1119 for (; c2; c2 = c2->next)
1120 {
1121 if (c2->control_type == while_stepping_control)
1122 error (_("The 'while-stepping' command cannot be nested"));
1123 }
1124 }
1125 }
1126 else
1127 {
1128 check_no_tracepoint_commands (commands);
1129 }
1130 }
1131
1132 /* Return a vector of all the static tracepoints set at ADDR. The
1133 caller is responsible for releasing the vector. */
1134
1135 std::vector<breakpoint *>
1136 static_tracepoints_here (CORE_ADDR addr)
1137 {
1138 struct breakpoint *b;
1139 std::vector<breakpoint *> found;
1140 struct bp_location *loc;
1141
1142 ALL_BREAKPOINTS (b)
1143 if (b->type == bp_static_tracepoint)
1144 {
1145 for (loc = b->loc; loc; loc = loc->next)
1146 if (loc->address == addr)
1147 found.push_back (b);
1148 }
1149
1150 return found;
1151 }
1152
1153 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1154 validate that only allowed commands are included. */
1155
1156 void
1157 breakpoint_set_commands (struct breakpoint *b,
1158 counted_command_line &&commands)
1159 {
1160 validate_commands_for_breakpoint (b, commands.get ());
1161
1162 b->commands = std::move (commands);
1163 gdb::observers::breakpoint_modified.notify (b);
1164 }
1165
1166 /* Set the internal `silent' flag on the breakpoint. Note that this
1167 is not the same as the "silent" that may appear in the breakpoint's
1168 commands. */
1169
1170 void
1171 breakpoint_set_silent (struct breakpoint *b, int silent)
1172 {
1173 int old_silent = b->silent;
1174
1175 b->silent = silent;
1176 if (old_silent != silent)
1177 gdb::observers::breakpoint_modified.notify (b);
1178 }
1179
1180 /* Set the thread for this breakpoint. If THREAD is -1, make the
1181 breakpoint work for any thread. */
1182
1183 void
1184 breakpoint_set_thread (struct breakpoint *b, int thread)
1185 {
1186 int old_thread = b->thread;
1187
1188 b->thread = thread;
1189 if (old_thread != thread)
1190 gdb::observers::breakpoint_modified.notify (b);
1191 }
1192
1193 /* Set the task for this breakpoint. If TASK is 0, make the
1194 breakpoint work for any task. */
1195
1196 void
1197 breakpoint_set_task (struct breakpoint *b, int task)
1198 {
1199 int old_task = b->task;
1200
1201 b->task = task;
1202 if (old_task != task)
1203 gdb::observers::breakpoint_modified.notify (b);
1204 }
1205
1206 static void
1207 commands_command_1 (const char *arg, int from_tty,
1208 struct command_line *control)
1209 {
1210 counted_command_line cmd;
1211 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1212 NULL after the call to read_command_lines if the user provides an empty
1213 list of command by just typing "end". */
1214 bool cmd_read = false;
1215
1216 std::string new_arg;
1217
1218 if (arg == NULL || !*arg)
1219 {
1220 if (breakpoint_count - prev_breakpoint_count > 1)
1221 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1222 breakpoint_count);
1223 else if (breakpoint_count > 0)
1224 new_arg = string_printf ("%d", breakpoint_count);
1225 arg = new_arg.c_str ();
1226 }
1227
1228 map_breakpoint_numbers
1229 (arg, [&] (breakpoint *b)
1230 {
1231 if (!cmd_read)
1232 {
1233 gdb_assert (cmd == NULL);
1234 if (control != NULL)
1235 cmd = control->body_list_0;
1236 else
1237 {
1238 std::string str
1239 = string_printf (_("Type commands for breakpoint(s) "
1240 "%s, one per line."),
1241 arg);
1242
1243 auto do_validate = [=] (const char *line)
1244 {
1245 validate_actionline (line, b);
1246 };
1247 gdb::function_view<void (const char *)> validator;
1248 if (is_tracepoint (b))
1249 validator = do_validate;
1250
1251 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1252 }
1253 cmd_read = true;
1254 }
1255
1256 /* If a breakpoint was on the list more than once, we don't need to
1257 do anything. */
1258 if (b->commands != cmd)
1259 {
1260 validate_commands_for_breakpoint (b, cmd.get ());
1261 b->commands = cmd;
1262 gdb::observers::breakpoint_modified.notify (b);
1263 }
1264 });
1265 }
1266
1267 static void
1268 commands_command (const char *arg, int from_tty)
1269 {
1270 commands_command_1 (arg, from_tty, NULL);
1271 }
1272
1273 /* Like commands_command, but instead of reading the commands from
1274 input stream, takes them from an already parsed command structure.
1275
1276 This is used by cli-script.c to DTRT with breakpoint commands
1277 that are part of if and while bodies. */
1278 enum command_control_type
1279 commands_from_control_command (const char *arg, struct command_line *cmd)
1280 {
1281 commands_command_1 (arg, 0, cmd);
1282 return simple_control;
1283 }
1284
1285 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1286
1287 static int
1288 bp_location_has_shadow (struct bp_location *bl)
1289 {
1290 if (bl->loc_type != bp_loc_software_breakpoint)
1291 return 0;
1292 if (!bl->inserted)
1293 return 0;
1294 if (bl->target_info.shadow_len == 0)
1295 /* BL isn't valid, or doesn't shadow memory. */
1296 return 0;
1297 return 1;
1298 }
1299
1300 /* Update BUF, which is LEN bytes read from the target address
1301 MEMADDR, by replacing a memory breakpoint with its shadowed
1302 contents.
1303
1304 If READBUF is not NULL, this buffer must not overlap with the of
1305 the breakpoint location's shadow_contents buffer. Otherwise, a
1306 failed assertion internal error will be raised. */
1307
1308 static void
1309 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1310 const gdb_byte *writebuf_org,
1311 ULONGEST memaddr, LONGEST len,
1312 struct bp_target_info *target_info,
1313 struct gdbarch *gdbarch)
1314 {
1315 /* Now do full processing of the found relevant range of elements. */
1316 CORE_ADDR bp_addr = 0;
1317 int bp_size = 0;
1318 int bptoffset = 0;
1319
1320 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1321 current_program_space->aspace, 0))
1322 {
1323 /* The breakpoint is inserted in a different address space. */
1324 return;
1325 }
1326
1327 /* Addresses and length of the part of the breakpoint that
1328 we need to copy. */
1329 bp_addr = target_info->placed_address;
1330 bp_size = target_info->shadow_len;
1331
1332 if (bp_addr + bp_size <= memaddr)
1333 {
1334 /* The breakpoint is entirely before the chunk of memory we are
1335 reading. */
1336 return;
1337 }
1338
1339 if (bp_addr >= memaddr + len)
1340 {
1341 /* The breakpoint is entirely after the chunk of memory we are
1342 reading. */
1343 return;
1344 }
1345
1346 /* Offset within shadow_contents. */
1347 if (bp_addr < memaddr)
1348 {
1349 /* Only copy the second part of the breakpoint. */
1350 bp_size -= memaddr - bp_addr;
1351 bptoffset = memaddr - bp_addr;
1352 bp_addr = memaddr;
1353 }
1354
1355 if (bp_addr + bp_size > memaddr + len)
1356 {
1357 /* Only copy the first part of the breakpoint. */
1358 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1359 }
1360
1361 if (readbuf != NULL)
1362 {
1363 /* Verify that the readbuf buffer does not overlap with the
1364 shadow_contents buffer. */
1365 gdb_assert (target_info->shadow_contents >= readbuf + len
1366 || readbuf >= (target_info->shadow_contents
1367 + target_info->shadow_len));
1368
1369 /* Update the read buffer with this inserted breakpoint's
1370 shadow. */
1371 memcpy (readbuf + bp_addr - memaddr,
1372 target_info->shadow_contents + bptoffset, bp_size);
1373 }
1374 else
1375 {
1376 const unsigned char *bp;
1377 CORE_ADDR addr = target_info->reqstd_address;
1378 int placed_size;
1379
1380 /* Update the shadow with what we want to write to memory. */
1381 memcpy (target_info->shadow_contents + bptoffset,
1382 writebuf_org + bp_addr - memaddr, bp_size);
1383
1384 /* Determine appropriate breakpoint contents and size for this
1385 address. */
1386 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1387
1388 /* Update the final write buffer with this inserted
1389 breakpoint's INSN. */
1390 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1391 }
1392 }
1393
1394 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1395 by replacing any memory breakpoints with their shadowed contents.
1396
1397 If READBUF is not NULL, this buffer must not overlap with any of
1398 the breakpoint location's shadow_contents buffers. Otherwise,
1399 a failed assertion internal error will be raised.
1400
1401 The range of shadowed area by each bp_location is:
1402 bl->address - bp_locations_placed_address_before_address_max
1403 up to bl->address + bp_locations_shadow_len_after_address_max
1404 The range we were requested to resolve shadows for is:
1405 memaddr ... memaddr + len
1406 Thus the safe cutoff boundaries for performance optimization are
1407 memaddr + len <= (bl->address
1408 - bp_locations_placed_address_before_address_max)
1409 and:
1410 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1411
1412 void
1413 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1414 const gdb_byte *writebuf_org,
1415 ULONGEST memaddr, LONGEST len)
1416 {
1417 /* Left boundary, right boundary and median element of our binary
1418 search. */
1419 unsigned bc_l, bc_r, bc;
1420
1421 /* Find BC_L which is a leftmost element which may affect BUF
1422 content. It is safe to report lower value but a failure to
1423 report higher one. */
1424
1425 bc_l = 0;
1426 bc_r = bp_locations_count;
1427 while (bc_l + 1 < bc_r)
1428 {
1429 struct bp_location *bl;
1430
1431 bc = (bc_l + bc_r) / 2;
1432 bl = bp_locations[bc];
1433
1434 /* Check first BL->ADDRESS will not overflow due to the added
1435 constant. Then advance the left boundary only if we are sure
1436 the BC element can in no way affect the BUF content (MEMADDR
1437 to MEMADDR + LEN range).
1438
1439 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1440 offset so that we cannot miss a breakpoint with its shadow
1441 range tail still reaching MEMADDR. */
1442
1443 if ((bl->address + bp_locations_shadow_len_after_address_max
1444 >= bl->address)
1445 && (bl->address + bp_locations_shadow_len_after_address_max
1446 <= memaddr))
1447 bc_l = bc;
1448 else
1449 bc_r = bc;
1450 }
1451
1452 /* Due to the binary search above, we need to make sure we pick the
1453 first location that's at BC_L's address. E.g., if there are
1454 multiple locations at the same address, BC_L may end up pointing
1455 at a duplicate location, and miss the "master"/"inserted"
1456 location. Say, given locations L1, L2 and L3 at addresses A and
1457 B:
1458
1459 L1@A, L2@A, L3@B, ...
1460
1461 BC_L could end up pointing at location L2, while the "master"
1462 location could be L1. Since the `loc->inserted' flag is only set
1463 on "master" locations, we'd forget to restore the shadow of L1
1464 and L2. */
1465 while (bc_l > 0
1466 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1467 bc_l--;
1468
1469 /* Now do full processing of the found relevant range of elements. */
1470
1471 for (bc = bc_l; bc < bp_locations_count; bc++)
1472 {
1473 struct bp_location *bl = bp_locations[bc];
1474
1475 /* bp_location array has BL->OWNER always non-NULL. */
1476 if (bl->owner->type == bp_none)
1477 warning (_("reading through apparently deleted breakpoint #%d?"),
1478 bl->owner->number);
1479
1480 /* Performance optimization: any further element can no longer affect BUF
1481 content. */
1482
1483 if (bl->address >= bp_locations_placed_address_before_address_max
1484 && memaddr + len <= (bl->address
1485 - bp_locations_placed_address_before_address_max))
1486 break;
1487
1488 if (!bp_location_has_shadow (bl))
1489 continue;
1490
1491 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1492 memaddr, len, &bl->target_info, bl->gdbarch);
1493 }
1494 }
1495
1496 /* See breakpoint.h. */
1497
1498 bool
1499 is_breakpoint (const struct breakpoint *bpt)
1500 {
1501 return (bpt->type == bp_breakpoint
1502 || bpt->type == bp_hardware_breakpoint
1503 || bpt->type == bp_dprintf);
1504 }
1505
1506 /* Return true if BPT is of any hardware watchpoint kind. */
1507
1508 static bool
1509 is_hardware_watchpoint (const struct breakpoint *bpt)
1510 {
1511 return (bpt->type == bp_hardware_watchpoint
1512 || bpt->type == bp_read_watchpoint
1513 || bpt->type == bp_access_watchpoint);
1514 }
1515
1516 /* See breakpoint.h. */
1517
1518 bool
1519 is_watchpoint (const struct breakpoint *bpt)
1520 {
1521 return (is_hardware_watchpoint (bpt)
1522 || bpt->type == bp_watchpoint);
1523 }
1524
1525 /* Returns true if the current thread and its running state are safe
1526 to evaluate or update watchpoint B. Watchpoints on local
1527 expressions need to be evaluated in the context of the thread that
1528 was current when the watchpoint was created, and, that thread needs
1529 to be stopped to be able to select the correct frame context.
1530 Watchpoints on global expressions can be evaluated on any thread,
1531 and in any state. It is presently left to the target allowing
1532 memory accesses when threads are running. */
1533
1534 static int
1535 watchpoint_in_thread_scope (struct watchpoint *b)
1536 {
1537 return (b->pspace == current_program_space
1538 && (b->watchpoint_thread == null_ptid
1539 || (inferior_ptid == b->watchpoint_thread
1540 && !inferior_thread ()->executing)));
1541 }
1542
1543 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1544 associated bp_watchpoint_scope breakpoint. */
1545
1546 static void
1547 watchpoint_del_at_next_stop (struct watchpoint *w)
1548 {
1549 if (w->related_breakpoint != w)
1550 {
1551 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1552 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1553 w->related_breakpoint->disposition = disp_del_at_next_stop;
1554 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1555 w->related_breakpoint = w;
1556 }
1557 w->disposition = disp_del_at_next_stop;
1558 }
1559
1560 /* Extract a bitfield value from value VAL using the bit parameters contained in
1561 watchpoint W. */
1562
1563 static struct value *
1564 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1565 {
1566 struct value *bit_val;
1567
1568 if (val == NULL)
1569 return NULL;
1570
1571 bit_val = allocate_value (value_type (val));
1572
1573 unpack_value_bitfield (bit_val,
1574 w->val_bitpos,
1575 w->val_bitsize,
1576 value_contents_for_printing (val),
1577 value_offset (val),
1578 val);
1579
1580 return bit_val;
1581 }
1582
1583 /* Allocate a dummy location and add it to B, which must be a software
1584 watchpoint. This is required because even if a software watchpoint
1585 is not watching any memory, bpstat_stop_status requires a location
1586 to be able to report stops. */
1587
1588 static void
1589 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1590 struct program_space *pspace)
1591 {
1592 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1593
1594 b->loc = allocate_bp_location (b);
1595 b->loc->pspace = pspace;
1596 b->loc->address = -1;
1597 b->loc->length = -1;
1598 }
1599
1600 /* Returns true if B is a software watchpoint that is not watching any
1601 memory (e.g., "watch $pc"). */
1602
1603 static bool
1604 is_no_memory_software_watchpoint (struct breakpoint *b)
1605 {
1606 return (b->type == bp_watchpoint
1607 && b->loc != NULL
1608 && b->loc->next == NULL
1609 && b->loc->address == -1
1610 && b->loc->length == -1);
1611 }
1612
1613 /* Assuming that B is a watchpoint:
1614 - Reparse watchpoint expression, if REPARSE is non-zero
1615 - Evaluate expression and store the result in B->val
1616 - Evaluate the condition if there is one, and store the result
1617 in b->loc->cond.
1618 - Update the list of values that must be watched in B->loc.
1619
1620 If the watchpoint disposition is disp_del_at_next_stop, then do
1621 nothing. If this is local watchpoint that is out of scope, delete
1622 it.
1623
1624 Even with `set breakpoint always-inserted on' the watchpoints are
1625 removed + inserted on each stop here. Normal breakpoints must
1626 never be removed because they might be missed by a running thread
1627 when debugging in non-stop mode. On the other hand, hardware
1628 watchpoints (is_hardware_watchpoint; processed here) are specific
1629 to each LWP since they are stored in each LWP's hardware debug
1630 registers. Therefore, such LWP must be stopped first in order to
1631 be able to modify its hardware watchpoints.
1632
1633 Hardware watchpoints must be reset exactly once after being
1634 presented to the user. It cannot be done sooner, because it would
1635 reset the data used to present the watchpoint hit to the user. And
1636 it must not be done later because it could display the same single
1637 watchpoint hit during multiple GDB stops. Note that the latter is
1638 relevant only to the hardware watchpoint types bp_read_watchpoint
1639 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1640 not user-visible - its hit is suppressed if the memory content has
1641 not changed.
1642
1643 The following constraints influence the location where we can reset
1644 hardware watchpoints:
1645
1646 * target_stopped_by_watchpoint and target_stopped_data_address are
1647 called several times when GDB stops.
1648
1649 [linux]
1650 * Multiple hardware watchpoints can be hit at the same time,
1651 causing GDB to stop. GDB only presents one hardware watchpoint
1652 hit at a time as the reason for stopping, and all the other hits
1653 are presented later, one after the other, each time the user
1654 requests the execution to be resumed. Execution is not resumed
1655 for the threads still having pending hit event stored in
1656 LWP_INFO->STATUS. While the watchpoint is already removed from
1657 the inferior on the first stop the thread hit event is kept being
1658 reported from its cached value by linux_nat_stopped_data_address
1659 until the real thread resume happens after the watchpoint gets
1660 presented and thus its LWP_INFO->STATUS gets reset.
1661
1662 Therefore the hardware watchpoint hit can get safely reset on the
1663 watchpoint removal from inferior. */
1664
1665 static void
1666 update_watchpoint (struct watchpoint *b, int reparse)
1667 {
1668 int within_current_scope;
1669 struct frame_id saved_frame_id;
1670 int frame_saved;
1671
1672 /* If this is a local watchpoint, we only want to check if the
1673 watchpoint frame is in scope if the current thread is the thread
1674 that was used to create the watchpoint. */
1675 if (!watchpoint_in_thread_scope (b))
1676 return;
1677
1678 if (b->disposition == disp_del_at_next_stop)
1679 return;
1680
1681 frame_saved = 0;
1682
1683 /* Determine if the watchpoint is within scope. */
1684 if (b->exp_valid_block == NULL)
1685 within_current_scope = 1;
1686 else
1687 {
1688 struct frame_info *fi = get_current_frame ();
1689 struct gdbarch *frame_arch = get_frame_arch (fi);
1690 CORE_ADDR frame_pc = get_frame_pc (fi);
1691
1692 /* If we're at a point where the stack has been destroyed
1693 (e.g. in a function epilogue), unwinding may not work
1694 properly. Do not attempt to recreate locations at this
1695 point. See similar comments in watchpoint_check. */
1696 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1697 return;
1698
1699 /* Save the current frame's ID so we can restore it after
1700 evaluating the watchpoint expression on its own frame. */
1701 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1702 took a frame parameter, so that we didn't have to change the
1703 selected frame. */
1704 frame_saved = 1;
1705 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1706
1707 fi = frame_find_by_id (b->watchpoint_frame);
1708 within_current_scope = (fi != NULL);
1709 if (within_current_scope)
1710 select_frame (fi);
1711 }
1712
1713 /* We don't free locations. They are stored in the bp_location array
1714 and update_global_location_list will eventually delete them and
1715 remove breakpoints if needed. */
1716 b->loc = NULL;
1717
1718 if (within_current_scope && reparse)
1719 {
1720 const char *s;
1721
1722 b->exp.reset ();
1723 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1724 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1725 /* If the meaning of expression itself changed, the old value is
1726 no longer relevant. We don't want to report a watchpoint hit
1727 to the user when the old value and the new value may actually
1728 be completely different objects. */
1729 b->val = NULL;
1730 b->val_valid = false;
1731
1732 /* Note that unlike with breakpoints, the watchpoint's condition
1733 expression is stored in the breakpoint object, not in the
1734 locations (re)created below. */
1735 if (b->cond_string != NULL)
1736 {
1737 b->cond_exp.reset ();
1738
1739 s = b->cond_string;
1740 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1741 }
1742 }
1743
1744 /* If we failed to parse the expression, for example because
1745 it refers to a global variable in a not-yet-loaded shared library,
1746 don't try to insert watchpoint. We don't automatically delete
1747 such watchpoint, though, since failure to parse expression
1748 is different from out-of-scope watchpoint. */
1749 if (!target_has_execution)
1750 {
1751 /* Without execution, memory can't change. No use to try and
1752 set watchpoint locations. The watchpoint will be reset when
1753 the target gains execution, through breakpoint_re_set. */
1754 if (!can_use_hw_watchpoints)
1755 {
1756 if (b->ops->works_in_software_mode (b))
1757 b->type = bp_watchpoint;
1758 else
1759 error (_("Can't set read/access watchpoint when "
1760 "hardware watchpoints are disabled."));
1761 }
1762 }
1763 else if (within_current_scope && b->exp)
1764 {
1765 int pc = 0;
1766 std::vector<value_ref_ptr> val_chain;
1767 struct value *v, *result;
1768 struct program_space *frame_pspace;
1769
1770 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1771
1772 /* Avoid setting b->val if it's already set. The meaning of
1773 b->val is 'the last value' user saw, and we should update
1774 it only if we reported that last value to user. As it
1775 happens, the code that reports it updates b->val directly.
1776 We don't keep track of the memory value for masked
1777 watchpoints. */
1778 if (!b->val_valid && !is_masked_watchpoint (b))
1779 {
1780 if (b->val_bitsize != 0)
1781 v = extract_bitfield_from_watchpoint_value (b, v);
1782 b->val = release_value (v);
1783 b->val_valid = true;
1784 }
1785
1786 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1787
1788 /* Look at each value on the value chain. */
1789 gdb_assert (!val_chain.empty ());
1790 for (const value_ref_ptr &iter : val_chain)
1791 {
1792 v = iter.get ();
1793
1794 /* If it's a memory location, and GDB actually needed
1795 its contents to evaluate the expression, then we
1796 must watch it. If the first value returned is
1797 still lazy, that means an error occurred reading it;
1798 watch it anyway in case it becomes readable. */
1799 if (VALUE_LVAL (v) == lval_memory
1800 && (v == val_chain[0] || ! value_lazy (v)))
1801 {
1802 struct type *vtype = check_typedef (value_type (v));
1803
1804 /* We only watch structs and arrays if user asked
1805 for it explicitly, never if they just happen to
1806 appear in the middle of some value chain. */
1807 if (v == result
1808 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1809 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1810 {
1811 CORE_ADDR addr;
1812 enum target_hw_bp_type type;
1813 struct bp_location *loc, **tmp;
1814 int bitpos = 0, bitsize = 0;
1815
1816 if (value_bitsize (v) != 0)
1817 {
1818 /* Extract the bit parameters out from the bitfield
1819 sub-expression. */
1820 bitpos = value_bitpos (v);
1821 bitsize = value_bitsize (v);
1822 }
1823 else if (v == result && b->val_bitsize != 0)
1824 {
1825 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1826 lvalue whose bit parameters are saved in the fields
1827 VAL_BITPOS and VAL_BITSIZE. */
1828 bitpos = b->val_bitpos;
1829 bitsize = b->val_bitsize;
1830 }
1831
1832 addr = value_address (v);
1833 if (bitsize != 0)
1834 {
1835 /* Skip the bytes that don't contain the bitfield. */
1836 addr += bitpos / 8;
1837 }
1838
1839 type = hw_write;
1840 if (b->type == bp_read_watchpoint)
1841 type = hw_read;
1842 else if (b->type == bp_access_watchpoint)
1843 type = hw_access;
1844
1845 loc = allocate_bp_location (b);
1846 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1847 ;
1848 *tmp = loc;
1849 loc->gdbarch = get_type_arch (value_type (v));
1850
1851 loc->pspace = frame_pspace;
1852 loc->address = address_significant (loc->gdbarch, addr);
1853
1854 if (bitsize != 0)
1855 {
1856 /* Just cover the bytes that make up the bitfield. */
1857 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1858 }
1859 else
1860 loc->length = TYPE_LENGTH (value_type (v));
1861
1862 loc->watchpoint_type = type;
1863 }
1864 }
1865 }
1866
1867 /* Change the type of breakpoint between hardware assisted or
1868 an ordinary watchpoint depending on the hardware support
1869 and free hardware slots. REPARSE is set when the inferior
1870 is started. */
1871 if (reparse)
1872 {
1873 int reg_cnt;
1874 enum bp_loc_type loc_type;
1875 struct bp_location *bl;
1876
1877 reg_cnt = can_use_hardware_watchpoint (val_chain);
1878
1879 if (reg_cnt)
1880 {
1881 int i, target_resources_ok, other_type_used;
1882 enum bptype type;
1883
1884 /* Use an exact watchpoint when there's only one memory region to be
1885 watched, and only one debug register is needed to watch it. */
1886 b->exact = target_exact_watchpoints && reg_cnt == 1;
1887
1888 /* We need to determine how many resources are already
1889 used for all other hardware watchpoints plus this one
1890 to see if we still have enough resources to also fit
1891 this watchpoint in as well. */
1892
1893 /* If this is a software watchpoint, we try to turn it
1894 to a hardware one -- count resources as if B was of
1895 hardware watchpoint type. */
1896 type = b->type;
1897 if (type == bp_watchpoint)
1898 type = bp_hardware_watchpoint;
1899
1900 /* This watchpoint may or may not have been placed on
1901 the list yet at this point (it won't be in the list
1902 if we're trying to create it for the first time,
1903 through watch_command), so always account for it
1904 manually. */
1905
1906 /* Count resources used by all watchpoints except B. */
1907 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1908
1909 /* Add in the resources needed for B. */
1910 i += hw_watchpoint_use_count (b);
1911
1912 target_resources_ok
1913 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1914 if (target_resources_ok <= 0)
1915 {
1916 int sw_mode = b->ops->works_in_software_mode (b);
1917
1918 if (target_resources_ok == 0 && !sw_mode)
1919 error (_("Target does not support this type of "
1920 "hardware watchpoint."));
1921 else if (target_resources_ok < 0 && !sw_mode)
1922 error (_("There are not enough available hardware "
1923 "resources for this watchpoint."));
1924
1925 /* Downgrade to software watchpoint. */
1926 b->type = bp_watchpoint;
1927 }
1928 else
1929 {
1930 /* If this was a software watchpoint, we've just
1931 found we have enough resources to turn it to a
1932 hardware watchpoint. Otherwise, this is a
1933 nop. */
1934 b->type = type;
1935 }
1936 }
1937 else if (!b->ops->works_in_software_mode (b))
1938 {
1939 if (!can_use_hw_watchpoints)
1940 error (_("Can't set read/access watchpoint when "
1941 "hardware watchpoints are disabled."));
1942 else
1943 error (_("Expression cannot be implemented with "
1944 "read/access watchpoint."));
1945 }
1946 else
1947 b->type = bp_watchpoint;
1948
1949 loc_type = (b->type == bp_watchpoint? bp_loc_other
1950 : bp_loc_hardware_watchpoint);
1951 for (bl = b->loc; bl; bl = bl->next)
1952 bl->loc_type = loc_type;
1953 }
1954
1955 /* If a software watchpoint is not watching any memory, then the
1956 above left it without any location set up. But,
1957 bpstat_stop_status requires a location to be able to report
1958 stops, so make sure there's at least a dummy one. */
1959 if (b->type == bp_watchpoint && b->loc == NULL)
1960 software_watchpoint_add_no_memory_location (b, frame_pspace);
1961 }
1962 else if (!within_current_scope)
1963 {
1964 printf_filtered (_("\
1965 Watchpoint %d deleted because the program has left the block\n\
1966 in which its expression is valid.\n"),
1967 b->number);
1968 watchpoint_del_at_next_stop (b);
1969 }
1970
1971 /* Restore the selected frame. */
1972 if (frame_saved)
1973 select_frame (frame_find_by_id (saved_frame_id));
1974 }
1975
1976
1977 /* Returns 1 iff breakpoint location should be
1978 inserted in the inferior. We don't differentiate the type of BL's owner
1979 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1980 breakpoint_ops is not defined, because in insert_bp_location,
1981 tracepoint's insert_location will not be called. */
1982 static int
1983 should_be_inserted (struct bp_location *bl)
1984 {
1985 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1986 return 0;
1987
1988 if (bl->owner->disposition == disp_del_at_next_stop)
1989 return 0;
1990
1991 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1992 return 0;
1993
1994 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1995 return 0;
1996
1997 /* This is set for example, when we're attached to the parent of a
1998 vfork, and have detached from the child. The child is running
1999 free, and we expect it to do an exec or exit, at which point the
2000 OS makes the parent schedulable again (and the target reports
2001 that the vfork is done). Until the child is done with the shared
2002 memory region, do not insert breakpoints in the parent, otherwise
2003 the child could still trip on the parent's breakpoints. Since
2004 the parent is blocked anyway, it won't miss any breakpoint. */
2005 if (bl->pspace->breakpoints_not_allowed)
2006 return 0;
2007
2008 /* Don't insert a breakpoint if we're trying to step past its
2009 location, except if the breakpoint is a single-step breakpoint,
2010 and the breakpoint's thread is the thread which is stepping past
2011 a breakpoint. */
2012 if ((bl->loc_type == bp_loc_software_breakpoint
2013 || bl->loc_type == bp_loc_hardware_breakpoint)
2014 && stepping_past_instruction_at (bl->pspace->aspace,
2015 bl->address)
2016 /* The single-step breakpoint may be inserted at the location
2017 we're trying to step if the instruction branches to itself.
2018 However, the instruction won't be executed at all and it may
2019 break the semantics of the instruction, for example, the
2020 instruction is a conditional branch or updates some flags.
2021 We can't fix it unless GDB is able to emulate the instruction
2022 or switch to displaced stepping. */
2023 && !(bl->owner->type == bp_single_step
2024 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2025 {
2026 if (debug_infrun)
2027 {
2028 fprintf_unfiltered (gdb_stdlog,
2029 "infrun: skipping breakpoint: "
2030 "stepping past insn at: %s\n",
2031 paddress (bl->gdbarch, bl->address));
2032 }
2033 return 0;
2034 }
2035
2036 /* Don't insert watchpoints if we're trying to step past the
2037 instruction that triggered one. */
2038 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2039 && stepping_past_nonsteppable_watchpoint ())
2040 {
2041 if (debug_infrun)
2042 {
2043 fprintf_unfiltered (gdb_stdlog,
2044 "infrun: stepping past non-steppable watchpoint. "
2045 "skipping watchpoint at %s:%d\n",
2046 paddress (bl->gdbarch, bl->address),
2047 bl->length);
2048 }
2049 return 0;
2050 }
2051
2052 return 1;
2053 }
2054
2055 /* Same as should_be_inserted but does the check assuming
2056 that the location is not duplicated. */
2057
2058 static int
2059 unduplicated_should_be_inserted (struct bp_location *bl)
2060 {
2061 int result;
2062 const int save_duplicate = bl->duplicate;
2063
2064 bl->duplicate = 0;
2065 result = should_be_inserted (bl);
2066 bl->duplicate = save_duplicate;
2067 return result;
2068 }
2069
2070 /* Parses a conditional described by an expression COND into an
2071 agent expression bytecode suitable for evaluation
2072 by the bytecode interpreter. Return NULL if there was
2073 any error during parsing. */
2074
2075 static agent_expr_up
2076 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2077 {
2078 if (cond == NULL)
2079 return NULL;
2080
2081 agent_expr_up aexpr;
2082
2083 /* We don't want to stop processing, so catch any errors
2084 that may show up. */
2085 try
2086 {
2087 aexpr = gen_eval_for_expr (scope, cond);
2088 }
2089
2090 catch (const gdb_exception_error &ex)
2091 {
2092 /* If we got here, it means the condition could not be parsed to a valid
2093 bytecode expression and thus can't be evaluated on the target's side.
2094 It's no use iterating through the conditions. */
2095 }
2096
2097 /* We have a valid agent expression. */
2098 return aexpr;
2099 }
2100
2101 /* Based on location BL, create a list of breakpoint conditions to be
2102 passed on to the target. If we have duplicated locations with different
2103 conditions, we will add such conditions to the list. The idea is that the
2104 target will evaluate the list of conditions and will only notify GDB when
2105 one of them is true. */
2106
2107 static void
2108 build_target_condition_list (struct bp_location *bl)
2109 {
2110 struct bp_location **locp = NULL, **loc2p;
2111 int null_condition_or_parse_error = 0;
2112 int modified = bl->needs_update;
2113 struct bp_location *loc;
2114
2115 /* Release conditions left over from a previous insert. */
2116 bl->target_info.conditions.clear ();
2117
2118 /* This is only meaningful if the target is
2119 evaluating conditions and if the user has
2120 opted for condition evaluation on the target's
2121 side. */
2122 if (gdb_evaluates_breakpoint_condition_p ()
2123 || !target_supports_evaluation_of_breakpoint_conditions ())
2124 return;
2125
2126 /* Do a first pass to check for locations with no assigned
2127 conditions or conditions that fail to parse to a valid agent expression
2128 bytecode. If any of these happen, then it's no use to send conditions
2129 to the target since this location will always trigger and generate a
2130 response back to GDB. */
2131 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2132 {
2133 loc = (*loc2p);
2134 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2135 {
2136 if (modified)
2137 {
2138 /* Re-parse the conditions since something changed. In that
2139 case we already freed the condition bytecodes (see
2140 force_breakpoint_reinsertion). We just
2141 need to parse the condition to bytecodes again. */
2142 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2143 loc->cond.get ());
2144 }
2145
2146 /* If we have a NULL bytecode expression, it means something
2147 went wrong or we have a null condition expression. */
2148 if (!loc->cond_bytecode)
2149 {
2150 null_condition_or_parse_error = 1;
2151 break;
2152 }
2153 }
2154 }
2155
2156 /* If any of these happened, it means we will have to evaluate the conditions
2157 for the location's address on gdb's side. It is no use keeping bytecodes
2158 for all the other duplicate locations, thus we free all of them here.
2159
2160 This is so we have a finer control over which locations' conditions are
2161 being evaluated by GDB or the remote stub. */
2162 if (null_condition_or_parse_error)
2163 {
2164 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2165 {
2166 loc = (*loc2p);
2167 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2168 {
2169 /* Only go as far as the first NULL bytecode is
2170 located. */
2171 if (!loc->cond_bytecode)
2172 return;
2173
2174 loc->cond_bytecode.reset ();
2175 }
2176 }
2177 }
2178
2179 /* No NULL conditions or failed bytecode generation. Build a condition list
2180 for this location's address. */
2181 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2182 {
2183 loc = (*loc2p);
2184 if (loc->cond
2185 && is_breakpoint (loc->owner)
2186 && loc->pspace->num == bl->pspace->num
2187 && loc->owner->enable_state == bp_enabled
2188 && loc->enabled)
2189 {
2190 /* Add the condition to the vector. This will be used later
2191 to send the conditions to the target. */
2192 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2193 }
2194 }
2195
2196 return;
2197 }
2198
2199 /* Parses a command described by string CMD into an agent expression
2200 bytecode suitable for evaluation by the bytecode interpreter.
2201 Return NULL if there was any error during parsing. */
2202
2203 static agent_expr_up
2204 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2205 {
2206 const char *cmdrest;
2207 const char *format_start, *format_end;
2208 struct gdbarch *gdbarch = get_current_arch ();
2209
2210 if (cmd == NULL)
2211 return NULL;
2212
2213 cmdrest = cmd;
2214
2215 if (*cmdrest == ',')
2216 ++cmdrest;
2217 cmdrest = skip_spaces (cmdrest);
2218
2219 if (*cmdrest++ != '"')
2220 error (_("No format string following the location"));
2221
2222 format_start = cmdrest;
2223
2224 format_pieces fpieces (&cmdrest);
2225
2226 format_end = cmdrest;
2227
2228 if (*cmdrest++ != '"')
2229 error (_("Bad format string, non-terminated '\"'."));
2230
2231 cmdrest = skip_spaces (cmdrest);
2232
2233 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2234 error (_("Invalid argument syntax"));
2235
2236 if (*cmdrest == ',')
2237 cmdrest++;
2238 cmdrest = skip_spaces (cmdrest);
2239
2240 /* For each argument, make an expression. */
2241
2242 std::vector<struct expression *> argvec;
2243 while (*cmdrest != '\0')
2244 {
2245 const char *cmd1;
2246
2247 cmd1 = cmdrest;
2248 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2249 argvec.push_back (expr.release ());
2250 cmdrest = cmd1;
2251 if (*cmdrest == ',')
2252 ++cmdrest;
2253 }
2254
2255 agent_expr_up aexpr;
2256
2257 /* We don't want to stop processing, so catch any errors
2258 that may show up. */
2259 try
2260 {
2261 aexpr = gen_printf (scope, gdbarch, 0, 0,
2262 format_start, format_end - format_start,
2263 argvec.size (), argvec.data ());
2264 }
2265 catch (const gdb_exception_error &ex)
2266 {
2267 /* If we got here, it means the command could not be parsed to a valid
2268 bytecode expression and thus can't be evaluated on the target's side.
2269 It's no use iterating through the other commands. */
2270 }
2271
2272 /* We have a valid agent expression, return it. */
2273 return aexpr;
2274 }
2275
2276 /* Based on location BL, create a list of breakpoint commands to be
2277 passed on to the target. If we have duplicated locations with
2278 different commands, we will add any such to the list. */
2279
2280 static void
2281 build_target_command_list (struct bp_location *bl)
2282 {
2283 struct bp_location **locp = NULL, **loc2p;
2284 int null_command_or_parse_error = 0;
2285 int modified = bl->needs_update;
2286 struct bp_location *loc;
2287
2288 /* Clear commands left over from a previous insert. */
2289 bl->target_info.tcommands.clear ();
2290
2291 if (!target_can_run_breakpoint_commands ())
2292 return;
2293
2294 /* For now, limit to agent-style dprintf breakpoints. */
2295 if (dprintf_style != dprintf_style_agent)
2296 return;
2297
2298 /* For now, if we have any duplicate location that isn't a dprintf,
2299 don't install the target-side commands, as that would make the
2300 breakpoint not be reported to the core, and we'd lose
2301 control. */
2302 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2303 {
2304 loc = (*loc2p);
2305 if (is_breakpoint (loc->owner)
2306 && loc->pspace->num == bl->pspace->num
2307 && loc->owner->type != bp_dprintf)
2308 return;
2309 }
2310
2311 /* Do a first pass to check for locations with no assigned
2312 conditions or conditions that fail to parse to a valid agent expression
2313 bytecode. If any of these happen, then it's no use to send conditions
2314 to the target since this location will always trigger and generate a
2315 response back to GDB. */
2316 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2317 {
2318 loc = (*loc2p);
2319 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2320 {
2321 if (modified)
2322 {
2323 /* Re-parse the commands since something changed. In that
2324 case we already freed the command bytecodes (see
2325 force_breakpoint_reinsertion). We just
2326 need to parse the command to bytecodes again. */
2327 loc->cmd_bytecode
2328 = parse_cmd_to_aexpr (bl->address,
2329 loc->owner->extra_string);
2330 }
2331
2332 /* If we have a NULL bytecode expression, it means something
2333 went wrong or we have a null command expression. */
2334 if (!loc->cmd_bytecode)
2335 {
2336 null_command_or_parse_error = 1;
2337 break;
2338 }
2339 }
2340 }
2341
2342 /* If anything failed, then we're not doing target-side commands,
2343 and so clean up. */
2344 if (null_command_or_parse_error)
2345 {
2346 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2347 {
2348 loc = (*loc2p);
2349 if (is_breakpoint (loc->owner)
2350 && loc->pspace->num == bl->pspace->num)
2351 {
2352 /* Only go as far as the first NULL bytecode is
2353 located. */
2354 if (loc->cmd_bytecode == NULL)
2355 return;
2356
2357 loc->cmd_bytecode.reset ();
2358 }
2359 }
2360 }
2361
2362 /* No NULL commands or failed bytecode generation. Build a command list
2363 for this location's address. */
2364 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2365 {
2366 loc = (*loc2p);
2367 if (loc->owner->extra_string
2368 && is_breakpoint (loc->owner)
2369 && loc->pspace->num == bl->pspace->num
2370 && loc->owner->enable_state == bp_enabled
2371 && loc->enabled)
2372 {
2373 /* Add the command to the vector. This will be used later
2374 to send the commands to the target. */
2375 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2376 }
2377 }
2378
2379 bl->target_info.persist = 0;
2380 /* Maybe flag this location as persistent. */
2381 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2382 bl->target_info.persist = 1;
2383 }
2384
2385 /* Return the kind of breakpoint on address *ADDR. Get the kind
2386 of breakpoint according to ADDR except single-step breakpoint.
2387 Get the kind of single-step breakpoint according to the current
2388 registers state. */
2389
2390 static int
2391 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2392 {
2393 if (bl->owner->type == bp_single_step)
2394 {
2395 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2396 struct regcache *regcache;
2397
2398 regcache = get_thread_regcache (thr);
2399
2400 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2401 regcache, addr);
2402 }
2403 else
2404 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2405 }
2406
2407 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2408 location. Any error messages are printed to TMP_ERROR_STREAM; and
2409 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2410 Returns 0 for success, 1 if the bp_location type is not supported or
2411 -1 for failure.
2412
2413 NOTE drow/2003-09-09: This routine could be broken down to an
2414 object-style method for each breakpoint or catchpoint type. */
2415 static int
2416 insert_bp_location (struct bp_location *bl,
2417 struct ui_file *tmp_error_stream,
2418 int *disabled_breaks,
2419 int *hw_breakpoint_error,
2420 int *hw_bp_error_explained_already)
2421 {
2422 gdb_exception bp_excpt;
2423
2424 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2425 return 0;
2426
2427 /* Note we don't initialize bl->target_info, as that wipes out
2428 the breakpoint location's shadow_contents if the breakpoint
2429 is still inserted at that location. This in turn breaks
2430 target_read_memory which depends on these buffers when
2431 a memory read is requested at the breakpoint location:
2432 Once the target_info has been wiped, we fail to see that
2433 we have a breakpoint inserted at that address and thus
2434 read the breakpoint instead of returning the data saved in
2435 the breakpoint location's shadow contents. */
2436 bl->target_info.reqstd_address = bl->address;
2437 bl->target_info.placed_address_space = bl->pspace->aspace;
2438 bl->target_info.length = bl->length;
2439
2440 /* When working with target-side conditions, we must pass all the conditions
2441 for the same breakpoint address down to the target since GDB will not
2442 insert those locations. With a list of breakpoint conditions, the target
2443 can decide when to stop and notify GDB. */
2444
2445 if (is_breakpoint (bl->owner))
2446 {
2447 build_target_condition_list (bl);
2448 build_target_command_list (bl);
2449 /* Reset the modification marker. */
2450 bl->needs_update = 0;
2451 }
2452
2453 if (bl->loc_type == bp_loc_software_breakpoint
2454 || bl->loc_type == bp_loc_hardware_breakpoint)
2455 {
2456 if (bl->owner->type != bp_hardware_breakpoint)
2457 {
2458 /* If the explicitly specified breakpoint type
2459 is not hardware breakpoint, check the memory map to see
2460 if the breakpoint address is in read only memory or not.
2461
2462 Two important cases are:
2463 - location type is not hardware breakpoint, memory
2464 is readonly. We change the type of the location to
2465 hardware breakpoint.
2466 - location type is hardware breakpoint, memory is
2467 read-write. This means we've previously made the
2468 location hardware one, but then the memory map changed,
2469 so we undo.
2470
2471 When breakpoints are removed, remove_breakpoints will use
2472 location types we've just set here, the only possible
2473 problem is that memory map has changed during running
2474 program, but it's not going to work anyway with current
2475 gdb. */
2476 struct mem_region *mr
2477 = lookup_mem_region (bl->target_info.reqstd_address);
2478
2479 if (mr)
2480 {
2481 if (automatic_hardware_breakpoints)
2482 {
2483 enum bp_loc_type new_type;
2484
2485 if (mr->attrib.mode != MEM_RW)
2486 new_type = bp_loc_hardware_breakpoint;
2487 else
2488 new_type = bp_loc_software_breakpoint;
2489
2490 if (new_type != bl->loc_type)
2491 {
2492 static int said = 0;
2493
2494 bl->loc_type = new_type;
2495 if (!said)
2496 {
2497 fprintf_filtered (gdb_stdout,
2498 _("Note: automatically using "
2499 "hardware breakpoints for "
2500 "read-only addresses.\n"));
2501 said = 1;
2502 }
2503 }
2504 }
2505 else if (bl->loc_type == bp_loc_software_breakpoint
2506 && mr->attrib.mode != MEM_RW)
2507 {
2508 fprintf_unfiltered (tmp_error_stream,
2509 _("Cannot insert breakpoint %d.\n"
2510 "Cannot set software breakpoint "
2511 "at read-only address %s\n"),
2512 bl->owner->number,
2513 paddress (bl->gdbarch, bl->address));
2514 return 1;
2515 }
2516 }
2517 }
2518
2519 /* First check to see if we have to handle an overlay. */
2520 if (overlay_debugging == ovly_off
2521 || bl->section == NULL
2522 || !(section_is_overlay (bl->section)))
2523 {
2524 /* No overlay handling: just set the breakpoint. */
2525 try
2526 {
2527 int val;
2528
2529 val = bl->owner->ops->insert_location (bl);
2530 if (val)
2531 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2532 }
2533 catch (gdb_exception &e)
2534 {
2535 bp_excpt = std::move (e);
2536 }
2537 }
2538 else
2539 {
2540 /* This breakpoint is in an overlay section.
2541 Shall we set a breakpoint at the LMA? */
2542 if (!overlay_events_enabled)
2543 {
2544 /* Yes -- overlay event support is not active,
2545 so we must try to set a breakpoint at the LMA.
2546 This will not work for a hardware breakpoint. */
2547 if (bl->loc_type == bp_loc_hardware_breakpoint)
2548 warning (_("hardware breakpoint %d not supported in overlay!"),
2549 bl->owner->number);
2550 else
2551 {
2552 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2553 bl->section);
2554 /* Set a software (trap) breakpoint at the LMA. */
2555 bl->overlay_target_info = bl->target_info;
2556 bl->overlay_target_info.reqstd_address = addr;
2557
2558 /* No overlay handling: just set the breakpoint. */
2559 try
2560 {
2561 int val;
2562
2563 bl->overlay_target_info.kind
2564 = breakpoint_kind (bl, &addr);
2565 bl->overlay_target_info.placed_address = addr;
2566 val = target_insert_breakpoint (bl->gdbarch,
2567 &bl->overlay_target_info);
2568 if (val)
2569 bp_excpt
2570 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2571 }
2572 catch (gdb_exception &e)
2573 {
2574 bp_excpt = std::move (e);
2575 }
2576
2577 if (bp_excpt.reason != 0)
2578 fprintf_unfiltered (tmp_error_stream,
2579 "Overlay breakpoint %d "
2580 "failed: in ROM?\n",
2581 bl->owner->number);
2582 }
2583 }
2584 /* Shall we set a breakpoint at the VMA? */
2585 if (section_is_mapped (bl->section))
2586 {
2587 /* Yes. This overlay section is mapped into memory. */
2588 try
2589 {
2590 int val;
2591
2592 val = bl->owner->ops->insert_location (bl);
2593 if (val)
2594 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2595 }
2596 catch (gdb_exception &e)
2597 {
2598 bp_excpt = std::move (e);
2599 }
2600 }
2601 else
2602 {
2603 /* No. This breakpoint will not be inserted.
2604 No error, but do not mark the bp as 'inserted'. */
2605 return 0;
2606 }
2607 }
2608
2609 if (bp_excpt.reason != 0)
2610 {
2611 /* Can't set the breakpoint. */
2612
2613 /* In some cases, we might not be able to insert a
2614 breakpoint in a shared library that has already been
2615 removed, but we have not yet processed the shlib unload
2616 event. Unfortunately, some targets that implement
2617 breakpoint insertion themselves can't tell why the
2618 breakpoint insertion failed (e.g., the remote target
2619 doesn't define error codes), so we must treat generic
2620 errors as memory errors. */
2621 if (bp_excpt.reason == RETURN_ERROR
2622 && (bp_excpt.error == GENERIC_ERROR
2623 || bp_excpt.error == MEMORY_ERROR)
2624 && bl->loc_type == bp_loc_software_breakpoint
2625 && (solib_name_from_address (bl->pspace, bl->address)
2626 || shared_objfile_contains_address_p (bl->pspace,
2627 bl->address)))
2628 {
2629 /* See also: disable_breakpoints_in_shlibs. */
2630 bl->shlib_disabled = 1;
2631 gdb::observers::breakpoint_modified.notify (bl->owner);
2632 if (!*disabled_breaks)
2633 {
2634 fprintf_unfiltered (tmp_error_stream,
2635 "Cannot insert breakpoint %d.\n",
2636 bl->owner->number);
2637 fprintf_unfiltered (tmp_error_stream,
2638 "Temporarily disabling shared "
2639 "library breakpoints:\n");
2640 }
2641 *disabled_breaks = 1;
2642 fprintf_unfiltered (tmp_error_stream,
2643 "breakpoint #%d\n", bl->owner->number);
2644 return 0;
2645 }
2646 else
2647 {
2648 if (bl->loc_type == bp_loc_hardware_breakpoint)
2649 {
2650 *hw_breakpoint_error = 1;
2651 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2652 fprintf_unfiltered (tmp_error_stream,
2653 "Cannot insert hardware breakpoint %d%s",
2654 bl->owner->number,
2655 bp_excpt.message ? ":" : ".\n");
2656 if (bp_excpt.message != NULL)
2657 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2658 bp_excpt.what ());
2659 }
2660 else
2661 {
2662 if (bp_excpt.message == NULL)
2663 {
2664 std::string message
2665 = memory_error_message (TARGET_XFER_E_IO,
2666 bl->gdbarch, bl->address);
2667
2668 fprintf_unfiltered (tmp_error_stream,
2669 "Cannot insert breakpoint %d.\n"
2670 "%s\n",
2671 bl->owner->number, message.c_str ());
2672 }
2673 else
2674 {
2675 fprintf_unfiltered (tmp_error_stream,
2676 "Cannot insert breakpoint %d: %s\n",
2677 bl->owner->number,
2678 bp_excpt.what ());
2679 }
2680 }
2681 return 1;
2682
2683 }
2684 }
2685 else
2686 bl->inserted = 1;
2687
2688 return 0;
2689 }
2690
2691 else if (bl->loc_type == bp_loc_hardware_watchpoint
2692 /* NOTE drow/2003-09-08: This state only exists for removing
2693 watchpoints. It's not clear that it's necessary... */
2694 && bl->owner->disposition != disp_del_at_next_stop)
2695 {
2696 int val;
2697
2698 gdb_assert (bl->owner->ops != NULL
2699 && bl->owner->ops->insert_location != NULL);
2700
2701 val = bl->owner->ops->insert_location (bl);
2702
2703 /* If trying to set a read-watchpoint, and it turns out it's not
2704 supported, try emulating one with an access watchpoint. */
2705 if (val == 1 && bl->watchpoint_type == hw_read)
2706 {
2707 struct bp_location *loc, **loc_temp;
2708
2709 /* But don't try to insert it, if there's already another
2710 hw_access location that would be considered a duplicate
2711 of this one. */
2712 ALL_BP_LOCATIONS (loc, loc_temp)
2713 if (loc != bl
2714 && loc->watchpoint_type == hw_access
2715 && watchpoint_locations_match (bl, loc))
2716 {
2717 bl->duplicate = 1;
2718 bl->inserted = 1;
2719 bl->target_info = loc->target_info;
2720 bl->watchpoint_type = hw_access;
2721 val = 0;
2722 break;
2723 }
2724
2725 if (val == 1)
2726 {
2727 bl->watchpoint_type = hw_access;
2728 val = bl->owner->ops->insert_location (bl);
2729
2730 if (val)
2731 /* Back to the original value. */
2732 bl->watchpoint_type = hw_read;
2733 }
2734 }
2735
2736 bl->inserted = (val == 0);
2737 }
2738
2739 else if (bl->owner->type == bp_catchpoint)
2740 {
2741 int val;
2742
2743 gdb_assert (bl->owner->ops != NULL
2744 && bl->owner->ops->insert_location != NULL);
2745
2746 val = bl->owner->ops->insert_location (bl);
2747 if (val)
2748 {
2749 bl->owner->enable_state = bp_disabled;
2750
2751 if (val == 1)
2752 warning (_("\
2753 Error inserting catchpoint %d: Your system does not support this type\n\
2754 of catchpoint."), bl->owner->number);
2755 else
2756 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2757 }
2758
2759 bl->inserted = (val == 0);
2760
2761 /* We've already printed an error message if there was a problem
2762 inserting this catchpoint, and we've disabled the catchpoint,
2763 so just return success. */
2764 return 0;
2765 }
2766
2767 return 0;
2768 }
2769
2770 /* This function is called when program space PSPACE is about to be
2771 deleted. It takes care of updating breakpoints to not reference
2772 PSPACE anymore. */
2773
2774 void
2775 breakpoint_program_space_exit (struct program_space *pspace)
2776 {
2777 struct breakpoint *b, *b_temp;
2778 struct bp_location *loc, **loc_temp;
2779
2780 /* Remove any breakpoint that was set through this program space. */
2781 ALL_BREAKPOINTS_SAFE (b, b_temp)
2782 {
2783 if (b->pspace == pspace)
2784 delete_breakpoint (b);
2785 }
2786
2787 /* Breakpoints set through other program spaces could have locations
2788 bound to PSPACE as well. Remove those. */
2789 ALL_BP_LOCATIONS (loc, loc_temp)
2790 {
2791 struct bp_location *tmp;
2792
2793 if (loc->pspace == pspace)
2794 {
2795 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2796 if (loc->owner->loc == loc)
2797 loc->owner->loc = loc->next;
2798 else
2799 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2800 if (tmp->next == loc)
2801 {
2802 tmp->next = loc->next;
2803 break;
2804 }
2805 }
2806 }
2807
2808 /* Now update the global location list to permanently delete the
2809 removed locations above. */
2810 update_global_location_list (UGLL_DONT_INSERT);
2811 }
2812
2813 /* Make sure all breakpoints are inserted in inferior.
2814 Throws exception on any error.
2815 A breakpoint that is already inserted won't be inserted
2816 again, so calling this function twice is safe. */
2817 void
2818 insert_breakpoints (void)
2819 {
2820 struct breakpoint *bpt;
2821
2822 ALL_BREAKPOINTS (bpt)
2823 if (is_hardware_watchpoint (bpt))
2824 {
2825 struct watchpoint *w = (struct watchpoint *) bpt;
2826
2827 update_watchpoint (w, 0 /* don't reparse. */);
2828 }
2829
2830 /* Updating watchpoints creates new locations, so update the global
2831 location list. Explicitly tell ugll to insert locations and
2832 ignore breakpoints_always_inserted_mode. */
2833 update_global_location_list (UGLL_INSERT);
2834 }
2835
2836 /* Invoke CALLBACK for each of bp_location. */
2837
2838 void
2839 iterate_over_bp_locations (walk_bp_location_callback callback)
2840 {
2841 struct bp_location *loc, **loc_tmp;
2842
2843 ALL_BP_LOCATIONS (loc, loc_tmp)
2844 {
2845 callback (loc, NULL);
2846 }
2847 }
2848
2849 /* This is used when we need to synch breakpoint conditions between GDB and the
2850 target. It is the case with deleting and disabling of breakpoints when using
2851 always-inserted mode. */
2852
2853 static void
2854 update_inserted_breakpoint_locations (void)
2855 {
2856 struct bp_location *bl, **blp_tmp;
2857 int error_flag = 0;
2858 int val = 0;
2859 int disabled_breaks = 0;
2860 int hw_breakpoint_error = 0;
2861 int hw_bp_details_reported = 0;
2862
2863 string_file tmp_error_stream;
2864
2865 /* Explicitly mark the warning -- this will only be printed if
2866 there was an error. */
2867 tmp_error_stream.puts ("Warning:\n");
2868
2869 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2870
2871 ALL_BP_LOCATIONS (bl, blp_tmp)
2872 {
2873 /* We only want to update software breakpoints and hardware
2874 breakpoints. */
2875 if (!is_breakpoint (bl->owner))
2876 continue;
2877
2878 /* We only want to update locations that are already inserted
2879 and need updating. This is to avoid unwanted insertion during
2880 deletion of breakpoints. */
2881 if (!bl->inserted || !bl->needs_update)
2882 continue;
2883
2884 switch_to_program_space_and_thread (bl->pspace);
2885
2886 /* For targets that support global breakpoints, there's no need
2887 to select an inferior to insert breakpoint to. In fact, even
2888 if we aren't attached to any process yet, we should still
2889 insert breakpoints. */
2890 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2891 && inferior_ptid == null_ptid)
2892 continue;
2893
2894 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2895 &hw_breakpoint_error, &hw_bp_details_reported);
2896 if (val)
2897 error_flag = val;
2898 }
2899
2900 if (error_flag)
2901 {
2902 target_terminal::ours_for_output ();
2903 error_stream (tmp_error_stream);
2904 }
2905 }
2906
2907 /* Used when starting or continuing the program. */
2908
2909 static void
2910 insert_breakpoint_locations (void)
2911 {
2912 struct breakpoint *bpt;
2913 struct bp_location *bl, **blp_tmp;
2914 int error_flag = 0;
2915 int val = 0;
2916 int disabled_breaks = 0;
2917 int hw_breakpoint_error = 0;
2918 int hw_bp_error_explained_already = 0;
2919
2920 string_file tmp_error_stream;
2921
2922 /* Explicitly mark the warning -- this will only be printed if
2923 there was an error. */
2924 tmp_error_stream.puts ("Warning:\n");
2925
2926 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2927
2928 ALL_BP_LOCATIONS (bl, blp_tmp)
2929 {
2930 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2931 continue;
2932
2933 /* There is no point inserting thread-specific breakpoints if
2934 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2935 has BL->OWNER always non-NULL. */
2936 if (bl->owner->thread != -1
2937 && !valid_global_thread_id (bl->owner->thread))
2938 continue;
2939
2940 switch_to_program_space_and_thread (bl->pspace);
2941
2942 /* For targets that support global breakpoints, there's no need
2943 to select an inferior to insert breakpoint to. In fact, even
2944 if we aren't attached to any process yet, we should still
2945 insert breakpoints. */
2946 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2947 && inferior_ptid == null_ptid)
2948 continue;
2949
2950 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2951 &hw_breakpoint_error, &hw_bp_error_explained_already);
2952 if (val)
2953 error_flag = val;
2954 }
2955
2956 /* If we failed to insert all locations of a watchpoint, remove
2957 them, as half-inserted watchpoint is of limited use. */
2958 ALL_BREAKPOINTS (bpt)
2959 {
2960 int some_failed = 0;
2961 struct bp_location *loc;
2962
2963 if (!is_hardware_watchpoint (bpt))
2964 continue;
2965
2966 if (!breakpoint_enabled (bpt))
2967 continue;
2968
2969 if (bpt->disposition == disp_del_at_next_stop)
2970 continue;
2971
2972 for (loc = bpt->loc; loc; loc = loc->next)
2973 if (!loc->inserted && should_be_inserted (loc))
2974 {
2975 some_failed = 1;
2976 break;
2977 }
2978 if (some_failed)
2979 {
2980 for (loc = bpt->loc; loc; loc = loc->next)
2981 if (loc->inserted)
2982 remove_breakpoint (loc);
2983
2984 hw_breakpoint_error = 1;
2985 tmp_error_stream.printf ("Could not insert "
2986 "hardware watchpoint %d.\n",
2987 bpt->number);
2988 error_flag = -1;
2989 }
2990 }
2991
2992 if (error_flag)
2993 {
2994 /* If a hardware breakpoint or watchpoint was inserted, add a
2995 message about possibly exhausted resources. */
2996 if (hw_breakpoint_error && !hw_bp_error_explained_already)
2997 {
2998 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
2999 You may have requested too many hardware breakpoints/watchpoints.\n");
3000 }
3001 target_terminal::ours_for_output ();
3002 error_stream (tmp_error_stream);
3003 }
3004 }
3005
3006 /* Used when the program stops.
3007 Returns zero if successful, or non-zero if there was a problem
3008 removing a breakpoint location. */
3009
3010 int
3011 remove_breakpoints (void)
3012 {
3013 struct bp_location *bl, **blp_tmp;
3014 int val = 0;
3015
3016 ALL_BP_LOCATIONS (bl, blp_tmp)
3017 {
3018 if (bl->inserted && !is_tracepoint (bl->owner))
3019 val |= remove_breakpoint (bl);
3020 }
3021 return val;
3022 }
3023
3024 /* When a thread exits, remove breakpoints that are related to
3025 that thread. */
3026
3027 static void
3028 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3029 {
3030 struct breakpoint *b, *b_tmp;
3031
3032 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3033 {
3034 if (b->thread == tp->global_num && user_breakpoint_p (b))
3035 {
3036 b->disposition = disp_del_at_next_stop;
3037
3038 printf_filtered (_("\
3039 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3040 b->number, print_thread_id (tp));
3041
3042 /* Hide it from the user. */
3043 b->number = 0;
3044 }
3045 }
3046 }
3047
3048 /* See breakpoint.h. */
3049
3050 void
3051 remove_breakpoints_inf (inferior *inf)
3052 {
3053 struct bp_location *bl, **blp_tmp;
3054 int val;
3055
3056 ALL_BP_LOCATIONS (bl, blp_tmp)
3057 {
3058 if (bl->pspace != inf->pspace)
3059 continue;
3060
3061 if (bl->inserted && !bl->target_info.persist)
3062 {
3063 val = remove_breakpoint (bl);
3064 if (val != 0)
3065 return;
3066 }
3067 }
3068 }
3069
3070 static int internal_breakpoint_number = -1;
3071
3072 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3073 If INTERNAL is non-zero, the breakpoint number will be populated
3074 from internal_breakpoint_number and that variable decremented.
3075 Otherwise the breakpoint number will be populated from
3076 breakpoint_count and that value incremented. Internal breakpoints
3077 do not set the internal var bpnum. */
3078 static void
3079 set_breakpoint_number (int internal, struct breakpoint *b)
3080 {
3081 if (internal)
3082 b->number = internal_breakpoint_number--;
3083 else
3084 {
3085 set_breakpoint_count (breakpoint_count + 1);
3086 b->number = breakpoint_count;
3087 }
3088 }
3089
3090 static struct breakpoint *
3091 create_internal_breakpoint (struct gdbarch *gdbarch,
3092 CORE_ADDR address, enum bptype type,
3093 const struct breakpoint_ops *ops)
3094 {
3095 symtab_and_line sal;
3096 sal.pc = address;
3097 sal.section = find_pc_overlay (sal.pc);
3098 sal.pspace = current_program_space;
3099
3100 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3101 b->number = internal_breakpoint_number--;
3102 b->disposition = disp_donttouch;
3103
3104 return b;
3105 }
3106
3107 static const char *const longjmp_names[] =
3108 {
3109 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3110 };
3111 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3112
3113 /* Per-objfile data private to breakpoint.c. */
3114 struct breakpoint_objfile_data
3115 {
3116 /* Minimal symbol for "_ovly_debug_event" (if any). */
3117 struct bound_minimal_symbol overlay_msym {};
3118
3119 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3120 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3121
3122 /* True if we have looked for longjmp probes. */
3123 int longjmp_searched = 0;
3124
3125 /* SystemTap probe points for longjmp (if any). These are non-owning
3126 references. */
3127 std::vector<probe *> longjmp_probes;
3128
3129 /* Minimal symbol for "std::terminate()" (if any). */
3130 struct bound_minimal_symbol terminate_msym {};
3131
3132 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3133 struct bound_minimal_symbol exception_msym {};
3134
3135 /* True if we have looked for exception probes. */
3136 int exception_searched = 0;
3137
3138 /* SystemTap probe points for unwinding (if any). These are non-owning
3139 references. */
3140 std::vector<probe *> exception_probes;
3141 };
3142
3143 static const struct objfile_key<breakpoint_objfile_data>
3144 breakpoint_objfile_key;
3145
3146 /* Minimal symbol not found sentinel. */
3147 static struct minimal_symbol msym_not_found;
3148
3149 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3150
3151 static int
3152 msym_not_found_p (const struct minimal_symbol *msym)
3153 {
3154 return msym == &msym_not_found;
3155 }
3156
3157 /* Return per-objfile data needed by breakpoint.c.
3158 Allocate the data if necessary. */
3159
3160 static struct breakpoint_objfile_data *
3161 get_breakpoint_objfile_data (struct objfile *objfile)
3162 {
3163 struct breakpoint_objfile_data *bp_objfile_data;
3164
3165 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3166 if (bp_objfile_data == NULL)
3167 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3168 return bp_objfile_data;
3169 }
3170
3171 static void
3172 create_overlay_event_breakpoint (void)
3173 {
3174 const char *const func_name = "_ovly_debug_event";
3175
3176 for (objfile *objfile : current_program_space->objfiles ())
3177 {
3178 struct breakpoint *b;
3179 struct breakpoint_objfile_data *bp_objfile_data;
3180 CORE_ADDR addr;
3181 struct explicit_location explicit_loc;
3182
3183 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3184
3185 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3186 continue;
3187
3188 if (bp_objfile_data->overlay_msym.minsym == NULL)
3189 {
3190 struct bound_minimal_symbol m;
3191
3192 m = lookup_minimal_symbol_text (func_name, objfile);
3193 if (m.minsym == NULL)
3194 {
3195 /* Avoid future lookups in this objfile. */
3196 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3197 continue;
3198 }
3199 bp_objfile_data->overlay_msym = m;
3200 }
3201
3202 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3203 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3204 bp_overlay_event,
3205 &internal_breakpoint_ops);
3206 initialize_explicit_location (&explicit_loc);
3207 explicit_loc.function_name = ASTRDUP (func_name);
3208 b->location = new_explicit_location (&explicit_loc);
3209
3210 if (overlay_debugging == ovly_auto)
3211 {
3212 b->enable_state = bp_enabled;
3213 overlay_events_enabled = 1;
3214 }
3215 else
3216 {
3217 b->enable_state = bp_disabled;
3218 overlay_events_enabled = 0;
3219 }
3220 }
3221 }
3222
3223 static void
3224 create_longjmp_master_breakpoint (void)
3225 {
3226 struct program_space *pspace;
3227
3228 scoped_restore_current_program_space restore_pspace;
3229
3230 ALL_PSPACES (pspace)
3231 {
3232 set_current_program_space (pspace);
3233
3234 for (objfile *objfile : current_program_space->objfiles ())
3235 {
3236 int i;
3237 struct gdbarch *gdbarch;
3238 struct breakpoint_objfile_data *bp_objfile_data;
3239
3240 gdbarch = get_objfile_arch (objfile);
3241
3242 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3243
3244 if (!bp_objfile_data->longjmp_searched)
3245 {
3246 std::vector<probe *> ret
3247 = find_probes_in_objfile (objfile, "libc", "longjmp");
3248
3249 if (!ret.empty ())
3250 {
3251 /* We are only interested in checking one element. */
3252 probe *p = ret[0];
3253
3254 if (!p->can_evaluate_arguments ())
3255 {
3256 /* We cannot use the probe interface here, because it does
3257 not know how to evaluate arguments. */
3258 ret.clear ();
3259 }
3260 }
3261 bp_objfile_data->longjmp_probes = ret;
3262 bp_objfile_data->longjmp_searched = 1;
3263 }
3264
3265 if (!bp_objfile_data->longjmp_probes.empty ())
3266 {
3267 for (probe *p : bp_objfile_data->longjmp_probes)
3268 {
3269 struct breakpoint *b;
3270
3271 b = create_internal_breakpoint (gdbarch,
3272 p->get_relocated_address (objfile),
3273 bp_longjmp_master,
3274 &internal_breakpoint_ops);
3275 b->location = new_probe_location ("-probe-stap libc:longjmp");
3276 b->enable_state = bp_disabled;
3277 }
3278
3279 continue;
3280 }
3281
3282 if (!gdbarch_get_longjmp_target_p (gdbarch))
3283 continue;
3284
3285 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3286 {
3287 struct breakpoint *b;
3288 const char *func_name;
3289 CORE_ADDR addr;
3290 struct explicit_location explicit_loc;
3291
3292 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3293 continue;
3294
3295 func_name = longjmp_names[i];
3296 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3297 {
3298 struct bound_minimal_symbol m;
3299
3300 m = lookup_minimal_symbol_text (func_name, objfile);
3301 if (m.minsym == NULL)
3302 {
3303 /* Prevent future lookups in this objfile. */
3304 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3305 continue;
3306 }
3307 bp_objfile_data->longjmp_msym[i] = m;
3308 }
3309
3310 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3311 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3312 &internal_breakpoint_ops);
3313 initialize_explicit_location (&explicit_loc);
3314 explicit_loc.function_name = ASTRDUP (func_name);
3315 b->location = new_explicit_location (&explicit_loc);
3316 b->enable_state = bp_disabled;
3317 }
3318 }
3319 }
3320 }
3321
3322 /* Create a master std::terminate breakpoint. */
3323 static void
3324 create_std_terminate_master_breakpoint (void)
3325 {
3326 struct program_space *pspace;
3327 const char *const func_name = "std::terminate()";
3328
3329 scoped_restore_current_program_space restore_pspace;
3330
3331 ALL_PSPACES (pspace)
3332 {
3333 CORE_ADDR addr;
3334
3335 set_current_program_space (pspace);
3336
3337 for (objfile *objfile : current_program_space->objfiles ())
3338 {
3339 struct breakpoint *b;
3340 struct breakpoint_objfile_data *bp_objfile_data;
3341 struct explicit_location explicit_loc;
3342
3343 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3344
3345 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3346 continue;
3347
3348 if (bp_objfile_data->terminate_msym.minsym == NULL)
3349 {
3350 struct bound_minimal_symbol m;
3351
3352 m = lookup_minimal_symbol (func_name, NULL, objfile);
3353 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3354 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3355 {
3356 /* Prevent future lookups in this objfile. */
3357 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3358 continue;
3359 }
3360 bp_objfile_data->terminate_msym = m;
3361 }
3362
3363 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3364 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3365 bp_std_terminate_master,
3366 &internal_breakpoint_ops);
3367 initialize_explicit_location (&explicit_loc);
3368 explicit_loc.function_name = ASTRDUP (func_name);
3369 b->location = new_explicit_location (&explicit_loc);
3370 b->enable_state = bp_disabled;
3371 }
3372 }
3373 }
3374
3375 /* Install a master breakpoint on the unwinder's debug hook. */
3376
3377 static void
3378 create_exception_master_breakpoint (void)
3379 {
3380 const char *const func_name = "_Unwind_DebugHook";
3381
3382 for (objfile *objfile : current_program_space->objfiles ())
3383 {
3384 struct breakpoint *b;
3385 struct gdbarch *gdbarch;
3386 struct breakpoint_objfile_data *bp_objfile_data;
3387 CORE_ADDR addr;
3388 struct explicit_location explicit_loc;
3389
3390 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3391
3392 /* We prefer the SystemTap probe point if it exists. */
3393 if (!bp_objfile_data->exception_searched)
3394 {
3395 std::vector<probe *> ret
3396 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3397
3398 if (!ret.empty ())
3399 {
3400 /* We are only interested in checking one element. */
3401 probe *p = ret[0];
3402
3403 if (!p->can_evaluate_arguments ())
3404 {
3405 /* We cannot use the probe interface here, because it does
3406 not know how to evaluate arguments. */
3407 ret.clear ();
3408 }
3409 }
3410 bp_objfile_data->exception_probes = ret;
3411 bp_objfile_data->exception_searched = 1;
3412 }
3413
3414 if (!bp_objfile_data->exception_probes.empty ())
3415 {
3416 gdbarch = get_objfile_arch (objfile);
3417
3418 for (probe *p : bp_objfile_data->exception_probes)
3419 {
3420 b = create_internal_breakpoint (gdbarch,
3421 p->get_relocated_address (objfile),
3422 bp_exception_master,
3423 &internal_breakpoint_ops);
3424 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3425 b->enable_state = bp_disabled;
3426 }
3427
3428 continue;
3429 }
3430
3431 /* Otherwise, try the hook function. */
3432
3433 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3434 continue;
3435
3436 gdbarch = get_objfile_arch (objfile);
3437
3438 if (bp_objfile_data->exception_msym.minsym == NULL)
3439 {
3440 struct bound_minimal_symbol debug_hook;
3441
3442 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3443 if (debug_hook.minsym == NULL)
3444 {
3445 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3446 continue;
3447 }
3448
3449 bp_objfile_data->exception_msym = debug_hook;
3450 }
3451
3452 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3453 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3454 current_top_target ());
3455 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3456 &internal_breakpoint_ops);
3457 initialize_explicit_location (&explicit_loc);
3458 explicit_loc.function_name = ASTRDUP (func_name);
3459 b->location = new_explicit_location (&explicit_loc);
3460 b->enable_state = bp_disabled;
3461 }
3462 }
3463
3464 /* Does B have a location spec? */
3465
3466 static int
3467 breakpoint_event_location_empty_p (const struct breakpoint *b)
3468 {
3469 return b->location != NULL && event_location_empty_p (b->location.get ());
3470 }
3471
3472 void
3473 update_breakpoints_after_exec (void)
3474 {
3475 struct breakpoint *b, *b_tmp;
3476 struct bp_location *bploc, **bplocp_tmp;
3477
3478 /* We're about to delete breakpoints from GDB's lists. If the
3479 INSERTED flag is true, GDB will try to lift the breakpoints by
3480 writing the breakpoints' "shadow contents" back into memory. The
3481 "shadow contents" are NOT valid after an exec, so GDB should not
3482 do that. Instead, the target is responsible from marking
3483 breakpoints out as soon as it detects an exec. We don't do that
3484 here instead, because there may be other attempts to delete
3485 breakpoints after detecting an exec and before reaching here. */
3486 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3487 if (bploc->pspace == current_program_space)
3488 gdb_assert (!bploc->inserted);
3489
3490 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3491 {
3492 if (b->pspace != current_program_space)
3493 continue;
3494
3495 /* Solib breakpoints must be explicitly reset after an exec(). */
3496 if (b->type == bp_shlib_event)
3497 {
3498 delete_breakpoint (b);
3499 continue;
3500 }
3501
3502 /* JIT breakpoints must be explicitly reset after an exec(). */
3503 if (b->type == bp_jit_event)
3504 {
3505 delete_breakpoint (b);
3506 continue;
3507 }
3508
3509 /* Thread event breakpoints must be set anew after an exec(),
3510 as must overlay event and longjmp master breakpoints. */
3511 if (b->type == bp_thread_event || b->type == bp_overlay_event
3512 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3513 || b->type == bp_exception_master)
3514 {
3515 delete_breakpoint (b);
3516 continue;
3517 }
3518
3519 /* Step-resume breakpoints are meaningless after an exec(). */
3520 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3521 {
3522 delete_breakpoint (b);
3523 continue;
3524 }
3525
3526 /* Just like single-step breakpoints. */
3527 if (b->type == bp_single_step)
3528 {
3529 delete_breakpoint (b);
3530 continue;
3531 }
3532
3533 /* Longjmp and longjmp-resume breakpoints are also meaningless
3534 after an exec. */
3535 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3536 || b->type == bp_longjmp_call_dummy
3537 || b->type == bp_exception || b->type == bp_exception_resume)
3538 {
3539 delete_breakpoint (b);
3540 continue;
3541 }
3542
3543 if (b->type == bp_catchpoint)
3544 {
3545 /* For now, none of the bp_catchpoint breakpoints need to
3546 do anything at this point. In the future, if some of
3547 the catchpoints need to something, we will need to add
3548 a new method, and call this method from here. */
3549 continue;
3550 }
3551
3552 /* bp_finish is a special case. The only way we ought to be able
3553 to see one of these when an exec() has happened, is if the user
3554 caught a vfork, and then said "finish". Ordinarily a finish just
3555 carries them to the call-site of the current callee, by setting
3556 a temporary bp there and resuming. But in this case, the finish
3557 will carry them entirely through the vfork & exec.
3558
3559 We don't want to allow a bp_finish to remain inserted now. But
3560 we can't safely delete it, 'cause finish_command has a handle to
3561 the bp on a bpstat, and will later want to delete it. There's a
3562 chance (and I've seen it happen) that if we delete the bp_finish
3563 here, that its storage will get reused by the time finish_command
3564 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3565 We really must allow finish_command to delete a bp_finish.
3566
3567 In the absence of a general solution for the "how do we know
3568 it's safe to delete something others may have handles to?"
3569 problem, what we'll do here is just uninsert the bp_finish, and
3570 let finish_command delete it.
3571
3572 (We know the bp_finish is "doomed" in the sense that it's
3573 momentary, and will be deleted as soon as finish_command sees
3574 the inferior stopped. So it doesn't matter that the bp's
3575 address is probably bogus in the new a.out, unlike e.g., the
3576 solib breakpoints.) */
3577
3578 if (b->type == bp_finish)
3579 {
3580 continue;
3581 }
3582
3583 /* Without a symbolic address, we have little hope of the
3584 pre-exec() address meaning the same thing in the post-exec()
3585 a.out. */
3586 if (breakpoint_event_location_empty_p (b))
3587 {
3588 delete_breakpoint (b);
3589 continue;
3590 }
3591 }
3592 }
3593
3594 int
3595 detach_breakpoints (ptid_t ptid)
3596 {
3597 struct bp_location *bl, **blp_tmp;
3598 int val = 0;
3599 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3600 struct inferior *inf = current_inferior ();
3601
3602 if (ptid.pid () == inferior_ptid.pid ())
3603 error (_("Cannot detach breakpoints of inferior_ptid"));
3604
3605 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3606 inferior_ptid = ptid;
3607 ALL_BP_LOCATIONS (bl, blp_tmp)
3608 {
3609 if (bl->pspace != inf->pspace)
3610 continue;
3611
3612 /* This function must physically remove breakpoints locations
3613 from the specified ptid, without modifying the breakpoint
3614 package's state. Locations of type bp_loc_other are only
3615 maintained at GDB side. So, there is no need to remove
3616 these bp_loc_other locations. Moreover, removing these
3617 would modify the breakpoint package's state. */
3618 if (bl->loc_type == bp_loc_other)
3619 continue;
3620
3621 if (bl->inserted)
3622 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3623 }
3624
3625 return val;
3626 }
3627
3628 /* Remove the breakpoint location BL from the current address space.
3629 Note that this is used to detach breakpoints from a child fork.
3630 When we get here, the child isn't in the inferior list, and neither
3631 do we have objects to represent its address space --- we should
3632 *not* look at bl->pspace->aspace here. */
3633
3634 static int
3635 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3636 {
3637 int val;
3638
3639 /* BL is never in moribund_locations by our callers. */
3640 gdb_assert (bl->owner != NULL);
3641
3642 /* The type of none suggests that owner is actually deleted.
3643 This should not ever happen. */
3644 gdb_assert (bl->owner->type != bp_none);
3645
3646 if (bl->loc_type == bp_loc_software_breakpoint
3647 || bl->loc_type == bp_loc_hardware_breakpoint)
3648 {
3649 /* "Normal" instruction breakpoint: either the standard
3650 trap-instruction bp (bp_breakpoint), or a
3651 bp_hardware_breakpoint. */
3652
3653 /* First check to see if we have to handle an overlay. */
3654 if (overlay_debugging == ovly_off
3655 || bl->section == NULL
3656 || !(section_is_overlay (bl->section)))
3657 {
3658 /* No overlay handling: just remove the breakpoint. */
3659
3660 /* If we're trying to uninsert a memory breakpoint that we
3661 know is set in a dynamic object that is marked
3662 shlib_disabled, then either the dynamic object was
3663 removed with "remove-symbol-file" or with
3664 "nosharedlibrary". In the former case, we don't know
3665 whether another dynamic object might have loaded over the
3666 breakpoint's address -- the user might well let us know
3667 about it next with add-symbol-file (the whole point of
3668 add-symbol-file is letting the user manually maintain a
3669 list of dynamically loaded objects). If we have the
3670 breakpoint's shadow memory, that is, this is a software
3671 breakpoint managed by GDB, check whether the breakpoint
3672 is still inserted in memory, to avoid overwriting wrong
3673 code with stale saved shadow contents. Note that HW
3674 breakpoints don't have shadow memory, as they're
3675 implemented using a mechanism that is not dependent on
3676 being able to modify the target's memory, and as such
3677 they should always be removed. */
3678 if (bl->shlib_disabled
3679 && bl->target_info.shadow_len != 0
3680 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3681 val = 0;
3682 else
3683 val = bl->owner->ops->remove_location (bl, reason);
3684 }
3685 else
3686 {
3687 /* This breakpoint is in an overlay section.
3688 Did we set a breakpoint at the LMA? */
3689 if (!overlay_events_enabled)
3690 {
3691 /* Yes -- overlay event support is not active, so we
3692 should have set a breakpoint at the LMA. Remove it.
3693 */
3694 /* Ignore any failures: if the LMA is in ROM, we will
3695 have already warned when we failed to insert it. */
3696 if (bl->loc_type == bp_loc_hardware_breakpoint)
3697 target_remove_hw_breakpoint (bl->gdbarch,
3698 &bl->overlay_target_info);
3699 else
3700 target_remove_breakpoint (bl->gdbarch,
3701 &bl->overlay_target_info,
3702 reason);
3703 }
3704 /* Did we set a breakpoint at the VMA?
3705 If so, we will have marked the breakpoint 'inserted'. */
3706 if (bl->inserted)
3707 {
3708 /* Yes -- remove it. Previously we did not bother to
3709 remove the breakpoint if the section had been
3710 unmapped, but let's not rely on that being safe. We
3711 don't know what the overlay manager might do. */
3712
3713 /* However, we should remove *software* breakpoints only
3714 if the section is still mapped, or else we overwrite
3715 wrong code with the saved shadow contents. */
3716 if (bl->loc_type == bp_loc_hardware_breakpoint
3717 || section_is_mapped (bl->section))
3718 val = bl->owner->ops->remove_location (bl, reason);
3719 else
3720 val = 0;
3721 }
3722 else
3723 {
3724 /* No -- not inserted, so no need to remove. No error. */
3725 val = 0;
3726 }
3727 }
3728
3729 /* In some cases, we might not be able to remove a breakpoint in
3730 a shared library that has already been removed, but we have
3731 not yet processed the shlib unload event. Similarly for an
3732 unloaded add-symbol-file object - the user might not yet have
3733 had the chance to remove-symbol-file it. shlib_disabled will
3734 be set if the library/object has already been removed, but
3735 the breakpoint hasn't been uninserted yet, e.g., after
3736 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3737 always-inserted mode. */
3738 if (val
3739 && (bl->loc_type == bp_loc_software_breakpoint
3740 && (bl->shlib_disabled
3741 || solib_name_from_address (bl->pspace, bl->address)
3742 || shared_objfile_contains_address_p (bl->pspace,
3743 bl->address))))
3744 val = 0;
3745
3746 if (val)
3747 return val;
3748 bl->inserted = (reason == DETACH_BREAKPOINT);
3749 }
3750 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3751 {
3752 gdb_assert (bl->owner->ops != NULL
3753 && bl->owner->ops->remove_location != NULL);
3754
3755 bl->inserted = (reason == DETACH_BREAKPOINT);
3756 bl->owner->ops->remove_location (bl, reason);
3757
3758 /* Failure to remove any of the hardware watchpoints comes here. */
3759 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3760 warning (_("Could not remove hardware watchpoint %d."),
3761 bl->owner->number);
3762 }
3763 else if (bl->owner->type == bp_catchpoint
3764 && breakpoint_enabled (bl->owner)
3765 && !bl->duplicate)
3766 {
3767 gdb_assert (bl->owner->ops != NULL
3768 && bl->owner->ops->remove_location != NULL);
3769
3770 val = bl->owner->ops->remove_location (bl, reason);
3771 if (val)
3772 return val;
3773
3774 bl->inserted = (reason == DETACH_BREAKPOINT);
3775 }
3776
3777 return 0;
3778 }
3779
3780 static int
3781 remove_breakpoint (struct bp_location *bl)
3782 {
3783 /* BL is never in moribund_locations by our callers. */
3784 gdb_assert (bl->owner != NULL);
3785
3786 /* The type of none suggests that owner is actually deleted.
3787 This should not ever happen. */
3788 gdb_assert (bl->owner->type != bp_none);
3789
3790 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3791
3792 switch_to_program_space_and_thread (bl->pspace);
3793
3794 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3795 }
3796
3797 /* Clear the "inserted" flag in all breakpoints. */
3798
3799 void
3800 mark_breakpoints_out (void)
3801 {
3802 struct bp_location *bl, **blp_tmp;
3803
3804 ALL_BP_LOCATIONS (bl, blp_tmp)
3805 if (bl->pspace == current_program_space)
3806 bl->inserted = 0;
3807 }
3808
3809 /* Clear the "inserted" flag in all breakpoints and delete any
3810 breakpoints which should go away between runs of the program.
3811
3812 Plus other such housekeeping that has to be done for breakpoints
3813 between runs.
3814
3815 Note: this function gets called at the end of a run (by
3816 generic_mourn_inferior) and when a run begins (by
3817 init_wait_for_inferior). */
3818
3819
3820
3821 void
3822 breakpoint_init_inferior (enum inf_context context)
3823 {
3824 struct breakpoint *b, *b_tmp;
3825 struct program_space *pspace = current_program_space;
3826
3827 /* If breakpoint locations are shared across processes, then there's
3828 nothing to do. */
3829 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3830 return;
3831
3832 mark_breakpoints_out ();
3833
3834 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3835 {
3836 if (b->loc && b->loc->pspace != pspace)
3837 continue;
3838
3839 switch (b->type)
3840 {
3841 case bp_call_dummy:
3842 case bp_longjmp_call_dummy:
3843
3844 /* If the call dummy breakpoint is at the entry point it will
3845 cause problems when the inferior is rerun, so we better get
3846 rid of it. */
3847
3848 case bp_watchpoint_scope:
3849
3850 /* Also get rid of scope breakpoints. */
3851
3852 case bp_shlib_event:
3853
3854 /* Also remove solib event breakpoints. Their addresses may
3855 have changed since the last time we ran the program.
3856 Actually we may now be debugging against different target;
3857 and so the solib backend that installed this breakpoint may
3858 not be used in by the target. E.g.,
3859
3860 (gdb) file prog-linux
3861 (gdb) run # native linux target
3862 ...
3863 (gdb) kill
3864 (gdb) file prog-win.exe
3865 (gdb) tar rem :9999 # remote Windows gdbserver.
3866 */
3867
3868 case bp_step_resume:
3869
3870 /* Also remove step-resume breakpoints. */
3871
3872 case bp_single_step:
3873
3874 /* Also remove single-step breakpoints. */
3875
3876 delete_breakpoint (b);
3877 break;
3878
3879 case bp_watchpoint:
3880 case bp_hardware_watchpoint:
3881 case bp_read_watchpoint:
3882 case bp_access_watchpoint:
3883 {
3884 struct watchpoint *w = (struct watchpoint *) b;
3885
3886 /* Likewise for watchpoints on local expressions. */
3887 if (w->exp_valid_block != NULL)
3888 delete_breakpoint (b);
3889 else
3890 {
3891 /* Get rid of existing locations, which are no longer
3892 valid. New ones will be created in
3893 update_watchpoint, when the inferior is restarted.
3894 The next update_global_location_list call will
3895 garbage collect them. */
3896 b->loc = NULL;
3897
3898 if (context == inf_starting)
3899 {
3900 /* Reset val field to force reread of starting value in
3901 insert_breakpoints. */
3902 w->val.reset (nullptr);
3903 w->val_valid = false;
3904 }
3905 }
3906 }
3907 break;
3908 default:
3909 break;
3910 }
3911 }
3912
3913 /* Get rid of the moribund locations. */
3914 for (bp_location *bl : moribund_locations)
3915 decref_bp_location (&bl);
3916 moribund_locations.clear ();
3917 }
3918
3919 /* These functions concern about actual breakpoints inserted in the
3920 target --- to e.g. check if we need to do decr_pc adjustment or if
3921 we need to hop over the bkpt --- so we check for address space
3922 match, not program space. */
3923
3924 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3925 exists at PC. It returns ordinary_breakpoint_here if it's an
3926 ordinary breakpoint, or permanent_breakpoint_here if it's a
3927 permanent breakpoint.
3928 - When continuing from a location with an ordinary breakpoint, we
3929 actually single step once before calling insert_breakpoints.
3930 - When continuing from a location with a permanent breakpoint, we
3931 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3932 the target, to advance the PC past the breakpoint. */
3933
3934 enum breakpoint_here
3935 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3936 {
3937 struct bp_location *bl, **blp_tmp;
3938 int any_breakpoint_here = 0;
3939
3940 ALL_BP_LOCATIONS (bl, blp_tmp)
3941 {
3942 if (bl->loc_type != bp_loc_software_breakpoint
3943 && bl->loc_type != bp_loc_hardware_breakpoint)
3944 continue;
3945
3946 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3947 if ((breakpoint_enabled (bl->owner)
3948 || bl->permanent)
3949 && breakpoint_location_address_match (bl, aspace, pc))
3950 {
3951 if (overlay_debugging
3952 && section_is_overlay (bl->section)
3953 && !section_is_mapped (bl->section))
3954 continue; /* unmapped overlay -- can't be a match */
3955 else if (bl->permanent)
3956 return permanent_breakpoint_here;
3957 else
3958 any_breakpoint_here = 1;
3959 }
3960 }
3961
3962 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
3963 }
3964
3965 /* See breakpoint.h. */
3966
3967 int
3968 breakpoint_in_range_p (const address_space *aspace,
3969 CORE_ADDR addr, ULONGEST len)
3970 {
3971 struct bp_location *bl, **blp_tmp;
3972
3973 ALL_BP_LOCATIONS (bl, blp_tmp)
3974 {
3975 if (bl->loc_type != bp_loc_software_breakpoint
3976 && bl->loc_type != bp_loc_hardware_breakpoint)
3977 continue;
3978
3979 if ((breakpoint_enabled (bl->owner)
3980 || bl->permanent)
3981 && breakpoint_location_address_range_overlap (bl, aspace,
3982 addr, len))
3983 {
3984 if (overlay_debugging
3985 && section_is_overlay (bl->section)
3986 && !section_is_mapped (bl->section))
3987 {
3988 /* Unmapped overlay -- can't be a match. */
3989 continue;
3990 }
3991
3992 return 1;
3993 }
3994 }
3995
3996 return 0;
3997 }
3998
3999 /* Return true if there's a moribund breakpoint at PC. */
4000
4001 int
4002 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4003 {
4004 for (bp_location *loc : moribund_locations)
4005 if (breakpoint_location_address_match (loc, aspace, pc))
4006 return 1;
4007
4008 return 0;
4009 }
4010
4011 /* Returns non-zero iff BL is inserted at PC, in address space
4012 ASPACE. */
4013
4014 static int
4015 bp_location_inserted_here_p (struct bp_location *bl,
4016 const address_space *aspace, CORE_ADDR pc)
4017 {
4018 if (bl->inserted
4019 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4020 aspace, pc))
4021 {
4022 if (overlay_debugging
4023 && section_is_overlay (bl->section)
4024 && !section_is_mapped (bl->section))
4025 return 0; /* unmapped overlay -- can't be a match */
4026 else
4027 return 1;
4028 }
4029 return 0;
4030 }
4031
4032 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4033
4034 int
4035 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4036 {
4037 struct bp_location **blp, **blp_tmp = NULL;
4038
4039 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4040 {
4041 struct bp_location *bl = *blp;
4042
4043 if (bl->loc_type != bp_loc_software_breakpoint
4044 && bl->loc_type != bp_loc_hardware_breakpoint)
4045 continue;
4046
4047 if (bp_location_inserted_here_p (bl, aspace, pc))
4048 return 1;
4049 }
4050 return 0;
4051 }
4052
4053 /* This function returns non-zero iff there is a software breakpoint
4054 inserted at PC. */
4055
4056 int
4057 software_breakpoint_inserted_here_p (const address_space *aspace,
4058 CORE_ADDR pc)
4059 {
4060 struct bp_location **blp, **blp_tmp = NULL;
4061
4062 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4063 {
4064 struct bp_location *bl = *blp;
4065
4066 if (bl->loc_type != bp_loc_software_breakpoint)
4067 continue;
4068
4069 if (bp_location_inserted_here_p (bl, aspace, pc))
4070 return 1;
4071 }
4072
4073 return 0;
4074 }
4075
4076 /* See breakpoint.h. */
4077
4078 int
4079 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4080 CORE_ADDR pc)
4081 {
4082 struct bp_location **blp, **blp_tmp = NULL;
4083
4084 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4085 {
4086 struct bp_location *bl = *blp;
4087
4088 if (bl->loc_type != bp_loc_hardware_breakpoint)
4089 continue;
4090
4091 if (bp_location_inserted_here_p (bl, aspace, pc))
4092 return 1;
4093 }
4094
4095 return 0;
4096 }
4097
4098 int
4099 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4100 CORE_ADDR addr, ULONGEST len)
4101 {
4102 struct breakpoint *bpt;
4103
4104 ALL_BREAKPOINTS (bpt)
4105 {
4106 struct bp_location *loc;
4107
4108 if (bpt->type != bp_hardware_watchpoint
4109 && bpt->type != bp_access_watchpoint)
4110 continue;
4111
4112 if (!breakpoint_enabled (bpt))
4113 continue;
4114
4115 for (loc = bpt->loc; loc; loc = loc->next)
4116 if (loc->pspace->aspace == aspace && loc->inserted)
4117 {
4118 CORE_ADDR l, h;
4119
4120 /* Check for intersection. */
4121 l = std::max<CORE_ADDR> (loc->address, addr);
4122 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4123 if (l < h)
4124 return 1;
4125 }
4126 }
4127 return 0;
4128 }
4129
4130 /* See breakpoint.h. */
4131
4132 bool
4133 is_catchpoint (struct breakpoint *b)
4134 {
4135 return (b->type == bp_catchpoint);
4136 }
4137
4138 /* Frees any storage that is part of a bpstat. Does not walk the
4139 'next' chain. */
4140
4141 bpstats::~bpstats ()
4142 {
4143 if (bp_location_at != NULL)
4144 decref_bp_location (&bp_location_at);
4145 }
4146
4147 /* Clear a bpstat so that it says we are not at any breakpoint.
4148 Also free any storage that is part of a bpstat. */
4149
4150 void
4151 bpstat_clear (bpstat *bsp)
4152 {
4153 bpstat p;
4154 bpstat q;
4155
4156 if (bsp == 0)
4157 return;
4158 p = *bsp;
4159 while (p != NULL)
4160 {
4161 q = p->next;
4162 delete p;
4163 p = q;
4164 }
4165 *bsp = NULL;
4166 }
4167
4168 bpstats::bpstats (const bpstats &other)
4169 : next (NULL),
4170 bp_location_at (other.bp_location_at),
4171 breakpoint_at (other.breakpoint_at),
4172 commands (other.commands),
4173 print (other.print),
4174 stop (other.stop),
4175 print_it (other.print_it)
4176 {
4177 if (other.old_val != NULL)
4178 old_val = release_value (value_copy (other.old_val.get ()));
4179 incref_bp_location (bp_location_at);
4180 }
4181
4182 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4183 is part of the bpstat is copied as well. */
4184
4185 bpstat
4186 bpstat_copy (bpstat bs)
4187 {
4188 bpstat p = NULL;
4189 bpstat tmp;
4190 bpstat retval = NULL;
4191
4192 if (bs == NULL)
4193 return bs;
4194
4195 for (; bs != NULL; bs = bs->next)
4196 {
4197 tmp = new bpstats (*bs);
4198
4199 if (p == NULL)
4200 /* This is the first thing in the chain. */
4201 retval = tmp;
4202 else
4203 p->next = tmp;
4204 p = tmp;
4205 }
4206 p->next = NULL;
4207 return retval;
4208 }
4209
4210 /* Find the bpstat associated with this breakpoint. */
4211
4212 bpstat
4213 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4214 {
4215 if (bsp == NULL)
4216 return NULL;
4217
4218 for (; bsp != NULL; bsp = bsp->next)
4219 {
4220 if (bsp->breakpoint_at == breakpoint)
4221 return bsp;
4222 }
4223 return NULL;
4224 }
4225
4226 /* See breakpoint.h. */
4227
4228 bool
4229 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4230 {
4231 for (; bsp != NULL; bsp = bsp->next)
4232 {
4233 if (bsp->breakpoint_at == NULL)
4234 {
4235 /* A moribund location can never explain a signal other than
4236 GDB_SIGNAL_TRAP. */
4237 if (sig == GDB_SIGNAL_TRAP)
4238 return true;
4239 }
4240 else
4241 {
4242 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4243 sig))
4244 return true;
4245 }
4246 }
4247
4248 return false;
4249 }
4250
4251 /* Put in *NUM the breakpoint number of the first breakpoint we are
4252 stopped at. *BSP upon return is a bpstat which points to the
4253 remaining breakpoints stopped at (but which is not guaranteed to be
4254 good for anything but further calls to bpstat_num).
4255
4256 Return 0 if passed a bpstat which does not indicate any breakpoints.
4257 Return -1 if stopped at a breakpoint that has been deleted since
4258 we set it.
4259 Return 1 otherwise. */
4260
4261 int
4262 bpstat_num (bpstat *bsp, int *num)
4263 {
4264 struct breakpoint *b;
4265
4266 if ((*bsp) == NULL)
4267 return 0; /* No more breakpoint values */
4268
4269 /* We assume we'll never have several bpstats that correspond to a
4270 single breakpoint -- otherwise, this function might return the
4271 same number more than once and this will look ugly. */
4272 b = (*bsp)->breakpoint_at;
4273 *bsp = (*bsp)->next;
4274 if (b == NULL)
4275 return -1; /* breakpoint that's been deleted since */
4276
4277 *num = b->number; /* We have its number */
4278 return 1;
4279 }
4280
4281 /* See breakpoint.h. */
4282
4283 void
4284 bpstat_clear_actions (void)
4285 {
4286 bpstat bs;
4287
4288 if (inferior_ptid == null_ptid)
4289 return;
4290
4291 thread_info *tp = inferior_thread ();
4292 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4293 {
4294 bs->commands = NULL;
4295 bs->old_val.reset (nullptr);
4296 }
4297 }
4298
4299 /* Called when a command is about to proceed the inferior. */
4300
4301 static void
4302 breakpoint_about_to_proceed (void)
4303 {
4304 if (inferior_ptid != null_ptid)
4305 {
4306 struct thread_info *tp = inferior_thread ();
4307
4308 /* Allow inferior function calls in breakpoint commands to not
4309 interrupt the command list. When the call finishes
4310 successfully, the inferior will be standing at the same
4311 breakpoint as if nothing happened. */
4312 if (tp->control.in_infcall)
4313 return;
4314 }
4315
4316 breakpoint_proceeded = 1;
4317 }
4318
4319 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4320 or its equivalent. */
4321
4322 static int
4323 command_line_is_silent (struct command_line *cmd)
4324 {
4325 return cmd && (strcmp ("silent", cmd->line) == 0);
4326 }
4327
4328 /* Execute all the commands associated with all the breakpoints at
4329 this location. Any of these commands could cause the process to
4330 proceed beyond this point, etc. We look out for such changes by
4331 checking the global "breakpoint_proceeded" after each command.
4332
4333 Returns true if a breakpoint command resumed the inferior. In that
4334 case, it is the caller's responsibility to recall it again with the
4335 bpstat of the current thread. */
4336
4337 static int
4338 bpstat_do_actions_1 (bpstat *bsp)
4339 {
4340 bpstat bs;
4341 int again = 0;
4342
4343 /* Avoid endless recursion if a `source' command is contained
4344 in bs->commands. */
4345 if (executing_breakpoint_commands)
4346 return 0;
4347
4348 scoped_restore save_executing
4349 = make_scoped_restore (&executing_breakpoint_commands, 1);
4350
4351 scoped_restore preventer = prevent_dont_repeat ();
4352
4353 /* This pointer will iterate over the list of bpstat's. */
4354 bs = *bsp;
4355
4356 breakpoint_proceeded = 0;
4357 for (; bs != NULL; bs = bs->next)
4358 {
4359 struct command_line *cmd = NULL;
4360
4361 /* Take ownership of the BSP's command tree, if it has one.
4362
4363 The command tree could legitimately contain commands like
4364 'step' and 'next', which call clear_proceed_status, which
4365 frees stop_bpstat's command tree. To make sure this doesn't
4366 free the tree we're executing out from under us, we need to
4367 take ownership of the tree ourselves. Since a given bpstat's
4368 commands are only executed once, we don't need to copy it; we
4369 can clear the pointer in the bpstat, and make sure we free
4370 the tree when we're done. */
4371 counted_command_line ccmd = bs->commands;
4372 bs->commands = NULL;
4373 if (ccmd != NULL)
4374 cmd = ccmd.get ();
4375 if (command_line_is_silent (cmd))
4376 {
4377 /* The action has been already done by bpstat_stop_status. */
4378 cmd = cmd->next;
4379 }
4380
4381 while (cmd != NULL)
4382 {
4383 execute_control_command (cmd);
4384
4385 if (breakpoint_proceeded)
4386 break;
4387 else
4388 cmd = cmd->next;
4389 }
4390
4391 if (breakpoint_proceeded)
4392 {
4393 if (current_ui->async)
4394 /* If we are in async mode, then the target might be still
4395 running, not stopped at any breakpoint, so nothing for
4396 us to do here -- just return to the event loop. */
4397 ;
4398 else
4399 /* In sync mode, when execute_control_command returns
4400 we're already standing on the next breakpoint.
4401 Breakpoint commands for that stop were not run, since
4402 execute_command does not run breakpoint commands --
4403 only command_line_handler does, but that one is not
4404 involved in execution of breakpoint commands. So, we
4405 can now execute breakpoint commands. It should be
4406 noted that making execute_command do bpstat actions is
4407 not an option -- in this case we'll have recursive
4408 invocation of bpstat for each breakpoint with a
4409 command, and can easily blow up GDB stack. Instead, we
4410 return true, which will trigger the caller to recall us
4411 with the new stop_bpstat. */
4412 again = 1;
4413 break;
4414 }
4415 }
4416 return again;
4417 }
4418
4419 /* Helper for bpstat_do_actions. Get the current thread, if there's
4420 one, is alive and has execution. Return NULL otherwise. */
4421
4422 static thread_info *
4423 get_bpstat_thread ()
4424 {
4425 if (inferior_ptid == null_ptid || !target_has_execution)
4426 return NULL;
4427
4428 thread_info *tp = inferior_thread ();
4429 if (tp->state == THREAD_EXITED || tp->executing)
4430 return NULL;
4431 return tp;
4432 }
4433
4434 void
4435 bpstat_do_actions (void)
4436 {
4437 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4438 thread_info *tp;
4439
4440 /* Do any commands attached to breakpoint we are stopped at. */
4441 while ((tp = get_bpstat_thread ()) != NULL)
4442 {
4443 /* Since in sync mode, bpstat_do_actions may resume the
4444 inferior, and only return when it is stopped at the next
4445 breakpoint, we keep doing breakpoint actions until it returns
4446 false to indicate the inferior was not resumed. */
4447 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4448 break;
4449 }
4450
4451 cleanup_if_error.release ();
4452 }
4453
4454 /* Print out the (old or new) value associated with a watchpoint. */
4455
4456 static void
4457 watchpoint_value_print (struct value *val, struct ui_file *stream)
4458 {
4459 if (val == NULL)
4460 fprintf_unfiltered (stream, _("<unreadable>"));
4461 else
4462 {
4463 struct value_print_options opts;
4464 get_user_print_options (&opts);
4465 value_print (val, stream, &opts);
4466 }
4467 }
4468
4469 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4470 debugging multiple threads. */
4471
4472 void
4473 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4474 {
4475 if (uiout->is_mi_like_p ())
4476 return;
4477
4478 uiout->text ("\n");
4479
4480 if (show_thread_that_caused_stop ())
4481 {
4482 const char *name;
4483 struct thread_info *thr = inferior_thread ();
4484
4485 uiout->text ("Thread ");
4486 uiout->field_string ("thread-id", print_thread_id (thr));
4487
4488 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4489 if (name != NULL)
4490 {
4491 uiout->text (" \"");
4492 uiout->field_string ("name", name);
4493 uiout->text ("\"");
4494 }
4495
4496 uiout->text (" hit ");
4497 }
4498 }
4499
4500 /* Generic routine for printing messages indicating why we
4501 stopped. The behavior of this function depends on the value
4502 'print_it' in the bpstat structure. Under some circumstances we
4503 may decide not to print anything here and delegate the task to
4504 normal_stop(). */
4505
4506 static enum print_stop_action
4507 print_bp_stop_message (bpstat bs)
4508 {
4509 switch (bs->print_it)
4510 {
4511 case print_it_noop:
4512 /* Nothing should be printed for this bpstat entry. */
4513 return PRINT_UNKNOWN;
4514 break;
4515
4516 case print_it_done:
4517 /* We still want to print the frame, but we already printed the
4518 relevant messages. */
4519 return PRINT_SRC_AND_LOC;
4520 break;
4521
4522 case print_it_normal:
4523 {
4524 struct breakpoint *b = bs->breakpoint_at;
4525
4526 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4527 which has since been deleted. */
4528 if (b == NULL)
4529 return PRINT_UNKNOWN;
4530
4531 /* Normal case. Call the breakpoint's print_it method. */
4532 return b->ops->print_it (bs);
4533 }
4534 break;
4535
4536 default:
4537 internal_error (__FILE__, __LINE__,
4538 _("print_bp_stop_message: unrecognized enum value"));
4539 break;
4540 }
4541 }
4542
4543 /* A helper function that prints a shared library stopped event. */
4544
4545 static void
4546 print_solib_event (int is_catchpoint)
4547 {
4548 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4549 bool any_added = !current_program_space->added_solibs.empty ();
4550
4551 if (!is_catchpoint)
4552 {
4553 if (any_added || any_deleted)
4554 current_uiout->text (_("Stopped due to shared library event:\n"));
4555 else
4556 current_uiout->text (_("Stopped due to shared library event (no "
4557 "libraries added or removed)\n"));
4558 }
4559
4560 if (current_uiout->is_mi_like_p ())
4561 current_uiout->field_string ("reason",
4562 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4563
4564 if (any_deleted)
4565 {
4566 current_uiout->text (_(" Inferior unloaded "));
4567 ui_out_emit_list list_emitter (current_uiout, "removed");
4568 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4569 {
4570 const std::string &name = current_program_space->deleted_solibs[ix];
4571
4572 if (ix > 0)
4573 current_uiout->text (" ");
4574 current_uiout->field_string ("library", name);
4575 current_uiout->text ("\n");
4576 }
4577 }
4578
4579 if (any_added)
4580 {
4581 current_uiout->text (_(" Inferior loaded "));
4582 ui_out_emit_list list_emitter (current_uiout, "added");
4583 bool first = true;
4584 for (so_list *iter : current_program_space->added_solibs)
4585 {
4586 if (!first)
4587 current_uiout->text (" ");
4588 first = false;
4589 current_uiout->field_string ("library", iter->so_name);
4590 current_uiout->text ("\n");
4591 }
4592 }
4593 }
4594
4595 /* Print a message indicating what happened. This is called from
4596 normal_stop(). The input to this routine is the head of the bpstat
4597 list - a list of the eventpoints that caused this stop. KIND is
4598 the target_waitkind for the stopping event. This
4599 routine calls the generic print routine for printing a message
4600 about reasons for stopping. This will print (for example) the
4601 "Breakpoint n," part of the output. The return value of this
4602 routine is one of:
4603
4604 PRINT_UNKNOWN: Means we printed nothing.
4605 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4606 code to print the location. An example is
4607 "Breakpoint 1, " which should be followed by
4608 the location.
4609 PRINT_SRC_ONLY: Means we printed something, but there is no need
4610 to also print the location part of the message.
4611 An example is the catch/throw messages, which
4612 don't require a location appended to the end.
4613 PRINT_NOTHING: We have done some printing and we don't need any
4614 further info to be printed. */
4615
4616 enum print_stop_action
4617 bpstat_print (bpstat bs, int kind)
4618 {
4619 enum print_stop_action val;
4620
4621 /* Maybe another breakpoint in the chain caused us to stop.
4622 (Currently all watchpoints go on the bpstat whether hit or not.
4623 That probably could (should) be changed, provided care is taken
4624 with respect to bpstat_explains_signal). */
4625 for (; bs; bs = bs->next)
4626 {
4627 val = print_bp_stop_message (bs);
4628 if (val == PRINT_SRC_ONLY
4629 || val == PRINT_SRC_AND_LOC
4630 || val == PRINT_NOTHING)
4631 return val;
4632 }
4633
4634 /* If we had hit a shared library event breakpoint,
4635 print_bp_stop_message would print out this message. If we hit an
4636 OS-level shared library event, do the same thing. */
4637 if (kind == TARGET_WAITKIND_LOADED)
4638 {
4639 print_solib_event (0);
4640 return PRINT_NOTHING;
4641 }
4642
4643 /* We reached the end of the chain, or we got a null BS to start
4644 with and nothing was printed. */
4645 return PRINT_UNKNOWN;
4646 }
4647
4648 /* Evaluate the boolean expression EXP and return the result. */
4649
4650 static bool
4651 breakpoint_cond_eval (expression *exp)
4652 {
4653 struct value *mark = value_mark ();
4654 bool res = value_true (evaluate_expression (exp));
4655
4656 value_free_to_mark (mark);
4657 return res;
4658 }
4659
4660 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4661
4662 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4663 : next (NULL),
4664 bp_location_at (bl),
4665 breakpoint_at (bl->owner),
4666 commands (NULL),
4667 print (0),
4668 stop (0),
4669 print_it (print_it_normal)
4670 {
4671 incref_bp_location (bl);
4672 **bs_link_pointer = this;
4673 *bs_link_pointer = &next;
4674 }
4675
4676 bpstats::bpstats ()
4677 : next (NULL),
4678 bp_location_at (NULL),
4679 breakpoint_at (NULL),
4680 commands (NULL),
4681 print (0),
4682 stop (0),
4683 print_it (print_it_normal)
4684 {
4685 }
4686 \f
4687 /* The target has stopped with waitstatus WS. Check if any hardware
4688 watchpoints have triggered, according to the target. */
4689
4690 int
4691 watchpoints_triggered (struct target_waitstatus *ws)
4692 {
4693 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4694 CORE_ADDR addr;
4695 struct breakpoint *b;
4696
4697 if (!stopped_by_watchpoint)
4698 {
4699 /* We were not stopped by a watchpoint. Mark all watchpoints
4700 as not triggered. */
4701 ALL_BREAKPOINTS (b)
4702 if (is_hardware_watchpoint (b))
4703 {
4704 struct watchpoint *w = (struct watchpoint *) b;
4705
4706 w->watchpoint_triggered = watch_triggered_no;
4707 }
4708
4709 return 0;
4710 }
4711
4712 if (!target_stopped_data_address (current_top_target (), &addr))
4713 {
4714 /* We were stopped by a watchpoint, but we don't know where.
4715 Mark all watchpoints as unknown. */
4716 ALL_BREAKPOINTS (b)
4717 if (is_hardware_watchpoint (b))
4718 {
4719 struct watchpoint *w = (struct watchpoint *) b;
4720
4721 w->watchpoint_triggered = watch_triggered_unknown;
4722 }
4723
4724 return 1;
4725 }
4726
4727 /* The target could report the data address. Mark watchpoints
4728 affected by this data address as triggered, and all others as not
4729 triggered. */
4730
4731 ALL_BREAKPOINTS (b)
4732 if (is_hardware_watchpoint (b))
4733 {
4734 struct watchpoint *w = (struct watchpoint *) b;
4735 struct bp_location *loc;
4736
4737 w->watchpoint_triggered = watch_triggered_no;
4738 for (loc = b->loc; loc; loc = loc->next)
4739 {
4740 if (is_masked_watchpoint (b))
4741 {
4742 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4743 CORE_ADDR start = loc->address & w->hw_wp_mask;
4744
4745 if (newaddr == start)
4746 {
4747 w->watchpoint_triggered = watch_triggered_yes;
4748 break;
4749 }
4750 }
4751 /* Exact match not required. Within range is sufficient. */
4752 else if (target_watchpoint_addr_within_range (current_top_target (),
4753 addr, loc->address,
4754 loc->length))
4755 {
4756 w->watchpoint_triggered = watch_triggered_yes;
4757 break;
4758 }
4759 }
4760 }
4761
4762 return 1;
4763 }
4764
4765 /* Possible return values for watchpoint_check. */
4766 enum wp_check_result
4767 {
4768 /* The watchpoint has been deleted. */
4769 WP_DELETED = 1,
4770
4771 /* The value has changed. */
4772 WP_VALUE_CHANGED = 2,
4773
4774 /* The value has not changed. */
4775 WP_VALUE_NOT_CHANGED = 3,
4776
4777 /* Ignore this watchpoint, no matter if the value changed or not. */
4778 WP_IGNORE = 4,
4779 };
4780
4781 #define BP_TEMPFLAG 1
4782 #define BP_HARDWAREFLAG 2
4783
4784 /* Evaluate watchpoint condition expression and check if its value
4785 changed. */
4786
4787 static wp_check_result
4788 watchpoint_check (bpstat bs)
4789 {
4790 struct watchpoint *b;
4791 struct frame_info *fr;
4792 int within_current_scope;
4793
4794 /* BS is built from an existing struct breakpoint. */
4795 gdb_assert (bs->breakpoint_at != NULL);
4796 b = (struct watchpoint *) bs->breakpoint_at;
4797
4798 /* If this is a local watchpoint, we only want to check if the
4799 watchpoint frame is in scope if the current thread is the thread
4800 that was used to create the watchpoint. */
4801 if (!watchpoint_in_thread_scope (b))
4802 return WP_IGNORE;
4803
4804 if (b->exp_valid_block == NULL)
4805 within_current_scope = 1;
4806 else
4807 {
4808 struct frame_info *frame = get_current_frame ();
4809 struct gdbarch *frame_arch = get_frame_arch (frame);
4810 CORE_ADDR frame_pc = get_frame_pc (frame);
4811
4812 /* stack_frame_destroyed_p() returns a non-zero value if we're
4813 still in the function but the stack frame has already been
4814 invalidated. Since we can't rely on the values of local
4815 variables after the stack has been destroyed, we are treating
4816 the watchpoint in that state as `not changed' without further
4817 checking. Don't mark watchpoints as changed if the current
4818 frame is in an epilogue - even if they are in some other
4819 frame, our view of the stack is likely to be wrong and
4820 frame_find_by_id could error out. */
4821 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4822 return WP_IGNORE;
4823
4824 fr = frame_find_by_id (b->watchpoint_frame);
4825 within_current_scope = (fr != NULL);
4826
4827 /* If we've gotten confused in the unwinder, we might have
4828 returned a frame that can't describe this variable. */
4829 if (within_current_scope)
4830 {
4831 struct symbol *function;
4832
4833 function = get_frame_function (fr);
4834 if (function == NULL
4835 || !contained_in (b->exp_valid_block,
4836 SYMBOL_BLOCK_VALUE (function)))
4837 within_current_scope = 0;
4838 }
4839
4840 if (within_current_scope)
4841 /* If we end up stopping, the current frame will get selected
4842 in normal_stop. So this call to select_frame won't affect
4843 the user. */
4844 select_frame (fr);
4845 }
4846
4847 if (within_current_scope)
4848 {
4849 /* We use value_{,free_to_}mark because it could be a *long*
4850 time before we return to the command level and call
4851 free_all_values. We can't call free_all_values because we
4852 might be in the middle of evaluating a function call. */
4853
4854 int pc = 0;
4855 struct value *mark;
4856 struct value *new_val;
4857
4858 if (is_masked_watchpoint (b))
4859 /* Since we don't know the exact trigger address (from
4860 stopped_data_address), just tell the user we've triggered
4861 a mask watchpoint. */
4862 return WP_VALUE_CHANGED;
4863
4864 mark = value_mark ();
4865 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4866
4867 if (b->val_bitsize != 0)
4868 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4869
4870 /* We use value_equal_contents instead of value_equal because
4871 the latter coerces an array to a pointer, thus comparing just
4872 the address of the array instead of its contents. This is
4873 not what we want. */
4874 if ((b->val != NULL) != (new_val != NULL)
4875 || (b->val != NULL && !value_equal_contents (b->val.get (),
4876 new_val)))
4877 {
4878 bs->old_val = b->val;
4879 b->val = release_value (new_val);
4880 b->val_valid = true;
4881 if (new_val != NULL)
4882 value_free_to_mark (mark);
4883 return WP_VALUE_CHANGED;
4884 }
4885 else
4886 {
4887 /* Nothing changed. */
4888 value_free_to_mark (mark);
4889 return WP_VALUE_NOT_CHANGED;
4890 }
4891 }
4892 else
4893 {
4894 /* This seems like the only logical thing to do because
4895 if we temporarily ignored the watchpoint, then when
4896 we reenter the block in which it is valid it contains
4897 garbage (in the case of a function, it may have two
4898 garbage values, one before and one after the prologue).
4899 So we can't even detect the first assignment to it and
4900 watch after that (since the garbage may or may not equal
4901 the first value assigned). */
4902 /* We print all the stop information in
4903 breakpoint_ops->print_it, but in this case, by the time we
4904 call breakpoint_ops->print_it this bp will be deleted
4905 already. So we have no choice but print the information
4906 here. */
4907
4908 SWITCH_THRU_ALL_UIS ()
4909 {
4910 struct ui_out *uiout = current_uiout;
4911
4912 if (uiout->is_mi_like_p ())
4913 uiout->field_string
4914 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4915 uiout->message ("\nWatchpoint %pF deleted because the program has "
4916 "left the block in\n"
4917 "which its expression is valid.\n",
4918 signed_field ("wpnum", b->number));
4919 }
4920
4921 /* Make sure the watchpoint's commands aren't executed. */
4922 b->commands = NULL;
4923 watchpoint_del_at_next_stop (b);
4924
4925 return WP_DELETED;
4926 }
4927 }
4928
4929 /* Return true if it looks like target has stopped due to hitting
4930 breakpoint location BL. This function does not check if we should
4931 stop, only if BL explains the stop. */
4932
4933 static int
4934 bpstat_check_location (const struct bp_location *bl,
4935 const address_space *aspace, CORE_ADDR bp_addr,
4936 const struct target_waitstatus *ws)
4937 {
4938 struct breakpoint *b = bl->owner;
4939
4940 /* BL is from an existing breakpoint. */
4941 gdb_assert (b != NULL);
4942
4943 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4944 }
4945
4946 /* Determine if the watched values have actually changed, and we
4947 should stop. If not, set BS->stop to 0. */
4948
4949 static void
4950 bpstat_check_watchpoint (bpstat bs)
4951 {
4952 const struct bp_location *bl;
4953 struct watchpoint *b;
4954
4955 /* BS is built for existing struct breakpoint. */
4956 bl = bs->bp_location_at;
4957 gdb_assert (bl != NULL);
4958 b = (struct watchpoint *) bs->breakpoint_at;
4959 gdb_assert (b != NULL);
4960
4961 {
4962 int must_check_value = 0;
4963
4964 if (b->type == bp_watchpoint)
4965 /* For a software watchpoint, we must always check the
4966 watched value. */
4967 must_check_value = 1;
4968 else if (b->watchpoint_triggered == watch_triggered_yes)
4969 /* We have a hardware watchpoint (read, write, or access)
4970 and the target earlier reported an address watched by
4971 this watchpoint. */
4972 must_check_value = 1;
4973 else if (b->watchpoint_triggered == watch_triggered_unknown
4974 && b->type == bp_hardware_watchpoint)
4975 /* We were stopped by a hardware watchpoint, but the target could
4976 not report the data address. We must check the watchpoint's
4977 value. Access and read watchpoints are out of luck; without
4978 a data address, we can't figure it out. */
4979 must_check_value = 1;
4980
4981 if (must_check_value)
4982 {
4983 wp_check_result e;
4984
4985 try
4986 {
4987 e = watchpoint_check (bs);
4988 }
4989 catch (const gdb_exception &ex)
4990 {
4991 exception_fprintf (gdb_stderr, ex,
4992 "Error evaluating expression "
4993 "for watchpoint %d\n",
4994 b->number);
4995
4996 SWITCH_THRU_ALL_UIS ()
4997 {
4998 printf_filtered (_("Watchpoint %d deleted.\n"),
4999 b->number);
5000 }
5001 watchpoint_del_at_next_stop (b);
5002 e = WP_DELETED;
5003 }
5004
5005 switch (e)
5006 {
5007 case WP_DELETED:
5008 /* We've already printed what needs to be printed. */
5009 bs->print_it = print_it_done;
5010 /* Stop. */
5011 break;
5012 case WP_IGNORE:
5013 bs->print_it = print_it_noop;
5014 bs->stop = 0;
5015 break;
5016 case WP_VALUE_CHANGED:
5017 if (b->type == bp_read_watchpoint)
5018 {
5019 /* There are two cases to consider here:
5020
5021 1. We're watching the triggered memory for reads.
5022 In that case, trust the target, and always report
5023 the watchpoint hit to the user. Even though
5024 reads don't cause value changes, the value may
5025 have changed since the last time it was read, and
5026 since we're not trapping writes, we will not see
5027 those, and as such we should ignore our notion of
5028 old value.
5029
5030 2. We're watching the triggered memory for both
5031 reads and writes. There are two ways this may
5032 happen:
5033
5034 2.1. This is a target that can't break on data
5035 reads only, but can break on accesses (reads or
5036 writes), such as e.g., x86. We detect this case
5037 at the time we try to insert read watchpoints.
5038
5039 2.2. Otherwise, the target supports read
5040 watchpoints, but, the user set an access or write
5041 watchpoint watching the same memory as this read
5042 watchpoint.
5043
5044 If we're watching memory writes as well as reads,
5045 ignore watchpoint hits when we find that the
5046 value hasn't changed, as reads don't cause
5047 changes. This still gives false positives when
5048 the program writes the same value to memory as
5049 what there was already in memory (we will confuse
5050 it for a read), but it's much better than
5051 nothing. */
5052
5053 int other_write_watchpoint = 0;
5054
5055 if (bl->watchpoint_type == hw_read)
5056 {
5057 struct breakpoint *other_b;
5058
5059 ALL_BREAKPOINTS (other_b)
5060 if (other_b->type == bp_hardware_watchpoint
5061 || other_b->type == bp_access_watchpoint)
5062 {
5063 struct watchpoint *other_w =
5064 (struct watchpoint *) other_b;
5065
5066 if (other_w->watchpoint_triggered
5067 == watch_triggered_yes)
5068 {
5069 other_write_watchpoint = 1;
5070 break;
5071 }
5072 }
5073 }
5074
5075 if (other_write_watchpoint
5076 || bl->watchpoint_type == hw_access)
5077 {
5078 /* We're watching the same memory for writes,
5079 and the value changed since the last time we
5080 updated it, so this trap must be for a write.
5081 Ignore it. */
5082 bs->print_it = print_it_noop;
5083 bs->stop = 0;
5084 }
5085 }
5086 break;
5087 case WP_VALUE_NOT_CHANGED:
5088 if (b->type == bp_hardware_watchpoint
5089 || b->type == bp_watchpoint)
5090 {
5091 /* Don't stop: write watchpoints shouldn't fire if
5092 the value hasn't changed. */
5093 bs->print_it = print_it_noop;
5094 bs->stop = 0;
5095 }
5096 /* Stop. */
5097 break;
5098 default:
5099 /* Can't happen. */
5100 break;
5101 }
5102 }
5103 else /* must_check_value == 0 */
5104 {
5105 /* This is a case where some watchpoint(s) triggered, but
5106 not at the address of this watchpoint, or else no
5107 watchpoint triggered after all. So don't print
5108 anything for this watchpoint. */
5109 bs->print_it = print_it_noop;
5110 bs->stop = 0;
5111 }
5112 }
5113 }
5114
5115 /* For breakpoints that are currently marked as telling gdb to stop,
5116 check conditions (condition proper, frame, thread and ignore count)
5117 of breakpoint referred to by BS. If we should not stop for this
5118 breakpoint, set BS->stop to 0. */
5119
5120 static void
5121 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5122 {
5123 const struct bp_location *bl;
5124 struct breakpoint *b;
5125 /* Assume stop. */
5126 bool condition_result = true;
5127 struct expression *cond;
5128
5129 gdb_assert (bs->stop);
5130
5131 /* BS is built for existing struct breakpoint. */
5132 bl = bs->bp_location_at;
5133 gdb_assert (bl != NULL);
5134 b = bs->breakpoint_at;
5135 gdb_assert (b != NULL);
5136
5137 /* Even if the target evaluated the condition on its end and notified GDB, we
5138 need to do so again since GDB does not know if we stopped due to a
5139 breakpoint or a single step breakpoint. */
5140
5141 if (frame_id_p (b->frame_id)
5142 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5143 {
5144 bs->stop = 0;
5145 return;
5146 }
5147
5148 /* If this is a thread/task-specific breakpoint, don't waste cpu
5149 evaluating the condition if this isn't the specified
5150 thread/task. */
5151 if ((b->thread != -1 && b->thread != thread->global_num)
5152 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5153 {
5154 bs->stop = 0;
5155 return;
5156 }
5157
5158 /* Evaluate extension language breakpoints that have a "stop" method
5159 implemented. */
5160 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5161
5162 if (is_watchpoint (b))
5163 {
5164 struct watchpoint *w = (struct watchpoint *) b;
5165
5166 cond = w->cond_exp.get ();
5167 }
5168 else
5169 cond = bl->cond.get ();
5170
5171 if (cond && b->disposition != disp_del_at_next_stop)
5172 {
5173 int within_current_scope = 1;
5174 struct watchpoint * w;
5175
5176 /* We use value_mark and value_free_to_mark because it could
5177 be a long time before we return to the command level and
5178 call free_all_values. We can't call free_all_values
5179 because we might be in the middle of evaluating a
5180 function call. */
5181 struct value *mark = value_mark ();
5182
5183 if (is_watchpoint (b))
5184 w = (struct watchpoint *) b;
5185 else
5186 w = NULL;
5187
5188 /* Need to select the frame, with all that implies so that
5189 the conditions will have the right context. Because we
5190 use the frame, we will not see an inlined function's
5191 variables when we arrive at a breakpoint at the start
5192 of the inlined function; the current frame will be the
5193 call site. */
5194 if (w == NULL || w->cond_exp_valid_block == NULL)
5195 select_frame (get_current_frame ());
5196 else
5197 {
5198 struct frame_info *frame;
5199
5200 /* For local watchpoint expressions, which particular
5201 instance of a local is being watched matters, so we
5202 keep track of the frame to evaluate the expression
5203 in. To evaluate the condition however, it doesn't
5204 really matter which instantiation of the function
5205 where the condition makes sense triggers the
5206 watchpoint. This allows an expression like "watch
5207 global if q > 10" set in `func', catch writes to
5208 global on all threads that call `func', or catch
5209 writes on all recursive calls of `func' by a single
5210 thread. We simply always evaluate the condition in
5211 the innermost frame that's executing where it makes
5212 sense to evaluate the condition. It seems
5213 intuitive. */
5214 frame = block_innermost_frame (w->cond_exp_valid_block);
5215 if (frame != NULL)
5216 select_frame (frame);
5217 else
5218 within_current_scope = 0;
5219 }
5220 if (within_current_scope)
5221 {
5222 try
5223 {
5224 condition_result = breakpoint_cond_eval (cond);
5225 }
5226 catch (const gdb_exception &ex)
5227 {
5228 exception_fprintf (gdb_stderr, ex,
5229 "Error in testing breakpoint condition:\n");
5230 }
5231 }
5232 else
5233 {
5234 warning (_("Watchpoint condition cannot be tested "
5235 "in the current scope"));
5236 /* If we failed to set the right context for this
5237 watchpoint, unconditionally report it. */
5238 }
5239 /* FIXME-someday, should give breakpoint #. */
5240 value_free_to_mark (mark);
5241 }
5242
5243 if (cond && !condition_result)
5244 {
5245 bs->stop = 0;
5246 }
5247 else if (b->ignore_count > 0)
5248 {
5249 b->ignore_count--;
5250 bs->stop = 0;
5251 /* Increase the hit count even though we don't stop. */
5252 ++(b->hit_count);
5253 gdb::observers::breakpoint_modified.notify (b);
5254 }
5255 }
5256
5257 /* Returns true if we need to track moribund locations of LOC's type
5258 on the current target. */
5259
5260 static int
5261 need_moribund_for_location_type (struct bp_location *loc)
5262 {
5263 return ((loc->loc_type == bp_loc_software_breakpoint
5264 && !target_supports_stopped_by_sw_breakpoint ())
5265 || (loc->loc_type == bp_loc_hardware_breakpoint
5266 && !target_supports_stopped_by_hw_breakpoint ()));
5267 }
5268
5269 /* See breakpoint.h. */
5270
5271 bpstat
5272 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5273 const struct target_waitstatus *ws)
5274 {
5275 struct breakpoint *b;
5276 bpstat bs_head = NULL, *bs_link = &bs_head;
5277
5278 ALL_BREAKPOINTS (b)
5279 {
5280 if (!breakpoint_enabled (b))
5281 continue;
5282
5283 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5284 {
5285 /* For hardware watchpoints, we look only at the first
5286 location. The watchpoint_check function will work on the
5287 entire expression, not the individual locations. For
5288 read watchpoints, the watchpoints_triggered function has
5289 checked all locations already. */
5290 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5291 break;
5292
5293 if (!bl->enabled || bl->shlib_disabled)
5294 continue;
5295
5296 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5297 continue;
5298
5299 /* Come here if it's a watchpoint, or if the break address
5300 matches. */
5301
5302 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5303 explain stop. */
5304
5305 /* Assume we stop. Should we find a watchpoint that is not
5306 actually triggered, or if the condition of the breakpoint
5307 evaluates as false, we'll reset 'stop' to 0. */
5308 bs->stop = 1;
5309 bs->print = 1;
5310
5311 /* If this is a scope breakpoint, mark the associated
5312 watchpoint as triggered so that we will handle the
5313 out-of-scope event. We'll get to the watchpoint next
5314 iteration. */
5315 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5316 {
5317 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5318
5319 w->watchpoint_triggered = watch_triggered_yes;
5320 }
5321 }
5322 }
5323
5324 /* Check if a moribund breakpoint explains the stop. */
5325 if (!target_supports_stopped_by_sw_breakpoint ()
5326 || !target_supports_stopped_by_hw_breakpoint ())
5327 {
5328 for (bp_location *loc : moribund_locations)
5329 {
5330 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5331 && need_moribund_for_location_type (loc))
5332 {
5333 bpstat bs = new bpstats (loc, &bs_link);
5334 /* For hits of moribund locations, we should just proceed. */
5335 bs->stop = 0;
5336 bs->print = 0;
5337 bs->print_it = print_it_noop;
5338 }
5339 }
5340 }
5341
5342 return bs_head;
5343 }
5344
5345 /* See breakpoint.h. */
5346
5347 bpstat
5348 bpstat_stop_status (const address_space *aspace,
5349 CORE_ADDR bp_addr, thread_info *thread,
5350 const struct target_waitstatus *ws,
5351 bpstat stop_chain)
5352 {
5353 struct breakpoint *b = NULL;
5354 /* First item of allocated bpstat's. */
5355 bpstat bs_head = stop_chain;
5356 bpstat bs;
5357 int need_remove_insert;
5358 int removed_any;
5359
5360 /* First, build the bpstat chain with locations that explain a
5361 target stop, while being careful to not set the target running,
5362 as that may invalidate locations (in particular watchpoint
5363 locations are recreated). Resuming will happen here with
5364 breakpoint conditions or watchpoint expressions that include
5365 inferior function calls. */
5366 if (bs_head == NULL)
5367 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5368
5369 /* A bit of special processing for shlib breakpoints. We need to
5370 process solib loading here, so that the lists of loaded and
5371 unloaded libraries are correct before we handle "catch load" and
5372 "catch unload". */
5373 for (bs = bs_head; bs != NULL; bs = bs->next)
5374 {
5375 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5376 {
5377 handle_solib_event ();
5378 break;
5379 }
5380 }
5381
5382 /* Now go through the locations that caused the target to stop, and
5383 check whether we're interested in reporting this stop to higher
5384 layers, or whether we should resume the target transparently. */
5385
5386 removed_any = 0;
5387
5388 for (bs = bs_head; bs != NULL; bs = bs->next)
5389 {
5390 if (!bs->stop)
5391 continue;
5392
5393 b = bs->breakpoint_at;
5394 b->ops->check_status (bs);
5395 if (bs->stop)
5396 {
5397 bpstat_check_breakpoint_conditions (bs, thread);
5398
5399 if (bs->stop)
5400 {
5401 ++(b->hit_count);
5402 gdb::observers::breakpoint_modified.notify (b);
5403
5404 /* We will stop here. */
5405 if (b->disposition == disp_disable)
5406 {
5407 --(b->enable_count);
5408 if (b->enable_count <= 0)
5409 b->enable_state = bp_disabled;
5410 removed_any = 1;
5411 }
5412 if (b->silent)
5413 bs->print = 0;
5414 bs->commands = b->commands;
5415 if (command_line_is_silent (bs->commands
5416 ? bs->commands.get () : NULL))
5417 bs->print = 0;
5418
5419 b->ops->after_condition_true (bs);
5420 }
5421
5422 }
5423
5424 /* Print nothing for this entry if we don't stop or don't
5425 print. */
5426 if (!bs->stop || !bs->print)
5427 bs->print_it = print_it_noop;
5428 }
5429
5430 /* If we aren't stopping, the value of some hardware watchpoint may
5431 not have changed, but the intermediate memory locations we are
5432 watching may have. Don't bother if we're stopping; this will get
5433 done later. */
5434 need_remove_insert = 0;
5435 if (! bpstat_causes_stop (bs_head))
5436 for (bs = bs_head; bs != NULL; bs = bs->next)
5437 if (!bs->stop
5438 && bs->breakpoint_at
5439 && is_hardware_watchpoint (bs->breakpoint_at))
5440 {
5441 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5442
5443 update_watchpoint (w, 0 /* don't reparse. */);
5444 need_remove_insert = 1;
5445 }
5446
5447 if (need_remove_insert)
5448 update_global_location_list (UGLL_MAY_INSERT);
5449 else if (removed_any)
5450 update_global_location_list (UGLL_DONT_INSERT);
5451
5452 return bs_head;
5453 }
5454
5455 static void
5456 handle_jit_event (void)
5457 {
5458 struct frame_info *frame;
5459 struct gdbarch *gdbarch;
5460
5461 if (debug_infrun)
5462 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5463
5464 /* Switch terminal for any messages produced by
5465 breakpoint_re_set. */
5466 target_terminal::ours_for_output ();
5467
5468 frame = get_current_frame ();
5469 gdbarch = get_frame_arch (frame);
5470
5471 jit_event_handler (gdbarch);
5472
5473 target_terminal::inferior ();
5474 }
5475
5476 /* Prepare WHAT final decision for infrun. */
5477
5478 /* Decide what infrun needs to do with this bpstat. */
5479
5480 struct bpstat_what
5481 bpstat_what (bpstat bs_head)
5482 {
5483 struct bpstat_what retval;
5484 bpstat bs;
5485
5486 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5487 retval.call_dummy = STOP_NONE;
5488 retval.is_longjmp = false;
5489
5490 for (bs = bs_head; bs != NULL; bs = bs->next)
5491 {
5492 /* Extract this BS's action. After processing each BS, we check
5493 if its action overrides all we've seem so far. */
5494 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5495 enum bptype bptype;
5496
5497 if (bs->breakpoint_at == NULL)
5498 {
5499 /* I suspect this can happen if it was a momentary
5500 breakpoint which has since been deleted. */
5501 bptype = bp_none;
5502 }
5503 else
5504 bptype = bs->breakpoint_at->type;
5505
5506 switch (bptype)
5507 {
5508 case bp_none:
5509 break;
5510 case bp_breakpoint:
5511 case bp_hardware_breakpoint:
5512 case bp_single_step:
5513 case bp_until:
5514 case bp_finish:
5515 case bp_shlib_event:
5516 if (bs->stop)
5517 {
5518 if (bs->print)
5519 this_action = BPSTAT_WHAT_STOP_NOISY;
5520 else
5521 this_action = BPSTAT_WHAT_STOP_SILENT;
5522 }
5523 else
5524 this_action = BPSTAT_WHAT_SINGLE;
5525 break;
5526 case bp_watchpoint:
5527 case bp_hardware_watchpoint:
5528 case bp_read_watchpoint:
5529 case bp_access_watchpoint:
5530 if (bs->stop)
5531 {
5532 if (bs->print)
5533 this_action = BPSTAT_WHAT_STOP_NOISY;
5534 else
5535 this_action = BPSTAT_WHAT_STOP_SILENT;
5536 }
5537 else
5538 {
5539 /* There was a watchpoint, but we're not stopping.
5540 This requires no further action. */
5541 }
5542 break;
5543 case bp_longjmp:
5544 case bp_longjmp_call_dummy:
5545 case bp_exception:
5546 if (bs->stop)
5547 {
5548 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5549 retval.is_longjmp = bptype != bp_exception;
5550 }
5551 else
5552 this_action = BPSTAT_WHAT_SINGLE;
5553 break;
5554 case bp_longjmp_resume:
5555 case bp_exception_resume:
5556 if (bs->stop)
5557 {
5558 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5559 retval.is_longjmp = bptype == bp_longjmp_resume;
5560 }
5561 else
5562 this_action = BPSTAT_WHAT_SINGLE;
5563 break;
5564 case bp_step_resume:
5565 if (bs->stop)
5566 this_action = BPSTAT_WHAT_STEP_RESUME;
5567 else
5568 {
5569 /* It is for the wrong frame. */
5570 this_action = BPSTAT_WHAT_SINGLE;
5571 }
5572 break;
5573 case bp_hp_step_resume:
5574 if (bs->stop)
5575 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5576 else
5577 {
5578 /* It is for the wrong frame. */
5579 this_action = BPSTAT_WHAT_SINGLE;
5580 }
5581 break;
5582 case bp_watchpoint_scope:
5583 case bp_thread_event:
5584 case bp_overlay_event:
5585 case bp_longjmp_master:
5586 case bp_std_terminate_master:
5587 case bp_exception_master:
5588 this_action = BPSTAT_WHAT_SINGLE;
5589 break;
5590 case bp_catchpoint:
5591 if (bs->stop)
5592 {
5593 if (bs->print)
5594 this_action = BPSTAT_WHAT_STOP_NOISY;
5595 else
5596 this_action = BPSTAT_WHAT_STOP_SILENT;
5597 }
5598 else
5599 {
5600 /* Some catchpoints are implemented with breakpoints.
5601 For those, we need to step over the breakpoint. */
5602 if (bs->bp_location_at->loc_type != bp_loc_other)
5603 this_action = BPSTAT_WHAT_SINGLE;
5604 }
5605 break;
5606 case bp_jit_event:
5607 this_action = BPSTAT_WHAT_SINGLE;
5608 break;
5609 case bp_call_dummy:
5610 /* Make sure the action is stop (silent or noisy),
5611 so infrun.c pops the dummy frame. */
5612 retval.call_dummy = STOP_STACK_DUMMY;
5613 this_action = BPSTAT_WHAT_STOP_SILENT;
5614 break;
5615 case bp_std_terminate:
5616 /* Make sure the action is stop (silent or noisy),
5617 so infrun.c pops the dummy frame. */
5618 retval.call_dummy = STOP_STD_TERMINATE;
5619 this_action = BPSTAT_WHAT_STOP_SILENT;
5620 break;
5621 case bp_tracepoint:
5622 case bp_fast_tracepoint:
5623 case bp_static_tracepoint:
5624 /* Tracepoint hits should not be reported back to GDB, and
5625 if one got through somehow, it should have been filtered
5626 out already. */
5627 internal_error (__FILE__, __LINE__,
5628 _("bpstat_what: tracepoint encountered"));
5629 break;
5630 case bp_gnu_ifunc_resolver:
5631 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5632 this_action = BPSTAT_WHAT_SINGLE;
5633 break;
5634 case bp_gnu_ifunc_resolver_return:
5635 /* The breakpoint will be removed, execution will restart from the
5636 PC of the former breakpoint. */
5637 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5638 break;
5639
5640 case bp_dprintf:
5641 if (bs->stop)
5642 this_action = BPSTAT_WHAT_STOP_SILENT;
5643 else
5644 this_action = BPSTAT_WHAT_SINGLE;
5645 break;
5646
5647 default:
5648 internal_error (__FILE__, __LINE__,
5649 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5650 }
5651
5652 retval.main_action = std::max (retval.main_action, this_action);
5653 }
5654
5655 return retval;
5656 }
5657
5658 void
5659 bpstat_run_callbacks (bpstat bs_head)
5660 {
5661 bpstat bs;
5662
5663 for (bs = bs_head; bs != NULL; bs = bs->next)
5664 {
5665 struct breakpoint *b = bs->breakpoint_at;
5666
5667 if (b == NULL)
5668 continue;
5669 switch (b->type)
5670 {
5671 case bp_jit_event:
5672 handle_jit_event ();
5673 break;
5674 case bp_gnu_ifunc_resolver:
5675 gnu_ifunc_resolver_stop (b);
5676 break;
5677 case bp_gnu_ifunc_resolver_return:
5678 gnu_ifunc_resolver_return_stop (b);
5679 break;
5680 }
5681 }
5682 }
5683
5684 /* See breakpoint.h. */
5685
5686 bool
5687 bpstat_should_step ()
5688 {
5689 struct breakpoint *b;
5690
5691 ALL_BREAKPOINTS (b)
5692 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5693 return true;
5694 return false;
5695 }
5696
5697 /* See breakpoint.h. */
5698
5699 bool
5700 bpstat_causes_stop (bpstat bs)
5701 {
5702 for (; bs != NULL; bs = bs->next)
5703 if (bs->stop)
5704 return true;
5705
5706 return false;
5707 }
5708
5709 \f
5710
5711 /* Compute a string of spaces suitable to indent the next line
5712 so it starts at the position corresponding to the table column
5713 named COL_NAME in the currently active table of UIOUT. */
5714
5715 static char *
5716 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5717 {
5718 static char wrap_indent[80];
5719 int i, total_width, width, align;
5720 const char *text;
5721
5722 total_width = 0;
5723 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5724 {
5725 if (strcmp (text, col_name) == 0)
5726 {
5727 gdb_assert (total_width < sizeof wrap_indent);
5728 memset (wrap_indent, ' ', total_width);
5729 wrap_indent[total_width] = 0;
5730
5731 return wrap_indent;
5732 }
5733
5734 total_width += width + 1;
5735 }
5736
5737 return NULL;
5738 }
5739
5740 /* Determine if the locations of this breakpoint will have their conditions
5741 evaluated by the target, host or a mix of both. Returns the following:
5742
5743 "host": Host evals condition.
5744 "host or target": Host or Target evals condition.
5745 "target": Target evals condition.
5746 */
5747
5748 static const char *
5749 bp_condition_evaluator (struct breakpoint *b)
5750 {
5751 struct bp_location *bl;
5752 char host_evals = 0;
5753 char target_evals = 0;
5754
5755 if (!b)
5756 return NULL;
5757
5758 if (!is_breakpoint (b))
5759 return NULL;
5760
5761 if (gdb_evaluates_breakpoint_condition_p ()
5762 || !target_supports_evaluation_of_breakpoint_conditions ())
5763 return condition_evaluation_host;
5764
5765 for (bl = b->loc; bl; bl = bl->next)
5766 {
5767 if (bl->cond_bytecode)
5768 target_evals++;
5769 else
5770 host_evals++;
5771 }
5772
5773 if (host_evals && target_evals)
5774 return condition_evaluation_both;
5775 else if (target_evals)
5776 return condition_evaluation_target;
5777 else
5778 return condition_evaluation_host;
5779 }
5780
5781 /* Determine the breakpoint location's condition evaluator. This is
5782 similar to bp_condition_evaluator, but for locations. */
5783
5784 static const char *
5785 bp_location_condition_evaluator (struct bp_location *bl)
5786 {
5787 if (bl && !is_breakpoint (bl->owner))
5788 return NULL;
5789
5790 if (gdb_evaluates_breakpoint_condition_p ()
5791 || !target_supports_evaluation_of_breakpoint_conditions ())
5792 return condition_evaluation_host;
5793
5794 if (bl && bl->cond_bytecode)
5795 return condition_evaluation_target;
5796 else
5797 return condition_evaluation_host;
5798 }
5799
5800 /* Print the LOC location out of the list of B->LOC locations. */
5801
5802 static void
5803 print_breakpoint_location (struct breakpoint *b,
5804 struct bp_location *loc)
5805 {
5806 struct ui_out *uiout = current_uiout;
5807
5808 scoped_restore_current_program_space restore_pspace;
5809
5810 if (loc != NULL && loc->shlib_disabled)
5811 loc = NULL;
5812
5813 if (loc != NULL)
5814 set_current_program_space (loc->pspace);
5815
5816 if (b->display_canonical)
5817 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5818 else if (loc && loc->symtab)
5819 {
5820 const struct symbol *sym = loc->symbol;
5821
5822 if (sym)
5823 {
5824 uiout->text ("in ");
5825 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
5826 function_name_style.style ());
5827 uiout->text (" ");
5828 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5829 uiout->text ("at ");
5830 }
5831 uiout->field_string ("file",
5832 symtab_to_filename_for_display (loc->symtab),
5833 file_name_style.style ());
5834 uiout->text (":");
5835
5836 if (uiout->is_mi_like_p ())
5837 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5838
5839 uiout->field_signed ("line", loc->line_number);
5840 }
5841 else if (loc)
5842 {
5843 string_file stb;
5844
5845 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5846 demangle, "");
5847 uiout->field_stream ("at", stb);
5848 }
5849 else
5850 {
5851 uiout->field_string ("pending",
5852 event_location_to_string (b->location.get ()));
5853 /* If extra_string is available, it could be holding a condition
5854 or dprintf arguments. In either case, make sure it is printed,
5855 too, but only for non-MI streams. */
5856 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5857 {
5858 if (b->type == bp_dprintf)
5859 uiout->text (",");
5860 else
5861 uiout->text (" ");
5862 uiout->text (b->extra_string);
5863 }
5864 }
5865
5866 if (loc && is_breakpoint (b)
5867 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5868 && bp_condition_evaluator (b) == condition_evaluation_both)
5869 {
5870 uiout->text (" (");
5871 uiout->field_string ("evaluated-by",
5872 bp_location_condition_evaluator (loc));
5873 uiout->text (")");
5874 }
5875 }
5876
5877 static const char *
5878 bptype_string (enum bptype type)
5879 {
5880 struct ep_type_description
5881 {
5882 enum bptype type;
5883 const char *description;
5884 };
5885 static struct ep_type_description bptypes[] =
5886 {
5887 {bp_none, "?deleted?"},
5888 {bp_breakpoint, "breakpoint"},
5889 {bp_hardware_breakpoint, "hw breakpoint"},
5890 {bp_single_step, "sw single-step"},
5891 {bp_until, "until"},
5892 {bp_finish, "finish"},
5893 {bp_watchpoint, "watchpoint"},
5894 {bp_hardware_watchpoint, "hw watchpoint"},
5895 {bp_read_watchpoint, "read watchpoint"},
5896 {bp_access_watchpoint, "acc watchpoint"},
5897 {bp_longjmp, "longjmp"},
5898 {bp_longjmp_resume, "longjmp resume"},
5899 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5900 {bp_exception, "exception"},
5901 {bp_exception_resume, "exception resume"},
5902 {bp_step_resume, "step resume"},
5903 {bp_hp_step_resume, "high-priority step resume"},
5904 {bp_watchpoint_scope, "watchpoint scope"},
5905 {bp_call_dummy, "call dummy"},
5906 {bp_std_terminate, "std::terminate"},
5907 {bp_shlib_event, "shlib events"},
5908 {bp_thread_event, "thread events"},
5909 {bp_overlay_event, "overlay events"},
5910 {bp_longjmp_master, "longjmp master"},
5911 {bp_std_terminate_master, "std::terminate master"},
5912 {bp_exception_master, "exception master"},
5913 {bp_catchpoint, "catchpoint"},
5914 {bp_tracepoint, "tracepoint"},
5915 {bp_fast_tracepoint, "fast tracepoint"},
5916 {bp_static_tracepoint, "static tracepoint"},
5917 {bp_dprintf, "dprintf"},
5918 {bp_jit_event, "jit events"},
5919 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5920 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5921 };
5922
5923 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5924 || ((int) type != bptypes[(int) type].type))
5925 internal_error (__FILE__, __LINE__,
5926 _("bptypes table does not describe type #%d."),
5927 (int) type);
5928
5929 return bptypes[(int) type].description;
5930 }
5931
5932 /* For MI, output a field named 'thread-groups' with a list as the value.
5933 For CLI, prefix the list with the string 'inf'. */
5934
5935 static void
5936 output_thread_groups (struct ui_out *uiout,
5937 const char *field_name,
5938 const std::vector<int> &inf_nums,
5939 int mi_only)
5940 {
5941 int is_mi = uiout->is_mi_like_p ();
5942
5943 /* For backward compatibility, don't display inferiors in CLI unless
5944 there are several. Always display them for MI. */
5945 if (!is_mi && mi_only)
5946 return;
5947
5948 ui_out_emit_list list_emitter (uiout, field_name);
5949
5950 for (size_t i = 0; i < inf_nums.size (); i++)
5951 {
5952 if (is_mi)
5953 {
5954 char mi_group[10];
5955
5956 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
5957 uiout->field_string (NULL, mi_group);
5958 }
5959 else
5960 {
5961 if (i == 0)
5962 uiout->text (" inf ");
5963 else
5964 uiout->text (", ");
5965
5966 uiout->text (plongest (inf_nums[i]));
5967 }
5968 }
5969 }
5970
5971 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5972 instead of going via breakpoint_ops::print_one. This makes "maint
5973 info breakpoints" show the software breakpoint locations of
5974 catchpoints, which are considered internal implementation
5975 detail. */
5976
5977 static void
5978 print_one_breakpoint_location (struct breakpoint *b,
5979 struct bp_location *loc,
5980 int loc_number,
5981 struct bp_location **last_loc,
5982 int allflag, bool raw_loc)
5983 {
5984 struct command_line *l;
5985 static char bpenables[] = "nynny";
5986
5987 struct ui_out *uiout = current_uiout;
5988 int header_of_multiple = 0;
5989 int part_of_multiple = (loc != NULL);
5990 struct value_print_options opts;
5991
5992 get_user_print_options (&opts);
5993
5994 gdb_assert (!loc || loc_number != 0);
5995 /* See comment in print_one_breakpoint concerning treatment of
5996 breakpoints with single disabled location. */
5997 if (loc == NULL
5998 && (b->loc != NULL
5999 && (b->loc->next != NULL || !b->loc->enabled)))
6000 header_of_multiple = 1;
6001 if (loc == NULL)
6002 loc = b->loc;
6003
6004 annotate_record ();
6005
6006 /* 1 */
6007 annotate_field (0);
6008 if (part_of_multiple)
6009 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6010 else
6011 uiout->field_signed ("number", b->number);
6012
6013 /* 2 */
6014 annotate_field (1);
6015 if (part_of_multiple)
6016 uiout->field_skip ("type");
6017 else
6018 uiout->field_string ("type", bptype_string (b->type));
6019
6020 /* 3 */
6021 annotate_field (2);
6022 if (part_of_multiple)
6023 uiout->field_skip ("disp");
6024 else
6025 uiout->field_string ("disp", bpdisp_text (b->disposition));
6026
6027 /* 4 */
6028 annotate_field (3);
6029 if (part_of_multiple)
6030 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6031 else
6032 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6033
6034 /* 5 and 6 */
6035 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6036 b->ops->print_one (b, last_loc);
6037 else
6038 {
6039 if (is_watchpoint (b))
6040 {
6041 struct watchpoint *w = (struct watchpoint *) b;
6042
6043 /* Field 4, the address, is omitted (which makes the columns
6044 not line up too nicely with the headers, but the effect
6045 is relatively readable). */
6046 if (opts.addressprint)
6047 uiout->field_skip ("addr");
6048 annotate_field (5);
6049 uiout->field_string ("what", w->exp_string);
6050 }
6051 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6052 || is_ada_exception_catchpoint (b))
6053 {
6054 if (opts.addressprint)
6055 {
6056 annotate_field (4);
6057 if (header_of_multiple)
6058 uiout->field_string ("addr", "<MULTIPLE>");
6059 else if (b->loc == NULL || loc->shlib_disabled)
6060 uiout->field_string ("addr", "<PENDING>");
6061 else
6062 uiout->field_core_addr ("addr",
6063 loc->gdbarch, loc->address);
6064 }
6065 annotate_field (5);
6066 if (!header_of_multiple)
6067 print_breakpoint_location (b, loc);
6068 if (b->loc)
6069 *last_loc = b->loc;
6070 }
6071 }
6072
6073 if (loc != NULL && !header_of_multiple)
6074 {
6075 std::vector<int> inf_nums;
6076 int mi_only = 1;
6077
6078 for (inferior *inf : all_inferiors ())
6079 {
6080 if (inf->pspace == loc->pspace)
6081 inf_nums.push_back (inf->num);
6082 }
6083
6084 /* For backward compatibility, don't display inferiors in CLI unless
6085 there are several. Always display for MI. */
6086 if (allflag
6087 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6088 && (number_of_program_spaces () > 1
6089 || number_of_inferiors () > 1)
6090 /* LOC is for existing B, it cannot be in
6091 moribund_locations and thus having NULL OWNER. */
6092 && loc->owner->type != bp_catchpoint))
6093 mi_only = 0;
6094 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6095 }
6096
6097 if (!part_of_multiple)
6098 {
6099 if (b->thread != -1)
6100 {
6101 /* FIXME: This seems to be redundant and lost here; see the
6102 "stop only in" line a little further down. */
6103 uiout->text (" thread ");
6104 uiout->field_signed ("thread", b->thread);
6105 }
6106 else if (b->task != 0)
6107 {
6108 uiout->text (" task ");
6109 uiout->field_signed ("task", b->task);
6110 }
6111 }
6112
6113 uiout->text ("\n");
6114
6115 if (!part_of_multiple)
6116 b->ops->print_one_detail (b, uiout);
6117
6118 if (part_of_multiple && frame_id_p (b->frame_id))
6119 {
6120 annotate_field (6);
6121 uiout->text ("\tstop only in stack frame at ");
6122 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6123 the frame ID. */
6124 uiout->field_core_addr ("frame",
6125 b->gdbarch, b->frame_id.stack_addr);
6126 uiout->text ("\n");
6127 }
6128
6129 if (!part_of_multiple && b->cond_string)
6130 {
6131 annotate_field (7);
6132 if (is_tracepoint (b))
6133 uiout->text ("\ttrace only if ");
6134 else
6135 uiout->text ("\tstop only if ");
6136 uiout->field_string ("cond", b->cond_string);
6137
6138 /* Print whether the target is doing the breakpoint's condition
6139 evaluation. If GDB is doing the evaluation, don't print anything. */
6140 if (is_breakpoint (b)
6141 && breakpoint_condition_evaluation_mode ()
6142 == condition_evaluation_target)
6143 {
6144 uiout->message (" (%pF evals)",
6145 string_field ("evaluated-by",
6146 bp_condition_evaluator (b)));
6147 }
6148 uiout->text ("\n");
6149 }
6150
6151 if (!part_of_multiple && b->thread != -1)
6152 {
6153 /* FIXME should make an annotation for this. */
6154 uiout->text ("\tstop only in thread ");
6155 if (uiout->is_mi_like_p ())
6156 uiout->field_signed ("thread", b->thread);
6157 else
6158 {
6159 struct thread_info *thr = find_thread_global_id (b->thread);
6160
6161 uiout->field_string ("thread", print_thread_id (thr));
6162 }
6163 uiout->text ("\n");
6164 }
6165
6166 if (!part_of_multiple)
6167 {
6168 if (b->hit_count)
6169 {
6170 /* FIXME should make an annotation for this. */
6171 if (is_catchpoint (b))
6172 uiout->text ("\tcatchpoint");
6173 else if (is_tracepoint (b))
6174 uiout->text ("\ttracepoint");
6175 else
6176 uiout->text ("\tbreakpoint");
6177 uiout->text (" already hit ");
6178 uiout->field_signed ("times", b->hit_count);
6179 if (b->hit_count == 1)
6180 uiout->text (" time\n");
6181 else
6182 uiout->text (" times\n");
6183 }
6184 else
6185 {
6186 /* Output the count also if it is zero, but only if this is mi. */
6187 if (uiout->is_mi_like_p ())
6188 uiout->field_signed ("times", b->hit_count);
6189 }
6190 }
6191
6192 if (!part_of_multiple && b->ignore_count)
6193 {
6194 annotate_field (8);
6195 uiout->message ("\tignore next %pF hits\n",
6196 signed_field ("ignore", b->ignore_count));
6197 }
6198
6199 /* Note that an enable count of 1 corresponds to "enable once"
6200 behavior, which is reported by the combination of enablement and
6201 disposition, so we don't need to mention it here. */
6202 if (!part_of_multiple && b->enable_count > 1)
6203 {
6204 annotate_field (8);
6205 uiout->text ("\tdisable after ");
6206 /* Tweak the wording to clarify that ignore and enable counts
6207 are distinct, and have additive effect. */
6208 if (b->ignore_count)
6209 uiout->text ("additional ");
6210 else
6211 uiout->text ("next ");
6212 uiout->field_signed ("enable", b->enable_count);
6213 uiout->text (" hits\n");
6214 }
6215
6216 if (!part_of_multiple && is_tracepoint (b))
6217 {
6218 struct tracepoint *tp = (struct tracepoint *) b;
6219
6220 if (tp->traceframe_usage)
6221 {
6222 uiout->text ("\ttrace buffer usage ");
6223 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6224 uiout->text (" bytes\n");
6225 }
6226 }
6227
6228 l = b->commands ? b->commands.get () : NULL;
6229 if (!part_of_multiple && l)
6230 {
6231 annotate_field (9);
6232 ui_out_emit_tuple tuple_emitter (uiout, "script");
6233 print_command_lines (uiout, l, 4);
6234 }
6235
6236 if (is_tracepoint (b))
6237 {
6238 struct tracepoint *t = (struct tracepoint *) b;
6239
6240 if (!part_of_multiple && t->pass_count)
6241 {
6242 annotate_field (10);
6243 uiout->text ("\tpass count ");
6244 uiout->field_signed ("pass", t->pass_count);
6245 uiout->text (" \n");
6246 }
6247
6248 /* Don't display it when tracepoint or tracepoint location is
6249 pending. */
6250 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6251 {
6252 annotate_field (11);
6253
6254 if (uiout->is_mi_like_p ())
6255 uiout->field_string ("installed",
6256 loc->inserted ? "y" : "n");
6257 else
6258 {
6259 if (loc->inserted)
6260 uiout->text ("\t");
6261 else
6262 uiout->text ("\tnot ");
6263 uiout->text ("installed on target\n");
6264 }
6265 }
6266 }
6267
6268 if (uiout->is_mi_like_p () && !part_of_multiple)
6269 {
6270 if (is_watchpoint (b))
6271 {
6272 struct watchpoint *w = (struct watchpoint *) b;
6273
6274 uiout->field_string ("original-location", w->exp_string);
6275 }
6276 else if (b->location != NULL
6277 && event_location_to_string (b->location.get ()) != NULL)
6278 uiout->field_string ("original-location",
6279 event_location_to_string (b->location.get ()));
6280 }
6281 }
6282
6283 /* See breakpoint.h. */
6284
6285 bool fix_multi_location_breakpoint_output_globally = false;
6286
6287 static void
6288 print_one_breakpoint (struct breakpoint *b,
6289 struct bp_location **last_loc,
6290 int allflag)
6291 {
6292 struct ui_out *uiout = current_uiout;
6293 bool use_fixed_output
6294 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6295 || fix_multi_location_breakpoint_output_globally);
6296
6297 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6298 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6299
6300 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6301 are outside. */
6302 if (!use_fixed_output)
6303 bkpt_tuple_emitter.reset ();
6304
6305 /* If this breakpoint has custom print function,
6306 it's already printed. Otherwise, print individual
6307 locations, if any. */
6308 if (b->ops == NULL
6309 || b->ops->print_one == NULL
6310 || allflag)
6311 {
6312 /* If breakpoint has a single location that is disabled, we
6313 print it as if it had several locations, since otherwise it's
6314 hard to represent "breakpoint enabled, location disabled"
6315 situation.
6316
6317 Note that while hardware watchpoints have several locations
6318 internally, that's not a property exposed to users.
6319
6320 Likewise, while catchpoints may be implemented with
6321 breakpoints (e.g., catch throw), that's not a property
6322 exposed to users. We do however display the internal
6323 breakpoint locations with "maint info breakpoints". */
6324 if (!is_hardware_watchpoint (b)
6325 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6326 || is_ada_exception_catchpoint (b))
6327 && (allflag
6328 || (b->loc && (b->loc->next || !b->loc->enabled))))
6329 {
6330 gdb::optional<ui_out_emit_list> locations_list;
6331
6332 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6333 MI record. For later versions, place breakpoint locations in a
6334 list. */
6335 if (uiout->is_mi_like_p () && use_fixed_output)
6336 locations_list.emplace (uiout, "locations");
6337
6338 int n = 1;
6339 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6340 {
6341 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6342 print_one_breakpoint_location (b, loc, n, last_loc,
6343 allflag, allflag);
6344 }
6345 }
6346 }
6347 }
6348
6349 static int
6350 breakpoint_address_bits (struct breakpoint *b)
6351 {
6352 int print_address_bits = 0;
6353 struct bp_location *loc;
6354
6355 /* Software watchpoints that aren't watching memory don't have an
6356 address to print. */
6357 if (is_no_memory_software_watchpoint (b))
6358 return 0;
6359
6360 for (loc = b->loc; loc; loc = loc->next)
6361 {
6362 int addr_bit;
6363
6364 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6365 if (addr_bit > print_address_bits)
6366 print_address_bits = addr_bit;
6367 }
6368
6369 return print_address_bits;
6370 }
6371
6372 /* See breakpoint.h. */
6373
6374 void
6375 print_breakpoint (breakpoint *b)
6376 {
6377 struct bp_location *dummy_loc = NULL;
6378 print_one_breakpoint (b, &dummy_loc, 0);
6379 }
6380
6381 /* Return true if this breakpoint was set by the user, false if it is
6382 internal or momentary. */
6383
6384 int
6385 user_breakpoint_p (struct breakpoint *b)
6386 {
6387 return b->number > 0;
6388 }
6389
6390 /* See breakpoint.h. */
6391
6392 int
6393 pending_breakpoint_p (struct breakpoint *b)
6394 {
6395 return b->loc == NULL;
6396 }
6397
6398 /* Print information on breakpoints (including watchpoints and tracepoints).
6399
6400 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6401 understood by number_or_range_parser. Only breakpoints included in this
6402 list are then printed.
6403
6404 If SHOW_INTERNAL is true, print internal breakpoints.
6405
6406 If FILTER is non-NULL, call it on each breakpoint and only include the
6407 ones for which it returns true.
6408
6409 Return the total number of breakpoints listed. */
6410
6411 static int
6412 breakpoint_1 (const char *bp_num_list, bool show_internal,
6413 bool (*filter) (const struct breakpoint *))
6414 {
6415 struct breakpoint *b;
6416 struct bp_location *last_loc = NULL;
6417 int nr_printable_breakpoints;
6418 struct value_print_options opts;
6419 int print_address_bits = 0;
6420 int print_type_col_width = 14;
6421 struct ui_out *uiout = current_uiout;
6422
6423 get_user_print_options (&opts);
6424
6425 /* Compute the number of rows in the table, as well as the size
6426 required for address fields. */
6427 nr_printable_breakpoints = 0;
6428 ALL_BREAKPOINTS (b)
6429 {
6430 /* If we have a filter, only list the breakpoints it accepts. */
6431 if (filter && !filter (b))
6432 continue;
6433
6434 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6435 accept. Skip the others. */
6436 if (bp_num_list != NULL && *bp_num_list != '\0')
6437 {
6438 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6439 continue;
6440 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6441 continue;
6442 }
6443
6444 if (show_internal || user_breakpoint_p (b))
6445 {
6446 int addr_bit, type_len;
6447
6448 addr_bit = breakpoint_address_bits (b);
6449 if (addr_bit > print_address_bits)
6450 print_address_bits = addr_bit;
6451
6452 type_len = strlen (bptype_string (b->type));
6453 if (type_len > print_type_col_width)
6454 print_type_col_width = type_len;
6455
6456 nr_printable_breakpoints++;
6457 }
6458 }
6459
6460 {
6461 ui_out_emit_table table_emitter (uiout,
6462 opts.addressprint ? 6 : 5,
6463 nr_printable_breakpoints,
6464 "BreakpointTable");
6465
6466 if (nr_printable_breakpoints > 0)
6467 annotate_breakpoints_headers ();
6468 if (nr_printable_breakpoints > 0)
6469 annotate_field (0);
6470 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6471 if (nr_printable_breakpoints > 0)
6472 annotate_field (1);
6473 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6474 if (nr_printable_breakpoints > 0)
6475 annotate_field (2);
6476 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6477 if (nr_printable_breakpoints > 0)
6478 annotate_field (3);
6479 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6480 if (opts.addressprint)
6481 {
6482 if (nr_printable_breakpoints > 0)
6483 annotate_field (4);
6484 if (print_address_bits <= 32)
6485 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6486 else
6487 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6488 }
6489 if (nr_printable_breakpoints > 0)
6490 annotate_field (5);
6491 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6492 uiout->table_body ();
6493 if (nr_printable_breakpoints > 0)
6494 annotate_breakpoints_table ();
6495
6496 ALL_BREAKPOINTS (b)
6497 {
6498 QUIT;
6499 /* If we have a filter, only list the breakpoints it accepts. */
6500 if (filter && !filter (b))
6501 continue;
6502
6503 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6504 accept. Skip the others. */
6505
6506 if (bp_num_list != NULL && *bp_num_list != '\0')
6507 {
6508 if (show_internal) /* maintenance info breakpoint */
6509 {
6510 if (parse_and_eval_long (bp_num_list) != b->number)
6511 continue;
6512 }
6513 else /* all others */
6514 {
6515 if (!number_is_in_list (bp_num_list, b->number))
6516 continue;
6517 }
6518 }
6519 /* We only print out user settable breakpoints unless the
6520 show_internal is set. */
6521 if (show_internal || user_breakpoint_p (b))
6522 print_one_breakpoint (b, &last_loc, show_internal);
6523 }
6524 }
6525
6526 if (nr_printable_breakpoints == 0)
6527 {
6528 /* If there's a filter, let the caller decide how to report
6529 empty list. */
6530 if (!filter)
6531 {
6532 if (bp_num_list == NULL || *bp_num_list == '\0')
6533 uiout->message ("No breakpoints or watchpoints.\n");
6534 else
6535 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6536 bp_num_list);
6537 }
6538 }
6539 else
6540 {
6541 if (last_loc && !server_command)
6542 set_next_address (last_loc->gdbarch, last_loc->address);
6543 }
6544
6545 /* FIXME? Should this be moved up so that it is only called when
6546 there have been breakpoints? */
6547 annotate_breakpoints_table_end ();
6548
6549 return nr_printable_breakpoints;
6550 }
6551
6552 /* Display the value of default-collect in a way that is generally
6553 compatible with the breakpoint list. */
6554
6555 static void
6556 default_collect_info (void)
6557 {
6558 struct ui_out *uiout = current_uiout;
6559
6560 /* If it has no value (which is frequently the case), say nothing; a
6561 message like "No default-collect." gets in user's face when it's
6562 not wanted. */
6563 if (!*default_collect)
6564 return;
6565
6566 /* The following phrase lines up nicely with per-tracepoint collect
6567 actions. */
6568 uiout->text ("default collect ");
6569 uiout->field_string ("default-collect", default_collect);
6570 uiout->text (" \n");
6571 }
6572
6573 static void
6574 info_breakpoints_command (const char *args, int from_tty)
6575 {
6576 breakpoint_1 (args, false, NULL);
6577
6578 default_collect_info ();
6579 }
6580
6581 static void
6582 info_watchpoints_command (const char *args, int from_tty)
6583 {
6584 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6585 struct ui_out *uiout = current_uiout;
6586
6587 if (num_printed == 0)
6588 {
6589 if (args == NULL || *args == '\0')
6590 uiout->message ("No watchpoints.\n");
6591 else
6592 uiout->message ("No watchpoint matching '%s'.\n", args);
6593 }
6594 }
6595
6596 static void
6597 maintenance_info_breakpoints (const char *args, int from_tty)
6598 {
6599 breakpoint_1 (args, true, NULL);
6600
6601 default_collect_info ();
6602 }
6603
6604 static int
6605 breakpoint_has_pc (struct breakpoint *b,
6606 struct program_space *pspace,
6607 CORE_ADDR pc, struct obj_section *section)
6608 {
6609 struct bp_location *bl = b->loc;
6610
6611 for (; bl; bl = bl->next)
6612 {
6613 if (bl->pspace == pspace
6614 && bl->address == pc
6615 && (!overlay_debugging || bl->section == section))
6616 return 1;
6617 }
6618 return 0;
6619 }
6620
6621 /* Print a message describing any user-breakpoints set at PC. This
6622 concerns with logical breakpoints, so we match program spaces, not
6623 address spaces. */
6624
6625 static void
6626 describe_other_breakpoints (struct gdbarch *gdbarch,
6627 struct program_space *pspace, CORE_ADDR pc,
6628 struct obj_section *section, int thread)
6629 {
6630 int others = 0;
6631 struct breakpoint *b;
6632
6633 ALL_BREAKPOINTS (b)
6634 others += (user_breakpoint_p (b)
6635 && breakpoint_has_pc (b, pspace, pc, section));
6636 if (others > 0)
6637 {
6638 if (others == 1)
6639 printf_filtered (_("Note: breakpoint "));
6640 else /* if (others == ???) */
6641 printf_filtered (_("Note: breakpoints "));
6642 ALL_BREAKPOINTS (b)
6643 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6644 {
6645 others--;
6646 printf_filtered ("%d", b->number);
6647 if (b->thread == -1 && thread != -1)
6648 printf_filtered (" (all threads)");
6649 else if (b->thread != -1)
6650 printf_filtered (" (thread %d)", b->thread);
6651 printf_filtered ("%s%s ",
6652 ((b->enable_state == bp_disabled
6653 || b->enable_state == bp_call_disabled)
6654 ? " (disabled)"
6655 : ""),
6656 (others > 1) ? ","
6657 : ((others == 1) ? " and" : ""));
6658 }
6659 current_uiout->message (_("also set at pc %ps.\n"),
6660 styled_string (address_style.style (),
6661 paddress (gdbarch, pc)));
6662 }
6663 }
6664 \f
6665
6666 /* Return true iff it is meaningful to use the address member of LOC.
6667 For some breakpoint types, the locations' address members are
6668 irrelevant and it makes no sense to attempt to compare them to
6669 other addresses (or use them for any other purpose either).
6670
6671 More specifically, software watchpoints and catchpoints that are
6672 not backed by breakpoints always have a zero valued location
6673 address and we don't want to mark breakpoints of any of these types
6674 to be a duplicate of an actual breakpoint location at address
6675 zero. */
6676
6677 static bool
6678 bl_address_is_meaningful (bp_location *loc)
6679 {
6680 return loc->loc_type != bp_loc_other;
6681 }
6682
6683 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6684 true if LOC1 and LOC2 represent the same watchpoint location. */
6685
6686 static int
6687 watchpoint_locations_match (struct bp_location *loc1,
6688 struct bp_location *loc2)
6689 {
6690 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6691 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6692
6693 /* Both of them must exist. */
6694 gdb_assert (w1 != NULL);
6695 gdb_assert (w2 != NULL);
6696
6697 /* If the target can evaluate the condition expression in hardware,
6698 then we we need to insert both watchpoints even if they are at
6699 the same place. Otherwise the watchpoint will only trigger when
6700 the condition of whichever watchpoint was inserted evaluates to
6701 true, not giving a chance for GDB to check the condition of the
6702 other watchpoint. */
6703 if ((w1->cond_exp
6704 && target_can_accel_watchpoint_condition (loc1->address,
6705 loc1->length,
6706 loc1->watchpoint_type,
6707 w1->cond_exp.get ()))
6708 || (w2->cond_exp
6709 && target_can_accel_watchpoint_condition (loc2->address,
6710 loc2->length,
6711 loc2->watchpoint_type,
6712 w2->cond_exp.get ())))
6713 return 0;
6714
6715 /* Note that this checks the owner's type, not the location's. In
6716 case the target does not support read watchpoints, but does
6717 support access watchpoints, we'll have bp_read_watchpoint
6718 watchpoints with hw_access locations. Those should be considered
6719 duplicates of hw_read locations. The hw_read locations will
6720 become hw_access locations later. */
6721 return (loc1->owner->type == loc2->owner->type
6722 && loc1->pspace->aspace == loc2->pspace->aspace
6723 && loc1->address == loc2->address
6724 && loc1->length == loc2->length);
6725 }
6726
6727 /* See breakpoint.h. */
6728
6729 int
6730 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6731 const address_space *aspace2, CORE_ADDR addr2)
6732 {
6733 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6734 || aspace1 == aspace2)
6735 && addr1 == addr2);
6736 }
6737
6738 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6739 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6740 matches ASPACE2. On targets that have global breakpoints, the address
6741 space doesn't really matter. */
6742
6743 static int
6744 breakpoint_address_match_range (const address_space *aspace1,
6745 CORE_ADDR addr1,
6746 int len1, const address_space *aspace2,
6747 CORE_ADDR addr2)
6748 {
6749 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6750 || aspace1 == aspace2)
6751 && addr2 >= addr1 && addr2 < addr1 + len1);
6752 }
6753
6754 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6755 a ranged breakpoint. In most targets, a match happens only if ASPACE
6756 matches the breakpoint's address space. On targets that have global
6757 breakpoints, the address space doesn't really matter. */
6758
6759 static int
6760 breakpoint_location_address_match (struct bp_location *bl,
6761 const address_space *aspace,
6762 CORE_ADDR addr)
6763 {
6764 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6765 aspace, addr)
6766 || (bl->length
6767 && breakpoint_address_match_range (bl->pspace->aspace,
6768 bl->address, bl->length,
6769 aspace, addr)));
6770 }
6771
6772 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6773 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6774 match happens only if ASPACE matches the breakpoint's address
6775 space. On targets that have global breakpoints, the address space
6776 doesn't really matter. */
6777
6778 static int
6779 breakpoint_location_address_range_overlap (struct bp_location *bl,
6780 const address_space *aspace,
6781 CORE_ADDR addr, int len)
6782 {
6783 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6784 || bl->pspace->aspace == aspace)
6785 {
6786 int bl_len = bl->length != 0 ? bl->length : 1;
6787
6788 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6789 return 1;
6790 }
6791 return 0;
6792 }
6793
6794 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6795 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6796 true, otherwise returns false. */
6797
6798 static int
6799 tracepoint_locations_match (struct bp_location *loc1,
6800 struct bp_location *loc2)
6801 {
6802 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6803 /* Since tracepoint locations are never duplicated with others', tracepoint
6804 locations at the same address of different tracepoints are regarded as
6805 different locations. */
6806 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6807 else
6808 return 0;
6809 }
6810
6811 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6812 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6813 the same location. */
6814
6815 static int
6816 breakpoint_locations_match (struct bp_location *loc1,
6817 struct bp_location *loc2)
6818 {
6819 int hw_point1, hw_point2;
6820
6821 /* Both of them must not be in moribund_locations. */
6822 gdb_assert (loc1->owner != NULL);
6823 gdb_assert (loc2->owner != NULL);
6824
6825 hw_point1 = is_hardware_watchpoint (loc1->owner);
6826 hw_point2 = is_hardware_watchpoint (loc2->owner);
6827
6828 if (hw_point1 != hw_point2)
6829 return 0;
6830 else if (hw_point1)
6831 return watchpoint_locations_match (loc1, loc2);
6832 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6833 return tracepoint_locations_match (loc1, loc2);
6834 else
6835 /* We compare bp_location.length in order to cover ranged breakpoints. */
6836 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6837 loc2->pspace->aspace, loc2->address)
6838 && loc1->length == loc2->length);
6839 }
6840
6841 static void
6842 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6843 int bnum, int have_bnum)
6844 {
6845 /* The longest string possibly returned by hex_string_custom
6846 is 50 chars. These must be at least that big for safety. */
6847 char astr1[64];
6848 char astr2[64];
6849
6850 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6851 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6852 if (have_bnum)
6853 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6854 bnum, astr1, astr2);
6855 else
6856 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6857 }
6858
6859 /* Adjust a breakpoint's address to account for architectural
6860 constraints on breakpoint placement. Return the adjusted address.
6861 Note: Very few targets require this kind of adjustment. For most
6862 targets, this function is simply the identity function. */
6863
6864 static CORE_ADDR
6865 adjust_breakpoint_address (struct gdbarch *gdbarch,
6866 CORE_ADDR bpaddr, enum bptype bptype)
6867 {
6868 if (bptype == bp_watchpoint
6869 || bptype == bp_hardware_watchpoint
6870 || bptype == bp_read_watchpoint
6871 || bptype == bp_access_watchpoint
6872 || bptype == bp_catchpoint)
6873 {
6874 /* Watchpoints and the various bp_catch_* eventpoints should not
6875 have their addresses modified. */
6876 return bpaddr;
6877 }
6878 else if (bptype == bp_single_step)
6879 {
6880 /* Single-step breakpoints should not have their addresses
6881 modified. If there's any architectural constrain that
6882 applies to this address, then it should have already been
6883 taken into account when the breakpoint was created in the
6884 first place. If we didn't do this, stepping through e.g.,
6885 Thumb-2 IT blocks would break. */
6886 return bpaddr;
6887 }
6888 else
6889 {
6890 CORE_ADDR adjusted_bpaddr = bpaddr;
6891
6892 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6893 {
6894 /* Some targets have architectural constraints on the placement
6895 of breakpoint instructions. Obtain the adjusted address. */
6896 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6897 }
6898
6899 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
6900
6901 /* An adjusted breakpoint address can significantly alter
6902 a user's expectations. Print a warning if an adjustment
6903 is required. */
6904 if (adjusted_bpaddr != bpaddr)
6905 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6906
6907 return adjusted_bpaddr;
6908 }
6909 }
6910
6911 static bp_loc_type
6912 bp_location_from_bp_type (bptype type)
6913 {
6914 switch (type)
6915 {
6916 case bp_breakpoint:
6917 case bp_single_step:
6918 case bp_until:
6919 case bp_finish:
6920 case bp_longjmp:
6921 case bp_longjmp_resume:
6922 case bp_longjmp_call_dummy:
6923 case bp_exception:
6924 case bp_exception_resume:
6925 case bp_step_resume:
6926 case bp_hp_step_resume:
6927 case bp_watchpoint_scope:
6928 case bp_call_dummy:
6929 case bp_std_terminate:
6930 case bp_shlib_event:
6931 case bp_thread_event:
6932 case bp_overlay_event:
6933 case bp_jit_event:
6934 case bp_longjmp_master:
6935 case bp_std_terminate_master:
6936 case bp_exception_master:
6937 case bp_gnu_ifunc_resolver:
6938 case bp_gnu_ifunc_resolver_return:
6939 case bp_dprintf:
6940 return bp_loc_software_breakpoint;
6941 case bp_hardware_breakpoint:
6942 return bp_loc_hardware_breakpoint;
6943 case bp_hardware_watchpoint:
6944 case bp_read_watchpoint:
6945 case bp_access_watchpoint:
6946 return bp_loc_hardware_watchpoint;
6947 case bp_watchpoint:
6948 case bp_catchpoint:
6949 case bp_tracepoint:
6950 case bp_fast_tracepoint:
6951 case bp_static_tracepoint:
6952 return bp_loc_other;
6953 default:
6954 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6955 }
6956 }
6957
6958 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6959 {
6960 this->owner = owner;
6961 this->cond_bytecode = NULL;
6962 this->shlib_disabled = 0;
6963 this->enabled = 1;
6964
6965 this->loc_type = type;
6966
6967 if (this->loc_type == bp_loc_software_breakpoint
6968 || this->loc_type == bp_loc_hardware_breakpoint)
6969 mark_breakpoint_location_modified (this);
6970
6971 this->refc = 1;
6972 }
6973
6974 bp_location::bp_location (breakpoint *owner)
6975 : bp_location::bp_location (owner,
6976 bp_location_from_bp_type (owner->type))
6977 {
6978 }
6979
6980 /* Allocate a struct bp_location. */
6981
6982 static struct bp_location *
6983 allocate_bp_location (struct breakpoint *bpt)
6984 {
6985 return bpt->ops->allocate_location (bpt);
6986 }
6987
6988 static void
6989 free_bp_location (struct bp_location *loc)
6990 {
6991 delete loc;
6992 }
6993
6994 /* Increment reference count. */
6995
6996 static void
6997 incref_bp_location (struct bp_location *bl)
6998 {
6999 ++bl->refc;
7000 }
7001
7002 /* Decrement reference count. If the reference count reaches 0,
7003 destroy the bp_location. Sets *BLP to NULL. */
7004
7005 static void
7006 decref_bp_location (struct bp_location **blp)
7007 {
7008 gdb_assert ((*blp)->refc > 0);
7009
7010 if (--(*blp)->refc == 0)
7011 free_bp_location (*blp);
7012 *blp = NULL;
7013 }
7014
7015 /* Add breakpoint B at the end of the global breakpoint chain. */
7016
7017 static breakpoint *
7018 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7019 {
7020 struct breakpoint *b1;
7021 struct breakpoint *result = b.get ();
7022
7023 /* Add this breakpoint to the end of the chain so that a list of
7024 breakpoints will come out in order of increasing numbers. */
7025
7026 b1 = breakpoint_chain;
7027 if (b1 == 0)
7028 breakpoint_chain = b.release ();
7029 else
7030 {
7031 while (b1->next)
7032 b1 = b1->next;
7033 b1->next = b.release ();
7034 }
7035
7036 return result;
7037 }
7038
7039 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7040
7041 static void
7042 init_raw_breakpoint_without_location (struct breakpoint *b,
7043 struct gdbarch *gdbarch,
7044 enum bptype bptype,
7045 const struct breakpoint_ops *ops)
7046 {
7047 gdb_assert (ops != NULL);
7048
7049 b->ops = ops;
7050 b->type = bptype;
7051 b->gdbarch = gdbarch;
7052 b->language = current_language->la_language;
7053 b->input_radix = input_radix;
7054 b->related_breakpoint = b;
7055 }
7056
7057 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7058 that has type BPTYPE and has no locations as yet. */
7059
7060 static struct breakpoint *
7061 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7062 enum bptype bptype,
7063 const struct breakpoint_ops *ops)
7064 {
7065 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7066
7067 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7068 return add_to_breakpoint_chain (std::move (b));
7069 }
7070
7071 /* Initialize loc->function_name. */
7072
7073 static void
7074 set_breakpoint_location_function (struct bp_location *loc)
7075 {
7076 gdb_assert (loc->owner != NULL);
7077
7078 if (loc->owner->type == bp_breakpoint
7079 || loc->owner->type == bp_hardware_breakpoint
7080 || is_tracepoint (loc->owner))
7081 {
7082 const char *function_name;
7083
7084 if (loc->msymbol != NULL
7085 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7086 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7087 {
7088 struct breakpoint *b = loc->owner;
7089
7090 function_name = MSYMBOL_LINKAGE_NAME (loc->msymbol);
7091
7092 if (b->type == bp_breakpoint && b->loc == loc
7093 && loc->next == NULL && b->related_breakpoint == b)
7094 {
7095 /* Create only the whole new breakpoint of this type but do not
7096 mess more complicated breakpoints with multiple locations. */
7097 b->type = bp_gnu_ifunc_resolver;
7098 /* Remember the resolver's address for use by the return
7099 breakpoint. */
7100 loc->related_address = loc->address;
7101 }
7102 }
7103 else
7104 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7105
7106 if (function_name)
7107 loc->function_name = xstrdup (function_name);
7108 }
7109 }
7110
7111 /* Attempt to determine architecture of location identified by SAL. */
7112 struct gdbarch *
7113 get_sal_arch (struct symtab_and_line sal)
7114 {
7115 if (sal.section)
7116 return get_objfile_arch (sal.section->objfile);
7117 if (sal.symtab)
7118 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7119
7120 return NULL;
7121 }
7122
7123 /* Low level routine for partially initializing a breakpoint of type
7124 BPTYPE. The newly created breakpoint's address, section, source
7125 file name, and line number are provided by SAL.
7126
7127 It is expected that the caller will complete the initialization of
7128 the newly created breakpoint struct as well as output any status
7129 information regarding the creation of a new breakpoint. */
7130
7131 static void
7132 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7133 struct symtab_and_line sal, enum bptype bptype,
7134 const struct breakpoint_ops *ops)
7135 {
7136 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7137
7138 add_location_to_breakpoint (b, &sal);
7139
7140 if (bptype != bp_catchpoint)
7141 gdb_assert (sal.pspace != NULL);
7142
7143 /* Store the program space that was used to set the breakpoint,
7144 except for ordinary breakpoints, which are independent of the
7145 program space. */
7146 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7147 b->pspace = sal.pspace;
7148 }
7149
7150 /* set_raw_breakpoint is a low level routine for allocating and
7151 partially initializing a breakpoint of type BPTYPE. The newly
7152 created breakpoint's address, section, source file name, and line
7153 number are provided by SAL. The newly created and partially
7154 initialized breakpoint is added to the breakpoint chain and
7155 is also returned as the value of this function.
7156
7157 It is expected that the caller will complete the initialization of
7158 the newly created breakpoint struct as well as output any status
7159 information regarding the creation of a new breakpoint. In
7160 particular, set_raw_breakpoint does NOT set the breakpoint
7161 number! Care should be taken to not allow an error to occur
7162 prior to completing the initialization of the breakpoint. If this
7163 should happen, a bogus breakpoint will be left on the chain. */
7164
7165 struct breakpoint *
7166 set_raw_breakpoint (struct gdbarch *gdbarch,
7167 struct symtab_and_line sal, enum bptype bptype,
7168 const struct breakpoint_ops *ops)
7169 {
7170 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7171
7172 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7173 return add_to_breakpoint_chain (std::move (b));
7174 }
7175
7176 /* Call this routine when stepping and nexting to enable a breakpoint
7177 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7178 initiated the operation. */
7179
7180 void
7181 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7182 {
7183 struct breakpoint *b, *b_tmp;
7184 int thread = tp->global_num;
7185
7186 /* To avoid having to rescan all objfile symbols at every step,
7187 we maintain a list of continually-inserted but always disabled
7188 longjmp "master" breakpoints. Here, we simply create momentary
7189 clones of those and enable them for the requested thread. */
7190 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7191 if (b->pspace == current_program_space
7192 && (b->type == bp_longjmp_master
7193 || b->type == bp_exception_master))
7194 {
7195 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7196 struct breakpoint *clone;
7197
7198 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7199 after their removal. */
7200 clone = momentary_breakpoint_from_master (b, type,
7201 &momentary_breakpoint_ops, 1);
7202 clone->thread = thread;
7203 }
7204
7205 tp->initiating_frame = frame;
7206 }
7207
7208 /* Delete all longjmp breakpoints from THREAD. */
7209 void
7210 delete_longjmp_breakpoint (int thread)
7211 {
7212 struct breakpoint *b, *b_tmp;
7213
7214 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7215 if (b->type == bp_longjmp || b->type == bp_exception)
7216 {
7217 if (b->thread == thread)
7218 delete_breakpoint (b);
7219 }
7220 }
7221
7222 void
7223 delete_longjmp_breakpoint_at_next_stop (int thread)
7224 {
7225 struct breakpoint *b, *b_tmp;
7226
7227 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7228 if (b->type == bp_longjmp || b->type == bp_exception)
7229 {
7230 if (b->thread == thread)
7231 b->disposition = disp_del_at_next_stop;
7232 }
7233 }
7234
7235 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7236 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7237 pointer to any of them. Return NULL if this system cannot place longjmp
7238 breakpoints. */
7239
7240 struct breakpoint *
7241 set_longjmp_breakpoint_for_call_dummy (void)
7242 {
7243 struct breakpoint *b, *retval = NULL;
7244
7245 ALL_BREAKPOINTS (b)
7246 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7247 {
7248 struct breakpoint *new_b;
7249
7250 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7251 &momentary_breakpoint_ops,
7252 1);
7253 new_b->thread = inferior_thread ()->global_num;
7254
7255 /* Link NEW_B into the chain of RETVAL breakpoints. */
7256
7257 gdb_assert (new_b->related_breakpoint == new_b);
7258 if (retval == NULL)
7259 retval = new_b;
7260 new_b->related_breakpoint = retval;
7261 while (retval->related_breakpoint != new_b->related_breakpoint)
7262 retval = retval->related_breakpoint;
7263 retval->related_breakpoint = new_b;
7264 }
7265
7266 return retval;
7267 }
7268
7269 /* Verify all existing dummy frames and their associated breakpoints for
7270 TP. Remove those which can no longer be found in the current frame
7271 stack.
7272
7273 You should call this function only at places where it is safe to currently
7274 unwind the whole stack. Failed stack unwind would discard live dummy
7275 frames. */
7276
7277 void
7278 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7279 {
7280 struct breakpoint *b, *b_tmp;
7281
7282 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7283 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7284 {
7285 struct breakpoint *dummy_b = b->related_breakpoint;
7286
7287 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7288 dummy_b = dummy_b->related_breakpoint;
7289 if (dummy_b->type != bp_call_dummy
7290 || frame_find_by_id (dummy_b->frame_id) != NULL)
7291 continue;
7292
7293 dummy_frame_discard (dummy_b->frame_id, tp);
7294
7295 while (b->related_breakpoint != b)
7296 {
7297 if (b_tmp == b->related_breakpoint)
7298 b_tmp = b->related_breakpoint->next;
7299 delete_breakpoint (b->related_breakpoint);
7300 }
7301 delete_breakpoint (b);
7302 }
7303 }
7304
7305 void
7306 enable_overlay_breakpoints (void)
7307 {
7308 struct breakpoint *b;
7309
7310 ALL_BREAKPOINTS (b)
7311 if (b->type == bp_overlay_event)
7312 {
7313 b->enable_state = bp_enabled;
7314 update_global_location_list (UGLL_MAY_INSERT);
7315 overlay_events_enabled = 1;
7316 }
7317 }
7318
7319 void
7320 disable_overlay_breakpoints (void)
7321 {
7322 struct breakpoint *b;
7323
7324 ALL_BREAKPOINTS (b)
7325 if (b->type == bp_overlay_event)
7326 {
7327 b->enable_state = bp_disabled;
7328 update_global_location_list (UGLL_DONT_INSERT);
7329 overlay_events_enabled = 0;
7330 }
7331 }
7332
7333 /* Set an active std::terminate breakpoint for each std::terminate
7334 master breakpoint. */
7335 void
7336 set_std_terminate_breakpoint (void)
7337 {
7338 struct breakpoint *b, *b_tmp;
7339
7340 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7341 if (b->pspace == current_program_space
7342 && b->type == bp_std_terminate_master)
7343 {
7344 momentary_breakpoint_from_master (b, bp_std_terminate,
7345 &momentary_breakpoint_ops, 1);
7346 }
7347 }
7348
7349 /* Delete all the std::terminate breakpoints. */
7350 void
7351 delete_std_terminate_breakpoint (void)
7352 {
7353 struct breakpoint *b, *b_tmp;
7354
7355 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7356 if (b->type == bp_std_terminate)
7357 delete_breakpoint (b);
7358 }
7359
7360 struct breakpoint *
7361 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7362 {
7363 struct breakpoint *b;
7364
7365 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7366 &internal_breakpoint_ops);
7367
7368 b->enable_state = bp_enabled;
7369 /* location has to be used or breakpoint_re_set will delete me. */
7370 b->location = new_address_location (b->loc->address, NULL, 0);
7371
7372 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7373
7374 return b;
7375 }
7376
7377 struct lang_and_radix
7378 {
7379 enum language lang;
7380 int radix;
7381 };
7382
7383 /* Create a breakpoint for JIT code registration and unregistration. */
7384
7385 struct breakpoint *
7386 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7387 {
7388 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7389 &internal_breakpoint_ops);
7390 }
7391
7392 /* Remove JIT code registration and unregistration breakpoint(s). */
7393
7394 void
7395 remove_jit_event_breakpoints (void)
7396 {
7397 struct breakpoint *b, *b_tmp;
7398
7399 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7400 if (b->type == bp_jit_event
7401 && b->loc->pspace == current_program_space)
7402 delete_breakpoint (b);
7403 }
7404
7405 void
7406 remove_solib_event_breakpoints (void)
7407 {
7408 struct breakpoint *b, *b_tmp;
7409
7410 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7411 if (b->type == bp_shlib_event
7412 && b->loc->pspace == current_program_space)
7413 delete_breakpoint (b);
7414 }
7415
7416 /* See breakpoint.h. */
7417
7418 void
7419 remove_solib_event_breakpoints_at_next_stop (void)
7420 {
7421 struct breakpoint *b, *b_tmp;
7422
7423 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7424 if (b->type == bp_shlib_event
7425 && b->loc->pspace == current_program_space)
7426 b->disposition = disp_del_at_next_stop;
7427 }
7428
7429 /* Helper for create_solib_event_breakpoint /
7430 create_and_insert_solib_event_breakpoint. Allows specifying which
7431 INSERT_MODE to pass through to update_global_location_list. */
7432
7433 static struct breakpoint *
7434 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7435 enum ugll_insert_mode insert_mode)
7436 {
7437 struct breakpoint *b;
7438
7439 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7440 &internal_breakpoint_ops);
7441 update_global_location_list_nothrow (insert_mode);
7442 return b;
7443 }
7444
7445 struct breakpoint *
7446 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7447 {
7448 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7449 }
7450
7451 /* See breakpoint.h. */
7452
7453 struct breakpoint *
7454 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7455 {
7456 struct breakpoint *b;
7457
7458 /* Explicitly tell update_global_location_list to insert
7459 locations. */
7460 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7461 if (!b->loc->inserted)
7462 {
7463 delete_breakpoint (b);
7464 return NULL;
7465 }
7466 return b;
7467 }
7468
7469 /* Disable any breakpoints that are on code in shared libraries. Only
7470 apply to enabled breakpoints, disabled ones can just stay disabled. */
7471
7472 void
7473 disable_breakpoints_in_shlibs (void)
7474 {
7475 struct bp_location *loc, **locp_tmp;
7476
7477 ALL_BP_LOCATIONS (loc, locp_tmp)
7478 {
7479 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7480 struct breakpoint *b = loc->owner;
7481
7482 /* We apply the check to all breakpoints, including disabled for
7483 those with loc->duplicate set. This is so that when breakpoint
7484 becomes enabled, or the duplicate is removed, gdb will try to
7485 insert all breakpoints. If we don't set shlib_disabled here,
7486 we'll try to insert those breakpoints and fail. */
7487 if (((b->type == bp_breakpoint)
7488 || (b->type == bp_jit_event)
7489 || (b->type == bp_hardware_breakpoint)
7490 || (is_tracepoint (b)))
7491 && loc->pspace == current_program_space
7492 && !loc->shlib_disabled
7493 && solib_name_from_address (loc->pspace, loc->address)
7494 )
7495 {
7496 loc->shlib_disabled = 1;
7497 }
7498 }
7499 }
7500
7501 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7502 notification of unloaded_shlib. Only apply to enabled breakpoints,
7503 disabled ones can just stay disabled. */
7504
7505 static void
7506 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7507 {
7508 struct bp_location *loc, **locp_tmp;
7509 int disabled_shlib_breaks = 0;
7510
7511 ALL_BP_LOCATIONS (loc, locp_tmp)
7512 {
7513 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7514 struct breakpoint *b = loc->owner;
7515
7516 if (solib->pspace == loc->pspace
7517 && !loc->shlib_disabled
7518 && (((b->type == bp_breakpoint
7519 || b->type == bp_jit_event
7520 || b->type == bp_hardware_breakpoint)
7521 && (loc->loc_type == bp_loc_hardware_breakpoint
7522 || loc->loc_type == bp_loc_software_breakpoint))
7523 || is_tracepoint (b))
7524 && solib_contains_address_p (solib, loc->address))
7525 {
7526 loc->shlib_disabled = 1;
7527 /* At this point, we cannot rely on remove_breakpoint
7528 succeeding so we must mark the breakpoint as not inserted
7529 to prevent future errors occurring in remove_breakpoints. */
7530 loc->inserted = 0;
7531
7532 /* This may cause duplicate notifications for the same breakpoint. */
7533 gdb::observers::breakpoint_modified.notify (b);
7534
7535 if (!disabled_shlib_breaks)
7536 {
7537 target_terminal::ours_for_output ();
7538 warning (_("Temporarily disabling breakpoints "
7539 "for unloaded shared library \"%s\""),
7540 solib->so_name);
7541 }
7542 disabled_shlib_breaks = 1;
7543 }
7544 }
7545 }
7546
7547 /* Disable any breakpoints and tracepoints in OBJFILE upon
7548 notification of free_objfile. Only apply to enabled breakpoints,
7549 disabled ones can just stay disabled. */
7550
7551 static void
7552 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7553 {
7554 struct breakpoint *b;
7555
7556 if (objfile == NULL)
7557 return;
7558
7559 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7560 managed by the user with add-symbol-file/remove-symbol-file.
7561 Similarly to how breakpoints in shared libraries are handled in
7562 response to "nosharedlibrary", mark breakpoints in such modules
7563 shlib_disabled so they end up uninserted on the next global
7564 location list update. Shared libraries not loaded by the user
7565 aren't handled here -- they're already handled in
7566 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7567 solib_unloaded observer. We skip objfiles that are not
7568 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7569 main objfile). */
7570 if ((objfile->flags & OBJF_SHARED) == 0
7571 || (objfile->flags & OBJF_USERLOADED) == 0)
7572 return;
7573
7574 ALL_BREAKPOINTS (b)
7575 {
7576 struct bp_location *loc;
7577 int bp_modified = 0;
7578
7579 if (!is_breakpoint (b) && !is_tracepoint (b))
7580 continue;
7581
7582 for (loc = b->loc; loc != NULL; loc = loc->next)
7583 {
7584 CORE_ADDR loc_addr = loc->address;
7585
7586 if (loc->loc_type != bp_loc_hardware_breakpoint
7587 && loc->loc_type != bp_loc_software_breakpoint)
7588 continue;
7589
7590 if (loc->shlib_disabled != 0)
7591 continue;
7592
7593 if (objfile->pspace != loc->pspace)
7594 continue;
7595
7596 if (loc->loc_type != bp_loc_hardware_breakpoint
7597 && loc->loc_type != bp_loc_software_breakpoint)
7598 continue;
7599
7600 if (is_addr_in_objfile (loc_addr, objfile))
7601 {
7602 loc->shlib_disabled = 1;
7603 /* At this point, we don't know whether the object was
7604 unmapped from the inferior or not, so leave the
7605 inserted flag alone. We'll handle failure to
7606 uninsert quietly, in case the object was indeed
7607 unmapped. */
7608
7609 mark_breakpoint_location_modified (loc);
7610
7611 bp_modified = 1;
7612 }
7613 }
7614
7615 if (bp_modified)
7616 gdb::observers::breakpoint_modified.notify (b);
7617 }
7618 }
7619
7620 /* FORK & VFORK catchpoints. */
7621
7622 /* An instance of this type is used to represent a fork or vfork
7623 catchpoint. A breakpoint is really of this type iff its ops pointer points
7624 to CATCH_FORK_BREAKPOINT_OPS. */
7625
7626 struct fork_catchpoint : public breakpoint
7627 {
7628 /* Process id of a child process whose forking triggered this
7629 catchpoint. This field is only valid immediately after this
7630 catchpoint has triggered. */
7631 ptid_t forked_inferior_pid;
7632 };
7633
7634 /* Implement the "insert" breakpoint_ops method for fork
7635 catchpoints. */
7636
7637 static int
7638 insert_catch_fork (struct bp_location *bl)
7639 {
7640 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7641 }
7642
7643 /* Implement the "remove" breakpoint_ops method for fork
7644 catchpoints. */
7645
7646 static int
7647 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7648 {
7649 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7650 }
7651
7652 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7653 catchpoints. */
7654
7655 static int
7656 breakpoint_hit_catch_fork (const struct bp_location *bl,
7657 const address_space *aspace, CORE_ADDR bp_addr,
7658 const struct target_waitstatus *ws)
7659 {
7660 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7661
7662 if (ws->kind != TARGET_WAITKIND_FORKED)
7663 return 0;
7664
7665 c->forked_inferior_pid = ws->value.related_pid;
7666 return 1;
7667 }
7668
7669 /* Implement the "print_it" breakpoint_ops method for fork
7670 catchpoints. */
7671
7672 static enum print_stop_action
7673 print_it_catch_fork (bpstat bs)
7674 {
7675 struct ui_out *uiout = current_uiout;
7676 struct breakpoint *b = bs->breakpoint_at;
7677 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7678
7679 annotate_catchpoint (b->number);
7680 maybe_print_thread_hit_breakpoint (uiout);
7681 if (b->disposition == disp_del)
7682 uiout->text ("Temporary catchpoint ");
7683 else
7684 uiout->text ("Catchpoint ");
7685 if (uiout->is_mi_like_p ())
7686 {
7687 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7688 uiout->field_string ("disp", bpdisp_text (b->disposition));
7689 }
7690 uiout->field_signed ("bkptno", b->number);
7691 uiout->text (" (forked process ");
7692 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7693 uiout->text ("), ");
7694 return PRINT_SRC_AND_LOC;
7695 }
7696
7697 /* Implement the "print_one" breakpoint_ops method for fork
7698 catchpoints. */
7699
7700 static void
7701 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7702 {
7703 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7704 struct value_print_options opts;
7705 struct ui_out *uiout = current_uiout;
7706
7707 get_user_print_options (&opts);
7708
7709 /* Field 4, the address, is omitted (which makes the columns not
7710 line up too nicely with the headers, but the effect is relatively
7711 readable). */
7712 if (opts.addressprint)
7713 uiout->field_skip ("addr");
7714 annotate_field (5);
7715 uiout->text ("fork");
7716 if (c->forked_inferior_pid != null_ptid)
7717 {
7718 uiout->text (", process ");
7719 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7720 uiout->spaces (1);
7721 }
7722
7723 if (uiout->is_mi_like_p ())
7724 uiout->field_string ("catch-type", "fork");
7725 }
7726
7727 /* Implement the "print_mention" breakpoint_ops method for fork
7728 catchpoints. */
7729
7730 static void
7731 print_mention_catch_fork (struct breakpoint *b)
7732 {
7733 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7734 }
7735
7736 /* Implement the "print_recreate" breakpoint_ops method for fork
7737 catchpoints. */
7738
7739 static void
7740 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7741 {
7742 fprintf_unfiltered (fp, "catch fork");
7743 print_recreate_thread (b, fp);
7744 }
7745
7746 /* The breakpoint_ops structure to be used in fork catchpoints. */
7747
7748 static struct breakpoint_ops catch_fork_breakpoint_ops;
7749
7750 /* Implement the "insert" breakpoint_ops method for vfork
7751 catchpoints. */
7752
7753 static int
7754 insert_catch_vfork (struct bp_location *bl)
7755 {
7756 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7757 }
7758
7759 /* Implement the "remove" breakpoint_ops method for vfork
7760 catchpoints. */
7761
7762 static int
7763 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7764 {
7765 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7766 }
7767
7768 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7769 catchpoints. */
7770
7771 static int
7772 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7773 const address_space *aspace, CORE_ADDR bp_addr,
7774 const struct target_waitstatus *ws)
7775 {
7776 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7777
7778 if (ws->kind != TARGET_WAITKIND_VFORKED)
7779 return 0;
7780
7781 c->forked_inferior_pid = ws->value.related_pid;
7782 return 1;
7783 }
7784
7785 /* Implement the "print_it" breakpoint_ops method for vfork
7786 catchpoints. */
7787
7788 static enum print_stop_action
7789 print_it_catch_vfork (bpstat bs)
7790 {
7791 struct ui_out *uiout = current_uiout;
7792 struct breakpoint *b = bs->breakpoint_at;
7793 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7794
7795 annotate_catchpoint (b->number);
7796 maybe_print_thread_hit_breakpoint (uiout);
7797 if (b->disposition == disp_del)
7798 uiout->text ("Temporary catchpoint ");
7799 else
7800 uiout->text ("Catchpoint ");
7801 if (uiout->is_mi_like_p ())
7802 {
7803 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7804 uiout->field_string ("disp", bpdisp_text (b->disposition));
7805 }
7806 uiout->field_signed ("bkptno", b->number);
7807 uiout->text (" (vforked process ");
7808 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7809 uiout->text ("), ");
7810 return PRINT_SRC_AND_LOC;
7811 }
7812
7813 /* Implement the "print_one" breakpoint_ops method for vfork
7814 catchpoints. */
7815
7816 static void
7817 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7818 {
7819 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7820 struct value_print_options opts;
7821 struct ui_out *uiout = current_uiout;
7822
7823 get_user_print_options (&opts);
7824 /* Field 4, the address, is omitted (which makes the columns not
7825 line up too nicely with the headers, but the effect is relatively
7826 readable). */
7827 if (opts.addressprint)
7828 uiout->field_skip ("addr");
7829 annotate_field (5);
7830 uiout->text ("vfork");
7831 if (c->forked_inferior_pid != null_ptid)
7832 {
7833 uiout->text (", process ");
7834 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7835 uiout->spaces (1);
7836 }
7837
7838 if (uiout->is_mi_like_p ())
7839 uiout->field_string ("catch-type", "vfork");
7840 }
7841
7842 /* Implement the "print_mention" breakpoint_ops method for vfork
7843 catchpoints. */
7844
7845 static void
7846 print_mention_catch_vfork (struct breakpoint *b)
7847 {
7848 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7849 }
7850
7851 /* Implement the "print_recreate" breakpoint_ops method for vfork
7852 catchpoints. */
7853
7854 static void
7855 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7856 {
7857 fprintf_unfiltered (fp, "catch vfork");
7858 print_recreate_thread (b, fp);
7859 }
7860
7861 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7862
7863 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7864
7865 /* An instance of this type is used to represent an solib catchpoint.
7866 A breakpoint is really of this type iff its ops pointer points to
7867 CATCH_SOLIB_BREAKPOINT_OPS. */
7868
7869 struct solib_catchpoint : public breakpoint
7870 {
7871 ~solib_catchpoint () override;
7872
7873 /* True for "catch load", false for "catch unload". */
7874 unsigned char is_load;
7875
7876 /* Regular expression to match, if any. COMPILED is only valid when
7877 REGEX is non-NULL. */
7878 char *regex;
7879 std::unique_ptr<compiled_regex> compiled;
7880 };
7881
7882 solib_catchpoint::~solib_catchpoint ()
7883 {
7884 xfree (this->regex);
7885 }
7886
7887 static int
7888 insert_catch_solib (struct bp_location *ignore)
7889 {
7890 return 0;
7891 }
7892
7893 static int
7894 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7895 {
7896 return 0;
7897 }
7898
7899 static int
7900 breakpoint_hit_catch_solib (const struct bp_location *bl,
7901 const address_space *aspace,
7902 CORE_ADDR bp_addr,
7903 const struct target_waitstatus *ws)
7904 {
7905 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7906 struct breakpoint *other;
7907
7908 if (ws->kind == TARGET_WAITKIND_LOADED)
7909 return 1;
7910
7911 ALL_BREAKPOINTS (other)
7912 {
7913 struct bp_location *other_bl;
7914
7915 if (other == bl->owner)
7916 continue;
7917
7918 if (other->type != bp_shlib_event)
7919 continue;
7920
7921 if (self->pspace != NULL && other->pspace != self->pspace)
7922 continue;
7923
7924 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7925 {
7926 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7927 return 1;
7928 }
7929 }
7930
7931 return 0;
7932 }
7933
7934 static void
7935 check_status_catch_solib (struct bpstats *bs)
7936 {
7937 struct solib_catchpoint *self
7938 = (struct solib_catchpoint *) bs->breakpoint_at;
7939
7940 if (self->is_load)
7941 {
7942 for (so_list *iter : current_program_space->added_solibs)
7943 {
7944 if (!self->regex
7945 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
7946 return;
7947 }
7948 }
7949 else
7950 {
7951 for (const std::string &iter : current_program_space->deleted_solibs)
7952 {
7953 if (!self->regex
7954 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
7955 return;
7956 }
7957 }
7958
7959 bs->stop = 0;
7960 bs->print_it = print_it_noop;
7961 }
7962
7963 static enum print_stop_action
7964 print_it_catch_solib (bpstat bs)
7965 {
7966 struct breakpoint *b = bs->breakpoint_at;
7967 struct ui_out *uiout = current_uiout;
7968
7969 annotate_catchpoint (b->number);
7970 maybe_print_thread_hit_breakpoint (uiout);
7971 if (b->disposition == disp_del)
7972 uiout->text ("Temporary catchpoint ");
7973 else
7974 uiout->text ("Catchpoint ");
7975 uiout->field_signed ("bkptno", b->number);
7976 uiout->text ("\n");
7977 if (uiout->is_mi_like_p ())
7978 uiout->field_string ("disp", bpdisp_text (b->disposition));
7979 print_solib_event (1);
7980 return PRINT_SRC_AND_LOC;
7981 }
7982
7983 static void
7984 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7985 {
7986 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7987 struct value_print_options opts;
7988 struct ui_out *uiout = current_uiout;
7989
7990 get_user_print_options (&opts);
7991 /* Field 4, the address, is omitted (which makes the columns not
7992 line up too nicely with the headers, but the effect is relatively
7993 readable). */
7994 if (opts.addressprint)
7995 {
7996 annotate_field (4);
7997 uiout->field_skip ("addr");
7998 }
7999
8000 std::string msg;
8001 annotate_field (5);
8002 if (self->is_load)
8003 {
8004 if (self->regex)
8005 msg = string_printf (_("load of library matching %s"), self->regex);
8006 else
8007 msg = _("load of library");
8008 }
8009 else
8010 {
8011 if (self->regex)
8012 msg = string_printf (_("unload of library matching %s"), self->regex);
8013 else
8014 msg = _("unload of library");
8015 }
8016 uiout->field_string ("what", msg);
8017
8018 if (uiout->is_mi_like_p ())
8019 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8020 }
8021
8022 static void
8023 print_mention_catch_solib (struct breakpoint *b)
8024 {
8025 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8026
8027 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8028 self->is_load ? "load" : "unload");
8029 }
8030
8031 static void
8032 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8033 {
8034 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8035
8036 fprintf_unfiltered (fp, "%s %s",
8037 b->disposition == disp_del ? "tcatch" : "catch",
8038 self->is_load ? "load" : "unload");
8039 if (self->regex)
8040 fprintf_unfiltered (fp, " %s", self->regex);
8041 fprintf_unfiltered (fp, "\n");
8042 }
8043
8044 static struct breakpoint_ops catch_solib_breakpoint_ops;
8045
8046 /* Shared helper function (MI and CLI) for creating and installing
8047 a shared object event catchpoint. If IS_LOAD is non-zero then
8048 the events to be caught are load events, otherwise they are
8049 unload events. If IS_TEMP is non-zero the catchpoint is a
8050 temporary one. If ENABLED is non-zero the catchpoint is
8051 created in an enabled state. */
8052
8053 void
8054 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8055 {
8056 struct gdbarch *gdbarch = get_current_arch ();
8057
8058 if (!arg)
8059 arg = "";
8060 arg = skip_spaces (arg);
8061
8062 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8063
8064 if (*arg != '\0')
8065 {
8066 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8067 _("Invalid regexp")));
8068 c->regex = xstrdup (arg);
8069 }
8070
8071 c->is_load = is_load;
8072 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8073 &catch_solib_breakpoint_ops);
8074
8075 c->enable_state = enabled ? bp_enabled : bp_disabled;
8076
8077 install_breakpoint (0, std::move (c), 1);
8078 }
8079
8080 /* A helper function that does all the work for "catch load" and
8081 "catch unload". */
8082
8083 static void
8084 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8085 struct cmd_list_element *command)
8086 {
8087 int tempflag;
8088 const int enabled = 1;
8089
8090 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8091
8092 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8093 }
8094
8095 static void
8096 catch_load_command_1 (const char *arg, int from_tty,
8097 struct cmd_list_element *command)
8098 {
8099 catch_load_or_unload (arg, from_tty, 1, command);
8100 }
8101
8102 static void
8103 catch_unload_command_1 (const char *arg, int from_tty,
8104 struct cmd_list_element *command)
8105 {
8106 catch_load_or_unload (arg, from_tty, 0, command);
8107 }
8108
8109 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8110 is non-zero, then make the breakpoint temporary. If COND_STRING is
8111 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8112 the breakpoint_ops structure associated to the catchpoint. */
8113
8114 void
8115 init_catchpoint (struct breakpoint *b,
8116 struct gdbarch *gdbarch, int tempflag,
8117 const char *cond_string,
8118 const struct breakpoint_ops *ops)
8119 {
8120 symtab_and_line sal;
8121 sal.pspace = current_program_space;
8122
8123 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8124
8125 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8126 b->disposition = tempflag ? disp_del : disp_donttouch;
8127 }
8128
8129 void
8130 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8131 {
8132 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8133 set_breakpoint_number (internal, b);
8134 if (is_tracepoint (b))
8135 set_tracepoint_count (breakpoint_count);
8136 if (!internal)
8137 mention (b);
8138 gdb::observers::breakpoint_created.notify (b);
8139
8140 if (update_gll)
8141 update_global_location_list (UGLL_MAY_INSERT);
8142 }
8143
8144 static void
8145 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8146 int tempflag, const char *cond_string,
8147 const struct breakpoint_ops *ops)
8148 {
8149 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8150
8151 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8152
8153 c->forked_inferior_pid = null_ptid;
8154
8155 install_breakpoint (0, std::move (c), 1);
8156 }
8157
8158 /* Exec catchpoints. */
8159
8160 /* An instance of this type is used to represent an exec catchpoint.
8161 A breakpoint is really of this type iff its ops pointer points to
8162 CATCH_EXEC_BREAKPOINT_OPS. */
8163
8164 struct exec_catchpoint : public breakpoint
8165 {
8166 ~exec_catchpoint () override;
8167
8168 /* Filename of a program whose exec triggered this catchpoint.
8169 This field is only valid immediately after this catchpoint has
8170 triggered. */
8171 char *exec_pathname;
8172 };
8173
8174 /* Exec catchpoint destructor. */
8175
8176 exec_catchpoint::~exec_catchpoint ()
8177 {
8178 xfree (this->exec_pathname);
8179 }
8180
8181 static int
8182 insert_catch_exec (struct bp_location *bl)
8183 {
8184 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8185 }
8186
8187 static int
8188 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8189 {
8190 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8191 }
8192
8193 static int
8194 breakpoint_hit_catch_exec (const struct bp_location *bl,
8195 const address_space *aspace, CORE_ADDR bp_addr,
8196 const struct target_waitstatus *ws)
8197 {
8198 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8199
8200 if (ws->kind != TARGET_WAITKIND_EXECD)
8201 return 0;
8202
8203 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8204 return 1;
8205 }
8206
8207 static enum print_stop_action
8208 print_it_catch_exec (bpstat bs)
8209 {
8210 struct ui_out *uiout = current_uiout;
8211 struct breakpoint *b = bs->breakpoint_at;
8212 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8213
8214 annotate_catchpoint (b->number);
8215 maybe_print_thread_hit_breakpoint (uiout);
8216 if (b->disposition == disp_del)
8217 uiout->text ("Temporary catchpoint ");
8218 else
8219 uiout->text ("Catchpoint ");
8220 if (uiout->is_mi_like_p ())
8221 {
8222 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8223 uiout->field_string ("disp", bpdisp_text (b->disposition));
8224 }
8225 uiout->field_signed ("bkptno", b->number);
8226 uiout->text (" (exec'd ");
8227 uiout->field_string ("new-exec", c->exec_pathname);
8228 uiout->text ("), ");
8229
8230 return PRINT_SRC_AND_LOC;
8231 }
8232
8233 static void
8234 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8235 {
8236 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8237 struct value_print_options opts;
8238 struct ui_out *uiout = current_uiout;
8239
8240 get_user_print_options (&opts);
8241
8242 /* Field 4, the address, is omitted (which makes the columns
8243 not line up too nicely with the headers, but the effect
8244 is relatively readable). */
8245 if (opts.addressprint)
8246 uiout->field_skip ("addr");
8247 annotate_field (5);
8248 uiout->text ("exec");
8249 if (c->exec_pathname != NULL)
8250 {
8251 uiout->text (", program \"");
8252 uiout->field_string ("what", c->exec_pathname);
8253 uiout->text ("\" ");
8254 }
8255
8256 if (uiout->is_mi_like_p ())
8257 uiout->field_string ("catch-type", "exec");
8258 }
8259
8260 static void
8261 print_mention_catch_exec (struct breakpoint *b)
8262 {
8263 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8264 }
8265
8266 /* Implement the "print_recreate" breakpoint_ops method for exec
8267 catchpoints. */
8268
8269 static void
8270 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8271 {
8272 fprintf_unfiltered (fp, "catch exec");
8273 print_recreate_thread (b, fp);
8274 }
8275
8276 static struct breakpoint_ops catch_exec_breakpoint_ops;
8277
8278 static int
8279 hw_breakpoint_used_count (void)
8280 {
8281 int i = 0;
8282 struct breakpoint *b;
8283 struct bp_location *bl;
8284
8285 ALL_BREAKPOINTS (b)
8286 {
8287 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8288 for (bl = b->loc; bl; bl = bl->next)
8289 {
8290 /* Special types of hardware breakpoints may use more than
8291 one register. */
8292 i += b->ops->resources_needed (bl);
8293 }
8294 }
8295
8296 return i;
8297 }
8298
8299 /* Returns the resources B would use if it were a hardware
8300 watchpoint. */
8301
8302 static int
8303 hw_watchpoint_use_count (struct breakpoint *b)
8304 {
8305 int i = 0;
8306 struct bp_location *bl;
8307
8308 if (!breakpoint_enabled (b))
8309 return 0;
8310
8311 for (bl = b->loc; bl; bl = bl->next)
8312 {
8313 /* Special types of hardware watchpoints may use more than
8314 one register. */
8315 i += b->ops->resources_needed (bl);
8316 }
8317
8318 return i;
8319 }
8320
8321 /* Returns the sum the used resources of all hardware watchpoints of
8322 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8323 the sum of the used resources of all hardware watchpoints of other
8324 types _not_ TYPE. */
8325
8326 static int
8327 hw_watchpoint_used_count_others (struct breakpoint *except,
8328 enum bptype type, int *other_type_used)
8329 {
8330 int i = 0;
8331 struct breakpoint *b;
8332
8333 *other_type_used = 0;
8334 ALL_BREAKPOINTS (b)
8335 {
8336 if (b == except)
8337 continue;
8338 if (!breakpoint_enabled (b))
8339 continue;
8340
8341 if (b->type == type)
8342 i += hw_watchpoint_use_count (b);
8343 else if (is_hardware_watchpoint (b))
8344 *other_type_used = 1;
8345 }
8346
8347 return i;
8348 }
8349
8350 void
8351 disable_watchpoints_before_interactive_call_start (void)
8352 {
8353 struct breakpoint *b;
8354
8355 ALL_BREAKPOINTS (b)
8356 {
8357 if (is_watchpoint (b) && breakpoint_enabled (b))
8358 {
8359 b->enable_state = bp_call_disabled;
8360 update_global_location_list (UGLL_DONT_INSERT);
8361 }
8362 }
8363 }
8364
8365 void
8366 enable_watchpoints_after_interactive_call_stop (void)
8367 {
8368 struct breakpoint *b;
8369
8370 ALL_BREAKPOINTS (b)
8371 {
8372 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8373 {
8374 b->enable_state = bp_enabled;
8375 update_global_location_list (UGLL_MAY_INSERT);
8376 }
8377 }
8378 }
8379
8380 void
8381 disable_breakpoints_before_startup (void)
8382 {
8383 current_program_space->executing_startup = 1;
8384 update_global_location_list (UGLL_DONT_INSERT);
8385 }
8386
8387 void
8388 enable_breakpoints_after_startup (void)
8389 {
8390 current_program_space->executing_startup = 0;
8391 breakpoint_re_set ();
8392 }
8393
8394 /* Create a new single-step breakpoint for thread THREAD, with no
8395 locations. */
8396
8397 static struct breakpoint *
8398 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8399 {
8400 std::unique_ptr<breakpoint> b (new breakpoint ());
8401
8402 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8403 &momentary_breakpoint_ops);
8404
8405 b->disposition = disp_donttouch;
8406 b->frame_id = null_frame_id;
8407
8408 b->thread = thread;
8409 gdb_assert (b->thread != 0);
8410
8411 return add_to_breakpoint_chain (std::move (b));
8412 }
8413
8414 /* Set a momentary breakpoint of type TYPE at address specified by
8415 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8416 frame. */
8417
8418 breakpoint_up
8419 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8420 struct frame_id frame_id, enum bptype type)
8421 {
8422 struct breakpoint *b;
8423
8424 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8425 tail-called one. */
8426 gdb_assert (!frame_id_artificial_p (frame_id));
8427
8428 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8429 b->enable_state = bp_enabled;
8430 b->disposition = disp_donttouch;
8431 b->frame_id = frame_id;
8432
8433 b->thread = inferior_thread ()->global_num;
8434
8435 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8436
8437 return breakpoint_up (b);
8438 }
8439
8440 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8441 The new breakpoint will have type TYPE, use OPS as its
8442 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8443
8444 static struct breakpoint *
8445 momentary_breakpoint_from_master (struct breakpoint *orig,
8446 enum bptype type,
8447 const struct breakpoint_ops *ops,
8448 int loc_enabled)
8449 {
8450 struct breakpoint *copy;
8451
8452 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8453 copy->loc = allocate_bp_location (copy);
8454 set_breakpoint_location_function (copy->loc);
8455
8456 copy->loc->gdbarch = orig->loc->gdbarch;
8457 copy->loc->requested_address = orig->loc->requested_address;
8458 copy->loc->address = orig->loc->address;
8459 copy->loc->section = orig->loc->section;
8460 copy->loc->pspace = orig->loc->pspace;
8461 copy->loc->probe = orig->loc->probe;
8462 copy->loc->line_number = orig->loc->line_number;
8463 copy->loc->symtab = orig->loc->symtab;
8464 copy->loc->enabled = loc_enabled;
8465 copy->frame_id = orig->frame_id;
8466 copy->thread = orig->thread;
8467 copy->pspace = orig->pspace;
8468
8469 copy->enable_state = bp_enabled;
8470 copy->disposition = disp_donttouch;
8471 copy->number = internal_breakpoint_number--;
8472
8473 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8474 return copy;
8475 }
8476
8477 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8478 ORIG is NULL. */
8479
8480 struct breakpoint *
8481 clone_momentary_breakpoint (struct breakpoint *orig)
8482 {
8483 /* If there's nothing to clone, then return nothing. */
8484 if (orig == NULL)
8485 return NULL;
8486
8487 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8488 }
8489
8490 breakpoint_up
8491 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8492 enum bptype type)
8493 {
8494 struct symtab_and_line sal;
8495
8496 sal = find_pc_line (pc, 0);
8497 sal.pc = pc;
8498 sal.section = find_pc_overlay (pc);
8499 sal.explicit_pc = 1;
8500
8501 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8502 }
8503 \f
8504
8505 /* Tell the user we have just set a breakpoint B. */
8506
8507 static void
8508 mention (struct breakpoint *b)
8509 {
8510 b->ops->print_mention (b);
8511 current_uiout->text ("\n");
8512 }
8513 \f
8514
8515 static int bp_loc_is_permanent (struct bp_location *loc);
8516
8517 static struct bp_location *
8518 add_location_to_breakpoint (struct breakpoint *b,
8519 const struct symtab_and_line *sal)
8520 {
8521 struct bp_location *loc, **tmp;
8522 CORE_ADDR adjusted_address;
8523 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8524
8525 if (loc_gdbarch == NULL)
8526 loc_gdbarch = b->gdbarch;
8527
8528 /* Adjust the breakpoint's address prior to allocating a location.
8529 Once we call allocate_bp_location(), that mostly uninitialized
8530 location will be placed on the location chain. Adjustment of the
8531 breakpoint may cause target_read_memory() to be called and we do
8532 not want its scan of the location chain to find a breakpoint and
8533 location that's only been partially initialized. */
8534 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8535 sal->pc, b->type);
8536
8537 /* Sort the locations by their ADDRESS. */
8538 loc = allocate_bp_location (b);
8539 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8540 tmp = &((*tmp)->next))
8541 ;
8542 loc->next = *tmp;
8543 *tmp = loc;
8544
8545 loc->requested_address = sal->pc;
8546 loc->address = adjusted_address;
8547 loc->pspace = sal->pspace;
8548 loc->probe.prob = sal->prob;
8549 loc->probe.objfile = sal->objfile;
8550 gdb_assert (loc->pspace != NULL);
8551 loc->section = sal->section;
8552 loc->gdbarch = loc_gdbarch;
8553 loc->line_number = sal->line;
8554 loc->symtab = sal->symtab;
8555 loc->symbol = sal->symbol;
8556 loc->msymbol = sal->msymbol;
8557 loc->objfile = sal->objfile;
8558
8559 set_breakpoint_location_function (loc);
8560
8561 /* While by definition, permanent breakpoints are already present in the
8562 code, we don't mark the location as inserted. Normally one would expect
8563 that GDB could rely on that breakpoint instruction to stop the program,
8564 thus removing the need to insert its own breakpoint, except that executing
8565 the breakpoint instruction can kill the target instead of reporting a
8566 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8567 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8568 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8569 breakpoint be inserted normally results in QEMU knowing about the GDB
8570 breakpoint, and thus trap before the breakpoint instruction is executed.
8571 (If GDB later needs to continue execution past the permanent breakpoint,
8572 it manually increments the PC, thus avoiding executing the breakpoint
8573 instruction.) */
8574 if (bp_loc_is_permanent (loc))
8575 loc->permanent = 1;
8576
8577 return loc;
8578 }
8579 \f
8580
8581 /* See breakpoint.h. */
8582
8583 int
8584 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8585 {
8586 int len;
8587 CORE_ADDR addr;
8588 const gdb_byte *bpoint;
8589 gdb_byte *target_mem;
8590
8591 addr = address;
8592 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8593
8594 /* Software breakpoints unsupported? */
8595 if (bpoint == NULL)
8596 return 0;
8597
8598 target_mem = (gdb_byte *) alloca (len);
8599
8600 /* Enable the automatic memory restoration from breakpoints while
8601 we read the memory. Otherwise we could say about our temporary
8602 breakpoints they are permanent. */
8603 scoped_restore restore_memory
8604 = make_scoped_restore_show_memory_breakpoints (0);
8605
8606 if (target_read_memory (address, target_mem, len) == 0
8607 && memcmp (target_mem, bpoint, len) == 0)
8608 return 1;
8609
8610 return 0;
8611 }
8612
8613 /* Return 1 if LOC is pointing to a permanent breakpoint,
8614 return 0 otherwise. */
8615
8616 static int
8617 bp_loc_is_permanent (struct bp_location *loc)
8618 {
8619 gdb_assert (loc != NULL);
8620
8621 /* If we have a non-breakpoint-backed catchpoint or a software
8622 watchpoint, just return 0. We should not attempt to read from
8623 the addresses the locations of these breakpoint types point to.
8624 program_breakpoint_here_p, below, will attempt to read
8625 memory. */
8626 if (!bl_address_is_meaningful (loc))
8627 return 0;
8628
8629 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8630 switch_to_program_space_and_thread (loc->pspace);
8631 return program_breakpoint_here_p (loc->gdbarch, loc->address);
8632 }
8633
8634 /* Build a command list for the dprintf corresponding to the current
8635 settings of the dprintf style options. */
8636
8637 static void
8638 update_dprintf_command_list (struct breakpoint *b)
8639 {
8640 char *dprintf_args = b->extra_string;
8641 char *printf_line = NULL;
8642
8643 if (!dprintf_args)
8644 return;
8645
8646 dprintf_args = skip_spaces (dprintf_args);
8647
8648 /* Allow a comma, as it may have terminated a location, but don't
8649 insist on it. */
8650 if (*dprintf_args == ',')
8651 ++dprintf_args;
8652 dprintf_args = skip_spaces (dprintf_args);
8653
8654 if (*dprintf_args != '"')
8655 error (_("Bad format string, missing '\"'."));
8656
8657 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8658 printf_line = xstrprintf ("printf %s", dprintf_args);
8659 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8660 {
8661 if (!dprintf_function)
8662 error (_("No function supplied for dprintf call"));
8663
8664 if (dprintf_channel && strlen (dprintf_channel) > 0)
8665 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8666 dprintf_function,
8667 dprintf_channel,
8668 dprintf_args);
8669 else
8670 printf_line = xstrprintf ("call (void) %s (%s)",
8671 dprintf_function,
8672 dprintf_args);
8673 }
8674 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8675 {
8676 if (target_can_run_breakpoint_commands ())
8677 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8678 else
8679 {
8680 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8681 printf_line = xstrprintf ("printf %s", dprintf_args);
8682 }
8683 }
8684 else
8685 internal_error (__FILE__, __LINE__,
8686 _("Invalid dprintf style."));
8687
8688 gdb_assert (printf_line != NULL);
8689
8690 /* Manufacture a printf sequence. */
8691 struct command_line *printf_cmd_line
8692 = new struct command_line (simple_control, printf_line);
8693 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8694 command_lines_deleter ()));
8695 }
8696
8697 /* Update all dprintf commands, making their command lists reflect
8698 current style settings. */
8699
8700 static void
8701 update_dprintf_commands (const char *args, int from_tty,
8702 struct cmd_list_element *c)
8703 {
8704 struct breakpoint *b;
8705
8706 ALL_BREAKPOINTS (b)
8707 {
8708 if (b->type == bp_dprintf)
8709 update_dprintf_command_list (b);
8710 }
8711 }
8712
8713 /* Create a breakpoint with SAL as location. Use LOCATION
8714 as a description of the location, and COND_STRING
8715 as condition expression. If LOCATION is NULL then create an
8716 "address location" from the address in the SAL. */
8717
8718 static void
8719 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8720 gdb::array_view<const symtab_and_line> sals,
8721 event_location_up &&location,
8722 gdb::unique_xmalloc_ptr<char> filter,
8723 gdb::unique_xmalloc_ptr<char> cond_string,
8724 gdb::unique_xmalloc_ptr<char> extra_string,
8725 enum bptype type, enum bpdisp disposition,
8726 int thread, int task, int ignore_count,
8727 const struct breakpoint_ops *ops, int from_tty,
8728 int enabled, int internal, unsigned flags,
8729 int display_canonical)
8730 {
8731 int i;
8732
8733 if (type == bp_hardware_breakpoint)
8734 {
8735 int target_resources_ok;
8736
8737 i = hw_breakpoint_used_count ();
8738 target_resources_ok =
8739 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8740 i + 1, 0);
8741 if (target_resources_ok == 0)
8742 error (_("No hardware breakpoint support in the target."));
8743 else if (target_resources_ok < 0)
8744 error (_("Hardware breakpoints used exceeds limit."));
8745 }
8746
8747 gdb_assert (!sals.empty ());
8748
8749 for (const auto &sal : sals)
8750 {
8751 struct bp_location *loc;
8752
8753 if (from_tty)
8754 {
8755 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8756 if (!loc_gdbarch)
8757 loc_gdbarch = gdbarch;
8758
8759 describe_other_breakpoints (loc_gdbarch,
8760 sal.pspace, sal.pc, sal.section, thread);
8761 }
8762
8763 if (&sal == &sals[0])
8764 {
8765 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8766 b->thread = thread;
8767 b->task = task;
8768
8769 b->cond_string = cond_string.release ();
8770 b->extra_string = extra_string.release ();
8771 b->ignore_count = ignore_count;
8772 b->enable_state = enabled ? bp_enabled : bp_disabled;
8773 b->disposition = disposition;
8774
8775 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8776 b->loc->inserted = 1;
8777
8778 if (type == bp_static_tracepoint)
8779 {
8780 struct tracepoint *t = (struct tracepoint *) b;
8781 struct static_tracepoint_marker marker;
8782
8783 if (strace_marker_p (b))
8784 {
8785 /* We already know the marker exists, otherwise, we
8786 wouldn't see a sal for it. */
8787 const char *p
8788 = &event_location_to_string (b->location.get ())[3];
8789 const char *endp;
8790
8791 p = skip_spaces (p);
8792
8793 endp = skip_to_space (p);
8794
8795 t->static_trace_marker_id.assign (p, endp - p);
8796
8797 printf_filtered (_("Probed static tracepoint "
8798 "marker \"%s\"\n"),
8799 t->static_trace_marker_id.c_str ());
8800 }
8801 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8802 {
8803 t->static_trace_marker_id = std::move (marker.str_id);
8804
8805 printf_filtered (_("Probed static tracepoint "
8806 "marker \"%s\"\n"),
8807 t->static_trace_marker_id.c_str ());
8808 }
8809 else
8810 warning (_("Couldn't determine the static "
8811 "tracepoint marker to probe"));
8812 }
8813
8814 loc = b->loc;
8815 }
8816 else
8817 {
8818 loc = add_location_to_breakpoint (b, &sal);
8819 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8820 loc->inserted = 1;
8821 }
8822
8823 if (b->cond_string)
8824 {
8825 const char *arg = b->cond_string;
8826
8827 loc->cond = parse_exp_1 (&arg, loc->address,
8828 block_for_pc (loc->address), 0);
8829 if (*arg)
8830 error (_("Garbage '%s' follows condition"), arg);
8831 }
8832
8833 /* Dynamic printf requires and uses additional arguments on the
8834 command line, otherwise it's an error. */
8835 if (type == bp_dprintf)
8836 {
8837 if (b->extra_string)
8838 update_dprintf_command_list (b);
8839 else
8840 error (_("Format string required"));
8841 }
8842 else if (b->extra_string)
8843 error (_("Garbage '%s' at end of command"), b->extra_string);
8844 }
8845
8846 b->display_canonical = display_canonical;
8847 if (location != NULL)
8848 b->location = std::move (location);
8849 else
8850 b->location = new_address_location (b->loc->address, NULL, 0);
8851 b->filter = std::move (filter);
8852 }
8853
8854 static void
8855 create_breakpoint_sal (struct gdbarch *gdbarch,
8856 gdb::array_view<const symtab_and_line> sals,
8857 event_location_up &&location,
8858 gdb::unique_xmalloc_ptr<char> filter,
8859 gdb::unique_xmalloc_ptr<char> cond_string,
8860 gdb::unique_xmalloc_ptr<char> extra_string,
8861 enum bptype type, enum bpdisp disposition,
8862 int thread, int task, int ignore_count,
8863 const struct breakpoint_ops *ops, int from_tty,
8864 int enabled, int internal, unsigned flags,
8865 int display_canonical)
8866 {
8867 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8868
8869 init_breakpoint_sal (b.get (), gdbarch,
8870 sals, std::move (location),
8871 std::move (filter),
8872 std::move (cond_string),
8873 std::move (extra_string),
8874 type, disposition,
8875 thread, task, ignore_count,
8876 ops, from_tty,
8877 enabled, internal, flags,
8878 display_canonical);
8879
8880 install_breakpoint (internal, std::move (b), 0);
8881 }
8882
8883 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8884 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8885 value. COND_STRING, if not NULL, specified the condition to be
8886 used for all breakpoints. Essentially the only case where
8887 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8888 function. In that case, it's still not possible to specify
8889 separate conditions for different overloaded functions, so
8890 we take just a single condition string.
8891
8892 NOTE: If the function succeeds, the caller is expected to cleanup
8893 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8894 array contents). If the function fails (error() is called), the
8895 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8896 COND and SALS arrays and each of those arrays contents. */
8897
8898 static void
8899 create_breakpoints_sal (struct gdbarch *gdbarch,
8900 struct linespec_result *canonical,
8901 gdb::unique_xmalloc_ptr<char> cond_string,
8902 gdb::unique_xmalloc_ptr<char> extra_string,
8903 enum bptype type, enum bpdisp disposition,
8904 int thread, int task, int ignore_count,
8905 const struct breakpoint_ops *ops, int from_tty,
8906 int enabled, int internal, unsigned flags)
8907 {
8908 if (canonical->pre_expanded)
8909 gdb_assert (canonical->lsals.size () == 1);
8910
8911 for (const auto &lsal : canonical->lsals)
8912 {
8913 /* Note that 'location' can be NULL in the case of a plain
8914 'break', without arguments. */
8915 event_location_up location
8916 = (canonical->location != NULL
8917 ? copy_event_location (canonical->location.get ()) : NULL);
8918 gdb::unique_xmalloc_ptr<char> filter_string
8919 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8920
8921 create_breakpoint_sal (gdbarch, lsal.sals,
8922 std::move (location),
8923 std::move (filter_string),
8924 std::move (cond_string),
8925 std::move (extra_string),
8926 type, disposition,
8927 thread, task, ignore_count, ops,
8928 from_tty, enabled, internal, flags,
8929 canonical->special_display);
8930 }
8931 }
8932
8933 /* Parse LOCATION which is assumed to be a SAL specification possibly
8934 followed by conditionals. On return, SALS contains an array of SAL
8935 addresses found. LOCATION points to the end of the SAL (for
8936 linespec locations).
8937
8938 The array and the line spec strings are allocated on the heap, it is
8939 the caller's responsibility to free them. */
8940
8941 static void
8942 parse_breakpoint_sals (const struct event_location *location,
8943 struct linespec_result *canonical)
8944 {
8945 struct symtab_and_line cursal;
8946
8947 if (event_location_type (location) == LINESPEC_LOCATION)
8948 {
8949 const char *spec = get_linespec_location (location)->spec_string;
8950
8951 if (spec == NULL)
8952 {
8953 /* The last displayed codepoint, if it's valid, is our default
8954 breakpoint address. */
8955 if (last_displayed_sal_is_valid ())
8956 {
8957 /* Set sal's pspace, pc, symtab, and line to the values
8958 corresponding to the last call to print_frame_info.
8959 Be sure to reinitialize LINE with NOTCURRENT == 0
8960 as the breakpoint line number is inappropriate otherwise.
8961 find_pc_line would adjust PC, re-set it back. */
8962 symtab_and_line sal = get_last_displayed_sal ();
8963 CORE_ADDR pc = sal.pc;
8964
8965 sal = find_pc_line (pc, 0);
8966
8967 /* "break" without arguments is equivalent to "break *PC"
8968 where PC is the last displayed codepoint's address. So
8969 make sure to set sal.explicit_pc to prevent GDB from
8970 trying to expand the list of sals to include all other
8971 instances with the same symtab and line. */
8972 sal.pc = pc;
8973 sal.explicit_pc = 1;
8974
8975 struct linespec_sals lsal;
8976 lsal.sals = {sal};
8977 lsal.canonical = NULL;
8978
8979 canonical->lsals.push_back (std::move (lsal));
8980 return;
8981 }
8982 else
8983 error (_("No default breakpoint address now."));
8984 }
8985 }
8986
8987 /* Force almost all breakpoints to be in terms of the
8988 current_source_symtab (which is decode_line_1's default).
8989 This should produce the results we want almost all of the
8990 time while leaving default_breakpoint_* alone.
8991
8992 ObjC: However, don't match an Objective-C method name which
8993 may have a '+' or '-' succeeded by a '['. */
8994 cursal = get_current_source_symtab_and_line ();
8995 if (last_displayed_sal_is_valid ())
8996 {
8997 const char *spec = NULL;
8998
8999 if (event_location_type (location) == LINESPEC_LOCATION)
9000 spec = get_linespec_location (location)->spec_string;
9001
9002 if (!cursal.symtab
9003 || (spec != NULL
9004 && strchr ("+-", spec[0]) != NULL
9005 && spec[1] != '['))
9006 {
9007 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9008 get_last_displayed_symtab (),
9009 get_last_displayed_line (),
9010 canonical, NULL, NULL);
9011 return;
9012 }
9013 }
9014
9015 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9016 cursal.symtab, cursal.line, canonical, NULL, NULL);
9017 }
9018
9019
9020 /* Convert each SAL into a real PC. Verify that the PC can be
9021 inserted as a breakpoint. If it can't throw an error. */
9022
9023 static void
9024 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9025 {
9026 for (auto &sal : sals)
9027 resolve_sal_pc (&sal);
9028 }
9029
9030 /* Fast tracepoints may have restrictions on valid locations. For
9031 instance, a fast tracepoint using a jump instead of a trap will
9032 likely have to overwrite more bytes than a trap would, and so can
9033 only be placed where the instruction is longer than the jump, or a
9034 multi-instruction sequence does not have a jump into the middle of
9035 it, etc. */
9036
9037 static void
9038 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9039 gdb::array_view<const symtab_and_line> sals)
9040 {
9041 for (const auto &sal : sals)
9042 {
9043 struct gdbarch *sarch;
9044
9045 sarch = get_sal_arch (sal);
9046 /* We fall back to GDBARCH if there is no architecture
9047 associated with SAL. */
9048 if (sarch == NULL)
9049 sarch = gdbarch;
9050 std::string msg;
9051 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9052 error (_("May not have a fast tracepoint at %s%s"),
9053 paddress (sarch, sal.pc), msg.c_str ());
9054 }
9055 }
9056
9057 /* Given TOK, a string specification of condition and thread, as
9058 accepted by the 'break' command, extract the condition
9059 string and thread number and set *COND_STRING and *THREAD.
9060 PC identifies the context at which the condition should be parsed.
9061 If no condition is found, *COND_STRING is set to NULL.
9062 If no thread is found, *THREAD is set to -1. */
9063
9064 static void
9065 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9066 char **cond_string, int *thread, int *task,
9067 char **rest)
9068 {
9069 *cond_string = NULL;
9070 *thread = -1;
9071 *task = 0;
9072 *rest = NULL;
9073
9074 while (tok && *tok)
9075 {
9076 const char *end_tok;
9077 int toklen;
9078 const char *cond_start = NULL;
9079 const char *cond_end = NULL;
9080
9081 tok = skip_spaces (tok);
9082
9083 if ((*tok == '"' || *tok == ',') && rest)
9084 {
9085 *rest = savestring (tok, strlen (tok));
9086 return;
9087 }
9088
9089 end_tok = skip_to_space (tok);
9090
9091 toklen = end_tok - tok;
9092
9093 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9094 {
9095 tok = cond_start = end_tok + 1;
9096 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9097 cond_end = tok;
9098 *cond_string = savestring (cond_start, cond_end - cond_start);
9099 }
9100 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9101 {
9102 const char *tmptok;
9103 struct thread_info *thr;
9104
9105 tok = end_tok + 1;
9106 thr = parse_thread_id (tok, &tmptok);
9107 if (tok == tmptok)
9108 error (_("Junk after thread keyword."));
9109 *thread = thr->global_num;
9110 tok = tmptok;
9111 }
9112 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9113 {
9114 char *tmptok;
9115
9116 tok = end_tok + 1;
9117 *task = strtol (tok, &tmptok, 0);
9118 if (tok == tmptok)
9119 error (_("Junk after task keyword."));
9120 if (!valid_task_id (*task))
9121 error (_("Unknown task %d."), *task);
9122 tok = tmptok;
9123 }
9124 else if (rest)
9125 {
9126 *rest = savestring (tok, strlen (tok));
9127 return;
9128 }
9129 else
9130 error (_("Junk at end of arguments."));
9131 }
9132 }
9133
9134 /* Decode a static tracepoint marker spec. */
9135
9136 static std::vector<symtab_and_line>
9137 decode_static_tracepoint_spec (const char **arg_p)
9138 {
9139 const char *p = &(*arg_p)[3];
9140 const char *endp;
9141
9142 p = skip_spaces (p);
9143
9144 endp = skip_to_space (p);
9145
9146 std::string marker_str (p, endp - p);
9147
9148 std::vector<static_tracepoint_marker> markers
9149 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9150 if (markers.empty ())
9151 error (_("No known static tracepoint marker named %s"),
9152 marker_str.c_str ());
9153
9154 std::vector<symtab_and_line> sals;
9155 sals.reserve (markers.size ());
9156
9157 for (const static_tracepoint_marker &marker : markers)
9158 {
9159 symtab_and_line sal = find_pc_line (marker.address, 0);
9160 sal.pc = marker.address;
9161 sals.push_back (sal);
9162 }
9163
9164 *arg_p = endp;
9165 return sals;
9166 }
9167
9168 /* See breakpoint.h. */
9169
9170 int
9171 create_breakpoint (struct gdbarch *gdbarch,
9172 const struct event_location *location,
9173 const char *cond_string,
9174 int thread, const char *extra_string,
9175 int parse_extra,
9176 int tempflag, enum bptype type_wanted,
9177 int ignore_count,
9178 enum auto_boolean pending_break_support,
9179 const struct breakpoint_ops *ops,
9180 int from_tty, int enabled, int internal,
9181 unsigned flags)
9182 {
9183 struct linespec_result canonical;
9184 int pending = 0;
9185 int task = 0;
9186 int prev_bkpt_count = breakpoint_count;
9187
9188 gdb_assert (ops != NULL);
9189
9190 /* If extra_string isn't useful, set it to NULL. */
9191 if (extra_string != NULL && *extra_string == '\0')
9192 extra_string = NULL;
9193
9194 try
9195 {
9196 ops->create_sals_from_location (location, &canonical, type_wanted);
9197 }
9198 catch (const gdb_exception_error &e)
9199 {
9200 /* If caller is interested in rc value from parse, set
9201 value. */
9202 if (e.error == NOT_FOUND_ERROR)
9203 {
9204 /* If pending breakpoint support is turned off, throw
9205 error. */
9206
9207 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9208 throw;
9209
9210 exception_print (gdb_stderr, e);
9211
9212 /* If pending breakpoint support is auto query and the user
9213 selects no, then simply return the error code. */
9214 if (pending_break_support == AUTO_BOOLEAN_AUTO
9215 && !nquery (_("Make %s pending on future shared library load? "),
9216 bptype_string (type_wanted)))
9217 return 0;
9218
9219 /* At this point, either the user was queried about setting
9220 a pending breakpoint and selected yes, or pending
9221 breakpoint behavior is on and thus a pending breakpoint
9222 is defaulted on behalf of the user. */
9223 pending = 1;
9224 }
9225 else
9226 throw;
9227 }
9228
9229 if (!pending && canonical.lsals.empty ())
9230 return 0;
9231
9232 /* Resolve all line numbers to PC's and verify that the addresses
9233 are ok for the target. */
9234 if (!pending)
9235 {
9236 for (auto &lsal : canonical.lsals)
9237 breakpoint_sals_to_pc (lsal.sals);
9238 }
9239
9240 /* Fast tracepoints may have additional restrictions on location. */
9241 if (!pending && type_wanted == bp_fast_tracepoint)
9242 {
9243 for (const auto &lsal : canonical.lsals)
9244 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9245 }
9246
9247 /* Verify that condition can be parsed, before setting any
9248 breakpoints. Allocate a separate condition expression for each
9249 breakpoint. */
9250 if (!pending)
9251 {
9252 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9253 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9254
9255 if (parse_extra)
9256 {
9257 char *rest;
9258 char *cond;
9259
9260 const linespec_sals &lsal = canonical.lsals[0];
9261
9262 /* Here we only parse 'arg' to separate condition
9263 from thread number, so parsing in context of first
9264 sal is OK. When setting the breakpoint we'll
9265 re-parse it in context of each sal. */
9266
9267 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9268 &cond, &thread, &task, &rest);
9269 cond_string_copy.reset (cond);
9270 extra_string_copy.reset (rest);
9271 }
9272 else
9273 {
9274 if (type_wanted != bp_dprintf
9275 && extra_string != NULL && *extra_string != '\0')
9276 error (_("Garbage '%s' at end of location"), extra_string);
9277
9278 /* Create a private copy of condition string. */
9279 if (cond_string)
9280 cond_string_copy.reset (xstrdup (cond_string));
9281 /* Create a private copy of any extra string. */
9282 if (extra_string)
9283 extra_string_copy.reset (xstrdup (extra_string));
9284 }
9285
9286 ops->create_breakpoints_sal (gdbarch, &canonical,
9287 std::move (cond_string_copy),
9288 std::move (extra_string_copy),
9289 type_wanted,
9290 tempflag ? disp_del : disp_donttouch,
9291 thread, task, ignore_count, ops,
9292 from_tty, enabled, internal, flags);
9293 }
9294 else
9295 {
9296 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9297
9298 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9299 b->location = copy_event_location (location);
9300
9301 if (parse_extra)
9302 b->cond_string = NULL;
9303 else
9304 {
9305 /* Create a private copy of condition string. */
9306 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9307 b->thread = thread;
9308 }
9309
9310 /* Create a private copy of any extra string. */
9311 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9312 b->ignore_count = ignore_count;
9313 b->disposition = tempflag ? disp_del : disp_donttouch;
9314 b->condition_not_parsed = 1;
9315 b->enable_state = enabled ? bp_enabled : bp_disabled;
9316 if ((type_wanted != bp_breakpoint
9317 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9318 b->pspace = current_program_space;
9319
9320 install_breakpoint (internal, std::move (b), 0);
9321 }
9322
9323 if (canonical.lsals.size () > 1)
9324 {
9325 warning (_("Multiple breakpoints were set.\nUse the "
9326 "\"delete\" command to delete unwanted breakpoints."));
9327 prev_breakpoint_count = prev_bkpt_count;
9328 }
9329
9330 update_global_location_list (UGLL_MAY_INSERT);
9331
9332 return 1;
9333 }
9334
9335 /* Set a breakpoint.
9336 ARG is a string describing breakpoint address,
9337 condition, and thread.
9338 FLAG specifies if a breakpoint is hardware on,
9339 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9340 and BP_TEMPFLAG. */
9341
9342 static void
9343 break_command_1 (const char *arg, int flag, int from_tty)
9344 {
9345 int tempflag = flag & BP_TEMPFLAG;
9346 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9347 ? bp_hardware_breakpoint
9348 : bp_breakpoint);
9349 struct breakpoint_ops *ops;
9350
9351 event_location_up location = string_to_event_location (&arg, current_language);
9352
9353 /* Matching breakpoints on probes. */
9354 if (location != NULL
9355 && event_location_type (location.get ()) == PROBE_LOCATION)
9356 ops = &bkpt_probe_breakpoint_ops;
9357 else
9358 ops = &bkpt_breakpoint_ops;
9359
9360 create_breakpoint (get_current_arch (),
9361 location.get (),
9362 NULL, 0, arg, 1 /* parse arg */,
9363 tempflag, type_wanted,
9364 0 /* Ignore count */,
9365 pending_break_support,
9366 ops,
9367 from_tty,
9368 1 /* enabled */,
9369 0 /* internal */,
9370 0);
9371 }
9372
9373 /* Helper function for break_command_1 and disassemble_command. */
9374
9375 void
9376 resolve_sal_pc (struct symtab_and_line *sal)
9377 {
9378 CORE_ADDR pc;
9379
9380 if (sal->pc == 0 && sal->symtab != NULL)
9381 {
9382 if (!find_line_pc (sal->symtab, sal->line, &pc))
9383 error (_("No line %d in file \"%s\"."),
9384 sal->line, symtab_to_filename_for_display (sal->symtab));
9385 sal->pc = pc;
9386
9387 /* If this SAL corresponds to a breakpoint inserted using a line
9388 number, then skip the function prologue if necessary. */
9389 if (sal->explicit_line)
9390 skip_prologue_sal (sal);
9391 }
9392
9393 if (sal->section == 0 && sal->symtab != NULL)
9394 {
9395 const struct blockvector *bv;
9396 const struct block *b;
9397 struct symbol *sym;
9398
9399 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9400 SYMTAB_COMPUNIT (sal->symtab));
9401 if (bv != NULL)
9402 {
9403 sym = block_linkage_function (b);
9404 if (sym != NULL)
9405 {
9406 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9407 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9408 sym);
9409 }
9410 else
9411 {
9412 /* It really is worthwhile to have the section, so we'll
9413 just have to look harder. This case can be executed
9414 if we have line numbers but no functions (as can
9415 happen in assembly source). */
9416
9417 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9418 switch_to_program_space_and_thread (sal->pspace);
9419
9420 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9421 if (msym.minsym)
9422 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9423 }
9424 }
9425 }
9426 }
9427
9428 void
9429 break_command (const char *arg, int from_tty)
9430 {
9431 break_command_1 (arg, 0, from_tty);
9432 }
9433
9434 void
9435 tbreak_command (const char *arg, int from_tty)
9436 {
9437 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9438 }
9439
9440 static void
9441 hbreak_command (const char *arg, int from_tty)
9442 {
9443 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9444 }
9445
9446 static void
9447 thbreak_command (const char *arg, int from_tty)
9448 {
9449 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9450 }
9451
9452 static void
9453 stop_command (const char *arg, int from_tty)
9454 {
9455 printf_filtered (_("Specify the type of breakpoint to set.\n\
9456 Usage: stop in <function | address>\n\
9457 stop at <line>\n"));
9458 }
9459
9460 static void
9461 stopin_command (const char *arg, int from_tty)
9462 {
9463 int badInput = 0;
9464
9465 if (arg == NULL)
9466 badInput = 1;
9467 else if (*arg != '*')
9468 {
9469 const char *argptr = arg;
9470 int hasColon = 0;
9471
9472 /* Look for a ':'. If this is a line number specification, then
9473 say it is bad, otherwise, it should be an address or
9474 function/method name. */
9475 while (*argptr && !hasColon)
9476 {
9477 hasColon = (*argptr == ':');
9478 argptr++;
9479 }
9480
9481 if (hasColon)
9482 badInput = (*argptr != ':'); /* Not a class::method */
9483 else
9484 badInput = isdigit (*arg); /* a simple line number */
9485 }
9486
9487 if (badInput)
9488 printf_filtered (_("Usage: stop in <function | address>\n"));
9489 else
9490 break_command_1 (arg, 0, from_tty);
9491 }
9492
9493 static void
9494 stopat_command (const char *arg, int from_tty)
9495 {
9496 int badInput = 0;
9497
9498 if (arg == NULL || *arg == '*') /* no line number */
9499 badInput = 1;
9500 else
9501 {
9502 const char *argptr = arg;
9503 int hasColon = 0;
9504
9505 /* Look for a ':'. If there is a '::' then get out, otherwise
9506 it is probably a line number. */
9507 while (*argptr && !hasColon)
9508 {
9509 hasColon = (*argptr == ':');
9510 argptr++;
9511 }
9512
9513 if (hasColon)
9514 badInput = (*argptr == ':'); /* we have class::method */
9515 else
9516 badInput = !isdigit (*arg); /* not a line number */
9517 }
9518
9519 if (badInput)
9520 printf_filtered (_("Usage: stop at LINE\n"));
9521 else
9522 break_command_1 (arg, 0, from_tty);
9523 }
9524
9525 /* The dynamic printf command is mostly like a regular breakpoint, but
9526 with a prewired command list consisting of a single output command,
9527 built from extra arguments supplied on the dprintf command
9528 line. */
9529
9530 static void
9531 dprintf_command (const char *arg, int from_tty)
9532 {
9533 event_location_up location = string_to_event_location (&arg, current_language);
9534
9535 /* If non-NULL, ARG should have been advanced past the location;
9536 the next character must be ','. */
9537 if (arg != NULL)
9538 {
9539 if (arg[0] != ',' || arg[1] == '\0')
9540 error (_("Format string required"));
9541 else
9542 {
9543 /* Skip the comma. */
9544 ++arg;
9545 }
9546 }
9547
9548 create_breakpoint (get_current_arch (),
9549 location.get (),
9550 NULL, 0, arg, 1 /* parse arg */,
9551 0, bp_dprintf,
9552 0 /* Ignore count */,
9553 pending_break_support,
9554 &dprintf_breakpoint_ops,
9555 from_tty,
9556 1 /* enabled */,
9557 0 /* internal */,
9558 0);
9559 }
9560
9561 static void
9562 agent_printf_command (const char *arg, int from_tty)
9563 {
9564 error (_("May only run agent-printf on the target"));
9565 }
9566
9567 /* Implement the "breakpoint_hit" breakpoint_ops method for
9568 ranged breakpoints. */
9569
9570 static int
9571 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9572 const address_space *aspace,
9573 CORE_ADDR bp_addr,
9574 const struct target_waitstatus *ws)
9575 {
9576 if (ws->kind != TARGET_WAITKIND_STOPPED
9577 || ws->value.sig != GDB_SIGNAL_TRAP)
9578 return 0;
9579
9580 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9581 bl->length, aspace, bp_addr);
9582 }
9583
9584 /* Implement the "resources_needed" breakpoint_ops method for
9585 ranged breakpoints. */
9586
9587 static int
9588 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9589 {
9590 return target_ranged_break_num_registers ();
9591 }
9592
9593 /* Implement the "print_it" breakpoint_ops method for
9594 ranged breakpoints. */
9595
9596 static enum print_stop_action
9597 print_it_ranged_breakpoint (bpstat bs)
9598 {
9599 struct breakpoint *b = bs->breakpoint_at;
9600 struct bp_location *bl = b->loc;
9601 struct ui_out *uiout = current_uiout;
9602
9603 gdb_assert (b->type == bp_hardware_breakpoint);
9604
9605 /* Ranged breakpoints have only one location. */
9606 gdb_assert (bl && bl->next == NULL);
9607
9608 annotate_breakpoint (b->number);
9609
9610 maybe_print_thread_hit_breakpoint (uiout);
9611
9612 if (b->disposition == disp_del)
9613 uiout->text ("Temporary ranged breakpoint ");
9614 else
9615 uiout->text ("Ranged breakpoint ");
9616 if (uiout->is_mi_like_p ())
9617 {
9618 uiout->field_string ("reason",
9619 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9620 uiout->field_string ("disp", bpdisp_text (b->disposition));
9621 }
9622 uiout->field_signed ("bkptno", b->number);
9623 uiout->text (", ");
9624
9625 return PRINT_SRC_AND_LOC;
9626 }
9627
9628 /* Implement the "print_one" breakpoint_ops method for
9629 ranged breakpoints. */
9630
9631 static void
9632 print_one_ranged_breakpoint (struct breakpoint *b,
9633 struct bp_location **last_loc)
9634 {
9635 struct bp_location *bl = b->loc;
9636 struct value_print_options opts;
9637 struct ui_out *uiout = current_uiout;
9638
9639 /* Ranged breakpoints have only one location. */
9640 gdb_assert (bl && bl->next == NULL);
9641
9642 get_user_print_options (&opts);
9643
9644 if (opts.addressprint)
9645 /* We don't print the address range here, it will be printed later
9646 by print_one_detail_ranged_breakpoint. */
9647 uiout->field_skip ("addr");
9648 annotate_field (5);
9649 print_breakpoint_location (b, bl);
9650 *last_loc = bl;
9651 }
9652
9653 /* Implement the "print_one_detail" breakpoint_ops method for
9654 ranged breakpoints. */
9655
9656 static void
9657 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9658 struct ui_out *uiout)
9659 {
9660 CORE_ADDR address_start, address_end;
9661 struct bp_location *bl = b->loc;
9662 string_file stb;
9663
9664 gdb_assert (bl);
9665
9666 address_start = bl->address;
9667 address_end = address_start + bl->length - 1;
9668
9669 uiout->text ("\taddress range: ");
9670 stb.printf ("[%s, %s]",
9671 print_core_address (bl->gdbarch, address_start),
9672 print_core_address (bl->gdbarch, address_end));
9673 uiout->field_stream ("addr", stb);
9674 uiout->text ("\n");
9675 }
9676
9677 /* Implement the "print_mention" breakpoint_ops method for
9678 ranged breakpoints. */
9679
9680 static void
9681 print_mention_ranged_breakpoint (struct breakpoint *b)
9682 {
9683 struct bp_location *bl = b->loc;
9684 struct ui_out *uiout = current_uiout;
9685
9686 gdb_assert (bl);
9687 gdb_assert (b->type == bp_hardware_breakpoint);
9688
9689 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9690 b->number, paddress (bl->gdbarch, bl->address),
9691 paddress (bl->gdbarch, bl->address + bl->length - 1));
9692 }
9693
9694 /* Implement the "print_recreate" breakpoint_ops method for
9695 ranged breakpoints. */
9696
9697 static void
9698 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9699 {
9700 fprintf_unfiltered (fp, "break-range %s, %s",
9701 event_location_to_string (b->location.get ()),
9702 event_location_to_string (b->location_range_end.get ()));
9703 print_recreate_thread (b, fp);
9704 }
9705
9706 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9707
9708 static struct breakpoint_ops ranged_breakpoint_ops;
9709
9710 /* Find the address where the end of the breakpoint range should be
9711 placed, given the SAL of the end of the range. This is so that if
9712 the user provides a line number, the end of the range is set to the
9713 last instruction of the given line. */
9714
9715 static CORE_ADDR
9716 find_breakpoint_range_end (struct symtab_and_line sal)
9717 {
9718 CORE_ADDR end;
9719
9720 /* If the user provided a PC value, use it. Otherwise,
9721 find the address of the end of the given location. */
9722 if (sal.explicit_pc)
9723 end = sal.pc;
9724 else
9725 {
9726 int ret;
9727 CORE_ADDR start;
9728
9729 ret = find_line_pc_range (sal, &start, &end);
9730 if (!ret)
9731 error (_("Could not find location of the end of the range."));
9732
9733 /* find_line_pc_range returns the start of the next line. */
9734 end--;
9735 }
9736
9737 return end;
9738 }
9739
9740 /* Implement the "break-range" CLI command. */
9741
9742 static void
9743 break_range_command (const char *arg, int from_tty)
9744 {
9745 const char *arg_start;
9746 struct linespec_result canonical_start, canonical_end;
9747 int bp_count, can_use_bp, length;
9748 CORE_ADDR end;
9749 struct breakpoint *b;
9750
9751 /* We don't support software ranged breakpoints. */
9752 if (target_ranged_break_num_registers () < 0)
9753 error (_("This target does not support hardware ranged breakpoints."));
9754
9755 bp_count = hw_breakpoint_used_count ();
9756 bp_count += target_ranged_break_num_registers ();
9757 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9758 bp_count, 0);
9759 if (can_use_bp < 0)
9760 error (_("Hardware breakpoints used exceeds limit."));
9761
9762 arg = skip_spaces (arg);
9763 if (arg == NULL || arg[0] == '\0')
9764 error(_("No address range specified."));
9765
9766 arg_start = arg;
9767 event_location_up start_location = string_to_event_location (&arg,
9768 current_language);
9769 parse_breakpoint_sals (start_location.get (), &canonical_start);
9770
9771 if (arg[0] != ',')
9772 error (_("Too few arguments."));
9773 else if (canonical_start.lsals.empty ())
9774 error (_("Could not find location of the beginning of the range."));
9775
9776 const linespec_sals &lsal_start = canonical_start.lsals[0];
9777
9778 if (canonical_start.lsals.size () > 1
9779 || lsal_start.sals.size () != 1)
9780 error (_("Cannot create a ranged breakpoint with multiple locations."));
9781
9782 const symtab_and_line &sal_start = lsal_start.sals[0];
9783 std::string addr_string_start (arg_start, arg - arg_start);
9784
9785 arg++; /* Skip the comma. */
9786 arg = skip_spaces (arg);
9787
9788 /* Parse the end location. */
9789
9790 arg_start = arg;
9791
9792 /* We call decode_line_full directly here instead of using
9793 parse_breakpoint_sals because we need to specify the start location's
9794 symtab and line as the default symtab and line for the end of the
9795 range. This makes it possible to have ranges like "foo.c:27, +14",
9796 where +14 means 14 lines from the start location. */
9797 event_location_up end_location = string_to_event_location (&arg,
9798 current_language);
9799 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9800 sal_start.symtab, sal_start.line,
9801 &canonical_end, NULL, NULL);
9802
9803 if (canonical_end.lsals.empty ())
9804 error (_("Could not find location of the end of the range."));
9805
9806 const linespec_sals &lsal_end = canonical_end.lsals[0];
9807 if (canonical_end.lsals.size () > 1
9808 || lsal_end.sals.size () != 1)
9809 error (_("Cannot create a ranged breakpoint with multiple locations."));
9810
9811 const symtab_and_line &sal_end = lsal_end.sals[0];
9812
9813 end = find_breakpoint_range_end (sal_end);
9814 if (sal_start.pc > end)
9815 error (_("Invalid address range, end precedes start."));
9816
9817 length = end - sal_start.pc + 1;
9818 if (length < 0)
9819 /* Length overflowed. */
9820 error (_("Address range too large."));
9821 else if (length == 1)
9822 {
9823 /* This range is simple enough to be handled by
9824 the `hbreak' command. */
9825 hbreak_command (&addr_string_start[0], 1);
9826
9827 return;
9828 }
9829
9830 /* Now set up the breakpoint. */
9831 b = set_raw_breakpoint (get_current_arch (), sal_start,
9832 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9833 set_breakpoint_count (breakpoint_count + 1);
9834 b->number = breakpoint_count;
9835 b->disposition = disp_donttouch;
9836 b->location = std::move (start_location);
9837 b->location_range_end = std::move (end_location);
9838 b->loc->length = length;
9839
9840 mention (b);
9841 gdb::observers::breakpoint_created.notify (b);
9842 update_global_location_list (UGLL_MAY_INSERT);
9843 }
9844
9845 /* Return non-zero if EXP is verified as constant. Returned zero
9846 means EXP is variable. Also the constant detection may fail for
9847 some constant expressions and in such case still falsely return
9848 zero. */
9849
9850 static int
9851 watchpoint_exp_is_const (const struct expression *exp)
9852 {
9853 int i = exp->nelts;
9854
9855 while (i > 0)
9856 {
9857 int oplenp, argsp;
9858
9859 /* We are only interested in the descriptor of each element. */
9860 operator_length (exp, i, &oplenp, &argsp);
9861 i -= oplenp;
9862
9863 switch (exp->elts[i].opcode)
9864 {
9865 case BINOP_ADD:
9866 case BINOP_SUB:
9867 case BINOP_MUL:
9868 case BINOP_DIV:
9869 case BINOP_REM:
9870 case BINOP_MOD:
9871 case BINOP_LSH:
9872 case BINOP_RSH:
9873 case BINOP_LOGICAL_AND:
9874 case BINOP_LOGICAL_OR:
9875 case BINOP_BITWISE_AND:
9876 case BINOP_BITWISE_IOR:
9877 case BINOP_BITWISE_XOR:
9878 case BINOP_EQUAL:
9879 case BINOP_NOTEQUAL:
9880 case BINOP_LESS:
9881 case BINOP_GTR:
9882 case BINOP_LEQ:
9883 case BINOP_GEQ:
9884 case BINOP_REPEAT:
9885 case BINOP_COMMA:
9886 case BINOP_EXP:
9887 case BINOP_MIN:
9888 case BINOP_MAX:
9889 case BINOP_INTDIV:
9890 case BINOP_CONCAT:
9891 case TERNOP_COND:
9892 case TERNOP_SLICE:
9893
9894 case OP_LONG:
9895 case OP_FLOAT:
9896 case OP_LAST:
9897 case OP_COMPLEX:
9898 case OP_STRING:
9899 case OP_ARRAY:
9900 case OP_TYPE:
9901 case OP_TYPEOF:
9902 case OP_DECLTYPE:
9903 case OP_TYPEID:
9904 case OP_NAME:
9905 case OP_OBJC_NSSTRING:
9906
9907 case UNOP_NEG:
9908 case UNOP_LOGICAL_NOT:
9909 case UNOP_COMPLEMENT:
9910 case UNOP_ADDR:
9911 case UNOP_HIGH:
9912 case UNOP_CAST:
9913
9914 case UNOP_CAST_TYPE:
9915 case UNOP_REINTERPRET_CAST:
9916 case UNOP_DYNAMIC_CAST:
9917 /* Unary, binary and ternary operators: We have to check
9918 their operands. If they are constant, then so is the
9919 result of that operation. For instance, if A and B are
9920 determined to be constants, then so is "A + B".
9921
9922 UNOP_IND is one exception to the rule above, because the
9923 value of *ADDR is not necessarily a constant, even when
9924 ADDR is. */
9925 break;
9926
9927 case OP_VAR_VALUE:
9928 /* Check whether the associated symbol is a constant.
9929
9930 We use SYMBOL_CLASS rather than TYPE_CONST because it's
9931 possible that a buggy compiler could mark a variable as
9932 constant even when it is not, and TYPE_CONST would return
9933 true in this case, while SYMBOL_CLASS wouldn't.
9934
9935 We also have to check for function symbols because they
9936 are always constant. */
9937 {
9938 struct symbol *s = exp->elts[i + 2].symbol;
9939
9940 if (SYMBOL_CLASS (s) != LOC_BLOCK
9941 && SYMBOL_CLASS (s) != LOC_CONST
9942 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9943 return 0;
9944 break;
9945 }
9946
9947 /* The default action is to return 0 because we are using
9948 the optimistic approach here: If we don't know something,
9949 then it is not a constant. */
9950 default:
9951 return 0;
9952 }
9953 }
9954
9955 return 1;
9956 }
9957
9958 /* Watchpoint destructor. */
9959
9960 watchpoint::~watchpoint ()
9961 {
9962 xfree (this->exp_string);
9963 xfree (this->exp_string_reparse);
9964 }
9965
9966 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
9967
9968 static void
9969 re_set_watchpoint (struct breakpoint *b)
9970 {
9971 struct watchpoint *w = (struct watchpoint *) b;
9972
9973 /* Watchpoint can be either on expression using entirely global
9974 variables, or it can be on local variables.
9975
9976 Watchpoints of the first kind are never auto-deleted, and even
9977 persist across program restarts. Since they can use variables
9978 from shared libraries, we need to reparse expression as libraries
9979 are loaded and unloaded.
9980
9981 Watchpoints on local variables can also change meaning as result
9982 of solib event. For example, if a watchpoint uses both a local
9983 and a global variables in expression, it's a local watchpoint,
9984 but unloading of a shared library will make the expression
9985 invalid. This is not a very common use case, but we still
9986 re-evaluate expression, to avoid surprises to the user.
9987
9988 Note that for local watchpoints, we re-evaluate it only if
9989 watchpoints frame id is still valid. If it's not, it means the
9990 watchpoint is out of scope and will be deleted soon. In fact,
9991 I'm not sure we'll ever be called in this case.
9992
9993 If a local watchpoint's frame id is still valid, then
9994 w->exp_valid_block is likewise valid, and we can safely use it.
9995
9996 Don't do anything about disabled watchpoints, since they will be
9997 reevaluated again when enabled. */
9998 update_watchpoint (w, 1 /* reparse */);
9999 }
10000
10001 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10002
10003 static int
10004 insert_watchpoint (struct bp_location *bl)
10005 {
10006 struct watchpoint *w = (struct watchpoint *) bl->owner;
10007 int length = w->exact ? 1 : bl->length;
10008
10009 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10010 w->cond_exp.get ());
10011 }
10012
10013 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10014
10015 static int
10016 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10017 {
10018 struct watchpoint *w = (struct watchpoint *) bl->owner;
10019 int length = w->exact ? 1 : bl->length;
10020
10021 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10022 w->cond_exp.get ());
10023 }
10024
10025 static int
10026 breakpoint_hit_watchpoint (const struct bp_location *bl,
10027 const address_space *aspace, CORE_ADDR bp_addr,
10028 const struct target_waitstatus *ws)
10029 {
10030 struct breakpoint *b = bl->owner;
10031 struct watchpoint *w = (struct watchpoint *) b;
10032
10033 /* Continuable hardware watchpoints are treated as non-existent if the
10034 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10035 some data address). Otherwise gdb won't stop on a break instruction
10036 in the code (not from a breakpoint) when a hardware watchpoint has
10037 been defined. Also skip watchpoints which we know did not trigger
10038 (did not match the data address). */
10039 if (is_hardware_watchpoint (b)
10040 && w->watchpoint_triggered == watch_triggered_no)
10041 return 0;
10042
10043 return 1;
10044 }
10045
10046 static void
10047 check_status_watchpoint (bpstat bs)
10048 {
10049 gdb_assert (is_watchpoint (bs->breakpoint_at));
10050
10051 bpstat_check_watchpoint (bs);
10052 }
10053
10054 /* Implement the "resources_needed" breakpoint_ops method for
10055 hardware watchpoints. */
10056
10057 static int
10058 resources_needed_watchpoint (const struct bp_location *bl)
10059 {
10060 struct watchpoint *w = (struct watchpoint *) bl->owner;
10061 int length = w->exact? 1 : bl->length;
10062
10063 return target_region_ok_for_hw_watchpoint (bl->address, length);
10064 }
10065
10066 /* Implement the "works_in_software_mode" breakpoint_ops method for
10067 hardware watchpoints. */
10068
10069 static int
10070 works_in_software_mode_watchpoint (const struct breakpoint *b)
10071 {
10072 /* Read and access watchpoints only work with hardware support. */
10073 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10074 }
10075
10076 static enum print_stop_action
10077 print_it_watchpoint (bpstat bs)
10078 {
10079 struct breakpoint *b;
10080 enum print_stop_action result;
10081 struct watchpoint *w;
10082 struct ui_out *uiout = current_uiout;
10083
10084 gdb_assert (bs->bp_location_at != NULL);
10085
10086 b = bs->breakpoint_at;
10087 w = (struct watchpoint *) b;
10088
10089 annotate_watchpoint (b->number);
10090 maybe_print_thread_hit_breakpoint (uiout);
10091
10092 string_file stb;
10093
10094 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10095 switch (b->type)
10096 {
10097 case bp_watchpoint:
10098 case bp_hardware_watchpoint:
10099 if (uiout->is_mi_like_p ())
10100 uiout->field_string
10101 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10102 mention (b);
10103 tuple_emitter.emplace (uiout, "value");
10104 uiout->text ("\nOld value = ");
10105 watchpoint_value_print (bs->old_val.get (), &stb);
10106 uiout->field_stream ("old", stb);
10107 uiout->text ("\nNew value = ");
10108 watchpoint_value_print (w->val.get (), &stb);
10109 uiout->field_stream ("new", stb);
10110 uiout->text ("\n");
10111 /* More than one watchpoint may have been triggered. */
10112 result = PRINT_UNKNOWN;
10113 break;
10114
10115 case bp_read_watchpoint:
10116 if (uiout->is_mi_like_p ())
10117 uiout->field_string
10118 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10119 mention (b);
10120 tuple_emitter.emplace (uiout, "value");
10121 uiout->text ("\nValue = ");
10122 watchpoint_value_print (w->val.get (), &stb);
10123 uiout->field_stream ("value", stb);
10124 uiout->text ("\n");
10125 result = PRINT_UNKNOWN;
10126 break;
10127
10128 case bp_access_watchpoint:
10129 if (bs->old_val != NULL)
10130 {
10131 if (uiout->is_mi_like_p ())
10132 uiout->field_string
10133 ("reason",
10134 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10135 mention (b);
10136 tuple_emitter.emplace (uiout, "value");
10137 uiout->text ("\nOld value = ");
10138 watchpoint_value_print (bs->old_val.get (), &stb);
10139 uiout->field_stream ("old", stb);
10140 uiout->text ("\nNew value = ");
10141 }
10142 else
10143 {
10144 mention (b);
10145 if (uiout->is_mi_like_p ())
10146 uiout->field_string
10147 ("reason",
10148 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10149 tuple_emitter.emplace (uiout, "value");
10150 uiout->text ("\nValue = ");
10151 }
10152 watchpoint_value_print (w->val.get (), &stb);
10153 uiout->field_stream ("new", stb);
10154 uiout->text ("\n");
10155 result = PRINT_UNKNOWN;
10156 break;
10157 default:
10158 result = PRINT_UNKNOWN;
10159 }
10160
10161 return result;
10162 }
10163
10164 /* Implement the "print_mention" breakpoint_ops method for hardware
10165 watchpoints. */
10166
10167 static void
10168 print_mention_watchpoint (struct breakpoint *b)
10169 {
10170 struct watchpoint *w = (struct watchpoint *) b;
10171 struct ui_out *uiout = current_uiout;
10172 const char *tuple_name;
10173
10174 switch (b->type)
10175 {
10176 case bp_watchpoint:
10177 uiout->text ("Watchpoint ");
10178 tuple_name = "wpt";
10179 break;
10180 case bp_hardware_watchpoint:
10181 uiout->text ("Hardware watchpoint ");
10182 tuple_name = "wpt";
10183 break;
10184 case bp_read_watchpoint:
10185 uiout->text ("Hardware read watchpoint ");
10186 tuple_name = "hw-rwpt";
10187 break;
10188 case bp_access_watchpoint:
10189 uiout->text ("Hardware access (read/write) watchpoint ");
10190 tuple_name = "hw-awpt";
10191 break;
10192 default:
10193 internal_error (__FILE__, __LINE__,
10194 _("Invalid hardware watchpoint type."));
10195 }
10196
10197 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10198 uiout->field_signed ("number", b->number);
10199 uiout->text (": ");
10200 uiout->field_string ("exp", w->exp_string);
10201 }
10202
10203 /* Implement the "print_recreate" breakpoint_ops method for
10204 watchpoints. */
10205
10206 static void
10207 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10208 {
10209 struct watchpoint *w = (struct watchpoint *) b;
10210
10211 switch (b->type)
10212 {
10213 case bp_watchpoint:
10214 case bp_hardware_watchpoint:
10215 fprintf_unfiltered (fp, "watch");
10216 break;
10217 case bp_read_watchpoint:
10218 fprintf_unfiltered (fp, "rwatch");
10219 break;
10220 case bp_access_watchpoint:
10221 fprintf_unfiltered (fp, "awatch");
10222 break;
10223 default:
10224 internal_error (__FILE__, __LINE__,
10225 _("Invalid watchpoint type."));
10226 }
10227
10228 fprintf_unfiltered (fp, " %s", w->exp_string);
10229 print_recreate_thread (b, fp);
10230 }
10231
10232 /* Implement the "explains_signal" breakpoint_ops method for
10233 watchpoints. */
10234
10235 static int
10236 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10237 {
10238 /* A software watchpoint cannot cause a signal other than
10239 GDB_SIGNAL_TRAP. */
10240 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10241 return 0;
10242
10243 return 1;
10244 }
10245
10246 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10247
10248 static struct breakpoint_ops watchpoint_breakpoint_ops;
10249
10250 /* Implement the "insert" breakpoint_ops method for
10251 masked hardware watchpoints. */
10252
10253 static int
10254 insert_masked_watchpoint (struct bp_location *bl)
10255 {
10256 struct watchpoint *w = (struct watchpoint *) bl->owner;
10257
10258 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10259 bl->watchpoint_type);
10260 }
10261
10262 /* Implement the "remove" breakpoint_ops method for
10263 masked hardware watchpoints. */
10264
10265 static int
10266 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10267 {
10268 struct watchpoint *w = (struct watchpoint *) bl->owner;
10269
10270 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10271 bl->watchpoint_type);
10272 }
10273
10274 /* Implement the "resources_needed" breakpoint_ops method for
10275 masked hardware watchpoints. */
10276
10277 static int
10278 resources_needed_masked_watchpoint (const struct bp_location *bl)
10279 {
10280 struct watchpoint *w = (struct watchpoint *) bl->owner;
10281
10282 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10283 }
10284
10285 /* Implement the "works_in_software_mode" breakpoint_ops method for
10286 masked hardware watchpoints. */
10287
10288 static int
10289 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10290 {
10291 return 0;
10292 }
10293
10294 /* Implement the "print_it" breakpoint_ops method for
10295 masked hardware watchpoints. */
10296
10297 static enum print_stop_action
10298 print_it_masked_watchpoint (bpstat bs)
10299 {
10300 struct breakpoint *b = bs->breakpoint_at;
10301 struct ui_out *uiout = current_uiout;
10302
10303 /* Masked watchpoints have only one location. */
10304 gdb_assert (b->loc && b->loc->next == NULL);
10305
10306 annotate_watchpoint (b->number);
10307 maybe_print_thread_hit_breakpoint (uiout);
10308
10309 switch (b->type)
10310 {
10311 case bp_hardware_watchpoint:
10312 if (uiout->is_mi_like_p ())
10313 uiout->field_string
10314 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10315 break;
10316
10317 case bp_read_watchpoint:
10318 if (uiout->is_mi_like_p ())
10319 uiout->field_string
10320 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10321 break;
10322
10323 case bp_access_watchpoint:
10324 if (uiout->is_mi_like_p ())
10325 uiout->field_string
10326 ("reason",
10327 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10328 break;
10329 default:
10330 internal_error (__FILE__, __LINE__,
10331 _("Invalid hardware watchpoint type."));
10332 }
10333
10334 mention (b);
10335 uiout->text (_("\n\
10336 Check the underlying instruction at PC for the memory\n\
10337 address and value which triggered this watchpoint.\n"));
10338 uiout->text ("\n");
10339
10340 /* More than one watchpoint may have been triggered. */
10341 return PRINT_UNKNOWN;
10342 }
10343
10344 /* Implement the "print_one_detail" breakpoint_ops method for
10345 masked hardware watchpoints. */
10346
10347 static void
10348 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10349 struct ui_out *uiout)
10350 {
10351 struct watchpoint *w = (struct watchpoint *) b;
10352
10353 /* Masked watchpoints have only one location. */
10354 gdb_assert (b->loc && b->loc->next == NULL);
10355
10356 uiout->text ("\tmask ");
10357 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10358 uiout->text ("\n");
10359 }
10360
10361 /* Implement the "print_mention" breakpoint_ops method for
10362 masked hardware watchpoints. */
10363
10364 static void
10365 print_mention_masked_watchpoint (struct breakpoint *b)
10366 {
10367 struct watchpoint *w = (struct watchpoint *) b;
10368 struct ui_out *uiout = current_uiout;
10369 const char *tuple_name;
10370
10371 switch (b->type)
10372 {
10373 case bp_hardware_watchpoint:
10374 uiout->text ("Masked hardware watchpoint ");
10375 tuple_name = "wpt";
10376 break;
10377 case bp_read_watchpoint:
10378 uiout->text ("Masked hardware read watchpoint ");
10379 tuple_name = "hw-rwpt";
10380 break;
10381 case bp_access_watchpoint:
10382 uiout->text ("Masked hardware access (read/write) watchpoint ");
10383 tuple_name = "hw-awpt";
10384 break;
10385 default:
10386 internal_error (__FILE__, __LINE__,
10387 _("Invalid hardware watchpoint type."));
10388 }
10389
10390 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10391 uiout->field_signed ("number", b->number);
10392 uiout->text (": ");
10393 uiout->field_string ("exp", w->exp_string);
10394 }
10395
10396 /* Implement the "print_recreate" breakpoint_ops method for
10397 masked hardware watchpoints. */
10398
10399 static void
10400 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10401 {
10402 struct watchpoint *w = (struct watchpoint *) b;
10403 char tmp[40];
10404
10405 switch (b->type)
10406 {
10407 case bp_hardware_watchpoint:
10408 fprintf_unfiltered (fp, "watch");
10409 break;
10410 case bp_read_watchpoint:
10411 fprintf_unfiltered (fp, "rwatch");
10412 break;
10413 case bp_access_watchpoint:
10414 fprintf_unfiltered (fp, "awatch");
10415 break;
10416 default:
10417 internal_error (__FILE__, __LINE__,
10418 _("Invalid hardware watchpoint type."));
10419 }
10420
10421 sprintf_vma (tmp, w->hw_wp_mask);
10422 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10423 print_recreate_thread (b, fp);
10424 }
10425
10426 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10427
10428 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10429
10430 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10431
10432 static bool
10433 is_masked_watchpoint (const struct breakpoint *b)
10434 {
10435 return b->ops == &masked_watchpoint_breakpoint_ops;
10436 }
10437
10438 /* accessflag: hw_write: watch write,
10439 hw_read: watch read,
10440 hw_access: watch access (read or write) */
10441 static void
10442 watch_command_1 (const char *arg, int accessflag, int from_tty,
10443 int just_location, int internal)
10444 {
10445 struct breakpoint *scope_breakpoint = NULL;
10446 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10447 struct value *result;
10448 int saved_bitpos = 0, saved_bitsize = 0;
10449 const char *exp_start = NULL;
10450 const char *exp_end = NULL;
10451 const char *tok, *end_tok;
10452 int toklen = -1;
10453 const char *cond_start = NULL;
10454 const char *cond_end = NULL;
10455 enum bptype bp_type;
10456 int thread = -1;
10457 int pc = 0;
10458 /* Flag to indicate whether we are going to use masks for
10459 the hardware watchpoint. */
10460 int use_mask = 0;
10461 CORE_ADDR mask = 0;
10462
10463 /* Make sure that we actually have parameters to parse. */
10464 if (arg != NULL && arg[0] != '\0')
10465 {
10466 const char *value_start;
10467
10468 exp_end = arg + strlen (arg);
10469
10470 /* Look for "parameter value" pairs at the end
10471 of the arguments string. */
10472 for (tok = exp_end - 1; tok > arg; tok--)
10473 {
10474 /* Skip whitespace at the end of the argument list. */
10475 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10476 tok--;
10477
10478 /* Find the beginning of the last token.
10479 This is the value of the parameter. */
10480 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10481 tok--;
10482 value_start = tok + 1;
10483
10484 /* Skip whitespace. */
10485 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10486 tok--;
10487
10488 end_tok = tok;
10489
10490 /* Find the beginning of the second to last token.
10491 This is the parameter itself. */
10492 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10493 tok--;
10494 tok++;
10495 toklen = end_tok - tok + 1;
10496
10497 if (toklen == 6 && startswith (tok, "thread"))
10498 {
10499 struct thread_info *thr;
10500 /* At this point we've found a "thread" token, which means
10501 the user is trying to set a watchpoint that triggers
10502 only in a specific thread. */
10503 const char *endp;
10504
10505 if (thread != -1)
10506 error(_("You can specify only one thread."));
10507
10508 /* Extract the thread ID from the next token. */
10509 thr = parse_thread_id (value_start, &endp);
10510
10511 /* Check if the user provided a valid thread ID. */
10512 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10513 invalid_thread_id_error (value_start);
10514
10515 thread = thr->global_num;
10516 }
10517 else if (toklen == 4 && startswith (tok, "mask"))
10518 {
10519 /* We've found a "mask" token, which means the user wants to
10520 create a hardware watchpoint that is going to have the mask
10521 facility. */
10522 struct value *mask_value, *mark;
10523
10524 if (use_mask)
10525 error(_("You can specify only one mask."));
10526
10527 use_mask = just_location = 1;
10528
10529 mark = value_mark ();
10530 mask_value = parse_to_comma_and_eval (&value_start);
10531 mask = value_as_address (mask_value);
10532 value_free_to_mark (mark);
10533 }
10534 else
10535 /* We didn't recognize what we found. We should stop here. */
10536 break;
10537
10538 /* Truncate the string and get rid of the "parameter value" pair before
10539 the arguments string is parsed by the parse_exp_1 function. */
10540 exp_end = tok;
10541 }
10542 }
10543 else
10544 exp_end = arg;
10545
10546 /* Parse the rest of the arguments. From here on out, everything
10547 is in terms of a newly allocated string instead of the original
10548 ARG. */
10549 std::string expression (arg, exp_end - arg);
10550 exp_start = arg = expression.c_str ();
10551 innermost_block_tracker tracker;
10552 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10553 exp_end = arg;
10554 /* Remove trailing whitespace from the expression before saving it.
10555 This makes the eventual display of the expression string a bit
10556 prettier. */
10557 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10558 --exp_end;
10559
10560 /* Checking if the expression is not constant. */
10561 if (watchpoint_exp_is_const (exp.get ()))
10562 {
10563 int len;
10564
10565 len = exp_end - exp_start;
10566 while (len > 0 && isspace (exp_start[len - 1]))
10567 len--;
10568 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10569 }
10570
10571 exp_valid_block = tracker.block ();
10572 struct value *mark = value_mark ();
10573 struct value *val_as_value = nullptr;
10574 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10575 just_location);
10576
10577 if (val_as_value != NULL && just_location)
10578 {
10579 saved_bitpos = value_bitpos (val_as_value);
10580 saved_bitsize = value_bitsize (val_as_value);
10581 }
10582
10583 value_ref_ptr val;
10584 if (just_location)
10585 {
10586 int ret;
10587
10588 exp_valid_block = NULL;
10589 val = release_value (value_addr (result));
10590 value_free_to_mark (mark);
10591
10592 if (use_mask)
10593 {
10594 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10595 mask);
10596 if (ret == -1)
10597 error (_("This target does not support masked watchpoints."));
10598 else if (ret == -2)
10599 error (_("Invalid mask or memory region."));
10600 }
10601 }
10602 else if (val_as_value != NULL)
10603 val = release_value (val_as_value);
10604
10605 tok = skip_spaces (arg);
10606 end_tok = skip_to_space (tok);
10607
10608 toklen = end_tok - tok;
10609 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10610 {
10611 tok = cond_start = end_tok + 1;
10612 innermost_block_tracker if_tracker;
10613 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10614
10615 /* The watchpoint expression may not be local, but the condition
10616 may still be. E.g.: `watch global if local > 0'. */
10617 cond_exp_valid_block = if_tracker.block ();
10618
10619 cond_end = tok;
10620 }
10621 if (*tok)
10622 error (_("Junk at end of command."));
10623
10624 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10625
10626 /* Save this because create_internal_breakpoint below invalidates
10627 'wp_frame'. */
10628 frame_id watchpoint_frame = get_frame_id (wp_frame);
10629
10630 /* If the expression is "local", then set up a "watchpoint scope"
10631 breakpoint at the point where we've left the scope of the watchpoint
10632 expression. Create the scope breakpoint before the watchpoint, so
10633 that we will encounter it first in bpstat_stop_status. */
10634 if (exp_valid_block != NULL && wp_frame != NULL)
10635 {
10636 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10637
10638 if (frame_id_p (caller_frame_id))
10639 {
10640 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10641 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10642
10643 scope_breakpoint
10644 = create_internal_breakpoint (caller_arch, caller_pc,
10645 bp_watchpoint_scope,
10646 &momentary_breakpoint_ops);
10647
10648 /* create_internal_breakpoint could invalidate WP_FRAME. */
10649 wp_frame = NULL;
10650
10651 scope_breakpoint->enable_state = bp_enabled;
10652
10653 /* Automatically delete the breakpoint when it hits. */
10654 scope_breakpoint->disposition = disp_del;
10655
10656 /* Only break in the proper frame (help with recursion). */
10657 scope_breakpoint->frame_id = caller_frame_id;
10658
10659 /* Set the address at which we will stop. */
10660 scope_breakpoint->loc->gdbarch = caller_arch;
10661 scope_breakpoint->loc->requested_address = caller_pc;
10662 scope_breakpoint->loc->address
10663 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10664 scope_breakpoint->loc->requested_address,
10665 scope_breakpoint->type);
10666 }
10667 }
10668
10669 /* Now set up the breakpoint. We create all watchpoints as hardware
10670 watchpoints here even if hardware watchpoints are turned off, a call
10671 to update_watchpoint later in this function will cause the type to
10672 drop back to bp_watchpoint (software watchpoint) if required. */
10673
10674 if (accessflag == hw_read)
10675 bp_type = bp_read_watchpoint;
10676 else if (accessflag == hw_access)
10677 bp_type = bp_access_watchpoint;
10678 else
10679 bp_type = bp_hardware_watchpoint;
10680
10681 std::unique_ptr<watchpoint> w (new watchpoint ());
10682
10683 if (use_mask)
10684 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10685 &masked_watchpoint_breakpoint_ops);
10686 else
10687 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10688 &watchpoint_breakpoint_ops);
10689 w->thread = thread;
10690 w->disposition = disp_donttouch;
10691 w->pspace = current_program_space;
10692 w->exp = std::move (exp);
10693 w->exp_valid_block = exp_valid_block;
10694 w->cond_exp_valid_block = cond_exp_valid_block;
10695 if (just_location)
10696 {
10697 struct type *t = value_type (val.get ());
10698 CORE_ADDR addr = value_as_address (val.get ());
10699
10700 w->exp_string_reparse
10701 = current_language->la_watch_location_expression (t, addr).release ();
10702
10703 w->exp_string = xstrprintf ("-location %.*s",
10704 (int) (exp_end - exp_start), exp_start);
10705 }
10706 else
10707 w->exp_string = savestring (exp_start, exp_end - exp_start);
10708
10709 if (use_mask)
10710 {
10711 w->hw_wp_mask = mask;
10712 }
10713 else
10714 {
10715 w->val = val;
10716 w->val_bitpos = saved_bitpos;
10717 w->val_bitsize = saved_bitsize;
10718 w->val_valid = true;
10719 }
10720
10721 if (cond_start)
10722 w->cond_string = savestring (cond_start, cond_end - cond_start);
10723 else
10724 w->cond_string = 0;
10725
10726 if (frame_id_p (watchpoint_frame))
10727 {
10728 w->watchpoint_frame = watchpoint_frame;
10729 w->watchpoint_thread = inferior_ptid;
10730 }
10731 else
10732 {
10733 w->watchpoint_frame = null_frame_id;
10734 w->watchpoint_thread = null_ptid;
10735 }
10736
10737 if (scope_breakpoint != NULL)
10738 {
10739 /* The scope breakpoint is related to the watchpoint. We will
10740 need to act on them together. */
10741 w->related_breakpoint = scope_breakpoint;
10742 scope_breakpoint->related_breakpoint = w.get ();
10743 }
10744
10745 if (!just_location)
10746 value_free_to_mark (mark);
10747
10748 /* Finally update the new watchpoint. This creates the locations
10749 that should be inserted. */
10750 update_watchpoint (w.get (), 1);
10751
10752 install_breakpoint (internal, std::move (w), 1);
10753 }
10754
10755 /* Return count of debug registers needed to watch the given expression.
10756 If the watchpoint cannot be handled in hardware return zero. */
10757
10758 static int
10759 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10760 {
10761 int found_memory_cnt = 0;
10762
10763 /* Did the user specifically forbid us to use hardware watchpoints? */
10764 if (!can_use_hw_watchpoints)
10765 return 0;
10766
10767 gdb_assert (!vals.empty ());
10768 struct value *head = vals[0].get ();
10769
10770 /* Make sure that the value of the expression depends only upon
10771 memory contents, and values computed from them within GDB. If we
10772 find any register references or function calls, we can't use a
10773 hardware watchpoint.
10774
10775 The idea here is that evaluating an expression generates a series
10776 of values, one holding the value of every subexpression. (The
10777 expression a*b+c has five subexpressions: a, b, a*b, c, and
10778 a*b+c.) GDB's values hold almost enough information to establish
10779 the criteria given above --- they identify memory lvalues,
10780 register lvalues, computed values, etcetera. So we can evaluate
10781 the expression, and then scan the chain of values that leaves
10782 behind to decide whether we can detect any possible change to the
10783 expression's final value using only hardware watchpoints.
10784
10785 However, I don't think that the values returned by inferior
10786 function calls are special in any way. So this function may not
10787 notice that an expression involving an inferior function call
10788 can't be watched with hardware watchpoints. FIXME. */
10789 for (const value_ref_ptr &iter : vals)
10790 {
10791 struct value *v = iter.get ();
10792
10793 if (VALUE_LVAL (v) == lval_memory)
10794 {
10795 if (v != head && value_lazy (v))
10796 /* A lazy memory lvalue in the chain is one that GDB never
10797 needed to fetch; we either just used its address (e.g.,
10798 `a' in `a.b') or we never needed it at all (e.g., `a'
10799 in `a,b'). This doesn't apply to HEAD; if that is
10800 lazy then it was not readable, but watch it anyway. */
10801 ;
10802 else
10803 {
10804 /* Ahh, memory we actually used! Check if we can cover
10805 it with hardware watchpoints. */
10806 struct type *vtype = check_typedef (value_type (v));
10807
10808 /* We only watch structs and arrays if user asked for it
10809 explicitly, never if they just happen to appear in a
10810 middle of some value chain. */
10811 if (v == head
10812 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10813 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10814 {
10815 CORE_ADDR vaddr = value_address (v);
10816 int len;
10817 int num_regs;
10818
10819 len = (target_exact_watchpoints
10820 && is_scalar_type_recursive (vtype))?
10821 1 : TYPE_LENGTH (value_type (v));
10822
10823 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10824 if (!num_regs)
10825 return 0;
10826 else
10827 found_memory_cnt += num_regs;
10828 }
10829 }
10830 }
10831 else if (VALUE_LVAL (v) != not_lval
10832 && deprecated_value_modifiable (v) == 0)
10833 return 0; /* These are values from the history (e.g., $1). */
10834 else if (VALUE_LVAL (v) == lval_register)
10835 return 0; /* Cannot watch a register with a HW watchpoint. */
10836 }
10837
10838 /* The expression itself looks suitable for using a hardware
10839 watchpoint, but give the target machine a chance to reject it. */
10840 return found_memory_cnt;
10841 }
10842
10843 void
10844 watch_command_wrapper (const char *arg, int from_tty, int internal)
10845 {
10846 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10847 }
10848
10849 /* A helper function that looks for the "-location" argument and then
10850 calls watch_command_1. */
10851
10852 static void
10853 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10854 {
10855 int just_location = 0;
10856
10857 if (arg
10858 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10859 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10860 just_location = 1;
10861
10862 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10863 }
10864
10865 static void
10866 watch_command (const char *arg, int from_tty)
10867 {
10868 watch_maybe_just_location (arg, hw_write, from_tty);
10869 }
10870
10871 void
10872 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
10873 {
10874 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10875 }
10876
10877 static void
10878 rwatch_command (const char *arg, int from_tty)
10879 {
10880 watch_maybe_just_location (arg, hw_read, from_tty);
10881 }
10882
10883 void
10884 awatch_command_wrapper (const char *arg, int from_tty, int internal)
10885 {
10886 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10887 }
10888
10889 static void
10890 awatch_command (const char *arg, int from_tty)
10891 {
10892 watch_maybe_just_location (arg, hw_access, from_tty);
10893 }
10894 \f
10895
10896 /* Data for the FSM that manages the until(location)/advance commands
10897 in infcmd.c. Here because it uses the mechanisms of
10898 breakpoints. */
10899
10900 struct until_break_fsm : public thread_fsm
10901 {
10902 /* The thread that was current when the command was executed. */
10903 int thread;
10904
10905 /* The breakpoint set at the destination location. */
10906 breakpoint_up location_breakpoint;
10907
10908 /* Breakpoint set at the return address in the caller frame. May be
10909 NULL. */
10910 breakpoint_up caller_breakpoint;
10911
10912 until_break_fsm (struct interp *cmd_interp, int thread,
10913 breakpoint_up &&location_breakpoint,
10914 breakpoint_up &&caller_breakpoint)
10915 : thread_fsm (cmd_interp),
10916 thread (thread),
10917 location_breakpoint (std::move (location_breakpoint)),
10918 caller_breakpoint (std::move (caller_breakpoint))
10919 {
10920 }
10921
10922 void clean_up (struct thread_info *thread) override;
10923 bool should_stop (struct thread_info *thread) override;
10924 enum async_reply_reason do_async_reply_reason () override;
10925 };
10926
10927 /* Implementation of the 'should_stop' FSM method for the
10928 until(location)/advance commands. */
10929
10930 bool
10931 until_break_fsm::should_stop (struct thread_info *tp)
10932 {
10933 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10934 location_breakpoint.get ()) != NULL
10935 || (caller_breakpoint != NULL
10936 && bpstat_find_breakpoint (tp->control.stop_bpstat,
10937 caller_breakpoint.get ()) != NULL))
10938 set_finished ();
10939
10940 return true;
10941 }
10942
10943 /* Implementation of the 'clean_up' FSM method for the
10944 until(location)/advance commands. */
10945
10946 void
10947 until_break_fsm::clean_up (struct thread_info *)
10948 {
10949 /* Clean up our temporary breakpoints. */
10950 location_breakpoint.reset ();
10951 caller_breakpoint.reset ();
10952 delete_longjmp_breakpoint (thread);
10953 }
10954
10955 /* Implementation of the 'async_reply_reason' FSM method for the
10956 until(location)/advance commands. */
10957
10958 enum async_reply_reason
10959 until_break_fsm::do_async_reply_reason ()
10960 {
10961 return EXEC_ASYNC_LOCATION_REACHED;
10962 }
10963
10964 void
10965 until_break_command (const char *arg, int from_tty, int anywhere)
10966 {
10967 struct frame_info *frame;
10968 struct gdbarch *frame_gdbarch;
10969 struct frame_id stack_frame_id;
10970 struct frame_id caller_frame_id;
10971 int thread;
10972 struct thread_info *tp;
10973
10974 clear_proceed_status (0);
10975
10976 /* Set a breakpoint where the user wants it and at return from
10977 this function. */
10978
10979 event_location_up location = string_to_event_location (&arg, current_language);
10980
10981 std::vector<symtab_and_line> sals
10982 = (last_displayed_sal_is_valid ()
10983 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10984 get_last_displayed_symtab (),
10985 get_last_displayed_line ())
10986 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
10987 NULL, NULL, 0));
10988
10989 if (sals.size () != 1)
10990 error (_("Couldn't get information on specified line."));
10991
10992 symtab_and_line &sal = sals[0];
10993
10994 if (*arg)
10995 error (_("Junk at end of arguments."));
10996
10997 resolve_sal_pc (&sal);
10998
10999 tp = inferior_thread ();
11000 thread = tp->global_num;
11001
11002 /* Note linespec handling above invalidates the frame chain.
11003 Installing a breakpoint also invalidates the frame chain (as it
11004 may need to switch threads), so do any frame handling before
11005 that. */
11006
11007 frame = get_selected_frame (NULL);
11008 frame_gdbarch = get_frame_arch (frame);
11009 stack_frame_id = get_stack_frame_id (frame);
11010 caller_frame_id = frame_unwind_caller_id (frame);
11011
11012 /* Keep within the current frame, or in frames called by the current
11013 one. */
11014
11015 breakpoint_up caller_breakpoint;
11016
11017 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11018
11019 if (frame_id_p (caller_frame_id))
11020 {
11021 struct symtab_and_line sal2;
11022 struct gdbarch *caller_gdbarch;
11023
11024 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11025 sal2.pc = frame_unwind_caller_pc (frame);
11026 caller_gdbarch = frame_unwind_caller_arch (frame);
11027 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11028 sal2,
11029 caller_frame_id,
11030 bp_until);
11031
11032 set_longjmp_breakpoint (tp, caller_frame_id);
11033 lj_deleter.emplace (thread);
11034 }
11035
11036 /* set_momentary_breakpoint could invalidate FRAME. */
11037 frame = NULL;
11038
11039 breakpoint_up location_breakpoint;
11040 if (anywhere)
11041 /* If the user told us to continue until a specified location,
11042 we don't specify a frame at which we need to stop. */
11043 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11044 null_frame_id, bp_until);
11045 else
11046 /* Otherwise, specify the selected frame, because we want to stop
11047 only at the very same frame. */
11048 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11049 stack_frame_id, bp_until);
11050
11051 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11052 std::move (location_breakpoint),
11053 std::move (caller_breakpoint));
11054
11055 if (lj_deleter)
11056 lj_deleter->release ();
11057
11058 proceed (-1, GDB_SIGNAL_DEFAULT);
11059 }
11060
11061 /* This function attempts to parse an optional "if <cond>" clause
11062 from the arg string. If one is not found, it returns NULL.
11063
11064 Else, it returns a pointer to the condition string. (It does not
11065 attempt to evaluate the string against a particular block.) And,
11066 it updates arg to point to the first character following the parsed
11067 if clause in the arg string. */
11068
11069 const char *
11070 ep_parse_optional_if_clause (const char **arg)
11071 {
11072 const char *cond_string;
11073
11074 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11075 return NULL;
11076
11077 /* Skip the "if" keyword. */
11078 (*arg) += 2;
11079
11080 /* Skip any extra leading whitespace, and record the start of the
11081 condition string. */
11082 *arg = skip_spaces (*arg);
11083 cond_string = *arg;
11084
11085 /* Assume that the condition occupies the remainder of the arg
11086 string. */
11087 (*arg) += strlen (cond_string);
11088
11089 return cond_string;
11090 }
11091
11092 /* Commands to deal with catching events, such as signals, exceptions,
11093 process start/exit, etc. */
11094
11095 typedef enum
11096 {
11097 catch_fork_temporary, catch_vfork_temporary,
11098 catch_fork_permanent, catch_vfork_permanent
11099 }
11100 catch_fork_kind;
11101
11102 static void
11103 catch_fork_command_1 (const char *arg, int from_tty,
11104 struct cmd_list_element *command)
11105 {
11106 struct gdbarch *gdbarch = get_current_arch ();
11107 const char *cond_string = NULL;
11108 catch_fork_kind fork_kind;
11109 int tempflag;
11110
11111 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11112 tempflag = (fork_kind == catch_fork_temporary
11113 || fork_kind == catch_vfork_temporary);
11114
11115 if (!arg)
11116 arg = "";
11117 arg = skip_spaces (arg);
11118
11119 /* The allowed syntax is:
11120 catch [v]fork
11121 catch [v]fork if <cond>
11122
11123 First, check if there's an if clause. */
11124 cond_string = ep_parse_optional_if_clause (&arg);
11125
11126 if ((*arg != '\0') && !isspace (*arg))
11127 error (_("Junk at end of arguments."));
11128
11129 /* If this target supports it, create a fork or vfork catchpoint
11130 and enable reporting of such events. */
11131 switch (fork_kind)
11132 {
11133 case catch_fork_temporary:
11134 case catch_fork_permanent:
11135 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11136 &catch_fork_breakpoint_ops);
11137 break;
11138 case catch_vfork_temporary:
11139 case catch_vfork_permanent:
11140 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11141 &catch_vfork_breakpoint_ops);
11142 break;
11143 default:
11144 error (_("unsupported or unknown fork kind; cannot catch it"));
11145 break;
11146 }
11147 }
11148
11149 static void
11150 catch_exec_command_1 (const char *arg, int from_tty,
11151 struct cmd_list_element *command)
11152 {
11153 struct gdbarch *gdbarch = get_current_arch ();
11154 int tempflag;
11155 const char *cond_string = NULL;
11156
11157 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11158
11159 if (!arg)
11160 arg = "";
11161 arg = skip_spaces (arg);
11162
11163 /* The allowed syntax is:
11164 catch exec
11165 catch exec if <cond>
11166
11167 First, check if there's an if clause. */
11168 cond_string = ep_parse_optional_if_clause (&arg);
11169
11170 if ((*arg != '\0') && !isspace (*arg))
11171 error (_("Junk at end of arguments."));
11172
11173 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11174 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11175 &catch_exec_breakpoint_ops);
11176 c->exec_pathname = NULL;
11177
11178 install_breakpoint (0, std::move (c), 1);
11179 }
11180
11181 void
11182 init_ada_exception_breakpoint (struct breakpoint *b,
11183 struct gdbarch *gdbarch,
11184 struct symtab_and_line sal,
11185 const char *addr_string,
11186 const struct breakpoint_ops *ops,
11187 int tempflag,
11188 int enabled,
11189 int from_tty)
11190 {
11191 if (from_tty)
11192 {
11193 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11194 if (!loc_gdbarch)
11195 loc_gdbarch = gdbarch;
11196
11197 describe_other_breakpoints (loc_gdbarch,
11198 sal.pspace, sal.pc, sal.section, -1);
11199 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11200 version for exception catchpoints, because two catchpoints
11201 used for different exception names will use the same address.
11202 In this case, a "breakpoint ... also set at..." warning is
11203 unproductive. Besides, the warning phrasing is also a bit
11204 inappropriate, we should use the word catchpoint, and tell
11205 the user what type of catchpoint it is. The above is good
11206 enough for now, though. */
11207 }
11208
11209 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11210
11211 b->enable_state = enabled ? bp_enabled : bp_disabled;
11212 b->disposition = tempflag ? disp_del : disp_donttouch;
11213 b->location = string_to_event_location (&addr_string,
11214 language_def (language_ada));
11215 b->language = language_ada;
11216 }
11217
11218 static void
11219 catch_command (const char *arg, int from_tty)
11220 {
11221 error (_("Catch requires an event name."));
11222 }
11223 \f
11224
11225 static void
11226 tcatch_command (const char *arg, int from_tty)
11227 {
11228 error (_("Catch requires an event name."));
11229 }
11230
11231 /* Compare two breakpoints and return a strcmp-like result. */
11232
11233 static int
11234 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11235 {
11236 uintptr_t ua = (uintptr_t) a;
11237 uintptr_t ub = (uintptr_t) b;
11238
11239 if (a->number < b->number)
11240 return -1;
11241 else if (a->number > b->number)
11242 return 1;
11243
11244 /* Now sort by address, in case we see, e..g, two breakpoints with
11245 the number 0. */
11246 if (ua < ub)
11247 return -1;
11248 return ua > ub ? 1 : 0;
11249 }
11250
11251 /* Delete breakpoints by address or line. */
11252
11253 static void
11254 clear_command (const char *arg, int from_tty)
11255 {
11256 struct breakpoint *b;
11257 int default_match;
11258
11259 std::vector<symtab_and_line> decoded_sals;
11260 symtab_and_line last_sal;
11261 gdb::array_view<symtab_and_line> sals;
11262 if (arg)
11263 {
11264 decoded_sals
11265 = decode_line_with_current_source (arg,
11266 (DECODE_LINE_FUNFIRSTLINE
11267 | DECODE_LINE_LIST_MODE));
11268 default_match = 0;
11269 sals = decoded_sals;
11270 }
11271 else
11272 {
11273 /* Set sal's line, symtab, pc, and pspace to the values
11274 corresponding to the last call to print_frame_info. If the
11275 codepoint is not valid, this will set all the fields to 0. */
11276 last_sal = get_last_displayed_sal ();
11277 if (last_sal.symtab == 0)
11278 error (_("No source file specified."));
11279
11280 default_match = 1;
11281 sals = last_sal;
11282 }
11283
11284 /* We don't call resolve_sal_pc here. That's not as bad as it
11285 seems, because all existing breakpoints typically have both
11286 file/line and pc set. So, if clear is given file/line, we can
11287 match this to existing breakpoint without obtaining pc at all.
11288
11289 We only support clearing given the address explicitly
11290 present in breakpoint table. Say, we've set breakpoint
11291 at file:line. There were several PC values for that file:line,
11292 due to optimization, all in one block.
11293
11294 We've picked one PC value. If "clear" is issued with another
11295 PC corresponding to the same file:line, the breakpoint won't
11296 be cleared. We probably can still clear the breakpoint, but
11297 since the other PC value is never presented to user, user
11298 can only find it by guessing, and it does not seem important
11299 to support that. */
11300
11301 /* For each line spec given, delete bps which correspond to it. Do
11302 it in two passes, solely to preserve the current behavior that
11303 from_tty is forced true if we delete more than one
11304 breakpoint. */
11305
11306 std::vector<struct breakpoint *> found;
11307 for (const auto &sal : sals)
11308 {
11309 const char *sal_fullname;
11310
11311 /* If exact pc given, clear bpts at that pc.
11312 If line given (pc == 0), clear all bpts on specified line.
11313 If defaulting, clear all bpts on default line
11314 or at default pc.
11315
11316 defaulting sal.pc != 0 tests to do
11317
11318 0 1 pc
11319 1 1 pc _and_ line
11320 0 0 line
11321 1 0 <can't happen> */
11322
11323 sal_fullname = (sal.symtab == NULL
11324 ? NULL : symtab_to_fullname (sal.symtab));
11325
11326 /* Find all matching breakpoints and add them to 'found'. */
11327 ALL_BREAKPOINTS (b)
11328 {
11329 int match = 0;
11330 /* Are we going to delete b? */
11331 if (b->type != bp_none && !is_watchpoint (b))
11332 {
11333 struct bp_location *loc = b->loc;
11334 for (; loc; loc = loc->next)
11335 {
11336 /* If the user specified file:line, don't allow a PC
11337 match. This matches historical gdb behavior. */
11338 int pc_match = (!sal.explicit_line
11339 && sal.pc
11340 && (loc->pspace == sal.pspace)
11341 && (loc->address == sal.pc)
11342 && (!section_is_overlay (loc->section)
11343 || loc->section == sal.section));
11344 int line_match = 0;
11345
11346 if ((default_match || sal.explicit_line)
11347 && loc->symtab != NULL
11348 && sal_fullname != NULL
11349 && sal.pspace == loc->pspace
11350 && loc->line_number == sal.line
11351 && filename_cmp (symtab_to_fullname (loc->symtab),
11352 sal_fullname) == 0)
11353 line_match = 1;
11354
11355 if (pc_match || line_match)
11356 {
11357 match = 1;
11358 break;
11359 }
11360 }
11361 }
11362
11363 if (match)
11364 found.push_back (b);
11365 }
11366 }
11367
11368 /* Now go thru the 'found' chain and delete them. */
11369 if (found.empty ())
11370 {
11371 if (arg)
11372 error (_("No breakpoint at %s."), arg);
11373 else
11374 error (_("No breakpoint at this line."));
11375 }
11376
11377 /* Remove duplicates from the vec. */
11378 std::sort (found.begin (), found.end (),
11379 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11380 {
11381 return compare_breakpoints (bp_a, bp_b) < 0;
11382 });
11383 found.erase (std::unique (found.begin (), found.end (),
11384 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11385 {
11386 return compare_breakpoints (bp_a, bp_b) == 0;
11387 }),
11388 found.end ());
11389
11390 if (found.size () > 1)
11391 from_tty = 1; /* Always report if deleted more than one. */
11392 if (from_tty)
11393 {
11394 if (found.size () == 1)
11395 printf_unfiltered (_("Deleted breakpoint "));
11396 else
11397 printf_unfiltered (_("Deleted breakpoints "));
11398 }
11399
11400 for (breakpoint *iter : found)
11401 {
11402 if (from_tty)
11403 printf_unfiltered ("%d ", iter->number);
11404 delete_breakpoint (iter);
11405 }
11406 if (from_tty)
11407 putchar_unfiltered ('\n');
11408 }
11409 \f
11410 /* Delete breakpoint in BS if they are `delete' breakpoints and
11411 all breakpoints that are marked for deletion, whether hit or not.
11412 This is called after any breakpoint is hit, or after errors. */
11413
11414 void
11415 breakpoint_auto_delete (bpstat bs)
11416 {
11417 struct breakpoint *b, *b_tmp;
11418
11419 for (; bs; bs = bs->next)
11420 if (bs->breakpoint_at
11421 && bs->breakpoint_at->disposition == disp_del
11422 && bs->stop)
11423 delete_breakpoint (bs->breakpoint_at);
11424
11425 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11426 {
11427 if (b->disposition == disp_del_at_next_stop)
11428 delete_breakpoint (b);
11429 }
11430 }
11431
11432 /* A comparison function for bp_location AP and BP being interfaced to
11433 qsort. Sort elements primarily by their ADDRESS (no matter what
11434 bl_address_is_meaningful says), secondarily by ordering first
11435 permanent elements and terciarily just ensuring the array is sorted
11436 stable way despite qsort being an unstable algorithm. */
11437
11438 static int
11439 bp_locations_compare (const void *ap, const void *bp)
11440 {
11441 const struct bp_location *a = *(const struct bp_location **) ap;
11442 const struct bp_location *b = *(const struct bp_location **) bp;
11443
11444 if (a->address != b->address)
11445 return (a->address > b->address) - (a->address < b->address);
11446
11447 /* Sort locations at the same address by their pspace number, keeping
11448 locations of the same inferior (in a multi-inferior environment)
11449 grouped. */
11450
11451 if (a->pspace->num != b->pspace->num)
11452 return ((a->pspace->num > b->pspace->num)
11453 - (a->pspace->num < b->pspace->num));
11454
11455 /* Sort permanent breakpoints first. */
11456 if (a->permanent != b->permanent)
11457 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11458
11459 /* Make the internal GDB representation stable across GDB runs
11460 where A and B memory inside GDB can differ. Breakpoint locations of
11461 the same type at the same address can be sorted in arbitrary order. */
11462
11463 if (a->owner->number != b->owner->number)
11464 return ((a->owner->number > b->owner->number)
11465 - (a->owner->number < b->owner->number));
11466
11467 return (a > b) - (a < b);
11468 }
11469
11470 /* Set bp_locations_placed_address_before_address_max and
11471 bp_locations_shadow_len_after_address_max according to the current
11472 content of the bp_locations array. */
11473
11474 static void
11475 bp_locations_target_extensions_update (void)
11476 {
11477 struct bp_location *bl, **blp_tmp;
11478
11479 bp_locations_placed_address_before_address_max = 0;
11480 bp_locations_shadow_len_after_address_max = 0;
11481
11482 ALL_BP_LOCATIONS (bl, blp_tmp)
11483 {
11484 CORE_ADDR start, end, addr;
11485
11486 if (!bp_location_has_shadow (bl))
11487 continue;
11488
11489 start = bl->target_info.placed_address;
11490 end = start + bl->target_info.shadow_len;
11491
11492 gdb_assert (bl->address >= start);
11493 addr = bl->address - start;
11494 if (addr > bp_locations_placed_address_before_address_max)
11495 bp_locations_placed_address_before_address_max = addr;
11496
11497 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11498
11499 gdb_assert (bl->address < end);
11500 addr = end - bl->address;
11501 if (addr > bp_locations_shadow_len_after_address_max)
11502 bp_locations_shadow_len_after_address_max = addr;
11503 }
11504 }
11505
11506 /* Download tracepoint locations if they haven't been. */
11507
11508 static void
11509 download_tracepoint_locations (void)
11510 {
11511 struct breakpoint *b;
11512 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11513
11514 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11515
11516 ALL_TRACEPOINTS (b)
11517 {
11518 struct bp_location *bl;
11519 struct tracepoint *t;
11520 int bp_location_downloaded = 0;
11521
11522 if ((b->type == bp_fast_tracepoint
11523 ? !may_insert_fast_tracepoints
11524 : !may_insert_tracepoints))
11525 continue;
11526
11527 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11528 {
11529 if (target_can_download_tracepoint ())
11530 can_download_tracepoint = TRIBOOL_TRUE;
11531 else
11532 can_download_tracepoint = TRIBOOL_FALSE;
11533 }
11534
11535 if (can_download_tracepoint == TRIBOOL_FALSE)
11536 break;
11537
11538 for (bl = b->loc; bl; bl = bl->next)
11539 {
11540 /* In tracepoint, locations are _never_ duplicated, so
11541 should_be_inserted is equivalent to
11542 unduplicated_should_be_inserted. */
11543 if (!should_be_inserted (bl) || bl->inserted)
11544 continue;
11545
11546 switch_to_program_space_and_thread (bl->pspace);
11547
11548 target_download_tracepoint (bl);
11549
11550 bl->inserted = 1;
11551 bp_location_downloaded = 1;
11552 }
11553 t = (struct tracepoint *) b;
11554 t->number_on_target = b->number;
11555 if (bp_location_downloaded)
11556 gdb::observers::breakpoint_modified.notify (b);
11557 }
11558 }
11559
11560 /* Swap the insertion/duplication state between two locations. */
11561
11562 static void
11563 swap_insertion (struct bp_location *left, struct bp_location *right)
11564 {
11565 const int left_inserted = left->inserted;
11566 const int left_duplicate = left->duplicate;
11567 const int left_needs_update = left->needs_update;
11568 const struct bp_target_info left_target_info = left->target_info;
11569
11570 /* Locations of tracepoints can never be duplicated. */
11571 if (is_tracepoint (left->owner))
11572 gdb_assert (!left->duplicate);
11573 if (is_tracepoint (right->owner))
11574 gdb_assert (!right->duplicate);
11575
11576 left->inserted = right->inserted;
11577 left->duplicate = right->duplicate;
11578 left->needs_update = right->needs_update;
11579 left->target_info = right->target_info;
11580 right->inserted = left_inserted;
11581 right->duplicate = left_duplicate;
11582 right->needs_update = left_needs_update;
11583 right->target_info = left_target_info;
11584 }
11585
11586 /* Force the re-insertion of the locations at ADDRESS. This is called
11587 once a new/deleted/modified duplicate location is found and we are evaluating
11588 conditions on the target's side. Such conditions need to be updated on
11589 the target. */
11590
11591 static void
11592 force_breakpoint_reinsertion (struct bp_location *bl)
11593 {
11594 struct bp_location **locp = NULL, **loc2p;
11595 struct bp_location *loc;
11596 CORE_ADDR address = 0;
11597 int pspace_num;
11598
11599 address = bl->address;
11600 pspace_num = bl->pspace->num;
11601
11602 /* This is only meaningful if the target is
11603 evaluating conditions and if the user has
11604 opted for condition evaluation on the target's
11605 side. */
11606 if (gdb_evaluates_breakpoint_condition_p ()
11607 || !target_supports_evaluation_of_breakpoint_conditions ())
11608 return;
11609
11610 /* Flag all breakpoint locations with this address and
11611 the same program space as the location
11612 as "its condition has changed". We need to
11613 update the conditions on the target's side. */
11614 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11615 {
11616 loc = *loc2p;
11617
11618 if (!is_breakpoint (loc->owner)
11619 || pspace_num != loc->pspace->num)
11620 continue;
11621
11622 /* Flag the location appropriately. We use a different state to
11623 let everyone know that we already updated the set of locations
11624 with addr bl->address and program space bl->pspace. This is so
11625 we don't have to keep calling these functions just to mark locations
11626 that have already been marked. */
11627 loc->condition_changed = condition_updated;
11628
11629 /* Free the agent expression bytecode as well. We will compute
11630 it later on. */
11631 loc->cond_bytecode.reset ();
11632 }
11633 }
11634 /* Called whether new breakpoints are created, or existing breakpoints
11635 deleted, to update the global location list and recompute which
11636 locations are duplicate of which.
11637
11638 The INSERT_MODE flag determines whether locations may not, may, or
11639 shall be inserted now. See 'enum ugll_insert_mode' for more
11640 info. */
11641
11642 static void
11643 update_global_location_list (enum ugll_insert_mode insert_mode)
11644 {
11645 struct breakpoint *b;
11646 struct bp_location **locp, *loc;
11647 /* Last breakpoint location address that was marked for update. */
11648 CORE_ADDR last_addr = 0;
11649 /* Last breakpoint location program space that was marked for update. */
11650 int last_pspace_num = -1;
11651
11652 /* Used in the duplicates detection below. When iterating over all
11653 bp_locations, points to the first bp_location of a given address.
11654 Breakpoints and watchpoints of different types are never
11655 duplicates of each other. Keep one pointer for each type of
11656 breakpoint/watchpoint, so we only need to loop over all locations
11657 once. */
11658 struct bp_location *bp_loc_first; /* breakpoint */
11659 struct bp_location *wp_loc_first; /* hardware watchpoint */
11660 struct bp_location *awp_loc_first; /* access watchpoint */
11661 struct bp_location *rwp_loc_first; /* read watchpoint */
11662
11663 /* Saved former bp_locations array which we compare against the newly
11664 built bp_locations from the current state of ALL_BREAKPOINTS. */
11665 struct bp_location **old_locp;
11666 unsigned old_locations_count;
11667 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11668
11669 old_locations_count = bp_locations_count;
11670 bp_locations = NULL;
11671 bp_locations_count = 0;
11672
11673 ALL_BREAKPOINTS (b)
11674 for (loc = b->loc; loc; loc = loc->next)
11675 bp_locations_count++;
11676
11677 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11678 locp = bp_locations;
11679 ALL_BREAKPOINTS (b)
11680 for (loc = b->loc; loc; loc = loc->next)
11681 *locp++ = loc;
11682 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11683 bp_locations_compare);
11684
11685 bp_locations_target_extensions_update ();
11686
11687 /* Identify bp_location instances that are no longer present in the
11688 new list, and therefore should be freed. Note that it's not
11689 necessary that those locations should be removed from inferior --
11690 if there's another location at the same address (previously
11691 marked as duplicate), we don't need to remove/insert the
11692 location.
11693
11694 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11695 and former bp_location array state respectively. */
11696
11697 locp = bp_locations;
11698 for (old_locp = old_locations.get ();
11699 old_locp < old_locations.get () + old_locations_count;
11700 old_locp++)
11701 {
11702 struct bp_location *old_loc = *old_locp;
11703 struct bp_location **loc2p;
11704
11705 /* Tells if 'old_loc' is found among the new locations. If
11706 not, we have to free it. */
11707 int found_object = 0;
11708 /* Tells if the location should remain inserted in the target. */
11709 int keep_in_target = 0;
11710 int removed = 0;
11711
11712 /* Skip LOCP entries which will definitely never be needed.
11713 Stop either at or being the one matching OLD_LOC. */
11714 while (locp < bp_locations + bp_locations_count
11715 && (*locp)->address < old_loc->address)
11716 locp++;
11717
11718 for (loc2p = locp;
11719 (loc2p < bp_locations + bp_locations_count
11720 && (*loc2p)->address == old_loc->address);
11721 loc2p++)
11722 {
11723 /* Check if this is a new/duplicated location or a duplicated
11724 location that had its condition modified. If so, we want to send
11725 its condition to the target if evaluation of conditions is taking
11726 place there. */
11727 if ((*loc2p)->condition_changed == condition_modified
11728 && (last_addr != old_loc->address
11729 || last_pspace_num != old_loc->pspace->num))
11730 {
11731 force_breakpoint_reinsertion (*loc2p);
11732 last_pspace_num = old_loc->pspace->num;
11733 }
11734
11735 if (*loc2p == old_loc)
11736 found_object = 1;
11737 }
11738
11739 /* We have already handled this address, update it so that we don't
11740 have to go through updates again. */
11741 last_addr = old_loc->address;
11742
11743 /* Target-side condition evaluation: Handle deleted locations. */
11744 if (!found_object)
11745 force_breakpoint_reinsertion (old_loc);
11746
11747 /* If this location is no longer present, and inserted, look if
11748 there's maybe a new location at the same address. If so,
11749 mark that one inserted, and don't remove this one. This is
11750 needed so that we don't have a time window where a breakpoint
11751 at certain location is not inserted. */
11752
11753 if (old_loc->inserted)
11754 {
11755 /* If the location is inserted now, we might have to remove
11756 it. */
11757
11758 if (found_object && should_be_inserted (old_loc))
11759 {
11760 /* The location is still present in the location list,
11761 and still should be inserted. Don't do anything. */
11762 keep_in_target = 1;
11763 }
11764 else
11765 {
11766 /* This location still exists, but it won't be kept in the
11767 target since it may have been disabled. We proceed to
11768 remove its target-side condition. */
11769
11770 /* The location is either no longer present, or got
11771 disabled. See if there's another location at the
11772 same address, in which case we don't need to remove
11773 this one from the target. */
11774
11775 /* OLD_LOC comes from existing struct breakpoint. */
11776 if (bl_address_is_meaningful (old_loc))
11777 {
11778 for (loc2p = locp;
11779 (loc2p < bp_locations + bp_locations_count
11780 && (*loc2p)->address == old_loc->address);
11781 loc2p++)
11782 {
11783 struct bp_location *loc2 = *loc2p;
11784
11785 if (breakpoint_locations_match (loc2, old_loc))
11786 {
11787 /* Read watchpoint locations are switched to
11788 access watchpoints, if the former are not
11789 supported, but the latter are. */
11790 if (is_hardware_watchpoint (old_loc->owner))
11791 {
11792 gdb_assert (is_hardware_watchpoint (loc2->owner));
11793 loc2->watchpoint_type = old_loc->watchpoint_type;
11794 }
11795
11796 /* loc2 is a duplicated location. We need to check
11797 if it should be inserted in case it will be
11798 unduplicated. */
11799 if (loc2 != old_loc
11800 && unduplicated_should_be_inserted (loc2))
11801 {
11802 swap_insertion (old_loc, loc2);
11803 keep_in_target = 1;
11804 break;
11805 }
11806 }
11807 }
11808 }
11809 }
11810
11811 if (!keep_in_target)
11812 {
11813 if (remove_breakpoint (old_loc))
11814 {
11815 /* This is just about all we can do. We could keep
11816 this location on the global list, and try to
11817 remove it next time, but there's no particular
11818 reason why we will succeed next time.
11819
11820 Note that at this point, old_loc->owner is still
11821 valid, as delete_breakpoint frees the breakpoint
11822 only after calling us. */
11823 printf_filtered (_("warning: Error removing "
11824 "breakpoint %d\n"),
11825 old_loc->owner->number);
11826 }
11827 removed = 1;
11828 }
11829 }
11830
11831 if (!found_object)
11832 {
11833 if (removed && target_is_non_stop_p ()
11834 && need_moribund_for_location_type (old_loc))
11835 {
11836 /* This location was removed from the target. In
11837 non-stop mode, a race condition is possible where
11838 we've removed a breakpoint, but stop events for that
11839 breakpoint are already queued and will arrive later.
11840 We apply an heuristic to be able to distinguish such
11841 SIGTRAPs from other random SIGTRAPs: we keep this
11842 breakpoint location for a bit, and will retire it
11843 after we see some number of events. The theory here
11844 is that reporting of events should, "on the average",
11845 be fair, so after a while we'll see events from all
11846 threads that have anything of interest, and no longer
11847 need to keep this breakpoint location around. We
11848 don't hold locations forever so to reduce chances of
11849 mistaking a non-breakpoint SIGTRAP for a breakpoint
11850 SIGTRAP.
11851
11852 The heuristic failing can be disastrous on
11853 decr_pc_after_break targets.
11854
11855 On decr_pc_after_break targets, like e.g., x86-linux,
11856 if we fail to recognize a late breakpoint SIGTRAP,
11857 because events_till_retirement has reached 0 too
11858 soon, we'll fail to do the PC adjustment, and report
11859 a random SIGTRAP to the user. When the user resumes
11860 the inferior, it will most likely immediately crash
11861 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11862 corrupted, because of being resumed e.g., in the
11863 middle of a multi-byte instruction, or skipped a
11864 one-byte instruction. This was actually seen happen
11865 on native x86-linux, and should be less rare on
11866 targets that do not support new thread events, like
11867 remote, due to the heuristic depending on
11868 thread_count.
11869
11870 Mistaking a random SIGTRAP for a breakpoint trap
11871 causes similar symptoms (PC adjustment applied when
11872 it shouldn't), but then again, playing with SIGTRAPs
11873 behind the debugger's back is asking for trouble.
11874
11875 Since hardware watchpoint traps are always
11876 distinguishable from other traps, so we don't need to
11877 apply keep hardware watchpoint moribund locations
11878 around. We simply always ignore hardware watchpoint
11879 traps we can no longer explain. */
11880
11881 old_loc->events_till_retirement = 3 * (thread_count () + 1);
11882 old_loc->owner = NULL;
11883
11884 moribund_locations.push_back (old_loc);
11885 }
11886 else
11887 {
11888 old_loc->owner = NULL;
11889 decref_bp_location (&old_loc);
11890 }
11891 }
11892 }
11893
11894 /* Rescan breakpoints at the same address and section, marking the
11895 first one as "first" and any others as "duplicates". This is so
11896 that the bpt instruction is only inserted once. If we have a
11897 permanent breakpoint at the same place as BPT, make that one the
11898 official one, and the rest as duplicates. Permanent breakpoints
11899 are sorted first for the same address.
11900
11901 Do the same for hardware watchpoints, but also considering the
11902 watchpoint's type (regular/access/read) and length. */
11903
11904 bp_loc_first = NULL;
11905 wp_loc_first = NULL;
11906 awp_loc_first = NULL;
11907 rwp_loc_first = NULL;
11908 ALL_BP_LOCATIONS (loc, locp)
11909 {
11910 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11911 non-NULL. */
11912 struct bp_location **loc_first_p;
11913 b = loc->owner;
11914
11915 if (!unduplicated_should_be_inserted (loc)
11916 || !bl_address_is_meaningful (loc)
11917 /* Don't detect duplicate for tracepoint locations because they are
11918 never duplicated. See the comments in field `duplicate' of
11919 `struct bp_location'. */
11920 || is_tracepoint (b))
11921 {
11922 /* Clear the condition modification flag. */
11923 loc->condition_changed = condition_unchanged;
11924 continue;
11925 }
11926
11927 if (b->type == bp_hardware_watchpoint)
11928 loc_first_p = &wp_loc_first;
11929 else if (b->type == bp_read_watchpoint)
11930 loc_first_p = &rwp_loc_first;
11931 else if (b->type == bp_access_watchpoint)
11932 loc_first_p = &awp_loc_first;
11933 else
11934 loc_first_p = &bp_loc_first;
11935
11936 if (*loc_first_p == NULL
11937 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11938 || !breakpoint_locations_match (loc, *loc_first_p))
11939 {
11940 *loc_first_p = loc;
11941 loc->duplicate = 0;
11942
11943 if (is_breakpoint (loc->owner) && loc->condition_changed)
11944 {
11945 loc->needs_update = 1;
11946 /* Clear the condition modification flag. */
11947 loc->condition_changed = condition_unchanged;
11948 }
11949 continue;
11950 }
11951
11952
11953 /* This and the above ensure the invariant that the first location
11954 is not duplicated, and is the inserted one.
11955 All following are marked as duplicated, and are not inserted. */
11956 if (loc->inserted)
11957 swap_insertion (loc, *loc_first_p);
11958 loc->duplicate = 1;
11959
11960 /* Clear the condition modification flag. */
11961 loc->condition_changed = condition_unchanged;
11962 }
11963
11964 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11965 {
11966 if (insert_mode != UGLL_DONT_INSERT)
11967 insert_breakpoint_locations ();
11968 else
11969 {
11970 /* Even though the caller told us to not insert new
11971 locations, we may still need to update conditions on the
11972 target's side of breakpoints that were already inserted
11973 if the target is evaluating breakpoint conditions. We
11974 only update conditions for locations that are marked
11975 "needs_update". */
11976 update_inserted_breakpoint_locations ();
11977 }
11978 }
11979
11980 if (insert_mode != UGLL_DONT_INSERT)
11981 download_tracepoint_locations ();
11982 }
11983
11984 void
11985 breakpoint_retire_moribund (void)
11986 {
11987 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11988 {
11989 struct bp_location *loc = moribund_locations[ix];
11990 if (--(loc->events_till_retirement) == 0)
11991 {
11992 decref_bp_location (&loc);
11993 unordered_remove (moribund_locations, ix);
11994 --ix;
11995 }
11996 }
11997 }
11998
11999 static void
12000 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12001 {
12002
12003 try
12004 {
12005 update_global_location_list (insert_mode);
12006 }
12007 catch (const gdb_exception_error &e)
12008 {
12009 }
12010 }
12011
12012 /* Clear BKP from a BPS. */
12013
12014 static void
12015 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12016 {
12017 bpstat bs;
12018
12019 for (bs = bps; bs; bs = bs->next)
12020 if (bs->breakpoint_at == bpt)
12021 {
12022 bs->breakpoint_at = NULL;
12023 bs->old_val = NULL;
12024 /* bs->commands will be freed later. */
12025 }
12026 }
12027
12028 /* Callback for iterate_over_threads. */
12029 static int
12030 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12031 {
12032 struct breakpoint *bpt = (struct breakpoint *) data;
12033
12034 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12035 return 0;
12036 }
12037
12038 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12039 callbacks. */
12040
12041 static void
12042 say_where (struct breakpoint *b)
12043 {
12044 struct value_print_options opts;
12045
12046 get_user_print_options (&opts);
12047
12048 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12049 single string. */
12050 if (b->loc == NULL)
12051 {
12052 /* For pending locations, the output differs slightly based
12053 on b->extra_string. If this is non-NULL, it contains either
12054 a condition or dprintf arguments. */
12055 if (b->extra_string == NULL)
12056 {
12057 printf_filtered (_(" (%s) pending."),
12058 event_location_to_string (b->location.get ()));
12059 }
12060 else if (b->type == bp_dprintf)
12061 {
12062 printf_filtered (_(" (%s,%s) pending."),
12063 event_location_to_string (b->location.get ()),
12064 b->extra_string);
12065 }
12066 else
12067 {
12068 printf_filtered (_(" (%s %s) pending."),
12069 event_location_to_string (b->location.get ()),
12070 b->extra_string);
12071 }
12072 }
12073 else
12074 {
12075 if (opts.addressprint || b->loc->symtab == NULL)
12076 printf_filtered (" at %ps",
12077 styled_string (address_style.style (),
12078 paddress (b->loc->gdbarch,
12079 b->loc->address)));
12080 if (b->loc->symtab != NULL)
12081 {
12082 /* If there is a single location, we can print the location
12083 more nicely. */
12084 if (b->loc->next == NULL)
12085 {
12086 const char *filename
12087 = symtab_to_filename_for_display (b->loc->symtab);
12088 printf_filtered (": file %ps, line %d.",
12089 styled_string (file_name_style.style (),
12090 filename),
12091 b->loc->line_number);
12092 }
12093 else
12094 /* This is not ideal, but each location may have a
12095 different file name, and this at least reflects the
12096 real situation somewhat. */
12097 printf_filtered (": %s.",
12098 event_location_to_string (b->location.get ()));
12099 }
12100
12101 if (b->loc->next)
12102 {
12103 struct bp_location *loc = b->loc;
12104 int n = 0;
12105 for (; loc; loc = loc->next)
12106 ++n;
12107 printf_filtered (" (%d locations)", n);
12108 }
12109 }
12110 }
12111
12112 bp_location::~bp_location ()
12113 {
12114 xfree (function_name);
12115 }
12116
12117 /* Destructor for the breakpoint base class. */
12118
12119 breakpoint::~breakpoint ()
12120 {
12121 xfree (this->cond_string);
12122 xfree (this->extra_string);
12123 }
12124
12125 static struct bp_location *
12126 base_breakpoint_allocate_location (struct breakpoint *self)
12127 {
12128 return new bp_location (self);
12129 }
12130
12131 static void
12132 base_breakpoint_re_set (struct breakpoint *b)
12133 {
12134 /* Nothing to re-set. */
12135 }
12136
12137 #define internal_error_pure_virtual_called() \
12138 gdb_assert_not_reached ("pure virtual function called")
12139
12140 static int
12141 base_breakpoint_insert_location (struct bp_location *bl)
12142 {
12143 internal_error_pure_virtual_called ();
12144 }
12145
12146 static int
12147 base_breakpoint_remove_location (struct bp_location *bl,
12148 enum remove_bp_reason reason)
12149 {
12150 internal_error_pure_virtual_called ();
12151 }
12152
12153 static int
12154 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12155 const address_space *aspace,
12156 CORE_ADDR bp_addr,
12157 const struct target_waitstatus *ws)
12158 {
12159 internal_error_pure_virtual_called ();
12160 }
12161
12162 static void
12163 base_breakpoint_check_status (bpstat bs)
12164 {
12165 /* Always stop. */
12166 }
12167
12168 /* A "works_in_software_mode" breakpoint_ops method that just internal
12169 errors. */
12170
12171 static int
12172 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12173 {
12174 internal_error_pure_virtual_called ();
12175 }
12176
12177 /* A "resources_needed" breakpoint_ops method that just internal
12178 errors. */
12179
12180 static int
12181 base_breakpoint_resources_needed (const struct bp_location *bl)
12182 {
12183 internal_error_pure_virtual_called ();
12184 }
12185
12186 static enum print_stop_action
12187 base_breakpoint_print_it (bpstat bs)
12188 {
12189 internal_error_pure_virtual_called ();
12190 }
12191
12192 static void
12193 base_breakpoint_print_one_detail (const struct breakpoint *self,
12194 struct ui_out *uiout)
12195 {
12196 /* nothing */
12197 }
12198
12199 static void
12200 base_breakpoint_print_mention (struct breakpoint *b)
12201 {
12202 internal_error_pure_virtual_called ();
12203 }
12204
12205 static void
12206 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12207 {
12208 internal_error_pure_virtual_called ();
12209 }
12210
12211 static void
12212 base_breakpoint_create_sals_from_location
12213 (const struct event_location *location,
12214 struct linespec_result *canonical,
12215 enum bptype type_wanted)
12216 {
12217 internal_error_pure_virtual_called ();
12218 }
12219
12220 static void
12221 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12222 struct linespec_result *c,
12223 gdb::unique_xmalloc_ptr<char> cond_string,
12224 gdb::unique_xmalloc_ptr<char> extra_string,
12225 enum bptype type_wanted,
12226 enum bpdisp disposition,
12227 int thread,
12228 int task, int ignore_count,
12229 const struct breakpoint_ops *o,
12230 int from_tty, int enabled,
12231 int internal, unsigned flags)
12232 {
12233 internal_error_pure_virtual_called ();
12234 }
12235
12236 static std::vector<symtab_and_line>
12237 base_breakpoint_decode_location (struct breakpoint *b,
12238 const struct event_location *location,
12239 struct program_space *search_pspace)
12240 {
12241 internal_error_pure_virtual_called ();
12242 }
12243
12244 /* The default 'explains_signal' method. */
12245
12246 static int
12247 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12248 {
12249 return 1;
12250 }
12251
12252 /* The default "after_condition_true" method. */
12253
12254 static void
12255 base_breakpoint_after_condition_true (struct bpstats *bs)
12256 {
12257 /* Nothing to do. */
12258 }
12259
12260 struct breakpoint_ops base_breakpoint_ops =
12261 {
12262 base_breakpoint_allocate_location,
12263 base_breakpoint_re_set,
12264 base_breakpoint_insert_location,
12265 base_breakpoint_remove_location,
12266 base_breakpoint_breakpoint_hit,
12267 base_breakpoint_check_status,
12268 base_breakpoint_resources_needed,
12269 base_breakpoint_works_in_software_mode,
12270 base_breakpoint_print_it,
12271 NULL,
12272 base_breakpoint_print_one_detail,
12273 base_breakpoint_print_mention,
12274 base_breakpoint_print_recreate,
12275 base_breakpoint_create_sals_from_location,
12276 base_breakpoint_create_breakpoints_sal,
12277 base_breakpoint_decode_location,
12278 base_breakpoint_explains_signal,
12279 base_breakpoint_after_condition_true,
12280 };
12281
12282 /* Default breakpoint_ops methods. */
12283
12284 static void
12285 bkpt_re_set (struct breakpoint *b)
12286 {
12287 /* FIXME: is this still reachable? */
12288 if (breakpoint_event_location_empty_p (b))
12289 {
12290 /* Anything without a location can't be re-set. */
12291 delete_breakpoint (b);
12292 return;
12293 }
12294
12295 breakpoint_re_set_default (b);
12296 }
12297
12298 static int
12299 bkpt_insert_location (struct bp_location *bl)
12300 {
12301 CORE_ADDR addr = bl->target_info.reqstd_address;
12302
12303 bl->target_info.kind = breakpoint_kind (bl, &addr);
12304 bl->target_info.placed_address = addr;
12305
12306 if (bl->loc_type == bp_loc_hardware_breakpoint)
12307 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12308 else
12309 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12310 }
12311
12312 static int
12313 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12314 {
12315 if (bl->loc_type == bp_loc_hardware_breakpoint)
12316 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12317 else
12318 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12319 }
12320
12321 static int
12322 bkpt_breakpoint_hit (const struct bp_location *bl,
12323 const address_space *aspace, CORE_ADDR bp_addr,
12324 const struct target_waitstatus *ws)
12325 {
12326 if (ws->kind != TARGET_WAITKIND_STOPPED
12327 || ws->value.sig != GDB_SIGNAL_TRAP)
12328 return 0;
12329
12330 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12331 aspace, bp_addr))
12332 return 0;
12333
12334 if (overlay_debugging /* unmapped overlay section */
12335 && section_is_overlay (bl->section)
12336 && !section_is_mapped (bl->section))
12337 return 0;
12338
12339 return 1;
12340 }
12341
12342 static int
12343 dprintf_breakpoint_hit (const struct bp_location *bl,
12344 const address_space *aspace, CORE_ADDR bp_addr,
12345 const struct target_waitstatus *ws)
12346 {
12347 if (dprintf_style == dprintf_style_agent
12348 && target_can_run_breakpoint_commands ())
12349 {
12350 /* An agent-style dprintf never causes a stop. If we see a trap
12351 for this address it must be for a breakpoint that happens to
12352 be set at the same address. */
12353 return 0;
12354 }
12355
12356 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12357 }
12358
12359 static int
12360 bkpt_resources_needed (const struct bp_location *bl)
12361 {
12362 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12363
12364 return 1;
12365 }
12366
12367 static enum print_stop_action
12368 bkpt_print_it (bpstat bs)
12369 {
12370 struct breakpoint *b;
12371 const struct bp_location *bl;
12372 int bp_temp;
12373 struct ui_out *uiout = current_uiout;
12374
12375 gdb_assert (bs->bp_location_at != NULL);
12376
12377 bl = bs->bp_location_at;
12378 b = bs->breakpoint_at;
12379
12380 bp_temp = b->disposition == disp_del;
12381 if (bl->address != bl->requested_address)
12382 breakpoint_adjustment_warning (bl->requested_address,
12383 bl->address,
12384 b->number, 1);
12385 annotate_breakpoint (b->number);
12386 maybe_print_thread_hit_breakpoint (uiout);
12387
12388 if (uiout->is_mi_like_p ())
12389 {
12390 uiout->field_string ("reason",
12391 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12392 uiout->field_string ("disp", bpdisp_text (b->disposition));
12393 }
12394 if (bp_temp)
12395 uiout->message ("Temporary breakpoint %pF, ",
12396 signed_field ("bkptno", b->number));
12397 else
12398 uiout->message ("Breakpoint %pF, ",
12399 signed_field ("bkptno", b->number));
12400
12401 return PRINT_SRC_AND_LOC;
12402 }
12403
12404 static void
12405 bkpt_print_mention (struct breakpoint *b)
12406 {
12407 if (current_uiout->is_mi_like_p ())
12408 return;
12409
12410 switch (b->type)
12411 {
12412 case bp_breakpoint:
12413 case bp_gnu_ifunc_resolver:
12414 if (b->disposition == disp_del)
12415 printf_filtered (_("Temporary breakpoint"));
12416 else
12417 printf_filtered (_("Breakpoint"));
12418 printf_filtered (_(" %d"), b->number);
12419 if (b->type == bp_gnu_ifunc_resolver)
12420 printf_filtered (_(" at gnu-indirect-function resolver"));
12421 break;
12422 case bp_hardware_breakpoint:
12423 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12424 break;
12425 case bp_dprintf:
12426 printf_filtered (_("Dprintf %d"), b->number);
12427 break;
12428 }
12429
12430 say_where (b);
12431 }
12432
12433 static void
12434 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12435 {
12436 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12437 fprintf_unfiltered (fp, "tbreak");
12438 else if (tp->type == bp_breakpoint)
12439 fprintf_unfiltered (fp, "break");
12440 else if (tp->type == bp_hardware_breakpoint
12441 && tp->disposition == disp_del)
12442 fprintf_unfiltered (fp, "thbreak");
12443 else if (tp->type == bp_hardware_breakpoint)
12444 fprintf_unfiltered (fp, "hbreak");
12445 else
12446 internal_error (__FILE__, __LINE__,
12447 _("unhandled breakpoint type %d"), (int) tp->type);
12448
12449 fprintf_unfiltered (fp, " %s",
12450 event_location_to_string (tp->location.get ()));
12451
12452 /* Print out extra_string if this breakpoint is pending. It might
12453 contain, for example, conditions that were set by the user. */
12454 if (tp->loc == NULL && tp->extra_string != NULL)
12455 fprintf_unfiltered (fp, " %s", tp->extra_string);
12456
12457 print_recreate_thread (tp, fp);
12458 }
12459
12460 static void
12461 bkpt_create_sals_from_location (const struct event_location *location,
12462 struct linespec_result *canonical,
12463 enum bptype type_wanted)
12464 {
12465 create_sals_from_location_default (location, canonical, type_wanted);
12466 }
12467
12468 static void
12469 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12470 struct linespec_result *canonical,
12471 gdb::unique_xmalloc_ptr<char> cond_string,
12472 gdb::unique_xmalloc_ptr<char> extra_string,
12473 enum bptype type_wanted,
12474 enum bpdisp disposition,
12475 int thread,
12476 int task, int ignore_count,
12477 const struct breakpoint_ops *ops,
12478 int from_tty, int enabled,
12479 int internal, unsigned flags)
12480 {
12481 create_breakpoints_sal_default (gdbarch, canonical,
12482 std::move (cond_string),
12483 std::move (extra_string),
12484 type_wanted,
12485 disposition, thread, task,
12486 ignore_count, ops, from_tty,
12487 enabled, internal, flags);
12488 }
12489
12490 static std::vector<symtab_and_line>
12491 bkpt_decode_location (struct breakpoint *b,
12492 const struct event_location *location,
12493 struct program_space *search_pspace)
12494 {
12495 return decode_location_default (b, location, search_pspace);
12496 }
12497
12498 /* Virtual table for internal breakpoints. */
12499
12500 static void
12501 internal_bkpt_re_set (struct breakpoint *b)
12502 {
12503 switch (b->type)
12504 {
12505 /* Delete overlay event and longjmp master breakpoints; they
12506 will be reset later by breakpoint_re_set. */
12507 case bp_overlay_event:
12508 case bp_longjmp_master:
12509 case bp_std_terminate_master:
12510 case bp_exception_master:
12511 delete_breakpoint (b);
12512 break;
12513
12514 /* This breakpoint is special, it's set up when the inferior
12515 starts and we really don't want to touch it. */
12516 case bp_shlib_event:
12517
12518 /* Like bp_shlib_event, this breakpoint type is special. Once
12519 it is set up, we do not want to touch it. */
12520 case bp_thread_event:
12521 break;
12522 }
12523 }
12524
12525 static void
12526 internal_bkpt_check_status (bpstat bs)
12527 {
12528 if (bs->breakpoint_at->type == bp_shlib_event)
12529 {
12530 /* If requested, stop when the dynamic linker notifies GDB of
12531 events. This allows the user to get control and place
12532 breakpoints in initializer routines for dynamically loaded
12533 objects (among other things). */
12534 bs->stop = stop_on_solib_events;
12535 bs->print = stop_on_solib_events;
12536 }
12537 else
12538 bs->stop = 0;
12539 }
12540
12541 static enum print_stop_action
12542 internal_bkpt_print_it (bpstat bs)
12543 {
12544 struct breakpoint *b;
12545
12546 b = bs->breakpoint_at;
12547
12548 switch (b->type)
12549 {
12550 case bp_shlib_event:
12551 /* Did we stop because the user set the stop_on_solib_events
12552 variable? (If so, we report this as a generic, "Stopped due
12553 to shlib event" message.) */
12554 print_solib_event (0);
12555 break;
12556
12557 case bp_thread_event:
12558 /* Not sure how we will get here.
12559 GDB should not stop for these breakpoints. */
12560 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12561 break;
12562
12563 case bp_overlay_event:
12564 /* By analogy with the thread event, GDB should not stop for these. */
12565 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12566 break;
12567
12568 case bp_longjmp_master:
12569 /* These should never be enabled. */
12570 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12571 break;
12572
12573 case bp_std_terminate_master:
12574 /* These should never be enabled. */
12575 printf_filtered (_("std::terminate Master Breakpoint: "
12576 "gdb should not stop!\n"));
12577 break;
12578
12579 case bp_exception_master:
12580 /* These should never be enabled. */
12581 printf_filtered (_("Exception Master Breakpoint: "
12582 "gdb should not stop!\n"));
12583 break;
12584 }
12585
12586 return PRINT_NOTHING;
12587 }
12588
12589 static void
12590 internal_bkpt_print_mention (struct breakpoint *b)
12591 {
12592 /* Nothing to mention. These breakpoints are internal. */
12593 }
12594
12595 /* Virtual table for momentary breakpoints */
12596
12597 static void
12598 momentary_bkpt_re_set (struct breakpoint *b)
12599 {
12600 /* Keep temporary breakpoints, which can be encountered when we step
12601 over a dlopen call and solib_add is resetting the breakpoints.
12602 Otherwise these should have been blown away via the cleanup chain
12603 or by breakpoint_init_inferior when we rerun the executable. */
12604 }
12605
12606 static void
12607 momentary_bkpt_check_status (bpstat bs)
12608 {
12609 /* Nothing. The point of these breakpoints is causing a stop. */
12610 }
12611
12612 static enum print_stop_action
12613 momentary_bkpt_print_it (bpstat bs)
12614 {
12615 return PRINT_UNKNOWN;
12616 }
12617
12618 static void
12619 momentary_bkpt_print_mention (struct breakpoint *b)
12620 {
12621 /* Nothing to mention. These breakpoints are internal. */
12622 }
12623
12624 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12625
12626 It gets cleared already on the removal of the first one of such placed
12627 breakpoints. This is OK as they get all removed altogether. */
12628
12629 longjmp_breakpoint::~longjmp_breakpoint ()
12630 {
12631 thread_info *tp = find_thread_global_id (this->thread);
12632
12633 if (tp != NULL)
12634 tp->initiating_frame = null_frame_id;
12635 }
12636
12637 /* Specific methods for probe breakpoints. */
12638
12639 static int
12640 bkpt_probe_insert_location (struct bp_location *bl)
12641 {
12642 int v = bkpt_insert_location (bl);
12643
12644 if (v == 0)
12645 {
12646 /* The insertion was successful, now let's set the probe's semaphore
12647 if needed. */
12648 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12649 }
12650
12651 return v;
12652 }
12653
12654 static int
12655 bkpt_probe_remove_location (struct bp_location *bl,
12656 enum remove_bp_reason reason)
12657 {
12658 /* Let's clear the semaphore before removing the location. */
12659 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12660
12661 return bkpt_remove_location (bl, reason);
12662 }
12663
12664 static void
12665 bkpt_probe_create_sals_from_location (const struct event_location *location,
12666 struct linespec_result *canonical,
12667 enum bptype type_wanted)
12668 {
12669 struct linespec_sals lsal;
12670
12671 lsal.sals = parse_probes (location, NULL, canonical);
12672 lsal.canonical
12673 = xstrdup (event_location_to_string (canonical->location.get ()));
12674 canonical->lsals.push_back (std::move (lsal));
12675 }
12676
12677 static std::vector<symtab_and_line>
12678 bkpt_probe_decode_location (struct breakpoint *b,
12679 const struct event_location *location,
12680 struct program_space *search_pspace)
12681 {
12682 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12683 if (sals.empty ())
12684 error (_("probe not found"));
12685 return sals;
12686 }
12687
12688 /* The breakpoint_ops structure to be used in tracepoints. */
12689
12690 static void
12691 tracepoint_re_set (struct breakpoint *b)
12692 {
12693 breakpoint_re_set_default (b);
12694 }
12695
12696 static int
12697 tracepoint_breakpoint_hit (const struct bp_location *bl,
12698 const address_space *aspace, CORE_ADDR bp_addr,
12699 const struct target_waitstatus *ws)
12700 {
12701 /* By definition, the inferior does not report stops at
12702 tracepoints. */
12703 return 0;
12704 }
12705
12706 static void
12707 tracepoint_print_one_detail (const struct breakpoint *self,
12708 struct ui_out *uiout)
12709 {
12710 struct tracepoint *tp = (struct tracepoint *) self;
12711 if (!tp->static_trace_marker_id.empty ())
12712 {
12713 gdb_assert (self->type == bp_static_tracepoint);
12714
12715 uiout->message ("\tmarker id is %pF\n",
12716 string_field ("static-tracepoint-marker-string-id",
12717 tp->static_trace_marker_id.c_str ()));
12718 }
12719 }
12720
12721 static void
12722 tracepoint_print_mention (struct breakpoint *b)
12723 {
12724 if (current_uiout->is_mi_like_p ())
12725 return;
12726
12727 switch (b->type)
12728 {
12729 case bp_tracepoint:
12730 printf_filtered (_("Tracepoint"));
12731 printf_filtered (_(" %d"), b->number);
12732 break;
12733 case bp_fast_tracepoint:
12734 printf_filtered (_("Fast tracepoint"));
12735 printf_filtered (_(" %d"), b->number);
12736 break;
12737 case bp_static_tracepoint:
12738 printf_filtered (_("Static tracepoint"));
12739 printf_filtered (_(" %d"), b->number);
12740 break;
12741 default:
12742 internal_error (__FILE__, __LINE__,
12743 _("unhandled tracepoint type %d"), (int) b->type);
12744 }
12745
12746 say_where (b);
12747 }
12748
12749 static void
12750 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12751 {
12752 struct tracepoint *tp = (struct tracepoint *) self;
12753
12754 if (self->type == bp_fast_tracepoint)
12755 fprintf_unfiltered (fp, "ftrace");
12756 else if (self->type == bp_static_tracepoint)
12757 fprintf_unfiltered (fp, "strace");
12758 else if (self->type == bp_tracepoint)
12759 fprintf_unfiltered (fp, "trace");
12760 else
12761 internal_error (__FILE__, __LINE__,
12762 _("unhandled tracepoint type %d"), (int) self->type);
12763
12764 fprintf_unfiltered (fp, " %s",
12765 event_location_to_string (self->location.get ()));
12766 print_recreate_thread (self, fp);
12767
12768 if (tp->pass_count)
12769 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12770 }
12771
12772 static void
12773 tracepoint_create_sals_from_location (const struct event_location *location,
12774 struct linespec_result *canonical,
12775 enum bptype type_wanted)
12776 {
12777 create_sals_from_location_default (location, canonical, type_wanted);
12778 }
12779
12780 static void
12781 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12782 struct linespec_result *canonical,
12783 gdb::unique_xmalloc_ptr<char> cond_string,
12784 gdb::unique_xmalloc_ptr<char> extra_string,
12785 enum bptype type_wanted,
12786 enum bpdisp disposition,
12787 int thread,
12788 int task, int ignore_count,
12789 const struct breakpoint_ops *ops,
12790 int from_tty, int enabled,
12791 int internal, unsigned flags)
12792 {
12793 create_breakpoints_sal_default (gdbarch, canonical,
12794 std::move (cond_string),
12795 std::move (extra_string),
12796 type_wanted,
12797 disposition, thread, task,
12798 ignore_count, ops, from_tty,
12799 enabled, internal, flags);
12800 }
12801
12802 static std::vector<symtab_and_line>
12803 tracepoint_decode_location (struct breakpoint *b,
12804 const struct event_location *location,
12805 struct program_space *search_pspace)
12806 {
12807 return decode_location_default (b, location, search_pspace);
12808 }
12809
12810 struct breakpoint_ops tracepoint_breakpoint_ops;
12811
12812 /* The breakpoint_ops structure to be use on tracepoints placed in a
12813 static probe. */
12814
12815 static void
12816 tracepoint_probe_create_sals_from_location
12817 (const struct event_location *location,
12818 struct linespec_result *canonical,
12819 enum bptype type_wanted)
12820 {
12821 /* We use the same method for breakpoint on probes. */
12822 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12823 }
12824
12825 static std::vector<symtab_and_line>
12826 tracepoint_probe_decode_location (struct breakpoint *b,
12827 const struct event_location *location,
12828 struct program_space *search_pspace)
12829 {
12830 /* We use the same method for breakpoint on probes. */
12831 return bkpt_probe_decode_location (b, location, search_pspace);
12832 }
12833
12834 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
12835
12836 /* Dprintf breakpoint_ops methods. */
12837
12838 static void
12839 dprintf_re_set (struct breakpoint *b)
12840 {
12841 breakpoint_re_set_default (b);
12842
12843 /* extra_string should never be non-NULL for dprintf. */
12844 gdb_assert (b->extra_string != NULL);
12845
12846 /* 1 - connect to target 1, that can run breakpoint commands.
12847 2 - create a dprintf, which resolves fine.
12848 3 - disconnect from target 1
12849 4 - connect to target 2, that can NOT run breakpoint commands.
12850
12851 After steps #3/#4, you'll want the dprintf command list to
12852 be updated, because target 1 and 2 may well return different
12853 answers for target_can_run_breakpoint_commands().
12854 Given absence of finer grained resetting, we get to do
12855 it all the time. */
12856 if (b->extra_string != NULL)
12857 update_dprintf_command_list (b);
12858 }
12859
12860 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12861
12862 static void
12863 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12864 {
12865 fprintf_unfiltered (fp, "dprintf %s,%s",
12866 event_location_to_string (tp->location.get ()),
12867 tp->extra_string);
12868 print_recreate_thread (tp, fp);
12869 }
12870
12871 /* Implement the "after_condition_true" breakpoint_ops method for
12872 dprintf.
12873
12874 dprintf's are implemented with regular commands in their command
12875 list, but we run the commands here instead of before presenting the
12876 stop to the user, as dprintf's don't actually cause a stop. This
12877 also makes it so that the commands of multiple dprintfs at the same
12878 address are all handled. */
12879
12880 static void
12881 dprintf_after_condition_true (struct bpstats *bs)
12882 {
12883 struct bpstats tmp_bs;
12884 struct bpstats *tmp_bs_p = &tmp_bs;
12885
12886 /* dprintf's never cause a stop. This wasn't set in the
12887 check_status hook instead because that would make the dprintf's
12888 condition not be evaluated. */
12889 bs->stop = 0;
12890
12891 /* Run the command list here. Take ownership of it instead of
12892 copying. We never want these commands to run later in
12893 bpstat_do_actions, if a breakpoint that causes a stop happens to
12894 be set at same address as this dprintf, or even if running the
12895 commands here throws. */
12896 tmp_bs.commands = bs->commands;
12897 bs->commands = NULL;
12898
12899 bpstat_do_actions_1 (&tmp_bs_p);
12900
12901 /* 'tmp_bs.commands' will usually be NULL by now, but
12902 bpstat_do_actions_1 may return early without processing the whole
12903 list. */
12904 }
12905
12906 /* The breakpoint_ops structure to be used on static tracepoints with
12907 markers (`-m'). */
12908
12909 static void
12910 strace_marker_create_sals_from_location (const struct event_location *location,
12911 struct linespec_result *canonical,
12912 enum bptype type_wanted)
12913 {
12914 struct linespec_sals lsal;
12915 const char *arg_start, *arg;
12916
12917 arg = arg_start = get_linespec_location (location)->spec_string;
12918 lsal.sals = decode_static_tracepoint_spec (&arg);
12919
12920 std::string str (arg_start, arg - arg_start);
12921 const char *ptr = str.c_str ();
12922 canonical->location
12923 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12924
12925 lsal.canonical
12926 = xstrdup (event_location_to_string (canonical->location.get ()));
12927 canonical->lsals.push_back (std::move (lsal));
12928 }
12929
12930 static void
12931 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12932 struct linespec_result *canonical,
12933 gdb::unique_xmalloc_ptr<char> cond_string,
12934 gdb::unique_xmalloc_ptr<char> extra_string,
12935 enum bptype type_wanted,
12936 enum bpdisp disposition,
12937 int thread,
12938 int task, int ignore_count,
12939 const struct breakpoint_ops *ops,
12940 int from_tty, int enabled,
12941 int internal, unsigned flags)
12942 {
12943 const linespec_sals &lsal = canonical->lsals[0];
12944
12945 /* If the user is creating a static tracepoint by marker id
12946 (strace -m MARKER_ID), then store the sals index, so that
12947 breakpoint_re_set can try to match up which of the newly
12948 found markers corresponds to this one, and, don't try to
12949 expand multiple locations for each sal, given than SALS
12950 already should contain all sals for MARKER_ID. */
12951
12952 for (size_t i = 0; i < lsal.sals.size (); i++)
12953 {
12954 event_location_up location
12955 = copy_event_location (canonical->location.get ());
12956
12957 std::unique_ptr<tracepoint> tp (new tracepoint ());
12958 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
12959 std::move (location), NULL,
12960 std::move (cond_string),
12961 std::move (extra_string),
12962 type_wanted, disposition,
12963 thread, task, ignore_count, ops,
12964 from_tty, enabled, internal, flags,
12965 canonical->special_display);
12966 /* Given that its possible to have multiple markers with
12967 the same string id, if the user is creating a static
12968 tracepoint by marker id ("strace -m MARKER_ID"), then
12969 store the sals index, so that breakpoint_re_set can
12970 try to match up which of the newly found markers
12971 corresponds to this one */
12972 tp->static_trace_marker_id_idx = i;
12973
12974 install_breakpoint (internal, std::move (tp), 0);
12975 }
12976 }
12977
12978 static std::vector<symtab_and_line>
12979 strace_marker_decode_location (struct breakpoint *b,
12980 const struct event_location *location,
12981 struct program_space *search_pspace)
12982 {
12983 struct tracepoint *tp = (struct tracepoint *) b;
12984 const char *s = get_linespec_location (location)->spec_string;
12985
12986 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12987 if (sals.size () > tp->static_trace_marker_id_idx)
12988 {
12989 sals[0] = sals[tp->static_trace_marker_id_idx];
12990 sals.resize (1);
12991 return sals;
12992 }
12993 else
12994 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
12995 }
12996
12997 static struct breakpoint_ops strace_marker_breakpoint_ops;
12998
12999 static int
13000 strace_marker_p (struct breakpoint *b)
13001 {
13002 return b->ops == &strace_marker_breakpoint_ops;
13003 }
13004
13005 /* Delete a breakpoint and clean up all traces of it in the data
13006 structures. */
13007
13008 void
13009 delete_breakpoint (struct breakpoint *bpt)
13010 {
13011 struct breakpoint *b;
13012
13013 gdb_assert (bpt != NULL);
13014
13015 /* Has this bp already been deleted? This can happen because
13016 multiple lists can hold pointers to bp's. bpstat lists are
13017 especial culprits.
13018
13019 One example of this happening is a watchpoint's scope bp. When
13020 the scope bp triggers, we notice that the watchpoint is out of
13021 scope, and delete it. We also delete its scope bp. But the
13022 scope bp is marked "auto-deleting", and is already on a bpstat.
13023 That bpstat is then checked for auto-deleting bp's, which are
13024 deleted.
13025
13026 A real solution to this problem might involve reference counts in
13027 bp's, and/or giving them pointers back to their referencing
13028 bpstat's, and teaching delete_breakpoint to only free a bp's
13029 storage when no more references were extent. A cheaper bandaid
13030 was chosen. */
13031 if (bpt->type == bp_none)
13032 return;
13033
13034 /* At least avoid this stale reference until the reference counting
13035 of breakpoints gets resolved. */
13036 if (bpt->related_breakpoint != bpt)
13037 {
13038 struct breakpoint *related;
13039 struct watchpoint *w;
13040
13041 if (bpt->type == bp_watchpoint_scope)
13042 w = (struct watchpoint *) bpt->related_breakpoint;
13043 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13044 w = (struct watchpoint *) bpt;
13045 else
13046 w = NULL;
13047 if (w != NULL)
13048 watchpoint_del_at_next_stop (w);
13049
13050 /* Unlink bpt from the bpt->related_breakpoint ring. */
13051 for (related = bpt; related->related_breakpoint != bpt;
13052 related = related->related_breakpoint);
13053 related->related_breakpoint = bpt->related_breakpoint;
13054 bpt->related_breakpoint = bpt;
13055 }
13056
13057 /* watch_command_1 creates a watchpoint but only sets its number if
13058 update_watchpoint succeeds in creating its bp_locations. If there's
13059 a problem in that process, we'll be asked to delete the half-created
13060 watchpoint. In that case, don't announce the deletion. */
13061 if (bpt->number)
13062 gdb::observers::breakpoint_deleted.notify (bpt);
13063
13064 if (breakpoint_chain == bpt)
13065 breakpoint_chain = bpt->next;
13066
13067 ALL_BREAKPOINTS (b)
13068 if (b->next == bpt)
13069 {
13070 b->next = bpt->next;
13071 break;
13072 }
13073
13074 /* Be sure no bpstat's are pointing at the breakpoint after it's
13075 been freed. */
13076 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13077 in all threads for now. Note that we cannot just remove bpstats
13078 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13079 commands are associated with the bpstat; if we remove it here,
13080 then the later call to bpstat_do_actions (&stop_bpstat); in
13081 event-top.c won't do anything, and temporary breakpoints with
13082 commands won't work. */
13083
13084 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13085
13086 /* Now that breakpoint is removed from breakpoint list, update the
13087 global location list. This will remove locations that used to
13088 belong to this breakpoint. Do this before freeing the breakpoint
13089 itself, since remove_breakpoint looks at location's owner. It
13090 might be better design to have location completely
13091 self-contained, but it's not the case now. */
13092 update_global_location_list (UGLL_DONT_INSERT);
13093
13094 /* On the chance that someone will soon try again to delete this
13095 same bp, we mark it as deleted before freeing its storage. */
13096 bpt->type = bp_none;
13097 delete bpt;
13098 }
13099
13100 /* Iterator function to call a user-provided callback function once
13101 for each of B and its related breakpoints. */
13102
13103 static void
13104 iterate_over_related_breakpoints (struct breakpoint *b,
13105 gdb::function_view<void (breakpoint *)> function)
13106 {
13107 struct breakpoint *related;
13108
13109 related = b;
13110 do
13111 {
13112 struct breakpoint *next;
13113
13114 /* FUNCTION may delete RELATED. */
13115 next = related->related_breakpoint;
13116
13117 if (next == related)
13118 {
13119 /* RELATED is the last ring entry. */
13120 function (related);
13121
13122 /* FUNCTION may have deleted it, so we'd never reach back to
13123 B. There's nothing left to do anyway, so just break
13124 out. */
13125 break;
13126 }
13127 else
13128 function (related);
13129
13130 related = next;
13131 }
13132 while (related != b);
13133 }
13134
13135 static void
13136 delete_command (const char *arg, int from_tty)
13137 {
13138 struct breakpoint *b, *b_tmp;
13139
13140 dont_repeat ();
13141
13142 if (arg == 0)
13143 {
13144 int breaks_to_delete = 0;
13145
13146 /* Delete all breakpoints if no argument. Do not delete
13147 internal breakpoints, these have to be deleted with an
13148 explicit breakpoint number argument. */
13149 ALL_BREAKPOINTS (b)
13150 if (user_breakpoint_p (b))
13151 {
13152 breaks_to_delete = 1;
13153 break;
13154 }
13155
13156 /* Ask user only if there are some breakpoints to delete. */
13157 if (!from_tty
13158 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13159 {
13160 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13161 if (user_breakpoint_p (b))
13162 delete_breakpoint (b);
13163 }
13164 }
13165 else
13166 map_breakpoint_numbers
13167 (arg, [&] (breakpoint *br)
13168 {
13169 iterate_over_related_breakpoints (br, delete_breakpoint);
13170 });
13171 }
13172
13173 /* Return true if all locations of B bound to PSPACE are pending. If
13174 PSPACE is NULL, all locations of all program spaces are
13175 considered. */
13176
13177 static int
13178 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13179 {
13180 struct bp_location *loc;
13181
13182 for (loc = b->loc; loc != NULL; loc = loc->next)
13183 if ((pspace == NULL
13184 || loc->pspace == pspace)
13185 && !loc->shlib_disabled
13186 && !loc->pspace->executing_startup)
13187 return 0;
13188 return 1;
13189 }
13190
13191 /* Subroutine of update_breakpoint_locations to simplify it.
13192 Return non-zero if multiple fns in list LOC have the same name.
13193 Null names are ignored. */
13194
13195 static int
13196 ambiguous_names_p (struct bp_location *loc)
13197 {
13198 struct bp_location *l;
13199 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13200 xcalloc, xfree);
13201
13202 for (l = loc; l != NULL; l = l->next)
13203 {
13204 const char **slot;
13205 const char *name = l->function_name;
13206
13207 /* Allow for some names to be NULL, ignore them. */
13208 if (name == NULL)
13209 continue;
13210
13211 slot = (const char **) htab_find_slot (htab, (const void *) name,
13212 INSERT);
13213 /* NOTE: We can assume slot != NULL here because xcalloc never
13214 returns NULL. */
13215 if (*slot != NULL)
13216 {
13217 htab_delete (htab);
13218 return 1;
13219 }
13220 *slot = name;
13221 }
13222
13223 htab_delete (htab);
13224 return 0;
13225 }
13226
13227 /* When symbols change, it probably means the sources changed as well,
13228 and it might mean the static tracepoint markers are no longer at
13229 the same address or line numbers they used to be at last we
13230 checked. Losing your static tracepoints whenever you rebuild is
13231 undesirable. This function tries to resync/rematch gdb static
13232 tracepoints with the markers on the target, for static tracepoints
13233 that have not been set by marker id. Static tracepoint that have
13234 been set by marker id are reset by marker id in breakpoint_re_set.
13235 The heuristic is:
13236
13237 1) For a tracepoint set at a specific address, look for a marker at
13238 the old PC. If one is found there, assume to be the same marker.
13239 If the name / string id of the marker found is different from the
13240 previous known name, assume that means the user renamed the marker
13241 in the sources, and output a warning.
13242
13243 2) For a tracepoint set at a given line number, look for a marker
13244 at the new address of the old line number. If one is found there,
13245 assume to be the same marker. If the name / string id of the
13246 marker found is different from the previous known name, assume that
13247 means the user renamed the marker in the sources, and output a
13248 warning.
13249
13250 3) If a marker is no longer found at the same address or line, it
13251 may mean the marker no longer exists. But it may also just mean
13252 the code changed a bit. Maybe the user added a few lines of code
13253 that made the marker move up or down (in line number terms). Ask
13254 the target for info about the marker with the string id as we knew
13255 it. If found, update line number and address in the matching
13256 static tracepoint. This will get confused if there's more than one
13257 marker with the same ID (possible in UST, although unadvised
13258 precisely because it confuses tools). */
13259
13260 static struct symtab_and_line
13261 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13262 {
13263 struct tracepoint *tp = (struct tracepoint *) b;
13264 struct static_tracepoint_marker marker;
13265 CORE_ADDR pc;
13266
13267 pc = sal.pc;
13268 if (sal.line)
13269 find_line_pc (sal.symtab, sal.line, &pc);
13270
13271 if (target_static_tracepoint_marker_at (pc, &marker))
13272 {
13273 if (tp->static_trace_marker_id != marker.str_id)
13274 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13275 b->number, tp->static_trace_marker_id.c_str (),
13276 marker.str_id.c_str ());
13277
13278 tp->static_trace_marker_id = std::move (marker.str_id);
13279
13280 return sal;
13281 }
13282
13283 /* Old marker wasn't found on target at lineno. Try looking it up
13284 by string ID. */
13285 if (!sal.explicit_pc
13286 && sal.line != 0
13287 && sal.symtab != NULL
13288 && !tp->static_trace_marker_id.empty ())
13289 {
13290 std::vector<static_tracepoint_marker> markers
13291 = target_static_tracepoint_markers_by_strid
13292 (tp->static_trace_marker_id.c_str ());
13293
13294 if (!markers.empty ())
13295 {
13296 struct symbol *sym;
13297 struct static_tracepoint_marker *tpmarker;
13298 struct ui_out *uiout = current_uiout;
13299 struct explicit_location explicit_loc;
13300
13301 tpmarker = &markers[0];
13302
13303 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13304
13305 warning (_("marker for static tracepoint %d (%s) not "
13306 "found at previous line number"),
13307 b->number, tp->static_trace_marker_id.c_str ());
13308
13309 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13310 sym = find_pc_sect_function (tpmarker->address, NULL);
13311 uiout->text ("Now in ");
13312 if (sym)
13313 {
13314 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
13315 function_name_style.style ());
13316 uiout->text (" at ");
13317 }
13318 uiout->field_string ("file",
13319 symtab_to_filename_for_display (sal2.symtab),
13320 file_name_style.style ());
13321 uiout->text (":");
13322
13323 if (uiout->is_mi_like_p ())
13324 {
13325 const char *fullname = symtab_to_fullname (sal2.symtab);
13326
13327 uiout->field_string ("fullname", fullname);
13328 }
13329
13330 uiout->field_signed ("line", sal2.line);
13331 uiout->text ("\n");
13332
13333 b->loc->line_number = sal2.line;
13334 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13335
13336 b->location.reset (NULL);
13337 initialize_explicit_location (&explicit_loc);
13338 explicit_loc.source_filename
13339 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13340 explicit_loc.line_offset.offset = b->loc->line_number;
13341 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13342 b->location = new_explicit_location (&explicit_loc);
13343
13344 /* Might be nice to check if function changed, and warn if
13345 so. */
13346 }
13347 }
13348 return sal;
13349 }
13350
13351 /* Returns 1 iff locations A and B are sufficiently same that
13352 we don't need to report breakpoint as changed. */
13353
13354 static int
13355 locations_are_equal (struct bp_location *a, struct bp_location *b)
13356 {
13357 while (a && b)
13358 {
13359 if (a->address != b->address)
13360 return 0;
13361
13362 if (a->shlib_disabled != b->shlib_disabled)
13363 return 0;
13364
13365 if (a->enabled != b->enabled)
13366 return 0;
13367
13368 a = a->next;
13369 b = b->next;
13370 }
13371
13372 if ((a == NULL) != (b == NULL))
13373 return 0;
13374
13375 return 1;
13376 }
13377
13378 /* Split all locations of B that are bound to PSPACE out of B's
13379 location list to a separate list and return that list's head. If
13380 PSPACE is NULL, hoist out all locations of B. */
13381
13382 static struct bp_location *
13383 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13384 {
13385 struct bp_location head;
13386 struct bp_location *i = b->loc;
13387 struct bp_location **i_link = &b->loc;
13388 struct bp_location *hoisted = &head;
13389
13390 if (pspace == NULL)
13391 {
13392 i = b->loc;
13393 b->loc = NULL;
13394 return i;
13395 }
13396
13397 head.next = NULL;
13398
13399 while (i != NULL)
13400 {
13401 if (i->pspace == pspace)
13402 {
13403 *i_link = i->next;
13404 i->next = NULL;
13405 hoisted->next = i;
13406 hoisted = i;
13407 }
13408 else
13409 i_link = &i->next;
13410 i = *i_link;
13411 }
13412
13413 return head.next;
13414 }
13415
13416 /* Create new breakpoint locations for B (a hardware or software
13417 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13418 zero, then B is a ranged breakpoint. Only recreates locations for
13419 FILTER_PSPACE. Locations of other program spaces are left
13420 untouched. */
13421
13422 void
13423 update_breakpoint_locations (struct breakpoint *b,
13424 struct program_space *filter_pspace,
13425 gdb::array_view<const symtab_and_line> sals,
13426 gdb::array_view<const symtab_and_line> sals_end)
13427 {
13428 struct bp_location *existing_locations;
13429
13430 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13431 {
13432 /* Ranged breakpoints have only one start location and one end
13433 location. */
13434 b->enable_state = bp_disabled;
13435 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13436 "multiple locations found\n"),
13437 b->number);
13438 return;
13439 }
13440
13441 /* If there's no new locations, and all existing locations are
13442 pending, don't do anything. This optimizes the common case where
13443 all locations are in the same shared library, that was unloaded.
13444 We'd like to retain the location, so that when the library is
13445 loaded again, we don't loose the enabled/disabled status of the
13446 individual locations. */
13447 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13448 return;
13449
13450 existing_locations = hoist_existing_locations (b, filter_pspace);
13451
13452 for (const auto &sal : sals)
13453 {
13454 struct bp_location *new_loc;
13455
13456 switch_to_program_space_and_thread (sal.pspace);
13457
13458 new_loc = add_location_to_breakpoint (b, &sal);
13459
13460 /* Reparse conditions, they might contain references to the
13461 old symtab. */
13462 if (b->cond_string != NULL)
13463 {
13464 const char *s;
13465
13466 s = b->cond_string;
13467 try
13468 {
13469 new_loc->cond = parse_exp_1 (&s, sal.pc,
13470 block_for_pc (sal.pc),
13471 0);
13472 }
13473 catch (const gdb_exception_error &e)
13474 {
13475 warning (_("failed to reevaluate condition "
13476 "for breakpoint %d: %s"),
13477 b->number, e.what ());
13478 new_loc->enabled = 0;
13479 }
13480 }
13481
13482 if (!sals_end.empty ())
13483 {
13484 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13485
13486 new_loc->length = end - sals[0].pc + 1;
13487 }
13488 }
13489
13490 /* If possible, carry over 'disable' status from existing
13491 breakpoints. */
13492 {
13493 struct bp_location *e = existing_locations;
13494 /* If there are multiple breakpoints with the same function name,
13495 e.g. for inline functions, comparing function names won't work.
13496 Instead compare pc addresses; this is just a heuristic as things
13497 may have moved, but in practice it gives the correct answer
13498 often enough until a better solution is found. */
13499 int have_ambiguous_names = ambiguous_names_p (b->loc);
13500
13501 for (; e; e = e->next)
13502 {
13503 if (!e->enabled && e->function_name)
13504 {
13505 struct bp_location *l = b->loc;
13506 if (have_ambiguous_names)
13507 {
13508 for (; l; l = l->next)
13509 if (breakpoint_locations_match (e, l))
13510 {
13511 l->enabled = 0;
13512 break;
13513 }
13514 }
13515 else
13516 {
13517 for (; l; l = l->next)
13518 if (l->function_name
13519 && strcmp (e->function_name, l->function_name) == 0)
13520 {
13521 l->enabled = 0;
13522 break;
13523 }
13524 }
13525 }
13526 }
13527 }
13528
13529 if (!locations_are_equal (existing_locations, b->loc))
13530 gdb::observers::breakpoint_modified.notify (b);
13531 }
13532
13533 /* Find the SaL locations corresponding to the given LOCATION.
13534 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13535
13536 static std::vector<symtab_and_line>
13537 location_to_sals (struct breakpoint *b, struct event_location *location,
13538 struct program_space *search_pspace, int *found)
13539 {
13540 struct gdb_exception exception;
13541
13542 gdb_assert (b->ops != NULL);
13543
13544 std::vector<symtab_and_line> sals;
13545
13546 try
13547 {
13548 sals = b->ops->decode_location (b, location, search_pspace);
13549 }
13550 catch (gdb_exception_error &e)
13551 {
13552 int not_found_and_ok = 0;
13553
13554 /* For pending breakpoints, it's expected that parsing will
13555 fail until the right shared library is loaded. User has
13556 already told to create pending breakpoints and don't need
13557 extra messages. If breakpoint is in bp_shlib_disabled
13558 state, then user already saw the message about that
13559 breakpoint being disabled, and don't want to see more
13560 errors. */
13561 if (e.error == NOT_FOUND_ERROR
13562 && (b->condition_not_parsed
13563 || (b->loc != NULL
13564 && search_pspace != NULL
13565 && b->loc->pspace != search_pspace)
13566 || (b->loc && b->loc->shlib_disabled)
13567 || (b->loc && b->loc->pspace->executing_startup)
13568 || b->enable_state == bp_disabled))
13569 not_found_and_ok = 1;
13570
13571 if (!not_found_and_ok)
13572 {
13573 /* We surely don't want to warn about the same breakpoint
13574 10 times. One solution, implemented here, is disable
13575 the breakpoint on error. Another solution would be to
13576 have separate 'warning emitted' flag. Since this
13577 happens only when a binary has changed, I don't know
13578 which approach is better. */
13579 b->enable_state = bp_disabled;
13580 throw;
13581 }
13582
13583 exception = std::move (e);
13584 }
13585
13586 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13587 {
13588 for (auto &sal : sals)
13589 resolve_sal_pc (&sal);
13590 if (b->condition_not_parsed && b->extra_string != NULL)
13591 {
13592 char *cond_string, *extra_string;
13593 int thread, task;
13594
13595 find_condition_and_thread (b->extra_string, sals[0].pc,
13596 &cond_string, &thread, &task,
13597 &extra_string);
13598 gdb_assert (b->cond_string == NULL);
13599 if (cond_string)
13600 b->cond_string = cond_string;
13601 b->thread = thread;
13602 b->task = task;
13603 if (extra_string)
13604 {
13605 xfree (b->extra_string);
13606 b->extra_string = extra_string;
13607 }
13608 b->condition_not_parsed = 0;
13609 }
13610
13611 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13612 sals[0] = update_static_tracepoint (b, sals[0]);
13613
13614 *found = 1;
13615 }
13616 else
13617 *found = 0;
13618
13619 return sals;
13620 }
13621
13622 /* The default re_set method, for typical hardware or software
13623 breakpoints. Reevaluate the breakpoint and recreate its
13624 locations. */
13625
13626 static void
13627 breakpoint_re_set_default (struct breakpoint *b)
13628 {
13629 struct program_space *filter_pspace = current_program_space;
13630 std::vector<symtab_and_line> expanded, expanded_end;
13631
13632 int found;
13633 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13634 filter_pspace, &found);
13635 if (found)
13636 expanded = std::move (sals);
13637
13638 if (b->location_range_end != NULL)
13639 {
13640 std::vector<symtab_and_line> sals_end
13641 = location_to_sals (b, b->location_range_end.get (),
13642 filter_pspace, &found);
13643 if (found)
13644 expanded_end = std::move (sals_end);
13645 }
13646
13647 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13648 }
13649
13650 /* Default method for creating SALs from an address string. It basically
13651 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13652
13653 static void
13654 create_sals_from_location_default (const struct event_location *location,
13655 struct linespec_result *canonical,
13656 enum bptype type_wanted)
13657 {
13658 parse_breakpoint_sals (location, canonical);
13659 }
13660
13661 /* Call create_breakpoints_sal for the given arguments. This is the default
13662 function for the `create_breakpoints_sal' method of
13663 breakpoint_ops. */
13664
13665 static void
13666 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13667 struct linespec_result *canonical,
13668 gdb::unique_xmalloc_ptr<char> cond_string,
13669 gdb::unique_xmalloc_ptr<char> extra_string,
13670 enum bptype type_wanted,
13671 enum bpdisp disposition,
13672 int thread,
13673 int task, int ignore_count,
13674 const struct breakpoint_ops *ops,
13675 int from_tty, int enabled,
13676 int internal, unsigned flags)
13677 {
13678 create_breakpoints_sal (gdbarch, canonical,
13679 std::move (cond_string),
13680 std::move (extra_string),
13681 type_wanted, disposition,
13682 thread, task, ignore_count, ops, from_tty,
13683 enabled, internal, flags);
13684 }
13685
13686 /* Decode the line represented by S by calling decode_line_full. This is the
13687 default function for the `decode_location' method of breakpoint_ops. */
13688
13689 static std::vector<symtab_and_line>
13690 decode_location_default (struct breakpoint *b,
13691 const struct event_location *location,
13692 struct program_space *search_pspace)
13693 {
13694 struct linespec_result canonical;
13695
13696 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13697 NULL, 0, &canonical, multiple_symbols_all,
13698 b->filter.get ());
13699
13700 /* We should get 0 or 1 resulting SALs. */
13701 gdb_assert (canonical.lsals.size () < 2);
13702
13703 if (!canonical.lsals.empty ())
13704 {
13705 const linespec_sals &lsal = canonical.lsals[0];
13706 return std::move (lsal.sals);
13707 }
13708 return {};
13709 }
13710
13711 /* Reset a breakpoint. */
13712
13713 static void
13714 breakpoint_re_set_one (breakpoint *b)
13715 {
13716 input_radix = b->input_radix;
13717 set_language (b->language);
13718
13719 b->ops->re_set (b);
13720 }
13721
13722 /* Re-set breakpoint locations for the current program space.
13723 Locations bound to other program spaces are left untouched. */
13724
13725 void
13726 breakpoint_re_set (void)
13727 {
13728 struct breakpoint *b, *b_tmp;
13729
13730 {
13731 scoped_restore_current_language save_language;
13732 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13733 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13734
13735 /* breakpoint_re_set_one sets the current_language to the language
13736 of the breakpoint it is resetting (see prepare_re_set_context)
13737 before re-evaluating the breakpoint's location. This change can
13738 unfortunately get undone by accident if the language_mode is set
13739 to auto, and we either switch frames, or more likely in this context,
13740 we select the current frame.
13741
13742 We prevent this by temporarily turning the language_mode to
13743 language_mode_manual. We restore it once all breakpoints
13744 have been reset. */
13745 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13746 language_mode = language_mode_manual;
13747
13748 /* Note: we must not try to insert locations until after all
13749 breakpoints have been re-set. Otherwise, e.g., when re-setting
13750 breakpoint 1, we'd insert the locations of breakpoint 2, which
13751 hadn't been re-set yet, and thus may have stale locations. */
13752
13753 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13754 {
13755 try
13756 {
13757 breakpoint_re_set_one (b);
13758 }
13759 catch (const gdb_exception &ex)
13760 {
13761 exception_fprintf (gdb_stderr, ex,
13762 "Error in re-setting breakpoint %d: ",
13763 b->number);
13764 }
13765 }
13766
13767 jit_breakpoint_re_set ();
13768 }
13769
13770 create_overlay_event_breakpoint ();
13771 create_longjmp_master_breakpoint ();
13772 create_std_terminate_master_breakpoint ();
13773 create_exception_master_breakpoint ();
13774
13775 /* Now we can insert. */
13776 update_global_location_list (UGLL_MAY_INSERT);
13777 }
13778 \f
13779 /* Reset the thread number of this breakpoint:
13780
13781 - If the breakpoint is for all threads, leave it as-is.
13782 - Else, reset it to the current thread for inferior_ptid. */
13783 void
13784 breakpoint_re_set_thread (struct breakpoint *b)
13785 {
13786 if (b->thread != -1)
13787 {
13788 b->thread = inferior_thread ()->global_num;
13789
13790 /* We're being called after following a fork. The new fork is
13791 selected as current, and unless this was a vfork will have a
13792 different program space from the original thread. Reset that
13793 as well. */
13794 b->loc->pspace = current_program_space;
13795 }
13796 }
13797
13798 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13799 If from_tty is nonzero, it prints a message to that effect,
13800 which ends with a period (no newline). */
13801
13802 void
13803 set_ignore_count (int bptnum, int count, int from_tty)
13804 {
13805 struct breakpoint *b;
13806
13807 if (count < 0)
13808 count = 0;
13809
13810 ALL_BREAKPOINTS (b)
13811 if (b->number == bptnum)
13812 {
13813 if (is_tracepoint (b))
13814 {
13815 if (from_tty && count != 0)
13816 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13817 bptnum);
13818 return;
13819 }
13820
13821 b->ignore_count = count;
13822 if (from_tty)
13823 {
13824 if (count == 0)
13825 printf_filtered (_("Will stop next time "
13826 "breakpoint %d is reached."),
13827 bptnum);
13828 else if (count == 1)
13829 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13830 bptnum);
13831 else
13832 printf_filtered (_("Will ignore next %d "
13833 "crossings of breakpoint %d."),
13834 count, bptnum);
13835 }
13836 gdb::observers::breakpoint_modified.notify (b);
13837 return;
13838 }
13839
13840 error (_("No breakpoint number %d."), bptnum);
13841 }
13842
13843 /* Command to set ignore-count of breakpoint N to COUNT. */
13844
13845 static void
13846 ignore_command (const char *args, int from_tty)
13847 {
13848 const char *p = args;
13849 int num;
13850
13851 if (p == 0)
13852 error_no_arg (_("a breakpoint number"));
13853
13854 num = get_number (&p);
13855 if (num == 0)
13856 error (_("bad breakpoint number: '%s'"), args);
13857 if (*p == 0)
13858 error (_("Second argument (specified ignore-count) is missing."));
13859
13860 set_ignore_count (num,
13861 longest_to_int (value_as_long (parse_and_eval (p))),
13862 from_tty);
13863 if (from_tty)
13864 printf_filtered ("\n");
13865 }
13866 \f
13867
13868 /* Call FUNCTION on each of the breakpoints with numbers in the range
13869 defined by BP_NUM_RANGE (an inclusive range). */
13870
13871 static void
13872 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13873 gdb::function_view<void (breakpoint *)> function)
13874 {
13875 if (bp_num_range.first == 0)
13876 {
13877 warning (_("bad breakpoint number at or near '%d'"),
13878 bp_num_range.first);
13879 }
13880 else
13881 {
13882 struct breakpoint *b, *tmp;
13883
13884 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13885 {
13886 bool match = false;
13887
13888 ALL_BREAKPOINTS_SAFE (b, tmp)
13889 if (b->number == i)
13890 {
13891 match = true;
13892 function (b);
13893 break;
13894 }
13895 if (!match)
13896 printf_unfiltered (_("No breakpoint number %d.\n"), i);
13897 }
13898 }
13899 }
13900
13901 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13902 ARGS. */
13903
13904 static void
13905 map_breakpoint_numbers (const char *args,
13906 gdb::function_view<void (breakpoint *)> function)
13907 {
13908 if (args == NULL || *args == '\0')
13909 error_no_arg (_("one or more breakpoint numbers"));
13910
13911 number_or_range_parser parser (args);
13912
13913 while (!parser.finished ())
13914 {
13915 int num = parser.get_number ();
13916 map_breakpoint_number_range (std::make_pair (num, num), function);
13917 }
13918 }
13919
13920 /* Return the breakpoint location structure corresponding to the
13921 BP_NUM and LOC_NUM values. */
13922
13923 static struct bp_location *
13924 find_location_by_number (int bp_num, int loc_num)
13925 {
13926 struct breakpoint *b;
13927
13928 ALL_BREAKPOINTS (b)
13929 if (b->number == bp_num)
13930 {
13931 break;
13932 }
13933
13934 if (!b || b->number != bp_num)
13935 error (_("Bad breakpoint number '%d'"), bp_num);
13936
13937 if (loc_num == 0)
13938 error (_("Bad breakpoint location number '%d'"), loc_num);
13939
13940 int n = 0;
13941 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
13942 if (++n == loc_num)
13943 return loc;
13944
13945 error (_("Bad breakpoint location number '%d'"), loc_num);
13946 }
13947
13948 /* Modes of operation for extract_bp_num. */
13949 enum class extract_bp_kind
13950 {
13951 /* Extracting a breakpoint number. */
13952 bp,
13953
13954 /* Extracting a location number. */
13955 loc,
13956 };
13957
13958 /* Extract a breakpoint or location number (as determined by KIND)
13959 from the string starting at START. TRAILER is a character which
13960 can be found after the number. If you don't want a trailer, use
13961 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13962 string. This always returns a positive integer. */
13963
13964 static int
13965 extract_bp_num (extract_bp_kind kind, const char *start,
13966 int trailer, const char **end_out = NULL)
13967 {
13968 const char *end = start;
13969 int num = get_number_trailer (&end, trailer);
13970 if (num < 0)
13971 error (kind == extract_bp_kind::bp
13972 ? _("Negative breakpoint number '%.*s'")
13973 : _("Negative breakpoint location number '%.*s'"),
13974 int (end - start), start);
13975 if (num == 0)
13976 error (kind == extract_bp_kind::bp
13977 ? _("Bad breakpoint number '%.*s'")
13978 : _("Bad breakpoint location number '%.*s'"),
13979 int (end - start), start);
13980
13981 if (end_out != NULL)
13982 *end_out = end;
13983 return num;
13984 }
13985
13986 /* Extract a breakpoint or location range (as determined by KIND) in
13987 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13988 representing the (inclusive) range. The returned pair's elements
13989 are always positive integers. */
13990
13991 static std::pair<int, int>
13992 extract_bp_or_bp_range (extract_bp_kind kind,
13993 const std::string &arg,
13994 std::string::size_type arg_offset)
13995 {
13996 std::pair<int, int> range;
13997 const char *bp_loc = &arg[arg_offset];
13998 std::string::size_type dash = arg.find ('-', arg_offset);
13999 if (dash != std::string::npos)
14000 {
14001 /* bp_loc is a range (x-z). */
14002 if (arg.length () == dash + 1)
14003 error (kind == extract_bp_kind::bp
14004 ? _("Bad breakpoint number at or near: '%s'")
14005 : _("Bad breakpoint location number at or near: '%s'"),
14006 bp_loc);
14007
14008 const char *end;
14009 const char *start_first = bp_loc;
14010 const char *start_second = &arg[dash + 1];
14011 range.first = extract_bp_num (kind, start_first, '-');
14012 range.second = extract_bp_num (kind, start_second, '\0', &end);
14013
14014 if (range.first > range.second)
14015 error (kind == extract_bp_kind::bp
14016 ? _("Inverted breakpoint range at '%.*s'")
14017 : _("Inverted breakpoint location range at '%.*s'"),
14018 int (end - start_first), start_first);
14019 }
14020 else
14021 {
14022 /* bp_loc is a single value. */
14023 range.first = extract_bp_num (kind, bp_loc, '\0');
14024 range.second = range.first;
14025 }
14026 return range;
14027 }
14028
14029 /* Extract the breakpoint/location range specified by ARG. Returns
14030 the breakpoint range in BP_NUM_RANGE, and the location range in
14031 BP_LOC_RANGE.
14032
14033 ARG may be in any of the following forms:
14034
14035 x where 'x' is a breakpoint number.
14036 x-y where 'x' and 'y' specify a breakpoint numbers range.
14037 x.y where 'x' is a breakpoint number and 'y' a location number.
14038 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14039 location number range.
14040 */
14041
14042 static void
14043 extract_bp_number_and_location (const std::string &arg,
14044 std::pair<int, int> &bp_num_range,
14045 std::pair<int, int> &bp_loc_range)
14046 {
14047 std::string::size_type dot = arg.find ('.');
14048
14049 if (dot != std::string::npos)
14050 {
14051 /* Handle 'x.y' and 'x.y-z' cases. */
14052
14053 if (arg.length () == dot + 1 || dot == 0)
14054 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14055
14056 bp_num_range.first
14057 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14058 bp_num_range.second = bp_num_range.first;
14059
14060 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14061 arg, dot + 1);
14062 }
14063 else
14064 {
14065 /* Handle x and x-y cases. */
14066
14067 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14068 bp_loc_range.first = 0;
14069 bp_loc_range.second = 0;
14070 }
14071 }
14072
14073 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14074 specifies whether to enable or disable. */
14075
14076 static void
14077 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14078 {
14079 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14080 if (loc != NULL)
14081 {
14082 if (loc->enabled != enable)
14083 {
14084 loc->enabled = enable;
14085 mark_breakpoint_location_modified (loc);
14086 }
14087 if (target_supports_enable_disable_tracepoint ()
14088 && current_trace_status ()->running && loc->owner
14089 && is_tracepoint (loc->owner))
14090 target_disable_tracepoint (loc);
14091 }
14092 update_global_location_list (UGLL_DONT_INSERT);
14093
14094 gdb::observers::breakpoint_modified.notify (loc->owner);
14095 }
14096
14097 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14098 number of the breakpoint, and BP_LOC_RANGE specifies the
14099 (inclusive) range of location numbers of that breakpoint to
14100 enable/disable. ENABLE specifies whether to enable or disable the
14101 location. */
14102
14103 static void
14104 enable_disable_breakpoint_location_range (int bp_num,
14105 std::pair<int, int> &bp_loc_range,
14106 bool enable)
14107 {
14108 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14109 enable_disable_bp_num_loc (bp_num, i, enable);
14110 }
14111
14112 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14113 If from_tty is nonzero, it prints a message to that effect,
14114 which ends with a period (no newline). */
14115
14116 void
14117 disable_breakpoint (struct breakpoint *bpt)
14118 {
14119 /* Never disable a watchpoint scope breakpoint; we want to
14120 hit them when we leave scope so we can delete both the
14121 watchpoint and its scope breakpoint at that time. */
14122 if (bpt->type == bp_watchpoint_scope)
14123 return;
14124
14125 bpt->enable_state = bp_disabled;
14126
14127 /* Mark breakpoint locations modified. */
14128 mark_breakpoint_modified (bpt);
14129
14130 if (target_supports_enable_disable_tracepoint ()
14131 && current_trace_status ()->running && is_tracepoint (bpt))
14132 {
14133 struct bp_location *location;
14134
14135 for (location = bpt->loc; location; location = location->next)
14136 target_disable_tracepoint (location);
14137 }
14138
14139 update_global_location_list (UGLL_DONT_INSERT);
14140
14141 gdb::observers::breakpoint_modified.notify (bpt);
14142 }
14143
14144 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14145 specified in ARGS. ARGS may be in any of the formats handled by
14146 extract_bp_number_and_location. ENABLE specifies whether to enable
14147 or disable the breakpoints/locations. */
14148
14149 static void
14150 enable_disable_command (const char *args, int from_tty, bool enable)
14151 {
14152 if (args == 0)
14153 {
14154 struct breakpoint *bpt;
14155
14156 ALL_BREAKPOINTS (bpt)
14157 if (user_breakpoint_p (bpt))
14158 {
14159 if (enable)
14160 enable_breakpoint (bpt);
14161 else
14162 disable_breakpoint (bpt);
14163 }
14164 }
14165 else
14166 {
14167 std::string num = extract_arg (&args);
14168
14169 while (!num.empty ())
14170 {
14171 std::pair<int, int> bp_num_range, bp_loc_range;
14172
14173 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14174
14175 if (bp_loc_range.first == bp_loc_range.second
14176 && bp_loc_range.first == 0)
14177 {
14178 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14179 map_breakpoint_number_range (bp_num_range,
14180 enable
14181 ? enable_breakpoint
14182 : disable_breakpoint);
14183 }
14184 else
14185 {
14186 /* Handle breakpoint ids with formats 'x.y' or
14187 'x.y-z'. */
14188 enable_disable_breakpoint_location_range
14189 (bp_num_range.first, bp_loc_range, enable);
14190 }
14191 num = extract_arg (&args);
14192 }
14193 }
14194 }
14195
14196 /* The disable command disables the specified breakpoints/locations
14197 (or all defined breakpoints) so they're no longer effective in
14198 stopping the inferior. ARGS may be in any of the forms defined in
14199 extract_bp_number_and_location. */
14200
14201 static void
14202 disable_command (const char *args, int from_tty)
14203 {
14204 enable_disable_command (args, from_tty, false);
14205 }
14206
14207 static void
14208 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14209 int count)
14210 {
14211 int target_resources_ok;
14212
14213 if (bpt->type == bp_hardware_breakpoint)
14214 {
14215 int i;
14216 i = hw_breakpoint_used_count ();
14217 target_resources_ok =
14218 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14219 i + 1, 0);
14220 if (target_resources_ok == 0)
14221 error (_("No hardware breakpoint support in the target."));
14222 else if (target_resources_ok < 0)
14223 error (_("Hardware breakpoints used exceeds limit."));
14224 }
14225
14226 if (is_watchpoint (bpt))
14227 {
14228 /* Initialize it just to avoid a GCC false warning. */
14229 enum enable_state orig_enable_state = bp_disabled;
14230
14231 try
14232 {
14233 struct watchpoint *w = (struct watchpoint *) bpt;
14234
14235 orig_enable_state = bpt->enable_state;
14236 bpt->enable_state = bp_enabled;
14237 update_watchpoint (w, 1 /* reparse */);
14238 }
14239 catch (const gdb_exception &e)
14240 {
14241 bpt->enable_state = orig_enable_state;
14242 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14243 bpt->number);
14244 return;
14245 }
14246 }
14247
14248 bpt->enable_state = bp_enabled;
14249
14250 /* Mark breakpoint locations modified. */
14251 mark_breakpoint_modified (bpt);
14252
14253 if (target_supports_enable_disable_tracepoint ()
14254 && current_trace_status ()->running && is_tracepoint (bpt))
14255 {
14256 struct bp_location *location;
14257
14258 for (location = bpt->loc; location; location = location->next)
14259 target_enable_tracepoint (location);
14260 }
14261
14262 bpt->disposition = disposition;
14263 bpt->enable_count = count;
14264 update_global_location_list (UGLL_MAY_INSERT);
14265
14266 gdb::observers::breakpoint_modified.notify (bpt);
14267 }
14268
14269
14270 void
14271 enable_breakpoint (struct breakpoint *bpt)
14272 {
14273 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14274 }
14275
14276 /* The enable command enables the specified breakpoints/locations (or
14277 all defined breakpoints) so they once again become (or continue to
14278 be) effective in stopping the inferior. ARGS may be in any of the
14279 forms defined in extract_bp_number_and_location. */
14280
14281 static void
14282 enable_command (const char *args, int from_tty)
14283 {
14284 enable_disable_command (args, from_tty, true);
14285 }
14286
14287 static void
14288 enable_once_command (const char *args, int from_tty)
14289 {
14290 map_breakpoint_numbers
14291 (args, [&] (breakpoint *b)
14292 {
14293 iterate_over_related_breakpoints
14294 (b, [&] (breakpoint *bpt)
14295 {
14296 enable_breakpoint_disp (bpt, disp_disable, 1);
14297 });
14298 });
14299 }
14300
14301 static void
14302 enable_count_command (const char *args, int from_tty)
14303 {
14304 int count;
14305
14306 if (args == NULL)
14307 error_no_arg (_("hit count"));
14308
14309 count = get_number (&args);
14310
14311 map_breakpoint_numbers
14312 (args, [&] (breakpoint *b)
14313 {
14314 iterate_over_related_breakpoints
14315 (b, [&] (breakpoint *bpt)
14316 {
14317 enable_breakpoint_disp (bpt, disp_disable, count);
14318 });
14319 });
14320 }
14321
14322 static void
14323 enable_delete_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_del, 1);
14332 });
14333 });
14334 }
14335 \f
14336 static void
14337 set_breakpoint_cmd (const char *args, int from_tty)
14338 {
14339 }
14340
14341 static void
14342 show_breakpoint_cmd (const char *args, int from_tty)
14343 {
14344 }
14345
14346 /* Invalidate last known value of any hardware watchpoint if
14347 the memory which that value represents has been written to by
14348 GDB itself. */
14349
14350 static void
14351 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14352 CORE_ADDR addr, ssize_t len,
14353 const bfd_byte *data)
14354 {
14355 struct breakpoint *bp;
14356
14357 ALL_BREAKPOINTS (bp)
14358 if (bp->enable_state == bp_enabled
14359 && bp->type == bp_hardware_watchpoint)
14360 {
14361 struct watchpoint *wp = (struct watchpoint *) bp;
14362
14363 if (wp->val_valid && wp->val != nullptr)
14364 {
14365 struct bp_location *loc;
14366
14367 for (loc = bp->loc; loc != NULL; loc = loc->next)
14368 if (loc->loc_type == bp_loc_hardware_watchpoint
14369 && loc->address + loc->length > addr
14370 && addr + len > loc->address)
14371 {
14372 wp->val = NULL;
14373 wp->val_valid = false;
14374 }
14375 }
14376 }
14377 }
14378
14379 /* Create and insert a breakpoint for software single step. */
14380
14381 void
14382 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14383 const address_space *aspace,
14384 CORE_ADDR next_pc)
14385 {
14386 struct thread_info *tp = inferior_thread ();
14387 struct symtab_and_line sal;
14388 CORE_ADDR pc = next_pc;
14389
14390 if (tp->control.single_step_breakpoints == NULL)
14391 {
14392 tp->control.single_step_breakpoints
14393 = new_single_step_breakpoint (tp->global_num, gdbarch);
14394 }
14395
14396 sal = find_pc_line (pc, 0);
14397 sal.pc = pc;
14398 sal.section = find_pc_overlay (pc);
14399 sal.explicit_pc = 1;
14400 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14401
14402 update_global_location_list (UGLL_INSERT);
14403 }
14404
14405 /* Insert single step breakpoints according to the current state. */
14406
14407 int
14408 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14409 {
14410 struct regcache *regcache = get_current_regcache ();
14411 std::vector<CORE_ADDR> next_pcs;
14412
14413 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14414
14415 if (!next_pcs.empty ())
14416 {
14417 struct frame_info *frame = get_current_frame ();
14418 const address_space *aspace = get_frame_address_space (frame);
14419
14420 for (CORE_ADDR pc : next_pcs)
14421 insert_single_step_breakpoint (gdbarch, aspace, pc);
14422
14423 return 1;
14424 }
14425 else
14426 return 0;
14427 }
14428
14429 /* See breakpoint.h. */
14430
14431 int
14432 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14433 const address_space *aspace,
14434 CORE_ADDR pc)
14435 {
14436 struct bp_location *loc;
14437
14438 for (loc = bp->loc; loc != NULL; loc = loc->next)
14439 if (loc->inserted
14440 && breakpoint_location_address_match (loc, aspace, pc))
14441 return 1;
14442
14443 return 0;
14444 }
14445
14446 /* Check whether a software single-step breakpoint is inserted at
14447 PC. */
14448
14449 int
14450 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14451 CORE_ADDR pc)
14452 {
14453 struct breakpoint *bpt;
14454
14455 ALL_BREAKPOINTS (bpt)
14456 {
14457 if (bpt->type == bp_single_step
14458 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14459 return 1;
14460 }
14461 return 0;
14462 }
14463
14464 /* Tracepoint-specific operations. */
14465
14466 /* Set tracepoint count to NUM. */
14467 static void
14468 set_tracepoint_count (int num)
14469 {
14470 tracepoint_count = num;
14471 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14472 }
14473
14474 static void
14475 trace_command (const char *arg, int from_tty)
14476 {
14477 struct breakpoint_ops *ops;
14478
14479 event_location_up location = string_to_event_location (&arg,
14480 current_language);
14481 if (location != NULL
14482 && event_location_type (location.get ()) == PROBE_LOCATION)
14483 ops = &tracepoint_probe_breakpoint_ops;
14484 else
14485 ops = &tracepoint_breakpoint_ops;
14486
14487 create_breakpoint (get_current_arch (),
14488 location.get (),
14489 NULL, 0, arg, 1 /* parse arg */,
14490 0 /* tempflag */,
14491 bp_tracepoint /* type_wanted */,
14492 0 /* Ignore count */,
14493 pending_break_support,
14494 ops,
14495 from_tty,
14496 1 /* enabled */,
14497 0 /* internal */, 0);
14498 }
14499
14500 static void
14501 ftrace_command (const char *arg, int from_tty)
14502 {
14503 event_location_up location = string_to_event_location (&arg,
14504 current_language);
14505 create_breakpoint (get_current_arch (),
14506 location.get (),
14507 NULL, 0, arg, 1 /* parse arg */,
14508 0 /* tempflag */,
14509 bp_fast_tracepoint /* type_wanted */,
14510 0 /* Ignore count */,
14511 pending_break_support,
14512 &tracepoint_breakpoint_ops,
14513 from_tty,
14514 1 /* enabled */,
14515 0 /* internal */, 0);
14516 }
14517
14518 /* strace command implementation. Creates a static tracepoint. */
14519
14520 static void
14521 strace_command (const char *arg, int from_tty)
14522 {
14523 struct breakpoint_ops *ops;
14524 event_location_up location;
14525
14526 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14527 or with a normal static tracepoint. */
14528 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14529 {
14530 ops = &strace_marker_breakpoint_ops;
14531 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14532 }
14533 else
14534 {
14535 ops = &tracepoint_breakpoint_ops;
14536 location = string_to_event_location (&arg, current_language);
14537 }
14538
14539 create_breakpoint (get_current_arch (),
14540 location.get (),
14541 NULL, 0, arg, 1 /* parse arg */,
14542 0 /* tempflag */,
14543 bp_static_tracepoint /* type_wanted */,
14544 0 /* Ignore count */,
14545 pending_break_support,
14546 ops,
14547 from_tty,
14548 1 /* enabled */,
14549 0 /* internal */, 0);
14550 }
14551
14552 /* Set up a fake reader function that gets command lines from a linked
14553 list that was acquired during tracepoint uploading. */
14554
14555 static struct uploaded_tp *this_utp;
14556 static int next_cmd;
14557
14558 static char *
14559 read_uploaded_action (void)
14560 {
14561 char *rslt = nullptr;
14562
14563 if (next_cmd < this_utp->cmd_strings.size ())
14564 {
14565 rslt = this_utp->cmd_strings[next_cmd].get ();
14566 next_cmd++;
14567 }
14568
14569 return rslt;
14570 }
14571
14572 /* Given information about a tracepoint as recorded on a target (which
14573 can be either a live system or a trace file), attempt to create an
14574 equivalent GDB tracepoint. This is not a reliable process, since
14575 the target does not necessarily have all the information used when
14576 the tracepoint was originally defined. */
14577
14578 struct tracepoint *
14579 create_tracepoint_from_upload (struct uploaded_tp *utp)
14580 {
14581 const char *addr_str;
14582 char small_buf[100];
14583 struct tracepoint *tp;
14584
14585 if (utp->at_string)
14586 addr_str = utp->at_string.get ();
14587 else
14588 {
14589 /* In the absence of a source location, fall back to raw
14590 address. Since there is no way to confirm that the address
14591 means the same thing as when the trace was started, warn the
14592 user. */
14593 warning (_("Uploaded tracepoint %d has no "
14594 "source location, using raw address"),
14595 utp->number);
14596 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14597 addr_str = small_buf;
14598 }
14599
14600 /* There's not much we can do with a sequence of bytecodes. */
14601 if (utp->cond && !utp->cond_string)
14602 warning (_("Uploaded tracepoint %d condition "
14603 "has no source form, ignoring it"),
14604 utp->number);
14605
14606 event_location_up location = string_to_event_location (&addr_str,
14607 current_language);
14608 if (!create_breakpoint (get_current_arch (),
14609 location.get (),
14610 utp->cond_string.get (), -1, addr_str,
14611 0 /* parse cond/thread */,
14612 0 /* tempflag */,
14613 utp->type /* type_wanted */,
14614 0 /* Ignore count */,
14615 pending_break_support,
14616 &tracepoint_breakpoint_ops,
14617 0 /* from_tty */,
14618 utp->enabled /* enabled */,
14619 0 /* internal */,
14620 CREATE_BREAKPOINT_FLAGS_INSERTED))
14621 return NULL;
14622
14623 /* Get the tracepoint we just created. */
14624 tp = get_tracepoint (tracepoint_count);
14625 gdb_assert (tp != NULL);
14626
14627 if (utp->pass > 0)
14628 {
14629 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14630 tp->number);
14631
14632 trace_pass_command (small_buf, 0);
14633 }
14634
14635 /* If we have uploaded versions of the original commands, set up a
14636 special-purpose "reader" function and call the usual command line
14637 reader, then pass the result to the breakpoint command-setting
14638 function. */
14639 if (!utp->cmd_strings.empty ())
14640 {
14641 counted_command_line cmd_list;
14642
14643 this_utp = utp;
14644 next_cmd = 0;
14645
14646 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14647
14648 breakpoint_set_commands (tp, std::move (cmd_list));
14649 }
14650 else if (!utp->actions.empty ()
14651 || !utp->step_actions.empty ())
14652 warning (_("Uploaded tracepoint %d actions "
14653 "have no source form, ignoring them"),
14654 utp->number);
14655
14656 /* Copy any status information that might be available. */
14657 tp->hit_count = utp->hit_count;
14658 tp->traceframe_usage = utp->traceframe_usage;
14659
14660 return tp;
14661 }
14662
14663 /* Print information on tracepoint number TPNUM_EXP, or all if
14664 omitted. */
14665
14666 static void
14667 info_tracepoints_command (const char *args, int from_tty)
14668 {
14669 struct ui_out *uiout = current_uiout;
14670 int num_printed;
14671
14672 num_printed = breakpoint_1 (args, false, is_tracepoint);
14673
14674 if (num_printed == 0)
14675 {
14676 if (args == NULL || *args == '\0')
14677 uiout->message ("No tracepoints.\n");
14678 else
14679 uiout->message ("No tracepoint matching '%s'.\n", args);
14680 }
14681
14682 default_collect_info ();
14683 }
14684
14685 /* The 'enable trace' command enables tracepoints.
14686 Not supported by all targets. */
14687 static void
14688 enable_trace_command (const char *args, int from_tty)
14689 {
14690 enable_command (args, from_tty);
14691 }
14692
14693 /* The 'disable trace' command disables tracepoints.
14694 Not supported by all targets. */
14695 static void
14696 disable_trace_command (const char *args, int from_tty)
14697 {
14698 disable_command (args, from_tty);
14699 }
14700
14701 /* Remove a tracepoint (or all if no argument). */
14702 static void
14703 delete_trace_command (const char *arg, int from_tty)
14704 {
14705 struct breakpoint *b, *b_tmp;
14706
14707 dont_repeat ();
14708
14709 if (arg == 0)
14710 {
14711 int breaks_to_delete = 0;
14712
14713 /* Delete all breakpoints if no argument.
14714 Do not delete internal or call-dummy breakpoints, these
14715 have to be deleted with an explicit breakpoint number
14716 argument. */
14717 ALL_TRACEPOINTS (b)
14718 if (is_tracepoint (b) && user_breakpoint_p (b))
14719 {
14720 breaks_to_delete = 1;
14721 break;
14722 }
14723
14724 /* Ask user only if there are some breakpoints to delete. */
14725 if (!from_tty
14726 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14727 {
14728 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14729 if (is_tracepoint (b) && user_breakpoint_p (b))
14730 delete_breakpoint (b);
14731 }
14732 }
14733 else
14734 map_breakpoint_numbers
14735 (arg, [&] (breakpoint *br)
14736 {
14737 iterate_over_related_breakpoints (br, delete_breakpoint);
14738 });
14739 }
14740
14741 /* Helper function for trace_pass_command. */
14742
14743 static void
14744 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14745 {
14746 tp->pass_count = count;
14747 gdb::observers::breakpoint_modified.notify (tp);
14748 if (from_tty)
14749 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14750 tp->number, count);
14751 }
14752
14753 /* Set passcount for tracepoint.
14754
14755 First command argument is passcount, second is tracepoint number.
14756 If tracepoint number omitted, apply to most recently defined.
14757 Also accepts special argument "all". */
14758
14759 static void
14760 trace_pass_command (const char *args, int from_tty)
14761 {
14762 struct tracepoint *t1;
14763 ULONGEST count;
14764
14765 if (args == 0 || *args == 0)
14766 error (_("passcount command requires an "
14767 "argument (count + optional TP num)"));
14768
14769 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14770
14771 args = skip_spaces (args);
14772 if (*args && strncasecmp (args, "all", 3) == 0)
14773 {
14774 struct breakpoint *b;
14775
14776 args += 3; /* Skip special argument "all". */
14777 if (*args)
14778 error (_("Junk at end of arguments."));
14779
14780 ALL_TRACEPOINTS (b)
14781 {
14782 t1 = (struct tracepoint *) b;
14783 trace_pass_set_count (t1, count, from_tty);
14784 }
14785 }
14786 else if (*args == '\0')
14787 {
14788 t1 = get_tracepoint_by_number (&args, NULL);
14789 if (t1)
14790 trace_pass_set_count (t1, count, from_tty);
14791 }
14792 else
14793 {
14794 number_or_range_parser parser (args);
14795 while (!parser.finished ())
14796 {
14797 t1 = get_tracepoint_by_number (&args, &parser);
14798 if (t1)
14799 trace_pass_set_count (t1, count, from_tty);
14800 }
14801 }
14802 }
14803
14804 struct tracepoint *
14805 get_tracepoint (int num)
14806 {
14807 struct breakpoint *t;
14808
14809 ALL_TRACEPOINTS (t)
14810 if (t->number == num)
14811 return (struct tracepoint *) t;
14812
14813 return NULL;
14814 }
14815
14816 /* Find the tracepoint with the given target-side number (which may be
14817 different from the tracepoint number after disconnecting and
14818 reconnecting). */
14819
14820 struct tracepoint *
14821 get_tracepoint_by_number_on_target (int num)
14822 {
14823 struct breakpoint *b;
14824
14825 ALL_TRACEPOINTS (b)
14826 {
14827 struct tracepoint *t = (struct tracepoint *) b;
14828
14829 if (t->number_on_target == num)
14830 return t;
14831 }
14832
14833 return NULL;
14834 }
14835
14836 /* Utility: parse a tracepoint number and look it up in the list.
14837 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14838 If the argument is missing, the most recent tracepoint
14839 (tracepoint_count) is returned. */
14840
14841 struct tracepoint *
14842 get_tracepoint_by_number (const char **arg,
14843 number_or_range_parser *parser)
14844 {
14845 struct breakpoint *t;
14846 int tpnum;
14847 const char *instring = arg == NULL ? NULL : *arg;
14848
14849 if (parser != NULL)
14850 {
14851 gdb_assert (!parser->finished ());
14852 tpnum = parser->get_number ();
14853 }
14854 else if (arg == NULL || *arg == NULL || ! **arg)
14855 tpnum = tracepoint_count;
14856 else
14857 tpnum = get_number (arg);
14858
14859 if (tpnum <= 0)
14860 {
14861 if (instring && *instring)
14862 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14863 instring);
14864 else
14865 printf_filtered (_("No previous tracepoint\n"));
14866 return NULL;
14867 }
14868
14869 ALL_TRACEPOINTS (t)
14870 if (t->number == tpnum)
14871 {
14872 return (struct tracepoint *) t;
14873 }
14874
14875 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14876 return NULL;
14877 }
14878
14879 void
14880 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14881 {
14882 if (b->thread != -1)
14883 fprintf_unfiltered (fp, " thread %d", b->thread);
14884
14885 if (b->task != 0)
14886 fprintf_unfiltered (fp, " task %d", b->task);
14887
14888 fprintf_unfiltered (fp, "\n");
14889 }
14890
14891 /* Save information on user settable breakpoints (watchpoints, etc) to
14892 a new script file named FILENAME. If FILTER is non-NULL, call it
14893 on each breakpoint and only include the ones for which it returns
14894 true. */
14895
14896 static void
14897 save_breakpoints (const char *filename, int from_tty,
14898 bool (*filter) (const struct breakpoint *))
14899 {
14900 struct breakpoint *tp;
14901 int any = 0;
14902 int extra_trace_bits = 0;
14903
14904 if (filename == 0 || *filename == 0)
14905 error (_("Argument required (file name in which to save)"));
14906
14907 /* See if we have anything to save. */
14908 ALL_BREAKPOINTS (tp)
14909 {
14910 /* Skip internal and momentary breakpoints. */
14911 if (!user_breakpoint_p (tp))
14912 continue;
14913
14914 /* If we have a filter, only save the breakpoints it accepts. */
14915 if (filter && !filter (tp))
14916 continue;
14917
14918 any = 1;
14919
14920 if (is_tracepoint (tp))
14921 {
14922 extra_trace_bits = 1;
14923
14924 /* We can stop searching. */
14925 break;
14926 }
14927 }
14928
14929 if (!any)
14930 {
14931 warning (_("Nothing to save."));
14932 return;
14933 }
14934
14935 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14936
14937 stdio_file fp;
14938
14939 if (!fp.open (expanded_filename.get (), "w"))
14940 error (_("Unable to open file '%s' for saving (%s)"),
14941 expanded_filename.get (), safe_strerror (errno));
14942
14943 if (extra_trace_bits)
14944 save_trace_state_variables (&fp);
14945
14946 ALL_BREAKPOINTS (tp)
14947 {
14948 /* Skip internal and momentary breakpoints. */
14949 if (!user_breakpoint_p (tp))
14950 continue;
14951
14952 /* If we have a filter, only save the breakpoints it accepts. */
14953 if (filter && !filter (tp))
14954 continue;
14955
14956 tp->ops->print_recreate (tp, &fp);
14957
14958 /* Note, we can't rely on tp->number for anything, as we can't
14959 assume the recreated breakpoint numbers will match. Use $bpnum
14960 instead. */
14961
14962 if (tp->cond_string)
14963 fp.printf (" condition $bpnum %s\n", tp->cond_string);
14964
14965 if (tp->ignore_count)
14966 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14967
14968 if (tp->type != bp_dprintf && tp->commands)
14969 {
14970 fp.puts (" commands\n");
14971
14972 current_uiout->redirect (&fp);
14973 try
14974 {
14975 print_command_lines (current_uiout, tp->commands.get (), 2);
14976 }
14977 catch (const gdb_exception &ex)
14978 {
14979 current_uiout->redirect (NULL);
14980 throw;
14981 }
14982
14983 current_uiout->redirect (NULL);
14984 fp.puts (" end\n");
14985 }
14986
14987 if (tp->enable_state == bp_disabled)
14988 fp.puts ("disable $bpnum\n");
14989
14990 /* If this is a multi-location breakpoint, check if the locations
14991 should be individually disabled. Watchpoint locations are
14992 special, and not user visible. */
14993 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14994 {
14995 struct bp_location *loc;
14996 int n = 1;
14997
14998 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
14999 if (!loc->enabled)
15000 fp.printf ("disable $bpnum.%d\n", n);
15001 }
15002 }
15003
15004 if (extra_trace_bits && *default_collect)
15005 fp.printf ("set default-collect %s\n", default_collect);
15006
15007 if (from_tty)
15008 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15009 }
15010
15011 /* The `save breakpoints' command. */
15012
15013 static void
15014 save_breakpoints_command (const char *args, int from_tty)
15015 {
15016 save_breakpoints (args, from_tty, NULL);
15017 }
15018
15019 /* The `save tracepoints' command. */
15020
15021 static void
15022 save_tracepoints_command (const char *args, int from_tty)
15023 {
15024 save_breakpoints (args, from_tty, is_tracepoint);
15025 }
15026
15027 /* Create a vector of all tracepoints. */
15028
15029 std::vector<breakpoint *>
15030 all_tracepoints (void)
15031 {
15032 std::vector<breakpoint *> tp_vec;
15033 struct breakpoint *tp;
15034
15035 ALL_TRACEPOINTS (tp)
15036 {
15037 tp_vec.push_back (tp);
15038 }
15039
15040 return tp_vec;
15041 }
15042
15043 \f
15044 /* This help string is used to consolidate all the help string for specifying
15045 locations used by several commands. */
15046
15047 #define LOCATION_HELP_STRING \
15048 "Linespecs are colon-separated lists of location parameters, such as\n\
15049 source filename, function name, label name, and line number.\n\
15050 Example: To specify the start of a label named \"the_top\" in the\n\
15051 function \"fact\" in the file \"factorial.c\", use\n\
15052 \"factorial.c:fact:the_top\".\n\
15053 \n\
15054 Address locations begin with \"*\" and specify an exact address in the\n\
15055 program. Example: To specify the fourth byte past the start function\n\
15056 \"main\", use \"*main + 4\".\n\
15057 \n\
15058 Explicit locations are similar to linespecs but use an option/argument\n\
15059 syntax to specify location parameters.\n\
15060 Example: To specify the start of the label named \"the_top\" in the\n\
15061 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15062 -function fact -label the_top\".\n\
15063 \n\
15064 By default, a specified function is matched against the program's\n\
15065 functions in all scopes. For C++, this means in all namespaces and\n\
15066 classes. For Ada, this means in all packages. E.g., in C++,\n\
15067 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15068 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15069 specified name as a complete fully-qualified name instead."
15070
15071 /* This help string is used for the break, hbreak, tbreak and thbreak
15072 commands. It is defined as a macro to prevent duplication.
15073 COMMAND should be a string constant containing the name of the
15074 command. */
15075
15076 #define BREAK_ARGS_HELP(command) \
15077 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15078 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15079 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15080 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15081 `-probe-dtrace' (for a DTrace probe).\n\
15082 LOCATION may be a linespec, address, or explicit location as described\n\
15083 below.\n\
15084 \n\
15085 With no LOCATION, uses current execution address of the selected\n\
15086 stack frame. This is useful for breaking on return to a stack frame.\n\
15087 \n\
15088 THREADNUM is the number from \"info threads\".\n\
15089 CONDITION is a boolean expression.\n\
15090 \n" LOCATION_HELP_STRING "\n\n\
15091 Multiple breakpoints at one place are permitted, and useful if their\n\
15092 conditions are different.\n\
15093 \n\
15094 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15095
15096 /* List of subcommands for "catch". */
15097 static struct cmd_list_element *catch_cmdlist;
15098
15099 /* List of subcommands for "tcatch". */
15100 static struct cmd_list_element *tcatch_cmdlist;
15101
15102 void
15103 add_catch_command (const char *name, const char *docstring,
15104 cmd_const_sfunc_ftype *sfunc,
15105 completer_ftype *completer,
15106 void *user_data_catch,
15107 void *user_data_tcatch)
15108 {
15109 struct cmd_list_element *command;
15110
15111 command = add_cmd (name, class_breakpoint, docstring,
15112 &catch_cmdlist);
15113 set_cmd_sfunc (command, sfunc);
15114 set_cmd_context (command, user_data_catch);
15115 set_cmd_completer (command, completer);
15116
15117 command = add_cmd (name, class_breakpoint, docstring,
15118 &tcatch_cmdlist);
15119 set_cmd_sfunc (command, sfunc);
15120 set_cmd_context (command, user_data_tcatch);
15121 set_cmd_completer (command, completer);
15122 }
15123
15124 static void
15125 save_command (const char *arg, int from_tty)
15126 {
15127 printf_unfiltered (_("\"save\" must be followed by "
15128 "the name of a save subcommand.\n"));
15129 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15130 }
15131
15132 struct breakpoint *
15133 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15134 void *data)
15135 {
15136 struct breakpoint *b, *b_tmp;
15137
15138 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15139 {
15140 if ((*callback) (b, data))
15141 return b;
15142 }
15143
15144 return NULL;
15145 }
15146
15147 /* Zero if any of the breakpoint's locations could be a location where
15148 functions have been inlined, nonzero otherwise. */
15149
15150 static int
15151 is_non_inline_function (struct breakpoint *b)
15152 {
15153 /* The shared library event breakpoint is set on the address of a
15154 non-inline function. */
15155 if (b->type == bp_shlib_event)
15156 return 1;
15157
15158 return 0;
15159 }
15160
15161 /* Nonzero if the specified PC cannot be a location where functions
15162 have been inlined. */
15163
15164 int
15165 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15166 const struct target_waitstatus *ws)
15167 {
15168 struct breakpoint *b;
15169 struct bp_location *bl;
15170
15171 ALL_BREAKPOINTS (b)
15172 {
15173 if (!is_non_inline_function (b))
15174 continue;
15175
15176 for (bl = b->loc; bl != NULL; bl = bl->next)
15177 {
15178 if (!bl->shlib_disabled
15179 && bpstat_check_location (bl, aspace, pc, ws))
15180 return 1;
15181 }
15182 }
15183
15184 return 0;
15185 }
15186
15187 /* Remove any references to OBJFILE which is going to be freed. */
15188
15189 void
15190 breakpoint_free_objfile (struct objfile *objfile)
15191 {
15192 struct bp_location **locp, *loc;
15193
15194 ALL_BP_LOCATIONS (loc, locp)
15195 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15196 loc->symtab = NULL;
15197 }
15198
15199 void
15200 initialize_breakpoint_ops (void)
15201 {
15202 static int initialized = 0;
15203
15204 struct breakpoint_ops *ops;
15205
15206 if (initialized)
15207 return;
15208 initialized = 1;
15209
15210 /* The breakpoint_ops structure to be inherit by all kinds of
15211 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15212 internal and momentary breakpoints, etc.). */
15213 ops = &bkpt_base_breakpoint_ops;
15214 *ops = base_breakpoint_ops;
15215 ops->re_set = bkpt_re_set;
15216 ops->insert_location = bkpt_insert_location;
15217 ops->remove_location = bkpt_remove_location;
15218 ops->breakpoint_hit = bkpt_breakpoint_hit;
15219 ops->create_sals_from_location = bkpt_create_sals_from_location;
15220 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15221 ops->decode_location = bkpt_decode_location;
15222
15223 /* The breakpoint_ops structure to be used in regular breakpoints. */
15224 ops = &bkpt_breakpoint_ops;
15225 *ops = bkpt_base_breakpoint_ops;
15226 ops->re_set = bkpt_re_set;
15227 ops->resources_needed = bkpt_resources_needed;
15228 ops->print_it = bkpt_print_it;
15229 ops->print_mention = bkpt_print_mention;
15230 ops->print_recreate = bkpt_print_recreate;
15231
15232 /* Ranged breakpoints. */
15233 ops = &ranged_breakpoint_ops;
15234 *ops = bkpt_breakpoint_ops;
15235 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15236 ops->resources_needed = resources_needed_ranged_breakpoint;
15237 ops->print_it = print_it_ranged_breakpoint;
15238 ops->print_one = print_one_ranged_breakpoint;
15239 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15240 ops->print_mention = print_mention_ranged_breakpoint;
15241 ops->print_recreate = print_recreate_ranged_breakpoint;
15242
15243 /* Internal breakpoints. */
15244 ops = &internal_breakpoint_ops;
15245 *ops = bkpt_base_breakpoint_ops;
15246 ops->re_set = internal_bkpt_re_set;
15247 ops->check_status = internal_bkpt_check_status;
15248 ops->print_it = internal_bkpt_print_it;
15249 ops->print_mention = internal_bkpt_print_mention;
15250
15251 /* Momentary breakpoints. */
15252 ops = &momentary_breakpoint_ops;
15253 *ops = bkpt_base_breakpoint_ops;
15254 ops->re_set = momentary_bkpt_re_set;
15255 ops->check_status = momentary_bkpt_check_status;
15256 ops->print_it = momentary_bkpt_print_it;
15257 ops->print_mention = momentary_bkpt_print_mention;
15258
15259 /* Probe breakpoints. */
15260 ops = &bkpt_probe_breakpoint_ops;
15261 *ops = bkpt_breakpoint_ops;
15262 ops->insert_location = bkpt_probe_insert_location;
15263 ops->remove_location = bkpt_probe_remove_location;
15264 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15265 ops->decode_location = bkpt_probe_decode_location;
15266
15267 /* Watchpoints. */
15268 ops = &watchpoint_breakpoint_ops;
15269 *ops = base_breakpoint_ops;
15270 ops->re_set = re_set_watchpoint;
15271 ops->insert_location = insert_watchpoint;
15272 ops->remove_location = remove_watchpoint;
15273 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15274 ops->check_status = check_status_watchpoint;
15275 ops->resources_needed = resources_needed_watchpoint;
15276 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15277 ops->print_it = print_it_watchpoint;
15278 ops->print_mention = print_mention_watchpoint;
15279 ops->print_recreate = print_recreate_watchpoint;
15280 ops->explains_signal = explains_signal_watchpoint;
15281
15282 /* Masked watchpoints. */
15283 ops = &masked_watchpoint_breakpoint_ops;
15284 *ops = watchpoint_breakpoint_ops;
15285 ops->insert_location = insert_masked_watchpoint;
15286 ops->remove_location = remove_masked_watchpoint;
15287 ops->resources_needed = resources_needed_masked_watchpoint;
15288 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15289 ops->print_it = print_it_masked_watchpoint;
15290 ops->print_one_detail = print_one_detail_masked_watchpoint;
15291 ops->print_mention = print_mention_masked_watchpoint;
15292 ops->print_recreate = print_recreate_masked_watchpoint;
15293
15294 /* Tracepoints. */
15295 ops = &tracepoint_breakpoint_ops;
15296 *ops = base_breakpoint_ops;
15297 ops->re_set = tracepoint_re_set;
15298 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15299 ops->print_one_detail = tracepoint_print_one_detail;
15300 ops->print_mention = tracepoint_print_mention;
15301 ops->print_recreate = tracepoint_print_recreate;
15302 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15303 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15304 ops->decode_location = tracepoint_decode_location;
15305
15306 /* Probe tracepoints. */
15307 ops = &tracepoint_probe_breakpoint_ops;
15308 *ops = tracepoint_breakpoint_ops;
15309 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15310 ops->decode_location = tracepoint_probe_decode_location;
15311
15312 /* Static tracepoints with marker (`-m'). */
15313 ops = &strace_marker_breakpoint_ops;
15314 *ops = tracepoint_breakpoint_ops;
15315 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15316 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15317 ops->decode_location = strace_marker_decode_location;
15318
15319 /* Fork catchpoints. */
15320 ops = &catch_fork_breakpoint_ops;
15321 *ops = base_breakpoint_ops;
15322 ops->insert_location = insert_catch_fork;
15323 ops->remove_location = remove_catch_fork;
15324 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15325 ops->print_it = print_it_catch_fork;
15326 ops->print_one = print_one_catch_fork;
15327 ops->print_mention = print_mention_catch_fork;
15328 ops->print_recreate = print_recreate_catch_fork;
15329
15330 /* Vfork catchpoints. */
15331 ops = &catch_vfork_breakpoint_ops;
15332 *ops = base_breakpoint_ops;
15333 ops->insert_location = insert_catch_vfork;
15334 ops->remove_location = remove_catch_vfork;
15335 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15336 ops->print_it = print_it_catch_vfork;
15337 ops->print_one = print_one_catch_vfork;
15338 ops->print_mention = print_mention_catch_vfork;
15339 ops->print_recreate = print_recreate_catch_vfork;
15340
15341 /* Exec catchpoints. */
15342 ops = &catch_exec_breakpoint_ops;
15343 *ops = base_breakpoint_ops;
15344 ops->insert_location = insert_catch_exec;
15345 ops->remove_location = remove_catch_exec;
15346 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15347 ops->print_it = print_it_catch_exec;
15348 ops->print_one = print_one_catch_exec;
15349 ops->print_mention = print_mention_catch_exec;
15350 ops->print_recreate = print_recreate_catch_exec;
15351
15352 /* Solib-related catchpoints. */
15353 ops = &catch_solib_breakpoint_ops;
15354 *ops = base_breakpoint_ops;
15355 ops->insert_location = insert_catch_solib;
15356 ops->remove_location = remove_catch_solib;
15357 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15358 ops->check_status = check_status_catch_solib;
15359 ops->print_it = print_it_catch_solib;
15360 ops->print_one = print_one_catch_solib;
15361 ops->print_mention = print_mention_catch_solib;
15362 ops->print_recreate = print_recreate_catch_solib;
15363
15364 ops = &dprintf_breakpoint_ops;
15365 *ops = bkpt_base_breakpoint_ops;
15366 ops->re_set = dprintf_re_set;
15367 ops->resources_needed = bkpt_resources_needed;
15368 ops->print_it = bkpt_print_it;
15369 ops->print_mention = bkpt_print_mention;
15370 ops->print_recreate = dprintf_print_recreate;
15371 ops->after_condition_true = dprintf_after_condition_true;
15372 ops->breakpoint_hit = dprintf_breakpoint_hit;
15373 }
15374
15375 /* Chain containing all defined "enable breakpoint" subcommands. */
15376
15377 static struct cmd_list_element *enablebreaklist = NULL;
15378
15379 /* See breakpoint.h. */
15380
15381 cmd_list_element *commands_cmd_element = nullptr;
15382
15383 void
15384 _initialize_breakpoint (void)
15385 {
15386 struct cmd_list_element *c;
15387
15388 initialize_breakpoint_ops ();
15389
15390 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15391 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15392 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15393
15394 breakpoint_chain = 0;
15395 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15396 before a breakpoint is set. */
15397 breakpoint_count = 0;
15398
15399 tracepoint_count = 0;
15400
15401 add_com ("ignore", class_breakpoint, ignore_command, _("\
15402 Set ignore-count of breakpoint number N to COUNT.\n\
15403 Usage is `ignore N COUNT'."));
15404
15405 commands_cmd_element = add_com ("commands", class_breakpoint,
15406 commands_command, _("\
15407 Set commands to be executed when the given breakpoints are hit.\n\
15408 Give a space-separated breakpoint list as argument after \"commands\".\n\
15409 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15410 (e.g. `5-7').\n\
15411 With no argument, the targeted breakpoint is the last one set.\n\
15412 The commands themselves follow starting on the next line.\n\
15413 Type a line containing \"end\" to indicate the end of them.\n\
15414 Give \"silent\" as the first line to make the breakpoint silent;\n\
15415 then no output is printed when it is hit, except what the commands print."));
15416
15417 c = add_com ("condition", class_breakpoint, condition_command, _("\
15418 Specify breakpoint number N to break only if COND is true.\n\
15419 Usage is `condition N COND', where N is an integer and COND is an\n\
15420 expression to be evaluated whenever breakpoint N is reached."));
15421 set_cmd_completer (c, condition_completer);
15422
15423 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15424 Set a temporary breakpoint.\n\
15425 Like \"break\" except the breakpoint is only temporary,\n\
15426 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15427 by using \"enable delete\" on the breakpoint number.\n\
15428 \n"
15429 BREAK_ARGS_HELP ("tbreak")));
15430 set_cmd_completer (c, location_completer);
15431
15432 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15433 Set a hardware assisted breakpoint.\n\
15434 Like \"break\" except the breakpoint requires hardware support,\n\
15435 some target hardware may not have this support.\n\
15436 \n"
15437 BREAK_ARGS_HELP ("hbreak")));
15438 set_cmd_completer (c, location_completer);
15439
15440 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15441 Set a temporary hardware assisted breakpoint.\n\
15442 Like \"hbreak\" except the breakpoint is only temporary,\n\
15443 so it will be deleted when hit.\n\
15444 \n"
15445 BREAK_ARGS_HELP ("thbreak")));
15446 set_cmd_completer (c, location_completer);
15447
15448 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15449 Enable all or some breakpoints.\n\
15450 Usage: enable [BREAKPOINTNUM]...\n\
15451 Give breakpoint numbers (separated by spaces) as arguments.\n\
15452 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15453 This is used to cancel the effect of the \"disable\" command.\n\
15454 With a subcommand you can enable temporarily."),
15455 &enablelist, "enable ", 1, &cmdlist);
15456
15457 add_com_alias ("en", "enable", class_breakpoint, 1);
15458
15459 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15460 Enable all or some breakpoints.\n\
15461 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15462 Give breakpoint numbers (separated by spaces) as arguments.\n\
15463 This is used to cancel the effect of the \"disable\" command.\n\
15464 May be abbreviated to simply \"enable\"."),
15465 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15466
15467 add_cmd ("once", no_class, enable_once_command, _("\
15468 Enable some breakpoints for one hit.\n\
15469 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15470 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15471 &enablebreaklist);
15472
15473 add_cmd ("delete", no_class, enable_delete_command, _("\
15474 Enable some breakpoints and delete when hit.\n\
15475 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15476 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15477 &enablebreaklist);
15478
15479 add_cmd ("count", no_class, enable_count_command, _("\
15480 Enable some breakpoints for COUNT hits.\n\
15481 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15482 If a breakpoint is hit while enabled in this fashion,\n\
15483 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15484 &enablebreaklist);
15485
15486 add_cmd ("delete", no_class, enable_delete_command, _("\
15487 Enable some breakpoints and delete when hit.\n\
15488 Usage: enable delete BREAKPOINTNUM...\n\
15489 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15490 &enablelist);
15491
15492 add_cmd ("once", no_class, enable_once_command, _("\
15493 Enable some breakpoints for one hit.\n\
15494 Usage: enable once BREAKPOINTNUM...\n\
15495 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15496 &enablelist);
15497
15498 add_cmd ("count", no_class, enable_count_command, _("\
15499 Enable some breakpoints for COUNT hits.\n\
15500 Usage: enable count COUNT BREAKPOINTNUM...\n\
15501 If a breakpoint is hit while enabled in this fashion,\n\
15502 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15503 &enablelist);
15504
15505 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15506 Disable all or some breakpoints.\n\
15507 Usage: disable [BREAKPOINTNUM]...\n\
15508 Arguments are breakpoint numbers with spaces in between.\n\
15509 To disable all breakpoints, give no argument.\n\
15510 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15511 &disablelist, "disable ", 1, &cmdlist);
15512 add_com_alias ("dis", "disable", class_breakpoint, 1);
15513 add_com_alias ("disa", "disable", class_breakpoint, 1);
15514
15515 add_cmd ("breakpoints", class_alias, disable_command, _("\
15516 Disable all or some breakpoints.\n\
15517 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15518 Arguments are breakpoint numbers with spaces in between.\n\
15519 To disable all breakpoints, give no argument.\n\
15520 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15521 This command may be abbreviated \"disable\"."),
15522 &disablelist);
15523
15524 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15525 Delete all or some breakpoints.\n\
15526 Usage: delete [BREAKPOINTNUM]...\n\
15527 Arguments are breakpoint numbers with spaces in between.\n\
15528 To delete all breakpoints, give no argument.\n\
15529 \n\
15530 Also a prefix command for deletion of other GDB objects."),
15531 &deletelist, "delete ", 1, &cmdlist);
15532 add_com_alias ("d", "delete", class_breakpoint, 1);
15533 add_com_alias ("del", "delete", class_breakpoint, 1);
15534
15535 add_cmd ("breakpoints", class_alias, delete_command, _("\
15536 Delete all or some breakpoints or auto-display expressions.\n\
15537 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15538 Arguments are breakpoint numbers with spaces in between.\n\
15539 To delete all breakpoints, give no argument.\n\
15540 This command may be abbreviated \"delete\"."),
15541 &deletelist);
15542
15543 add_com ("clear", class_breakpoint, clear_command, _("\
15544 Clear breakpoint at specified location.\n\
15545 Argument may be a linespec, explicit, or address location as described below.\n\
15546 \n\
15547 With no argument, clears all breakpoints in the line that the selected frame\n\
15548 is executing in.\n"
15549 "\n" LOCATION_HELP_STRING "\n\n\
15550 See also the \"delete\" command which clears breakpoints by number."));
15551 add_com_alias ("cl", "clear", class_breakpoint, 1);
15552
15553 c = add_com ("break", class_breakpoint, break_command, _("\
15554 Set breakpoint at specified location.\n"
15555 BREAK_ARGS_HELP ("break")));
15556 set_cmd_completer (c, location_completer);
15557
15558 add_com_alias ("b", "break", class_run, 1);
15559 add_com_alias ("br", "break", class_run, 1);
15560 add_com_alias ("bre", "break", class_run, 1);
15561 add_com_alias ("brea", "break", class_run, 1);
15562
15563 if (dbx_commands)
15564 {
15565 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15566 Break in function/address or break at a line in the current file."),
15567 &stoplist, "stop ", 1, &cmdlist);
15568 add_cmd ("in", class_breakpoint, stopin_command,
15569 _("Break in function or address."), &stoplist);
15570 add_cmd ("at", class_breakpoint, stopat_command,
15571 _("Break at a line in the current file."), &stoplist);
15572 add_com ("status", class_info, info_breakpoints_command, _("\
15573 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15574 The \"Type\" column indicates one of:\n\
15575 \tbreakpoint - normal breakpoint\n\
15576 \twatchpoint - watchpoint\n\
15577 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15578 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15579 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15580 address and file/line number respectively.\n\
15581 \n\
15582 Convenience variable \"$_\" and default examine address for \"x\"\n\
15583 are set to the address of the last breakpoint listed unless the command\n\
15584 is prefixed with \"server \".\n\n\
15585 Convenience variable \"$bpnum\" contains the number of the last\n\
15586 breakpoint set."));
15587 }
15588
15589 add_info ("breakpoints", info_breakpoints_command, _("\
15590 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15591 The \"Type\" column indicates one of:\n\
15592 \tbreakpoint - normal breakpoint\n\
15593 \twatchpoint - watchpoint\n\
15594 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15595 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15596 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15597 address and file/line number respectively.\n\
15598 \n\
15599 Convenience variable \"$_\" and default examine address for \"x\"\n\
15600 are set to the address of the last breakpoint listed unless the command\n\
15601 is prefixed with \"server \".\n\n\
15602 Convenience variable \"$bpnum\" contains the number of the last\n\
15603 breakpoint set."));
15604
15605 add_info_alias ("b", "breakpoints", 1);
15606
15607 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15608 Status of all breakpoints, or breakpoint number NUMBER.\n\
15609 The \"Type\" column indicates one of:\n\
15610 \tbreakpoint - normal breakpoint\n\
15611 \twatchpoint - watchpoint\n\
15612 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15613 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15614 \tuntil - internal breakpoint used by the \"until\" command\n\
15615 \tfinish - internal breakpoint used by the \"finish\" command\n\
15616 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15617 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15618 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15619 address and file/line number respectively.\n\
15620 \n\
15621 Convenience variable \"$_\" and default examine address for \"x\"\n\
15622 are set to the address of the last breakpoint listed unless the command\n\
15623 is prefixed with \"server \".\n\n\
15624 Convenience variable \"$bpnum\" contains the number of the last\n\
15625 breakpoint set."),
15626 &maintenanceinfolist);
15627
15628 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15629 Set catchpoints to catch events."),
15630 &catch_cmdlist, "catch ",
15631 0/*allow-unknown*/, &cmdlist);
15632
15633 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15634 Set temporary catchpoints to catch events."),
15635 &tcatch_cmdlist, "tcatch ",
15636 0/*allow-unknown*/, &cmdlist);
15637
15638 add_catch_command ("fork", _("Catch calls to fork."),
15639 catch_fork_command_1,
15640 NULL,
15641 (void *) (uintptr_t) catch_fork_permanent,
15642 (void *) (uintptr_t) catch_fork_temporary);
15643 add_catch_command ("vfork", _("Catch calls to vfork."),
15644 catch_fork_command_1,
15645 NULL,
15646 (void *) (uintptr_t) catch_vfork_permanent,
15647 (void *) (uintptr_t) catch_vfork_temporary);
15648 add_catch_command ("exec", _("Catch calls to exec."),
15649 catch_exec_command_1,
15650 NULL,
15651 CATCH_PERMANENT,
15652 CATCH_TEMPORARY);
15653 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15654 Usage: catch load [REGEX]\n\
15655 If REGEX is given, only stop for libraries matching the regular expression."),
15656 catch_load_command_1,
15657 NULL,
15658 CATCH_PERMANENT,
15659 CATCH_TEMPORARY);
15660 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15661 Usage: catch unload [REGEX]\n\
15662 If REGEX is given, only stop for libraries matching the regular expression."),
15663 catch_unload_command_1,
15664 NULL,
15665 CATCH_PERMANENT,
15666 CATCH_TEMPORARY);
15667
15668 c = add_com ("watch", class_breakpoint, watch_command, _("\
15669 Set a watchpoint for an expression.\n\
15670 Usage: watch [-l|-location] EXPRESSION\n\
15671 A watchpoint stops execution of your program whenever the value of\n\
15672 an expression changes.\n\
15673 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15674 the memory to which it refers."));
15675 set_cmd_completer (c, expression_completer);
15676
15677 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15678 Set a read watchpoint for an expression.\n\
15679 Usage: rwatch [-l|-location] EXPRESSION\n\
15680 A watchpoint stops execution of your program whenever the value of\n\
15681 an expression is read.\n\
15682 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15683 the memory to which it refers."));
15684 set_cmd_completer (c, expression_completer);
15685
15686 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15687 Set a watchpoint for an expression.\n\
15688 Usage: awatch [-l|-location] EXPRESSION\n\
15689 A watchpoint stops execution of your program whenever the value of\n\
15690 an expression is either read or written.\n\
15691 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15692 the memory to which it refers."));
15693 set_cmd_completer (c, expression_completer);
15694
15695 add_info ("watchpoints", info_watchpoints_command, _("\
15696 Status of specified watchpoints (all watchpoints if no argument)."));
15697
15698 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15699 respond to changes - contrary to the description. */
15700 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15701 &can_use_hw_watchpoints, _("\
15702 Set debugger's willingness to use watchpoint hardware."), _("\
15703 Show debugger's willingness to use watchpoint hardware."), _("\
15704 If zero, gdb will not use hardware for new watchpoints, even if\n\
15705 such is available. (However, any hardware watchpoints that were\n\
15706 created before setting this to nonzero, will continue to use watchpoint\n\
15707 hardware.)"),
15708 NULL,
15709 show_can_use_hw_watchpoints,
15710 &setlist, &showlist);
15711
15712 can_use_hw_watchpoints = 1;
15713
15714 /* Tracepoint manipulation commands. */
15715
15716 c = add_com ("trace", class_breakpoint, trace_command, _("\
15717 Set a tracepoint at specified location.\n\
15718 \n"
15719 BREAK_ARGS_HELP ("trace") "\n\
15720 Do \"help tracepoints\" for info on other tracepoint commands."));
15721 set_cmd_completer (c, location_completer);
15722
15723 add_com_alias ("tp", "trace", class_alias, 0);
15724 add_com_alias ("tr", "trace", class_alias, 1);
15725 add_com_alias ("tra", "trace", class_alias, 1);
15726 add_com_alias ("trac", "trace", class_alias, 1);
15727
15728 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15729 Set a fast tracepoint at specified location.\n\
15730 \n"
15731 BREAK_ARGS_HELP ("ftrace") "\n\
15732 Do \"help tracepoints\" for info on other tracepoint commands."));
15733 set_cmd_completer (c, location_completer);
15734
15735 c = add_com ("strace", class_breakpoint, strace_command, _("\
15736 Set a static tracepoint at location or marker.\n\
15737 \n\
15738 strace [LOCATION] [if CONDITION]\n\
15739 LOCATION may be a linespec, explicit, or address location (described below) \n\
15740 or -m MARKER_ID.\n\n\
15741 If a marker id is specified, probe the marker with that name. With\n\
15742 no LOCATION, uses current execution address of the selected stack frame.\n\
15743 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15744 This collects arbitrary user data passed in the probe point call to the\n\
15745 tracing library. You can inspect it when analyzing the trace buffer,\n\
15746 by printing the $_sdata variable like any other convenience variable.\n\
15747 \n\
15748 CONDITION is a boolean expression.\n\
15749 \n" LOCATION_HELP_STRING "\n\n\
15750 Multiple tracepoints at one place are permitted, and useful if their\n\
15751 conditions are different.\n\
15752 \n\
15753 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15754 Do \"help tracepoints\" for info on other tracepoint commands."));
15755 set_cmd_completer (c, location_completer);
15756
15757 add_info ("tracepoints", info_tracepoints_command, _("\
15758 Status of specified tracepoints (all tracepoints if no argument).\n\
15759 Convenience variable \"$tpnum\" contains the number of the\n\
15760 last tracepoint set."));
15761
15762 add_info_alias ("tp", "tracepoints", 1);
15763
15764 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15765 Delete specified tracepoints.\n\
15766 Arguments are tracepoint numbers, separated by spaces.\n\
15767 No argument means delete all tracepoints."),
15768 &deletelist);
15769 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15770
15771 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15772 Disable specified tracepoints.\n\
15773 Arguments are tracepoint numbers, separated by spaces.\n\
15774 No argument means disable all tracepoints."),
15775 &disablelist);
15776 deprecate_cmd (c, "disable");
15777
15778 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15779 Enable specified tracepoints.\n\
15780 Arguments are tracepoint numbers, separated by spaces.\n\
15781 No argument means enable all tracepoints."),
15782 &enablelist);
15783 deprecate_cmd (c, "enable");
15784
15785 add_com ("passcount", class_trace, trace_pass_command, _("\
15786 Set the passcount for a tracepoint.\n\
15787 The trace will end when the tracepoint has been passed 'count' times.\n\
15788 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15789 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15790
15791 add_prefix_cmd ("save", class_breakpoint, save_command,
15792 _("Save breakpoint definitions as a script."),
15793 &save_cmdlist, "save ",
15794 0/*allow-unknown*/, &cmdlist);
15795
15796 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15797 Save current breakpoint definitions as a script.\n\
15798 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15799 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15800 session to restore them."),
15801 &save_cmdlist);
15802 set_cmd_completer (c, filename_completer);
15803
15804 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15805 Save current tracepoint definitions as a script.\n\
15806 Use the 'source' command in another debug session to restore them."),
15807 &save_cmdlist);
15808 set_cmd_completer (c, filename_completer);
15809
15810 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15811 deprecate_cmd (c, "save tracepoints");
15812
15813 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
15814 Breakpoint specific settings.\n\
15815 Configure various breakpoint-specific variables such as\n\
15816 pending breakpoint behavior."),
15817 &breakpoint_set_cmdlist, "set breakpoint ",
15818 0/*allow-unknown*/, &setlist);
15819 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
15820 Breakpoint specific settings.\n\
15821 Configure various breakpoint-specific variables such as\n\
15822 pending breakpoint behavior."),
15823 &breakpoint_show_cmdlist, "show breakpoint ",
15824 0/*allow-unknown*/, &showlist);
15825
15826 add_setshow_auto_boolean_cmd ("pending", no_class,
15827 &pending_break_support, _("\
15828 Set debugger's behavior regarding pending breakpoints."), _("\
15829 Show debugger's behavior regarding pending breakpoints."), _("\
15830 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15831 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15832 an error. If auto, an unrecognized breakpoint location results in a\n\
15833 user-query to see if a pending breakpoint should be created."),
15834 NULL,
15835 show_pending_break_support,
15836 &breakpoint_set_cmdlist,
15837 &breakpoint_show_cmdlist);
15838
15839 pending_break_support = AUTO_BOOLEAN_AUTO;
15840
15841 add_setshow_boolean_cmd ("auto-hw", no_class,
15842 &automatic_hardware_breakpoints, _("\
15843 Set automatic usage of hardware breakpoints."), _("\
15844 Show automatic usage of hardware breakpoints."), _("\
15845 If set, the debugger will automatically use hardware breakpoints for\n\
15846 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15847 a warning will be emitted for such breakpoints."),
15848 NULL,
15849 show_automatic_hardware_breakpoints,
15850 &breakpoint_set_cmdlist,
15851 &breakpoint_show_cmdlist);
15852
15853 add_setshow_boolean_cmd ("always-inserted", class_support,
15854 &always_inserted_mode, _("\
15855 Set mode for inserting breakpoints."), _("\
15856 Show mode for inserting breakpoints."), _("\
15857 When this mode is on, breakpoints are inserted immediately as soon as\n\
15858 they're created, kept inserted even when execution stops, and removed\n\
15859 only when the user deletes them. When this mode is off (the default),\n\
15860 breakpoints are inserted only when execution continues, and removed\n\
15861 when execution stops."),
15862 NULL,
15863 &show_always_inserted_mode,
15864 &breakpoint_set_cmdlist,
15865 &breakpoint_show_cmdlist);
15866
15867 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15868 condition_evaluation_enums,
15869 &condition_evaluation_mode_1, _("\
15870 Set mode of breakpoint condition evaluation."), _("\
15871 Show mode of breakpoint condition evaluation."), _("\
15872 When this is set to \"host\", breakpoint conditions will be\n\
15873 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15874 breakpoint conditions will be downloaded to the target (if the target\n\
15875 supports such feature) and conditions will be evaluated on the target's side.\n\
15876 If this is set to \"auto\" (default), this will be automatically set to\n\
15877 \"target\" if it supports condition evaluation, otherwise it will\n\
15878 be set to \"gdb\""),
15879 &set_condition_evaluation_mode,
15880 &show_condition_evaluation_mode,
15881 &breakpoint_set_cmdlist,
15882 &breakpoint_show_cmdlist);
15883
15884 add_com ("break-range", class_breakpoint, break_range_command, _("\
15885 Set a breakpoint for an address range.\n\
15886 break-range START-LOCATION, END-LOCATION\n\
15887 where START-LOCATION and END-LOCATION can be one of the following:\n\
15888 LINENUM, for that line in the current file,\n\
15889 FILE:LINENUM, for that line in that file,\n\
15890 +OFFSET, for that number of lines after the current line\n\
15891 or the start of the range\n\
15892 FUNCTION, for the first line in that function,\n\
15893 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15894 *ADDRESS, for the instruction at that address.\n\
15895 \n\
15896 The breakpoint will stop execution of the inferior whenever it executes\n\
15897 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15898 range (including START-LOCATION and END-LOCATION)."));
15899
15900 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15901 Set a dynamic printf at specified location.\n\
15902 dprintf location,format string,arg1,arg2,...\n\
15903 location may be a linespec, explicit, or address location.\n"
15904 "\n" LOCATION_HELP_STRING));
15905 set_cmd_completer (c, location_completer);
15906
15907 add_setshow_enum_cmd ("dprintf-style", class_support,
15908 dprintf_style_enums, &dprintf_style, _("\
15909 Set the style of usage for dynamic printf."), _("\
15910 Show the style of usage for dynamic printf."), _("\
15911 This setting chooses how GDB will do a dynamic printf.\n\
15912 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15913 console, as with the \"printf\" command.\n\
15914 If the value is \"call\", the print is done by calling a function in your\n\
15915 program; by default printf(), but you can choose a different function or\n\
15916 output stream by setting dprintf-function and dprintf-channel."),
15917 update_dprintf_commands, NULL,
15918 &setlist, &showlist);
15919
15920 dprintf_function = xstrdup ("printf");
15921 add_setshow_string_cmd ("dprintf-function", class_support,
15922 &dprintf_function, _("\
15923 Set the function to use for dynamic printf."), _("\
15924 Show the function to use for dynamic printf."), NULL,
15925 update_dprintf_commands, NULL,
15926 &setlist, &showlist);
15927
15928 dprintf_channel = xstrdup ("");
15929 add_setshow_string_cmd ("dprintf-channel", class_support,
15930 &dprintf_channel, _("\
15931 Set the channel to use for dynamic printf."), _("\
15932 Show the channel to use for dynamic printf."), NULL,
15933 update_dprintf_commands, NULL,
15934 &setlist, &showlist);
15935
15936 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15937 &disconnected_dprintf, _("\
15938 Set whether dprintf continues after GDB disconnects."), _("\
15939 Show whether dprintf continues after GDB disconnects."), _("\
15940 Use this to let dprintf commands continue to hit and produce output\n\
15941 even if GDB disconnects or detaches from the target."),
15942 NULL,
15943 NULL,
15944 &setlist, &showlist);
15945
15946 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15947 Target agent only formatted printing, like the C \"printf\" function.\n\
15948 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15949 This supports most C printf format specifications, like %s, %d, etc.\n\
15950 This is useful for formatted output in user-defined commands."));
15951
15952 automatic_hardware_breakpoints = true;
15953
15954 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
15955 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
15956 }
This page took 0.502213 seconds and 4 git commands to generate.