Merge branch 'master' into merge-job
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
4 Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "arch-utils.h"
23 #include <ctype.h>
24 #include "hashtab.h"
25 #include "symtab.h"
26 #include "frame.h"
27 #include "breakpoint.h"
28 #include "tracepoint.h"
29 #include "gdbtypes.h"
30 #include "expression.h"
31 #include "gdbcore.h"
32 #include "gdbcmd.h"
33 #include "value.h"
34 #include "command.h"
35 #include "inferior.h"
36 #include "infrun.h"
37 #include "gdbthread.h"
38 #include "target.h"
39 #include "language.h"
40 #include "gdb-demangle.h"
41 #include "filenames.h"
42 #include "annotate.h"
43 #include "symfile.h"
44 #include "objfiles.h"
45 #include "source.h"
46 #include "linespec.h"
47 #include "completer.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observable.h"
54 #include "memattr.h"
55 #include "ada-lang.h"
56 #include "top.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "parser-defs.h"
60 #include "gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "stack.h"
64 #include "ax-gdb.h"
65 #include "dummy-frame.h"
66 #include "interps.h"
67 #include "gdbsupport/format.h"
68 #include "thread-fsm.h"
69 #include "tid-parse.h"
70 #include "cli/cli-style.h"
71
72 /* readline include files */
73 #include "readline/tilde.h"
74
75 /* readline defines this. */
76 #undef savestring
77
78 #include "mi/mi-common.h"
79 #include "extension.h"
80 #include <algorithm>
81 #include "progspace-and-thread.h"
82 #include "gdbsupport/array-view.h"
83 #include "gdbsupport/gdb_optional.h"
84
85 /* Prototypes for local functions. */
86
87 static void map_breakpoint_numbers (const char *,
88 gdb::function_view<void (breakpoint *)>);
89
90 static void breakpoint_re_set_default (struct breakpoint *);
91
92 static void
93 create_sals_from_location_default (const struct event_location *location,
94 struct linespec_result *canonical,
95 enum bptype type_wanted);
96
97 static void create_breakpoints_sal_default (struct gdbarch *,
98 struct linespec_result *,
99 gdb::unique_xmalloc_ptr<char>,
100 gdb::unique_xmalloc_ptr<char>,
101 enum bptype,
102 enum bpdisp, int, int,
103 int,
104 const struct breakpoint_ops *,
105 int, int, int, unsigned);
106
107 static std::vector<symtab_and_line> decode_location_default
108 (struct breakpoint *b, const struct event_location *location,
109 struct program_space *search_pspace);
110
111 static int can_use_hardware_watchpoint
112 (const std::vector<value_ref_ptr> &vals);
113
114 static void mention (struct breakpoint *);
115
116 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
117 enum bptype,
118 const struct breakpoint_ops *);
119 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
120 const struct symtab_and_line *);
121
122 /* This function is used in gdbtk sources and thus can not be made
123 static. */
124 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
125 struct symtab_and_line,
126 enum bptype,
127 const struct breakpoint_ops *);
128
129 static struct breakpoint *
130 momentary_breakpoint_from_master (struct breakpoint *orig,
131 enum bptype type,
132 const struct breakpoint_ops *ops,
133 int loc_enabled);
134
135 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
136
137 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
138 CORE_ADDR bpaddr,
139 enum bptype bptype);
140
141 static void describe_other_breakpoints (struct gdbarch *,
142 struct program_space *, CORE_ADDR,
143 struct obj_section *, int);
144
145 static int watchpoint_locations_match (struct bp_location *loc1,
146 struct bp_location *loc2);
147
148 static int breakpoint_location_address_match (struct bp_location *bl,
149 const struct address_space *aspace,
150 CORE_ADDR addr);
151
152 static int breakpoint_location_address_range_overlap (struct bp_location *,
153 const address_space *,
154 CORE_ADDR, int);
155
156 static int remove_breakpoint (struct bp_location *);
157 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
158
159 static enum print_stop_action print_bp_stop_message (bpstat bs);
160
161 static int hw_breakpoint_used_count (void);
162
163 static int hw_watchpoint_use_count (struct breakpoint *);
164
165 static int hw_watchpoint_used_count_others (struct breakpoint *except,
166 enum bptype type,
167 int *other_type_used);
168
169 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
170 int count);
171
172 static void free_bp_location (struct bp_location *loc);
173 static void incref_bp_location (struct bp_location *loc);
174 static void decref_bp_location (struct bp_location **loc);
175
176 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
177
178 /* update_global_location_list's modes of operation wrt to whether to
179 insert locations now. */
180 enum ugll_insert_mode
181 {
182 /* Don't insert any breakpoint locations into the inferior, only
183 remove already-inserted locations that no longer should be
184 inserted. Functions that delete a breakpoint or breakpoints
185 should specify this mode, so that deleting a breakpoint doesn't
186 have the side effect of inserting the locations of other
187 breakpoints that are marked not-inserted, but should_be_inserted
188 returns true on them.
189
190 This behavior is useful is situations close to tear-down -- e.g.,
191 after an exec, while the target still has execution, but
192 breakpoint shadows of the previous executable image should *NOT*
193 be restored to the new image; or before detaching, where the
194 target still has execution and wants to delete breakpoints from
195 GDB's lists, and all breakpoints had already been removed from
196 the inferior. */
197 UGLL_DONT_INSERT,
198
199 /* May insert breakpoints iff breakpoints_should_be_inserted_now
200 claims breakpoints should be inserted now. */
201 UGLL_MAY_INSERT,
202
203 /* Insert locations now, irrespective of
204 breakpoints_should_be_inserted_now. E.g., say all threads are
205 stopped right now, and the user did "continue". We need to
206 insert breakpoints _before_ resuming the target, but
207 UGLL_MAY_INSERT wouldn't insert them, because
208 breakpoints_should_be_inserted_now returns false at that point,
209 as no thread is running yet. */
210 UGLL_INSERT
211 };
212
213 static void update_global_location_list (enum ugll_insert_mode);
214
215 static void update_global_location_list_nothrow (enum ugll_insert_mode);
216
217 static void insert_breakpoint_locations (void);
218
219 static void trace_pass_command (const char *, int);
220
221 static void set_tracepoint_count (int num);
222
223 static bool is_masked_watchpoint (const struct breakpoint *b);
224
225 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
226
227 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
228 otherwise. */
229
230 static int strace_marker_p (struct breakpoint *b);
231
232 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
233 that are implemented on top of software or hardware breakpoints
234 (user breakpoints, internal and momentary breakpoints, etc.). */
235 static struct breakpoint_ops bkpt_base_breakpoint_ops;
236
237 /* Internal breakpoints class type. */
238 static struct breakpoint_ops internal_breakpoint_ops;
239
240 /* Momentary breakpoints class type. */
241 static struct breakpoint_ops momentary_breakpoint_ops;
242
243 /* The breakpoint_ops structure to be used in regular user created
244 breakpoints. */
245 struct breakpoint_ops bkpt_breakpoint_ops;
246
247 /* Breakpoints set on probes. */
248 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
249
250 /* Tracepoints set on probes. */
251 static struct breakpoint_ops tracepoint_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 /* FIXME: this is a temporary workaround to make sure waves created while
372 all known threads are stopped, and the gdb prompt is presented, do not
373 execute past the enabled breakpoints. */
374 static bool always_inserted_mode = true;
375
376 static void
377 show_always_inserted_mode (struct ui_file *file, int from_tty,
378 struct cmd_list_element *c, const char *value)
379 {
380 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
381 value);
382 }
383
384 /* See breakpoint.h. */
385
386 int
387 breakpoints_should_be_inserted_now (void)
388 {
389 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
390 {
391 /* If breakpoints are global, they should be inserted even if no
392 thread under gdb's control is running, or even if there are
393 no threads under GDB's control yet. */
394 return 1;
395 }
396 else if (target_has_execution)
397 {
398 if (always_inserted_mode)
399 {
400 /* The user wants breakpoints inserted even if all threads
401 are stopped. */
402 return 1;
403 }
404
405 if (threads_are_executing ())
406 return 1;
407
408 /* Don't remove breakpoints yet if, even though all threads are
409 stopped, we still have events to process. */
410 for (thread_info *tp : all_non_exited_threads ())
411 if (tp->resumed
412 && tp->suspend.waitstatus_pending_p)
413 return 1;
414 }
415 return 0;
416 }
417
418 static const char condition_evaluation_both[] = "host or target";
419
420 /* Modes for breakpoint condition evaluation. */
421 static const char condition_evaluation_auto[] = "auto";
422 static const char condition_evaluation_host[] = "host";
423 static const char condition_evaluation_target[] = "target";
424 static const char *const condition_evaluation_enums[] = {
425 condition_evaluation_auto,
426 condition_evaluation_host,
427 condition_evaluation_target,
428 NULL
429 };
430
431 /* Global that holds the current mode for breakpoint condition evaluation. */
432 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434 /* Global that we use to display information to the user (gets its value from
435 condition_evaluation_mode_1. */
436 static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438 /* Translate a condition evaluation mode MODE into either "host"
439 or "target". This is used mostly to translate from "auto" to the
440 real setting that is being used. It returns the translated
441 evaluation mode. */
442
443 static const char *
444 translate_condition_evaluation_mode (const char *mode)
445 {
446 if (mode == condition_evaluation_auto)
447 {
448 if (target_supports_evaluation_of_breakpoint_conditions ())
449 return condition_evaluation_target;
450 else
451 return condition_evaluation_host;
452 }
453 else
454 return mode;
455 }
456
457 /* Discovers what condition_evaluation_auto translates to. */
458
459 static const char *
460 breakpoint_condition_evaluation_mode (void)
461 {
462 return translate_condition_evaluation_mode (condition_evaluation_mode);
463 }
464
465 /* Return true if GDB should evaluate breakpoint conditions or false
466 otherwise. */
467
468 static int
469 gdb_evaluates_breakpoint_condition_p (void)
470 {
471 const char *mode = breakpoint_condition_evaluation_mode ();
472
473 return (mode == condition_evaluation_host);
474 }
475
476 /* Are we executing breakpoint commands? */
477 static int executing_breakpoint_commands;
478
479 /* Are overlay event breakpoints enabled? */
480 static int overlay_events_enabled;
481
482 /* See description in breakpoint.h. */
483 bool target_exact_watchpoints = false;
484
485 /* Walk the following statement or block through all breakpoints.
486 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
487 current breakpoint. */
488
489 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
490
491 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
492 for (B = breakpoint_chain; \
493 B ? (TMP=B->next, 1): 0; \
494 B = TMP)
495
496 /* Similar iterator for the low-level breakpoints. SAFE variant is
497 not provided so update_global_location_list must not be called
498 while executing the block of ALL_BP_LOCATIONS. */
499
500 #define ALL_BP_LOCATIONS(B,BP_TMP) \
501 for (BP_TMP = bp_locations; \
502 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
503 BP_TMP++)
504
505 /* Iterates through locations with address ADDRESS for the currently selected
506 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
507 to where the loop should start from.
508 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
509 appropriate location to start with. */
510
511 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
512 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
513 BP_LOCP_TMP = BP_LOCP_START; \
514 BP_LOCP_START \
515 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
516 && (*BP_LOCP_TMP)->address == ADDRESS); \
517 BP_LOCP_TMP++)
518
519 /* Iterator for tracepoints only. */
520
521 #define ALL_TRACEPOINTS(B) \
522 for (B = breakpoint_chain; B; B = B->next) \
523 if (is_tracepoint (B))
524
525 /* Chains of all breakpoints defined. */
526
527 static struct breakpoint *breakpoint_chain;
528
529 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
530
531 static struct bp_location **bp_locations;
532
533 /* Number of elements of BP_LOCATIONS. */
534
535 static unsigned bp_locations_count;
536
537 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
538 ADDRESS for the current elements of BP_LOCATIONS which get a valid
539 result from bp_location_has_shadow. You can use it for roughly
540 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
541 an address you need to read. */
542
543 static CORE_ADDR bp_locations_placed_address_before_address_max;
544
545 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
546 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
547 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
548 You can use it for roughly limiting the subrange of BP_LOCATIONS to
549 scan for shadow bytes for an address you need to read. */
550
551 static CORE_ADDR bp_locations_shadow_len_after_address_max;
552
553 /* The locations that no longer correspond to any breakpoint, unlinked
554 from the bp_locations array, but for which a hit may still be
555 reported by a target. */
556 static std::vector<bp_location *> moribund_locations;
557
558 /* Number of last breakpoint made. */
559
560 static int breakpoint_count;
561
562 /* The value of `breakpoint_count' before the last command that
563 created breakpoints. If the last (break-like) command created more
564 than one breakpoint, then the difference between BREAKPOINT_COUNT
565 and PREV_BREAKPOINT_COUNT is more than one. */
566 static int prev_breakpoint_count;
567
568 /* Number of last tracepoint made. */
569
570 static int tracepoint_count;
571
572 static struct cmd_list_element *breakpoint_set_cmdlist;
573 static struct cmd_list_element *breakpoint_show_cmdlist;
574 struct cmd_list_element *save_cmdlist;
575
576 /* See declaration at breakpoint.h. */
577
578 struct breakpoint *
579 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
580 void *user_data)
581 {
582 struct breakpoint *b = NULL;
583
584 ALL_BREAKPOINTS (b)
585 {
586 if (func (b, user_data) != 0)
587 break;
588 }
589
590 return b;
591 }
592
593 /* Return whether a breakpoint is an active enabled breakpoint. */
594 static int
595 breakpoint_enabled (struct breakpoint *b)
596 {
597 return (b->enable_state == bp_enabled);
598 }
599
600 /* Set breakpoint count to NUM. */
601
602 static void
603 set_breakpoint_count (int num)
604 {
605 prev_breakpoint_count = breakpoint_count;
606 breakpoint_count = num;
607 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
608 }
609
610 /* Used by `start_rbreak_breakpoints' below, to record the current
611 breakpoint count before "rbreak" creates any breakpoint. */
612 static int rbreak_start_breakpoint_count;
613
614 /* Called at the start an "rbreak" command to record the first
615 breakpoint made. */
616
617 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
618 {
619 rbreak_start_breakpoint_count = breakpoint_count;
620 }
621
622 /* Called at the end of an "rbreak" command to record the last
623 breakpoint made. */
624
625 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
626 {
627 prev_breakpoint_count = rbreak_start_breakpoint_count;
628 }
629
630 /* Used in run_command to zero the hit count when a new run starts. */
631
632 void
633 clear_breakpoint_hit_counts (void)
634 {
635 struct breakpoint *b;
636
637 ALL_BREAKPOINTS (b)
638 b->hit_count = 0;
639 }
640
641 \f
642 /* Return the breakpoint with the specified number, or NULL
643 if the number does not refer to an existing breakpoint. */
644
645 struct breakpoint *
646 get_breakpoint (int num)
647 {
648 struct breakpoint *b;
649
650 ALL_BREAKPOINTS (b)
651 if (b->number == num)
652 return b;
653
654 return NULL;
655 }
656
657 \f
658
659 /* Mark locations as "conditions have changed" in case the target supports
660 evaluating conditions on its side. */
661
662 static void
663 mark_breakpoint_modified (struct breakpoint *b)
664 {
665 struct bp_location *loc;
666
667 /* This is only meaningful if the target is
668 evaluating conditions and if the user has
669 opted for condition evaluation on the target's
670 side. */
671 if (gdb_evaluates_breakpoint_condition_p ()
672 || !target_supports_evaluation_of_breakpoint_conditions ())
673 return;
674
675 if (!is_breakpoint (b))
676 return;
677
678 for (loc = b->loc; loc; loc = loc->next)
679 loc->condition_changed = condition_modified;
680 }
681
682 /* Mark location as "conditions have changed" in case the target supports
683 evaluating conditions on its side. */
684
685 static void
686 mark_breakpoint_location_modified (struct bp_location *loc)
687 {
688 /* This is only meaningful if the target is
689 evaluating conditions and if the user has
690 opted for condition evaluation on the target's
691 side. */
692 if (gdb_evaluates_breakpoint_condition_p ()
693 || !target_supports_evaluation_of_breakpoint_conditions ())
694
695 return;
696
697 if (!is_breakpoint (loc->owner))
698 return;
699
700 loc->condition_changed = condition_modified;
701 }
702
703 /* Sets the condition-evaluation mode using the static global
704 condition_evaluation_mode. */
705
706 static void
707 set_condition_evaluation_mode (const char *args, int from_tty,
708 struct cmd_list_element *c)
709 {
710 const char *old_mode, *new_mode;
711
712 if ((condition_evaluation_mode_1 == condition_evaluation_target)
713 && !target_supports_evaluation_of_breakpoint_conditions ())
714 {
715 condition_evaluation_mode_1 = condition_evaluation_mode;
716 warning (_("Target does not support breakpoint condition evaluation.\n"
717 "Using host evaluation mode instead."));
718 return;
719 }
720
721 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
722 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
723
724 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
725 settings was "auto". */
726 condition_evaluation_mode = condition_evaluation_mode_1;
727
728 /* Only update the mode if the user picked a different one. */
729 if (new_mode != old_mode)
730 {
731 struct bp_location *loc, **loc_tmp;
732 /* If the user switched to a different evaluation mode, we
733 need to synch the changes with the target as follows:
734
735 "host" -> "target": Send all (valid) conditions to the target.
736 "target" -> "host": Remove all the conditions from the target.
737 */
738
739 if (new_mode == condition_evaluation_target)
740 {
741 /* Mark everything modified and synch conditions with the
742 target. */
743 ALL_BP_LOCATIONS (loc, loc_tmp)
744 mark_breakpoint_location_modified (loc);
745 }
746 else
747 {
748 /* Manually mark non-duplicate locations to synch conditions
749 with the target. We do this to remove all the conditions the
750 target knows about. */
751 ALL_BP_LOCATIONS (loc, loc_tmp)
752 if (is_breakpoint (loc->owner) && loc->inserted)
753 loc->needs_update = 1;
754 }
755
756 /* Do the update. */
757 update_global_location_list (UGLL_MAY_INSERT);
758 }
759
760 return;
761 }
762
763 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
764 what "auto" is translating to. */
765
766 static void
767 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
768 struct cmd_list_element *c, const char *value)
769 {
770 if (condition_evaluation_mode == condition_evaluation_auto)
771 fprintf_filtered (file,
772 _("Breakpoint condition evaluation "
773 "mode is %s (currently %s).\n"),
774 value,
775 breakpoint_condition_evaluation_mode ());
776 else
777 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
778 value);
779 }
780
781 /* A comparison function for bp_location AP and BP that is used by
782 bsearch. This comparison function only cares about addresses, unlike
783 the more general bp_location_is_less_than function. */
784
785 static int
786 bp_locations_compare_addrs (const void *ap, const void *bp)
787 {
788 const struct bp_location *a = *(const struct bp_location **) ap;
789 const struct bp_location *b = *(const struct bp_location **) bp;
790
791 if (a->address == b->address)
792 return 0;
793 else
794 return ((a->address > b->address) - (a->address < b->address));
795 }
796
797 /* Helper function to skip all bp_locations with addresses
798 less than ADDRESS. It returns the first bp_location that
799 is greater than or equal to ADDRESS. If none is found, just
800 return NULL. */
801
802 static struct bp_location **
803 get_first_locp_gte_addr (CORE_ADDR address)
804 {
805 struct bp_location dummy_loc;
806 struct bp_location *dummy_locp = &dummy_loc;
807 struct bp_location **locp_found = NULL;
808
809 /* Initialize the dummy location's address field. */
810 dummy_loc.address = address;
811
812 /* Find a close match to the first location at ADDRESS. */
813 locp_found = ((struct bp_location **)
814 bsearch (&dummy_locp, bp_locations, bp_locations_count,
815 sizeof (struct bp_location **),
816 bp_locations_compare_addrs));
817
818 /* Nothing was found, nothing left to do. */
819 if (locp_found == NULL)
820 return NULL;
821
822 /* We may have found a location that is at ADDRESS but is not the first in the
823 location's list. Go backwards (if possible) and locate the first one. */
824 while ((locp_found - 1) >= bp_locations
825 && (*(locp_found - 1))->address == address)
826 locp_found--;
827
828 return locp_found;
829 }
830
831 void
832 set_breakpoint_condition (struct breakpoint *b, const char *exp,
833 int from_tty)
834 {
835 xfree (b->cond_string);
836 b->cond_string = NULL;
837
838 if (is_watchpoint (b))
839 {
840 struct watchpoint *w = (struct watchpoint *) b;
841
842 w->cond_exp.reset ();
843 }
844 else
845 {
846 struct bp_location *loc;
847
848 for (loc = b->loc; loc; loc = loc->next)
849 {
850 loc->cond.reset ();
851
852 /* No need to free the condition agent expression
853 bytecode (if we have one). We will handle this
854 when we go through update_global_location_list. */
855 }
856 }
857
858 if (*exp == 0)
859 {
860 if (from_tty)
861 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
862 }
863 else
864 {
865 const char *arg = exp;
866
867 /* I don't know if it matters whether this is the string the user
868 typed in or the decompiled expression. */
869 b->cond_string = xstrdup (arg);
870 b->condition_not_parsed = 0;
871
872 if (is_watchpoint (b))
873 {
874 struct watchpoint *w = (struct watchpoint *) b;
875
876 innermost_block_tracker tracker;
877 arg = exp;
878 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
879 if (*arg)
880 error (_("Junk at end of expression"));
881 w->cond_exp_valid_block = tracker.block ();
882 }
883 else
884 {
885 struct bp_location *loc;
886
887 for (loc = b->loc; loc; loc = loc->next)
888 {
889 arg = exp;
890 loc->cond =
891 parse_exp_1 (&arg, loc->address,
892 block_for_pc (loc->address), 0);
893 if (*arg)
894 error (_("Junk at end of expression"));
895 }
896 }
897 }
898 mark_breakpoint_modified (b);
899
900 gdb::observers::breakpoint_modified.notify (b);
901 }
902
903 /* Completion for the "condition" command. */
904
905 static void
906 condition_completer (struct cmd_list_element *cmd,
907 completion_tracker &tracker,
908 const char *text, const char *word)
909 {
910 const char *space;
911
912 text = skip_spaces (text);
913 space = skip_to_space (text);
914 if (*space == '\0')
915 {
916 int len;
917 struct breakpoint *b;
918
919 if (text[0] == '$')
920 {
921 /* We don't support completion of history indices. */
922 if (!isdigit (text[1]))
923 complete_internalvar (tracker, &text[1]);
924 return;
925 }
926
927 /* We're completing the breakpoint number. */
928 len = strlen (text);
929
930 ALL_BREAKPOINTS (b)
931 {
932 char number[50];
933
934 xsnprintf (number, sizeof (number), "%d", b->number);
935
936 if (strncmp (number, text, len) == 0)
937 tracker.add_completion (make_unique_xstrdup (number));
938 }
939
940 return;
941 }
942
943 /* We're completing the expression part. */
944 text = skip_spaces (space);
945 expression_completer (cmd, tracker, text, word);
946 }
947
948 /* condition N EXP -- set break condition of breakpoint N to EXP. */
949
950 static void
951 condition_command (const char *arg, int from_tty)
952 {
953 struct breakpoint *b;
954 const char *p;
955 int bnum;
956
957 if (arg == 0)
958 error_no_arg (_("breakpoint number"));
959
960 p = arg;
961 bnum = get_number (&p);
962 if (bnum == 0)
963 error (_("Bad breakpoint argument: '%s'"), arg);
964
965 ALL_BREAKPOINTS (b)
966 if (b->number == bnum)
967 {
968 /* Check if this breakpoint has a "stop" method implemented in an
969 extension language. This method and conditions entered into GDB
970 from the CLI are mutually exclusive. */
971 const struct extension_language_defn *extlang
972 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
973
974 if (extlang != NULL)
975 {
976 error (_("Only one stop condition allowed. There is currently"
977 " a %s stop condition defined for this breakpoint."),
978 ext_lang_capitalized_name (extlang));
979 }
980 set_breakpoint_condition (b, p, from_tty);
981
982 if (is_breakpoint (b))
983 update_global_location_list (UGLL_MAY_INSERT);
984
985 return;
986 }
987
988 error (_("No breakpoint number %d."), bnum);
989 }
990
991 /* Check that COMMAND do not contain commands that are suitable
992 only for tracepoints and not suitable for ordinary breakpoints.
993 Throw if any such commands is found. */
994
995 static void
996 check_no_tracepoint_commands (struct command_line *commands)
997 {
998 struct command_line *c;
999
1000 for (c = commands; c; c = c->next)
1001 {
1002 if (c->control_type == while_stepping_control)
1003 error (_("The 'while-stepping' command can "
1004 "only be used for tracepoints"));
1005
1006 check_no_tracepoint_commands (c->body_list_0.get ());
1007 check_no_tracepoint_commands (c->body_list_1.get ());
1008
1009 /* Not that command parsing removes leading whitespace and comment
1010 lines and also empty lines. So, we only need to check for
1011 command directly. */
1012 if (strstr (c->line, "collect ") == c->line)
1013 error (_("The 'collect' command can only be used for tracepoints"));
1014
1015 if (strstr (c->line, "teval ") == c->line)
1016 error (_("The 'teval' command can only be used for tracepoints"));
1017 }
1018 }
1019
1020 struct longjmp_breakpoint : public breakpoint
1021 {
1022 ~longjmp_breakpoint () override;
1023 };
1024
1025 /* Encapsulate tests for different types of tracepoints. */
1026
1027 static bool
1028 is_tracepoint_type (bptype type)
1029 {
1030 return (type == bp_tracepoint
1031 || type == bp_fast_tracepoint
1032 || type == bp_static_tracepoint);
1033 }
1034
1035 static bool
1036 is_longjmp_type (bptype type)
1037 {
1038 return type == bp_longjmp || type == bp_exception;
1039 }
1040
1041 /* See breakpoint.h. */
1042
1043 bool
1044 is_tracepoint (const struct breakpoint *b)
1045 {
1046 return is_tracepoint_type (b->type);
1047 }
1048
1049 /* Factory function to create an appropriate instance of breakpoint given
1050 TYPE. */
1051
1052 static std::unique_ptr<breakpoint>
1053 new_breakpoint_from_type (bptype type)
1054 {
1055 breakpoint *b;
1056
1057 if (is_tracepoint_type (type))
1058 b = new tracepoint ();
1059 else if (is_longjmp_type (type))
1060 b = new longjmp_breakpoint ();
1061 else
1062 b = new breakpoint ();
1063
1064 return std::unique_ptr<breakpoint> (b);
1065 }
1066
1067 /* A helper function that validates that COMMANDS are valid for a
1068 breakpoint. This function will throw an exception if a problem is
1069 found. */
1070
1071 static void
1072 validate_commands_for_breakpoint (struct breakpoint *b,
1073 struct command_line *commands)
1074 {
1075 if (is_tracepoint (b))
1076 {
1077 struct tracepoint *t = (struct tracepoint *) b;
1078 struct command_line *c;
1079 struct command_line *while_stepping = 0;
1080
1081 /* Reset the while-stepping step count. The previous commands
1082 might have included a while-stepping action, while the new
1083 ones might not. */
1084 t->step_count = 0;
1085
1086 /* We need to verify that each top-level element of commands is
1087 valid for tracepoints, that there's at most one
1088 while-stepping element, and that the while-stepping's body
1089 has valid tracing commands excluding nested while-stepping.
1090 We also need to validate the tracepoint action line in the
1091 context of the tracepoint --- validate_actionline actually
1092 has side effects, like setting the tracepoint's
1093 while-stepping STEP_COUNT, in addition to checking if the
1094 collect/teval actions parse and make sense in the
1095 tracepoint's context. */
1096 for (c = commands; c; c = c->next)
1097 {
1098 if (c->control_type == while_stepping_control)
1099 {
1100 if (b->type == bp_fast_tracepoint)
1101 error (_("The 'while-stepping' command "
1102 "cannot be used for fast tracepoint"));
1103 else if (b->type == bp_static_tracepoint)
1104 error (_("The 'while-stepping' command "
1105 "cannot be used for static tracepoint"));
1106
1107 if (while_stepping)
1108 error (_("The 'while-stepping' command "
1109 "can be used only once"));
1110 else
1111 while_stepping = c;
1112 }
1113
1114 validate_actionline (c->line, b);
1115 }
1116 if (while_stepping)
1117 {
1118 struct command_line *c2;
1119
1120 gdb_assert (while_stepping->body_list_1 == nullptr);
1121 c2 = while_stepping->body_list_0.get ();
1122 for (; c2; c2 = c2->next)
1123 {
1124 if (c2->control_type == while_stepping_control)
1125 error (_("The 'while-stepping' command cannot be nested"));
1126 }
1127 }
1128 }
1129 else
1130 {
1131 check_no_tracepoint_commands (commands);
1132 }
1133 }
1134
1135 /* Return a vector of all the static tracepoints set at ADDR. The
1136 caller is responsible for releasing the vector. */
1137
1138 std::vector<breakpoint *>
1139 static_tracepoints_here (CORE_ADDR addr)
1140 {
1141 struct breakpoint *b;
1142 std::vector<breakpoint *> found;
1143 struct bp_location *loc;
1144
1145 ALL_BREAKPOINTS (b)
1146 if (b->type == bp_static_tracepoint)
1147 {
1148 for (loc = b->loc; loc; loc = loc->next)
1149 if (loc->address == addr)
1150 found.push_back (b);
1151 }
1152
1153 return found;
1154 }
1155
1156 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1157 validate that only allowed commands are included. */
1158
1159 void
1160 breakpoint_set_commands (struct breakpoint *b,
1161 counted_command_line &&commands)
1162 {
1163 validate_commands_for_breakpoint (b, commands.get ());
1164
1165 b->commands = std::move (commands);
1166 gdb::observers::breakpoint_modified.notify (b);
1167 }
1168
1169 /* Set the internal `silent' flag on the breakpoint. Note that this
1170 is not the same as the "silent" that may appear in the breakpoint's
1171 commands. */
1172
1173 void
1174 breakpoint_set_silent (struct breakpoint *b, int silent)
1175 {
1176 int old_silent = b->silent;
1177
1178 b->silent = silent;
1179 if (old_silent != silent)
1180 gdb::observers::breakpoint_modified.notify (b);
1181 }
1182
1183 /* Set the thread for this breakpoint. If THREAD is -1, make the
1184 breakpoint work for any thread. */
1185
1186 void
1187 breakpoint_set_thread (struct breakpoint *b, int thread)
1188 {
1189 int old_thread = b->thread;
1190
1191 b->thread = thread;
1192 if (old_thread != thread)
1193 gdb::observers::breakpoint_modified.notify (b);
1194 }
1195
1196 /* Set the task for this breakpoint. If TASK is 0, make the
1197 breakpoint work for any task. */
1198
1199 void
1200 breakpoint_set_task (struct breakpoint *b, int task)
1201 {
1202 int old_task = b->task;
1203
1204 b->task = task;
1205 if (old_task != task)
1206 gdb::observers::breakpoint_modified.notify (b);
1207 }
1208
1209 static void
1210 commands_command_1 (const char *arg, int from_tty,
1211 struct command_line *control)
1212 {
1213 counted_command_line cmd;
1214 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1215 NULL after the call to read_command_lines if the user provides an empty
1216 list of command by just typing "end". */
1217 bool cmd_read = false;
1218
1219 std::string new_arg;
1220
1221 if (arg == NULL || !*arg)
1222 {
1223 if (breakpoint_count - prev_breakpoint_count > 1)
1224 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1225 breakpoint_count);
1226 else if (breakpoint_count > 0)
1227 new_arg = string_printf ("%d", breakpoint_count);
1228 arg = new_arg.c_str ();
1229 }
1230
1231 map_breakpoint_numbers
1232 (arg, [&] (breakpoint *b)
1233 {
1234 if (!cmd_read)
1235 {
1236 gdb_assert (cmd == NULL);
1237 if (control != NULL)
1238 cmd = control->body_list_0;
1239 else
1240 {
1241 std::string str
1242 = string_printf (_("Type commands for breakpoint(s) "
1243 "%s, one per line."),
1244 arg);
1245
1246 auto do_validate = [=] (const char *line)
1247 {
1248 validate_actionline (line, b);
1249 };
1250 gdb::function_view<void (const char *)> validator;
1251 if (is_tracepoint (b))
1252 validator = do_validate;
1253
1254 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1255 }
1256 cmd_read = true;
1257 }
1258
1259 /* If a breakpoint was on the list more than once, we don't need to
1260 do anything. */
1261 if (b->commands != cmd)
1262 {
1263 validate_commands_for_breakpoint (b, cmd.get ());
1264 b->commands = cmd;
1265 gdb::observers::breakpoint_modified.notify (b);
1266 }
1267 });
1268 }
1269
1270 static void
1271 commands_command (const char *arg, int from_tty)
1272 {
1273 commands_command_1 (arg, from_tty, NULL);
1274 }
1275
1276 /* Like commands_command, but instead of reading the commands from
1277 input stream, takes them from an already parsed command structure.
1278
1279 This is used by cli-script.c to DTRT with breakpoint commands
1280 that are part of if and while bodies. */
1281 enum command_control_type
1282 commands_from_control_command (const char *arg, struct command_line *cmd)
1283 {
1284 commands_command_1 (arg, 0, cmd);
1285 return simple_control;
1286 }
1287
1288 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1289
1290 static int
1291 bp_location_has_shadow (struct bp_location *bl)
1292 {
1293 if (bl->loc_type != bp_loc_software_breakpoint)
1294 return 0;
1295 if (!bl->inserted)
1296 return 0;
1297 if (bl->target_info.shadow_len == 0)
1298 /* BL isn't valid, or doesn't shadow memory. */
1299 return 0;
1300 return 1;
1301 }
1302
1303 /* Update BUF, which is LEN bytes read from the target address
1304 MEMADDR, by replacing a memory breakpoint with its shadowed
1305 contents.
1306
1307 If READBUF is not NULL, this buffer must not overlap with the of
1308 the breakpoint location's shadow_contents buffer. Otherwise, a
1309 failed assertion internal error will be raised. */
1310
1311 static void
1312 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1313 const gdb_byte *writebuf_org,
1314 ULONGEST memaddr, LONGEST len,
1315 struct bp_target_info *target_info,
1316 struct gdbarch *gdbarch)
1317 {
1318 /* Now do full processing of the found relevant range of elements. */
1319 CORE_ADDR bp_addr = 0;
1320 int bp_size = 0;
1321 int bptoffset = 0;
1322
1323 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1324 current_program_space->aspace, 0))
1325 {
1326 /* The breakpoint is inserted in a different address space. */
1327 return;
1328 }
1329
1330 /* Addresses and length of the part of the breakpoint that
1331 we need to copy. */
1332 bp_addr = target_info->placed_address;
1333 bp_size = target_info->shadow_len;
1334
1335 if (bp_addr + bp_size <= memaddr)
1336 {
1337 /* The breakpoint is entirely before the chunk of memory we are
1338 reading. */
1339 return;
1340 }
1341
1342 if (bp_addr >= memaddr + len)
1343 {
1344 /* The breakpoint is entirely after the chunk of memory we are
1345 reading. */
1346 return;
1347 }
1348
1349 /* Offset within shadow_contents. */
1350 if (bp_addr < memaddr)
1351 {
1352 /* Only copy the second part of the breakpoint. */
1353 bp_size -= memaddr - bp_addr;
1354 bptoffset = memaddr - bp_addr;
1355 bp_addr = memaddr;
1356 }
1357
1358 if (bp_addr + bp_size > memaddr + len)
1359 {
1360 /* Only copy the first part of the breakpoint. */
1361 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1362 }
1363
1364 if (readbuf != NULL)
1365 {
1366 /* Verify that the readbuf buffer does not overlap with the
1367 shadow_contents buffer. */
1368 gdb_assert (target_info->shadow_contents >= readbuf + len
1369 || readbuf >= (target_info->shadow_contents
1370 + target_info->shadow_len));
1371
1372 /* Update the read buffer with this inserted breakpoint's
1373 shadow. */
1374 memcpy (readbuf + bp_addr - memaddr,
1375 target_info->shadow_contents + bptoffset, bp_size);
1376 }
1377 else
1378 {
1379 const unsigned char *bp;
1380 CORE_ADDR addr = target_info->reqstd_address;
1381 int placed_size;
1382
1383 /* Update the shadow with what we want to write to memory. */
1384 memcpy (target_info->shadow_contents + bptoffset,
1385 writebuf_org + bp_addr - memaddr, bp_size);
1386
1387 /* Determine appropriate breakpoint contents and size for this
1388 address. */
1389 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1390
1391 /* Update the final write buffer with this inserted
1392 breakpoint's INSN. */
1393 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1394 }
1395 }
1396
1397 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1398 by replacing any memory breakpoints with their shadowed contents.
1399
1400 If READBUF is not NULL, this buffer must not overlap with any of
1401 the breakpoint location's shadow_contents buffers. Otherwise,
1402 a failed assertion internal error will be raised.
1403
1404 The range of shadowed area by each bp_location is:
1405 bl->address - bp_locations_placed_address_before_address_max
1406 up to bl->address + bp_locations_shadow_len_after_address_max
1407 The range we were requested to resolve shadows for is:
1408 memaddr ... memaddr + len
1409 Thus the safe cutoff boundaries for performance optimization are
1410 memaddr + len <= (bl->address
1411 - bp_locations_placed_address_before_address_max)
1412 and:
1413 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1414
1415 void
1416 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1417 const gdb_byte *writebuf_org,
1418 ULONGEST memaddr, LONGEST len)
1419 {
1420 /* Left boundary, right boundary and median element of our binary
1421 search. */
1422 unsigned bc_l, bc_r, bc;
1423
1424 /* Find BC_L which is a leftmost element which may affect BUF
1425 content. It is safe to report lower value but a failure to
1426 report higher one. */
1427
1428 bc_l = 0;
1429 bc_r = bp_locations_count;
1430 while (bc_l + 1 < bc_r)
1431 {
1432 struct bp_location *bl;
1433
1434 bc = (bc_l + bc_r) / 2;
1435 bl = bp_locations[bc];
1436
1437 /* Check first BL->ADDRESS will not overflow due to the added
1438 constant. Then advance the left boundary only if we are sure
1439 the BC element can in no way affect the BUF content (MEMADDR
1440 to MEMADDR + LEN range).
1441
1442 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1443 offset so that we cannot miss a breakpoint with its shadow
1444 range tail still reaching MEMADDR. */
1445
1446 if ((bl->address + bp_locations_shadow_len_after_address_max
1447 >= bl->address)
1448 && (bl->address + bp_locations_shadow_len_after_address_max
1449 <= memaddr))
1450 bc_l = bc;
1451 else
1452 bc_r = bc;
1453 }
1454
1455 /* Due to the binary search above, we need to make sure we pick the
1456 first location that's at BC_L's address. E.g., if there are
1457 multiple locations at the same address, BC_L may end up pointing
1458 at a duplicate location, and miss the "master"/"inserted"
1459 location. Say, given locations L1, L2 and L3 at addresses A and
1460 B:
1461
1462 L1@A, L2@A, L3@B, ...
1463
1464 BC_L could end up pointing at location L2, while the "master"
1465 location could be L1. Since the `loc->inserted' flag is only set
1466 on "master" locations, we'd forget to restore the shadow of L1
1467 and L2. */
1468 while (bc_l > 0
1469 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1470 bc_l--;
1471
1472 /* Now do full processing of the found relevant range of elements. */
1473
1474 for (bc = bc_l; bc < bp_locations_count; bc++)
1475 {
1476 struct bp_location *bl = bp_locations[bc];
1477
1478 /* bp_location array has BL->OWNER always non-NULL. */
1479 if (bl->owner->type == bp_none)
1480 warning (_("reading through apparently deleted breakpoint #%d?"),
1481 bl->owner->number);
1482
1483 /* Performance optimization: any further element can no longer affect BUF
1484 content. */
1485
1486 if (bl->address >= bp_locations_placed_address_before_address_max
1487 && memaddr + len <= (bl->address
1488 - bp_locations_placed_address_before_address_max))
1489 break;
1490
1491 if (!bp_location_has_shadow (bl))
1492 continue;
1493
1494 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1495 memaddr, len, &bl->target_info, bl->gdbarch);
1496 }
1497 }
1498
1499 /* See breakpoint.h. */
1500
1501 bool
1502 is_breakpoint (const struct breakpoint *bpt)
1503 {
1504 return (bpt->type == bp_breakpoint
1505 || bpt->type == bp_hardware_breakpoint
1506 || bpt->type == bp_dprintf);
1507 }
1508
1509 /* Return true if BPT is of any hardware watchpoint kind. */
1510
1511 static bool
1512 is_hardware_watchpoint (const struct breakpoint *bpt)
1513 {
1514 return (bpt->type == bp_hardware_watchpoint
1515 || bpt->type == bp_read_watchpoint
1516 || bpt->type == bp_access_watchpoint);
1517 }
1518
1519 /* See breakpoint.h. */
1520
1521 bool
1522 is_watchpoint (const struct breakpoint *bpt)
1523 {
1524 return (is_hardware_watchpoint (bpt)
1525 || bpt->type == bp_watchpoint);
1526 }
1527
1528 /* Returns true if the current thread and its running state are safe
1529 to evaluate or update watchpoint B. Watchpoints on local
1530 expressions need to be evaluated in the context of the thread that
1531 was current when the watchpoint was created, and, that thread needs
1532 to be stopped to be able to select the correct frame context.
1533 Watchpoints on global expressions can be evaluated on any thread,
1534 and in any state. It is presently left to the target allowing
1535 memory accesses when threads are running. */
1536
1537 static int
1538 watchpoint_in_thread_scope (struct watchpoint *b)
1539 {
1540 return (b->pspace == current_program_space
1541 && (b->watchpoint_thread == null_ptid
1542 || (inferior_ptid == b->watchpoint_thread
1543 && !inferior_thread ()->executing)));
1544 }
1545
1546 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1547 associated bp_watchpoint_scope breakpoint. */
1548
1549 static void
1550 watchpoint_del_at_next_stop (struct watchpoint *w)
1551 {
1552 if (w->related_breakpoint != w)
1553 {
1554 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1555 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1556 w->related_breakpoint->disposition = disp_del_at_next_stop;
1557 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1558 w->related_breakpoint = w;
1559 }
1560 w->disposition = disp_del_at_next_stop;
1561 }
1562
1563 /* Extract a bitfield value from value VAL using the bit parameters contained in
1564 watchpoint W. */
1565
1566 static struct value *
1567 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1568 {
1569 struct value *bit_val;
1570
1571 if (val == NULL)
1572 return NULL;
1573
1574 bit_val = allocate_value (value_type (val));
1575
1576 unpack_value_bitfield (bit_val,
1577 w->val_bitpos,
1578 w->val_bitsize,
1579 value_contents_for_printing (val),
1580 value_offset (val),
1581 val);
1582
1583 return bit_val;
1584 }
1585
1586 /* Allocate a dummy location and add it to B, which must be a software
1587 watchpoint. This is required because even if a software watchpoint
1588 is not watching any memory, bpstat_stop_status requires a location
1589 to be able to report stops. */
1590
1591 static void
1592 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1593 struct program_space *pspace)
1594 {
1595 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1596
1597 b->loc = allocate_bp_location (b);
1598 b->loc->pspace = pspace;
1599 b->loc->address = -1;
1600 b->loc->length = -1;
1601 }
1602
1603 /* Returns true if B is a software watchpoint that is not watching any
1604 memory (e.g., "watch $pc"). */
1605
1606 static bool
1607 is_no_memory_software_watchpoint (struct breakpoint *b)
1608 {
1609 return (b->type == bp_watchpoint
1610 && b->loc != NULL
1611 && b->loc->next == NULL
1612 && b->loc->address == -1
1613 && b->loc->length == -1);
1614 }
1615
1616 /* Assuming that B is a watchpoint:
1617 - Reparse watchpoint expression, if REPARSE is non-zero
1618 - Evaluate expression and store the result in B->val
1619 - Evaluate the condition if there is one, and store the result
1620 in b->loc->cond.
1621 - Update the list of values that must be watched in B->loc.
1622
1623 If the watchpoint disposition is disp_del_at_next_stop, then do
1624 nothing. If this is local watchpoint that is out of scope, delete
1625 it.
1626
1627 Even with `set breakpoint always-inserted on' the watchpoints are
1628 removed + inserted on each stop here. Normal breakpoints must
1629 never be removed because they might be missed by a running thread
1630 when debugging in non-stop mode. On the other hand, hardware
1631 watchpoints (is_hardware_watchpoint; processed here) are specific
1632 to each LWP since they are stored in each LWP's hardware debug
1633 registers. Therefore, such LWP must be stopped first in order to
1634 be able to modify its hardware watchpoints.
1635
1636 Hardware watchpoints must be reset exactly once after being
1637 presented to the user. It cannot be done sooner, because it would
1638 reset the data used to present the watchpoint hit to the user. And
1639 it must not be done later because it could display the same single
1640 watchpoint hit during multiple GDB stops. Note that the latter is
1641 relevant only to the hardware watchpoint types bp_read_watchpoint
1642 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1643 not user-visible - its hit is suppressed if the memory content has
1644 not changed.
1645
1646 The following constraints influence the location where we can reset
1647 hardware watchpoints:
1648
1649 * target_stopped_by_watchpoint and target_stopped_data_address are
1650 called several times when GDB stops.
1651
1652 [linux]
1653 * Multiple hardware watchpoints can be hit at the same time,
1654 causing GDB to stop. GDB only presents one hardware watchpoint
1655 hit at a time as the reason for stopping, and all the other hits
1656 are presented later, one after the other, each time the user
1657 requests the execution to be resumed. Execution is not resumed
1658 for the threads still having pending hit event stored in
1659 LWP_INFO->STATUS. While the watchpoint is already removed from
1660 the inferior on the first stop the thread hit event is kept being
1661 reported from its cached value by linux_nat_stopped_data_address
1662 until the real thread resume happens after the watchpoint gets
1663 presented and thus its LWP_INFO->STATUS gets reset.
1664
1665 Therefore the hardware watchpoint hit can get safely reset on the
1666 watchpoint removal from inferior. */
1667
1668 static void
1669 update_watchpoint (struct watchpoint *b, int reparse)
1670 {
1671 int within_current_scope;
1672 struct frame_id saved_frame_id;
1673 int frame_saved;
1674
1675 /* If this is a local watchpoint, we only want to check if the
1676 watchpoint frame is in scope if the current thread is the thread
1677 that was used to create the watchpoint. */
1678 if (!watchpoint_in_thread_scope (b))
1679 return;
1680
1681 if (b->disposition == disp_del_at_next_stop)
1682 return;
1683
1684 frame_saved = 0;
1685
1686 /* Determine if the watchpoint is within scope. */
1687 if (b->exp_valid_block == NULL)
1688 within_current_scope = 1;
1689 else
1690 {
1691 struct frame_info *fi = get_current_frame ();
1692 struct gdbarch *frame_arch = get_frame_arch (fi);
1693 CORE_ADDR frame_pc = get_frame_pc (fi);
1694
1695 /* If we're at a point where the stack has been destroyed
1696 (e.g. in a function epilogue), unwinding may not work
1697 properly. Do not attempt to recreate locations at this
1698 point. See similar comments in watchpoint_check. */
1699 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1700 return;
1701
1702 /* Save the current frame's ID so we can restore it after
1703 evaluating the watchpoint expression on its own frame. */
1704 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1705 took a frame parameter, so that we didn't have to change the
1706 selected frame. */
1707 frame_saved = 1;
1708 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1709
1710 fi = frame_find_by_id (b->watchpoint_frame);
1711 within_current_scope = (fi != NULL);
1712 if (within_current_scope)
1713 select_frame (fi);
1714 }
1715
1716 /* We don't free locations. They are stored in the bp_location array
1717 and update_global_location_list will eventually delete them and
1718 remove breakpoints if needed. */
1719 b->loc = NULL;
1720
1721 if (within_current_scope && reparse)
1722 {
1723 const char *s;
1724
1725 b->exp.reset ();
1726 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1727 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1728 /* If the meaning of expression itself changed, the old value is
1729 no longer relevant. We don't want to report a watchpoint hit
1730 to the user when the old value and the new value may actually
1731 be completely different objects. */
1732 b->val = NULL;
1733 b->val_valid = false;
1734
1735 /* Note that unlike with breakpoints, the watchpoint's condition
1736 expression is stored in the breakpoint object, not in the
1737 locations (re)created below. */
1738 if (b->cond_string != NULL)
1739 {
1740 b->cond_exp.reset ();
1741
1742 s = b->cond_string;
1743 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1744 }
1745 }
1746
1747 /* If we failed to parse the expression, for example because
1748 it refers to a global variable in a not-yet-loaded shared library,
1749 don't try to insert watchpoint. We don't automatically delete
1750 such watchpoint, though, since failure to parse expression
1751 is different from out-of-scope watchpoint. */
1752 if (!target_has_execution)
1753 {
1754 /* Without execution, memory can't change. No use to try and
1755 set watchpoint locations. The watchpoint will be reset when
1756 the target gains execution, through breakpoint_re_set. */
1757 if (!can_use_hw_watchpoints)
1758 {
1759 if (b->ops->works_in_software_mode (b))
1760 b->type = bp_watchpoint;
1761 else
1762 error (_("Can't set read/access watchpoint when "
1763 "hardware watchpoints are disabled."));
1764 }
1765 }
1766 else if (within_current_scope && b->exp)
1767 {
1768 int pc = 0;
1769 std::vector<value_ref_ptr> val_chain;
1770 struct value *v, *result;
1771 struct program_space *frame_pspace;
1772
1773 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1774
1775 /* Avoid setting b->val if it's already set. The meaning of
1776 b->val is 'the last value' user saw, and we should update
1777 it only if we reported that last value to user. As it
1778 happens, the code that reports it updates b->val directly.
1779 We don't keep track of the memory value for masked
1780 watchpoints. */
1781 if (!b->val_valid && !is_masked_watchpoint (b))
1782 {
1783 if (b->val_bitsize != 0)
1784 v = extract_bitfield_from_watchpoint_value (b, v);
1785 b->val = release_value (v);
1786 b->val_valid = true;
1787 }
1788
1789 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1790
1791 /* Look at each value on the value chain. */
1792 gdb_assert (!val_chain.empty ());
1793 for (const value_ref_ptr &iter : val_chain)
1794 {
1795 v = iter.get ();
1796
1797 /* If it's a memory location, and GDB actually needed
1798 its contents to evaluate the expression, then we
1799 must watch it. If the first value returned is
1800 still lazy, that means an error occurred reading it;
1801 watch it anyway in case it becomes readable. */
1802 if (VALUE_LVAL (v) == lval_memory
1803 && (v == val_chain[0] || ! value_lazy (v)))
1804 {
1805 struct type *vtype = check_typedef (value_type (v));
1806
1807 /* We only watch structs and arrays if user asked
1808 for it explicitly, never if they just happen to
1809 appear in the middle of some value chain. */
1810 if (v == result
1811 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1812 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1813 {
1814 CORE_ADDR addr;
1815 enum target_hw_bp_type type;
1816 struct bp_location *loc, **tmp;
1817 int bitpos = 0, bitsize = 0;
1818
1819 if (value_bitsize (v) != 0)
1820 {
1821 /* Extract the bit parameters out from the bitfield
1822 sub-expression. */
1823 bitpos = value_bitpos (v);
1824 bitsize = value_bitsize (v);
1825 }
1826 else if (v == result && b->val_bitsize != 0)
1827 {
1828 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1829 lvalue whose bit parameters are saved in the fields
1830 VAL_BITPOS and VAL_BITSIZE. */
1831 bitpos = b->val_bitpos;
1832 bitsize = b->val_bitsize;
1833 }
1834
1835 addr = value_address (v);
1836 if (bitsize != 0)
1837 {
1838 /* Skip the bytes that don't contain the bitfield. */
1839 addr += bitpos / 8;
1840 }
1841
1842 type = hw_write;
1843 if (b->type == bp_read_watchpoint)
1844 type = hw_read;
1845 else if (b->type == bp_access_watchpoint)
1846 type = hw_access;
1847
1848 loc = allocate_bp_location (b);
1849 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1850 ;
1851 *tmp = loc;
1852 loc->gdbarch = get_type_arch (value_type (v));
1853
1854 loc->pspace = frame_pspace;
1855 loc->address = address_significant (loc->gdbarch, addr);
1856
1857 if (bitsize != 0)
1858 {
1859 /* Just cover the bytes that make up the bitfield. */
1860 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1861 }
1862 else
1863 loc->length = TYPE_LENGTH (value_type (v));
1864
1865 loc->watchpoint_type = type;
1866 }
1867 }
1868 }
1869
1870 /* Change the type of breakpoint between hardware assisted or
1871 an ordinary watchpoint depending on the hardware support
1872 and free hardware slots. REPARSE is set when the inferior
1873 is started. */
1874 if (reparse)
1875 {
1876 int reg_cnt;
1877 enum bp_loc_type loc_type;
1878 struct bp_location *bl;
1879
1880 reg_cnt = can_use_hardware_watchpoint (val_chain);
1881
1882 if (reg_cnt)
1883 {
1884 int i, target_resources_ok, other_type_used;
1885 enum bptype type;
1886
1887 /* Use an exact watchpoint when there's only one memory region to be
1888 watched, and only one debug register is needed to watch it. */
1889 b->exact = target_exact_watchpoints && reg_cnt == 1;
1890
1891 /* We need to determine how many resources are already
1892 used for all other hardware watchpoints plus this one
1893 to see if we still have enough resources to also fit
1894 this watchpoint in as well. */
1895
1896 /* If this is a software watchpoint, we try to turn it
1897 to a hardware one -- count resources as if B was of
1898 hardware watchpoint type. */
1899 type = b->type;
1900 if (type == bp_watchpoint)
1901 type = bp_hardware_watchpoint;
1902
1903 /* This watchpoint may or may not have been placed on
1904 the list yet at this point (it won't be in the list
1905 if we're trying to create it for the first time,
1906 through watch_command), so always account for it
1907 manually. */
1908
1909 /* Count resources used by all watchpoints except B. */
1910 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1911
1912 /* Add in the resources needed for B. */
1913 i += hw_watchpoint_use_count (b);
1914
1915 target_resources_ok
1916 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1917 if (target_resources_ok <= 0)
1918 {
1919 int sw_mode = b->ops->works_in_software_mode (b);
1920
1921 if (target_resources_ok == 0 && !sw_mode)
1922 error (_("Target does not support this type of "
1923 "hardware watchpoint."));
1924 else if (target_resources_ok < 0 && !sw_mode)
1925 error (_("There are not enough available hardware "
1926 "resources for this watchpoint."));
1927
1928 /* Downgrade to software watchpoint. */
1929 b->type = bp_watchpoint;
1930 }
1931 else
1932 {
1933 /* If this was a software watchpoint, we've just
1934 found we have enough resources to turn it to a
1935 hardware watchpoint. Otherwise, this is a
1936 nop. */
1937 b->type = type;
1938 }
1939 }
1940 else if (!b->ops->works_in_software_mode (b))
1941 {
1942 if (!can_use_hw_watchpoints)
1943 error (_("Can't set read/access watchpoint when "
1944 "hardware watchpoints are disabled."));
1945 else
1946 error (_("Expression cannot be implemented with "
1947 "read/access watchpoint."));
1948 }
1949 else
1950 b->type = bp_watchpoint;
1951
1952 loc_type = (b->type == bp_watchpoint? bp_loc_other
1953 : bp_loc_hardware_watchpoint);
1954 for (bl = b->loc; bl; bl = bl->next)
1955 bl->loc_type = loc_type;
1956 }
1957
1958 /* If a software watchpoint is not watching any memory, then the
1959 above left it without any location set up. But,
1960 bpstat_stop_status requires a location to be able to report
1961 stops, so make sure there's at least a dummy one. */
1962 if (b->type == bp_watchpoint && b->loc == NULL)
1963 software_watchpoint_add_no_memory_location (b, frame_pspace);
1964 }
1965 else if (!within_current_scope)
1966 {
1967 printf_filtered (_("\
1968 Watchpoint %d deleted because the program has left the block\n\
1969 in which its expression is valid.\n"),
1970 b->number);
1971 watchpoint_del_at_next_stop (b);
1972 }
1973
1974 /* Restore the selected frame. */
1975 if (frame_saved)
1976 select_frame (frame_find_by_id (saved_frame_id));
1977 }
1978
1979
1980 /* Returns 1 iff breakpoint location should be
1981 inserted in the inferior. We don't differentiate the type of BL's owner
1982 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1983 breakpoint_ops is not defined, because in insert_bp_location,
1984 tracepoint's insert_location will not be called. */
1985 static int
1986 should_be_inserted (struct bp_location *bl)
1987 {
1988 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1989 return 0;
1990
1991 if (bl->owner->disposition == disp_del_at_next_stop)
1992 return 0;
1993
1994 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1995 return 0;
1996
1997 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1998 return 0;
1999
2000 /* This is set for example, when we're attached to the parent of a
2001 vfork, and have detached from the child. The child is running
2002 free, and we expect it to do an exec or exit, at which point the
2003 OS makes the parent schedulable again (and the target reports
2004 that the vfork is done). Until the child is done with the shared
2005 memory region, do not insert breakpoints in the parent, otherwise
2006 the child could still trip on the parent's breakpoints. Since
2007 the parent is blocked anyway, it won't miss any breakpoint. */
2008 if (bl->pspace->breakpoints_not_allowed)
2009 return 0;
2010
2011 /* Don't insert a breakpoint if we're trying to step past its
2012 location, except if the breakpoint is a single-step breakpoint,
2013 and the breakpoint's thread is the thread which is stepping past
2014 a breakpoint. */
2015 if ((bl->loc_type == bp_loc_software_breakpoint
2016 || bl->loc_type == bp_loc_hardware_breakpoint)
2017 && stepping_past_instruction_at (bl->pspace->aspace,
2018 bl->address)
2019 /* The single-step breakpoint may be inserted at the location
2020 we're trying to step if the instruction branches to itself.
2021 However, the instruction won't be executed at all and it may
2022 break the semantics of the instruction, for example, the
2023 instruction is a conditional branch or updates some flags.
2024 We can't fix it unless GDB is able to emulate the instruction
2025 or switch to displaced stepping. */
2026 && !(bl->owner->type == bp_single_step
2027 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2028 {
2029 if (debug_infrun)
2030 {
2031 fprintf_unfiltered (gdb_stdlog,
2032 "infrun: skipping breakpoint: "
2033 "stepping past insn at: %s\n",
2034 paddress (bl->gdbarch, bl->address));
2035 }
2036 return 0;
2037 }
2038
2039 /* Don't insert watchpoints if we're trying to step past the
2040 instruction that triggered one. */
2041 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2042 && stepping_past_nonsteppable_watchpoint ())
2043 {
2044 if (debug_infrun)
2045 {
2046 fprintf_unfiltered (gdb_stdlog,
2047 "infrun: stepping past non-steppable watchpoint. "
2048 "skipping watchpoint at %s:%d\n",
2049 paddress (bl->gdbarch, bl->address),
2050 bl->length);
2051 }
2052 return 0;
2053 }
2054
2055 return 1;
2056 }
2057
2058 /* Same as should_be_inserted but does the check assuming
2059 that the location is not duplicated. */
2060
2061 static int
2062 unduplicated_should_be_inserted (struct bp_location *bl)
2063 {
2064 int result;
2065 const int save_duplicate = bl->duplicate;
2066
2067 bl->duplicate = 0;
2068 result = should_be_inserted (bl);
2069 bl->duplicate = save_duplicate;
2070 return result;
2071 }
2072
2073 /* Parses a conditional described by an expression COND into an
2074 agent expression bytecode suitable for evaluation
2075 by the bytecode interpreter. Return NULL if there was
2076 any error during parsing. */
2077
2078 static agent_expr_up
2079 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2080 {
2081 if (cond == NULL)
2082 return NULL;
2083
2084 agent_expr_up aexpr;
2085
2086 /* We don't want to stop processing, so catch any errors
2087 that may show up. */
2088 try
2089 {
2090 aexpr = gen_eval_for_expr (scope, cond);
2091 }
2092
2093 catch (const gdb_exception_error &ex)
2094 {
2095 /* If we got here, it means the condition could not be parsed to a valid
2096 bytecode expression and thus can't be evaluated on the target's side.
2097 It's no use iterating through the conditions. */
2098 }
2099
2100 /* We have a valid agent expression. */
2101 return aexpr;
2102 }
2103
2104 /* Based on location BL, create a list of breakpoint conditions to be
2105 passed on to the target. If we have duplicated locations with different
2106 conditions, we will add such conditions to the list. The idea is that the
2107 target will evaluate the list of conditions and will only notify GDB when
2108 one of them is true. */
2109
2110 static void
2111 build_target_condition_list (struct bp_location *bl)
2112 {
2113 struct bp_location **locp = NULL, **loc2p;
2114 int null_condition_or_parse_error = 0;
2115 int modified = bl->needs_update;
2116 struct bp_location *loc;
2117
2118 /* Release conditions left over from a previous insert. */
2119 bl->target_info.conditions.clear ();
2120
2121 /* This is only meaningful if the target is
2122 evaluating conditions and if the user has
2123 opted for condition evaluation on the target's
2124 side. */
2125 if (gdb_evaluates_breakpoint_condition_p ()
2126 || !target_supports_evaluation_of_breakpoint_conditions ())
2127 return;
2128
2129 /* Do a first pass to check for locations with no assigned
2130 conditions or conditions that fail to parse to a valid agent expression
2131 bytecode. If any of these happen, then it's no use to send conditions
2132 to the target since this location will always trigger and generate a
2133 response back to GDB. */
2134 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2135 {
2136 loc = (*loc2p);
2137 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2138 {
2139 if (modified)
2140 {
2141 /* Re-parse the conditions since something changed. In that
2142 case we already freed the condition bytecodes (see
2143 force_breakpoint_reinsertion). We just
2144 need to parse the condition to bytecodes again. */
2145 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2146 loc->cond.get ());
2147 }
2148
2149 /* If we have a NULL bytecode expression, it means something
2150 went wrong or we have a null condition expression. */
2151 if (!loc->cond_bytecode)
2152 {
2153 null_condition_or_parse_error = 1;
2154 break;
2155 }
2156 }
2157 }
2158
2159 /* If any of these happened, it means we will have to evaluate the conditions
2160 for the location's address on gdb's side. It is no use keeping bytecodes
2161 for all the other duplicate locations, thus we free all of them here.
2162
2163 This is so we have a finer control over which locations' conditions are
2164 being evaluated by GDB or the remote stub. */
2165 if (null_condition_or_parse_error)
2166 {
2167 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2168 {
2169 loc = (*loc2p);
2170 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2171 {
2172 /* Only go as far as the first NULL bytecode is
2173 located. */
2174 if (!loc->cond_bytecode)
2175 return;
2176
2177 loc->cond_bytecode.reset ();
2178 }
2179 }
2180 }
2181
2182 /* No NULL conditions or failed bytecode generation. Build a condition list
2183 for this location's address. */
2184 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2185 {
2186 loc = (*loc2p);
2187 if (loc->cond
2188 && is_breakpoint (loc->owner)
2189 && loc->pspace->num == bl->pspace->num
2190 && loc->owner->enable_state == bp_enabled
2191 && loc->enabled)
2192 {
2193 /* Add the condition to the vector. This will be used later
2194 to send the conditions to the target. */
2195 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2196 }
2197 }
2198
2199 return;
2200 }
2201
2202 /* Parses a command described by string CMD into an agent expression
2203 bytecode suitable for evaluation by the bytecode interpreter.
2204 Return NULL if there was any error during parsing. */
2205
2206 static agent_expr_up
2207 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2208 {
2209 const char *cmdrest;
2210 const char *format_start, *format_end;
2211 struct gdbarch *gdbarch = get_current_arch ();
2212
2213 if (cmd == NULL)
2214 return NULL;
2215
2216 cmdrest = cmd;
2217
2218 if (*cmdrest == ',')
2219 ++cmdrest;
2220 cmdrest = skip_spaces (cmdrest);
2221
2222 if (*cmdrest++ != '"')
2223 error (_("No format string following the location"));
2224
2225 format_start = cmdrest;
2226
2227 format_pieces fpieces (&cmdrest);
2228
2229 format_end = cmdrest;
2230
2231 if (*cmdrest++ != '"')
2232 error (_("Bad format string, non-terminated '\"'."));
2233
2234 cmdrest = skip_spaces (cmdrest);
2235
2236 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2237 error (_("Invalid argument syntax"));
2238
2239 if (*cmdrest == ',')
2240 cmdrest++;
2241 cmdrest = skip_spaces (cmdrest);
2242
2243 /* For each argument, make an expression. */
2244
2245 std::vector<struct expression *> argvec;
2246 while (*cmdrest != '\0')
2247 {
2248 const char *cmd1;
2249
2250 cmd1 = cmdrest;
2251 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2252 argvec.push_back (expr.release ());
2253 cmdrest = cmd1;
2254 if (*cmdrest == ',')
2255 ++cmdrest;
2256 }
2257
2258 agent_expr_up aexpr;
2259
2260 /* We don't want to stop processing, so catch any errors
2261 that may show up. */
2262 try
2263 {
2264 aexpr = gen_printf (scope, gdbarch, 0, 0,
2265 format_start, format_end - format_start,
2266 argvec.size (), argvec.data ());
2267 }
2268 catch (const gdb_exception_error &ex)
2269 {
2270 /* If we got here, it means the command could not be parsed to a valid
2271 bytecode expression and thus can't be evaluated on the target's side.
2272 It's no use iterating through the other commands. */
2273 }
2274
2275 /* We have a valid agent expression, return it. */
2276 return aexpr;
2277 }
2278
2279 /* Based on location BL, create a list of breakpoint commands to be
2280 passed on to the target. If we have duplicated locations with
2281 different commands, we will add any such to the list. */
2282
2283 static void
2284 build_target_command_list (struct bp_location *bl)
2285 {
2286 struct bp_location **locp = NULL, **loc2p;
2287 int null_command_or_parse_error = 0;
2288 int modified = bl->needs_update;
2289 struct bp_location *loc;
2290
2291 /* Clear commands left over from a previous insert. */
2292 bl->target_info.tcommands.clear ();
2293
2294 if (!target_can_run_breakpoint_commands ())
2295 return;
2296
2297 /* For now, limit to agent-style dprintf breakpoints. */
2298 if (dprintf_style != dprintf_style_agent)
2299 return;
2300
2301 /* For now, if we have any duplicate location that isn't a dprintf,
2302 don't install the target-side commands, as that would make the
2303 breakpoint not be reported to the core, and we'd lose
2304 control. */
2305 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2306 {
2307 loc = (*loc2p);
2308 if (is_breakpoint (loc->owner)
2309 && loc->pspace->num == bl->pspace->num
2310 && loc->owner->type != bp_dprintf)
2311 return;
2312 }
2313
2314 /* Do a first pass to check for locations with no assigned
2315 conditions or conditions that fail to parse to a valid agent expression
2316 bytecode. If any of these happen, then it's no use to send conditions
2317 to the target since this location will always trigger and generate a
2318 response back to GDB. */
2319 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2320 {
2321 loc = (*loc2p);
2322 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2323 {
2324 if (modified)
2325 {
2326 /* Re-parse the commands since something changed. In that
2327 case we already freed the command bytecodes (see
2328 force_breakpoint_reinsertion). We just
2329 need to parse the command to bytecodes again. */
2330 loc->cmd_bytecode
2331 = parse_cmd_to_aexpr (bl->address,
2332 loc->owner->extra_string);
2333 }
2334
2335 /* If we have a NULL bytecode expression, it means something
2336 went wrong or we have a null command expression. */
2337 if (!loc->cmd_bytecode)
2338 {
2339 null_command_or_parse_error = 1;
2340 break;
2341 }
2342 }
2343 }
2344
2345 /* If anything failed, then we're not doing target-side commands,
2346 and so clean up. */
2347 if (null_command_or_parse_error)
2348 {
2349 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2350 {
2351 loc = (*loc2p);
2352 if (is_breakpoint (loc->owner)
2353 && loc->pspace->num == bl->pspace->num)
2354 {
2355 /* Only go as far as the first NULL bytecode is
2356 located. */
2357 if (loc->cmd_bytecode == NULL)
2358 return;
2359
2360 loc->cmd_bytecode.reset ();
2361 }
2362 }
2363 }
2364
2365 /* No NULL commands or failed bytecode generation. Build a command list
2366 for this location's address. */
2367 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2368 {
2369 loc = (*loc2p);
2370 if (loc->owner->extra_string
2371 && is_breakpoint (loc->owner)
2372 && loc->pspace->num == bl->pspace->num
2373 && loc->owner->enable_state == bp_enabled
2374 && loc->enabled)
2375 {
2376 /* Add the command to the vector. This will be used later
2377 to send the commands to the target. */
2378 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2379 }
2380 }
2381
2382 bl->target_info.persist = 0;
2383 /* Maybe flag this location as persistent. */
2384 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2385 bl->target_info.persist = 1;
2386 }
2387
2388 /* Return the kind of breakpoint on address *ADDR. Get the kind
2389 of breakpoint according to ADDR except single-step breakpoint.
2390 Get the kind of single-step breakpoint according to the current
2391 registers state. */
2392
2393 static int
2394 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2395 {
2396 if (bl->owner->type == bp_single_step)
2397 {
2398 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2399 struct regcache *regcache;
2400
2401 regcache = get_thread_regcache (thr);
2402
2403 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2404 regcache, addr);
2405 }
2406 else
2407 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2408 }
2409
2410 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2411 location. Any error messages are printed to TMP_ERROR_STREAM; and
2412 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2413 Returns 0 for success, 1 if the bp_location type is not supported or
2414 -1 for failure.
2415
2416 NOTE drow/2003-09-09: This routine could be broken down to an
2417 object-style method for each breakpoint or catchpoint type. */
2418 static int
2419 insert_bp_location (struct bp_location *bl,
2420 struct ui_file *tmp_error_stream,
2421 int *disabled_breaks,
2422 int *hw_breakpoint_error,
2423 int *hw_bp_error_explained_already)
2424 {
2425 gdb_exception bp_excpt;
2426
2427 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2428 return 0;
2429
2430 /* Note we don't initialize bl->target_info, as that wipes out
2431 the breakpoint location's shadow_contents if the breakpoint
2432 is still inserted at that location. This in turn breaks
2433 target_read_memory which depends on these buffers when
2434 a memory read is requested at the breakpoint location:
2435 Once the target_info has been wiped, we fail to see that
2436 we have a breakpoint inserted at that address and thus
2437 read the breakpoint instead of returning the data saved in
2438 the breakpoint location's shadow contents. */
2439 bl->target_info.reqstd_address = bl->address;
2440 bl->target_info.placed_address_space = bl->pspace->aspace;
2441 bl->target_info.length = bl->length;
2442
2443 /* When working with target-side conditions, we must pass all the conditions
2444 for the same breakpoint address down to the target since GDB will not
2445 insert those locations. With a list of breakpoint conditions, the target
2446 can decide when to stop and notify GDB. */
2447
2448 if (is_breakpoint (bl->owner))
2449 {
2450 build_target_condition_list (bl);
2451 build_target_command_list (bl);
2452 /* Reset the modification marker. */
2453 bl->needs_update = 0;
2454 }
2455
2456 if (bl->loc_type == bp_loc_software_breakpoint
2457 || bl->loc_type == bp_loc_hardware_breakpoint)
2458 {
2459 if (bl->owner->type != bp_hardware_breakpoint)
2460 {
2461 /* If the explicitly specified breakpoint type
2462 is not hardware breakpoint, check the memory map to see
2463 if the breakpoint address is in read only memory or not.
2464
2465 Two important cases are:
2466 - location type is not hardware breakpoint, memory
2467 is readonly. We change the type of the location to
2468 hardware breakpoint.
2469 - location type is hardware breakpoint, memory is
2470 read-write. This means we've previously made the
2471 location hardware one, but then the memory map changed,
2472 so we undo.
2473
2474 When breakpoints are removed, remove_breakpoints will use
2475 location types we've just set here, the only possible
2476 problem is that memory map has changed during running
2477 program, but it's not going to work anyway with current
2478 gdb. */
2479 struct mem_region *mr
2480 = lookup_mem_region (bl->target_info.reqstd_address);
2481
2482 if (mr)
2483 {
2484 if (automatic_hardware_breakpoints)
2485 {
2486 enum bp_loc_type new_type;
2487
2488 if (mr->attrib.mode != MEM_RW)
2489 new_type = bp_loc_hardware_breakpoint;
2490 else
2491 new_type = bp_loc_software_breakpoint;
2492
2493 if (new_type != bl->loc_type)
2494 {
2495 static int said = 0;
2496
2497 bl->loc_type = new_type;
2498 if (!said)
2499 {
2500 fprintf_filtered (gdb_stdout,
2501 _("Note: automatically using "
2502 "hardware breakpoints for "
2503 "read-only addresses.\n"));
2504 said = 1;
2505 }
2506 }
2507 }
2508 else if (bl->loc_type == bp_loc_software_breakpoint
2509 && mr->attrib.mode != MEM_RW)
2510 {
2511 fprintf_unfiltered (tmp_error_stream,
2512 _("Cannot insert breakpoint %d.\n"
2513 "Cannot set software breakpoint "
2514 "at read-only address %s\n"),
2515 bl->owner->number,
2516 paddress (bl->gdbarch, bl->address));
2517 return 1;
2518 }
2519 }
2520 }
2521
2522 /* First check to see if we have to handle an overlay. */
2523 if (overlay_debugging == ovly_off
2524 || bl->section == NULL
2525 || !(section_is_overlay (bl->section)))
2526 {
2527 /* No overlay handling: just set the breakpoint. */
2528 try
2529 {
2530 int val;
2531
2532 val = bl->owner->ops->insert_location (bl);
2533 if (val)
2534 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2535 }
2536 catch (gdb_exception &e)
2537 {
2538 bp_excpt = std::move (e);
2539 }
2540 }
2541 else
2542 {
2543 /* This breakpoint is in an overlay section.
2544 Shall we set a breakpoint at the LMA? */
2545 if (!overlay_events_enabled)
2546 {
2547 /* Yes -- overlay event support is not active,
2548 so we must try to set a breakpoint at the LMA.
2549 This will not work for a hardware breakpoint. */
2550 if (bl->loc_type == bp_loc_hardware_breakpoint)
2551 warning (_("hardware breakpoint %d not supported in overlay!"),
2552 bl->owner->number);
2553 else
2554 {
2555 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2556 bl->section);
2557 /* Set a software (trap) breakpoint at the LMA. */
2558 bl->overlay_target_info = bl->target_info;
2559 bl->overlay_target_info.reqstd_address = addr;
2560
2561 /* No overlay handling: just set the breakpoint. */
2562 try
2563 {
2564 int val;
2565
2566 bl->overlay_target_info.kind
2567 = breakpoint_kind (bl, &addr);
2568 bl->overlay_target_info.placed_address = addr;
2569 val = target_insert_breakpoint (bl->gdbarch,
2570 &bl->overlay_target_info);
2571 if (val)
2572 bp_excpt
2573 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2574 }
2575 catch (gdb_exception &e)
2576 {
2577 bp_excpt = std::move (e);
2578 }
2579
2580 if (bp_excpt.reason != 0)
2581 fprintf_unfiltered (tmp_error_stream,
2582 "Overlay breakpoint %d "
2583 "failed: in ROM?\n",
2584 bl->owner->number);
2585 }
2586 }
2587 /* Shall we set a breakpoint at the VMA? */
2588 if (section_is_mapped (bl->section))
2589 {
2590 /* Yes. This overlay section is mapped into memory. */
2591 try
2592 {
2593 int val;
2594
2595 val = bl->owner->ops->insert_location (bl);
2596 if (val)
2597 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2598 }
2599 catch (gdb_exception &e)
2600 {
2601 bp_excpt = std::move (e);
2602 }
2603 }
2604 else
2605 {
2606 /* No. This breakpoint will not be inserted.
2607 No error, but do not mark the bp as 'inserted'. */
2608 return 0;
2609 }
2610 }
2611
2612 if (bp_excpt.reason != 0)
2613 {
2614 /* Can't set the breakpoint. */
2615
2616 /* In some cases, we might not be able to insert a
2617 breakpoint in a shared library that has already been
2618 removed, but we have not yet processed the shlib unload
2619 event. Unfortunately, some targets that implement
2620 breakpoint insertion themselves can't tell why the
2621 breakpoint insertion failed (e.g., the remote target
2622 doesn't define error codes), so we must treat generic
2623 errors as memory errors. */
2624 if (bp_excpt.reason == RETURN_ERROR
2625 && (bp_excpt.error == GENERIC_ERROR
2626 || bp_excpt.error == MEMORY_ERROR)
2627 && bl->loc_type == bp_loc_software_breakpoint
2628 && (solib_name_from_address (bl->pspace, bl->address)
2629 || shared_objfile_contains_address_p (bl->pspace,
2630 bl->address)))
2631 {
2632 /* See also: disable_breakpoints_in_shlibs. */
2633 bl->shlib_disabled = 1;
2634 gdb::observers::breakpoint_modified.notify (bl->owner);
2635 if (!*disabled_breaks)
2636 {
2637 fprintf_unfiltered (tmp_error_stream,
2638 "Cannot insert breakpoint %d.\n",
2639 bl->owner->number);
2640 fprintf_unfiltered (tmp_error_stream,
2641 "Temporarily disabling shared "
2642 "library breakpoints:\n");
2643 }
2644 *disabled_breaks = 1;
2645 fprintf_unfiltered (tmp_error_stream,
2646 "breakpoint #%d\n", bl->owner->number);
2647 return 0;
2648 }
2649 else
2650 {
2651 if (bl->loc_type == bp_loc_hardware_breakpoint)
2652 {
2653 *hw_breakpoint_error = 1;
2654 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2655 fprintf_unfiltered (tmp_error_stream,
2656 "Cannot insert hardware breakpoint %d%s",
2657 bl->owner->number,
2658 bp_excpt.message ? ":" : ".\n");
2659 if (bp_excpt.message != NULL)
2660 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2661 bp_excpt.what ());
2662 }
2663 else
2664 {
2665 if (bp_excpt.message == NULL)
2666 {
2667 std::string message
2668 = memory_error_message (TARGET_XFER_E_IO,
2669 bl->gdbarch, bl->address);
2670
2671 fprintf_unfiltered (tmp_error_stream,
2672 "Cannot insert breakpoint %d.\n"
2673 "%s\n",
2674 bl->owner->number, message.c_str ());
2675 }
2676 else
2677 {
2678 fprintf_unfiltered (tmp_error_stream,
2679 "Cannot insert breakpoint %d: %s\n",
2680 bl->owner->number,
2681 bp_excpt.what ());
2682 }
2683 }
2684 return 1;
2685
2686 }
2687 }
2688 else
2689 bl->inserted = 1;
2690
2691 return 0;
2692 }
2693
2694 else if (bl->loc_type == bp_loc_hardware_watchpoint
2695 /* NOTE drow/2003-09-08: This state only exists for removing
2696 watchpoints. It's not clear that it's necessary... */
2697 && bl->owner->disposition != disp_del_at_next_stop)
2698 {
2699 int val;
2700
2701 gdb_assert (bl->owner->ops != NULL
2702 && bl->owner->ops->insert_location != NULL);
2703
2704 val = bl->owner->ops->insert_location (bl);
2705
2706 /* If trying to set a read-watchpoint, and it turns out it's not
2707 supported, try emulating one with an access watchpoint. */
2708 if (val == 1 && bl->watchpoint_type == hw_read)
2709 {
2710 struct bp_location *loc, **loc_temp;
2711
2712 /* But don't try to insert it, if there's already another
2713 hw_access location that would be considered a duplicate
2714 of this one. */
2715 ALL_BP_LOCATIONS (loc, loc_temp)
2716 if (loc != bl
2717 && loc->watchpoint_type == hw_access
2718 && watchpoint_locations_match (bl, loc))
2719 {
2720 bl->duplicate = 1;
2721 bl->inserted = 1;
2722 bl->target_info = loc->target_info;
2723 bl->watchpoint_type = hw_access;
2724 val = 0;
2725 break;
2726 }
2727
2728 if (val == 1)
2729 {
2730 bl->watchpoint_type = hw_access;
2731 val = bl->owner->ops->insert_location (bl);
2732
2733 if (val)
2734 /* Back to the original value. */
2735 bl->watchpoint_type = hw_read;
2736 }
2737 }
2738
2739 bl->inserted = (val == 0);
2740 }
2741
2742 else if (bl->owner->type == bp_catchpoint)
2743 {
2744 int val;
2745
2746 gdb_assert (bl->owner->ops != NULL
2747 && bl->owner->ops->insert_location != NULL);
2748
2749 val = bl->owner->ops->insert_location (bl);
2750 if (val)
2751 {
2752 bl->owner->enable_state = bp_disabled;
2753
2754 if (val == 1)
2755 warning (_("\
2756 Error inserting catchpoint %d: Your system does not support this type\n\
2757 of catchpoint."), bl->owner->number);
2758 else
2759 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2760 }
2761
2762 bl->inserted = (val == 0);
2763
2764 /* We've already printed an error message if there was a problem
2765 inserting this catchpoint, and we've disabled the catchpoint,
2766 so just return success. */
2767 return 0;
2768 }
2769
2770 return 0;
2771 }
2772
2773 /* This function is called when program space PSPACE is about to be
2774 deleted. It takes care of updating breakpoints to not reference
2775 PSPACE anymore. */
2776
2777 void
2778 breakpoint_program_space_exit (struct program_space *pspace)
2779 {
2780 struct breakpoint *b, *b_temp;
2781 struct bp_location *loc, **loc_temp;
2782
2783 /* Remove any breakpoint that was set through this program space. */
2784 ALL_BREAKPOINTS_SAFE (b, b_temp)
2785 {
2786 if (b->pspace == pspace)
2787 delete_breakpoint (b);
2788 }
2789
2790 /* Breakpoints set through other program spaces could have locations
2791 bound to PSPACE as well. Remove those. */
2792 ALL_BP_LOCATIONS (loc, loc_temp)
2793 {
2794 struct bp_location *tmp;
2795
2796 if (loc->pspace == pspace)
2797 {
2798 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2799 if (loc->owner->loc == loc)
2800 loc->owner->loc = loc->next;
2801 else
2802 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2803 if (tmp->next == loc)
2804 {
2805 tmp->next = loc->next;
2806 break;
2807 }
2808 }
2809 }
2810
2811 /* Now update the global location list to permanently delete the
2812 removed locations above. */
2813 update_global_location_list (UGLL_DONT_INSERT);
2814 }
2815
2816 /* Make sure all breakpoints are inserted in inferior.
2817 Throws exception on any error.
2818 A breakpoint that is already inserted won't be inserted
2819 again, so calling this function twice is safe. */
2820 void
2821 insert_breakpoints (void)
2822 {
2823 struct breakpoint *bpt;
2824
2825 ALL_BREAKPOINTS (bpt)
2826 if (is_hardware_watchpoint (bpt))
2827 {
2828 struct watchpoint *w = (struct watchpoint *) bpt;
2829
2830 update_watchpoint (w, 0 /* don't reparse. */);
2831 }
2832
2833 /* Updating watchpoints creates new locations, so update the global
2834 location list. Explicitly tell ugll to insert locations and
2835 ignore breakpoints_always_inserted_mode. */
2836 update_global_location_list (UGLL_INSERT);
2837 }
2838
2839 /* Invoke CALLBACK for each of bp_location. */
2840
2841 void
2842 iterate_over_bp_locations (walk_bp_location_callback callback)
2843 {
2844 struct bp_location *loc, **loc_tmp;
2845
2846 ALL_BP_LOCATIONS (loc, loc_tmp)
2847 {
2848 callback (loc, NULL);
2849 }
2850 }
2851
2852 /* This is used when we need to synch breakpoint conditions between GDB and the
2853 target. It is the case with deleting and disabling of breakpoints when using
2854 always-inserted mode. */
2855
2856 static void
2857 update_inserted_breakpoint_locations (void)
2858 {
2859 struct bp_location *bl, **blp_tmp;
2860 int error_flag = 0;
2861 int val = 0;
2862 int disabled_breaks = 0;
2863 int hw_breakpoint_error = 0;
2864 int hw_bp_details_reported = 0;
2865
2866 string_file tmp_error_stream;
2867
2868 /* Explicitly mark the warning -- this will only be printed if
2869 there was an error. */
2870 tmp_error_stream.puts ("Warning:\n");
2871
2872 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2873
2874 ALL_BP_LOCATIONS (bl, blp_tmp)
2875 {
2876 /* We only want to update software breakpoints and hardware
2877 breakpoints. */
2878 if (!is_breakpoint (bl->owner))
2879 continue;
2880
2881 /* We only want to update locations that are already inserted
2882 and need updating. This is to avoid unwanted insertion during
2883 deletion of breakpoints. */
2884 if (!bl->inserted || !bl->needs_update)
2885 continue;
2886
2887 switch_to_program_space_and_thread (bl->pspace);
2888
2889 /* For targets that support global breakpoints, there's no need
2890 to select an inferior to insert breakpoint to. In fact, even
2891 if we aren't attached to any process yet, we should still
2892 insert breakpoints. */
2893 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2894 && inferior_ptid == null_ptid)
2895 continue;
2896
2897 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2898 &hw_breakpoint_error, &hw_bp_details_reported);
2899 if (val)
2900 error_flag = val;
2901 }
2902
2903 if (error_flag)
2904 {
2905 target_terminal::ours_for_output ();
2906 error_stream (tmp_error_stream);
2907 }
2908 }
2909
2910 /* Used when starting or continuing the program. */
2911
2912 static void
2913 insert_breakpoint_locations (void)
2914 {
2915 struct breakpoint *bpt;
2916 struct bp_location *bl, **blp_tmp;
2917 int error_flag = 0;
2918 int val = 0;
2919 int disabled_breaks = 0;
2920 int hw_breakpoint_error = 0;
2921 int hw_bp_error_explained_already = 0;
2922
2923 string_file tmp_error_stream;
2924
2925 /* Explicitly mark the warning -- this will only be printed if
2926 there was an error. */
2927 tmp_error_stream.puts ("Warning:\n");
2928
2929 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2930
2931 ALL_BP_LOCATIONS (bl, blp_tmp)
2932 {
2933 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2934 continue;
2935
2936 /* There is no point inserting thread-specific breakpoints if
2937 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2938 has BL->OWNER always non-NULL. */
2939 if (bl->owner->thread != -1
2940 && !valid_global_thread_id (bl->owner->thread))
2941 continue;
2942
2943 switch_to_program_space_and_thread (bl->pspace);
2944
2945 /* For targets that support global breakpoints, there's no need
2946 to select an inferior to insert breakpoint to. In fact, even
2947 if we aren't attached to any process yet, we should still
2948 insert breakpoints. */
2949 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2950 && inferior_ptid == null_ptid)
2951 continue;
2952
2953 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2954 &hw_breakpoint_error, &hw_bp_error_explained_already);
2955 if (val)
2956 error_flag = val;
2957 }
2958
2959 /* If we failed to insert all locations of a watchpoint, remove
2960 them, as half-inserted watchpoint is of limited use. */
2961 ALL_BREAKPOINTS (bpt)
2962 {
2963 int some_failed = 0;
2964 struct bp_location *loc;
2965
2966 if (!is_hardware_watchpoint (bpt))
2967 continue;
2968
2969 if (!breakpoint_enabled (bpt))
2970 continue;
2971
2972 if (bpt->disposition == disp_del_at_next_stop)
2973 continue;
2974
2975 for (loc = bpt->loc; loc; loc = loc->next)
2976 if (!loc->inserted && should_be_inserted (loc))
2977 {
2978 some_failed = 1;
2979 break;
2980 }
2981 if (some_failed)
2982 {
2983 for (loc = bpt->loc; loc; loc = loc->next)
2984 if (loc->inserted)
2985 remove_breakpoint (loc);
2986
2987 hw_breakpoint_error = 1;
2988 tmp_error_stream.printf ("Could not insert "
2989 "hardware watchpoint %d.\n",
2990 bpt->number);
2991 error_flag = -1;
2992 }
2993 }
2994
2995 if (error_flag)
2996 {
2997 /* If a hardware breakpoint or watchpoint was inserted, add a
2998 message about possibly exhausted resources. */
2999 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3000 {
3001 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3002 You may have requested too many hardware breakpoints/watchpoints.\n");
3003 }
3004 target_terminal::ours_for_output ();
3005 error_stream (tmp_error_stream);
3006 }
3007 }
3008
3009 /* Used when the program stops.
3010 Returns zero if successful, or non-zero if there was a problem
3011 removing a breakpoint location. */
3012
3013 int
3014 remove_breakpoints (void)
3015 {
3016 struct bp_location *bl, **blp_tmp;
3017 int val = 0;
3018
3019 ALL_BP_LOCATIONS (bl, blp_tmp)
3020 {
3021 if (bl->inserted && !is_tracepoint (bl->owner))
3022 val |= remove_breakpoint (bl);
3023 }
3024 return val;
3025 }
3026
3027 /* When a thread exits, remove breakpoints that are related to
3028 that thread. */
3029
3030 static void
3031 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3032 {
3033 struct breakpoint *b, *b_tmp;
3034
3035 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3036 {
3037 if (b->thread == tp->global_num && user_breakpoint_p (b))
3038 {
3039 b->disposition = disp_del_at_next_stop;
3040
3041 printf_filtered (_("\
3042 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3043 b->number, print_thread_id (tp));
3044
3045 /* Hide it from the user. */
3046 b->number = 0;
3047 }
3048 }
3049 }
3050
3051 /* See breakpoint.h. */
3052
3053 void
3054 remove_breakpoints_inf (inferior *inf)
3055 {
3056 struct bp_location *bl, **blp_tmp;
3057 int val;
3058
3059 ALL_BP_LOCATIONS (bl, blp_tmp)
3060 {
3061 if (bl->pspace != inf->pspace)
3062 continue;
3063
3064 if (bl->inserted && !bl->target_info.persist)
3065 {
3066 val = remove_breakpoint (bl);
3067 if (val != 0)
3068 return;
3069 }
3070 }
3071 }
3072
3073 static int internal_breakpoint_number = -1;
3074
3075 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3076 If INTERNAL is non-zero, the breakpoint number will be populated
3077 from internal_breakpoint_number and that variable decremented.
3078 Otherwise the breakpoint number will be populated from
3079 breakpoint_count and that value incremented. Internal breakpoints
3080 do not set the internal var bpnum. */
3081 static void
3082 set_breakpoint_number (int internal, struct breakpoint *b)
3083 {
3084 if (internal)
3085 b->number = internal_breakpoint_number--;
3086 else
3087 {
3088 set_breakpoint_count (breakpoint_count + 1);
3089 b->number = breakpoint_count;
3090 }
3091 }
3092
3093 static struct breakpoint *
3094 create_internal_breakpoint (struct gdbarch *gdbarch,
3095 CORE_ADDR address, enum bptype type,
3096 const struct breakpoint_ops *ops)
3097 {
3098 symtab_and_line sal;
3099 sal.pc = address;
3100 sal.section = find_pc_overlay (sal.pc);
3101 sal.pspace = current_program_space;
3102
3103 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3104 b->number = internal_breakpoint_number--;
3105 b->disposition = disp_donttouch;
3106
3107 return b;
3108 }
3109
3110 static const char *const longjmp_names[] =
3111 {
3112 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3113 };
3114 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3115
3116 /* Per-objfile data private to breakpoint.c. */
3117 struct breakpoint_objfile_data
3118 {
3119 /* Minimal symbol for "_ovly_debug_event" (if any). */
3120 struct bound_minimal_symbol overlay_msym {};
3121
3122 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3123 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3124
3125 /* True if we have looked for longjmp probes. */
3126 int longjmp_searched = 0;
3127
3128 /* SystemTap probe points for longjmp (if any). These are non-owning
3129 references. */
3130 std::vector<probe *> longjmp_probes;
3131
3132 /* Minimal symbol for "std::terminate()" (if any). */
3133 struct bound_minimal_symbol terminate_msym {};
3134
3135 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3136 struct bound_minimal_symbol exception_msym {};
3137
3138 /* True if we have looked for exception probes. */
3139 int exception_searched = 0;
3140
3141 /* SystemTap probe points for unwinding (if any). These are non-owning
3142 references. */
3143 std::vector<probe *> exception_probes;
3144 };
3145
3146 static const struct objfile_key<breakpoint_objfile_data>
3147 breakpoint_objfile_key;
3148
3149 /* Minimal symbol not found sentinel. */
3150 static struct minimal_symbol msym_not_found;
3151
3152 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3153
3154 static int
3155 msym_not_found_p (const struct minimal_symbol *msym)
3156 {
3157 return msym == &msym_not_found;
3158 }
3159
3160 /* Return per-objfile data needed by breakpoint.c.
3161 Allocate the data if necessary. */
3162
3163 static struct breakpoint_objfile_data *
3164 get_breakpoint_objfile_data (struct objfile *objfile)
3165 {
3166 struct breakpoint_objfile_data *bp_objfile_data;
3167
3168 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3169 if (bp_objfile_data == NULL)
3170 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3171 return bp_objfile_data;
3172 }
3173
3174 static void
3175 create_overlay_event_breakpoint (void)
3176 {
3177 const char *const func_name = "_ovly_debug_event";
3178
3179 for (objfile *objfile : current_program_space->objfiles ())
3180 {
3181 struct breakpoint *b;
3182 struct breakpoint_objfile_data *bp_objfile_data;
3183 CORE_ADDR addr;
3184 struct explicit_location explicit_loc;
3185
3186 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3187
3188 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3189 continue;
3190
3191 if (bp_objfile_data->overlay_msym.minsym == NULL)
3192 {
3193 struct bound_minimal_symbol m;
3194
3195 m = lookup_minimal_symbol_text (func_name, objfile);
3196 if (m.minsym == NULL)
3197 {
3198 /* Avoid future lookups in this objfile. */
3199 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3200 continue;
3201 }
3202 bp_objfile_data->overlay_msym = m;
3203 }
3204
3205 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3206 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3207 bp_overlay_event,
3208 &internal_breakpoint_ops);
3209 initialize_explicit_location (&explicit_loc);
3210 explicit_loc.function_name = ASTRDUP (func_name);
3211 b->location = new_explicit_location (&explicit_loc);
3212
3213 if (overlay_debugging == ovly_auto)
3214 {
3215 b->enable_state = bp_enabled;
3216 overlay_events_enabled = 1;
3217 }
3218 else
3219 {
3220 b->enable_state = bp_disabled;
3221 overlay_events_enabled = 0;
3222 }
3223 }
3224 }
3225
3226 static void
3227 create_longjmp_master_breakpoint (void)
3228 {
3229 struct program_space *pspace;
3230
3231 scoped_restore_current_program_space restore_pspace;
3232
3233 ALL_PSPACES (pspace)
3234 {
3235 set_current_program_space (pspace);
3236
3237 for (objfile *objfile : current_program_space->objfiles ())
3238 {
3239 int i;
3240 struct gdbarch *gdbarch;
3241 struct breakpoint_objfile_data *bp_objfile_data;
3242
3243 gdbarch = get_objfile_arch (objfile);
3244
3245 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3246
3247 if (!bp_objfile_data->longjmp_searched)
3248 {
3249 std::vector<probe *> ret
3250 = find_probes_in_objfile (objfile, "libc", "longjmp");
3251
3252 if (!ret.empty ())
3253 {
3254 /* We are only interested in checking one element. */
3255 probe *p = ret[0];
3256
3257 if (!p->can_evaluate_arguments ())
3258 {
3259 /* We cannot use the probe interface here, because it does
3260 not know how to evaluate arguments. */
3261 ret.clear ();
3262 }
3263 }
3264 bp_objfile_data->longjmp_probes = ret;
3265 bp_objfile_data->longjmp_searched = 1;
3266 }
3267
3268 if (!bp_objfile_data->longjmp_probes.empty ())
3269 {
3270 for (probe *p : bp_objfile_data->longjmp_probes)
3271 {
3272 struct breakpoint *b;
3273
3274 b = create_internal_breakpoint (gdbarch,
3275 p->get_relocated_address (objfile),
3276 bp_longjmp_master,
3277 &internal_breakpoint_ops);
3278 b->location = new_probe_location ("-probe-stap libc:longjmp");
3279 b->enable_state = bp_disabled;
3280 }
3281
3282 continue;
3283 }
3284
3285 if (!gdbarch_get_longjmp_target_p (gdbarch))
3286 continue;
3287
3288 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3289 {
3290 struct breakpoint *b;
3291 const char *func_name;
3292 CORE_ADDR addr;
3293 struct explicit_location explicit_loc;
3294
3295 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3296 continue;
3297
3298 func_name = longjmp_names[i];
3299 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3300 {
3301 struct bound_minimal_symbol m;
3302
3303 m = lookup_minimal_symbol_text (func_name, objfile);
3304 if (m.minsym == NULL)
3305 {
3306 /* Prevent future lookups in this objfile. */
3307 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3308 continue;
3309 }
3310 bp_objfile_data->longjmp_msym[i] = m;
3311 }
3312
3313 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3314 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3315 &internal_breakpoint_ops);
3316 initialize_explicit_location (&explicit_loc);
3317 explicit_loc.function_name = ASTRDUP (func_name);
3318 b->location = new_explicit_location (&explicit_loc);
3319 b->enable_state = bp_disabled;
3320 }
3321 }
3322 }
3323 }
3324
3325 /* Create a master std::terminate breakpoint. */
3326 static void
3327 create_std_terminate_master_breakpoint (void)
3328 {
3329 struct program_space *pspace;
3330 const char *const func_name = "std::terminate()";
3331
3332 scoped_restore_current_program_space restore_pspace;
3333
3334 ALL_PSPACES (pspace)
3335 {
3336 CORE_ADDR addr;
3337
3338 set_current_program_space (pspace);
3339
3340 for (objfile *objfile : current_program_space->objfiles ())
3341 {
3342 struct breakpoint *b;
3343 struct breakpoint_objfile_data *bp_objfile_data;
3344 struct explicit_location explicit_loc;
3345
3346 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3347
3348 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3349 continue;
3350
3351 if (bp_objfile_data->terminate_msym.minsym == NULL)
3352 {
3353 struct bound_minimal_symbol m;
3354
3355 m = lookup_minimal_symbol (func_name, NULL, objfile);
3356 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3357 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3358 {
3359 /* Prevent future lookups in this objfile. */
3360 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3361 continue;
3362 }
3363 bp_objfile_data->terminate_msym = m;
3364 }
3365
3366 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3367 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3368 bp_std_terminate_master,
3369 &internal_breakpoint_ops);
3370 initialize_explicit_location (&explicit_loc);
3371 explicit_loc.function_name = ASTRDUP (func_name);
3372 b->location = new_explicit_location (&explicit_loc);
3373 b->enable_state = bp_disabled;
3374 }
3375 }
3376 }
3377
3378 /* Install a master breakpoint on the unwinder's debug hook. */
3379
3380 static void
3381 create_exception_master_breakpoint (void)
3382 {
3383 const char *const func_name = "_Unwind_DebugHook";
3384
3385 for (objfile *objfile : current_program_space->objfiles ())
3386 {
3387 struct breakpoint *b;
3388 struct gdbarch *gdbarch;
3389 struct breakpoint_objfile_data *bp_objfile_data;
3390 CORE_ADDR addr;
3391 struct explicit_location explicit_loc;
3392
3393 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3394
3395 /* We prefer the SystemTap probe point if it exists. */
3396 if (!bp_objfile_data->exception_searched)
3397 {
3398 std::vector<probe *> ret
3399 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3400
3401 if (!ret.empty ())
3402 {
3403 /* We are only interested in checking one element. */
3404 probe *p = ret[0];
3405
3406 if (!p->can_evaluate_arguments ())
3407 {
3408 /* We cannot use the probe interface here, because it does
3409 not know how to evaluate arguments. */
3410 ret.clear ();
3411 }
3412 }
3413 bp_objfile_data->exception_probes = ret;
3414 bp_objfile_data->exception_searched = 1;
3415 }
3416
3417 if (!bp_objfile_data->exception_probes.empty ())
3418 {
3419 gdbarch = get_objfile_arch (objfile);
3420
3421 for (probe *p : bp_objfile_data->exception_probes)
3422 {
3423 b = create_internal_breakpoint (gdbarch,
3424 p->get_relocated_address (objfile),
3425 bp_exception_master,
3426 &internal_breakpoint_ops);
3427 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3428 b->enable_state = bp_disabled;
3429 }
3430
3431 continue;
3432 }
3433
3434 /* Otherwise, try the hook function. */
3435
3436 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3437 continue;
3438
3439 gdbarch = get_objfile_arch (objfile);
3440
3441 if (bp_objfile_data->exception_msym.minsym == NULL)
3442 {
3443 struct bound_minimal_symbol debug_hook;
3444
3445 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3446 if (debug_hook.minsym == NULL)
3447 {
3448 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3449 continue;
3450 }
3451
3452 bp_objfile_data->exception_msym = debug_hook;
3453 }
3454
3455 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3456 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3457 current_top_target ());
3458 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3459 &internal_breakpoint_ops);
3460 initialize_explicit_location (&explicit_loc);
3461 explicit_loc.function_name = ASTRDUP (func_name);
3462 b->location = new_explicit_location (&explicit_loc);
3463 b->enable_state = bp_disabled;
3464 }
3465 }
3466
3467 /* Does B have a location spec? */
3468
3469 static int
3470 breakpoint_event_location_empty_p (const struct breakpoint *b)
3471 {
3472 return b->location != NULL && event_location_empty_p (b->location.get ());
3473 }
3474
3475 void
3476 update_breakpoints_after_exec (void)
3477 {
3478 struct breakpoint *b, *b_tmp;
3479 struct bp_location *bploc, **bplocp_tmp;
3480
3481 /* We're about to delete breakpoints from GDB's lists. If the
3482 INSERTED flag is true, GDB will try to lift the breakpoints by
3483 writing the breakpoints' "shadow contents" back into memory. The
3484 "shadow contents" are NOT valid after an exec, so GDB should not
3485 do that. Instead, the target is responsible from marking
3486 breakpoints out as soon as it detects an exec. We don't do that
3487 here instead, because there may be other attempts to delete
3488 breakpoints after detecting an exec and before reaching here. */
3489 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3490 if (bploc->pspace == current_program_space)
3491 gdb_assert (!bploc->inserted);
3492
3493 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3494 {
3495 if (b->pspace != current_program_space)
3496 continue;
3497
3498 /* Solib breakpoints must be explicitly reset after an exec(). */
3499 if (b->type == bp_shlib_event)
3500 {
3501 delete_breakpoint (b);
3502 continue;
3503 }
3504
3505 /* JIT breakpoints must be explicitly reset after an exec(). */
3506 if (b->type == bp_jit_event)
3507 {
3508 delete_breakpoint (b);
3509 continue;
3510 }
3511
3512 /* Thread event breakpoints must be set anew after an exec(),
3513 as must overlay event and longjmp master breakpoints. */
3514 if (b->type == bp_thread_event || b->type == bp_overlay_event
3515 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3516 || b->type == bp_exception_master)
3517 {
3518 delete_breakpoint (b);
3519 continue;
3520 }
3521
3522 /* Step-resume breakpoints are meaningless after an exec(). */
3523 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3524 {
3525 delete_breakpoint (b);
3526 continue;
3527 }
3528
3529 /* Just like single-step breakpoints. */
3530 if (b->type == bp_single_step)
3531 {
3532 delete_breakpoint (b);
3533 continue;
3534 }
3535
3536 /* Longjmp and longjmp-resume breakpoints are also meaningless
3537 after an exec. */
3538 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3539 || b->type == bp_longjmp_call_dummy
3540 || b->type == bp_exception || b->type == bp_exception_resume)
3541 {
3542 delete_breakpoint (b);
3543 continue;
3544 }
3545
3546 if (b->type == bp_catchpoint)
3547 {
3548 /* For now, none of the bp_catchpoint breakpoints need to
3549 do anything at this point. In the future, if some of
3550 the catchpoints need to something, we will need to add
3551 a new method, and call this method from here. */
3552 continue;
3553 }
3554
3555 /* bp_finish is a special case. The only way we ought to be able
3556 to see one of these when an exec() has happened, is if the user
3557 caught a vfork, and then said "finish". Ordinarily a finish just
3558 carries them to the call-site of the current callee, by setting
3559 a temporary bp there and resuming. But in this case, the finish
3560 will carry them entirely through the vfork & exec.
3561
3562 We don't want to allow a bp_finish to remain inserted now. But
3563 we can't safely delete it, 'cause finish_command has a handle to
3564 the bp on a bpstat, and will later want to delete it. There's a
3565 chance (and I've seen it happen) that if we delete the bp_finish
3566 here, that its storage will get reused by the time finish_command
3567 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3568 We really must allow finish_command to delete a bp_finish.
3569
3570 In the absence of a general solution for the "how do we know
3571 it's safe to delete something others may have handles to?"
3572 problem, what we'll do here is just uninsert the bp_finish, and
3573 let finish_command delete it.
3574
3575 (We know the bp_finish is "doomed" in the sense that it's
3576 momentary, and will be deleted as soon as finish_command sees
3577 the inferior stopped. So it doesn't matter that the bp's
3578 address is probably bogus in the new a.out, unlike e.g., the
3579 solib breakpoints.) */
3580
3581 if (b->type == bp_finish)
3582 {
3583 continue;
3584 }
3585
3586 /* Without a symbolic address, we have little hope of the
3587 pre-exec() address meaning the same thing in the post-exec()
3588 a.out. */
3589 if (breakpoint_event_location_empty_p (b))
3590 {
3591 delete_breakpoint (b);
3592 continue;
3593 }
3594 }
3595 }
3596
3597 int
3598 detach_breakpoints (ptid_t ptid)
3599 {
3600 struct bp_location *bl, **blp_tmp;
3601 int val = 0;
3602 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3603 struct inferior *inf = current_inferior ();
3604
3605 if (ptid.pid () == inferior_ptid.pid ())
3606 error (_("Cannot detach breakpoints of inferior_ptid"));
3607
3608 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3609 inferior_ptid = ptid;
3610 ALL_BP_LOCATIONS (bl, blp_tmp)
3611 {
3612 if (bl->pspace != inf->pspace)
3613 continue;
3614
3615 /* This function must physically remove breakpoints locations
3616 from the specified ptid, without modifying the breakpoint
3617 package's state. Locations of type bp_loc_other are only
3618 maintained at GDB side. So, there is no need to remove
3619 these bp_loc_other locations. Moreover, removing these
3620 would modify the breakpoint package's state. */
3621 if (bl->loc_type == bp_loc_other)
3622 continue;
3623
3624 if (bl->inserted)
3625 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3626 }
3627
3628 return val;
3629 }
3630
3631 /* Remove the breakpoint location BL from the current address space.
3632 Note that this is used to detach breakpoints from a child fork.
3633 When we get here, the child isn't in the inferior list, and neither
3634 do we have objects to represent its address space --- we should
3635 *not* look at bl->pspace->aspace here. */
3636
3637 static int
3638 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3639 {
3640 int val;
3641
3642 /* BL is never in moribund_locations by our callers. */
3643 gdb_assert (bl->owner != NULL);
3644
3645 /* The type of none suggests that owner is actually deleted.
3646 This should not ever happen. */
3647 gdb_assert (bl->owner->type != bp_none);
3648
3649 if (bl->loc_type == bp_loc_software_breakpoint
3650 || bl->loc_type == bp_loc_hardware_breakpoint)
3651 {
3652 /* "Normal" instruction breakpoint: either the standard
3653 trap-instruction bp (bp_breakpoint), or a
3654 bp_hardware_breakpoint. */
3655
3656 /* First check to see if we have to handle an overlay. */
3657 if (overlay_debugging == ovly_off
3658 || bl->section == NULL
3659 || !(section_is_overlay (bl->section)))
3660 {
3661 /* No overlay handling: just remove the breakpoint. */
3662
3663 /* If we're trying to uninsert a memory breakpoint that we
3664 know is set in a dynamic object that is marked
3665 shlib_disabled, then either the dynamic object was
3666 removed with "remove-symbol-file" or with
3667 "nosharedlibrary". In the former case, we don't know
3668 whether another dynamic object might have loaded over the
3669 breakpoint's address -- the user might well let us know
3670 about it next with add-symbol-file (the whole point of
3671 add-symbol-file is letting the user manually maintain a
3672 list of dynamically loaded objects). If we have the
3673 breakpoint's shadow memory, that is, this is a software
3674 breakpoint managed by GDB, check whether the breakpoint
3675 is still inserted in memory, to avoid overwriting wrong
3676 code with stale saved shadow contents. Note that HW
3677 breakpoints don't have shadow memory, as they're
3678 implemented using a mechanism that is not dependent on
3679 being able to modify the target's memory, and as such
3680 they should always be removed. */
3681 if (bl->shlib_disabled
3682 && bl->target_info.shadow_len != 0
3683 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3684 val = 0;
3685 else
3686 val = bl->owner->ops->remove_location (bl, reason);
3687 }
3688 else
3689 {
3690 /* This breakpoint is in an overlay section.
3691 Did we set a breakpoint at the LMA? */
3692 if (!overlay_events_enabled)
3693 {
3694 /* Yes -- overlay event support is not active, so we
3695 should have set a breakpoint at the LMA. Remove it.
3696 */
3697 /* Ignore any failures: if the LMA is in ROM, we will
3698 have already warned when we failed to insert it. */
3699 if (bl->loc_type == bp_loc_hardware_breakpoint)
3700 target_remove_hw_breakpoint (bl->gdbarch,
3701 &bl->overlay_target_info);
3702 else
3703 target_remove_breakpoint (bl->gdbarch,
3704 &bl->overlay_target_info,
3705 reason);
3706 }
3707 /* Did we set a breakpoint at the VMA?
3708 If so, we will have marked the breakpoint 'inserted'. */
3709 if (bl->inserted)
3710 {
3711 /* Yes -- remove it. Previously we did not bother to
3712 remove the breakpoint if the section had been
3713 unmapped, but let's not rely on that being safe. We
3714 don't know what the overlay manager might do. */
3715
3716 /* However, we should remove *software* breakpoints only
3717 if the section is still mapped, or else we overwrite
3718 wrong code with the saved shadow contents. */
3719 if (bl->loc_type == bp_loc_hardware_breakpoint
3720 || section_is_mapped (bl->section))
3721 val = bl->owner->ops->remove_location (bl, reason);
3722 else
3723 val = 0;
3724 }
3725 else
3726 {
3727 /* No -- not inserted, so no need to remove. No error. */
3728 val = 0;
3729 }
3730 }
3731
3732 /* In some cases, we might not be able to remove a breakpoint in
3733 a shared library that has already been removed, but we have
3734 not yet processed the shlib unload event. Similarly for an
3735 unloaded add-symbol-file object - the user might not yet have
3736 had the chance to remove-symbol-file it. shlib_disabled will
3737 be set if the library/object has already been removed, but
3738 the breakpoint hasn't been uninserted yet, e.g., after
3739 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3740 always-inserted mode. */
3741 if (val
3742 && (bl->loc_type == bp_loc_software_breakpoint
3743 && (bl->shlib_disabled
3744 || solib_name_from_address (bl->pspace, bl->address)
3745 || shared_objfile_contains_address_p (bl->pspace,
3746 bl->address))))
3747 val = 0;
3748
3749 if (val)
3750 return val;
3751 bl->inserted = (reason == DETACH_BREAKPOINT);
3752 }
3753 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3754 {
3755 gdb_assert (bl->owner->ops != NULL
3756 && bl->owner->ops->remove_location != NULL);
3757
3758 bl->inserted = (reason == DETACH_BREAKPOINT);
3759 bl->owner->ops->remove_location (bl, reason);
3760
3761 /* Failure to remove any of the hardware watchpoints comes here. */
3762 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3763 warning (_("Could not remove hardware watchpoint %d."),
3764 bl->owner->number);
3765 }
3766 else if (bl->owner->type == bp_catchpoint
3767 && breakpoint_enabled (bl->owner)
3768 && !bl->duplicate)
3769 {
3770 gdb_assert (bl->owner->ops != NULL
3771 && bl->owner->ops->remove_location != NULL);
3772
3773 val = bl->owner->ops->remove_location (bl, reason);
3774 if (val)
3775 return val;
3776
3777 bl->inserted = (reason == DETACH_BREAKPOINT);
3778 }
3779
3780 return 0;
3781 }
3782
3783 static int
3784 remove_breakpoint (struct bp_location *bl)
3785 {
3786 /* BL is never in moribund_locations by our callers. */
3787 gdb_assert (bl->owner != NULL);
3788
3789 /* The type of none suggests that owner is actually deleted.
3790 This should not ever happen. */
3791 gdb_assert (bl->owner->type != bp_none);
3792
3793 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3794
3795 switch_to_program_space_and_thread (bl->pspace);
3796
3797 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3798 }
3799
3800 /* Clear the "inserted" flag in all breakpoints. */
3801
3802 void
3803 mark_breakpoints_out (void)
3804 {
3805 struct bp_location *bl, **blp_tmp;
3806
3807 ALL_BP_LOCATIONS (bl, blp_tmp)
3808 if (bl->pspace == current_program_space)
3809 bl->inserted = 0;
3810 }
3811
3812 /* Clear the "inserted" flag in all breakpoints and delete any
3813 breakpoints which should go away between runs of the program.
3814
3815 Plus other such housekeeping that has to be done for breakpoints
3816 between runs.
3817
3818 Note: this function gets called at the end of a run (by
3819 generic_mourn_inferior) and when a run begins (by
3820 init_wait_for_inferior). */
3821
3822
3823
3824 void
3825 breakpoint_init_inferior (enum inf_context context)
3826 {
3827 struct breakpoint *b, *b_tmp;
3828 struct program_space *pspace = current_program_space;
3829
3830 /* If breakpoint locations are shared across processes, then there's
3831 nothing to do. */
3832 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3833 return;
3834
3835 mark_breakpoints_out ();
3836
3837 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3838 {
3839 if (b->loc && b->loc->pspace != pspace)
3840 continue;
3841
3842 switch (b->type)
3843 {
3844 case bp_call_dummy:
3845 case bp_longjmp_call_dummy:
3846
3847 /* If the call dummy breakpoint is at the entry point it will
3848 cause problems when the inferior is rerun, so we better get
3849 rid of it. */
3850
3851 case bp_watchpoint_scope:
3852
3853 /* Also get rid of scope breakpoints. */
3854
3855 case bp_shlib_event:
3856
3857 /* Also remove solib event breakpoints. Their addresses may
3858 have changed since the last time we ran the program.
3859 Actually we may now be debugging against different target;
3860 and so the solib backend that installed this breakpoint may
3861 not be used in by the target. E.g.,
3862
3863 (gdb) file prog-linux
3864 (gdb) run # native linux target
3865 ...
3866 (gdb) kill
3867 (gdb) file prog-win.exe
3868 (gdb) tar rem :9999 # remote Windows gdbserver.
3869 */
3870
3871 case bp_step_resume:
3872
3873 /* Also remove step-resume breakpoints. */
3874
3875 case bp_single_step:
3876
3877 /* Also remove single-step breakpoints. */
3878
3879 delete_breakpoint (b);
3880 break;
3881
3882 case bp_watchpoint:
3883 case bp_hardware_watchpoint:
3884 case bp_read_watchpoint:
3885 case bp_access_watchpoint:
3886 {
3887 struct watchpoint *w = (struct watchpoint *) b;
3888
3889 /* Likewise for watchpoints on local expressions. */
3890 if (w->exp_valid_block != NULL)
3891 delete_breakpoint (b);
3892 else
3893 {
3894 /* Get rid of existing locations, which are no longer
3895 valid. New ones will be created in
3896 update_watchpoint, when the inferior is restarted.
3897 The next update_global_location_list call will
3898 garbage collect them. */
3899 b->loc = NULL;
3900
3901 if (context == inf_starting)
3902 {
3903 /* Reset val field to force reread of starting value in
3904 insert_breakpoints. */
3905 w->val.reset (nullptr);
3906 w->val_valid = false;
3907 }
3908 }
3909 }
3910 break;
3911 default:
3912 break;
3913 }
3914 }
3915
3916 /* Get rid of the moribund locations. */
3917 for (bp_location *bl : moribund_locations)
3918 decref_bp_location (&bl);
3919 moribund_locations.clear ();
3920 }
3921
3922 /* These functions concern about actual breakpoints inserted in the
3923 target --- to e.g. check if we need to do decr_pc adjustment or if
3924 we need to hop over the bkpt --- so we check for address space
3925 match, not program space. */
3926
3927 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3928 exists at PC. It returns ordinary_breakpoint_here if it's an
3929 ordinary breakpoint, or permanent_breakpoint_here if it's a
3930 permanent breakpoint.
3931 - When continuing from a location with an ordinary breakpoint, we
3932 actually single step once before calling insert_breakpoints.
3933 - When continuing from a location with a permanent breakpoint, we
3934 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3935 the target, to advance the PC past the breakpoint. */
3936
3937 enum breakpoint_here
3938 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3939 {
3940 struct bp_location *bl, **blp_tmp;
3941 int any_breakpoint_here = 0;
3942
3943 ALL_BP_LOCATIONS (bl, blp_tmp)
3944 {
3945 if (bl->loc_type != bp_loc_software_breakpoint
3946 && bl->loc_type != bp_loc_hardware_breakpoint)
3947 continue;
3948
3949 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3950 if ((breakpoint_enabled (bl->owner)
3951 || bl->permanent)
3952 && breakpoint_location_address_match (bl, aspace, pc))
3953 {
3954 if (overlay_debugging
3955 && section_is_overlay (bl->section)
3956 && !section_is_mapped (bl->section))
3957 continue; /* unmapped overlay -- can't be a match */
3958 else if (bl->permanent)
3959 return permanent_breakpoint_here;
3960 else
3961 any_breakpoint_here = 1;
3962 }
3963 }
3964
3965 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
3966 }
3967
3968 /* See breakpoint.h. */
3969
3970 int
3971 breakpoint_in_range_p (const address_space *aspace,
3972 CORE_ADDR addr, ULONGEST len)
3973 {
3974 struct bp_location *bl, **blp_tmp;
3975
3976 ALL_BP_LOCATIONS (bl, blp_tmp)
3977 {
3978 if (bl->loc_type != bp_loc_software_breakpoint
3979 && bl->loc_type != bp_loc_hardware_breakpoint)
3980 continue;
3981
3982 if ((breakpoint_enabled (bl->owner)
3983 || bl->permanent)
3984 && breakpoint_location_address_range_overlap (bl, aspace,
3985 addr, len))
3986 {
3987 if (overlay_debugging
3988 && section_is_overlay (bl->section)
3989 && !section_is_mapped (bl->section))
3990 {
3991 /* Unmapped overlay -- can't be a match. */
3992 continue;
3993 }
3994
3995 return 1;
3996 }
3997 }
3998
3999 return 0;
4000 }
4001
4002 /* Return true if there's a moribund breakpoint at PC. */
4003
4004 int
4005 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4006 {
4007 for (bp_location *loc : moribund_locations)
4008 if (breakpoint_location_address_match (loc, aspace, pc))
4009 return 1;
4010
4011 return 0;
4012 }
4013
4014 /* Returns non-zero iff BL is inserted at PC, in address space
4015 ASPACE. */
4016
4017 static int
4018 bp_location_inserted_here_p (struct bp_location *bl,
4019 const address_space *aspace, CORE_ADDR pc)
4020 {
4021 if (bl->inserted
4022 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4023 aspace, pc))
4024 {
4025 if (overlay_debugging
4026 && section_is_overlay (bl->section)
4027 && !section_is_mapped (bl->section))
4028 return 0; /* unmapped overlay -- can't be a match */
4029 else
4030 return 1;
4031 }
4032 return 0;
4033 }
4034
4035 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4036
4037 int
4038 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4039 {
4040 struct bp_location **blp, **blp_tmp = NULL;
4041
4042 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4043 {
4044 struct bp_location *bl = *blp;
4045
4046 if (bl->loc_type != bp_loc_software_breakpoint
4047 && bl->loc_type != bp_loc_hardware_breakpoint)
4048 continue;
4049
4050 if (bp_location_inserted_here_p (bl, aspace, pc))
4051 return 1;
4052 }
4053 return 0;
4054 }
4055
4056 /* This function returns non-zero iff there is a software breakpoint
4057 inserted at PC. */
4058
4059 int
4060 software_breakpoint_inserted_here_p (const address_space *aspace,
4061 CORE_ADDR pc)
4062 {
4063 struct bp_location **blp, **blp_tmp = NULL;
4064
4065 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4066 {
4067 struct bp_location *bl = *blp;
4068
4069 if (bl->loc_type != bp_loc_software_breakpoint)
4070 continue;
4071
4072 if (bp_location_inserted_here_p (bl, aspace, pc))
4073 return 1;
4074 }
4075
4076 return 0;
4077 }
4078
4079 /* See breakpoint.h. */
4080
4081 int
4082 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4083 CORE_ADDR pc)
4084 {
4085 struct bp_location **blp, **blp_tmp = NULL;
4086
4087 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4088 {
4089 struct bp_location *bl = *blp;
4090
4091 if (bl->loc_type != bp_loc_hardware_breakpoint)
4092 continue;
4093
4094 if (bp_location_inserted_here_p (bl, aspace, pc))
4095 return 1;
4096 }
4097
4098 return 0;
4099 }
4100
4101 int
4102 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4103 CORE_ADDR addr, ULONGEST len)
4104 {
4105 struct breakpoint *bpt;
4106
4107 ALL_BREAKPOINTS (bpt)
4108 {
4109 struct bp_location *loc;
4110
4111 if (bpt->type != bp_hardware_watchpoint
4112 && bpt->type != bp_access_watchpoint)
4113 continue;
4114
4115 if (!breakpoint_enabled (bpt))
4116 continue;
4117
4118 for (loc = bpt->loc; loc; loc = loc->next)
4119 if (loc->pspace->aspace == aspace && loc->inserted)
4120 {
4121 CORE_ADDR l, h;
4122
4123 /* Check for intersection. */
4124 l = std::max<CORE_ADDR> (loc->address, addr);
4125 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4126 if (l < h)
4127 return 1;
4128 }
4129 }
4130 return 0;
4131 }
4132
4133 /* See breakpoint.h. */
4134
4135 bool
4136 is_catchpoint (struct breakpoint *b)
4137 {
4138 return (b->type == bp_catchpoint);
4139 }
4140
4141 /* Frees any storage that is part of a bpstat. Does not walk the
4142 'next' chain. */
4143
4144 bpstats::~bpstats ()
4145 {
4146 if (bp_location_at != NULL)
4147 decref_bp_location (&bp_location_at);
4148 }
4149
4150 /* Clear a bpstat so that it says we are not at any breakpoint.
4151 Also free any storage that is part of a bpstat. */
4152
4153 void
4154 bpstat_clear (bpstat *bsp)
4155 {
4156 bpstat p;
4157 bpstat q;
4158
4159 if (bsp == 0)
4160 return;
4161 p = *bsp;
4162 while (p != NULL)
4163 {
4164 q = p->next;
4165 delete p;
4166 p = q;
4167 }
4168 *bsp = NULL;
4169 }
4170
4171 bpstats::bpstats (const bpstats &other)
4172 : next (NULL),
4173 bp_location_at (other.bp_location_at),
4174 breakpoint_at (other.breakpoint_at),
4175 commands (other.commands),
4176 print (other.print),
4177 stop (other.stop),
4178 print_it (other.print_it)
4179 {
4180 if (other.old_val != NULL)
4181 old_val = release_value (value_copy (other.old_val.get ()));
4182 incref_bp_location (bp_location_at);
4183 }
4184
4185 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4186 is part of the bpstat is copied as well. */
4187
4188 bpstat
4189 bpstat_copy (bpstat bs)
4190 {
4191 bpstat p = NULL;
4192 bpstat tmp;
4193 bpstat retval = NULL;
4194
4195 if (bs == NULL)
4196 return bs;
4197
4198 for (; bs != NULL; bs = bs->next)
4199 {
4200 tmp = new bpstats (*bs);
4201
4202 if (p == NULL)
4203 /* This is the first thing in the chain. */
4204 retval = tmp;
4205 else
4206 p->next = tmp;
4207 p = tmp;
4208 }
4209 p->next = NULL;
4210 return retval;
4211 }
4212
4213 /* Find the bpstat associated with this breakpoint. */
4214
4215 bpstat
4216 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4217 {
4218 if (bsp == NULL)
4219 return NULL;
4220
4221 for (; bsp != NULL; bsp = bsp->next)
4222 {
4223 if (bsp->breakpoint_at == breakpoint)
4224 return bsp;
4225 }
4226 return NULL;
4227 }
4228
4229 /* See breakpoint.h. */
4230
4231 bool
4232 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4233 {
4234 for (; bsp != NULL; bsp = bsp->next)
4235 {
4236 if (bsp->breakpoint_at == NULL)
4237 {
4238 /* A moribund location can never explain a signal other than
4239 GDB_SIGNAL_TRAP. */
4240 if (sig == GDB_SIGNAL_TRAP)
4241 return true;
4242 }
4243 else
4244 {
4245 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4246 sig))
4247 return true;
4248 }
4249 }
4250
4251 return false;
4252 }
4253
4254 /* Put in *NUM the breakpoint number of the first breakpoint we are
4255 stopped at. *BSP upon return is a bpstat which points to the
4256 remaining breakpoints stopped at (but which is not guaranteed to be
4257 good for anything but further calls to bpstat_num).
4258
4259 Return 0 if passed a bpstat which does not indicate any breakpoints.
4260 Return -1 if stopped at a breakpoint that has been deleted since
4261 we set it.
4262 Return 1 otherwise. */
4263
4264 int
4265 bpstat_num (bpstat *bsp, int *num)
4266 {
4267 struct breakpoint *b;
4268
4269 if ((*bsp) == NULL)
4270 return 0; /* No more breakpoint values */
4271
4272 /* We assume we'll never have several bpstats that correspond to a
4273 single breakpoint -- otherwise, this function might return the
4274 same number more than once and this will look ugly. */
4275 b = (*bsp)->breakpoint_at;
4276 *bsp = (*bsp)->next;
4277 if (b == NULL)
4278 return -1; /* breakpoint that's been deleted since */
4279
4280 *num = b->number; /* We have its number */
4281 return 1;
4282 }
4283
4284 /* See breakpoint.h. */
4285
4286 void
4287 bpstat_clear_actions (void)
4288 {
4289 bpstat bs;
4290
4291 if (inferior_ptid == null_ptid)
4292 return;
4293
4294 thread_info *tp = inferior_thread ();
4295 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4296 {
4297 bs->commands = NULL;
4298 bs->old_val.reset (nullptr);
4299 }
4300 }
4301
4302 /* Called when a command is about to proceed the inferior. */
4303
4304 static void
4305 breakpoint_about_to_proceed (void)
4306 {
4307 if (inferior_ptid != null_ptid)
4308 {
4309 struct thread_info *tp = inferior_thread ();
4310
4311 /* Allow inferior function calls in breakpoint commands to not
4312 interrupt the command list. When the call finishes
4313 successfully, the inferior will be standing at the same
4314 breakpoint as if nothing happened. */
4315 if (tp->control.in_infcall)
4316 return;
4317 }
4318
4319 breakpoint_proceeded = 1;
4320 }
4321
4322 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4323 or its equivalent. */
4324
4325 static int
4326 command_line_is_silent (struct command_line *cmd)
4327 {
4328 return cmd && (strcmp ("silent", cmd->line) == 0);
4329 }
4330
4331 /* Execute all the commands associated with all the breakpoints at
4332 this location. Any of these commands could cause the process to
4333 proceed beyond this point, etc. We look out for such changes by
4334 checking the global "breakpoint_proceeded" after each command.
4335
4336 Returns true if a breakpoint command resumed the inferior. In that
4337 case, it is the caller's responsibility to recall it again with the
4338 bpstat of the current thread. */
4339
4340 static int
4341 bpstat_do_actions_1 (bpstat *bsp)
4342 {
4343 bpstat bs;
4344 int again = 0;
4345
4346 /* Avoid endless recursion if a `source' command is contained
4347 in bs->commands. */
4348 if (executing_breakpoint_commands)
4349 return 0;
4350
4351 scoped_restore save_executing
4352 = make_scoped_restore (&executing_breakpoint_commands, 1);
4353
4354 scoped_restore preventer = prevent_dont_repeat ();
4355
4356 /* This pointer will iterate over the list of bpstat's. */
4357 bs = *bsp;
4358
4359 breakpoint_proceeded = 0;
4360 for (; bs != NULL; bs = bs->next)
4361 {
4362 struct command_line *cmd = NULL;
4363
4364 /* Take ownership of the BSP's command tree, if it has one.
4365
4366 The command tree could legitimately contain commands like
4367 'step' and 'next', which call clear_proceed_status, which
4368 frees stop_bpstat's command tree. To make sure this doesn't
4369 free the tree we're executing out from under us, we need to
4370 take ownership of the tree ourselves. Since a given bpstat's
4371 commands are only executed once, we don't need to copy it; we
4372 can clear the pointer in the bpstat, and make sure we free
4373 the tree when we're done. */
4374 counted_command_line ccmd = bs->commands;
4375 bs->commands = NULL;
4376 if (ccmd != NULL)
4377 cmd = ccmd.get ();
4378 if (command_line_is_silent (cmd))
4379 {
4380 /* The action has been already done by bpstat_stop_status. */
4381 cmd = cmd->next;
4382 }
4383
4384 while (cmd != NULL)
4385 {
4386 execute_control_command (cmd);
4387
4388 if (breakpoint_proceeded)
4389 break;
4390 else
4391 cmd = cmd->next;
4392 }
4393
4394 if (breakpoint_proceeded)
4395 {
4396 if (current_ui->async)
4397 /* If we are in async mode, then the target might be still
4398 running, not stopped at any breakpoint, so nothing for
4399 us to do here -- just return to the event loop. */
4400 ;
4401 else
4402 /* In sync mode, when execute_control_command returns
4403 we're already standing on the next breakpoint.
4404 Breakpoint commands for that stop were not run, since
4405 execute_command does not run breakpoint commands --
4406 only command_line_handler does, but that one is not
4407 involved in execution of breakpoint commands. So, we
4408 can now execute breakpoint commands. It should be
4409 noted that making execute_command do bpstat actions is
4410 not an option -- in this case we'll have recursive
4411 invocation of bpstat for each breakpoint with a
4412 command, and can easily blow up GDB stack. Instead, we
4413 return true, which will trigger the caller to recall us
4414 with the new stop_bpstat. */
4415 again = 1;
4416 break;
4417 }
4418 }
4419 return again;
4420 }
4421
4422 /* Helper for bpstat_do_actions. Get the current thread, if there's
4423 one, is alive and has execution. Return NULL otherwise. */
4424
4425 static thread_info *
4426 get_bpstat_thread ()
4427 {
4428 if (inferior_ptid == null_ptid || !target_has_execution)
4429 return NULL;
4430
4431 thread_info *tp = inferior_thread ();
4432 if (tp->state == THREAD_EXITED || tp->executing)
4433 return NULL;
4434 return tp;
4435 }
4436
4437 void
4438 bpstat_do_actions (void)
4439 {
4440 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4441 thread_info *tp;
4442
4443 /* Do any commands attached to breakpoint we are stopped at. */
4444 while ((tp = get_bpstat_thread ()) != NULL)
4445 {
4446 /* Since in sync mode, bpstat_do_actions may resume the
4447 inferior, and only return when it is stopped at the next
4448 breakpoint, we keep doing breakpoint actions until it returns
4449 false to indicate the inferior was not resumed. */
4450 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4451 break;
4452 }
4453
4454 cleanup_if_error.release ();
4455 }
4456
4457 /* Print out the (old or new) value associated with a watchpoint. */
4458
4459 static void
4460 watchpoint_value_print (struct value *val, struct ui_file *stream)
4461 {
4462 if (val == NULL)
4463 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4464 else
4465 {
4466 struct value_print_options opts;
4467 get_user_print_options (&opts);
4468 value_print (val, stream, &opts);
4469 }
4470 }
4471
4472 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4473 debugging multiple threads. */
4474
4475 void
4476 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4477 {
4478 if (uiout->is_mi_like_p ())
4479 return;
4480
4481 uiout->text ("\n");
4482
4483 if (show_thread_that_caused_stop ())
4484 {
4485 const char *name;
4486 struct thread_info *thr = inferior_thread ();
4487
4488 uiout->text ("Thread ");
4489 uiout->field_string ("thread-id", print_thread_id (thr));
4490
4491 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4492 if (name != NULL)
4493 {
4494 uiout->text (" \"");
4495 uiout->field_string ("name", name);
4496 uiout->text ("\"");
4497 }
4498
4499 uiout->text (" hit ");
4500 }
4501 }
4502
4503 /* Generic routine for printing messages indicating why we
4504 stopped. The behavior of this function depends on the value
4505 'print_it' in the bpstat structure. Under some circumstances we
4506 may decide not to print anything here and delegate the task to
4507 normal_stop(). */
4508
4509 static enum print_stop_action
4510 print_bp_stop_message (bpstat bs)
4511 {
4512 switch (bs->print_it)
4513 {
4514 case print_it_noop:
4515 /* Nothing should be printed for this bpstat entry. */
4516 return PRINT_UNKNOWN;
4517 break;
4518
4519 case print_it_done:
4520 /* We still want to print the frame, but we already printed the
4521 relevant messages. */
4522 return PRINT_SRC_AND_LOC;
4523 break;
4524
4525 case print_it_normal:
4526 {
4527 struct breakpoint *b = bs->breakpoint_at;
4528
4529 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4530 which has since been deleted. */
4531 if (b == NULL)
4532 return PRINT_UNKNOWN;
4533
4534 /* Normal case. Call the breakpoint's print_it method. */
4535 return b->ops->print_it (bs);
4536 }
4537 break;
4538
4539 default:
4540 internal_error (__FILE__, __LINE__,
4541 _("print_bp_stop_message: unrecognized enum value"));
4542 break;
4543 }
4544 }
4545
4546 /* A helper function that prints a shared library stopped event. */
4547
4548 static void
4549 print_solib_event (int is_catchpoint)
4550 {
4551 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4552 bool any_added = !current_program_space->added_solibs.empty ();
4553
4554 if (!is_catchpoint)
4555 {
4556 if (any_added || any_deleted)
4557 current_uiout->text (_("Stopped due to shared library event:\n"));
4558 else
4559 current_uiout->text (_("Stopped due to shared library event (no "
4560 "libraries added or removed)\n"));
4561 }
4562
4563 if (current_uiout->is_mi_like_p ())
4564 current_uiout->field_string ("reason",
4565 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4566
4567 if (any_deleted)
4568 {
4569 current_uiout->text (_(" Inferior unloaded "));
4570 ui_out_emit_list list_emitter (current_uiout, "removed");
4571 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4572 {
4573 const std::string &name = current_program_space->deleted_solibs[ix];
4574
4575 if (ix > 0)
4576 current_uiout->text (" ");
4577 current_uiout->field_string ("library", name);
4578 current_uiout->text ("\n");
4579 }
4580 }
4581
4582 if (any_added)
4583 {
4584 current_uiout->text (_(" Inferior loaded "));
4585 ui_out_emit_list list_emitter (current_uiout, "added");
4586 bool first = true;
4587 for (so_list *iter : current_program_space->added_solibs)
4588 {
4589 if (!first)
4590 current_uiout->text (" ");
4591 first = false;
4592 current_uiout->field_string ("library", iter->so_name);
4593 current_uiout->text ("\n");
4594 }
4595 }
4596 }
4597
4598 /* Print a message indicating what happened. This is called from
4599 normal_stop(). The input to this routine is the head of the bpstat
4600 list - a list of the eventpoints that caused this stop. KIND is
4601 the target_waitkind for the stopping event. This
4602 routine calls the generic print routine for printing a message
4603 about reasons for stopping. This will print (for example) the
4604 "Breakpoint n," part of the output. The return value of this
4605 routine is one of:
4606
4607 PRINT_UNKNOWN: Means we printed nothing.
4608 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4609 code to print the location. An example is
4610 "Breakpoint 1, " which should be followed by
4611 the location.
4612 PRINT_SRC_ONLY: Means we printed something, but there is no need
4613 to also print the location part of the message.
4614 An example is the catch/throw messages, which
4615 don't require a location appended to the end.
4616 PRINT_NOTHING: We have done some printing and we don't need any
4617 further info to be printed. */
4618
4619 enum print_stop_action
4620 bpstat_print (bpstat bs, int kind)
4621 {
4622 enum print_stop_action val;
4623
4624 /* Maybe another breakpoint in the chain caused us to stop.
4625 (Currently all watchpoints go on the bpstat whether hit or not.
4626 That probably could (should) be changed, provided care is taken
4627 with respect to bpstat_explains_signal). */
4628 for (; bs; bs = bs->next)
4629 {
4630 val = print_bp_stop_message (bs);
4631 if (val == PRINT_SRC_ONLY
4632 || val == PRINT_SRC_AND_LOC
4633 || val == PRINT_NOTHING)
4634 return val;
4635 }
4636
4637 /* If we had hit a shared library event breakpoint,
4638 print_bp_stop_message would print out this message. If we hit an
4639 OS-level shared library event, do the same thing. */
4640 if (kind == TARGET_WAITKIND_LOADED)
4641 {
4642 print_solib_event (0);
4643 return PRINT_NOTHING;
4644 }
4645
4646 /* We reached the end of the chain, or we got a null BS to start
4647 with and nothing was printed. */
4648 return PRINT_UNKNOWN;
4649 }
4650
4651 /* Evaluate the boolean expression EXP and return the result. */
4652
4653 static bool
4654 breakpoint_cond_eval (expression *exp)
4655 {
4656 struct value *mark = value_mark ();
4657 bool res = value_true (evaluate_expression (exp));
4658
4659 value_free_to_mark (mark);
4660 return res;
4661 }
4662
4663 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4664
4665 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4666 : next (NULL),
4667 bp_location_at (bl),
4668 breakpoint_at (bl->owner),
4669 commands (NULL),
4670 print (0),
4671 stop (0),
4672 print_it (print_it_normal)
4673 {
4674 incref_bp_location (bl);
4675 **bs_link_pointer = this;
4676 *bs_link_pointer = &next;
4677 }
4678
4679 bpstats::bpstats ()
4680 : next (NULL),
4681 bp_location_at (NULL),
4682 breakpoint_at (NULL),
4683 commands (NULL),
4684 print (0),
4685 stop (0),
4686 print_it (print_it_normal)
4687 {
4688 }
4689 \f
4690 /* The target has stopped with waitstatus WS. Check if any hardware
4691 watchpoints have triggered, according to the target. */
4692
4693 int
4694 watchpoints_triggered (struct target_waitstatus *ws)
4695 {
4696 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4697 CORE_ADDR addr;
4698 struct breakpoint *b;
4699
4700 if (!stopped_by_watchpoint)
4701 {
4702 /* We were not stopped by a watchpoint. Mark all watchpoints
4703 as not triggered. */
4704 ALL_BREAKPOINTS (b)
4705 if (is_hardware_watchpoint (b))
4706 {
4707 struct watchpoint *w = (struct watchpoint *) b;
4708
4709 w->watchpoint_triggered = watch_triggered_no;
4710 }
4711
4712 return 0;
4713 }
4714
4715 if (!target_stopped_data_address (current_top_target (), &addr))
4716 {
4717 /* We were stopped by a watchpoint, but we don't know where.
4718 Mark all watchpoints as unknown. */
4719 ALL_BREAKPOINTS (b)
4720 if (is_hardware_watchpoint (b))
4721 {
4722 struct watchpoint *w = (struct watchpoint *) b;
4723
4724 w->watchpoint_triggered = watch_triggered_unknown;
4725 }
4726
4727 return 1;
4728 }
4729
4730 /* The target could report the data address. Mark watchpoints
4731 affected by this data address as triggered, and all others as not
4732 triggered. */
4733
4734 ALL_BREAKPOINTS (b)
4735 if (is_hardware_watchpoint (b))
4736 {
4737 struct watchpoint *w = (struct watchpoint *) b;
4738 struct bp_location *loc;
4739
4740 w->watchpoint_triggered = watch_triggered_no;
4741 for (loc = b->loc; loc; loc = loc->next)
4742 {
4743 if (is_masked_watchpoint (b))
4744 {
4745 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4746 CORE_ADDR start = loc->address & w->hw_wp_mask;
4747
4748 if (newaddr == start)
4749 {
4750 w->watchpoint_triggered = watch_triggered_yes;
4751 break;
4752 }
4753 }
4754 /* Exact match not required. Within range is sufficient. */
4755 else if (target_watchpoint_addr_within_range (current_top_target (),
4756 addr, loc->address,
4757 loc->length))
4758 {
4759 w->watchpoint_triggered = watch_triggered_yes;
4760 break;
4761 }
4762 }
4763 }
4764
4765 return 1;
4766 }
4767
4768 /* Possible return values for watchpoint_check. */
4769 enum wp_check_result
4770 {
4771 /* The watchpoint has been deleted. */
4772 WP_DELETED = 1,
4773
4774 /* The value has changed. */
4775 WP_VALUE_CHANGED = 2,
4776
4777 /* The value has not changed. */
4778 WP_VALUE_NOT_CHANGED = 3,
4779
4780 /* Ignore this watchpoint, no matter if the value changed or not. */
4781 WP_IGNORE = 4,
4782 };
4783
4784 #define BP_TEMPFLAG 1
4785 #define BP_HARDWAREFLAG 2
4786
4787 /* Evaluate watchpoint condition expression and check if its value
4788 changed. */
4789
4790 static wp_check_result
4791 watchpoint_check (bpstat bs)
4792 {
4793 struct watchpoint *b;
4794 struct frame_info *fr;
4795 int within_current_scope;
4796
4797 /* BS is built from an existing struct breakpoint. */
4798 gdb_assert (bs->breakpoint_at != NULL);
4799 b = (struct watchpoint *) bs->breakpoint_at;
4800
4801 /* If this is a local watchpoint, we only want to check if the
4802 watchpoint frame is in scope if the current thread is the thread
4803 that was used to create the watchpoint. */
4804 if (!watchpoint_in_thread_scope (b))
4805 return WP_IGNORE;
4806
4807 if (b->exp_valid_block == NULL)
4808 within_current_scope = 1;
4809 else
4810 {
4811 struct frame_info *frame = get_current_frame ();
4812 struct gdbarch *frame_arch = get_frame_arch (frame);
4813 CORE_ADDR frame_pc = get_frame_pc (frame);
4814
4815 /* stack_frame_destroyed_p() returns a non-zero value if we're
4816 still in the function but the stack frame has already been
4817 invalidated. Since we can't rely on the values of local
4818 variables after the stack has been destroyed, we are treating
4819 the watchpoint in that state as `not changed' without further
4820 checking. Don't mark watchpoints as changed if the current
4821 frame is in an epilogue - even if they are in some other
4822 frame, our view of the stack is likely to be wrong and
4823 frame_find_by_id could error out. */
4824 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4825 return WP_IGNORE;
4826
4827 fr = frame_find_by_id (b->watchpoint_frame);
4828 within_current_scope = (fr != NULL);
4829
4830 /* If we've gotten confused in the unwinder, we might have
4831 returned a frame that can't describe this variable. */
4832 if (within_current_scope)
4833 {
4834 struct symbol *function;
4835
4836 function = get_frame_function (fr);
4837 if (function == NULL
4838 || !contained_in (b->exp_valid_block,
4839 SYMBOL_BLOCK_VALUE (function)))
4840 within_current_scope = 0;
4841 }
4842
4843 if (within_current_scope)
4844 /* If we end up stopping, the current frame will get selected
4845 in normal_stop. So this call to select_frame won't affect
4846 the user. */
4847 select_frame (fr);
4848 }
4849
4850 if (within_current_scope)
4851 {
4852 /* We use value_{,free_to_}mark because it could be a *long*
4853 time before we return to the command level and call
4854 free_all_values. We can't call free_all_values because we
4855 might be in the middle of evaluating a function call. */
4856
4857 int pc = 0;
4858 struct value *mark;
4859 struct value *new_val;
4860
4861 if (is_masked_watchpoint (b))
4862 /* Since we don't know the exact trigger address (from
4863 stopped_data_address), just tell the user we've triggered
4864 a mask watchpoint. */
4865 return WP_VALUE_CHANGED;
4866
4867 mark = value_mark ();
4868 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4869
4870 if (b->val_bitsize != 0)
4871 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4872
4873 /* We use value_equal_contents instead of value_equal because
4874 the latter coerces an array to a pointer, thus comparing just
4875 the address of the array instead of its contents. This is
4876 not what we want. */
4877 if ((b->val != NULL) != (new_val != NULL)
4878 || (b->val != NULL && !value_equal_contents (b->val.get (),
4879 new_val)))
4880 {
4881 bs->old_val = b->val;
4882 b->val = release_value (new_val);
4883 b->val_valid = true;
4884 if (new_val != NULL)
4885 value_free_to_mark (mark);
4886 return WP_VALUE_CHANGED;
4887 }
4888 else
4889 {
4890 /* Nothing changed. */
4891 value_free_to_mark (mark);
4892 return WP_VALUE_NOT_CHANGED;
4893 }
4894 }
4895 else
4896 {
4897 /* This seems like the only logical thing to do because
4898 if we temporarily ignored the watchpoint, then when
4899 we reenter the block in which it is valid it contains
4900 garbage (in the case of a function, it may have two
4901 garbage values, one before and one after the prologue).
4902 So we can't even detect the first assignment to it and
4903 watch after that (since the garbage may or may not equal
4904 the first value assigned). */
4905 /* We print all the stop information in
4906 breakpoint_ops->print_it, but in this case, by the time we
4907 call breakpoint_ops->print_it this bp will be deleted
4908 already. So we have no choice but print the information
4909 here. */
4910
4911 SWITCH_THRU_ALL_UIS ()
4912 {
4913 struct ui_out *uiout = current_uiout;
4914
4915 if (uiout->is_mi_like_p ())
4916 uiout->field_string
4917 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4918 uiout->message ("\nWatchpoint %pF deleted because the program has "
4919 "left the block in\n"
4920 "which its expression is valid.\n",
4921 signed_field ("wpnum", b->number));
4922 }
4923
4924 /* Make sure the watchpoint's commands aren't executed. */
4925 b->commands = NULL;
4926 watchpoint_del_at_next_stop (b);
4927
4928 return WP_DELETED;
4929 }
4930 }
4931
4932 /* Return true if it looks like target has stopped due to hitting
4933 breakpoint location BL. This function does not check if we should
4934 stop, only if BL explains the stop. */
4935
4936 static int
4937 bpstat_check_location (const struct bp_location *bl,
4938 const address_space *aspace, CORE_ADDR bp_addr,
4939 const struct target_waitstatus *ws)
4940 {
4941 struct breakpoint *b = bl->owner;
4942
4943 /* BL is from an existing breakpoint. */
4944 gdb_assert (b != NULL);
4945
4946 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4947 }
4948
4949 /* Determine if the watched values have actually changed, and we
4950 should stop. If not, set BS->stop to 0. */
4951
4952 static void
4953 bpstat_check_watchpoint (bpstat bs)
4954 {
4955 const struct bp_location *bl;
4956 struct watchpoint *b;
4957
4958 /* BS is built for existing struct breakpoint. */
4959 bl = bs->bp_location_at;
4960 gdb_assert (bl != NULL);
4961 b = (struct watchpoint *) bs->breakpoint_at;
4962 gdb_assert (b != NULL);
4963
4964 {
4965 int must_check_value = 0;
4966
4967 if (b->type == bp_watchpoint)
4968 /* For a software watchpoint, we must always check the
4969 watched value. */
4970 must_check_value = 1;
4971 else if (b->watchpoint_triggered == watch_triggered_yes)
4972 /* We have a hardware watchpoint (read, write, or access)
4973 and the target earlier reported an address watched by
4974 this watchpoint. */
4975 must_check_value = 1;
4976 else if (b->watchpoint_triggered == watch_triggered_unknown
4977 && b->type == bp_hardware_watchpoint)
4978 /* We were stopped by a hardware watchpoint, but the target could
4979 not report the data address. We must check the watchpoint's
4980 value. Access and read watchpoints are out of luck; without
4981 a data address, we can't figure it out. */
4982 must_check_value = 1;
4983
4984 if (must_check_value)
4985 {
4986 wp_check_result e;
4987
4988 try
4989 {
4990 e = watchpoint_check (bs);
4991 }
4992 catch (const gdb_exception &ex)
4993 {
4994 exception_fprintf (gdb_stderr, ex,
4995 "Error evaluating expression "
4996 "for watchpoint %d\n",
4997 b->number);
4998
4999 SWITCH_THRU_ALL_UIS ()
5000 {
5001 printf_filtered (_("Watchpoint %d deleted.\n"),
5002 b->number);
5003 }
5004 watchpoint_del_at_next_stop (b);
5005 e = WP_DELETED;
5006 }
5007
5008 switch (e)
5009 {
5010 case WP_DELETED:
5011 /* We've already printed what needs to be printed. */
5012 bs->print_it = print_it_done;
5013 /* Stop. */
5014 break;
5015 case WP_IGNORE:
5016 bs->print_it = print_it_noop;
5017 bs->stop = 0;
5018 break;
5019 case WP_VALUE_CHANGED:
5020 if (b->type == bp_read_watchpoint)
5021 {
5022 /* There are two cases to consider here:
5023
5024 1. We're watching the triggered memory for reads.
5025 In that case, trust the target, and always report
5026 the watchpoint hit to the user. Even though
5027 reads don't cause value changes, the value may
5028 have changed since the last time it was read, and
5029 since we're not trapping writes, we will not see
5030 those, and as such we should ignore our notion of
5031 old value.
5032
5033 2. We're watching the triggered memory for both
5034 reads and writes. There are two ways this may
5035 happen:
5036
5037 2.1. This is a target that can't break on data
5038 reads only, but can break on accesses (reads or
5039 writes), such as e.g., x86. We detect this case
5040 at the time we try to insert read watchpoints.
5041
5042 2.2. Otherwise, the target supports read
5043 watchpoints, but, the user set an access or write
5044 watchpoint watching the same memory as this read
5045 watchpoint.
5046
5047 If we're watching memory writes as well as reads,
5048 ignore watchpoint hits when we find that the
5049 value hasn't changed, as reads don't cause
5050 changes. This still gives false positives when
5051 the program writes the same value to memory as
5052 what there was already in memory (we will confuse
5053 it for a read), but it's much better than
5054 nothing. */
5055
5056 int other_write_watchpoint = 0;
5057
5058 if (bl->watchpoint_type == hw_read)
5059 {
5060 struct breakpoint *other_b;
5061
5062 ALL_BREAKPOINTS (other_b)
5063 if (other_b->type == bp_hardware_watchpoint
5064 || other_b->type == bp_access_watchpoint)
5065 {
5066 struct watchpoint *other_w =
5067 (struct watchpoint *) other_b;
5068
5069 if (other_w->watchpoint_triggered
5070 == watch_triggered_yes)
5071 {
5072 other_write_watchpoint = 1;
5073 break;
5074 }
5075 }
5076 }
5077
5078 if (other_write_watchpoint
5079 || bl->watchpoint_type == hw_access)
5080 {
5081 /* We're watching the same memory for writes,
5082 and the value changed since the last time we
5083 updated it, so this trap must be for a write.
5084 Ignore it. */
5085 bs->print_it = print_it_noop;
5086 bs->stop = 0;
5087 }
5088 }
5089 break;
5090 case WP_VALUE_NOT_CHANGED:
5091 if (b->type == bp_hardware_watchpoint
5092 || b->type == bp_watchpoint)
5093 {
5094 /* Don't stop: write watchpoints shouldn't fire if
5095 the value hasn't changed. */
5096 bs->print_it = print_it_noop;
5097 bs->stop = 0;
5098 }
5099 /* Stop. */
5100 break;
5101 default:
5102 /* Can't happen. */
5103 break;
5104 }
5105 }
5106 else /* must_check_value == 0 */
5107 {
5108 /* This is a case where some watchpoint(s) triggered, but
5109 not at the address of this watchpoint, or else no
5110 watchpoint triggered after all. So don't print
5111 anything for this watchpoint. */
5112 bs->print_it = print_it_noop;
5113 bs->stop = 0;
5114 }
5115 }
5116 }
5117
5118 /* For breakpoints that are currently marked as telling gdb to stop,
5119 check conditions (condition proper, frame, thread and ignore count)
5120 of breakpoint referred to by BS. If we should not stop for this
5121 breakpoint, set BS->stop to 0. */
5122
5123 static void
5124 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5125 {
5126 const struct bp_location *bl;
5127 struct breakpoint *b;
5128 /* Assume stop. */
5129 bool condition_result = true;
5130 struct expression *cond;
5131
5132 gdb_assert (bs->stop);
5133
5134 /* BS is built for existing struct breakpoint. */
5135 bl = bs->bp_location_at;
5136 gdb_assert (bl != NULL);
5137 b = bs->breakpoint_at;
5138 gdb_assert (b != NULL);
5139
5140 /* Even if the target evaluated the condition on its end and notified GDB, we
5141 need to do so again since GDB does not know if we stopped due to a
5142 breakpoint or a single step breakpoint. */
5143
5144 if (frame_id_p (b->frame_id)
5145 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5146 {
5147 bs->stop = 0;
5148 return;
5149 }
5150
5151 /* If this is a thread/task-specific breakpoint, don't waste cpu
5152 evaluating the condition if this isn't the specified
5153 thread/task. */
5154 if ((b->thread != -1 && b->thread != thread->global_num)
5155 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5156 {
5157 bs->stop = 0;
5158 return;
5159 }
5160
5161 /* Evaluate extension language breakpoints that have a "stop" method
5162 implemented. */
5163 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5164
5165 if (is_watchpoint (b))
5166 {
5167 struct watchpoint *w = (struct watchpoint *) b;
5168
5169 cond = w->cond_exp.get ();
5170 }
5171 else
5172 cond = bl->cond.get ();
5173
5174 if (cond && b->disposition != disp_del_at_next_stop)
5175 {
5176 int within_current_scope = 1;
5177 struct watchpoint * w;
5178
5179 /* We use value_mark and value_free_to_mark because it could
5180 be a long time before we return to the command level and
5181 call free_all_values. We can't call free_all_values
5182 because we might be in the middle of evaluating a
5183 function call. */
5184 struct value *mark = value_mark ();
5185
5186 if (is_watchpoint (b))
5187 w = (struct watchpoint *) b;
5188 else
5189 w = NULL;
5190
5191 /* Need to select the frame, with all that implies so that
5192 the conditions will have the right context. Because we
5193 use the frame, we will not see an inlined function's
5194 variables when we arrive at a breakpoint at the start
5195 of the inlined function; the current frame will be the
5196 call site. */
5197 if (w == NULL || w->cond_exp_valid_block == NULL)
5198 select_frame (get_current_frame ());
5199 else
5200 {
5201 struct frame_info *frame;
5202
5203 /* For local watchpoint expressions, which particular
5204 instance of a local is being watched matters, so we
5205 keep track of the frame to evaluate the expression
5206 in. To evaluate the condition however, it doesn't
5207 really matter which instantiation of the function
5208 where the condition makes sense triggers the
5209 watchpoint. This allows an expression like "watch
5210 global if q > 10" set in `func', catch writes to
5211 global on all threads that call `func', or catch
5212 writes on all recursive calls of `func' by a single
5213 thread. We simply always evaluate the condition in
5214 the innermost frame that's executing where it makes
5215 sense to evaluate the condition. It seems
5216 intuitive. */
5217 frame = block_innermost_frame (w->cond_exp_valid_block);
5218 if (frame != NULL)
5219 select_frame (frame);
5220 else
5221 within_current_scope = 0;
5222 }
5223 if (within_current_scope)
5224 {
5225 try
5226 {
5227 condition_result = breakpoint_cond_eval (cond);
5228 }
5229 catch (const gdb_exception &ex)
5230 {
5231 exception_fprintf (gdb_stderr, ex,
5232 "Error in testing breakpoint condition:\n");
5233 }
5234 }
5235 else
5236 {
5237 warning (_("Watchpoint condition cannot be tested "
5238 "in the current scope"));
5239 /* If we failed to set the right context for this
5240 watchpoint, unconditionally report it. */
5241 }
5242 /* FIXME-someday, should give breakpoint #. */
5243 value_free_to_mark (mark);
5244 }
5245
5246 if (cond && !condition_result)
5247 {
5248 bs->stop = 0;
5249 }
5250 else if (b->ignore_count > 0)
5251 {
5252 b->ignore_count--;
5253 bs->stop = 0;
5254 /* Increase the hit count even though we don't stop. */
5255 ++(b->hit_count);
5256 gdb::observers::breakpoint_modified.notify (b);
5257 }
5258 }
5259
5260 /* Returns true if we need to track moribund locations of LOC's type
5261 on the current target. */
5262
5263 static int
5264 need_moribund_for_location_type (struct bp_location *loc)
5265 {
5266 return ((loc->loc_type == bp_loc_software_breakpoint
5267 && !target_supports_stopped_by_sw_breakpoint ())
5268 || (loc->loc_type == bp_loc_hardware_breakpoint
5269 && !target_supports_stopped_by_hw_breakpoint ()));
5270 }
5271
5272 /* See breakpoint.h. */
5273
5274 bpstat
5275 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5276 const struct target_waitstatus *ws)
5277 {
5278 struct breakpoint *b;
5279 bpstat bs_head = NULL, *bs_link = &bs_head;
5280
5281 ALL_BREAKPOINTS (b)
5282 {
5283 if (!breakpoint_enabled (b))
5284 continue;
5285
5286 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5287 {
5288 /* For hardware watchpoints, we look only at the first
5289 location. The watchpoint_check function will work on the
5290 entire expression, not the individual locations. For
5291 read watchpoints, the watchpoints_triggered function has
5292 checked all locations already. */
5293 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5294 break;
5295
5296 if (!bl->enabled || bl->shlib_disabled)
5297 continue;
5298
5299 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5300 continue;
5301
5302 /* Come here if it's a watchpoint, or if the break address
5303 matches. */
5304
5305 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5306 explain stop. */
5307
5308 /* Assume we stop. Should we find a watchpoint that is not
5309 actually triggered, or if the condition of the breakpoint
5310 evaluates as false, we'll reset 'stop' to 0. */
5311 bs->stop = 1;
5312 bs->print = 1;
5313
5314 /* If this is a scope breakpoint, mark the associated
5315 watchpoint as triggered so that we will handle the
5316 out-of-scope event. We'll get to the watchpoint next
5317 iteration. */
5318 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5319 {
5320 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5321
5322 w->watchpoint_triggered = watch_triggered_yes;
5323 }
5324 }
5325 }
5326
5327 /* Check if a moribund breakpoint explains the stop. */
5328 if (!target_supports_stopped_by_sw_breakpoint ()
5329 || !target_supports_stopped_by_hw_breakpoint ())
5330 {
5331 for (bp_location *loc : moribund_locations)
5332 {
5333 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5334 && need_moribund_for_location_type (loc))
5335 {
5336 bpstat bs = new bpstats (loc, &bs_link);
5337 /* For hits of moribund locations, we should just proceed. */
5338 bs->stop = 0;
5339 bs->print = 0;
5340 bs->print_it = print_it_noop;
5341 }
5342 }
5343 }
5344
5345 return bs_head;
5346 }
5347
5348 /* See breakpoint.h. */
5349
5350 bpstat
5351 bpstat_stop_status (const address_space *aspace,
5352 CORE_ADDR bp_addr, thread_info *thread,
5353 const struct target_waitstatus *ws,
5354 bpstat stop_chain)
5355 {
5356 struct breakpoint *b = NULL;
5357 /* First item of allocated bpstat's. */
5358 bpstat bs_head = stop_chain;
5359 bpstat bs;
5360 int need_remove_insert;
5361 int removed_any;
5362
5363 /* First, build the bpstat chain with locations that explain a
5364 target stop, while being careful to not set the target running,
5365 as that may invalidate locations (in particular watchpoint
5366 locations are recreated). Resuming will happen here with
5367 breakpoint conditions or watchpoint expressions that include
5368 inferior function calls. */
5369 if (bs_head == NULL)
5370 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5371
5372 /* A bit of special processing for shlib breakpoints. We need to
5373 process solib loading here, so that the lists of loaded and
5374 unloaded libraries are correct before we handle "catch load" and
5375 "catch unload". */
5376 for (bs = bs_head; bs != NULL; bs = bs->next)
5377 {
5378 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5379 {
5380 handle_solib_event ();
5381 break;
5382 }
5383 }
5384
5385 /* Now go through the locations that caused the target to stop, and
5386 check whether we're interested in reporting this stop to higher
5387 layers, or whether we should resume the target transparently. */
5388
5389 removed_any = 0;
5390
5391 for (bs = bs_head; bs != NULL; bs = bs->next)
5392 {
5393 if (!bs->stop)
5394 continue;
5395
5396 b = bs->breakpoint_at;
5397 b->ops->check_status (bs);
5398 if (bs->stop)
5399 {
5400 bpstat_check_breakpoint_conditions (bs, thread);
5401
5402 if (bs->stop)
5403 {
5404 ++(b->hit_count);
5405 gdb::observers::breakpoint_modified.notify (b);
5406
5407 /* We will stop here. */
5408 if (b->disposition == disp_disable)
5409 {
5410 --(b->enable_count);
5411 if (b->enable_count <= 0)
5412 b->enable_state = bp_disabled;
5413 removed_any = 1;
5414 }
5415 if (b->silent)
5416 bs->print = 0;
5417 bs->commands = b->commands;
5418 if (command_line_is_silent (bs->commands
5419 ? bs->commands.get () : NULL))
5420 bs->print = 0;
5421
5422 b->ops->after_condition_true (bs);
5423 }
5424
5425 }
5426
5427 /* Print nothing for this entry if we don't stop or don't
5428 print. */
5429 if (!bs->stop || !bs->print)
5430 bs->print_it = print_it_noop;
5431 }
5432
5433 /* If we aren't stopping, the value of some hardware watchpoint may
5434 not have changed, but the intermediate memory locations we are
5435 watching may have. Don't bother if we're stopping; this will get
5436 done later. */
5437 need_remove_insert = 0;
5438 if (! bpstat_causes_stop (bs_head))
5439 for (bs = bs_head; bs != NULL; bs = bs->next)
5440 if (!bs->stop
5441 && bs->breakpoint_at
5442 && is_hardware_watchpoint (bs->breakpoint_at))
5443 {
5444 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5445
5446 update_watchpoint (w, 0 /* don't reparse. */);
5447 need_remove_insert = 1;
5448 }
5449
5450 if (need_remove_insert)
5451 update_global_location_list (UGLL_MAY_INSERT);
5452 else if (removed_any)
5453 update_global_location_list (UGLL_DONT_INSERT);
5454
5455 return bs_head;
5456 }
5457
5458 static void
5459 handle_jit_event (void)
5460 {
5461 struct frame_info *frame;
5462 struct gdbarch *gdbarch;
5463
5464 if (debug_infrun)
5465 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5466
5467 /* Switch terminal for any messages produced by
5468 breakpoint_re_set. */
5469 target_terminal::ours_for_output ();
5470
5471 frame = get_current_frame ();
5472 gdbarch = get_frame_arch (frame);
5473
5474 jit_event_handler (gdbarch);
5475
5476 target_terminal::inferior ();
5477 }
5478
5479 /* Prepare WHAT final decision for infrun. */
5480
5481 /* Decide what infrun needs to do with this bpstat. */
5482
5483 struct bpstat_what
5484 bpstat_what (bpstat bs_head)
5485 {
5486 struct bpstat_what retval;
5487 bpstat bs;
5488
5489 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5490 retval.call_dummy = STOP_NONE;
5491 retval.is_longjmp = false;
5492
5493 for (bs = bs_head; bs != NULL; bs = bs->next)
5494 {
5495 /* Extract this BS's action. After processing each BS, we check
5496 if its action overrides all we've seem so far. */
5497 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5498 enum bptype bptype;
5499
5500 if (bs->breakpoint_at == NULL)
5501 {
5502 /* I suspect this can happen if it was a momentary
5503 breakpoint which has since been deleted. */
5504 bptype = bp_none;
5505 }
5506 else
5507 bptype = bs->breakpoint_at->type;
5508
5509 switch (bptype)
5510 {
5511 case bp_none:
5512 break;
5513 case bp_breakpoint:
5514 case bp_hardware_breakpoint:
5515 case bp_single_step:
5516 case bp_until:
5517 case bp_finish:
5518 case bp_shlib_event:
5519 if (bs->stop)
5520 {
5521 if (bs->print)
5522 this_action = BPSTAT_WHAT_STOP_NOISY;
5523 else
5524 this_action = BPSTAT_WHAT_STOP_SILENT;
5525 }
5526 else
5527 this_action = BPSTAT_WHAT_SINGLE;
5528 break;
5529 case bp_watchpoint:
5530 case bp_hardware_watchpoint:
5531 case bp_read_watchpoint:
5532 case bp_access_watchpoint:
5533 if (bs->stop)
5534 {
5535 if (bs->print)
5536 this_action = BPSTAT_WHAT_STOP_NOISY;
5537 else
5538 this_action = BPSTAT_WHAT_STOP_SILENT;
5539 }
5540 else
5541 {
5542 /* There was a watchpoint, but we're not stopping.
5543 This requires no further action. */
5544 }
5545 break;
5546 case bp_longjmp:
5547 case bp_longjmp_call_dummy:
5548 case bp_exception:
5549 if (bs->stop)
5550 {
5551 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5552 retval.is_longjmp = bptype != bp_exception;
5553 }
5554 else
5555 this_action = BPSTAT_WHAT_SINGLE;
5556 break;
5557 case bp_longjmp_resume:
5558 case bp_exception_resume:
5559 if (bs->stop)
5560 {
5561 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5562 retval.is_longjmp = bptype == bp_longjmp_resume;
5563 }
5564 else
5565 this_action = BPSTAT_WHAT_SINGLE;
5566 break;
5567 case bp_step_resume:
5568 if (bs->stop)
5569 this_action = BPSTAT_WHAT_STEP_RESUME;
5570 else
5571 {
5572 /* It is for the wrong frame. */
5573 this_action = BPSTAT_WHAT_SINGLE;
5574 }
5575 break;
5576 case bp_hp_step_resume:
5577 if (bs->stop)
5578 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5579 else
5580 {
5581 /* It is for the wrong frame. */
5582 this_action = BPSTAT_WHAT_SINGLE;
5583 }
5584 break;
5585 case bp_watchpoint_scope:
5586 case bp_thread_event:
5587 case bp_overlay_event:
5588 case bp_longjmp_master:
5589 case bp_std_terminate_master:
5590 case bp_exception_master:
5591 this_action = BPSTAT_WHAT_SINGLE;
5592 break;
5593 case bp_catchpoint:
5594 if (bs->stop)
5595 {
5596 if (bs->print)
5597 this_action = BPSTAT_WHAT_STOP_NOISY;
5598 else
5599 this_action = BPSTAT_WHAT_STOP_SILENT;
5600 }
5601 else
5602 {
5603 /* Some catchpoints are implemented with breakpoints.
5604 For those, we need to step over the breakpoint. */
5605 if (bs->bp_location_at->loc_type != bp_loc_other)
5606 this_action = BPSTAT_WHAT_SINGLE;
5607 }
5608 break;
5609 case bp_jit_event:
5610 this_action = BPSTAT_WHAT_SINGLE;
5611 break;
5612 case bp_call_dummy:
5613 /* Make sure the action is stop (silent or noisy),
5614 so infrun.c pops the dummy frame. */
5615 retval.call_dummy = STOP_STACK_DUMMY;
5616 this_action = BPSTAT_WHAT_STOP_SILENT;
5617 break;
5618 case bp_std_terminate:
5619 /* Make sure the action is stop (silent or noisy),
5620 so infrun.c pops the dummy frame. */
5621 retval.call_dummy = STOP_STD_TERMINATE;
5622 this_action = BPSTAT_WHAT_STOP_SILENT;
5623 break;
5624 case bp_tracepoint:
5625 case bp_fast_tracepoint:
5626 case bp_static_tracepoint:
5627 /* Tracepoint hits should not be reported back to GDB, and
5628 if one got through somehow, it should have been filtered
5629 out already. */
5630 internal_error (__FILE__, __LINE__,
5631 _("bpstat_what: tracepoint encountered"));
5632 break;
5633 case bp_gnu_ifunc_resolver:
5634 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5635 this_action = BPSTAT_WHAT_SINGLE;
5636 break;
5637 case bp_gnu_ifunc_resolver_return:
5638 /* The breakpoint will be removed, execution will restart from the
5639 PC of the former breakpoint. */
5640 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5641 break;
5642
5643 case bp_dprintf:
5644 if (bs->stop)
5645 this_action = BPSTAT_WHAT_STOP_SILENT;
5646 else
5647 this_action = BPSTAT_WHAT_SINGLE;
5648 break;
5649
5650 default:
5651 internal_error (__FILE__, __LINE__,
5652 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5653 }
5654
5655 retval.main_action = std::max (retval.main_action, this_action);
5656 }
5657
5658 return retval;
5659 }
5660
5661 void
5662 bpstat_run_callbacks (bpstat bs_head)
5663 {
5664 bpstat bs;
5665
5666 for (bs = bs_head; bs != NULL; bs = bs->next)
5667 {
5668 struct breakpoint *b = bs->breakpoint_at;
5669
5670 if (b == NULL)
5671 continue;
5672 switch (b->type)
5673 {
5674 case bp_jit_event:
5675 handle_jit_event ();
5676 break;
5677 case bp_gnu_ifunc_resolver:
5678 gnu_ifunc_resolver_stop (b);
5679 break;
5680 case bp_gnu_ifunc_resolver_return:
5681 gnu_ifunc_resolver_return_stop (b);
5682 break;
5683 }
5684 }
5685 }
5686
5687 /* See breakpoint.h. */
5688
5689 bool
5690 bpstat_should_step ()
5691 {
5692 struct breakpoint *b;
5693
5694 ALL_BREAKPOINTS (b)
5695 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5696 return true;
5697 return false;
5698 }
5699
5700 /* See breakpoint.h. */
5701
5702 bool
5703 bpstat_causes_stop (bpstat bs)
5704 {
5705 for (; bs != NULL; bs = bs->next)
5706 if (bs->stop)
5707 return true;
5708
5709 return false;
5710 }
5711
5712 \f
5713
5714 /* Compute a string of spaces suitable to indent the next line
5715 so it starts at the position corresponding to the table column
5716 named COL_NAME in the currently active table of UIOUT. */
5717
5718 static char *
5719 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5720 {
5721 static char wrap_indent[80];
5722 int i, total_width, width, align;
5723 const char *text;
5724
5725 total_width = 0;
5726 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5727 {
5728 if (strcmp (text, col_name) == 0)
5729 {
5730 gdb_assert (total_width < sizeof wrap_indent);
5731 memset (wrap_indent, ' ', total_width);
5732 wrap_indent[total_width] = 0;
5733
5734 return wrap_indent;
5735 }
5736
5737 total_width += width + 1;
5738 }
5739
5740 return NULL;
5741 }
5742
5743 /* Determine if the locations of this breakpoint will have their conditions
5744 evaluated by the target, host or a mix of both. Returns the following:
5745
5746 "host": Host evals condition.
5747 "host or target": Host or Target evals condition.
5748 "target": Target evals condition.
5749 */
5750
5751 static const char *
5752 bp_condition_evaluator (struct breakpoint *b)
5753 {
5754 struct bp_location *bl;
5755 char host_evals = 0;
5756 char target_evals = 0;
5757
5758 if (!b)
5759 return NULL;
5760
5761 if (!is_breakpoint (b))
5762 return NULL;
5763
5764 if (gdb_evaluates_breakpoint_condition_p ()
5765 || !target_supports_evaluation_of_breakpoint_conditions ())
5766 return condition_evaluation_host;
5767
5768 for (bl = b->loc; bl; bl = bl->next)
5769 {
5770 if (bl->cond_bytecode)
5771 target_evals++;
5772 else
5773 host_evals++;
5774 }
5775
5776 if (host_evals && target_evals)
5777 return condition_evaluation_both;
5778 else if (target_evals)
5779 return condition_evaluation_target;
5780 else
5781 return condition_evaluation_host;
5782 }
5783
5784 /* Determine the breakpoint location's condition evaluator. This is
5785 similar to bp_condition_evaluator, but for locations. */
5786
5787 static const char *
5788 bp_location_condition_evaluator (struct bp_location *bl)
5789 {
5790 if (bl && !is_breakpoint (bl->owner))
5791 return NULL;
5792
5793 if (gdb_evaluates_breakpoint_condition_p ()
5794 || !target_supports_evaluation_of_breakpoint_conditions ())
5795 return condition_evaluation_host;
5796
5797 if (bl && bl->cond_bytecode)
5798 return condition_evaluation_target;
5799 else
5800 return condition_evaluation_host;
5801 }
5802
5803 /* Print the LOC location out of the list of B->LOC locations. */
5804
5805 static void
5806 print_breakpoint_location (struct breakpoint *b,
5807 struct bp_location *loc)
5808 {
5809 struct ui_out *uiout = current_uiout;
5810
5811 scoped_restore_current_program_space restore_pspace;
5812
5813 if (loc != NULL && loc->shlib_disabled)
5814 loc = NULL;
5815
5816 if (loc != NULL)
5817 set_current_program_space (loc->pspace);
5818
5819 if (b->display_canonical)
5820 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5821 else if (loc && loc->symtab)
5822 {
5823 const struct symbol *sym = loc->symbol;
5824
5825 if (sym)
5826 {
5827 uiout->text ("in ");
5828 uiout->field_string ("func", sym->print_name (),
5829 function_name_style.style ());
5830 uiout->text (" ");
5831 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5832 uiout->text ("at ");
5833 }
5834 uiout->field_string ("file",
5835 symtab_to_filename_for_display (loc->symtab),
5836 file_name_style.style ());
5837 uiout->text (":");
5838
5839 if (uiout->is_mi_like_p ())
5840 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5841
5842 uiout->field_signed ("line", loc->line_number);
5843 }
5844 else if (loc)
5845 {
5846 string_file stb;
5847
5848 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5849 demangle, "");
5850 uiout->field_stream ("at", stb);
5851 }
5852 else
5853 {
5854 uiout->field_string ("pending",
5855 event_location_to_string (b->location.get ()));
5856 /* If extra_string is available, it could be holding a condition
5857 or dprintf arguments. In either case, make sure it is printed,
5858 too, but only for non-MI streams. */
5859 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5860 {
5861 if (b->type == bp_dprintf)
5862 uiout->text (",");
5863 else
5864 uiout->text (" ");
5865 uiout->text (b->extra_string);
5866 }
5867 }
5868
5869 if (loc && is_breakpoint (b)
5870 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5871 && bp_condition_evaluator (b) == condition_evaluation_both)
5872 {
5873 uiout->text (" (");
5874 uiout->field_string ("evaluated-by",
5875 bp_location_condition_evaluator (loc));
5876 uiout->text (")");
5877 }
5878 }
5879
5880 static const char *
5881 bptype_string (enum bptype type)
5882 {
5883 struct ep_type_description
5884 {
5885 enum bptype type;
5886 const char *description;
5887 };
5888 static struct ep_type_description bptypes[] =
5889 {
5890 {bp_none, "?deleted?"},
5891 {bp_breakpoint, "breakpoint"},
5892 {bp_hardware_breakpoint, "hw breakpoint"},
5893 {bp_single_step, "sw single-step"},
5894 {bp_until, "until"},
5895 {bp_finish, "finish"},
5896 {bp_watchpoint, "watchpoint"},
5897 {bp_hardware_watchpoint, "hw watchpoint"},
5898 {bp_read_watchpoint, "read watchpoint"},
5899 {bp_access_watchpoint, "acc watchpoint"},
5900 {bp_longjmp, "longjmp"},
5901 {bp_longjmp_resume, "longjmp resume"},
5902 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5903 {bp_exception, "exception"},
5904 {bp_exception_resume, "exception resume"},
5905 {bp_step_resume, "step resume"},
5906 {bp_hp_step_resume, "high-priority step resume"},
5907 {bp_watchpoint_scope, "watchpoint scope"},
5908 {bp_call_dummy, "call dummy"},
5909 {bp_std_terminate, "std::terminate"},
5910 {bp_shlib_event, "shlib events"},
5911 {bp_thread_event, "thread events"},
5912 {bp_overlay_event, "overlay events"},
5913 {bp_longjmp_master, "longjmp master"},
5914 {bp_std_terminate_master, "std::terminate master"},
5915 {bp_exception_master, "exception master"},
5916 {bp_catchpoint, "catchpoint"},
5917 {bp_tracepoint, "tracepoint"},
5918 {bp_fast_tracepoint, "fast tracepoint"},
5919 {bp_static_tracepoint, "static tracepoint"},
5920 {bp_dprintf, "dprintf"},
5921 {bp_jit_event, "jit events"},
5922 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5923 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5924 };
5925
5926 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5927 || ((int) type != bptypes[(int) type].type))
5928 internal_error (__FILE__, __LINE__,
5929 _("bptypes table does not describe type #%d."),
5930 (int) type);
5931
5932 return bptypes[(int) type].description;
5933 }
5934
5935 /* For MI, output a field named 'thread-groups' with a list as the value.
5936 For CLI, prefix the list with the string 'inf'. */
5937
5938 static void
5939 output_thread_groups (struct ui_out *uiout,
5940 const char *field_name,
5941 const std::vector<int> &inf_nums,
5942 int mi_only)
5943 {
5944 int is_mi = uiout->is_mi_like_p ();
5945
5946 /* For backward compatibility, don't display inferiors in CLI unless
5947 there are several. Always display them for MI. */
5948 if (!is_mi && mi_only)
5949 return;
5950
5951 ui_out_emit_list list_emitter (uiout, field_name);
5952
5953 for (size_t i = 0; i < inf_nums.size (); i++)
5954 {
5955 if (is_mi)
5956 {
5957 char mi_group[10];
5958
5959 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
5960 uiout->field_string (NULL, mi_group);
5961 }
5962 else
5963 {
5964 if (i == 0)
5965 uiout->text (" inf ");
5966 else
5967 uiout->text (", ");
5968
5969 uiout->text (plongest (inf_nums[i]));
5970 }
5971 }
5972 }
5973
5974 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5975 instead of going via breakpoint_ops::print_one. This makes "maint
5976 info breakpoints" show the software breakpoint locations of
5977 catchpoints, which are considered internal implementation
5978 detail. */
5979
5980 static void
5981 print_one_breakpoint_location (struct breakpoint *b,
5982 struct bp_location *loc,
5983 int loc_number,
5984 struct bp_location **last_loc,
5985 int allflag, bool raw_loc)
5986 {
5987 struct command_line *l;
5988 static char bpenables[] = "nynny";
5989
5990 struct ui_out *uiout = current_uiout;
5991 int header_of_multiple = 0;
5992 int part_of_multiple = (loc != NULL);
5993 struct value_print_options opts;
5994
5995 get_user_print_options (&opts);
5996
5997 gdb_assert (!loc || loc_number != 0);
5998 /* See comment in print_one_breakpoint concerning treatment of
5999 breakpoints with single disabled location. */
6000 if (loc == NULL
6001 && (b->loc != NULL
6002 && (b->loc->next != NULL || !b->loc->enabled)))
6003 header_of_multiple = 1;
6004 if (loc == NULL)
6005 loc = b->loc;
6006
6007 annotate_record ();
6008
6009 /* 1 */
6010 annotate_field (0);
6011 if (part_of_multiple)
6012 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6013 else
6014 uiout->field_signed ("number", b->number);
6015
6016 /* 2 */
6017 annotate_field (1);
6018 if (part_of_multiple)
6019 uiout->field_skip ("type");
6020 else
6021 uiout->field_string ("type", bptype_string (b->type));
6022
6023 /* 3 */
6024 annotate_field (2);
6025 if (part_of_multiple)
6026 uiout->field_skip ("disp");
6027 else
6028 uiout->field_string ("disp", bpdisp_text (b->disposition));
6029
6030 /* 4 */
6031 annotate_field (3);
6032 if (part_of_multiple)
6033 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6034 else
6035 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6036
6037 /* 5 and 6 */
6038 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6039 b->ops->print_one (b, last_loc);
6040 else
6041 {
6042 if (is_watchpoint (b))
6043 {
6044 struct watchpoint *w = (struct watchpoint *) b;
6045
6046 /* Field 4, the address, is omitted (which makes the columns
6047 not line up too nicely with the headers, but the effect
6048 is relatively readable). */
6049 if (opts.addressprint)
6050 uiout->field_skip ("addr");
6051 annotate_field (5);
6052 uiout->field_string ("what", w->exp_string);
6053 }
6054 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6055 || is_ada_exception_catchpoint (b))
6056 {
6057 if (opts.addressprint)
6058 {
6059 annotate_field (4);
6060 if (header_of_multiple)
6061 uiout->field_string ("addr", "<MULTIPLE>",
6062 metadata_style.style ());
6063 else if (b->loc == NULL || loc->shlib_disabled)
6064 uiout->field_string ("addr", "<PENDING>",
6065 metadata_style.style ());
6066 else
6067 uiout->field_core_addr ("addr",
6068 loc->gdbarch, loc->address);
6069 }
6070 annotate_field (5);
6071 if (!header_of_multiple)
6072 print_breakpoint_location (b, loc);
6073 if (b->loc)
6074 *last_loc = b->loc;
6075 }
6076 }
6077
6078 if (loc != NULL && !header_of_multiple)
6079 {
6080 std::vector<int> inf_nums;
6081 int mi_only = 1;
6082
6083 for (inferior *inf : all_inferiors ())
6084 {
6085 if (inf->pspace == loc->pspace)
6086 inf_nums.push_back (inf->num);
6087 }
6088
6089 /* For backward compatibility, don't display inferiors in CLI unless
6090 there are several. Always display for MI. */
6091 if (allflag
6092 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6093 && (number_of_program_spaces () > 1
6094 || number_of_inferiors () > 1)
6095 /* LOC is for existing B, it cannot be in
6096 moribund_locations and thus having NULL OWNER. */
6097 && loc->owner->type != bp_catchpoint))
6098 mi_only = 0;
6099 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6100 }
6101
6102 if (!part_of_multiple)
6103 {
6104 if (b->thread != -1)
6105 {
6106 /* FIXME: This seems to be redundant and lost here; see the
6107 "stop only in" line a little further down. */
6108 uiout->text (" thread ");
6109 uiout->field_signed ("thread", b->thread);
6110 }
6111 else if (b->task != 0)
6112 {
6113 uiout->text (" task ");
6114 uiout->field_signed ("task", b->task);
6115 }
6116 }
6117
6118 uiout->text ("\n");
6119
6120 if (!part_of_multiple)
6121 b->ops->print_one_detail (b, uiout);
6122
6123 if (part_of_multiple && frame_id_p (b->frame_id))
6124 {
6125 annotate_field (6);
6126 uiout->text ("\tstop only in stack frame at ");
6127 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6128 the frame ID. */
6129 uiout->field_core_addr ("frame",
6130 b->gdbarch, b->frame_id.stack_addr);
6131 uiout->text ("\n");
6132 }
6133
6134 if (!part_of_multiple && b->cond_string)
6135 {
6136 annotate_field (7);
6137 if (is_tracepoint (b))
6138 uiout->text ("\ttrace only if ");
6139 else
6140 uiout->text ("\tstop only if ");
6141 uiout->field_string ("cond", b->cond_string);
6142
6143 /* Print whether the target is doing the breakpoint's condition
6144 evaluation. If GDB is doing the evaluation, don't print anything. */
6145 if (is_breakpoint (b)
6146 && breakpoint_condition_evaluation_mode ()
6147 == condition_evaluation_target)
6148 {
6149 uiout->message (" (%pF evals)",
6150 string_field ("evaluated-by",
6151 bp_condition_evaluator (b)));
6152 }
6153 uiout->text ("\n");
6154 }
6155
6156 if (!part_of_multiple && b->thread != -1)
6157 {
6158 /* FIXME should make an annotation for this. */
6159 uiout->text ("\tstop only in thread ");
6160 if (uiout->is_mi_like_p ())
6161 uiout->field_signed ("thread", b->thread);
6162 else
6163 {
6164 struct thread_info *thr = find_thread_global_id (b->thread);
6165
6166 uiout->field_string ("thread", print_thread_id (thr));
6167 }
6168 uiout->text ("\n");
6169 }
6170
6171 if (!part_of_multiple)
6172 {
6173 if (b->hit_count)
6174 {
6175 /* FIXME should make an annotation for this. */
6176 if (is_catchpoint (b))
6177 uiout->text ("\tcatchpoint");
6178 else if (is_tracepoint (b))
6179 uiout->text ("\ttracepoint");
6180 else
6181 uiout->text ("\tbreakpoint");
6182 uiout->text (" already hit ");
6183 uiout->field_signed ("times", b->hit_count);
6184 if (b->hit_count == 1)
6185 uiout->text (" time\n");
6186 else
6187 uiout->text (" times\n");
6188 }
6189 else
6190 {
6191 /* Output the count also if it is zero, but only if this is mi. */
6192 if (uiout->is_mi_like_p ())
6193 uiout->field_signed ("times", b->hit_count);
6194 }
6195 }
6196
6197 if (!part_of_multiple && b->ignore_count)
6198 {
6199 annotate_field (8);
6200 uiout->message ("\tignore next %pF hits\n",
6201 signed_field ("ignore", b->ignore_count));
6202 }
6203
6204 /* Note that an enable count of 1 corresponds to "enable once"
6205 behavior, which is reported by the combination of enablement and
6206 disposition, so we don't need to mention it here. */
6207 if (!part_of_multiple && b->enable_count > 1)
6208 {
6209 annotate_field (8);
6210 uiout->text ("\tdisable after ");
6211 /* Tweak the wording to clarify that ignore and enable counts
6212 are distinct, and have additive effect. */
6213 if (b->ignore_count)
6214 uiout->text ("additional ");
6215 else
6216 uiout->text ("next ");
6217 uiout->field_signed ("enable", b->enable_count);
6218 uiout->text (" hits\n");
6219 }
6220
6221 if (!part_of_multiple && is_tracepoint (b))
6222 {
6223 struct tracepoint *tp = (struct tracepoint *) b;
6224
6225 if (tp->traceframe_usage)
6226 {
6227 uiout->text ("\ttrace buffer usage ");
6228 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6229 uiout->text (" bytes\n");
6230 }
6231 }
6232
6233 l = b->commands ? b->commands.get () : NULL;
6234 if (!part_of_multiple && l)
6235 {
6236 annotate_field (9);
6237 ui_out_emit_tuple tuple_emitter (uiout, "script");
6238 print_command_lines (uiout, l, 4);
6239 }
6240
6241 if (is_tracepoint (b))
6242 {
6243 struct tracepoint *t = (struct tracepoint *) b;
6244
6245 if (!part_of_multiple && t->pass_count)
6246 {
6247 annotate_field (10);
6248 uiout->text ("\tpass count ");
6249 uiout->field_signed ("pass", t->pass_count);
6250 uiout->text (" \n");
6251 }
6252
6253 /* Don't display it when tracepoint or tracepoint location is
6254 pending. */
6255 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6256 {
6257 annotate_field (11);
6258
6259 if (uiout->is_mi_like_p ())
6260 uiout->field_string ("installed",
6261 loc->inserted ? "y" : "n");
6262 else
6263 {
6264 if (loc->inserted)
6265 uiout->text ("\t");
6266 else
6267 uiout->text ("\tnot ");
6268 uiout->text ("installed on target\n");
6269 }
6270 }
6271 }
6272
6273 if (uiout->is_mi_like_p () && !part_of_multiple)
6274 {
6275 if (is_watchpoint (b))
6276 {
6277 struct watchpoint *w = (struct watchpoint *) b;
6278
6279 uiout->field_string ("original-location", w->exp_string);
6280 }
6281 else if (b->location != NULL
6282 && event_location_to_string (b->location.get ()) != NULL)
6283 uiout->field_string ("original-location",
6284 event_location_to_string (b->location.get ()));
6285 }
6286 }
6287
6288 /* See breakpoint.h. */
6289
6290 bool fix_multi_location_breakpoint_output_globally = false;
6291
6292 static void
6293 print_one_breakpoint (struct breakpoint *b,
6294 struct bp_location **last_loc,
6295 int allflag)
6296 {
6297 struct ui_out *uiout = current_uiout;
6298 bool use_fixed_output
6299 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6300 || fix_multi_location_breakpoint_output_globally);
6301
6302 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6303 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6304
6305 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6306 are outside. */
6307 if (!use_fixed_output)
6308 bkpt_tuple_emitter.reset ();
6309
6310 /* If this breakpoint has custom print function,
6311 it's already printed. Otherwise, print individual
6312 locations, if any. */
6313 if (b->ops == NULL
6314 || b->ops->print_one == NULL
6315 || allflag)
6316 {
6317 /* If breakpoint has a single location that is disabled, we
6318 print it as if it had several locations, since otherwise it's
6319 hard to represent "breakpoint enabled, location disabled"
6320 situation.
6321
6322 Note that while hardware watchpoints have several locations
6323 internally, that's not a property exposed to users.
6324
6325 Likewise, while catchpoints may be implemented with
6326 breakpoints (e.g., catch throw), that's not a property
6327 exposed to users. We do however display the internal
6328 breakpoint locations with "maint info breakpoints". */
6329 if (!is_hardware_watchpoint (b)
6330 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6331 || is_ada_exception_catchpoint (b))
6332 && (allflag
6333 || (b->loc && (b->loc->next || !b->loc->enabled))))
6334 {
6335 gdb::optional<ui_out_emit_list> locations_list;
6336
6337 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6338 MI record. For later versions, place breakpoint locations in a
6339 list. */
6340 if (uiout->is_mi_like_p () && use_fixed_output)
6341 locations_list.emplace (uiout, "locations");
6342
6343 int n = 1;
6344 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6345 {
6346 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6347 print_one_breakpoint_location (b, loc, n, last_loc,
6348 allflag, allflag);
6349 }
6350 }
6351 }
6352 }
6353
6354 static int
6355 breakpoint_address_bits (struct breakpoint *b)
6356 {
6357 int print_address_bits = 0;
6358 struct bp_location *loc;
6359
6360 /* Software watchpoints that aren't watching memory don't have an
6361 address to print. */
6362 if (is_no_memory_software_watchpoint (b))
6363 return 0;
6364
6365 for (loc = b->loc; loc; loc = loc->next)
6366 {
6367 int addr_bit;
6368
6369 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6370 if (addr_bit > print_address_bits)
6371 print_address_bits = addr_bit;
6372 }
6373
6374 return print_address_bits;
6375 }
6376
6377 /* See breakpoint.h. */
6378
6379 void
6380 print_breakpoint (breakpoint *b)
6381 {
6382 struct bp_location *dummy_loc = NULL;
6383 print_one_breakpoint (b, &dummy_loc, 0);
6384 }
6385
6386 /* Return true if this breakpoint was set by the user, false if it is
6387 internal or momentary. */
6388
6389 int
6390 user_breakpoint_p (struct breakpoint *b)
6391 {
6392 return b->number > 0;
6393 }
6394
6395 /* See breakpoint.h. */
6396
6397 int
6398 pending_breakpoint_p (struct breakpoint *b)
6399 {
6400 return b->loc == NULL;
6401 }
6402
6403 /* Print information on breakpoints (including watchpoints and tracepoints).
6404
6405 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6406 understood by number_or_range_parser. Only breakpoints included in this
6407 list are then printed.
6408
6409 If SHOW_INTERNAL is true, print internal breakpoints.
6410
6411 If FILTER is non-NULL, call it on each breakpoint and only include the
6412 ones for which it returns true.
6413
6414 Return the total number of breakpoints listed. */
6415
6416 static int
6417 breakpoint_1 (const char *bp_num_list, bool show_internal,
6418 bool (*filter) (const struct breakpoint *))
6419 {
6420 struct breakpoint *b;
6421 struct bp_location *last_loc = NULL;
6422 int nr_printable_breakpoints;
6423 struct value_print_options opts;
6424 int print_address_bits = 0;
6425 int print_type_col_width = 14;
6426 struct ui_out *uiout = current_uiout;
6427
6428 get_user_print_options (&opts);
6429
6430 /* Compute the number of rows in the table, as well as the size
6431 required for address fields. */
6432 nr_printable_breakpoints = 0;
6433 ALL_BREAKPOINTS (b)
6434 {
6435 /* If we have a filter, only list the breakpoints it accepts. */
6436 if (filter && !filter (b))
6437 continue;
6438
6439 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6440 accept. Skip the others. */
6441 if (bp_num_list != NULL && *bp_num_list != '\0')
6442 {
6443 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6444 continue;
6445 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6446 continue;
6447 }
6448
6449 if (show_internal || user_breakpoint_p (b))
6450 {
6451 int addr_bit, type_len;
6452
6453 addr_bit = breakpoint_address_bits (b);
6454 if (addr_bit > print_address_bits)
6455 print_address_bits = addr_bit;
6456
6457 type_len = strlen (bptype_string (b->type));
6458 if (type_len > print_type_col_width)
6459 print_type_col_width = type_len;
6460
6461 nr_printable_breakpoints++;
6462 }
6463 }
6464
6465 {
6466 ui_out_emit_table table_emitter (uiout,
6467 opts.addressprint ? 6 : 5,
6468 nr_printable_breakpoints,
6469 "BreakpointTable");
6470
6471 if (nr_printable_breakpoints > 0)
6472 annotate_breakpoints_headers ();
6473 if (nr_printable_breakpoints > 0)
6474 annotate_field (0);
6475 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6476 if (nr_printable_breakpoints > 0)
6477 annotate_field (1);
6478 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6479 if (nr_printable_breakpoints > 0)
6480 annotate_field (2);
6481 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6482 if (nr_printable_breakpoints > 0)
6483 annotate_field (3);
6484 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6485 if (opts.addressprint)
6486 {
6487 if (nr_printable_breakpoints > 0)
6488 annotate_field (4);
6489 if (print_address_bits <= 32)
6490 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6491 else
6492 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6493 }
6494 if (nr_printable_breakpoints > 0)
6495 annotate_field (5);
6496 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6497 uiout->table_body ();
6498 if (nr_printable_breakpoints > 0)
6499 annotate_breakpoints_table ();
6500
6501 ALL_BREAKPOINTS (b)
6502 {
6503 QUIT;
6504 /* If we have a filter, only list the breakpoints it accepts. */
6505 if (filter && !filter (b))
6506 continue;
6507
6508 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6509 accept. Skip the others. */
6510
6511 if (bp_num_list != NULL && *bp_num_list != '\0')
6512 {
6513 if (show_internal) /* maintenance info breakpoint */
6514 {
6515 if (parse_and_eval_long (bp_num_list) != b->number)
6516 continue;
6517 }
6518 else /* all others */
6519 {
6520 if (!number_is_in_list (bp_num_list, b->number))
6521 continue;
6522 }
6523 }
6524 /* We only print out user settable breakpoints unless the
6525 show_internal is set. */
6526 if (show_internal || user_breakpoint_p (b))
6527 print_one_breakpoint (b, &last_loc, show_internal);
6528 }
6529 }
6530
6531 if (nr_printable_breakpoints == 0)
6532 {
6533 /* If there's a filter, let the caller decide how to report
6534 empty list. */
6535 if (!filter)
6536 {
6537 if (bp_num_list == NULL || *bp_num_list == '\0')
6538 uiout->message ("No breakpoints or watchpoints.\n");
6539 else
6540 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6541 bp_num_list);
6542 }
6543 }
6544 else
6545 {
6546 if (last_loc && !server_command)
6547 set_next_address (last_loc->gdbarch, last_loc->address);
6548 }
6549
6550 /* FIXME? Should this be moved up so that it is only called when
6551 there have been breakpoints? */
6552 annotate_breakpoints_table_end ();
6553
6554 return nr_printable_breakpoints;
6555 }
6556
6557 /* Display the value of default-collect in a way that is generally
6558 compatible with the breakpoint list. */
6559
6560 static void
6561 default_collect_info (void)
6562 {
6563 struct ui_out *uiout = current_uiout;
6564
6565 /* If it has no value (which is frequently the case), say nothing; a
6566 message like "No default-collect." gets in user's face when it's
6567 not wanted. */
6568 if (!*default_collect)
6569 return;
6570
6571 /* The following phrase lines up nicely with per-tracepoint collect
6572 actions. */
6573 uiout->text ("default collect ");
6574 uiout->field_string ("default-collect", default_collect);
6575 uiout->text (" \n");
6576 }
6577
6578 static void
6579 info_breakpoints_command (const char *args, int from_tty)
6580 {
6581 breakpoint_1 (args, false, NULL);
6582
6583 default_collect_info ();
6584 }
6585
6586 static void
6587 info_watchpoints_command (const char *args, int from_tty)
6588 {
6589 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6590 struct ui_out *uiout = current_uiout;
6591
6592 if (num_printed == 0)
6593 {
6594 if (args == NULL || *args == '\0')
6595 uiout->message ("No watchpoints.\n");
6596 else
6597 uiout->message ("No watchpoint matching '%s'.\n", args);
6598 }
6599 }
6600
6601 static void
6602 maintenance_info_breakpoints (const char *args, int from_tty)
6603 {
6604 breakpoint_1 (args, true, NULL);
6605
6606 default_collect_info ();
6607 }
6608
6609 static int
6610 breakpoint_has_pc (struct breakpoint *b,
6611 struct program_space *pspace,
6612 CORE_ADDR pc, struct obj_section *section)
6613 {
6614 struct bp_location *bl = b->loc;
6615
6616 for (; bl; bl = bl->next)
6617 {
6618 if (bl->pspace == pspace
6619 && bl->address == pc
6620 && (!overlay_debugging || bl->section == section))
6621 return 1;
6622 }
6623 return 0;
6624 }
6625
6626 /* Print a message describing any user-breakpoints set at PC. This
6627 concerns with logical breakpoints, so we match program spaces, not
6628 address spaces. */
6629
6630 static void
6631 describe_other_breakpoints (struct gdbarch *gdbarch,
6632 struct program_space *pspace, CORE_ADDR pc,
6633 struct obj_section *section, int thread)
6634 {
6635 int others = 0;
6636 struct breakpoint *b;
6637
6638 ALL_BREAKPOINTS (b)
6639 others += (user_breakpoint_p (b)
6640 && breakpoint_has_pc (b, pspace, pc, section));
6641 if (others > 0)
6642 {
6643 if (others == 1)
6644 printf_filtered (_("Note: breakpoint "));
6645 else /* if (others == ???) */
6646 printf_filtered (_("Note: breakpoints "));
6647 ALL_BREAKPOINTS (b)
6648 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6649 {
6650 others--;
6651 printf_filtered ("%d", b->number);
6652 if (b->thread == -1 && thread != -1)
6653 printf_filtered (" (all threads)");
6654 else if (b->thread != -1)
6655 printf_filtered (" (thread %d)", b->thread);
6656 printf_filtered ("%s%s ",
6657 ((b->enable_state == bp_disabled
6658 || b->enable_state == bp_call_disabled)
6659 ? " (disabled)"
6660 : ""),
6661 (others > 1) ? ","
6662 : ((others == 1) ? " and" : ""));
6663 }
6664 current_uiout->message (_("also set at pc %ps.\n"),
6665 styled_string (address_style.style (),
6666 paddress (gdbarch, pc)));
6667 }
6668 }
6669 \f
6670
6671 /* Return true iff it is meaningful to use the address member of LOC.
6672 For some breakpoint types, the locations' address members are
6673 irrelevant and it makes no sense to attempt to compare them to
6674 other addresses (or use them for any other purpose either).
6675
6676 More specifically, software watchpoints and catchpoints that are
6677 not backed by breakpoints always have a zero valued location
6678 address and we don't want to mark breakpoints of any of these types
6679 to be a duplicate of an actual breakpoint location at address
6680 zero. */
6681
6682 static bool
6683 bl_address_is_meaningful (bp_location *loc)
6684 {
6685 return loc->loc_type != bp_loc_other;
6686 }
6687
6688 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6689 true if LOC1 and LOC2 represent the same watchpoint location. */
6690
6691 static int
6692 watchpoint_locations_match (struct bp_location *loc1,
6693 struct bp_location *loc2)
6694 {
6695 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6696 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6697
6698 /* Both of them must exist. */
6699 gdb_assert (w1 != NULL);
6700 gdb_assert (w2 != NULL);
6701
6702 /* If the target can evaluate the condition expression in hardware,
6703 then we we need to insert both watchpoints even if they are at
6704 the same place. Otherwise the watchpoint will only trigger when
6705 the condition of whichever watchpoint was inserted evaluates to
6706 true, not giving a chance for GDB to check the condition of the
6707 other watchpoint. */
6708 if ((w1->cond_exp
6709 && target_can_accel_watchpoint_condition (loc1->address,
6710 loc1->length,
6711 loc1->watchpoint_type,
6712 w1->cond_exp.get ()))
6713 || (w2->cond_exp
6714 && target_can_accel_watchpoint_condition (loc2->address,
6715 loc2->length,
6716 loc2->watchpoint_type,
6717 w2->cond_exp.get ())))
6718 return 0;
6719
6720 /* Note that this checks the owner's type, not the location's. In
6721 case the target does not support read watchpoints, but does
6722 support access watchpoints, we'll have bp_read_watchpoint
6723 watchpoints with hw_access locations. Those should be considered
6724 duplicates of hw_read locations. The hw_read locations will
6725 become hw_access locations later. */
6726 return (loc1->owner->type == loc2->owner->type
6727 && loc1->pspace->aspace == loc2->pspace->aspace
6728 && loc1->address == loc2->address
6729 && loc1->length == loc2->length);
6730 }
6731
6732 /* See breakpoint.h. */
6733
6734 int
6735 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6736 const address_space *aspace2, CORE_ADDR addr2)
6737 {
6738 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6739 || aspace1 == aspace2)
6740 && addr1 == addr2);
6741 }
6742
6743 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6744 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6745 matches ASPACE2. On targets that have global breakpoints, the address
6746 space doesn't really matter. */
6747
6748 static int
6749 breakpoint_address_match_range (const address_space *aspace1,
6750 CORE_ADDR addr1,
6751 int len1, const address_space *aspace2,
6752 CORE_ADDR addr2)
6753 {
6754 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6755 || aspace1 == aspace2)
6756 && addr2 >= addr1 && addr2 < addr1 + len1);
6757 }
6758
6759 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6760 a ranged breakpoint. In most targets, a match happens only if ASPACE
6761 matches the breakpoint's address space. On targets that have global
6762 breakpoints, the address space doesn't really matter. */
6763
6764 static int
6765 breakpoint_location_address_match (struct bp_location *bl,
6766 const address_space *aspace,
6767 CORE_ADDR addr)
6768 {
6769 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6770 aspace, addr)
6771 || (bl->length
6772 && breakpoint_address_match_range (bl->pspace->aspace,
6773 bl->address, bl->length,
6774 aspace, addr)));
6775 }
6776
6777 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6778 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6779 match happens only if ASPACE matches the breakpoint's address
6780 space. On targets that have global breakpoints, the address space
6781 doesn't really matter. */
6782
6783 static int
6784 breakpoint_location_address_range_overlap (struct bp_location *bl,
6785 const address_space *aspace,
6786 CORE_ADDR addr, int len)
6787 {
6788 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6789 || bl->pspace->aspace == aspace)
6790 {
6791 int bl_len = bl->length != 0 ? bl->length : 1;
6792
6793 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6794 return 1;
6795 }
6796 return 0;
6797 }
6798
6799 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6800 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6801 true, otherwise returns false. */
6802
6803 static int
6804 tracepoint_locations_match (struct bp_location *loc1,
6805 struct bp_location *loc2)
6806 {
6807 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6808 /* Since tracepoint locations are never duplicated with others', tracepoint
6809 locations at the same address of different tracepoints are regarded as
6810 different locations. */
6811 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6812 else
6813 return 0;
6814 }
6815
6816 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6817 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6818 the same location. */
6819
6820 static int
6821 breakpoint_locations_match (struct bp_location *loc1,
6822 struct bp_location *loc2)
6823 {
6824 int hw_point1, hw_point2;
6825
6826 /* Both of them must not be in moribund_locations. */
6827 gdb_assert (loc1->owner != NULL);
6828 gdb_assert (loc2->owner != NULL);
6829
6830 hw_point1 = is_hardware_watchpoint (loc1->owner);
6831 hw_point2 = is_hardware_watchpoint (loc2->owner);
6832
6833 if (hw_point1 != hw_point2)
6834 return 0;
6835 else if (hw_point1)
6836 return watchpoint_locations_match (loc1, loc2);
6837 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6838 return tracepoint_locations_match (loc1, loc2);
6839 else
6840 /* We compare bp_location.length in order to cover ranged breakpoints. */
6841 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6842 loc2->pspace->aspace, loc2->address)
6843 && loc1->length == loc2->length);
6844 }
6845
6846 static void
6847 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6848 int bnum, int have_bnum)
6849 {
6850 /* The longest string possibly returned by hex_string_custom
6851 is 50 chars. These must be at least that big for safety. */
6852 char astr1[64];
6853 char astr2[64];
6854
6855 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6856 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6857 if (have_bnum)
6858 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6859 bnum, astr1, astr2);
6860 else
6861 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6862 }
6863
6864 /* Adjust a breakpoint's address to account for architectural
6865 constraints on breakpoint placement. Return the adjusted address.
6866 Note: Very few targets require this kind of adjustment. For most
6867 targets, this function is simply the identity function. */
6868
6869 static CORE_ADDR
6870 adjust_breakpoint_address (struct gdbarch *gdbarch,
6871 CORE_ADDR bpaddr, enum bptype bptype)
6872 {
6873 if (bptype == bp_watchpoint
6874 || bptype == bp_hardware_watchpoint
6875 || bptype == bp_read_watchpoint
6876 || bptype == bp_access_watchpoint
6877 || bptype == bp_catchpoint)
6878 {
6879 /* Watchpoints and the various bp_catch_* eventpoints should not
6880 have their addresses modified. */
6881 return bpaddr;
6882 }
6883 else if (bptype == bp_single_step)
6884 {
6885 /* Single-step breakpoints should not have their addresses
6886 modified. If there's any architectural constrain that
6887 applies to this address, then it should have already been
6888 taken into account when the breakpoint was created in the
6889 first place. If we didn't do this, stepping through e.g.,
6890 Thumb-2 IT blocks would break. */
6891 return bpaddr;
6892 }
6893 else
6894 {
6895 CORE_ADDR adjusted_bpaddr = bpaddr;
6896
6897 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6898 {
6899 /* Some targets have architectural constraints on the placement
6900 of breakpoint instructions. Obtain the adjusted address. */
6901 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6902 }
6903
6904 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
6905
6906 /* An adjusted breakpoint address can significantly alter
6907 a user's expectations. Print a warning if an adjustment
6908 is required. */
6909 if (adjusted_bpaddr != bpaddr)
6910 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6911
6912 return adjusted_bpaddr;
6913 }
6914 }
6915
6916 static bp_loc_type
6917 bp_location_from_bp_type (bptype type)
6918 {
6919 switch (type)
6920 {
6921 case bp_breakpoint:
6922 case bp_single_step:
6923 case bp_until:
6924 case bp_finish:
6925 case bp_longjmp:
6926 case bp_longjmp_resume:
6927 case bp_longjmp_call_dummy:
6928 case bp_exception:
6929 case bp_exception_resume:
6930 case bp_step_resume:
6931 case bp_hp_step_resume:
6932 case bp_watchpoint_scope:
6933 case bp_call_dummy:
6934 case bp_std_terminate:
6935 case bp_shlib_event:
6936 case bp_thread_event:
6937 case bp_overlay_event:
6938 case bp_jit_event:
6939 case bp_longjmp_master:
6940 case bp_std_terminate_master:
6941 case bp_exception_master:
6942 case bp_gnu_ifunc_resolver:
6943 case bp_gnu_ifunc_resolver_return:
6944 case bp_dprintf:
6945 return bp_loc_software_breakpoint;
6946 case bp_hardware_breakpoint:
6947 return bp_loc_hardware_breakpoint;
6948 case bp_hardware_watchpoint:
6949 case bp_read_watchpoint:
6950 case bp_access_watchpoint:
6951 return bp_loc_hardware_watchpoint;
6952 case bp_watchpoint:
6953 case bp_catchpoint:
6954 case bp_tracepoint:
6955 case bp_fast_tracepoint:
6956 case bp_static_tracepoint:
6957 return bp_loc_other;
6958 default:
6959 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6960 }
6961 }
6962
6963 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6964 {
6965 this->owner = owner;
6966 this->cond_bytecode = NULL;
6967 this->shlib_disabled = 0;
6968 this->enabled = 1;
6969
6970 this->loc_type = type;
6971
6972 if (this->loc_type == bp_loc_software_breakpoint
6973 || this->loc_type == bp_loc_hardware_breakpoint)
6974 mark_breakpoint_location_modified (this);
6975
6976 this->refc = 1;
6977 }
6978
6979 bp_location::bp_location (breakpoint *owner)
6980 : bp_location::bp_location (owner,
6981 bp_location_from_bp_type (owner->type))
6982 {
6983 }
6984
6985 /* Allocate a struct bp_location. */
6986
6987 static struct bp_location *
6988 allocate_bp_location (struct breakpoint *bpt)
6989 {
6990 return bpt->ops->allocate_location (bpt);
6991 }
6992
6993 static void
6994 free_bp_location (struct bp_location *loc)
6995 {
6996 delete loc;
6997 }
6998
6999 /* Increment reference count. */
7000
7001 static void
7002 incref_bp_location (struct bp_location *bl)
7003 {
7004 ++bl->refc;
7005 }
7006
7007 /* Decrement reference count. If the reference count reaches 0,
7008 destroy the bp_location. Sets *BLP to NULL. */
7009
7010 static void
7011 decref_bp_location (struct bp_location **blp)
7012 {
7013 gdb_assert ((*blp)->refc > 0);
7014
7015 if (--(*blp)->refc == 0)
7016 free_bp_location (*blp);
7017 *blp = NULL;
7018 }
7019
7020 /* Add breakpoint B at the end of the global breakpoint chain. */
7021
7022 static breakpoint *
7023 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7024 {
7025 struct breakpoint *b1;
7026 struct breakpoint *result = b.get ();
7027
7028 /* Add this breakpoint to the end of the chain so that a list of
7029 breakpoints will come out in order of increasing numbers. */
7030
7031 b1 = breakpoint_chain;
7032 if (b1 == 0)
7033 breakpoint_chain = b.release ();
7034 else
7035 {
7036 while (b1->next)
7037 b1 = b1->next;
7038 b1->next = b.release ();
7039 }
7040
7041 return result;
7042 }
7043
7044 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7045
7046 static void
7047 init_raw_breakpoint_without_location (struct breakpoint *b,
7048 struct gdbarch *gdbarch,
7049 enum bptype bptype,
7050 const struct breakpoint_ops *ops)
7051 {
7052 gdb_assert (ops != NULL);
7053
7054 b->ops = ops;
7055 b->type = bptype;
7056 b->gdbarch = gdbarch;
7057 b->language = current_language->la_language;
7058 b->input_radix = input_radix;
7059 b->related_breakpoint = b;
7060 }
7061
7062 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7063 that has type BPTYPE and has no locations as yet. */
7064
7065 static struct breakpoint *
7066 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7067 enum bptype bptype,
7068 const struct breakpoint_ops *ops)
7069 {
7070 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7071
7072 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7073 return add_to_breakpoint_chain (std::move (b));
7074 }
7075
7076 /* Initialize loc->function_name. */
7077
7078 static void
7079 set_breakpoint_location_function (struct bp_location *loc)
7080 {
7081 gdb_assert (loc->owner != NULL);
7082
7083 if (loc->owner->type == bp_breakpoint
7084 || loc->owner->type == bp_hardware_breakpoint
7085 || is_tracepoint (loc->owner))
7086 {
7087 const char *function_name;
7088
7089 if (loc->msymbol != NULL
7090 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7091 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7092 {
7093 struct breakpoint *b = loc->owner;
7094
7095 function_name = loc->msymbol->linkage_name ();
7096
7097 if (b->type == bp_breakpoint && b->loc == loc
7098 && loc->next == NULL && b->related_breakpoint == b)
7099 {
7100 /* Create only the whole new breakpoint of this type but do not
7101 mess more complicated breakpoints with multiple locations. */
7102 b->type = bp_gnu_ifunc_resolver;
7103 /* Remember the resolver's address for use by the return
7104 breakpoint. */
7105 loc->related_address = loc->address;
7106 }
7107 }
7108 else
7109 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7110
7111 if (function_name)
7112 loc->function_name = xstrdup (function_name);
7113 }
7114 }
7115
7116 /* Attempt to determine architecture of location identified by SAL. */
7117 struct gdbarch *
7118 get_sal_arch (struct symtab_and_line sal)
7119 {
7120 if (sal.section)
7121 return get_objfile_arch (sal.section->objfile);
7122 if (sal.symtab)
7123 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7124
7125 return NULL;
7126 }
7127
7128 /* Low level routine for partially initializing a breakpoint of type
7129 BPTYPE. The newly created breakpoint's address, section, source
7130 file name, and line number are provided by SAL.
7131
7132 It is expected that the caller will complete the initialization of
7133 the newly created breakpoint struct as well as output any status
7134 information regarding the creation of a new breakpoint. */
7135
7136 static void
7137 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7138 struct symtab_and_line sal, enum bptype bptype,
7139 const struct breakpoint_ops *ops)
7140 {
7141 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7142
7143 add_location_to_breakpoint (b, &sal);
7144
7145 if (bptype != bp_catchpoint)
7146 gdb_assert (sal.pspace != NULL);
7147
7148 /* Store the program space that was used to set the breakpoint,
7149 except for ordinary breakpoints, which are independent of the
7150 program space. */
7151 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7152 b->pspace = sal.pspace;
7153 }
7154
7155 /* set_raw_breakpoint is a low level routine for allocating and
7156 partially initializing a breakpoint of type BPTYPE. The newly
7157 created breakpoint's address, section, source file name, and line
7158 number are provided by SAL. The newly created and partially
7159 initialized breakpoint is added to the breakpoint chain and
7160 is also returned as the value of this function.
7161
7162 It is expected that the caller will complete the initialization of
7163 the newly created breakpoint struct as well as output any status
7164 information regarding the creation of a new breakpoint. In
7165 particular, set_raw_breakpoint does NOT set the breakpoint
7166 number! Care should be taken to not allow an error to occur
7167 prior to completing the initialization of the breakpoint. If this
7168 should happen, a bogus breakpoint will be left on the chain. */
7169
7170 struct breakpoint *
7171 set_raw_breakpoint (struct gdbarch *gdbarch,
7172 struct symtab_and_line sal, enum bptype bptype,
7173 const struct breakpoint_ops *ops)
7174 {
7175 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7176
7177 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7178 return add_to_breakpoint_chain (std::move (b));
7179 }
7180
7181 /* Call this routine when stepping and nexting to enable a breakpoint
7182 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7183 initiated the operation. */
7184
7185 void
7186 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7187 {
7188 struct breakpoint *b, *b_tmp;
7189 int thread = tp->global_num;
7190
7191 /* To avoid having to rescan all objfile symbols at every step,
7192 we maintain a list of continually-inserted but always disabled
7193 longjmp "master" breakpoints. Here, we simply create momentary
7194 clones of those and enable them for the requested thread. */
7195 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7196 if (b->pspace == current_program_space
7197 && (b->type == bp_longjmp_master
7198 || b->type == bp_exception_master))
7199 {
7200 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7201 struct breakpoint *clone;
7202
7203 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7204 after their removal. */
7205 clone = momentary_breakpoint_from_master (b, type,
7206 &momentary_breakpoint_ops, 1);
7207 clone->thread = thread;
7208 }
7209
7210 tp->initiating_frame = frame;
7211 }
7212
7213 /* Delete all longjmp breakpoints from THREAD. */
7214 void
7215 delete_longjmp_breakpoint (int thread)
7216 {
7217 struct breakpoint *b, *b_tmp;
7218
7219 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7220 if (b->type == bp_longjmp || b->type == bp_exception)
7221 {
7222 if (b->thread == thread)
7223 delete_breakpoint (b);
7224 }
7225 }
7226
7227 void
7228 delete_longjmp_breakpoint_at_next_stop (int thread)
7229 {
7230 struct breakpoint *b, *b_tmp;
7231
7232 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7233 if (b->type == bp_longjmp || b->type == bp_exception)
7234 {
7235 if (b->thread == thread)
7236 b->disposition = disp_del_at_next_stop;
7237 }
7238 }
7239
7240 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7241 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7242 pointer to any of them. Return NULL if this system cannot place longjmp
7243 breakpoints. */
7244
7245 struct breakpoint *
7246 set_longjmp_breakpoint_for_call_dummy (void)
7247 {
7248 struct breakpoint *b, *retval = NULL;
7249
7250 ALL_BREAKPOINTS (b)
7251 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7252 {
7253 struct breakpoint *new_b;
7254
7255 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7256 &momentary_breakpoint_ops,
7257 1);
7258 new_b->thread = inferior_thread ()->global_num;
7259
7260 /* Link NEW_B into the chain of RETVAL breakpoints. */
7261
7262 gdb_assert (new_b->related_breakpoint == new_b);
7263 if (retval == NULL)
7264 retval = new_b;
7265 new_b->related_breakpoint = retval;
7266 while (retval->related_breakpoint != new_b->related_breakpoint)
7267 retval = retval->related_breakpoint;
7268 retval->related_breakpoint = new_b;
7269 }
7270
7271 return retval;
7272 }
7273
7274 /* Verify all existing dummy frames and their associated breakpoints for
7275 TP. Remove those which can no longer be found in the current frame
7276 stack.
7277
7278 You should call this function only at places where it is safe to currently
7279 unwind the whole stack. Failed stack unwind would discard live dummy
7280 frames. */
7281
7282 void
7283 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7284 {
7285 struct breakpoint *b, *b_tmp;
7286
7287 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7288 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7289 {
7290 struct breakpoint *dummy_b = b->related_breakpoint;
7291
7292 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7293 dummy_b = dummy_b->related_breakpoint;
7294 if (dummy_b->type != bp_call_dummy
7295 || frame_find_by_id (dummy_b->frame_id) != NULL)
7296 continue;
7297
7298 dummy_frame_discard (dummy_b->frame_id, tp);
7299
7300 while (b->related_breakpoint != b)
7301 {
7302 if (b_tmp == b->related_breakpoint)
7303 b_tmp = b->related_breakpoint->next;
7304 delete_breakpoint (b->related_breakpoint);
7305 }
7306 delete_breakpoint (b);
7307 }
7308 }
7309
7310 void
7311 enable_overlay_breakpoints (void)
7312 {
7313 struct breakpoint *b;
7314
7315 ALL_BREAKPOINTS (b)
7316 if (b->type == bp_overlay_event)
7317 {
7318 b->enable_state = bp_enabled;
7319 update_global_location_list (UGLL_MAY_INSERT);
7320 overlay_events_enabled = 1;
7321 }
7322 }
7323
7324 void
7325 disable_overlay_breakpoints (void)
7326 {
7327 struct breakpoint *b;
7328
7329 ALL_BREAKPOINTS (b)
7330 if (b->type == bp_overlay_event)
7331 {
7332 b->enable_state = bp_disabled;
7333 update_global_location_list (UGLL_DONT_INSERT);
7334 overlay_events_enabled = 0;
7335 }
7336 }
7337
7338 /* Set an active std::terminate breakpoint for each std::terminate
7339 master breakpoint. */
7340 void
7341 set_std_terminate_breakpoint (void)
7342 {
7343 struct breakpoint *b, *b_tmp;
7344
7345 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7346 if (b->pspace == current_program_space
7347 && b->type == bp_std_terminate_master)
7348 {
7349 momentary_breakpoint_from_master (b, bp_std_terminate,
7350 &momentary_breakpoint_ops, 1);
7351 }
7352 }
7353
7354 /* Delete all the std::terminate breakpoints. */
7355 void
7356 delete_std_terminate_breakpoint (void)
7357 {
7358 struct breakpoint *b, *b_tmp;
7359
7360 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7361 if (b->type == bp_std_terminate)
7362 delete_breakpoint (b);
7363 }
7364
7365 struct breakpoint *
7366 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7367 {
7368 struct breakpoint *b;
7369
7370 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7371 &internal_breakpoint_ops);
7372
7373 b->enable_state = bp_enabled;
7374 /* location has to be used or breakpoint_re_set will delete me. */
7375 b->location = new_address_location (b->loc->address, NULL, 0);
7376
7377 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7378
7379 return b;
7380 }
7381
7382 struct lang_and_radix
7383 {
7384 enum language lang;
7385 int radix;
7386 };
7387
7388 /* Create a breakpoint for JIT code registration and unregistration. */
7389
7390 struct breakpoint *
7391 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7392 {
7393 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7394 &internal_breakpoint_ops);
7395 }
7396
7397 /* Remove JIT code registration and unregistration breakpoint(s). */
7398
7399 void
7400 remove_jit_event_breakpoints (void)
7401 {
7402 struct breakpoint *b, *b_tmp;
7403
7404 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7405 if (b->type == bp_jit_event
7406 && b->loc->pspace == current_program_space)
7407 delete_breakpoint (b);
7408 }
7409
7410 void
7411 remove_solib_event_breakpoints (void)
7412 {
7413 struct breakpoint *b, *b_tmp;
7414
7415 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7416 if (b->type == bp_shlib_event
7417 && b->loc->pspace == current_program_space)
7418 delete_breakpoint (b);
7419 }
7420
7421 /* See breakpoint.h. */
7422
7423 void
7424 remove_solib_event_breakpoints_at_next_stop (void)
7425 {
7426 struct breakpoint *b, *b_tmp;
7427
7428 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7429 if (b->type == bp_shlib_event
7430 && b->loc->pspace == current_program_space)
7431 b->disposition = disp_del_at_next_stop;
7432 }
7433
7434 /* Helper for create_solib_event_breakpoint /
7435 create_and_insert_solib_event_breakpoint. Allows specifying which
7436 INSERT_MODE to pass through to update_global_location_list. */
7437
7438 static struct breakpoint *
7439 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7440 enum ugll_insert_mode insert_mode)
7441 {
7442 struct breakpoint *b;
7443
7444 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7445 &internal_breakpoint_ops);
7446 update_global_location_list_nothrow (insert_mode);
7447 return b;
7448 }
7449
7450 struct breakpoint *
7451 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7452 {
7453 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7454 }
7455
7456 /* See breakpoint.h. */
7457
7458 struct breakpoint *
7459 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7460 {
7461 struct breakpoint *b;
7462
7463 /* Explicitly tell update_global_location_list to insert
7464 locations. */
7465 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7466 if (!b->loc->inserted)
7467 {
7468 delete_breakpoint (b);
7469 return NULL;
7470 }
7471 return b;
7472 }
7473
7474 /* Disable any breakpoints that are on code in shared libraries. Only
7475 apply to enabled breakpoints, disabled ones can just stay disabled. */
7476
7477 void
7478 disable_breakpoints_in_shlibs (void)
7479 {
7480 struct bp_location *loc, **locp_tmp;
7481
7482 ALL_BP_LOCATIONS (loc, locp_tmp)
7483 {
7484 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7485 struct breakpoint *b = loc->owner;
7486
7487 /* We apply the check to all breakpoints, including disabled for
7488 those with loc->duplicate set. This is so that when breakpoint
7489 becomes enabled, or the duplicate is removed, gdb will try to
7490 insert all breakpoints. If we don't set shlib_disabled here,
7491 we'll try to insert those breakpoints and fail. */
7492 if (((b->type == bp_breakpoint)
7493 || (b->type == bp_jit_event)
7494 || (b->type == bp_hardware_breakpoint)
7495 || (is_tracepoint (b)))
7496 && loc->pspace == current_program_space
7497 && !loc->shlib_disabled
7498 && solib_name_from_address (loc->pspace, loc->address)
7499 )
7500 {
7501 loc->shlib_disabled = 1;
7502 }
7503 }
7504 }
7505
7506 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7507 notification of unloaded_shlib. Only apply to enabled breakpoints,
7508 disabled ones can just stay disabled. */
7509
7510 static void
7511 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7512 {
7513 struct bp_location *loc, **locp_tmp;
7514 int disabled_shlib_breaks = 0;
7515
7516 ALL_BP_LOCATIONS (loc, locp_tmp)
7517 {
7518 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7519 struct breakpoint *b = loc->owner;
7520
7521 if (solib->pspace == loc->pspace
7522 && !loc->shlib_disabled
7523 && (((b->type == bp_breakpoint
7524 || b->type == bp_jit_event
7525 || b->type == bp_hardware_breakpoint)
7526 && (loc->loc_type == bp_loc_hardware_breakpoint
7527 || loc->loc_type == bp_loc_software_breakpoint))
7528 || is_tracepoint (b))
7529 && solib_contains_address_p (solib, loc->address))
7530 {
7531 loc->shlib_disabled = 1;
7532 /* At this point, we cannot rely on remove_breakpoint
7533 succeeding so we must mark the breakpoint as not inserted
7534 to prevent future errors occurring in remove_breakpoints. */
7535 loc->inserted = 0;
7536
7537 /* This may cause duplicate notifications for the same breakpoint. */
7538 gdb::observers::breakpoint_modified.notify (b);
7539
7540 if (!disabled_shlib_breaks)
7541 {
7542 target_terminal::ours_for_output ();
7543 warning (_("Temporarily disabling breakpoints "
7544 "for unloaded shared library \"%s\""),
7545 solib->so_name);
7546 }
7547 disabled_shlib_breaks = 1;
7548 }
7549 }
7550 }
7551
7552 /* Disable any breakpoints and tracepoints in OBJFILE upon
7553 notification of free_objfile. Only apply to enabled breakpoints,
7554 disabled ones can just stay disabled. */
7555
7556 static void
7557 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7558 {
7559 struct breakpoint *b;
7560
7561 if (objfile == NULL)
7562 return;
7563
7564 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7565 managed by the user with add-symbol-file/remove-symbol-file.
7566 Similarly to how breakpoints in shared libraries are handled in
7567 response to "nosharedlibrary", mark breakpoints in such modules
7568 shlib_disabled so they end up uninserted on the next global
7569 location list update. Shared libraries not loaded by the user
7570 aren't handled here -- they're already handled in
7571 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7572 solib_unloaded observer. We skip objfiles that are not
7573 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7574 main objfile). */
7575 if ((objfile->flags & OBJF_SHARED) == 0
7576 || (objfile->flags & OBJF_USERLOADED) == 0)
7577 return;
7578
7579 ALL_BREAKPOINTS (b)
7580 {
7581 struct bp_location *loc;
7582 int bp_modified = 0;
7583
7584 if (!is_breakpoint (b) && !is_tracepoint (b))
7585 continue;
7586
7587 for (loc = b->loc; loc != NULL; loc = loc->next)
7588 {
7589 CORE_ADDR loc_addr = loc->address;
7590
7591 if (loc->loc_type != bp_loc_hardware_breakpoint
7592 && loc->loc_type != bp_loc_software_breakpoint)
7593 continue;
7594
7595 if (loc->shlib_disabled != 0)
7596 continue;
7597
7598 if (objfile->pspace != loc->pspace)
7599 continue;
7600
7601 if (loc->loc_type != bp_loc_hardware_breakpoint
7602 && loc->loc_type != bp_loc_software_breakpoint)
7603 continue;
7604
7605 if (is_addr_in_objfile (loc_addr, objfile))
7606 {
7607 loc->shlib_disabled = 1;
7608 /* At this point, we don't know whether the object was
7609 unmapped from the inferior or not, so leave the
7610 inserted flag alone. We'll handle failure to
7611 uninsert quietly, in case the object was indeed
7612 unmapped. */
7613
7614 mark_breakpoint_location_modified (loc);
7615
7616 bp_modified = 1;
7617 }
7618 }
7619
7620 if (bp_modified)
7621 gdb::observers::breakpoint_modified.notify (b);
7622 }
7623 }
7624
7625 /* FORK & VFORK catchpoints. */
7626
7627 /* An instance of this type is used to represent a fork or vfork
7628 catchpoint. A breakpoint is really of this type iff its ops pointer points
7629 to CATCH_FORK_BREAKPOINT_OPS. */
7630
7631 struct fork_catchpoint : public breakpoint
7632 {
7633 /* Process id of a child process whose forking triggered this
7634 catchpoint. This field is only valid immediately after this
7635 catchpoint has triggered. */
7636 ptid_t forked_inferior_pid;
7637 };
7638
7639 /* Implement the "insert" breakpoint_ops method for fork
7640 catchpoints. */
7641
7642 static int
7643 insert_catch_fork (struct bp_location *bl)
7644 {
7645 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7646 }
7647
7648 /* Implement the "remove" breakpoint_ops method for fork
7649 catchpoints. */
7650
7651 static int
7652 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7653 {
7654 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7655 }
7656
7657 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7658 catchpoints. */
7659
7660 static int
7661 breakpoint_hit_catch_fork (const struct bp_location *bl,
7662 const address_space *aspace, CORE_ADDR bp_addr,
7663 const struct target_waitstatus *ws)
7664 {
7665 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7666
7667 if (ws->kind != TARGET_WAITKIND_FORKED)
7668 return 0;
7669
7670 c->forked_inferior_pid = ws->value.related_pid;
7671 return 1;
7672 }
7673
7674 /* Implement the "print_it" breakpoint_ops method for fork
7675 catchpoints. */
7676
7677 static enum print_stop_action
7678 print_it_catch_fork (bpstat bs)
7679 {
7680 struct ui_out *uiout = current_uiout;
7681 struct breakpoint *b = bs->breakpoint_at;
7682 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7683
7684 annotate_catchpoint (b->number);
7685 maybe_print_thread_hit_breakpoint (uiout);
7686 if (b->disposition == disp_del)
7687 uiout->text ("Temporary catchpoint ");
7688 else
7689 uiout->text ("Catchpoint ");
7690 if (uiout->is_mi_like_p ())
7691 {
7692 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7693 uiout->field_string ("disp", bpdisp_text (b->disposition));
7694 }
7695 uiout->field_signed ("bkptno", b->number);
7696 uiout->text (" (forked process ");
7697 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7698 uiout->text ("), ");
7699 return PRINT_SRC_AND_LOC;
7700 }
7701
7702 /* Implement the "print_one" breakpoint_ops method for fork
7703 catchpoints. */
7704
7705 static void
7706 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7707 {
7708 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7709 struct value_print_options opts;
7710 struct ui_out *uiout = current_uiout;
7711
7712 get_user_print_options (&opts);
7713
7714 /* Field 4, the address, is omitted (which makes the columns not
7715 line up too nicely with the headers, but the effect is relatively
7716 readable). */
7717 if (opts.addressprint)
7718 uiout->field_skip ("addr");
7719 annotate_field (5);
7720 uiout->text ("fork");
7721 if (c->forked_inferior_pid != null_ptid)
7722 {
7723 uiout->text (", process ");
7724 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7725 uiout->spaces (1);
7726 }
7727
7728 if (uiout->is_mi_like_p ())
7729 uiout->field_string ("catch-type", "fork");
7730 }
7731
7732 /* Implement the "print_mention" breakpoint_ops method for fork
7733 catchpoints. */
7734
7735 static void
7736 print_mention_catch_fork (struct breakpoint *b)
7737 {
7738 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7739 }
7740
7741 /* Implement the "print_recreate" breakpoint_ops method for fork
7742 catchpoints. */
7743
7744 static void
7745 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7746 {
7747 fprintf_unfiltered (fp, "catch fork");
7748 print_recreate_thread (b, fp);
7749 }
7750
7751 /* The breakpoint_ops structure to be used in fork catchpoints. */
7752
7753 static struct breakpoint_ops catch_fork_breakpoint_ops;
7754
7755 /* Implement the "insert" breakpoint_ops method for vfork
7756 catchpoints. */
7757
7758 static int
7759 insert_catch_vfork (struct bp_location *bl)
7760 {
7761 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7762 }
7763
7764 /* Implement the "remove" breakpoint_ops method for vfork
7765 catchpoints. */
7766
7767 static int
7768 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7769 {
7770 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7771 }
7772
7773 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7774 catchpoints. */
7775
7776 static int
7777 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7778 const address_space *aspace, CORE_ADDR bp_addr,
7779 const struct target_waitstatus *ws)
7780 {
7781 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7782
7783 if (ws->kind != TARGET_WAITKIND_VFORKED)
7784 return 0;
7785
7786 c->forked_inferior_pid = ws->value.related_pid;
7787 return 1;
7788 }
7789
7790 /* Implement the "print_it" breakpoint_ops method for vfork
7791 catchpoints. */
7792
7793 static enum print_stop_action
7794 print_it_catch_vfork (bpstat bs)
7795 {
7796 struct ui_out *uiout = current_uiout;
7797 struct breakpoint *b = bs->breakpoint_at;
7798 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7799
7800 annotate_catchpoint (b->number);
7801 maybe_print_thread_hit_breakpoint (uiout);
7802 if (b->disposition == disp_del)
7803 uiout->text ("Temporary catchpoint ");
7804 else
7805 uiout->text ("Catchpoint ");
7806 if (uiout->is_mi_like_p ())
7807 {
7808 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7809 uiout->field_string ("disp", bpdisp_text (b->disposition));
7810 }
7811 uiout->field_signed ("bkptno", b->number);
7812 uiout->text (" (vforked process ");
7813 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7814 uiout->text ("), ");
7815 return PRINT_SRC_AND_LOC;
7816 }
7817
7818 /* Implement the "print_one" breakpoint_ops method for vfork
7819 catchpoints. */
7820
7821 static void
7822 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7823 {
7824 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7825 struct value_print_options opts;
7826 struct ui_out *uiout = current_uiout;
7827
7828 get_user_print_options (&opts);
7829 /* Field 4, the address, is omitted (which makes the columns not
7830 line up too nicely with the headers, but the effect is relatively
7831 readable). */
7832 if (opts.addressprint)
7833 uiout->field_skip ("addr");
7834 annotate_field (5);
7835 uiout->text ("vfork");
7836 if (c->forked_inferior_pid != null_ptid)
7837 {
7838 uiout->text (", process ");
7839 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7840 uiout->spaces (1);
7841 }
7842
7843 if (uiout->is_mi_like_p ())
7844 uiout->field_string ("catch-type", "vfork");
7845 }
7846
7847 /* Implement the "print_mention" breakpoint_ops method for vfork
7848 catchpoints. */
7849
7850 static void
7851 print_mention_catch_vfork (struct breakpoint *b)
7852 {
7853 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7854 }
7855
7856 /* Implement the "print_recreate" breakpoint_ops method for vfork
7857 catchpoints. */
7858
7859 static void
7860 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7861 {
7862 fprintf_unfiltered (fp, "catch vfork");
7863 print_recreate_thread (b, fp);
7864 }
7865
7866 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7867
7868 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7869
7870 /* An instance of this type is used to represent an solib catchpoint.
7871 A breakpoint is really of this type iff its ops pointer points to
7872 CATCH_SOLIB_BREAKPOINT_OPS. */
7873
7874 struct solib_catchpoint : public breakpoint
7875 {
7876 ~solib_catchpoint () override;
7877
7878 /* True for "catch load", false for "catch unload". */
7879 unsigned char is_load;
7880
7881 /* Regular expression to match, if any. COMPILED is only valid when
7882 REGEX is non-NULL. */
7883 char *regex;
7884 std::unique_ptr<compiled_regex> compiled;
7885 };
7886
7887 solib_catchpoint::~solib_catchpoint ()
7888 {
7889 xfree (this->regex);
7890 }
7891
7892 static int
7893 insert_catch_solib (struct bp_location *ignore)
7894 {
7895 return 0;
7896 }
7897
7898 static int
7899 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7900 {
7901 return 0;
7902 }
7903
7904 static int
7905 breakpoint_hit_catch_solib (const struct bp_location *bl,
7906 const address_space *aspace,
7907 CORE_ADDR bp_addr,
7908 const struct target_waitstatus *ws)
7909 {
7910 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7911 struct breakpoint *other;
7912
7913 if (ws->kind == TARGET_WAITKIND_LOADED)
7914 return 1;
7915
7916 ALL_BREAKPOINTS (other)
7917 {
7918 struct bp_location *other_bl;
7919
7920 if (other == bl->owner)
7921 continue;
7922
7923 if (other->type != bp_shlib_event)
7924 continue;
7925
7926 if (self->pspace != NULL && other->pspace != self->pspace)
7927 continue;
7928
7929 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7930 {
7931 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7932 return 1;
7933 }
7934 }
7935
7936 return 0;
7937 }
7938
7939 static void
7940 check_status_catch_solib (struct bpstats *bs)
7941 {
7942 struct solib_catchpoint *self
7943 = (struct solib_catchpoint *) bs->breakpoint_at;
7944
7945 if (self->is_load)
7946 {
7947 for (so_list *iter : current_program_space->added_solibs)
7948 {
7949 if (!self->regex
7950 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
7951 return;
7952 }
7953 }
7954 else
7955 {
7956 for (const std::string &iter : current_program_space->deleted_solibs)
7957 {
7958 if (!self->regex
7959 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
7960 return;
7961 }
7962 }
7963
7964 bs->stop = 0;
7965 bs->print_it = print_it_noop;
7966 }
7967
7968 static enum print_stop_action
7969 print_it_catch_solib (bpstat bs)
7970 {
7971 struct breakpoint *b = bs->breakpoint_at;
7972 struct ui_out *uiout = current_uiout;
7973
7974 annotate_catchpoint (b->number);
7975 maybe_print_thread_hit_breakpoint (uiout);
7976 if (b->disposition == disp_del)
7977 uiout->text ("Temporary catchpoint ");
7978 else
7979 uiout->text ("Catchpoint ");
7980 uiout->field_signed ("bkptno", b->number);
7981 uiout->text ("\n");
7982 if (uiout->is_mi_like_p ())
7983 uiout->field_string ("disp", bpdisp_text (b->disposition));
7984 print_solib_event (1);
7985 return PRINT_SRC_AND_LOC;
7986 }
7987
7988 static void
7989 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7990 {
7991 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7992 struct value_print_options opts;
7993 struct ui_out *uiout = current_uiout;
7994
7995 get_user_print_options (&opts);
7996 /* Field 4, the address, is omitted (which makes the columns not
7997 line up too nicely with the headers, but the effect is relatively
7998 readable). */
7999 if (opts.addressprint)
8000 {
8001 annotate_field (4);
8002 uiout->field_skip ("addr");
8003 }
8004
8005 std::string msg;
8006 annotate_field (5);
8007 if (self->is_load)
8008 {
8009 if (self->regex)
8010 msg = string_printf (_("load of library matching %s"), self->regex);
8011 else
8012 msg = _("load of library");
8013 }
8014 else
8015 {
8016 if (self->regex)
8017 msg = string_printf (_("unload of library matching %s"), self->regex);
8018 else
8019 msg = _("unload of library");
8020 }
8021 uiout->field_string ("what", msg);
8022
8023 if (uiout->is_mi_like_p ())
8024 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8025 }
8026
8027 static void
8028 print_mention_catch_solib (struct breakpoint *b)
8029 {
8030 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8031
8032 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8033 self->is_load ? "load" : "unload");
8034 }
8035
8036 static void
8037 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8038 {
8039 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8040
8041 fprintf_unfiltered (fp, "%s %s",
8042 b->disposition == disp_del ? "tcatch" : "catch",
8043 self->is_load ? "load" : "unload");
8044 if (self->regex)
8045 fprintf_unfiltered (fp, " %s", self->regex);
8046 fprintf_unfiltered (fp, "\n");
8047 }
8048
8049 static struct breakpoint_ops catch_solib_breakpoint_ops;
8050
8051 /* Shared helper function (MI and CLI) for creating and installing
8052 a shared object event catchpoint. If IS_LOAD is non-zero then
8053 the events to be caught are load events, otherwise they are
8054 unload events. If IS_TEMP is non-zero the catchpoint is a
8055 temporary one. If ENABLED is non-zero the catchpoint is
8056 created in an enabled state. */
8057
8058 void
8059 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8060 {
8061 struct gdbarch *gdbarch = get_current_arch ();
8062
8063 if (!arg)
8064 arg = "";
8065 arg = skip_spaces (arg);
8066
8067 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8068
8069 if (*arg != '\0')
8070 {
8071 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8072 _("Invalid regexp")));
8073 c->regex = xstrdup (arg);
8074 }
8075
8076 c->is_load = is_load;
8077 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8078 &catch_solib_breakpoint_ops);
8079
8080 c->enable_state = enabled ? bp_enabled : bp_disabled;
8081
8082 install_breakpoint (0, std::move (c), 1);
8083 }
8084
8085 /* A helper function that does all the work for "catch load" and
8086 "catch unload". */
8087
8088 static void
8089 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8090 struct cmd_list_element *command)
8091 {
8092 int tempflag;
8093 const int enabled = 1;
8094
8095 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8096
8097 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8098 }
8099
8100 static void
8101 catch_load_command_1 (const char *arg, int from_tty,
8102 struct cmd_list_element *command)
8103 {
8104 catch_load_or_unload (arg, from_tty, 1, command);
8105 }
8106
8107 static void
8108 catch_unload_command_1 (const char *arg, int from_tty,
8109 struct cmd_list_element *command)
8110 {
8111 catch_load_or_unload (arg, from_tty, 0, command);
8112 }
8113
8114 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8115 is non-zero, then make the breakpoint temporary. If COND_STRING is
8116 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8117 the breakpoint_ops structure associated to the catchpoint. */
8118
8119 void
8120 init_catchpoint (struct breakpoint *b,
8121 struct gdbarch *gdbarch, int tempflag,
8122 const char *cond_string,
8123 const struct breakpoint_ops *ops)
8124 {
8125 symtab_and_line sal;
8126 sal.pspace = current_program_space;
8127
8128 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8129
8130 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8131 b->disposition = tempflag ? disp_del : disp_donttouch;
8132 }
8133
8134 void
8135 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8136 {
8137 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8138 set_breakpoint_number (internal, b);
8139 if (is_tracepoint (b))
8140 set_tracepoint_count (breakpoint_count);
8141 if (!internal)
8142 mention (b);
8143 gdb::observers::breakpoint_created.notify (b);
8144
8145 if (update_gll)
8146 update_global_location_list (UGLL_MAY_INSERT);
8147 }
8148
8149 static void
8150 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8151 int tempflag, const char *cond_string,
8152 const struct breakpoint_ops *ops)
8153 {
8154 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8155
8156 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8157
8158 c->forked_inferior_pid = null_ptid;
8159
8160 install_breakpoint (0, std::move (c), 1);
8161 }
8162
8163 /* Exec catchpoints. */
8164
8165 /* An instance of this type is used to represent an exec catchpoint.
8166 A breakpoint is really of this type iff its ops pointer points to
8167 CATCH_EXEC_BREAKPOINT_OPS. */
8168
8169 struct exec_catchpoint : public breakpoint
8170 {
8171 ~exec_catchpoint () override;
8172
8173 /* Filename of a program whose exec triggered this catchpoint.
8174 This field is only valid immediately after this catchpoint has
8175 triggered. */
8176 char *exec_pathname;
8177 };
8178
8179 /* Exec catchpoint destructor. */
8180
8181 exec_catchpoint::~exec_catchpoint ()
8182 {
8183 xfree (this->exec_pathname);
8184 }
8185
8186 static int
8187 insert_catch_exec (struct bp_location *bl)
8188 {
8189 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8190 }
8191
8192 static int
8193 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8194 {
8195 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8196 }
8197
8198 static int
8199 breakpoint_hit_catch_exec (const struct bp_location *bl,
8200 const address_space *aspace, CORE_ADDR bp_addr,
8201 const struct target_waitstatus *ws)
8202 {
8203 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8204
8205 if (ws->kind != TARGET_WAITKIND_EXECD)
8206 return 0;
8207
8208 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8209 return 1;
8210 }
8211
8212 static enum print_stop_action
8213 print_it_catch_exec (bpstat bs)
8214 {
8215 struct ui_out *uiout = current_uiout;
8216 struct breakpoint *b = bs->breakpoint_at;
8217 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8218
8219 annotate_catchpoint (b->number);
8220 maybe_print_thread_hit_breakpoint (uiout);
8221 if (b->disposition == disp_del)
8222 uiout->text ("Temporary catchpoint ");
8223 else
8224 uiout->text ("Catchpoint ");
8225 if (uiout->is_mi_like_p ())
8226 {
8227 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8228 uiout->field_string ("disp", bpdisp_text (b->disposition));
8229 }
8230 uiout->field_signed ("bkptno", b->number);
8231 uiout->text (" (exec'd ");
8232 uiout->field_string ("new-exec", c->exec_pathname);
8233 uiout->text ("), ");
8234
8235 return PRINT_SRC_AND_LOC;
8236 }
8237
8238 static void
8239 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8240 {
8241 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8242 struct value_print_options opts;
8243 struct ui_out *uiout = current_uiout;
8244
8245 get_user_print_options (&opts);
8246
8247 /* Field 4, the address, is omitted (which makes the columns
8248 not line up too nicely with the headers, but the effect
8249 is relatively readable). */
8250 if (opts.addressprint)
8251 uiout->field_skip ("addr");
8252 annotate_field (5);
8253 uiout->text ("exec");
8254 if (c->exec_pathname != NULL)
8255 {
8256 uiout->text (", program \"");
8257 uiout->field_string ("what", c->exec_pathname);
8258 uiout->text ("\" ");
8259 }
8260
8261 if (uiout->is_mi_like_p ())
8262 uiout->field_string ("catch-type", "exec");
8263 }
8264
8265 static void
8266 print_mention_catch_exec (struct breakpoint *b)
8267 {
8268 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8269 }
8270
8271 /* Implement the "print_recreate" breakpoint_ops method for exec
8272 catchpoints. */
8273
8274 static void
8275 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8276 {
8277 fprintf_unfiltered (fp, "catch exec");
8278 print_recreate_thread (b, fp);
8279 }
8280
8281 static struct breakpoint_ops catch_exec_breakpoint_ops;
8282
8283 static int
8284 hw_breakpoint_used_count (void)
8285 {
8286 int i = 0;
8287 struct breakpoint *b;
8288 struct bp_location *bl;
8289
8290 ALL_BREAKPOINTS (b)
8291 {
8292 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8293 for (bl = b->loc; bl; bl = bl->next)
8294 {
8295 /* Special types of hardware breakpoints may use more than
8296 one register. */
8297 i += b->ops->resources_needed (bl);
8298 }
8299 }
8300
8301 return i;
8302 }
8303
8304 /* Returns the resources B would use if it were a hardware
8305 watchpoint. */
8306
8307 static int
8308 hw_watchpoint_use_count (struct breakpoint *b)
8309 {
8310 int i = 0;
8311 struct bp_location *bl;
8312
8313 if (!breakpoint_enabled (b))
8314 return 0;
8315
8316 for (bl = b->loc; bl; bl = bl->next)
8317 {
8318 /* Special types of hardware watchpoints may use more than
8319 one register. */
8320 i += b->ops->resources_needed (bl);
8321 }
8322
8323 return i;
8324 }
8325
8326 /* Returns the sum the used resources of all hardware watchpoints of
8327 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8328 the sum of the used resources of all hardware watchpoints of other
8329 types _not_ TYPE. */
8330
8331 static int
8332 hw_watchpoint_used_count_others (struct breakpoint *except,
8333 enum bptype type, int *other_type_used)
8334 {
8335 int i = 0;
8336 struct breakpoint *b;
8337
8338 *other_type_used = 0;
8339 ALL_BREAKPOINTS (b)
8340 {
8341 if (b == except)
8342 continue;
8343 if (!breakpoint_enabled (b))
8344 continue;
8345
8346 if (b->type == type)
8347 i += hw_watchpoint_use_count (b);
8348 else if (is_hardware_watchpoint (b))
8349 *other_type_used = 1;
8350 }
8351
8352 return i;
8353 }
8354
8355 void
8356 disable_watchpoints_before_interactive_call_start (void)
8357 {
8358 struct breakpoint *b;
8359
8360 ALL_BREAKPOINTS (b)
8361 {
8362 if (is_watchpoint (b) && breakpoint_enabled (b))
8363 {
8364 b->enable_state = bp_call_disabled;
8365 update_global_location_list (UGLL_DONT_INSERT);
8366 }
8367 }
8368 }
8369
8370 void
8371 enable_watchpoints_after_interactive_call_stop (void)
8372 {
8373 struct breakpoint *b;
8374
8375 ALL_BREAKPOINTS (b)
8376 {
8377 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8378 {
8379 b->enable_state = bp_enabled;
8380 update_global_location_list (UGLL_MAY_INSERT);
8381 }
8382 }
8383 }
8384
8385 void
8386 disable_breakpoints_before_startup (void)
8387 {
8388 current_program_space->executing_startup = 1;
8389 update_global_location_list (UGLL_DONT_INSERT);
8390 }
8391
8392 void
8393 enable_breakpoints_after_startup (void)
8394 {
8395 current_program_space->executing_startup = 0;
8396 breakpoint_re_set ();
8397 }
8398
8399 /* Create a new single-step breakpoint for thread THREAD, with no
8400 locations. */
8401
8402 static struct breakpoint *
8403 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8404 {
8405 std::unique_ptr<breakpoint> b (new breakpoint ());
8406
8407 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8408 &momentary_breakpoint_ops);
8409
8410 b->disposition = disp_donttouch;
8411 b->frame_id = null_frame_id;
8412
8413 b->thread = thread;
8414 gdb_assert (b->thread != 0);
8415
8416 return add_to_breakpoint_chain (std::move (b));
8417 }
8418
8419 /* Set a momentary breakpoint of type TYPE at address specified by
8420 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8421 frame. */
8422
8423 breakpoint_up
8424 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8425 struct frame_id frame_id, enum bptype type)
8426 {
8427 struct breakpoint *b;
8428
8429 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8430 tail-called one. */
8431 gdb_assert (!frame_id_artificial_p (frame_id));
8432
8433 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8434 b->enable_state = bp_enabled;
8435 b->disposition = disp_donttouch;
8436 b->frame_id = frame_id;
8437
8438 b->thread = inferior_thread ()->global_num;
8439
8440 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8441
8442 return breakpoint_up (b);
8443 }
8444
8445 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8446 The new breakpoint will have type TYPE, use OPS as its
8447 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8448
8449 static struct breakpoint *
8450 momentary_breakpoint_from_master (struct breakpoint *orig,
8451 enum bptype type,
8452 const struct breakpoint_ops *ops,
8453 int loc_enabled)
8454 {
8455 struct breakpoint *copy;
8456
8457 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8458 copy->loc = allocate_bp_location (copy);
8459 set_breakpoint_location_function (copy->loc);
8460
8461 copy->loc->gdbarch = orig->loc->gdbarch;
8462 copy->loc->requested_address = orig->loc->requested_address;
8463 copy->loc->address = orig->loc->address;
8464 copy->loc->section = orig->loc->section;
8465 copy->loc->pspace = orig->loc->pspace;
8466 copy->loc->probe = orig->loc->probe;
8467 copy->loc->line_number = orig->loc->line_number;
8468 copy->loc->symtab = orig->loc->symtab;
8469 copy->loc->enabled = loc_enabled;
8470 copy->frame_id = orig->frame_id;
8471 copy->thread = orig->thread;
8472 copy->pspace = orig->pspace;
8473
8474 copy->enable_state = bp_enabled;
8475 copy->disposition = disp_donttouch;
8476 copy->number = internal_breakpoint_number--;
8477
8478 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8479 return copy;
8480 }
8481
8482 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8483 ORIG is NULL. */
8484
8485 struct breakpoint *
8486 clone_momentary_breakpoint (struct breakpoint *orig)
8487 {
8488 /* If there's nothing to clone, then return nothing. */
8489 if (orig == NULL)
8490 return NULL;
8491
8492 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8493 }
8494
8495 breakpoint_up
8496 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8497 enum bptype type)
8498 {
8499 struct symtab_and_line sal;
8500
8501 sal = find_pc_line (pc, 0);
8502 sal.pc = pc;
8503 sal.section = find_pc_overlay (pc);
8504 sal.explicit_pc = 1;
8505
8506 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8507 }
8508 \f
8509
8510 /* Tell the user we have just set a breakpoint B. */
8511
8512 static void
8513 mention (struct breakpoint *b)
8514 {
8515 b->ops->print_mention (b);
8516 if (current_uiout->is_mi_like_p ())
8517 return;
8518 printf_filtered ("\n");
8519 }
8520 \f
8521
8522 static int bp_loc_is_permanent (struct bp_location *loc);
8523
8524 static struct bp_location *
8525 add_location_to_breakpoint (struct breakpoint *b,
8526 const struct symtab_and_line *sal)
8527 {
8528 struct bp_location *loc, **tmp;
8529 CORE_ADDR adjusted_address;
8530 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8531
8532 if (loc_gdbarch == NULL)
8533 loc_gdbarch = b->gdbarch;
8534
8535 /* Adjust the breakpoint's address prior to allocating a location.
8536 Once we call allocate_bp_location(), that mostly uninitialized
8537 location will be placed on the location chain. Adjustment of the
8538 breakpoint may cause target_read_memory() to be called and we do
8539 not want its scan of the location chain to find a breakpoint and
8540 location that's only been partially initialized. */
8541 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8542 sal->pc, b->type);
8543
8544 /* Sort the locations by their ADDRESS. */
8545 loc = allocate_bp_location (b);
8546 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8547 tmp = &((*tmp)->next))
8548 ;
8549 loc->next = *tmp;
8550 *tmp = loc;
8551
8552 loc->requested_address = sal->pc;
8553 loc->address = adjusted_address;
8554 loc->pspace = sal->pspace;
8555 loc->probe.prob = sal->prob;
8556 loc->probe.objfile = sal->objfile;
8557 gdb_assert (loc->pspace != NULL);
8558 loc->section = sal->section;
8559 loc->gdbarch = loc_gdbarch;
8560 loc->line_number = sal->line;
8561 loc->symtab = sal->symtab;
8562 loc->symbol = sal->symbol;
8563 loc->msymbol = sal->msymbol;
8564 loc->objfile = sal->objfile;
8565
8566 set_breakpoint_location_function (loc);
8567
8568 /* While by definition, permanent breakpoints are already present in the
8569 code, we don't mark the location as inserted. Normally one would expect
8570 that GDB could rely on that breakpoint instruction to stop the program,
8571 thus removing the need to insert its own breakpoint, except that executing
8572 the breakpoint instruction can kill the target instead of reporting a
8573 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8574 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8575 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8576 breakpoint be inserted normally results in QEMU knowing about the GDB
8577 breakpoint, and thus trap before the breakpoint instruction is executed.
8578 (If GDB later needs to continue execution past the permanent breakpoint,
8579 it manually increments the PC, thus avoiding executing the breakpoint
8580 instruction.) */
8581 if (bp_loc_is_permanent (loc))
8582 loc->permanent = 1;
8583
8584 return loc;
8585 }
8586 \f
8587
8588 /* See breakpoint.h. */
8589
8590 int
8591 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8592 {
8593 int len;
8594 CORE_ADDR addr;
8595 const gdb_byte *bpoint;
8596 gdb_byte *target_mem;
8597
8598 addr = address;
8599 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8600
8601 /* Software breakpoints unsupported? */
8602 if (bpoint == NULL)
8603 return 0;
8604
8605 target_mem = (gdb_byte *) alloca (len);
8606
8607 /* Enable the automatic memory restoration from breakpoints while
8608 we read the memory. Otherwise we could say about our temporary
8609 breakpoints they are permanent. */
8610 scoped_restore restore_memory
8611 = make_scoped_restore_show_memory_breakpoints (0);
8612
8613 if (target_read_memory (address, target_mem, len) == 0
8614 && memcmp (target_mem, bpoint, len) == 0)
8615 return 1;
8616
8617 return 0;
8618 }
8619
8620 /* Return 1 if LOC is pointing to a permanent breakpoint,
8621 return 0 otherwise. */
8622
8623 static int
8624 bp_loc_is_permanent (struct bp_location *loc)
8625 {
8626 gdb_assert (loc != NULL);
8627
8628 /* If we have a non-breakpoint-backed catchpoint or a software
8629 watchpoint, just return 0. We should not attempt to read from
8630 the addresses the locations of these breakpoint types point to.
8631 program_breakpoint_here_p, below, will attempt to read
8632 memory. */
8633 if (!bl_address_is_meaningful (loc))
8634 return 0;
8635
8636 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8637 switch_to_program_space_and_thread (loc->pspace);
8638 return program_breakpoint_here_p (loc->gdbarch, loc->address);
8639 }
8640
8641 /* Build a command list for the dprintf corresponding to the current
8642 settings of the dprintf style options. */
8643
8644 static void
8645 update_dprintf_command_list (struct breakpoint *b)
8646 {
8647 char *dprintf_args = b->extra_string;
8648 char *printf_line = NULL;
8649
8650 if (!dprintf_args)
8651 return;
8652
8653 dprintf_args = skip_spaces (dprintf_args);
8654
8655 /* Allow a comma, as it may have terminated a location, but don't
8656 insist on it. */
8657 if (*dprintf_args == ',')
8658 ++dprintf_args;
8659 dprintf_args = skip_spaces (dprintf_args);
8660
8661 if (*dprintf_args != '"')
8662 error (_("Bad format string, missing '\"'."));
8663
8664 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8665 printf_line = xstrprintf ("printf %s", dprintf_args);
8666 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8667 {
8668 if (!dprintf_function)
8669 error (_("No function supplied for dprintf call"));
8670
8671 if (dprintf_channel && strlen (dprintf_channel) > 0)
8672 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8673 dprintf_function,
8674 dprintf_channel,
8675 dprintf_args);
8676 else
8677 printf_line = xstrprintf ("call (void) %s (%s)",
8678 dprintf_function,
8679 dprintf_args);
8680 }
8681 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8682 {
8683 if (target_can_run_breakpoint_commands ())
8684 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8685 else
8686 {
8687 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8688 printf_line = xstrprintf ("printf %s", dprintf_args);
8689 }
8690 }
8691 else
8692 internal_error (__FILE__, __LINE__,
8693 _("Invalid dprintf style."));
8694
8695 gdb_assert (printf_line != NULL);
8696
8697 /* Manufacture a printf sequence. */
8698 struct command_line *printf_cmd_line
8699 = new struct command_line (simple_control, printf_line);
8700 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8701 command_lines_deleter ()));
8702 }
8703
8704 /* Update all dprintf commands, making their command lists reflect
8705 current style settings. */
8706
8707 static void
8708 update_dprintf_commands (const char *args, int from_tty,
8709 struct cmd_list_element *c)
8710 {
8711 struct breakpoint *b;
8712
8713 ALL_BREAKPOINTS (b)
8714 {
8715 if (b->type == bp_dprintf)
8716 update_dprintf_command_list (b);
8717 }
8718 }
8719
8720 /* Create a breakpoint with SAL as location. Use LOCATION
8721 as a description of the location, and COND_STRING
8722 as condition expression. If LOCATION is NULL then create an
8723 "address location" from the address in the SAL. */
8724
8725 static void
8726 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8727 gdb::array_view<const symtab_and_line> sals,
8728 event_location_up &&location,
8729 gdb::unique_xmalloc_ptr<char> filter,
8730 gdb::unique_xmalloc_ptr<char> cond_string,
8731 gdb::unique_xmalloc_ptr<char> extra_string,
8732 enum bptype type, enum bpdisp disposition,
8733 int thread, int task, int ignore_count,
8734 const struct breakpoint_ops *ops, int from_tty,
8735 int enabled, int internal, unsigned flags,
8736 int display_canonical)
8737 {
8738 int i;
8739
8740 if (type == bp_hardware_breakpoint)
8741 {
8742 int target_resources_ok;
8743
8744 i = hw_breakpoint_used_count ();
8745 target_resources_ok =
8746 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8747 i + 1, 0);
8748 if (target_resources_ok == 0)
8749 error (_("No hardware breakpoint support in the target."));
8750 else if (target_resources_ok < 0)
8751 error (_("Hardware breakpoints used exceeds limit."));
8752 }
8753
8754 gdb_assert (!sals.empty ());
8755
8756 for (const auto &sal : sals)
8757 {
8758 struct bp_location *loc;
8759
8760 if (from_tty)
8761 {
8762 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8763 if (!loc_gdbarch)
8764 loc_gdbarch = gdbarch;
8765
8766 describe_other_breakpoints (loc_gdbarch,
8767 sal.pspace, sal.pc, sal.section, thread);
8768 }
8769
8770 if (&sal == &sals[0])
8771 {
8772 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8773 b->thread = thread;
8774 b->task = task;
8775
8776 b->cond_string = cond_string.release ();
8777 b->extra_string = extra_string.release ();
8778 b->ignore_count = ignore_count;
8779 b->enable_state = enabled ? bp_enabled : bp_disabled;
8780 b->disposition = disposition;
8781
8782 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8783 b->loc->inserted = 1;
8784
8785 if (type == bp_static_tracepoint)
8786 {
8787 struct tracepoint *t = (struct tracepoint *) b;
8788 struct static_tracepoint_marker marker;
8789
8790 if (strace_marker_p (b))
8791 {
8792 /* We already know the marker exists, otherwise, we
8793 wouldn't see a sal for it. */
8794 const char *p
8795 = &event_location_to_string (b->location.get ())[3];
8796 const char *endp;
8797
8798 p = skip_spaces (p);
8799
8800 endp = skip_to_space (p);
8801
8802 t->static_trace_marker_id.assign (p, endp - p);
8803
8804 printf_filtered (_("Probed static tracepoint "
8805 "marker \"%s\"\n"),
8806 t->static_trace_marker_id.c_str ());
8807 }
8808 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8809 {
8810 t->static_trace_marker_id = std::move (marker.str_id);
8811
8812 printf_filtered (_("Probed static tracepoint "
8813 "marker \"%s\"\n"),
8814 t->static_trace_marker_id.c_str ());
8815 }
8816 else
8817 warning (_("Couldn't determine the static "
8818 "tracepoint marker to probe"));
8819 }
8820
8821 loc = b->loc;
8822 }
8823 else
8824 {
8825 loc = add_location_to_breakpoint (b, &sal);
8826 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8827 loc->inserted = 1;
8828 }
8829
8830 if (b->cond_string)
8831 {
8832 const char *arg = b->cond_string;
8833
8834 loc->cond = parse_exp_1 (&arg, loc->address,
8835 block_for_pc (loc->address), 0);
8836 if (*arg)
8837 error (_("Garbage '%s' follows condition"), arg);
8838 }
8839
8840 /* Dynamic printf requires and uses additional arguments on the
8841 command line, otherwise it's an error. */
8842 if (type == bp_dprintf)
8843 {
8844 if (b->extra_string)
8845 update_dprintf_command_list (b);
8846 else
8847 error (_("Format string required"));
8848 }
8849 else if (b->extra_string)
8850 error (_("Garbage '%s' at end of command"), b->extra_string);
8851 }
8852
8853 b->display_canonical = display_canonical;
8854 if (location != NULL)
8855 b->location = std::move (location);
8856 else
8857 b->location = new_address_location (b->loc->address, NULL, 0);
8858 b->filter = std::move (filter);
8859 }
8860
8861 static void
8862 create_breakpoint_sal (struct gdbarch *gdbarch,
8863 gdb::array_view<const symtab_and_line> sals,
8864 event_location_up &&location,
8865 gdb::unique_xmalloc_ptr<char> filter,
8866 gdb::unique_xmalloc_ptr<char> cond_string,
8867 gdb::unique_xmalloc_ptr<char> extra_string,
8868 enum bptype type, enum bpdisp disposition,
8869 int thread, int task, int ignore_count,
8870 const struct breakpoint_ops *ops, int from_tty,
8871 int enabled, int internal, unsigned flags,
8872 int display_canonical)
8873 {
8874 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8875
8876 init_breakpoint_sal (b.get (), gdbarch,
8877 sals, std::move (location),
8878 std::move (filter),
8879 std::move (cond_string),
8880 std::move (extra_string),
8881 type, disposition,
8882 thread, task, ignore_count,
8883 ops, from_tty,
8884 enabled, internal, flags,
8885 display_canonical);
8886
8887 install_breakpoint (internal, std::move (b), 0);
8888 }
8889
8890 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8891 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8892 value. COND_STRING, if not NULL, specified the condition to be
8893 used for all breakpoints. Essentially the only case where
8894 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8895 function. In that case, it's still not possible to specify
8896 separate conditions for different overloaded functions, so
8897 we take just a single condition string.
8898
8899 NOTE: If the function succeeds, the caller is expected to cleanup
8900 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8901 array contents). If the function fails (error() is called), the
8902 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8903 COND and SALS arrays and each of those arrays contents. */
8904
8905 static void
8906 create_breakpoints_sal (struct gdbarch *gdbarch,
8907 struct linespec_result *canonical,
8908 gdb::unique_xmalloc_ptr<char> cond_string,
8909 gdb::unique_xmalloc_ptr<char> extra_string,
8910 enum bptype type, enum bpdisp disposition,
8911 int thread, int task, int ignore_count,
8912 const struct breakpoint_ops *ops, int from_tty,
8913 int enabled, int internal, unsigned flags)
8914 {
8915 if (canonical->pre_expanded)
8916 gdb_assert (canonical->lsals.size () == 1);
8917
8918 for (const auto &lsal : canonical->lsals)
8919 {
8920 /* Note that 'location' can be NULL in the case of a plain
8921 'break', without arguments. */
8922 event_location_up location
8923 = (canonical->location != NULL
8924 ? copy_event_location (canonical->location.get ()) : NULL);
8925 gdb::unique_xmalloc_ptr<char> filter_string
8926 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8927
8928 create_breakpoint_sal (gdbarch, lsal.sals,
8929 std::move (location),
8930 std::move (filter_string),
8931 std::move (cond_string),
8932 std::move (extra_string),
8933 type, disposition,
8934 thread, task, ignore_count, ops,
8935 from_tty, enabled, internal, flags,
8936 canonical->special_display);
8937 }
8938 }
8939
8940 /* Parse LOCATION which is assumed to be a SAL specification possibly
8941 followed by conditionals. On return, SALS contains an array of SAL
8942 addresses found. LOCATION points to the end of the SAL (for
8943 linespec locations).
8944
8945 The array and the line spec strings are allocated on the heap, it is
8946 the caller's responsibility to free them. */
8947
8948 static void
8949 parse_breakpoint_sals (const struct event_location *location,
8950 struct linespec_result *canonical)
8951 {
8952 struct symtab_and_line cursal;
8953
8954 if (event_location_type (location) == LINESPEC_LOCATION)
8955 {
8956 const char *spec = get_linespec_location (location)->spec_string;
8957
8958 if (spec == NULL)
8959 {
8960 /* The last displayed codepoint, if it's valid, is our default
8961 breakpoint address. */
8962 if (last_displayed_sal_is_valid ())
8963 {
8964 /* Set sal's pspace, pc, symtab, and line to the values
8965 corresponding to the last call to print_frame_info.
8966 Be sure to reinitialize LINE with NOTCURRENT == 0
8967 as the breakpoint line number is inappropriate otherwise.
8968 find_pc_line would adjust PC, re-set it back. */
8969 symtab_and_line sal = get_last_displayed_sal ();
8970 CORE_ADDR pc = sal.pc;
8971
8972 sal = find_pc_line (pc, 0);
8973
8974 /* "break" without arguments is equivalent to "break *PC"
8975 where PC is the last displayed codepoint's address. So
8976 make sure to set sal.explicit_pc to prevent GDB from
8977 trying to expand the list of sals to include all other
8978 instances with the same symtab and line. */
8979 sal.pc = pc;
8980 sal.explicit_pc = 1;
8981
8982 struct linespec_sals lsal;
8983 lsal.sals = {sal};
8984 lsal.canonical = NULL;
8985
8986 canonical->lsals.push_back (std::move (lsal));
8987 return;
8988 }
8989 else
8990 error (_("No default breakpoint address now."));
8991 }
8992 }
8993
8994 /* Force almost all breakpoints to be in terms of the
8995 current_source_symtab (which is decode_line_1's default).
8996 This should produce the results we want almost all of the
8997 time while leaving default_breakpoint_* alone.
8998
8999 ObjC: However, don't match an Objective-C method name which
9000 may have a '+' or '-' succeeded by a '['. */
9001 cursal = get_current_source_symtab_and_line ();
9002 if (last_displayed_sal_is_valid ())
9003 {
9004 const char *spec = NULL;
9005
9006 if (event_location_type (location) == LINESPEC_LOCATION)
9007 spec = get_linespec_location (location)->spec_string;
9008
9009 if (!cursal.symtab
9010 || (spec != NULL
9011 && strchr ("+-", spec[0]) != NULL
9012 && spec[1] != '['))
9013 {
9014 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9015 get_last_displayed_symtab (),
9016 get_last_displayed_line (),
9017 canonical, NULL, NULL);
9018 return;
9019 }
9020 }
9021
9022 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9023 cursal.symtab, cursal.line, canonical, NULL, NULL);
9024 }
9025
9026
9027 /* Convert each SAL into a real PC. Verify that the PC can be
9028 inserted as a breakpoint. If it can't throw an error. */
9029
9030 static void
9031 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9032 {
9033 for (auto &sal : sals)
9034 resolve_sal_pc (&sal);
9035 }
9036
9037 /* Fast tracepoints may have restrictions on valid locations. For
9038 instance, a fast tracepoint using a jump instead of a trap will
9039 likely have to overwrite more bytes than a trap would, and so can
9040 only be placed where the instruction is longer than the jump, or a
9041 multi-instruction sequence does not have a jump into the middle of
9042 it, etc. */
9043
9044 static void
9045 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9046 gdb::array_view<const symtab_and_line> sals)
9047 {
9048 for (const auto &sal : sals)
9049 {
9050 struct gdbarch *sarch;
9051
9052 sarch = get_sal_arch (sal);
9053 /* We fall back to GDBARCH if there is no architecture
9054 associated with SAL. */
9055 if (sarch == NULL)
9056 sarch = gdbarch;
9057 std::string msg;
9058 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9059 error (_("May not have a fast tracepoint at %s%s"),
9060 paddress (sarch, sal.pc), msg.c_str ());
9061 }
9062 }
9063
9064 /* Given TOK, a string specification of condition and thread, as
9065 accepted by the 'break' command, extract the condition
9066 string and thread number and set *COND_STRING and *THREAD.
9067 PC identifies the context at which the condition should be parsed.
9068 If no condition is found, *COND_STRING is set to NULL.
9069 If no thread is found, *THREAD is set to -1. */
9070
9071 static void
9072 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9073 char **cond_string, int *thread, int *task,
9074 char **rest)
9075 {
9076 *cond_string = NULL;
9077 *thread = -1;
9078 *task = 0;
9079 *rest = NULL;
9080
9081 while (tok && *tok)
9082 {
9083 const char *end_tok;
9084 int toklen;
9085 const char *cond_start = NULL;
9086 const char *cond_end = NULL;
9087
9088 tok = skip_spaces (tok);
9089
9090 if ((*tok == '"' || *tok == ',') && rest)
9091 {
9092 *rest = savestring (tok, strlen (tok));
9093 return;
9094 }
9095
9096 end_tok = skip_to_space (tok);
9097
9098 toklen = end_tok - tok;
9099
9100 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9101 {
9102 tok = cond_start = end_tok + 1;
9103 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9104 cond_end = tok;
9105 *cond_string = savestring (cond_start, cond_end - cond_start);
9106 }
9107 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9108 {
9109 const char *tmptok;
9110 struct thread_info *thr;
9111
9112 tok = end_tok + 1;
9113 thr = parse_thread_id (tok, &tmptok);
9114 if (tok == tmptok)
9115 error (_("Junk after thread keyword."));
9116 *thread = thr->global_num;
9117 tok = tmptok;
9118 }
9119 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9120 {
9121 char *tmptok;
9122
9123 tok = end_tok + 1;
9124 *task = strtol (tok, &tmptok, 0);
9125 if (tok == tmptok)
9126 error (_("Junk after task keyword."));
9127 if (!valid_task_id (*task))
9128 error (_("Unknown task %d."), *task);
9129 tok = tmptok;
9130 }
9131 else if (rest)
9132 {
9133 *rest = savestring (tok, strlen (tok));
9134 return;
9135 }
9136 else
9137 error (_("Junk at end of arguments."));
9138 }
9139 }
9140
9141 /* Decode a static tracepoint marker spec. */
9142
9143 static std::vector<symtab_and_line>
9144 decode_static_tracepoint_spec (const char **arg_p)
9145 {
9146 const char *p = &(*arg_p)[3];
9147 const char *endp;
9148
9149 p = skip_spaces (p);
9150
9151 endp = skip_to_space (p);
9152
9153 std::string marker_str (p, endp - p);
9154
9155 std::vector<static_tracepoint_marker> markers
9156 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9157 if (markers.empty ())
9158 error (_("No known static tracepoint marker named %s"),
9159 marker_str.c_str ());
9160
9161 std::vector<symtab_and_line> sals;
9162 sals.reserve (markers.size ());
9163
9164 for (const static_tracepoint_marker &marker : markers)
9165 {
9166 symtab_and_line sal = find_pc_line (marker.address, 0);
9167 sal.pc = marker.address;
9168 sals.push_back (sal);
9169 }
9170
9171 *arg_p = endp;
9172 return sals;
9173 }
9174
9175 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9176 according to IS_TRACEPOINT. */
9177
9178 static const struct breakpoint_ops *
9179 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9180 bool is_tracepoint)
9181 {
9182 if (is_tracepoint)
9183 {
9184 if (location_type == PROBE_LOCATION)
9185 return &tracepoint_probe_breakpoint_ops;
9186 else
9187 return &tracepoint_breakpoint_ops;
9188 }
9189 else
9190 {
9191 if (location_type == PROBE_LOCATION)
9192 return &bkpt_probe_breakpoint_ops;
9193 else
9194 return &bkpt_breakpoint_ops;
9195 }
9196 }
9197
9198 /* See breakpoint.h. */
9199
9200 const struct breakpoint_ops *
9201 breakpoint_ops_for_event_location (const struct event_location *location,
9202 bool is_tracepoint)
9203 {
9204 if (location != nullptr)
9205 return breakpoint_ops_for_event_location_type
9206 (event_location_type (location), is_tracepoint);
9207 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9208 }
9209
9210 /* See breakpoint.h. */
9211
9212 int
9213 create_breakpoint (struct gdbarch *gdbarch,
9214 const struct event_location *location,
9215 const char *cond_string,
9216 int thread, const char *extra_string,
9217 int parse_extra,
9218 int tempflag, enum bptype type_wanted,
9219 int ignore_count,
9220 enum auto_boolean pending_break_support,
9221 const struct breakpoint_ops *ops,
9222 int from_tty, int enabled, int internal,
9223 unsigned flags)
9224 {
9225 struct linespec_result canonical;
9226 int pending = 0;
9227 int task = 0;
9228 int prev_bkpt_count = breakpoint_count;
9229
9230 gdb_assert (ops != NULL);
9231
9232 /* If extra_string isn't useful, set it to NULL. */
9233 if (extra_string != NULL && *extra_string == '\0')
9234 extra_string = NULL;
9235
9236 try
9237 {
9238 ops->create_sals_from_location (location, &canonical, type_wanted);
9239 }
9240 catch (const gdb_exception_error &e)
9241 {
9242 /* If caller is interested in rc value from parse, set
9243 value. */
9244 if (e.error == NOT_FOUND_ERROR)
9245 {
9246 /* If pending breakpoint support is turned off, throw
9247 error. */
9248
9249 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9250 throw;
9251
9252 exception_print (gdb_stderr, e);
9253
9254 /* If pending breakpoint support is auto query and the user
9255 selects no, then simply return the error code. */
9256 if (pending_break_support == AUTO_BOOLEAN_AUTO
9257 && !nquery (_("Make %s pending on future shared library load? "),
9258 bptype_string (type_wanted)))
9259 return 0;
9260
9261 /* At this point, either the user was queried about setting
9262 a pending breakpoint and selected yes, or pending
9263 breakpoint behavior is on and thus a pending breakpoint
9264 is defaulted on behalf of the user. */
9265 pending = 1;
9266 }
9267 else
9268 throw;
9269 }
9270
9271 if (!pending && canonical.lsals.empty ())
9272 return 0;
9273
9274 /* Resolve all line numbers to PC's and verify that the addresses
9275 are ok for the target. */
9276 if (!pending)
9277 {
9278 for (auto &lsal : canonical.lsals)
9279 breakpoint_sals_to_pc (lsal.sals);
9280 }
9281
9282 /* Fast tracepoints may have additional restrictions on location. */
9283 if (!pending && type_wanted == bp_fast_tracepoint)
9284 {
9285 for (const auto &lsal : canonical.lsals)
9286 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9287 }
9288
9289 /* Verify that condition can be parsed, before setting any
9290 breakpoints. Allocate a separate condition expression for each
9291 breakpoint. */
9292 if (!pending)
9293 {
9294 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9295 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9296
9297 if (parse_extra)
9298 {
9299 char *rest;
9300 char *cond;
9301
9302 const linespec_sals &lsal = canonical.lsals[0];
9303
9304 /* Here we only parse 'arg' to separate condition
9305 from thread number, so parsing in context of first
9306 sal is OK. When setting the breakpoint we'll
9307 re-parse it in context of each sal. */
9308
9309 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9310 &cond, &thread, &task, &rest);
9311 cond_string_copy.reset (cond);
9312 extra_string_copy.reset (rest);
9313 }
9314 else
9315 {
9316 if (type_wanted != bp_dprintf
9317 && extra_string != NULL && *extra_string != '\0')
9318 error (_("Garbage '%s' at end of location"), extra_string);
9319
9320 /* Create a private copy of condition string. */
9321 if (cond_string)
9322 cond_string_copy.reset (xstrdup (cond_string));
9323 /* Create a private copy of any extra string. */
9324 if (extra_string)
9325 extra_string_copy.reset (xstrdup (extra_string));
9326 }
9327
9328 ops->create_breakpoints_sal (gdbarch, &canonical,
9329 std::move (cond_string_copy),
9330 std::move (extra_string_copy),
9331 type_wanted,
9332 tempflag ? disp_del : disp_donttouch,
9333 thread, task, ignore_count, ops,
9334 from_tty, enabled, internal, flags);
9335 }
9336 else
9337 {
9338 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9339
9340 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9341 b->location = copy_event_location (location);
9342
9343 if (parse_extra)
9344 b->cond_string = NULL;
9345 else
9346 {
9347 /* Create a private copy of condition string. */
9348 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9349 b->thread = thread;
9350 }
9351
9352 /* Create a private copy of any extra string. */
9353 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9354 b->ignore_count = ignore_count;
9355 b->disposition = tempflag ? disp_del : disp_donttouch;
9356 b->condition_not_parsed = 1;
9357 b->enable_state = enabled ? bp_enabled : bp_disabled;
9358 if ((type_wanted != bp_breakpoint
9359 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9360 b->pspace = current_program_space;
9361
9362 install_breakpoint (internal, std::move (b), 0);
9363 }
9364
9365 if (canonical.lsals.size () > 1)
9366 {
9367 warning (_("Multiple breakpoints were set.\nUse the "
9368 "\"delete\" command to delete unwanted breakpoints."));
9369 prev_breakpoint_count = prev_bkpt_count;
9370 }
9371
9372 update_global_location_list (UGLL_MAY_INSERT);
9373
9374 return 1;
9375 }
9376
9377 /* Set a breakpoint.
9378 ARG is a string describing breakpoint address,
9379 condition, and thread.
9380 FLAG specifies if a breakpoint is hardware on,
9381 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9382 and BP_TEMPFLAG. */
9383
9384 static void
9385 break_command_1 (const char *arg, int flag, int from_tty)
9386 {
9387 int tempflag = flag & BP_TEMPFLAG;
9388 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9389 ? bp_hardware_breakpoint
9390 : bp_breakpoint);
9391
9392 event_location_up location = string_to_event_location (&arg, current_language);
9393 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9394 (location.get (), false /* is_tracepoint */);
9395
9396 create_breakpoint (get_current_arch (),
9397 location.get (),
9398 NULL, 0, arg, 1 /* parse arg */,
9399 tempflag, type_wanted,
9400 0 /* Ignore count */,
9401 pending_break_support,
9402 ops,
9403 from_tty,
9404 1 /* enabled */,
9405 0 /* internal */,
9406 0);
9407 }
9408
9409 /* Helper function for break_command_1 and disassemble_command. */
9410
9411 void
9412 resolve_sal_pc (struct symtab_and_line *sal)
9413 {
9414 CORE_ADDR pc;
9415
9416 if (sal->pc == 0 && sal->symtab != NULL)
9417 {
9418 if (!find_line_pc (sal->symtab, sal->line, &pc))
9419 error (_("No line %d in file \"%s\"."),
9420 sal->line, symtab_to_filename_for_display (sal->symtab));
9421 sal->pc = pc;
9422
9423 /* If this SAL corresponds to a breakpoint inserted using a line
9424 number, then skip the function prologue if necessary. */
9425 if (sal->explicit_line)
9426 skip_prologue_sal (sal);
9427 }
9428
9429 if (sal->section == 0 && sal->symtab != NULL)
9430 {
9431 const struct blockvector *bv;
9432 const struct block *b;
9433 struct symbol *sym;
9434
9435 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9436 SYMTAB_COMPUNIT (sal->symtab));
9437 if (bv != NULL)
9438 {
9439 sym = block_linkage_function (b);
9440 if (sym != NULL)
9441 {
9442 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9443 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9444 sym);
9445 }
9446 else
9447 {
9448 /* It really is worthwhile to have the section, so we'll
9449 just have to look harder. This case can be executed
9450 if we have line numbers but no functions (as can
9451 happen in assembly source). */
9452
9453 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9454 switch_to_program_space_and_thread (sal->pspace);
9455
9456 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9457 if (msym.minsym)
9458 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9459 }
9460 }
9461 }
9462 }
9463
9464 void
9465 break_command (const char *arg, int from_tty)
9466 {
9467 break_command_1 (arg, 0, from_tty);
9468 }
9469
9470 void
9471 tbreak_command (const char *arg, int from_tty)
9472 {
9473 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9474 }
9475
9476 static void
9477 hbreak_command (const char *arg, int from_tty)
9478 {
9479 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9480 }
9481
9482 static void
9483 thbreak_command (const char *arg, int from_tty)
9484 {
9485 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9486 }
9487
9488 static void
9489 stop_command (const char *arg, int from_tty)
9490 {
9491 printf_filtered (_("Specify the type of breakpoint to set.\n\
9492 Usage: stop in <function | address>\n\
9493 stop at <line>\n"));
9494 }
9495
9496 static void
9497 stopin_command (const char *arg, int from_tty)
9498 {
9499 int badInput = 0;
9500
9501 if (arg == NULL)
9502 badInput = 1;
9503 else if (*arg != '*')
9504 {
9505 const char *argptr = arg;
9506 int hasColon = 0;
9507
9508 /* Look for a ':'. If this is a line number specification, then
9509 say it is bad, otherwise, it should be an address or
9510 function/method name. */
9511 while (*argptr && !hasColon)
9512 {
9513 hasColon = (*argptr == ':');
9514 argptr++;
9515 }
9516
9517 if (hasColon)
9518 badInput = (*argptr != ':'); /* Not a class::method */
9519 else
9520 badInput = isdigit (*arg); /* a simple line number */
9521 }
9522
9523 if (badInput)
9524 printf_filtered (_("Usage: stop in <function | address>\n"));
9525 else
9526 break_command_1 (arg, 0, from_tty);
9527 }
9528
9529 static void
9530 stopat_command (const char *arg, int from_tty)
9531 {
9532 int badInput = 0;
9533
9534 if (arg == NULL || *arg == '*') /* no line number */
9535 badInput = 1;
9536 else
9537 {
9538 const char *argptr = arg;
9539 int hasColon = 0;
9540
9541 /* Look for a ':'. If there is a '::' then get out, otherwise
9542 it is probably a line number. */
9543 while (*argptr && !hasColon)
9544 {
9545 hasColon = (*argptr == ':');
9546 argptr++;
9547 }
9548
9549 if (hasColon)
9550 badInput = (*argptr == ':'); /* we have class::method */
9551 else
9552 badInput = !isdigit (*arg); /* not a line number */
9553 }
9554
9555 if (badInput)
9556 printf_filtered (_("Usage: stop at LINE\n"));
9557 else
9558 break_command_1 (arg, 0, from_tty);
9559 }
9560
9561 /* The dynamic printf command is mostly like a regular breakpoint, but
9562 with a prewired command list consisting of a single output command,
9563 built from extra arguments supplied on the dprintf command
9564 line. */
9565
9566 static void
9567 dprintf_command (const char *arg, int from_tty)
9568 {
9569 event_location_up location = string_to_event_location (&arg, current_language);
9570
9571 /* If non-NULL, ARG should have been advanced past the location;
9572 the next character must be ','. */
9573 if (arg != NULL)
9574 {
9575 if (arg[0] != ',' || arg[1] == '\0')
9576 error (_("Format string required"));
9577 else
9578 {
9579 /* Skip the comma. */
9580 ++arg;
9581 }
9582 }
9583
9584 create_breakpoint (get_current_arch (),
9585 location.get (),
9586 NULL, 0, arg, 1 /* parse arg */,
9587 0, bp_dprintf,
9588 0 /* Ignore count */,
9589 pending_break_support,
9590 &dprintf_breakpoint_ops,
9591 from_tty,
9592 1 /* enabled */,
9593 0 /* internal */,
9594 0);
9595 }
9596
9597 static void
9598 agent_printf_command (const char *arg, int from_tty)
9599 {
9600 error (_("May only run agent-printf on the target"));
9601 }
9602
9603 /* Implement the "breakpoint_hit" breakpoint_ops method for
9604 ranged breakpoints. */
9605
9606 static int
9607 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9608 const address_space *aspace,
9609 CORE_ADDR bp_addr,
9610 const struct target_waitstatus *ws)
9611 {
9612 if (ws->kind != TARGET_WAITKIND_STOPPED
9613 || ws->value.sig != GDB_SIGNAL_TRAP)
9614 return 0;
9615
9616 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9617 bl->length, aspace, bp_addr);
9618 }
9619
9620 /* Implement the "resources_needed" breakpoint_ops method for
9621 ranged breakpoints. */
9622
9623 static int
9624 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9625 {
9626 return target_ranged_break_num_registers ();
9627 }
9628
9629 /* Implement the "print_it" breakpoint_ops method for
9630 ranged breakpoints. */
9631
9632 static enum print_stop_action
9633 print_it_ranged_breakpoint (bpstat bs)
9634 {
9635 struct breakpoint *b = bs->breakpoint_at;
9636 struct bp_location *bl = b->loc;
9637 struct ui_out *uiout = current_uiout;
9638
9639 gdb_assert (b->type == bp_hardware_breakpoint);
9640
9641 /* Ranged breakpoints have only one location. */
9642 gdb_assert (bl && bl->next == NULL);
9643
9644 annotate_breakpoint (b->number);
9645
9646 maybe_print_thread_hit_breakpoint (uiout);
9647
9648 if (b->disposition == disp_del)
9649 uiout->text ("Temporary ranged breakpoint ");
9650 else
9651 uiout->text ("Ranged breakpoint ");
9652 if (uiout->is_mi_like_p ())
9653 {
9654 uiout->field_string ("reason",
9655 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9656 uiout->field_string ("disp", bpdisp_text (b->disposition));
9657 }
9658 uiout->field_signed ("bkptno", b->number);
9659 uiout->text (", ");
9660
9661 return PRINT_SRC_AND_LOC;
9662 }
9663
9664 /* Implement the "print_one" breakpoint_ops method for
9665 ranged breakpoints. */
9666
9667 static void
9668 print_one_ranged_breakpoint (struct breakpoint *b,
9669 struct bp_location **last_loc)
9670 {
9671 struct bp_location *bl = b->loc;
9672 struct value_print_options opts;
9673 struct ui_out *uiout = current_uiout;
9674
9675 /* Ranged breakpoints have only one location. */
9676 gdb_assert (bl && bl->next == NULL);
9677
9678 get_user_print_options (&opts);
9679
9680 if (opts.addressprint)
9681 /* We don't print the address range here, it will be printed later
9682 by print_one_detail_ranged_breakpoint. */
9683 uiout->field_skip ("addr");
9684 annotate_field (5);
9685 print_breakpoint_location (b, bl);
9686 *last_loc = bl;
9687 }
9688
9689 /* Implement the "print_one_detail" breakpoint_ops method for
9690 ranged breakpoints. */
9691
9692 static void
9693 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9694 struct ui_out *uiout)
9695 {
9696 CORE_ADDR address_start, address_end;
9697 struct bp_location *bl = b->loc;
9698 string_file stb;
9699
9700 gdb_assert (bl);
9701
9702 address_start = bl->address;
9703 address_end = address_start + bl->length - 1;
9704
9705 uiout->text ("\taddress range: ");
9706 stb.printf ("[%s, %s]",
9707 print_core_address (bl->gdbarch, address_start),
9708 print_core_address (bl->gdbarch, address_end));
9709 uiout->field_stream ("addr", stb);
9710 uiout->text ("\n");
9711 }
9712
9713 /* Implement the "print_mention" breakpoint_ops method for
9714 ranged breakpoints. */
9715
9716 static void
9717 print_mention_ranged_breakpoint (struct breakpoint *b)
9718 {
9719 struct bp_location *bl = b->loc;
9720 struct ui_out *uiout = current_uiout;
9721
9722 gdb_assert (bl);
9723 gdb_assert (b->type == bp_hardware_breakpoint);
9724
9725 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9726 b->number, paddress (bl->gdbarch, bl->address),
9727 paddress (bl->gdbarch, bl->address + bl->length - 1));
9728 }
9729
9730 /* Implement the "print_recreate" breakpoint_ops method for
9731 ranged breakpoints. */
9732
9733 static void
9734 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9735 {
9736 fprintf_unfiltered (fp, "break-range %s, %s",
9737 event_location_to_string (b->location.get ()),
9738 event_location_to_string (b->location_range_end.get ()));
9739 print_recreate_thread (b, fp);
9740 }
9741
9742 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9743
9744 static struct breakpoint_ops ranged_breakpoint_ops;
9745
9746 /* Find the address where the end of the breakpoint range should be
9747 placed, given the SAL of the end of the range. This is so that if
9748 the user provides a line number, the end of the range is set to the
9749 last instruction of the given line. */
9750
9751 static CORE_ADDR
9752 find_breakpoint_range_end (struct symtab_and_line sal)
9753 {
9754 CORE_ADDR end;
9755
9756 /* If the user provided a PC value, use it. Otherwise,
9757 find the address of the end of the given location. */
9758 if (sal.explicit_pc)
9759 end = sal.pc;
9760 else
9761 {
9762 int ret;
9763 CORE_ADDR start;
9764
9765 ret = find_line_pc_range (sal, &start, &end);
9766 if (!ret)
9767 error (_("Could not find location of the end of the range."));
9768
9769 /* find_line_pc_range returns the start of the next line. */
9770 end--;
9771 }
9772
9773 return end;
9774 }
9775
9776 /* Implement the "break-range" CLI command. */
9777
9778 static void
9779 break_range_command (const char *arg, int from_tty)
9780 {
9781 const char *arg_start;
9782 struct linespec_result canonical_start, canonical_end;
9783 int bp_count, can_use_bp, length;
9784 CORE_ADDR end;
9785 struct breakpoint *b;
9786
9787 /* We don't support software ranged breakpoints. */
9788 if (target_ranged_break_num_registers () < 0)
9789 error (_("This target does not support hardware ranged breakpoints."));
9790
9791 bp_count = hw_breakpoint_used_count ();
9792 bp_count += target_ranged_break_num_registers ();
9793 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9794 bp_count, 0);
9795 if (can_use_bp < 0)
9796 error (_("Hardware breakpoints used exceeds limit."));
9797
9798 arg = skip_spaces (arg);
9799 if (arg == NULL || arg[0] == '\0')
9800 error(_("No address range specified."));
9801
9802 arg_start = arg;
9803 event_location_up start_location = string_to_event_location (&arg,
9804 current_language);
9805 parse_breakpoint_sals (start_location.get (), &canonical_start);
9806
9807 if (arg[0] != ',')
9808 error (_("Too few arguments."));
9809 else if (canonical_start.lsals.empty ())
9810 error (_("Could not find location of the beginning of the range."));
9811
9812 const linespec_sals &lsal_start = canonical_start.lsals[0];
9813
9814 if (canonical_start.lsals.size () > 1
9815 || lsal_start.sals.size () != 1)
9816 error (_("Cannot create a ranged breakpoint with multiple locations."));
9817
9818 const symtab_and_line &sal_start = lsal_start.sals[0];
9819 std::string addr_string_start (arg_start, arg - arg_start);
9820
9821 arg++; /* Skip the comma. */
9822 arg = skip_spaces (arg);
9823
9824 /* Parse the end location. */
9825
9826 arg_start = arg;
9827
9828 /* We call decode_line_full directly here instead of using
9829 parse_breakpoint_sals because we need to specify the start location's
9830 symtab and line as the default symtab and line for the end of the
9831 range. This makes it possible to have ranges like "foo.c:27, +14",
9832 where +14 means 14 lines from the start location. */
9833 event_location_up end_location = string_to_event_location (&arg,
9834 current_language);
9835 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9836 sal_start.symtab, sal_start.line,
9837 &canonical_end, NULL, NULL);
9838
9839 if (canonical_end.lsals.empty ())
9840 error (_("Could not find location of the end of the range."));
9841
9842 const linespec_sals &lsal_end = canonical_end.lsals[0];
9843 if (canonical_end.lsals.size () > 1
9844 || lsal_end.sals.size () != 1)
9845 error (_("Cannot create a ranged breakpoint with multiple locations."));
9846
9847 const symtab_and_line &sal_end = lsal_end.sals[0];
9848
9849 end = find_breakpoint_range_end (sal_end);
9850 if (sal_start.pc > end)
9851 error (_("Invalid address range, end precedes start."));
9852
9853 length = end - sal_start.pc + 1;
9854 if (length < 0)
9855 /* Length overflowed. */
9856 error (_("Address range too large."));
9857 else if (length == 1)
9858 {
9859 /* This range is simple enough to be handled by
9860 the `hbreak' command. */
9861 hbreak_command (&addr_string_start[0], 1);
9862
9863 return;
9864 }
9865
9866 /* Now set up the breakpoint. */
9867 b = set_raw_breakpoint (get_current_arch (), sal_start,
9868 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9869 set_breakpoint_count (breakpoint_count + 1);
9870 b->number = breakpoint_count;
9871 b->disposition = disp_donttouch;
9872 b->location = std::move (start_location);
9873 b->location_range_end = std::move (end_location);
9874 b->loc->length = length;
9875
9876 mention (b);
9877 gdb::observers::breakpoint_created.notify (b);
9878 update_global_location_list (UGLL_MAY_INSERT);
9879 }
9880
9881 /* Return non-zero if EXP is verified as constant. Returned zero
9882 means EXP is variable. Also the constant detection may fail for
9883 some constant expressions and in such case still falsely return
9884 zero. */
9885
9886 static int
9887 watchpoint_exp_is_const (const struct expression *exp)
9888 {
9889 int i = exp->nelts;
9890
9891 while (i > 0)
9892 {
9893 int oplenp, argsp;
9894
9895 /* We are only interested in the descriptor of each element. */
9896 operator_length (exp, i, &oplenp, &argsp);
9897 i -= oplenp;
9898
9899 switch (exp->elts[i].opcode)
9900 {
9901 case BINOP_ADD:
9902 case BINOP_SUB:
9903 case BINOP_MUL:
9904 case BINOP_DIV:
9905 case BINOP_REM:
9906 case BINOP_MOD:
9907 case BINOP_LSH:
9908 case BINOP_RSH:
9909 case BINOP_LOGICAL_AND:
9910 case BINOP_LOGICAL_OR:
9911 case BINOP_BITWISE_AND:
9912 case BINOP_BITWISE_IOR:
9913 case BINOP_BITWISE_XOR:
9914 case BINOP_EQUAL:
9915 case BINOP_NOTEQUAL:
9916 case BINOP_LESS:
9917 case BINOP_GTR:
9918 case BINOP_LEQ:
9919 case BINOP_GEQ:
9920 case BINOP_REPEAT:
9921 case BINOP_COMMA:
9922 case BINOP_EXP:
9923 case BINOP_MIN:
9924 case BINOP_MAX:
9925 case BINOP_INTDIV:
9926 case BINOP_CONCAT:
9927 case TERNOP_COND:
9928 case TERNOP_SLICE:
9929
9930 case OP_LONG:
9931 case OP_FLOAT:
9932 case OP_LAST:
9933 case OP_COMPLEX:
9934 case OP_STRING:
9935 case OP_ARRAY:
9936 case OP_TYPE:
9937 case OP_TYPEOF:
9938 case OP_DECLTYPE:
9939 case OP_TYPEID:
9940 case OP_NAME:
9941 case OP_OBJC_NSSTRING:
9942
9943 case UNOP_NEG:
9944 case UNOP_LOGICAL_NOT:
9945 case UNOP_COMPLEMENT:
9946 case UNOP_ADDR:
9947 case UNOP_HIGH:
9948 case UNOP_CAST:
9949
9950 case UNOP_CAST_TYPE:
9951 case UNOP_REINTERPRET_CAST:
9952 case UNOP_DYNAMIC_CAST:
9953 /* Unary, binary and ternary operators: We have to check
9954 their operands. If they are constant, then so is the
9955 result of that operation. For instance, if A and B are
9956 determined to be constants, then so is "A + B".
9957
9958 UNOP_IND is one exception to the rule above, because the
9959 value of *ADDR is not necessarily a constant, even when
9960 ADDR is. */
9961 break;
9962
9963 case OP_VAR_VALUE:
9964 /* Check whether the associated symbol is a constant.
9965
9966 We use SYMBOL_CLASS rather than TYPE_CONST because it's
9967 possible that a buggy compiler could mark a variable as
9968 constant even when it is not, and TYPE_CONST would return
9969 true in this case, while SYMBOL_CLASS wouldn't.
9970
9971 We also have to check for function symbols because they
9972 are always constant. */
9973 {
9974 struct symbol *s = exp->elts[i + 2].symbol;
9975
9976 if (SYMBOL_CLASS (s) != LOC_BLOCK
9977 && SYMBOL_CLASS (s) != LOC_CONST
9978 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9979 return 0;
9980 break;
9981 }
9982
9983 /* The default action is to return 0 because we are using
9984 the optimistic approach here: If we don't know something,
9985 then it is not a constant. */
9986 default:
9987 return 0;
9988 }
9989 }
9990
9991 return 1;
9992 }
9993
9994 /* Watchpoint destructor. */
9995
9996 watchpoint::~watchpoint ()
9997 {
9998 xfree (this->exp_string);
9999 xfree (this->exp_string_reparse);
10000 }
10001
10002 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10003
10004 static void
10005 re_set_watchpoint (struct breakpoint *b)
10006 {
10007 struct watchpoint *w = (struct watchpoint *) b;
10008
10009 /* Watchpoint can be either on expression using entirely global
10010 variables, or it can be on local variables.
10011
10012 Watchpoints of the first kind are never auto-deleted, and even
10013 persist across program restarts. Since they can use variables
10014 from shared libraries, we need to reparse expression as libraries
10015 are loaded and unloaded.
10016
10017 Watchpoints on local variables can also change meaning as result
10018 of solib event. For example, if a watchpoint uses both a local
10019 and a global variables in expression, it's a local watchpoint,
10020 but unloading of a shared library will make the expression
10021 invalid. This is not a very common use case, but we still
10022 re-evaluate expression, to avoid surprises to the user.
10023
10024 Note that for local watchpoints, we re-evaluate it only if
10025 watchpoints frame id is still valid. If it's not, it means the
10026 watchpoint is out of scope and will be deleted soon. In fact,
10027 I'm not sure we'll ever be called in this case.
10028
10029 If a local watchpoint's frame id is still valid, then
10030 w->exp_valid_block is likewise valid, and we can safely use it.
10031
10032 Don't do anything about disabled watchpoints, since they will be
10033 reevaluated again when enabled. */
10034 update_watchpoint (w, 1 /* reparse */);
10035 }
10036
10037 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10038
10039 static int
10040 insert_watchpoint (struct bp_location *bl)
10041 {
10042 struct watchpoint *w = (struct watchpoint *) bl->owner;
10043 int length = w->exact ? 1 : bl->length;
10044
10045 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10046 w->cond_exp.get ());
10047 }
10048
10049 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10050
10051 static int
10052 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10053 {
10054 struct watchpoint *w = (struct watchpoint *) bl->owner;
10055 int length = w->exact ? 1 : bl->length;
10056
10057 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10058 w->cond_exp.get ());
10059 }
10060
10061 static int
10062 breakpoint_hit_watchpoint (const struct bp_location *bl,
10063 const address_space *aspace, CORE_ADDR bp_addr,
10064 const struct target_waitstatus *ws)
10065 {
10066 struct breakpoint *b = bl->owner;
10067 struct watchpoint *w = (struct watchpoint *) b;
10068
10069 /* Continuable hardware watchpoints are treated as non-existent if the
10070 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10071 some data address). Otherwise gdb won't stop on a break instruction
10072 in the code (not from a breakpoint) when a hardware watchpoint has
10073 been defined. Also skip watchpoints which we know did not trigger
10074 (did not match the data address). */
10075 if (is_hardware_watchpoint (b)
10076 && w->watchpoint_triggered == watch_triggered_no)
10077 return 0;
10078
10079 return 1;
10080 }
10081
10082 static void
10083 check_status_watchpoint (bpstat bs)
10084 {
10085 gdb_assert (is_watchpoint (bs->breakpoint_at));
10086
10087 bpstat_check_watchpoint (bs);
10088 }
10089
10090 /* Implement the "resources_needed" breakpoint_ops method for
10091 hardware watchpoints. */
10092
10093 static int
10094 resources_needed_watchpoint (const struct bp_location *bl)
10095 {
10096 struct watchpoint *w = (struct watchpoint *) bl->owner;
10097 int length = w->exact? 1 : bl->length;
10098
10099 return target_region_ok_for_hw_watchpoint (bl->address, length);
10100 }
10101
10102 /* Implement the "works_in_software_mode" breakpoint_ops method for
10103 hardware watchpoints. */
10104
10105 static int
10106 works_in_software_mode_watchpoint (const struct breakpoint *b)
10107 {
10108 /* Read and access watchpoints only work with hardware support. */
10109 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10110 }
10111
10112 static enum print_stop_action
10113 print_it_watchpoint (bpstat bs)
10114 {
10115 struct breakpoint *b;
10116 enum print_stop_action result;
10117 struct watchpoint *w;
10118 struct ui_out *uiout = current_uiout;
10119
10120 gdb_assert (bs->bp_location_at != NULL);
10121
10122 b = bs->breakpoint_at;
10123 w = (struct watchpoint *) b;
10124
10125 annotate_watchpoint (b->number);
10126 maybe_print_thread_hit_breakpoint (uiout);
10127
10128 string_file stb;
10129
10130 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10131 switch (b->type)
10132 {
10133 case bp_watchpoint:
10134 case bp_hardware_watchpoint:
10135 if (uiout->is_mi_like_p ())
10136 uiout->field_string
10137 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10138 mention (b);
10139 tuple_emitter.emplace (uiout, "value");
10140 uiout->text ("\nOld value = ");
10141 watchpoint_value_print (bs->old_val.get (), &stb);
10142 uiout->field_stream ("old", stb);
10143 uiout->text ("\nNew value = ");
10144 watchpoint_value_print (w->val.get (), &stb);
10145 uiout->field_stream ("new", stb);
10146 uiout->text ("\n");
10147 /* More than one watchpoint may have been triggered. */
10148 result = PRINT_UNKNOWN;
10149 break;
10150
10151 case bp_read_watchpoint:
10152 if (uiout->is_mi_like_p ())
10153 uiout->field_string
10154 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10155 mention (b);
10156 tuple_emitter.emplace (uiout, "value");
10157 uiout->text ("\nValue = ");
10158 watchpoint_value_print (w->val.get (), &stb);
10159 uiout->field_stream ("value", stb);
10160 uiout->text ("\n");
10161 result = PRINT_UNKNOWN;
10162 break;
10163
10164 case bp_access_watchpoint:
10165 if (bs->old_val != NULL)
10166 {
10167 if (uiout->is_mi_like_p ())
10168 uiout->field_string
10169 ("reason",
10170 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10171 mention (b);
10172 tuple_emitter.emplace (uiout, "value");
10173 uiout->text ("\nOld value = ");
10174 watchpoint_value_print (bs->old_val.get (), &stb);
10175 uiout->field_stream ("old", stb);
10176 uiout->text ("\nNew value = ");
10177 }
10178 else
10179 {
10180 mention (b);
10181 if (uiout->is_mi_like_p ())
10182 uiout->field_string
10183 ("reason",
10184 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10185 tuple_emitter.emplace (uiout, "value");
10186 uiout->text ("\nValue = ");
10187 }
10188 watchpoint_value_print (w->val.get (), &stb);
10189 uiout->field_stream ("new", stb);
10190 uiout->text ("\n");
10191 result = PRINT_UNKNOWN;
10192 break;
10193 default:
10194 result = PRINT_UNKNOWN;
10195 }
10196
10197 return result;
10198 }
10199
10200 /* Implement the "print_mention" breakpoint_ops method for hardware
10201 watchpoints. */
10202
10203 static void
10204 print_mention_watchpoint (struct breakpoint *b)
10205 {
10206 struct watchpoint *w = (struct watchpoint *) b;
10207 struct ui_out *uiout = current_uiout;
10208 const char *tuple_name;
10209
10210 switch (b->type)
10211 {
10212 case bp_watchpoint:
10213 uiout->text ("Watchpoint ");
10214 tuple_name = "wpt";
10215 break;
10216 case bp_hardware_watchpoint:
10217 uiout->text ("Hardware watchpoint ");
10218 tuple_name = "wpt";
10219 break;
10220 case bp_read_watchpoint:
10221 uiout->text ("Hardware read watchpoint ");
10222 tuple_name = "hw-rwpt";
10223 break;
10224 case bp_access_watchpoint:
10225 uiout->text ("Hardware access (read/write) watchpoint ");
10226 tuple_name = "hw-awpt";
10227 break;
10228 default:
10229 internal_error (__FILE__, __LINE__,
10230 _("Invalid hardware watchpoint type."));
10231 }
10232
10233 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10234 uiout->field_signed ("number", b->number);
10235 uiout->text (": ");
10236 uiout->field_string ("exp", w->exp_string);
10237 }
10238
10239 /* Implement the "print_recreate" breakpoint_ops method for
10240 watchpoints. */
10241
10242 static void
10243 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10244 {
10245 struct watchpoint *w = (struct watchpoint *) b;
10246
10247 switch (b->type)
10248 {
10249 case bp_watchpoint:
10250 case bp_hardware_watchpoint:
10251 fprintf_unfiltered (fp, "watch");
10252 break;
10253 case bp_read_watchpoint:
10254 fprintf_unfiltered (fp, "rwatch");
10255 break;
10256 case bp_access_watchpoint:
10257 fprintf_unfiltered (fp, "awatch");
10258 break;
10259 default:
10260 internal_error (__FILE__, __LINE__,
10261 _("Invalid watchpoint type."));
10262 }
10263
10264 fprintf_unfiltered (fp, " %s", w->exp_string);
10265 print_recreate_thread (b, fp);
10266 }
10267
10268 /* Implement the "explains_signal" breakpoint_ops method for
10269 watchpoints. */
10270
10271 static int
10272 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10273 {
10274 /* A software watchpoint cannot cause a signal other than
10275 GDB_SIGNAL_TRAP. */
10276 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10277 return 0;
10278
10279 return 1;
10280 }
10281
10282 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10283
10284 static struct breakpoint_ops watchpoint_breakpoint_ops;
10285
10286 /* Implement the "insert" breakpoint_ops method for
10287 masked hardware watchpoints. */
10288
10289 static int
10290 insert_masked_watchpoint (struct bp_location *bl)
10291 {
10292 struct watchpoint *w = (struct watchpoint *) bl->owner;
10293
10294 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10295 bl->watchpoint_type);
10296 }
10297
10298 /* Implement the "remove" breakpoint_ops method for
10299 masked hardware watchpoints. */
10300
10301 static int
10302 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10303 {
10304 struct watchpoint *w = (struct watchpoint *) bl->owner;
10305
10306 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10307 bl->watchpoint_type);
10308 }
10309
10310 /* Implement the "resources_needed" breakpoint_ops method for
10311 masked hardware watchpoints. */
10312
10313 static int
10314 resources_needed_masked_watchpoint (const struct bp_location *bl)
10315 {
10316 struct watchpoint *w = (struct watchpoint *) bl->owner;
10317
10318 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10319 }
10320
10321 /* Implement the "works_in_software_mode" breakpoint_ops method for
10322 masked hardware watchpoints. */
10323
10324 static int
10325 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10326 {
10327 return 0;
10328 }
10329
10330 /* Implement the "print_it" breakpoint_ops method for
10331 masked hardware watchpoints. */
10332
10333 static enum print_stop_action
10334 print_it_masked_watchpoint (bpstat bs)
10335 {
10336 struct breakpoint *b = bs->breakpoint_at;
10337 struct ui_out *uiout = current_uiout;
10338
10339 /* Masked watchpoints have only one location. */
10340 gdb_assert (b->loc && b->loc->next == NULL);
10341
10342 annotate_watchpoint (b->number);
10343 maybe_print_thread_hit_breakpoint (uiout);
10344
10345 switch (b->type)
10346 {
10347 case bp_hardware_watchpoint:
10348 if (uiout->is_mi_like_p ())
10349 uiout->field_string
10350 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10351 break;
10352
10353 case bp_read_watchpoint:
10354 if (uiout->is_mi_like_p ())
10355 uiout->field_string
10356 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10357 break;
10358
10359 case bp_access_watchpoint:
10360 if (uiout->is_mi_like_p ())
10361 uiout->field_string
10362 ("reason",
10363 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10364 break;
10365 default:
10366 internal_error (__FILE__, __LINE__,
10367 _("Invalid hardware watchpoint type."));
10368 }
10369
10370 mention (b);
10371 uiout->text (_("\n\
10372 Check the underlying instruction at PC for the memory\n\
10373 address and value which triggered this watchpoint.\n"));
10374 uiout->text ("\n");
10375
10376 /* More than one watchpoint may have been triggered. */
10377 return PRINT_UNKNOWN;
10378 }
10379
10380 /* Implement the "print_one_detail" breakpoint_ops method for
10381 masked hardware watchpoints. */
10382
10383 static void
10384 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10385 struct ui_out *uiout)
10386 {
10387 struct watchpoint *w = (struct watchpoint *) b;
10388
10389 /* Masked watchpoints have only one location. */
10390 gdb_assert (b->loc && b->loc->next == NULL);
10391
10392 uiout->text ("\tmask ");
10393 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10394 uiout->text ("\n");
10395 }
10396
10397 /* Implement the "print_mention" breakpoint_ops method for
10398 masked hardware watchpoints. */
10399
10400 static void
10401 print_mention_masked_watchpoint (struct breakpoint *b)
10402 {
10403 struct watchpoint *w = (struct watchpoint *) b;
10404 struct ui_out *uiout = current_uiout;
10405 const char *tuple_name;
10406
10407 switch (b->type)
10408 {
10409 case bp_hardware_watchpoint:
10410 uiout->text ("Masked hardware watchpoint ");
10411 tuple_name = "wpt";
10412 break;
10413 case bp_read_watchpoint:
10414 uiout->text ("Masked hardware read watchpoint ");
10415 tuple_name = "hw-rwpt";
10416 break;
10417 case bp_access_watchpoint:
10418 uiout->text ("Masked hardware access (read/write) watchpoint ");
10419 tuple_name = "hw-awpt";
10420 break;
10421 default:
10422 internal_error (__FILE__, __LINE__,
10423 _("Invalid hardware watchpoint type."));
10424 }
10425
10426 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10427 uiout->field_signed ("number", b->number);
10428 uiout->text (": ");
10429 uiout->field_string ("exp", w->exp_string);
10430 }
10431
10432 /* Implement the "print_recreate" breakpoint_ops method for
10433 masked hardware watchpoints. */
10434
10435 static void
10436 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10437 {
10438 struct watchpoint *w = (struct watchpoint *) b;
10439 char tmp[40];
10440
10441 switch (b->type)
10442 {
10443 case bp_hardware_watchpoint:
10444 fprintf_unfiltered (fp, "watch");
10445 break;
10446 case bp_read_watchpoint:
10447 fprintf_unfiltered (fp, "rwatch");
10448 break;
10449 case bp_access_watchpoint:
10450 fprintf_unfiltered (fp, "awatch");
10451 break;
10452 default:
10453 internal_error (__FILE__, __LINE__,
10454 _("Invalid hardware watchpoint type."));
10455 }
10456
10457 sprintf_vma (tmp, w->hw_wp_mask);
10458 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10459 print_recreate_thread (b, fp);
10460 }
10461
10462 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10463
10464 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10465
10466 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10467
10468 static bool
10469 is_masked_watchpoint (const struct breakpoint *b)
10470 {
10471 return b->ops == &masked_watchpoint_breakpoint_ops;
10472 }
10473
10474 /* accessflag: hw_write: watch write,
10475 hw_read: watch read,
10476 hw_access: watch access (read or write) */
10477 static void
10478 watch_command_1 (const char *arg, int accessflag, int from_tty,
10479 int just_location, int internal)
10480 {
10481 struct breakpoint *scope_breakpoint = NULL;
10482 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10483 struct value *result;
10484 int saved_bitpos = 0, saved_bitsize = 0;
10485 const char *exp_start = NULL;
10486 const char *exp_end = NULL;
10487 const char *tok, *end_tok;
10488 int toklen = -1;
10489 const char *cond_start = NULL;
10490 const char *cond_end = NULL;
10491 enum bptype bp_type;
10492 int thread = -1;
10493 int pc = 0;
10494 /* Flag to indicate whether we are going to use masks for
10495 the hardware watchpoint. */
10496 int use_mask = 0;
10497 CORE_ADDR mask = 0;
10498
10499 /* Make sure that we actually have parameters to parse. */
10500 if (arg != NULL && arg[0] != '\0')
10501 {
10502 const char *value_start;
10503
10504 exp_end = arg + strlen (arg);
10505
10506 /* Look for "parameter value" pairs at the end
10507 of the arguments string. */
10508 for (tok = exp_end - 1; tok > arg; tok--)
10509 {
10510 /* Skip whitespace at the end of the argument list. */
10511 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10512 tok--;
10513
10514 /* Find the beginning of the last token.
10515 This is the value of the parameter. */
10516 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10517 tok--;
10518 value_start = tok + 1;
10519
10520 /* Skip whitespace. */
10521 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10522 tok--;
10523
10524 end_tok = tok;
10525
10526 /* Find the beginning of the second to last token.
10527 This is the parameter itself. */
10528 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10529 tok--;
10530 tok++;
10531 toklen = end_tok - tok + 1;
10532
10533 if (toklen == 6 && startswith (tok, "thread"))
10534 {
10535 struct thread_info *thr;
10536 /* At this point we've found a "thread" token, which means
10537 the user is trying to set a watchpoint that triggers
10538 only in a specific thread. */
10539 const char *endp;
10540
10541 if (thread != -1)
10542 error(_("You can specify only one thread."));
10543
10544 /* Extract the thread ID from the next token. */
10545 thr = parse_thread_id (value_start, &endp);
10546
10547 /* Check if the user provided a valid thread ID. */
10548 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10549 invalid_thread_id_error (value_start);
10550
10551 thread = thr->global_num;
10552 }
10553 else if (toklen == 4 && startswith (tok, "mask"))
10554 {
10555 /* We've found a "mask" token, which means the user wants to
10556 create a hardware watchpoint that is going to have the mask
10557 facility. */
10558 struct value *mask_value, *mark;
10559
10560 if (use_mask)
10561 error(_("You can specify only one mask."));
10562
10563 use_mask = just_location = 1;
10564
10565 mark = value_mark ();
10566 mask_value = parse_to_comma_and_eval (&value_start);
10567 mask = value_as_address (mask_value);
10568 value_free_to_mark (mark);
10569 }
10570 else
10571 /* We didn't recognize what we found. We should stop here. */
10572 break;
10573
10574 /* Truncate the string and get rid of the "parameter value" pair before
10575 the arguments string is parsed by the parse_exp_1 function. */
10576 exp_end = tok;
10577 }
10578 }
10579 else
10580 exp_end = arg;
10581
10582 /* Parse the rest of the arguments. From here on out, everything
10583 is in terms of a newly allocated string instead of the original
10584 ARG. */
10585 std::string expression (arg, exp_end - arg);
10586 exp_start = arg = expression.c_str ();
10587 innermost_block_tracker tracker;
10588 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10589 exp_end = arg;
10590 /* Remove trailing whitespace from the expression before saving it.
10591 This makes the eventual display of the expression string a bit
10592 prettier. */
10593 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10594 --exp_end;
10595
10596 /* Checking if the expression is not constant. */
10597 if (watchpoint_exp_is_const (exp.get ()))
10598 {
10599 int len;
10600
10601 len = exp_end - exp_start;
10602 while (len > 0 && isspace (exp_start[len - 1]))
10603 len--;
10604 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10605 }
10606
10607 exp_valid_block = tracker.block ();
10608 struct value *mark = value_mark ();
10609 struct value *val_as_value = nullptr;
10610 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10611 just_location);
10612
10613 if (val_as_value != NULL && just_location)
10614 {
10615 saved_bitpos = value_bitpos (val_as_value);
10616 saved_bitsize = value_bitsize (val_as_value);
10617 }
10618
10619 value_ref_ptr val;
10620 if (just_location)
10621 {
10622 int ret;
10623
10624 exp_valid_block = NULL;
10625 val = release_value (value_addr (result));
10626 value_free_to_mark (mark);
10627
10628 if (use_mask)
10629 {
10630 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10631 mask);
10632 if (ret == -1)
10633 error (_("This target does not support masked watchpoints."));
10634 else if (ret == -2)
10635 error (_("Invalid mask or memory region."));
10636 }
10637 }
10638 else if (val_as_value != NULL)
10639 val = release_value (val_as_value);
10640
10641 tok = skip_spaces (arg);
10642 end_tok = skip_to_space (tok);
10643
10644 toklen = end_tok - tok;
10645 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10646 {
10647 tok = cond_start = end_tok + 1;
10648 innermost_block_tracker if_tracker;
10649 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10650
10651 /* The watchpoint expression may not be local, but the condition
10652 may still be. E.g.: `watch global if local > 0'. */
10653 cond_exp_valid_block = if_tracker.block ();
10654
10655 cond_end = tok;
10656 }
10657 if (*tok)
10658 error (_("Junk at end of command."));
10659
10660 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10661
10662 /* Save this because create_internal_breakpoint below invalidates
10663 'wp_frame'. */
10664 frame_id watchpoint_frame = get_frame_id (wp_frame);
10665
10666 /* If the expression is "local", then set up a "watchpoint scope"
10667 breakpoint at the point where we've left the scope of the watchpoint
10668 expression. Create the scope breakpoint before the watchpoint, so
10669 that we will encounter it first in bpstat_stop_status. */
10670 if (exp_valid_block != NULL && wp_frame != NULL)
10671 {
10672 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10673
10674 if (frame_id_p (caller_frame_id))
10675 {
10676 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10677 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10678
10679 scope_breakpoint
10680 = create_internal_breakpoint (caller_arch, caller_pc,
10681 bp_watchpoint_scope,
10682 &momentary_breakpoint_ops);
10683
10684 /* create_internal_breakpoint could invalidate WP_FRAME. */
10685 wp_frame = NULL;
10686
10687 scope_breakpoint->enable_state = bp_enabled;
10688
10689 /* Automatically delete the breakpoint when it hits. */
10690 scope_breakpoint->disposition = disp_del;
10691
10692 /* Only break in the proper frame (help with recursion). */
10693 scope_breakpoint->frame_id = caller_frame_id;
10694
10695 /* Set the address at which we will stop. */
10696 scope_breakpoint->loc->gdbarch = caller_arch;
10697 scope_breakpoint->loc->requested_address = caller_pc;
10698 scope_breakpoint->loc->address
10699 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10700 scope_breakpoint->loc->requested_address,
10701 scope_breakpoint->type);
10702 }
10703 }
10704
10705 /* Now set up the breakpoint. We create all watchpoints as hardware
10706 watchpoints here even if hardware watchpoints are turned off, a call
10707 to update_watchpoint later in this function will cause the type to
10708 drop back to bp_watchpoint (software watchpoint) if required. */
10709
10710 if (accessflag == hw_read)
10711 bp_type = bp_read_watchpoint;
10712 else if (accessflag == hw_access)
10713 bp_type = bp_access_watchpoint;
10714 else
10715 bp_type = bp_hardware_watchpoint;
10716
10717 std::unique_ptr<watchpoint> w (new watchpoint ());
10718
10719 if (use_mask)
10720 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10721 &masked_watchpoint_breakpoint_ops);
10722 else
10723 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10724 &watchpoint_breakpoint_ops);
10725 w->thread = thread;
10726 w->disposition = disp_donttouch;
10727 w->pspace = current_program_space;
10728 w->exp = std::move (exp);
10729 w->exp_valid_block = exp_valid_block;
10730 w->cond_exp_valid_block = cond_exp_valid_block;
10731 if (just_location)
10732 {
10733 struct type *t = value_type (val.get ());
10734 CORE_ADDR addr = value_as_address (val.get ());
10735
10736 w->exp_string_reparse
10737 = current_language->la_watch_location_expression (t, addr).release ();
10738
10739 w->exp_string = xstrprintf ("-location %.*s",
10740 (int) (exp_end - exp_start), exp_start);
10741 }
10742 else
10743 w->exp_string = savestring (exp_start, exp_end - exp_start);
10744
10745 if (use_mask)
10746 {
10747 w->hw_wp_mask = mask;
10748 }
10749 else
10750 {
10751 w->val = val;
10752 w->val_bitpos = saved_bitpos;
10753 w->val_bitsize = saved_bitsize;
10754 w->val_valid = true;
10755 }
10756
10757 if (cond_start)
10758 w->cond_string = savestring (cond_start, cond_end - cond_start);
10759 else
10760 w->cond_string = 0;
10761
10762 if (frame_id_p (watchpoint_frame))
10763 {
10764 w->watchpoint_frame = watchpoint_frame;
10765 w->watchpoint_thread = inferior_ptid;
10766 }
10767 else
10768 {
10769 w->watchpoint_frame = null_frame_id;
10770 w->watchpoint_thread = null_ptid;
10771 }
10772
10773 if (scope_breakpoint != NULL)
10774 {
10775 /* The scope breakpoint is related to the watchpoint. We will
10776 need to act on them together. */
10777 w->related_breakpoint = scope_breakpoint;
10778 scope_breakpoint->related_breakpoint = w.get ();
10779 }
10780
10781 if (!just_location)
10782 value_free_to_mark (mark);
10783
10784 /* Finally update the new watchpoint. This creates the locations
10785 that should be inserted. */
10786 update_watchpoint (w.get (), 1);
10787
10788 install_breakpoint (internal, std::move (w), 1);
10789 }
10790
10791 /* Return count of debug registers needed to watch the given expression.
10792 If the watchpoint cannot be handled in hardware return zero. */
10793
10794 static int
10795 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10796 {
10797 int found_memory_cnt = 0;
10798
10799 /* Did the user specifically forbid us to use hardware watchpoints? */
10800 if (!can_use_hw_watchpoints)
10801 return 0;
10802
10803 gdb_assert (!vals.empty ());
10804 struct value *head = vals[0].get ();
10805
10806 /* Make sure that the value of the expression depends only upon
10807 memory contents, and values computed from them within GDB. If we
10808 find any register references or function calls, we can't use a
10809 hardware watchpoint.
10810
10811 The idea here is that evaluating an expression generates a series
10812 of values, one holding the value of every subexpression. (The
10813 expression a*b+c has five subexpressions: a, b, a*b, c, and
10814 a*b+c.) GDB's values hold almost enough information to establish
10815 the criteria given above --- they identify memory lvalues,
10816 register lvalues, computed values, etcetera. So we can evaluate
10817 the expression, and then scan the chain of values that leaves
10818 behind to decide whether we can detect any possible change to the
10819 expression's final value using only hardware watchpoints.
10820
10821 However, I don't think that the values returned by inferior
10822 function calls are special in any way. So this function may not
10823 notice that an expression involving an inferior function call
10824 can't be watched with hardware watchpoints. FIXME. */
10825 for (const value_ref_ptr &iter : vals)
10826 {
10827 struct value *v = iter.get ();
10828
10829 if (VALUE_LVAL (v) == lval_memory)
10830 {
10831 if (v != head && value_lazy (v))
10832 /* A lazy memory lvalue in the chain is one that GDB never
10833 needed to fetch; we either just used its address (e.g.,
10834 `a' in `a.b') or we never needed it at all (e.g., `a'
10835 in `a,b'). This doesn't apply to HEAD; if that is
10836 lazy then it was not readable, but watch it anyway. */
10837 ;
10838 else
10839 {
10840 /* Ahh, memory we actually used! Check if we can cover
10841 it with hardware watchpoints. */
10842 struct type *vtype = check_typedef (value_type (v));
10843
10844 /* We only watch structs and arrays if user asked for it
10845 explicitly, never if they just happen to appear in a
10846 middle of some value chain. */
10847 if (v == head
10848 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10849 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10850 {
10851 CORE_ADDR vaddr = value_address (v);
10852 int len;
10853 int num_regs;
10854
10855 len = (target_exact_watchpoints
10856 && is_scalar_type_recursive (vtype))?
10857 1 : TYPE_LENGTH (value_type (v));
10858
10859 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10860 if (!num_regs)
10861 return 0;
10862 else
10863 found_memory_cnt += num_regs;
10864 }
10865 }
10866 }
10867 else if (VALUE_LVAL (v) != not_lval
10868 && deprecated_value_modifiable (v) == 0)
10869 return 0; /* These are values from the history (e.g., $1). */
10870 else if (VALUE_LVAL (v) == lval_register)
10871 return 0; /* Cannot watch a register with a HW watchpoint. */
10872 }
10873
10874 /* The expression itself looks suitable for using a hardware
10875 watchpoint, but give the target machine a chance to reject it. */
10876 return found_memory_cnt;
10877 }
10878
10879 void
10880 watch_command_wrapper (const char *arg, int from_tty, int internal)
10881 {
10882 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10883 }
10884
10885 /* A helper function that looks for the "-location" argument and then
10886 calls watch_command_1. */
10887
10888 static void
10889 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10890 {
10891 int just_location = 0;
10892
10893 if (arg
10894 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10895 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10896 just_location = 1;
10897
10898 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10899 }
10900
10901 static void
10902 watch_command (const char *arg, int from_tty)
10903 {
10904 watch_maybe_just_location (arg, hw_write, from_tty);
10905 }
10906
10907 void
10908 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
10909 {
10910 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10911 }
10912
10913 static void
10914 rwatch_command (const char *arg, int from_tty)
10915 {
10916 watch_maybe_just_location (arg, hw_read, from_tty);
10917 }
10918
10919 void
10920 awatch_command_wrapper (const char *arg, int from_tty, int internal)
10921 {
10922 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10923 }
10924
10925 static void
10926 awatch_command (const char *arg, int from_tty)
10927 {
10928 watch_maybe_just_location (arg, hw_access, from_tty);
10929 }
10930 \f
10931
10932 /* Data for the FSM that manages the until(location)/advance commands
10933 in infcmd.c. Here because it uses the mechanisms of
10934 breakpoints. */
10935
10936 struct until_break_fsm : public thread_fsm
10937 {
10938 /* The thread that was current when the command was executed. */
10939 int thread;
10940
10941 /* The breakpoint set at the destination location. */
10942 breakpoint_up location_breakpoint;
10943
10944 /* Breakpoint set at the return address in the caller frame. May be
10945 NULL. */
10946 breakpoint_up caller_breakpoint;
10947
10948 until_break_fsm (struct interp *cmd_interp, int thread,
10949 breakpoint_up &&location_breakpoint,
10950 breakpoint_up &&caller_breakpoint)
10951 : thread_fsm (cmd_interp),
10952 thread (thread),
10953 location_breakpoint (std::move (location_breakpoint)),
10954 caller_breakpoint (std::move (caller_breakpoint))
10955 {
10956 }
10957
10958 void clean_up (struct thread_info *thread) override;
10959 bool should_stop (struct thread_info *thread) override;
10960 enum async_reply_reason do_async_reply_reason () override;
10961 };
10962
10963 /* Implementation of the 'should_stop' FSM method for the
10964 until(location)/advance commands. */
10965
10966 bool
10967 until_break_fsm::should_stop (struct thread_info *tp)
10968 {
10969 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10970 location_breakpoint.get ()) != NULL
10971 || (caller_breakpoint != NULL
10972 && bpstat_find_breakpoint (tp->control.stop_bpstat,
10973 caller_breakpoint.get ()) != NULL))
10974 set_finished ();
10975
10976 return true;
10977 }
10978
10979 /* Implementation of the 'clean_up' FSM method for the
10980 until(location)/advance commands. */
10981
10982 void
10983 until_break_fsm::clean_up (struct thread_info *)
10984 {
10985 /* Clean up our temporary breakpoints. */
10986 location_breakpoint.reset ();
10987 caller_breakpoint.reset ();
10988 delete_longjmp_breakpoint (thread);
10989 }
10990
10991 /* Implementation of the 'async_reply_reason' FSM method for the
10992 until(location)/advance commands. */
10993
10994 enum async_reply_reason
10995 until_break_fsm::do_async_reply_reason ()
10996 {
10997 return EXEC_ASYNC_LOCATION_REACHED;
10998 }
10999
11000 void
11001 until_break_command (const char *arg, int from_tty, int anywhere)
11002 {
11003 struct frame_info *frame;
11004 struct gdbarch *frame_gdbarch;
11005 struct frame_id stack_frame_id;
11006 struct frame_id caller_frame_id;
11007 int thread;
11008 struct thread_info *tp;
11009
11010 clear_proceed_status (0);
11011
11012 /* Set a breakpoint where the user wants it and at return from
11013 this function. */
11014
11015 event_location_up location = string_to_event_location (&arg, current_language);
11016
11017 std::vector<symtab_and_line> sals
11018 = (last_displayed_sal_is_valid ()
11019 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11020 get_last_displayed_symtab (),
11021 get_last_displayed_line ())
11022 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11023 NULL, NULL, 0));
11024
11025 if (sals.size () != 1)
11026 error (_("Couldn't get information on specified line."));
11027
11028 symtab_and_line &sal = sals[0];
11029
11030 if (*arg)
11031 error (_("Junk at end of arguments."));
11032
11033 resolve_sal_pc (&sal);
11034
11035 tp = inferior_thread ();
11036 thread = tp->global_num;
11037
11038 /* Note linespec handling above invalidates the frame chain.
11039 Installing a breakpoint also invalidates the frame chain (as it
11040 may need to switch threads), so do any frame handling before
11041 that. */
11042
11043 frame = get_selected_frame (NULL);
11044 frame_gdbarch = get_frame_arch (frame);
11045 stack_frame_id = get_stack_frame_id (frame);
11046 caller_frame_id = frame_unwind_caller_id (frame);
11047
11048 /* Keep within the current frame, or in frames called by the current
11049 one. */
11050
11051 breakpoint_up caller_breakpoint;
11052
11053 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11054
11055 if (frame_id_p (caller_frame_id))
11056 {
11057 struct symtab_and_line sal2;
11058 struct gdbarch *caller_gdbarch;
11059
11060 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11061 sal2.pc = frame_unwind_caller_pc (frame);
11062 caller_gdbarch = frame_unwind_caller_arch (frame);
11063 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11064 sal2,
11065 caller_frame_id,
11066 bp_until);
11067
11068 set_longjmp_breakpoint (tp, caller_frame_id);
11069 lj_deleter.emplace (thread);
11070 }
11071
11072 /* set_momentary_breakpoint could invalidate FRAME. */
11073 frame = NULL;
11074
11075 breakpoint_up location_breakpoint;
11076 if (anywhere)
11077 /* If the user told us to continue until a specified location,
11078 we don't specify a frame at which we need to stop. */
11079 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11080 null_frame_id, bp_until);
11081 else
11082 /* Otherwise, specify the selected frame, because we want to stop
11083 only at the very same frame. */
11084 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11085 stack_frame_id, bp_until);
11086
11087 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11088 std::move (location_breakpoint),
11089 std::move (caller_breakpoint));
11090
11091 if (lj_deleter)
11092 lj_deleter->release ();
11093
11094 proceed (-1, GDB_SIGNAL_DEFAULT);
11095 }
11096
11097 /* This function attempts to parse an optional "if <cond>" clause
11098 from the arg string. If one is not found, it returns NULL.
11099
11100 Else, it returns a pointer to the condition string. (It does not
11101 attempt to evaluate the string against a particular block.) And,
11102 it updates arg to point to the first character following the parsed
11103 if clause in the arg string. */
11104
11105 const char *
11106 ep_parse_optional_if_clause (const char **arg)
11107 {
11108 const char *cond_string;
11109
11110 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11111 return NULL;
11112
11113 /* Skip the "if" keyword. */
11114 (*arg) += 2;
11115
11116 /* Skip any extra leading whitespace, and record the start of the
11117 condition string. */
11118 *arg = skip_spaces (*arg);
11119 cond_string = *arg;
11120
11121 /* Assume that the condition occupies the remainder of the arg
11122 string. */
11123 (*arg) += strlen (cond_string);
11124
11125 return cond_string;
11126 }
11127
11128 /* Commands to deal with catching events, such as signals, exceptions,
11129 process start/exit, etc. */
11130
11131 typedef enum
11132 {
11133 catch_fork_temporary, catch_vfork_temporary,
11134 catch_fork_permanent, catch_vfork_permanent
11135 }
11136 catch_fork_kind;
11137
11138 static void
11139 catch_fork_command_1 (const char *arg, int from_tty,
11140 struct cmd_list_element *command)
11141 {
11142 struct gdbarch *gdbarch = get_current_arch ();
11143 const char *cond_string = NULL;
11144 catch_fork_kind fork_kind;
11145 int tempflag;
11146
11147 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11148 tempflag = (fork_kind == catch_fork_temporary
11149 || fork_kind == catch_vfork_temporary);
11150
11151 if (!arg)
11152 arg = "";
11153 arg = skip_spaces (arg);
11154
11155 /* The allowed syntax is:
11156 catch [v]fork
11157 catch [v]fork if <cond>
11158
11159 First, check if there's an if clause. */
11160 cond_string = ep_parse_optional_if_clause (&arg);
11161
11162 if ((*arg != '\0') && !isspace (*arg))
11163 error (_("Junk at end of arguments."));
11164
11165 /* If this target supports it, create a fork or vfork catchpoint
11166 and enable reporting of such events. */
11167 switch (fork_kind)
11168 {
11169 case catch_fork_temporary:
11170 case catch_fork_permanent:
11171 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11172 &catch_fork_breakpoint_ops);
11173 break;
11174 case catch_vfork_temporary:
11175 case catch_vfork_permanent:
11176 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11177 &catch_vfork_breakpoint_ops);
11178 break;
11179 default:
11180 error (_("unsupported or unknown fork kind; cannot catch it"));
11181 break;
11182 }
11183 }
11184
11185 static void
11186 catch_exec_command_1 (const char *arg, int from_tty,
11187 struct cmd_list_element *command)
11188 {
11189 struct gdbarch *gdbarch = get_current_arch ();
11190 int tempflag;
11191 const char *cond_string = NULL;
11192
11193 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11194
11195 if (!arg)
11196 arg = "";
11197 arg = skip_spaces (arg);
11198
11199 /* The allowed syntax is:
11200 catch exec
11201 catch exec if <cond>
11202
11203 First, check if there's an if clause. */
11204 cond_string = ep_parse_optional_if_clause (&arg);
11205
11206 if ((*arg != '\0') && !isspace (*arg))
11207 error (_("Junk at end of arguments."));
11208
11209 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11210 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11211 &catch_exec_breakpoint_ops);
11212 c->exec_pathname = NULL;
11213
11214 install_breakpoint (0, std::move (c), 1);
11215 }
11216
11217 void
11218 init_ada_exception_breakpoint (struct breakpoint *b,
11219 struct gdbarch *gdbarch,
11220 struct symtab_and_line sal,
11221 const char *addr_string,
11222 const struct breakpoint_ops *ops,
11223 int tempflag,
11224 int enabled,
11225 int from_tty)
11226 {
11227 if (from_tty)
11228 {
11229 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11230 if (!loc_gdbarch)
11231 loc_gdbarch = gdbarch;
11232
11233 describe_other_breakpoints (loc_gdbarch,
11234 sal.pspace, sal.pc, sal.section, -1);
11235 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11236 version for exception catchpoints, because two catchpoints
11237 used for different exception names will use the same address.
11238 In this case, a "breakpoint ... also set at..." warning is
11239 unproductive. Besides, the warning phrasing is also a bit
11240 inappropriate, we should use the word catchpoint, and tell
11241 the user what type of catchpoint it is. The above is good
11242 enough for now, though. */
11243 }
11244
11245 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11246
11247 b->enable_state = enabled ? bp_enabled : bp_disabled;
11248 b->disposition = tempflag ? disp_del : disp_donttouch;
11249 b->location = string_to_event_location (&addr_string,
11250 language_def (language_ada));
11251 b->language = language_ada;
11252 }
11253
11254 static void
11255 catch_command (const char *arg, int from_tty)
11256 {
11257 error (_("Catch requires an event name."));
11258 }
11259 \f
11260
11261 static void
11262 tcatch_command (const char *arg, int from_tty)
11263 {
11264 error (_("Catch requires an event name."));
11265 }
11266
11267 /* Compare two breakpoints and return a strcmp-like result. */
11268
11269 static int
11270 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11271 {
11272 uintptr_t ua = (uintptr_t) a;
11273 uintptr_t ub = (uintptr_t) b;
11274
11275 if (a->number < b->number)
11276 return -1;
11277 else if (a->number > b->number)
11278 return 1;
11279
11280 /* Now sort by address, in case we see, e..g, two breakpoints with
11281 the number 0. */
11282 if (ua < ub)
11283 return -1;
11284 return ua > ub ? 1 : 0;
11285 }
11286
11287 /* Delete breakpoints by address or line. */
11288
11289 static void
11290 clear_command (const char *arg, int from_tty)
11291 {
11292 struct breakpoint *b;
11293 int default_match;
11294
11295 std::vector<symtab_and_line> decoded_sals;
11296 symtab_and_line last_sal;
11297 gdb::array_view<symtab_and_line> sals;
11298 if (arg)
11299 {
11300 decoded_sals
11301 = decode_line_with_current_source (arg,
11302 (DECODE_LINE_FUNFIRSTLINE
11303 | DECODE_LINE_LIST_MODE));
11304 default_match = 0;
11305 sals = decoded_sals;
11306 }
11307 else
11308 {
11309 /* Set sal's line, symtab, pc, and pspace to the values
11310 corresponding to the last call to print_frame_info. If the
11311 codepoint is not valid, this will set all the fields to 0. */
11312 last_sal = get_last_displayed_sal ();
11313 if (last_sal.symtab == 0)
11314 error (_("No source file specified."));
11315
11316 default_match = 1;
11317 sals = last_sal;
11318 }
11319
11320 /* We don't call resolve_sal_pc here. That's not as bad as it
11321 seems, because all existing breakpoints typically have both
11322 file/line and pc set. So, if clear is given file/line, we can
11323 match this to existing breakpoint without obtaining pc at all.
11324
11325 We only support clearing given the address explicitly
11326 present in breakpoint table. Say, we've set breakpoint
11327 at file:line. There were several PC values for that file:line,
11328 due to optimization, all in one block.
11329
11330 We've picked one PC value. If "clear" is issued with another
11331 PC corresponding to the same file:line, the breakpoint won't
11332 be cleared. We probably can still clear the breakpoint, but
11333 since the other PC value is never presented to user, user
11334 can only find it by guessing, and it does not seem important
11335 to support that. */
11336
11337 /* For each line spec given, delete bps which correspond to it. Do
11338 it in two passes, solely to preserve the current behavior that
11339 from_tty is forced true if we delete more than one
11340 breakpoint. */
11341
11342 std::vector<struct breakpoint *> found;
11343 for (const auto &sal : sals)
11344 {
11345 const char *sal_fullname;
11346
11347 /* If exact pc given, clear bpts at that pc.
11348 If line given (pc == 0), clear all bpts on specified line.
11349 If defaulting, clear all bpts on default line
11350 or at default pc.
11351
11352 defaulting sal.pc != 0 tests to do
11353
11354 0 1 pc
11355 1 1 pc _and_ line
11356 0 0 line
11357 1 0 <can't happen> */
11358
11359 sal_fullname = (sal.symtab == NULL
11360 ? NULL : symtab_to_fullname (sal.symtab));
11361
11362 /* Find all matching breakpoints and add them to 'found'. */
11363 ALL_BREAKPOINTS (b)
11364 {
11365 int match = 0;
11366 /* Are we going to delete b? */
11367 if (b->type != bp_none && !is_watchpoint (b))
11368 {
11369 struct bp_location *loc = b->loc;
11370 for (; loc; loc = loc->next)
11371 {
11372 /* If the user specified file:line, don't allow a PC
11373 match. This matches historical gdb behavior. */
11374 int pc_match = (!sal.explicit_line
11375 && sal.pc
11376 && (loc->pspace == sal.pspace)
11377 && (loc->address == sal.pc)
11378 && (!section_is_overlay (loc->section)
11379 || loc->section == sal.section));
11380 int line_match = 0;
11381
11382 if ((default_match || sal.explicit_line)
11383 && loc->symtab != NULL
11384 && sal_fullname != NULL
11385 && sal.pspace == loc->pspace
11386 && loc->line_number == sal.line
11387 && filename_cmp (symtab_to_fullname (loc->symtab),
11388 sal_fullname) == 0)
11389 line_match = 1;
11390
11391 if (pc_match || line_match)
11392 {
11393 match = 1;
11394 break;
11395 }
11396 }
11397 }
11398
11399 if (match)
11400 found.push_back (b);
11401 }
11402 }
11403
11404 /* Now go thru the 'found' chain and delete them. */
11405 if (found.empty ())
11406 {
11407 if (arg)
11408 error (_("No breakpoint at %s."), arg);
11409 else
11410 error (_("No breakpoint at this line."));
11411 }
11412
11413 /* Remove duplicates from the vec. */
11414 std::sort (found.begin (), found.end (),
11415 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11416 {
11417 return compare_breakpoints (bp_a, bp_b) < 0;
11418 });
11419 found.erase (std::unique (found.begin (), found.end (),
11420 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11421 {
11422 return compare_breakpoints (bp_a, bp_b) == 0;
11423 }),
11424 found.end ());
11425
11426 if (found.size () > 1)
11427 from_tty = 1; /* Always report if deleted more than one. */
11428 if (from_tty)
11429 {
11430 if (found.size () == 1)
11431 printf_unfiltered (_("Deleted breakpoint "));
11432 else
11433 printf_unfiltered (_("Deleted breakpoints "));
11434 }
11435
11436 for (breakpoint *iter : found)
11437 {
11438 if (from_tty)
11439 printf_unfiltered ("%d ", iter->number);
11440 delete_breakpoint (iter);
11441 }
11442 if (from_tty)
11443 putchar_unfiltered ('\n');
11444 }
11445 \f
11446 /* Delete breakpoint in BS if they are `delete' breakpoints and
11447 all breakpoints that are marked for deletion, whether hit or not.
11448 This is called after any breakpoint is hit, or after errors. */
11449
11450 void
11451 breakpoint_auto_delete (bpstat bs)
11452 {
11453 struct breakpoint *b, *b_tmp;
11454
11455 for (; bs; bs = bs->next)
11456 if (bs->breakpoint_at
11457 && bs->breakpoint_at->disposition == disp_del
11458 && bs->stop)
11459 delete_breakpoint (bs->breakpoint_at);
11460
11461 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11462 {
11463 if (b->disposition == disp_del_at_next_stop)
11464 delete_breakpoint (b);
11465 }
11466 }
11467
11468 /* A comparison function for bp_location AP and BP being interfaced to
11469 std::sort. Sort elements primarily by their ADDRESS (no matter what
11470 bl_address_is_meaningful says), secondarily by ordering first
11471 permanent elements and terciarily just ensuring the array is sorted
11472 stable way despite std::sort being an unstable algorithm. */
11473
11474 static int
11475 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11476 {
11477 if (a->address != b->address)
11478 return a->address < b->address;
11479
11480 /* Sort locations at the same address by their pspace number, keeping
11481 locations of the same inferior (in a multi-inferior environment)
11482 grouped. */
11483
11484 if (a->pspace->num != b->pspace->num)
11485 return a->pspace->num < b->pspace->num;
11486
11487 /* Sort permanent breakpoints first. */
11488 if (a->permanent != b->permanent)
11489 return a->permanent > b->permanent;
11490
11491 /* Make the internal GDB representation stable across GDB runs
11492 where A and B memory inside GDB can differ. Breakpoint locations of
11493 the same type at the same address can be sorted in arbitrary order. */
11494
11495 if (a->owner->number != b->owner->number)
11496 return a->owner->number < b->owner->number;
11497
11498 return a < b;
11499 }
11500
11501 /* Set bp_locations_placed_address_before_address_max and
11502 bp_locations_shadow_len_after_address_max according to the current
11503 content of the bp_locations array. */
11504
11505 static void
11506 bp_locations_target_extensions_update (void)
11507 {
11508 struct bp_location *bl, **blp_tmp;
11509
11510 bp_locations_placed_address_before_address_max = 0;
11511 bp_locations_shadow_len_after_address_max = 0;
11512
11513 ALL_BP_LOCATIONS (bl, blp_tmp)
11514 {
11515 CORE_ADDR start, end, addr;
11516
11517 if (!bp_location_has_shadow (bl))
11518 continue;
11519
11520 start = bl->target_info.placed_address;
11521 end = start + bl->target_info.shadow_len;
11522
11523 gdb_assert (bl->address >= start);
11524 addr = bl->address - start;
11525 if (addr > bp_locations_placed_address_before_address_max)
11526 bp_locations_placed_address_before_address_max = addr;
11527
11528 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11529
11530 gdb_assert (bl->address < end);
11531 addr = end - bl->address;
11532 if (addr > bp_locations_shadow_len_after_address_max)
11533 bp_locations_shadow_len_after_address_max = addr;
11534 }
11535 }
11536
11537 /* Download tracepoint locations if they haven't been. */
11538
11539 static void
11540 download_tracepoint_locations (void)
11541 {
11542 struct breakpoint *b;
11543 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11544
11545 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11546
11547 ALL_TRACEPOINTS (b)
11548 {
11549 struct bp_location *bl;
11550 struct tracepoint *t;
11551 int bp_location_downloaded = 0;
11552
11553 if ((b->type == bp_fast_tracepoint
11554 ? !may_insert_fast_tracepoints
11555 : !may_insert_tracepoints))
11556 continue;
11557
11558 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11559 {
11560 if (target_can_download_tracepoint ())
11561 can_download_tracepoint = TRIBOOL_TRUE;
11562 else
11563 can_download_tracepoint = TRIBOOL_FALSE;
11564 }
11565
11566 if (can_download_tracepoint == TRIBOOL_FALSE)
11567 break;
11568
11569 for (bl = b->loc; bl; bl = bl->next)
11570 {
11571 /* In tracepoint, locations are _never_ duplicated, so
11572 should_be_inserted is equivalent to
11573 unduplicated_should_be_inserted. */
11574 if (!should_be_inserted (bl) || bl->inserted)
11575 continue;
11576
11577 switch_to_program_space_and_thread (bl->pspace);
11578
11579 target_download_tracepoint (bl);
11580
11581 bl->inserted = 1;
11582 bp_location_downloaded = 1;
11583 }
11584 t = (struct tracepoint *) b;
11585 t->number_on_target = b->number;
11586 if (bp_location_downloaded)
11587 gdb::observers::breakpoint_modified.notify (b);
11588 }
11589 }
11590
11591 /* Swap the insertion/duplication state between two locations. */
11592
11593 static void
11594 swap_insertion (struct bp_location *left, struct bp_location *right)
11595 {
11596 const int left_inserted = left->inserted;
11597 const int left_duplicate = left->duplicate;
11598 const int left_needs_update = left->needs_update;
11599 const struct bp_target_info left_target_info = left->target_info;
11600
11601 /* Locations of tracepoints can never be duplicated. */
11602 if (is_tracepoint (left->owner))
11603 gdb_assert (!left->duplicate);
11604 if (is_tracepoint (right->owner))
11605 gdb_assert (!right->duplicate);
11606
11607 left->inserted = right->inserted;
11608 left->duplicate = right->duplicate;
11609 left->needs_update = right->needs_update;
11610 left->target_info = right->target_info;
11611 right->inserted = left_inserted;
11612 right->duplicate = left_duplicate;
11613 right->needs_update = left_needs_update;
11614 right->target_info = left_target_info;
11615 }
11616
11617 /* Force the re-insertion of the locations at ADDRESS. This is called
11618 once a new/deleted/modified duplicate location is found and we are evaluating
11619 conditions on the target's side. Such conditions need to be updated on
11620 the target. */
11621
11622 static void
11623 force_breakpoint_reinsertion (struct bp_location *bl)
11624 {
11625 struct bp_location **locp = NULL, **loc2p;
11626 struct bp_location *loc;
11627 CORE_ADDR address = 0;
11628 int pspace_num;
11629
11630 address = bl->address;
11631 pspace_num = bl->pspace->num;
11632
11633 /* This is only meaningful if the target is
11634 evaluating conditions and if the user has
11635 opted for condition evaluation on the target's
11636 side. */
11637 if (gdb_evaluates_breakpoint_condition_p ()
11638 || !target_supports_evaluation_of_breakpoint_conditions ())
11639 return;
11640
11641 /* Flag all breakpoint locations with this address and
11642 the same program space as the location
11643 as "its condition has changed". We need to
11644 update the conditions on the target's side. */
11645 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11646 {
11647 loc = *loc2p;
11648
11649 if (!is_breakpoint (loc->owner)
11650 || pspace_num != loc->pspace->num)
11651 continue;
11652
11653 /* Flag the location appropriately. We use a different state to
11654 let everyone know that we already updated the set of locations
11655 with addr bl->address and program space bl->pspace. This is so
11656 we don't have to keep calling these functions just to mark locations
11657 that have already been marked. */
11658 loc->condition_changed = condition_updated;
11659
11660 /* Free the agent expression bytecode as well. We will compute
11661 it later on. */
11662 loc->cond_bytecode.reset ();
11663 }
11664 }
11665 /* Called whether new breakpoints are created, or existing breakpoints
11666 deleted, to update the global location list and recompute which
11667 locations are duplicate of which.
11668
11669 The INSERT_MODE flag determines whether locations may not, may, or
11670 shall be inserted now. See 'enum ugll_insert_mode' for more
11671 info. */
11672
11673 static void
11674 update_global_location_list (enum ugll_insert_mode insert_mode)
11675 {
11676 struct breakpoint *b;
11677 struct bp_location **locp, *loc;
11678 /* Last breakpoint location address that was marked for update. */
11679 CORE_ADDR last_addr = 0;
11680 /* Last breakpoint location program space that was marked for update. */
11681 int last_pspace_num = -1;
11682
11683 /* Used in the duplicates detection below. When iterating over all
11684 bp_locations, points to the first bp_location of a given address.
11685 Breakpoints and watchpoints of different types are never
11686 duplicates of each other. Keep one pointer for each type of
11687 breakpoint/watchpoint, so we only need to loop over all locations
11688 once. */
11689 struct bp_location *bp_loc_first; /* breakpoint */
11690 struct bp_location *wp_loc_first; /* hardware watchpoint */
11691 struct bp_location *awp_loc_first; /* access watchpoint */
11692 struct bp_location *rwp_loc_first; /* read watchpoint */
11693
11694 /* Saved former bp_locations array which we compare against the newly
11695 built bp_locations from the current state of ALL_BREAKPOINTS. */
11696 struct bp_location **old_locp;
11697 unsigned old_locations_count;
11698 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11699
11700 old_locations_count = bp_locations_count;
11701 bp_locations = NULL;
11702 bp_locations_count = 0;
11703
11704 ALL_BREAKPOINTS (b)
11705 for (loc = b->loc; loc; loc = loc->next)
11706 bp_locations_count++;
11707
11708 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11709 locp = bp_locations;
11710 ALL_BREAKPOINTS (b)
11711 for (loc = b->loc; loc; loc = loc->next)
11712 *locp++ = loc;
11713 std::sort (bp_locations, bp_locations + bp_locations_count,
11714 bp_location_is_less_than);
11715
11716 bp_locations_target_extensions_update ();
11717
11718 /* Identify bp_location instances that are no longer present in the
11719 new list, and therefore should be freed. Note that it's not
11720 necessary that those locations should be removed from inferior --
11721 if there's another location at the same address (previously
11722 marked as duplicate), we don't need to remove/insert the
11723 location.
11724
11725 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11726 and former bp_location array state respectively. */
11727
11728 locp = bp_locations;
11729 for (old_locp = old_locations.get ();
11730 old_locp < old_locations.get () + old_locations_count;
11731 old_locp++)
11732 {
11733 struct bp_location *old_loc = *old_locp;
11734 struct bp_location **loc2p;
11735
11736 /* Tells if 'old_loc' is found among the new locations. If
11737 not, we have to free it. */
11738 int found_object = 0;
11739 /* Tells if the location should remain inserted in the target. */
11740 int keep_in_target = 0;
11741 int removed = 0;
11742
11743 /* Skip LOCP entries which will definitely never be needed.
11744 Stop either at or being the one matching OLD_LOC. */
11745 while (locp < bp_locations + bp_locations_count
11746 && (*locp)->address < old_loc->address)
11747 locp++;
11748
11749 for (loc2p = locp;
11750 (loc2p < bp_locations + bp_locations_count
11751 && (*loc2p)->address == old_loc->address);
11752 loc2p++)
11753 {
11754 /* Check if this is a new/duplicated location or a duplicated
11755 location that had its condition modified. If so, we want to send
11756 its condition to the target if evaluation of conditions is taking
11757 place there. */
11758 if ((*loc2p)->condition_changed == condition_modified
11759 && (last_addr != old_loc->address
11760 || last_pspace_num != old_loc->pspace->num))
11761 {
11762 force_breakpoint_reinsertion (*loc2p);
11763 last_pspace_num = old_loc->pspace->num;
11764 }
11765
11766 if (*loc2p == old_loc)
11767 found_object = 1;
11768 }
11769
11770 /* We have already handled this address, update it so that we don't
11771 have to go through updates again. */
11772 last_addr = old_loc->address;
11773
11774 /* Target-side condition evaluation: Handle deleted locations. */
11775 if (!found_object)
11776 force_breakpoint_reinsertion (old_loc);
11777
11778 /* If this location is no longer present, and inserted, look if
11779 there's maybe a new location at the same address. If so,
11780 mark that one inserted, and don't remove this one. This is
11781 needed so that we don't have a time window where a breakpoint
11782 at certain location is not inserted. */
11783
11784 if (old_loc->inserted)
11785 {
11786 /* If the location is inserted now, we might have to remove
11787 it. */
11788
11789 if (found_object && should_be_inserted (old_loc))
11790 {
11791 /* The location is still present in the location list,
11792 and still should be inserted. Don't do anything. */
11793 keep_in_target = 1;
11794 }
11795 else
11796 {
11797 /* This location still exists, but it won't be kept in the
11798 target since it may have been disabled. We proceed to
11799 remove its target-side condition. */
11800
11801 /* The location is either no longer present, or got
11802 disabled. See if there's another location at the
11803 same address, in which case we don't need to remove
11804 this one from the target. */
11805
11806 /* OLD_LOC comes from existing struct breakpoint. */
11807 if (bl_address_is_meaningful (old_loc))
11808 {
11809 for (loc2p = locp;
11810 (loc2p < bp_locations + bp_locations_count
11811 && (*loc2p)->address == old_loc->address);
11812 loc2p++)
11813 {
11814 struct bp_location *loc2 = *loc2p;
11815
11816 if (breakpoint_locations_match (loc2, old_loc))
11817 {
11818 /* Read watchpoint locations are switched to
11819 access watchpoints, if the former are not
11820 supported, but the latter are. */
11821 if (is_hardware_watchpoint (old_loc->owner))
11822 {
11823 gdb_assert (is_hardware_watchpoint (loc2->owner));
11824 loc2->watchpoint_type = old_loc->watchpoint_type;
11825 }
11826
11827 /* loc2 is a duplicated location. We need to check
11828 if it should be inserted in case it will be
11829 unduplicated. */
11830 if (loc2 != old_loc
11831 && unduplicated_should_be_inserted (loc2))
11832 {
11833 swap_insertion (old_loc, loc2);
11834 keep_in_target = 1;
11835 break;
11836 }
11837 }
11838 }
11839 }
11840 }
11841
11842 if (!keep_in_target)
11843 {
11844 if (remove_breakpoint (old_loc))
11845 {
11846 /* This is just about all we can do. We could keep
11847 this location on the global list, and try to
11848 remove it next time, but there's no particular
11849 reason why we will succeed next time.
11850
11851 Note that at this point, old_loc->owner is still
11852 valid, as delete_breakpoint frees the breakpoint
11853 only after calling us. */
11854 printf_filtered (_("warning: Error removing "
11855 "breakpoint %d\n"),
11856 old_loc->owner->number);
11857 }
11858 removed = 1;
11859 }
11860 }
11861
11862 if (!found_object)
11863 {
11864 if (removed && target_is_non_stop_p ()
11865 && need_moribund_for_location_type (old_loc))
11866 {
11867 /* This location was removed from the target. In
11868 non-stop mode, a race condition is possible where
11869 we've removed a breakpoint, but stop events for that
11870 breakpoint are already queued and will arrive later.
11871 We apply an heuristic to be able to distinguish such
11872 SIGTRAPs from other random SIGTRAPs: we keep this
11873 breakpoint location for a bit, and will retire it
11874 after we see some number of events. The theory here
11875 is that reporting of events should, "on the average",
11876 be fair, so after a while we'll see events from all
11877 threads that have anything of interest, and no longer
11878 need to keep this breakpoint location around. We
11879 don't hold locations forever so to reduce chances of
11880 mistaking a non-breakpoint SIGTRAP for a breakpoint
11881 SIGTRAP.
11882
11883 The heuristic failing can be disastrous on
11884 decr_pc_after_break targets.
11885
11886 On decr_pc_after_break targets, like e.g., x86-linux,
11887 if we fail to recognize a late breakpoint SIGTRAP,
11888 because events_till_retirement has reached 0 too
11889 soon, we'll fail to do the PC adjustment, and report
11890 a random SIGTRAP to the user. When the user resumes
11891 the inferior, it will most likely immediately crash
11892 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11893 corrupted, because of being resumed e.g., in the
11894 middle of a multi-byte instruction, or skipped a
11895 one-byte instruction. This was actually seen happen
11896 on native x86-linux, and should be less rare on
11897 targets that do not support new thread events, like
11898 remote, due to the heuristic depending on
11899 thread_count.
11900
11901 Mistaking a random SIGTRAP for a breakpoint trap
11902 causes similar symptoms (PC adjustment applied when
11903 it shouldn't), but then again, playing with SIGTRAPs
11904 behind the debugger's back is asking for trouble.
11905
11906 Since hardware watchpoint traps are always
11907 distinguishable from other traps, so we don't need to
11908 apply keep hardware watchpoint moribund locations
11909 around. We simply always ignore hardware watchpoint
11910 traps we can no longer explain. */
11911
11912 old_loc->events_till_retirement = 3 * (thread_count () + 1);
11913 old_loc->owner = NULL;
11914
11915 moribund_locations.push_back (old_loc);
11916 }
11917 else
11918 {
11919 old_loc->owner = NULL;
11920 decref_bp_location (&old_loc);
11921 }
11922 }
11923 }
11924
11925 /* Rescan breakpoints at the same address and section, marking the
11926 first one as "first" and any others as "duplicates". This is so
11927 that the bpt instruction is only inserted once. If we have a
11928 permanent breakpoint at the same place as BPT, make that one the
11929 official one, and the rest as duplicates. Permanent breakpoints
11930 are sorted first for the same address.
11931
11932 Do the same for hardware watchpoints, but also considering the
11933 watchpoint's type (regular/access/read) and length. */
11934
11935 bp_loc_first = NULL;
11936 wp_loc_first = NULL;
11937 awp_loc_first = NULL;
11938 rwp_loc_first = NULL;
11939 ALL_BP_LOCATIONS (loc, locp)
11940 {
11941 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11942 non-NULL. */
11943 struct bp_location **loc_first_p;
11944 b = loc->owner;
11945
11946 if (!unduplicated_should_be_inserted (loc)
11947 || !bl_address_is_meaningful (loc)
11948 /* Don't detect duplicate for tracepoint locations because they are
11949 never duplicated. See the comments in field `duplicate' of
11950 `struct bp_location'. */
11951 || is_tracepoint (b))
11952 {
11953 /* Clear the condition modification flag. */
11954 loc->condition_changed = condition_unchanged;
11955 continue;
11956 }
11957
11958 if (b->type == bp_hardware_watchpoint)
11959 loc_first_p = &wp_loc_first;
11960 else if (b->type == bp_read_watchpoint)
11961 loc_first_p = &rwp_loc_first;
11962 else if (b->type == bp_access_watchpoint)
11963 loc_first_p = &awp_loc_first;
11964 else
11965 loc_first_p = &bp_loc_first;
11966
11967 if (*loc_first_p == NULL
11968 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11969 || !breakpoint_locations_match (loc, *loc_first_p))
11970 {
11971 *loc_first_p = loc;
11972 loc->duplicate = 0;
11973
11974 if (is_breakpoint (loc->owner) && loc->condition_changed)
11975 {
11976 loc->needs_update = 1;
11977 /* Clear the condition modification flag. */
11978 loc->condition_changed = condition_unchanged;
11979 }
11980 continue;
11981 }
11982
11983
11984 /* This and the above ensure the invariant that the first location
11985 is not duplicated, and is the inserted one.
11986 All following are marked as duplicated, and are not inserted. */
11987 if (loc->inserted)
11988 swap_insertion (loc, *loc_first_p);
11989 loc->duplicate = 1;
11990
11991 /* Clear the condition modification flag. */
11992 loc->condition_changed = condition_unchanged;
11993 }
11994
11995 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11996 {
11997 if (insert_mode != UGLL_DONT_INSERT)
11998 insert_breakpoint_locations ();
11999 else
12000 {
12001 /* Even though the caller told us to not insert new
12002 locations, we may still need to update conditions on the
12003 target's side of breakpoints that were already inserted
12004 if the target is evaluating breakpoint conditions. We
12005 only update conditions for locations that are marked
12006 "needs_update". */
12007 update_inserted_breakpoint_locations ();
12008 }
12009 }
12010
12011 if (insert_mode != UGLL_DONT_INSERT)
12012 download_tracepoint_locations ();
12013 }
12014
12015 void
12016 breakpoint_retire_moribund (void)
12017 {
12018 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12019 {
12020 struct bp_location *loc = moribund_locations[ix];
12021 if (--(loc->events_till_retirement) == 0)
12022 {
12023 decref_bp_location (&loc);
12024 unordered_remove (moribund_locations, ix);
12025 --ix;
12026 }
12027 }
12028 }
12029
12030 static void
12031 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12032 {
12033
12034 try
12035 {
12036 update_global_location_list (insert_mode);
12037 }
12038 catch (const gdb_exception_error &e)
12039 {
12040 }
12041 }
12042
12043 /* Clear BKP from a BPS. */
12044
12045 static void
12046 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12047 {
12048 bpstat bs;
12049
12050 for (bs = bps; bs; bs = bs->next)
12051 if (bs->breakpoint_at == bpt)
12052 {
12053 bs->breakpoint_at = NULL;
12054 bs->old_val = NULL;
12055 /* bs->commands will be freed later. */
12056 }
12057 }
12058
12059 /* Callback for iterate_over_threads. */
12060 static int
12061 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12062 {
12063 struct breakpoint *bpt = (struct breakpoint *) data;
12064
12065 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12066 return 0;
12067 }
12068
12069 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12070 callbacks. */
12071
12072 static void
12073 say_where (struct breakpoint *b)
12074 {
12075 struct value_print_options opts;
12076
12077 get_user_print_options (&opts);
12078
12079 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12080 single string. */
12081 if (b->loc == NULL)
12082 {
12083 /* For pending locations, the output differs slightly based
12084 on b->extra_string. If this is non-NULL, it contains either
12085 a condition or dprintf arguments. */
12086 if (b->extra_string == NULL)
12087 {
12088 printf_filtered (_(" (%s) pending."),
12089 event_location_to_string (b->location.get ()));
12090 }
12091 else if (b->type == bp_dprintf)
12092 {
12093 printf_filtered (_(" (%s,%s) pending."),
12094 event_location_to_string (b->location.get ()),
12095 b->extra_string);
12096 }
12097 else
12098 {
12099 printf_filtered (_(" (%s %s) pending."),
12100 event_location_to_string (b->location.get ()),
12101 b->extra_string);
12102 }
12103 }
12104 else
12105 {
12106 if (opts.addressprint || b->loc->symtab == NULL)
12107 printf_filtered (" at %ps",
12108 styled_string (address_style.style (),
12109 paddress (b->loc->gdbarch,
12110 b->loc->address)));
12111 if (b->loc->symtab != NULL)
12112 {
12113 /* If there is a single location, we can print the location
12114 more nicely. */
12115 if (b->loc->next == NULL)
12116 {
12117 const char *filename
12118 = symtab_to_filename_for_display (b->loc->symtab);
12119 printf_filtered (": file %ps, line %d.",
12120 styled_string (file_name_style.style (),
12121 filename),
12122 b->loc->line_number);
12123 }
12124 else
12125 /* This is not ideal, but each location may have a
12126 different file name, and this at least reflects the
12127 real situation somewhat. */
12128 printf_filtered (": %s.",
12129 event_location_to_string (b->location.get ()));
12130 }
12131
12132 if (b->loc->next)
12133 {
12134 struct bp_location *loc = b->loc;
12135 int n = 0;
12136 for (; loc; loc = loc->next)
12137 ++n;
12138 printf_filtered (" (%d locations)", n);
12139 }
12140 }
12141 }
12142
12143 bp_location::~bp_location ()
12144 {
12145 xfree (function_name);
12146 }
12147
12148 /* Destructor for the breakpoint base class. */
12149
12150 breakpoint::~breakpoint ()
12151 {
12152 xfree (this->cond_string);
12153 xfree (this->extra_string);
12154 }
12155
12156 static struct bp_location *
12157 base_breakpoint_allocate_location (struct breakpoint *self)
12158 {
12159 return new bp_location (self);
12160 }
12161
12162 static void
12163 base_breakpoint_re_set (struct breakpoint *b)
12164 {
12165 /* Nothing to re-set. */
12166 }
12167
12168 #define internal_error_pure_virtual_called() \
12169 gdb_assert_not_reached ("pure virtual function called")
12170
12171 static int
12172 base_breakpoint_insert_location (struct bp_location *bl)
12173 {
12174 internal_error_pure_virtual_called ();
12175 }
12176
12177 static int
12178 base_breakpoint_remove_location (struct bp_location *bl,
12179 enum remove_bp_reason reason)
12180 {
12181 internal_error_pure_virtual_called ();
12182 }
12183
12184 static int
12185 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12186 const address_space *aspace,
12187 CORE_ADDR bp_addr,
12188 const struct target_waitstatus *ws)
12189 {
12190 internal_error_pure_virtual_called ();
12191 }
12192
12193 static void
12194 base_breakpoint_check_status (bpstat bs)
12195 {
12196 /* Always stop. */
12197 }
12198
12199 /* A "works_in_software_mode" breakpoint_ops method that just internal
12200 errors. */
12201
12202 static int
12203 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12204 {
12205 internal_error_pure_virtual_called ();
12206 }
12207
12208 /* A "resources_needed" breakpoint_ops method that just internal
12209 errors. */
12210
12211 static int
12212 base_breakpoint_resources_needed (const struct bp_location *bl)
12213 {
12214 internal_error_pure_virtual_called ();
12215 }
12216
12217 static enum print_stop_action
12218 base_breakpoint_print_it (bpstat bs)
12219 {
12220 internal_error_pure_virtual_called ();
12221 }
12222
12223 static void
12224 base_breakpoint_print_one_detail (const struct breakpoint *self,
12225 struct ui_out *uiout)
12226 {
12227 /* nothing */
12228 }
12229
12230 static void
12231 base_breakpoint_print_mention (struct breakpoint *b)
12232 {
12233 internal_error_pure_virtual_called ();
12234 }
12235
12236 static void
12237 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12238 {
12239 internal_error_pure_virtual_called ();
12240 }
12241
12242 static void
12243 base_breakpoint_create_sals_from_location
12244 (const struct event_location *location,
12245 struct linespec_result *canonical,
12246 enum bptype type_wanted)
12247 {
12248 internal_error_pure_virtual_called ();
12249 }
12250
12251 static void
12252 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12253 struct linespec_result *c,
12254 gdb::unique_xmalloc_ptr<char> cond_string,
12255 gdb::unique_xmalloc_ptr<char> extra_string,
12256 enum bptype type_wanted,
12257 enum bpdisp disposition,
12258 int thread,
12259 int task, int ignore_count,
12260 const struct breakpoint_ops *o,
12261 int from_tty, int enabled,
12262 int internal, unsigned flags)
12263 {
12264 internal_error_pure_virtual_called ();
12265 }
12266
12267 static std::vector<symtab_and_line>
12268 base_breakpoint_decode_location (struct breakpoint *b,
12269 const struct event_location *location,
12270 struct program_space *search_pspace)
12271 {
12272 internal_error_pure_virtual_called ();
12273 }
12274
12275 /* The default 'explains_signal' method. */
12276
12277 static int
12278 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12279 {
12280 return 1;
12281 }
12282
12283 /* The default "after_condition_true" method. */
12284
12285 static void
12286 base_breakpoint_after_condition_true (struct bpstats *bs)
12287 {
12288 /* Nothing to do. */
12289 }
12290
12291 struct breakpoint_ops base_breakpoint_ops =
12292 {
12293 base_breakpoint_allocate_location,
12294 base_breakpoint_re_set,
12295 base_breakpoint_insert_location,
12296 base_breakpoint_remove_location,
12297 base_breakpoint_breakpoint_hit,
12298 base_breakpoint_check_status,
12299 base_breakpoint_resources_needed,
12300 base_breakpoint_works_in_software_mode,
12301 base_breakpoint_print_it,
12302 NULL,
12303 base_breakpoint_print_one_detail,
12304 base_breakpoint_print_mention,
12305 base_breakpoint_print_recreate,
12306 base_breakpoint_create_sals_from_location,
12307 base_breakpoint_create_breakpoints_sal,
12308 base_breakpoint_decode_location,
12309 base_breakpoint_explains_signal,
12310 base_breakpoint_after_condition_true,
12311 };
12312
12313 /* Default breakpoint_ops methods. */
12314
12315 static void
12316 bkpt_re_set (struct breakpoint *b)
12317 {
12318 /* FIXME: is this still reachable? */
12319 if (breakpoint_event_location_empty_p (b))
12320 {
12321 /* Anything without a location can't be re-set. */
12322 delete_breakpoint (b);
12323 return;
12324 }
12325
12326 breakpoint_re_set_default (b);
12327 }
12328
12329 static int
12330 bkpt_insert_location (struct bp_location *bl)
12331 {
12332 CORE_ADDR addr = bl->target_info.reqstd_address;
12333
12334 bl->target_info.kind = breakpoint_kind (bl, &addr);
12335 bl->target_info.placed_address = addr;
12336
12337 if (bl->loc_type == bp_loc_hardware_breakpoint)
12338 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12339 else
12340 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12341 }
12342
12343 static int
12344 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12345 {
12346 if (bl->loc_type == bp_loc_hardware_breakpoint)
12347 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12348 else
12349 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12350 }
12351
12352 static int
12353 bkpt_breakpoint_hit (const struct bp_location *bl,
12354 const address_space *aspace, CORE_ADDR bp_addr,
12355 const struct target_waitstatus *ws)
12356 {
12357 if (ws->kind != TARGET_WAITKIND_STOPPED
12358 || ws->value.sig != GDB_SIGNAL_TRAP)
12359 return 0;
12360
12361 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12362 aspace, bp_addr))
12363 return 0;
12364
12365 if (overlay_debugging /* unmapped overlay section */
12366 && section_is_overlay (bl->section)
12367 && !section_is_mapped (bl->section))
12368 return 0;
12369
12370 return 1;
12371 }
12372
12373 static int
12374 dprintf_breakpoint_hit (const struct bp_location *bl,
12375 const address_space *aspace, CORE_ADDR bp_addr,
12376 const struct target_waitstatus *ws)
12377 {
12378 if (dprintf_style == dprintf_style_agent
12379 && target_can_run_breakpoint_commands ())
12380 {
12381 /* An agent-style dprintf never causes a stop. If we see a trap
12382 for this address it must be for a breakpoint that happens to
12383 be set at the same address. */
12384 return 0;
12385 }
12386
12387 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12388 }
12389
12390 static int
12391 bkpt_resources_needed (const struct bp_location *bl)
12392 {
12393 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12394
12395 return 1;
12396 }
12397
12398 static enum print_stop_action
12399 bkpt_print_it (bpstat bs)
12400 {
12401 struct breakpoint *b;
12402 const struct bp_location *bl;
12403 int bp_temp;
12404 struct ui_out *uiout = current_uiout;
12405
12406 gdb_assert (bs->bp_location_at != NULL);
12407
12408 bl = bs->bp_location_at;
12409 b = bs->breakpoint_at;
12410
12411 bp_temp = b->disposition == disp_del;
12412 if (bl->address != bl->requested_address)
12413 breakpoint_adjustment_warning (bl->requested_address,
12414 bl->address,
12415 b->number, 1);
12416 annotate_breakpoint (b->number);
12417 maybe_print_thread_hit_breakpoint (uiout);
12418
12419 if (uiout->is_mi_like_p ())
12420 {
12421 uiout->field_string ("reason",
12422 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12423 uiout->field_string ("disp", bpdisp_text (b->disposition));
12424 }
12425 if (bp_temp)
12426 uiout->message ("Temporary breakpoint %pF, ",
12427 signed_field ("bkptno", b->number));
12428 else
12429 uiout->message ("Breakpoint %pF, ",
12430 signed_field ("bkptno", b->number));
12431
12432 return PRINT_SRC_AND_LOC;
12433 }
12434
12435 static void
12436 bkpt_print_mention (struct breakpoint *b)
12437 {
12438 if (current_uiout->is_mi_like_p ())
12439 return;
12440
12441 switch (b->type)
12442 {
12443 case bp_breakpoint:
12444 case bp_gnu_ifunc_resolver:
12445 if (b->disposition == disp_del)
12446 printf_filtered (_("Temporary breakpoint"));
12447 else
12448 printf_filtered (_("Breakpoint"));
12449 printf_filtered (_(" %d"), b->number);
12450 if (b->type == bp_gnu_ifunc_resolver)
12451 printf_filtered (_(" at gnu-indirect-function resolver"));
12452 break;
12453 case bp_hardware_breakpoint:
12454 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12455 break;
12456 case bp_dprintf:
12457 printf_filtered (_("Dprintf %d"), b->number);
12458 break;
12459 }
12460
12461 say_where (b);
12462 }
12463
12464 static void
12465 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12466 {
12467 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12468 fprintf_unfiltered (fp, "tbreak");
12469 else if (tp->type == bp_breakpoint)
12470 fprintf_unfiltered (fp, "break");
12471 else if (tp->type == bp_hardware_breakpoint
12472 && tp->disposition == disp_del)
12473 fprintf_unfiltered (fp, "thbreak");
12474 else if (tp->type == bp_hardware_breakpoint)
12475 fprintf_unfiltered (fp, "hbreak");
12476 else
12477 internal_error (__FILE__, __LINE__,
12478 _("unhandled breakpoint type %d"), (int) tp->type);
12479
12480 fprintf_unfiltered (fp, " %s",
12481 event_location_to_string (tp->location.get ()));
12482
12483 /* Print out extra_string if this breakpoint is pending. It might
12484 contain, for example, conditions that were set by the user. */
12485 if (tp->loc == NULL && tp->extra_string != NULL)
12486 fprintf_unfiltered (fp, " %s", tp->extra_string);
12487
12488 print_recreate_thread (tp, fp);
12489 }
12490
12491 static void
12492 bkpt_create_sals_from_location (const struct event_location *location,
12493 struct linespec_result *canonical,
12494 enum bptype type_wanted)
12495 {
12496 create_sals_from_location_default (location, canonical, type_wanted);
12497 }
12498
12499 static void
12500 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12501 struct linespec_result *canonical,
12502 gdb::unique_xmalloc_ptr<char> cond_string,
12503 gdb::unique_xmalloc_ptr<char> extra_string,
12504 enum bptype type_wanted,
12505 enum bpdisp disposition,
12506 int thread,
12507 int task, int ignore_count,
12508 const struct breakpoint_ops *ops,
12509 int from_tty, int enabled,
12510 int internal, unsigned flags)
12511 {
12512 create_breakpoints_sal_default (gdbarch, canonical,
12513 std::move (cond_string),
12514 std::move (extra_string),
12515 type_wanted,
12516 disposition, thread, task,
12517 ignore_count, ops, from_tty,
12518 enabled, internal, flags);
12519 }
12520
12521 static std::vector<symtab_and_line>
12522 bkpt_decode_location (struct breakpoint *b,
12523 const struct event_location *location,
12524 struct program_space *search_pspace)
12525 {
12526 return decode_location_default (b, location, search_pspace);
12527 }
12528
12529 /* Virtual table for internal breakpoints. */
12530
12531 static void
12532 internal_bkpt_re_set (struct breakpoint *b)
12533 {
12534 switch (b->type)
12535 {
12536 /* Delete overlay event and longjmp master breakpoints; they
12537 will be reset later by breakpoint_re_set. */
12538 case bp_overlay_event:
12539 case bp_longjmp_master:
12540 case bp_std_terminate_master:
12541 case bp_exception_master:
12542 delete_breakpoint (b);
12543 break;
12544
12545 /* This breakpoint is special, it's set up when the inferior
12546 starts and we really don't want to touch it. */
12547 case bp_shlib_event:
12548
12549 /* Like bp_shlib_event, this breakpoint type is special. Once
12550 it is set up, we do not want to touch it. */
12551 case bp_thread_event:
12552 break;
12553 }
12554 }
12555
12556 static void
12557 internal_bkpt_check_status (bpstat bs)
12558 {
12559 if (bs->breakpoint_at->type == bp_shlib_event)
12560 {
12561 /* If requested, stop when the dynamic linker notifies GDB of
12562 events. This allows the user to get control and place
12563 breakpoints in initializer routines for dynamically loaded
12564 objects (among other things). */
12565 bs->stop = stop_on_solib_events;
12566 bs->print = stop_on_solib_events;
12567 }
12568 else
12569 bs->stop = 0;
12570 }
12571
12572 static enum print_stop_action
12573 internal_bkpt_print_it (bpstat bs)
12574 {
12575 struct breakpoint *b;
12576
12577 b = bs->breakpoint_at;
12578
12579 switch (b->type)
12580 {
12581 case bp_shlib_event:
12582 /* Did we stop because the user set the stop_on_solib_events
12583 variable? (If so, we report this as a generic, "Stopped due
12584 to shlib event" message.) */
12585 print_solib_event (0);
12586 break;
12587
12588 case bp_thread_event:
12589 /* Not sure how we will get here.
12590 GDB should not stop for these breakpoints. */
12591 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12592 break;
12593
12594 case bp_overlay_event:
12595 /* By analogy with the thread event, GDB should not stop for these. */
12596 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12597 break;
12598
12599 case bp_longjmp_master:
12600 /* These should never be enabled. */
12601 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12602 break;
12603
12604 case bp_std_terminate_master:
12605 /* These should never be enabled. */
12606 printf_filtered (_("std::terminate Master Breakpoint: "
12607 "gdb should not stop!\n"));
12608 break;
12609
12610 case bp_exception_master:
12611 /* These should never be enabled. */
12612 printf_filtered (_("Exception Master Breakpoint: "
12613 "gdb should not stop!\n"));
12614 break;
12615 }
12616
12617 return PRINT_NOTHING;
12618 }
12619
12620 static void
12621 internal_bkpt_print_mention (struct breakpoint *b)
12622 {
12623 /* Nothing to mention. These breakpoints are internal. */
12624 }
12625
12626 /* Virtual table for momentary breakpoints */
12627
12628 static void
12629 momentary_bkpt_re_set (struct breakpoint *b)
12630 {
12631 /* Keep temporary breakpoints, which can be encountered when we step
12632 over a dlopen call and solib_add is resetting the breakpoints.
12633 Otherwise these should have been blown away via the cleanup chain
12634 or by breakpoint_init_inferior when we rerun the executable. */
12635 }
12636
12637 static void
12638 momentary_bkpt_check_status (bpstat bs)
12639 {
12640 /* Nothing. The point of these breakpoints is causing a stop. */
12641 }
12642
12643 static enum print_stop_action
12644 momentary_bkpt_print_it (bpstat bs)
12645 {
12646 return PRINT_UNKNOWN;
12647 }
12648
12649 static void
12650 momentary_bkpt_print_mention (struct breakpoint *b)
12651 {
12652 /* Nothing to mention. These breakpoints are internal. */
12653 }
12654
12655 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12656
12657 It gets cleared already on the removal of the first one of such placed
12658 breakpoints. This is OK as they get all removed altogether. */
12659
12660 longjmp_breakpoint::~longjmp_breakpoint ()
12661 {
12662 thread_info *tp = find_thread_global_id (this->thread);
12663
12664 if (tp != NULL)
12665 tp->initiating_frame = null_frame_id;
12666 }
12667
12668 /* Specific methods for probe breakpoints. */
12669
12670 static int
12671 bkpt_probe_insert_location (struct bp_location *bl)
12672 {
12673 int v = bkpt_insert_location (bl);
12674
12675 if (v == 0)
12676 {
12677 /* The insertion was successful, now let's set the probe's semaphore
12678 if needed. */
12679 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12680 }
12681
12682 return v;
12683 }
12684
12685 static int
12686 bkpt_probe_remove_location (struct bp_location *bl,
12687 enum remove_bp_reason reason)
12688 {
12689 /* Let's clear the semaphore before removing the location. */
12690 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12691
12692 return bkpt_remove_location (bl, reason);
12693 }
12694
12695 static void
12696 bkpt_probe_create_sals_from_location (const struct event_location *location,
12697 struct linespec_result *canonical,
12698 enum bptype type_wanted)
12699 {
12700 struct linespec_sals lsal;
12701
12702 lsal.sals = parse_probes (location, NULL, canonical);
12703 lsal.canonical
12704 = xstrdup (event_location_to_string (canonical->location.get ()));
12705 canonical->lsals.push_back (std::move (lsal));
12706 }
12707
12708 static std::vector<symtab_and_line>
12709 bkpt_probe_decode_location (struct breakpoint *b,
12710 const struct event_location *location,
12711 struct program_space *search_pspace)
12712 {
12713 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12714 if (sals.empty ())
12715 error (_("probe not found"));
12716 return sals;
12717 }
12718
12719 /* The breakpoint_ops structure to be used in tracepoints. */
12720
12721 static void
12722 tracepoint_re_set (struct breakpoint *b)
12723 {
12724 breakpoint_re_set_default (b);
12725 }
12726
12727 static int
12728 tracepoint_breakpoint_hit (const struct bp_location *bl,
12729 const address_space *aspace, CORE_ADDR bp_addr,
12730 const struct target_waitstatus *ws)
12731 {
12732 /* By definition, the inferior does not report stops at
12733 tracepoints. */
12734 return 0;
12735 }
12736
12737 static void
12738 tracepoint_print_one_detail (const struct breakpoint *self,
12739 struct ui_out *uiout)
12740 {
12741 struct tracepoint *tp = (struct tracepoint *) self;
12742 if (!tp->static_trace_marker_id.empty ())
12743 {
12744 gdb_assert (self->type == bp_static_tracepoint);
12745
12746 uiout->message ("\tmarker id is %pF\n",
12747 string_field ("static-tracepoint-marker-string-id",
12748 tp->static_trace_marker_id.c_str ()));
12749 }
12750 }
12751
12752 static void
12753 tracepoint_print_mention (struct breakpoint *b)
12754 {
12755 if (current_uiout->is_mi_like_p ())
12756 return;
12757
12758 switch (b->type)
12759 {
12760 case bp_tracepoint:
12761 printf_filtered (_("Tracepoint"));
12762 printf_filtered (_(" %d"), b->number);
12763 break;
12764 case bp_fast_tracepoint:
12765 printf_filtered (_("Fast tracepoint"));
12766 printf_filtered (_(" %d"), b->number);
12767 break;
12768 case bp_static_tracepoint:
12769 printf_filtered (_("Static tracepoint"));
12770 printf_filtered (_(" %d"), b->number);
12771 break;
12772 default:
12773 internal_error (__FILE__, __LINE__,
12774 _("unhandled tracepoint type %d"), (int) b->type);
12775 }
12776
12777 say_where (b);
12778 }
12779
12780 static void
12781 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12782 {
12783 struct tracepoint *tp = (struct tracepoint *) self;
12784
12785 if (self->type == bp_fast_tracepoint)
12786 fprintf_unfiltered (fp, "ftrace");
12787 else if (self->type == bp_static_tracepoint)
12788 fprintf_unfiltered (fp, "strace");
12789 else if (self->type == bp_tracepoint)
12790 fprintf_unfiltered (fp, "trace");
12791 else
12792 internal_error (__FILE__, __LINE__,
12793 _("unhandled tracepoint type %d"), (int) self->type);
12794
12795 fprintf_unfiltered (fp, " %s",
12796 event_location_to_string (self->location.get ()));
12797 print_recreate_thread (self, fp);
12798
12799 if (tp->pass_count)
12800 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12801 }
12802
12803 static void
12804 tracepoint_create_sals_from_location (const struct event_location *location,
12805 struct linespec_result *canonical,
12806 enum bptype type_wanted)
12807 {
12808 create_sals_from_location_default (location, canonical, type_wanted);
12809 }
12810
12811 static void
12812 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12813 struct linespec_result *canonical,
12814 gdb::unique_xmalloc_ptr<char> cond_string,
12815 gdb::unique_xmalloc_ptr<char> extra_string,
12816 enum bptype type_wanted,
12817 enum bpdisp disposition,
12818 int thread,
12819 int task, int ignore_count,
12820 const struct breakpoint_ops *ops,
12821 int from_tty, int enabled,
12822 int internal, unsigned flags)
12823 {
12824 create_breakpoints_sal_default (gdbarch, canonical,
12825 std::move (cond_string),
12826 std::move (extra_string),
12827 type_wanted,
12828 disposition, thread, task,
12829 ignore_count, ops, from_tty,
12830 enabled, internal, flags);
12831 }
12832
12833 static std::vector<symtab_and_line>
12834 tracepoint_decode_location (struct breakpoint *b,
12835 const struct event_location *location,
12836 struct program_space *search_pspace)
12837 {
12838 return decode_location_default (b, location, search_pspace);
12839 }
12840
12841 struct breakpoint_ops tracepoint_breakpoint_ops;
12842
12843 /* Virtual table for tracepoints on static probes. */
12844
12845 static void
12846 tracepoint_probe_create_sals_from_location
12847 (const struct event_location *location,
12848 struct linespec_result *canonical,
12849 enum bptype type_wanted)
12850 {
12851 /* We use the same method for breakpoint on probes. */
12852 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12853 }
12854
12855 static std::vector<symtab_and_line>
12856 tracepoint_probe_decode_location (struct breakpoint *b,
12857 const struct event_location *location,
12858 struct program_space *search_pspace)
12859 {
12860 /* We use the same method for breakpoint on probes. */
12861 return bkpt_probe_decode_location (b, location, search_pspace);
12862 }
12863
12864 /* Dprintf breakpoint_ops methods. */
12865
12866 static void
12867 dprintf_re_set (struct breakpoint *b)
12868 {
12869 breakpoint_re_set_default (b);
12870
12871 /* extra_string should never be non-NULL for dprintf. */
12872 gdb_assert (b->extra_string != NULL);
12873
12874 /* 1 - connect to target 1, that can run breakpoint commands.
12875 2 - create a dprintf, which resolves fine.
12876 3 - disconnect from target 1
12877 4 - connect to target 2, that can NOT run breakpoint commands.
12878
12879 After steps #3/#4, you'll want the dprintf command list to
12880 be updated, because target 1 and 2 may well return different
12881 answers for target_can_run_breakpoint_commands().
12882 Given absence of finer grained resetting, we get to do
12883 it all the time. */
12884 if (b->extra_string != NULL)
12885 update_dprintf_command_list (b);
12886 }
12887
12888 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12889
12890 static void
12891 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12892 {
12893 fprintf_unfiltered (fp, "dprintf %s,%s",
12894 event_location_to_string (tp->location.get ()),
12895 tp->extra_string);
12896 print_recreate_thread (tp, fp);
12897 }
12898
12899 /* Implement the "after_condition_true" breakpoint_ops method for
12900 dprintf.
12901
12902 dprintf's are implemented with regular commands in their command
12903 list, but we run the commands here instead of before presenting the
12904 stop to the user, as dprintf's don't actually cause a stop. This
12905 also makes it so that the commands of multiple dprintfs at the same
12906 address are all handled. */
12907
12908 static void
12909 dprintf_after_condition_true (struct bpstats *bs)
12910 {
12911 struct bpstats tmp_bs;
12912 struct bpstats *tmp_bs_p = &tmp_bs;
12913
12914 /* dprintf's never cause a stop. This wasn't set in the
12915 check_status hook instead because that would make the dprintf's
12916 condition not be evaluated. */
12917 bs->stop = 0;
12918
12919 /* Run the command list here. Take ownership of it instead of
12920 copying. We never want these commands to run later in
12921 bpstat_do_actions, if a breakpoint that causes a stop happens to
12922 be set at same address as this dprintf, or even if running the
12923 commands here throws. */
12924 tmp_bs.commands = bs->commands;
12925 bs->commands = NULL;
12926
12927 bpstat_do_actions_1 (&tmp_bs_p);
12928
12929 /* 'tmp_bs.commands' will usually be NULL by now, but
12930 bpstat_do_actions_1 may return early without processing the whole
12931 list. */
12932 }
12933
12934 /* The breakpoint_ops structure to be used on static tracepoints with
12935 markers (`-m'). */
12936
12937 static void
12938 strace_marker_create_sals_from_location (const struct event_location *location,
12939 struct linespec_result *canonical,
12940 enum bptype type_wanted)
12941 {
12942 struct linespec_sals lsal;
12943 const char *arg_start, *arg;
12944
12945 arg = arg_start = get_linespec_location (location)->spec_string;
12946 lsal.sals = decode_static_tracepoint_spec (&arg);
12947
12948 std::string str (arg_start, arg - arg_start);
12949 const char *ptr = str.c_str ();
12950 canonical->location
12951 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12952
12953 lsal.canonical
12954 = xstrdup (event_location_to_string (canonical->location.get ()));
12955 canonical->lsals.push_back (std::move (lsal));
12956 }
12957
12958 static void
12959 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12960 struct linespec_result *canonical,
12961 gdb::unique_xmalloc_ptr<char> cond_string,
12962 gdb::unique_xmalloc_ptr<char> extra_string,
12963 enum bptype type_wanted,
12964 enum bpdisp disposition,
12965 int thread,
12966 int task, int ignore_count,
12967 const struct breakpoint_ops *ops,
12968 int from_tty, int enabled,
12969 int internal, unsigned flags)
12970 {
12971 const linespec_sals &lsal = canonical->lsals[0];
12972
12973 /* If the user is creating a static tracepoint by marker id
12974 (strace -m MARKER_ID), then store the sals index, so that
12975 breakpoint_re_set can try to match up which of the newly
12976 found markers corresponds to this one, and, don't try to
12977 expand multiple locations for each sal, given than SALS
12978 already should contain all sals for MARKER_ID. */
12979
12980 for (size_t i = 0; i < lsal.sals.size (); i++)
12981 {
12982 event_location_up location
12983 = copy_event_location (canonical->location.get ());
12984
12985 std::unique_ptr<tracepoint> tp (new tracepoint ());
12986 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
12987 std::move (location), NULL,
12988 std::move (cond_string),
12989 std::move (extra_string),
12990 type_wanted, disposition,
12991 thread, task, ignore_count, ops,
12992 from_tty, enabled, internal, flags,
12993 canonical->special_display);
12994 /* Given that its possible to have multiple markers with
12995 the same string id, if the user is creating a static
12996 tracepoint by marker id ("strace -m MARKER_ID"), then
12997 store the sals index, so that breakpoint_re_set can
12998 try to match up which of the newly found markers
12999 corresponds to this one */
13000 tp->static_trace_marker_id_idx = i;
13001
13002 install_breakpoint (internal, std::move (tp), 0);
13003 }
13004 }
13005
13006 static std::vector<symtab_and_line>
13007 strace_marker_decode_location (struct breakpoint *b,
13008 const struct event_location *location,
13009 struct program_space *search_pspace)
13010 {
13011 struct tracepoint *tp = (struct tracepoint *) b;
13012 const char *s = get_linespec_location (location)->spec_string;
13013
13014 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13015 if (sals.size () > tp->static_trace_marker_id_idx)
13016 {
13017 sals[0] = sals[tp->static_trace_marker_id_idx];
13018 sals.resize (1);
13019 return sals;
13020 }
13021 else
13022 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13023 }
13024
13025 static struct breakpoint_ops strace_marker_breakpoint_ops;
13026
13027 static int
13028 strace_marker_p (struct breakpoint *b)
13029 {
13030 return b->ops == &strace_marker_breakpoint_ops;
13031 }
13032
13033 /* Delete a breakpoint and clean up all traces of it in the data
13034 structures. */
13035
13036 void
13037 delete_breakpoint (struct breakpoint *bpt)
13038 {
13039 struct breakpoint *b;
13040
13041 gdb_assert (bpt != NULL);
13042
13043 /* Has this bp already been deleted? This can happen because
13044 multiple lists can hold pointers to bp's. bpstat lists are
13045 especial culprits.
13046
13047 One example of this happening is a watchpoint's scope bp. When
13048 the scope bp triggers, we notice that the watchpoint is out of
13049 scope, and delete it. We also delete its scope bp. But the
13050 scope bp is marked "auto-deleting", and is already on a bpstat.
13051 That bpstat is then checked for auto-deleting bp's, which are
13052 deleted.
13053
13054 A real solution to this problem might involve reference counts in
13055 bp's, and/or giving them pointers back to their referencing
13056 bpstat's, and teaching delete_breakpoint to only free a bp's
13057 storage when no more references were extent. A cheaper bandaid
13058 was chosen. */
13059 if (bpt->type == bp_none)
13060 return;
13061
13062 /* At least avoid this stale reference until the reference counting
13063 of breakpoints gets resolved. */
13064 if (bpt->related_breakpoint != bpt)
13065 {
13066 struct breakpoint *related;
13067 struct watchpoint *w;
13068
13069 if (bpt->type == bp_watchpoint_scope)
13070 w = (struct watchpoint *) bpt->related_breakpoint;
13071 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13072 w = (struct watchpoint *) bpt;
13073 else
13074 w = NULL;
13075 if (w != NULL)
13076 watchpoint_del_at_next_stop (w);
13077
13078 /* Unlink bpt from the bpt->related_breakpoint ring. */
13079 for (related = bpt; related->related_breakpoint != bpt;
13080 related = related->related_breakpoint);
13081 related->related_breakpoint = bpt->related_breakpoint;
13082 bpt->related_breakpoint = bpt;
13083 }
13084
13085 /* watch_command_1 creates a watchpoint but only sets its number if
13086 update_watchpoint succeeds in creating its bp_locations. If there's
13087 a problem in that process, we'll be asked to delete the half-created
13088 watchpoint. In that case, don't announce the deletion. */
13089 if (bpt->number)
13090 gdb::observers::breakpoint_deleted.notify (bpt);
13091
13092 if (breakpoint_chain == bpt)
13093 breakpoint_chain = bpt->next;
13094
13095 ALL_BREAKPOINTS (b)
13096 if (b->next == bpt)
13097 {
13098 b->next = bpt->next;
13099 break;
13100 }
13101
13102 /* Be sure no bpstat's are pointing at the breakpoint after it's
13103 been freed. */
13104 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13105 in all threads for now. Note that we cannot just remove bpstats
13106 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13107 commands are associated with the bpstat; if we remove it here,
13108 then the later call to bpstat_do_actions (&stop_bpstat); in
13109 event-top.c won't do anything, and temporary breakpoints with
13110 commands won't work. */
13111
13112 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13113
13114 /* Now that breakpoint is removed from breakpoint list, update the
13115 global location list. This will remove locations that used to
13116 belong to this breakpoint. Do this before freeing the breakpoint
13117 itself, since remove_breakpoint looks at location's owner. It
13118 might be better design to have location completely
13119 self-contained, but it's not the case now. */
13120 update_global_location_list (UGLL_DONT_INSERT);
13121
13122 /* On the chance that someone will soon try again to delete this
13123 same bp, we mark it as deleted before freeing its storage. */
13124 bpt->type = bp_none;
13125 delete bpt;
13126 }
13127
13128 /* Iterator function to call a user-provided callback function once
13129 for each of B and its related breakpoints. */
13130
13131 static void
13132 iterate_over_related_breakpoints (struct breakpoint *b,
13133 gdb::function_view<void (breakpoint *)> function)
13134 {
13135 struct breakpoint *related;
13136
13137 related = b;
13138 do
13139 {
13140 struct breakpoint *next;
13141
13142 /* FUNCTION may delete RELATED. */
13143 next = related->related_breakpoint;
13144
13145 if (next == related)
13146 {
13147 /* RELATED is the last ring entry. */
13148 function (related);
13149
13150 /* FUNCTION may have deleted it, so we'd never reach back to
13151 B. There's nothing left to do anyway, so just break
13152 out. */
13153 break;
13154 }
13155 else
13156 function (related);
13157
13158 related = next;
13159 }
13160 while (related != b);
13161 }
13162
13163 static void
13164 delete_command (const char *arg, int from_tty)
13165 {
13166 struct breakpoint *b, *b_tmp;
13167
13168 dont_repeat ();
13169
13170 if (arg == 0)
13171 {
13172 int breaks_to_delete = 0;
13173
13174 /* Delete all breakpoints if no argument. Do not delete
13175 internal breakpoints, these have to be deleted with an
13176 explicit breakpoint number argument. */
13177 ALL_BREAKPOINTS (b)
13178 if (user_breakpoint_p (b))
13179 {
13180 breaks_to_delete = 1;
13181 break;
13182 }
13183
13184 /* Ask user only if there are some breakpoints to delete. */
13185 if (!from_tty
13186 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13187 {
13188 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13189 if (user_breakpoint_p (b))
13190 delete_breakpoint (b);
13191 }
13192 }
13193 else
13194 map_breakpoint_numbers
13195 (arg, [&] (breakpoint *br)
13196 {
13197 iterate_over_related_breakpoints (br, delete_breakpoint);
13198 });
13199 }
13200
13201 /* Return true if all locations of B bound to PSPACE are pending. If
13202 PSPACE is NULL, all locations of all program spaces are
13203 considered. */
13204
13205 static int
13206 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13207 {
13208 struct bp_location *loc;
13209
13210 for (loc = b->loc; loc != NULL; loc = loc->next)
13211 if ((pspace == NULL
13212 || loc->pspace == pspace)
13213 && !loc->shlib_disabled
13214 && !loc->pspace->executing_startup)
13215 return 0;
13216 return 1;
13217 }
13218
13219 /* Subroutine of update_breakpoint_locations to simplify it.
13220 Return non-zero if multiple fns in list LOC have the same name.
13221 Null names are ignored. */
13222
13223 static int
13224 ambiguous_names_p (struct bp_location *loc)
13225 {
13226 struct bp_location *l;
13227 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13228 xcalloc, xfree);
13229
13230 for (l = loc; l != NULL; l = l->next)
13231 {
13232 const char **slot;
13233 const char *name = l->function_name;
13234
13235 /* Allow for some names to be NULL, ignore them. */
13236 if (name == NULL)
13237 continue;
13238
13239 slot = (const char **) htab_find_slot (htab, (const void *) name,
13240 INSERT);
13241 /* NOTE: We can assume slot != NULL here because xcalloc never
13242 returns NULL. */
13243 if (*slot != NULL)
13244 {
13245 htab_delete (htab);
13246 return 1;
13247 }
13248 *slot = name;
13249 }
13250
13251 htab_delete (htab);
13252 return 0;
13253 }
13254
13255 /* When symbols change, it probably means the sources changed as well,
13256 and it might mean the static tracepoint markers are no longer at
13257 the same address or line numbers they used to be at last we
13258 checked. Losing your static tracepoints whenever you rebuild is
13259 undesirable. This function tries to resync/rematch gdb static
13260 tracepoints with the markers on the target, for static tracepoints
13261 that have not been set by marker id. Static tracepoint that have
13262 been set by marker id are reset by marker id in breakpoint_re_set.
13263 The heuristic is:
13264
13265 1) For a tracepoint set at a specific address, look for a marker at
13266 the old PC. If one is found there, assume to be the same marker.
13267 If the name / string id of the marker found is different from the
13268 previous known name, assume that means the user renamed the marker
13269 in the sources, and output a warning.
13270
13271 2) For a tracepoint set at a given line number, look for a marker
13272 at the new address of the old line number. If one is found there,
13273 assume to be the same marker. If the name / string id of the
13274 marker found is different from the previous known name, assume that
13275 means the user renamed the marker in the sources, and output a
13276 warning.
13277
13278 3) If a marker is no longer found at the same address or line, it
13279 may mean the marker no longer exists. But it may also just mean
13280 the code changed a bit. Maybe the user added a few lines of code
13281 that made the marker move up or down (in line number terms). Ask
13282 the target for info about the marker with the string id as we knew
13283 it. If found, update line number and address in the matching
13284 static tracepoint. This will get confused if there's more than one
13285 marker with the same ID (possible in UST, although unadvised
13286 precisely because it confuses tools). */
13287
13288 static struct symtab_and_line
13289 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13290 {
13291 struct tracepoint *tp = (struct tracepoint *) b;
13292 struct static_tracepoint_marker marker;
13293 CORE_ADDR pc;
13294
13295 pc = sal.pc;
13296 if (sal.line)
13297 find_line_pc (sal.symtab, sal.line, &pc);
13298
13299 if (target_static_tracepoint_marker_at (pc, &marker))
13300 {
13301 if (tp->static_trace_marker_id != marker.str_id)
13302 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13303 b->number, tp->static_trace_marker_id.c_str (),
13304 marker.str_id.c_str ());
13305
13306 tp->static_trace_marker_id = std::move (marker.str_id);
13307
13308 return sal;
13309 }
13310
13311 /* Old marker wasn't found on target at lineno. Try looking it up
13312 by string ID. */
13313 if (!sal.explicit_pc
13314 && sal.line != 0
13315 && sal.symtab != NULL
13316 && !tp->static_trace_marker_id.empty ())
13317 {
13318 std::vector<static_tracepoint_marker> markers
13319 = target_static_tracepoint_markers_by_strid
13320 (tp->static_trace_marker_id.c_str ());
13321
13322 if (!markers.empty ())
13323 {
13324 struct symbol *sym;
13325 struct static_tracepoint_marker *tpmarker;
13326 struct ui_out *uiout = current_uiout;
13327 struct explicit_location explicit_loc;
13328
13329 tpmarker = &markers[0];
13330
13331 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13332
13333 warning (_("marker for static tracepoint %d (%s) not "
13334 "found at previous line number"),
13335 b->number, tp->static_trace_marker_id.c_str ());
13336
13337 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13338 sym = find_pc_sect_function (tpmarker->address, NULL);
13339 uiout->text ("Now in ");
13340 if (sym)
13341 {
13342 uiout->field_string ("func", sym->print_name (),
13343 function_name_style.style ());
13344 uiout->text (" at ");
13345 }
13346 uiout->field_string ("file",
13347 symtab_to_filename_for_display (sal2.symtab),
13348 file_name_style.style ());
13349 uiout->text (":");
13350
13351 if (uiout->is_mi_like_p ())
13352 {
13353 const char *fullname = symtab_to_fullname (sal2.symtab);
13354
13355 uiout->field_string ("fullname", fullname);
13356 }
13357
13358 uiout->field_signed ("line", sal2.line);
13359 uiout->text ("\n");
13360
13361 b->loc->line_number = sal2.line;
13362 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13363
13364 b->location.reset (NULL);
13365 initialize_explicit_location (&explicit_loc);
13366 explicit_loc.source_filename
13367 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13368 explicit_loc.line_offset.offset = b->loc->line_number;
13369 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13370 b->location = new_explicit_location (&explicit_loc);
13371
13372 /* Might be nice to check if function changed, and warn if
13373 so. */
13374 }
13375 }
13376 return sal;
13377 }
13378
13379 /* Returns 1 iff locations A and B are sufficiently same that
13380 we don't need to report breakpoint as changed. */
13381
13382 static int
13383 locations_are_equal (struct bp_location *a, struct bp_location *b)
13384 {
13385 while (a && b)
13386 {
13387 if (a->address != b->address)
13388 return 0;
13389
13390 if (a->shlib_disabled != b->shlib_disabled)
13391 return 0;
13392
13393 if (a->enabled != b->enabled)
13394 return 0;
13395
13396 a = a->next;
13397 b = b->next;
13398 }
13399
13400 if ((a == NULL) != (b == NULL))
13401 return 0;
13402
13403 return 1;
13404 }
13405
13406 /* Split all locations of B that are bound to PSPACE out of B's
13407 location list to a separate list and return that list's head. If
13408 PSPACE is NULL, hoist out all locations of B. */
13409
13410 static struct bp_location *
13411 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13412 {
13413 struct bp_location head;
13414 struct bp_location *i = b->loc;
13415 struct bp_location **i_link = &b->loc;
13416 struct bp_location *hoisted = &head;
13417
13418 if (pspace == NULL)
13419 {
13420 i = b->loc;
13421 b->loc = NULL;
13422 return i;
13423 }
13424
13425 head.next = NULL;
13426
13427 while (i != NULL)
13428 {
13429 if (i->pspace == pspace)
13430 {
13431 *i_link = i->next;
13432 i->next = NULL;
13433 hoisted->next = i;
13434 hoisted = i;
13435 }
13436 else
13437 i_link = &i->next;
13438 i = *i_link;
13439 }
13440
13441 return head.next;
13442 }
13443
13444 /* Create new breakpoint locations for B (a hardware or software
13445 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13446 zero, then B is a ranged breakpoint. Only recreates locations for
13447 FILTER_PSPACE. Locations of other program spaces are left
13448 untouched. */
13449
13450 void
13451 update_breakpoint_locations (struct breakpoint *b,
13452 struct program_space *filter_pspace,
13453 gdb::array_view<const symtab_and_line> sals,
13454 gdb::array_view<const symtab_and_line> sals_end)
13455 {
13456 struct bp_location *existing_locations;
13457
13458 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13459 {
13460 /* Ranged breakpoints have only one start location and one end
13461 location. */
13462 b->enable_state = bp_disabled;
13463 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13464 "multiple locations found\n"),
13465 b->number);
13466 return;
13467 }
13468
13469 /* If there's no new locations, and all existing locations are
13470 pending, don't do anything. This optimizes the common case where
13471 all locations are in the same shared library, that was unloaded.
13472 We'd like to retain the location, so that when the library is
13473 loaded again, we don't loose the enabled/disabled status of the
13474 individual locations. */
13475 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13476 return;
13477
13478 existing_locations = hoist_existing_locations (b, filter_pspace);
13479
13480 for (const auto &sal : sals)
13481 {
13482 struct bp_location *new_loc;
13483
13484 switch_to_program_space_and_thread (sal.pspace);
13485
13486 new_loc = add_location_to_breakpoint (b, &sal);
13487
13488 /* Reparse conditions, they might contain references to the
13489 old symtab. */
13490 if (b->cond_string != NULL)
13491 {
13492 const char *s;
13493
13494 s = b->cond_string;
13495 try
13496 {
13497 new_loc->cond = parse_exp_1 (&s, sal.pc,
13498 block_for_pc (sal.pc),
13499 0);
13500 }
13501 catch (const gdb_exception_error &e)
13502 {
13503 warning (_("failed to reevaluate condition "
13504 "for breakpoint %d: %s"),
13505 b->number, e.what ());
13506 new_loc->enabled = 0;
13507 }
13508 }
13509
13510 if (!sals_end.empty ())
13511 {
13512 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13513
13514 new_loc->length = end - sals[0].pc + 1;
13515 }
13516 }
13517
13518 /* If possible, carry over 'disable' status from existing
13519 breakpoints. */
13520 {
13521 struct bp_location *e = existing_locations;
13522 /* If there are multiple breakpoints with the same function name,
13523 e.g. for inline functions, comparing function names won't work.
13524 Instead compare pc addresses; this is just a heuristic as things
13525 may have moved, but in practice it gives the correct answer
13526 often enough until a better solution is found. */
13527 int have_ambiguous_names = ambiguous_names_p (b->loc);
13528
13529 for (; e; e = e->next)
13530 {
13531 if (!e->enabled && e->function_name)
13532 {
13533 struct bp_location *l = b->loc;
13534 if (have_ambiguous_names)
13535 {
13536 for (; l; l = l->next)
13537 if (breakpoint_locations_match (e, l))
13538 {
13539 l->enabled = 0;
13540 break;
13541 }
13542 }
13543 else
13544 {
13545 for (; l; l = l->next)
13546 if (l->function_name
13547 && strcmp (e->function_name, l->function_name) == 0)
13548 {
13549 l->enabled = 0;
13550 break;
13551 }
13552 }
13553 }
13554 }
13555 }
13556
13557 if (!locations_are_equal (existing_locations, b->loc))
13558 gdb::observers::breakpoint_modified.notify (b);
13559 }
13560
13561 /* Find the SaL locations corresponding to the given LOCATION.
13562 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13563
13564 static std::vector<symtab_and_line>
13565 location_to_sals (struct breakpoint *b, struct event_location *location,
13566 struct program_space *search_pspace, int *found)
13567 {
13568 struct gdb_exception exception;
13569
13570 gdb_assert (b->ops != NULL);
13571
13572 std::vector<symtab_and_line> sals;
13573
13574 try
13575 {
13576 sals = b->ops->decode_location (b, location, search_pspace);
13577 }
13578 catch (gdb_exception_error &e)
13579 {
13580 int not_found_and_ok = 0;
13581
13582 /* For pending breakpoints, it's expected that parsing will
13583 fail until the right shared library is loaded. User has
13584 already told to create pending breakpoints and don't need
13585 extra messages. If breakpoint is in bp_shlib_disabled
13586 state, then user already saw the message about that
13587 breakpoint being disabled, and don't want to see more
13588 errors. */
13589 if (e.error == NOT_FOUND_ERROR
13590 && (b->condition_not_parsed
13591 || (b->loc != NULL
13592 && search_pspace != NULL
13593 && b->loc->pspace != search_pspace)
13594 || (b->loc && b->loc->shlib_disabled)
13595 || (b->loc && b->loc->pspace->executing_startup)
13596 || b->enable_state == bp_disabled))
13597 not_found_and_ok = 1;
13598
13599 if (!not_found_and_ok)
13600 {
13601 /* We surely don't want to warn about the same breakpoint
13602 10 times. One solution, implemented here, is disable
13603 the breakpoint on error. Another solution would be to
13604 have separate 'warning emitted' flag. Since this
13605 happens only when a binary has changed, I don't know
13606 which approach is better. */
13607 b->enable_state = bp_disabled;
13608 throw;
13609 }
13610
13611 exception = std::move (e);
13612 }
13613
13614 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13615 {
13616 for (auto &sal : sals)
13617 resolve_sal_pc (&sal);
13618 if (b->condition_not_parsed && b->extra_string != NULL)
13619 {
13620 char *cond_string, *extra_string;
13621 int thread, task;
13622
13623 find_condition_and_thread (b->extra_string, sals[0].pc,
13624 &cond_string, &thread, &task,
13625 &extra_string);
13626 gdb_assert (b->cond_string == NULL);
13627 if (cond_string)
13628 b->cond_string = cond_string;
13629 b->thread = thread;
13630 b->task = task;
13631 if (extra_string)
13632 {
13633 xfree (b->extra_string);
13634 b->extra_string = extra_string;
13635 }
13636 b->condition_not_parsed = 0;
13637 }
13638
13639 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13640 sals[0] = update_static_tracepoint (b, sals[0]);
13641
13642 *found = 1;
13643 }
13644 else
13645 *found = 0;
13646
13647 return sals;
13648 }
13649
13650 /* The default re_set method, for typical hardware or software
13651 breakpoints. Reevaluate the breakpoint and recreate its
13652 locations. */
13653
13654 static void
13655 breakpoint_re_set_default (struct breakpoint *b)
13656 {
13657 struct program_space *filter_pspace = current_program_space;
13658 std::vector<symtab_and_line> expanded, expanded_end;
13659
13660 int found;
13661 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13662 filter_pspace, &found);
13663 if (found)
13664 expanded = std::move (sals);
13665
13666 if (b->location_range_end != NULL)
13667 {
13668 std::vector<symtab_and_line> sals_end
13669 = location_to_sals (b, b->location_range_end.get (),
13670 filter_pspace, &found);
13671 if (found)
13672 expanded_end = std::move (sals_end);
13673 }
13674
13675 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13676 }
13677
13678 /* Default method for creating SALs from an address string. It basically
13679 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13680
13681 static void
13682 create_sals_from_location_default (const struct event_location *location,
13683 struct linespec_result *canonical,
13684 enum bptype type_wanted)
13685 {
13686 parse_breakpoint_sals (location, canonical);
13687 }
13688
13689 /* Call create_breakpoints_sal for the given arguments. This is the default
13690 function for the `create_breakpoints_sal' method of
13691 breakpoint_ops. */
13692
13693 static void
13694 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13695 struct linespec_result *canonical,
13696 gdb::unique_xmalloc_ptr<char> cond_string,
13697 gdb::unique_xmalloc_ptr<char> extra_string,
13698 enum bptype type_wanted,
13699 enum bpdisp disposition,
13700 int thread,
13701 int task, int ignore_count,
13702 const struct breakpoint_ops *ops,
13703 int from_tty, int enabled,
13704 int internal, unsigned flags)
13705 {
13706 create_breakpoints_sal (gdbarch, canonical,
13707 std::move (cond_string),
13708 std::move (extra_string),
13709 type_wanted, disposition,
13710 thread, task, ignore_count, ops, from_tty,
13711 enabled, internal, flags);
13712 }
13713
13714 /* Decode the line represented by S by calling decode_line_full. This is the
13715 default function for the `decode_location' method of breakpoint_ops. */
13716
13717 static std::vector<symtab_and_line>
13718 decode_location_default (struct breakpoint *b,
13719 const struct event_location *location,
13720 struct program_space *search_pspace)
13721 {
13722 struct linespec_result canonical;
13723
13724 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13725 NULL, 0, &canonical, multiple_symbols_all,
13726 b->filter.get ());
13727
13728 /* We should get 0 or 1 resulting SALs. */
13729 gdb_assert (canonical.lsals.size () < 2);
13730
13731 if (!canonical.lsals.empty ())
13732 {
13733 const linespec_sals &lsal = canonical.lsals[0];
13734 return std::move (lsal.sals);
13735 }
13736 return {};
13737 }
13738
13739 /* Reset a breakpoint. */
13740
13741 static void
13742 breakpoint_re_set_one (breakpoint *b)
13743 {
13744 input_radix = b->input_radix;
13745 set_language (b->language);
13746
13747 b->ops->re_set (b);
13748 }
13749
13750 /* Re-set breakpoint locations for the current program space.
13751 Locations bound to other program spaces are left untouched. */
13752
13753 void
13754 breakpoint_re_set (void)
13755 {
13756 struct breakpoint *b, *b_tmp;
13757
13758 {
13759 scoped_restore_current_language save_language;
13760 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13761 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13762
13763 /* breakpoint_re_set_one sets the current_language to the language
13764 of the breakpoint it is resetting (see prepare_re_set_context)
13765 before re-evaluating the breakpoint's location. This change can
13766 unfortunately get undone by accident if the language_mode is set
13767 to auto, and we either switch frames, or more likely in this context,
13768 we select the current frame.
13769
13770 We prevent this by temporarily turning the language_mode to
13771 language_mode_manual. We restore it once all breakpoints
13772 have been reset. */
13773 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13774 language_mode = language_mode_manual;
13775
13776 /* Note: we must not try to insert locations until after all
13777 breakpoints have been re-set. Otherwise, e.g., when re-setting
13778 breakpoint 1, we'd insert the locations of breakpoint 2, which
13779 hadn't been re-set yet, and thus may have stale locations. */
13780
13781 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13782 {
13783 try
13784 {
13785 breakpoint_re_set_one (b);
13786 }
13787 catch (const gdb_exception &ex)
13788 {
13789 exception_fprintf (gdb_stderr, ex,
13790 "Error in re-setting breakpoint %d: ",
13791 b->number);
13792 }
13793 }
13794
13795 jit_breakpoint_re_set ();
13796 }
13797
13798 create_overlay_event_breakpoint ();
13799 create_longjmp_master_breakpoint ();
13800 create_std_terminate_master_breakpoint ();
13801 create_exception_master_breakpoint ();
13802
13803 /* Now we can insert. */
13804 update_global_location_list (UGLL_MAY_INSERT);
13805 }
13806 \f
13807 /* Reset the thread number of this breakpoint:
13808
13809 - If the breakpoint is for all threads, leave it as-is.
13810 - Else, reset it to the current thread for inferior_ptid. */
13811 void
13812 breakpoint_re_set_thread (struct breakpoint *b)
13813 {
13814 if (b->thread != -1)
13815 {
13816 b->thread = inferior_thread ()->global_num;
13817
13818 /* We're being called after following a fork. The new fork is
13819 selected as current, and unless this was a vfork will have a
13820 different program space from the original thread. Reset that
13821 as well. */
13822 b->loc->pspace = current_program_space;
13823 }
13824 }
13825
13826 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13827 If from_tty is nonzero, it prints a message to that effect,
13828 which ends with a period (no newline). */
13829
13830 void
13831 set_ignore_count (int bptnum, int count, int from_tty)
13832 {
13833 struct breakpoint *b;
13834
13835 if (count < 0)
13836 count = 0;
13837
13838 ALL_BREAKPOINTS (b)
13839 if (b->number == bptnum)
13840 {
13841 if (is_tracepoint (b))
13842 {
13843 if (from_tty && count != 0)
13844 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13845 bptnum);
13846 return;
13847 }
13848
13849 b->ignore_count = count;
13850 if (from_tty)
13851 {
13852 if (count == 0)
13853 printf_filtered (_("Will stop next time "
13854 "breakpoint %d is reached."),
13855 bptnum);
13856 else if (count == 1)
13857 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13858 bptnum);
13859 else
13860 printf_filtered (_("Will ignore next %d "
13861 "crossings of breakpoint %d."),
13862 count, bptnum);
13863 }
13864 gdb::observers::breakpoint_modified.notify (b);
13865 return;
13866 }
13867
13868 error (_("No breakpoint number %d."), bptnum);
13869 }
13870
13871 /* Command to set ignore-count of breakpoint N to COUNT. */
13872
13873 static void
13874 ignore_command (const char *args, int from_tty)
13875 {
13876 const char *p = args;
13877 int num;
13878
13879 if (p == 0)
13880 error_no_arg (_("a breakpoint number"));
13881
13882 num = get_number (&p);
13883 if (num == 0)
13884 error (_("bad breakpoint number: '%s'"), args);
13885 if (*p == 0)
13886 error (_("Second argument (specified ignore-count) is missing."));
13887
13888 set_ignore_count (num,
13889 longest_to_int (value_as_long (parse_and_eval (p))),
13890 from_tty);
13891 if (from_tty)
13892 printf_filtered ("\n");
13893 }
13894 \f
13895
13896 /* Call FUNCTION on each of the breakpoints with numbers in the range
13897 defined by BP_NUM_RANGE (an inclusive range). */
13898
13899 static void
13900 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13901 gdb::function_view<void (breakpoint *)> function)
13902 {
13903 if (bp_num_range.first == 0)
13904 {
13905 warning (_("bad breakpoint number at or near '%d'"),
13906 bp_num_range.first);
13907 }
13908 else
13909 {
13910 struct breakpoint *b, *tmp;
13911
13912 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13913 {
13914 bool match = false;
13915
13916 ALL_BREAKPOINTS_SAFE (b, tmp)
13917 if (b->number == i)
13918 {
13919 match = true;
13920 function (b);
13921 break;
13922 }
13923 if (!match)
13924 printf_unfiltered (_("No breakpoint number %d.\n"), i);
13925 }
13926 }
13927 }
13928
13929 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13930 ARGS. */
13931
13932 static void
13933 map_breakpoint_numbers (const char *args,
13934 gdb::function_view<void (breakpoint *)> function)
13935 {
13936 if (args == NULL || *args == '\0')
13937 error_no_arg (_("one or more breakpoint numbers"));
13938
13939 number_or_range_parser parser (args);
13940
13941 while (!parser.finished ())
13942 {
13943 int num = parser.get_number ();
13944 map_breakpoint_number_range (std::make_pair (num, num), function);
13945 }
13946 }
13947
13948 /* Return the breakpoint location structure corresponding to the
13949 BP_NUM and LOC_NUM values. */
13950
13951 static struct bp_location *
13952 find_location_by_number (int bp_num, int loc_num)
13953 {
13954 struct breakpoint *b;
13955
13956 ALL_BREAKPOINTS (b)
13957 if (b->number == bp_num)
13958 {
13959 break;
13960 }
13961
13962 if (!b || b->number != bp_num)
13963 error (_("Bad breakpoint number '%d'"), bp_num);
13964
13965 if (loc_num == 0)
13966 error (_("Bad breakpoint location number '%d'"), loc_num);
13967
13968 int n = 0;
13969 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
13970 if (++n == loc_num)
13971 return loc;
13972
13973 error (_("Bad breakpoint location number '%d'"), loc_num);
13974 }
13975
13976 /* Modes of operation for extract_bp_num. */
13977 enum class extract_bp_kind
13978 {
13979 /* Extracting a breakpoint number. */
13980 bp,
13981
13982 /* Extracting a location number. */
13983 loc,
13984 };
13985
13986 /* Extract a breakpoint or location number (as determined by KIND)
13987 from the string starting at START. TRAILER is a character which
13988 can be found after the number. If you don't want a trailer, use
13989 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13990 string. This always returns a positive integer. */
13991
13992 static int
13993 extract_bp_num (extract_bp_kind kind, const char *start,
13994 int trailer, const char **end_out = NULL)
13995 {
13996 const char *end = start;
13997 int num = get_number_trailer (&end, trailer);
13998 if (num < 0)
13999 error (kind == extract_bp_kind::bp
14000 ? _("Negative breakpoint number '%.*s'")
14001 : _("Negative breakpoint location number '%.*s'"),
14002 int (end - start), start);
14003 if (num == 0)
14004 error (kind == extract_bp_kind::bp
14005 ? _("Bad breakpoint number '%.*s'")
14006 : _("Bad breakpoint location number '%.*s'"),
14007 int (end - start), start);
14008
14009 if (end_out != NULL)
14010 *end_out = end;
14011 return num;
14012 }
14013
14014 /* Extract a breakpoint or location range (as determined by KIND) in
14015 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14016 representing the (inclusive) range. The returned pair's elements
14017 are always positive integers. */
14018
14019 static std::pair<int, int>
14020 extract_bp_or_bp_range (extract_bp_kind kind,
14021 const std::string &arg,
14022 std::string::size_type arg_offset)
14023 {
14024 std::pair<int, int> range;
14025 const char *bp_loc = &arg[arg_offset];
14026 std::string::size_type dash = arg.find ('-', arg_offset);
14027 if (dash != std::string::npos)
14028 {
14029 /* bp_loc is a range (x-z). */
14030 if (arg.length () == dash + 1)
14031 error (kind == extract_bp_kind::bp
14032 ? _("Bad breakpoint number at or near: '%s'")
14033 : _("Bad breakpoint location number at or near: '%s'"),
14034 bp_loc);
14035
14036 const char *end;
14037 const char *start_first = bp_loc;
14038 const char *start_second = &arg[dash + 1];
14039 range.first = extract_bp_num (kind, start_first, '-');
14040 range.second = extract_bp_num (kind, start_second, '\0', &end);
14041
14042 if (range.first > range.second)
14043 error (kind == extract_bp_kind::bp
14044 ? _("Inverted breakpoint range at '%.*s'")
14045 : _("Inverted breakpoint location range at '%.*s'"),
14046 int (end - start_first), start_first);
14047 }
14048 else
14049 {
14050 /* bp_loc is a single value. */
14051 range.first = extract_bp_num (kind, bp_loc, '\0');
14052 range.second = range.first;
14053 }
14054 return range;
14055 }
14056
14057 /* Extract the breakpoint/location range specified by ARG. Returns
14058 the breakpoint range in BP_NUM_RANGE, and the location range in
14059 BP_LOC_RANGE.
14060
14061 ARG may be in any of the following forms:
14062
14063 x where 'x' is a breakpoint number.
14064 x-y where 'x' and 'y' specify a breakpoint numbers range.
14065 x.y where 'x' is a breakpoint number and 'y' a location number.
14066 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14067 location number range.
14068 */
14069
14070 static void
14071 extract_bp_number_and_location (const std::string &arg,
14072 std::pair<int, int> &bp_num_range,
14073 std::pair<int, int> &bp_loc_range)
14074 {
14075 std::string::size_type dot = arg.find ('.');
14076
14077 if (dot != std::string::npos)
14078 {
14079 /* Handle 'x.y' and 'x.y-z' cases. */
14080
14081 if (arg.length () == dot + 1 || dot == 0)
14082 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14083
14084 bp_num_range.first
14085 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14086 bp_num_range.second = bp_num_range.first;
14087
14088 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14089 arg, dot + 1);
14090 }
14091 else
14092 {
14093 /* Handle x and x-y cases. */
14094
14095 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14096 bp_loc_range.first = 0;
14097 bp_loc_range.second = 0;
14098 }
14099 }
14100
14101 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14102 specifies whether to enable or disable. */
14103
14104 static void
14105 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14106 {
14107 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14108 if (loc != NULL)
14109 {
14110 if (loc->enabled != enable)
14111 {
14112 loc->enabled = enable;
14113 mark_breakpoint_location_modified (loc);
14114 }
14115 if (target_supports_enable_disable_tracepoint ()
14116 && current_trace_status ()->running && loc->owner
14117 && is_tracepoint (loc->owner))
14118 target_disable_tracepoint (loc);
14119 }
14120 update_global_location_list (UGLL_DONT_INSERT);
14121
14122 gdb::observers::breakpoint_modified.notify (loc->owner);
14123 }
14124
14125 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14126 number of the breakpoint, and BP_LOC_RANGE specifies the
14127 (inclusive) range of location numbers of that breakpoint to
14128 enable/disable. ENABLE specifies whether to enable or disable the
14129 location. */
14130
14131 static void
14132 enable_disable_breakpoint_location_range (int bp_num,
14133 std::pair<int, int> &bp_loc_range,
14134 bool enable)
14135 {
14136 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14137 enable_disable_bp_num_loc (bp_num, i, enable);
14138 }
14139
14140 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14141 If from_tty is nonzero, it prints a message to that effect,
14142 which ends with a period (no newline). */
14143
14144 void
14145 disable_breakpoint (struct breakpoint *bpt)
14146 {
14147 /* Never disable a watchpoint scope breakpoint; we want to
14148 hit them when we leave scope so we can delete both the
14149 watchpoint and its scope breakpoint at that time. */
14150 if (bpt->type == bp_watchpoint_scope)
14151 return;
14152
14153 bpt->enable_state = bp_disabled;
14154
14155 /* Mark breakpoint locations modified. */
14156 mark_breakpoint_modified (bpt);
14157
14158 if (target_supports_enable_disable_tracepoint ()
14159 && current_trace_status ()->running && is_tracepoint (bpt))
14160 {
14161 struct bp_location *location;
14162
14163 for (location = bpt->loc; location; location = location->next)
14164 target_disable_tracepoint (location);
14165 }
14166
14167 update_global_location_list (UGLL_DONT_INSERT);
14168
14169 gdb::observers::breakpoint_modified.notify (bpt);
14170 }
14171
14172 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14173 specified in ARGS. ARGS may be in any of the formats handled by
14174 extract_bp_number_and_location. ENABLE specifies whether to enable
14175 or disable the breakpoints/locations. */
14176
14177 static void
14178 enable_disable_command (const char *args, int from_tty, bool enable)
14179 {
14180 if (args == 0)
14181 {
14182 struct breakpoint *bpt;
14183
14184 ALL_BREAKPOINTS (bpt)
14185 if (user_breakpoint_p (bpt))
14186 {
14187 if (enable)
14188 enable_breakpoint (bpt);
14189 else
14190 disable_breakpoint (bpt);
14191 }
14192 }
14193 else
14194 {
14195 std::string num = extract_arg (&args);
14196
14197 while (!num.empty ())
14198 {
14199 std::pair<int, int> bp_num_range, bp_loc_range;
14200
14201 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14202
14203 if (bp_loc_range.first == bp_loc_range.second
14204 && bp_loc_range.first == 0)
14205 {
14206 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14207 map_breakpoint_number_range (bp_num_range,
14208 enable
14209 ? enable_breakpoint
14210 : disable_breakpoint);
14211 }
14212 else
14213 {
14214 /* Handle breakpoint ids with formats 'x.y' or
14215 'x.y-z'. */
14216 enable_disable_breakpoint_location_range
14217 (bp_num_range.first, bp_loc_range, enable);
14218 }
14219 num = extract_arg (&args);
14220 }
14221 }
14222 }
14223
14224 /* The disable command disables the specified breakpoints/locations
14225 (or all defined breakpoints) so they're no longer effective in
14226 stopping the inferior. ARGS may be in any of the forms defined in
14227 extract_bp_number_and_location. */
14228
14229 static void
14230 disable_command (const char *args, int from_tty)
14231 {
14232 enable_disable_command (args, from_tty, false);
14233 }
14234
14235 static void
14236 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14237 int count)
14238 {
14239 int target_resources_ok;
14240
14241 if (bpt->type == bp_hardware_breakpoint)
14242 {
14243 int i;
14244 i = hw_breakpoint_used_count ();
14245 target_resources_ok =
14246 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14247 i + 1, 0);
14248 if (target_resources_ok == 0)
14249 error (_("No hardware breakpoint support in the target."));
14250 else if (target_resources_ok < 0)
14251 error (_("Hardware breakpoints used exceeds limit."));
14252 }
14253
14254 if (is_watchpoint (bpt))
14255 {
14256 /* Initialize it just to avoid a GCC false warning. */
14257 enum enable_state orig_enable_state = bp_disabled;
14258
14259 try
14260 {
14261 struct watchpoint *w = (struct watchpoint *) bpt;
14262
14263 orig_enable_state = bpt->enable_state;
14264 bpt->enable_state = bp_enabled;
14265 update_watchpoint (w, 1 /* reparse */);
14266 }
14267 catch (const gdb_exception &e)
14268 {
14269 bpt->enable_state = orig_enable_state;
14270 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14271 bpt->number);
14272 return;
14273 }
14274 }
14275
14276 bpt->enable_state = bp_enabled;
14277
14278 /* Mark breakpoint locations modified. */
14279 mark_breakpoint_modified (bpt);
14280
14281 if (target_supports_enable_disable_tracepoint ()
14282 && current_trace_status ()->running && is_tracepoint (bpt))
14283 {
14284 struct bp_location *location;
14285
14286 for (location = bpt->loc; location; location = location->next)
14287 target_enable_tracepoint (location);
14288 }
14289
14290 bpt->disposition = disposition;
14291 bpt->enable_count = count;
14292 update_global_location_list (UGLL_MAY_INSERT);
14293
14294 gdb::observers::breakpoint_modified.notify (bpt);
14295 }
14296
14297
14298 void
14299 enable_breakpoint (struct breakpoint *bpt)
14300 {
14301 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14302 }
14303
14304 /* The enable command enables the specified breakpoints/locations (or
14305 all defined breakpoints) so they once again become (or continue to
14306 be) effective in stopping the inferior. ARGS may be in any of the
14307 forms defined in extract_bp_number_and_location. */
14308
14309 static void
14310 enable_command (const char *args, int from_tty)
14311 {
14312 enable_disable_command (args, from_tty, true);
14313 }
14314
14315 static void
14316 enable_once_command (const char *args, int from_tty)
14317 {
14318 map_breakpoint_numbers
14319 (args, [&] (breakpoint *b)
14320 {
14321 iterate_over_related_breakpoints
14322 (b, [&] (breakpoint *bpt)
14323 {
14324 enable_breakpoint_disp (bpt, disp_disable, 1);
14325 });
14326 });
14327 }
14328
14329 static void
14330 enable_count_command (const char *args, int from_tty)
14331 {
14332 int count;
14333
14334 if (args == NULL)
14335 error_no_arg (_("hit count"));
14336
14337 count = get_number (&args);
14338
14339 map_breakpoint_numbers
14340 (args, [&] (breakpoint *b)
14341 {
14342 iterate_over_related_breakpoints
14343 (b, [&] (breakpoint *bpt)
14344 {
14345 enable_breakpoint_disp (bpt, disp_disable, count);
14346 });
14347 });
14348 }
14349
14350 static void
14351 enable_delete_command (const char *args, int from_tty)
14352 {
14353 map_breakpoint_numbers
14354 (args, [&] (breakpoint *b)
14355 {
14356 iterate_over_related_breakpoints
14357 (b, [&] (breakpoint *bpt)
14358 {
14359 enable_breakpoint_disp (bpt, disp_del, 1);
14360 });
14361 });
14362 }
14363 \f
14364 static void
14365 set_breakpoint_cmd (const char *args, int from_tty)
14366 {
14367 }
14368
14369 static void
14370 show_breakpoint_cmd (const char *args, int from_tty)
14371 {
14372 }
14373
14374 /* Invalidate last known value of any hardware watchpoint if
14375 the memory which that value represents has been written to by
14376 GDB itself. */
14377
14378 static void
14379 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14380 CORE_ADDR addr, ssize_t len,
14381 const bfd_byte *data)
14382 {
14383 struct breakpoint *bp;
14384
14385 ALL_BREAKPOINTS (bp)
14386 if (bp->enable_state == bp_enabled
14387 && bp->type == bp_hardware_watchpoint)
14388 {
14389 struct watchpoint *wp = (struct watchpoint *) bp;
14390
14391 if (wp->val_valid && wp->val != nullptr)
14392 {
14393 struct bp_location *loc;
14394
14395 for (loc = bp->loc; loc != NULL; loc = loc->next)
14396 if (loc->loc_type == bp_loc_hardware_watchpoint
14397 && loc->address + loc->length > addr
14398 && addr + len > loc->address)
14399 {
14400 wp->val = NULL;
14401 wp->val_valid = false;
14402 }
14403 }
14404 }
14405 }
14406
14407 /* Create and insert a breakpoint for software single step. */
14408
14409 void
14410 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14411 const address_space *aspace,
14412 CORE_ADDR next_pc)
14413 {
14414 struct thread_info *tp = inferior_thread ();
14415 struct symtab_and_line sal;
14416 CORE_ADDR pc = next_pc;
14417
14418 if (tp->control.single_step_breakpoints == NULL)
14419 {
14420 tp->control.single_step_breakpoints
14421 = new_single_step_breakpoint (tp->global_num, gdbarch);
14422 }
14423
14424 sal = find_pc_line (pc, 0);
14425 sal.pc = pc;
14426 sal.section = find_pc_overlay (pc);
14427 sal.explicit_pc = 1;
14428 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14429
14430 update_global_location_list (UGLL_INSERT);
14431 }
14432
14433 /* Insert single step breakpoints according to the current state. */
14434
14435 int
14436 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14437 {
14438 struct regcache *regcache = get_current_regcache ();
14439 std::vector<CORE_ADDR> next_pcs;
14440
14441 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14442
14443 if (!next_pcs.empty ())
14444 {
14445 struct frame_info *frame = get_current_frame ();
14446 const address_space *aspace = get_frame_address_space (frame);
14447
14448 for (CORE_ADDR pc : next_pcs)
14449 insert_single_step_breakpoint (gdbarch, aspace, pc);
14450
14451 return 1;
14452 }
14453 else
14454 return 0;
14455 }
14456
14457 /* See breakpoint.h. */
14458
14459 int
14460 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14461 const address_space *aspace,
14462 CORE_ADDR pc)
14463 {
14464 struct bp_location *loc;
14465
14466 for (loc = bp->loc; loc != NULL; loc = loc->next)
14467 if (loc->inserted
14468 && breakpoint_location_address_match (loc, aspace, pc))
14469 return 1;
14470
14471 return 0;
14472 }
14473
14474 /* Check whether a software single-step breakpoint is inserted at
14475 PC. */
14476
14477 int
14478 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14479 CORE_ADDR pc)
14480 {
14481 struct breakpoint *bpt;
14482
14483 ALL_BREAKPOINTS (bpt)
14484 {
14485 if (bpt->type == bp_single_step
14486 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14487 return 1;
14488 }
14489 return 0;
14490 }
14491
14492 /* Tracepoint-specific operations. */
14493
14494 /* Set tracepoint count to NUM. */
14495 static void
14496 set_tracepoint_count (int num)
14497 {
14498 tracepoint_count = num;
14499 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14500 }
14501
14502 static void
14503 trace_command (const char *arg, int from_tty)
14504 {
14505 event_location_up location = string_to_event_location (&arg,
14506 current_language);
14507 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14508 (location.get (), true /* is_tracepoint */);
14509
14510 create_breakpoint (get_current_arch (),
14511 location.get (),
14512 NULL, 0, arg, 1 /* parse arg */,
14513 0 /* tempflag */,
14514 bp_tracepoint /* type_wanted */,
14515 0 /* Ignore count */,
14516 pending_break_support,
14517 ops,
14518 from_tty,
14519 1 /* enabled */,
14520 0 /* internal */, 0);
14521 }
14522
14523 static void
14524 ftrace_command (const char *arg, int from_tty)
14525 {
14526 event_location_up location = string_to_event_location (&arg,
14527 current_language);
14528 create_breakpoint (get_current_arch (),
14529 location.get (),
14530 NULL, 0, arg, 1 /* parse arg */,
14531 0 /* tempflag */,
14532 bp_fast_tracepoint /* type_wanted */,
14533 0 /* Ignore count */,
14534 pending_break_support,
14535 &tracepoint_breakpoint_ops,
14536 from_tty,
14537 1 /* enabled */,
14538 0 /* internal */, 0);
14539 }
14540
14541 /* strace command implementation. Creates a static tracepoint. */
14542
14543 static void
14544 strace_command (const char *arg, int from_tty)
14545 {
14546 struct breakpoint_ops *ops;
14547 event_location_up location;
14548
14549 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14550 or with a normal static tracepoint. */
14551 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14552 {
14553 ops = &strace_marker_breakpoint_ops;
14554 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14555 }
14556 else
14557 {
14558 ops = &tracepoint_breakpoint_ops;
14559 location = string_to_event_location (&arg, current_language);
14560 }
14561
14562 create_breakpoint (get_current_arch (),
14563 location.get (),
14564 NULL, 0, arg, 1 /* parse arg */,
14565 0 /* tempflag */,
14566 bp_static_tracepoint /* type_wanted */,
14567 0 /* Ignore count */,
14568 pending_break_support,
14569 ops,
14570 from_tty,
14571 1 /* enabled */,
14572 0 /* internal */, 0);
14573 }
14574
14575 /* Set up a fake reader function that gets command lines from a linked
14576 list that was acquired during tracepoint uploading. */
14577
14578 static struct uploaded_tp *this_utp;
14579 static int next_cmd;
14580
14581 static char *
14582 read_uploaded_action (void)
14583 {
14584 char *rslt = nullptr;
14585
14586 if (next_cmd < this_utp->cmd_strings.size ())
14587 {
14588 rslt = this_utp->cmd_strings[next_cmd].get ();
14589 next_cmd++;
14590 }
14591
14592 return rslt;
14593 }
14594
14595 /* Given information about a tracepoint as recorded on a target (which
14596 can be either a live system or a trace file), attempt to create an
14597 equivalent GDB tracepoint. This is not a reliable process, since
14598 the target does not necessarily have all the information used when
14599 the tracepoint was originally defined. */
14600
14601 struct tracepoint *
14602 create_tracepoint_from_upload (struct uploaded_tp *utp)
14603 {
14604 const char *addr_str;
14605 char small_buf[100];
14606 struct tracepoint *tp;
14607
14608 if (utp->at_string)
14609 addr_str = utp->at_string.get ();
14610 else
14611 {
14612 /* In the absence of a source location, fall back to raw
14613 address. Since there is no way to confirm that the address
14614 means the same thing as when the trace was started, warn the
14615 user. */
14616 warning (_("Uploaded tracepoint %d has no "
14617 "source location, using raw address"),
14618 utp->number);
14619 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14620 addr_str = small_buf;
14621 }
14622
14623 /* There's not much we can do with a sequence of bytecodes. */
14624 if (utp->cond && !utp->cond_string)
14625 warning (_("Uploaded tracepoint %d condition "
14626 "has no source form, ignoring it"),
14627 utp->number);
14628
14629 event_location_up location = string_to_event_location (&addr_str,
14630 current_language);
14631 if (!create_breakpoint (get_current_arch (),
14632 location.get (),
14633 utp->cond_string.get (), -1, addr_str,
14634 0 /* parse cond/thread */,
14635 0 /* tempflag */,
14636 utp->type /* type_wanted */,
14637 0 /* Ignore count */,
14638 pending_break_support,
14639 &tracepoint_breakpoint_ops,
14640 0 /* from_tty */,
14641 utp->enabled /* enabled */,
14642 0 /* internal */,
14643 CREATE_BREAKPOINT_FLAGS_INSERTED))
14644 return NULL;
14645
14646 /* Get the tracepoint we just created. */
14647 tp = get_tracepoint (tracepoint_count);
14648 gdb_assert (tp != NULL);
14649
14650 if (utp->pass > 0)
14651 {
14652 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14653 tp->number);
14654
14655 trace_pass_command (small_buf, 0);
14656 }
14657
14658 /* If we have uploaded versions of the original commands, set up a
14659 special-purpose "reader" function and call the usual command line
14660 reader, then pass the result to the breakpoint command-setting
14661 function. */
14662 if (!utp->cmd_strings.empty ())
14663 {
14664 counted_command_line cmd_list;
14665
14666 this_utp = utp;
14667 next_cmd = 0;
14668
14669 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14670
14671 breakpoint_set_commands (tp, std::move (cmd_list));
14672 }
14673 else if (!utp->actions.empty ()
14674 || !utp->step_actions.empty ())
14675 warning (_("Uploaded tracepoint %d actions "
14676 "have no source form, ignoring them"),
14677 utp->number);
14678
14679 /* Copy any status information that might be available. */
14680 tp->hit_count = utp->hit_count;
14681 tp->traceframe_usage = utp->traceframe_usage;
14682
14683 return tp;
14684 }
14685
14686 /* Print information on tracepoint number TPNUM_EXP, or all if
14687 omitted. */
14688
14689 static void
14690 info_tracepoints_command (const char *args, int from_tty)
14691 {
14692 struct ui_out *uiout = current_uiout;
14693 int num_printed;
14694
14695 num_printed = breakpoint_1 (args, false, is_tracepoint);
14696
14697 if (num_printed == 0)
14698 {
14699 if (args == NULL || *args == '\0')
14700 uiout->message ("No tracepoints.\n");
14701 else
14702 uiout->message ("No tracepoint matching '%s'.\n", args);
14703 }
14704
14705 default_collect_info ();
14706 }
14707
14708 /* The 'enable trace' command enables tracepoints.
14709 Not supported by all targets. */
14710 static void
14711 enable_trace_command (const char *args, int from_tty)
14712 {
14713 enable_command (args, from_tty);
14714 }
14715
14716 /* The 'disable trace' command disables tracepoints.
14717 Not supported by all targets. */
14718 static void
14719 disable_trace_command (const char *args, int from_tty)
14720 {
14721 disable_command (args, from_tty);
14722 }
14723
14724 /* Remove a tracepoint (or all if no argument). */
14725 static void
14726 delete_trace_command (const char *arg, int from_tty)
14727 {
14728 struct breakpoint *b, *b_tmp;
14729
14730 dont_repeat ();
14731
14732 if (arg == 0)
14733 {
14734 int breaks_to_delete = 0;
14735
14736 /* Delete all breakpoints if no argument.
14737 Do not delete internal or call-dummy breakpoints, these
14738 have to be deleted with an explicit breakpoint number
14739 argument. */
14740 ALL_TRACEPOINTS (b)
14741 if (is_tracepoint (b) && user_breakpoint_p (b))
14742 {
14743 breaks_to_delete = 1;
14744 break;
14745 }
14746
14747 /* Ask user only if there are some breakpoints to delete. */
14748 if (!from_tty
14749 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14750 {
14751 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14752 if (is_tracepoint (b) && user_breakpoint_p (b))
14753 delete_breakpoint (b);
14754 }
14755 }
14756 else
14757 map_breakpoint_numbers
14758 (arg, [&] (breakpoint *br)
14759 {
14760 iterate_over_related_breakpoints (br, delete_breakpoint);
14761 });
14762 }
14763
14764 /* Helper function for trace_pass_command. */
14765
14766 static void
14767 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14768 {
14769 tp->pass_count = count;
14770 gdb::observers::breakpoint_modified.notify (tp);
14771 if (from_tty)
14772 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14773 tp->number, count);
14774 }
14775
14776 /* Set passcount for tracepoint.
14777
14778 First command argument is passcount, second is tracepoint number.
14779 If tracepoint number omitted, apply to most recently defined.
14780 Also accepts special argument "all". */
14781
14782 static void
14783 trace_pass_command (const char *args, int from_tty)
14784 {
14785 struct tracepoint *t1;
14786 ULONGEST count;
14787
14788 if (args == 0 || *args == 0)
14789 error (_("passcount command requires an "
14790 "argument (count + optional TP num)"));
14791
14792 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14793
14794 args = skip_spaces (args);
14795 if (*args && strncasecmp (args, "all", 3) == 0)
14796 {
14797 struct breakpoint *b;
14798
14799 args += 3; /* Skip special argument "all". */
14800 if (*args)
14801 error (_("Junk at end of arguments."));
14802
14803 ALL_TRACEPOINTS (b)
14804 {
14805 t1 = (struct tracepoint *) b;
14806 trace_pass_set_count (t1, count, from_tty);
14807 }
14808 }
14809 else if (*args == '\0')
14810 {
14811 t1 = get_tracepoint_by_number (&args, NULL);
14812 if (t1)
14813 trace_pass_set_count (t1, count, from_tty);
14814 }
14815 else
14816 {
14817 number_or_range_parser parser (args);
14818 while (!parser.finished ())
14819 {
14820 t1 = get_tracepoint_by_number (&args, &parser);
14821 if (t1)
14822 trace_pass_set_count (t1, count, from_tty);
14823 }
14824 }
14825 }
14826
14827 struct tracepoint *
14828 get_tracepoint (int num)
14829 {
14830 struct breakpoint *t;
14831
14832 ALL_TRACEPOINTS (t)
14833 if (t->number == num)
14834 return (struct tracepoint *) t;
14835
14836 return NULL;
14837 }
14838
14839 /* Find the tracepoint with the given target-side number (which may be
14840 different from the tracepoint number after disconnecting and
14841 reconnecting). */
14842
14843 struct tracepoint *
14844 get_tracepoint_by_number_on_target (int num)
14845 {
14846 struct breakpoint *b;
14847
14848 ALL_TRACEPOINTS (b)
14849 {
14850 struct tracepoint *t = (struct tracepoint *) b;
14851
14852 if (t->number_on_target == num)
14853 return t;
14854 }
14855
14856 return NULL;
14857 }
14858
14859 /* Utility: parse a tracepoint number and look it up in the list.
14860 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14861 If the argument is missing, the most recent tracepoint
14862 (tracepoint_count) is returned. */
14863
14864 struct tracepoint *
14865 get_tracepoint_by_number (const char **arg,
14866 number_or_range_parser *parser)
14867 {
14868 struct breakpoint *t;
14869 int tpnum;
14870 const char *instring = arg == NULL ? NULL : *arg;
14871
14872 if (parser != NULL)
14873 {
14874 gdb_assert (!parser->finished ());
14875 tpnum = parser->get_number ();
14876 }
14877 else if (arg == NULL || *arg == NULL || ! **arg)
14878 tpnum = tracepoint_count;
14879 else
14880 tpnum = get_number (arg);
14881
14882 if (tpnum <= 0)
14883 {
14884 if (instring && *instring)
14885 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14886 instring);
14887 else
14888 printf_filtered (_("No previous tracepoint\n"));
14889 return NULL;
14890 }
14891
14892 ALL_TRACEPOINTS (t)
14893 if (t->number == tpnum)
14894 {
14895 return (struct tracepoint *) t;
14896 }
14897
14898 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14899 return NULL;
14900 }
14901
14902 void
14903 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14904 {
14905 if (b->thread != -1)
14906 fprintf_unfiltered (fp, " thread %d", b->thread);
14907
14908 if (b->task != 0)
14909 fprintf_unfiltered (fp, " task %d", b->task);
14910
14911 fprintf_unfiltered (fp, "\n");
14912 }
14913
14914 /* Save information on user settable breakpoints (watchpoints, etc) to
14915 a new script file named FILENAME. If FILTER is non-NULL, call it
14916 on each breakpoint and only include the ones for which it returns
14917 true. */
14918
14919 static void
14920 save_breakpoints (const char *filename, int from_tty,
14921 bool (*filter) (const struct breakpoint *))
14922 {
14923 struct breakpoint *tp;
14924 int any = 0;
14925 int extra_trace_bits = 0;
14926
14927 if (filename == 0 || *filename == 0)
14928 error (_("Argument required (file name in which to save)"));
14929
14930 /* See if we have anything to save. */
14931 ALL_BREAKPOINTS (tp)
14932 {
14933 /* Skip internal and momentary breakpoints. */
14934 if (!user_breakpoint_p (tp))
14935 continue;
14936
14937 /* If we have a filter, only save the breakpoints it accepts. */
14938 if (filter && !filter (tp))
14939 continue;
14940
14941 any = 1;
14942
14943 if (is_tracepoint (tp))
14944 {
14945 extra_trace_bits = 1;
14946
14947 /* We can stop searching. */
14948 break;
14949 }
14950 }
14951
14952 if (!any)
14953 {
14954 warning (_("Nothing to save."));
14955 return;
14956 }
14957
14958 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14959
14960 stdio_file fp;
14961
14962 if (!fp.open (expanded_filename.get (), "w"))
14963 error (_("Unable to open file '%s' for saving (%s)"),
14964 expanded_filename.get (), safe_strerror (errno));
14965
14966 if (extra_trace_bits)
14967 save_trace_state_variables (&fp);
14968
14969 ALL_BREAKPOINTS (tp)
14970 {
14971 /* Skip internal and momentary breakpoints. */
14972 if (!user_breakpoint_p (tp))
14973 continue;
14974
14975 /* If we have a filter, only save the breakpoints it accepts. */
14976 if (filter && !filter (tp))
14977 continue;
14978
14979 tp->ops->print_recreate (tp, &fp);
14980
14981 /* Note, we can't rely on tp->number for anything, as we can't
14982 assume the recreated breakpoint numbers will match. Use $bpnum
14983 instead. */
14984
14985 if (tp->cond_string)
14986 fp.printf (" condition $bpnum %s\n", tp->cond_string);
14987
14988 if (tp->ignore_count)
14989 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14990
14991 if (tp->type != bp_dprintf && tp->commands)
14992 {
14993 fp.puts (" commands\n");
14994
14995 current_uiout->redirect (&fp);
14996 try
14997 {
14998 print_command_lines (current_uiout, tp->commands.get (), 2);
14999 }
15000 catch (const gdb_exception &ex)
15001 {
15002 current_uiout->redirect (NULL);
15003 throw;
15004 }
15005
15006 current_uiout->redirect (NULL);
15007 fp.puts (" end\n");
15008 }
15009
15010 if (tp->enable_state == bp_disabled)
15011 fp.puts ("disable $bpnum\n");
15012
15013 /* If this is a multi-location breakpoint, check if the locations
15014 should be individually disabled. Watchpoint locations are
15015 special, and not user visible. */
15016 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15017 {
15018 struct bp_location *loc;
15019 int n = 1;
15020
15021 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15022 if (!loc->enabled)
15023 fp.printf ("disable $bpnum.%d\n", n);
15024 }
15025 }
15026
15027 if (extra_trace_bits && *default_collect)
15028 fp.printf ("set default-collect %s\n", default_collect);
15029
15030 if (from_tty)
15031 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15032 }
15033
15034 /* The `save breakpoints' command. */
15035
15036 static void
15037 save_breakpoints_command (const char *args, int from_tty)
15038 {
15039 save_breakpoints (args, from_tty, NULL);
15040 }
15041
15042 /* The `save tracepoints' command. */
15043
15044 static void
15045 save_tracepoints_command (const char *args, int from_tty)
15046 {
15047 save_breakpoints (args, from_tty, is_tracepoint);
15048 }
15049
15050 /* Create a vector of all tracepoints. */
15051
15052 std::vector<breakpoint *>
15053 all_tracepoints (void)
15054 {
15055 std::vector<breakpoint *> tp_vec;
15056 struct breakpoint *tp;
15057
15058 ALL_TRACEPOINTS (tp)
15059 {
15060 tp_vec.push_back (tp);
15061 }
15062
15063 return tp_vec;
15064 }
15065
15066 \f
15067 /* This help string is used to consolidate all the help string for specifying
15068 locations used by several commands. */
15069
15070 #define LOCATION_HELP_STRING \
15071 "Linespecs are colon-separated lists of location parameters, such as\n\
15072 source filename, function name, label name, and line number.\n\
15073 Example: To specify the start of a label named \"the_top\" in the\n\
15074 function \"fact\" in the file \"factorial.c\", use\n\
15075 \"factorial.c:fact:the_top\".\n\
15076 \n\
15077 Address locations begin with \"*\" and specify an exact address in the\n\
15078 program. Example: To specify the fourth byte past the start function\n\
15079 \"main\", use \"*main + 4\".\n\
15080 \n\
15081 Explicit locations are similar to linespecs but use an option/argument\n\
15082 syntax to specify location parameters.\n\
15083 Example: To specify the start of the label named \"the_top\" in the\n\
15084 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15085 -function fact -label the_top\".\n\
15086 \n\
15087 By default, a specified function is matched against the program's\n\
15088 functions in all scopes. For C++, this means in all namespaces and\n\
15089 classes. For Ada, this means in all packages. E.g., in C++,\n\
15090 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15091 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15092 specified name as a complete fully-qualified name instead."
15093
15094 /* This help string is used for the break, hbreak, tbreak and thbreak
15095 commands. It is defined as a macro to prevent duplication.
15096 COMMAND should be a string constant containing the name of the
15097 command. */
15098
15099 #define BREAK_ARGS_HELP(command) \
15100 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15101 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15102 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15103 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15104 `-probe-dtrace' (for a DTrace probe).\n\
15105 LOCATION may be a linespec, address, or explicit location as described\n\
15106 below.\n\
15107 \n\
15108 With no LOCATION, uses current execution address of the selected\n\
15109 stack frame. This is useful for breaking on return to a stack frame.\n\
15110 \n\
15111 THREADNUM is the number from \"info threads\".\n\
15112 CONDITION is a boolean expression.\n\
15113 \n" LOCATION_HELP_STRING "\n\n\
15114 Multiple breakpoints at one place are permitted, and useful if their\n\
15115 conditions are different.\n\
15116 \n\
15117 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15118
15119 /* List of subcommands for "catch". */
15120 static struct cmd_list_element *catch_cmdlist;
15121
15122 /* List of subcommands for "tcatch". */
15123 static struct cmd_list_element *tcatch_cmdlist;
15124
15125 void
15126 add_catch_command (const char *name, const char *docstring,
15127 cmd_const_sfunc_ftype *sfunc,
15128 completer_ftype *completer,
15129 void *user_data_catch,
15130 void *user_data_tcatch)
15131 {
15132 struct cmd_list_element *command;
15133
15134 command = add_cmd (name, class_breakpoint, docstring,
15135 &catch_cmdlist);
15136 set_cmd_sfunc (command, sfunc);
15137 set_cmd_context (command, user_data_catch);
15138 set_cmd_completer (command, completer);
15139
15140 command = add_cmd (name, class_breakpoint, docstring,
15141 &tcatch_cmdlist);
15142 set_cmd_sfunc (command, sfunc);
15143 set_cmd_context (command, user_data_tcatch);
15144 set_cmd_completer (command, completer);
15145 }
15146
15147 static void
15148 save_command (const char *arg, int from_tty)
15149 {
15150 printf_unfiltered (_("\"save\" must be followed by "
15151 "the name of a save subcommand.\n"));
15152 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15153 }
15154
15155 struct breakpoint *
15156 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15157 {
15158 struct breakpoint *b, *b_tmp;
15159
15160 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15161 {
15162 if (callback (b))
15163 return b;
15164 }
15165
15166 return NULL;
15167 }
15168
15169 /* Zero if any of the breakpoint's locations could be a location where
15170 functions have been inlined, nonzero otherwise. */
15171
15172 static int
15173 is_non_inline_function (struct breakpoint *b)
15174 {
15175 /* The shared library event breakpoint is set on the address of a
15176 non-inline function. */
15177 if (b->type == bp_shlib_event)
15178 return 1;
15179
15180 return 0;
15181 }
15182
15183 /* Nonzero if the specified PC cannot be a location where functions
15184 have been inlined. */
15185
15186 int
15187 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15188 const struct target_waitstatus *ws)
15189 {
15190 struct breakpoint *b;
15191 struct bp_location *bl;
15192
15193 ALL_BREAKPOINTS (b)
15194 {
15195 if (!is_non_inline_function (b))
15196 continue;
15197
15198 for (bl = b->loc; bl != NULL; bl = bl->next)
15199 {
15200 if (!bl->shlib_disabled
15201 && bpstat_check_location (bl, aspace, pc, ws))
15202 return 1;
15203 }
15204 }
15205
15206 return 0;
15207 }
15208
15209 /* Remove any references to OBJFILE which is going to be freed. */
15210
15211 void
15212 breakpoint_free_objfile (struct objfile *objfile)
15213 {
15214 struct bp_location **locp, *loc;
15215
15216 ALL_BP_LOCATIONS (loc, locp)
15217 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15218 loc->symtab = NULL;
15219 }
15220
15221 void
15222 initialize_breakpoint_ops (void)
15223 {
15224 static int initialized = 0;
15225
15226 struct breakpoint_ops *ops;
15227
15228 if (initialized)
15229 return;
15230 initialized = 1;
15231
15232 /* The breakpoint_ops structure to be inherit by all kinds of
15233 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15234 internal and momentary breakpoints, etc.). */
15235 ops = &bkpt_base_breakpoint_ops;
15236 *ops = base_breakpoint_ops;
15237 ops->re_set = bkpt_re_set;
15238 ops->insert_location = bkpt_insert_location;
15239 ops->remove_location = bkpt_remove_location;
15240 ops->breakpoint_hit = bkpt_breakpoint_hit;
15241 ops->create_sals_from_location = bkpt_create_sals_from_location;
15242 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15243 ops->decode_location = bkpt_decode_location;
15244
15245 /* The breakpoint_ops structure to be used in regular breakpoints. */
15246 ops = &bkpt_breakpoint_ops;
15247 *ops = bkpt_base_breakpoint_ops;
15248 ops->re_set = bkpt_re_set;
15249 ops->resources_needed = bkpt_resources_needed;
15250 ops->print_it = bkpt_print_it;
15251 ops->print_mention = bkpt_print_mention;
15252 ops->print_recreate = bkpt_print_recreate;
15253
15254 /* Ranged breakpoints. */
15255 ops = &ranged_breakpoint_ops;
15256 *ops = bkpt_breakpoint_ops;
15257 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15258 ops->resources_needed = resources_needed_ranged_breakpoint;
15259 ops->print_it = print_it_ranged_breakpoint;
15260 ops->print_one = print_one_ranged_breakpoint;
15261 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15262 ops->print_mention = print_mention_ranged_breakpoint;
15263 ops->print_recreate = print_recreate_ranged_breakpoint;
15264
15265 /* Internal breakpoints. */
15266 ops = &internal_breakpoint_ops;
15267 *ops = bkpt_base_breakpoint_ops;
15268 ops->re_set = internal_bkpt_re_set;
15269 ops->check_status = internal_bkpt_check_status;
15270 ops->print_it = internal_bkpt_print_it;
15271 ops->print_mention = internal_bkpt_print_mention;
15272
15273 /* Momentary breakpoints. */
15274 ops = &momentary_breakpoint_ops;
15275 *ops = bkpt_base_breakpoint_ops;
15276 ops->re_set = momentary_bkpt_re_set;
15277 ops->check_status = momentary_bkpt_check_status;
15278 ops->print_it = momentary_bkpt_print_it;
15279 ops->print_mention = momentary_bkpt_print_mention;
15280
15281 /* Probe breakpoints. */
15282 ops = &bkpt_probe_breakpoint_ops;
15283 *ops = bkpt_breakpoint_ops;
15284 ops->insert_location = bkpt_probe_insert_location;
15285 ops->remove_location = bkpt_probe_remove_location;
15286 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15287 ops->decode_location = bkpt_probe_decode_location;
15288
15289 /* Watchpoints. */
15290 ops = &watchpoint_breakpoint_ops;
15291 *ops = base_breakpoint_ops;
15292 ops->re_set = re_set_watchpoint;
15293 ops->insert_location = insert_watchpoint;
15294 ops->remove_location = remove_watchpoint;
15295 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15296 ops->check_status = check_status_watchpoint;
15297 ops->resources_needed = resources_needed_watchpoint;
15298 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15299 ops->print_it = print_it_watchpoint;
15300 ops->print_mention = print_mention_watchpoint;
15301 ops->print_recreate = print_recreate_watchpoint;
15302 ops->explains_signal = explains_signal_watchpoint;
15303
15304 /* Masked watchpoints. */
15305 ops = &masked_watchpoint_breakpoint_ops;
15306 *ops = watchpoint_breakpoint_ops;
15307 ops->insert_location = insert_masked_watchpoint;
15308 ops->remove_location = remove_masked_watchpoint;
15309 ops->resources_needed = resources_needed_masked_watchpoint;
15310 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15311 ops->print_it = print_it_masked_watchpoint;
15312 ops->print_one_detail = print_one_detail_masked_watchpoint;
15313 ops->print_mention = print_mention_masked_watchpoint;
15314 ops->print_recreate = print_recreate_masked_watchpoint;
15315
15316 /* Tracepoints. */
15317 ops = &tracepoint_breakpoint_ops;
15318 *ops = base_breakpoint_ops;
15319 ops->re_set = tracepoint_re_set;
15320 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15321 ops->print_one_detail = tracepoint_print_one_detail;
15322 ops->print_mention = tracepoint_print_mention;
15323 ops->print_recreate = tracepoint_print_recreate;
15324 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15325 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15326 ops->decode_location = tracepoint_decode_location;
15327
15328 /* Probe tracepoints. */
15329 ops = &tracepoint_probe_breakpoint_ops;
15330 *ops = tracepoint_breakpoint_ops;
15331 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15332 ops->decode_location = tracepoint_probe_decode_location;
15333
15334 /* Static tracepoints with marker (`-m'). */
15335 ops = &strace_marker_breakpoint_ops;
15336 *ops = tracepoint_breakpoint_ops;
15337 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15338 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15339 ops->decode_location = strace_marker_decode_location;
15340
15341 /* Fork catchpoints. */
15342 ops = &catch_fork_breakpoint_ops;
15343 *ops = base_breakpoint_ops;
15344 ops->insert_location = insert_catch_fork;
15345 ops->remove_location = remove_catch_fork;
15346 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15347 ops->print_it = print_it_catch_fork;
15348 ops->print_one = print_one_catch_fork;
15349 ops->print_mention = print_mention_catch_fork;
15350 ops->print_recreate = print_recreate_catch_fork;
15351
15352 /* Vfork catchpoints. */
15353 ops = &catch_vfork_breakpoint_ops;
15354 *ops = base_breakpoint_ops;
15355 ops->insert_location = insert_catch_vfork;
15356 ops->remove_location = remove_catch_vfork;
15357 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15358 ops->print_it = print_it_catch_vfork;
15359 ops->print_one = print_one_catch_vfork;
15360 ops->print_mention = print_mention_catch_vfork;
15361 ops->print_recreate = print_recreate_catch_vfork;
15362
15363 /* Exec catchpoints. */
15364 ops = &catch_exec_breakpoint_ops;
15365 *ops = base_breakpoint_ops;
15366 ops->insert_location = insert_catch_exec;
15367 ops->remove_location = remove_catch_exec;
15368 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15369 ops->print_it = print_it_catch_exec;
15370 ops->print_one = print_one_catch_exec;
15371 ops->print_mention = print_mention_catch_exec;
15372 ops->print_recreate = print_recreate_catch_exec;
15373
15374 /* Solib-related catchpoints. */
15375 ops = &catch_solib_breakpoint_ops;
15376 *ops = base_breakpoint_ops;
15377 ops->insert_location = insert_catch_solib;
15378 ops->remove_location = remove_catch_solib;
15379 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15380 ops->check_status = check_status_catch_solib;
15381 ops->print_it = print_it_catch_solib;
15382 ops->print_one = print_one_catch_solib;
15383 ops->print_mention = print_mention_catch_solib;
15384 ops->print_recreate = print_recreate_catch_solib;
15385
15386 ops = &dprintf_breakpoint_ops;
15387 *ops = bkpt_base_breakpoint_ops;
15388 ops->re_set = dprintf_re_set;
15389 ops->resources_needed = bkpt_resources_needed;
15390 ops->print_it = bkpt_print_it;
15391 ops->print_mention = bkpt_print_mention;
15392 ops->print_recreate = dprintf_print_recreate;
15393 ops->after_condition_true = dprintf_after_condition_true;
15394 ops->breakpoint_hit = dprintf_breakpoint_hit;
15395 }
15396
15397 /* Chain containing all defined "enable breakpoint" subcommands. */
15398
15399 static struct cmd_list_element *enablebreaklist = NULL;
15400
15401 /* See breakpoint.h. */
15402
15403 cmd_list_element *commands_cmd_element = nullptr;
15404
15405 void
15406 _initialize_breakpoint (void)
15407 {
15408 struct cmd_list_element *c;
15409
15410 initialize_breakpoint_ops ();
15411
15412 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15413 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15414 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15415
15416 breakpoint_chain = 0;
15417 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15418 before a breakpoint is set. */
15419 breakpoint_count = 0;
15420
15421 tracepoint_count = 0;
15422
15423 add_com ("ignore", class_breakpoint, ignore_command, _("\
15424 Set ignore-count of breakpoint number N to COUNT.\n\
15425 Usage is `ignore N COUNT'."));
15426
15427 commands_cmd_element = add_com ("commands", class_breakpoint,
15428 commands_command, _("\
15429 Set commands to be executed when the given breakpoints are hit.\n\
15430 Give a space-separated breakpoint list as argument after \"commands\".\n\
15431 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15432 (e.g. `5-7').\n\
15433 With no argument, the targeted breakpoint is the last one set.\n\
15434 The commands themselves follow starting on the next line.\n\
15435 Type a line containing \"end\" to indicate the end of them.\n\
15436 Give \"silent\" as the first line to make the breakpoint silent;\n\
15437 then no output is printed when it is hit, except what the commands print."));
15438
15439 c = add_com ("condition", class_breakpoint, condition_command, _("\
15440 Specify breakpoint number N to break only if COND is true.\n\
15441 Usage is `condition N COND', where N is an integer and COND is an\n\
15442 expression to be evaluated whenever breakpoint N is reached."));
15443 set_cmd_completer (c, condition_completer);
15444
15445 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15446 Set a temporary breakpoint.\n\
15447 Like \"break\" except the breakpoint is only temporary,\n\
15448 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15449 by using \"enable delete\" on the breakpoint number.\n\
15450 \n"
15451 BREAK_ARGS_HELP ("tbreak")));
15452 set_cmd_completer (c, location_completer);
15453
15454 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15455 Set a hardware assisted breakpoint.\n\
15456 Like \"break\" except the breakpoint requires hardware support,\n\
15457 some target hardware may not have this support.\n\
15458 \n"
15459 BREAK_ARGS_HELP ("hbreak")));
15460 set_cmd_completer (c, location_completer);
15461
15462 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15463 Set a temporary hardware assisted breakpoint.\n\
15464 Like \"hbreak\" except the breakpoint is only temporary,\n\
15465 so it will be deleted when hit.\n\
15466 \n"
15467 BREAK_ARGS_HELP ("thbreak")));
15468 set_cmd_completer (c, location_completer);
15469
15470 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15471 Enable all or some breakpoints.\n\
15472 Usage: enable [BREAKPOINTNUM]...\n\
15473 Give breakpoint numbers (separated by spaces) as arguments.\n\
15474 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15475 This is used to cancel the effect of the \"disable\" command.\n\
15476 With a subcommand you can enable temporarily."),
15477 &enablelist, "enable ", 1, &cmdlist);
15478
15479 add_com_alias ("en", "enable", class_breakpoint, 1);
15480
15481 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15482 Enable all or some breakpoints.\n\
15483 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15484 Give breakpoint numbers (separated by spaces) as arguments.\n\
15485 This is used to cancel the effect of the \"disable\" command.\n\
15486 May be abbreviated to simply \"enable\"."),
15487 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15488
15489 add_cmd ("once", no_class, enable_once_command, _("\
15490 Enable some breakpoints for one hit.\n\
15491 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15492 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15493 &enablebreaklist);
15494
15495 add_cmd ("delete", no_class, enable_delete_command, _("\
15496 Enable some breakpoints and delete when hit.\n\
15497 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15498 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15499 &enablebreaklist);
15500
15501 add_cmd ("count", no_class, enable_count_command, _("\
15502 Enable some breakpoints for COUNT hits.\n\
15503 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15504 If a breakpoint is hit while enabled in this fashion,\n\
15505 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15506 &enablebreaklist);
15507
15508 add_cmd ("delete", no_class, enable_delete_command, _("\
15509 Enable some breakpoints and delete when hit.\n\
15510 Usage: enable delete BREAKPOINTNUM...\n\
15511 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15512 &enablelist);
15513
15514 add_cmd ("once", no_class, enable_once_command, _("\
15515 Enable some breakpoints for one hit.\n\
15516 Usage: enable once BREAKPOINTNUM...\n\
15517 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15518 &enablelist);
15519
15520 add_cmd ("count", no_class, enable_count_command, _("\
15521 Enable some breakpoints for COUNT hits.\n\
15522 Usage: enable count COUNT BREAKPOINTNUM...\n\
15523 If a breakpoint is hit while enabled in this fashion,\n\
15524 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15525 &enablelist);
15526
15527 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15528 Disable all or some breakpoints.\n\
15529 Usage: disable [BREAKPOINTNUM]...\n\
15530 Arguments are breakpoint numbers with spaces in between.\n\
15531 To disable all breakpoints, give no argument.\n\
15532 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15533 &disablelist, "disable ", 1, &cmdlist);
15534 add_com_alias ("dis", "disable", class_breakpoint, 1);
15535 add_com_alias ("disa", "disable", class_breakpoint, 1);
15536
15537 add_cmd ("breakpoints", class_alias, disable_command, _("\
15538 Disable all or some breakpoints.\n\
15539 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15540 Arguments are breakpoint numbers with spaces in between.\n\
15541 To disable all breakpoints, give no argument.\n\
15542 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15543 This command may be abbreviated \"disable\"."),
15544 &disablelist);
15545
15546 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15547 Delete all or some breakpoints.\n\
15548 Usage: delete [BREAKPOINTNUM]...\n\
15549 Arguments are breakpoint numbers with spaces in between.\n\
15550 To delete all breakpoints, give no argument.\n\
15551 \n\
15552 Also a prefix command for deletion of other GDB objects."),
15553 &deletelist, "delete ", 1, &cmdlist);
15554 add_com_alias ("d", "delete", class_breakpoint, 1);
15555 add_com_alias ("del", "delete", class_breakpoint, 1);
15556
15557 add_cmd ("breakpoints", class_alias, delete_command, _("\
15558 Delete all or some breakpoints or auto-display expressions.\n\
15559 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15560 Arguments are breakpoint numbers with spaces in between.\n\
15561 To delete all breakpoints, give no argument.\n\
15562 This command may be abbreviated \"delete\"."),
15563 &deletelist);
15564
15565 add_com ("clear", class_breakpoint, clear_command, _("\
15566 Clear breakpoint at specified location.\n\
15567 Argument may be a linespec, explicit, or address location as described below.\n\
15568 \n\
15569 With no argument, clears all breakpoints in the line that the selected frame\n\
15570 is executing in.\n"
15571 "\n" LOCATION_HELP_STRING "\n\n\
15572 See also the \"delete\" command which clears breakpoints by number."));
15573 add_com_alias ("cl", "clear", class_breakpoint, 1);
15574
15575 c = add_com ("break", class_breakpoint, break_command, _("\
15576 Set breakpoint at specified location.\n"
15577 BREAK_ARGS_HELP ("break")));
15578 set_cmd_completer (c, location_completer);
15579
15580 add_com_alias ("b", "break", class_run, 1);
15581 add_com_alias ("br", "break", class_run, 1);
15582 add_com_alias ("bre", "break", class_run, 1);
15583 add_com_alias ("brea", "break", class_run, 1);
15584
15585 if (dbx_commands)
15586 {
15587 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15588 Break in function/address or break at a line in the current file."),
15589 &stoplist, "stop ", 1, &cmdlist);
15590 add_cmd ("in", class_breakpoint, stopin_command,
15591 _("Break in function or address."), &stoplist);
15592 add_cmd ("at", class_breakpoint, stopat_command,
15593 _("Break at a line in the current file."), &stoplist);
15594 add_com ("status", class_info, info_breakpoints_command, _("\
15595 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15596 The \"Type\" column indicates one of:\n\
15597 \tbreakpoint - normal breakpoint\n\
15598 \twatchpoint - watchpoint\n\
15599 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15600 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15601 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15602 address and file/line number respectively.\n\
15603 \n\
15604 Convenience variable \"$_\" and default examine address for \"x\"\n\
15605 are set to the address of the last breakpoint listed unless the command\n\
15606 is prefixed with \"server \".\n\n\
15607 Convenience variable \"$bpnum\" contains the number of the last\n\
15608 breakpoint set."));
15609 }
15610
15611 add_info ("breakpoints", info_breakpoints_command, _("\
15612 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15613 The \"Type\" column indicates one of:\n\
15614 \tbreakpoint - normal breakpoint\n\
15615 \twatchpoint - watchpoint\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
15627 add_info_alias ("b", "breakpoints", 1);
15628
15629 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15630 Status of all breakpoints, or breakpoint number NUMBER.\n\
15631 The \"Type\" column indicates one of:\n\
15632 \tbreakpoint - normal breakpoint\n\
15633 \twatchpoint - watchpoint\n\
15634 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15635 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15636 \tuntil - internal breakpoint used by the \"until\" command\n\
15637 \tfinish - internal breakpoint used by the \"finish\" command\n\
15638 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15639 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15640 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15641 address and file/line number respectively.\n\
15642 \n\
15643 Convenience variable \"$_\" and default examine address for \"x\"\n\
15644 are set to the address of the last breakpoint listed unless the command\n\
15645 is prefixed with \"server \".\n\n\
15646 Convenience variable \"$bpnum\" contains the number of the last\n\
15647 breakpoint set."),
15648 &maintenanceinfolist);
15649
15650 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15651 Set catchpoints to catch events."),
15652 &catch_cmdlist, "catch ",
15653 0/*allow-unknown*/, &cmdlist);
15654
15655 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15656 Set temporary catchpoints to catch events."),
15657 &tcatch_cmdlist, "tcatch ",
15658 0/*allow-unknown*/, &cmdlist);
15659
15660 add_catch_command ("fork", _("Catch calls to fork."),
15661 catch_fork_command_1,
15662 NULL,
15663 (void *) (uintptr_t) catch_fork_permanent,
15664 (void *) (uintptr_t) catch_fork_temporary);
15665 add_catch_command ("vfork", _("Catch calls to vfork."),
15666 catch_fork_command_1,
15667 NULL,
15668 (void *) (uintptr_t) catch_vfork_permanent,
15669 (void *) (uintptr_t) catch_vfork_temporary);
15670 add_catch_command ("exec", _("Catch calls to exec."),
15671 catch_exec_command_1,
15672 NULL,
15673 CATCH_PERMANENT,
15674 CATCH_TEMPORARY);
15675 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15676 Usage: catch load [REGEX]\n\
15677 If REGEX is given, only stop for libraries matching the regular expression."),
15678 catch_load_command_1,
15679 NULL,
15680 CATCH_PERMANENT,
15681 CATCH_TEMPORARY);
15682 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15683 Usage: catch unload [REGEX]\n\
15684 If REGEX is given, only stop for libraries matching the regular expression."),
15685 catch_unload_command_1,
15686 NULL,
15687 CATCH_PERMANENT,
15688 CATCH_TEMPORARY);
15689
15690 c = add_com ("watch", class_breakpoint, watch_command, _("\
15691 Set a watchpoint for an expression.\n\
15692 Usage: watch [-l|-location] EXPRESSION\n\
15693 A watchpoint stops execution of your program whenever the value of\n\
15694 an expression changes.\n\
15695 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15696 the memory to which it refers."));
15697 set_cmd_completer (c, expression_completer);
15698
15699 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15700 Set a read watchpoint for an expression.\n\
15701 Usage: rwatch [-l|-location] EXPRESSION\n\
15702 A watchpoint stops execution of your program whenever the value of\n\
15703 an expression is read.\n\
15704 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15705 the memory to which it refers."));
15706 set_cmd_completer (c, expression_completer);
15707
15708 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15709 Set a watchpoint for an expression.\n\
15710 Usage: awatch [-l|-location] EXPRESSION\n\
15711 A watchpoint stops execution of your program whenever the value of\n\
15712 an expression is either read or written.\n\
15713 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15714 the memory to which it refers."));
15715 set_cmd_completer (c, expression_completer);
15716
15717 add_info ("watchpoints", info_watchpoints_command, _("\
15718 Status of specified watchpoints (all watchpoints if no argument)."));
15719
15720 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15721 respond to changes - contrary to the description. */
15722 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15723 &can_use_hw_watchpoints, _("\
15724 Set debugger's willingness to use watchpoint hardware."), _("\
15725 Show debugger's willingness to use watchpoint hardware."), _("\
15726 If zero, gdb will not use hardware for new watchpoints, even if\n\
15727 such is available. (However, any hardware watchpoints that were\n\
15728 created before setting this to nonzero, will continue to use watchpoint\n\
15729 hardware.)"),
15730 NULL,
15731 show_can_use_hw_watchpoints,
15732 &setlist, &showlist);
15733
15734 can_use_hw_watchpoints = 1;
15735
15736 /* Tracepoint manipulation commands. */
15737
15738 c = add_com ("trace", class_breakpoint, trace_command, _("\
15739 Set a tracepoint at specified location.\n\
15740 \n"
15741 BREAK_ARGS_HELP ("trace") "\n\
15742 Do \"help tracepoints\" for info on other tracepoint commands."));
15743 set_cmd_completer (c, location_completer);
15744
15745 add_com_alias ("tp", "trace", class_alias, 0);
15746 add_com_alias ("tr", "trace", class_alias, 1);
15747 add_com_alias ("tra", "trace", class_alias, 1);
15748 add_com_alias ("trac", "trace", class_alias, 1);
15749
15750 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15751 Set a fast tracepoint at specified location.\n\
15752 \n"
15753 BREAK_ARGS_HELP ("ftrace") "\n\
15754 Do \"help tracepoints\" for info on other tracepoint commands."));
15755 set_cmd_completer (c, location_completer);
15756
15757 c = add_com ("strace", class_breakpoint, strace_command, _("\
15758 Set a static tracepoint at location or marker.\n\
15759 \n\
15760 strace [LOCATION] [if CONDITION]\n\
15761 LOCATION may be a linespec, explicit, or address location (described below) \n\
15762 or -m MARKER_ID.\n\n\
15763 If a marker id is specified, probe the marker with that name. With\n\
15764 no LOCATION, uses current execution address of the selected stack frame.\n\
15765 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15766 This collects arbitrary user data passed in the probe point call to the\n\
15767 tracing library. You can inspect it when analyzing the trace buffer,\n\
15768 by printing the $_sdata variable like any other convenience variable.\n\
15769 \n\
15770 CONDITION is a boolean expression.\n\
15771 \n" LOCATION_HELP_STRING "\n\n\
15772 Multiple tracepoints at one place are permitted, and useful if their\n\
15773 conditions are different.\n\
15774 \n\
15775 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15776 Do \"help tracepoints\" for info on other tracepoint commands."));
15777 set_cmd_completer (c, location_completer);
15778
15779 add_info ("tracepoints", info_tracepoints_command, _("\
15780 Status of specified tracepoints (all tracepoints if no argument).\n\
15781 Convenience variable \"$tpnum\" contains the number of the\n\
15782 last tracepoint set."));
15783
15784 add_info_alias ("tp", "tracepoints", 1);
15785
15786 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15787 Delete specified tracepoints.\n\
15788 Arguments are tracepoint numbers, separated by spaces.\n\
15789 No argument means delete all tracepoints."),
15790 &deletelist);
15791 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15792
15793 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15794 Disable specified tracepoints.\n\
15795 Arguments are tracepoint numbers, separated by spaces.\n\
15796 No argument means disable all tracepoints."),
15797 &disablelist);
15798 deprecate_cmd (c, "disable");
15799
15800 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15801 Enable specified tracepoints.\n\
15802 Arguments are tracepoint numbers, separated by spaces.\n\
15803 No argument means enable all tracepoints."),
15804 &enablelist);
15805 deprecate_cmd (c, "enable");
15806
15807 add_com ("passcount", class_trace, trace_pass_command, _("\
15808 Set the passcount for a tracepoint.\n\
15809 The trace will end when the tracepoint has been passed 'count' times.\n\
15810 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15811 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15812
15813 add_prefix_cmd ("save", class_breakpoint, save_command,
15814 _("Save breakpoint definitions as a script."),
15815 &save_cmdlist, "save ",
15816 0/*allow-unknown*/, &cmdlist);
15817
15818 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15819 Save current breakpoint definitions as a script.\n\
15820 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15821 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15822 session to restore them."),
15823 &save_cmdlist);
15824 set_cmd_completer (c, filename_completer);
15825
15826 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15827 Save current tracepoint definitions as a script.\n\
15828 Use the 'source' command in another debug session to restore them."),
15829 &save_cmdlist);
15830 set_cmd_completer (c, filename_completer);
15831
15832 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15833 deprecate_cmd (c, "save tracepoints");
15834
15835 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
15836 Breakpoint specific settings.\n\
15837 Configure various breakpoint-specific variables such as\n\
15838 pending breakpoint behavior."),
15839 &breakpoint_set_cmdlist, "set breakpoint ",
15840 0/*allow-unknown*/, &setlist);
15841 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
15842 Breakpoint specific settings.\n\
15843 Configure various breakpoint-specific variables such as\n\
15844 pending breakpoint behavior."),
15845 &breakpoint_show_cmdlist, "show breakpoint ",
15846 0/*allow-unknown*/, &showlist);
15847
15848 add_setshow_auto_boolean_cmd ("pending", no_class,
15849 &pending_break_support, _("\
15850 Set debugger's behavior regarding pending breakpoints."), _("\
15851 Show debugger's behavior regarding pending breakpoints."), _("\
15852 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15853 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15854 an error. If auto, an unrecognized breakpoint location results in a\n\
15855 user-query to see if a pending breakpoint should be created."),
15856 NULL,
15857 show_pending_break_support,
15858 &breakpoint_set_cmdlist,
15859 &breakpoint_show_cmdlist);
15860
15861 pending_break_support = AUTO_BOOLEAN_AUTO;
15862
15863 add_setshow_boolean_cmd ("auto-hw", no_class,
15864 &automatic_hardware_breakpoints, _("\
15865 Set automatic usage of hardware breakpoints."), _("\
15866 Show automatic usage of hardware breakpoints."), _("\
15867 If set, the debugger will automatically use hardware breakpoints for\n\
15868 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15869 a warning will be emitted for such breakpoints."),
15870 NULL,
15871 show_automatic_hardware_breakpoints,
15872 &breakpoint_set_cmdlist,
15873 &breakpoint_show_cmdlist);
15874
15875 add_setshow_boolean_cmd ("always-inserted", class_support,
15876 &always_inserted_mode, _("\
15877 Set mode for inserting breakpoints."), _("\
15878 Show mode for inserting breakpoints."), _("\
15879 When this mode is on, breakpoints are inserted immediately as soon as\n\
15880 they're created, kept inserted even when execution stops, and removed\n\
15881 only when the user deletes them. When this mode is off (the default),\n\
15882 breakpoints are inserted only when execution continues, and removed\n\
15883 when execution stops."),
15884 NULL,
15885 &show_always_inserted_mode,
15886 &breakpoint_set_cmdlist,
15887 &breakpoint_show_cmdlist);
15888
15889 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15890 condition_evaluation_enums,
15891 &condition_evaluation_mode_1, _("\
15892 Set mode of breakpoint condition evaluation."), _("\
15893 Show mode of breakpoint condition evaluation."), _("\
15894 When this is set to \"host\", breakpoint conditions will be\n\
15895 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15896 breakpoint conditions will be downloaded to the target (if the target\n\
15897 supports such feature) and conditions will be evaluated on the target's side.\n\
15898 If this is set to \"auto\" (default), this will be automatically set to\n\
15899 \"target\" if it supports condition evaluation, otherwise it will\n\
15900 be set to \"gdb\""),
15901 &set_condition_evaluation_mode,
15902 &show_condition_evaluation_mode,
15903 &breakpoint_set_cmdlist,
15904 &breakpoint_show_cmdlist);
15905
15906 add_com ("break-range", class_breakpoint, break_range_command, _("\
15907 Set a breakpoint for an address range.\n\
15908 break-range START-LOCATION, END-LOCATION\n\
15909 where START-LOCATION and END-LOCATION can be one of the following:\n\
15910 LINENUM, for that line in the current file,\n\
15911 FILE:LINENUM, for that line in that file,\n\
15912 +OFFSET, for that number of lines after the current line\n\
15913 or the start of the range\n\
15914 FUNCTION, for the first line in that function,\n\
15915 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15916 *ADDRESS, for the instruction at that address.\n\
15917 \n\
15918 The breakpoint will stop execution of the inferior whenever it executes\n\
15919 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15920 range (including START-LOCATION and END-LOCATION)."));
15921
15922 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15923 Set a dynamic printf at specified location.\n\
15924 dprintf location,format string,arg1,arg2,...\n\
15925 location may be a linespec, explicit, or address location.\n"
15926 "\n" LOCATION_HELP_STRING));
15927 set_cmd_completer (c, location_completer);
15928
15929 add_setshow_enum_cmd ("dprintf-style", class_support,
15930 dprintf_style_enums, &dprintf_style, _("\
15931 Set the style of usage for dynamic printf."), _("\
15932 Show the style of usage for dynamic printf."), _("\
15933 This setting chooses how GDB will do a dynamic printf.\n\
15934 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15935 console, as with the \"printf\" command.\n\
15936 If the value is \"call\", the print is done by calling a function in your\n\
15937 program; by default printf(), but you can choose a different function or\n\
15938 output stream by setting dprintf-function and dprintf-channel."),
15939 update_dprintf_commands, NULL,
15940 &setlist, &showlist);
15941
15942 dprintf_function = xstrdup ("printf");
15943 add_setshow_string_cmd ("dprintf-function", class_support,
15944 &dprintf_function, _("\
15945 Set the function to use for dynamic printf."), _("\
15946 Show the function to use for dynamic printf."), NULL,
15947 update_dprintf_commands, NULL,
15948 &setlist, &showlist);
15949
15950 dprintf_channel = xstrdup ("");
15951 add_setshow_string_cmd ("dprintf-channel", class_support,
15952 &dprintf_channel, _("\
15953 Set the channel to use for dynamic printf."), _("\
15954 Show the channel to use for dynamic printf."), NULL,
15955 update_dprintf_commands, NULL,
15956 &setlist, &showlist);
15957
15958 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15959 &disconnected_dprintf, _("\
15960 Set whether dprintf continues after GDB disconnects."), _("\
15961 Show whether dprintf continues after GDB disconnects."), _("\
15962 Use this to let dprintf commands continue to hit and produce output\n\
15963 even if GDB disconnects or detaches from the target."),
15964 NULL,
15965 NULL,
15966 &setlist, &showlist);
15967
15968 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15969 Target agent only formatted printing, like the C \"printf\" function.\n\
15970 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15971 This supports most C printf format specifications, like %s, %d, etc.\n\
15972 This is useful for formatted output in user-defined commands."));
15973
15974 automatic_hardware_breakpoints = true;
15975
15976 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
15977 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
15978 }
This page took 0.358997 seconds and 5 git commands to generate.