]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/breakpoint.c
Show locno for 'multi location' breakpoint hit msg+conv var $_hit_bbnum $_hit_locno...
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdbsupport/gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70 #include "cli/cli-decode.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
91 create_sals_from_location_spec_default (location_spec *locspec,
92 linespec_result *canonical);
93
94 static void create_breakpoints_sal (struct gdbarch *,
95 struct linespec_result *,
96 gdb::unique_xmalloc_ptr<char>,
97 gdb::unique_xmalloc_ptr<char>,
98 enum bptype,
99 enum bpdisp, int, int,
100 int,
101 int, int, int, unsigned);
102
103 static int can_use_hardware_watchpoint
104 (const std::vector<value_ref_ptr> &vals);
105
106 static void mention (const breakpoint *);
107
108 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
109
110 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
111
112 static struct breakpoint *
113 momentary_breakpoint_from_master (struct breakpoint *orig,
114 enum bptype type,
115 int loc_enabled, int thread);
116
117 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, bool);
118
119 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
120 CORE_ADDR bpaddr,
121 enum bptype bptype,
122 struct program_space *pspace);
123
124 static bool watchpoint_locations_match (const struct bp_location *loc1,
125 const struct bp_location *loc2);
126
127 static bool breakpoint_locations_match (const struct bp_location *loc1,
128 const struct bp_location *loc2,
129 bool sw_hw_bps_match = false);
130
131 static bool breakpoint_location_address_match (struct bp_location *bl,
132 const struct address_space *aspace,
133 CORE_ADDR addr);
134
135 static bool breakpoint_location_address_range_overlap (struct bp_location *,
136 const address_space *,
137 CORE_ADDR, int);
138
139 static int remove_breakpoint (struct bp_location *);
140 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
141
142 static enum print_stop_action print_bp_stop_message (bpstat *bs);
143
144 static int hw_breakpoint_used_count (void);
145
146 static int hw_watchpoint_use_count (struct breakpoint *);
147
148 static int hw_watchpoint_used_count_others (struct breakpoint *except,
149 enum bptype type,
150 int *other_type_used);
151
152 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
153 int count);
154
155 static void decref_bp_location (struct bp_location **loc);
156
157 static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
158 (struct breakpoint *b,
159 location_spec *locspec,
160 struct program_space *search_pspace);
161
162 static bool bl_address_is_meaningful (bp_location *loc);
163
164 static int find_loc_num_by_location (const bp_location *loc);
165
166 /* update_global_location_list's modes of operation wrt to whether to
167 insert locations now. */
168 enum ugll_insert_mode
169 {
170 /* Don't insert any breakpoint locations into the inferior, only
171 remove already-inserted locations that no longer should be
172 inserted. Functions that delete a breakpoint or breakpoints
173 should specify this mode, so that deleting a breakpoint doesn't
174 have the side effect of inserting the locations of other
175 breakpoints that are marked not-inserted, but should_be_inserted
176 returns true on them.
177
178 This behavior is useful is situations close to tear-down -- e.g.,
179 after an exec, while the target still has execution, but
180 breakpoint shadows of the previous executable image should *NOT*
181 be restored to the new image; or before detaching, where the
182 target still has execution and wants to delete breakpoints from
183 GDB's lists, and all breakpoints had already been removed from
184 the inferior. */
185 UGLL_DONT_INSERT,
186
187 /* May insert breakpoints iff breakpoints_should_be_inserted_now
188 claims breakpoints should be inserted now. */
189 UGLL_MAY_INSERT,
190
191 /* Insert locations now, irrespective of
192 breakpoints_should_be_inserted_now. E.g., say all threads are
193 stopped right now, and the user did "continue". We need to
194 insert breakpoints _before_ resuming the target, but
195 UGLL_MAY_INSERT wouldn't insert them, because
196 breakpoints_should_be_inserted_now returns false at that point,
197 as no thread is running yet. */
198 UGLL_INSERT
199 };
200
201 static void update_global_location_list (enum ugll_insert_mode);
202
203 static void update_global_location_list_nothrow (enum ugll_insert_mode);
204
205 static void insert_breakpoint_locations (void);
206
207 static void trace_pass_command (const char *, int);
208
209 static void set_tracepoint_count (int num);
210
211 static bool is_masked_watchpoint (const struct breakpoint *b);
212
213 /* Return true if B refers to a static tracepoint set by marker ("-m"),
214 zero otherwise. */
215
216 static bool strace_marker_p (struct breakpoint *b);
217
218 static void bkpt_probe_create_sals_from_location_spec
219 (location_spec *locspec,
220 struct linespec_result *canonical);
221 static void tracepoint_probe_create_sals_from_location_spec
222 (location_spec *locspec,
223 struct linespec_result *canonical);
224
225 const struct breakpoint_ops code_breakpoint_ops =
226 {
227 create_sals_from_location_spec_default,
228 create_breakpoints_sal,
229 };
230
231 /* Breakpoints set on probes. */
232 static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
233 {
234 bkpt_probe_create_sals_from_location_spec,
235 create_breakpoints_sal,
236 };
237
238 /* Tracepoints set on probes. */
239 static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
240 {
241 tracepoint_probe_create_sals_from_location_spec,
242 create_breakpoints_sal,
243 };
244
245 /* Implementation of abstract dtors. These must exist to satisfy the
246 linker. */
247
248 breakpoint::~breakpoint ()
249 {
250 }
251
252 code_breakpoint::~code_breakpoint ()
253 {
254 }
255
256 catchpoint::~catchpoint ()
257 {
258 }
259
260 /* The structure to be used in regular breakpoints. */
261 struct ordinary_breakpoint : public code_breakpoint
262 {
263 using code_breakpoint::code_breakpoint;
264
265 int resources_needed (const struct bp_location *) override;
266 enum print_stop_action print_it (const bpstat *bs) const override;
267 void print_mention () const override;
268 void print_recreate (struct ui_file *fp) const override;
269 };
270
271 /* Internal breakpoints. These typically have a lifetime the same as
272 the program, and they end up installed on the breakpoint chain with
273 a negative breakpoint number. They're visible in "maint info
274 breakpoints", but not "info breakpoints". */
275 struct internal_breakpoint : public code_breakpoint
276 {
277 internal_breakpoint (struct gdbarch *gdbarch,
278 enum bptype type, CORE_ADDR address)
279 : code_breakpoint (gdbarch, type)
280 {
281 symtab_and_line sal;
282 sal.pc = address;
283 sal.section = find_pc_overlay (sal.pc);
284 sal.pspace = current_program_space;
285 add_location (sal);
286
287 pspace = current_program_space;
288 disposition = disp_donttouch;
289 }
290
291 void re_set () override;
292 void check_status (struct bpstat *bs) override;
293 enum print_stop_action print_it (const bpstat *bs) const override;
294 void print_mention () const override;
295 };
296
297 /* Momentary breakpoints. These typically have a lifetime of some run
298 control command only, are always thread-specific, and have 0 for
299 breakpoint number. I.e., there can be many momentary breakpoints
300 on the breakpoint chain and they all same the same number (zero).
301 They're visible in "maint info breakpoints", but not "info
302 breakpoints". */
303 struct momentary_breakpoint : public code_breakpoint
304 {
305 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
306 program_space *pspace_,
307 const struct frame_id &frame_id_,
308 int thread_)
309 : code_breakpoint (gdbarch_, bptype)
310 {
311 /* If FRAME_ID is valid, it should be a real frame, not an inlined
312 or tail-called one. */
313 gdb_assert (!frame_id_artificial_p (frame_id));
314
315 /* Momentary breakpoints are always thread-specific. */
316 gdb_assert (thread_ > 0);
317
318 pspace = pspace_;
319 enable_state = bp_enabled;
320 disposition = disp_donttouch;
321 frame_id = frame_id_;
322 thread = thread_;
323 }
324
325 void re_set () override;
326 void check_status (struct bpstat *bs) override;
327 enum print_stop_action print_it (const bpstat *bs) const override;
328 void print_mention () const override;
329 };
330
331 /* DPrintf breakpoints. */
332 struct dprintf_breakpoint : public ordinary_breakpoint
333 {
334 using ordinary_breakpoint::ordinary_breakpoint;
335
336 void re_set () override;
337 int breakpoint_hit (const struct bp_location *bl,
338 const address_space *aspace,
339 CORE_ADDR bp_addr,
340 const target_waitstatus &ws) override;
341 void print_recreate (struct ui_file *fp) const override;
342 void after_condition_true (struct bpstat *bs) override;
343 };
344
345 /* Ranged breakpoints. */
346 struct ranged_breakpoint : public ordinary_breakpoint
347 {
348 explicit ranged_breakpoint (struct gdbarch *gdbarch,
349 const symtab_and_line &sal_start,
350 int length,
351 location_spec_up start_locspec,
352 location_spec_up end_locspec)
353 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
354 {
355 bp_location *bl = add_location (sal_start);
356 bl->length = length;
357
358 disposition = disp_donttouch;
359
360 locspec = std::move (start_locspec);
361 locspec_range_end = std::move (end_locspec);
362 }
363
364 int breakpoint_hit (const struct bp_location *bl,
365 const address_space *aspace,
366 CORE_ADDR bp_addr,
367 const target_waitstatus &ws) override;
368 int resources_needed (const struct bp_location *) override;
369 enum print_stop_action print_it (const bpstat *bs) const override;
370 bool print_one (bp_location **) const override;
371 void print_one_detail (struct ui_out *) const override;
372 void print_mention () const override;
373 void print_recreate (struct ui_file *fp) const override;
374 };
375
376 /* Static tracepoints with marker (`-m'). */
377 struct static_marker_tracepoint : public tracepoint
378 {
379 using tracepoint::tracepoint;
380
381 std::vector<symtab_and_line> decode_location_spec
382 (struct location_spec *locspec,
383 struct program_space *search_pspace) override;
384 };
385
386 /* The style in which to perform a dynamic printf. This is a user
387 option because different output options have different tradeoffs;
388 if GDB does the printing, there is better error handling if there
389 is a problem with any of the arguments, but using an inferior
390 function lets you have special-purpose printers and sending of
391 output to the same place as compiled-in print functions. */
392
393 static const char dprintf_style_gdb[] = "gdb";
394 static const char dprintf_style_call[] = "call";
395 static const char dprintf_style_agent[] = "agent";
396 static const char *const dprintf_style_enums[] = {
397 dprintf_style_gdb,
398 dprintf_style_call,
399 dprintf_style_agent,
400 NULL
401 };
402 static const char *dprintf_style = dprintf_style_gdb;
403
404 /* The function to use for dynamic printf if the preferred style is to
405 call into the inferior. The value is simply a string that is
406 copied into the command, so it can be anything that GDB can
407 evaluate to a callable address, not necessarily a function name. */
408
409 static std::string dprintf_function = "printf";
410
411 /* The channel to use for dynamic printf if the preferred style is to
412 call into the inferior; if a nonempty string, it will be passed to
413 the call as the first argument, with the format string as the
414 second. As with the dprintf function, this can be anything that
415 GDB knows how to evaluate, so in addition to common choices like
416 "stderr", this could be an app-specific expression like
417 "mystreams[curlogger]". */
418
419 static std::string dprintf_channel;
420
421 /* True if dprintf commands should continue to operate even if GDB
422 has disconnected. */
423 static bool disconnected_dprintf = true;
424
425 struct command_line *
426 breakpoint_commands (struct breakpoint *b)
427 {
428 return b->commands ? b->commands.get () : NULL;
429 }
430
431 /* Flag indicating that a command has proceeded the inferior past the
432 current breakpoint. */
433
434 static bool breakpoint_proceeded;
435
436 const char *
437 bpdisp_text (enum bpdisp disp)
438 {
439 /* NOTE: the following values are a part of MI protocol and
440 represent values of 'disp' field returned when inferior stops at
441 a breakpoint. */
442 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
443
444 return bpdisps[(int) disp];
445 }
446
447 /* Prototypes for exported functions. */
448 /* If FALSE, gdb will not use hardware support for watchpoints, even
449 if such is available. */
450 static int can_use_hw_watchpoints;
451
452 static void
453 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
454 struct cmd_list_element *c,
455 const char *value)
456 {
457 gdb_printf (file,
458 _("Debugger's willingness to use "
459 "watchpoint hardware is %s.\n"),
460 value);
461 }
462
463 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
464 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
465 for unrecognized breakpoint locations.
466 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
467 static enum auto_boolean pending_break_support;
468 static void
469 show_pending_break_support (struct ui_file *file, int from_tty,
470 struct cmd_list_element *c,
471 const char *value)
472 {
473 gdb_printf (file,
474 _("Debugger's behavior regarding "
475 "pending breakpoints is %s.\n"),
476 value);
477 }
478
479 /* If true, gdb will automatically use hardware breakpoints for breakpoints
480 set with "break" but falling in read-only memory.
481 If false, gdb will warn about such breakpoints, but won't automatically
482 use hardware breakpoints. */
483 static bool automatic_hardware_breakpoints;
484 static void
485 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
486 struct cmd_list_element *c,
487 const char *value)
488 {
489 gdb_printf (file,
490 _("Automatic usage of hardware breakpoints is %s.\n"),
491 value);
492 }
493
494 /* If on, GDB keeps breakpoints inserted even if the inferior is
495 stopped, and immediately inserts any new breakpoints as soon as
496 they're created. If off (default), GDB keeps breakpoints off of
497 the target as long as possible. That is, it delays inserting
498 breakpoints until the next resume, and removes them again when the
499 target fully stops. This is a bit safer in case GDB crashes while
500 processing user input. */
501 static bool always_inserted_mode = false;
502
503 static void
504 show_always_inserted_mode (struct ui_file *file, int from_tty,
505 struct cmd_list_element *c, const char *value)
506 {
507 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
508 value);
509 }
510
511 /* See breakpoint.h. */
512
513 int
514 breakpoints_should_be_inserted_now (void)
515 {
516 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
517 {
518 /* If breakpoints are global, they should be inserted even if no
519 thread under gdb's control is running, or even if there are
520 no threads under GDB's control yet. */
521 return 1;
522 }
523 else
524 {
525 if (always_inserted_mode)
526 {
527 /* The user wants breakpoints inserted even if all threads
528 are stopped. */
529 return 1;
530 }
531
532 for (inferior *inf : all_inferiors ())
533 if (inf->has_execution ()
534 && threads_are_executing (inf->process_target ()))
535 return 1;
536
537 /* Don't remove breakpoints yet if, even though all threads are
538 stopped, we still have events to process. */
539 for (thread_info *tp : all_non_exited_threads ())
540 if (tp->resumed () && tp->has_pending_waitstatus ())
541 return 1;
542 }
543 return 0;
544 }
545
546 static const char condition_evaluation_both[] = "host or target";
547
548 /* Modes for breakpoint condition evaluation. */
549 static const char condition_evaluation_auto[] = "auto";
550 static const char condition_evaluation_host[] = "host";
551 static const char condition_evaluation_target[] = "target";
552 static const char *const condition_evaluation_enums[] = {
553 condition_evaluation_auto,
554 condition_evaluation_host,
555 condition_evaluation_target,
556 NULL
557 };
558
559 /* Global that holds the current mode for breakpoint condition evaluation. */
560 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
561
562 /* Global that we use to display information to the user (gets its value from
563 condition_evaluation_mode_1. */
564 static const char *condition_evaluation_mode = condition_evaluation_auto;
565
566 /* Translate a condition evaluation mode MODE into either "host"
567 or "target". This is used mostly to translate from "auto" to the
568 real setting that is being used. It returns the translated
569 evaluation mode. */
570
571 static const char *
572 translate_condition_evaluation_mode (const char *mode)
573 {
574 if (mode == condition_evaluation_auto)
575 {
576 if (target_supports_evaluation_of_breakpoint_conditions ())
577 return condition_evaluation_target;
578 else
579 return condition_evaluation_host;
580 }
581 else
582 return mode;
583 }
584
585 /* Discovers what condition_evaluation_auto translates to. */
586
587 static const char *
588 breakpoint_condition_evaluation_mode (void)
589 {
590 return translate_condition_evaluation_mode (condition_evaluation_mode);
591 }
592
593 /* Return true if GDB should evaluate breakpoint conditions or false
594 otherwise. */
595
596 static bool
597 gdb_evaluates_breakpoint_condition_p (void)
598 {
599 const char *mode = breakpoint_condition_evaluation_mode ();
600
601 return (mode == condition_evaluation_host);
602 }
603
604 /* Are we executing breakpoint commands? */
605 static int executing_breakpoint_commands;
606
607 /* Are overlay event breakpoints enabled? */
608 static int overlay_events_enabled;
609
610 /* See description in breakpoint.h. */
611 bool target_exact_watchpoints = false;
612
613 /* Walk the following statement or block through all breakpoints.
614 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
615 current breakpoint. */
616
617 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
618 for (B = breakpoint_chain; \
619 B ? (TMP=B->next, 1): 0; \
620 B = TMP)
621
622 /* Chains of all breakpoints defined. */
623
624 static struct breakpoint *breakpoint_chain;
625
626 /* See breakpoint.h. */
627
628 breakpoint_range
629 all_breakpoints ()
630 {
631 return breakpoint_range (breakpoint_chain);
632 }
633
634 /* See breakpoint.h. */
635
636 breakpoint_safe_range
637 all_breakpoints_safe ()
638 {
639 return breakpoint_safe_range (all_breakpoints ());
640 }
641
642 /* See breakpoint.h. */
643
644 tracepoint_range
645 all_tracepoints ()
646 {
647 return tracepoint_range (breakpoint_chain);
648 }
649
650 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
651
652 static std::vector<bp_location *> bp_locations;
653
654 /* See breakpoint.h. */
655
656 const std::vector<bp_location *> &
657 all_bp_locations ()
658 {
659 return bp_locations;
660 }
661
662 /* Range to iterate over breakpoint locations at a given address. */
663
664 struct bp_locations_at_addr_range
665 {
666 using iterator = std::vector<bp_location *>::iterator;
667
668 bp_locations_at_addr_range (CORE_ADDR addr)
669 {
670 struct compare
671 {
672 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
673 { return loc->address < addr_; }
674
675 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
676 { return addr_ < loc->address; }
677 };
678
679 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
680 addr, compare ());
681
682 m_begin = it_pair.first;
683 m_end = it_pair.second;
684 }
685
686 iterator begin () const
687 { return m_begin; }
688
689 iterator end () const
690 { return m_end; }
691
692 private:
693 iterator m_begin;
694 iterator m_end;
695 };
696
697 /* Return a range to iterate over all breakpoint locations exactly at address
698 ADDR.
699
700 If it's needed to iterate multiple times on the same range, it's possible
701 to save the range in a local variable and use it multiple times:
702
703 auto range = all_bp_locations_at_addr (addr);
704
705 for (bp_location *loc : range)
706 // use loc
707
708 for (bp_location *loc : range)
709 // use loc
710
711 This saves a bit of time, as it avoids re-doing the binary searches to find
712 the range's boundaries. Just remember not to change the bp_locations vector
713 in the mean time, as it could make the range's iterators stale. */
714
715 static bp_locations_at_addr_range
716 all_bp_locations_at_addr (CORE_ADDR addr)
717 {
718 return bp_locations_at_addr_range (addr);
719 }
720
721 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
722 ADDRESS for the current elements of BP_LOCATIONS which get a valid
723 result from bp_location_has_shadow. You can use it for roughly
724 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
725 an address you need to read. */
726
727 static CORE_ADDR bp_locations_placed_address_before_address_max;
728
729 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
730 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
731 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
732 You can use it for roughly limiting the subrange of BP_LOCATIONS to
733 scan for shadow bytes for an address you need to read. */
734
735 static CORE_ADDR bp_locations_shadow_len_after_address_max;
736
737 /* The locations that no longer correspond to any breakpoint, unlinked
738 from the bp_locations array, but for which a hit may still be
739 reported by a target. */
740 static std::vector<bp_location *> moribund_locations;
741
742 /* Number of last breakpoint made. */
743
744 static int breakpoint_count;
745
746 /* The value of `breakpoint_count' before the last command that
747 created breakpoints. If the last (break-like) command created more
748 than one breakpoint, then the difference between BREAKPOINT_COUNT
749 and PREV_BREAKPOINT_COUNT is more than one. */
750 static int prev_breakpoint_count;
751
752 /* Number of last tracepoint made. */
753
754 static int tracepoint_count;
755
756 static struct cmd_list_element *breakpoint_set_cmdlist;
757 static struct cmd_list_element *breakpoint_show_cmdlist;
758 struct cmd_list_element *save_cmdlist;
759
760 /* Return whether a breakpoint is an active enabled breakpoint. */
761 static bool
762 breakpoint_enabled (struct breakpoint *b)
763 {
764 return (b->enable_state == bp_enabled);
765 }
766
767 /* Set breakpoint count to NUM. */
768
769 static void
770 set_breakpoint_count (int num)
771 {
772 prev_breakpoint_count = breakpoint_count;
773 breakpoint_count = num;
774 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
775 }
776
777 /* Used by `start_rbreak_breakpoints' below, to record the current
778 breakpoint count before "rbreak" creates any breakpoint. */
779 static int rbreak_start_breakpoint_count;
780
781 /* Called at the start an "rbreak" command to record the first
782 breakpoint made. */
783
784 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
785 {
786 rbreak_start_breakpoint_count = breakpoint_count;
787 }
788
789 /* Called at the end of an "rbreak" command to record the last
790 breakpoint made. */
791
792 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
793 {
794 prev_breakpoint_count = rbreak_start_breakpoint_count;
795 }
796
797 /* Used in run_command to zero the hit count when a new run starts. */
798
799 void
800 clear_breakpoint_hit_counts (void)
801 {
802 for (breakpoint *b : all_breakpoints ())
803 b->hit_count = 0;
804 }
805
806 \f
807 /* Return the breakpoint with the specified number, or NULL
808 if the number does not refer to an existing breakpoint. */
809
810 struct breakpoint *
811 get_breakpoint (int num)
812 {
813 for (breakpoint *b : all_breakpoints ())
814 if (b->number == num)
815 return b;
816
817 return nullptr;
818 }
819
820 /* Return TRUE if NUM refer to an existing breakpoint that has
821 multiple code locations. */
822
823 static bool
824 has_multiple_locations (int num)
825 {
826 for (breakpoint *b : all_breakpoints ())
827 if (b->number == num)
828 return b->loc != nullptr && b->loc->next != nullptr;
829
830 return false;
831 }
832
833 \f
834
835 /* Mark locations as "conditions have changed" in case the target supports
836 evaluating conditions on its side. */
837
838 static void
839 mark_breakpoint_modified (struct breakpoint *b)
840 {
841 /* This is only meaningful if the target is
842 evaluating conditions and if the user has
843 opted for condition evaluation on the target's
844 side. */
845 if (gdb_evaluates_breakpoint_condition_p ()
846 || !target_supports_evaluation_of_breakpoint_conditions ())
847 return;
848
849 if (!is_breakpoint (b))
850 return;
851
852 for (bp_location *loc : b->locations ())
853 loc->condition_changed = condition_modified;
854 }
855
856 /* Mark location as "conditions have changed" in case the target supports
857 evaluating conditions on its side. */
858
859 static void
860 mark_breakpoint_location_modified (struct bp_location *loc)
861 {
862 /* This is only meaningful if the target is
863 evaluating conditions and if the user has
864 opted for condition evaluation on the target's
865 side. */
866 if (gdb_evaluates_breakpoint_condition_p ()
867 || !target_supports_evaluation_of_breakpoint_conditions ())
868
869 return;
870
871 if (!is_breakpoint (loc->owner))
872 return;
873
874 loc->condition_changed = condition_modified;
875 }
876
877 /* Sets the condition-evaluation mode using the static global
878 condition_evaluation_mode. */
879
880 static void
881 set_condition_evaluation_mode (const char *args, int from_tty,
882 struct cmd_list_element *c)
883 {
884 const char *old_mode, *new_mode;
885
886 if ((condition_evaluation_mode_1 == condition_evaluation_target)
887 && !target_supports_evaluation_of_breakpoint_conditions ())
888 {
889 condition_evaluation_mode_1 = condition_evaluation_mode;
890 warning (_("Target does not support breakpoint condition evaluation.\n"
891 "Using host evaluation mode instead."));
892 return;
893 }
894
895 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
896 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
897
898 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
899 settings was "auto". */
900 condition_evaluation_mode = condition_evaluation_mode_1;
901
902 /* Only update the mode if the user picked a different one. */
903 if (new_mode != old_mode)
904 {
905 /* If the user switched to a different evaluation mode, we
906 need to synch the changes with the target as follows:
907
908 "host" -> "target": Send all (valid) conditions to the target.
909 "target" -> "host": Remove all the conditions from the target.
910 */
911
912 if (new_mode == condition_evaluation_target)
913 {
914 /* Mark everything modified and synch conditions with the
915 target. */
916 for (bp_location *loc : all_bp_locations ())
917 mark_breakpoint_location_modified (loc);
918 }
919 else
920 {
921 /* Manually mark non-duplicate locations to synch conditions
922 with the target. We do this to remove all the conditions the
923 target knows about. */
924 for (bp_location *loc : all_bp_locations ())
925 if (is_breakpoint (loc->owner) && loc->inserted)
926 loc->needs_update = 1;
927 }
928
929 /* Do the update. */
930 update_global_location_list (UGLL_MAY_INSERT);
931 }
932
933 return;
934 }
935
936 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
937 what "auto" is translating to. */
938
939 static void
940 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
941 struct cmd_list_element *c, const char *value)
942 {
943 if (condition_evaluation_mode == condition_evaluation_auto)
944 gdb_printf (file,
945 _("Breakpoint condition evaluation "
946 "mode is %s (currently %s).\n"),
947 value,
948 breakpoint_condition_evaluation_mode ());
949 else
950 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
951 value);
952 }
953
954 /* Parse COND_STRING in the context of LOC and set as the condition
955 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
956 the number of LOC within its owner. In case of parsing error, mark
957 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
958
959 static void
960 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
961 int bp_num, int loc_num)
962 {
963 bool has_junk = false;
964 try
965 {
966 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
967 block_for_pc (loc->address), 0);
968 if (*cond_string != 0)
969 has_junk = true;
970 else
971 {
972 loc->cond = std::move (new_exp);
973 if (loc->disabled_by_cond && loc->enabled)
974 gdb_printf (_("Breakpoint %d's condition is now valid at "
975 "location %d, enabling.\n"),
976 bp_num, loc_num);
977
978 loc->disabled_by_cond = false;
979 }
980 }
981 catch (const gdb_exception_error &e)
982 {
983 if (loc->enabled)
984 {
985 /* Warn if a user-enabled location is now becoming disabled-by-cond.
986 BP_NUM is 0 if the breakpoint is being defined for the first
987 time using the "break ... if ..." command, and non-zero if
988 already defined. */
989 if (bp_num != 0)
990 warning (_("failed to validate condition at location %d.%d, "
991 "disabling:\n %s"), bp_num, loc_num, e.what ());
992 else
993 warning (_("failed to validate condition at location %d, "
994 "disabling:\n %s"), loc_num, e.what ());
995 }
996
997 loc->disabled_by_cond = true;
998 }
999
1000 if (has_junk)
1001 error (_("Garbage '%s' follows condition"), cond_string);
1002 }
1003
1004 void
1005 set_breakpoint_condition (struct breakpoint *b, const char *exp,
1006 int from_tty, bool force)
1007 {
1008 if (*exp == 0)
1009 {
1010 b->cond_string.reset ();
1011
1012 if (is_watchpoint (b))
1013 gdb::checked_static_cast<watchpoint *> (b)->cond_exp.reset ();
1014 else
1015 {
1016 int loc_num = 1;
1017 for (bp_location *loc : b->locations ())
1018 {
1019 loc->cond.reset ();
1020 if (loc->disabled_by_cond && loc->enabled)
1021 gdb_printf (_("Breakpoint %d's condition is now valid at "
1022 "location %d, enabling.\n"),
1023 b->number, loc_num);
1024 loc->disabled_by_cond = false;
1025 loc_num++;
1026
1027 /* No need to free the condition agent expression
1028 bytecode (if we have one). We will handle this
1029 when we go through update_global_location_list. */
1030 }
1031 }
1032
1033 if (from_tty)
1034 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
1035 }
1036 else
1037 {
1038 if (is_watchpoint (b))
1039 {
1040 innermost_block_tracker tracker;
1041 const char *arg = exp;
1042 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
1043 if (*arg != 0)
1044 error (_("Junk at end of expression"));
1045 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
1046 w->cond_exp = std::move (new_exp);
1047 w->cond_exp_valid_block = tracker.block ();
1048 }
1049 else
1050 {
1051 /* Parse and set condition expressions. We make two passes.
1052 In the first, we parse the condition string to see if it
1053 is valid in at least one location. If so, the condition
1054 would be accepted. So we go ahead and set the locations'
1055 conditions. In case no valid case is found, we throw
1056 the error and the condition string will be rejected.
1057 This two-pass approach is taken to avoid setting the
1058 state of locations in case of a reject. */
1059 for (bp_location *loc : b->locations ())
1060 {
1061 try
1062 {
1063 const char *arg = exp;
1064 parse_exp_1 (&arg, loc->address,
1065 block_for_pc (loc->address), 0);
1066 if (*arg != 0)
1067 error (_("Junk at end of expression"));
1068 break;
1069 }
1070 catch (const gdb_exception_error &e)
1071 {
1072 /* Condition string is invalid. If this happens to
1073 be the last loc, abandon (if not forced) or continue
1074 (if forced). */
1075 if (loc->next == nullptr && !force)
1076 throw;
1077 }
1078 }
1079
1080 /* If we reach here, the condition is valid at some locations. */
1081 int loc_num = 1;
1082 for (bp_location *loc : b->locations ())
1083 {
1084 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1085 loc_num++;
1086 }
1087 }
1088
1089 /* We know that the new condition parsed successfully. The
1090 condition string of the breakpoint can be safely updated. */
1091 b->cond_string = make_unique_xstrdup (exp);
1092 b->condition_not_parsed = 0;
1093 }
1094 mark_breakpoint_modified (b);
1095
1096 gdb::observers::breakpoint_modified.notify (b);
1097 }
1098
1099 /* See breakpoint.h. */
1100
1101 void
1102 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1103 bool force)
1104 {
1105 for (breakpoint *b : all_breakpoints ())
1106 if (b->number == bpnum)
1107 {
1108 /* Check if this breakpoint has a "stop" method implemented in an
1109 extension language. This method and conditions entered into GDB
1110 from the CLI are mutually exclusive. */
1111 const struct extension_language_defn *extlang
1112 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1113
1114 if (extlang != NULL)
1115 {
1116 error (_("Only one stop condition allowed. There is currently"
1117 " a %s stop condition defined for this breakpoint."),
1118 ext_lang_capitalized_name (extlang));
1119 }
1120 set_breakpoint_condition (b, exp, from_tty, force);
1121
1122 if (is_breakpoint (b))
1123 update_global_location_list (UGLL_MAY_INSERT);
1124
1125 return;
1126 }
1127
1128 error (_("No breakpoint number %d."), bpnum);
1129 }
1130
1131 /* The options for the "condition" command. */
1132
1133 struct condition_command_opts
1134 {
1135 /* For "-force". */
1136 bool force_condition = false;
1137 };
1138
1139 static const gdb::option::option_def condition_command_option_defs[] = {
1140
1141 gdb::option::flag_option_def<condition_command_opts> {
1142 "force",
1143 [] (condition_command_opts *opts) { return &opts->force_condition; },
1144 N_("Set the condition even if it is invalid for all current locations."),
1145 },
1146
1147 };
1148
1149 /* Create an option_def_group for the "condition" options, with
1150 CC_OPTS as context. */
1151
1152 static inline gdb::option::option_def_group
1153 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1154 {
1155 return {{condition_command_option_defs}, cc_opts};
1156 }
1157
1158 /* Completion for the "condition" command. */
1159
1160 static void
1161 condition_completer (struct cmd_list_element *cmd,
1162 completion_tracker &tracker,
1163 const char *text, const char * /*word*/)
1164 {
1165 bool has_no_arguments = (*text == '\0');
1166 condition_command_opts cc_opts;
1167 const auto group = make_condition_command_options_def_group (&cc_opts);
1168 if (gdb::option::complete_options
1169 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1170 return;
1171
1172 text = skip_spaces (text);
1173 const char *space = skip_to_space (text);
1174 if (*space == '\0')
1175 {
1176 int len;
1177
1178 if (text[0] == '$')
1179 {
1180 tracker.advance_custom_word_point_by (1);
1181 /* We don't support completion of history indices. */
1182 if (!isdigit (text[1]))
1183 complete_internalvar (tracker, &text[1]);
1184 return;
1185 }
1186
1187 /* Suggest the "-force" flag if no arguments are given. If
1188 arguments were passed, they either already include the flag,
1189 or we are beyond the point of suggesting it because it's
1190 positionally the first argument. */
1191 if (has_no_arguments)
1192 gdb::option::complete_on_all_options (tracker, group);
1193
1194 /* We're completing the breakpoint number. */
1195 len = strlen (text);
1196
1197 for (breakpoint *b : all_breakpoints ())
1198 {
1199 char number[50];
1200
1201 xsnprintf (number, sizeof (number), "%d", b->number);
1202
1203 if (strncmp (number, text, len) == 0)
1204 tracker.add_completion (make_unique_xstrdup (number));
1205 }
1206
1207 return;
1208 }
1209
1210 /* We're completing the expression part. Skip the breakpoint num. */
1211 const char *exp_start = skip_spaces (space);
1212 tracker.advance_custom_word_point_by (exp_start - text);
1213 text = exp_start;
1214 const char *word = advance_to_expression_complete_word_point (tracker, text);
1215 expression_completer (cmd, tracker, text, word);
1216 }
1217
1218 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1219
1220 static void
1221 condition_command (const char *arg, int from_tty)
1222 {
1223 const char *p;
1224 int bnum;
1225
1226 if (arg == 0)
1227 error_no_arg (_("breakpoint number"));
1228
1229 p = arg;
1230
1231 /* Check if the "-force" flag was passed. */
1232 condition_command_opts cc_opts;
1233 const auto group = make_condition_command_options_def_group (&cc_opts);
1234 gdb::option::process_options
1235 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1236
1237 bnum = get_number (&p);
1238 if (bnum == 0)
1239 error (_("Bad breakpoint argument: '%s'"), arg);
1240
1241 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1242 }
1243
1244 /* Check that COMMAND do not contain commands that are suitable
1245 only for tracepoints and not suitable for ordinary breakpoints.
1246 Throw if any such commands is found. */
1247
1248 static void
1249 check_no_tracepoint_commands (struct command_line *commands)
1250 {
1251 struct command_line *c;
1252
1253 for (c = commands; c; c = c->next)
1254 {
1255 if (c->control_type == while_stepping_control)
1256 error (_("The 'while-stepping' command can "
1257 "only be used for tracepoints"));
1258
1259 check_no_tracepoint_commands (c->body_list_0.get ());
1260 check_no_tracepoint_commands (c->body_list_1.get ());
1261
1262 /* Not that command parsing removes leading whitespace and comment
1263 lines and also empty lines. So, we only need to check for
1264 command directly. */
1265 if (strstr (c->line, "collect ") == c->line)
1266 error (_("The 'collect' command can only be used for tracepoints"));
1267
1268 if (strstr (c->line, "teval ") == c->line)
1269 error (_("The 'teval' command can only be used for tracepoints"));
1270 }
1271 }
1272
1273 struct longjmp_breakpoint : public momentary_breakpoint
1274 {
1275 using momentary_breakpoint::momentary_breakpoint;
1276
1277 ~longjmp_breakpoint () override;
1278 };
1279
1280 /* Encapsulate tests for different types of tracepoints. */
1281
1282 static bool
1283 is_tracepoint_type (bptype type)
1284 {
1285 return (type == bp_tracepoint
1286 || type == bp_fast_tracepoint
1287 || type == bp_static_tracepoint
1288 || type == bp_static_marker_tracepoint);
1289 }
1290
1291 /* See breakpoint.h. */
1292
1293 bool
1294 is_tracepoint (const struct breakpoint *b)
1295 {
1296 return is_tracepoint_type (b->type);
1297 }
1298
1299 /* Factory function to create an appropriate instance of breakpoint given
1300 TYPE. */
1301
1302 template<typename... Arg>
1303 static std::unique_ptr<code_breakpoint>
1304 new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1305 Arg&&... args)
1306 {
1307 code_breakpoint *b;
1308
1309 switch (type)
1310 {
1311 case bp_breakpoint:
1312 case bp_hardware_breakpoint:
1313 b = new ordinary_breakpoint (gdbarch, type,
1314 std::forward<Arg> (args)...);
1315 break;
1316
1317 case bp_fast_tracepoint:
1318 case bp_static_tracepoint:
1319 case bp_tracepoint:
1320 b = new tracepoint (gdbarch, type,
1321 std::forward<Arg> (args)...);
1322 break;
1323
1324 case bp_static_marker_tracepoint:
1325 b = new static_marker_tracepoint (gdbarch, type,
1326 std::forward<Arg> (args)...);
1327 break;
1328
1329 case bp_dprintf:
1330 b = new dprintf_breakpoint (gdbarch, type,
1331 std::forward<Arg> (args)...);
1332 break;
1333
1334 default:
1335 gdb_assert_not_reached ("invalid type");
1336 }
1337
1338 return std::unique_ptr<code_breakpoint> (b);
1339 }
1340
1341 /* A helper function that validates that COMMANDS are valid for a
1342 breakpoint. This function will throw an exception if a problem is
1343 found. */
1344
1345 static void
1346 validate_commands_for_breakpoint (struct breakpoint *b,
1347 struct command_line *commands)
1348 {
1349 if (is_tracepoint (b))
1350 {
1351 struct tracepoint *t = (struct tracepoint *) b;
1352 struct command_line *c;
1353 struct command_line *while_stepping = 0;
1354
1355 /* Reset the while-stepping step count. The previous commands
1356 might have included a while-stepping action, while the new
1357 ones might not. */
1358 t->step_count = 0;
1359
1360 /* We need to verify that each top-level element of commands is
1361 valid for tracepoints, that there's at most one
1362 while-stepping element, and that the while-stepping's body
1363 has valid tracing commands excluding nested while-stepping.
1364 We also need to validate the tracepoint action line in the
1365 context of the tracepoint --- validate_actionline actually
1366 has side effects, like setting the tracepoint's
1367 while-stepping STEP_COUNT, in addition to checking if the
1368 collect/teval actions parse and make sense in the
1369 tracepoint's context. */
1370 for (c = commands; c; c = c->next)
1371 {
1372 if (c->control_type == while_stepping_control)
1373 {
1374 if (b->type == bp_fast_tracepoint)
1375 error (_("The 'while-stepping' command "
1376 "cannot be used for fast tracepoint"));
1377 else if (b->type == bp_static_tracepoint
1378 || b->type == bp_static_marker_tracepoint)
1379 error (_("The 'while-stepping' command "
1380 "cannot be used for static tracepoint"));
1381
1382 if (while_stepping)
1383 error (_("The 'while-stepping' command "
1384 "can be used only once"));
1385 else
1386 while_stepping = c;
1387 }
1388
1389 validate_actionline (c->line, b);
1390 }
1391 if (while_stepping)
1392 {
1393 struct command_line *c2;
1394
1395 gdb_assert (while_stepping->body_list_1 == nullptr);
1396 c2 = while_stepping->body_list_0.get ();
1397 for (; c2; c2 = c2->next)
1398 {
1399 if (c2->control_type == while_stepping_control)
1400 error (_("The 'while-stepping' command cannot be nested"));
1401 }
1402 }
1403 }
1404 else
1405 {
1406 check_no_tracepoint_commands (commands);
1407 }
1408 }
1409
1410 /* Return a vector of all the static tracepoints set at ADDR. The
1411 caller is responsible for releasing the vector. */
1412
1413 std::vector<breakpoint *>
1414 static_tracepoints_here (CORE_ADDR addr)
1415 {
1416 std::vector<breakpoint *> found;
1417
1418 for (breakpoint *b : all_breakpoints ())
1419 if (b->type == bp_static_tracepoint
1420 || b->type == bp_static_marker_tracepoint)
1421 {
1422 for (bp_location *loc : b->locations ())
1423 if (loc->address == addr)
1424 found.push_back (b);
1425 }
1426
1427 return found;
1428 }
1429
1430 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1431 validate that only allowed commands are included. */
1432
1433 void
1434 breakpoint_set_commands (struct breakpoint *b,
1435 counted_command_line &&commands)
1436 {
1437 validate_commands_for_breakpoint (b, commands.get ());
1438
1439 b->commands = std::move (commands);
1440 gdb::observers::breakpoint_modified.notify (b);
1441 }
1442
1443 /* Set the internal `silent' flag on the breakpoint. Note that this
1444 is not the same as the "silent" that may appear in the breakpoint's
1445 commands. */
1446
1447 void
1448 breakpoint_set_silent (struct breakpoint *b, int silent)
1449 {
1450 int old_silent = b->silent;
1451
1452 b->silent = silent;
1453 if (old_silent != silent)
1454 gdb::observers::breakpoint_modified.notify (b);
1455 }
1456
1457 /* Set the thread for this breakpoint. If THREAD is -1, make the
1458 breakpoint work for any thread. */
1459
1460 void
1461 breakpoint_set_thread (struct breakpoint *b, int thread)
1462 {
1463 int old_thread = b->thread;
1464
1465 b->thread = thread;
1466 if (old_thread != thread)
1467 gdb::observers::breakpoint_modified.notify (b);
1468 }
1469
1470 /* Set the task for this breakpoint. If TASK is 0, make the
1471 breakpoint work for any task. */
1472
1473 void
1474 breakpoint_set_task (struct breakpoint *b, int task)
1475 {
1476 int old_task = b->task;
1477
1478 b->task = task;
1479 if (old_task != task)
1480 gdb::observers::breakpoint_modified.notify (b);
1481 }
1482
1483 static void
1484 commands_command_1 (const char *arg, int from_tty,
1485 struct command_line *control)
1486 {
1487 counted_command_line cmd;
1488 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1489 NULL after the call to read_command_lines if the user provides an empty
1490 list of command by just typing "end". */
1491 bool cmd_read = false;
1492
1493 std::string new_arg;
1494
1495 if (arg == NULL || !*arg)
1496 {
1497 /* Argument not explicitly given. Synthesize it. */
1498 if (breakpoint_count - prev_breakpoint_count > 1)
1499 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1500 breakpoint_count);
1501 else if (breakpoint_count > 0)
1502 new_arg = string_printf ("%d", breakpoint_count);
1503 }
1504 else
1505 {
1506 /* Create a copy of ARG. This is needed because the "commands"
1507 command may be coming from a script. In that case, the read
1508 line buffer is going to be overwritten in the lambda of
1509 'map_breakpoint_numbers' below when reading the next line
1510 before we are are done parsing the breakpoint numbers. */
1511 new_arg = arg;
1512 }
1513 arg = new_arg.c_str ();
1514
1515 map_breakpoint_numbers
1516 (arg, [&] (breakpoint *b)
1517 {
1518 if (!cmd_read)
1519 {
1520 gdb_assert (cmd == NULL);
1521 if (control != NULL)
1522 cmd = control->body_list_0;
1523 else
1524 {
1525 std::string str
1526 = string_printf (_("Type commands for breakpoint(s) "
1527 "%s, one per line."),
1528 arg);
1529
1530 auto do_validate = [=] (const char *line)
1531 {
1532 validate_actionline (line, b);
1533 };
1534 gdb::function_view<void (const char *)> validator;
1535 if (is_tracepoint (b))
1536 validator = do_validate;
1537
1538 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1539 }
1540 cmd_read = true;
1541 }
1542
1543 /* If a breakpoint was on the list more than once, we don't need to
1544 do anything. */
1545 if (b->commands != cmd)
1546 {
1547 validate_commands_for_breakpoint (b, cmd.get ());
1548 b->commands = cmd;
1549 gdb::observers::breakpoint_modified.notify (b);
1550 }
1551 });
1552 }
1553
1554 static void
1555 commands_command (const char *arg, int from_tty)
1556 {
1557 commands_command_1 (arg, from_tty, NULL);
1558 }
1559
1560 /* Like commands_command, but instead of reading the commands from
1561 input stream, takes them from an already parsed command structure.
1562
1563 This is used by cli-script.c to DTRT with breakpoint commands
1564 that are part of if and while bodies. */
1565 enum command_control_type
1566 commands_from_control_command (const char *arg, struct command_line *cmd)
1567 {
1568 commands_command_1 (arg, 0, cmd);
1569 return simple_control;
1570 }
1571
1572 /* Return true if BL->TARGET_INFO contains valid information. */
1573
1574 static bool
1575 bp_location_has_shadow (struct bp_location *bl)
1576 {
1577 if (bl->loc_type != bp_loc_software_breakpoint)
1578 return false;
1579 if (!bl->inserted)
1580 return false;
1581 if (bl->target_info.shadow_len == 0)
1582 /* BL isn't valid, or doesn't shadow memory. */
1583 return false;
1584 return true;
1585 }
1586
1587 /* Update BUF, which is LEN bytes read from the target address
1588 MEMADDR, by replacing a memory breakpoint with its shadowed
1589 contents.
1590
1591 If READBUF is not NULL, this buffer must not overlap with the of
1592 the breakpoint location's shadow_contents buffer. Otherwise, a
1593 failed assertion internal error will be raised. */
1594
1595 static void
1596 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1597 const gdb_byte *writebuf_org,
1598 ULONGEST memaddr, LONGEST len,
1599 struct bp_target_info *target_info,
1600 struct gdbarch *gdbarch)
1601 {
1602 /* Now do full processing of the found relevant range of elements. */
1603 CORE_ADDR bp_addr = 0;
1604 int bp_size = 0;
1605 int bptoffset = 0;
1606
1607 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1608 current_program_space->aspace, 0))
1609 {
1610 /* The breakpoint is inserted in a different address space. */
1611 return;
1612 }
1613
1614 /* Addresses and length of the part of the breakpoint that
1615 we need to copy. */
1616 bp_addr = target_info->placed_address;
1617 bp_size = target_info->shadow_len;
1618
1619 if (bp_addr + bp_size <= memaddr)
1620 {
1621 /* The breakpoint is entirely before the chunk of memory we are
1622 reading. */
1623 return;
1624 }
1625
1626 if (bp_addr >= memaddr + len)
1627 {
1628 /* The breakpoint is entirely after the chunk of memory we are
1629 reading. */
1630 return;
1631 }
1632
1633 /* Offset within shadow_contents. */
1634 if (bp_addr < memaddr)
1635 {
1636 /* Only copy the second part of the breakpoint. */
1637 bp_size -= memaddr - bp_addr;
1638 bptoffset = memaddr - bp_addr;
1639 bp_addr = memaddr;
1640 }
1641
1642 if (bp_addr + bp_size > memaddr + len)
1643 {
1644 /* Only copy the first part of the breakpoint. */
1645 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1646 }
1647
1648 if (readbuf != NULL)
1649 {
1650 /* Verify that the readbuf buffer does not overlap with the
1651 shadow_contents buffer. */
1652 gdb_assert (target_info->shadow_contents >= readbuf + len
1653 || readbuf >= (target_info->shadow_contents
1654 + target_info->shadow_len));
1655
1656 /* Update the read buffer with this inserted breakpoint's
1657 shadow. */
1658 memcpy (readbuf + bp_addr - memaddr,
1659 target_info->shadow_contents + bptoffset, bp_size);
1660 }
1661 else
1662 {
1663 const unsigned char *bp;
1664 CORE_ADDR addr = target_info->reqstd_address;
1665 int placed_size;
1666
1667 /* Update the shadow with what we want to write to memory. */
1668 memcpy (target_info->shadow_contents + bptoffset,
1669 writebuf_org + bp_addr - memaddr, bp_size);
1670
1671 /* Determine appropriate breakpoint contents and size for this
1672 address. */
1673 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1674
1675 /* Update the final write buffer with this inserted
1676 breakpoint's INSN. */
1677 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1678 }
1679 }
1680
1681 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1682 by replacing any memory breakpoints with their shadowed contents.
1683
1684 If READBUF is not NULL, this buffer must not overlap with any of
1685 the breakpoint location's shadow_contents buffers. Otherwise,
1686 a failed assertion internal error will be raised.
1687
1688 The range of shadowed area by each bp_location is:
1689 bl->address - bp_locations_placed_address_before_address_max
1690 up to bl->address + bp_locations_shadow_len_after_address_max
1691 The range we were requested to resolve shadows for is:
1692 memaddr ... memaddr + len
1693 Thus the safe cutoff boundaries for performance optimization are
1694 memaddr + len <= (bl->address
1695 - bp_locations_placed_address_before_address_max)
1696 and:
1697 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1698
1699 void
1700 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1701 const gdb_byte *writebuf_org,
1702 ULONGEST memaddr, LONGEST len)
1703 {
1704 /* Left boundary, right boundary and median element of our binary
1705 search. */
1706 unsigned bc_l, bc_r, bc;
1707
1708 /* Find BC_L which is a leftmost element which may affect BUF
1709 content. It is safe to report lower value but a failure to
1710 report higher one. */
1711
1712 bc_l = 0;
1713 bc_r = bp_locations.size ();
1714 while (bc_l + 1 < bc_r)
1715 {
1716 struct bp_location *bl;
1717
1718 bc = (bc_l + bc_r) / 2;
1719 bl = bp_locations[bc];
1720
1721 /* Check first BL->ADDRESS will not overflow due to the added
1722 constant. Then advance the left boundary only if we are sure
1723 the BC element can in no way affect the BUF content (MEMADDR
1724 to MEMADDR + LEN range).
1725
1726 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1727 offset so that we cannot miss a breakpoint with its shadow
1728 range tail still reaching MEMADDR. */
1729
1730 if ((bl->address + bp_locations_shadow_len_after_address_max
1731 >= bl->address)
1732 && (bl->address + bp_locations_shadow_len_after_address_max
1733 <= memaddr))
1734 bc_l = bc;
1735 else
1736 bc_r = bc;
1737 }
1738
1739 /* Due to the binary search above, we need to make sure we pick the
1740 first location that's at BC_L's address. E.g., if there are
1741 multiple locations at the same address, BC_L may end up pointing
1742 at a duplicate location, and miss the "master"/"inserted"
1743 location. Say, given locations L1, L2 and L3 at addresses A and
1744 B:
1745
1746 L1@A, L2@A, L3@B, ...
1747
1748 BC_L could end up pointing at location L2, while the "master"
1749 location could be L1. Since the `loc->inserted' flag is only set
1750 on "master" locations, we'd forget to restore the shadow of L1
1751 and L2. */
1752 while (bc_l > 0
1753 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1754 bc_l--;
1755
1756 /* Now do full processing of the found relevant range of elements. */
1757
1758 for (bc = bc_l; bc < bp_locations.size (); bc++)
1759 {
1760 struct bp_location *bl = bp_locations[bc];
1761
1762 /* bp_location array has BL->OWNER always non-NULL. */
1763 if (bl->owner->type == bp_none)
1764 warning (_("reading through apparently deleted breakpoint #%d?"),
1765 bl->owner->number);
1766
1767 /* Performance optimization: any further element can no longer affect BUF
1768 content. */
1769
1770 if (bl->address >= bp_locations_placed_address_before_address_max
1771 && (memaddr + len
1772 <= (bl->address
1773 - bp_locations_placed_address_before_address_max)))
1774 break;
1775
1776 if (!bp_location_has_shadow (bl))
1777 continue;
1778
1779 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1780 memaddr, len, &bl->target_info, bl->gdbarch);
1781 }
1782 }
1783
1784 /* See breakpoint.h. */
1785
1786 bool
1787 is_breakpoint (const struct breakpoint *bpt)
1788 {
1789 return (bpt->type == bp_breakpoint
1790 || bpt->type == bp_hardware_breakpoint
1791 || bpt->type == bp_dprintf);
1792 }
1793
1794 /* Return true if BPT is of any hardware watchpoint kind. */
1795
1796 static bool
1797 is_hardware_watchpoint (const struct breakpoint *bpt)
1798 {
1799 return (bpt->type == bp_hardware_watchpoint
1800 || bpt->type == bp_read_watchpoint
1801 || bpt->type == bp_access_watchpoint);
1802 }
1803
1804 /* See breakpoint.h. */
1805
1806 bool
1807 is_watchpoint (const struct breakpoint *bpt)
1808 {
1809 return (is_hardware_watchpoint (bpt)
1810 || bpt->type == bp_watchpoint);
1811 }
1812
1813 /* Returns true if the current thread and its running state are safe
1814 to evaluate or update watchpoint B. Watchpoints on local
1815 expressions need to be evaluated in the context of the thread that
1816 was current when the watchpoint was created, and, that thread needs
1817 to be stopped to be able to select the correct frame context.
1818 Watchpoints on global expressions can be evaluated on any thread,
1819 and in any state. It is presently left to the target allowing
1820 memory accesses when threads are running. */
1821
1822 static bool
1823 watchpoint_in_thread_scope (struct watchpoint *b)
1824 {
1825 return (b->pspace == current_program_space
1826 && (b->watchpoint_thread == null_ptid
1827 || (inferior_ptid == b->watchpoint_thread
1828 && !inferior_thread ()->executing ())));
1829 }
1830
1831 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1832 associated bp_watchpoint_scope breakpoint. */
1833
1834 static void
1835 watchpoint_del_at_next_stop (struct watchpoint *w)
1836 {
1837 if (w->related_breakpoint != w)
1838 {
1839 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1840 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1841 w->related_breakpoint->disposition = disp_del_at_next_stop;
1842 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1843 w->related_breakpoint = w;
1844 }
1845 w->disposition = disp_del_at_next_stop;
1846 }
1847
1848 /* Extract a bitfield value from value VAL using the bit parameters contained in
1849 watchpoint W. */
1850
1851 static struct value *
1852 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1853 {
1854 struct value *bit_val;
1855
1856 if (val == NULL)
1857 return NULL;
1858
1859 bit_val = allocate_value (value_type (val));
1860
1861 unpack_value_bitfield (bit_val,
1862 w->val_bitpos,
1863 w->val_bitsize,
1864 value_contents_for_printing (val).data (),
1865 value_offset (val),
1866 val);
1867
1868 return bit_val;
1869 }
1870
1871 /* Allocate a dummy location and add it to B. This is required
1872 because bpstat_stop_status requires a location to be able to report
1873 stops. */
1874
1875 static void
1876 add_dummy_location (struct breakpoint *b,
1877 struct program_space *pspace)
1878 {
1879 gdb_assert (b->loc == NULL);
1880
1881 b->loc = new bp_location (b, bp_loc_other);
1882 b->loc->pspace = pspace;
1883 }
1884
1885 /* Assuming that B is a watchpoint:
1886 - Reparse watchpoint expression, if REPARSE is true
1887 - Evaluate expression and store the result in B->val
1888 - Evaluate the condition if there is one, and store the result
1889 in b->loc->cond.
1890 - Update the list of values that must be watched in B->loc.
1891
1892 If the watchpoint disposition is disp_del_at_next_stop, then do
1893 nothing. If this is local watchpoint that is out of scope, delete
1894 it.
1895
1896 Even with `set breakpoint always-inserted on' the watchpoints are
1897 removed + inserted on each stop here. Normal breakpoints must
1898 never be removed because they might be missed by a running thread
1899 when debugging in non-stop mode. On the other hand, hardware
1900 watchpoints (is_hardware_watchpoint; processed here) are specific
1901 to each LWP since they are stored in each LWP's hardware debug
1902 registers. Therefore, such LWP must be stopped first in order to
1903 be able to modify its hardware watchpoints.
1904
1905 Hardware watchpoints must be reset exactly once after being
1906 presented to the user. It cannot be done sooner, because it would
1907 reset the data used to present the watchpoint hit to the user. And
1908 it must not be done later because it could display the same single
1909 watchpoint hit during multiple GDB stops. Note that the latter is
1910 relevant only to the hardware watchpoint types bp_read_watchpoint
1911 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1912 not user-visible - its hit is suppressed if the memory content has
1913 not changed.
1914
1915 The following constraints influence the location where we can reset
1916 hardware watchpoints:
1917
1918 * target_stopped_by_watchpoint and target_stopped_data_address are
1919 called several times when GDB stops.
1920
1921 [linux]
1922 * Multiple hardware watchpoints can be hit at the same time,
1923 causing GDB to stop. GDB only presents one hardware watchpoint
1924 hit at a time as the reason for stopping, and all the other hits
1925 are presented later, one after the other, each time the user
1926 requests the execution to be resumed. Execution is not resumed
1927 for the threads still having pending hit event stored in
1928 LWP_INFO->STATUS. While the watchpoint is already removed from
1929 the inferior on the first stop the thread hit event is kept being
1930 reported from its cached value by linux_nat_stopped_data_address
1931 until the real thread resume happens after the watchpoint gets
1932 presented and thus its LWP_INFO->STATUS gets reset.
1933
1934 Therefore the hardware watchpoint hit can get safely reset on the
1935 watchpoint removal from inferior. */
1936
1937 static void
1938 update_watchpoint (struct watchpoint *b, bool reparse)
1939 {
1940 bool within_current_scope;
1941
1942 /* If this is a local watchpoint, we only want to check if the
1943 watchpoint frame is in scope if the current thread is the thread
1944 that was used to create the watchpoint. */
1945 if (!watchpoint_in_thread_scope (b))
1946 return;
1947
1948 if (b->disposition == disp_del_at_next_stop)
1949 return;
1950
1951 gdb::optional<scoped_restore_selected_frame> restore_frame;
1952
1953 /* Determine if the watchpoint is within scope. */
1954 if (b->exp_valid_block == NULL)
1955 within_current_scope = true;
1956 else
1957 {
1958 frame_info_ptr fi = get_current_frame ();
1959 struct gdbarch *frame_arch = get_frame_arch (fi);
1960 CORE_ADDR frame_pc = get_frame_pc (fi);
1961
1962 /* If we're at a point where the stack has been destroyed
1963 (e.g. in a function epilogue), unwinding may not work
1964 properly. Do not attempt to recreate locations at this
1965 point. See similar comments in watchpoint_check. */
1966 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1967 return;
1968
1969 /* Save the current frame's ID so we can restore it after
1970 evaluating the watchpoint expression on its own frame. */
1971 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1972 took a frame parameter, so that we didn't have to change the
1973 selected frame. */
1974 restore_frame.emplace ();
1975
1976 fi = frame_find_by_id (b->watchpoint_frame);
1977 within_current_scope = (fi != NULL);
1978 if (within_current_scope)
1979 select_frame (fi);
1980 }
1981
1982 /* We don't free locations. They are stored in the bp_location array
1983 and update_global_location_list will eventually delete them and
1984 remove breakpoints if needed. */
1985 b->loc = NULL;
1986
1987 if (within_current_scope && reparse)
1988 {
1989 const char *s;
1990
1991 b->exp.reset ();
1992 s = (b->exp_string_reparse
1993 ? b->exp_string_reparse.get ()
1994 : b->exp_string.get ());
1995 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1996 /* If the meaning of expression itself changed, the old value is
1997 no longer relevant. We don't want to report a watchpoint hit
1998 to the user when the old value and the new value may actually
1999 be completely different objects. */
2000 b->val = NULL;
2001 b->val_valid = false;
2002
2003 /* Note that unlike with breakpoints, the watchpoint's condition
2004 expression is stored in the breakpoint object, not in the
2005 locations (re)created below. */
2006 if (b->cond_string != NULL)
2007 {
2008 b->cond_exp.reset ();
2009
2010 s = b->cond_string.get ();
2011 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
2012 }
2013 }
2014
2015 /* If we failed to parse the expression, for example because
2016 it refers to a global variable in a not-yet-loaded shared library,
2017 don't try to insert watchpoint. We don't automatically delete
2018 such watchpoint, though, since failure to parse expression
2019 is different from out-of-scope watchpoint. */
2020 if (!target_has_execution ())
2021 {
2022 /* Without execution, memory can't change. No use to try and
2023 set watchpoint locations. The watchpoint will be reset when
2024 the target gains execution, through breakpoint_re_set. */
2025 if (!can_use_hw_watchpoints)
2026 {
2027 if (b->works_in_software_mode ())
2028 b->type = bp_watchpoint;
2029 else
2030 error (_("Can't set read/access watchpoint when "
2031 "hardware watchpoints are disabled."));
2032 }
2033 }
2034 else if (within_current_scope && b->exp)
2035 {
2036 std::vector<value_ref_ptr> val_chain;
2037 struct value *v, *result;
2038 struct program_space *frame_pspace;
2039
2040 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2041 &val_chain, false);
2042
2043 /* Avoid setting b->val if it's already set. The meaning of
2044 b->val is 'the last value' user saw, and we should update
2045 it only if we reported that last value to user. As it
2046 happens, the code that reports it updates b->val directly.
2047 We don't keep track of the memory value for masked
2048 watchpoints. */
2049 if (!b->val_valid && !is_masked_watchpoint (b))
2050 {
2051 if (b->val_bitsize != 0)
2052 v = extract_bitfield_from_watchpoint_value (b, v);
2053 b->val = release_value (v);
2054 b->val_valid = true;
2055 }
2056
2057 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2058
2059 /* Look at each value on the value chain. */
2060 gdb_assert (!val_chain.empty ());
2061 for (const value_ref_ptr &iter : val_chain)
2062 {
2063 v = iter.get ();
2064
2065 /* If it's a memory location, and GDB actually needed
2066 its contents to evaluate the expression, then we
2067 must watch it. If the first value returned is
2068 still lazy, that means an error occurred reading it;
2069 watch it anyway in case it becomes readable. */
2070 if (VALUE_LVAL (v) == lval_memory
2071 && (v == val_chain[0] || ! value_lazy (v)))
2072 {
2073 struct type *vtype = check_typedef (value_type (v));
2074
2075 /* We only watch structs and arrays if user asked
2076 for it explicitly, never if they just happen to
2077 appear in the middle of some value chain. */
2078 if (v == result
2079 || (vtype->code () != TYPE_CODE_STRUCT
2080 && vtype->code () != TYPE_CODE_ARRAY))
2081 {
2082 CORE_ADDR addr;
2083 enum target_hw_bp_type type;
2084 struct bp_location *loc, **tmp;
2085 int bitpos = 0, bitsize = 0;
2086
2087 if (value_bitsize (v) != 0)
2088 {
2089 /* Extract the bit parameters out from the bitfield
2090 sub-expression. */
2091 bitpos = value_bitpos (v);
2092 bitsize = value_bitsize (v);
2093 }
2094 else if (v == result && b->val_bitsize != 0)
2095 {
2096 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2097 lvalue whose bit parameters are saved in the fields
2098 VAL_BITPOS and VAL_BITSIZE. */
2099 bitpos = b->val_bitpos;
2100 bitsize = b->val_bitsize;
2101 }
2102
2103 addr = value_address (v);
2104 if (bitsize != 0)
2105 {
2106 /* Skip the bytes that don't contain the bitfield. */
2107 addr += bitpos / 8;
2108 }
2109
2110 type = hw_write;
2111 if (b->type == bp_read_watchpoint)
2112 type = hw_read;
2113 else if (b->type == bp_access_watchpoint)
2114 type = hw_access;
2115
2116 loc = b->allocate_location ();
2117 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2118 ;
2119 *tmp = loc;
2120 loc->gdbarch = value_type (v)->arch ();
2121
2122 loc->pspace = frame_pspace;
2123 loc->address = address_significant (loc->gdbarch, addr);
2124
2125 if (bitsize != 0)
2126 {
2127 /* Just cover the bytes that make up the bitfield. */
2128 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2129 }
2130 else
2131 loc->length = value_type (v)->length ();
2132
2133 loc->watchpoint_type = type;
2134 }
2135 }
2136 }
2137
2138 /* Change the type of breakpoint between hardware assisted or
2139 an ordinary watchpoint depending on the hardware support and
2140 free hardware slots. Recheck the number of free hardware slots
2141 as the value chain may have changed. */
2142 {
2143 int reg_cnt;
2144 enum bp_loc_type loc_type;
2145
2146 reg_cnt = can_use_hardware_watchpoint (val_chain);
2147
2148 if (reg_cnt)
2149 {
2150 int i, target_resources_ok, other_type_used;
2151 enum bptype type;
2152
2153 /* Use an exact watchpoint when there's only one memory region to be
2154 watched, and only one debug register is needed to watch it. */
2155 b->exact = target_exact_watchpoints && reg_cnt == 1;
2156
2157 /* We need to determine how many resources are already
2158 used for all other hardware watchpoints plus this one
2159 to see if we still have enough resources to also fit
2160 this watchpoint in as well. */
2161
2162 /* If this is a software watchpoint, we try to turn it
2163 to a hardware one -- count resources as if B was of
2164 hardware watchpoint type. */
2165 type = b->type;
2166 if (type == bp_watchpoint)
2167 type = bp_hardware_watchpoint;
2168
2169 /* This watchpoint may or may not have been placed on
2170 the list yet at this point (it won't be in the list
2171 if we're trying to create it for the first time,
2172 through watch_command), so always account for it
2173 manually. */
2174
2175 /* Count resources used by all watchpoints except B. */
2176 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2177
2178 /* Add in the resources needed for B. */
2179 i += hw_watchpoint_use_count (b);
2180
2181 target_resources_ok
2182 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2183 if (target_resources_ok <= 0)
2184 {
2185 bool sw_mode = b->works_in_software_mode ();
2186
2187 if (target_resources_ok == 0 && !sw_mode)
2188 error (_("Target does not support this type of "
2189 "hardware watchpoint."));
2190 else if (target_resources_ok < 0 && !sw_mode)
2191 error (_("There are not enough available hardware "
2192 "resources for this watchpoint."));
2193
2194 /* Downgrade to software watchpoint. */
2195 b->type = bp_watchpoint;
2196 }
2197 else
2198 {
2199 /* If this was a software watchpoint, we've just
2200 found we have enough resources to turn it to a
2201 hardware watchpoint. Otherwise, this is a
2202 nop. */
2203 b->type = type;
2204 }
2205 }
2206 else if (!b->works_in_software_mode ())
2207 {
2208 if (!can_use_hw_watchpoints)
2209 error (_("Can't set read/access watchpoint when "
2210 "hardware watchpoints are disabled."));
2211 else
2212 error (_("Expression cannot be implemented with "
2213 "read/access watchpoint."));
2214 }
2215 else
2216 b->type = bp_watchpoint;
2217
2218 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
2219 : bp_loc_hardware_watchpoint);
2220 for (bp_location *bl : b->locations ())
2221 bl->loc_type = loc_type;
2222 }
2223
2224 /* If a software watchpoint is not watching any memory, then the
2225 above left it without any location set up. But,
2226 bpstat_stop_status requires a location to be able to report
2227 stops, so make sure there's at least a dummy one. */
2228 if (b->type == bp_watchpoint && b->loc == NULL)
2229 add_dummy_location (b, frame_pspace);
2230 }
2231 else if (!within_current_scope)
2232 {
2233 gdb_printf (_("\
2234 Watchpoint %d deleted because the program has left the block\n\
2235 in which its expression is valid.\n"),
2236 b->number);
2237 watchpoint_del_at_next_stop (b);
2238 }
2239 }
2240
2241 /* Returns true iff breakpoint location should be
2242 inserted in the inferior. We don't differentiate the type of BL's owner
2243 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2244 breakpoint_ops is not defined, because in insert_bp_location,
2245 tracepoint's insert_location will not be called. */
2246
2247 static bool
2248 should_be_inserted (struct bp_location *bl)
2249 {
2250 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2251 return false;
2252
2253 if (bl->owner->disposition == disp_del_at_next_stop)
2254 return false;
2255
2256 if (!bl->enabled || bl->disabled_by_cond
2257 || bl->shlib_disabled || bl->duplicate)
2258 return false;
2259
2260 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2261 return false;
2262
2263 /* This is set for example, when we're attached to the parent of a
2264 vfork, and have detached from the child. The child is running
2265 free, and we expect it to do an exec or exit, at which point the
2266 OS makes the parent schedulable again (and the target reports
2267 that the vfork is done). Until the child is done with the shared
2268 memory region, do not insert breakpoints in the parent, otherwise
2269 the child could still trip on the parent's breakpoints. Since
2270 the parent is blocked anyway, it won't miss any breakpoint. */
2271 if (bl->pspace->breakpoints_not_allowed)
2272 return false;
2273
2274 /* Don't insert a breakpoint if we're trying to step past its
2275 location, except if the breakpoint is a single-step breakpoint,
2276 and the breakpoint's thread is the thread which is stepping past
2277 a breakpoint. */
2278 if ((bl->loc_type == bp_loc_software_breakpoint
2279 || bl->loc_type == bp_loc_hardware_breakpoint)
2280 && stepping_past_instruction_at (bl->pspace->aspace,
2281 bl->address)
2282 /* The single-step breakpoint may be inserted at the location
2283 we're trying to step if the instruction branches to itself.
2284 However, the instruction won't be executed at all and it may
2285 break the semantics of the instruction, for example, the
2286 instruction is a conditional branch or updates some flags.
2287 We can't fix it unless GDB is able to emulate the instruction
2288 or switch to displaced stepping. */
2289 && !(bl->owner->type == bp_single_step
2290 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2291 {
2292 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2293 paddress (bl->gdbarch, bl->address));
2294 return false;
2295 }
2296
2297 /* Don't insert watchpoints if we're trying to step past the
2298 instruction that triggered one. */
2299 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2300 && stepping_past_nonsteppable_watchpoint ())
2301 {
2302 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2303 "skipping watchpoint at %s:%d",
2304 paddress (bl->gdbarch, bl->address), bl->length);
2305 return false;
2306 }
2307
2308 return true;
2309 }
2310
2311 /* Same as should_be_inserted but does the check assuming
2312 that the location is not duplicated. */
2313
2314 static bool
2315 unduplicated_should_be_inserted (struct bp_location *bl)
2316 {
2317 scoped_restore restore_bl_duplicate
2318 = make_scoped_restore (&bl->duplicate, 0);
2319
2320 return should_be_inserted (bl);
2321 }
2322
2323 /* Parses a conditional described by an expression COND into an
2324 agent expression bytecode suitable for evaluation
2325 by the bytecode interpreter. Return NULL if there was
2326 any error during parsing. */
2327
2328 static agent_expr_up
2329 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2330 {
2331 if (cond == NULL)
2332 return NULL;
2333
2334 agent_expr_up aexpr;
2335
2336 /* We don't want to stop processing, so catch any errors
2337 that may show up. */
2338 try
2339 {
2340 aexpr = gen_eval_for_expr (scope, cond);
2341 }
2342
2343 catch (const gdb_exception_error &ex)
2344 {
2345 /* If we got here, it means the condition could not be parsed to a valid
2346 bytecode expression and thus can't be evaluated on the target's side.
2347 It's no use iterating through the conditions. */
2348 }
2349
2350 /* We have a valid agent expression. */
2351 return aexpr;
2352 }
2353
2354 /* Based on location BL, create a list of breakpoint conditions to be
2355 passed on to the target. If we have duplicated locations with different
2356 conditions, we will add such conditions to the list. The idea is that the
2357 target will evaluate the list of conditions and will only notify GDB when
2358 one of them is true. */
2359
2360 static void
2361 build_target_condition_list (struct bp_location *bl)
2362 {
2363 bool null_condition_or_parse_error = false;
2364 int modified = bl->needs_update;
2365
2366 /* Release conditions left over from a previous insert. */
2367 bl->target_info.conditions.clear ();
2368
2369 /* This is only meaningful if the target is
2370 evaluating conditions and if the user has
2371 opted for condition evaluation on the target's
2372 side. */
2373 if (gdb_evaluates_breakpoint_condition_p ()
2374 || !target_supports_evaluation_of_breakpoint_conditions ())
2375 return;
2376
2377 auto loc_range = all_bp_locations_at_addr (bl->address);
2378
2379 /* Do a first pass to check for locations with no assigned
2380 conditions or conditions that fail to parse to a valid agent
2381 expression bytecode. If any of these happen, then it's no use to
2382 send conditions to the target since this location will always
2383 trigger and generate a response back to GDB. Note we consider
2384 all locations at the same address irrespective of type, i.e.,
2385 even if the locations aren't considered duplicates (e.g.,
2386 software breakpoint and hardware breakpoint at the same
2387 address). */
2388 for (bp_location *loc : loc_range)
2389 {
2390 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2391 {
2392 if (modified)
2393 {
2394 /* Re-parse the conditions since something changed. In that
2395 case we already freed the condition bytecodes (see
2396 force_breakpoint_reinsertion). We just
2397 need to parse the condition to bytecodes again. */
2398 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2399 loc->cond.get ());
2400 }
2401
2402 /* If we have a NULL bytecode expression, it means something
2403 went wrong or we have a null condition expression. */
2404 if (!loc->cond_bytecode)
2405 {
2406 null_condition_or_parse_error = true;
2407 break;
2408 }
2409 }
2410 }
2411
2412 /* If any of these happened, it means we will have to evaluate the conditions
2413 for the location's address on gdb's side. It is no use keeping bytecodes
2414 for all the other duplicate locations, thus we free all of them here.
2415
2416 This is so we have a finer control over which locations' conditions are
2417 being evaluated by GDB or the remote stub. */
2418 if (null_condition_or_parse_error)
2419 {
2420 for (bp_location *loc : loc_range)
2421 {
2422 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2423 {
2424 /* Only go as far as the first NULL bytecode is
2425 located. */
2426 if (!loc->cond_bytecode)
2427 return;
2428
2429 loc->cond_bytecode.reset ();
2430 }
2431 }
2432 }
2433
2434 /* No NULL conditions or failed bytecode generation. Build a
2435 condition list for this location's address. If we have software
2436 and hardware locations at the same address, they aren't
2437 considered duplicates, but we still marge all the conditions
2438 anyway, as it's simpler, and doesn't really make a practical
2439 difference. */
2440 for (bp_location *loc : loc_range)
2441 if (loc->cond
2442 && is_breakpoint (loc->owner)
2443 && loc->pspace->num == bl->pspace->num
2444 && loc->owner->enable_state == bp_enabled
2445 && loc->enabled
2446 && !loc->disabled_by_cond)
2447 {
2448 /* Add the condition to the vector. This will be used later
2449 to send the conditions to the target. */
2450 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2451 }
2452
2453 return;
2454 }
2455
2456 /* Parses a command described by string CMD into an agent expression
2457 bytecode suitable for evaluation by the bytecode interpreter.
2458 Return NULL if there was any error during parsing. */
2459
2460 static agent_expr_up
2461 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2462 {
2463 const char *cmdrest;
2464 const char *format_start, *format_end;
2465 struct gdbarch *gdbarch = get_current_arch ();
2466
2467 if (cmd == NULL)
2468 return NULL;
2469
2470 cmdrest = cmd;
2471
2472 if (*cmdrest == ',')
2473 ++cmdrest;
2474 cmdrest = skip_spaces (cmdrest);
2475
2476 if (*cmdrest++ != '"')
2477 error (_("No format string following the location"));
2478
2479 format_start = cmdrest;
2480
2481 format_pieces fpieces (&cmdrest);
2482
2483 format_end = cmdrest;
2484
2485 if (*cmdrest++ != '"')
2486 error (_("Bad format string, non-terminated '\"'."));
2487
2488 cmdrest = skip_spaces (cmdrest);
2489
2490 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2491 error (_("Invalid argument syntax"));
2492
2493 if (*cmdrest == ',')
2494 cmdrest++;
2495 cmdrest = skip_spaces (cmdrest);
2496
2497 /* For each argument, make an expression. */
2498
2499 std::vector<struct expression *> argvec;
2500 while (*cmdrest != '\0')
2501 {
2502 const char *cmd1;
2503
2504 cmd1 = cmdrest;
2505 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2506 argvec.push_back (expr.release ());
2507 cmdrest = cmd1;
2508 if (*cmdrest == ',')
2509 ++cmdrest;
2510 }
2511
2512 agent_expr_up aexpr;
2513
2514 /* We don't want to stop processing, so catch any errors
2515 that may show up. */
2516 try
2517 {
2518 aexpr = gen_printf (scope, gdbarch, 0, 0,
2519 format_start, format_end - format_start,
2520 argvec.size (), argvec.data ());
2521 }
2522 catch (const gdb_exception_error &ex)
2523 {
2524 /* If we got here, it means the command could not be parsed to a valid
2525 bytecode expression and thus can't be evaluated on the target's side.
2526 It's no use iterating through the other commands. */
2527 }
2528
2529 /* We have a valid agent expression, return it. */
2530 return aexpr;
2531 }
2532
2533 /* Based on location BL, create a list of breakpoint commands to be
2534 passed on to the target. If we have duplicated locations with
2535 different commands, we will add any such to the list. */
2536
2537 static void
2538 build_target_command_list (struct bp_location *bl)
2539 {
2540 bool null_command_or_parse_error = false;
2541 int modified = bl->needs_update;
2542
2543 /* Clear commands left over from a previous insert. */
2544 bl->target_info.tcommands.clear ();
2545
2546 if (!target_can_run_breakpoint_commands ())
2547 return;
2548
2549 /* For now, limit to agent-style dprintf breakpoints. */
2550 if (dprintf_style != dprintf_style_agent)
2551 return;
2552
2553 auto loc_range = all_bp_locations_at_addr (bl->address);
2554
2555 /* For now, if we have any location at the same address that isn't a
2556 dprintf, don't install the target-side commands, as that would
2557 make the breakpoint not be reported to the core, and we'd lose
2558 control. */
2559 for (bp_location *loc : loc_range)
2560 if (is_breakpoint (loc->owner)
2561 && loc->pspace->num == bl->pspace->num
2562 && loc->owner->type != bp_dprintf)
2563 return;
2564
2565 /* Do a first pass to check for locations with no assigned
2566 conditions or conditions that fail to parse to a valid agent expression
2567 bytecode. If any of these happen, then it's no use to send conditions
2568 to the target since this location will always trigger and generate a
2569 response back to GDB. */
2570 for (bp_location *loc : loc_range)
2571 {
2572 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2573 {
2574 if (modified)
2575 {
2576 /* Re-parse the commands since something changed. In that
2577 case we already freed the command bytecodes (see
2578 force_breakpoint_reinsertion). We just
2579 need to parse the command to bytecodes again. */
2580 loc->cmd_bytecode
2581 = parse_cmd_to_aexpr (bl->address,
2582 loc->owner->extra_string.get ());
2583 }
2584
2585 /* If we have a NULL bytecode expression, it means something
2586 went wrong or we have a null command expression. */
2587 if (!loc->cmd_bytecode)
2588 {
2589 null_command_or_parse_error = true;
2590 break;
2591 }
2592 }
2593 }
2594
2595 /* If anything failed, then we're not doing target-side commands,
2596 and so clean up. */
2597 if (null_command_or_parse_error)
2598 {
2599 for (bp_location *loc : loc_range)
2600 if (is_breakpoint (loc->owner)
2601 && loc->pspace->num == bl->pspace->num)
2602 {
2603 /* Only go as far as the first NULL bytecode is
2604 located. */
2605 if (loc->cmd_bytecode == NULL)
2606 return;
2607
2608 loc->cmd_bytecode.reset ();
2609 }
2610 }
2611
2612 /* No NULL commands or failed bytecode generation. Build a command
2613 list for all duplicate locations at this location's address.
2614 Note that here we must care for whether the breakpoint location
2615 types are considered duplicates, otherwise, say, if we have a
2616 software and hardware location at the same address, the target
2617 could end up running the commands twice. For the moment, we only
2618 support targets-side commands with dprintf, but it doesn't hurt
2619 to be pedantically correct in case that changes. */
2620 for (bp_location *loc : loc_range)
2621 if (breakpoint_locations_match (bl, loc)
2622 && loc->owner->extra_string
2623 && is_breakpoint (loc->owner)
2624 && loc->pspace->num == bl->pspace->num
2625 && loc->owner->enable_state == bp_enabled
2626 && loc->enabled
2627 && !loc->disabled_by_cond)
2628 {
2629 /* Add the command to the vector. This will be used later
2630 to send the commands to the target. */
2631 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2632 }
2633
2634 bl->target_info.persist = 0;
2635 /* Maybe flag this location as persistent. */
2636 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2637 bl->target_info.persist = 1;
2638 }
2639
2640 /* Return the kind of breakpoint on address *ADDR. Get the kind
2641 of breakpoint according to ADDR except single-step breakpoint.
2642 Get the kind of single-step breakpoint according to the current
2643 registers state. */
2644
2645 static int
2646 breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
2647 {
2648 if (bl->owner->type == bp_single_step)
2649 {
2650 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2651 struct regcache *regcache;
2652
2653 regcache = get_thread_regcache (thr);
2654
2655 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2656 regcache, addr);
2657 }
2658 else
2659 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2660 }
2661
2662 /* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2663 E is either the currently handled exception, or a copy, or a sliced copy,
2664 so we can't rethrow that one, but we can use it to inspect the properties
2665 of the currently handled exception. */
2666
2667 static void
2668 rethrow_on_target_close_error (const gdb_exception &e)
2669 {
2670 if (e.reason == 0)
2671 return;
2672 /* Can't set the breakpoint. */
2673
2674 if (e.error != TARGET_CLOSE_ERROR)
2675 return;
2676
2677 /* If the target has closed then it will have deleted any breakpoints
2678 inserted within the target inferior, as a result any further attempts
2679 to interact with the breakpoint objects is not possible. Just rethrow
2680 the error. Don't use e to rethrow, to prevent object slicing of the
2681 exception. */
2682 throw;
2683 }
2684
2685 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2686 location. Any error messages are printed to TMP_ERROR_STREAM; and
2687 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2688 Returns 0 for success, 1 if the bp_location type is not supported or
2689 -1 for failure.
2690
2691 NOTE drow/2003-09-09: This routine could be broken down to an
2692 object-style method for each breakpoint or catchpoint type. */
2693 static int
2694 insert_bp_location (struct bp_location *bl,
2695 struct ui_file *tmp_error_stream,
2696 int *disabled_breaks,
2697 int *hw_breakpoint_error,
2698 int *hw_bp_error_explained_already)
2699 {
2700 gdb_exception bp_excpt;
2701
2702 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2703 return 0;
2704
2705 /* Note we don't initialize bl->target_info, as that wipes out
2706 the breakpoint location's shadow_contents if the breakpoint
2707 is still inserted at that location. This in turn breaks
2708 target_read_memory which depends on these buffers when
2709 a memory read is requested at the breakpoint location:
2710 Once the target_info has been wiped, we fail to see that
2711 we have a breakpoint inserted at that address and thus
2712 read the breakpoint instead of returning the data saved in
2713 the breakpoint location's shadow contents. */
2714 bl->target_info.reqstd_address = bl->address;
2715 bl->target_info.placed_address_space = bl->pspace->aspace;
2716 bl->target_info.length = bl->length;
2717
2718 /* When working with target-side conditions, we must pass all the conditions
2719 for the same breakpoint address down to the target since GDB will not
2720 insert those locations. With a list of breakpoint conditions, the target
2721 can decide when to stop and notify GDB. */
2722
2723 if (is_breakpoint (bl->owner))
2724 {
2725 build_target_condition_list (bl);
2726 build_target_command_list (bl);
2727 /* Reset the modification marker. */
2728 bl->needs_update = 0;
2729 }
2730
2731 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2732 set at a read-only address, then a breakpoint location will have
2733 been changed to hardware breakpoint before we get here. If it is
2734 "off" however, error out before actually trying to insert the
2735 breakpoint, with a nicer error message. */
2736 if (bl->loc_type == bp_loc_software_breakpoint
2737 && !automatic_hardware_breakpoints)
2738 {
2739 mem_region *mr = lookup_mem_region (bl->address);
2740
2741 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2742 {
2743 gdb_printf (tmp_error_stream,
2744 _("Cannot insert breakpoint %d.\n"
2745 "Cannot set software breakpoint "
2746 "at read-only address %s\n"),
2747 bl->owner->number,
2748 paddress (bl->gdbarch, bl->address));
2749 return 1;
2750 }
2751 }
2752
2753 if (bl->loc_type == bp_loc_software_breakpoint
2754 || bl->loc_type == bp_loc_hardware_breakpoint)
2755 {
2756 /* First check to see if we have to handle an overlay. */
2757 if (overlay_debugging == ovly_off
2758 || bl->section == NULL
2759 || !(section_is_overlay (bl->section)))
2760 {
2761 /* No overlay handling: just set the breakpoint. */
2762 try
2763 {
2764 int val;
2765
2766 val = bl->owner->insert_location (bl);
2767 if (val)
2768 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2769 }
2770 catch (gdb_exception &e)
2771 {
2772 rethrow_on_target_close_error (e);
2773 bp_excpt = std::move (e);
2774 }
2775 }
2776 else
2777 {
2778 /* This breakpoint is in an overlay section.
2779 Shall we set a breakpoint at the LMA? */
2780 if (!overlay_events_enabled)
2781 {
2782 /* Yes -- overlay event support is not active,
2783 so we must try to set a breakpoint at the LMA.
2784 This will not work for a hardware breakpoint. */
2785 if (bl->loc_type == bp_loc_hardware_breakpoint)
2786 warning (_("hardware breakpoint %d not supported in overlay!"),
2787 bl->owner->number);
2788 else
2789 {
2790 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2791 bl->section);
2792 /* Set a software (trap) breakpoint at the LMA. */
2793 bl->overlay_target_info = bl->target_info;
2794 bl->overlay_target_info.reqstd_address = addr;
2795
2796 /* No overlay handling: just set the breakpoint. */
2797 try
2798 {
2799 int val;
2800
2801 bl->overlay_target_info.kind
2802 = breakpoint_kind (bl, &addr);
2803 bl->overlay_target_info.placed_address = addr;
2804 val = target_insert_breakpoint (bl->gdbarch,
2805 &bl->overlay_target_info);
2806 if (val)
2807 bp_excpt
2808 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2809 }
2810 catch (gdb_exception &e)
2811 {
2812 rethrow_on_target_close_error (e);
2813 bp_excpt = std::move (e);
2814 }
2815
2816 if (bp_excpt.reason != 0)
2817 gdb_printf (tmp_error_stream,
2818 "Overlay breakpoint %d "
2819 "failed: in ROM?\n",
2820 bl->owner->number);
2821 }
2822 }
2823 /* Shall we set a breakpoint at the VMA? */
2824 if (section_is_mapped (bl->section))
2825 {
2826 /* Yes. This overlay section is mapped into memory. */
2827 try
2828 {
2829 int val;
2830
2831 val = bl->owner->insert_location (bl);
2832 if (val)
2833 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2834 }
2835 catch (gdb_exception &e)
2836 {
2837 rethrow_on_target_close_error (e);
2838 bp_excpt = std::move (e);
2839 }
2840 }
2841 else
2842 {
2843 /* No. This breakpoint will not be inserted.
2844 No error, but do not mark the bp as 'inserted'. */
2845 return 0;
2846 }
2847 }
2848
2849 if (bp_excpt.reason != 0)
2850 {
2851 /* Can't set the breakpoint. */
2852 gdb_assert (bl->owner != nullptr);
2853
2854 /* In some cases, we might not be able to insert a
2855 breakpoint in a shared library that has already been
2856 removed, but we have not yet processed the shlib unload
2857 event. Unfortunately, some targets that implement
2858 breakpoint insertion themselves can't tell why the
2859 breakpoint insertion failed (e.g., the remote target
2860 doesn't define error codes), so we must treat generic
2861 errors as memory errors. */
2862 if (bp_excpt.reason == RETURN_ERROR
2863 && (bp_excpt.error == GENERIC_ERROR
2864 || bp_excpt.error == MEMORY_ERROR)
2865 && bl->loc_type == bp_loc_software_breakpoint
2866 && (solib_name_from_address (bl->pspace, bl->address)
2867 || shared_objfile_contains_address_p (bl->pspace,
2868 bl->address)))
2869 {
2870 /* See also: disable_breakpoints_in_shlibs. */
2871 bl->shlib_disabled = 1;
2872 gdb::observers::breakpoint_modified.notify (bl->owner);
2873 if (!*disabled_breaks)
2874 {
2875 gdb_printf (tmp_error_stream,
2876 "Cannot insert breakpoint %d.\n",
2877 bl->owner->number);
2878 gdb_printf (tmp_error_stream,
2879 "Temporarily disabling shared "
2880 "library breakpoints:\n");
2881 }
2882 *disabled_breaks = 1;
2883 gdb_printf (tmp_error_stream,
2884 "breakpoint #%d\n", bl->owner->number);
2885 return 0;
2886 }
2887 else
2888 {
2889 if (bl->loc_type == bp_loc_hardware_breakpoint)
2890 {
2891 *hw_breakpoint_error = 1;
2892 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2893 gdb_printf (tmp_error_stream,
2894 "Cannot insert hardware breakpoint %d%s",
2895 bl->owner->number,
2896 bp_excpt.message ? ":" : ".\n");
2897 if (bp_excpt.message != NULL)
2898 gdb_printf (tmp_error_stream, "%s.\n",
2899 bp_excpt.what ());
2900 }
2901 else
2902 {
2903 if (bp_excpt.message == NULL)
2904 {
2905 std::string message
2906 = memory_error_message (TARGET_XFER_E_IO,
2907 bl->gdbarch, bl->address);
2908
2909 gdb_printf (tmp_error_stream,
2910 "Cannot insert breakpoint %d.\n"
2911 "%s\n",
2912 bl->owner->number, message.c_str ());
2913 }
2914 else
2915 {
2916 gdb_printf (tmp_error_stream,
2917 "Cannot insert breakpoint %d: %s\n",
2918 bl->owner->number,
2919 bp_excpt.what ());
2920 }
2921 }
2922 return 1;
2923
2924 }
2925 }
2926 else
2927 bl->inserted = 1;
2928
2929 return 0;
2930 }
2931
2932 else if (bl->loc_type == bp_loc_hardware_watchpoint
2933 /* NOTE drow/2003-09-08: This state only exists for removing
2934 watchpoints. It's not clear that it's necessary... */
2935 && bl->owner->disposition != disp_del_at_next_stop)
2936 {
2937 int val;
2938
2939 val = bl->owner->insert_location (bl);
2940
2941 /* If trying to set a read-watchpoint, and it turns out it's not
2942 supported, try emulating one with an access watchpoint. */
2943 if (val == 1 && bl->watchpoint_type == hw_read)
2944 {
2945 /* But don't try to insert it, if there's already another
2946 hw_access location that would be considered a duplicate
2947 of this one. */
2948 for (bp_location *loc : all_bp_locations ())
2949 if (loc != bl
2950 && loc->watchpoint_type == hw_access
2951 && watchpoint_locations_match (bl, loc))
2952 {
2953 bl->duplicate = 1;
2954 bl->inserted = 1;
2955 bl->target_info = loc->target_info;
2956 bl->watchpoint_type = hw_access;
2957 val = 0;
2958 break;
2959 }
2960
2961 if (val == 1)
2962 {
2963 bl->watchpoint_type = hw_access;
2964 val = bl->owner->insert_location (bl);
2965
2966 if (val)
2967 /* Back to the original value. */
2968 bl->watchpoint_type = hw_read;
2969 }
2970 }
2971
2972 bl->inserted = (val == 0);
2973 }
2974
2975 else if (bl->owner->type == bp_catchpoint)
2976 {
2977 int val;
2978
2979 val = bl->owner->insert_location (bl);
2980 if (val)
2981 {
2982 bl->owner->enable_state = bp_disabled;
2983
2984 if (val == 1)
2985 warning (_("\
2986 Error inserting catchpoint %d: Your system does not support this type\n\
2987 of catchpoint."), bl->owner->number);
2988 else
2989 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2990 }
2991
2992 bl->inserted = (val == 0);
2993
2994 /* We've already printed an error message if there was a problem
2995 inserting this catchpoint, and we've disabled the catchpoint,
2996 so just return success. */
2997 return 0;
2998 }
2999
3000 return 0;
3001 }
3002
3003 /* This function is called when program space PSPACE is about to be
3004 deleted. It takes care of updating breakpoints to not reference
3005 PSPACE anymore. */
3006
3007 void
3008 breakpoint_program_space_exit (struct program_space *pspace)
3009 {
3010 /* Remove any breakpoint that was set through this program space. */
3011 for (breakpoint *b : all_breakpoints_safe ())
3012 if (b->pspace == pspace)
3013 delete_breakpoint (b);
3014
3015 /* Breakpoints set through other program spaces could have locations
3016 bound to PSPACE as well. Remove those. */
3017 for (bp_location *loc : all_bp_locations ())
3018 {
3019 struct bp_location *tmp;
3020
3021 if (loc->pspace == pspace)
3022 {
3023 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3024 if (loc->owner->loc == loc)
3025 loc->owner->loc = loc->next;
3026 else
3027 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3028 if (tmp->next == loc)
3029 {
3030 tmp->next = loc->next;
3031 break;
3032 }
3033 }
3034 }
3035
3036 /* Now update the global location list to permanently delete the
3037 removed locations above. */
3038 update_global_location_list (UGLL_DONT_INSERT);
3039 }
3040
3041 /* Make sure all breakpoints are inserted in inferior.
3042 Throws exception on any error.
3043 A breakpoint that is already inserted won't be inserted
3044 again, so calling this function twice is safe. */
3045 void
3046 insert_breakpoints (void)
3047 {
3048 for (breakpoint *bpt : all_breakpoints ())
3049 if (is_hardware_watchpoint (bpt))
3050 {
3051 struct watchpoint *w = (struct watchpoint *) bpt;
3052
3053 update_watchpoint (w, false /* don't reparse. */);
3054 }
3055
3056 /* Updating watchpoints creates new locations, so update the global
3057 location list. Explicitly tell ugll to insert locations and
3058 ignore breakpoints_always_inserted_mode. Also,
3059 update_global_location_list tries to "upgrade" software
3060 breakpoints to hardware breakpoints to handle "set breakpoint
3061 auto-hw", so we need to call it even if we don't have new
3062 locations. */
3063 update_global_location_list (UGLL_INSERT);
3064 }
3065
3066 /* This is used when we need to synch breakpoint conditions between GDB and the
3067 target. It is the case with deleting and disabling of breakpoints when using
3068 always-inserted mode. */
3069
3070 static void
3071 update_inserted_breakpoint_locations (void)
3072 {
3073 int error_flag = 0;
3074 int val = 0;
3075 int disabled_breaks = 0;
3076 int hw_breakpoint_error = 0;
3077 int hw_bp_details_reported = 0;
3078
3079 string_file tmp_error_stream;
3080
3081 /* Explicitly mark the warning -- this will only be printed if
3082 there was an error. */
3083 tmp_error_stream.puts ("Warning:\n");
3084
3085 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3086
3087 for (bp_location *bl : all_bp_locations ())
3088 {
3089 /* We only want to update software breakpoints and hardware
3090 breakpoints. */
3091 if (!is_breakpoint (bl->owner))
3092 continue;
3093
3094 /* We only want to update locations that are already inserted
3095 and need updating. This is to avoid unwanted insertion during
3096 deletion of breakpoints. */
3097 if (!bl->inserted || !bl->needs_update)
3098 continue;
3099
3100 switch_to_program_space_and_thread (bl->pspace);
3101
3102 /* For targets that support global breakpoints, there's no need
3103 to select an inferior to insert breakpoint to. In fact, even
3104 if we aren't attached to any process yet, we should still
3105 insert breakpoints. */
3106 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3107 && (inferior_ptid == null_ptid || !target_has_execution ()))
3108 continue;
3109
3110 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3111 &hw_breakpoint_error, &hw_bp_details_reported);
3112 if (val)
3113 error_flag = val;
3114 }
3115
3116 if (error_flag)
3117 {
3118 target_terminal::ours_for_output ();
3119 error_stream (tmp_error_stream);
3120 }
3121 }
3122
3123 /* Used when starting or continuing the program. */
3124
3125 static void
3126 insert_breakpoint_locations (void)
3127 {
3128 int error_flag = 0;
3129 int val = 0;
3130 int disabled_breaks = 0;
3131 int hw_breakpoint_error = 0;
3132 int hw_bp_error_explained_already = 0;
3133
3134 string_file tmp_error_stream;
3135
3136 /* Explicitly mark the warning -- this will only be printed if
3137 there was an error. */
3138 tmp_error_stream.puts ("Warning:\n");
3139
3140 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3141
3142 for (bp_location *bl : all_bp_locations ())
3143 {
3144 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3145 continue;
3146
3147 /* There is no point inserting thread-specific breakpoints if
3148 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3149 has BL->OWNER always non-NULL. */
3150 if (bl->owner->thread != -1
3151 && !valid_global_thread_id (bl->owner->thread))
3152 continue;
3153
3154 switch_to_program_space_and_thread (bl->pspace);
3155
3156 /* For targets that support global breakpoints, there's no need
3157 to select an inferior to insert breakpoint to. In fact, even
3158 if we aren't attached to any process yet, we should still
3159 insert breakpoints. */
3160 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3161 && (inferior_ptid == null_ptid || !target_has_execution ()))
3162 continue;
3163
3164 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3165 &hw_breakpoint_error, &hw_bp_error_explained_already);
3166 if (val)
3167 error_flag = val;
3168 }
3169
3170 /* If we failed to insert all locations of a watchpoint, remove
3171 them, as half-inserted watchpoint is of limited use. */
3172 for (breakpoint *bpt : all_breakpoints ())
3173 {
3174 bool some_failed = false;
3175
3176 if (!is_hardware_watchpoint (bpt))
3177 continue;
3178
3179 if (!breakpoint_enabled (bpt))
3180 continue;
3181
3182 if (bpt->disposition == disp_del_at_next_stop)
3183 continue;
3184
3185 for (bp_location *loc : bpt->locations ())
3186 if (!loc->inserted && should_be_inserted (loc))
3187 {
3188 some_failed = true;
3189 break;
3190 }
3191
3192 if (some_failed)
3193 {
3194 for (bp_location *loc : bpt->locations ())
3195 if (loc->inserted)
3196 remove_breakpoint (loc);
3197
3198 hw_breakpoint_error = 1;
3199 tmp_error_stream.printf ("Could not insert "
3200 "hardware watchpoint %d.\n",
3201 bpt->number);
3202 error_flag = -1;
3203 }
3204 }
3205
3206 if (error_flag)
3207 {
3208 /* If a hardware breakpoint or watchpoint was inserted, add a
3209 message about possibly exhausted resources. */
3210 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3211 {
3212 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3213 You may have requested too many hardware breakpoints/watchpoints.\n");
3214 }
3215 target_terminal::ours_for_output ();
3216 error_stream (tmp_error_stream);
3217 }
3218 }
3219
3220 /* Used when the program stops.
3221 Returns zero if successful, or non-zero if there was a problem
3222 removing a breakpoint location. */
3223
3224 int
3225 remove_breakpoints (void)
3226 {
3227 int val = 0;
3228
3229 for (bp_location *bl : all_bp_locations ())
3230 if (bl->inserted && !is_tracepoint (bl->owner))
3231 val |= remove_breakpoint (bl);
3232
3233 return val;
3234 }
3235
3236 /* When a thread exits, remove breakpoints that are related to
3237 that thread. */
3238
3239 static void
3240 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3241 {
3242 for (breakpoint *b : all_breakpoints_safe ())
3243 {
3244 if (b->thread == tp->global_num && user_breakpoint_p (b))
3245 {
3246 b->disposition = disp_del_at_next_stop;
3247
3248 gdb_printf (_("\
3249 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3250 b->number, print_thread_id (tp));
3251
3252 /* Hide it from the user. */
3253 b->number = 0;
3254 }
3255 }
3256 }
3257
3258 /* See breakpoint.h. */
3259
3260 void
3261 remove_breakpoints_inf (inferior *inf)
3262 {
3263 int val;
3264
3265 for (bp_location *bl : all_bp_locations ())
3266 {
3267 if (bl->pspace != inf->pspace)
3268 continue;
3269
3270 if (bl->inserted && !bl->target_info.persist)
3271 {
3272 val = remove_breakpoint (bl);
3273 if (val != 0)
3274 return;
3275 }
3276 }
3277 }
3278
3279 static int internal_breakpoint_number = -1;
3280
3281 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3282 If INTERNAL is non-zero, the breakpoint number will be populated
3283 from internal_breakpoint_number and that variable decremented.
3284 Otherwise the breakpoint number will be populated from
3285 breakpoint_count and that value incremented. Internal breakpoints
3286 do not set the internal var bpnum. */
3287 static void
3288 set_breakpoint_number (int internal, struct breakpoint *b)
3289 {
3290 if (internal)
3291 b->number = internal_breakpoint_number--;
3292 else
3293 {
3294 set_breakpoint_count (breakpoint_count + 1);
3295 b->number = breakpoint_count;
3296 }
3297 }
3298
3299 static struct breakpoint *
3300 create_internal_breakpoint (struct gdbarch *gdbarch,
3301 CORE_ADDR address, enum bptype type)
3302 {
3303 std::unique_ptr<internal_breakpoint> b
3304 (new internal_breakpoint (gdbarch, type, address));
3305
3306 b->number = internal_breakpoint_number--;
3307
3308 return add_to_breakpoint_chain (std::move (b));
3309 }
3310
3311 static const char *const longjmp_names[] =
3312 {
3313 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3314 };
3315 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3316
3317 /* Per-objfile data private to breakpoint.c. */
3318 struct breakpoint_objfile_data
3319 {
3320 /* Minimal symbol for "_ovly_debug_event" (if any). */
3321 struct bound_minimal_symbol overlay_msym;
3322
3323 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3324 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3325
3326 /* True if we have looked for longjmp probes. */
3327 int longjmp_searched = 0;
3328
3329 /* SystemTap probe points for longjmp (if any). These are non-owning
3330 references. */
3331 std::vector<probe *> longjmp_probes;
3332
3333 /* Minimal symbol for "std::terminate()" (if any). */
3334 struct bound_minimal_symbol terminate_msym;
3335
3336 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3337 struct bound_minimal_symbol exception_msym;
3338
3339 /* True if we have looked for exception probes. */
3340 int exception_searched = 0;
3341
3342 /* SystemTap probe points for unwinding (if any). These are non-owning
3343 references. */
3344 std::vector<probe *> exception_probes;
3345 };
3346
3347 static const registry<objfile>::key<breakpoint_objfile_data>
3348 breakpoint_objfile_key;
3349
3350 /* Minimal symbol not found sentinel. */
3351 static struct minimal_symbol msym_not_found;
3352
3353 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3354
3355 static bool
3356 msym_not_found_p (const struct minimal_symbol *msym)
3357 {
3358 return msym == &msym_not_found;
3359 }
3360
3361 /* Return per-objfile data needed by breakpoint.c.
3362 Allocate the data if necessary. */
3363
3364 static struct breakpoint_objfile_data *
3365 get_breakpoint_objfile_data (struct objfile *objfile)
3366 {
3367 struct breakpoint_objfile_data *bp_objfile_data;
3368
3369 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3370 if (bp_objfile_data == NULL)
3371 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3372 return bp_objfile_data;
3373 }
3374
3375 static void
3376 create_overlay_event_breakpoint (void)
3377 {
3378 const char *const func_name = "_ovly_debug_event";
3379
3380 for (objfile *objfile : current_program_space->objfiles ())
3381 {
3382 struct breakpoint *b;
3383 struct breakpoint_objfile_data *bp_objfile_data;
3384 CORE_ADDR addr;
3385
3386 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3387
3388 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3389 continue;
3390
3391 if (bp_objfile_data->overlay_msym.minsym == NULL)
3392 {
3393 struct bound_minimal_symbol m;
3394
3395 m = lookup_minimal_symbol_text (func_name, objfile);
3396 if (m.minsym == NULL)
3397 {
3398 /* Avoid future lookups in this objfile. */
3399 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3400 continue;
3401 }
3402 bp_objfile_data->overlay_msym = m;
3403 }
3404
3405 addr = bp_objfile_data->overlay_msym.value_address ();
3406 b = create_internal_breakpoint (objfile->arch (), addr,
3407 bp_overlay_event);
3408 b->locspec = new_explicit_location_spec_function (func_name);
3409
3410 if (overlay_debugging == ovly_auto)
3411 {
3412 b->enable_state = bp_enabled;
3413 overlay_events_enabled = 1;
3414 }
3415 else
3416 {
3417 b->enable_state = bp_disabled;
3418 overlay_events_enabled = 0;
3419 }
3420 }
3421 }
3422
3423 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3424 true if a breakpoint was installed. */
3425
3426 static bool
3427 create_longjmp_master_breakpoint_probe (objfile *objfile)
3428 {
3429 struct gdbarch *gdbarch = objfile->arch ();
3430 struct breakpoint_objfile_data *bp_objfile_data
3431 = get_breakpoint_objfile_data (objfile);
3432
3433 if (!bp_objfile_data->longjmp_searched)
3434 {
3435 std::vector<probe *> ret
3436 = find_probes_in_objfile (objfile, "libc", "longjmp");
3437
3438 if (!ret.empty ())
3439 {
3440 /* We are only interested in checking one element. */
3441 probe *p = ret[0];
3442
3443 if (!p->can_evaluate_arguments ())
3444 {
3445 /* We cannot use the probe interface here,
3446 because it does not know how to evaluate
3447 arguments. */
3448 ret.clear ();
3449 }
3450 }
3451 bp_objfile_data->longjmp_probes = ret;
3452 bp_objfile_data->longjmp_searched = 1;
3453 }
3454
3455 if (bp_objfile_data->longjmp_probes.empty ())
3456 return false;
3457
3458 for (probe *p : bp_objfile_data->longjmp_probes)
3459 {
3460 struct breakpoint *b;
3461
3462 b = create_internal_breakpoint (gdbarch,
3463 p->get_relocated_address (objfile),
3464 bp_longjmp_master);
3465 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
3466 b->enable_state = bp_disabled;
3467 }
3468
3469 return true;
3470 }
3471
3472 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3473 Return true if at least one breakpoint was installed. */
3474
3475 static bool
3476 create_longjmp_master_breakpoint_names (objfile *objfile)
3477 {
3478 struct gdbarch *gdbarch = objfile->arch ();
3479 if (!gdbarch_get_longjmp_target_p (gdbarch))
3480 return false;
3481
3482 struct breakpoint_objfile_data *bp_objfile_data
3483 = get_breakpoint_objfile_data (objfile);
3484 unsigned int installed_bp = 0;
3485
3486 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3487 {
3488 struct breakpoint *b;
3489 const char *func_name;
3490 CORE_ADDR addr;
3491
3492 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3493 continue;
3494
3495 func_name = longjmp_names[i];
3496 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3497 {
3498 struct bound_minimal_symbol m;
3499
3500 m = lookup_minimal_symbol_text (func_name, objfile);
3501 if (m.minsym == NULL)
3502 {
3503 /* Prevent future lookups in this objfile. */
3504 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3505 continue;
3506 }
3507 bp_objfile_data->longjmp_msym[i] = m;
3508 }
3509
3510 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3511 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3512 b->locspec = new_explicit_location_spec_function (func_name);
3513 b->enable_state = bp_disabled;
3514 installed_bp++;
3515 }
3516
3517 return installed_bp > 0;
3518 }
3519
3520 /* Create a master longjmp breakpoint. */
3521
3522 static void
3523 create_longjmp_master_breakpoint (void)
3524 {
3525 scoped_restore_current_program_space restore_pspace;
3526
3527 for (struct program_space *pspace : program_spaces)
3528 {
3529 set_current_program_space (pspace);
3530
3531 for (objfile *obj : current_program_space->objfiles ())
3532 {
3533 /* Skip separate debug object, it's handled in the loop below. */
3534 if (obj->separate_debug_objfile_backlink != nullptr)
3535 continue;
3536
3537 /* Try a probe kind breakpoint on main objfile. */
3538 if (create_longjmp_master_breakpoint_probe (obj))
3539 continue;
3540
3541 /* Try longjmp_names kind breakpoints on main and separate_debug
3542 objfiles. */
3543 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3544 if (create_longjmp_master_breakpoint_names (debug_objfile))
3545 break;
3546 }
3547 }
3548 }
3549
3550 /* Create a master std::terminate breakpoint. */
3551 static void
3552 create_std_terminate_master_breakpoint (void)
3553 {
3554 const char *const func_name = "std::terminate()";
3555
3556 scoped_restore_current_program_space restore_pspace;
3557
3558 for (struct program_space *pspace : program_spaces)
3559 {
3560 CORE_ADDR addr;
3561
3562 set_current_program_space (pspace);
3563
3564 for (objfile *objfile : current_program_space->objfiles ())
3565 {
3566 struct breakpoint *b;
3567 struct breakpoint_objfile_data *bp_objfile_data;
3568
3569 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3570
3571 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3572 continue;
3573
3574 if (bp_objfile_data->terminate_msym.minsym == NULL)
3575 {
3576 struct bound_minimal_symbol m;
3577
3578 m = lookup_minimal_symbol (func_name, NULL, objfile);
3579 if (m.minsym == NULL || (m.minsym->type () != mst_text
3580 && m.minsym->type () != mst_file_text))
3581 {
3582 /* Prevent future lookups in this objfile. */
3583 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3584 continue;
3585 }
3586 bp_objfile_data->terminate_msym = m;
3587 }
3588
3589 addr = bp_objfile_data->terminate_msym.value_address ();
3590 b = create_internal_breakpoint (objfile->arch (), addr,
3591 bp_std_terminate_master);
3592 b->locspec = new_explicit_location_spec_function (func_name);
3593 b->enable_state = bp_disabled;
3594 }
3595 }
3596 }
3597
3598 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3599 probe. Return true if a breakpoint was installed. */
3600
3601 static bool
3602 create_exception_master_breakpoint_probe (objfile *objfile)
3603 {
3604 struct breakpoint *b;
3605 struct gdbarch *gdbarch;
3606 struct breakpoint_objfile_data *bp_objfile_data;
3607
3608 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3609
3610 /* We prefer the SystemTap probe point if it exists. */
3611 if (!bp_objfile_data->exception_searched)
3612 {
3613 std::vector<probe *> ret
3614 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3615
3616 if (!ret.empty ())
3617 {
3618 /* We are only interested in checking one element. */
3619 probe *p = ret[0];
3620
3621 if (!p->can_evaluate_arguments ())
3622 {
3623 /* We cannot use the probe interface here, because it does
3624 not know how to evaluate arguments. */
3625 ret.clear ();
3626 }
3627 }
3628 bp_objfile_data->exception_probes = ret;
3629 bp_objfile_data->exception_searched = 1;
3630 }
3631
3632 if (bp_objfile_data->exception_probes.empty ())
3633 return false;
3634
3635 gdbarch = objfile->arch ();
3636
3637 for (probe *p : bp_objfile_data->exception_probes)
3638 {
3639 b = create_internal_breakpoint (gdbarch,
3640 p->get_relocated_address (objfile),
3641 bp_exception_master);
3642 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
3643 b->enable_state = bp_disabled;
3644 }
3645
3646 return true;
3647 }
3648
3649 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3650 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3651
3652 static bool
3653 create_exception_master_breakpoint_hook (objfile *objfile)
3654 {
3655 const char *const func_name = "_Unwind_DebugHook";
3656 struct breakpoint *b;
3657 struct gdbarch *gdbarch;
3658 struct breakpoint_objfile_data *bp_objfile_data;
3659 CORE_ADDR addr;
3660
3661 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3662
3663 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3664 return false;
3665
3666 gdbarch = objfile->arch ();
3667
3668 if (bp_objfile_data->exception_msym.minsym == NULL)
3669 {
3670 struct bound_minimal_symbol debug_hook;
3671
3672 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3673 if (debug_hook.minsym == NULL)
3674 {
3675 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3676 return false;
3677 }
3678
3679 bp_objfile_data->exception_msym = debug_hook;
3680 }
3681
3682 addr = bp_objfile_data->exception_msym.value_address ();
3683 addr = gdbarch_convert_from_func_ptr_addr
3684 (gdbarch, addr, current_inferior ()->top_target ());
3685 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
3686 b->locspec = new_explicit_location_spec_function (func_name);
3687 b->enable_state = bp_disabled;
3688
3689 return true;
3690 }
3691
3692 /* Install a master breakpoint on the unwinder's debug hook. */
3693
3694 static void
3695 create_exception_master_breakpoint (void)
3696 {
3697 for (objfile *obj : current_program_space->objfiles ())
3698 {
3699 /* Skip separate debug object. */
3700 if (obj->separate_debug_objfile_backlink)
3701 continue;
3702
3703 /* Try a probe kind breakpoint. */
3704 if (create_exception_master_breakpoint_probe (obj))
3705 continue;
3706
3707 /* Iterate over main and separate debug objects and try an
3708 _Unwind_DebugHook kind breakpoint. */
3709 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3710 if (create_exception_master_breakpoint_hook (debug_objfile))
3711 break;
3712 }
3713 }
3714
3715 /* Does B have a location spec? */
3716
3717 static bool
3718 breakpoint_location_spec_empty_p (const struct breakpoint *b)
3719 {
3720 return (b->locspec != nullptr && b->locspec->empty_p ());
3721 }
3722
3723 void
3724 update_breakpoints_after_exec (void)
3725 {
3726 /* We're about to delete breakpoints from GDB's lists. If the
3727 INSERTED flag is true, GDB will try to lift the breakpoints by
3728 writing the breakpoints' "shadow contents" back into memory. The
3729 "shadow contents" are NOT valid after an exec, so GDB should not
3730 do that. Instead, the target is responsible from marking
3731 breakpoints out as soon as it detects an exec. We don't do that
3732 here instead, because there may be other attempts to delete
3733 breakpoints after detecting an exec and before reaching here. */
3734 for (bp_location *bploc : all_bp_locations ())
3735 if (bploc->pspace == current_program_space)
3736 gdb_assert (!bploc->inserted);
3737
3738 for (breakpoint *b : all_breakpoints_safe ())
3739 {
3740 if (b->pspace != current_program_space)
3741 continue;
3742
3743 /* Solib breakpoints must be explicitly reset after an exec(). */
3744 if (b->type == bp_shlib_event)
3745 {
3746 delete_breakpoint (b);
3747 continue;
3748 }
3749
3750 /* JIT breakpoints must be explicitly reset after an exec(). */
3751 if (b->type == bp_jit_event)
3752 {
3753 delete_breakpoint (b);
3754 continue;
3755 }
3756
3757 /* Thread event breakpoints must be set anew after an exec(),
3758 as must overlay event and longjmp master breakpoints. */
3759 if (b->type == bp_thread_event || b->type == bp_overlay_event
3760 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3761 || b->type == bp_exception_master)
3762 {
3763 delete_breakpoint (b);
3764 continue;
3765 }
3766
3767 /* Step-resume breakpoints are meaningless after an exec(). */
3768 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3769 {
3770 delete_breakpoint (b);
3771 continue;
3772 }
3773
3774 /* Just like single-step breakpoints. */
3775 if (b->type == bp_single_step)
3776 {
3777 delete_breakpoint (b);
3778 continue;
3779 }
3780
3781 /* Longjmp and longjmp-resume breakpoints are also meaningless
3782 after an exec. */
3783 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3784 || b->type == bp_longjmp_call_dummy
3785 || b->type == bp_exception || b->type == bp_exception_resume)
3786 {
3787 delete_breakpoint (b);
3788 continue;
3789 }
3790
3791 if (b->type == bp_catchpoint)
3792 {
3793 /* For now, none of the bp_catchpoint breakpoints need to
3794 do anything at this point. In the future, if some of
3795 the catchpoints need to something, we will need to add
3796 a new method, and call this method from here. */
3797 continue;
3798 }
3799
3800 /* bp_finish is a special case. The only way we ought to be able
3801 to see one of these when an exec() has happened, is if the user
3802 caught a vfork, and then said "finish". Ordinarily a finish just
3803 carries them to the call-site of the current callee, by setting
3804 a temporary bp there and resuming. But in this case, the finish
3805 will carry them entirely through the vfork & exec.
3806
3807 We don't want to allow a bp_finish to remain inserted now. But
3808 we can't safely delete it, 'cause finish_command has a handle to
3809 the bp on a bpstat, and will later want to delete it. There's a
3810 chance (and I've seen it happen) that if we delete the bp_finish
3811 here, that its storage will get reused by the time finish_command
3812 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3813 We really must allow finish_command to delete a bp_finish.
3814
3815 In the absence of a general solution for the "how do we know
3816 it's safe to delete something others may have handles to?"
3817 problem, what we'll do here is just uninsert the bp_finish, and
3818 let finish_command delete it.
3819
3820 (We know the bp_finish is "doomed" in the sense that it's
3821 momentary, and will be deleted as soon as finish_command sees
3822 the inferior stopped. So it doesn't matter that the bp's
3823 address is probably bogus in the new a.out, unlike e.g., the
3824 solib breakpoints.) */
3825
3826 if (b->type == bp_finish)
3827 {
3828 continue;
3829 }
3830
3831 /* Without a symbolic address, we have little hope of the
3832 pre-exec() address meaning the same thing in the post-exec()
3833 a.out. */
3834 if (breakpoint_location_spec_empty_p (b))
3835 {
3836 delete_breakpoint (b);
3837 continue;
3838 }
3839 }
3840 }
3841
3842 int
3843 detach_breakpoints (ptid_t ptid)
3844 {
3845 int val = 0;
3846 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3847 struct inferior *inf = current_inferior ();
3848
3849 if (ptid.pid () == inferior_ptid.pid ())
3850 error (_("Cannot detach breakpoints of inferior_ptid"));
3851
3852 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3853 inferior_ptid = ptid;
3854 for (bp_location *bl : all_bp_locations ())
3855 {
3856 if (bl->pspace != inf->pspace)
3857 continue;
3858
3859 /* This function must physically remove breakpoints locations
3860 from the specified ptid, without modifying the breakpoint
3861 package's state. Locations of type bp_loc_other and
3862 bp_loc_software_watchpoint are only maintained at GDB side,
3863 so there is no need to remove them. Moreover, removing these
3864 would modify the breakpoint package's state. */
3865 if (bl->loc_type == bp_loc_other
3866 || bl->loc_type == bp_loc_software_watchpoint)
3867 continue;
3868
3869 if (bl->inserted)
3870 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3871 }
3872
3873 return val;
3874 }
3875
3876 /* Remove the breakpoint location BL from the current address space.
3877 Note that this is used to detach breakpoints from a child fork.
3878 When we get here, the child isn't in the inferior list, and neither
3879 do we have objects to represent its address space --- we should
3880 *not* look at bl->pspace->aspace here. */
3881
3882 static int
3883 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3884 {
3885 int val;
3886
3887 /* BL is never in moribund_locations by our callers. */
3888 gdb_assert (bl->owner != NULL);
3889
3890 /* The type of none suggests that owner is actually deleted.
3891 This should not ever happen. */
3892 gdb_assert (bl->owner->type != bp_none);
3893
3894 if (bl->loc_type == bp_loc_software_breakpoint
3895 || bl->loc_type == bp_loc_hardware_breakpoint)
3896 {
3897 /* "Normal" instruction breakpoint: either the standard
3898 trap-instruction bp (bp_breakpoint), or a
3899 bp_hardware_breakpoint. */
3900
3901 /* First check to see if we have to handle an overlay. */
3902 if (overlay_debugging == ovly_off
3903 || bl->section == NULL
3904 || !(section_is_overlay (bl->section)))
3905 {
3906 /* No overlay handling: just remove the breakpoint. */
3907
3908 /* If we're trying to uninsert a memory breakpoint that we
3909 know is set in a dynamic object that is marked
3910 shlib_disabled, then either the dynamic object was
3911 removed with "remove-symbol-file" or with
3912 "nosharedlibrary". In the former case, we don't know
3913 whether another dynamic object might have loaded over the
3914 breakpoint's address -- the user might well let us know
3915 about it next with add-symbol-file (the whole point of
3916 add-symbol-file is letting the user manually maintain a
3917 list of dynamically loaded objects). If we have the
3918 breakpoint's shadow memory, that is, this is a software
3919 breakpoint managed by GDB, check whether the breakpoint
3920 is still inserted in memory, to avoid overwriting wrong
3921 code with stale saved shadow contents. Note that HW
3922 breakpoints don't have shadow memory, as they're
3923 implemented using a mechanism that is not dependent on
3924 being able to modify the target's memory, and as such
3925 they should always be removed. */
3926 if (bl->shlib_disabled
3927 && bl->target_info.shadow_len != 0
3928 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3929 val = 0;
3930 else
3931 val = bl->owner->remove_location (bl, reason);
3932 }
3933 else
3934 {
3935 /* This breakpoint is in an overlay section.
3936 Did we set a breakpoint at the LMA? */
3937 if (!overlay_events_enabled)
3938 {
3939 /* Yes -- overlay event support is not active, so we
3940 should have set a breakpoint at the LMA. Remove it.
3941 */
3942 /* Ignore any failures: if the LMA is in ROM, we will
3943 have already warned when we failed to insert it. */
3944 if (bl->loc_type == bp_loc_hardware_breakpoint)
3945 target_remove_hw_breakpoint (bl->gdbarch,
3946 &bl->overlay_target_info);
3947 else
3948 target_remove_breakpoint (bl->gdbarch,
3949 &bl->overlay_target_info,
3950 reason);
3951 }
3952 /* Did we set a breakpoint at the VMA?
3953 If so, we will have marked the breakpoint 'inserted'. */
3954 if (bl->inserted)
3955 {
3956 /* Yes -- remove it. Previously we did not bother to
3957 remove the breakpoint if the section had been
3958 unmapped, but let's not rely on that being safe. We
3959 don't know what the overlay manager might do. */
3960
3961 /* However, we should remove *software* breakpoints only
3962 if the section is still mapped, or else we overwrite
3963 wrong code with the saved shadow contents. */
3964 if (bl->loc_type == bp_loc_hardware_breakpoint
3965 || section_is_mapped (bl->section))
3966 val = bl->owner->remove_location (bl, reason);
3967 else
3968 val = 0;
3969 }
3970 else
3971 {
3972 /* No -- not inserted, so no need to remove. No error. */
3973 val = 0;
3974 }
3975 }
3976
3977 /* In some cases, we might not be able to remove a breakpoint in
3978 a shared library that has already been removed, but we have
3979 not yet processed the shlib unload event. Similarly for an
3980 unloaded add-symbol-file object - the user might not yet have
3981 had the chance to remove-symbol-file it. shlib_disabled will
3982 be set if the library/object has already been removed, but
3983 the breakpoint hasn't been uninserted yet, e.g., after
3984 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3985 always-inserted mode. */
3986 if (val
3987 && (bl->loc_type == bp_loc_software_breakpoint
3988 && (bl->shlib_disabled
3989 || solib_name_from_address (bl->pspace, bl->address)
3990 || shared_objfile_contains_address_p (bl->pspace,
3991 bl->address))))
3992 val = 0;
3993
3994 if (val)
3995 return val;
3996 bl->inserted = (reason == DETACH_BREAKPOINT);
3997 }
3998 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3999 {
4000 bl->inserted = (reason == DETACH_BREAKPOINT);
4001 bl->owner->remove_location (bl, reason);
4002
4003 /* Failure to remove any of the hardware watchpoints comes here. */
4004 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4005 warning (_("Could not remove hardware watchpoint %d."),
4006 bl->owner->number);
4007 }
4008 else if (bl->owner->type == bp_catchpoint
4009 && breakpoint_enabled (bl->owner)
4010 && !bl->duplicate)
4011 {
4012 val = bl->owner->remove_location (bl, reason);
4013 if (val)
4014 return val;
4015
4016 bl->inserted = (reason == DETACH_BREAKPOINT);
4017 }
4018
4019 return 0;
4020 }
4021
4022 static int
4023 remove_breakpoint (struct bp_location *bl)
4024 {
4025 /* BL is never in moribund_locations by our callers. */
4026 gdb_assert (bl->owner != NULL);
4027
4028 /* The type of none suggests that owner is actually deleted.
4029 This should not ever happen. */
4030 gdb_assert (bl->owner->type != bp_none);
4031
4032 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4033
4034 switch_to_program_space_and_thread (bl->pspace);
4035
4036 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4037 }
4038
4039 /* Clear the "inserted" flag in all breakpoints. */
4040
4041 void
4042 mark_breakpoints_out (void)
4043 {
4044 for (bp_location *bl : all_bp_locations ())
4045 if (bl->pspace == current_program_space)
4046 bl->inserted = 0;
4047 }
4048
4049 /* Clear the "inserted" flag in all breakpoints and delete any
4050 breakpoints which should go away between runs of the program.
4051
4052 Plus other such housekeeping that has to be done for breakpoints
4053 between runs.
4054
4055 Note: this function gets called at the end of a run (by
4056 generic_mourn_inferior) and when a run begins (by
4057 init_wait_for_inferior). */
4058
4059
4060
4061 void
4062 breakpoint_init_inferior (enum inf_context context)
4063 {
4064 struct program_space *pspace = current_program_space;
4065
4066 /* If breakpoint locations are shared across processes, then there's
4067 nothing to do. */
4068 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4069 return;
4070
4071 mark_breakpoints_out ();
4072
4073 for (breakpoint *b : all_breakpoints_safe ())
4074 {
4075 if (b->loc && b->loc->pspace != pspace)
4076 continue;
4077
4078 switch (b->type)
4079 {
4080 case bp_call_dummy:
4081 case bp_longjmp_call_dummy:
4082
4083 /* If the call dummy breakpoint is at the entry point it will
4084 cause problems when the inferior is rerun, so we better get
4085 rid of it. */
4086
4087 case bp_watchpoint_scope:
4088
4089 /* Also get rid of scope breakpoints. */
4090
4091 case bp_shlib_event:
4092
4093 /* Also remove solib event breakpoints. Their addresses may
4094 have changed since the last time we ran the program.
4095 Actually we may now be debugging against different target;
4096 and so the solib backend that installed this breakpoint may
4097 not be used in by the target. E.g.,
4098
4099 (gdb) file prog-linux
4100 (gdb) run # native linux target
4101 ...
4102 (gdb) kill
4103 (gdb) file prog-win.exe
4104 (gdb) tar rem :9999 # remote Windows gdbserver.
4105 */
4106
4107 case bp_step_resume:
4108
4109 /* Also remove step-resume breakpoints. */
4110
4111 case bp_single_step:
4112
4113 /* Also remove single-step breakpoints. */
4114
4115 delete_breakpoint (b);
4116 break;
4117
4118 case bp_watchpoint:
4119 case bp_hardware_watchpoint:
4120 case bp_read_watchpoint:
4121 case bp_access_watchpoint:
4122 {
4123 struct watchpoint *w = (struct watchpoint *) b;
4124
4125 /* Likewise for watchpoints on local expressions. */
4126 if (w->exp_valid_block != NULL)
4127 delete_breakpoint (b);
4128 else
4129 {
4130 /* Get rid of existing locations, which are no longer
4131 valid. New ones will be created in
4132 update_watchpoint, when the inferior is restarted.
4133 The next update_global_location_list call will
4134 garbage collect them. */
4135 b->loc = NULL;
4136
4137 if (context == inf_starting)
4138 {
4139 /* Reset val field to force reread of starting value in
4140 insert_breakpoints. */
4141 w->val.reset (nullptr);
4142 w->val_valid = false;
4143 }
4144 }
4145 }
4146 break;
4147 default:
4148 break;
4149 }
4150 }
4151
4152 /* Get rid of the moribund locations. */
4153 for (bp_location *bl : moribund_locations)
4154 decref_bp_location (&bl);
4155 moribund_locations.clear ();
4156 }
4157
4158 /* These functions concern about actual breakpoints inserted in the
4159 target --- to e.g. check if we need to do decr_pc adjustment or if
4160 we need to hop over the bkpt --- so we check for address space
4161 match, not program space. */
4162
4163 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4164 exists at PC. It returns ordinary_breakpoint_here if it's an
4165 ordinary breakpoint, or permanent_breakpoint_here if it's a
4166 permanent breakpoint.
4167 - When continuing from a location with an ordinary breakpoint, we
4168 actually single step once before calling insert_breakpoints.
4169 - When continuing from a location with a permanent breakpoint, we
4170 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4171 the target, to advance the PC past the breakpoint. */
4172
4173 enum breakpoint_here
4174 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4175 {
4176 bool any_breakpoint_here = false;
4177
4178 for (bp_location *bl : all_bp_locations ())
4179 {
4180 if (bl->loc_type != bp_loc_software_breakpoint
4181 && bl->loc_type != bp_loc_hardware_breakpoint)
4182 continue;
4183
4184 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4185 if ((breakpoint_enabled (bl->owner)
4186 || bl->permanent)
4187 && breakpoint_location_address_match (bl, aspace, pc))
4188 {
4189 if (overlay_debugging
4190 && section_is_overlay (bl->section)
4191 && !section_is_mapped (bl->section))
4192 continue; /* unmapped overlay -- can't be a match */
4193 else if (bl->permanent)
4194 return permanent_breakpoint_here;
4195 else
4196 any_breakpoint_here = true;
4197 }
4198 }
4199
4200 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4201 }
4202
4203 /* See breakpoint.h. */
4204
4205 int
4206 breakpoint_in_range_p (const address_space *aspace,
4207 CORE_ADDR addr, ULONGEST len)
4208 {
4209 for (bp_location *bl : all_bp_locations ())
4210 {
4211 if (bl->loc_type != bp_loc_software_breakpoint
4212 && bl->loc_type != bp_loc_hardware_breakpoint)
4213 continue;
4214
4215 if ((breakpoint_enabled (bl->owner)
4216 || bl->permanent)
4217 && breakpoint_location_address_range_overlap (bl, aspace,
4218 addr, len))
4219 {
4220 if (overlay_debugging
4221 && section_is_overlay (bl->section)
4222 && !section_is_mapped (bl->section))
4223 {
4224 /* Unmapped overlay -- can't be a match. */
4225 continue;
4226 }
4227
4228 return 1;
4229 }
4230 }
4231
4232 return 0;
4233 }
4234
4235 /* Return true if there's a moribund breakpoint at PC. */
4236
4237 int
4238 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4239 {
4240 for (bp_location *loc : moribund_locations)
4241 if (breakpoint_location_address_match (loc, aspace, pc))
4242 return 1;
4243
4244 return 0;
4245 }
4246
4247 /* Returns true iff BL is inserted at PC, in address space ASPACE. */
4248
4249 static bool
4250 bp_location_inserted_here_p (const struct bp_location *bl,
4251 const address_space *aspace, CORE_ADDR pc)
4252 {
4253 if (bl->inserted
4254 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4255 aspace, pc))
4256 {
4257 /* An unmapped overlay can't be a match. */
4258 return !(overlay_debugging
4259 && section_is_overlay (bl->section)
4260 && !section_is_mapped (bl->section));
4261 }
4262 return false;
4263 }
4264
4265 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4266
4267 int
4268 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4269 {
4270 for (bp_location *bl : all_bp_locations_at_addr (pc))
4271 {
4272 if (bl->loc_type != bp_loc_software_breakpoint
4273 && bl->loc_type != bp_loc_hardware_breakpoint)
4274 continue;
4275
4276 if (bp_location_inserted_here_p (bl, aspace, pc))
4277 return 1;
4278 }
4279 return 0;
4280 }
4281
4282 /* This function returns non-zero iff there is a software breakpoint
4283 inserted at PC. */
4284
4285 int
4286 software_breakpoint_inserted_here_p (const address_space *aspace,
4287 CORE_ADDR pc)
4288 {
4289 for (bp_location *bl : all_bp_locations_at_addr (pc))
4290 {
4291 if (bl->loc_type != bp_loc_software_breakpoint)
4292 continue;
4293
4294 if (bp_location_inserted_here_p (bl, aspace, pc))
4295 return 1;
4296 }
4297
4298 return 0;
4299 }
4300
4301 /* See breakpoint.h. */
4302
4303 int
4304 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4305 CORE_ADDR pc)
4306 {
4307 for (bp_location *bl : all_bp_locations_at_addr (pc))
4308 {
4309 if (bl->loc_type != bp_loc_hardware_breakpoint)
4310 continue;
4311
4312 if (bp_location_inserted_here_p (bl, aspace, pc))
4313 return 1;
4314 }
4315
4316 return 0;
4317 }
4318
4319 int
4320 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4321 CORE_ADDR addr, ULONGEST len)
4322 {
4323 for (breakpoint *bpt : all_breakpoints ())
4324 {
4325 if (bpt->type != bp_hardware_watchpoint
4326 && bpt->type != bp_access_watchpoint)
4327 continue;
4328
4329 if (!breakpoint_enabled (bpt))
4330 continue;
4331
4332 for (bp_location *loc : bpt->locations ())
4333 if (loc->pspace->aspace == aspace && loc->inserted)
4334 {
4335 CORE_ADDR l, h;
4336
4337 /* Check for intersection. */
4338 l = std::max<CORE_ADDR> (loc->address, addr);
4339 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4340 if (l < h)
4341 return 1;
4342 }
4343 }
4344 return 0;
4345 }
4346
4347 /* See breakpoint.h. */
4348
4349 bool
4350 is_catchpoint (struct breakpoint *b)
4351 {
4352 return (b->type == bp_catchpoint);
4353 }
4354
4355 /* Clear a bpstat so that it says we are not at any breakpoint.
4356 Also free any storage that is part of a bpstat. */
4357
4358 void
4359 bpstat_clear (bpstat **bsp)
4360 {
4361 bpstat *p;
4362 bpstat *q;
4363
4364 if (bsp == 0)
4365 return;
4366 p = *bsp;
4367 while (p != NULL)
4368 {
4369 q = p->next;
4370 delete p;
4371 p = q;
4372 }
4373 *bsp = NULL;
4374 }
4375
4376 bpstat::bpstat (const bpstat &other)
4377 : next (NULL),
4378 bp_location_at (other.bp_location_at),
4379 breakpoint_at (other.breakpoint_at),
4380 commands (other.commands),
4381 print (other.print),
4382 stop (other.stop),
4383 print_it (other.print_it)
4384 {
4385 if (other.old_val != NULL)
4386 old_val = release_value (value_copy (other.old_val.get ()));
4387 }
4388
4389 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4390 is part of the bpstat is copied as well. */
4391
4392 bpstat *
4393 bpstat_copy (bpstat *bs)
4394 {
4395 bpstat *p = nullptr;
4396 bpstat *tmp;
4397 bpstat *retval = nullptr;
4398
4399 if (bs == NULL)
4400 return bs;
4401
4402 for (; bs != NULL; bs = bs->next)
4403 {
4404 tmp = new bpstat (*bs);
4405
4406 if (p == NULL)
4407 /* This is the first thing in the chain. */
4408 retval = tmp;
4409 else
4410 p->next = tmp;
4411 p = tmp;
4412 }
4413 p->next = NULL;
4414 return retval;
4415 }
4416
4417 /* Find the bpstat associated with this breakpoint. */
4418
4419 bpstat *
4420 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4421 {
4422 if (bsp == NULL)
4423 return NULL;
4424
4425 for (; bsp != NULL; bsp = bsp->next)
4426 {
4427 if (bsp->breakpoint_at == breakpoint)
4428 return bsp;
4429 }
4430 return NULL;
4431 }
4432
4433 /* See breakpoint.h. */
4434
4435 bool
4436 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4437 {
4438 for (; bsp != NULL; bsp = bsp->next)
4439 {
4440 if (bsp->breakpoint_at == NULL)
4441 {
4442 /* A moribund location can never explain a signal other than
4443 GDB_SIGNAL_TRAP. */
4444 if (sig == GDB_SIGNAL_TRAP)
4445 return true;
4446 }
4447 else
4448 {
4449 if (bsp->breakpoint_at->explains_signal (sig))
4450 return true;
4451 }
4452 }
4453
4454 return false;
4455 }
4456
4457 /* See breakpoint.h. */
4458
4459 int
4460 bpstat_num (bpstat **bsp, int *num)
4461 {
4462 struct breakpoint *b;
4463
4464 if ((*bsp) == NULL)
4465 return 0; /* No more breakpoint values */
4466
4467 /* We assume we'll never have several bpstats that correspond to a
4468 single breakpoint -- otherwise, this function might return the
4469 same number more than once and this will look ugly. */
4470 b = (*bsp)->breakpoint_at;
4471 *bsp = (*bsp)->next;
4472 if (b == NULL)
4473 return -1; /* breakpoint that's been deleted since */
4474
4475 *num = b->number; /* We have its number */
4476 return 1;
4477 }
4478
4479 /* See breakpoint.h */
4480
4481 int
4482 bpstat_locno (const bpstat *bs)
4483 {
4484 const struct breakpoint *b = bs->breakpoint_at;
4485 const struct bp_location *bl = bs->bp_location_at.get ();
4486
4487 int locno = 0;
4488
4489 if (b != nullptr && b->loc->next != nullptr)
4490 {
4491 const bp_location *bl_i;
4492
4493 for (bl_i = b->loc;
4494 bl_i != bl && bl_i->next != nullptr;
4495 bl_i = bl_i->next)
4496 locno++;
4497
4498 if (bl_i == bl)
4499 locno++;
4500 else
4501 {
4502 warning (_("location number not found for breakpoint %d address %s."),
4503 b->number, paddress (bl->gdbarch, bl->address));
4504 locno = 0;
4505 }
4506 }
4507
4508 return locno;
4509 }
4510
4511 /* See breakpoint.h. */
4512
4513 void
4514 print_num_locno (const bpstat *bs, struct ui_out *uiout)
4515 {
4516 struct breakpoint *b = bs->breakpoint_at;
4517
4518 if (b == nullptr)
4519 uiout->text (_("deleted breakpoint"));
4520 else
4521 {
4522 uiout->field_signed ("bkptno", b->number);
4523
4524 int locno = bpstat_locno (bs);
4525 if (locno != 0)
4526 uiout->message (".%pF", signed_field ("locno", locno));
4527 }
4528 }
4529
4530 /* See breakpoint.h. */
4531
4532 void
4533 bpstat_clear_actions (void)
4534 {
4535 bpstat *bs;
4536
4537 if (inferior_ptid == null_ptid)
4538 return;
4539
4540 thread_info *tp = inferior_thread ();
4541 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4542 {
4543 bs->commands = NULL;
4544 bs->old_val.reset (nullptr);
4545 }
4546 }
4547
4548 /* Called when a command is about to proceed the inferior. */
4549
4550 static void
4551 breakpoint_about_to_proceed (void)
4552 {
4553 if (inferior_ptid != null_ptid)
4554 {
4555 struct thread_info *tp = inferior_thread ();
4556
4557 /* Allow inferior function calls in breakpoint commands to not
4558 interrupt the command list. When the call finishes
4559 successfully, the inferior will be standing at the same
4560 breakpoint as if nothing happened. */
4561 if (tp->control.in_infcall)
4562 return;
4563 }
4564
4565 breakpoint_proceeded = 1;
4566 }
4567
4568 /* Return true iff CMD as the first line of a command sequence is `silent'
4569 or its equivalent. */
4570
4571 static bool
4572 command_line_is_silent (struct command_line *cmd)
4573 {
4574 return cmd && (strcmp ("silent", cmd->line) == 0);
4575 }
4576
4577 /* Sets the $_hit_bpnum and $_hit_locno to the bpnum and locno of bs. */
4578 static void
4579 set_hit_convenience_vars (bpstat *bs)
4580 {
4581 const struct breakpoint *b = bs->breakpoint_at;
4582 if (b != nullptr)
4583 {
4584 int locno = bpstat_locno (bs);
4585 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), b->number);
4586 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4587 (locno > 0 ? locno : 1));
4588 }
4589 }
4590
4591 /* Execute all the commands associated with all the breakpoints at
4592 this location. Any of these commands could cause the process to
4593 proceed beyond this point, etc. We look out for such changes by
4594 checking the global "breakpoint_proceeded" after each command.
4595
4596 Returns true if a breakpoint command resumed the inferior. In that
4597 case, it is the caller's responsibility to recall it again with the
4598 bpstat of the current thread. */
4599
4600 static bool
4601 bpstat_do_actions_1 (bpstat **bsp)
4602 {
4603 bpstat *bs;
4604 bool again = false;
4605 bpstat *bs_print_hit_var;
4606
4607 /* Avoid endless recursion if a `source' command is contained
4608 in bs->commands. */
4609 if (executing_breakpoint_commands)
4610 return false;
4611
4612 scoped_restore save_executing
4613 = make_scoped_restore (&executing_breakpoint_commands, 1);
4614
4615 scoped_restore preventer = prevent_dont_repeat ();
4616
4617 /* This pointer will iterate over the list of bpstat's. */
4618 bs = *bsp;
4619
4620 /* The $_hit_* convenience variables are set before running the
4621 commands of bs. In case we have several bs, after the loop,
4622 we set again the variables to the first bs to print. */
4623 bs_print_hit_var = nullptr;
4624
4625 breakpoint_proceeded = 0;
4626 for (; bs != NULL; bs = bs->next)
4627 {
4628 struct command_line *cmd = NULL;
4629
4630 /* Set the _hit_* convenience variables before running the commands of
4631 each bs. If this is the first bs to be printed, remember it so as to
4632 set the convenience variable again to this bs after the loop so that in
4633 case of multiple breakpoints, the variables are set to the breakpoint
4634 printed for the user. */
4635 set_hit_convenience_vars (bs);
4636 if (bs_print_hit_var == nullptr && bs->print)
4637 bs_print_hit_var = bs;
4638
4639 /* Take ownership of the BSP's command tree, if it has one.
4640
4641 The command tree could legitimately contain commands like
4642 'step' and 'next', which call clear_proceed_status, which
4643 frees stop_bpstat's command tree. To make sure this doesn't
4644 free the tree we're executing out from under us, we need to
4645 take ownership of the tree ourselves. Since a given bpstat's
4646 commands are only executed once, we don't need to copy it; we
4647 can clear the pointer in the bpstat, and make sure we free
4648 the tree when we're done. */
4649 counted_command_line ccmd = bs->commands;
4650 bs->commands = NULL;
4651 if (ccmd != NULL)
4652 cmd = ccmd.get ();
4653 if (command_line_is_silent (cmd))
4654 {
4655 /* The action has been already done by bpstat_stop_status. */
4656 cmd = cmd->next;
4657 }
4658
4659 while (cmd != NULL)
4660 {
4661 execute_control_command (cmd);
4662
4663 if (breakpoint_proceeded)
4664 break;
4665 else
4666 cmd = cmd->next;
4667 }
4668
4669 if (breakpoint_proceeded)
4670 {
4671 if (current_ui->async)
4672 /* If we are in async mode, then the target might be still
4673 running, not stopped at any breakpoint, so nothing for
4674 us to do here -- just return to the event loop. */
4675 ;
4676 else
4677 /* In sync mode, when execute_control_command returns
4678 we're already standing on the next breakpoint.
4679 Breakpoint commands for that stop were not run, since
4680 execute_command does not run breakpoint commands --
4681 only command_line_handler does, but that one is not
4682 involved in execution of breakpoint commands. So, we
4683 can now execute breakpoint commands. It should be
4684 noted that making execute_command do bpstat actions is
4685 not an option -- in this case we'll have recursive
4686 invocation of bpstat for each breakpoint with a
4687 command, and can easily blow up GDB stack. Instead, we
4688 return true, which will trigger the caller to recall us
4689 with the new stop_bpstat. */
4690 again = true;
4691 break;
4692 }
4693 }
4694
4695 /* Now that we have executed the commands of all bs, set the _hit_*
4696 convenience variables to the printed bs. */
4697 if (bs_print_hit_var != nullptr)
4698 set_hit_convenience_vars (bs_print_hit_var);
4699
4700 return again;
4701 }
4702
4703 /* Helper for bpstat_do_actions. Get the current thread, if there's
4704 one, is alive and has execution. Return NULL otherwise. */
4705
4706 static thread_info *
4707 get_bpstat_thread ()
4708 {
4709 if (inferior_ptid == null_ptid || !target_has_execution ())
4710 return NULL;
4711
4712 thread_info *tp = inferior_thread ();
4713 if (tp->state == THREAD_EXITED || tp->executing ())
4714 return NULL;
4715 return tp;
4716 }
4717
4718 void
4719 bpstat_do_actions (void)
4720 {
4721 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4722 thread_info *tp;
4723
4724 /* Do any commands attached to breakpoint we are stopped at. */
4725 while ((tp = get_bpstat_thread ()) != NULL)
4726 {
4727 /* Since in sync mode, bpstat_do_actions may resume the
4728 inferior, and only return when it is stopped at the next
4729 breakpoint, we keep doing breakpoint actions until it returns
4730 false to indicate the inferior was not resumed. */
4731 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4732 break;
4733 }
4734
4735 cleanup_if_error.release ();
4736 }
4737
4738 /* Print out the (old or new) value associated with a watchpoint. */
4739
4740 static void
4741 watchpoint_value_print (struct value *val, struct ui_file *stream)
4742 {
4743 if (val == NULL)
4744 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4745 else
4746 {
4747 struct value_print_options opts;
4748 get_user_print_options (&opts);
4749 value_print (val, stream, &opts);
4750 }
4751 }
4752
4753 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4754 debugging multiple threads. */
4755
4756 void
4757 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4758 {
4759 if (uiout->is_mi_like_p ())
4760 return;
4761
4762 uiout->text ("\n");
4763
4764 if (show_thread_that_caused_stop ())
4765 {
4766 struct thread_info *thr = inferior_thread ();
4767
4768 uiout->text ("Thread ");
4769 uiout->field_string ("thread-id", print_thread_id (thr));
4770
4771 const char *name = thread_name (thr);
4772 if (name != NULL)
4773 {
4774 uiout->text (" \"");
4775 uiout->field_string ("name", name);
4776 uiout->text ("\"");
4777 }
4778
4779 uiout->text (" hit ");
4780 }
4781 }
4782
4783 /* Generic routine for printing messages indicating why we
4784 stopped. The behavior of this function depends on the value
4785 'print_it' in the bpstat structure. Under some circumstances we
4786 may decide not to print anything here and delegate the task to
4787 normal_stop(). */
4788
4789 static enum print_stop_action
4790 print_bp_stop_message (bpstat *bs)
4791 {
4792 switch (bs->print_it)
4793 {
4794 case print_it_noop:
4795 /* Nothing should be printed for this bpstat entry. */
4796 return PRINT_UNKNOWN;
4797 break;
4798
4799 case print_it_done:
4800 /* We still want to print the frame, but we already printed the
4801 relevant messages. */
4802 return PRINT_SRC_AND_LOC;
4803 break;
4804
4805 case print_it_normal:
4806 {
4807 struct breakpoint *b = bs->breakpoint_at;
4808
4809 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4810 which has since been deleted. */
4811 if (b == NULL)
4812 return PRINT_UNKNOWN;
4813
4814 /* Normal case. Call the breakpoint's print_it method. */
4815 return b->print_it (bs);
4816 }
4817 break;
4818
4819 default:
4820 internal_error (_("print_bp_stop_message: unrecognized enum value"));
4821 break;
4822 }
4823 }
4824
4825 /* See breakpoint.h. */
4826
4827 void
4828 print_solib_event (bool is_catchpoint)
4829 {
4830 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4831 bool any_added = !current_program_space->added_solibs.empty ();
4832
4833 if (!is_catchpoint)
4834 {
4835 if (any_added || any_deleted)
4836 current_uiout->text (_("Stopped due to shared library event:\n"));
4837 else
4838 current_uiout->text (_("Stopped due to shared library event (no "
4839 "libraries added or removed)\n"));
4840 }
4841
4842 if (current_uiout->is_mi_like_p ())
4843 current_uiout->field_string ("reason",
4844 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4845
4846 if (any_deleted)
4847 {
4848 current_uiout->text (_(" Inferior unloaded "));
4849 ui_out_emit_list list_emitter (current_uiout, "removed");
4850 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4851 {
4852 const std::string &name = current_program_space->deleted_solibs[ix];
4853
4854 if (ix > 0)
4855 current_uiout->text (" ");
4856 current_uiout->field_string ("library", name);
4857 current_uiout->text ("\n");
4858 }
4859 }
4860
4861 if (any_added)
4862 {
4863 current_uiout->text (_(" Inferior loaded "));
4864 ui_out_emit_list list_emitter (current_uiout, "added");
4865 bool first = true;
4866 for (so_list *iter : current_program_space->added_solibs)
4867 {
4868 if (!first)
4869 current_uiout->text (" ");
4870 first = false;
4871 current_uiout->field_string ("library", iter->so_name);
4872 current_uiout->text ("\n");
4873 }
4874 }
4875 }
4876
4877 /* Print a message indicating what happened. This is called from
4878 normal_stop(). The input to this routine is the head of the bpstat
4879 list - a list of the eventpoints that caused this stop. KIND is
4880 the target_waitkind for the stopping event. This
4881 routine calls the generic print routine for printing a message
4882 about reasons for stopping. This will print (for example) the
4883 "Breakpoint n," part of the output. The return value of this
4884 routine is one of:
4885
4886 PRINT_UNKNOWN: Means we printed nothing.
4887 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4888 code to print the location. An example is
4889 "Breakpoint 1, " which should be followed by
4890 the location.
4891 PRINT_SRC_ONLY: Means we printed something, but there is no need
4892 to also print the location part of the message.
4893 An example is the catch/throw messages, which
4894 don't require a location appended to the end.
4895 PRINT_NOTHING: We have done some printing and we don't need any
4896 further info to be printed. */
4897
4898 enum print_stop_action
4899 bpstat_print (bpstat *bs, target_waitkind kind)
4900 {
4901 enum print_stop_action val;
4902
4903 /* Maybe another breakpoint in the chain caused us to stop.
4904 (Currently all watchpoints go on the bpstat whether hit or not.
4905 That probably could (should) be changed, provided care is taken
4906 with respect to bpstat_explains_signal). */
4907 for (; bs; bs = bs->next)
4908 {
4909 val = print_bp_stop_message (bs);
4910 if (val == PRINT_SRC_ONLY
4911 || val == PRINT_SRC_AND_LOC
4912 || val == PRINT_NOTHING)
4913 return val;
4914 }
4915
4916 /* If we had hit a shared library event breakpoint,
4917 print_bp_stop_message would print out this message. If we hit an
4918 OS-level shared library event, do the same thing. */
4919 if (kind == TARGET_WAITKIND_LOADED)
4920 {
4921 print_solib_event (false);
4922 return PRINT_NOTHING;
4923 }
4924
4925 /* We reached the end of the chain, or we got a null BS to start
4926 with and nothing was printed. */
4927 return PRINT_UNKNOWN;
4928 }
4929
4930 /* Evaluate the boolean expression EXP and return the result. */
4931
4932 static bool
4933 breakpoint_cond_eval (expression *exp)
4934 {
4935 scoped_value_mark mark;
4936 return value_true (evaluate_expression (exp));
4937 }
4938
4939 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4940
4941 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
4942 : next (NULL),
4943 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4944 breakpoint_at (bl->owner),
4945 commands (NULL),
4946 print (0),
4947 stop (0),
4948 print_it (print_it_normal)
4949 {
4950 **bs_link_pointer = this;
4951 *bs_link_pointer = &next;
4952 }
4953
4954 bpstat::bpstat ()
4955 : next (NULL),
4956 breakpoint_at (NULL),
4957 commands (NULL),
4958 print (0),
4959 stop (0),
4960 print_it (print_it_normal)
4961 {
4962 }
4963 \f
4964 /* The target has stopped with waitstatus WS. Check if any hardware
4965 watchpoints have triggered, according to the target. */
4966
4967 int
4968 watchpoints_triggered (const target_waitstatus &ws)
4969 {
4970 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4971 CORE_ADDR addr;
4972
4973 if (!stopped_by_watchpoint)
4974 {
4975 /* We were not stopped by a watchpoint. Mark all watchpoints
4976 as not triggered. */
4977 for (breakpoint *b : all_breakpoints ())
4978 if (is_hardware_watchpoint (b))
4979 {
4980 struct watchpoint *w = (struct watchpoint *) b;
4981
4982 w->watchpoint_triggered = watch_triggered_no;
4983 }
4984
4985 return 0;
4986 }
4987
4988 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4989 {
4990 /* We were stopped by a watchpoint, but we don't know where.
4991 Mark all watchpoints as unknown. */
4992 for (breakpoint *b : all_breakpoints ())
4993 if (is_hardware_watchpoint (b))
4994 {
4995 struct watchpoint *w = (struct watchpoint *) b;
4996
4997 w->watchpoint_triggered = watch_triggered_unknown;
4998 }
4999
5000 return 1;
5001 }
5002
5003 /* The target could report the data address. Mark watchpoints
5004 affected by this data address as triggered, and all others as not
5005 triggered. */
5006
5007 for (breakpoint *b : all_breakpoints ())
5008 if (is_hardware_watchpoint (b))
5009 {
5010 struct watchpoint *w = (struct watchpoint *) b;
5011
5012 w->watchpoint_triggered = watch_triggered_no;
5013 for (bp_location *loc : b->locations ())
5014 {
5015 if (is_masked_watchpoint (b))
5016 {
5017 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5018 CORE_ADDR start = loc->address & w->hw_wp_mask;
5019
5020 if (newaddr == start)
5021 {
5022 w->watchpoint_triggered = watch_triggered_yes;
5023 break;
5024 }
5025 }
5026 /* Exact match not required. Within range is sufficient. */
5027 else if (target_watchpoint_addr_within_range
5028 (current_inferior ()->top_target (), addr, loc->address,
5029 loc->length))
5030 {
5031 w->watchpoint_triggered = watch_triggered_yes;
5032 break;
5033 }
5034 }
5035 }
5036
5037 return 1;
5038 }
5039
5040 /* Possible return values for watchpoint_check. */
5041 enum wp_check_result
5042 {
5043 /* The watchpoint has been deleted. */
5044 WP_DELETED = 1,
5045
5046 /* The value has changed. */
5047 WP_VALUE_CHANGED = 2,
5048
5049 /* The value has not changed. */
5050 WP_VALUE_NOT_CHANGED = 3,
5051
5052 /* Ignore this watchpoint, no matter if the value changed or not. */
5053 WP_IGNORE = 4,
5054 };
5055
5056 #define BP_TEMPFLAG 1
5057 #define BP_HARDWAREFLAG 2
5058
5059 /* Evaluate watchpoint condition expression and check if its value
5060 changed. */
5061
5062 static wp_check_result
5063 watchpoint_check (bpstat *bs)
5064 {
5065 struct watchpoint *b;
5066 frame_info_ptr fr;
5067 bool within_current_scope;
5068
5069 /* BS is built from an existing struct breakpoint. */
5070 gdb_assert (bs->breakpoint_at != NULL);
5071 b = (struct watchpoint *) bs->breakpoint_at;
5072
5073 /* If this is a local watchpoint, we only want to check if the
5074 watchpoint frame is in scope if the current thread is the thread
5075 that was used to create the watchpoint. */
5076 if (!watchpoint_in_thread_scope (b))
5077 return WP_IGNORE;
5078
5079 if (b->exp_valid_block == NULL)
5080 within_current_scope = true;
5081 else
5082 {
5083 frame_info_ptr frame = get_current_frame ();
5084 struct gdbarch *frame_arch = get_frame_arch (frame);
5085 CORE_ADDR frame_pc = get_frame_pc (frame);
5086
5087 /* stack_frame_destroyed_p() returns a non-zero value if we're
5088 still in the function but the stack frame has already been
5089 invalidated. Since we can't rely on the values of local
5090 variables after the stack has been destroyed, we are treating
5091 the watchpoint in that state as `not changed' without further
5092 checking. Don't mark watchpoints as changed if the current
5093 frame is in an epilogue - even if they are in some other
5094 frame, our view of the stack is likely to be wrong and
5095 frame_find_by_id could error out. */
5096 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5097 return WP_IGNORE;
5098
5099 fr = frame_find_by_id (b->watchpoint_frame);
5100 within_current_scope = (fr != NULL);
5101
5102 /* If we've gotten confused in the unwinder, we might have
5103 returned a frame that can't describe this variable. */
5104 if (within_current_scope)
5105 {
5106 struct symbol *function;
5107
5108 function = get_frame_function (fr);
5109 if (function == NULL
5110 || !contained_in (b->exp_valid_block, function->value_block ()))
5111 within_current_scope = false;
5112 }
5113
5114 if (within_current_scope)
5115 /* If we end up stopping, the current frame will get selected
5116 in normal_stop. So this call to select_frame won't affect
5117 the user. */
5118 select_frame (fr);
5119 }
5120
5121 if (within_current_scope)
5122 {
5123 /* We use value_{,free_to_}mark because it could be a *long*
5124 time before we return to the command level and call
5125 free_all_values. We can't call free_all_values because we
5126 might be in the middle of evaluating a function call. */
5127
5128 struct value *mark;
5129 struct value *new_val;
5130
5131 if (is_masked_watchpoint (b))
5132 /* Since we don't know the exact trigger address (from
5133 stopped_data_address), just tell the user we've triggered
5134 a mask watchpoint. */
5135 return WP_VALUE_CHANGED;
5136
5137 mark = value_mark ();
5138 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5139 NULL, NULL, false);
5140
5141 if (b->val_bitsize != 0)
5142 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5143
5144 /* We use value_equal_contents instead of value_equal because
5145 the latter coerces an array to a pointer, thus comparing just
5146 the address of the array instead of its contents. This is
5147 not what we want. */
5148 if ((b->val != NULL) != (new_val != NULL)
5149 || (b->val != NULL && !value_equal_contents (b->val.get (),
5150 new_val)))
5151 {
5152 bs->old_val = b->val;
5153 b->val = release_value (new_val);
5154 b->val_valid = true;
5155 if (new_val != NULL)
5156 value_free_to_mark (mark);
5157 return WP_VALUE_CHANGED;
5158 }
5159 else
5160 {
5161 /* Nothing changed. */
5162 value_free_to_mark (mark);
5163 return WP_VALUE_NOT_CHANGED;
5164 }
5165 }
5166 else
5167 {
5168 /* This seems like the only logical thing to do because
5169 if we temporarily ignored the watchpoint, then when
5170 we reenter the block in which it is valid it contains
5171 garbage (in the case of a function, it may have two
5172 garbage values, one before and one after the prologue).
5173 So we can't even detect the first assignment to it and
5174 watch after that (since the garbage may or may not equal
5175 the first value assigned). */
5176 /* We print all the stop information in
5177 breakpointprint_it, but in this case, by the time we
5178 call breakpoint->print_it this bp will be deleted
5179 already. So we have no choice but print the information
5180 here. */
5181
5182 SWITCH_THRU_ALL_UIS ()
5183 {
5184 struct ui_out *uiout = current_uiout;
5185
5186 if (uiout->is_mi_like_p ())
5187 uiout->field_string
5188 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5189 uiout->message ("\nWatchpoint %pF deleted because the program has "
5190 "left the block in\n"
5191 "which its expression is valid.\n",
5192 signed_field ("wpnum", b->number));
5193 }
5194
5195 /* Make sure the watchpoint's commands aren't executed. */
5196 b->commands = NULL;
5197 watchpoint_del_at_next_stop (b);
5198
5199 return WP_DELETED;
5200 }
5201 }
5202
5203 /* Return true if it looks like target has stopped due to hitting
5204 breakpoint location BL. This function does not check if we should
5205 stop, only if BL explains the stop. */
5206
5207 static bool
5208 bpstat_check_location (const struct bp_location *bl,
5209 const address_space *aspace, CORE_ADDR bp_addr,
5210 const target_waitstatus &ws)
5211 {
5212 struct breakpoint *b = bl->owner;
5213
5214 /* BL is from an existing breakpoint. */
5215 gdb_assert (b != NULL);
5216
5217 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5218 }
5219
5220 /* Determine if the watched values have actually changed, and we
5221 should stop. If not, set BS->stop to 0. */
5222
5223 static void
5224 bpstat_check_watchpoint (bpstat *bs)
5225 {
5226 const struct bp_location *bl;
5227 struct watchpoint *b;
5228
5229 /* BS is built for existing struct breakpoint. */
5230 bl = bs->bp_location_at.get ();
5231 gdb_assert (bl != NULL);
5232 b = (struct watchpoint *) bs->breakpoint_at;
5233 gdb_assert (b != NULL);
5234
5235 {
5236 bool must_check_value = false;
5237
5238 if (b->type == bp_watchpoint)
5239 /* For a software watchpoint, we must always check the
5240 watched value. */
5241 must_check_value = true;
5242 else if (b->watchpoint_triggered == watch_triggered_yes)
5243 /* We have a hardware watchpoint (read, write, or access)
5244 and the target earlier reported an address watched by
5245 this watchpoint. */
5246 must_check_value = true;
5247 else if (b->watchpoint_triggered == watch_triggered_unknown
5248 && b->type == bp_hardware_watchpoint)
5249 /* We were stopped by a hardware watchpoint, but the target could
5250 not report the data address. We must check the watchpoint's
5251 value. Access and read watchpoints are out of luck; without
5252 a data address, we can't figure it out. */
5253 must_check_value = true;
5254
5255 if (must_check_value)
5256 {
5257 wp_check_result e;
5258
5259 try
5260 {
5261 e = watchpoint_check (bs);
5262 }
5263 catch (const gdb_exception &ex)
5264 {
5265 exception_fprintf (gdb_stderr, ex,
5266 "Error evaluating expression "
5267 "for watchpoint %d\n",
5268 b->number);
5269
5270 SWITCH_THRU_ALL_UIS ()
5271 {
5272 gdb_printf (_("Watchpoint %d deleted.\n"),
5273 b->number);
5274 }
5275 watchpoint_del_at_next_stop (b);
5276 e = WP_DELETED;
5277 }
5278
5279 switch (e)
5280 {
5281 case WP_DELETED:
5282 /* We've already printed what needs to be printed. */
5283 bs->print_it = print_it_done;
5284 /* Stop. */
5285 break;
5286 case WP_IGNORE:
5287 bs->print_it = print_it_noop;
5288 bs->stop = 0;
5289 break;
5290 case WP_VALUE_CHANGED:
5291 if (b->type == bp_read_watchpoint)
5292 {
5293 /* There are two cases to consider here:
5294
5295 1. We're watching the triggered memory for reads.
5296 In that case, trust the target, and always report
5297 the watchpoint hit to the user. Even though
5298 reads don't cause value changes, the value may
5299 have changed since the last time it was read, and
5300 since we're not trapping writes, we will not see
5301 those, and as such we should ignore our notion of
5302 old value.
5303
5304 2. We're watching the triggered memory for both
5305 reads and writes. There are two ways this may
5306 happen:
5307
5308 2.1. This is a target that can't break on data
5309 reads only, but can break on accesses (reads or
5310 writes), such as e.g., x86. We detect this case
5311 at the time we try to insert read watchpoints.
5312
5313 2.2. Otherwise, the target supports read
5314 watchpoints, but, the user set an access or write
5315 watchpoint watching the same memory as this read
5316 watchpoint.
5317
5318 If we're watching memory writes as well as reads,
5319 ignore watchpoint hits when we find that the
5320 value hasn't changed, as reads don't cause
5321 changes. This still gives false positives when
5322 the program writes the same value to memory as
5323 what there was already in memory (we will confuse
5324 it for a read), but it's much better than
5325 nothing. */
5326
5327 int other_write_watchpoint = 0;
5328
5329 if (bl->watchpoint_type == hw_read)
5330 {
5331 for (breakpoint *other_b : all_breakpoints ())
5332 if (other_b->type == bp_hardware_watchpoint
5333 || other_b->type == bp_access_watchpoint)
5334 {
5335 struct watchpoint *other_w =
5336 (struct watchpoint *) other_b;
5337
5338 if (other_w->watchpoint_triggered
5339 == watch_triggered_yes)
5340 {
5341 other_write_watchpoint = 1;
5342 break;
5343 }
5344 }
5345 }
5346
5347 if (other_write_watchpoint
5348 || bl->watchpoint_type == hw_access)
5349 {
5350 /* We're watching the same memory for writes,
5351 and the value changed since the last time we
5352 updated it, so this trap must be for a write.
5353 Ignore it. */
5354 bs->print_it = print_it_noop;
5355 bs->stop = 0;
5356 }
5357 }
5358 break;
5359 case WP_VALUE_NOT_CHANGED:
5360 if (b->type == bp_hardware_watchpoint
5361 || b->type == bp_watchpoint)
5362 {
5363 /* Don't stop: write watchpoints shouldn't fire if
5364 the value hasn't changed. */
5365 bs->print_it = print_it_noop;
5366 bs->stop = 0;
5367 }
5368 /* Stop. */
5369 break;
5370 default:
5371 /* Can't happen. */
5372 break;
5373 }
5374 }
5375 else /* !must_check_value */
5376 {
5377 /* This is a case where some watchpoint(s) triggered, but
5378 not at the address of this watchpoint, or else no
5379 watchpoint triggered after all. So don't print
5380 anything for this watchpoint. */
5381 bs->print_it = print_it_noop;
5382 bs->stop = 0;
5383 }
5384 }
5385 }
5386
5387 /* For breakpoints that are currently marked as telling gdb to stop,
5388 check conditions (condition proper, frame, thread and ignore count)
5389 of breakpoint referred to by BS. If we should not stop for this
5390 breakpoint, set BS->stop to 0. */
5391
5392 static void
5393 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5394 {
5395 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5396
5397 const struct bp_location *bl;
5398 struct breakpoint *b;
5399 /* Assume stop. */
5400 bool condition_result = true;
5401 struct expression *cond;
5402
5403 gdb_assert (bs->stop);
5404
5405 /* BS is built for existing struct breakpoint. */
5406 bl = bs->bp_location_at.get ();
5407 gdb_assert (bl != NULL);
5408 b = bs->breakpoint_at;
5409 gdb_assert (b != NULL);
5410
5411 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5412 thread->ptid.to_string ().c_str (),
5413 b->number, find_loc_num_by_location (bl));
5414
5415 /* Even if the target evaluated the condition on its end and notified GDB, we
5416 need to do so again since GDB does not know if we stopped due to a
5417 breakpoint or a single step breakpoint. */
5418
5419 if (frame_id_p (b->frame_id)
5420 && b->frame_id != get_stack_frame_id (get_current_frame ()))
5421 {
5422 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5423 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5424 b->frame_id.to_string ().c_str ());
5425 bs->stop = 0;
5426 return;
5427 }
5428
5429 /* If this is a thread/task-specific breakpoint, don't waste cpu
5430 evaluating the condition if this isn't the specified
5431 thread/task. */
5432 if ((b->thread != -1 && b->thread != thread->global_num)
5433 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5434 {
5435 infrun_debug_printf ("incorrect thread or task, not stopping");
5436 bs->stop = 0;
5437 return;
5438 }
5439
5440 /* Evaluate extension language breakpoints that have a "stop" method
5441 implemented. */
5442 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5443
5444 if (is_watchpoint (b))
5445 {
5446 struct watchpoint *w = (struct watchpoint *) b;
5447
5448 cond = w->cond_exp.get ();
5449 }
5450 else
5451 cond = bl->cond.get ();
5452
5453 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
5454 {
5455 bool within_current_scope = true;
5456 struct watchpoint * w;
5457
5458 /* We use scoped_value_mark because it could be a long time
5459 before we return to the command level and call
5460 free_all_values. We can't call free_all_values because we
5461 might be in the middle of evaluating a function call. */
5462 scoped_value_mark mark;
5463
5464 if (is_watchpoint (b))
5465 w = (struct watchpoint *) b;
5466 else
5467 w = NULL;
5468
5469 /* Need to select the frame, with all that implies so that
5470 the conditions will have the right context. Because we
5471 use the frame, we will not see an inlined function's
5472 variables when we arrive at a breakpoint at the start
5473 of the inlined function; the current frame will be the
5474 call site. */
5475 if (w == NULL || w->cond_exp_valid_block == NULL)
5476 select_frame (get_current_frame ());
5477 else
5478 {
5479 frame_info_ptr frame;
5480
5481 /* For local watchpoint expressions, which particular
5482 instance of a local is being watched matters, so we
5483 keep track of the frame to evaluate the expression
5484 in. To evaluate the condition however, it doesn't
5485 really matter which instantiation of the function
5486 where the condition makes sense triggers the
5487 watchpoint. This allows an expression like "watch
5488 global if q > 10" set in `func', catch writes to
5489 global on all threads that call `func', or catch
5490 writes on all recursive calls of `func' by a single
5491 thread. We simply always evaluate the condition in
5492 the innermost frame that's executing where it makes
5493 sense to evaluate the condition. It seems
5494 intuitive. */
5495 frame = block_innermost_frame (w->cond_exp_valid_block);
5496 if (frame != NULL)
5497 select_frame (frame);
5498 else
5499 within_current_scope = false;
5500 }
5501 if (within_current_scope)
5502 {
5503 try
5504 {
5505 condition_result = breakpoint_cond_eval (cond);
5506 }
5507 catch (const gdb_exception &ex)
5508 {
5509 exception_fprintf (gdb_stderr, ex,
5510 "Error in testing breakpoint condition:\n");
5511 }
5512 }
5513 else
5514 {
5515 warning (_("Watchpoint condition cannot be tested "
5516 "in the current scope"));
5517 /* If we failed to set the right context for this
5518 watchpoint, unconditionally report it. */
5519 }
5520 /* FIXME-someday, should give breakpoint #. */
5521 }
5522
5523 if (cond != nullptr && !condition_result)
5524 {
5525 infrun_debug_printf ("condition_result = false, not stopping");
5526 bs->stop = 0;
5527 return;
5528 }
5529 else if (b->ignore_count > 0)
5530 {
5531 infrun_debug_printf ("ignore count %d, not stopping",
5532 b->ignore_count);
5533 b->ignore_count--;
5534 bs->stop = 0;
5535 /* Increase the hit count even though we don't stop. */
5536 ++(b->hit_count);
5537 gdb::observers::breakpoint_modified.notify (b);
5538 return;
5539 }
5540
5541 if (bs->stop)
5542 infrun_debug_printf ("stopping at this breakpoint");
5543 else
5544 infrun_debug_printf ("not stopping at this breakpoint");
5545 }
5546
5547 /* Returns true if we need to track moribund locations of LOC's type
5548 on the current target. */
5549
5550 static bool
5551 need_moribund_for_location_type (const struct bp_location *loc)
5552 {
5553 return ((loc->loc_type == bp_loc_software_breakpoint
5554 && !target_supports_stopped_by_sw_breakpoint ())
5555 || (loc->loc_type == bp_loc_hardware_breakpoint
5556 && !target_supports_stopped_by_hw_breakpoint ()));
5557 }
5558
5559 /* See breakpoint.h. */
5560
5561 bpstat *
5562 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5563 const target_waitstatus &ws)
5564 {
5565 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5566
5567 for (breakpoint *b : all_breakpoints ())
5568 {
5569 if (!breakpoint_enabled (b))
5570 continue;
5571
5572 for (bp_location *bl : b->locations ())
5573 {
5574 /* For hardware watchpoints, we look only at the first
5575 location. The watchpoint_check function will work on the
5576 entire expression, not the individual locations. For
5577 read watchpoints, the watchpoints_triggered function has
5578 checked all locations already. */
5579 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5580 break;
5581
5582 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5583 continue;
5584
5585 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5586 continue;
5587
5588 /* Come here if it's a watchpoint, or if the break address
5589 matches. */
5590
5591 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5592 explain stop. */
5593
5594 /* Assume we stop. Should we find a watchpoint that is not
5595 actually triggered, or if the condition of the breakpoint
5596 evaluates as false, we'll reset 'stop' to 0. */
5597 bs->stop = 1;
5598 bs->print = 1;
5599
5600 /* If this is a scope breakpoint, mark the associated
5601 watchpoint as triggered so that we will handle the
5602 out-of-scope event. We'll get to the watchpoint next
5603 iteration. */
5604 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5605 {
5606 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5607
5608 w->watchpoint_triggered = watch_triggered_yes;
5609 }
5610 }
5611 }
5612
5613 /* Check if a moribund breakpoint explains the stop. */
5614 if (!target_supports_stopped_by_sw_breakpoint ()
5615 || !target_supports_stopped_by_hw_breakpoint ())
5616 {
5617 for (bp_location *loc : moribund_locations)
5618 {
5619 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5620 && need_moribund_for_location_type (loc))
5621 {
5622 bpstat *bs = new bpstat (loc, &bs_link);
5623 /* For hits of moribund locations, we should just proceed. */
5624 bs->stop = 0;
5625 bs->print = 0;
5626 bs->print_it = print_it_noop;
5627 }
5628 }
5629 }
5630
5631 return bs_head;
5632 }
5633
5634 /* See breakpoint.h. */
5635
5636 bpstat *
5637 bpstat_stop_status (const address_space *aspace,
5638 CORE_ADDR bp_addr, thread_info *thread,
5639 const target_waitstatus &ws,
5640 bpstat *stop_chain)
5641 {
5642 struct breakpoint *b = NULL;
5643 /* First item of allocated bpstat's. */
5644 bpstat *bs_head = stop_chain;
5645 bpstat *bs;
5646 int need_remove_insert;
5647 int removed_any;
5648
5649 /* First, build the bpstat chain with locations that explain a
5650 target stop, while being careful to not set the target running,
5651 as that may invalidate locations (in particular watchpoint
5652 locations are recreated). Resuming will happen here with
5653 breakpoint conditions or watchpoint expressions that include
5654 inferior function calls. */
5655 if (bs_head == NULL)
5656 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5657
5658 /* A bit of special processing for shlib breakpoints. We need to
5659 process solib loading here, so that the lists of loaded and
5660 unloaded libraries are correct before we handle "catch load" and
5661 "catch unload". */
5662 for (bs = bs_head; bs != NULL; bs = bs->next)
5663 {
5664 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5665 {
5666 handle_solib_event ();
5667 break;
5668 }
5669 }
5670
5671 /* Now go through the locations that caused the target to stop, and
5672 check whether we're interested in reporting this stop to higher
5673 layers, or whether we should resume the target transparently. */
5674
5675 removed_any = 0;
5676
5677 for (bs = bs_head; bs != NULL; bs = bs->next)
5678 {
5679 if (!bs->stop)
5680 continue;
5681
5682 b = bs->breakpoint_at;
5683 b->check_status (bs);
5684 if (bs->stop)
5685 {
5686 bpstat_check_breakpoint_conditions (bs, thread);
5687
5688 if (bs->stop)
5689 {
5690 ++(b->hit_count);
5691
5692 /* We will stop here. */
5693 if (b->disposition == disp_disable)
5694 {
5695 --(b->enable_count);
5696 if (b->enable_count <= 0)
5697 b->enable_state = bp_disabled;
5698 removed_any = 1;
5699 }
5700 gdb::observers::breakpoint_modified.notify (b);
5701 if (b->silent)
5702 bs->print = 0;
5703 bs->commands = b->commands;
5704 if (command_line_is_silent (bs->commands
5705 ? bs->commands.get () : NULL))
5706 bs->print = 0;
5707
5708 b->after_condition_true (bs);
5709 }
5710
5711 }
5712
5713 /* Print nothing for this entry if we don't stop or don't
5714 print. */
5715 if (!bs->stop || !bs->print)
5716 bs->print_it = print_it_noop;
5717 }
5718
5719 /* If we aren't stopping, the value of some hardware watchpoint may
5720 not have changed, but the intermediate memory locations we are
5721 watching may have. Don't bother if we're stopping; this will get
5722 done later. */
5723 need_remove_insert = 0;
5724 if (! bpstat_causes_stop (bs_head))
5725 for (bs = bs_head; bs != NULL; bs = bs->next)
5726 if (!bs->stop
5727 && bs->breakpoint_at
5728 && is_hardware_watchpoint (bs->breakpoint_at))
5729 {
5730 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5731
5732 update_watchpoint (w, false /* don't reparse. */);
5733 need_remove_insert = 1;
5734 }
5735
5736 if (need_remove_insert)
5737 update_global_location_list (UGLL_MAY_INSERT);
5738 else if (removed_any)
5739 update_global_location_list (UGLL_DONT_INSERT);
5740
5741 return bs_head;
5742 }
5743
5744 /* See breakpoint.h. */
5745
5746 bpstat *
5747 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5748 thread_info *thread, const target_waitstatus &ws)
5749 {
5750 gdb_assert (!target_stopped_by_watchpoint ());
5751
5752 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5753 previous stop to avoid confusing bpstat_stop_status. */
5754 watchpoints_triggered (ws);
5755
5756 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5757 }
5758
5759 static void
5760 handle_jit_event (CORE_ADDR address)
5761 {
5762 struct gdbarch *gdbarch;
5763
5764 infrun_debug_printf ("handling bp_jit_event");
5765
5766 /* Switch terminal for any messages produced by
5767 breakpoint_re_set. */
5768 target_terminal::ours_for_output ();
5769
5770 gdbarch = get_frame_arch (get_current_frame ());
5771 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5772 thus it is expected that its objectfile can be found through
5773 minimal symbol lookup. If it doesn't work (and assert fails), it
5774 most likely means that `jit_breakpoint_re_set` was changes and this
5775 function needs to be updated too. */
5776 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5777 gdb_assert (jit_bp_sym.objfile != nullptr);
5778 objfile *objfile = jit_bp_sym.objfile;
5779 if (objfile->separate_debug_objfile_backlink)
5780 objfile = objfile->separate_debug_objfile_backlink;
5781 jit_event_handler (gdbarch, objfile);
5782
5783 target_terminal::inferior ();
5784 }
5785
5786 /* Prepare WHAT final decision for infrun. */
5787
5788 /* Decide what infrun needs to do with this bpstat. */
5789
5790 struct bpstat_what
5791 bpstat_what (bpstat *bs_head)
5792 {
5793 struct bpstat_what retval;
5794 bpstat *bs;
5795
5796 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5797 retval.call_dummy = STOP_NONE;
5798 retval.is_longjmp = false;
5799
5800 for (bs = bs_head; bs != NULL; bs = bs->next)
5801 {
5802 /* Extract this BS's action. After processing each BS, we check
5803 if its action overrides all we've seem so far. */
5804 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5805 enum bptype bptype;
5806
5807 if (bs->breakpoint_at == NULL)
5808 {
5809 /* I suspect this can happen if it was a momentary
5810 breakpoint which has since been deleted. */
5811 bptype = bp_none;
5812 }
5813 else
5814 bptype = bs->breakpoint_at->type;
5815
5816 switch (bptype)
5817 {
5818 case bp_none:
5819 break;
5820 case bp_breakpoint:
5821 case bp_hardware_breakpoint:
5822 case bp_single_step:
5823 case bp_until:
5824 case bp_finish:
5825 case bp_shlib_event:
5826 if (bs->stop)
5827 {
5828 if (bs->print)
5829 this_action = BPSTAT_WHAT_STOP_NOISY;
5830 else
5831 this_action = BPSTAT_WHAT_STOP_SILENT;
5832 }
5833 else
5834 this_action = BPSTAT_WHAT_SINGLE;
5835 break;
5836 case bp_watchpoint:
5837 case bp_hardware_watchpoint:
5838 case bp_read_watchpoint:
5839 case bp_access_watchpoint:
5840 if (bs->stop)
5841 {
5842 if (bs->print)
5843 this_action = BPSTAT_WHAT_STOP_NOISY;
5844 else
5845 this_action = BPSTAT_WHAT_STOP_SILENT;
5846 }
5847 else
5848 {
5849 /* There was a watchpoint, but we're not stopping.
5850 This requires no further action. */
5851 }
5852 break;
5853 case bp_longjmp:
5854 case bp_longjmp_call_dummy:
5855 case bp_exception:
5856 if (bs->stop)
5857 {
5858 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5859 retval.is_longjmp = bptype != bp_exception;
5860 }
5861 else
5862 this_action = BPSTAT_WHAT_SINGLE;
5863 break;
5864 case bp_longjmp_resume:
5865 case bp_exception_resume:
5866 if (bs->stop)
5867 {
5868 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5869 retval.is_longjmp = bptype == bp_longjmp_resume;
5870 }
5871 else
5872 this_action = BPSTAT_WHAT_SINGLE;
5873 break;
5874 case bp_step_resume:
5875 if (bs->stop)
5876 this_action = BPSTAT_WHAT_STEP_RESUME;
5877 else
5878 {
5879 /* It is for the wrong frame. */
5880 this_action = BPSTAT_WHAT_SINGLE;
5881 }
5882 break;
5883 case bp_hp_step_resume:
5884 if (bs->stop)
5885 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5886 else
5887 {
5888 /* It is for the wrong frame. */
5889 this_action = BPSTAT_WHAT_SINGLE;
5890 }
5891 break;
5892 case bp_watchpoint_scope:
5893 case bp_thread_event:
5894 case bp_overlay_event:
5895 case bp_longjmp_master:
5896 case bp_std_terminate_master:
5897 case bp_exception_master:
5898 this_action = BPSTAT_WHAT_SINGLE;
5899 break;
5900 case bp_catchpoint:
5901 if (bs->stop)
5902 {
5903 if (bs->print)
5904 this_action = BPSTAT_WHAT_STOP_NOISY;
5905 else
5906 this_action = BPSTAT_WHAT_STOP_SILENT;
5907 }
5908 else
5909 {
5910 /* Some catchpoints are implemented with breakpoints.
5911 For those, we need to step over the breakpoint. */
5912 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5913 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
5914 this_action = BPSTAT_WHAT_SINGLE;
5915 }
5916 break;
5917 case bp_jit_event:
5918 this_action = BPSTAT_WHAT_SINGLE;
5919 break;
5920 case bp_call_dummy:
5921 /* Make sure the action is stop (silent or noisy),
5922 so infrun.c pops the dummy frame. */
5923 retval.call_dummy = STOP_STACK_DUMMY;
5924 this_action = BPSTAT_WHAT_STOP_SILENT;
5925 break;
5926 case bp_std_terminate:
5927 /* Make sure the action is stop (silent or noisy),
5928 so infrun.c pops the dummy frame. */
5929 retval.call_dummy = STOP_STD_TERMINATE;
5930 this_action = BPSTAT_WHAT_STOP_SILENT;
5931 break;
5932 case bp_tracepoint:
5933 case bp_fast_tracepoint:
5934 case bp_static_tracepoint:
5935 case bp_static_marker_tracepoint:
5936 /* Tracepoint hits should not be reported back to GDB, and
5937 if one got through somehow, it should have been filtered
5938 out already. */
5939 internal_error (_("bpstat_what: tracepoint encountered"));
5940 break;
5941 case bp_gnu_ifunc_resolver:
5942 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5943 this_action = BPSTAT_WHAT_SINGLE;
5944 break;
5945 case bp_gnu_ifunc_resolver_return:
5946 /* The breakpoint will be removed, execution will restart from the
5947 PC of the former breakpoint. */
5948 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5949 break;
5950
5951 case bp_dprintf:
5952 if (bs->stop)
5953 this_action = BPSTAT_WHAT_STOP_SILENT;
5954 else
5955 this_action = BPSTAT_WHAT_SINGLE;
5956 break;
5957
5958 default:
5959 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
5960 }
5961
5962 retval.main_action = std::max (retval.main_action, this_action);
5963 }
5964
5965 return retval;
5966 }
5967
5968 void
5969 bpstat_run_callbacks (bpstat *bs_head)
5970 {
5971 bpstat *bs;
5972
5973 for (bs = bs_head; bs != NULL; bs = bs->next)
5974 {
5975 struct breakpoint *b = bs->breakpoint_at;
5976
5977 if (b == NULL)
5978 continue;
5979 switch (b->type)
5980 {
5981 case bp_jit_event:
5982 handle_jit_event (bs->bp_location_at->address);
5983 break;
5984 case bp_gnu_ifunc_resolver:
5985 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
5986 break;
5987 case bp_gnu_ifunc_resolver_return:
5988 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
5989 break;
5990 }
5991 }
5992 }
5993
5994 /* See breakpoint.h. */
5995
5996 bool
5997 bpstat_should_step ()
5998 {
5999 for (breakpoint *b : all_breakpoints ())
6000 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6001 return true;
6002
6003 return false;
6004 }
6005
6006 /* See breakpoint.h. */
6007
6008 bool
6009 bpstat_causes_stop (bpstat *bs)
6010 {
6011 for (; bs != NULL; bs = bs->next)
6012 if (bs->stop)
6013 return true;
6014
6015 return false;
6016 }
6017
6018 \f
6019
6020 /* Compute a number of spaces suitable to indent the next line
6021 so it starts at the position corresponding to the table column
6022 named COL_NAME in the currently active table of UIOUT. */
6023
6024 static int
6025 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6026 {
6027 int i, total_width, width, align;
6028 const char *text;
6029
6030 total_width = 0;
6031 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6032 {
6033 if (strcmp (text, col_name) == 0)
6034 return total_width;
6035
6036 total_width += width + 1;
6037 }
6038
6039 return 0;
6040 }
6041
6042 /* Determine if the locations of this breakpoint will have their conditions
6043 evaluated by the target, host or a mix of both. Returns the following:
6044
6045 "host": Host evals condition.
6046 "host or target": Host or Target evals condition.
6047 "target": Target evals condition.
6048 */
6049
6050 static const char *
6051 bp_condition_evaluator (const breakpoint *b)
6052 {
6053 char host_evals = 0;
6054 char target_evals = 0;
6055
6056 if (!b)
6057 return NULL;
6058
6059 if (!is_breakpoint (b))
6060 return NULL;
6061
6062 if (gdb_evaluates_breakpoint_condition_p ()
6063 || !target_supports_evaluation_of_breakpoint_conditions ())
6064 return condition_evaluation_host;
6065
6066 for (bp_location *bl : b->locations ())
6067 {
6068 if (bl->cond_bytecode)
6069 target_evals++;
6070 else
6071 host_evals++;
6072 }
6073
6074 if (host_evals && target_evals)
6075 return condition_evaluation_both;
6076 else if (target_evals)
6077 return condition_evaluation_target;
6078 else
6079 return condition_evaluation_host;
6080 }
6081
6082 /* Determine the breakpoint location's condition evaluator. This is
6083 similar to bp_condition_evaluator, but for locations. */
6084
6085 static const char *
6086 bp_location_condition_evaluator (const struct bp_location *bl)
6087 {
6088 if (bl && !is_breakpoint (bl->owner))
6089 return NULL;
6090
6091 if (gdb_evaluates_breakpoint_condition_p ()
6092 || !target_supports_evaluation_of_breakpoint_conditions ())
6093 return condition_evaluation_host;
6094
6095 if (bl && bl->cond_bytecode)
6096 return condition_evaluation_target;
6097 else
6098 return condition_evaluation_host;
6099 }
6100
6101 /* Print the LOC location out of the list of B->LOC locations. */
6102
6103 static void
6104 print_breakpoint_location (const breakpoint *b,
6105 struct bp_location *loc)
6106 {
6107 struct ui_out *uiout = current_uiout;
6108
6109 scoped_restore_current_program_space restore_pspace;
6110
6111 if (loc != NULL && loc->shlib_disabled)
6112 loc = NULL;
6113
6114 if (loc != NULL)
6115 set_current_program_space (loc->pspace);
6116
6117 if (b->display_canonical)
6118 uiout->field_string ("what", b->locspec->to_string ());
6119 else if (loc && loc->symtab)
6120 {
6121 const struct symbol *sym = loc->symbol;
6122
6123 if (sym)
6124 {
6125 uiout->text ("in ");
6126 uiout->field_string ("func", sym->print_name (),
6127 function_name_style.style ());
6128 uiout->text (" ");
6129 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6130 uiout->text ("at ");
6131 }
6132 uiout->field_string ("file",
6133 symtab_to_filename_for_display (loc->symtab),
6134 file_name_style.style ());
6135 uiout->text (":");
6136
6137 if (uiout->is_mi_like_p ())
6138 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6139
6140 uiout->field_signed ("line", loc->line_number);
6141 }
6142 else if (loc)
6143 {
6144 string_file stb;
6145
6146 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6147 demangle, "");
6148 uiout->field_stream ("at", stb);
6149 }
6150 else
6151 {
6152 uiout->field_string ("pending", b->locspec->to_string ());
6153 /* If extra_string is available, it could be holding a condition
6154 or dprintf arguments. In either case, make sure it is printed,
6155 too, but only for non-MI streams. */
6156 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6157 {
6158 if (b->type == bp_dprintf)
6159 uiout->text (",");
6160 else
6161 uiout->text (" ");
6162 uiout->text (b->extra_string.get ());
6163 }
6164 }
6165
6166 if (loc && is_breakpoint (b)
6167 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6168 && bp_condition_evaluator (b) == condition_evaluation_both)
6169 {
6170 uiout->text (" (");
6171 uiout->field_string ("evaluated-by",
6172 bp_location_condition_evaluator (loc));
6173 uiout->text (")");
6174 }
6175 }
6176
6177 static const char *
6178 bptype_string (enum bptype type)
6179 {
6180 struct ep_type_description
6181 {
6182 enum bptype type;
6183 const char *description;
6184 };
6185 static struct ep_type_description bptypes[] =
6186 {
6187 {bp_none, "?deleted?"},
6188 {bp_breakpoint, "breakpoint"},
6189 {bp_hardware_breakpoint, "hw breakpoint"},
6190 {bp_single_step, "sw single-step"},
6191 {bp_until, "until"},
6192 {bp_finish, "finish"},
6193 {bp_watchpoint, "watchpoint"},
6194 {bp_hardware_watchpoint, "hw watchpoint"},
6195 {bp_read_watchpoint, "read watchpoint"},
6196 {bp_access_watchpoint, "acc watchpoint"},
6197 {bp_longjmp, "longjmp"},
6198 {bp_longjmp_resume, "longjmp resume"},
6199 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6200 {bp_exception, "exception"},
6201 {bp_exception_resume, "exception resume"},
6202 {bp_step_resume, "step resume"},
6203 {bp_hp_step_resume, "high-priority step resume"},
6204 {bp_watchpoint_scope, "watchpoint scope"},
6205 {bp_call_dummy, "call dummy"},
6206 {bp_std_terminate, "std::terminate"},
6207 {bp_shlib_event, "shlib events"},
6208 {bp_thread_event, "thread events"},
6209 {bp_overlay_event, "overlay events"},
6210 {bp_longjmp_master, "longjmp master"},
6211 {bp_std_terminate_master, "std::terminate master"},
6212 {bp_exception_master, "exception master"},
6213 {bp_catchpoint, "catchpoint"},
6214 {bp_tracepoint, "tracepoint"},
6215 {bp_fast_tracepoint, "fast tracepoint"},
6216 {bp_static_tracepoint, "static tracepoint"},
6217 {bp_static_marker_tracepoint, "static marker tracepoint"},
6218 {bp_dprintf, "dprintf"},
6219 {bp_jit_event, "jit events"},
6220 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6221 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6222 };
6223
6224 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6225 || ((int) type != bptypes[(int) type].type))
6226 internal_error (_("bptypes table does not describe type #%d."),
6227 (int) type);
6228
6229 return bptypes[(int) type].description;
6230 }
6231
6232 /* For MI, output a field named 'thread-groups' with a list as the value.
6233 For CLI, prefix the list with the string 'inf'. */
6234
6235 static void
6236 output_thread_groups (struct ui_out *uiout,
6237 const char *field_name,
6238 const std::vector<int> &inf_nums,
6239 int mi_only)
6240 {
6241 int is_mi = uiout->is_mi_like_p ();
6242
6243 /* For backward compatibility, don't display inferiors in CLI unless
6244 there are several. Always display them for MI. */
6245 if (!is_mi && mi_only)
6246 return;
6247
6248 ui_out_emit_list list_emitter (uiout, field_name);
6249
6250 for (size_t i = 0; i < inf_nums.size (); i++)
6251 {
6252 if (is_mi)
6253 {
6254 char mi_group[10];
6255
6256 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6257 uiout->field_string (NULL, mi_group);
6258 }
6259 else
6260 {
6261 if (i == 0)
6262 uiout->text (" inf ");
6263 else
6264 uiout->text (", ");
6265
6266 uiout->text (plongest (inf_nums[i]));
6267 }
6268 }
6269 }
6270
6271 /* See breakpoint.h. */
6272
6273 bool fix_breakpoint_script_output_globally = false;
6274
6275 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6276 instead of going via breakpoint_ops::print_one. This makes "maint
6277 info breakpoints" show the software breakpoint locations of
6278 catchpoints, which are considered internal implementation
6279 detail. Returns true if RAW_LOC is false and if the breakpoint's
6280 print_one method did something; false otherwise. */
6281
6282 static bool
6283 print_one_breakpoint_location (struct breakpoint *b,
6284 struct bp_location *loc,
6285 int loc_number,
6286 struct bp_location **last_loc,
6287 int allflag, bool raw_loc)
6288 {
6289 struct command_line *l;
6290 static char bpenables[] = "nynny";
6291
6292 struct ui_out *uiout = current_uiout;
6293 bool header_of_multiple = false;
6294 bool part_of_multiple = (loc != NULL);
6295 struct value_print_options opts;
6296
6297 get_user_print_options (&opts);
6298
6299 gdb_assert (!loc || loc_number != 0);
6300 /* See comment in print_one_breakpoint concerning treatment of
6301 breakpoints with single disabled location. */
6302 if (loc == NULL
6303 && (b->loc != NULL
6304 && (b->loc->next != NULL
6305 || !b->loc->enabled || b->loc->disabled_by_cond)))
6306 header_of_multiple = true;
6307 if (loc == NULL)
6308 loc = b->loc;
6309
6310 annotate_record ();
6311
6312 /* 1 */
6313 annotate_field (0);
6314 if (part_of_multiple)
6315 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6316 else
6317 uiout->field_signed ("number", b->number);
6318
6319 /* 2 */
6320 annotate_field (1);
6321 if (part_of_multiple)
6322 uiout->field_skip ("type");
6323 else
6324 uiout->field_string ("type", bptype_string (b->type));
6325
6326 /* 3 */
6327 annotate_field (2);
6328 if (part_of_multiple)
6329 uiout->field_skip ("disp");
6330 else
6331 uiout->field_string ("disp", bpdisp_text (b->disposition));
6332
6333 /* 4 */
6334 annotate_field (3);
6335 if (part_of_multiple)
6336 {
6337 /* For locations that are disabled because of an invalid
6338 condition, display "N*" on the CLI, where "*" refers to a
6339 footnote below the table. For MI, simply display a "N"
6340 without a footnote. On the CLI, for enabled locations whose
6341 breakpoint is disabled, display "y-". */
6342 auto get_enable_state = [uiout, loc] () -> const char *
6343 {
6344 if (uiout->is_mi_like_p ())
6345 {
6346 if (loc->disabled_by_cond)
6347 return "N";
6348 else if (!loc->enabled)
6349 return "n";
6350 else
6351 return "y";
6352 }
6353 else
6354 {
6355 if (loc->disabled_by_cond)
6356 return "N*";
6357 else if (!loc->enabled)
6358 return "n";
6359 else if (!breakpoint_enabled (loc->owner))
6360 return "y-";
6361 else
6362 return "y";
6363 }
6364 };
6365 uiout->field_string ("enabled", get_enable_state ());
6366 }
6367 else
6368 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6369
6370 /* 5 and 6 */
6371 bool result = false;
6372 if (!raw_loc && b->print_one (last_loc))
6373 result = true;
6374 else
6375 {
6376 if (is_watchpoint (b))
6377 {
6378 struct watchpoint *w = (struct watchpoint *) b;
6379
6380 /* Field 4, the address, is omitted (which makes the columns
6381 not line up too nicely with the headers, but the effect
6382 is relatively readable). */
6383 if (opts.addressprint)
6384 uiout->field_skip ("addr");
6385 annotate_field (5);
6386 uiout->field_string ("what", w->exp_string.get ());
6387 }
6388 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6389 || is_ada_exception_catchpoint (b))
6390 {
6391 if (opts.addressprint)
6392 {
6393 annotate_field (4);
6394 if (header_of_multiple)
6395 uiout->field_string ("addr", "<MULTIPLE>",
6396 metadata_style.style ());
6397 else if (b->loc == NULL || loc->shlib_disabled)
6398 uiout->field_string ("addr", "<PENDING>",
6399 metadata_style.style ());
6400 else
6401 uiout->field_core_addr ("addr",
6402 loc->gdbarch, loc->address);
6403 }
6404 annotate_field (5);
6405 if (!header_of_multiple)
6406 print_breakpoint_location (b, loc);
6407 if (b->loc)
6408 *last_loc = b->loc;
6409 }
6410 }
6411
6412 if (loc != NULL && !header_of_multiple)
6413 {
6414 std::vector<int> inf_nums;
6415 int mi_only = 1;
6416
6417 for (inferior *inf : all_inferiors ())
6418 {
6419 if (inf->pspace == loc->pspace)
6420 inf_nums.push_back (inf->num);
6421 }
6422
6423 /* For backward compatibility, don't display inferiors in CLI unless
6424 there are several. Always display for MI. */
6425 if (allflag
6426 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6427 && (program_spaces.size () > 1
6428 || number_of_inferiors () > 1)
6429 /* LOC is for existing B, it cannot be in
6430 moribund_locations and thus having NULL OWNER. */
6431 && loc->owner->type != bp_catchpoint))
6432 mi_only = 0;
6433 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6434 }
6435
6436 if (!part_of_multiple)
6437 {
6438 if (b->thread != -1)
6439 {
6440 /* FIXME: This seems to be redundant and lost here; see the
6441 "stop only in" line a little further down. */
6442 uiout->text (" thread ");
6443 uiout->field_signed ("thread", b->thread);
6444 }
6445 else if (b->task != 0)
6446 {
6447 uiout->text (" task ");
6448 uiout->field_signed ("task", b->task);
6449 }
6450 }
6451
6452 uiout->text ("\n");
6453
6454 if (!part_of_multiple)
6455 b->print_one_detail (uiout);
6456
6457 if (part_of_multiple && frame_id_p (b->frame_id))
6458 {
6459 annotate_field (6);
6460 uiout->text ("\tstop only in stack frame at ");
6461 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6462 the frame ID. */
6463 uiout->field_core_addr ("frame",
6464 b->gdbarch, b->frame_id.stack_addr);
6465 uiout->text ("\n");
6466 }
6467
6468 if (!part_of_multiple && b->cond_string)
6469 {
6470 annotate_field (7);
6471 if (is_tracepoint (b))
6472 uiout->text ("\ttrace only if ");
6473 else
6474 uiout->text ("\tstop only if ");
6475 uiout->field_string ("cond", b->cond_string.get ());
6476
6477 /* Print whether the target is doing the breakpoint's condition
6478 evaluation. If GDB is doing the evaluation, don't print anything. */
6479 if (is_breakpoint (b)
6480 && breakpoint_condition_evaluation_mode ()
6481 == condition_evaluation_target)
6482 {
6483 uiout->message (" (%pF evals)",
6484 string_field ("evaluated-by",
6485 bp_condition_evaluator (b)));
6486 }
6487 uiout->text ("\n");
6488 }
6489
6490 if (!part_of_multiple && b->thread != -1)
6491 {
6492 /* FIXME should make an annotation for this. */
6493 uiout->text ("\tstop only in thread ");
6494 if (uiout->is_mi_like_p ())
6495 uiout->field_signed ("thread", b->thread);
6496 else
6497 {
6498 struct thread_info *thr = find_thread_global_id (b->thread);
6499
6500 uiout->field_string ("thread", print_thread_id (thr));
6501 }
6502 uiout->text ("\n");
6503 }
6504
6505 if (!part_of_multiple)
6506 {
6507 if (b->hit_count)
6508 {
6509 /* FIXME should make an annotation for this. */
6510 if (is_catchpoint (b))
6511 uiout->text ("\tcatchpoint");
6512 else if (is_tracepoint (b))
6513 uiout->text ("\ttracepoint");
6514 else
6515 uiout->text ("\tbreakpoint");
6516 uiout->text (" already hit ");
6517 uiout->field_signed ("times", b->hit_count);
6518 if (b->hit_count == 1)
6519 uiout->text (" time\n");
6520 else
6521 uiout->text (" times\n");
6522 }
6523 else
6524 {
6525 /* Output the count also if it is zero, but only if this is mi. */
6526 if (uiout->is_mi_like_p ())
6527 uiout->field_signed ("times", b->hit_count);
6528 }
6529 }
6530
6531 if (!part_of_multiple && b->ignore_count)
6532 {
6533 annotate_field (8);
6534 uiout->message ("\tignore next %pF hits\n",
6535 signed_field ("ignore", b->ignore_count));
6536 }
6537
6538 /* Note that an enable count of 1 corresponds to "enable once"
6539 behavior, which is reported by the combination of enablement and
6540 disposition, so we don't need to mention it here. */
6541 if (!part_of_multiple && b->enable_count > 1)
6542 {
6543 annotate_field (8);
6544 uiout->text ("\tdisable after ");
6545 /* Tweak the wording to clarify that ignore and enable counts
6546 are distinct, and have additive effect. */
6547 if (b->ignore_count)
6548 uiout->text ("additional ");
6549 else
6550 uiout->text ("next ");
6551 uiout->field_signed ("enable", b->enable_count);
6552 uiout->text (" hits\n");
6553 }
6554
6555 if (!part_of_multiple && is_tracepoint (b))
6556 {
6557 struct tracepoint *tp = (struct tracepoint *) b;
6558
6559 if (tp->traceframe_usage)
6560 {
6561 uiout->text ("\ttrace buffer usage ");
6562 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6563 uiout->text (" bytes\n");
6564 }
6565 }
6566
6567 l = b->commands ? b->commands.get () : NULL;
6568 if (!part_of_multiple && l)
6569 {
6570 annotate_field (9);
6571
6572 bool use_fixed_output =
6573 (uiout->test_flags (fix_breakpoint_script_output)
6574 || fix_breakpoint_script_output_globally);
6575
6576 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6577 gdb::optional<ui_out_emit_list> list_emitter;
6578
6579 if (use_fixed_output)
6580 list_emitter.emplace (uiout, "script");
6581 else
6582 tuple_emitter.emplace (uiout, "script");
6583
6584 print_command_lines (uiout, l, 4);
6585 }
6586
6587 if (is_tracepoint (b))
6588 {
6589 struct tracepoint *t = (struct tracepoint *) b;
6590
6591 if (!part_of_multiple && t->pass_count)
6592 {
6593 annotate_field (10);
6594 uiout->text ("\tpass count ");
6595 uiout->field_signed ("pass", t->pass_count);
6596 uiout->text (" \n");
6597 }
6598
6599 /* Don't display it when tracepoint or tracepoint location is
6600 pending. */
6601 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6602 {
6603 annotate_field (11);
6604
6605 if (uiout->is_mi_like_p ())
6606 uiout->field_string ("installed",
6607 loc->inserted ? "y" : "n");
6608 else
6609 {
6610 if (loc->inserted)
6611 uiout->text ("\t");
6612 else
6613 uiout->text ("\tnot ");
6614 uiout->text ("installed on target\n");
6615 }
6616 }
6617 }
6618
6619 if (uiout->is_mi_like_p () && !part_of_multiple)
6620 {
6621 if (is_watchpoint (b))
6622 {
6623 struct watchpoint *w = (struct watchpoint *) b;
6624
6625 uiout->field_string ("original-location", w->exp_string.get ());
6626 }
6627 else if (b->locspec != nullptr)
6628 {
6629 const char *str = b->locspec->to_string ();
6630 if (str != nullptr)
6631 uiout->field_string ("original-location", str);
6632 }
6633 }
6634
6635 return result;
6636 }
6637
6638 /* See breakpoint.h. */
6639
6640 bool fix_multi_location_breakpoint_output_globally = false;
6641
6642 static void
6643 print_one_breakpoint (struct breakpoint *b,
6644 struct bp_location **last_loc,
6645 int allflag)
6646 {
6647 struct ui_out *uiout = current_uiout;
6648 bool use_fixed_output
6649 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6650 || fix_multi_location_breakpoint_output_globally);
6651
6652 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6653 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6654 allflag, false);
6655
6656 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6657 are outside. */
6658 if (!use_fixed_output)
6659 bkpt_tuple_emitter.reset ();
6660
6661 /* If this breakpoint has custom print function,
6662 it's already printed. Otherwise, print individual
6663 locations, if any. */
6664 if (!printed || allflag)
6665 {
6666 /* If breakpoint has a single location that is disabled, we
6667 print it as if it had several locations, since otherwise it's
6668 hard to represent "breakpoint enabled, location disabled"
6669 situation.
6670
6671 Note that while hardware watchpoints have several locations
6672 internally, that's not a property exposed to users.
6673
6674 Likewise, while catchpoints may be implemented with
6675 breakpoints (e.g., catch throw), that's not a property
6676 exposed to users. We do however display the internal
6677 breakpoint locations with "maint info breakpoints". */
6678 if (!is_hardware_watchpoint (b)
6679 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6680 || is_ada_exception_catchpoint (b))
6681 && (allflag
6682 || (b->loc && (b->loc->next
6683 || !b->loc->enabled
6684 || b->loc->disabled_by_cond))))
6685 {
6686 gdb::optional<ui_out_emit_list> locations_list;
6687
6688 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6689 MI record. For later versions, place breakpoint locations in a
6690 list. */
6691 if (uiout->is_mi_like_p () && use_fixed_output)
6692 locations_list.emplace (uiout, "locations");
6693
6694 int n = 1;
6695 for (bp_location *loc : b->locations ())
6696 {
6697 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6698 print_one_breakpoint_location (b, loc, n, last_loc,
6699 allflag, allflag);
6700 n++;
6701 }
6702 }
6703 }
6704 }
6705
6706 static int
6707 breakpoint_address_bits (struct breakpoint *b)
6708 {
6709 int print_address_bits = 0;
6710
6711 for (bp_location *loc : b->locations ())
6712 {
6713 if (!bl_address_is_meaningful (loc))
6714 continue;
6715
6716 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
6717 if (addr_bit > print_address_bits)
6718 print_address_bits = addr_bit;
6719 }
6720
6721 return print_address_bits;
6722 }
6723
6724 /* See breakpoint.h. */
6725
6726 void
6727 print_breakpoint (breakpoint *b)
6728 {
6729 struct bp_location *dummy_loc = NULL;
6730 print_one_breakpoint (b, &dummy_loc, 0);
6731 }
6732
6733 /* Return true if this breakpoint was set by the user, false if it is
6734 internal or momentary. */
6735
6736 int
6737 user_breakpoint_p (struct breakpoint *b)
6738 {
6739 return b->number > 0;
6740 }
6741
6742 /* See breakpoint.h. */
6743
6744 int
6745 pending_breakpoint_p (struct breakpoint *b)
6746 {
6747 return b->loc == NULL;
6748 }
6749
6750 /* Print information on breakpoints (including watchpoints and tracepoints).
6751
6752 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6753 understood by number_or_range_parser. Only breakpoints included in this
6754 list are then printed.
6755
6756 If SHOW_INTERNAL is true, print internal breakpoints.
6757
6758 If FILTER is non-NULL, call it on each breakpoint and only include the
6759 ones for which it returns true.
6760
6761 Return the total number of breakpoints listed. */
6762
6763 static int
6764 breakpoint_1 (const char *bp_num_list, bool show_internal,
6765 bool (*filter) (const struct breakpoint *))
6766 {
6767 struct bp_location *last_loc = NULL;
6768 int nr_printable_breakpoints;
6769 struct value_print_options opts;
6770 int print_address_bits = 0;
6771 int print_type_col_width = 14;
6772 struct ui_out *uiout = current_uiout;
6773 bool has_disabled_by_cond_location = false;
6774
6775 get_user_print_options (&opts);
6776
6777 /* Compute the number of rows in the table, as well as the size
6778 required for address fields. */
6779 nr_printable_breakpoints = 0;
6780 for (breakpoint *b : all_breakpoints ())
6781 {
6782 /* If we have a filter, only list the breakpoints it accepts. */
6783 if (filter && !filter (b))
6784 continue;
6785
6786 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6787 accept. Skip the others. */
6788 if (bp_num_list != NULL && *bp_num_list != '\0')
6789 {
6790 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6791 continue;
6792 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6793 continue;
6794 }
6795
6796 if (show_internal || user_breakpoint_p (b))
6797 {
6798 int addr_bit, type_len;
6799
6800 addr_bit = breakpoint_address_bits (b);
6801 if (addr_bit > print_address_bits)
6802 print_address_bits = addr_bit;
6803
6804 type_len = strlen (bptype_string (b->type));
6805 if (type_len > print_type_col_width)
6806 print_type_col_width = type_len;
6807
6808 nr_printable_breakpoints++;
6809 }
6810 }
6811
6812 {
6813 ui_out_emit_table table_emitter (uiout,
6814 opts.addressprint ? 6 : 5,
6815 nr_printable_breakpoints,
6816 "BreakpointTable");
6817
6818 if (nr_printable_breakpoints > 0)
6819 annotate_breakpoints_headers ();
6820 if (nr_printable_breakpoints > 0)
6821 annotate_field (0);
6822 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6823 if (nr_printable_breakpoints > 0)
6824 annotate_field (1);
6825 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6826 if (nr_printable_breakpoints > 0)
6827 annotate_field (2);
6828 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6829 if (nr_printable_breakpoints > 0)
6830 annotate_field (3);
6831 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6832 if (opts.addressprint)
6833 {
6834 if (nr_printable_breakpoints > 0)
6835 annotate_field (4);
6836 if (print_address_bits <= 32)
6837 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6838 else
6839 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6840 }
6841 if (nr_printable_breakpoints > 0)
6842 annotate_field (5);
6843 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6844 uiout->table_body ();
6845 if (nr_printable_breakpoints > 0)
6846 annotate_breakpoints_table ();
6847
6848 for (breakpoint *b : all_breakpoints ())
6849 {
6850 QUIT;
6851 /* If we have a filter, only list the breakpoints it accepts. */
6852 if (filter && !filter (b))
6853 continue;
6854
6855 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6856 accept. Skip the others. */
6857
6858 if (bp_num_list != NULL && *bp_num_list != '\0')
6859 {
6860 if (show_internal) /* maintenance info breakpoint */
6861 {
6862 if (parse_and_eval_long (bp_num_list) != b->number)
6863 continue;
6864 }
6865 else /* all others */
6866 {
6867 if (!number_is_in_list (bp_num_list, b->number))
6868 continue;
6869 }
6870 }
6871 /* We only print out user settable breakpoints unless the
6872 show_internal is set. */
6873 if (show_internal || user_breakpoint_p (b))
6874 {
6875 print_one_breakpoint (b, &last_loc, show_internal);
6876 for (bp_location *loc : b->locations ())
6877 if (loc->disabled_by_cond)
6878 has_disabled_by_cond_location = true;
6879 }
6880 }
6881 }
6882
6883 if (nr_printable_breakpoints == 0)
6884 {
6885 /* If there's a filter, let the caller decide how to report
6886 empty list. */
6887 if (!filter)
6888 {
6889 if (bp_num_list == NULL || *bp_num_list == '\0')
6890 uiout->message ("No breakpoints or watchpoints.\n");
6891 else
6892 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6893 bp_num_list);
6894 }
6895 }
6896 else
6897 {
6898 if (last_loc && !server_command)
6899 set_next_address (last_loc->gdbarch, last_loc->address);
6900
6901 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6902 uiout->message (_("(*): Breakpoint condition is invalid at this "
6903 "location.\n"));
6904 }
6905
6906 /* FIXME? Should this be moved up so that it is only called when
6907 there have been breakpoints? */
6908 annotate_breakpoints_table_end ();
6909
6910 return nr_printable_breakpoints;
6911 }
6912
6913 /* Display the value of default-collect in a way that is generally
6914 compatible with the breakpoint list. */
6915
6916 static void
6917 default_collect_info (void)
6918 {
6919 struct ui_out *uiout = current_uiout;
6920
6921 /* If it has no value (which is frequently the case), say nothing; a
6922 message like "No default-collect." gets in user's face when it's
6923 not wanted. */
6924 if (default_collect.empty ())
6925 return;
6926
6927 /* The following phrase lines up nicely with per-tracepoint collect
6928 actions. */
6929 uiout->text ("default collect ");
6930 uiout->field_string ("default-collect", default_collect);
6931 uiout->text (" \n");
6932 }
6933
6934 static void
6935 info_breakpoints_command (const char *args, int from_tty)
6936 {
6937 breakpoint_1 (args, false, NULL);
6938
6939 default_collect_info ();
6940 }
6941
6942 static void
6943 info_watchpoints_command (const char *args, int from_tty)
6944 {
6945 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6946 struct ui_out *uiout = current_uiout;
6947
6948 if (num_printed == 0)
6949 {
6950 if (args == NULL || *args == '\0')
6951 uiout->message ("No watchpoints.\n");
6952 else
6953 uiout->message ("No watchpoint matching '%s'.\n", args);
6954 }
6955 }
6956
6957 static void
6958 maintenance_info_breakpoints (const char *args, int from_tty)
6959 {
6960 breakpoint_1 (args, true, NULL);
6961
6962 default_collect_info ();
6963 }
6964
6965 static bool
6966 breakpoint_has_pc (struct breakpoint *b,
6967 struct program_space *pspace,
6968 CORE_ADDR pc, struct obj_section *section)
6969 {
6970 for (bp_location *bl : b->locations ())
6971 {
6972 if (bl->pspace == pspace
6973 && bl->address == pc
6974 && (!overlay_debugging || bl->section == section))
6975 return true;
6976 }
6977 return false;
6978 }
6979
6980 /* See breakpoint.h. */
6981
6982 void
6983 describe_other_breakpoints (struct gdbarch *gdbarch,
6984 struct program_space *pspace, CORE_ADDR pc,
6985 struct obj_section *section, int thread)
6986 {
6987 int others = 0;
6988
6989 for (breakpoint *b : all_breakpoints ())
6990 others += (user_breakpoint_p (b)
6991 && breakpoint_has_pc (b, pspace, pc, section));
6992
6993 if (others > 0)
6994 {
6995 if (others == 1)
6996 gdb_printf (_("Note: breakpoint "));
6997 else /* if (others == ???) */
6998 gdb_printf (_("Note: breakpoints "));
6999 for (breakpoint *b : all_breakpoints ())
7000 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7001 {
7002 others--;
7003 gdb_printf ("%d", b->number);
7004 if (b->thread == -1 && thread != -1)
7005 gdb_printf (" (all threads)");
7006 else if (b->thread != -1)
7007 gdb_printf (" (thread %d)", b->thread);
7008 gdb_printf ("%s%s ",
7009 ((b->enable_state == bp_disabled
7010 || b->enable_state == bp_call_disabled)
7011 ? " (disabled)"
7012 : ""),
7013 (others > 1) ? ","
7014 : ((others == 1) ? " and" : ""));
7015 }
7016 current_uiout->message (_("also set at pc %ps.\n"),
7017 styled_string (address_style.style (),
7018 paddress (gdbarch, pc)));
7019 }
7020 }
7021 \f
7022
7023 /* Return true iff it is meaningful to use the address member of LOC.
7024 For some breakpoint types, the locations' address members are
7025 irrelevant and it makes no sense to attempt to compare them to
7026 other addresses (or use them for any other purpose either).
7027
7028 More specifically, software watchpoints and catchpoints that are
7029 not backed by breakpoints always have a zero valued location
7030 address and we don't want to mark breakpoints of any of these types
7031 to be a duplicate of an actual breakpoint location at address
7032 zero. */
7033
7034 static bool
7035 bl_address_is_meaningful (bp_location *loc)
7036 {
7037 return loc->loc_type != bp_loc_other;
7038 }
7039
7040 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7041 true if LOC1 and LOC2 represent the same watchpoint location. */
7042
7043 static bool
7044 watchpoint_locations_match (const struct bp_location *loc1,
7045 const struct bp_location *loc2)
7046 {
7047 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7048 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7049
7050 /* Both of them must exist. */
7051 gdb_assert (w1 != NULL);
7052 gdb_assert (w2 != NULL);
7053
7054 /* If the target can evaluate the condition expression in hardware,
7055 then we we need to insert both watchpoints even if they are at
7056 the same place. Otherwise the watchpoint will only trigger when
7057 the condition of whichever watchpoint was inserted evaluates to
7058 true, not giving a chance for GDB to check the condition of the
7059 other watchpoint. */
7060 if ((w1->cond_exp
7061 && target_can_accel_watchpoint_condition (loc1->address,
7062 loc1->length,
7063 loc1->watchpoint_type,
7064 w1->cond_exp.get ()))
7065 || (w2->cond_exp
7066 && target_can_accel_watchpoint_condition (loc2->address,
7067 loc2->length,
7068 loc2->watchpoint_type,
7069 w2->cond_exp.get ())))
7070 return false;
7071
7072 /* Note that this checks the owner's type, not the location's. In
7073 case the target does not support read watchpoints, but does
7074 support access watchpoints, we'll have bp_read_watchpoint
7075 watchpoints with hw_access locations. Those should be considered
7076 duplicates of hw_read locations. The hw_read locations will
7077 become hw_access locations later. */
7078 return (loc1->owner->type == loc2->owner->type
7079 && loc1->pspace->aspace == loc2->pspace->aspace
7080 && loc1->address == loc2->address
7081 && loc1->length == loc2->length);
7082 }
7083
7084 /* See breakpoint.h. */
7085
7086 int
7087 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7088 const address_space *aspace2, CORE_ADDR addr2)
7089 {
7090 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7091 || aspace1 == aspace2)
7092 && addr1 == addr2);
7093 }
7094
7095 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7096 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7097 matches ASPACE2. On targets that have global breakpoints, the address
7098 space doesn't really matter. */
7099
7100 static bool
7101 breakpoint_address_match_range (const address_space *aspace1,
7102 CORE_ADDR addr1,
7103 int len1, const address_space *aspace2,
7104 CORE_ADDR addr2)
7105 {
7106 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7107 || aspace1 == aspace2)
7108 && addr2 >= addr1 && addr2 < addr1 + len1);
7109 }
7110
7111 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7112 a ranged breakpoint. In most targets, a match happens only if ASPACE
7113 matches the breakpoint's address space. On targets that have global
7114 breakpoints, the address space doesn't really matter. */
7115
7116 static bool
7117 breakpoint_location_address_match (struct bp_location *bl,
7118 const address_space *aspace,
7119 CORE_ADDR addr)
7120 {
7121 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7122 aspace, addr)
7123 || (bl->length
7124 && breakpoint_address_match_range (bl->pspace->aspace,
7125 bl->address, bl->length,
7126 aspace, addr)));
7127 }
7128
7129 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7130 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7131 match happens only if ASPACE matches the breakpoint's address
7132 space. On targets that have global breakpoints, the address space
7133 doesn't really matter. */
7134
7135 static bool
7136 breakpoint_location_address_range_overlap (struct bp_location *bl,
7137 const address_space *aspace,
7138 CORE_ADDR addr, int len)
7139 {
7140 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7141 || bl->pspace->aspace == aspace)
7142 {
7143 int bl_len = bl->length != 0 ? bl->length : 1;
7144
7145 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7146 return 1;
7147 }
7148 return 0;
7149 }
7150
7151 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7152 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7153 true, otherwise returns false. */
7154
7155 static bool
7156 tracepoint_locations_match (const struct bp_location *loc1,
7157 const struct bp_location *loc2)
7158 {
7159 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7160 /* Since tracepoint locations are never duplicated with others', tracepoint
7161 locations at the same address of different tracepoints are regarded as
7162 different locations. */
7163 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7164 else
7165 return false;
7166 }
7167
7168 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7169 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7170 the same location. If SW_HW_BPS_MATCH is true, then software
7171 breakpoint locations and hardware breakpoint locations match,
7172 otherwise they don't. */
7173
7174 static bool
7175 breakpoint_locations_match (const struct bp_location *loc1,
7176 const struct bp_location *loc2,
7177 bool sw_hw_bps_match)
7178 {
7179 int hw_point1, hw_point2;
7180
7181 /* Both of them must not be in moribund_locations. */
7182 gdb_assert (loc1->owner != NULL);
7183 gdb_assert (loc2->owner != NULL);
7184
7185 hw_point1 = is_hardware_watchpoint (loc1->owner);
7186 hw_point2 = is_hardware_watchpoint (loc2->owner);
7187
7188 if (hw_point1 != hw_point2)
7189 return false;
7190 else if (hw_point1)
7191 return watchpoint_locations_match (loc1, loc2);
7192 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7193 return tracepoint_locations_match (loc1, loc2);
7194 else
7195 /* We compare bp_location.length in order to cover ranged
7196 breakpoints. Keep this in sync with
7197 bp_location_is_less_than. */
7198 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7199 loc2->pspace->aspace, loc2->address)
7200 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7201 && loc1->length == loc2->length);
7202 }
7203
7204 static void
7205 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7206 int bnum, bool have_bnum)
7207 {
7208 /* The longest string possibly returned by hex_string_custom
7209 is 50 chars. These must be at least that big for safety. */
7210 char astr1[64];
7211 char astr2[64];
7212
7213 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7214 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7215 if (have_bnum)
7216 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7217 bnum, astr1, astr2);
7218 else
7219 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7220 }
7221
7222 /* Adjust a breakpoint's address to account for architectural
7223 constraints on breakpoint placement. Return the adjusted address.
7224 Note: Very few targets require this kind of adjustment. For most
7225 targets, this function is simply the identity function. */
7226
7227 static CORE_ADDR
7228 adjust_breakpoint_address (struct gdbarch *gdbarch,
7229 CORE_ADDR bpaddr, enum bptype bptype,
7230 struct program_space *pspace)
7231 {
7232 gdb_assert (pspace != nullptr);
7233
7234 if (bptype == bp_watchpoint
7235 || bptype == bp_hardware_watchpoint
7236 || bptype == bp_read_watchpoint
7237 || bptype == bp_access_watchpoint
7238 || bptype == bp_catchpoint)
7239 {
7240 /* Watchpoints and the various bp_catch_* eventpoints should not
7241 have their addresses modified. */
7242 return bpaddr;
7243 }
7244 else if (bptype == bp_single_step)
7245 {
7246 /* Single-step breakpoints should not have their addresses
7247 modified. If there's any architectural constrain that
7248 applies to this address, then it should have already been
7249 taken into account when the breakpoint was created in the
7250 first place. If we didn't do this, stepping through e.g.,
7251 Thumb-2 IT blocks would break. */
7252 return bpaddr;
7253 }
7254 else
7255 {
7256 CORE_ADDR adjusted_bpaddr = bpaddr;
7257
7258 /* Some targets have architectural constraints on the placement
7259 of breakpoint instructions. Obtain the adjusted address. */
7260 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7261 {
7262 /* Targets that implement this adjustment function will likely
7263 inspect either the symbol table, target memory at BPADDR, or
7264 even state registers, so ensure a suitable thread (and its
7265 associated program space) are currently selected. */
7266 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7267 switch_to_program_space_and_thread (pspace);
7268 adjusted_bpaddr
7269 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7270 }
7271
7272 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7273
7274 /* An adjusted breakpoint address can significantly alter
7275 a user's expectations. Print a warning if an adjustment
7276 is required. */
7277 if (adjusted_bpaddr != bpaddr)
7278 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
7279
7280 return adjusted_bpaddr;
7281 }
7282 }
7283
7284 static bp_loc_type
7285 bp_location_from_bp_type (bptype type)
7286 {
7287 switch (type)
7288 {
7289 case bp_breakpoint:
7290 case bp_single_step:
7291 case bp_until:
7292 case bp_finish:
7293 case bp_longjmp:
7294 case bp_longjmp_resume:
7295 case bp_longjmp_call_dummy:
7296 case bp_exception:
7297 case bp_exception_resume:
7298 case bp_step_resume:
7299 case bp_hp_step_resume:
7300 case bp_watchpoint_scope:
7301 case bp_call_dummy:
7302 case bp_std_terminate:
7303 case bp_shlib_event:
7304 case bp_thread_event:
7305 case bp_overlay_event:
7306 case bp_jit_event:
7307 case bp_longjmp_master:
7308 case bp_std_terminate_master:
7309 case bp_exception_master:
7310 case bp_gnu_ifunc_resolver:
7311 case bp_gnu_ifunc_resolver_return:
7312 case bp_dprintf:
7313 return bp_loc_software_breakpoint;
7314 case bp_hardware_breakpoint:
7315 return bp_loc_hardware_breakpoint;
7316 case bp_hardware_watchpoint:
7317 case bp_read_watchpoint:
7318 case bp_access_watchpoint:
7319 return bp_loc_hardware_watchpoint;
7320 case bp_watchpoint:
7321 return bp_loc_software_watchpoint;
7322 case bp_catchpoint:
7323 case bp_tracepoint:
7324 case bp_fast_tracepoint:
7325 case bp_static_tracepoint:
7326 case bp_static_marker_tracepoint:
7327 return bp_loc_other;
7328 default:
7329 internal_error (_("unknown breakpoint type"));
7330 }
7331 }
7332
7333 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7334 {
7335 this->owner = owner;
7336 this->cond_bytecode = NULL;
7337 this->shlib_disabled = 0;
7338 this->enabled = 1;
7339 this->disabled_by_cond = false;
7340
7341 this->loc_type = type;
7342
7343 if (this->loc_type == bp_loc_software_breakpoint
7344 || this->loc_type == bp_loc_hardware_breakpoint)
7345 mark_breakpoint_location_modified (this);
7346
7347 incref ();
7348 }
7349
7350 bp_location::bp_location (breakpoint *owner)
7351 : bp_location::bp_location (owner,
7352 bp_location_from_bp_type (owner->type))
7353 {
7354 }
7355
7356 /* Decrement reference count. If the reference count reaches 0,
7357 destroy the bp_location. Sets *BLP to NULL. */
7358
7359 static void
7360 decref_bp_location (struct bp_location **blp)
7361 {
7362 bp_location_ref_policy::decref (*blp);
7363 *blp = NULL;
7364 }
7365
7366 /* Add breakpoint B at the end of the global breakpoint chain. */
7367
7368 static breakpoint *
7369 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7370 {
7371 struct breakpoint *b1;
7372 struct breakpoint *result = b.get ();
7373
7374 /* Add this breakpoint to the end of the chain so that a list of
7375 breakpoints will come out in order of increasing numbers. */
7376
7377 b1 = breakpoint_chain;
7378 if (b1 == 0)
7379 breakpoint_chain = b.release ();
7380 else
7381 {
7382 while (b1->next)
7383 b1 = b1->next;
7384 b1->next = b.release ();
7385 }
7386
7387 return result;
7388 }
7389
7390 /* Initialize loc->function_name. */
7391
7392 static void
7393 set_breakpoint_location_function (struct bp_location *loc)
7394 {
7395 gdb_assert (loc->owner != NULL);
7396
7397 if (loc->owner->type == bp_breakpoint
7398 || loc->owner->type == bp_hardware_breakpoint
7399 || is_tracepoint (loc->owner))
7400 {
7401 const char *function_name;
7402
7403 if (loc->msymbol != NULL
7404 && (loc->msymbol->type () == mst_text_gnu_ifunc
7405 || loc->msymbol->type () == mst_data_gnu_ifunc))
7406 {
7407 struct breakpoint *b = loc->owner;
7408
7409 function_name = loc->msymbol->linkage_name ();
7410
7411 if (b->type == bp_breakpoint && b->loc == loc
7412 && loc->next == NULL && b->related_breakpoint == b)
7413 {
7414 /* Create only the whole new breakpoint of this type but do not
7415 mess more complicated breakpoints with multiple locations. */
7416 b->type = bp_gnu_ifunc_resolver;
7417 /* Remember the resolver's address for use by the return
7418 breakpoint. */
7419 loc->related_address = loc->address;
7420 }
7421 }
7422 else
7423 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7424
7425 if (function_name)
7426 loc->function_name = make_unique_xstrdup (function_name);
7427 }
7428 }
7429
7430 /* Attempt to determine architecture of location identified by SAL. */
7431 struct gdbarch *
7432 get_sal_arch (struct symtab_and_line sal)
7433 {
7434 if (sal.section)
7435 return sal.section->objfile->arch ();
7436 if (sal.symtab)
7437 return sal.symtab->compunit ()->objfile ()->arch ();
7438
7439 return NULL;
7440 }
7441
7442 /* Call this routine when stepping and nexting to enable a breakpoint
7443 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7444 initiated the operation. */
7445
7446 void
7447 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7448 {
7449 int thread = tp->global_num;
7450
7451 /* To avoid having to rescan all objfile symbols at every step,
7452 we maintain a list of continually-inserted but always disabled
7453 longjmp "master" breakpoints. Here, we simply create momentary
7454 clones of those and enable them for the requested thread. */
7455 for (breakpoint *b : all_breakpoints_safe ())
7456 if (b->pspace == current_program_space
7457 && (b->type == bp_longjmp_master
7458 || b->type == bp_exception_master))
7459 {
7460 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7461 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7462 after their removal. */
7463 momentary_breakpoint_from_master (b, type, 1, thread);
7464 }
7465
7466 tp->initiating_frame = frame;
7467 }
7468
7469 /* Delete all longjmp breakpoints from THREAD. */
7470 void
7471 delete_longjmp_breakpoint (int thread)
7472 {
7473 for (breakpoint *b : all_breakpoints_safe ())
7474 if (b->type == bp_longjmp || b->type == bp_exception)
7475 {
7476 if (b->thread == thread)
7477 delete_breakpoint (b);
7478 }
7479 }
7480
7481 void
7482 delete_longjmp_breakpoint_at_next_stop (int thread)
7483 {
7484 for (breakpoint *b : all_breakpoints_safe ())
7485 if (b->type == bp_longjmp || b->type == bp_exception)
7486 {
7487 if (b->thread == thread)
7488 b->disposition = disp_del_at_next_stop;
7489 }
7490 }
7491
7492 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7493 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7494 pointer to any of them. Return NULL if this system cannot place longjmp
7495 breakpoints. */
7496
7497 struct breakpoint *
7498 set_longjmp_breakpoint_for_call_dummy (void)
7499 {
7500 breakpoint *retval = nullptr;
7501
7502 for (breakpoint *b : all_breakpoints ())
7503 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7504 {
7505 int thread = inferior_thread ()->global_num;
7506 breakpoint *new_b
7507 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7508 1, thread);
7509
7510 /* Link NEW_B into the chain of RETVAL breakpoints. */
7511
7512 gdb_assert (new_b->related_breakpoint == new_b);
7513 if (retval == NULL)
7514 retval = new_b;
7515 new_b->related_breakpoint = retval;
7516 while (retval->related_breakpoint != new_b->related_breakpoint)
7517 retval = retval->related_breakpoint;
7518 retval->related_breakpoint = new_b;
7519 }
7520
7521 return retval;
7522 }
7523
7524 /* Verify all existing dummy frames and their associated breakpoints for
7525 TP. Remove those which can no longer be found in the current frame
7526 stack.
7527
7528 If the unwind fails then there is not sufficient information to discard
7529 dummy frames. In this case, elide the clean up and the dummy frames will
7530 be cleaned up next time this function is called from a location where
7531 unwinding is possible. */
7532
7533 void
7534 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7535 {
7536 struct breakpoint *b, *b_tmp;
7537
7538 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7539 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7540 {
7541 struct breakpoint *dummy_b = b->related_breakpoint;
7542
7543 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7544 chained off b->related_breakpoint. */
7545 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7546 dummy_b = dummy_b->related_breakpoint;
7547
7548 /* If there was no bp_call_dummy breakpoint then there's nothing
7549 more to do. Or, if the dummy frame associated with the
7550 bp_call_dummy is still on the stack then we need to leave this
7551 bp_call_dummy in place. */
7552 if (dummy_b->type != bp_call_dummy
7553 || frame_find_by_id (dummy_b->frame_id) != NULL)
7554 continue;
7555
7556 /* We didn't find the dummy frame on the stack, this could be
7557 because we have longjmp'd to a stack frame that is previous to
7558 the dummy frame, or it could be because the stack unwind is
7559 broken at some point between the longjmp frame and the dummy
7560 frame.
7561
7562 Next we figure out why the stack unwind stopped. If it looks
7563 like the unwind is complete then we assume the dummy frame has
7564 been jumped over, however, if the unwind stopped for an
7565 unexpected reason then we assume the stack unwind is currently
7566 broken, and that we will (eventually) return to the dummy
7567 frame.
7568
7569 It might be tempting to consider using frame_id_inner here, but
7570 that is not safe. There is no guarantee that the stack frames
7571 we are looking at here are even on the same stack as the
7572 original dummy frame, hence frame_id_inner can't be used. See
7573 the comments on frame_id_inner for more details. */
7574 bool unwind_finished_unexpectedly = false;
7575 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
7576 {
7577 frame_info_ptr prev = get_prev_frame (fi);
7578 if (prev == nullptr)
7579 {
7580 /* FI is the last stack frame. Why did this frame not
7581 unwind further? */
7582 auto stop_reason = get_frame_unwind_stop_reason (fi);
7583 if (stop_reason != UNWIND_NO_REASON
7584 && stop_reason != UNWIND_OUTERMOST)
7585 unwind_finished_unexpectedly = true;
7586 }
7587 fi = prev;
7588 }
7589 if (unwind_finished_unexpectedly)
7590 continue;
7591
7592 dummy_frame_discard (dummy_b->frame_id, tp);
7593
7594 while (b->related_breakpoint != b)
7595 {
7596 if (b_tmp == b->related_breakpoint)
7597 b_tmp = b->related_breakpoint->next;
7598 delete_breakpoint (b->related_breakpoint);
7599 }
7600 delete_breakpoint (b);
7601 }
7602 }
7603
7604 void
7605 enable_overlay_breakpoints (void)
7606 {
7607 for (breakpoint *b : all_breakpoints ())
7608 if (b->type == bp_overlay_event)
7609 {
7610 b->enable_state = bp_enabled;
7611 update_global_location_list (UGLL_MAY_INSERT);
7612 overlay_events_enabled = 1;
7613 }
7614 }
7615
7616 void
7617 disable_overlay_breakpoints (void)
7618 {
7619 for (breakpoint *b : all_breakpoints ())
7620 if (b->type == bp_overlay_event)
7621 {
7622 b->enable_state = bp_disabled;
7623 update_global_location_list (UGLL_DONT_INSERT);
7624 overlay_events_enabled = 0;
7625 }
7626 }
7627
7628 /* Set an active std::terminate breakpoint for each std::terminate
7629 master breakpoint. */
7630 void
7631 set_std_terminate_breakpoint (void)
7632 {
7633 for (breakpoint *b : all_breakpoints_safe ())
7634 if (b->pspace == current_program_space
7635 && b->type == bp_std_terminate_master)
7636 {
7637 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7638 inferior_thread ()->global_num);
7639 }
7640 }
7641
7642 /* Delete all the std::terminate breakpoints. */
7643 void
7644 delete_std_terminate_breakpoint (void)
7645 {
7646 for (breakpoint *b : all_breakpoints_safe ())
7647 if (b->type == bp_std_terminate)
7648 delete_breakpoint (b);
7649 }
7650
7651 struct breakpoint *
7652 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7653 {
7654 struct breakpoint *b;
7655
7656 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7657
7658 b->enable_state = bp_enabled;
7659 /* locspec has to be used or breakpoint_re_set will delete me. */
7660 b->locspec = new_address_location_spec (b->loc->address, NULL, 0);
7661
7662 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7663
7664 return b;
7665 }
7666
7667 struct lang_and_radix
7668 {
7669 enum language lang;
7670 int radix;
7671 };
7672
7673 /* Create a breakpoint for JIT code registration and unregistration. */
7674
7675 struct breakpoint *
7676 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7677 {
7678 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7679 }
7680
7681 /* Remove JIT code registration and unregistration breakpoint(s). */
7682
7683 void
7684 remove_jit_event_breakpoints (void)
7685 {
7686 for (breakpoint *b : all_breakpoints_safe ())
7687 if (b->type == bp_jit_event
7688 && b->loc->pspace == current_program_space)
7689 delete_breakpoint (b);
7690 }
7691
7692 void
7693 remove_solib_event_breakpoints (void)
7694 {
7695 for (breakpoint *b : all_breakpoints_safe ())
7696 if (b->type == bp_shlib_event
7697 && b->loc->pspace == current_program_space)
7698 delete_breakpoint (b);
7699 }
7700
7701 /* See breakpoint.h. */
7702
7703 void
7704 remove_solib_event_breakpoints_at_next_stop (void)
7705 {
7706 for (breakpoint *b : all_breakpoints_safe ())
7707 if (b->type == bp_shlib_event
7708 && b->loc->pspace == current_program_space)
7709 b->disposition = disp_del_at_next_stop;
7710 }
7711
7712 /* Helper for create_solib_event_breakpoint /
7713 create_and_insert_solib_event_breakpoint. Allows specifying which
7714 INSERT_MODE to pass through to update_global_location_list. */
7715
7716 static struct breakpoint *
7717 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7718 enum ugll_insert_mode insert_mode)
7719 {
7720 struct breakpoint *b;
7721
7722 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
7723 update_global_location_list_nothrow (insert_mode);
7724 return b;
7725 }
7726
7727 struct breakpoint *
7728 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7729 {
7730 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7731 }
7732
7733 /* See breakpoint.h. */
7734
7735 struct breakpoint *
7736 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7737 {
7738 struct breakpoint *b;
7739
7740 /* Explicitly tell update_global_location_list to insert
7741 locations. */
7742 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7743 if (!b->loc->inserted)
7744 {
7745 delete_breakpoint (b);
7746 return NULL;
7747 }
7748 return b;
7749 }
7750
7751 /* Disable any breakpoints that are on code in shared libraries. Only
7752 apply to enabled breakpoints, disabled ones can just stay disabled. */
7753
7754 void
7755 disable_breakpoints_in_shlibs (void)
7756 {
7757 for (bp_location *loc : all_bp_locations ())
7758 {
7759 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7760 struct breakpoint *b = loc->owner;
7761
7762 /* We apply the check to all breakpoints, including disabled for
7763 those with loc->duplicate set. This is so that when breakpoint
7764 becomes enabled, or the duplicate is removed, gdb will try to
7765 insert all breakpoints. If we don't set shlib_disabled here,
7766 we'll try to insert those breakpoints and fail. */
7767 if (((b->type == bp_breakpoint)
7768 || (b->type == bp_jit_event)
7769 || (b->type == bp_hardware_breakpoint)
7770 || (is_tracepoint (b)))
7771 && loc->pspace == current_program_space
7772 && !loc->shlib_disabled
7773 && solib_name_from_address (loc->pspace, loc->address)
7774 )
7775 {
7776 loc->shlib_disabled = 1;
7777 }
7778 }
7779 }
7780
7781 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7782 notification of unloaded_shlib. Only apply to enabled breakpoints,
7783 disabled ones can just stay disabled. */
7784
7785 static void
7786 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7787 {
7788 bool disabled_shlib_breaks = false;
7789
7790 for (bp_location *loc : all_bp_locations ())
7791 {
7792 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7793 struct breakpoint *b = loc->owner;
7794
7795 if (solib->pspace == loc->pspace
7796 && !loc->shlib_disabled
7797 && (((b->type == bp_breakpoint
7798 || b->type == bp_jit_event
7799 || b->type == bp_hardware_breakpoint)
7800 && (loc->loc_type == bp_loc_hardware_breakpoint
7801 || loc->loc_type == bp_loc_software_breakpoint))
7802 || is_tracepoint (b))
7803 && solib_contains_address_p (solib, loc->address))
7804 {
7805 loc->shlib_disabled = 1;
7806 /* At this point, we cannot rely on remove_breakpoint
7807 succeeding so we must mark the breakpoint as not inserted
7808 to prevent future errors occurring in remove_breakpoints. */
7809 loc->inserted = 0;
7810
7811 /* This may cause duplicate notifications for the same breakpoint. */
7812 gdb::observers::breakpoint_modified.notify (b);
7813
7814 if (!disabled_shlib_breaks)
7815 {
7816 target_terminal::ours_for_output ();
7817 warning (_("Temporarily disabling breakpoints "
7818 "for unloaded shared library \"%s\""),
7819 solib->so_name);
7820 }
7821 disabled_shlib_breaks = true;
7822 }
7823 }
7824 }
7825
7826 /* Disable any breakpoints and tracepoints in OBJFILE upon
7827 notification of free_objfile. Only apply to enabled breakpoints,
7828 disabled ones can just stay disabled. */
7829
7830 static void
7831 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7832 {
7833 if (objfile == NULL)
7834 return;
7835
7836 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7837 managed by the user with add-symbol-file/remove-symbol-file.
7838 Similarly to how breakpoints in shared libraries are handled in
7839 response to "nosharedlibrary", mark breakpoints in such modules
7840 shlib_disabled so they end up uninserted on the next global
7841 location list update. Shared libraries not loaded by the user
7842 aren't handled here -- they're already handled in
7843 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7844 solib_unloaded observer. We skip objfiles that are not
7845 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7846 main objfile). */
7847 if ((objfile->flags & OBJF_SHARED) == 0
7848 || (objfile->flags & OBJF_USERLOADED) == 0)
7849 return;
7850
7851 for (breakpoint *b : all_breakpoints ())
7852 {
7853 bool bp_modified = false;
7854
7855 if (!is_breakpoint (b) && !is_tracepoint (b))
7856 continue;
7857
7858 for (bp_location *loc : b->locations ())
7859 {
7860 CORE_ADDR loc_addr = loc->address;
7861
7862 if (loc->loc_type != bp_loc_hardware_breakpoint
7863 && loc->loc_type != bp_loc_software_breakpoint)
7864 continue;
7865
7866 if (loc->shlib_disabled != 0)
7867 continue;
7868
7869 if (objfile->pspace != loc->pspace)
7870 continue;
7871
7872 if (loc->loc_type != bp_loc_hardware_breakpoint
7873 && loc->loc_type != bp_loc_software_breakpoint)
7874 continue;
7875
7876 if (is_addr_in_objfile (loc_addr, objfile))
7877 {
7878 loc->shlib_disabled = 1;
7879 /* At this point, we don't know whether the object was
7880 unmapped from the inferior or not, so leave the
7881 inserted flag alone. We'll handle failure to
7882 uninsert quietly, in case the object was indeed
7883 unmapped. */
7884
7885 mark_breakpoint_location_modified (loc);
7886
7887 bp_modified = true;
7888 }
7889 }
7890
7891 if (bp_modified)
7892 gdb::observers::breakpoint_modified.notify (b);
7893 }
7894 }
7895
7896 /* See breakpoint.h. */
7897
7898 breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7899 bool temp, const char *cond_string_)
7900 : type (bptype),
7901 disposition (temp ? disp_del : disp_donttouch),
7902 gdbarch (gdbarch_),
7903 language (current_language->la_language),
7904 input_radix (::input_radix),
7905 cond_string (cond_string_ != nullptr
7906 ? make_unique_xstrdup (cond_string_)
7907 : nullptr),
7908 related_breakpoint (this)
7909 {
7910 }
7911
7912 /* See breakpoint.h. */
7913
7914 catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
7915 const char *cond_string)
7916 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
7917 {
7918 add_dummy_location (this, current_program_space);
7919
7920 pspace = current_program_space;
7921 }
7922
7923 void
7924 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
7925 {
7926 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
7927 set_breakpoint_number (internal, b);
7928 if (is_tracepoint (b))
7929 set_tracepoint_count (breakpoint_count);
7930 if (!internal)
7931 mention (b);
7932 gdb::observers::breakpoint_created.notify (b);
7933
7934 if (update_gll)
7935 update_global_location_list (UGLL_MAY_INSERT);
7936 }
7937
7938 static int
7939 hw_breakpoint_used_count (void)
7940 {
7941 int i = 0;
7942
7943 for (breakpoint *b : all_breakpoints ())
7944 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
7945 for (bp_location *bl : b->locations ())
7946 {
7947 /* Special types of hardware breakpoints may use more than
7948 one register. */
7949 i += b->resources_needed (bl);
7950 }
7951
7952 return i;
7953 }
7954
7955 /* Returns the resources B would use if it were a hardware
7956 watchpoint. */
7957
7958 static int
7959 hw_watchpoint_use_count (struct breakpoint *b)
7960 {
7961 int i = 0;
7962
7963 if (!breakpoint_enabled (b))
7964 return 0;
7965
7966 for (bp_location *bl : b->locations ())
7967 {
7968 /* Special types of hardware watchpoints may use more than
7969 one register. */
7970 i += b->resources_needed (bl);
7971 }
7972
7973 return i;
7974 }
7975
7976 /* Returns the sum the used resources of all hardware watchpoints of
7977 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7978 the sum of the used resources of all hardware watchpoints of other
7979 types _not_ TYPE. */
7980
7981 static int
7982 hw_watchpoint_used_count_others (struct breakpoint *except,
7983 enum bptype type, int *other_type_used)
7984 {
7985 int i = 0;
7986
7987 *other_type_used = 0;
7988 for (breakpoint *b : all_breakpoints ())
7989 {
7990 if (b == except)
7991 continue;
7992 if (!breakpoint_enabled (b))
7993 continue;
7994
7995 if (b->type == type)
7996 i += hw_watchpoint_use_count (b);
7997 else if (is_hardware_watchpoint (b))
7998 *other_type_used = 1;
7999 }
8000
8001 return i;
8002 }
8003
8004 void
8005 disable_watchpoints_before_interactive_call_start (void)
8006 {
8007 for (breakpoint *b : all_breakpoints ())
8008 if (is_watchpoint (b) && breakpoint_enabled (b))
8009 {
8010 b->enable_state = bp_call_disabled;
8011 update_global_location_list (UGLL_DONT_INSERT);
8012 }
8013 }
8014
8015 void
8016 enable_watchpoints_after_interactive_call_stop (void)
8017 {
8018 for (breakpoint *b : all_breakpoints ())
8019 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8020 {
8021 b->enable_state = bp_enabled;
8022 update_global_location_list (UGLL_MAY_INSERT);
8023 }
8024 }
8025
8026 void
8027 disable_breakpoints_before_startup (void)
8028 {
8029 current_program_space->executing_startup = 1;
8030 update_global_location_list (UGLL_DONT_INSERT);
8031 }
8032
8033 void
8034 enable_breakpoints_after_startup (void)
8035 {
8036 current_program_space->executing_startup = 0;
8037 breakpoint_re_set ();
8038 }
8039
8040 /* Allocate a new momentary breakpoint. */
8041
8042 template<typename... Arg>
8043 static momentary_breakpoint *
8044 new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8045 Arg&&... args)
8046 {
8047 if (type == bp_longjmp || type == bp_exception)
8048 return new longjmp_breakpoint (gdbarch, type,
8049 std::forward<Arg> (args)...);
8050 else
8051 return new momentary_breakpoint (gdbarch, type,
8052 std::forward<Arg> (args)...);
8053 }
8054
8055 /* Set a momentary breakpoint of type TYPE at address specified by
8056 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8057 frame. */
8058
8059 breakpoint_up
8060 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8061 struct frame_id frame_id, enum bptype type)
8062 {
8063 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8064 tail-called one. */
8065 gdb_assert (!frame_id_artificial_p (frame_id));
8066
8067 std::unique_ptr<momentary_breakpoint> b
8068 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8069 inferior_thread ()->global_num));
8070
8071 b->add_location (sal);
8072
8073 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8074
8075 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8076
8077 return bp;
8078 }
8079
8080 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8081 The new breakpoint will have type TYPE, use OPS as its
8082 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8083
8084 static struct breakpoint *
8085 momentary_breakpoint_from_master (struct breakpoint *orig,
8086 enum bptype type,
8087 int loc_enabled,
8088 int thread)
8089 {
8090 std::unique_ptr<breakpoint> copy
8091 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8092 orig->frame_id, thread));
8093 copy->loc = copy->allocate_location ();
8094 set_breakpoint_location_function (copy->loc);
8095
8096 copy->loc->gdbarch = orig->loc->gdbarch;
8097 copy->loc->requested_address = orig->loc->requested_address;
8098 copy->loc->address = orig->loc->address;
8099 copy->loc->section = orig->loc->section;
8100 copy->loc->pspace = orig->loc->pspace;
8101 copy->loc->probe = orig->loc->probe;
8102 copy->loc->line_number = orig->loc->line_number;
8103 copy->loc->symtab = orig->loc->symtab;
8104 copy->loc->enabled = loc_enabled;
8105
8106 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
8107 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8108 return b;
8109 }
8110
8111 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8112 ORIG is NULL. */
8113
8114 struct breakpoint *
8115 clone_momentary_breakpoint (struct breakpoint *orig)
8116 {
8117 /* If there's nothing to clone, then return nothing. */
8118 if (orig == NULL)
8119 return NULL;
8120
8121 return momentary_breakpoint_from_master (orig, orig->type, 0,
8122 orig->thread);
8123 }
8124
8125 breakpoint_up
8126 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8127 enum bptype type)
8128 {
8129 struct symtab_and_line sal;
8130
8131 sal = find_pc_line (pc, 0);
8132 sal.pc = pc;
8133 sal.section = find_pc_overlay (pc);
8134 sal.explicit_pc = 1;
8135
8136 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8137 }
8138 \f
8139
8140 /* Tell the user we have just set a breakpoint B. */
8141
8142 static void
8143 mention (const breakpoint *b)
8144 {
8145 b->print_mention ();
8146 current_uiout->text ("\n");
8147 }
8148 \f
8149
8150 static bool bp_loc_is_permanent (struct bp_location *loc);
8151
8152 /* Handle "set breakpoint auto-hw on".
8153
8154 If the explicitly specified breakpoint type is not hardware
8155 breakpoint, check the memory map to see whether the breakpoint
8156 address is in read-only memory.
8157
8158 - location type is not hardware breakpoint, memory is read-only.
8159 We change the type of the location to hardware breakpoint.
8160
8161 - location type is hardware breakpoint, memory is read-write. This
8162 means we've previously made the location hardware one, but then the
8163 memory map changed, so we undo.
8164 */
8165
8166 static void
8167 handle_automatic_hardware_breakpoints (bp_location *bl)
8168 {
8169 if (automatic_hardware_breakpoints
8170 && bl->owner->type != bp_hardware_breakpoint
8171 && (bl->loc_type == bp_loc_software_breakpoint
8172 || bl->loc_type == bp_loc_hardware_breakpoint))
8173 {
8174 /* When breakpoints are removed, remove_breakpoints will use
8175 location types we've just set here, the only possible problem
8176 is that memory map has changed during running program, but
8177 it's not going to work anyway with current gdb. */
8178 mem_region *mr = lookup_mem_region (bl->address);
8179
8180 if (mr != nullptr)
8181 {
8182 enum bp_loc_type new_type;
8183
8184 if (mr->attrib.mode != MEM_RW)
8185 new_type = bp_loc_hardware_breakpoint;
8186 else
8187 new_type = bp_loc_software_breakpoint;
8188
8189 if (new_type != bl->loc_type)
8190 {
8191 static bool said = false;
8192
8193 bl->loc_type = new_type;
8194 if (!said)
8195 {
8196 gdb_printf (_("Note: automatically using "
8197 "hardware breakpoints for "
8198 "read-only addresses.\n"));
8199 said = true;
8200 }
8201 }
8202 }
8203 }
8204 }
8205
8206 bp_location *
8207 code_breakpoint::add_location (const symtab_and_line &sal)
8208 {
8209 struct bp_location *new_loc, **tmp;
8210 CORE_ADDR adjusted_address;
8211 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8212
8213 if (loc_gdbarch == NULL)
8214 loc_gdbarch = gdbarch;
8215
8216 /* Adjust the breakpoint's address prior to allocating a location.
8217 Once we call allocate_location(), that mostly uninitialized
8218 location will be placed on the location chain. Adjustment of the
8219 breakpoint may cause target_read_memory() to be called and we do
8220 not want its scan of the location chain to find a breakpoint and
8221 location that's only been partially initialized. */
8222 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8223 sal.pc, type,
8224 sal.pspace);
8225
8226 /* Sort the locations by their ADDRESS. */
8227 new_loc = allocate_location ();
8228 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8229 tmp = &((*tmp)->next))
8230 ;
8231 new_loc->next = *tmp;
8232 *tmp = new_loc;
8233
8234 new_loc->requested_address = sal.pc;
8235 new_loc->address = adjusted_address;
8236 new_loc->pspace = sal.pspace;
8237 new_loc->probe.prob = sal.prob;
8238 new_loc->probe.objfile = sal.objfile;
8239 gdb_assert (new_loc->pspace != NULL);
8240 new_loc->section = sal.section;
8241 new_loc->gdbarch = loc_gdbarch;
8242 new_loc->line_number = sal.line;
8243 new_loc->symtab = sal.symtab;
8244 new_loc->symbol = sal.symbol;
8245 new_loc->msymbol = sal.msymbol;
8246 new_loc->objfile = sal.objfile;
8247
8248 set_breakpoint_location_function (new_loc);
8249
8250 /* While by definition, permanent breakpoints are already present in the
8251 code, we don't mark the location as inserted. Normally one would expect
8252 that GDB could rely on that breakpoint instruction to stop the program,
8253 thus removing the need to insert its own breakpoint, except that executing
8254 the breakpoint instruction can kill the target instead of reporting a
8255 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8256 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8257 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8258 breakpoint be inserted normally results in QEMU knowing about the GDB
8259 breakpoint, and thus trap before the breakpoint instruction is executed.
8260 (If GDB later needs to continue execution past the permanent breakpoint,
8261 it manually increments the PC, thus avoiding executing the breakpoint
8262 instruction.) */
8263 if (bp_loc_is_permanent (new_loc))
8264 new_loc->permanent = 1;
8265
8266 return new_loc;
8267 }
8268 \f
8269
8270 /* Return true if LOC is pointing to a permanent breakpoint,
8271 return false otherwise. */
8272
8273 static bool
8274 bp_loc_is_permanent (struct bp_location *loc)
8275 {
8276 gdb_assert (loc != NULL);
8277
8278 /* If we have a non-breakpoint-backed catchpoint or a software
8279 watchpoint, just return 0. We should not attempt to read from
8280 the addresses the locations of these breakpoint types point to.
8281 gdbarch_program_breakpoint_here_p, below, will attempt to read
8282 memory. */
8283 if (!bl_address_is_meaningful (loc))
8284 return false;
8285
8286 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8287 switch_to_program_space_and_thread (loc->pspace);
8288 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8289 }
8290
8291 /* Build a command list for the dprintf corresponding to the current
8292 settings of the dprintf style options. */
8293
8294 static void
8295 update_dprintf_command_list (struct breakpoint *b)
8296 {
8297 const char *dprintf_args = b->extra_string.get ();
8298 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8299
8300 if (!dprintf_args)
8301 return;
8302
8303 dprintf_args = skip_spaces (dprintf_args);
8304
8305 /* Allow a comma, as it may have terminated a location, but don't
8306 insist on it. */
8307 if (*dprintf_args == ',')
8308 ++dprintf_args;
8309 dprintf_args = skip_spaces (dprintf_args);
8310
8311 if (*dprintf_args != '"')
8312 error (_("Bad format string, missing '\"'."));
8313
8314 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8315 printf_line = xstrprintf ("printf %s", dprintf_args);
8316 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8317 {
8318 if (dprintf_function.empty ())
8319 error (_("No function supplied for dprintf call"));
8320
8321 if (!dprintf_channel.empty ())
8322 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8323 dprintf_function.c_str (),
8324 dprintf_channel.c_str (),
8325 dprintf_args);
8326 else
8327 printf_line = xstrprintf ("call (void) %s (%s)",
8328 dprintf_function.c_str (),
8329 dprintf_args);
8330 }
8331 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8332 {
8333 if (target_can_run_breakpoint_commands ())
8334 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8335 else
8336 {
8337 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8338 printf_line = xstrprintf ("printf %s", dprintf_args);
8339 }
8340 }
8341 else
8342 internal_error (_("Invalid dprintf style."));
8343
8344 gdb_assert (printf_line != NULL);
8345
8346 /* Manufacture a printf sequence. */
8347 struct command_line *printf_cmd_line
8348 = new struct command_line (simple_control, printf_line.release ());
8349 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8350 command_lines_deleter ()));
8351 }
8352
8353 /* Update all dprintf commands, making their command lists reflect
8354 current style settings. */
8355
8356 static void
8357 update_dprintf_commands (const char *args, int from_tty,
8358 struct cmd_list_element *c)
8359 {
8360 for (breakpoint *b : all_breakpoints ())
8361 if (b->type == bp_dprintf)
8362 update_dprintf_command_list (b);
8363 }
8364
8365 code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
8366 enum bptype type_,
8367 gdb::array_view<const symtab_and_line> sals,
8368 location_spec_up &&locspec_,
8369 gdb::unique_xmalloc_ptr<char> filter_,
8370 gdb::unique_xmalloc_ptr<char> cond_string_,
8371 gdb::unique_xmalloc_ptr<char> extra_string_,
8372 enum bpdisp disposition_,
8373 int thread_, int task_, int ignore_count_,
8374 int from_tty,
8375 int enabled_, unsigned flags,
8376 int display_canonical_)
8377 : breakpoint (gdbarch_, type_)
8378 {
8379 int i;
8380
8381 if (type == bp_hardware_breakpoint)
8382 {
8383 int target_resources_ok;
8384
8385 i = hw_breakpoint_used_count ();
8386 target_resources_ok =
8387 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8388 i + 1, 0);
8389 if (target_resources_ok == 0)
8390 error (_("No hardware breakpoint support in the target."));
8391 else if (target_resources_ok < 0)
8392 error (_("Hardware breakpoints used exceeds limit."));
8393 }
8394
8395 gdb_assert (!sals.empty ());
8396
8397 thread = thread_;
8398 task = task_;
8399
8400 cond_string = std::move (cond_string_);
8401 extra_string = std::move (extra_string_);
8402 ignore_count = ignore_count_;
8403 enable_state = enabled_ ? bp_enabled : bp_disabled;
8404 disposition = disposition_;
8405
8406 if (type == bp_static_tracepoint
8407 || type == bp_static_marker_tracepoint)
8408 {
8409 auto *t = gdb::checked_static_cast<struct tracepoint *> (this);
8410 struct static_tracepoint_marker marker;
8411
8412 if (strace_marker_p (this))
8413 {
8414 /* We already know the marker exists, otherwise, we wouldn't
8415 see a sal for it. */
8416 const char *p = &locspec_->to_string ()[3];
8417 const char *endp;
8418
8419 p = skip_spaces (p);
8420
8421 endp = skip_to_space (p);
8422
8423 t->static_trace_marker_id.assign (p, endp - p);
8424
8425 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8426 t->static_trace_marker_id.c_str ());
8427 }
8428 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8429 {
8430 t->static_trace_marker_id = std::move (marker.str_id);
8431
8432 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8433 t->static_trace_marker_id.c_str ());
8434 }
8435 else
8436 warning (_("Couldn't determine the static tracepoint marker to probe"));
8437 }
8438
8439 for (const auto &sal : sals)
8440 {
8441 if (from_tty)
8442 {
8443 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8444 if (loc_gdbarch == nullptr)
8445 loc_gdbarch = gdbarch;
8446
8447 describe_other_breakpoints (loc_gdbarch,
8448 sal.pspace, sal.pc, sal.section, thread);
8449 }
8450
8451 bp_location *new_loc = add_location (sal);
8452 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8453 new_loc->inserted = 1;
8454
8455 /* Do not set breakpoint locations conditions yet. As locations
8456 are inserted, they get sorted based on their addresses. Let
8457 the list stabilize to have reliable location numbers. */
8458
8459 /* Dynamic printf requires and uses additional arguments on the
8460 command line, otherwise it's an error. */
8461 if (type == bp_dprintf)
8462 {
8463 if (extra_string != nullptr)
8464 update_dprintf_command_list (this);
8465 else
8466 error (_("Format string required"));
8467 }
8468 else if (extra_string != nullptr)
8469 error (_("Garbage '%s' at end of command"), extra_string.get ());
8470 }
8471
8472 /* The order of the locations is now stable. Set the location
8473 condition using the location's number. */
8474 int loc_num = 1;
8475 for (bp_location *bl : locations ())
8476 {
8477 if (cond_string != nullptr)
8478 set_breakpoint_location_condition (cond_string.get (), bl,
8479 number, loc_num);
8480
8481 ++loc_num;
8482 }
8483
8484 display_canonical = display_canonical_;
8485 if (locspec_ != nullptr)
8486 locspec = std::move (locspec_);
8487 else
8488 locspec = new_address_location_spec (this->loc->address, NULL, 0);
8489 filter = std::move (filter_);
8490 }
8491
8492 static void
8493 create_breakpoint_sal (struct gdbarch *gdbarch,
8494 gdb::array_view<const symtab_and_line> sals,
8495 location_spec_up &&locspec,
8496 gdb::unique_xmalloc_ptr<char> filter,
8497 gdb::unique_xmalloc_ptr<char> cond_string,
8498 gdb::unique_xmalloc_ptr<char> extra_string,
8499 enum bptype type, enum bpdisp disposition,
8500 int thread, int task, int ignore_count,
8501 int from_tty,
8502 int enabled, int internal, unsigned flags,
8503 int display_canonical)
8504 {
8505 std::unique_ptr<code_breakpoint> b
8506 = new_breakpoint_from_type (gdbarch,
8507 type,
8508 sals,
8509 std::move (locspec),
8510 std::move (filter),
8511 std::move (cond_string),
8512 std::move (extra_string),
8513 disposition,
8514 thread, task, ignore_count,
8515 from_tty,
8516 enabled, flags,
8517 display_canonical);
8518
8519 install_breakpoint (internal, std::move (b), 0);
8520 }
8521
8522 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8523 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8524 value. COND_STRING, if not NULL, specified the condition to be
8525 used for all breakpoints. Essentially the only case where
8526 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8527 function. In that case, it's still not possible to specify
8528 separate conditions for different overloaded functions, so
8529 we take just a single condition string.
8530
8531 NOTE: If the function succeeds, the caller is expected to cleanup
8532 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8533 array contents). If the function fails (error() is called), the
8534 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8535 COND and SALS arrays and each of those arrays contents. */
8536
8537 static void
8538 create_breakpoints_sal (struct gdbarch *gdbarch,
8539 struct linespec_result *canonical,
8540 gdb::unique_xmalloc_ptr<char> cond_string,
8541 gdb::unique_xmalloc_ptr<char> extra_string,
8542 enum bptype type, enum bpdisp disposition,
8543 int thread, int task, int ignore_count,
8544 int from_tty,
8545 int enabled, int internal, unsigned flags)
8546 {
8547 if (canonical->pre_expanded)
8548 gdb_assert (canonical->lsals.size () == 1);
8549
8550 for (const auto &lsal : canonical->lsals)
8551 {
8552 /* Note that 'location' can be NULL in the case of a plain
8553 'break', without arguments. */
8554 location_spec_up locspec
8555 = (canonical->locspec != nullptr
8556 ? canonical->locspec->clone ()
8557 : nullptr);
8558 gdb::unique_xmalloc_ptr<char> filter_string
8559 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8560
8561 create_breakpoint_sal (gdbarch, lsal.sals,
8562 std::move (locspec),
8563 std::move (filter_string),
8564 std::move (cond_string),
8565 std::move (extra_string),
8566 type, disposition,
8567 thread, task, ignore_count,
8568 from_tty, enabled, internal, flags,
8569 canonical->special_display);
8570 }
8571 }
8572
8573 /* Parse LOCSPEC which is assumed to be a SAL specification possibly
8574 followed by conditionals. On return, SALS contains an array of SAL
8575 addresses found. LOCSPEC points to the end of the SAL (for
8576 linespec locspecs).
8577
8578 The array and the line spec strings are allocated on the heap, it is
8579 the caller's responsibility to free them. */
8580
8581 static void
8582 parse_breakpoint_sals (location_spec *locspec,
8583 struct linespec_result *canonical)
8584 {
8585 struct symtab_and_line cursal;
8586
8587 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8588 {
8589 const char *spec = as_linespec_location_spec (locspec)->spec_string;
8590
8591 if (spec == NULL)
8592 {
8593 /* The last displayed codepoint, if it's valid, is our default
8594 breakpoint address. */
8595 if (last_displayed_sal_is_valid ())
8596 {
8597 /* Set sal's pspace, pc, symtab, and line to the values
8598 corresponding to the last call to print_frame_info.
8599 Be sure to reinitialize LINE with NOTCURRENT == 0
8600 as the breakpoint line number is inappropriate otherwise.
8601 find_pc_line would adjust PC, re-set it back. */
8602 symtab_and_line sal = get_last_displayed_sal ();
8603 CORE_ADDR pc = sal.pc;
8604
8605 sal = find_pc_line (pc, 0);
8606
8607 /* "break" without arguments is equivalent to "break *PC"
8608 where PC is the last displayed codepoint's address. So
8609 make sure to set sal.explicit_pc to prevent GDB from
8610 trying to expand the list of sals to include all other
8611 instances with the same symtab and line. */
8612 sal.pc = pc;
8613 sal.explicit_pc = 1;
8614
8615 struct linespec_sals lsal;
8616 lsal.sals = {sal};
8617 lsal.canonical = NULL;
8618
8619 canonical->lsals.push_back (std::move (lsal));
8620 return;
8621 }
8622 else
8623 error (_("No default breakpoint address now."));
8624 }
8625 }
8626
8627 /* Force almost all breakpoints to be in terms of the
8628 current_source_symtab (which is decode_line_1's default).
8629 This should produce the results we want almost all of the
8630 time while leaving default_breakpoint_* alone.
8631
8632 ObjC: However, don't match an Objective-C method name which
8633 may have a '+' or '-' succeeded by a '['. */
8634 cursal = get_current_source_symtab_and_line ();
8635 if (last_displayed_sal_is_valid ())
8636 {
8637 const char *spec = NULL;
8638
8639 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8640 spec = as_linespec_location_spec (locspec)->spec_string;
8641
8642 if (!cursal.symtab
8643 || (spec != NULL
8644 && strchr ("+-", spec[0]) != NULL
8645 && spec[1] != '['))
8646 {
8647 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8648 get_last_displayed_symtab (),
8649 get_last_displayed_line (),
8650 canonical, NULL, NULL);
8651 return;
8652 }
8653 }
8654
8655 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8656 cursal.symtab, cursal.line, canonical, NULL, NULL);
8657 }
8658
8659
8660 /* Convert each SAL into a real PC. Verify that the PC can be
8661 inserted as a breakpoint. If it can't throw an error. */
8662
8663 static void
8664 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8665 {
8666 for (auto &sal : sals)
8667 resolve_sal_pc (&sal);
8668 }
8669
8670 /* Fast tracepoints may have restrictions on valid locations. For
8671 instance, a fast tracepoint using a jump instead of a trap will
8672 likely have to overwrite more bytes than a trap would, and so can
8673 only be placed where the instruction is longer than the jump, or a
8674 multi-instruction sequence does not have a jump into the middle of
8675 it, etc. */
8676
8677 static void
8678 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8679 gdb::array_view<const symtab_and_line> sals)
8680 {
8681 for (const auto &sal : sals)
8682 {
8683 struct gdbarch *sarch;
8684
8685 sarch = get_sal_arch (sal);
8686 /* We fall back to GDBARCH if there is no architecture
8687 associated with SAL. */
8688 if (sarch == NULL)
8689 sarch = gdbarch;
8690 std::string msg;
8691 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8692 error (_("May not have a fast tracepoint at %s%s"),
8693 paddress (sarch, sal.pc), msg.c_str ());
8694 }
8695 }
8696
8697 /* Given TOK, a string specification of condition and thread, as
8698 accepted by the 'break' command, extract the condition
8699 string and thread number and set *COND_STRING and *THREAD.
8700 PC identifies the context at which the condition should be parsed.
8701 If no condition is found, *COND_STRING is set to NULL.
8702 If no thread is found, *THREAD is set to -1. */
8703
8704 static void
8705 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8706 gdb::unique_xmalloc_ptr<char> *cond_string,
8707 int *thread, int *task,
8708 gdb::unique_xmalloc_ptr<char> *rest)
8709 {
8710 cond_string->reset ();
8711 *thread = -1;
8712 *task = 0;
8713 rest->reset ();
8714 bool force = false;
8715
8716 while (tok && *tok)
8717 {
8718 const char *end_tok;
8719 int toklen;
8720 const char *cond_start = NULL;
8721 const char *cond_end = NULL;
8722
8723 tok = skip_spaces (tok);
8724
8725 if ((*tok == '"' || *tok == ',') && rest)
8726 {
8727 rest->reset (savestring (tok, strlen (tok)));
8728 return;
8729 }
8730
8731 end_tok = skip_to_space (tok);
8732
8733 toklen = end_tok - tok;
8734
8735 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8736 {
8737 tok = cond_start = end_tok + 1;
8738 try
8739 {
8740 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8741 }
8742 catch (const gdb_exception_error &)
8743 {
8744 if (!force)
8745 throw;
8746 else
8747 tok = tok + strlen (tok);
8748 }
8749 cond_end = tok;
8750 cond_string->reset (savestring (cond_start, cond_end - cond_start));
8751 }
8752 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8753 {
8754 tok = tok + toklen;
8755 force = true;
8756 }
8757 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8758 {
8759 const char *tmptok;
8760 struct thread_info *thr;
8761
8762 tok = end_tok + 1;
8763 thr = parse_thread_id (tok, &tmptok);
8764 if (tok == tmptok)
8765 error (_("Junk after thread keyword."));
8766 *thread = thr->global_num;
8767 tok = tmptok;
8768 }
8769 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8770 {
8771 char *tmptok;
8772
8773 tok = end_tok + 1;
8774 *task = strtol (tok, &tmptok, 0);
8775 if (tok == tmptok)
8776 error (_("Junk after task keyword."));
8777 if (!valid_task_id (*task))
8778 error (_("Unknown task %d."), *task);
8779 tok = tmptok;
8780 }
8781 else if (rest)
8782 {
8783 rest->reset (savestring (tok, strlen (tok)));
8784 return;
8785 }
8786 else
8787 error (_("Junk at end of arguments."));
8788 }
8789 }
8790
8791 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
8792 succeeds. The parsed values are written to COND_STRING, THREAD,
8793 TASK, and REST. See the comment of 'find_condition_and_thread'
8794 for the description of these parameters and INPUT. */
8795
8796 static void
8797 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
8798 const char *input,
8799 gdb::unique_xmalloc_ptr<char> *cond_string,
8800 int *thread, int *task,
8801 gdb::unique_xmalloc_ptr<char> *rest)
8802 {
8803 int num_failures = 0;
8804 for (auto &sal : sals)
8805 {
8806 gdb::unique_xmalloc_ptr<char> cond;
8807 int thread_id = 0;
8808 int task_id = 0;
8809 gdb::unique_xmalloc_ptr<char> remaining;
8810
8811 /* Here we want to parse 'arg' to separate condition from thread
8812 number. But because parsing happens in a context and the
8813 contexts of sals might be different, try each until there is
8814 success. Finding one successful parse is sufficient for our
8815 goal. When setting the breakpoint we'll re-parse the
8816 condition in the context of each sal. */
8817 try
8818 {
8819 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8820 &task_id, &remaining);
8821 *cond_string = std::move (cond);
8822 *thread = thread_id;
8823 *task = task_id;
8824 *rest = std::move (remaining);
8825 break;
8826 }
8827 catch (const gdb_exception_error &e)
8828 {
8829 num_failures++;
8830 /* If no sal remains, do not continue. */
8831 if (num_failures == sals.size ())
8832 throw;
8833 }
8834 }
8835 }
8836
8837 /* Decode a static tracepoint marker spec. */
8838
8839 static std::vector<symtab_and_line>
8840 decode_static_tracepoint_spec (const char **arg_p)
8841 {
8842 const char *p = &(*arg_p)[3];
8843 const char *endp;
8844
8845 p = skip_spaces (p);
8846
8847 endp = skip_to_space (p);
8848
8849 std::string marker_str (p, endp - p);
8850
8851 std::vector<static_tracepoint_marker> markers
8852 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8853 if (markers.empty ())
8854 error (_("No known static tracepoint marker named %s"),
8855 marker_str.c_str ());
8856
8857 std::vector<symtab_and_line> sals;
8858 sals.reserve (markers.size ());
8859
8860 for (const static_tracepoint_marker &marker : markers)
8861 {
8862 symtab_and_line sal = find_pc_line (marker.address, 0);
8863 sal.pc = marker.address;
8864 sals.push_back (sal);
8865 }
8866
8867 *arg_p = endp;
8868 return sals;
8869 }
8870
8871 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8872 according to IS_TRACEPOINT. */
8873
8874 static const struct breakpoint_ops *
8875 breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
8876 bool is_tracepoint)
8877 {
8878 if (is_tracepoint)
8879 {
8880 if (locspec_type == PROBE_LOCATION_SPEC)
8881 return &tracepoint_probe_breakpoint_ops;
8882 else
8883 return &code_breakpoint_ops;
8884 }
8885 else
8886 {
8887 if (locspec_type == PROBE_LOCATION_SPEC)
8888 return &bkpt_probe_breakpoint_ops;
8889 else
8890 return &code_breakpoint_ops;
8891 }
8892 }
8893
8894 /* See breakpoint.h. */
8895
8896 const struct breakpoint_ops *
8897 breakpoint_ops_for_location_spec (const location_spec *locspec,
8898 bool is_tracepoint)
8899 {
8900 if (locspec != nullptr)
8901 return (breakpoint_ops_for_location_spec_type
8902 (locspec->type (), is_tracepoint));
8903 return &code_breakpoint_ops;
8904 }
8905
8906 /* See breakpoint.h. */
8907
8908 int
8909 create_breakpoint (struct gdbarch *gdbarch,
8910 location_spec *locspec,
8911 const char *cond_string,
8912 int thread, const char *extra_string,
8913 bool force_condition, int parse_extra,
8914 int tempflag, enum bptype type_wanted,
8915 int ignore_count,
8916 enum auto_boolean pending_break_support,
8917 const struct breakpoint_ops *ops,
8918 int from_tty, int enabled, int internal,
8919 unsigned flags)
8920 {
8921 struct linespec_result canonical;
8922 bool pending = false;
8923 int task = 0;
8924 int prev_bkpt_count = breakpoint_count;
8925
8926 gdb_assert (ops != NULL);
8927
8928 /* If extra_string isn't useful, set it to NULL. */
8929 if (extra_string != NULL && *extra_string == '\0')
8930 extra_string = NULL;
8931
8932 try
8933 {
8934 ops->create_sals_from_location_spec (locspec, &canonical);
8935 }
8936 catch (const gdb_exception_error &e)
8937 {
8938 /* If caller is interested in rc value from parse, set
8939 value. */
8940 if (e.error == NOT_FOUND_ERROR)
8941 {
8942 /* If pending breakpoint support is turned off, throw
8943 error. */
8944
8945 if (pending_break_support == AUTO_BOOLEAN_FALSE)
8946 throw;
8947
8948 exception_print (gdb_stderr, e);
8949
8950 /* If pending breakpoint support is auto query and the user
8951 selects no, then simply return the error code. */
8952 if (pending_break_support == AUTO_BOOLEAN_AUTO
8953 && !nquery (_("Make %s pending on future shared library load? "),
8954 bptype_string (type_wanted)))
8955 return 0;
8956
8957 /* At this point, either the user was queried about setting
8958 a pending breakpoint and selected yes, or pending
8959 breakpoint behavior is on and thus a pending breakpoint
8960 is defaulted on behalf of the user. */
8961 pending = true;
8962 }
8963 else
8964 throw;
8965 }
8966
8967 if (!pending && canonical.lsals.empty ())
8968 return 0;
8969
8970 /* Resolve all line numbers to PC's and verify that the addresses
8971 are ok for the target. */
8972 if (!pending)
8973 {
8974 for (auto &lsal : canonical.lsals)
8975 breakpoint_sals_to_pc (lsal.sals);
8976 }
8977
8978 /* Fast tracepoints may have additional restrictions on location. */
8979 if (!pending && type_wanted == bp_fast_tracepoint)
8980 {
8981 for (const auto &lsal : canonical.lsals)
8982 check_fast_tracepoint_sals (gdbarch, lsal.sals);
8983 }
8984
8985 /* Verify that condition can be parsed, before setting any
8986 breakpoints. Allocate a separate condition expression for each
8987 breakpoint. */
8988 if (!pending)
8989 {
8990 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8991 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8992
8993 if (parse_extra)
8994 {
8995 gdb::unique_xmalloc_ptr<char> rest;
8996 gdb::unique_xmalloc_ptr<char> cond;
8997
8998 const linespec_sals &lsal = canonical.lsals[0];
8999
9000 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9001 &cond, &thread, &task, &rest);
9002 cond_string_copy = std::move (cond);
9003 extra_string_copy = std::move (rest);
9004 }
9005 else
9006 {
9007 if (type_wanted != bp_dprintf
9008 && extra_string != NULL && *extra_string != '\0')
9009 error (_("Garbage '%s' at end of location"), extra_string);
9010
9011 /* Check the validity of the condition. We should error out
9012 if the condition is invalid at all of the locations and
9013 if it is not forced. In the PARSE_EXTRA case above, this
9014 check is done when parsing the EXTRA_STRING. */
9015 if (cond_string != nullptr && !force_condition)
9016 {
9017 int num_failures = 0;
9018 const linespec_sals &lsal = canonical.lsals[0];
9019 for (const auto &sal : lsal.sals)
9020 {
9021 const char *cond = cond_string;
9022 try
9023 {
9024 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9025 /* One success is sufficient to keep going. */
9026 break;
9027 }
9028 catch (const gdb_exception_error &)
9029 {
9030 num_failures++;
9031 /* If this is the last sal, error out. */
9032 if (num_failures == lsal.sals.size ())
9033 throw;
9034 }
9035 }
9036 }
9037
9038 /* Create a private copy of condition string. */
9039 if (cond_string)
9040 cond_string_copy.reset (xstrdup (cond_string));
9041 /* Create a private copy of any extra string. */
9042 if (extra_string)
9043 extra_string_copy.reset (xstrdup (extra_string));
9044 }
9045
9046 ops->create_breakpoints_sal (gdbarch, &canonical,
9047 std::move (cond_string_copy),
9048 std::move (extra_string_copy),
9049 type_wanted,
9050 tempflag ? disp_del : disp_donttouch,
9051 thread, task, ignore_count,
9052 from_tty, enabled, internal, flags);
9053 }
9054 else
9055 {
9056 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9057 type_wanted);
9058 b->locspec = locspec->clone ();
9059
9060 if (parse_extra)
9061 b->cond_string = NULL;
9062 else
9063 {
9064 /* Create a private copy of condition string. */
9065 b->cond_string.reset (cond_string != NULL
9066 ? xstrdup (cond_string)
9067 : NULL);
9068 b->thread = thread;
9069 }
9070
9071 /* Create a private copy of any extra string. */
9072 b->extra_string.reset (extra_string != NULL
9073 ? xstrdup (extra_string)
9074 : NULL);
9075 b->ignore_count = ignore_count;
9076 b->disposition = tempflag ? disp_del : disp_donttouch;
9077 b->condition_not_parsed = 1;
9078 b->enable_state = enabled ? bp_enabled : bp_disabled;
9079 if ((type_wanted != bp_breakpoint
9080 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9081 b->pspace = current_program_space;
9082
9083 install_breakpoint (internal, std::move (b), 0);
9084 }
9085
9086 if (canonical.lsals.size () > 1)
9087 {
9088 warning (_("Multiple breakpoints were set.\nUse the "
9089 "\"delete\" command to delete unwanted breakpoints."));
9090 prev_breakpoint_count = prev_bkpt_count;
9091 }
9092
9093 update_global_location_list (UGLL_MAY_INSERT);
9094
9095 return 1;
9096 }
9097
9098 /* Set a breakpoint.
9099 ARG is a string describing breakpoint address,
9100 condition, and thread.
9101 FLAG specifies if a breakpoint is hardware on,
9102 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9103 and BP_TEMPFLAG. */
9104
9105 static void
9106 break_command_1 (const char *arg, int flag, int from_tty)
9107 {
9108 int tempflag = flag & BP_TEMPFLAG;
9109 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9110 ? bp_hardware_breakpoint
9111 : bp_breakpoint);
9112
9113 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9114 const struct breakpoint_ops *ops
9115 = breakpoint_ops_for_location_spec (locspec.get (),
9116 false /* is_tracepoint */);
9117
9118 create_breakpoint (get_current_arch (),
9119 locspec.get (),
9120 NULL, 0, arg, false, 1 /* parse arg */,
9121 tempflag, type_wanted,
9122 0 /* Ignore count */,
9123 pending_break_support,
9124 ops,
9125 from_tty,
9126 1 /* enabled */,
9127 0 /* internal */,
9128 0);
9129 }
9130
9131 /* Helper function for break_command_1 and disassemble_command. */
9132
9133 void
9134 resolve_sal_pc (struct symtab_and_line *sal)
9135 {
9136 CORE_ADDR pc;
9137
9138 if (sal->pc == 0 && sal->symtab != NULL)
9139 {
9140 if (!find_line_pc (sal->symtab, sal->line, &pc))
9141 error (_("No line %d in file \"%s\"."),
9142 sal->line, symtab_to_filename_for_display (sal->symtab));
9143 sal->pc = pc;
9144
9145 /* If this SAL corresponds to a breakpoint inserted using a line
9146 number, then skip the function prologue if necessary. */
9147 if (sal->explicit_line)
9148 skip_prologue_sal (sal);
9149 }
9150
9151 if (sal->section == 0 && sal->symtab != NULL)
9152 {
9153 const struct blockvector *bv;
9154 const struct block *b;
9155 struct symbol *sym;
9156
9157 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9158 sal->symtab->compunit ());
9159 if (bv != NULL)
9160 {
9161 sym = block_linkage_function (b);
9162 if (sym != NULL)
9163 {
9164 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9165 sal->section
9166 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9167 }
9168 else
9169 {
9170 /* It really is worthwhile to have the section, so we'll
9171 just have to look harder. This case can be executed
9172 if we have line numbers but no functions (as can
9173 happen in assembly source). */
9174
9175 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9176 switch_to_program_space_and_thread (sal->pspace);
9177
9178 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9179 if (msym.minsym)
9180 sal->section = msym.obj_section ();
9181 }
9182 }
9183 }
9184 }
9185
9186 void
9187 break_command (const char *arg, int from_tty)
9188 {
9189 break_command_1 (arg, 0, from_tty);
9190 }
9191
9192 void
9193 tbreak_command (const char *arg, int from_tty)
9194 {
9195 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9196 }
9197
9198 static void
9199 hbreak_command (const char *arg, int from_tty)
9200 {
9201 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9202 }
9203
9204 static void
9205 thbreak_command (const char *arg, int from_tty)
9206 {
9207 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9208 }
9209
9210 /* The dynamic printf command is mostly like a regular breakpoint, but
9211 with a prewired command list consisting of a single output command,
9212 built from extra arguments supplied on the dprintf command
9213 line. */
9214
9215 static void
9216 dprintf_command (const char *arg, int from_tty)
9217 {
9218 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9219
9220 /* If non-NULL, ARG should have been advanced past the location;
9221 the next character must be ','. */
9222 if (arg != NULL)
9223 {
9224 if (arg[0] != ',' || arg[1] == '\0')
9225 error (_("Format string required"));
9226 else
9227 {
9228 /* Skip the comma. */
9229 ++arg;
9230 }
9231 }
9232
9233 create_breakpoint (get_current_arch (),
9234 locspec.get (),
9235 NULL, 0, arg, false, 1 /* parse arg */,
9236 0, bp_dprintf,
9237 0 /* Ignore count */,
9238 pending_break_support,
9239 &code_breakpoint_ops,
9240 from_tty,
9241 1 /* enabled */,
9242 0 /* internal */,
9243 0);
9244 }
9245
9246 static void
9247 agent_printf_command (const char *arg, int from_tty)
9248 {
9249 error (_("May only run agent-printf on the target"));
9250 }
9251
9252 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9253
9254 int
9255 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9256 const address_space *aspace,
9257 CORE_ADDR bp_addr,
9258 const target_waitstatus &ws)
9259 {
9260 if (ws.kind () != TARGET_WAITKIND_STOPPED
9261 || ws.sig () != GDB_SIGNAL_TRAP)
9262 return 0;
9263
9264 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9265 bl->length, aspace, bp_addr);
9266 }
9267
9268 /* Implement the "resources_needed" method for ranged breakpoints. */
9269
9270 int
9271 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9272 {
9273 return target_ranged_break_num_registers ();
9274 }
9275
9276 /* Implement the "print_it" method for ranged breakpoints. */
9277
9278 enum print_stop_action
9279 ranged_breakpoint::print_it (const bpstat *bs) const
9280 {
9281 struct bp_location *bl = loc;
9282 struct ui_out *uiout = current_uiout;
9283
9284 gdb_assert (type == bp_hardware_breakpoint);
9285
9286 /* Ranged breakpoints have only one location. */
9287 gdb_assert (bl && bl->next == NULL);
9288
9289 annotate_breakpoint (number);
9290
9291 maybe_print_thread_hit_breakpoint (uiout);
9292
9293 if (disposition == disp_del)
9294 uiout->text ("Temporary ranged breakpoint ");
9295 else
9296 uiout->text ("Ranged breakpoint ");
9297 if (uiout->is_mi_like_p ())
9298 {
9299 uiout->field_string ("reason",
9300 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9301 uiout->field_string ("disp", bpdisp_text (disposition));
9302 }
9303 print_num_locno (bs, uiout);
9304 uiout->text (", ");
9305
9306 return PRINT_SRC_AND_LOC;
9307 }
9308
9309 /* Implement the "print_one" method for ranged breakpoints. */
9310
9311 bool
9312 ranged_breakpoint::print_one (bp_location **last_loc) const
9313 {
9314 struct bp_location *bl = loc;
9315 struct value_print_options opts;
9316 struct ui_out *uiout = current_uiout;
9317
9318 /* Ranged breakpoints have only one location. */
9319 gdb_assert (bl && bl->next == NULL);
9320
9321 get_user_print_options (&opts);
9322
9323 if (opts.addressprint)
9324 /* We don't print the address range here, it will be printed later
9325 by ranged_breakpoint::print_one_detail. */
9326 uiout->field_skip ("addr");
9327 annotate_field (5);
9328 print_breakpoint_location (this, bl);
9329 *last_loc = bl;
9330
9331 return true;
9332 }
9333
9334 /* Implement the "print_one_detail" method for ranged breakpoints. */
9335
9336 void
9337 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9338 {
9339 CORE_ADDR address_start, address_end;
9340 struct bp_location *bl = loc;
9341 string_file stb;
9342
9343 gdb_assert (bl);
9344
9345 address_start = bl->address;
9346 address_end = address_start + bl->length - 1;
9347
9348 uiout->text ("\taddress range: ");
9349 stb.printf ("[%s, %s]",
9350 print_core_address (bl->gdbarch, address_start),
9351 print_core_address (bl->gdbarch, address_end));
9352 uiout->field_stream ("addr", stb);
9353 uiout->text ("\n");
9354 }
9355
9356 /* Implement the "print_mention" method for ranged breakpoints. */
9357
9358 void
9359 ranged_breakpoint::print_mention () const
9360 {
9361 struct bp_location *bl = loc;
9362 struct ui_out *uiout = current_uiout;
9363
9364 gdb_assert (bl);
9365 gdb_assert (type == bp_hardware_breakpoint);
9366
9367 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9368 number, paddress (bl->gdbarch, bl->address),
9369 paddress (bl->gdbarch, bl->address + bl->length - 1));
9370 }
9371
9372 /* Implement the "print_recreate" method for ranged breakpoints. */
9373
9374 void
9375 ranged_breakpoint::print_recreate (struct ui_file *fp) const
9376 {
9377 gdb_printf (fp, "break-range %s, %s",
9378 locspec->to_string (),
9379 locspec_range_end->to_string ());
9380 print_recreate_thread (fp);
9381 }
9382
9383 /* Find the address where the end of the breakpoint range should be
9384 placed, given the SAL of the end of the range. This is so that if
9385 the user provides a line number, the end of the range is set to the
9386 last instruction of the given line. */
9387
9388 static CORE_ADDR
9389 find_breakpoint_range_end (struct symtab_and_line sal)
9390 {
9391 CORE_ADDR end;
9392
9393 /* If the user provided a PC value, use it. Otherwise,
9394 find the address of the end of the given location. */
9395 if (sal.explicit_pc)
9396 end = sal.pc;
9397 else
9398 {
9399 int ret;
9400 CORE_ADDR start;
9401
9402 ret = find_line_pc_range (sal, &start, &end);
9403 if (!ret)
9404 error (_("Could not find location of the end of the range."));
9405
9406 /* find_line_pc_range returns the start of the next line. */
9407 end--;
9408 }
9409
9410 return end;
9411 }
9412
9413 /* Implement the "break-range" CLI command. */
9414
9415 static void
9416 break_range_command (const char *arg, int from_tty)
9417 {
9418 const char *arg_start;
9419 struct linespec_result canonical_start, canonical_end;
9420 int bp_count, can_use_bp, length;
9421 CORE_ADDR end;
9422
9423 /* We don't support software ranged breakpoints. */
9424 if (target_ranged_break_num_registers () < 0)
9425 error (_("This target does not support hardware ranged breakpoints."));
9426
9427 bp_count = hw_breakpoint_used_count ();
9428 bp_count += target_ranged_break_num_registers ();
9429 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9430 bp_count, 0);
9431 if (can_use_bp < 0)
9432 error (_("Hardware breakpoints used exceeds limit."));
9433
9434 arg = skip_spaces (arg);
9435 if (arg == NULL || arg[0] == '\0')
9436 error(_("No address range specified."));
9437
9438 arg_start = arg;
9439 location_spec_up start_locspec
9440 = string_to_location_spec (&arg, current_language);
9441 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
9442
9443 if (arg[0] != ',')
9444 error (_("Too few arguments."));
9445 else if (canonical_start.lsals.empty ())
9446 error (_("Could not find location of the beginning of the range."));
9447
9448 const linespec_sals &lsal_start = canonical_start.lsals[0];
9449
9450 if (canonical_start.lsals.size () > 1
9451 || lsal_start.sals.size () != 1)
9452 error (_("Cannot create a ranged breakpoint with multiple locations."));
9453
9454 const symtab_and_line &sal_start = lsal_start.sals[0];
9455 std::string addr_string_start (arg_start, arg - arg_start);
9456
9457 arg++; /* Skip the comma. */
9458 arg = skip_spaces (arg);
9459
9460 /* Parse the end location specification. */
9461
9462 arg_start = arg;
9463
9464 /* We call decode_line_full directly here instead of using
9465 parse_breakpoint_sals because we need to specify the start
9466 location spec's symtab and line as the default symtab and line
9467 for the end of the range. This makes it possible to have ranges
9468 like "foo.c:27, +14", where +14 means 14 lines from the start
9469 location spec. */
9470 location_spec_up end_locspec
9471 = string_to_location_spec (&arg, current_language);
9472 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9473 sal_start.symtab, sal_start.line,
9474 &canonical_end, NULL, NULL);
9475
9476 if (canonical_end.lsals.empty ())
9477 error (_("Could not find location of the end of the range."));
9478
9479 const linespec_sals &lsal_end = canonical_end.lsals[0];
9480 if (canonical_end.lsals.size () > 1
9481 || lsal_end.sals.size () != 1)
9482 error (_("Cannot create a ranged breakpoint with multiple locations."));
9483
9484 const symtab_and_line &sal_end = lsal_end.sals[0];
9485
9486 end = find_breakpoint_range_end (sal_end);
9487 if (sal_start.pc > end)
9488 error (_("Invalid address range, end precedes start."));
9489
9490 length = end - sal_start.pc + 1;
9491 if (length < 0)
9492 /* Length overflowed. */
9493 error (_("Address range too large."));
9494 else if (length == 1)
9495 {
9496 /* This range is simple enough to be handled by
9497 the `hbreak' command. */
9498 hbreak_command (&addr_string_start[0], 1);
9499
9500 return;
9501 }
9502
9503 /* Now set up the breakpoint and install it. */
9504
9505 std::unique_ptr<breakpoint> br
9506 (new ranged_breakpoint (get_current_arch (),
9507 sal_start, length,
9508 std::move (start_locspec),
9509 std::move (end_locspec)));
9510
9511 install_breakpoint (false, std::move (br), true);
9512 }
9513
9514 /* Return non-zero if EXP is verified as constant. Returned zero
9515 means EXP is variable. Also the constant detection may fail for
9516 some constant expressions and in such case still falsely return
9517 zero. */
9518
9519 static bool
9520 watchpoint_exp_is_const (const struct expression *exp)
9521 {
9522 return exp->op->constant_p ();
9523 }
9524
9525 /* Implement the "re_set" method for watchpoints. */
9526
9527 void
9528 watchpoint::re_set ()
9529 {
9530 /* Watchpoint can be either on expression using entirely global
9531 variables, or it can be on local variables.
9532
9533 Watchpoints of the first kind are never auto-deleted, and even
9534 persist across program restarts. Since they can use variables
9535 from shared libraries, we need to reparse expression as libraries
9536 are loaded and unloaded.
9537
9538 Watchpoints on local variables can also change meaning as result
9539 of solib event. For example, if a watchpoint uses both a local
9540 and a global variables in expression, it's a local watchpoint,
9541 but unloading of a shared library will make the expression
9542 invalid. This is not a very common use case, but we still
9543 re-evaluate expression, to avoid surprises to the user.
9544
9545 Note that for local watchpoints, we re-evaluate it only if
9546 watchpoints frame id is still valid. If it's not, it means the
9547 watchpoint is out of scope and will be deleted soon. In fact,
9548 I'm not sure we'll ever be called in this case.
9549
9550 If a local watchpoint's frame id is still valid, then
9551 exp_valid_block is likewise valid, and we can safely use it.
9552
9553 Don't do anything about disabled watchpoints, since they will be
9554 reevaluated again when enabled. */
9555 update_watchpoint (this, true /* reparse */);
9556 }
9557
9558 /* Implement the "insert" method for hardware watchpoints. */
9559
9560 int
9561 watchpoint::insert_location (struct bp_location *bl)
9562 {
9563 int length = exact ? 1 : bl->length;
9564
9565 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9566 cond_exp.get ());
9567 }
9568
9569 /* Implement the "remove" method for hardware watchpoints. */
9570
9571 int
9572 watchpoint::remove_location (struct bp_location *bl,
9573 enum remove_bp_reason reason)
9574 {
9575 int length = exact ? 1 : bl->length;
9576
9577 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9578 cond_exp.get ());
9579 }
9580
9581 int
9582 watchpoint::breakpoint_hit (const struct bp_location *bl,
9583 const address_space *aspace, CORE_ADDR bp_addr,
9584 const target_waitstatus &ws)
9585 {
9586 struct breakpoint *b = bl->owner;
9587
9588 /* Continuable hardware watchpoints are treated as non-existent if the
9589 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9590 some data address). Otherwise gdb won't stop on a break instruction
9591 in the code (not from a breakpoint) when a hardware watchpoint has
9592 been defined. Also skip watchpoints which we know did not trigger
9593 (did not match the data address). */
9594 if (is_hardware_watchpoint (b)
9595 && watchpoint_triggered == watch_triggered_no)
9596 return 0;
9597
9598 return 1;
9599 }
9600
9601 void
9602 watchpoint::check_status (bpstat *bs)
9603 {
9604 gdb_assert (is_watchpoint (bs->breakpoint_at));
9605
9606 bpstat_check_watchpoint (bs);
9607 }
9608
9609 /* Implement the "resources_needed" method for hardware
9610 watchpoints. */
9611
9612 int
9613 watchpoint::resources_needed (const struct bp_location *bl)
9614 {
9615 int length = exact? 1 : bl->length;
9616
9617 return target_region_ok_for_hw_watchpoint (bl->address, length);
9618 }
9619
9620 /* Implement the "works_in_software_mode" method for hardware
9621 watchpoints. */
9622
9623 bool
9624 watchpoint::works_in_software_mode () const
9625 {
9626 /* Read and access watchpoints only work with hardware support. */
9627 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9628 }
9629
9630 enum print_stop_action
9631 watchpoint::print_it (const bpstat *bs) const
9632 {
9633 struct breakpoint *b;
9634 enum print_stop_action result;
9635 struct ui_out *uiout = current_uiout;
9636
9637 gdb_assert (bs->bp_location_at != NULL);
9638
9639 b = bs->breakpoint_at;
9640
9641 annotate_watchpoint (b->number);
9642 maybe_print_thread_hit_breakpoint (uiout);
9643
9644 string_file stb;
9645
9646 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9647 switch (b->type)
9648 {
9649 case bp_watchpoint:
9650 case bp_hardware_watchpoint:
9651 if (uiout->is_mi_like_p ())
9652 uiout->field_string
9653 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9654 mention (b);
9655 tuple_emitter.emplace (uiout, "value");
9656 uiout->text ("\nOld value = ");
9657 watchpoint_value_print (bs->old_val.get (), &stb);
9658 uiout->field_stream ("old", stb);
9659 uiout->text ("\nNew value = ");
9660 watchpoint_value_print (val.get (), &stb);
9661 uiout->field_stream ("new", stb);
9662 uiout->text ("\n");
9663 /* More than one watchpoint may have been triggered. */
9664 result = PRINT_UNKNOWN;
9665 break;
9666
9667 case bp_read_watchpoint:
9668 if (uiout->is_mi_like_p ())
9669 uiout->field_string
9670 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9671 mention (b);
9672 tuple_emitter.emplace (uiout, "value");
9673 uiout->text ("\nValue = ");
9674 watchpoint_value_print (val.get (), &stb);
9675 uiout->field_stream ("value", stb);
9676 uiout->text ("\n");
9677 result = PRINT_UNKNOWN;
9678 break;
9679
9680 case bp_access_watchpoint:
9681 if (bs->old_val != NULL)
9682 {
9683 if (uiout->is_mi_like_p ())
9684 uiout->field_string
9685 ("reason",
9686 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9687 mention (b);
9688 tuple_emitter.emplace (uiout, "value");
9689 uiout->text ("\nOld value = ");
9690 watchpoint_value_print (bs->old_val.get (), &stb);
9691 uiout->field_stream ("old", stb);
9692 uiout->text ("\nNew value = ");
9693 }
9694 else
9695 {
9696 mention (b);
9697 if (uiout->is_mi_like_p ())
9698 uiout->field_string
9699 ("reason",
9700 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9701 tuple_emitter.emplace (uiout, "value");
9702 uiout->text ("\nValue = ");
9703 }
9704 watchpoint_value_print (val.get (), &stb);
9705 uiout->field_stream ("new", stb);
9706 uiout->text ("\n");
9707 result = PRINT_UNKNOWN;
9708 break;
9709 default:
9710 result = PRINT_UNKNOWN;
9711 }
9712
9713 return result;
9714 }
9715
9716 /* Implement the "print_mention" method for hardware watchpoints. */
9717
9718 void
9719 watchpoint::print_mention () const
9720 {
9721 struct ui_out *uiout = current_uiout;
9722 const char *tuple_name;
9723
9724 switch (type)
9725 {
9726 case bp_watchpoint:
9727 uiout->text ("Watchpoint ");
9728 tuple_name = "wpt";
9729 break;
9730 case bp_hardware_watchpoint:
9731 uiout->text ("Hardware watchpoint ");
9732 tuple_name = "wpt";
9733 break;
9734 case bp_read_watchpoint:
9735 uiout->text ("Hardware read watchpoint ");
9736 tuple_name = "hw-rwpt";
9737 break;
9738 case bp_access_watchpoint:
9739 uiout->text ("Hardware access (read/write) watchpoint ");
9740 tuple_name = "hw-awpt";
9741 break;
9742 default:
9743 internal_error (_("Invalid hardware watchpoint type."));
9744 }
9745
9746 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9747 uiout->field_signed ("number", number);
9748 uiout->text (": ");
9749 uiout->field_string ("exp", exp_string.get ());
9750 }
9751
9752 /* Implement the "print_recreate" method for watchpoints. */
9753
9754 void
9755 watchpoint::print_recreate (struct ui_file *fp) const
9756 {
9757 switch (type)
9758 {
9759 case bp_watchpoint:
9760 case bp_hardware_watchpoint:
9761 gdb_printf (fp, "watch");
9762 break;
9763 case bp_read_watchpoint:
9764 gdb_printf (fp, "rwatch");
9765 break;
9766 case bp_access_watchpoint:
9767 gdb_printf (fp, "awatch");
9768 break;
9769 default:
9770 internal_error (_("Invalid watchpoint type."));
9771 }
9772
9773 gdb_printf (fp, " %s", exp_string.get ());
9774 print_recreate_thread (fp);
9775 }
9776
9777 /* Implement the "explains_signal" method for watchpoints. */
9778
9779 bool
9780 watchpoint::explains_signal (enum gdb_signal sig)
9781 {
9782 /* A software watchpoint cannot cause a signal other than
9783 GDB_SIGNAL_TRAP. */
9784 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
9785 return false;
9786
9787 return true;
9788 }
9789
9790 struct masked_watchpoint : public watchpoint
9791 {
9792 using watchpoint::watchpoint;
9793
9794 int insert_location (struct bp_location *) override;
9795 int remove_location (struct bp_location *,
9796 enum remove_bp_reason reason) override;
9797 int resources_needed (const struct bp_location *) override;
9798 bool works_in_software_mode () const override;
9799 enum print_stop_action print_it (const bpstat *bs) const override;
9800 void print_one_detail (struct ui_out *) const override;
9801 void print_mention () const override;
9802 void print_recreate (struct ui_file *fp) const override;
9803 };
9804
9805 /* Implement the "insert" method for masked hardware watchpoints. */
9806
9807 int
9808 masked_watchpoint::insert_location (struct bp_location *bl)
9809 {
9810 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
9811 bl->watchpoint_type);
9812 }
9813
9814 /* Implement the "remove" method for masked hardware watchpoints. */
9815
9816 int
9817 masked_watchpoint::remove_location (struct bp_location *bl,
9818 enum remove_bp_reason reason)
9819 {
9820 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
9821 bl->watchpoint_type);
9822 }
9823
9824 /* Implement the "resources_needed" method for masked hardware
9825 watchpoints. */
9826
9827 int
9828 masked_watchpoint::resources_needed (const struct bp_location *bl)
9829 {
9830 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
9831 }
9832
9833 /* Implement the "works_in_software_mode" method for masked hardware
9834 watchpoints. */
9835
9836 bool
9837 masked_watchpoint::works_in_software_mode () const
9838 {
9839 return false;
9840 }
9841
9842 /* Implement the "print_it" method for masked hardware
9843 watchpoints. */
9844
9845 enum print_stop_action
9846 masked_watchpoint::print_it (const bpstat *bs) const
9847 {
9848 struct breakpoint *b = bs->breakpoint_at;
9849 struct ui_out *uiout = current_uiout;
9850
9851 /* Masked watchpoints have only one location. */
9852 gdb_assert (b->loc && b->loc->next == NULL);
9853
9854 annotate_watchpoint (b->number);
9855 maybe_print_thread_hit_breakpoint (uiout);
9856
9857 switch (b->type)
9858 {
9859 case bp_hardware_watchpoint:
9860 if (uiout->is_mi_like_p ())
9861 uiout->field_string
9862 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9863 break;
9864
9865 case bp_read_watchpoint:
9866 if (uiout->is_mi_like_p ())
9867 uiout->field_string
9868 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9869 break;
9870
9871 case bp_access_watchpoint:
9872 if (uiout->is_mi_like_p ())
9873 uiout->field_string
9874 ("reason",
9875 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9876 break;
9877 default:
9878 internal_error (_("Invalid hardware watchpoint type."));
9879 }
9880
9881 mention (b);
9882 uiout->text (_("\n\
9883 Check the underlying instruction at PC for the memory\n\
9884 address and value which triggered this watchpoint.\n"));
9885 uiout->text ("\n");
9886
9887 /* More than one watchpoint may have been triggered. */
9888 return PRINT_UNKNOWN;
9889 }
9890
9891 /* Implement the "print_one_detail" method for masked hardware
9892 watchpoints. */
9893
9894 void
9895 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9896 {
9897 /* Masked watchpoints have only one location. */
9898 gdb_assert (loc && loc->next == NULL);
9899
9900 uiout->text ("\tmask ");
9901 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
9902 uiout->text ("\n");
9903 }
9904
9905 /* Implement the "print_mention" method for masked hardware
9906 watchpoints. */
9907
9908 void
9909 masked_watchpoint::print_mention () const
9910 {
9911 struct ui_out *uiout = current_uiout;
9912 const char *tuple_name;
9913
9914 switch (type)
9915 {
9916 case bp_hardware_watchpoint:
9917 uiout->text ("Masked hardware watchpoint ");
9918 tuple_name = "wpt";
9919 break;
9920 case bp_read_watchpoint:
9921 uiout->text ("Masked hardware read watchpoint ");
9922 tuple_name = "hw-rwpt";
9923 break;
9924 case bp_access_watchpoint:
9925 uiout->text ("Masked hardware access (read/write) watchpoint ");
9926 tuple_name = "hw-awpt";
9927 break;
9928 default:
9929 internal_error (_("Invalid hardware watchpoint type."));
9930 }
9931
9932 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9933 uiout->field_signed ("number", number);
9934 uiout->text (": ");
9935 uiout->field_string ("exp", exp_string.get ());
9936 }
9937
9938 /* Implement the "print_recreate" method for masked hardware
9939 watchpoints. */
9940
9941 void
9942 masked_watchpoint::print_recreate (struct ui_file *fp) const
9943 {
9944 switch (type)
9945 {
9946 case bp_hardware_watchpoint:
9947 gdb_printf (fp, "watch");
9948 break;
9949 case bp_read_watchpoint:
9950 gdb_printf (fp, "rwatch");
9951 break;
9952 case bp_access_watchpoint:
9953 gdb_printf (fp, "awatch");
9954 break;
9955 default:
9956 internal_error (_("Invalid hardware watchpoint type."));
9957 }
9958
9959 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9960 phex (hw_wp_mask, sizeof (CORE_ADDR)));
9961 print_recreate_thread (fp);
9962 }
9963
9964 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
9965
9966 static bool
9967 is_masked_watchpoint (const struct breakpoint *b)
9968 {
9969 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9970 }
9971
9972 /* accessflag: hw_write: watch write,
9973 hw_read: watch read,
9974 hw_access: watch access (read or write) */
9975 static void
9976 watch_command_1 (const char *arg, int accessflag, int from_tty,
9977 bool just_location, bool internal)
9978 {
9979 struct breakpoint *scope_breakpoint = NULL;
9980 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
9981 struct value *result;
9982 int saved_bitpos = 0, saved_bitsize = 0;
9983 const char *exp_start = NULL;
9984 const char *exp_end = NULL;
9985 const char *tok, *end_tok;
9986 int toklen = -1;
9987 const char *cond_start = NULL;
9988 const char *cond_end = NULL;
9989 enum bptype bp_type;
9990 int thread = -1;
9991 /* Flag to indicate whether we are going to use masks for
9992 the hardware watchpoint. */
9993 bool use_mask = false;
9994 CORE_ADDR mask = 0;
9995 int task = 0;
9996
9997 /* Make sure that we actually have parameters to parse. */
9998 if (arg != NULL && arg[0] != '\0')
9999 {
10000 const char *value_start;
10001
10002 exp_end = arg + strlen (arg);
10003
10004 /* Look for "parameter value" pairs at the end
10005 of the arguments string. */
10006 for (tok = exp_end - 1; tok > arg; tok--)
10007 {
10008 /* Skip whitespace at the end of the argument list. */
10009 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10010 tok--;
10011
10012 /* Find the beginning of the last token.
10013 This is the value of the parameter. */
10014 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10015 tok--;
10016 value_start = tok + 1;
10017
10018 /* Skip whitespace. */
10019 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10020 tok--;
10021
10022 end_tok = tok;
10023
10024 /* Find the beginning of the second to last token.
10025 This is the parameter itself. */
10026 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10027 tok--;
10028 tok++;
10029 toklen = end_tok - tok + 1;
10030
10031 if (toklen == 6 && startswith (tok, "thread"))
10032 {
10033 struct thread_info *thr;
10034 /* At this point we've found a "thread" token, which means
10035 the user is trying to set a watchpoint that triggers
10036 only in a specific thread. */
10037 const char *endp;
10038
10039 if (thread != -1)
10040 error(_("You can specify only one thread."));
10041
10042 /* Extract the thread ID from the next token. */
10043 thr = parse_thread_id (value_start, &endp);
10044
10045 /* Check if the user provided a valid thread ID. */
10046 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10047 invalid_thread_id_error (value_start);
10048
10049 thread = thr->global_num;
10050 }
10051 else if (toklen == 4 && startswith (tok, "task"))
10052 {
10053 char *tmp;
10054
10055 task = strtol (value_start, &tmp, 0);
10056 if (tmp == value_start)
10057 error (_("Junk after task keyword."));
10058 if (!valid_task_id (task))
10059 error (_("Unknown task %d."), task);
10060 }
10061 else if (toklen == 4 && startswith (tok, "mask"))
10062 {
10063 /* We've found a "mask" token, which means the user wants to
10064 create a hardware watchpoint that is going to have the mask
10065 facility. */
10066 struct value *mask_value;
10067
10068 if (use_mask)
10069 error(_("You can specify only one mask."));
10070
10071 use_mask = just_location = true;
10072
10073 scoped_value_mark mark;
10074 mask_value = parse_to_comma_and_eval (&value_start);
10075 mask = value_as_address (mask_value);
10076 }
10077 else
10078 /* We didn't recognize what we found. We should stop here. */
10079 break;
10080
10081 /* Truncate the string and get rid of the "parameter value" pair before
10082 the arguments string is parsed by the parse_exp_1 function. */
10083 exp_end = tok;
10084 }
10085 }
10086 else
10087 exp_end = arg;
10088
10089 /* Parse the rest of the arguments. From here on out, everything
10090 is in terms of a newly allocated string instead of the original
10091 ARG. */
10092 std::string expression (arg, exp_end - arg);
10093 exp_start = arg = expression.c_str ();
10094 innermost_block_tracker tracker;
10095 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10096 exp_end = arg;
10097 /* Remove trailing whitespace from the expression before saving it.
10098 This makes the eventual display of the expression string a bit
10099 prettier. */
10100 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10101 --exp_end;
10102
10103 /* Checking if the expression is not constant. */
10104 if (watchpoint_exp_is_const (exp.get ()))
10105 {
10106 int len;
10107
10108 len = exp_end - exp_start;
10109 while (len > 0 && isspace (exp_start[len - 1]))
10110 len--;
10111 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10112 }
10113
10114 exp_valid_block = tracker.block ();
10115 struct value *mark = value_mark ();
10116 struct value *val_as_value = nullptr;
10117 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10118 just_location);
10119
10120 if (val_as_value != NULL && just_location)
10121 {
10122 saved_bitpos = value_bitpos (val_as_value);
10123 saved_bitsize = value_bitsize (val_as_value);
10124 }
10125
10126 value_ref_ptr val;
10127 if (just_location)
10128 {
10129 int ret;
10130
10131 exp_valid_block = NULL;
10132 val = release_value (value_addr (result));
10133 value_free_to_mark (mark);
10134
10135 if (use_mask)
10136 {
10137 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10138 mask);
10139 if (ret == -1)
10140 error (_("This target does not support masked watchpoints."));
10141 else if (ret == -2)
10142 error (_("Invalid mask or memory region."));
10143 }
10144 }
10145 else if (val_as_value != NULL)
10146 val = release_value (val_as_value);
10147
10148 tok = skip_spaces (arg);
10149 end_tok = skip_to_space (tok);
10150
10151 toklen = end_tok - tok;
10152 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10153 {
10154 tok = cond_start = end_tok + 1;
10155 innermost_block_tracker if_tracker;
10156 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10157
10158 /* The watchpoint expression may not be local, but the condition
10159 may still be. E.g.: `watch global if local > 0'. */
10160 cond_exp_valid_block = if_tracker.block ();
10161
10162 cond_end = tok;
10163 }
10164 if (*tok)
10165 error (_("Junk at end of command."));
10166
10167 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
10168
10169 /* Save this because create_internal_breakpoint below invalidates
10170 'wp_frame'. */
10171 frame_id watchpoint_frame = get_frame_id (wp_frame);
10172
10173 /* If the expression is "local", then set up a "watchpoint scope"
10174 breakpoint at the point where we've left the scope of the watchpoint
10175 expression. Create the scope breakpoint before the watchpoint, so
10176 that we will encounter it first in bpstat_stop_status. */
10177 if (exp_valid_block != NULL && wp_frame != NULL)
10178 {
10179 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10180
10181 if (frame_id_p (caller_frame_id))
10182 {
10183 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10184 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10185
10186 scope_breakpoint
10187 = create_internal_breakpoint (caller_arch, caller_pc,
10188 bp_watchpoint_scope);
10189
10190 /* create_internal_breakpoint could invalidate WP_FRAME. */
10191 wp_frame = NULL;
10192
10193 scope_breakpoint->enable_state = bp_enabled;
10194
10195 /* Automatically delete the breakpoint when it hits. */
10196 scope_breakpoint->disposition = disp_del;
10197
10198 /* Only break in the proper frame (help with recursion). */
10199 scope_breakpoint->frame_id = caller_frame_id;
10200
10201 /* Set the address at which we will stop. */
10202 scope_breakpoint->loc->gdbarch = caller_arch;
10203 scope_breakpoint->loc->requested_address = caller_pc;
10204 scope_breakpoint->loc->address
10205 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10206 scope_breakpoint->loc->requested_address,
10207 scope_breakpoint->type,
10208 current_program_space);
10209 }
10210 }
10211
10212 /* Now set up the breakpoint. We create all watchpoints as hardware
10213 watchpoints here even if hardware watchpoints are turned off, a call
10214 to update_watchpoint later in this function will cause the type to
10215 drop back to bp_watchpoint (software watchpoint) if required. */
10216
10217 if (accessflag == hw_read)
10218 bp_type = bp_read_watchpoint;
10219 else if (accessflag == hw_access)
10220 bp_type = bp_access_watchpoint;
10221 else
10222 bp_type = bp_hardware_watchpoint;
10223
10224 std::unique_ptr<watchpoint> w;
10225 if (use_mask)
10226 w.reset (new masked_watchpoint (nullptr, bp_type));
10227 else
10228 w.reset (new watchpoint (nullptr, bp_type));
10229
10230 w->thread = thread;
10231 w->task = task;
10232 w->disposition = disp_donttouch;
10233 w->pspace = current_program_space;
10234 w->exp = std::move (exp);
10235 w->exp_valid_block = exp_valid_block;
10236 w->cond_exp_valid_block = cond_exp_valid_block;
10237 if (just_location)
10238 {
10239 struct type *t = value_type (val.get ());
10240 CORE_ADDR addr = value_as_address (val.get ());
10241
10242 w->exp_string_reparse
10243 = current_language->watch_location_expression (t, addr);
10244
10245 w->exp_string = xstrprintf ("-location %.*s",
10246 (int) (exp_end - exp_start), exp_start);
10247 }
10248 else
10249 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10250
10251 if (use_mask)
10252 {
10253 w->hw_wp_mask = mask;
10254 }
10255 else
10256 {
10257 w->val = val;
10258 w->val_bitpos = saved_bitpos;
10259 w->val_bitsize = saved_bitsize;
10260 w->val_valid = true;
10261 }
10262
10263 if (cond_start)
10264 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10265 else
10266 w->cond_string = 0;
10267
10268 if (frame_id_p (watchpoint_frame))
10269 {
10270 w->watchpoint_frame = watchpoint_frame;
10271 w->watchpoint_thread = inferior_ptid;
10272 }
10273 else
10274 {
10275 w->watchpoint_frame = null_frame_id;
10276 w->watchpoint_thread = null_ptid;
10277 }
10278
10279 if (scope_breakpoint != NULL)
10280 {
10281 /* The scope breakpoint is related to the watchpoint. We will
10282 need to act on them together. */
10283 w->related_breakpoint = scope_breakpoint;
10284 scope_breakpoint->related_breakpoint = w.get ();
10285 }
10286
10287 if (!just_location)
10288 value_free_to_mark (mark);
10289
10290 /* Finally update the new watchpoint. This creates the locations
10291 that should be inserted. */
10292 update_watchpoint (w.get (), true /* reparse */);
10293
10294 install_breakpoint (internal, std::move (w), 1);
10295 }
10296
10297 /* Return count of debug registers needed to watch the given expression.
10298 If the watchpoint cannot be handled in hardware return zero. */
10299
10300 static int
10301 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10302 {
10303 int found_memory_cnt = 0;
10304
10305 /* Did the user specifically forbid us to use hardware watchpoints? */
10306 if (!can_use_hw_watchpoints)
10307 return 0;
10308
10309 gdb_assert (!vals.empty ());
10310 struct value *head = vals[0].get ();
10311
10312 /* Make sure that the value of the expression depends only upon
10313 memory contents, and values computed from them within GDB. If we
10314 find any register references or function calls, we can't use a
10315 hardware watchpoint.
10316
10317 The idea here is that evaluating an expression generates a series
10318 of values, one holding the value of every subexpression. (The
10319 expression a*b+c has five subexpressions: a, b, a*b, c, and
10320 a*b+c.) GDB's values hold almost enough information to establish
10321 the criteria given above --- they identify memory lvalues,
10322 register lvalues, computed values, etcetera. So we can evaluate
10323 the expression, and then scan the chain of values that leaves
10324 behind to decide whether we can detect any possible change to the
10325 expression's final value using only hardware watchpoints.
10326
10327 However, I don't think that the values returned by inferior
10328 function calls are special in any way. So this function may not
10329 notice that an expression involving an inferior function call
10330 can't be watched with hardware watchpoints. FIXME. */
10331 for (const value_ref_ptr &iter : vals)
10332 {
10333 struct value *v = iter.get ();
10334
10335 if (VALUE_LVAL (v) == lval_memory)
10336 {
10337 if (v != head && value_lazy (v))
10338 /* A lazy memory lvalue in the chain is one that GDB never
10339 needed to fetch; we either just used its address (e.g.,
10340 `a' in `a.b') or we never needed it at all (e.g., `a'
10341 in `a,b'). This doesn't apply to HEAD; if that is
10342 lazy then it was not readable, but watch it anyway. */
10343 ;
10344 else
10345 {
10346 /* Ahh, memory we actually used! Check if we can cover
10347 it with hardware watchpoints. */
10348 struct type *vtype = check_typedef (value_type (v));
10349
10350 /* We only watch structs and arrays if user asked for it
10351 explicitly, never if they just happen to appear in a
10352 middle of some value chain. */
10353 if (v == head
10354 || (vtype->code () != TYPE_CODE_STRUCT
10355 && vtype->code () != TYPE_CODE_ARRAY))
10356 {
10357 CORE_ADDR vaddr = value_address (v);
10358 int len;
10359 int num_regs;
10360
10361 len = (target_exact_watchpoints
10362 && is_scalar_type_recursive (vtype))?
10363 1 : value_type (v)->length ();
10364
10365 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10366 if (!num_regs)
10367 return 0;
10368 else
10369 found_memory_cnt += num_regs;
10370 }
10371 }
10372 }
10373 else if (VALUE_LVAL (v) != not_lval
10374 && deprecated_value_modifiable (v) == 0)
10375 return 0; /* These are values from the history (e.g., $1). */
10376 else if (VALUE_LVAL (v) == lval_register)
10377 return 0; /* Cannot watch a register with a HW watchpoint. */
10378 }
10379
10380 /* The expression itself looks suitable for using a hardware
10381 watchpoint, but give the target machine a chance to reject it. */
10382 return found_memory_cnt;
10383 }
10384
10385 void
10386 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10387 {
10388 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10389 }
10390
10391 /* Options for the watch, awatch, and rwatch commands. */
10392
10393 struct watch_options
10394 {
10395 /* For -location. */
10396 bool location = false;
10397 };
10398
10399 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10400
10401 Historically GDB always accepted both '-location' and '-l' flags for
10402 these commands (both flags being synonyms). When converting to the
10403 newer option scheme only '-location' is added here. That's fine (for
10404 backward compatibility) as any non-ambiguous prefix of a flag will be
10405 accepted, so '-l', '-loc', are now all accepted.
10406
10407 What this means is that, if in the future, we add any new flag here
10408 that starts with '-l' then this will break backward compatibility, so
10409 please, don't do that! */
10410
10411 static const gdb::option::option_def watch_option_defs[] = {
10412 gdb::option::flag_option_def<watch_options> {
10413 "location",
10414 [] (watch_options *opt) { return &opt->location; },
10415 N_("\
10416 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10417 -l can be used as a short form of -location."),
10418 },
10419 };
10420
10421 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10422 commands. */
10423
10424 static gdb::option::option_def_group
10425 make_watch_options_def_group (watch_options *opts)
10426 {
10427 return {{watch_option_defs}, opts};
10428 }
10429
10430 /* A helper function that looks for the "-location" argument and then
10431 calls watch_command_1. */
10432
10433 static void
10434 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10435 {
10436 watch_options opts;
10437 auto grp = make_watch_options_def_group (&opts);
10438 gdb::option::process_options
10439 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10440 if (arg != nullptr && *arg == '\0')
10441 arg = nullptr;
10442
10443 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10444 }
10445
10446 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10447 static void
10448 watch_command_completer (struct cmd_list_element *ignore,
10449 completion_tracker &tracker,
10450 const char *text, const char * /*word*/)
10451 {
10452 const auto group = make_watch_options_def_group (nullptr);
10453 if (gdb::option::complete_options
10454 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10455 return;
10456
10457 const char *word = advance_to_expression_complete_word_point (tracker, text);
10458 expression_completer (ignore, tracker, text, word);
10459 }
10460
10461 static void
10462 watch_command (const char *arg, int from_tty)
10463 {
10464 watch_maybe_just_location (arg, hw_write, from_tty);
10465 }
10466
10467 void
10468 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10469 {
10470 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10471 }
10472
10473 static void
10474 rwatch_command (const char *arg, int from_tty)
10475 {
10476 watch_maybe_just_location (arg, hw_read, from_tty);
10477 }
10478
10479 void
10480 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10481 {
10482 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10483 }
10484
10485 static void
10486 awatch_command (const char *arg, int from_tty)
10487 {
10488 watch_maybe_just_location (arg, hw_access, from_tty);
10489 }
10490 \f
10491
10492 /* Data for the FSM that manages the until(location)/advance commands
10493 in infcmd.c. Here because it uses the mechanisms of
10494 breakpoints. */
10495
10496 struct until_break_fsm : public thread_fsm
10497 {
10498 /* The thread that was current when the command was executed. */
10499 int thread;
10500
10501 /* The breakpoint set at the return address in the caller frame,
10502 plus breakpoints at all the destination locations. */
10503 std::vector<breakpoint_up> breakpoints;
10504
10505 until_break_fsm (struct interp *cmd_interp, int thread,
10506 std::vector<breakpoint_up> &&breakpoints)
10507 : thread_fsm (cmd_interp),
10508 thread (thread),
10509 breakpoints (std::move (breakpoints))
10510 {
10511 }
10512
10513 void clean_up (struct thread_info *thread) override;
10514 bool should_stop (struct thread_info *thread) override;
10515 enum async_reply_reason do_async_reply_reason () override;
10516 };
10517
10518 /* Implementation of the 'should_stop' FSM method for the
10519 until(location)/advance commands. */
10520
10521 bool
10522 until_break_fsm::should_stop (struct thread_info *tp)
10523 {
10524 for (const breakpoint_up &bp : breakpoints)
10525 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10526 bp.get ()) != NULL)
10527 {
10528 set_finished ();
10529 break;
10530 }
10531
10532 return true;
10533 }
10534
10535 /* Implementation of the 'clean_up' FSM method for the
10536 until(location)/advance commands. */
10537
10538 void
10539 until_break_fsm::clean_up (struct thread_info *)
10540 {
10541 /* Clean up our temporary breakpoints. */
10542 breakpoints.clear ();
10543 delete_longjmp_breakpoint (thread);
10544 }
10545
10546 /* Implementation of the 'async_reply_reason' FSM method for the
10547 until(location)/advance commands. */
10548
10549 enum async_reply_reason
10550 until_break_fsm::do_async_reply_reason ()
10551 {
10552 return EXEC_ASYNC_LOCATION_REACHED;
10553 }
10554
10555 void
10556 until_break_command (const char *arg, int from_tty, int anywhere)
10557 {
10558 frame_info_ptr frame;
10559 struct gdbarch *frame_gdbarch;
10560 struct frame_id stack_frame_id;
10561 struct frame_id caller_frame_id;
10562 int thread;
10563 struct thread_info *tp;
10564
10565 clear_proceed_status (0);
10566
10567 /* Set a breakpoint where the user wants it and at return from
10568 this function. */
10569
10570 location_spec_up locspec = string_to_location_spec (&arg, current_language);
10571
10572 std::vector<symtab_and_line> sals
10573 = (last_displayed_sal_is_valid ()
10574 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10575 get_last_displayed_symtab (),
10576 get_last_displayed_line ())
10577 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
10578 NULL, NULL, 0));
10579
10580 if (sals.empty ())
10581 error (_("Couldn't get information on specified line."));
10582
10583 if (*arg)
10584 error (_("Junk at end of arguments."));
10585
10586 tp = inferior_thread ();
10587 thread = tp->global_num;
10588
10589 /* Note linespec handling above invalidates the frame chain.
10590 Installing a breakpoint also invalidates the frame chain (as it
10591 may need to switch threads), so do any frame handling before
10592 that. */
10593
10594 frame = get_selected_frame (NULL);
10595 frame_gdbarch = get_frame_arch (frame);
10596 stack_frame_id = get_stack_frame_id (frame);
10597 caller_frame_id = frame_unwind_caller_id (frame);
10598
10599 /* Keep within the current frame, or in frames called by the current
10600 one. */
10601
10602 std::vector<breakpoint_up> breakpoints;
10603
10604 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10605
10606 if (frame_id_p (caller_frame_id))
10607 {
10608 struct symtab_and_line sal2;
10609 struct gdbarch *caller_gdbarch;
10610
10611 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10612 sal2.pc = frame_unwind_caller_pc (frame);
10613 caller_gdbarch = frame_unwind_caller_arch (frame);
10614
10615 breakpoint_up caller_breakpoint
10616 = set_momentary_breakpoint (caller_gdbarch, sal2,
10617 caller_frame_id, bp_until);
10618 breakpoints.emplace_back (std::move (caller_breakpoint));
10619
10620 set_longjmp_breakpoint (tp, stack_frame_id);
10621 lj_deleter.emplace (thread);
10622 }
10623
10624 /* set_momentary_breakpoint could invalidate FRAME. */
10625 frame = NULL;
10626
10627 /* If the user told us to continue until a specified location, we
10628 don't specify a frame at which we need to stop. Otherwise,
10629 specify the selected frame, because we want to stop only at the
10630 very same frame. */
10631 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10632
10633 for (symtab_and_line &sal : sals)
10634 {
10635 resolve_sal_pc (&sal);
10636
10637 breakpoint_up location_breakpoint
10638 = set_momentary_breakpoint (frame_gdbarch, sal,
10639 stop_frame_id, bp_until);
10640 breakpoints.emplace_back (std::move (location_breakpoint));
10641 }
10642
10643 tp->set_thread_fsm
10644 (std::unique_ptr<thread_fsm>
10645 (new until_break_fsm (command_interp (), tp->global_num,
10646 std::move (breakpoints))));
10647
10648 if (lj_deleter)
10649 lj_deleter->release ();
10650
10651 proceed (-1, GDB_SIGNAL_DEFAULT);
10652 }
10653
10654 \f
10655
10656 /* Compare two breakpoints and return a strcmp-like result. */
10657
10658 static int
10659 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10660 {
10661 uintptr_t ua = (uintptr_t) a;
10662 uintptr_t ub = (uintptr_t) b;
10663
10664 if (a->number < b->number)
10665 return -1;
10666 else if (a->number > b->number)
10667 return 1;
10668
10669 /* Now sort by address, in case we see, e..g, two breakpoints with
10670 the number 0. */
10671 if (ua < ub)
10672 return -1;
10673 return ua > ub ? 1 : 0;
10674 }
10675
10676 /* Delete breakpoints by address or line. */
10677
10678 static void
10679 clear_command (const char *arg, int from_tty)
10680 {
10681 int default_match;
10682
10683 std::vector<symtab_and_line> decoded_sals;
10684 symtab_and_line last_sal;
10685 gdb::array_view<symtab_and_line> sals;
10686 if (arg)
10687 {
10688 decoded_sals
10689 = decode_line_with_current_source (arg,
10690 (DECODE_LINE_FUNFIRSTLINE
10691 | DECODE_LINE_LIST_MODE));
10692 default_match = 0;
10693 sals = decoded_sals;
10694 }
10695 else
10696 {
10697 /* Set sal's line, symtab, pc, and pspace to the values
10698 corresponding to the last call to print_frame_info. If the
10699 codepoint is not valid, this will set all the fields to 0. */
10700 last_sal = get_last_displayed_sal ();
10701 if (last_sal.symtab == 0)
10702 error (_("No source file specified."));
10703
10704 default_match = 1;
10705 sals = last_sal;
10706 }
10707
10708 /* We don't call resolve_sal_pc here. That's not as bad as it
10709 seems, because all existing breakpoints typically have both
10710 file/line and pc set. So, if clear is given file/line, we can
10711 match this to existing breakpoint without obtaining pc at all.
10712
10713 We only support clearing given the address explicitly
10714 present in breakpoint table. Say, we've set breakpoint
10715 at file:line. There were several PC values for that file:line,
10716 due to optimization, all in one block.
10717
10718 We've picked one PC value. If "clear" is issued with another
10719 PC corresponding to the same file:line, the breakpoint won't
10720 be cleared. We probably can still clear the breakpoint, but
10721 since the other PC value is never presented to user, user
10722 can only find it by guessing, and it does not seem important
10723 to support that. */
10724
10725 /* For each line spec given, delete bps which correspond to it. Do
10726 it in two passes, solely to preserve the current behavior that
10727 from_tty is forced true if we delete more than one
10728 breakpoint. */
10729
10730 std::vector<struct breakpoint *> found;
10731 for (const auto &sal : sals)
10732 {
10733 const char *sal_fullname;
10734
10735 /* If exact pc given, clear bpts at that pc.
10736 If line given (pc == 0), clear all bpts on specified line.
10737 If defaulting, clear all bpts on default line
10738 or at default pc.
10739
10740 defaulting sal.pc != 0 tests to do
10741
10742 0 1 pc
10743 1 1 pc _and_ line
10744 0 0 line
10745 1 0 <can't happen> */
10746
10747 sal_fullname = (sal.symtab == NULL
10748 ? NULL : symtab_to_fullname (sal.symtab));
10749
10750 /* Find all matching breakpoints and add them to 'found'. */
10751 for (breakpoint *b : all_breakpoints ())
10752 {
10753 int match = 0;
10754 /* Are we going to delete b? */
10755 if (b->type != bp_none && !is_watchpoint (b)
10756 && user_breakpoint_p (b))
10757 {
10758 for (bp_location *loc : b->locations ())
10759 {
10760 /* If the user specified file:line, don't allow a PC
10761 match. This matches historical gdb behavior. */
10762 int pc_match = (!sal.explicit_line
10763 && sal.pc
10764 && (loc->pspace == sal.pspace)
10765 && (loc->address == sal.pc)
10766 && (!section_is_overlay (loc->section)
10767 || loc->section == sal.section));
10768 int line_match = 0;
10769
10770 if ((default_match || sal.explicit_line)
10771 && loc->symtab != NULL
10772 && sal_fullname != NULL
10773 && sal.pspace == loc->pspace
10774 && loc->line_number == sal.line
10775 && filename_cmp (symtab_to_fullname (loc->symtab),
10776 sal_fullname) == 0)
10777 line_match = 1;
10778
10779 if (pc_match || line_match)
10780 {
10781 match = 1;
10782 break;
10783 }
10784 }
10785 }
10786
10787 if (match)
10788 found.push_back (b);
10789 }
10790 }
10791
10792 /* Now go thru the 'found' chain and delete them. */
10793 if (found.empty ())
10794 {
10795 if (arg)
10796 error (_("No breakpoint at %s."), arg);
10797 else
10798 error (_("No breakpoint at this line."));
10799 }
10800
10801 /* Remove duplicates from the vec. */
10802 std::sort (found.begin (), found.end (),
10803 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10804 {
10805 return compare_breakpoints (bp_a, bp_b) < 0;
10806 });
10807 found.erase (std::unique (found.begin (), found.end (),
10808 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10809 {
10810 return compare_breakpoints (bp_a, bp_b) == 0;
10811 }),
10812 found.end ());
10813
10814 if (found.size () > 1)
10815 from_tty = 1; /* Always report if deleted more than one. */
10816 if (from_tty)
10817 {
10818 if (found.size () == 1)
10819 gdb_printf (_("Deleted breakpoint "));
10820 else
10821 gdb_printf (_("Deleted breakpoints "));
10822 }
10823
10824 for (breakpoint *iter : found)
10825 {
10826 if (from_tty)
10827 gdb_printf ("%d ", iter->number);
10828 delete_breakpoint (iter);
10829 }
10830 if (from_tty)
10831 gdb_putc ('\n');
10832 }
10833 \f
10834 /* Delete breakpoint in BS if they are `delete' breakpoints and
10835 all breakpoints that are marked for deletion, whether hit or not.
10836 This is called after any breakpoint is hit, or after errors. */
10837
10838 void
10839 breakpoint_auto_delete (bpstat *bs)
10840 {
10841 for (; bs; bs = bs->next)
10842 if (bs->breakpoint_at
10843 && bs->breakpoint_at->disposition == disp_del
10844 && bs->stop)
10845 delete_breakpoint (bs->breakpoint_at);
10846
10847 for (breakpoint *b : all_breakpoints_safe ())
10848 if (b->disposition == disp_del_at_next_stop)
10849 delete_breakpoint (b);
10850 }
10851
10852 /* A comparison function for bp_location AP and BP being interfaced to
10853 std::sort. Sort elements primarily by their ADDRESS (no matter what
10854 bl_address_is_meaningful says), secondarily by ordering first
10855 permanent elements and terciarily just ensuring the array is sorted
10856 stable way despite std::sort being an unstable algorithm. */
10857
10858 static int
10859 bp_location_is_less_than (const bp_location *a, const bp_location *b)
10860 {
10861 if (a->address != b->address)
10862 return a->address < b->address;
10863
10864 /* Sort locations at the same address by their pspace number, keeping
10865 locations of the same inferior (in a multi-inferior environment)
10866 grouped. */
10867
10868 if (a->pspace->num != b->pspace->num)
10869 return a->pspace->num < b->pspace->num;
10870
10871 /* Sort permanent breakpoints first. */
10872 if (a->permanent != b->permanent)
10873 return a->permanent > b->permanent;
10874
10875 /* Sort by type in order to make duplicate determination easier.
10876 See update_global_location_list. This is kept in sync with
10877 breakpoint_locations_match. */
10878 if (a->loc_type < b->loc_type)
10879 return true;
10880
10881 /* Likewise, for range-breakpoints, sort by length. */
10882 if (a->loc_type == bp_loc_hardware_breakpoint
10883 && b->loc_type == bp_loc_hardware_breakpoint
10884 && a->length < b->length)
10885 return true;
10886
10887 /* Make the internal GDB representation stable across GDB runs
10888 where A and B memory inside GDB can differ. Breakpoint locations of
10889 the same type at the same address can be sorted in arbitrary order. */
10890
10891 if (a->owner->number != b->owner->number)
10892 return a->owner->number < b->owner->number;
10893
10894 return a < b;
10895 }
10896
10897 /* Set bp_locations_placed_address_before_address_max and
10898 bp_locations_shadow_len_after_address_max according to the current
10899 content of the bp_locations array. */
10900
10901 static void
10902 bp_locations_target_extensions_update (void)
10903 {
10904 bp_locations_placed_address_before_address_max = 0;
10905 bp_locations_shadow_len_after_address_max = 0;
10906
10907 for (bp_location *bl : all_bp_locations ())
10908 {
10909 CORE_ADDR start, end, addr;
10910
10911 if (!bp_location_has_shadow (bl))
10912 continue;
10913
10914 start = bl->target_info.placed_address;
10915 end = start + bl->target_info.shadow_len;
10916
10917 gdb_assert (bl->address >= start);
10918 addr = bl->address - start;
10919 if (addr > bp_locations_placed_address_before_address_max)
10920 bp_locations_placed_address_before_address_max = addr;
10921
10922 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10923
10924 gdb_assert (bl->address < end);
10925 addr = end - bl->address;
10926 if (addr > bp_locations_shadow_len_after_address_max)
10927 bp_locations_shadow_len_after_address_max = addr;
10928 }
10929 }
10930
10931 /* Download tracepoint locations if they haven't been. */
10932
10933 static void
10934 download_tracepoint_locations (void)
10935 {
10936 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
10937
10938 scoped_restore_current_pspace_and_thread restore_pspace_thread;
10939
10940 for (breakpoint *b : all_tracepoints ())
10941 {
10942 struct tracepoint *t;
10943 bool bp_location_downloaded = false;
10944
10945 if ((b->type == bp_fast_tracepoint
10946 ? !may_insert_fast_tracepoints
10947 : !may_insert_tracepoints))
10948 continue;
10949
10950 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10951 {
10952 if (target_can_download_tracepoint ())
10953 can_download_tracepoint = TRIBOOL_TRUE;
10954 else
10955 can_download_tracepoint = TRIBOOL_FALSE;
10956 }
10957
10958 if (can_download_tracepoint == TRIBOOL_FALSE)
10959 break;
10960
10961 for (bp_location *bl : b->locations ())
10962 {
10963 /* In tracepoint, locations are _never_ duplicated, so
10964 should_be_inserted is equivalent to
10965 unduplicated_should_be_inserted. */
10966 if (!should_be_inserted (bl) || bl->inserted)
10967 continue;
10968
10969 switch_to_program_space_and_thread (bl->pspace);
10970
10971 target_download_tracepoint (bl);
10972
10973 bl->inserted = 1;
10974 bp_location_downloaded = true;
10975 }
10976 t = (struct tracepoint *) b;
10977 t->number_on_target = b->number;
10978 if (bp_location_downloaded)
10979 gdb::observers::breakpoint_modified.notify (b);
10980 }
10981 }
10982
10983 /* Swap the insertion/duplication state between two locations. */
10984
10985 static void
10986 swap_insertion (struct bp_location *left, struct bp_location *right)
10987 {
10988 const int left_inserted = left->inserted;
10989 const int left_duplicate = left->duplicate;
10990 const int left_needs_update = left->needs_update;
10991 const struct bp_target_info left_target_info = left->target_info;
10992
10993 /* Locations of tracepoints can never be duplicated. */
10994 if (is_tracepoint (left->owner))
10995 gdb_assert (!left->duplicate);
10996 if (is_tracepoint (right->owner))
10997 gdb_assert (!right->duplicate);
10998
10999 left->inserted = right->inserted;
11000 left->duplicate = right->duplicate;
11001 left->needs_update = right->needs_update;
11002 left->target_info = right->target_info;
11003 right->inserted = left_inserted;
11004 right->duplicate = left_duplicate;
11005 right->needs_update = left_needs_update;
11006 right->target_info = left_target_info;
11007 }
11008
11009 /* Force the re-insertion of the locations at ADDRESS. This is called
11010 once a new/deleted/modified duplicate location is found and we are evaluating
11011 conditions on the target's side. Such conditions need to be updated on
11012 the target. */
11013
11014 static void
11015 force_breakpoint_reinsertion (struct bp_location *bl)
11016 {
11017 CORE_ADDR address = 0;
11018 int pspace_num;
11019
11020 address = bl->address;
11021 pspace_num = bl->pspace->num;
11022
11023 /* This is only meaningful if the target is
11024 evaluating conditions and if the user has
11025 opted for condition evaluation on the target's
11026 side. */
11027 if (gdb_evaluates_breakpoint_condition_p ()
11028 || !target_supports_evaluation_of_breakpoint_conditions ())
11029 return;
11030
11031 /* Flag all breakpoint locations with this address and
11032 the same program space as the location
11033 as "its condition has changed". We need to
11034 update the conditions on the target's side. */
11035 for (bp_location *loc : all_bp_locations_at_addr (address))
11036 {
11037 if (!is_breakpoint (loc->owner)
11038 || pspace_num != loc->pspace->num)
11039 continue;
11040
11041 /* Flag the location appropriately. We use a different state to
11042 let everyone know that we already updated the set of locations
11043 with addr bl->address and program space bl->pspace. This is so
11044 we don't have to keep calling these functions just to mark locations
11045 that have already been marked. */
11046 loc->condition_changed = condition_updated;
11047
11048 /* Free the agent expression bytecode as well. We will compute
11049 it later on. */
11050 loc->cond_bytecode.reset ();
11051 }
11052 }
11053
11054 /* Called whether new breakpoints are created, or existing breakpoints
11055 deleted, to update the global location list and recompute which
11056 locations are duplicate of which.
11057
11058 The INSERT_MODE flag determines whether locations may not, may, or
11059 shall be inserted now. See 'enum ugll_insert_mode' for more
11060 info. */
11061
11062 static void
11063 update_global_location_list (enum ugll_insert_mode insert_mode)
11064 {
11065 /* Last breakpoint location address that was marked for update. */
11066 CORE_ADDR last_addr = 0;
11067 /* Last breakpoint location program space that was marked for update. */
11068 int last_pspace_num = -1;
11069
11070 /* Used in the duplicates detection below. When iterating over all
11071 bp_locations, points to the first bp_location of a given address.
11072 Breakpoints and watchpoints of different types are never
11073 duplicates of each other. Keep one pointer for each type of
11074 breakpoint/watchpoint, so we only need to loop over all locations
11075 once. */
11076 struct bp_location *bp_loc_first; /* breakpoint */
11077 struct bp_location *wp_loc_first; /* hardware watchpoint */
11078 struct bp_location *awp_loc_first; /* access watchpoint */
11079 struct bp_location *rwp_loc_first; /* read watchpoint */
11080
11081 /* Saved former bp_locations array which we compare against the newly
11082 built bp_locations from the current state of ALL_BREAKPOINTS. */
11083 std::vector<bp_location *> old_locations = std::move (bp_locations);
11084 bp_locations.clear ();
11085
11086 for (breakpoint *b : all_breakpoints ())
11087 for (bp_location *loc : b->locations ())
11088 bp_locations.push_back (loc);
11089
11090 /* See if we need to "upgrade" a software breakpoint to a hardware
11091 breakpoint. Do this before deciding whether locations are
11092 duplicates. Also do this before sorting because sorting order
11093 depends on location type. */
11094 for (bp_location *loc : bp_locations)
11095 if (!loc->inserted && should_be_inserted (loc))
11096 handle_automatic_hardware_breakpoints (loc);
11097
11098 std::sort (bp_locations.begin (), bp_locations.end (),
11099 bp_location_is_less_than);
11100
11101 bp_locations_target_extensions_update ();
11102
11103 /* Identify bp_location instances that are no longer present in the
11104 new list, and therefore should be freed. Note that it's not
11105 necessary that those locations should be removed from inferior --
11106 if there's another location at the same address (previously
11107 marked as duplicate), we don't need to remove/insert the
11108 location.
11109
11110 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11111 and former bp_location array state respectively. */
11112
11113 size_t loc_i = 0;
11114 for (bp_location *old_loc : old_locations)
11115 {
11116 /* Tells if 'old_loc' is found among the new locations. If
11117 not, we have to free it. */
11118 bool found_object = false;
11119 /* Tells if the location should remain inserted in the target. */
11120 bool keep_in_target = false;
11121 bool removed = false;
11122
11123 /* Skip LOCP entries which will definitely never be needed.
11124 Stop either at or being the one matching OLD_LOC. */
11125 while (loc_i < bp_locations.size ()
11126 && bp_locations[loc_i]->address < old_loc->address)
11127 loc_i++;
11128
11129 for (size_t loc2_i = loc_i;
11130 (loc2_i < bp_locations.size ()
11131 && bp_locations[loc2_i]->address == old_loc->address);
11132 loc2_i++)
11133 {
11134 /* Check if this is a new/duplicated location or a duplicated
11135 location that had its condition modified. If so, we want to send
11136 its condition to the target if evaluation of conditions is taking
11137 place there. */
11138 if (bp_locations[loc2_i]->condition_changed == condition_modified
11139 && (last_addr != old_loc->address
11140 || last_pspace_num != old_loc->pspace->num))
11141 {
11142 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11143 last_pspace_num = old_loc->pspace->num;
11144 }
11145
11146 if (bp_locations[loc2_i] == old_loc)
11147 found_object = true;
11148 }
11149
11150 /* We have already handled this address, update it so that we don't
11151 have to go through updates again. */
11152 last_addr = old_loc->address;
11153
11154 /* Target-side condition evaluation: Handle deleted locations. */
11155 if (!found_object)
11156 force_breakpoint_reinsertion (old_loc);
11157
11158 /* If this location is no longer present, and inserted, look if
11159 there's maybe a new location at the same address. If so,
11160 mark that one inserted, and don't remove this one. This is
11161 needed so that we don't have a time window where a breakpoint
11162 at certain location is not inserted. */
11163
11164 if (old_loc->inserted)
11165 {
11166 /* If the location is inserted now, we might have to remove
11167 it. */
11168
11169 if (found_object && should_be_inserted (old_loc))
11170 {
11171 /* The location is still present in the location list,
11172 and still should be inserted. Don't do anything. */
11173 keep_in_target = true;
11174 }
11175 else
11176 {
11177 /* This location still exists, but it won't be kept in the
11178 target since it may have been disabled. We proceed to
11179 remove its target-side condition. */
11180
11181 /* The location is either no longer present, or got
11182 disabled. See if there's another location at the
11183 same address, in which case we don't need to remove
11184 this one from the target. */
11185
11186 /* OLD_LOC comes from existing struct breakpoint. */
11187 if (bl_address_is_meaningful (old_loc))
11188 {
11189 for (size_t loc2_i = loc_i;
11190 (loc2_i < bp_locations.size ()
11191 && bp_locations[loc2_i]->address == old_loc->address);
11192 loc2_i++)
11193 {
11194 bp_location *loc2 = bp_locations[loc2_i];
11195
11196 if (loc2 == old_loc)
11197 continue;
11198
11199 if (breakpoint_locations_match (loc2, old_loc))
11200 {
11201 /* Read watchpoint locations are switched to
11202 access watchpoints, if the former are not
11203 supported, but the latter are. */
11204 if (is_hardware_watchpoint (old_loc->owner))
11205 {
11206 gdb_assert (is_hardware_watchpoint (loc2->owner));
11207 loc2->watchpoint_type = old_loc->watchpoint_type;
11208 }
11209
11210 /* loc2 is a duplicated location. We need to check
11211 if it should be inserted in case it will be
11212 unduplicated. */
11213 if (unduplicated_should_be_inserted (loc2))
11214 {
11215 swap_insertion (old_loc, loc2);
11216 keep_in_target = true;
11217 break;
11218 }
11219 }
11220 }
11221 }
11222 }
11223
11224 if (!keep_in_target)
11225 {
11226 if (remove_breakpoint (old_loc))
11227 {
11228 /* This is just about all we can do. We could keep
11229 this location on the global list, and try to
11230 remove it next time, but there's no particular
11231 reason why we will succeed next time.
11232
11233 Note that at this point, old_loc->owner is still
11234 valid, as delete_breakpoint frees the breakpoint
11235 only after calling us. */
11236 gdb_printf (_("warning: Error removing "
11237 "breakpoint %d\n"),
11238 old_loc->owner->number);
11239 }
11240 removed = true;
11241 }
11242 }
11243
11244 if (!found_object)
11245 {
11246 if (removed && target_is_non_stop_p ()
11247 && need_moribund_for_location_type (old_loc))
11248 {
11249 /* This location was removed from the target. In
11250 non-stop mode, a race condition is possible where
11251 we've removed a breakpoint, but stop events for that
11252 breakpoint are already queued and will arrive later.
11253 We apply an heuristic to be able to distinguish such
11254 SIGTRAPs from other random SIGTRAPs: we keep this
11255 breakpoint location for a bit, and will retire it
11256 after we see some number of events. The theory here
11257 is that reporting of events should, "on the average",
11258 be fair, so after a while we'll see events from all
11259 threads that have anything of interest, and no longer
11260 need to keep this breakpoint location around. We
11261 don't hold locations forever so to reduce chances of
11262 mistaking a non-breakpoint SIGTRAP for a breakpoint
11263 SIGTRAP.
11264
11265 The heuristic failing can be disastrous on
11266 decr_pc_after_break targets.
11267
11268 On decr_pc_after_break targets, like e.g., x86-linux,
11269 if we fail to recognize a late breakpoint SIGTRAP,
11270 because events_till_retirement has reached 0 too
11271 soon, we'll fail to do the PC adjustment, and report
11272 a random SIGTRAP to the user. When the user resumes
11273 the inferior, it will most likely immediately crash
11274 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11275 corrupted, because of being resumed e.g., in the
11276 middle of a multi-byte instruction, or skipped a
11277 one-byte instruction. This was actually seen happen
11278 on native x86-linux, and should be less rare on
11279 targets that do not support new thread events, like
11280 remote, due to the heuristic depending on
11281 thread_count.
11282
11283 Mistaking a random SIGTRAP for a breakpoint trap
11284 causes similar symptoms (PC adjustment applied when
11285 it shouldn't), but then again, playing with SIGTRAPs
11286 behind the debugger's back is asking for trouble.
11287
11288 Since hardware watchpoint traps are always
11289 distinguishable from other traps, so we don't need to
11290 apply keep hardware watchpoint moribund locations
11291 around. We simply always ignore hardware watchpoint
11292 traps we can no longer explain. */
11293
11294 process_stratum_target *proc_target = nullptr;
11295 for (inferior *inf : all_inferiors ())
11296 if (inf->pspace == old_loc->pspace)
11297 {
11298 proc_target = inf->process_target ();
11299 break;
11300 }
11301 if (proc_target != nullptr)
11302 old_loc->events_till_retirement
11303 = 3 * (thread_count (proc_target) + 1);
11304 else
11305 old_loc->events_till_retirement = 1;
11306 old_loc->owner = NULL;
11307
11308 moribund_locations.push_back (old_loc);
11309 }
11310 else
11311 {
11312 old_loc->owner = NULL;
11313 decref_bp_location (&old_loc);
11314 }
11315 }
11316 }
11317
11318 /* Rescan breakpoints at the same address and section, marking the
11319 first one as "first" and any others as "duplicates". This is so
11320 that the bpt instruction is only inserted once. If we have a
11321 permanent breakpoint at the same place as BPT, make that one the
11322 official one, and the rest as duplicates. Permanent breakpoints
11323 are sorted first for the same address.
11324
11325 Do the same for hardware watchpoints, but also considering the
11326 watchpoint's type (regular/access/read) and length. */
11327
11328 bp_loc_first = NULL;
11329 wp_loc_first = NULL;
11330 awp_loc_first = NULL;
11331 rwp_loc_first = NULL;
11332
11333 for (bp_location *loc : all_bp_locations ())
11334 {
11335 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11336 non-NULL. */
11337 struct bp_location **loc_first_p;
11338 breakpoint *b = loc->owner;
11339
11340 if (!unduplicated_should_be_inserted (loc)
11341 || !bl_address_is_meaningful (loc)
11342 /* Don't detect duplicate for tracepoint locations because they are
11343 never duplicated. See the comments in field `duplicate' of
11344 `struct bp_location'. */
11345 || is_tracepoint (b))
11346 {
11347 /* Clear the condition modification flag. */
11348 loc->condition_changed = condition_unchanged;
11349 continue;
11350 }
11351
11352 if (b->type == bp_hardware_watchpoint)
11353 loc_first_p = &wp_loc_first;
11354 else if (b->type == bp_read_watchpoint)
11355 loc_first_p = &rwp_loc_first;
11356 else if (b->type == bp_access_watchpoint)
11357 loc_first_p = &awp_loc_first;
11358 else
11359 loc_first_p = &bp_loc_first;
11360
11361 if (*loc_first_p == NULL
11362 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11363 || !breakpoint_locations_match (loc, *loc_first_p))
11364 {
11365 *loc_first_p = loc;
11366 loc->duplicate = 0;
11367
11368 if (is_breakpoint (loc->owner) && loc->condition_changed)
11369 {
11370 loc->needs_update = 1;
11371 /* Clear the condition modification flag. */
11372 loc->condition_changed = condition_unchanged;
11373 }
11374 continue;
11375 }
11376
11377
11378 /* This and the above ensure the invariant that the first location
11379 is not duplicated, and is the inserted one.
11380 All following are marked as duplicated, and are not inserted. */
11381 if (loc->inserted)
11382 swap_insertion (loc, *loc_first_p);
11383 loc->duplicate = 1;
11384
11385 /* Clear the condition modification flag. */
11386 loc->condition_changed = condition_unchanged;
11387 }
11388
11389 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11390 {
11391 if (insert_mode != UGLL_DONT_INSERT)
11392 insert_breakpoint_locations ();
11393 else
11394 {
11395 /* Even though the caller told us to not insert new
11396 locations, we may still need to update conditions on the
11397 target's side of breakpoints that were already inserted
11398 if the target is evaluating breakpoint conditions. We
11399 only update conditions for locations that are marked
11400 "needs_update". */
11401 update_inserted_breakpoint_locations ();
11402 }
11403 }
11404
11405 if (insert_mode != UGLL_DONT_INSERT)
11406 download_tracepoint_locations ();
11407 }
11408
11409 void
11410 breakpoint_retire_moribund (void)
11411 {
11412 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11413 {
11414 struct bp_location *loc = moribund_locations[ix];
11415 if (--(loc->events_till_retirement) == 0)
11416 {
11417 decref_bp_location (&loc);
11418 unordered_remove (moribund_locations, ix);
11419 --ix;
11420 }
11421 }
11422 }
11423
11424 static void
11425 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11426 {
11427
11428 try
11429 {
11430 update_global_location_list (insert_mode);
11431 }
11432 catch (const gdb_exception_error &e)
11433 {
11434 }
11435 }
11436
11437 /* Clear BKP from a BPS. */
11438
11439 static void
11440 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11441 {
11442 bpstat *bs;
11443
11444 for (bs = bps; bs; bs = bs->next)
11445 if (bs->breakpoint_at == bpt)
11446 {
11447 bs->breakpoint_at = NULL;
11448 bs->old_val = NULL;
11449 /* bs->commands will be freed later. */
11450 }
11451 }
11452
11453 /* Callback for iterate_over_threads. */
11454 static int
11455 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11456 {
11457 struct breakpoint *bpt = (struct breakpoint *) data;
11458
11459 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11460 return 0;
11461 }
11462
11463 /* Helper for breakpoint and tracepoint breakpoint->mention
11464 callbacks. */
11465
11466 static void
11467 say_where (const breakpoint *b)
11468 {
11469 struct value_print_options opts;
11470
11471 get_user_print_options (&opts);
11472
11473 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11474 single string. */
11475 if (b->loc == NULL)
11476 {
11477 /* For pending locations, the output differs slightly based
11478 on b->extra_string. If this is non-NULL, it contains either
11479 a condition or dprintf arguments. */
11480 if (b->extra_string == NULL)
11481 {
11482 gdb_printf (_(" (%s) pending."), b->locspec->to_string ());
11483 }
11484 else if (b->type == bp_dprintf)
11485 {
11486 gdb_printf (_(" (%s,%s) pending."),
11487 b->locspec->to_string (),
11488 b->extra_string.get ());
11489 }
11490 else
11491 {
11492 gdb_printf (_(" (%s %s) pending."),
11493 b->locspec->to_string (),
11494 b->extra_string.get ());
11495 }
11496 }
11497 else
11498 {
11499 if (opts.addressprint || b->loc->symtab == NULL)
11500 gdb_printf (" at %ps",
11501 styled_string (address_style.style (),
11502 paddress (b->loc->gdbarch,
11503 b->loc->address)));
11504 if (b->loc->symtab != NULL)
11505 {
11506 /* If there is a single location, we can print the location
11507 more nicely. */
11508 if (b->loc->next == NULL)
11509 {
11510 const char *filename
11511 = symtab_to_filename_for_display (b->loc->symtab);
11512 gdb_printf (": file %ps, line %d.",
11513 styled_string (file_name_style.style (),
11514 filename),
11515 b->loc->line_number);
11516 }
11517 else
11518 /* This is not ideal, but each location may have a
11519 different file name, and this at least reflects the
11520 real situation somewhat. */
11521 gdb_printf (": %s.", b->locspec->to_string ());
11522 }
11523
11524 if (b->loc->next)
11525 {
11526 struct bp_location *loc = b->loc;
11527 int n = 0;
11528 for (; loc; loc = loc->next)
11529 ++n;
11530 gdb_printf (" (%d locations)", n);
11531 }
11532 }
11533 }
11534
11535 /* See breakpoint.h. */
11536
11537 bp_location_range breakpoint::locations () const
11538 {
11539 return bp_location_range (this->loc);
11540 }
11541
11542 struct bp_location *
11543 breakpoint::allocate_location ()
11544 {
11545 return new bp_location (this);
11546 }
11547
11548 #define internal_error_pure_virtual_called() \
11549 gdb_assert_not_reached ("pure virtual function called")
11550
11551 int
11552 breakpoint::insert_location (struct bp_location *bl)
11553 {
11554 internal_error_pure_virtual_called ();
11555 }
11556
11557 int
11558 breakpoint::remove_location (struct bp_location *bl,
11559 enum remove_bp_reason reason)
11560 {
11561 internal_error_pure_virtual_called ();
11562 }
11563
11564 int
11565 breakpoint::breakpoint_hit (const struct bp_location *bl,
11566 const address_space *aspace,
11567 CORE_ADDR bp_addr,
11568 const target_waitstatus &ws)
11569 {
11570 internal_error_pure_virtual_called ();
11571 }
11572
11573 int
11574 breakpoint::resources_needed (const struct bp_location *bl)
11575 {
11576 internal_error_pure_virtual_called ();
11577 }
11578
11579 enum print_stop_action
11580 breakpoint::print_it (const bpstat *bs) const
11581 {
11582 internal_error_pure_virtual_called ();
11583 }
11584
11585 void
11586 breakpoint::print_mention () const
11587 {
11588 internal_error_pure_virtual_called ();
11589 }
11590
11591 void
11592 breakpoint::print_recreate (struct ui_file *fp) const
11593 {
11594 internal_error_pure_virtual_called ();
11595 }
11596
11597 /* Default breakpoint_ops methods. */
11598
11599 void
11600 code_breakpoint::re_set ()
11601 {
11602 /* FIXME: is this still reachable? */
11603 if (breakpoint_location_spec_empty_p (this))
11604 {
11605 /* Anything without a location can't be re-set. */
11606 delete_breakpoint (this);
11607 return;
11608 }
11609
11610 re_set_default ();
11611 }
11612
11613 int
11614 code_breakpoint::insert_location (struct bp_location *bl)
11615 {
11616 CORE_ADDR addr = bl->target_info.reqstd_address;
11617
11618 bl->target_info.kind = breakpoint_kind (bl, &addr);
11619 bl->target_info.placed_address = addr;
11620
11621 int result;
11622 if (bl->loc_type == bp_loc_hardware_breakpoint)
11623 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11624 else
11625 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11626
11627 if (result == 0 && bl->probe.prob != nullptr)
11628 {
11629 /* The insertion was successful, now let's set the probe's semaphore
11630 if needed. */
11631 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11632 }
11633
11634 return result;
11635 }
11636
11637 int
11638 code_breakpoint::remove_location (struct bp_location *bl,
11639 enum remove_bp_reason reason)
11640 {
11641 if (bl->probe.prob != nullptr)
11642 {
11643 /* Let's clear the semaphore before removing the location. */
11644 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11645 }
11646
11647 if (bl->loc_type == bp_loc_hardware_breakpoint)
11648 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11649 else
11650 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11651 }
11652
11653 int
11654 code_breakpoint::breakpoint_hit (const struct bp_location *bl,
11655 const address_space *aspace,
11656 CORE_ADDR bp_addr,
11657 const target_waitstatus &ws)
11658 {
11659 if (ws.kind () != TARGET_WAITKIND_STOPPED
11660 || ws.sig () != GDB_SIGNAL_TRAP)
11661 return 0;
11662
11663 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11664 aspace, bp_addr))
11665 return 0;
11666
11667 if (overlay_debugging /* unmapped overlay section */
11668 && section_is_overlay (bl->section)
11669 && !section_is_mapped (bl->section))
11670 return 0;
11671
11672 return 1;
11673 }
11674
11675 int
11676 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11677 const address_space *aspace,
11678 CORE_ADDR bp_addr,
11679 const target_waitstatus &ws)
11680 {
11681 if (dprintf_style == dprintf_style_agent
11682 && target_can_run_breakpoint_commands ())
11683 {
11684 /* An agent-style dprintf never causes a stop. If we see a trap
11685 for this address it must be for a breakpoint that happens to
11686 be set at the same address. */
11687 return 0;
11688 }
11689
11690 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
11691 }
11692
11693 int
11694 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
11695 {
11696 gdb_assert (type == bp_hardware_breakpoint);
11697
11698 return 1;
11699 }
11700
11701 enum print_stop_action
11702 ordinary_breakpoint::print_it (const bpstat *bs) const
11703 {
11704 const struct bp_location *bl;
11705 int bp_temp;
11706 struct ui_out *uiout = current_uiout;
11707
11708 bl = bs->bp_location_at.get ();
11709
11710 bp_temp = disposition == disp_del;
11711 if (bl->address != bl->requested_address)
11712 breakpoint_adjustment_warning (bl->requested_address,
11713 bl->address,
11714 number, true);
11715 annotate_breakpoint (number);
11716 maybe_print_thread_hit_breakpoint (uiout);
11717
11718 if (uiout->is_mi_like_p ())
11719 {
11720 uiout->field_string ("reason",
11721 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11722 uiout->field_string ("disp", bpdisp_text (disposition));
11723 }
11724
11725 if (bp_temp)
11726 uiout->text ("Temporary breakpoint ");
11727 else
11728 uiout->text ("Breakpoint ");
11729 print_num_locno (bs, uiout);
11730 uiout->text (", ");
11731
11732 return PRINT_SRC_AND_LOC;
11733 }
11734
11735 void
11736 ordinary_breakpoint::print_mention () const
11737 {
11738 if (current_uiout->is_mi_like_p ())
11739 return;
11740
11741 switch (type)
11742 {
11743 case bp_breakpoint:
11744 case bp_gnu_ifunc_resolver:
11745 if (disposition == disp_del)
11746 gdb_printf (_("Temporary breakpoint"));
11747 else
11748 gdb_printf (_("Breakpoint"));
11749 gdb_printf (_(" %d"), number);
11750 if (type == bp_gnu_ifunc_resolver)
11751 gdb_printf (_(" at gnu-indirect-function resolver"));
11752 break;
11753 case bp_hardware_breakpoint:
11754 gdb_printf (_("Hardware assisted breakpoint %d"), number);
11755 break;
11756 case bp_dprintf:
11757 gdb_printf (_("Dprintf %d"), number);
11758 break;
11759 }
11760
11761 say_where (this);
11762 }
11763
11764 void
11765 ordinary_breakpoint::print_recreate (struct ui_file *fp) const
11766 {
11767 if (type == bp_breakpoint && disposition == disp_del)
11768 gdb_printf (fp, "tbreak");
11769 else if (type == bp_breakpoint)
11770 gdb_printf (fp, "break");
11771 else if (type == bp_hardware_breakpoint
11772 && disposition == disp_del)
11773 gdb_printf (fp, "thbreak");
11774 else if (type == bp_hardware_breakpoint)
11775 gdb_printf (fp, "hbreak");
11776 else
11777 internal_error (_("unhandled breakpoint type %d"), (int) type);
11778
11779 gdb_printf (fp, " %s", locspec->to_string ());
11780
11781 /* Print out extra_string if this breakpoint is pending. It might
11782 contain, for example, conditions that were set by the user. */
11783 if (loc == NULL && extra_string != NULL)
11784 gdb_printf (fp, " %s", extra_string.get ());
11785
11786 print_recreate_thread (fp);
11787 }
11788
11789 std::vector<symtab_and_line>
11790 code_breakpoint::decode_location_spec (location_spec *locspec,
11791 program_space *search_pspace)
11792 {
11793 if (locspec->type () == PROBE_LOCATION_SPEC)
11794 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
11795
11796 struct linespec_result canonical;
11797
11798 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
11799 NULL, 0, &canonical, multiple_symbols_all,
11800 filter.get ());
11801
11802 /* We should get 0 or 1 resulting SALs. */
11803 gdb_assert (canonical.lsals.size () < 2);
11804
11805 if (!canonical.lsals.empty ())
11806 {
11807 const linespec_sals &lsal = canonical.lsals[0];
11808 return std::move (lsal.sals);
11809 }
11810 return {};
11811 }
11812
11813 /* Virtual table for internal breakpoints. */
11814
11815 void
11816 internal_breakpoint::re_set ()
11817 {
11818 switch (type)
11819 {
11820 /* Delete overlay event and longjmp master breakpoints; they
11821 will be reset later by breakpoint_re_set. */
11822 case bp_overlay_event:
11823 case bp_longjmp_master:
11824 case bp_std_terminate_master:
11825 case bp_exception_master:
11826 delete_breakpoint (this);
11827 break;
11828
11829 /* This breakpoint is special, it's set up when the inferior
11830 starts and we really don't want to touch it. */
11831 case bp_shlib_event:
11832
11833 /* Like bp_shlib_event, this breakpoint type is special. Once
11834 it is set up, we do not want to touch it. */
11835 case bp_thread_event:
11836 break;
11837 }
11838 }
11839
11840 void
11841 internal_breakpoint::check_status (bpstat *bs)
11842 {
11843 if (type == bp_shlib_event)
11844 {
11845 /* If requested, stop when the dynamic linker notifies GDB of
11846 events. This allows the user to get control and place
11847 breakpoints in initializer routines for dynamically loaded
11848 objects (among other things). */
11849 bs->stop = stop_on_solib_events;
11850 bs->print = stop_on_solib_events;
11851 }
11852 else
11853 bs->stop = 0;
11854 }
11855
11856 enum print_stop_action
11857 internal_breakpoint::print_it (const bpstat *bs) const
11858 {
11859 switch (type)
11860 {
11861 case bp_shlib_event:
11862 /* Did we stop because the user set the stop_on_solib_events
11863 variable? (If so, we report this as a generic, "Stopped due
11864 to shlib event" message.) */
11865 print_solib_event (false);
11866 break;
11867
11868 case bp_thread_event:
11869 /* Not sure how we will get here.
11870 GDB should not stop for these breakpoints. */
11871 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
11872 break;
11873
11874 case bp_overlay_event:
11875 /* By analogy with the thread event, GDB should not stop for these. */
11876 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
11877 break;
11878
11879 case bp_longjmp_master:
11880 /* These should never be enabled. */
11881 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
11882 break;
11883
11884 case bp_std_terminate_master:
11885 /* These should never be enabled. */
11886 gdb_printf (_("std::terminate Master Breakpoint: "
11887 "gdb should not stop!\n"));
11888 break;
11889
11890 case bp_exception_master:
11891 /* These should never be enabled. */
11892 gdb_printf (_("Exception Master Breakpoint: "
11893 "gdb should not stop!\n"));
11894 break;
11895 }
11896
11897 return PRINT_NOTHING;
11898 }
11899
11900 void
11901 internal_breakpoint::print_mention () const
11902 {
11903 /* Nothing to mention. These breakpoints are internal. */
11904 }
11905
11906 /* Virtual table for momentary breakpoints */
11907
11908 void
11909 momentary_breakpoint::re_set ()
11910 {
11911 /* Keep temporary breakpoints, which can be encountered when we step
11912 over a dlopen call and solib_add is resetting the breakpoints.
11913 Otherwise these should have been blown away via the cleanup chain
11914 or by breakpoint_init_inferior when we rerun the executable. */
11915 }
11916
11917 void
11918 momentary_breakpoint::check_status (bpstat *bs)
11919 {
11920 /* Nothing. The point of these breakpoints is causing a stop. */
11921 }
11922
11923 enum print_stop_action
11924 momentary_breakpoint::print_it (const bpstat *bs) const
11925 {
11926 return PRINT_UNKNOWN;
11927 }
11928
11929 void
11930 momentary_breakpoint::print_mention () const
11931 {
11932 /* Nothing to mention. These breakpoints are internal. */
11933 }
11934
11935 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
11936
11937 It gets cleared already on the removal of the first one of such placed
11938 breakpoints. This is OK as they get all removed altogether. */
11939
11940 longjmp_breakpoint::~longjmp_breakpoint ()
11941 {
11942 thread_info *tp = find_thread_global_id (this->thread);
11943
11944 if (tp != NULL)
11945 tp->initiating_frame = null_frame_id;
11946 }
11947
11948 static void
11949 bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
11950 struct linespec_result *canonical)
11951
11952 {
11953 struct linespec_sals lsal;
11954
11955 lsal.sals = parse_probes (locspec, NULL, canonical);
11956 lsal.canonical = xstrdup (canonical->locspec->to_string ());
11957 canonical->lsals.push_back (std::move (lsal));
11958 }
11959
11960 static std::vector<symtab_and_line>
11961 bkpt_probe_decode_location_spec (struct breakpoint *b,
11962 location_spec *locspec,
11963 program_space *search_pspace)
11964 {
11965 std::vector<symtab_and_line> sals
11966 = parse_probes (locspec, search_pspace, NULL);
11967 if (sals.empty ())
11968 error (_("probe not found"));
11969 return sals;
11970 }
11971
11972 int
11973 tracepoint::breakpoint_hit (const struct bp_location *bl,
11974 const address_space *aspace, CORE_ADDR bp_addr,
11975 const target_waitstatus &ws)
11976 {
11977 /* By definition, the inferior does not report stops at
11978 tracepoints. */
11979 return 0;
11980 }
11981
11982 void
11983 tracepoint::print_one_detail (struct ui_out *uiout) const
11984 {
11985 if (!static_trace_marker_id.empty ())
11986 {
11987 gdb_assert (type == bp_static_tracepoint
11988 || type == bp_static_marker_tracepoint);
11989
11990 uiout->message ("\tmarker id is %pF\n",
11991 string_field ("static-tracepoint-marker-string-id",
11992 static_trace_marker_id.c_str ()));
11993 }
11994 }
11995
11996 void
11997 tracepoint::print_mention () const
11998 {
11999 if (current_uiout->is_mi_like_p ())
12000 return;
12001
12002 switch (type)
12003 {
12004 case bp_tracepoint:
12005 gdb_printf (_("Tracepoint"));
12006 gdb_printf (_(" %d"), number);
12007 break;
12008 case bp_fast_tracepoint:
12009 gdb_printf (_("Fast tracepoint"));
12010 gdb_printf (_(" %d"), number);
12011 break;
12012 case bp_static_tracepoint:
12013 case bp_static_marker_tracepoint:
12014 gdb_printf (_("Static tracepoint"));
12015 gdb_printf (_(" %d"), number);
12016 break;
12017 default:
12018 internal_error (_("unhandled tracepoint type %d"), (int) type);
12019 }
12020
12021 say_where (this);
12022 }
12023
12024 void
12025 tracepoint::print_recreate (struct ui_file *fp) const
12026 {
12027 if (type == bp_fast_tracepoint)
12028 gdb_printf (fp, "ftrace");
12029 else if (type == bp_static_tracepoint
12030 || type == bp_static_marker_tracepoint)
12031 gdb_printf (fp, "strace");
12032 else if (type == bp_tracepoint)
12033 gdb_printf (fp, "trace");
12034 else
12035 internal_error (_("unhandled tracepoint type %d"), (int) type);
12036
12037 gdb_printf (fp, " %s", locspec->to_string ());
12038 print_recreate_thread (fp);
12039
12040 if (pass_count)
12041 gdb_printf (fp, " passcount %d\n", pass_count);
12042 }
12043
12044 /* Virtual table for tracepoints on static probes. */
12045
12046 static void
12047 tracepoint_probe_create_sals_from_location_spec
12048 (location_spec *locspec,
12049 struct linespec_result *canonical)
12050 {
12051 /* We use the same method for breakpoint on probes. */
12052 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
12053 }
12054
12055 void
12056 dprintf_breakpoint::re_set ()
12057 {
12058 re_set_default ();
12059
12060 /* extra_string should never be non-NULL for dprintf. */
12061 gdb_assert (extra_string != NULL);
12062
12063 /* 1 - connect to target 1, that can run breakpoint commands.
12064 2 - create a dprintf, which resolves fine.
12065 3 - disconnect from target 1
12066 4 - connect to target 2, that can NOT run breakpoint commands.
12067
12068 After steps #3/#4, you'll want the dprintf command list to
12069 be updated, because target 1 and 2 may well return different
12070 answers for target_can_run_breakpoint_commands().
12071 Given absence of finer grained resetting, we get to do
12072 it all the time. */
12073 if (extra_string != NULL)
12074 update_dprintf_command_list (this);
12075 }
12076
12077 /* Implement the "print_recreate" method for dprintf. */
12078
12079 void
12080 dprintf_breakpoint::print_recreate (struct ui_file *fp) const
12081 {
12082 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
12083 print_recreate_thread (fp);
12084 }
12085
12086 /* Implement the "after_condition_true" method for dprintf.
12087
12088 dprintf's are implemented with regular commands in their command
12089 list, but we run the commands here instead of before presenting the
12090 stop to the user, as dprintf's don't actually cause a stop. This
12091 also makes it so that the commands of multiple dprintfs at the same
12092 address are all handled. */
12093
12094 void
12095 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
12096 {
12097 /* dprintf's never cause a stop. This wasn't set in the
12098 check_status hook instead because that would make the dprintf's
12099 condition not be evaluated. */
12100 bs->stop = 0;
12101
12102 /* Run the command list here. Take ownership of it instead of
12103 copying. We never want these commands to run later in
12104 bpstat_do_actions, if a breakpoint that causes a stop happens to
12105 be set at same address as this dprintf, or even if running the
12106 commands here throws. */
12107 counted_command_line cmds = std::move (bs->commands);
12108 gdb_assert (cmds != nullptr);
12109 execute_control_commands (cmds.get (), 0);
12110 }
12111
12112 /* The breakpoint_ops structure to be used on static tracepoints with
12113 markers (`-m'). */
12114
12115 static void
12116 strace_marker_create_sals_from_location_spec (location_spec *locspec,
12117 struct linespec_result *canonical)
12118 {
12119 struct linespec_sals lsal;
12120 const char *arg_start, *arg;
12121
12122 arg = arg_start = as_linespec_location_spec (locspec)->spec_string;
12123 lsal.sals = decode_static_tracepoint_spec (&arg);
12124
12125 std::string str (arg_start, arg - arg_start);
12126 const char *ptr = str.c_str ();
12127 canonical->locspec
12128 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
12129
12130 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12131 canonical->lsals.push_back (std::move (lsal));
12132 }
12133
12134 static void
12135 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12136 struct linespec_result *canonical,
12137 gdb::unique_xmalloc_ptr<char> cond_string,
12138 gdb::unique_xmalloc_ptr<char> extra_string,
12139 enum bptype type_wanted,
12140 enum bpdisp disposition,
12141 int thread,
12142 int task, int ignore_count,
12143 int from_tty, int enabled,
12144 int internal, unsigned flags)
12145 {
12146 const linespec_sals &lsal = canonical->lsals[0];
12147
12148 /* If the user is creating a static tracepoint by marker id
12149 (strace -m MARKER_ID), then store the sals index, so that
12150 breakpoint_re_set can try to match up which of the newly
12151 found markers corresponds to this one, and, don't try to
12152 expand multiple locations for each sal, given than SALS
12153 already should contain all sals for MARKER_ID. */
12154
12155 for (size_t i = 0; i < lsal.sals.size (); i++)
12156 {
12157 location_spec_up locspec = canonical->locspec->clone ();
12158
12159 std::unique_ptr<tracepoint> tp
12160 (new tracepoint (gdbarch,
12161 type_wanted,
12162 lsal.sals[i],
12163 std::move (locspec),
12164 NULL,
12165 std::move (cond_string),
12166 std::move (extra_string),
12167 disposition,
12168 thread, task, ignore_count,
12169 from_tty, enabled, flags,
12170 canonical->special_display));
12171
12172 /* Given that its possible to have multiple markers with
12173 the same string id, if the user is creating a static
12174 tracepoint by marker id ("strace -m MARKER_ID"), then
12175 store the sals index, so that breakpoint_re_set can
12176 try to match up which of the newly found markers
12177 corresponds to this one */
12178 tp->static_trace_marker_id_idx = i;
12179
12180 install_breakpoint (internal, std::move (tp), 0);
12181 }
12182 }
12183
12184 std::vector<symtab_and_line>
12185 static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12186 program_space *search_pspace)
12187 {
12188 const char *s = as_linespec_location_spec (locspec)->spec_string;
12189
12190 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12191 if (sals.size () > static_trace_marker_id_idx)
12192 {
12193 sals[0] = sals[static_trace_marker_id_idx];
12194 sals.resize (1);
12195 return sals;
12196 }
12197 else
12198 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12199 }
12200
12201 /* Static tracepoints with marker (`-m'). */
12202 static struct breakpoint_ops strace_marker_breakpoint_ops =
12203 {
12204 strace_marker_create_sals_from_location_spec,
12205 strace_marker_create_breakpoints_sal,
12206 };
12207
12208 static bool
12209 strace_marker_p (struct breakpoint *b)
12210 {
12211 return b->type == bp_static_marker_tracepoint;
12212 }
12213
12214 /* Delete a breakpoint and clean up all traces of it in the data
12215 structures. */
12216
12217 void
12218 delete_breakpoint (struct breakpoint *bpt)
12219 {
12220 gdb_assert (bpt != NULL);
12221
12222 /* Has this bp already been deleted? This can happen because
12223 multiple lists can hold pointers to bp's. bpstat lists are
12224 especial culprits.
12225
12226 One example of this happening is a watchpoint's scope bp. When
12227 the scope bp triggers, we notice that the watchpoint is out of
12228 scope, and delete it. We also delete its scope bp. But the
12229 scope bp is marked "auto-deleting", and is already on a bpstat.
12230 That bpstat is then checked for auto-deleting bp's, which are
12231 deleted.
12232
12233 A real solution to this problem might involve reference counts in
12234 bp's, and/or giving them pointers back to their referencing
12235 bpstat's, and teaching delete_breakpoint to only free a bp's
12236 storage when no more references were extent. A cheaper bandaid
12237 was chosen. */
12238 if (bpt->type == bp_none)
12239 return;
12240
12241 /* At least avoid this stale reference until the reference counting
12242 of breakpoints gets resolved. */
12243 if (bpt->related_breakpoint != bpt)
12244 {
12245 struct breakpoint *related;
12246 struct watchpoint *w;
12247
12248 if (bpt->type == bp_watchpoint_scope)
12249 w = (struct watchpoint *) bpt->related_breakpoint;
12250 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12251 w = (struct watchpoint *) bpt;
12252 else
12253 w = NULL;
12254 if (w != NULL)
12255 watchpoint_del_at_next_stop (w);
12256
12257 /* Unlink bpt from the bpt->related_breakpoint ring. */
12258 for (related = bpt; related->related_breakpoint != bpt;
12259 related = related->related_breakpoint);
12260 related->related_breakpoint = bpt->related_breakpoint;
12261 bpt->related_breakpoint = bpt;
12262 }
12263
12264 /* watch_command_1 creates a watchpoint but only sets its number if
12265 update_watchpoint succeeds in creating its bp_locations. If there's
12266 a problem in that process, we'll be asked to delete the half-created
12267 watchpoint. In that case, don't announce the deletion. */
12268 if (bpt->number)
12269 gdb::observers::breakpoint_deleted.notify (bpt);
12270
12271 if (breakpoint_chain == bpt)
12272 breakpoint_chain = bpt->next;
12273
12274 for (breakpoint *b : all_breakpoints ())
12275 if (b->next == bpt)
12276 {
12277 b->next = bpt->next;
12278 break;
12279 }
12280
12281 /* Be sure no bpstat's are pointing at the breakpoint after it's
12282 been freed. */
12283 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12284 in all threads for now. Note that we cannot just remove bpstats
12285 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12286 commands are associated with the bpstat; if we remove it here,
12287 then the later call to bpstat_do_actions (&stop_bpstat); in
12288 event-top.c won't do anything, and temporary breakpoints with
12289 commands won't work. */
12290
12291 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12292
12293 /* Now that breakpoint is removed from breakpoint list, update the
12294 global location list. This will remove locations that used to
12295 belong to this breakpoint. Do this before freeing the breakpoint
12296 itself, since remove_breakpoint looks at location's owner. It
12297 might be better design to have location completely
12298 self-contained, but it's not the case now. */
12299 update_global_location_list (UGLL_DONT_INSERT);
12300
12301 /* On the chance that someone will soon try again to delete this
12302 same bp, we mark it as deleted before freeing its storage. */
12303 bpt->type = bp_none;
12304 delete bpt;
12305 }
12306
12307 /* Iterator function to call a user-provided callback function once
12308 for each of B and its related breakpoints. */
12309
12310 static void
12311 iterate_over_related_breakpoints (struct breakpoint *b,
12312 gdb::function_view<void (breakpoint *)> function)
12313 {
12314 struct breakpoint *related;
12315
12316 related = b;
12317 do
12318 {
12319 struct breakpoint *next;
12320
12321 /* FUNCTION may delete RELATED. */
12322 next = related->related_breakpoint;
12323
12324 if (next == related)
12325 {
12326 /* RELATED is the last ring entry. */
12327 function (related);
12328
12329 /* FUNCTION may have deleted it, so we'd never reach back to
12330 B. There's nothing left to do anyway, so just break
12331 out. */
12332 break;
12333 }
12334 else
12335 function (related);
12336
12337 related = next;
12338 }
12339 while (related != b);
12340 }
12341
12342 static void
12343 delete_command (const char *arg, int from_tty)
12344 {
12345 dont_repeat ();
12346
12347 if (arg == 0)
12348 {
12349 int breaks_to_delete = 0;
12350
12351 /* Delete all breakpoints if no argument. Do not delete
12352 internal breakpoints, these have to be deleted with an
12353 explicit breakpoint number argument. */
12354 for (breakpoint *b : all_breakpoints ())
12355 if (user_breakpoint_p (b))
12356 {
12357 breaks_to_delete = 1;
12358 break;
12359 }
12360
12361 /* Ask user only if there are some breakpoints to delete. */
12362 if (!from_tty
12363 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12364 for (breakpoint *b : all_breakpoints_safe ())
12365 if (user_breakpoint_p (b))
12366 delete_breakpoint (b);
12367 }
12368 else
12369 map_breakpoint_numbers
12370 (arg, [&] (breakpoint *br)
12371 {
12372 iterate_over_related_breakpoints (br, delete_breakpoint);
12373 });
12374 }
12375
12376 /* Return true if all locations of B bound to PSPACE are pending. If
12377 PSPACE is NULL, all locations of all program spaces are
12378 considered. */
12379
12380 static bool
12381 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12382 {
12383 for (bp_location *loc : b->locations ())
12384 if ((pspace == NULL
12385 || loc->pspace == pspace)
12386 && !loc->shlib_disabled
12387 && !loc->pspace->executing_startup)
12388 return false;
12389 return true;
12390 }
12391
12392 /* Subroutine of update_breakpoint_locations to simplify it.
12393 Return true if multiple fns in list LOC have the same name.
12394 Null names are ignored. */
12395
12396 static bool
12397 ambiguous_names_p (struct bp_location *loc)
12398 {
12399 struct bp_location *l;
12400 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12401 xcalloc, xfree));
12402
12403 for (l = loc; l != NULL; l = l->next)
12404 {
12405 const char **slot;
12406 const char *name = l->function_name.get ();
12407
12408 /* Allow for some names to be NULL, ignore them. */
12409 if (name == NULL)
12410 continue;
12411
12412 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12413 INSERT);
12414 /* NOTE: We can assume slot != NULL here because xcalloc never
12415 returns NULL. */
12416 if (*slot != NULL)
12417 return true;
12418 *slot = name;
12419 }
12420
12421 return false;
12422 }
12423
12424 /* When symbols change, it probably means the sources changed as well,
12425 and it might mean the static tracepoint markers are no longer at
12426 the same address or line numbers they used to be at last we
12427 checked. Losing your static tracepoints whenever you rebuild is
12428 undesirable. This function tries to resync/rematch gdb static
12429 tracepoints with the markers on the target, for static tracepoints
12430 that have not been set by marker id. Static tracepoint that have
12431 been set by marker id are reset by marker id in breakpoint_re_set.
12432 The heuristic is:
12433
12434 1) For a tracepoint set at a specific address, look for a marker at
12435 the old PC. If one is found there, assume to be the same marker.
12436 If the name / string id of the marker found is different from the
12437 previous known name, assume that means the user renamed the marker
12438 in the sources, and output a warning.
12439
12440 2) For a tracepoint set at a given line number, look for a marker
12441 at the new address of the old line number. If one is found there,
12442 assume to be the same marker. If the name / string id of the
12443 marker found is different from the previous known name, assume that
12444 means the user renamed the marker in the sources, and output a
12445 warning.
12446
12447 3) If a marker is no longer found at the same address or line, it
12448 may mean the marker no longer exists. But it may also just mean
12449 the code changed a bit. Maybe the user added a few lines of code
12450 that made the marker move up or down (in line number terms). Ask
12451 the target for info about the marker with the string id as we knew
12452 it. If found, update line number and address in the matching
12453 static tracepoint. This will get confused if there's more than one
12454 marker with the same ID (possible in UST, although unadvised
12455 precisely because it confuses tools). */
12456
12457 static struct symtab_and_line
12458 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12459 {
12460 struct tracepoint *tp = (struct tracepoint *) b;
12461 struct static_tracepoint_marker marker;
12462 CORE_ADDR pc;
12463
12464 pc = sal.pc;
12465 if (sal.line)
12466 find_line_pc (sal.symtab, sal.line, &pc);
12467
12468 if (target_static_tracepoint_marker_at (pc, &marker))
12469 {
12470 if (tp->static_trace_marker_id != marker.str_id)
12471 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12472 b->number, tp->static_trace_marker_id.c_str (),
12473 marker.str_id.c_str ());
12474
12475 tp->static_trace_marker_id = std::move (marker.str_id);
12476
12477 return sal;
12478 }
12479
12480 /* Old marker wasn't found on target at lineno. Try looking it up
12481 by string ID. */
12482 if (!sal.explicit_pc
12483 && sal.line != 0
12484 && sal.symtab != NULL
12485 && !tp->static_trace_marker_id.empty ())
12486 {
12487 std::vector<static_tracepoint_marker> markers
12488 = target_static_tracepoint_markers_by_strid
12489 (tp->static_trace_marker_id.c_str ());
12490
12491 if (!markers.empty ())
12492 {
12493 struct symbol *sym;
12494 struct static_tracepoint_marker *tpmarker;
12495 struct ui_out *uiout = current_uiout;
12496
12497 tpmarker = &markers[0];
12498
12499 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12500
12501 warning (_("marker for static tracepoint %d (%s) not "
12502 "found at previous line number"),
12503 b->number, tp->static_trace_marker_id.c_str ());
12504
12505 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12506 sym = find_pc_sect_function (tpmarker->address, NULL);
12507 uiout->text ("Now in ");
12508 if (sym)
12509 {
12510 uiout->field_string ("func", sym->print_name (),
12511 function_name_style.style ());
12512 uiout->text (" at ");
12513 }
12514 uiout->field_string ("file",
12515 symtab_to_filename_for_display (sal2.symtab),
12516 file_name_style.style ());
12517 uiout->text (":");
12518
12519 if (uiout->is_mi_like_p ())
12520 {
12521 const char *fullname = symtab_to_fullname (sal2.symtab);
12522
12523 uiout->field_string ("fullname", fullname);
12524 }
12525
12526 uiout->field_signed ("line", sal2.line);
12527 uiout->text ("\n");
12528
12529 b->loc->line_number = sal2.line;
12530 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
12531
12532 std::unique_ptr<explicit_location_spec> els
12533 (new explicit_location_spec ());
12534 els->source_filename
12535 = xstrdup (symtab_to_filename_for_display (sal2.symtab));
12536 els->line_offset.offset = b->loc->line_number;
12537 els->line_offset.sign = LINE_OFFSET_NONE;
12538
12539 b->locspec = std::move (els);
12540
12541 /* Might be nice to check if function changed, and warn if
12542 so. */
12543 }
12544 }
12545 return sal;
12546 }
12547
12548 /* Returns true iff locations A and B are sufficiently same that
12549 we don't need to report breakpoint as changed. */
12550
12551 static bool
12552 locations_are_equal (struct bp_location *a, struct bp_location *b)
12553 {
12554 while (a && b)
12555 {
12556 if (a->address != b->address)
12557 return false;
12558
12559 if (a->shlib_disabled != b->shlib_disabled)
12560 return false;
12561
12562 if (a->enabled != b->enabled)
12563 return false;
12564
12565 if (a->disabled_by_cond != b->disabled_by_cond)
12566 return false;
12567
12568 a = a->next;
12569 b = b->next;
12570 }
12571
12572 if ((a == NULL) != (b == NULL))
12573 return false;
12574
12575 return true;
12576 }
12577
12578 /* Split all locations of B that are bound to PSPACE out of B's
12579 location list to a separate list and return that list's head. If
12580 PSPACE is NULL, hoist out all locations of B. */
12581
12582 static struct bp_location *
12583 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12584 {
12585 struct bp_location head;
12586 struct bp_location *i = b->loc;
12587 struct bp_location **i_link = &b->loc;
12588 struct bp_location *hoisted = &head;
12589
12590 if (pspace == NULL)
12591 {
12592 i = b->loc;
12593 b->loc = NULL;
12594 return i;
12595 }
12596
12597 head.next = NULL;
12598
12599 while (i != NULL)
12600 {
12601 if (i->pspace == pspace)
12602 {
12603 *i_link = i->next;
12604 i->next = NULL;
12605 hoisted->next = i;
12606 hoisted = i;
12607 }
12608 else
12609 i_link = &i->next;
12610 i = *i_link;
12611 }
12612
12613 return head.next;
12614 }
12615
12616 /* Create new breakpoint locations for B (a hardware or software
12617 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12618 zero, then B is a ranged breakpoint. Only recreates locations for
12619 FILTER_PSPACE. Locations of other program spaces are left
12620 untouched. */
12621
12622 void
12623 update_breakpoint_locations (code_breakpoint *b,
12624 struct program_space *filter_pspace,
12625 gdb::array_view<const symtab_and_line> sals,
12626 gdb::array_view<const symtab_and_line> sals_end)
12627 {
12628 struct bp_location *existing_locations;
12629
12630 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12631 {
12632 /* Ranged breakpoints have only one start location and one end
12633 location. */
12634 b->enable_state = bp_disabled;
12635 gdb_printf (gdb_stderr,
12636 _("Could not reset ranged breakpoint %d: "
12637 "multiple locations found\n"),
12638 b->number);
12639 return;
12640 }
12641
12642 /* If there's no new locations, and all existing locations are
12643 pending, don't do anything. This optimizes the common case where
12644 all locations are in the same shared library, that was unloaded.
12645 We'd like to retain the location, so that when the library is
12646 loaded again, we don't loose the enabled/disabled status of the
12647 individual locations. */
12648 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12649 return;
12650
12651 existing_locations = hoist_existing_locations (b, filter_pspace);
12652
12653 for (const auto &sal : sals)
12654 {
12655 struct bp_location *new_loc;
12656
12657 switch_to_program_space_and_thread (sal.pspace);
12658
12659 new_loc = b->add_location (sal);
12660
12661 /* Reparse conditions, they might contain references to the
12662 old symtab. */
12663 if (b->cond_string != NULL)
12664 {
12665 const char *s;
12666
12667 s = b->cond_string.get ();
12668 try
12669 {
12670 new_loc->cond = parse_exp_1 (&s, sal.pc,
12671 block_for_pc (sal.pc),
12672 0);
12673 }
12674 catch (const gdb_exception_error &e)
12675 {
12676 new_loc->disabled_by_cond = true;
12677 }
12678 }
12679
12680 if (!sals_end.empty ())
12681 {
12682 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
12683
12684 new_loc->length = end - sals[0].pc + 1;
12685 }
12686 }
12687
12688 /* If possible, carry over 'disable' status from existing
12689 breakpoints. */
12690 {
12691 struct bp_location *e = existing_locations;
12692 /* If there are multiple breakpoints with the same function name,
12693 e.g. for inline functions, comparing function names won't work.
12694 Instead compare pc addresses; this is just a heuristic as things
12695 may have moved, but in practice it gives the correct answer
12696 often enough until a better solution is found. */
12697 int have_ambiguous_names = ambiguous_names_p (b->loc);
12698
12699 for (; e; e = e->next)
12700 {
12701 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
12702 {
12703 if (have_ambiguous_names)
12704 {
12705 for (bp_location *l : b->locations ())
12706 {
12707 /* Ignore software vs hardware location type at
12708 this point, because with "set breakpoint
12709 auto-hw", after a re-set, locations that were
12710 hardware can end up as software, or vice versa.
12711 As mentioned above, this is an heuristic and in
12712 practice should give the correct answer often
12713 enough. */
12714 if (breakpoint_locations_match (e, l, true))
12715 {
12716 l->enabled = e->enabled;
12717 l->disabled_by_cond = e->disabled_by_cond;
12718 break;
12719 }
12720 }
12721 }
12722 else
12723 {
12724 for (bp_location *l : b->locations ())
12725 if (l->function_name
12726 && strcmp (e->function_name.get (),
12727 l->function_name.get ()) == 0)
12728 {
12729 l->enabled = e->enabled;
12730 l->disabled_by_cond = e->disabled_by_cond;
12731 break;
12732 }
12733 }
12734 }
12735 }
12736 }
12737
12738 if (!locations_are_equal (existing_locations, b->loc))
12739 gdb::observers::breakpoint_modified.notify (b);
12740 }
12741
12742 /* Find the SaL locations corresponding to the given LOCSPEC.
12743 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12744
12745 std::vector<symtab_and_line>
12746 code_breakpoint::location_spec_to_sals (location_spec *locspec,
12747 struct program_space *search_pspace,
12748 int *found)
12749 {
12750 struct gdb_exception exception;
12751
12752 std::vector<symtab_and_line> sals;
12753
12754 try
12755 {
12756 sals = decode_location_spec (locspec, search_pspace);
12757 }
12758 catch (gdb_exception_error &e)
12759 {
12760 int not_found_and_ok = false;
12761
12762 /* For pending breakpoints, it's expected that parsing will
12763 fail until the right shared library is loaded. User has
12764 already told to create pending breakpoints and don't need
12765 extra messages. If breakpoint is in bp_shlib_disabled
12766 state, then user already saw the message about that
12767 breakpoint being disabled, and don't want to see more
12768 errors. */
12769 if (e.error == NOT_FOUND_ERROR
12770 && (condition_not_parsed
12771 || (loc != NULL
12772 && search_pspace != NULL
12773 && loc->pspace != search_pspace)
12774 || (loc && loc->shlib_disabled)
12775 || (loc && loc->pspace->executing_startup)
12776 || enable_state == bp_disabled))
12777 not_found_and_ok = true;
12778
12779 if (!not_found_and_ok)
12780 {
12781 /* We surely don't want to warn about the same breakpoint
12782 10 times. One solution, implemented here, is disable
12783 the breakpoint on error. Another solution would be to
12784 have separate 'warning emitted' flag. Since this
12785 happens only when a binary has changed, I don't know
12786 which approach is better. */
12787 enable_state = bp_disabled;
12788 throw;
12789 }
12790
12791 exception = std::move (e);
12792 }
12793
12794 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
12795 {
12796 for (auto &sal : sals)
12797 resolve_sal_pc (&sal);
12798 if (condition_not_parsed && extra_string != NULL)
12799 {
12800 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
12801 int local_thread, local_task;
12802
12803 find_condition_and_thread_for_sals (sals, extra_string.get (),
12804 &local_cond, &local_thread,
12805 &local_task, &local_extra);
12806 gdb_assert (cond_string == nullptr);
12807 if (local_cond != nullptr)
12808 cond_string = std::move (local_cond);
12809 thread = local_thread;
12810 task = local_task;
12811 if (local_extra != nullptr)
12812 extra_string = std::move (local_extra);
12813 condition_not_parsed = 0;
12814 }
12815
12816 if (type == bp_static_tracepoint)
12817 sals[0] = update_static_tracepoint (this, sals[0]);
12818
12819 *found = 1;
12820 }
12821 else
12822 *found = 0;
12823
12824 return sals;
12825 }
12826
12827 /* The default re_set method, for typical hardware or software
12828 breakpoints. Reevaluate the breakpoint and recreate its
12829 locations. */
12830
12831 void
12832 code_breakpoint::re_set_default ()
12833 {
12834 struct program_space *filter_pspace = current_program_space;
12835 std::vector<symtab_and_line> expanded, expanded_end;
12836
12837 int found;
12838 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
12839 filter_pspace,
12840 &found);
12841 if (found)
12842 expanded = std::move (sals);
12843
12844 if (locspec_range_end != nullptr)
12845 {
12846 std::vector<symtab_and_line> sals_end
12847 = location_spec_to_sals (locspec_range_end.get (),
12848 filter_pspace, &found);
12849 if (found)
12850 expanded_end = std::move (sals_end);
12851 }
12852
12853 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
12854 }
12855
12856 /* Default method for creating SALs from an address string. It basically
12857 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12858
12859 static void
12860 create_sals_from_location_spec_default (location_spec *locspec,
12861 struct linespec_result *canonical)
12862 {
12863 parse_breakpoint_sals (locspec, canonical);
12864 }
12865
12866 /* Reset a breakpoint. */
12867
12868 static void
12869 breakpoint_re_set_one (breakpoint *b)
12870 {
12871 input_radix = b->input_radix;
12872 set_language (b->language);
12873
12874 b->re_set ();
12875 }
12876
12877 /* Re-set breakpoint locations for the current program space.
12878 Locations bound to other program spaces are left untouched. */
12879
12880 void
12881 breakpoint_re_set (void)
12882 {
12883 {
12884 scoped_restore_current_language save_language;
12885 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
12886 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12887
12888 /* breakpoint_re_set_one sets the current_language to the language
12889 of the breakpoint it is resetting (see prepare_re_set_context)
12890 before re-evaluating the breakpoint's location. This change can
12891 unfortunately get undone by accident if the language_mode is set
12892 to auto, and we either switch frames, or more likely in this context,
12893 we select the current frame.
12894
12895 We prevent this by temporarily turning the language_mode to
12896 language_mode_manual. We restore it once all breakpoints
12897 have been reset. */
12898 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12899 language_mode = language_mode_manual;
12900
12901 /* Note: we must not try to insert locations until after all
12902 breakpoints have been re-set. Otherwise, e.g., when re-setting
12903 breakpoint 1, we'd insert the locations of breakpoint 2, which
12904 hadn't been re-set yet, and thus may have stale locations. */
12905
12906 for (breakpoint *b : all_breakpoints_safe ())
12907 {
12908 try
12909 {
12910 breakpoint_re_set_one (b);
12911 }
12912 catch (const gdb_exception &ex)
12913 {
12914 exception_fprintf (gdb_stderr, ex,
12915 "Error in re-setting breakpoint %d: ",
12916 b->number);
12917 }
12918 }
12919
12920 jit_breakpoint_re_set ();
12921 }
12922
12923 create_overlay_event_breakpoint ();
12924 create_longjmp_master_breakpoint ();
12925 create_std_terminate_master_breakpoint ();
12926 create_exception_master_breakpoint ();
12927
12928 /* Now we can insert. */
12929 update_global_location_list (UGLL_MAY_INSERT);
12930 }
12931 \f
12932 /* Reset the thread number of this breakpoint:
12933
12934 - If the breakpoint is for all threads, leave it as-is.
12935 - Else, reset it to the current thread for inferior_ptid. */
12936 void
12937 breakpoint_re_set_thread (struct breakpoint *b)
12938 {
12939 if (b->thread != -1)
12940 {
12941 b->thread = inferior_thread ()->global_num;
12942
12943 /* We're being called after following a fork. The new fork is
12944 selected as current, and unless this was a vfork will have a
12945 different program space from the original thread. Reset that
12946 as well. */
12947 b->loc->pspace = current_program_space;
12948 }
12949 }
12950
12951 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
12952 If from_tty is nonzero, it prints a message to that effect,
12953 which ends with a period (no newline). */
12954
12955 void
12956 set_ignore_count (int bptnum, int count, int from_tty)
12957 {
12958 if (count < 0)
12959 count = 0;
12960
12961 for (breakpoint *b : all_breakpoints ())
12962 if (b->number == bptnum)
12963 {
12964 if (is_tracepoint (b))
12965 {
12966 if (from_tty && count != 0)
12967 gdb_printf (_("Ignore count ignored for tracepoint %d."),
12968 bptnum);
12969 return;
12970 }
12971
12972 b->ignore_count = count;
12973 if (from_tty)
12974 {
12975 if (count == 0)
12976 gdb_printf (_("Will stop next time "
12977 "breakpoint %d is reached."),
12978 bptnum);
12979 else if (count == 1)
12980 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
12981 bptnum);
12982 else
12983 gdb_printf (_("Will ignore next %d "
12984 "crossings of breakpoint %d."),
12985 count, bptnum);
12986 }
12987 gdb::observers::breakpoint_modified.notify (b);
12988 return;
12989 }
12990
12991 error (_("No breakpoint number %d."), bptnum);
12992 }
12993
12994 /* Command to set ignore-count of breakpoint N to COUNT. */
12995
12996 static void
12997 ignore_command (const char *args, int from_tty)
12998 {
12999 const char *p = args;
13000 int num;
13001
13002 if (p == 0)
13003 error_no_arg (_("a breakpoint number"));
13004
13005 num = get_number (&p);
13006 if (num == 0)
13007 error (_("bad breakpoint number: '%s'"), args);
13008 if (*p == 0)
13009 error (_("Second argument (specified ignore-count) is missing."));
13010
13011 set_ignore_count (num,
13012 longest_to_int (value_as_long (parse_and_eval (p))),
13013 from_tty);
13014 if (from_tty)
13015 gdb_printf ("\n");
13016 }
13017 \f
13018
13019 /* Call FUNCTION on each of the breakpoints with numbers in the range
13020 defined by BP_NUM_RANGE (an inclusive range). */
13021
13022 static void
13023 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13024 gdb::function_view<void (breakpoint *)> function)
13025 {
13026 if (bp_num_range.first == 0)
13027 {
13028 warning (_("bad breakpoint number at or near '%d'"),
13029 bp_num_range.first);
13030 }
13031 else
13032 {
13033 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13034 {
13035 bool match = false;
13036
13037 for (breakpoint *b : all_breakpoints_safe ())
13038 if (b->number == i)
13039 {
13040 match = true;
13041 function (b);
13042 break;
13043 }
13044 if (!match)
13045 gdb_printf (_("No breakpoint number %d.\n"), i);
13046 }
13047 }
13048 }
13049
13050 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13051 ARGS. */
13052
13053 static void
13054 map_breakpoint_numbers (const char *args,
13055 gdb::function_view<void (breakpoint *)> function)
13056 {
13057 if (args == NULL || *args == '\0')
13058 error_no_arg (_("one or more breakpoint numbers"));
13059
13060 number_or_range_parser parser (args);
13061
13062 while (!parser.finished ())
13063 {
13064 int num = parser.get_number ();
13065 map_breakpoint_number_range (std::make_pair (num, num), function);
13066 }
13067 }
13068
13069 /* Return the breakpoint location structure corresponding to the
13070 BP_NUM and LOC_NUM values. */
13071
13072 static struct bp_location *
13073 find_location_by_number (int bp_num, int loc_num)
13074 {
13075 breakpoint *b = get_breakpoint (bp_num);
13076
13077 if (!b || b->number != bp_num)
13078 error (_("Bad breakpoint number '%d'"), bp_num);
13079
13080 if (loc_num == 0)
13081 error (_("Bad breakpoint location number '%d'"), loc_num);
13082
13083 int n = 0;
13084 for (bp_location *loc : b->locations ())
13085 if (++n == loc_num)
13086 return loc;
13087
13088 error (_("Bad breakpoint location number '%d'"), loc_num);
13089 }
13090
13091 /* Modes of operation for extract_bp_num. */
13092 enum class extract_bp_kind
13093 {
13094 /* Extracting a breakpoint number. */
13095 bp,
13096
13097 /* Extracting a location number. */
13098 loc,
13099 };
13100
13101 /* Extract a breakpoint or location number (as determined by KIND)
13102 from the string starting at START. TRAILER is a character which
13103 can be found after the number. If you don't want a trailer, use
13104 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13105 string. This always returns a positive integer. */
13106
13107 static int
13108 extract_bp_num (extract_bp_kind kind, const char *start,
13109 int trailer, const char **end_out = NULL)
13110 {
13111 const char *end = start;
13112 int num = get_number_trailer (&end, trailer);
13113 if (num < 0)
13114 error (kind == extract_bp_kind::bp
13115 ? _("Negative breakpoint number '%.*s'")
13116 : _("Negative breakpoint location number '%.*s'"),
13117 int (end - start), start);
13118 if (num == 0)
13119 error (kind == extract_bp_kind::bp
13120 ? _("Bad breakpoint number '%.*s'")
13121 : _("Bad breakpoint location number '%.*s'"),
13122 int (end - start), start);
13123
13124 if (end_out != NULL)
13125 *end_out = end;
13126 return num;
13127 }
13128
13129 /* Extract a breakpoint or location range (as determined by KIND) in
13130 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13131 representing the (inclusive) range. The returned pair's elements
13132 are always positive integers. */
13133
13134 static std::pair<int, int>
13135 extract_bp_or_bp_range (extract_bp_kind kind,
13136 const std::string &arg,
13137 std::string::size_type arg_offset)
13138 {
13139 std::pair<int, int> range;
13140 const char *bp_loc = &arg[arg_offset];
13141 std::string::size_type dash = arg.find ('-', arg_offset);
13142 if (dash != std::string::npos)
13143 {
13144 /* bp_loc is a range (x-z). */
13145 if (arg.length () == dash + 1)
13146 error (kind == extract_bp_kind::bp
13147 ? _("Bad breakpoint number at or near: '%s'")
13148 : _("Bad breakpoint location number at or near: '%s'"),
13149 bp_loc);
13150
13151 const char *end;
13152 const char *start_first = bp_loc;
13153 const char *start_second = &arg[dash + 1];
13154 range.first = extract_bp_num (kind, start_first, '-');
13155 range.second = extract_bp_num (kind, start_second, '\0', &end);
13156
13157 if (range.first > range.second)
13158 error (kind == extract_bp_kind::bp
13159 ? _("Inverted breakpoint range at '%.*s'")
13160 : _("Inverted breakpoint location range at '%.*s'"),
13161 int (end - start_first), start_first);
13162 }
13163 else
13164 {
13165 /* bp_loc is a single value. */
13166 range.first = extract_bp_num (kind, bp_loc, '\0');
13167 range.second = range.first;
13168 }
13169 return range;
13170 }
13171
13172 /* Extract the breakpoint/location range specified by ARG. Returns
13173 the breakpoint range in BP_NUM_RANGE, and the location range in
13174 BP_LOC_RANGE.
13175
13176 ARG may be in any of the following forms:
13177
13178 x where 'x' is a breakpoint number.
13179 x-y where 'x' and 'y' specify a breakpoint numbers range.
13180 x.y where 'x' is a breakpoint number and 'y' a location number.
13181 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13182 location number range.
13183 */
13184
13185 static void
13186 extract_bp_number_and_location (const std::string &arg,
13187 std::pair<int, int> &bp_num_range,
13188 std::pair<int, int> &bp_loc_range)
13189 {
13190 std::string::size_type dot = arg.find ('.');
13191
13192 if (dot != std::string::npos)
13193 {
13194 /* Handle 'x.y' and 'x.y-z' cases. */
13195
13196 if (arg.length () == dot + 1 || dot == 0)
13197 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13198
13199 bp_num_range.first
13200 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13201 bp_num_range.second = bp_num_range.first;
13202
13203 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13204 arg, dot + 1);
13205 }
13206 else
13207 {
13208 /* Handle x and x-y cases. */
13209
13210 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13211 bp_loc_range.first = 0;
13212 bp_loc_range.second = 0;
13213 }
13214 }
13215
13216 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13217 specifies whether to enable or disable. */
13218
13219 static void
13220 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13221 {
13222 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13223 if (loc != NULL)
13224 {
13225 if (loc->disabled_by_cond && enable)
13226 error (_("Breakpoint %d's condition is invalid at location %d, "
13227 "cannot enable."), bp_num, loc_num);
13228
13229 if (loc->enabled != enable)
13230 {
13231 loc->enabled = enable;
13232 mark_breakpoint_location_modified (loc);
13233 }
13234 if (target_supports_enable_disable_tracepoint ()
13235 && current_trace_status ()->running && loc->owner
13236 && is_tracepoint (loc->owner))
13237 target_disable_tracepoint (loc);
13238 }
13239 update_global_location_list (UGLL_DONT_INSERT);
13240
13241 gdb::observers::breakpoint_modified.notify (loc->owner);
13242 }
13243
13244 /* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13245 owner. 1-based indexing. -1 signals NOT FOUND. */
13246
13247 static int
13248 find_loc_num_by_location (const bp_location *loc)
13249 {
13250 if (loc != nullptr && loc->owner != nullptr)
13251 {
13252 /* Locations use 1-based indexing. */
13253 int loc_num = 1;
13254 for (bp_location *it : loc->owner->locations ())
13255 {
13256 if (it == loc)
13257 return loc_num;
13258 loc_num++;
13259 }
13260 }
13261 return -1;
13262 }
13263
13264 /* Enable or disable a breakpoint location LOC. ENABLE
13265 specifies whether to enable or disable. */
13266
13267 void
13268 enable_disable_bp_location (bp_location *loc, bool enable)
13269 {
13270 if (loc == nullptr)
13271 error (_("Breakpoint location is invalid."));
13272
13273 if (loc->owner == nullptr)
13274 error (_("Breakpoint location does not have an owner breakpoint."));
13275
13276 if (loc->disabled_by_cond && enable)
13277 {
13278 int loc_num = find_loc_num_by_location (loc);
13279 if (loc_num == -1)
13280 error (_("Breakpoint location LOC_NUM could not be found."));
13281 else
13282 error (_("Breakpoint %d's condition is invalid at location %d, "
13283 "cannot enable."), loc->owner->number, loc_num);
13284 }
13285
13286 if (loc->enabled != enable)
13287 {
13288 loc->enabled = enable;
13289 mark_breakpoint_location_modified (loc);
13290 }
13291
13292 if (target_supports_enable_disable_tracepoint ()
13293 && current_trace_status ()->running && loc->owner
13294 && is_tracepoint (loc->owner))
13295 target_disable_tracepoint (loc);
13296
13297 update_global_location_list (UGLL_DONT_INSERT);
13298 gdb::observers::breakpoint_modified.notify (loc->owner);
13299 }
13300
13301 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13302 number of the breakpoint, and BP_LOC_RANGE specifies the
13303 (inclusive) range of location numbers of that breakpoint to
13304 enable/disable. ENABLE specifies whether to enable or disable the
13305 location. */
13306
13307 static void
13308 enable_disable_breakpoint_location_range (int bp_num,
13309 std::pair<int, int> &bp_loc_range,
13310 bool enable)
13311 {
13312 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13313 enable_disable_bp_num_loc (bp_num, i, enable);
13314 }
13315
13316 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13317 If from_tty is nonzero, it prints a message to that effect,
13318 which ends with a period (no newline). */
13319
13320 void
13321 disable_breakpoint (struct breakpoint *bpt)
13322 {
13323 /* Never disable a watchpoint scope breakpoint; we want to
13324 hit them when we leave scope so we can delete both the
13325 watchpoint and its scope breakpoint at that time. */
13326 if (bpt->type == bp_watchpoint_scope)
13327 return;
13328
13329 bpt->enable_state = bp_disabled;
13330
13331 /* Mark breakpoint locations modified. */
13332 mark_breakpoint_modified (bpt);
13333
13334 if (target_supports_enable_disable_tracepoint ()
13335 && current_trace_status ()->running && is_tracepoint (bpt))
13336 {
13337 for (bp_location *location : bpt->locations ())
13338 target_disable_tracepoint (location);
13339 }
13340
13341 update_global_location_list (UGLL_DONT_INSERT);
13342
13343 gdb::observers::breakpoint_modified.notify (bpt);
13344 }
13345
13346 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13347 specified in ARGS. ARGS may be in any of the formats handled by
13348 extract_bp_number_and_location. ENABLE specifies whether to enable
13349 or disable the breakpoints/locations. */
13350
13351 static void
13352 enable_disable_command (const char *args, int from_tty, bool enable)
13353 {
13354 if (args == 0)
13355 {
13356 for (breakpoint *bpt : all_breakpoints ())
13357 if (user_breakpoint_p (bpt))
13358 {
13359 if (enable)
13360 enable_breakpoint (bpt);
13361 else
13362 disable_breakpoint (bpt);
13363 }
13364 }
13365 else
13366 {
13367 std::string num = extract_arg (&args);
13368
13369 while (!num.empty ())
13370 {
13371 std::pair<int, int> bp_num_range, bp_loc_range;
13372
13373 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13374
13375 if (bp_loc_range.first == bp_loc_range.second
13376 && (bp_loc_range.first == 0
13377 || (bp_loc_range.first == 1
13378 && bp_num_range.first == bp_num_range.second
13379 && !has_multiple_locations (bp_num_range.first))))
13380 {
13381 /* Handle breakpoint ids with formats 'x' or 'x-z'
13382 or 'y.1' where y has only one code location. */
13383 map_breakpoint_number_range (bp_num_range,
13384 enable
13385 ? enable_breakpoint
13386 : disable_breakpoint);
13387 }
13388 else
13389 {
13390 /* Handle breakpoint ids with formats 'x.y' or
13391 'x.y-z'. */
13392 enable_disable_breakpoint_location_range
13393 (bp_num_range.first, bp_loc_range, enable);
13394 }
13395 num = extract_arg (&args);
13396 }
13397 }
13398 }
13399
13400 /* The disable command disables the specified breakpoints/locations
13401 (or all defined breakpoints) so they're no longer effective in
13402 stopping the inferior. ARGS may be in any of the forms defined in
13403 extract_bp_number_and_location. */
13404
13405 static void
13406 disable_command (const char *args, int from_tty)
13407 {
13408 enable_disable_command (args, from_tty, false);
13409 }
13410
13411 static void
13412 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13413 int count)
13414 {
13415 int target_resources_ok;
13416
13417 if (bpt->type == bp_hardware_breakpoint)
13418 {
13419 int i;
13420 i = hw_breakpoint_used_count ();
13421 target_resources_ok =
13422 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13423 i + 1, 0);
13424 if (target_resources_ok == 0)
13425 error (_("No hardware breakpoint support in the target."));
13426 else if (target_resources_ok < 0)
13427 error (_("Hardware breakpoints used exceeds limit."));
13428 }
13429
13430 if (is_watchpoint (bpt))
13431 {
13432 /* Initialize it just to avoid a GCC false warning. */
13433 enum enable_state orig_enable_state = bp_disabled;
13434
13435 try
13436 {
13437 struct watchpoint *w = (struct watchpoint *) bpt;
13438
13439 orig_enable_state = bpt->enable_state;
13440 bpt->enable_state = bp_enabled;
13441 update_watchpoint (w, true /* reparse */);
13442 }
13443 catch (const gdb_exception &e)
13444 {
13445 bpt->enable_state = orig_enable_state;
13446 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13447 bpt->number);
13448 return;
13449 }
13450 }
13451
13452 bpt->enable_state = bp_enabled;
13453
13454 /* Mark breakpoint locations modified. */
13455 mark_breakpoint_modified (bpt);
13456
13457 if (target_supports_enable_disable_tracepoint ()
13458 && current_trace_status ()->running && is_tracepoint (bpt))
13459 {
13460 for (bp_location *location : bpt->locations ())
13461 target_enable_tracepoint (location);
13462 }
13463
13464 bpt->disposition = disposition;
13465 bpt->enable_count = count;
13466 update_global_location_list (UGLL_MAY_INSERT);
13467
13468 gdb::observers::breakpoint_modified.notify (bpt);
13469 }
13470
13471
13472 void
13473 enable_breakpoint (struct breakpoint *bpt)
13474 {
13475 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13476 }
13477
13478 /* The enable command enables the specified breakpoints/locations (or
13479 all defined breakpoints) so they once again become (or continue to
13480 be) effective in stopping the inferior. ARGS may be in any of the
13481 forms defined in extract_bp_number_and_location. */
13482
13483 static void
13484 enable_command (const char *args, int from_tty)
13485 {
13486 enable_disable_command (args, from_tty, true);
13487 }
13488
13489 static void
13490 enable_once_command (const char *args, int from_tty)
13491 {
13492 map_breakpoint_numbers
13493 (args, [&] (breakpoint *b)
13494 {
13495 iterate_over_related_breakpoints
13496 (b, [&] (breakpoint *bpt)
13497 {
13498 enable_breakpoint_disp (bpt, disp_disable, 1);
13499 });
13500 });
13501 }
13502
13503 static void
13504 enable_count_command (const char *args, int from_tty)
13505 {
13506 int count;
13507
13508 if (args == NULL)
13509 error_no_arg (_("hit count"));
13510
13511 count = get_number (&args);
13512
13513 map_breakpoint_numbers
13514 (args, [&] (breakpoint *b)
13515 {
13516 iterate_over_related_breakpoints
13517 (b, [&] (breakpoint *bpt)
13518 {
13519 enable_breakpoint_disp (bpt, disp_disable, count);
13520 });
13521 });
13522 }
13523
13524 static void
13525 enable_delete_command (const char *args, int from_tty)
13526 {
13527 map_breakpoint_numbers
13528 (args, [&] (breakpoint *b)
13529 {
13530 iterate_over_related_breakpoints
13531 (b, [&] (breakpoint *bpt)
13532 {
13533 enable_breakpoint_disp (bpt, disp_del, 1);
13534 });
13535 });
13536 }
13537 \f
13538 /* Invalidate last known value of any hardware watchpoint if
13539 the memory which that value represents has been written to by
13540 GDB itself. */
13541
13542 static void
13543 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13544 CORE_ADDR addr, ssize_t len,
13545 const bfd_byte *data)
13546 {
13547 for (breakpoint *bp : all_breakpoints ())
13548 if (bp->enable_state == bp_enabled
13549 && bp->type == bp_hardware_watchpoint)
13550 {
13551 struct watchpoint *wp = (struct watchpoint *) bp;
13552
13553 if (wp->val_valid && wp->val != nullptr)
13554 {
13555 for (bp_location *loc : bp->locations ())
13556 if (loc->loc_type == bp_loc_hardware_watchpoint
13557 && loc->address + loc->length > addr
13558 && addr + len > loc->address)
13559 {
13560 wp->val = NULL;
13561 wp->val_valid = false;
13562 }
13563 }
13564 }
13565 }
13566
13567 /* Create and insert a breakpoint for software single step. */
13568
13569 void
13570 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13571 const address_space *aspace,
13572 CORE_ADDR next_pc)
13573 {
13574 struct thread_info *tp = inferior_thread ();
13575 struct symtab_and_line sal;
13576 CORE_ADDR pc = next_pc;
13577
13578 if (tp->control.single_step_breakpoints == NULL)
13579 {
13580 std::unique_ptr<breakpoint> b
13581 (new momentary_breakpoint (gdbarch, bp_single_step,
13582 current_program_space,
13583 null_frame_id,
13584 tp->global_num));
13585
13586 tp->control.single_step_breakpoints
13587 = add_to_breakpoint_chain (std::move (b));
13588 }
13589
13590 sal = find_pc_line (pc, 0);
13591 sal.pc = pc;
13592 sal.section = find_pc_overlay (pc);
13593 sal.explicit_pc = 1;
13594
13595 auto *ss_bp
13596 = (gdb::checked_static_cast<momentary_breakpoint *>
13597 (tp->control.single_step_breakpoints));
13598 ss_bp->add_location (sal);
13599
13600 update_global_location_list (UGLL_INSERT);
13601 }
13602
13603 /* Insert single step breakpoints according to the current state. */
13604
13605 int
13606 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13607 {
13608 struct regcache *regcache = get_current_regcache ();
13609 std::vector<CORE_ADDR> next_pcs;
13610
13611 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13612
13613 if (!next_pcs.empty ())
13614 {
13615 frame_info_ptr frame = get_current_frame ();
13616 const address_space *aspace = get_frame_address_space (frame);
13617
13618 for (CORE_ADDR pc : next_pcs)
13619 insert_single_step_breakpoint (gdbarch, aspace, pc);
13620
13621 return 1;
13622 }
13623 else
13624 return 0;
13625 }
13626
13627 /* See breakpoint.h. */
13628
13629 int
13630 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13631 const address_space *aspace,
13632 CORE_ADDR pc)
13633 {
13634 for (bp_location *loc : bp->locations ())
13635 if (loc->inserted
13636 && breakpoint_location_address_match (loc, aspace, pc))
13637 return 1;
13638
13639 return 0;
13640 }
13641
13642 /* Check whether a software single-step breakpoint is inserted at
13643 PC. */
13644
13645 int
13646 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13647 CORE_ADDR pc)
13648 {
13649 for (breakpoint *bpt : all_breakpoints ())
13650 {
13651 if (bpt->type == bp_single_step
13652 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13653 return 1;
13654 }
13655 return 0;
13656 }
13657
13658 /* Tracepoint-specific operations. */
13659
13660 /* Set tracepoint count to NUM. */
13661 static void
13662 set_tracepoint_count (int num)
13663 {
13664 tracepoint_count = num;
13665 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
13666 }
13667
13668 static void
13669 trace_command (const char *arg, int from_tty)
13670 {
13671 location_spec_up locspec = string_to_location_spec (&arg,
13672 current_language);
13673 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
13674 (locspec.get (), true /* is_tracepoint */);
13675
13676 create_breakpoint (get_current_arch (),
13677 locspec.get (),
13678 NULL, 0, arg, false, 1 /* parse arg */,
13679 0 /* tempflag */,
13680 bp_tracepoint /* type_wanted */,
13681 0 /* Ignore count */,
13682 pending_break_support,
13683 ops,
13684 from_tty,
13685 1 /* enabled */,
13686 0 /* internal */, 0);
13687 }
13688
13689 static void
13690 ftrace_command (const char *arg, int from_tty)
13691 {
13692 location_spec_up locspec = string_to_location_spec (&arg,
13693 current_language);
13694 create_breakpoint (get_current_arch (),
13695 locspec.get (),
13696 NULL, 0, arg, false, 1 /* parse arg */,
13697 0 /* tempflag */,
13698 bp_fast_tracepoint /* type_wanted */,
13699 0 /* Ignore count */,
13700 pending_break_support,
13701 &code_breakpoint_ops,
13702 from_tty,
13703 1 /* enabled */,
13704 0 /* internal */, 0);
13705 }
13706
13707 /* strace command implementation. Creates a static tracepoint. */
13708
13709 static void
13710 strace_command (const char *arg, int from_tty)
13711 {
13712 const struct breakpoint_ops *ops;
13713 location_spec_up locspec;
13714 enum bptype type;
13715
13716 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13717 or with a normal static tracepoint. */
13718 if (arg && startswith (arg, "-m") && isspace (arg[2]))
13719 {
13720 ops = &strace_marker_breakpoint_ops;
13721 locspec = new_linespec_location_spec (&arg,
13722 symbol_name_match_type::FULL);
13723 type = bp_static_marker_tracepoint;
13724 }
13725 else
13726 {
13727 ops = &code_breakpoint_ops;
13728 locspec = string_to_location_spec (&arg, current_language);
13729 type = bp_static_tracepoint;
13730 }
13731
13732 create_breakpoint (get_current_arch (),
13733 locspec.get (),
13734 NULL, 0, arg, false, 1 /* parse arg */,
13735 0 /* tempflag */,
13736 type /* type_wanted */,
13737 0 /* Ignore count */,
13738 pending_break_support,
13739 ops,
13740 from_tty,
13741 1 /* enabled */,
13742 0 /* internal */, 0);
13743 }
13744
13745 /* Set up a fake reader function that gets command lines from a linked
13746 list that was acquired during tracepoint uploading. */
13747
13748 static struct uploaded_tp *this_utp;
13749 static int next_cmd;
13750
13751 static char *
13752 read_uploaded_action (void)
13753 {
13754 char *rslt = nullptr;
13755
13756 if (next_cmd < this_utp->cmd_strings.size ())
13757 {
13758 rslt = this_utp->cmd_strings[next_cmd].get ();
13759 next_cmd++;
13760 }
13761
13762 return rslt;
13763 }
13764
13765 /* Given information about a tracepoint as recorded on a target (which
13766 can be either a live system or a trace file), attempt to create an
13767 equivalent GDB tracepoint. This is not a reliable process, since
13768 the target does not necessarily have all the information used when
13769 the tracepoint was originally defined. */
13770
13771 struct tracepoint *
13772 create_tracepoint_from_upload (struct uploaded_tp *utp)
13773 {
13774 const char *addr_str;
13775 char small_buf[100];
13776 struct tracepoint *tp;
13777
13778 if (utp->at_string)
13779 addr_str = utp->at_string.get ();
13780 else
13781 {
13782 /* In the absence of a source location, fall back to raw
13783 address. Since there is no way to confirm that the address
13784 means the same thing as when the trace was started, warn the
13785 user. */
13786 warning (_("Uploaded tracepoint %d has no "
13787 "source location, using raw address"),
13788 utp->number);
13789 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
13790 addr_str = small_buf;
13791 }
13792
13793 /* There's not much we can do with a sequence of bytecodes. */
13794 if (utp->cond && !utp->cond_string)
13795 warning (_("Uploaded tracepoint %d condition "
13796 "has no source form, ignoring it"),
13797 utp->number);
13798
13799 location_spec_up locspec = string_to_location_spec (&addr_str,
13800 current_language);
13801 if (!create_breakpoint (get_current_arch (),
13802 locspec.get (),
13803 utp->cond_string.get (), -1, addr_str,
13804 false /* force_condition */,
13805 0 /* parse cond/thread */,
13806 0 /* tempflag */,
13807 utp->type /* type_wanted */,
13808 0 /* Ignore count */,
13809 pending_break_support,
13810 &code_breakpoint_ops,
13811 0 /* from_tty */,
13812 utp->enabled /* enabled */,
13813 0 /* internal */,
13814 CREATE_BREAKPOINT_FLAGS_INSERTED))
13815 return NULL;
13816
13817 /* Get the tracepoint we just created. */
13818 tp = get_tracepoint (tracepoint_count);
13819 gdb_assert (tp != NULL);
13820
13821 if (utp->pass > 0)
13822 {
13823 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
13824 tp->number);
13825
13826 trace_pass_command (small_buf, 0);
13827 }
13828
13829 /* If we have uploaded versions of the original commands, set up a
13830 special-purpose "reader" function and call the usual command line
13831 reader, then pass the result to the breakpoint command-setting
13832 function. */
13833 if (!utp->cmd_strings.empty ())
13834 {
13835 counted_command_line cmd_list;
13836
13837 this_utp = utp;
13838 next_cmd = 0;
13839
13840 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
13841
13842 breakpoint_set_commands (tp, std::move (cmd_list));
13843 }
13844 else if (!utp->actions.empty ()
13845 || !utp->step_actions.empty ())
13846 warning (_("Uploaded tracepoint %d actions "
13847 "have no source form, ignoring them"),
13848 utp->number);
13849
13850 /* Copy any status information that might be available. */
13851 tp->hit_count = utp->hit_count;
13852 tp->traceframe_usage = utp->traceframe_usage;
13853
13854 return tp;
13855 }
13856
13857 /* Print information on tracepoint number TPNUM_EXP, or all if
13858 omitted. */
13859
13860 static void
13861 info_tracepoints_command (const char *args, int from_tty)
13862 {
13863 struct ui_out *uiout = current_uiout;
13864 int num_printed;
13865
13866 num_printed = breakpoint_1 (args, false, is_tracepoint);
13867
13868 if (num_printed == 0)
13869 {
13870 if (args == NULL || *args == '\0')
13871 uiout->message ("No tracepoints.\n");
13872 else
13873 uiout->message ("No tracepoint matching '%s'.\n", args);
13874 }
13875
13876 default_collect_info ();
13877 }
13878
13879 /* The 'enable trace' command enables tracepoints.
13880 Not supported by all targets. */
13881 static void
13882 enable_trace_command (const char *args, int from_tty)
13883 {
13884 enable_command (args, from_tty);
13885 }
13886
13887 /* The 'disable trace' command disables tracepoints.
13888 Not supported by all targets. */
13889 static void
13890 disable_trace_command (const char *args, int from_tty)
13891 {
13892 disable_command (args, from_tty);
13893 }
13894
13895 /* Remove a tracepoint (or all if no argument). */
13896 static void
13897 delete_trace_command (const char *arg, int from_tty)
13898 {
13899 dont_repeat ();
13900
13901 if (arg == 0)
13902 {
13903 int breaks_to_delete = 0;
13904
13905 /* Delete all breakpoints if no argument.
13906 Do not delete internal or call-dummy breakpoints, these
13907 have to be deleted with an explicit breakpoint number
13908 argument. */
13909 for (breakpoint *tp : all_tracepoints ())
13910 if (is_tracepoint (tp) && user_breakpoint_p (tp))
13911 {
13912 breaks_to_delete = 1;
13913 break;
13914 }
13915
13916 /* Ask user only if there are some breakpoints to delete. */
13917 if (!from_tty
13918 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13919 {
13920 for (breakpoint *b : all_breakpoints_safe ())
13921 if (is_tracepoint (b) && user_breakpoint_p (b))
13922 delete_breakpoint (b);
13923 }
13924 }
13925 else
13926 map_breakpoint_numbers
13927 (arg, [&] (breakpoint *br)
13928 {
13929 iterate_over_related_breakpoints (br, delete_breakpoint);
13930 });
13931 }
13932
13933 /* Helper function for trace_pass_command. */
13934
13935 static void
13936 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
13937 {
13938 tp->pass_count = count;
13939 gdb::observers::breakpoint_modified.notify (tp);
13940 if (from_tty)
13941 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
13942 tp->number, count);
13943 }
13944
13945 /* Set passcount for tracepoint.
13946
13947 First command argument is passcount, second is tracepoint number.
13948 If tracepoint number omitted, apply to most recently defined.
13949 Also accepts special argument "all". */
13950
13951 static void
13952 trace_pass_command (const char *args, int from_tty)
13953 {
13954 struct tracepoint *t1;
13955 ULONGEST count;
13956
13957 if (args == 0 || *args == 0)
13958 error (_("passcount command requires an "
13959 "argument (count + optional TP num)"));
13960
13961 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
13962
13963 args = skip_spaces (args);
13964 if (*args && strncasecmp (args, "all", 3) == 0)
13965 {
13966 args += 3; /* Skip special argument "all". */
13967 if (*args)
13968 error (_("Junk at end of arguments."));
13969
13970 for (breakpoint *b : all_tracepoints ())
13971 {
13972 t1 = (struct tracepoint *) b;
13973 trace_pass_set_count (t1, count, from_tty);
13974 }
13975 }
13976 else if (*args == '\0')
13977 {
13978 t1 = get_tracepoint_by_number (&args, NULL);
13979 if (t1)
13980 trace_pass_set_count (t1, count, from_tty);
13981 }
13982 else
13983 {
13984 number_or_range_parser parser (args);
13985 while (!parser.finished ())
13986 {
13987 t1 = get_tracepoint_by_number (&args, &parser);
13988 if (t1)
13989 trace_pass_set_count (t1, count, from_tty);
13990 }
13991 }
13992 }
13993
13994 struct tracepoint *
13995 get_tracepoint (int num)
13996 {
13997 for (breakpoint *t : all_tracepoints ())
13998 if (t->number == num)
13999 return (struct tracepoint *) t;
14000
14001 return NULL;
14002 }
14003
14004 /* Find the tracepoint with the given target-side number (which may be
14005 different from the tracepoint number after disconnecting and
14006 reconnecting). */
14007
14008 struct tracepoint *
14009 get_tracepoint_by_number_on_target (int num)
14010 {
14011 for (breakpoint *b : all_tracepoints ())
14012 {
14013 struct tracepoint *t = (struct tracepoint *) b;
14014
14015 if (t->number_on_target == num)
14016 return t;
14017 }
14018
14019 return NULL;
14020 }
14021
14022 /* Utility: parse a tracepoint number and look it up in the list.
14023 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14024 If the argument is missing, the most recent tracepoint
14025 (tracepoint_count) is returned. */
14026
14027 struct tracepoint *
14028 get_tracepoint_by_number (const char **arg,
14029 number_or_range_parser *parser)
14030 {
14031 int tpnum;
14032 const char *instring = arg == NULL ? NULL : *arg;
14033
14034 if (parser != NULL)
14035 {
14036 gdb_assert (!parser->finished ());
14037 tpnum = parser->get_number ();
14038 }
14039 else if (arg == NULL || *arg == NULL || ! **arg)
14040 tpnum = tracepoint_count;
14041 else
14042 tpnum = get_number (arg);
14043
14044 if (tpnum <= 0)
14045 {
14046 if (instring && *instring)
14047 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14048 instring);
14049 else
14050 gdb_printf (_("No previous tracepoint\n"));
14051 return NULL;
14052 }
14053
14054 for (breakpoint *t : all_tracepoints ())
14055 if (t->number == tpnum)
14056 return (struct tracepoint *) t;
14057
14058 gdb_printf ("No tracepoint number %d.\n", tpnum);
14059 return NULL;
14060 }
14061
14062 void
14063 breakpoint::print_recreate_thread (struct ui_file *fp) const
14064 {
14065 if (thread != -1)
14066 gdb_printf (fp, " thread %d", thread);
14067
14068 if (task != 0)
14069 gdb_printf (fp, " task %d", task);
14070
14071 gdb_printf (fp, "\n");
14072 }
14073
14074 /* Save information on user settable breakpoints (watchpoints, etc) to
14075 a new script file named FILENAME. If FILTER is non-NULL, call it
14076 on each breakpoint and only include the ones for which it returns
14077 true. */
14078
14079 static void
14080 save_breakpoints (const char *filename, int from_tty,
14081 bool (*filter) (const struct breakpoint *))
14082 {
14083 bool any = false;
14084 int extra_trace_bits = 0;
14085
14086 if (filename == 0 || *filename == 0)
14087 error (_("Argument required (file name in which to save)"));
14088
14089 /* See if we have anything to save. */
14090 for (breakpoint *tp : all_breakpoints ())
14091 {
14092 /* Skip internal and momentary breakpoints. */
14093 if (!user_breakpoint_p (tp))
14094 continue;
14095
14096 /* If we have a filter, only save the breakpoints it accepts. */
14097 if (filter && !filter (tp))
14098 continue;
14099
14100 any = true;
14101
14102 if (is_tracepoint (tp))
14103 {
14104 extra_trace_bits = 1;
14105
14106 /* We can stop searching. */
14107 break;
14108 }
14109 }
14110
14111 if (!any)
14112 {
14113 warning (_("Nothing to save."));
14114 return;
14115 }
14116
14117 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14118
14119 stdio_file fp;
14120
14121 if (!fp.open (expanded_filename.get (), "w"))
14122 error (_("Unable to open file '%s' for saving (%s)"),
14123 expanded_filename.get (), safe_strerror (errno));
14124
14125 if (extra_trace_bits)
14126 save_trace_state_variables (&fp);
14127
14128 for (breakpoint *tp : all_breakpoints ())
14129 {
14130 /* Skip internal and momentary breakpoints. */
14131 if (!user_breakpoint_p (tp))
14132 continue;
14133
14134 /* If we have a filter, only save the breakpoints it accepts. */
14135 if (filter && !filter (tp))
14136 continue;
14137
14138 tp->print_recreate (&fp);
14139
14140 /* Note, we can't rely on tp->number for anything, as we can't
14141 assume the recreated breakpoint numbers will match. Use $bpnum
14142 instead. */
14143
14144 if (tp->cond_string)
14145 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
14146
14147 if (tp->ignore_count)
14148 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14149
14150 if (tp->type != bp_dprintf && tp->commands)
14151 {
14152 fp.puts (" commands\n");
14153
14154 ui_out_redirect_pop redir (current_uiout, &fp);
14155 print_command_lines (current_uiout, tp->commands.get (), 2);
14156
14157 fp.puts (" end\n");
14158 }
14159
14160 if (tp->enable_state == bp_disabled)
14161 fp.puts ("disable $bpnum\n");
14162
14163 /* If this is a multi-location breakpoint, check if the locations
14164 should be individually disabled. Watchpoint locations are
14165 special, and not user visible. */
14166 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14167 {
14168 int n = 1;
14169
14170 for (bp_location *loc : tp->locations ())
14171 {
14172 if (!loc->enabled)
14173 fp.printf ("disable $bpnum.%d\n", n);
14174
14175 n++;
14176 }
14177 }
14178 }
14179
14180 if (extra_trace_bits && !default_collect.empty ())
14181 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14182
14183 if (from_tty)
14184 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14185 }
14186
14187 /* The `save breakpoints' command. */
14188
14189 static void
14190 save_breakpoints_command (const char *args, int from_tty)
14191 {
14192 save_breakpoints (args, from_tty, NULL);
14193 }
14194
14195 /* The `save tracepoints' command. */
14196
14197 static void
14198 save_tracepoints_command (const char *args, int from_tty)
14199 {
14200 save_breakpoints (args, from_tty, is_tracepoint);
14201 }
14202
14203 \f
14204 /* This help string is used to consolidate all the help string for specifying
14205 locations used by several commands. */
14206
14207 #define LOCATION_SPEC_HELP_STRING \
14208 "Linespecs are colon-separated lists of location parameters, such as\n\
14209 source filename, function name, label name, and line number.\n\
14210 Example: To specify the start of a label named \"the_top\" in the\n\
14211 function \"fact\" in the file \"factorial.c\", use\n\
14212 \"factorial.c:fact:the_top\".\n\
14213 \n\
14214 Address locations begin with \"*\" and specify an exact address in the\n\
14215 program. Example: To specify the fourth byte past the start function\n\
14216 \"main\", use \"*main + 4\".\n\
14217 \n\
14218 Explicit locations are similar to linespecs but use an option/argument\n\
14219 syntax to specify location parameters.\n\
14220 Example: To specify the start of the label named \"the_top\" in the\n\
14221 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14222 -function fact -label the_top\".\n\
14223 \n\
14224 By default, a specified function is matched against the program's\n\
14225 functions in all scopes. For C++, this means in all namespaces and\n\
14226 classes. For Ada, this means in all packages. E.g., in C++,\n\
14227 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14228 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14229 specified name as a complete fully-qualified name instead."
14230
14231 /* This help string is used for the break, hbreak, tbreak and thbreak
14232 commands. It is defined as a macro to prevent duplication.
14233 COMMAND should be a string constant containing the name of the
14234 command. */
14235
14236 #define BREAK_ARGS_HELP(command) \
14237 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14238 \t[-force-condition] [if CONDITION]\n\
14239 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14240 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14241 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14242 `-probe-dtrace' (for a DTrace probe).\n\
14243 LOCATION may be a linespec, address, or explicit location as described\n\
14244 below.\n\
14245 \n\
14246 With no LOCATION, uses current execution address of the selected\n\
14247 stack frame. This is useful for breaking on return to a stack frame.\n\
14248 \n\
14249 THREADNUM is the number from \"info threads\".\n\
14250 CONDITION is a boolean expression.\n\
14251 \n\
14252 With the \"-force-condition\" flag, the condition is defined even when\n\
14253 it is invalid for all current locations.\n\
14254 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14255 Multiple breakpoints at one place are permitted, and useful if their\n\
14256 conditions are different.\n\
14257 \n\
14258 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14259
14260 /* List of subcommands for "catch". */
14261 static struct cmd_list_element *catch_cmdlist;
14262
14263 /* List of subcommands for "tcatch". */
14264 static struct cmd_list_element *tcatch_cmdlist;
14265
14266 void
14267 add_catch_command (const char *name, const char *docstring,
14268 cmd_func_ftype *func,
14269 completer_ftype *completer,
14270 void *user_data_catch,
14271 void *user_data_tcatch)
14272 {
14273 struct cmd_list_element *command;
14274
14275 command = add_cmd (name, class_breakpoint, docstring,
14276 &catch_cmdlist);
14277 command->func = func;
14278 command->set_context (user_data_catch);
14279 set_cmd_completer (command, completer);
14280
14281 command = add_cmd (name, class_breakpoint, docstring,
14282 &tcatch_cmdlist);
14283 command->func = func;
14284 command->set_context (user_data_tcatch);
14285 set_cmd_completer (command, completer);
14286 }
14287
14288 /* False if any of the breakpoint's locations could be a location where
14289 functions have been inlined, true otherwise. */
14290
14291 static bool
14292 is_non_inline_function (struct breakpoint *b)
14293 {
14294 /* The shared library event breakpoint is set on the address of a
14295 non-inline function. */
14296 return (b->type == bp_shlib_event);
14297 }
14298
14299 /* Nonzero if the specified PC cannot be a location where functions
14300 have been inlined. */
14301
14302 int
14303 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14304 const target_waitstatus &ws)
14305 {
14306 for (breakpoint *b : all_breakpoints ())
14307 {
14308 if (!is_non_inline_function (b))
14309 continue;
14310
14311 for (bp_location *bl : b->locations ())
14312 {
14313 if (!bl->shlib_disabled
14314 && bpstat_check_location (bl, aspace, pc, ws))
14315 return 1;
14316 }
14317 }
14318
14319 return 0;
14320 }
14321
14322 /* Remove any references to OBJFILE which is going to be freed. */
14323
14324 void
14325 breakpoint_free_objfile (struct objfile *objfile)
14326 {
14327 for (bp_location *loc : all_bp_locations ())
14328 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14329 loc->symtab = NULL;
14330 }
14331
14332 /* Chain containing all defined "enable breakpoint" subcommands. */
14333
14334 static struct cmd_list_element *enablebreaklist = NULL;
14335
14336 /* See breakpoint.h. */
14337
14338 cmd_list_element *commands_cmd_element = nullptr;
14339
14340 void _initialize_breakpoint ();
14341 void
14342 _initialize_breakpoint ()
14343 {
14344 struct cmd_list_element *c;
14345
14346 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14347 "breakpoint");
14348 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14349 "breakpoint");
14350 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14351 "breakpoint");
14352
14353 breakpoint_chain = 0;
14354 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14355 before a breakpoint is set. */
14356 breakpoint_count = 0;
14357
14358 tracepoint_count = 0;
14359
14360 add_com ("ignore", class_breakpoint, ignore_command, _("\
14361 Set ignore-count of breakpoint number N to COUNT.\n\
14362 Usage is `ignore N COUNT'."));
14363
14364 commands_cmd_element = add_com ("commands", class_breakpoint,
14365 commands_command, _("\
14366 Set commands to be executed when the given breakpoints are hit.\n\
14367 Give a space-separated breakpoint list as argument after \"commands\".\n\
14368 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14369 (e.g. `5-7').\n\
14370 With no argument, the targeted breakpoint is the last one set.\n\
14371 The commands themselves follow starting on the next line.\n\
14372 Type a line containing \"end\" to indicate the end of them.\n\
14373 Give \"silent\" as the first line to make the breakpoint silent;\n\
14374 then no output is printed when it is hit, except what the commands print."));
14375
14376 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14377 static std::string condition_command_help
14378 = gdb::option::build_help (_("\
14379 Specify breakpoint number N to break only if COND is true.\n\
14380 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14381 is an expression to be evaluated whenever breakpoint N is reached.\n\
14382 \n\
14383 Options:\n\
14384 %OPTIONS%"), cc_opts);
14385
14386 c = add_com ("condition", class_breakpoint, condition_command,
14387 condition_command_help.c_str ());
14388 set_cmd_completer_handle_brkchars (c, condition_completer);
14389
14390 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14391 Set a temporary breakpoint.\n\
14392 Like \"break\" except the breakpoint is only temporary,\n\
14393 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14394 by using \"enable delete\" on the breakpoint number.\n\
14395 \n"
14396 BREAK_ARGS_HELP ("tbreak")));
14397 set_cmd_completer (c, location_completer);
14398
14399 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14400 Set a hardware assisted breakpoint.\n\
14401 Like \"break\" except the breakpoint requires hardware support,\n\
14402 some target hardware may not have this support.\n\
14403 \n"
14404 BREAK_ARGS_HELP ("hbreak")));
14405 set_cmd_completer (c, location_completer);
14406
14407 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14408 Set a temporary hardware assisted breakpoint.\n\
14409 Like \"hbreak\" except the breakpoint is only temporary,\n\
14410 so it will be deleted when hit.\n\
14411 \n"
14412 BREAK_ARGS_HELP ("thbreak")));
14413 set_cmd_completer (c, location_completer);
14414
14415 cmd_list_element *enable_cmd
14416 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14417 Enable all or some breakpoints.\n\
14418 Usage: enable [BREAKPOINTNUM]...\n\
14419 Give breakpoint numbers (separated by spaces) as arguments.\n\
14420 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14421 This is used to cancel the effect of the \"disable\" command.\n\
14422 With a subcommand you can enable temporarily."),
14423 &enablelist, 1, &cmdlist);
14424
14425 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14426
14427 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14428 Enable all or some breakpoints.\n\
14429 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14430 Give breakpoint numbers (separated by spaces) as arguments.\n\
14431 This is used to cancel the effect of the \"disable\" command.\n\
14432 May be abbreviated to simply \"enable\"."),
14433 &enablebreaklist, 1, &enablelist);
14434
14435 add_cmd ("once", no_class, enable_once_command, _("\
14436 Enable some breakpoints for one hit.\n\
14437 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14438 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14439 &enablebreaklist);
14440
14441 add_cmd ("delete", no_class, enable_delete_command, _("\
14442 Enable some breakpoints and delete when hit.\n\
14443 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14444 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14445 &enablebreaklist);
14446
14447 add_cmd ("count", no_class, enable_count_command, _("\
14448 Enable some breakpoints for COUNT hits.\n\
14449 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14450 If a breakpoint is hit while enabled in this fashion,\n\
14451 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14452 &enablebreaklist);
14453
14454 add_cmd ("delete", no_class, enable_delete_command, _("\
14455 Enable some breakpoints and delete when hit.\n\
14456 Usage: enable delete BREAKPOINTNUM...\n\
14457 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14458 &enablelist);
14459
14460 add_cmd ("once", no_class, enable_once_command, _("\
14461 Enable some breakpoints for one hit.\n\
14462 Usage: enable once BREAKPOINTNUM...\n\
14463 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14464 &enablelist);
14465
14466 add_cmd ("count", no_class, enable_count_command, _("\
14467 Enable some breakpoints for COUNT hits.\n\
14468 Usage: enable count COUNT BREAKPOINTNUM...\n\
14469 If a breakpoint is hit while enabled in this fashion,\n\
14470 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14471 &enablelist);
14472
14473 cmd_list_element *disable_cmd
14474 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14475 Disable all or some breakpoints.\n\
14476 Usage: disable [BREAKPOINTNUM]...\n\
14477 Arguments are breakpoint numbers with spaces in between.\n\
14478 To disable all breakpoints, give no argument.\n\
14479 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14480 &disablelist, 1, &cmdlist);
14481 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14482 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14483
14484 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14485 Disable all or some breakpoints.\n\
14486 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14487 Arguments are breakpoint numbers with spaces in between.\n\
14488 To disable all breakpoints, give no argument.\n\
14489 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14490 This command may be abbreviated \"disable\"."),
14491 &disablelist);
14492
14493 cmd_list_element *delete_cmd
14494 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14495 Delete all or some breakpoints.\n\
14496 Usage: delete [BREAKPOINTNUM]...\n\
14497 Arguments are breakpoint numbers with spaces in between.\n\
14498 To delete all breakpoints, give no argument.\n\
14499 \n\
14500 Also a prefix command for deletion of other GDB objects."),
14501 &deletelist, 1, &cmdlist);
14502 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14503 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14504
14505 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14506 Delete all or some breakpoints or auto-display expressions.\n\
14507 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14508 Arguments are breakpoint numbers with spaces in between.\n\
14509 To delete all breakpoints, give no argument.\n\
14510 This command may be abbreviated \"delete\"."),
14511 &deletelist);
14512
14513 cmd_list_element *clear_cmd
14514 = add_com ("clear", class_breakpoint, clear_command, _("\
14515 Clear breakpoint at specified location.\n\
14516 Argument may be a linespec, explicit, or address location as described below.\n\
14517 \n\
14518 With no argument, clears all breakpoints in the line that the selected frame\n\
14519 is executing in.\n"
14520 "\n" LOCATION_SPEC_HELP_STRING "\n\n\
14521 See also the \"delete\" command which clears breakpoints by number."));
14522 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14523
14524 cmd_list_element *break_cmd
14525 = add_com ("break", class_breakpoint, break_command, _("\
14526 Set breakpoint at specified location.\n"
14527 BREAK_ARGS_HELP ("break")));
14528 set_cmd_completer (break_cmd, location_completer);
14529
14530 add_com_alias ("b", break_cmd, class_run, 1);
14531 add_com_alias ("br", break_cmd, class_run, 1);
14532 add_com_alias ("bre", break_cmd, class_run, 1);
14533 add_com_alias ("brea", break_cmd, class_run, 1);
14534
14535 cmd_list_element *info_breakpoints_cmd
14536 = add_info ("breakpoints", info_breakpoints_command, _("\
14537 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14538 The \"Type\" column indicates one of:\n\
14539 \tbreakpoint - normal breakpoint\n\
14540 \twatchpoint - watchpoint\n\
14541 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14542 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14543 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14544 address and file/line number respectively.\n\
14545 \n\
14546 Convenience variable \"$_\" and default examine address for \"x\"\n\
14547 are set to the address of the last breakpoint listed unless the command\n\
14548 is prefixed with \"server \".\n\n\
14549 Convenience variable \"$bpnum\" contains the number of the last\n\
14550 breakpoint set."));
14551
14552 add_info_alias ("b", info_breakpoints_cmd, 1);
14553
14554 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14555 Status of all breakpoints, or breakpoint number NUMBER.\n\
14556 The \"Type\" column indicates one of:\n\
14557 \tbreakpoint - normal breakpoint\n\
14558 \twatchpoint - watchpoint\n\
14559 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14560 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14561 \tuntil - internal breakpoint used by the \"until\" command\n\
14562 \tfinish - internal breakpoint used by the \"finish\" command\n\
14563 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14564 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14565 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14566 address and file/line number respectively.\n\
14567 \n\
14568 Convenience variable \"$_\" and default examine address for \"x\"\n\
14569 are set to the address of the last breakpoint listed unless the command\n\
14570 is prefixed with \"server \".\n\n\
14571 Convenience variable \"$bpnum\" contains the number of the last\n\
14572 breakpoint set."),
14573 &maintenanceinfolist);
14574
14575 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14576 Set catchpoints to catch events."),
14577 &catch_cmdlist,
14578 0/*allow-unknown*/, &cmdlist);
14579
14580 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14581 Set temporary catchpoints to catch events."),
14582 &tcatch_cmdlist,
14583 0/*allow-unknown*/, &cmdlist);
14584
14585 const auto opts = make_watch_options_def_group (nullptr);
14586
14587 static const std::string watch_help = gdb::option::build_help (_("\
14588 Set a watchpoint for EXPRESSION.\n\
14589 Usage: watch [-location] EXPRESSION\n\
14590 \n\
14591 Options:\n\
14592 %OPTIONS%\n\
14593 \n\
14594 A watchpoint stops execution of your program whenever the value of\n\
14595 an expression changes."), opts);
14596 c = add_com ("watch", class_breakpoint, watch_command,
14597 watch_help.c_str ());
14598 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14599
14600 static const std::string rwatch_help = gdb::option::build_help (_("\
14601 Set a read watchpoint for EXPRESSION.\n\
14602 Usage: rwatch [-location] EXPRESSION\n\
14603 \n\
14604 Options:\n\
14605 %OPTIONS%\n\
14606 \n\
14607 A read watchpoint stops execution of your program whenever the value of\n\
14608 an expression is read."), opts);
14609 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14610 rwatch_help.c_str ());
14611 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14612
14613 static const std::string awatch_help = gdb::option::build_help (_("\
14614 Set an access watchpoint for EXPRESSION.\n\
14615 Usage: awatch [-location] EXPRESSION\n\
14616 \n\
14617 Options:\n\
14618 %OPTIONS%\n\
14619 \n\
14620 An access watchpoint stops execution of your program whenever the value\n\
14621 of an expression is either read or written."), opts);
14622 c = add_com ("awatch", class_breakpoint, awatch_command,
14623 awatch_help.c_str ());
14624 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14625
14626 add_info ("watchpoints", info_watchpoints_command, _("\
14627 Status of specified watchpoints (all watchpoints if no argument)."));
14628
14629 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14630 respond to changes - contrary to the description. */
14631 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14632 &can_use_hw_watchpoints, _("\
14633 Set debugger's willingness to use watchpoint hardware."), _("\
14634 Show debugger's willingness to use watchpoint hardware."), _("\
14635 If zero, gdb will not use hardware for new watchpoints, even if\n\
14636 such is available. (However, any hardware watchpoints that were\n\
14637 created before setting this to nonzero, will continue to use watchpoint\n\
14638 hardware.)"),
14639 NULL,
14640 show_can_use_hw_watchpoints,
14641 &setlist, &showlist);
14642
14643 can_use_hw_watchpoints = 1;
14644
14645 /* Tracepoint manipulation commands. */
14646
14647 cmd_list_element *trace_cmd
14648 = add_com ("trace", class_breakpoint, trace_command, _("\
14649 Set a tracepoint at specified location.\n\
14650 \n"
14651 BREAK_ARGS_HELP ("trace") "\n\
14652 Do \"help tracepoints\" for info on other tracepoint commands."));
14653 set_cmd_completer (trace_cmd, location_completer);
14654
14655 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14656 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14657 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14658 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14659
14660 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14661 Set a fast tracepoint at specified location.\n\
14662 \n"
14663 BREAK_ARGS_HELP ("ftrace") "\n\
14664 Do \"help tracepoints\" for info on other tracepoint commands."));
14665 set_cmd_completer (c, location_completer);
14666
14667 c = add_com ("strace", class_breakpoint, strace_command, _("\
14668 Set a static tracepoint at location or marker.\n\
14669 \n\
14670 strace [LOCATION] [if CONDITION]\n\
14671 LOCATION may be a linespec, explicit, or address location (described below) \n\
14672 or -m MARKER_ID.\n\n\
14673 If a marker id is specified, probe the marker with that name. With\n\
14674 no LOCATION, uses current execution address of the selected stack frame.\n\
14675 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14676 This collects arbitrary user data passed in the probe point call to the\n\
14677 tracing library. You can inspect it when analyzing the trace buffer,\n\
14678 by printing the $_sdata variable like any other convenience variable.\n\
14679 \n\
14680 CONDITION is a boolean expression.\n\
14681 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14682 Multiple tracepoints at one place are permitted, and useful if their\n\
14683 conditions are different.\n\
14684 \n\
14685 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14686 Do \"help tracepoints\" for info on other tracepoint commands."));
14687 set_cmd_completer (c, location_completer);
14688
14689 cmd_list_element *info_tracepoints_cmd
14690 = add_info ("tracepoints", info_tracepoints_command, _("\
14691 Status of specified tracepoints (all tracepoints if no argument).\n\
14692 Convenience variable \"$tpnum\" contains the number of the\n\
14693 last tracepoint set."));
14694
14695 add_info_alias ("tp", info_tracepoints_cmd, 1);
14696
14697 cmd_list_element *delete_tracepoints_cmd
14698 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14699 Delete specified tracepoints.\n\
14700 Arguments are tracepoint numbers, separated by spaces.\n\
14701 No argument means delete all tracepoints."),
14702 &deletelist);
14703 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
14704
14705 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14706 Disable specified tracepoints.\n\
14707 Arguments are tracepoint numbers, separated by spaces.\n\
14708 No argument means disable all tracepoints."),
14709 &disablelist);
14710 deprecate_cmd (c, "disable");
14711
14712 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14713 Enable specified tracepoints.\n\
14714 Arguments are tracepoint numbers, separated by spaces.\n\
14715 No argument means enable all tracepoints."),
14716 &enablelist);
14717 deprecate_cmd (c, "enable");
14718
14719 add_com ("passcount", class_trace, trace_pass_command, _("\
14720 Set the passcount for a tracepoint.\n\
14721 The trace will end when the tracepoint has been passed 'count' times.\n\
14722 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14723 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14724
14725 add_basic_prefix_cmd ("save", class_breakpoint,
14726 _("Save breakpoint definitions as a script."),
14727 &save_cmdlist,
14728 0/*allow-unknown*/, &cmdlist);
14729
14730 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14731 Save current breakpoint definitions as a script.\n\
14732 This includes all types of breakpoints (breakpoints, watchpoints,\n\
14733 catchpoints, tracepoints). Use the 'source' command in another debug\n\
14734 session to restore them."),
14735 &save_cmdlist);
14736 set_cmd_completer (c, filename_completer);
14737
14738 cmd_list_element *save_tracepoints_cmd
14739 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
14740 Save current tracepoint definitions as a script.\n\
14741 Use the 'source' command in another debug session to restore them."),
14742 &save_cmdlist);
14743 set_cmd_completer (save_tracepoints_cmd, filename_completer);
14744
14745 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
14746 deprecate_cmd (c, "save tracepoints");
14747
14748 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14749 _("\
14750 Breakpoint specific settings.\n\
14751 Configure various breakpoint-specific variables such as\n\
14752 pending breakpoint behavior."),
14753 _("\
14754 Breakpoint specific settings.\n\
14755 Configure various breakpoint-specific variables such as\n\
14756 pending breakpoint behavior."),
14757 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14758 &setlist, &showlist);
14759
14760 add_setshow_auto_boolean_cmd ("pending", no_class,
14761 &pending_break_support, _("\
14762 Set debugger's behavior regarding pending breakpoints."), _("\
14763 Show debugger's behavior regarding pending breakpoints."), _("\
14764 If on, an unrecognized breakpoint location will cause gdb to create a\n\
14765 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14766 an error. If auto, an unrecognized breakpoint location results in a\n\
14767 user-query to see if a pending breakpoint should be created."),
14768 NULL,
14769 show_pending_break_support,
14770 &breakpoint_set_cmdlist,
14771 &breakpoint_show_cmdlist);
14772
14773 pending_break_support = AUTO_BOOLEAN_AUTO;
14774
14775 add_setshow_boolean_cmd ("auto-hw", no_class,
14776 &automatic_hardware_breakpoints, _("\
14777 Set automatic usage of hardware breakpoints."), _("\
14778 Show automatic usage of hardware breakpoints."), _("\
14779 If set, the debugger will automatically use hardware breakpoints for\n\
14780 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14781 a warning will be emitted for such breakpoints."),
14782 NULL,
14783 show_automatic_hardware_breakpoints,
14784 &breakpoint_set_cmdlist,
14785 &breakpoint_show_cmdlist);
14786
14787 add_setshow_boolean_cmd ("always-inserted", class_support,
14788 &always_inserted_mode, _("\
14789 Set mode for inserting breakpoints."), _("\
14790 Show mode for inserting breakpoints."), _("\
14791 When this mode is on, breakpoints are inserted immediately as soon as\n\
14792 they're created, kept inserted even when execution stops, and removed\n\
14793 only when the user deletes them. When this mode is off (the default),\n\
14794 breakpoints are inserted only when execution continues, and removed\n\
14795 when execution stops."),
14796 NULL,
14797 &show_always_inserted_mode,
14798 &breakpoint_set_cmdlist,
14799 &breakpoint_show_cmdlist);
14800
14801 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14802 condition_evaluation_enums,
14803 &condition_evaluation_mode_1, _("\
14804 Set mode of breakpoint condition evaluation."), _("\
14805 Show mode of breakpoint condition evaluation."), _("\
14806 When this is set to \"host\", breakpoint conditions will be\n\
14807 evaluated on the host's side by GDB. When it is set to \"target\",\n\
14808 breakpoint conditions will be downloaded to the target (if the target\n\
14809 supports such feature) and conditions will be evaluated on the target's side.\n\
14810 If this is set to \"auto\" (default), this will be automatically set to\n\
14811 \"target\" if it supports condition evaluation, otherwise it will\n\
14812 be set to \"host\"."),
14813 &set_condition_evaluation_mode,
14814 &show_condition_evaluation_mode,
14815 &breakpoint_set_cmdlist,
14816 &breakpoint_show_cmdlist);
14817
14818 add_com ("break-range", class_breakpoint, break_range_command, _("\
14819 Set a breakpoint for an address range.\n\
14820 break-range START-LOCATION, END-LOCATION\n\
14821 where START-LOCATION and END-LOCATION can be one of the following:\n\
14822 LINENUM, for that line in the current file,\n\
14823 FILE:LINENUM, for that line in that file,\n\
14824 +OFFSET, for that number of lines after the current line\n\
14825 or the start of the range\n\
14826 FUNCTION, for the first line in that function,\n\
14827 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14828 *ADDRESS, for the instruction at that address.\n\
14829 \n\
14830 The breakpoint will stop execution of the inferior whenever it executes\n\
14831 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14832 range (including START-LOCATION and END-LOCATION)."));
14833
14834 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
14835 Set a dynamic printf at specified location.\n\
14836 dprintf location,format string,arg1,arg2,...\n\
14837 location may be a linespec, explicit, or address location.\n"
14838 "\n" LOCATION_SPEC_HELP_STRING));
14839 set_cmd_completer (c, location_completer);
14840
14841 add_setshow_enum_cmd ("dprintf-style", class_support,
14842 dprintf_style_enums, &dprintf_style, _("\
14843 Set the style of usage for dynamic printf."), _("\
14844 Show the style of usage for dynamic printf."), _("\
14845 This setting chooses how GDB will do a dynamic printf.\n\
14846 If the value is \"gdb\", then the printing is done by GDB to its own\n\
14847 console, as with the \"printf\" command.\n\
14848 If the value is \"call\", the print is done by calling a function in your\n\
14849 program; by default printf(), but you can choose a different function or\n\
14850 output stream by setting dprintf-function and dprintf-channel."),
14851 update_dprintf_commands, NULL,
14852 &setlist, &showlist);
14853
14854 add_setshow_string_cmd ("dprintf-function", class_support,
14855 &dprintf_function, _("\
14856 Set the function to use for dynamic printf."), _("\
14857 Show the function to use for dynamic printf."), NULL,
14858 update_dprintf_commands, NULL,
14859 &setlist, &showlist);
14860
14861 add_setshow_string_cmd ("dprintf-channel", class_support,
14862 &dprintf_channel, _("\
14863 Set the channel to use for dynamic printf."), _("\
14864 Show the channel to use for dynamic printf."), NULL,
14865 update_dprintf_commands, NULL,
14866 &setlist, &showlist);
14867
14868 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14869 &disconnected_dprintf, _("\
14870 Set whether dprintf continues after GDB disconnects."), _("\
14871 Show whether dprintf continues after GDB disconnects."), _("\
14872 Use this to let dprintf commands continue to hit and produce output\n\
14873 even if GDB disconnects or detaches from the target."),
14874 NULL,
14875 NULL,
14876 &setlist, &showlist);
14877
14878 add_com ("agent-printf", class_vars, agent_printf_command, _("\
14879 Target agent only formatted printing, like the C \"printf\" function.\n\
14880 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14881 This supports most C printf format specifications, like %s, %d, etc.\n\
14882 This is useful for formatted output in user-defined commands."));
14883
14884 automatic_hardware_breakpoints = true;
14885
14886 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14887 "breakpoint");
14888 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14889 "breakpoint");
14890 }