gdb/breakpoint: set the condition exp after parsing the condition successfully
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70
71 /* readline include files */
72 #include "readline/tilde.h"
73
74 /* readline defines this. */
75 #undef savestring
76
77 #include "mi/mi-common.h"
78 #include "extension.h"
79 #include <algorithm>
80 #include "progspace-and-thread.h"
81 #include "gdbsupport/array-view.h"
82 #include "gdbsupport/gdb_optional.h"
83
84 /* Prototypes for local functions. */
85
86 static void map_breakpoint_numbers (const char *,
87 gdb::function_view<void (breakpoint *)>);
88
89 static void breakpoint_re_set_default (struct breakpoint *);
90
91 static void
92 create_sals_from_location_default (const struct event_location *location,
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
95
96 static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
104 int, int, int, unsigned);
105
106 static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, const struct event_location *location,
108 struct program_space *search_pspace);
109
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
112
113 static void mention (struct breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
117 const struct breakpoint_ops *);
118 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
121 /* This function is used in gdbtk sources and thus can not be made
122 static. */
123 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
124 struct symtab_and_line,
125 enum bptype,
126 const struct breakpoint_ops *);
127
128 static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
133
134 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
136 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
138 enum bptype bptype);
139
140 static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
142 struct obj_section *, int);
143
144 static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
147 static int breakpoint_locations_match (struct bp_location *loc1,
148 struct bp_location *loc2,
149 bool sw_hw_bps_match = false);
150
151 static int breakpoint_location_address_match (struct bp_location *bl,
152 const struct address_space *aspace,
153 CORE_ADDR addr);
154
155 static int breakpoint_location_address_range_overlap (struct bp_location *,
156 const address_space *,
157 CORE_ADDR, int);
158
159 static int remove_breakpoint (struct bp_location *);
160 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
161
162 static enum print_stop_action print_bp_stop_message (bpstat bs);
163
164 static int hw_breakpoint_used_count (void);
165
166 static int hw_watchpoint_use_count (struct breakpoint *);
167
168 static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
171
172 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
174
175 static void free_bp_location (struct bp_location *loc);
176 static void incref_bp_location (struct bp_location *loc);
177 static void decref_bp_location (struct bp_location **loc);
178
179 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
180
181 /* update_global_location_list's modes of operation wrt to whether to
182 insert locations now. */
183 enum ugll_insert_mode
184 {
185 /* Don't insert any breakpoint locations into the inferior, only
186 remove already-inserted locations that no longer should be
187 inserted. Functions that delete a breakpoint or breakpoints
188 should specify this mode, so that deleting a breakpoint doesn't
189 have the side effect of inserting the locations of other
190 breakpoints that are marked not-inserted, but should_be_inserted
191 returns true on them.
192
193 This behavior is useful is situations close to tear-down -- e.g.,
194 after an exec, while the target still has execution, but
195 breakpoint shadows of the previous executable image should *NOT*
196 be restored to the new image; or before detaching, where the
197 target still has execution and wants to delete breakpoints from
198 GDB's lists, and all breakpoints had already been removed from
199 the inferior. */
200 UGLL_DONT_INSERT,
201
202 /* May insert breakpoints iff breakpoints_should_be_inserted_now
203 claims breakpoints should be inserted now. */
204 UGLL_MAY_INSERT,
205
206 /* Insert locations now, irrespective of
207 breakpoints_should_be_inserted_now. E.g., say all threads are
208 stopped right now, and the user did "continue". We need to
209 insert breakpoints _before_ resuming the target, but
210 UGLL_MAY_INSERT wouldn't insert them, because
211 breakpoints_should_be_inserted_now returns false at that point,
212 as no thread is running yet. */
213 UGLL_INSERT
214 };
215
216 static void update_global_location_list (enum ugll_insert_mode);
217
218 static void update_global_location_list_nothrow (enum ugll_insert_mode);
219
220 static void insert_breakpoint_locations (void);
221
222 static void trace_pass_command (const char *, int);
223
224 static void set_tracepoint_count (int num);
225
226 static bool is_masked_watchpoint (const struct breakpoint *b);
227
228 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
229
230 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
231 otherwise. */
232
233 static int strace_marker_p (struct breakpoint *b);
234
235 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
236 that are implemented on top of software or hardware breakpoints
237 (user breakpoints, internal and momentary breakpoints, etc.). */
238 static struct breakpoint_ops bkpt_base_breakpoint_ops;
239
240 /* Internal breakpoints class type. */
241 static struct breakpoint_ops internal_breakpoint_ops;
242
243 /* Momentary breakpoints class type. */
244 static struct breakpoint_ops momentary_breakpoint_ops;
245
246 /* The breakpoint_ops structure to be used in regular user created
247 breakpoints. */
248 struct breakpoint_ops bkpt_breakpoint_ops;
249
250 /* Breakpoints set on probes. */
251 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
252
253 /* Tracepoints set on probes. */
254 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
255
256 /* Dynamic printf class type. */
257 struct breakpoint_ops dprintf_breakpoint_ops;
258
259 /* The style in which to perform a dynamic printf. This is a user
260 option because different output options have different tradeoffs;
261 if GDB does the printing, there is better error handling if there
262 is a problem with any of the arguments, but using an inferior
263 function lets you have special-purpose printers and sending of
264 output to the same place as compiled-in print functions. */
265
266 static const char dprintf_style_gdb[] = "gdb";
267 static const char dprintf_style_call[] = "call";
268 static const char dprintf_style_agent[] = "agent";
269 static const char *const dprintf_style_enums[] = {
270 dprintf_style_gdb,
271 dprintf_style_call,
272 dprintf_style_agent,
273 NULL
274 };
275 static const char *dprintf_style = dprintf_style_gdb;
276
277 /* The function to use for dynamic printf if the preferred style is to
278 call into the inferior. The value is simply a string that is
279 copied into the command, so it can be anything that GDB can
280 evaluate to a callable address, not necessarily a function name. */
281
282 static char *dprintf_function;
283
284 /* The channel to use for dynamic printf if the preferred style is to
285 call into the inferior; if a nonempty string, it will be passed to
286 the call as the first argument, with the format string as the
287 second. As with the dprintf function, this can be anything that
288 GDB knows how to evaluate, so in addition to common choices like
289 "stderr", this could be an app-specific expression like
290 "mystreams[curlogger]". */
291
292 static char *dprintf_channel;
293
294 /* True if dprintf commands should continue to operate even if GDB
295 has disconnected. */
296 static bool disconnected_dprintf = true;
297
298 struct command_line *
299 breakpoint_commands (struct breakpoint *b)
300 {
301 return b->commands ? b->commands.get () : NULL;
302 }
303
304 /* Flag indicating that a command has proceeded the inferior past the
305 current breakpoint. */
306
307 static bool breakpoint_proceeded;
308
309 const char *
310 bpdisp_text (enum bpdisp disp)
311 {
312 /* NOTE: the following values are a part of MI protocol and
313 represent values of 'disp' field returned when inferior stops at
314 a breakpoint. */
315 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
316
317 return bpdisps[(int) disp];
318 }
319
320 /* Prototypes for exported functions. */
321 /* If FALSE, gdb will not use hardware support for watchpoints, even
322 if such is available. */
323 static int can_use_hw_watchpoints;
324
325 static void
326 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
327 struct cmd_list_element *c,
328 const char *value)
329 {
330 fprintf_filtered (file,
331 _("Debugger's willingness to use "
332 "watchpoint hardware is %s.\n"),
333 value);
334 }
335
336 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
337 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
338 for unrecognized breakpoint locations.
339 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
340 static enum auto_boolean pending_break_support;
341 static void
342 show_pending_break_support (struct ui_file *file, int from_tty,
343 struct cmd_list_element *c,
344 const char *value)
345 {
346 fprintf_filtered (file,
347 _("Debugger's behavior regarding "
348 "pending breakpoints is %s.\n"),
349 value);
350 }
351
352 /* If true, gdb will automatically use hardware breakpoints for breakpoints
353 set with "break" but falling in read-only memory.
354 If false, gdb will warn about such breakpoints, but won't automatically
355 use hardware breakpoints. */
356 static bool automatic_hardware_breakpoints;
357 static void
358 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
359 struct cmd_list_element *c,
360 const char *value)
361 {
362 fprintf_filtered (file,
363 _("Automatic usage of hardware breakpoints is %s.\n"),
364 value);
365 }
366
367 /* If on, GDB keeps breakpoints inserted even if the inferior is
368 stopped, and immediately inserts any new breakpoints as soon as
369 they're created. If off (default), GDB keeps breakpoints off of
370 the target as long as possible. That is, it delays inserting
371 breakpoints until the next resume, and removes them again when the
372 target fully stops. This is a bit safer in case GDB crashes while
373 processing user input. */
374 static bool always_inserted_mode = false;
375
376 static void
377 show_always_inserted_mode (struct ui_file *file, int from_tty,
378 struct cmd_list_element *c, const char *value)
379 {
380 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
381 value);
382 }
383
384 /* See breakpoint.h. */
385
386 int
387 breakpoints_should_be_inserted_now (void)
388 {
389 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
390 {
391 /* If breakpoints are global, they should be inserted even if no
392 thread under gdb's control is running, or even if there are
393 no threads under GDB's control yet. */
394 return 1;
395 }
396 else
397 {
398 if (always_inserted_mode)
399 {
400 /* The user wants breakpoints inserted even if all threads
401 are stopped. */
402 return 1;
403 }
404
405 for (inferior *inf : all_inferiors ())
406 if (inf->has_execution ()
407 && threads_are_executing (inf->process_target ()))
408 return 1;
409
410 /* Don't remove breakpoints yet if, even though all threads are
411 stopped, we still have events to process. */
412 for (thread_info *tp : all_non_exited_threads ())
413 if (tp->resumed
414 && tp->suspend.waitstatus_pending_p)
415 return 1;
416 }
417 return 0;
418 }
419
420 static const char condition_evaluation_both[] = "host or target";
421
422 /* Modes for breakpoint condition evaluation. */
423 static const char condition_evaluation_auto[] = "auto";
424 static const char condition_evaluation_host[] = "host";
425 static const char condition_evaluation_target[] = "target";
426 static const char *const condition_evaluation_enums[] = {
427 condition_evaluation_auto,
428 condition_evaluation_host,
429 condition_evaluation_target,
430 NULL
431 };
432
433 /* Global that holds the current mode for breakpoint condition evaluation. */
434 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
435
436 /* Global that we use to display information to the user (gets its value from
437 condition_evaluation_mode_1. */
438 static const char *condition_evaluation_mode = condition_evaluation_auto;
439
440 /* Translate a condition evaluation mode MODE into either "host"
441 or "target". This is used mostly to translate from "auto" to the
442 real setting that is being used. It returns the translated
443 evaluation mode. */
444
445 static const char *
446 translate_condition_evaluation_mode (const char *mode)
447 {
448 if (mode == condition_evaluation_auto)
449 {
450 if (target_supports_evaluation_of_breakpoint_conditions ())
451 return condition_evaluation_target;
452 else
453 return condition_evaluation_host;
454 }
455 else
456 return mode;
457 }
458
459 /* Discovers what condition_evaluation_auto translates to. */
460
461 static const char *
462 breakpoint_condition_evaluation_mode (void)
463 {
464 return translate_condition_evaluation_mode (condition_evaluation_mode);
465 }
466
467 /* Return true if GDB should evaluate breakpoint conditions or false
468 otherwise. */
469
470 static int
471 gdb_evaluates_breakpoint_condition_p (void)
472 {
473 const char *mode = breakpoint_condition_evaluation_mode ();
474
475 return (mode == condition_evaluation_host);
476 }
477
478 /* Are we executing breakpoint commands? */
479 static int executing_breakpoint_commands;
480
481 /* Are overlay event breakpoints enabled? */
482 static int overlay_events_enabled;
483
484 /* See description in breakpoint.h. */
485 bool target_exact_watchpoints = false;
486
487 /* Walk the following statement or block through all breakpoints.
488 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
489 current breakpoint. */
490
491 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
492
493 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
494 for (B = breakpoint_chain; \
495 B ? (TMP=B->next, 1): 0; \
496 B = TMP)
497
498 /* Similar iterator for the low-level breakpoints. SAFE variant is
499 not provided so update_global_location_list must not be called
500 while executing the block of ALL_BP_LOCATIONS. */
501
502 #define ALL_BP_LOCATIONS(B,BP_TMP) \
503 for (BP_TMP = bp_locations; \
504 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
505 BP_TMP++)
506
507 /* Iterates through locations with address ADDRESS for the currently selected
508 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
509 to where the loop should start from.
510 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
511 appropriate location to start with. */
512
513 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
514 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
515 BP_LOCP_TMP = BP_LOCP_START; \
516 BP_LOCP_START \
517 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
518 && (*BP_LOCP_TMP)->address == ADDRESS); \
519 BP_LOCP_TMP++)
520
521 /* Iterator for tracepoints only. */
522
523 #define ALL_TRACEPOINTS(B) \
524 for (B = breakpoint_chain; B; B = B->next) \
525 if (is_tracepoint (B))
526
527 /* Chains of all breakpoints defined. */
528
529 static struct breakpoint *breakpoint_chain;
530
531 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
532
533 static struct bp_location **bp_locations;
534
535 /* Number of elements of BP_LOCATIONS. */
536
537 static unsigned bp_locations_count;
538
539 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
540 ADDRESS for the current elements of BP_LOCATIONS which get a valid
541 result from bp_location_has_shadow. You can use it for roughly
542 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
543 an address you need to read. */
544
545 static CORE_ADDR bp_locations_placed_address_before_address_max;
546
547 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
548 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
549 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
550 You can use it for roughly limiting the subrange of BP_LOCATIONS to
551 scan for shadow bytes for an address you need to read. */
552
553 static CORE_ADDR bp_locations_shadow_len_after_address_max;
554
555 /* The locations that no longer correspond to any breakpoint, unlinked
556 from the bp_locations array, but for which a hit may still be
557 reported by a target. */
558 static std::vector<bp_location *> moribund_locations;
559
560 /* Number of last breakpoint made. */
561
562 static int breakpoint_count;
563
564 /* The value of `breakpoint_count' before the last command that
565 created breakpoints. If the last (break-like) command created more
566 than one breakpoint, then the difference between BREAKPOINT_COUNT
567 and PREV_BREAKPOINT_COUNT is more than one. */
568 static int prev_breakpoint_count;
569
570 /* Number of last tracepoint made. */
571
572 static int tracepoint_count;
573
574 static struct cmd_list_element *breakpoint_set_cmdlist;
575 static struct cmd_list_element *breakpoint_show_cmdlist;
576 struct cmd_list_element *save_cmdlist;
577
578 /* See declaration at breakpoint.h. */
579
580 struct breakpoint *
581 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
582 void *user_data)
583 {
584 struct breakpoint *b = NULL;
585
586 ALL_BREAKPOINTS (b)
587 {
588 if (func (b, user_data) != 0)
589 break;
590 }
591
592 return b;
593 }
594
595 /* Return whether a breakpoint is an active enabled breakpoint. */
596 static int
597 breakpoint_enabled (struct breakpoint *b)
598 {
599 return (b->enable_state == bp_enabled);
600 }
601
602 /* Set breakpoint count to NUM. */
603
604 static void
605 set_breakpoint_count (int num)
606 {
607 prev_breakpoint_count = breakpoint_count;
608 breakpoint_count = num;
609 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
610 }
611
612 /* Used by `start_rbreak_breakpoints' below, to record the current
613 breakpoint count before "rbreak" creates any breakpoint. */
614 static int rbreak_start_breakpoint_count;
615
616 /* Called at the start an "rbreak" command to record the first
617 breakpoint made. */
618
619 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
620 {
621 rbreak_start_breakpoint_count = breakpoint_count;
622 }
623
624 /* Called at the end of an "rbreak" command to record the last
625 breakpoint made. */
626
627 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
628 {
629 prev_breakpoint_count = rbreak_start_breakpoint_count;
630 }
631
632 /* Used in run_command to zero the hit count when a new run starts. */
633
634 void
635 clear_breakpoint_hit_counts (void)
636 {
637 struct breakpoint *b;
638
639 ALL_BREAKPOINTS (b)
640 b->hit_count = 0;
641 }
642
643 \f
644 /* Return the breakpoint with the specified number, or NULL
645 if the number does not refer to an existing breakpoint. */
646
647 struct breakpoint *
648 get_breakpoint (int num)
649 {
650 struct breakpoint *b;
651
652 ALL_BREAKPOINTS (b)
653 if (b->number == num)
654 return b;
655
656 return NULL;
657 }
658
659 \f
660
661 /* Mark locations as "conditions have changed" in case the target supports
662 evaluating conditions on its side. */
663
664 static void
665 mark_breakpoint_modified (struct breakpoint *b)
666 {
667 struct bp_location *loc;
668
669 /* This is only meaningful if the target is
670 evaluating conditions and if the user has
671 opted for condition evaluation on the target's
672 side. */
673 if (gdb_evaluates_breakpoint_condition_p ()
674 || !target_supports_evaluation_of_breakpoint_conditions ())
675 return;
676
677 if (!is_breakpoint (b))
678 return;
679
680 for (loc = b->loc; loc; loc = loc->next)
681 loc->condition_changed = condition_modified;
682 }
683
684 /* Mark location as "conditions have changed" in case the target supports
685 evaluating conditions on its side. */
686
687 static void
688 mark_breakpoint_location_modified (struct bp_location *loc)
689 {
690 /* This is only meaningful if the target is
691 evaluating conditions and if the user has
692 opted for condition evaluation on the target's
693 side. */
694 if (gdb_evaluates_breakpoint_condition_p ()
695 || !target_supports_evaluation_of_breakpoint_conditions ())
696
697 return;
698
699 if (!is_breakpoint (loc->owner))
700 return;
701
702 loc->condition_changed = condition_modified;
703 }
704
705 /* Sets the condition-evaluation mode using the static global
706 condition_evaluation_mode. */
707
708 static void
709 set_condition_evaluation_mode (const char *args, int from_tty,
710 struct cmd_list_element *c)
711 {
712 const char *old_mode, *new_mode;
713
714 if ((condition_evaluation_mode_1 == condition_evaluation_target)
715 && !target_supports_evaluation_of_breakpoint_conditions ())
716 {
717 condition_evaluation_mode_1 = condition_evaluation_mode;
718 warning (_("Target does not support breakpoint condition evaluation.\n"
719 "Using host evaluation mode instead."));
720 return;
721 }
722
723 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
724 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
725
726 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
727 settings was "auto". */
728 condition_evaluation_mode = condition_evaluation_mode_1;
729
730 /* Only update the mode if the user picked a different one. */
731 if (new_mode != old_mode)
732 {
733 struct bp_location *loc, **loc_tmp;
734 /* If the user switched to a different evaluation mode, we
735 need to synch the changes with the target as follows:
736
737 "host" -> "target": Send all (valid) conditions to the target.
738 "target" -> "host": Remove all the conditions from the target.
739 */
740
741 if (new_mode == condition_evaluation_target)
742 {
743 /* Mark everything modified and synch conditions with the
744 target. */
745 ALL_BP_LOCATIONS (loc, loc_tmp)
746 mark_breakpoint_location_modified (loc);
747 }
748 else
749 {
750 /* Manually mark non-duplicate locations to synch conditions
751 with the target. We do this to remove all the conditions the
752 target knows about. */
753 ALL_BP_LOCATIONS (loc, loc_tmp)
754 if (is_breakpoint (loc->owner) && loc->inserted)
755 loc->needs_update = 1;
756 }
757
758 /* Do the update. */
759 update_global_location_list (UGLL_MAY_INSERT);
760 }
761
762 return;
763 }
764
765 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
766 what "auto" is translating to. */
767
768 static void
769 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
770 struct cmd_list_element *c, const char *value)
771 {
772 if (condition_evaluation_mode == condition_evaluation_auto)
773 fprintf_filtered (file,
774 _("Breakpoint condition evaluation "
775 "mode is %s (currently %s).\n"),
776 value,
777 breakpoint_condition_evaluation_mode ());
778 else
779 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
780 value);
781 }
782
783 /* A comparison function for bp_location AP and BP that is used by
784 bsearch. This comparison function only cares about addresses, unlike
785 the more general bp_location_is_less_than function. */
786
787 static int
788 bp_locations_compare_addrs (const void *ap, const void *bp)
789 {
790 const struct bp_location *a = *(const struct bp_location **) ap;
791 const struct bp_location *b = *(const struct bp_location **) bp;
792
793 if (a->address == b->address)
794 return 0;
795 else
796 return ((a->address > b->address) - (a->address < b->address));
797 }
798
799 /* Helper function to skip all bp_locations with addresses
800 less than ADDRESS. It returns the first bp_location that
801 is greater than or equal to ADDRESS. If none is found, just
802 return NULL. */
803
804 static struct bp_location **
805 get_first_locp_gte_addr (CORE_ADDR address)
806 {
807 struct bp_location dummy_loc;
808 struct bp_location *dummy_locp = &dummy_loc;
809 struct bp_location **locp_found = NULL;
810
811 /* Initialize the dummy location's address field. */
812 dummy_loc.address = address;
813
814 /* Find a close match to the first location at ADDRESS. */
815 locp_found = ((struct bp_location **)
816 bsearch (&dummy_locp, bp_locations, bp_locations_count,
817 sizeof (struct bp_location **),
818 bp_locations_compare_addrs));
819
820 /* Nothing was found, nothing left to do. */
821 if (locp_found == NULL)
822 return NULL;
823
824 /* We may have found a location that is at ADDRESS but is not the first in the
825 location's list. Go backwards (if possible) and locate the first one. */
826 while ((locp_found - 1) >= bp_locations
827 && (*(locp_found - 1))->address == address)
828 locp_found--;
829
830 return locp_found;
831 }
832
833 void
834 set_breakpoint_condition (struct breakpoint *b, const char *exp,
835 int from_tty)
836 {
837 if (*exp == 0)
838 {
839 xfree (b->cond_string);
840 b->cond_string = nullptr;
841
842 if (is_watchpoint (b))
843 {
844 struct watchpoint *w = (struct watchpoint *) b;
845
846 w->cond_exp.reset ();
847 }
848 else
849 {
850 struct bp_location *loc;
851
852 for (loc = b->loc; loc; loc = loc->next)
853 {
854 loc->cond.reset ();
855
856 /* No need to free the condition agent expression
857 bytecode (if we have one). We will handle this
858 when we go through update_global_location_list. */
859 }
860 }
861
862 if (from_tty)
863 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
864 }
865 else
866 {
867 const char *arg = exp;
868
869 if (is_watchpoint (b))
870 {
871 struct watchpoint *w = (struct watchpoint *) b;
872
873 innermost_block_tracker tracker;
874 arg = exp;
875 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
876 if (*arg)
877 error (_("Junk at end of expression"));
878 w->cond_exp = std::move (new_exp);
879 w->cond_exp_valid_block = tracker.block ();
880 }
881 else
882 {
883 struct bp_location *loc;
884
885 /* Parse and set condition expressions. We make two passes.
886 In the first, we parse the condition string to see if it
887 is valid in all locations. If so, the condition would be
888 accepted. So we go ahead and set the locations'
889 conditions. In case a failing case is found, we throw
890 the error and the condition string will be rejected.
891 This two-pass approach is taken to avoid setting the
892 state of locations in case of a reject. */
893 for (loc = b->loc; loc; loc = loc->next)
894 {
895 arg = exp;
896 parse_exp_1 (&arg, loc->address,
897 block_for_pc (loc->address), 0);
898 if (*arg != 0)
899 error (_("Junk at end of expression"));
900 }
901
902 /* If we reach here, the condition is valid at all locations. */
903 for (loc = b->loc; loc; loc = loc->next)
904 {
905 arg = exp;
906 loc->cond =
907 parse_exp_1 (&arg, loc->address,
908 block_for_pc (loc->address), 0);
909 }
910 }
911
912 /* We know that the new condition parsed successfully. The
913 condition string of the breakpoint can be safely updated. */
914 xfree (b->cond_string);
915 b->cond_string = xstrdup (exp);
916 b->condition_not_parsed = 0;
917 }
918 mark_breakpoint_modified (b);
919
920 gdb::observers::breakpoint_modified.notify (b);
921 }
922
923 /* Completion for the "condition" command. */
924
925 static void
926 condition_completer (struct cmd_list_element *cmd,
927 completion_tracker &tracker,
928 const char *text, const char *word)
929 {
930 const char *space;
931
932 text = skip_spaces (text);
933 space = skip_to_space (text);
934 if (*space == '\0')
935 {
936 int len;
937 struct breakpoint *b;
938
939 if (text[0] == '$')
940 {
941 /* We don't support completion of history indices. */
942 if (!isdigit (text[1]))
943 complete_internalvar (tracker, &text[1]);
944 return;
945 }
946
947 /* We're completing the breakpoint number. */
948 len = strlen (text);
949
950 ALL_BREAKPOINTS (b)
951 {
952 char number[50];
953
954 xsnprintf (number, sizeof (number), "%d", b->number);
955
956 if (strncmp (number, text, len) == 0)
957 tracker.add_completion (make_unique_xstrdup (number));
958 }
959
960 return;
961 }
962
963 /* We're completing the expression part. */
964 text = skip_spaces (space);
965 expression_completer (cmd, tracker, text, word);
966 }
967
968 /* condition N EXP -- set break condition of breakpoint N to EXP. */
969
970 static void
971 condition_command (const char *arg, int from_tty)
972 {
973 struct breakpoint *b;
974 const char *p;
975 int bnum;
976
977 if (arg == 0)
978 error_no_arg (_("breakpoint number"));
979
980 p = arg;
981 bnum = get_number (&p);
982 if (bnum == 0)
983 error (_("Bad breakpoint argument: '%s'"), arg);
984
985 ALL_BREAKPOINTS (b)
986 if (b->number == bnum)
987 {
988 /* Check if this breakpoint has a "stop" method implemented in an
989 extension language. This method and conditions entered into GDB
990 from the CLI are mutually exclusive. */
991 const struct extension_language_defn *extlang
992 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
993
994 if (extlang != NULL)
995 {
996 error (_("Only one stop condition allowed. There is currently"
997 " a %s stop condition defined for this breakpoint."),
998 ext_lang_capitalized_name (extlang));
999 }
1000 set_breakpoint_condition (b, p, from_tty);
1001
1002 if (is_breakpoint (b))
1003 update_global_location_list (UGLL_MAY_INSERT);
1004
1005 return;
1006 }
1007
1008 error (_("No breakpoint number %d."), bnum);
1009 }
1010
1011 /* Check that COMMAND do not contain commands that are suitable
1012 only for tracepoints and not suitable for ordinary breakpoints.
1013 Throw if any such commands is found. */
1014
1015 static void
1016 check_no_tracepoint_commands (struct command_line *commands)
1017 {
1018 struct command_line *c;
1019
1020 for (c = commands; c; c = c->next)
1021 {
1022 if (c->control_type == while_stepping_control)
1023 error (_("The 'while-stepping' command can "
1024 "only be used for tracepoints"));
1025
1026 check_no_tracepoint_commands (c->body_list_0.get ());
1027 check_no_tracepoint_commands (c->body_list_1.get ());
1028
1029 /* Not that command parsing removes leading whitespace and comment
1030 lines and also empty lines. So, we only need to check for
1031 command directly. */
1032 if (strstr (c->line, "collect ") == c->line)
1033 error (_("The 'collect' command can only be used for tracepoints"));
1034
1035 if (strstr (c->line, "teval ") == c->line)
1036 error (_("The 'teval' command can only be used for tracepoints"));
1037 }
1038 }
1039
1040 struct longjmp_breakpoint : public breakpoint
1041 {
1042 ~longjmp_breakpoint () override;
1043 };
1044
1045 /* Encapsulate tests for different types of tracepoints. */
1046
1047 static bool
1048 is_tracepoint_type (bptype type)
1049 {
1050 return (type == bp_tracepoint
1051 || type == bp_fast_tracepoint
1052 || type == bp_static_tracepoint);
1053 }
1054
1055 static bool
1056 is_longjmp_type (bptype type)
1057 {
1058 return type == bp_longjmp || type == bp_exception;
1059 }
1060
1061 /* See breakpoint.h. */
1062
1063 bool
1064 is_tracepoint (const struct breakpoint *b)
1065 {
1066 return is_tracepoint_type (b->type);
1067 }
1068
1069 /* Factory function to create an appropriate instance of breakpoint given
1070 TYPE. */
1071
1072 static std::unique_ptr<breakpoint>
1073 new_breakpoint_from_type (bptype type)
1074 {
1075 breakpoint *b;
1076
1077 if (is_tracepoint_type (type))
1078 b = new tracepoint ();
1079 else if (is_longjmp_type (type))
1080 b = new longjmp_breakpoint ();
1081 else
1082 b = new breakpoint ();
1083
1084 return std::unique_ptr<breakpoint> (b);
1085 }
1086
1087 /* A helper function that validates that COMMANDS are valid for a
1088 breakpoint. This function will throw an exception if a problem is
1089 found. */
1090
1091 static void
1092 validate_commands_for_breakpoint (struct breakpoint *b,
1093 struct command_line *commands)
1094 {
1095 if (is_tracepoint (b))
1096 {
1097 struct tracepoint *t = (struct tracepoint *) b;
1098 struct command_line *c;
1099 struct command_line *while_stepping = 0;
1100
1101 /* Reset the while-stepping step count. The previous commands
1102 might have included a while-stepping action, while the new
1103 ones might not. */
1104 t->step_count = 0;
1105
1106 /* We need to verify that each top-level element of commands is
1107 valid for tracepoints, that there's at most one
1108 while-stepping element, and that the while-stepping's body
1109 has valid tracing commands excluding nested while-stepping.
1110 We also need to validate the tracepoint action line in the
1111 context of the tracepoint --- validate_actionline actually
1112 has side effects, like setting the tracepoint's
1113 while-stepping STEP_COUNT, in addition to checking if the
1114 collect/teval actions parse and make sense in the
1115 tracepoint's context. */
1116 for (c = commands; c; c = c->next)
1117 {
1118 if (c->control_type == while_stepping_control)
1119 {
1120 if (b->type == bp_fast_tracepoint)
1121 error (_("The 'while-stepping' command "
1122 "cannot be used for fast tracepoint"));
1123 else if (b->type == bp_static_tracepoint)
1124 error (_("The 'while-stepping' command "
1125 "cannot be used for static tracepoint"));
1126
1127 if (while_stepping)
1128 error (_("The 'while-stepping' command "
1129 "can be used only once"));
1130 else
1131 while_stepping = c;
1132 }
1133
1134 validate_actionline (c->line, b);
1135 }
1136 if (while_stepping)
1137 {
1138 struct command_line *c2;
1139
1140 gdb_assert (while_stepping->body_list_1 == nullptr);
1141 c2 = while_stepping->body_list_0.get ();
1142 for (; c2; c2 = c2->next)
1143 {
1144 if (c2->control_type == while_stepping_control)
1145 error (_("The 'while-stepping' command cannot be nested"));
1146 }
1147 }
1148 }
1149 else
1150 {
1151 check_no_tracepoint_commands (commands);
1152 }
1153 }
1154
1155 /* Return a vector of all the static tracepoints set at ADDR. The
1156 caller is responsible for releasing the vector. */
1157
1158 std::vector<breakpoint *>
1159 static_tracepoints_here (CORE_ADDR addr)
1160 {
1161 struct breakpoint *b;
1162 std::vector<breakpoint *> found;
1163 struct bp_location *loc;
1164
1165 ALL_BREAKPOINTS (b)
1166 if (b->type == bp_static_tracepoint)
1167 {
1168 for (loc = b->loc; loc; loc = loc->next)
1169 if (loc->address == addr)
1170 found.push_back (b);
1171 }
1172
1173 return found;
1174 }
1175
1176 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1177 validate that only allowed commands are included. */
1178
1179 void
1180 breakpoint_set_commands (struct breakpoint *b,
1181 counted_command_line &&commands)
1182 {
1183 validate_commands_for_breakpoint (b, commands.get ());
1184
1185 b->commands = std::move (commands);
1186 gdb::observers::breakpoint_modified.notify (b);
1187 }
1188
1189 /* Set the internal `silent' flag on the breakpoint. Note that this
1190 is not the same as the "silent" that may appear in the breakpoint's
1191 commands. */
1192
1193 void
1194 breakpoint_set_silent (struct breakpoint *b, int silent)
1195 {
1196 int old_silent = b->silent;
1197
1198 b->silent = silent;
1199 if (old_silent != silent)
1200 gdb::observers::breakpoint_modified.notify (b);
1201 }
1202
1203 /* Set the thread for this breakpoint. If THREAD is -1, make the
1204 breakpoint work for any thread. */
1205
1206 void
1207 breakpoint_set_thread (struct breakpoint *b, int thread)
1208 {
1209 int old_thread = b->thread;
1210
1211 b->thread = thread;
1212 if (old_thread != thread)
1213 gdb::observers::breakpoint_modified.notify (b);
1214 }
1215
1216 /* Set the task for this breakpoint. If TASK is 0, make the
1217 breakpoint work for any task. */
1218
1219 void
1220 breakpoint_set_task (struct breakpoint *b, int task)
1221 {
1222 int old_task = b->task;
1223
1224 b->task = task;
1225 if (old_task != task)
1226 gdb::observers::breakpoint_modified.notify (b);
1227 }
1228
1229 static void
1230 commands_command_1 (const char *arg, int from_tty,
1231 struct command_line *control)
1232 {
1233 counted_command_line cmd;
1234 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1235 NULL after the call to read_command_lines if the user provides an empty
1236 list of command by just typing "end". */
1237 bool cmd_read = false;
1238
1239 std::string new_arg;
1240
1241 if (arg == NULL || !*arg)
1242 {
1243 if (breakpoint_count - prev_breakpoint_count > 1)
1244 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1245 breakpoint_count);
1246 else if (breakpoint_count > 0)
1247 new_arg = string_printf ("%d", breakpoint_count);
1248 arg = new_arg.c_str ();
1249 }
1250
1251 map_breakpoint_numbers
1252 (arg, [&] (breakpoint *b)
1253 {
1254 if (!cmd_read)
1255 {
1256 gdb_assert (cmd == NULL);
1257 if (control != NULL)
1258 cmd = control->body_list_0;
1259 else
1260 {
1261 std::string str
1262 = string_printf (_("Type commands for breakpoint(s) "
1263 "%s, one per line."),
1264 arg);
1265
1266 auto do_validate = [=] (const char *line)
1267 {
1268 validate_actionline (line, b);
1269 };
1270 gdb::function_view<void (const char *)> validator;
1271 if (is_tracepoint (b))
1272 validator = do_validate;
1273
1274 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1275 }
1276 cmd_read = true;
1277 }
1278
1279 /* If a breakpoint was on the list more than once, we don't need to
1280 do anything. */
1281 if (b->commands != cmd)
1282 {
1283 validate_commands_for_breakpoint (b, cmd.get ());
1284 b->commands = cmd;
1285 gdb::observers::breakpoint_modified.notify (b);
1286 }
1287 });
1288 }
1289
1290 static void
1291 commands_command (const char *arg, int from_tty)
1292 {
1293 commands_command_1 (arg, from_tty, NULL);
1294 }
1295
1296 /* Like commands_command, but instead of reading the commands from
1297 input stream, takes them from an already parsed command structure.
1298
1299 This is used by cli-script.c to DTRT with breakpoint commands
1300 that are part of if and while bodies. */
1301 enum command_control_type
1302 commands_from_control_command (const char *arg, struct command_line *cmd)
1303 {
1304 commands_command_1 (arg, 0, cmd);
1305 return simple_control;
1306 }
1307
1308 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1309
1310 static int
1311 bp_location_has_shadow (struct bp_location *bl)
1312 {
1313 if (bl->loc_type != bp_loc_software_breakpoint)
1314 return 0;
1315 if (!bl->inserted)
1316 return 0;
1317 if (bl->target_info.shadow_len == 0)
1318 /* BL isn't valid, or doesn't shadow memory. */
1319 return 0;
1320 return 1;
1321 }
1322
1323 /* Update BUF, which is LEN bytes read from the target address
1324 MEMADDR, by replacing a memory breakpoint with its shadowed
1325 contents.
1326
1327 If READBUF is not NULL, this buffer must not overlap with the of
1328 the breakpoint location's shadow_contents buffer. Otherwise, a
1329 failed assertion internal error will be raised. */
1330
1331 static void
1332 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1333 const gdb_byte *writebuf_org,
1334 ULONGEST memaddr, LONGEST len,
1335 struct bp_target_info *target_info,
1336 struct gdbarch *gdbarch)
1337 {
1338 /* Now do full processing of the found relevant range of elements. */
1339 CORE_ADDR bp_addr = 0;
1340 int bp_size = 0;
1341 int bptoffset = 0;
1342
1343 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1344 current_program_space->aspace, 0))
1345 {
1346 /* The breakpoint is inserted in a different address space. */
1347 return;
1348 }
1349
1350 /* Addresses and length of the part of the breakpoint that
1351 we need to copy. */
1352 bp_addr = target_info->placed_address;
1353 bp_size = target_info->shadow_len;
1354
1355 if (bp_addr + bp_size <= memaddr)
1356 {
1357 /* The breakpoint is entirely before the chunk of memory we are
1358 reading. */
1359 return;
1360 }
1361
1362 if (bp_addr >= memaddr + len)
1363 {
1364 /* The breakpoint is entirely after the chunk of memory we are
1365 reading. */
1366 return;
1367 }
1368
1369 /* Offset within shadow_contents. */
1370 if (bp_addr < memaddr)
1371 {
1372 /* Only copy the second part of the breakpoint. */
1373 bp_size -= memaddr - bp_addr;
1374 bptoffset = memaddr - bp_addr;
1375 bp_addr = memaddr;
1376 }
1377
1378 if (bp_addr + bp_size > memaddr + len)
1379 {
1380 /* Only copy the first part of the breakpoint. */
1381 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1382 }
1383
1384 if (readbuf != NULL)
1385 {
1386 /* Verify that the readbuf buffer does not overlap with the
1387 shadow_contents buffer. */
1388 gdb_assert (target_info->shadow_contents >= readbuf + len
1389 || readbuf >= (target_info->shadow_contents
1390 + target_info->shadow_len));
1391
1392 /* Update the read buffer with this inserted breakpoint's
1393 shadow. */
1394 memcpy (readbuf + bp_addr - memaddr,
1395 target_info->shadow_contents + bptoffset, bp_size);
1396 }
1397 else
1398 {
1399 const unsigned char *bp;
1400 CORE_ADDR addr = target_info->reqstd_address;
1401 int placed_size;
1402
1403 /* Update the shadow with what we want to write to memory. */
1404 memcpy (target_info->shadow_contents + bptoffset,
1405 writebuf_org + bp_addr - memaddr, bp_size);
1406
1407 /* Determine appropriate breakpoint contents and size for this
1408 address. */
1409 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1410
1411 /* Update the final write buffer with this inserted
1412 breakpoint's INSN. */
1413 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1414 }
1415 }
1416
1417 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1418 by replacing any memory breakpoints with their shadowed contents.
1419
1420 If READBUF is not NULL, this buffer must not overlap with any of
1421 the breakpoint location's shadow_contents buffers. Otherwise,
1422 a failed assertion internal error will be raised.
1423
1424 The range of shadowed area by each bp_location is:
1425 bl->address - bp_locations_placed_address_before_address_max
1426 up to bl->address + bp_locations_shadow_len_after_address_max
1427 The range we were requested to resolve shadows for is:
1428 memaddr ... memaddr + len
1429 Thus the safe cutoff boundaries for performance optimization are
1430 memaddr + len <= (bl->address
1431 - bp_locations_placed_address_before_address_max)
1432 and:
1433 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1434
1435 void
1436 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1437 const gdb_byte *writebuf_org,
1438 ULONGEST memaddr, LONGEST len)
1439 {
1440 /* Left boundary, right boundary and median element of our binary
1441 search. */
1442 unsigned bc_l, bc_r, bc;
1443
1444 /* Find BC_L which is a leftmost element which may affect BUF
1445 content. It is safe to report lower value but a failure to
1446 report higher one. */
1447
1448 bc_l = 0;
1449 bc_r = bp_locations_count;
1450 while (bc_l + 1 < bc_r)
1451 {
1452 struct bp_location *bl;
1453
1454 bc = (bc_l + bc_r) / 2;
1455 bl = bp_locations[bc];
1456
1457 /* Check first BL->ADDRESS will not overflow due to the added
1458 constant. Then advance the left boundary only if we are sure
1459 the BC element can in no way affect the BUF content (MEMADDR
1460 to MEMADDR + LEN range).
1461
1462 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1463 offset so that we cannot miss a breakpoint with its shadow
1464 range tail still reaching MEMADDR. */
1465
1466 if ((bl->address + bp_locations_shadow_len_after_address_max
1467 >= bl->address)
1468 && (bl->address + bp_locations_shadow_len_after_address_max
1469 <= memaddr))
1470 bc_l = bc;
1471 else
1472 bc_r = bc;
1473 }
1474
1475 /* Due to the binary search above, we need to make sure we pick the
1476 first location that's at BC_L's address. E.g., if there are
1477 multiple locations at the same address, BC_L may end up pointing
1478 at a duplicate location, and miss the "master"/"inserted"
1479 location. Say, given locations L1, L2 and L3 at addresses A and
1480 B:
1481
1482 L1@A, L2@A, L3@B, ...
1483
1484 BC_L could end up pointing at location L2, while the "master"
1485 location could be L1. Since the `loc->inserted' flag is only set
1486 on "master" locations, we'd forget to restore the shadow of L1
1487 and L2. */
1488 while (bc_l > 0
1489 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1490 bc_l--;
1491
1492 /* Now do full processing of the found relevant range of elements. */
1493
1494 for (bc = bc_l; bc < bp_locations_count; bc++)
1495 {
1496 struct bp_location *bl = bp_locations[bc];
1497
1498 /* bp_location array has BL->OWNER always non-NULL. */
1499 if (bl->owner->type == bp_none)
1500 warning (_("reading through apparently deleted breakpoint #%d?"),
1501 bl->owner->number);
1502
1503 /* Performance optimization: any further element can no longer affect BUF
1504 content. */
1505
1506 if (bl->address >= bp_locations_placed_address_before_address_max
1507 && memaddr + len <= (bl->address
1508 - bp_locations_placed_address_before_address_max))
1509 break;
1510
1511 if (!bp_location_has_shadow (bl))
1512 continue;
1513
1514 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1515 memaddr, len, &bl->target_info, bl->gdbarch);
1516 }
1517 }
1518
1519 /* See breakpoint.h. */
1520
1521 bool
1522 is_breakpoint (const struct breakpoint *bpt)
1523 {
1524 return (bpt->type == bp_breakpoint
1525 || bpt->type == bp_hardware_breakpoint
1526 || bpt->type == bp_dprintf);
1527 }
1528
1529 /* Return true if BPT is of any hardware watchpoint kind. */
1530
1531 static bool
1532 is_hardware_watchpoint (const struct breakpoint *bpt)
1533 {
1534 return (bpt->type == bp_hardware_watchpoint
1535 || bpt->type == bp_read_watchpoint
1536 || bpt->type == bp_access_watchpoint);
1537 }
1538
1539 /* See breakpoint.h. */
1540
1541 bool
1542 is_watchpoint (const struct breakpoint *bpt)
1543 {
1544 return (is_hardware_watchpoint (bpt)
1545 || bpt->type == bp_watchpoint);
1546 }
1547
1548 /* Returns true if the current thread and its running state are safe
1549 to evaluate or update watchpoint B. Watchpoints on local
1550 expressions need to be evaluated in the context of the thread that
1551 was current when the watchpoint was created, and, that thread needs
1552 to be stopped to be able to select the correct frame context.
1553 Watchpoints on global expressions can be evaluated on any thread,
1554 and in any state. It is presently left to the target allowing
1555 memory accesses when threads are running. */
1556
1557 static int
1558 watchpoint_in_thread_scope (struct watchpoint *b)
1559 {
1560 return (b->pspace == current_program_space
1561 && (b->watchpoint_thread == null_ptid
1562 || (inferior_ptid == b->watchpoint_thread
1563 && !inferior_thread ()->executing)));
1564 }
1565
1566 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1567 associated bp_watchpoint_scope breakpoint. */
1568
1569 static void
1570 watchpoint_del_at_next_stop (struct watchpoint *w)
1571 {
1572 if (w->related_breakpoint != w)
1573 {
1574 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1575 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1576 w->related_breakpoint->disposition = disp_del_at_next_stop;
1577 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1578 w->related_breakpoint = w;
1579 }
1580 w->disposition = disp_del_at_next_stop;
1581 }
1582
1583 /* Extract a bitfield value from value VAL using the bit parameters contained in
1584 watchpoint W. */
1585
1586 static struct value *
1587 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1588 {
1589 struct value *bit_val;
1590
1591 if (val == NULL)
1592 return NULL;
1593
1594 bit_val = allocate_value (value_type (val));
1595
1596 unpack_value_bitfield (bit_val,
1597 w->val_bitpos,
1598 w->val_bitsize,
1599 value_contents_for_printing (val),
1600 value_offset (val),
1601 val);
1602
1603 return bit_val;
1604 }
1605
1606 /* Allocate a dummy location and add it to B, which must be a software
1607 watchpoint. This is required because even if a software watchpoint
1608 is not watching any memory, bpstat_stop_status requires a location
1609 to be able to report stops. */
1610
1611 static void
1612 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1613 struct program_space *pspace)
1614 {
1615 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1616
1617 b->loc = allocate_bp_location (b);
1618 b->loc->pspace = pspace;
1619 b->loc->address = -1;
1620 b->loc->length = -1;
1621 }
1622
1623 /* Returns true if B is a software watchpoint that is not watching any
1624 memory (e.g., "watch $pc"). */
1625
1626 static bool
1627 is_no_memory_software_watchpoint (struct breakpoint *b)
1628 {
1629 return (b->type == bp_watchpoint
1630 && b->loc != NULL
1631 && b->loc->next == NULL
1632 && b->loc->address == -1
1633 && b->loc->length == -1);
1634 }
1635
1636 /* Assuming that B is a watchpoint:
1637 - Reparse watchpoint expression, if REPARSE is non-zero
1638 - Evaluate expression and store the result in B->val
1639 - Evaluate the condition if there is one, and store the result
1640 in b->loc->cond.
1641 - Update the list of values that must be watched in B->loc.
1642
1643 If the watchpoint disposition is disp_del_at_next_stop, then do
1644 nothing. If this is local watchpoint that is out of scope, delete
1645 it.
1646
1647 Even with `set breakpoint always-inserted on' the watchpoints are
1648 removed + inserted on each stop here. Normal breakpoints must
1649 never be removed because they might be missed by a running thread
1650 when debugging in non-stop mode. On the other hand, hardware
1651 watchpoints (is_hardware_watchpoint; processed here) are specific
1652 to each LWP since they are stored in each LWP's hardware debug
1653 registers. Therefore, such LWP must be stopped first in order to
1654 be able to modify its hardware watchpoints.
1655
1656 Hardware watchpoints must be reset exactly once after being
1657 presented to the user. It cannot be done sooner, because it would
1658 reset the data used to present the watchpoint hit to the user. And
1659 it must not be done later because it could display the same single
1660 watchpoint hit during multiple GDB stops. Note that the latter is
1661 relevant only to the hardware watchpoint types bp_read_watchpoint
1662 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1663 not user-visible - its hit is suppressed if the memory content has
1664 not changed.
1665
1666 The following constraints influence the location where we can reset
1667 hardware watchpoints:
1668
1669 * target_stopped_by_watchpoint and target_stopped_data_address are
1670 called several times when GDB stops.
1671
1672 [linux]
1673 * Multiple hardware watchpoints can be hit at the same time,
1674 causing GDB to stop. GDB only presents one hardware watchpoint
1675 hit at a time as the reason for stopping, and all the other hits
1676 are presented later, one after the other, each time the user
1677 requests the execution to be resumed. Execution is not resumed
1678 for the threads still having pending hit event stored in
1679 LWP_INFO->STATUS. While the watchpoint is already removed from
1680 the inferior on the first stop the thread hit event is kept being
1681 reported from its cached value by linux_nat_stopped_data_address
1682 until the real thread resume happens after the watchpoint gets
1683 presented and thus its LWP_INFO->STATUS gets reset.
1684
1685 Therefore the hardware watchpoint hit can get safely reset on the
1686 watchpoint removal from inferior. */
1687
1688 static void
1689 update_watchpoint (struct watchpoint *b, int reparse)
1690 {
1691 int within_current_scope;
1692 struct frame_id saved_frame_id;
1693 int frame_saved;
1694
1695 /* If this is a local watchpoint, we only want to check if the
1696 watchpoint frame is in scope if the current thread is the thread
1697 that was used to create the watchpoint. */
1698 if (!watchpoint_in_thread_scope (b))
1699 return;
1700
1701 if (b->disposition == disp_del_at_next_stop)
1702 return;
1703
1704 frame_saved = 0;
1705
1706 /* Determine if the watchpoint is within scope. */
1707 if (b->exp_valid_block == NULL)
1708 within_current_scope = 1;
1709 else
1710 {
1711 struct frame_info *fi = get_current_frame ();
1712 struct gdbarch *frame_arch = get_frame_arch (fi);
1713 CORE_ADDR frame_pc = get_frame_pc (fi);
1714
1715 /* If we're at a point where the stack has been destroyed
1716 (e.g. in a function epilogue), unwinding may not work
1717 properly. Do not attempt to recreate locations at this
1718 point. See similar comments in watchpoint_check. */
1719 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1720 return;
1721
1722 /* Save the current frame's ID so we can restore it after
1723 evaluating the watchpoint expression on its own frame. */
1724 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1725 took a frame parameter, so that we didn't have to change the
1726 selected frame. */
1727 frame_saved = 1;
1728 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1729
1730 fi = frame_find_by_id (b->watchpoint_frame);
1731 within_current_scope = (fi != NULL);
1732 if (within_current_scope)
1733 select_frame (fi);
1734 }
1735
1736 /* We don't free locations. They are stored in the bp_location array
1737 and update_global_location_list will eventually delete them and
1738 remove breakpoints if needed. */
1739 b->loc = NULL;
1740
1741 if (within_current_scope && reparse)
1742 {
1743 const char *s;
1744
1745 b->exp.reset ();
1746 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1747 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1748 /* If the meaning of expression itself changed, the old value is
1749 no longer relevant. We don't want to report a watchpoint hit
1750 to the user when the old value and the new value may actually
1751 be completely different objects. */
1752 b->val = NULL;
1753 b->val_valid = false;
1754
1755 /* Note that unlike with breakpoints, the watchpoint's condition
1756 expression is stored in the breakpoint object, not in the
1757 locations (re)created below. */
1758 if (b->cond_string != NULL)
1759 {
1760 b->cond_exp.reset ();
1761
1762 s = b->cond_string;
1763 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1764 }
1765 }
1766
1767 /* If we failed to parse the expression, for example because
1768 it refers to a global variable in a not-yet-loaded shared library,
1769 don't try to insert watchpoint. We don't automatically delete
1770 such watchpoint, though, since failure to parse expression
1771 is different from out-of-scope watchpoint. */
1772 if (!target_has_execution)
1773 {
1774 /* Without execution, memory can't change. No use to try and
1775 set watchpoint locations. The watchpoint will be reset when
1776 the target gains execution, through breakpoint_re_set. */
1777 if (!can_use_hw_watchpoints)
1778 {
1779 if (b->ops->works_in_software_mode (b))
1780 b->type = bp_watchpoint;
1781 else
1782 error (_("Can't set read/access watchpoint when "
1783 "hardware watchpoints are disabled."));
1784 }
1785 }
1786 else if (within_current_scope && b->exp)
1787 {
1788 int pc = 0;
1789 std::vector<value_ref_ptr> val_chain;
1790 struct value *v, *result;
1791 struct program_space *frame_pspace;
1792
1793 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1794
1795 /* Avoid setting b->val if it's already set. The meaning of
1796 b->val is 'the last value' user saw, and we should update
1797 it only if we reported that last value to user. As it
1798 happens, the code that reports it updates b->val directly.
1799 We don't keep track of the memory value for masked
1800 watchpoints. */
1801 if (!b->val_valid && !is_masked_watchpoint (b))
1802 {
1803 if (b->val_bitsize != 0)
1804 v = extract_bitfield_from_watchpoint_value (b, v);
1805 b->val = release_value (v);
1806 b->val_valid = true;
1807 }
1808
1809 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1810
1811 /* Look at each value on the value chain. */
1812 gdb_assert (!val_chain.empty ());
1813 for (const value_ref_ptr &iter : val_chain)
1814 {
1815 v = iter.get ();
1816
1817 /* If it's a memory location, and GDB actually needed
1818 its contents to evaluate the expression, then we
1819 must watch it. If the first value returned is
1820 still lazy, that means an error occurred reading it;
1821 watch it anyway in case it becomes readable. */
1822 if (VALUE_LVAL (v) == lval_memory
1823 && (v == val_chain[0] || ! value_lazy (v)))
1824 {
1825 struct type *vtype = check_typedef (value_type (v));
1826
1827 /* We only watch structs and arrays if user asked
1828 for it explicitly, never if they just happen to
1829 appear in the middle of some value chain. */
1830 if (v == result
1831 || (vtype->code () != TYPE_CODE_STRUCT
1832 && vtype->code () != TYPE_CODE_ARRAY))
1833 {
1834 CORE_ADDR addr;
1835 enum target_hw_bp_type type;
1836 struct bp_location *loc, **tmp;
1837 int bitpos = 0, bitsize = 0;
1838
1839 if (value_bitsize (v) != 0)
1840 {
1841 /* Extract the bit parameters out from the bitfield
1842 sub-expression. */
1843 bitpos = value_bitpos (v);
1844 bitsize = value_bitsize (v);
1845 }
1846 else if (v == result && b->val_bitsize != 0)
1847 {
1848 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1849 lvalue whose bit parameters are saved in the fields
1850 VAL_BITPOS and VAL_BITSIZE. */
1851 bitpos = b->val_bitpos;
1852 bitsize = b->val_bitsize;
1853 }
1854
1855 addr = value_address (v);
1856 if (bitsize != 0)
1857 {
1858 /* Skip the bytes that don't contain the bitfield. */
1859 addr += bitpos / 8;
1860 }
1861
1862 type = hw_write;
1863 if (b->type == bp_read_watchpoint)
1864 type = hw_read;
1865 else if (b->type == bp_access_watchpoint)
1866 type = hw_access;
1867
1868 loc = allocate_bp_location (b);
1869 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1870 ;
1871 *tmp = loc;
1872 loc->gdbarch = get_type_arch (value_type (v));
1873
1874 loc->pspace = frame_pspace;
1875 loc->address = address_significant (loc->gdbarch, addr);
1876
1877 if (bitsize != 0)
1878 {
1879 /* Just cover the bytes that make up the bitfield. */
1880 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1881 }
1882 else
1883 loc->length = TYPE_LENGTH (value_type (v));
1884
1885 loc->watchpoint_type = type;
1886 }
1887 }
1888 }
1889
1890 /* Change the type of breakpoint between hardware assisted or
1891 an ordinary watchpoint depending on the hardware support
1892 and free hardware slots. REPARSE is set when the inferior
1893 is started. */
1894 if (reparse)
1895 {
1896 int reg_cnt;
1897 enum bp_loc_type loc_type;
1898 struct bp_location *bl;
1899
1900 reg_cnt = can_use_hardware_watchpoint (val_chain);
1901
1902 if (reg_cnt)
1903 {
1904 int i, target_resources_ok, other_type_used;
1905 enum bptype type;
1906
1907 /* Use an exact watchpoint when there's only one memory region to be
1908 watched, and only one debug register is needed to watch it. */
1909 b->exact = target_exact_watchpoints && reg_cnt == 1;
1910
1911 /* We need to determine how many resources are already
1912 used for all other hardware watchpoints plus this one
1913 to see if we still have enough resources to also fit
1914 this watchpoint in as well. */
1915
1916 /* If this is a software watchpoint, we try to turn it
1917 to a hardware one -- count resources as if B was of
1918 hardware watchpoint type. */
1919 type = b->type;
1920 if (type == bp_watchpoint)
1921 type = bp_hardware_watchpoint;
1922
1923 /* This watchpoint may or may not have been placed on
1924 the list yet at this point (it won't be in the list
1925 if we're trying to create it for the first time,
1926 through watch_command), so always account for it
1927 manually. */
1928
1929 /* Count resources used by all watchpoints except B. */
1930 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1931
1932 /* Add in the resources needed for B. */
1933 i += hw_watchpoint_use_count (b);
1934
1935 target_resources_ok
1936 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1937 if (target_resources_ok <= 0)
1938 {
1939 int sw_mode = b->ops->works_in_software_mode (b);
1940
1941 if (target_resources_ok == 0 && !sw_mode)
1942 error (_("Target does not support this type of "
1943 "hardware watchpoint."));
1944 else if (target_resources_ok < 0 && !sw_mode)
1945 error (_("There are not enough available hardware "
1946 "resources for this watchpoint."));
1947
1948 /* Downgrade to software watchpoint. */
1949 b->type = bp_watchpoint;
1950 }
1951 else
1952 {
1953 /* If this was a software watchpoint, we've just
1954 found we have enough resources to turn it to a
1955 hardware watchpoint. Otherwise, this is a
1956 nop. */
1957 b->type = type;
1958 }
1959 }
1960 else if (!b->ops->works_in_software_mode (b))
1961 {
1962 if (!can_use_hw_watchpoints)
1963 error (_("Can't set read/access watchpoint when "
1964 "hardware watchpoints are disabled."));
1965 else
1966 error (_("Expression cannot be implemented with "
1967 "read/access watchpoint."));
1968 }
1969 else
1970 b->type = bp_watchpoint;
1971
1972 loc_type = (b->type == bp_watchpoint? bp_loc_other
1973 : bp_loc_hardware_watchpoint);
1974 for (bl = b->loc; bl; bl = bl->next)
1975 bl->loc_type = loc_type;
1976 }
1977
1978 /* If a software watchpoint is not watching any memory, then the
1979 above left it without any location set up. But,
1980 bpstat_stop_status requires a location to be able to report
1981 stops, so make sure there's at least a dummy one. */
1982 if (b->type == bp_watchpoint && b->loc == NULL)
1983 software_watchpoint_add_no_memory_location (b, frame_pspace);
1984 }
1985 else if (!within_current_scope)
1986 {
1987 printf_filtered (_("\
1988 Watchpoint %d deleted because the program has left the block\n\
1989 in which its expression is valid.\n"),
1990 b->number);
1991 watchpoint_del_at_next_stop (b);
1992 }
1993
1994 /* Restore the selected frame. */
1995 if (frame_saved)
1996 select_frame (frame_find_by_id (saved_frame_id));
1997 }
1998
1999
2000 /* Returns 1 iff breakpoint location should be
2001 inserted in the inferior. We don't differentiate the type of BL's owner
2002 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2003 breakpoint_ops is not defined, because in insert_bp_location,
2004 tracepoint's insert_location will not be called. */
2005 static int
2006 should_be_inserted (struct bp_location *bl)
2007 {
2008 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2009 return 0;
2010
2011 if (bl->owner->disposition == disp_del_at_next_stop)
2012 return 0;
2013
2014 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2015 return 0;
2016
2017 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2018 return 0;
2019
2020 /* This is set for example, when we're attached to the parent of a
2021 vfork, and have detached from the child. The child is running
2022 free, and we expect it to do an exec or exit, at which point the
2023 OS makes the parent schedulable again (and the target reports
2024 that the vfork is done). Until the child is done with the shared
2025 memory region, do not insert breakpoints in the parent, otherwise
2026 the child could still trip on the parent's breakpoints. Since
2027 the parent is blocked anyway, it won't miss any breakpoint. */
2028 if (bl->pspace->breakpoints_not_allowed)
2029 return 0;
2030
2031 /* Don't insert a breakpoint if we're trying to step past its
2032 location, except if the breakpoint is a single-step breakpoint,
2033 and the breakpoint's thread is the thread which is stepping past
2034 a breakpoint. */
2035 if ((bl->loc_type == bp_loc_software_breakpoint
2036 || bl->loc_type == bp_loc_hardware_breakpoint)
2037 && stepping_past_instruction_at (bl->pspace->aspace,
2038 bl->address)
2039 /* The single-step breakpoint may be inserted at the location
2040 we're trying to step if the instruction branches to itself.
2041 However, the instruction won't be executed at all and it may
2042 break the semantics of the instruction, for example, the
2043 instruction is a conditional branch or updates some flags.
2044 We can't fix it unless GDB is able to emulate the instruction
2045 or switch to displaced stepping. */
2046 && !(bl->owner->type == bp_single_step
2047 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2048 {
2049 if (debug_infrun)
2050 {
2051 fprintf_unfiltered (gdb_stdlog,
2052 "infrun: skipping breakpoint: "
2053 "stepping past insn at: %s\n",
2054 paddress (bl->gdbarch, bl->address));
2055 }
2056 return 0;
2057 }
2058
2059 /* Don't insert watchpoints if we're trying to step past the
2060 instruction that triggered one. */
2061 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2062 && stepping_past_nonsteppable_watchpoint ())
2063 {
2064 if (debug_infrun)
2065 {
2066 fprintf_unfiltered (gdb_stdlog,
2067 "infrun: stepping past non-steppable watchpoint. "
2068 "skipping watchpoint at %s:%d\n",
2069 paddress (bl->gdbarch, bl->address),
2070 bl->length);
2071 }
2072 return 0;
2073 }
2074
2075 return 1;
2076 }
2077
2078 /* Same as should_be_inserted but does the check assuming
2079 that the location is not duplicated. */
2080
2081 static int
2082 unduplicated_should_be_inserted (struct bp_location *bl)
2083 {
2084 int result;
2085 const int save_duplicate = bl->duplicate;
2086
2087 bl->duplicate = 0;
2088 result = should_be_inserted (bl);
2089 bl->duplicate = save_duplicate;
2090 return result;
2091 }
2092
2093 /* Parses a conditional described by an expression COND into an
2094 agent expression bytecode suitable for evaluation
2095 by the bytecode interpreter. Return NULL if there was
2096 any error during parsing. */
2097
2098 static agent_expr_up
2099 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2100 {
2101 if (cond == NULL)
2102 return NULL;
2103
2104 agent_expr_up aexpr;
2105
2106 /* We don't want to stop processing, so catch any errors
2107 that may show up. */
2108 try
2109 {
2110 aexpr = gen_eval_for_expr (scope, cond);
2111 }
2112
2113 catch (const gdb_exception_error &ex)
2114 {
2115 /* If we got here, it means the condition could not be parsed to a valid
2116 bytecode expression and thus can't be evaluated on the target's side.
2117 It's no use iterating through the conditions. */
2118 }
2119
2120 /* We have a valid agent expression. */
2121 return aexpr;
2122 }
2123
2124 /* Based on location BL, create a list of breakpoint conditions to be
2125 passed on to the target. If we have duplicated locations with different
2126 conditions, we will add such conditions to the list. The idea is that the
2127 target will evaluate the list of conditions and will only notify GDB when
2128 one of them is true. */
2129
2130 static void
2131 build_target_condition_list (struct bp_location *bl)
2132 {
2133 struct bp_location **locp = NULL, **loc2p;
2134 int null_condition_or_parse_error = 0;
2135 int modified = bl->needs_update;
2136 struct bp_location *loc;
2137
2138 /* Release conditions left over from a previous insert. */
2139 bl->target_info.conditions.clear ();
2140
2141 /* This is only meaningful if the target is
2142 evaluating conditions and if the user has
2143 opted for condition evaluation on the target's
2144 side. */
2145 if (gdb_evaluates_breakpoint_condition_p ()
2146 || !target_supports_evaluation_of_breakpoint_conditions ())
2147 return;
2148
2149 /* Do a first pass to check for locations with no assigned
2150 conditions or conditions that fail to parse to a valid agent
2151 expression bytecode. If any of these happen, then it's no use to
2152 send conditions to the target since this location will always
2153 trigger and generate a response back to GDB. Note we consider
2154 all locations at the same address irrespective of type, i.e.,
2155 even if the locations aren't considered duplicates (e.g.,
2156 software breakpoint and hardware breakpoint at the same
2157 address). */
2158 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2159 {
2160 loc = (*loc2p);
2161 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2162 {
2163 if (modified)
2164 {
2165 /* Re-parse the conditions since something changed. In that
2166 case we already freed the condition bytecodes (see
2167 force_breakpoint_reinsertion). We just
2168 need to parse the condition to bytecodes again. */
2169 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2170 loc->cond.get ());
2171 }
2172
2173 /* If we have a NULL bytecode expression, it means something
2174 went wrong or we have a null condition expression. */
2175 if (!loc->cond_bytecode)
2176 {
2177 null_condition_or_parse_error = 1;
2178 break;
2179 }
2180 }
2181 }
2182
2183 /* If any of these happened, it means we will have to evaluate the conditions
2184 for the location's address on gdb's side. It is no use keeping bytecodes
2185 for all the other duplicate locations, thus we free all of them here.
2186
2187 This is so we have a finer control over which locations' conditions are
2188 being evaluated by GDB or the remote stub. */
2189 if (null_condition_or_parse_error)
2190 {
2191 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2192 {
2193 loc = (*loc2p);
2194 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2195 {
2196 /* Only go as far as the first NULL bytecode is
2197 located. */
2198 if (!loc->cond_bytecode)
2199 return;
2200
2201 loc->cond_bytecode.reset ();
2202 }
2203 }
2204 }
2205
2206 /* No NULL conditions or failed bytecode generation. Build a
2207 condition list for this location's address. If we have software
2208 and hardware locations at the same address, they aren't
2209 considered duplicates, but we still marge all the conditions
2210 anyway, as it's simpler, and doesn't really make a practical
2211 difference. */
2212 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2213 {
2214 loc = (*loc2p);
2215 if (loc->cond
2216 && is_breakpoint (loc->owner)
2217 && loc->pspace->num == bl->pspace->num
2218 && loc->owner->enable_state == bp_enabled
2219 && loc->enabled)
2220 {
2221 /* Add the condition to the vector. This will be used later
2222 to send the conditions to the target. */
2223 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2224 }
2225 }
2226
2227 return;
2228 }
2229
2230 /* Parses a command described by string CMD into an agent expression
2231 bytecode suitable for evaluation by the bytecode interpreter.
2232 Return NULL if there was any error during parsing. */
2233
2234 static agent_expr_up
2235 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2236 {
2237 const char *cmdrest;
2238 const char *format_start, *format_end;
2239 struct gdbarch *gdbarch = get_current_arch ();
2240
2241 if (cmd == NULL)
2242 return NULL;
2243
2244 cmdrest = cmd;
2245
2246 if (*cmdrest == ',')
2247 ++cmdrest;
2248 cmdrest = skip_spaces (cmdrest);
2249
2250 if (*cmdrest++ != '"')
2251 error (_("No format string following the location"));
2252
2253 format_start = cmdrest;
2254
2255 format_pieces fpieces (&cmdrest);
2256
2257 format_end = cmdrest;
2258
2259 if (*cmdrest++ != '"')
2260 error (_("Bad format string, non-terminated '\"'."));
2261
2262 cmdrest = skip_spaces (cmdrest);
2263
2264 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2265 error (_("Invalid argument syntax"));
2266
2267 if (*cmdrest == ',')
2268 cmdrest++;
2269 cmdrest = skip_spaces (cmdrest);
2270
2271 /* For each argument, make an expression. */
2272
2273 std::vector<struct expression *> argvec;
2274 while (*cmdrest != '\0')
2275 {
2276 const char *cmd1;
2277
2278 cmd1 = cmdrest;
2279 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2280 argvec.push_back (expr.release ());
2281 cmdrest = cmd1;
2282 if (*cmdrest == ',')
2283 ++cmdrest;
2284 }
2285
2286 agent_expr_up aexpr;
2287
2288 /* We don't want to stop processing, so catch any errors
2289 that may show up. */
2290 try
2291 {
2292 aexpr = gen_printf (scope, gdbarch, 0, 0,
2293 format_start, format_end - format_start,
2294 argvec.size (), argvec.data ());
2295 }
2296 catch (const gdb_exception_error &ex)
2297 {
2298 /* If we got here, it means the command could not be parsed to a valid
2299 bytecode expression and thus can't be evaluated on the target's side.
2300 It's no use iterating through the other commands. */
2301 }
2302
2303 /* We have a valid agent expression, return it. */
2304 return aexpr;
2305 }
2306
2307 /* Based on location BL, create a list of breakpoint commands to be
2308 passed on to the target. If we have duplicated locations with
2309 different commands, we will add any such to the list. */
2310
2311 static void
2312 build_target_command_list (struct bp_location *bl)
2313 {
2314 struct bp_location **locp = NULL, **loc2p;
2315 int null_command_or_parse_error = 0;
2316 int modified = bl->needs_update;
2317 struct bp_location *loc;
2318
2319 /* Clear commands left over from a previous insert. */
2320 bl->target_info.tcommands.clear ();
2321
2322 if (!target_can_run_breakpoint_commands ())
2323 return;
2324
2325 /* For now, limit to agent-style dprintf breakpoints. */
2326 if (dprintf_style != dprintf_style_agent)
2327 return;
2328
2329 /* For now, if we have any location at the same address that isn't a
2330 dprintf, don't install the target-side commands, as that would
2331 make the breakpoint not be reported to the core, and we'd lose
2332 control. */
2333 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2334 {
2335 loc = (*loc2p);
2336 if (is_breakpoint (loc->owner)
2337 && loc->pspace->num == bl->pspace->num
2338 && loc->owner->type != bp_dprintf)
2339 return;
2340 }
2341
2342 /* Do a first pass to check for locations with no assigned
2343 conditions or conditions that fail to parse to a valid agent expression
2344 bytecode. If any of these happen, then it's no use to send conditions
2345 to the target since this location will always trigger and generate a
2346 response back to GDB. */
2347 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2348 {
2349 loc = (*loc2p);
2350 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2351 {
2352 if (modified)
2353 {
2354 /* Re-parse the commands since something changed. In that
2355 case we already freed the command bytecodes (see
2356 force_breakpoint_reinsertion). We just
2357 need to parse the command to bytecodes again. */
2358 loc->cmd_bytecode
2359 = parse_cmd_to_aexpr (bl->address,
2360 loc->owner->extra_string);
2361 }
2362
2363 /* If we have a NULL bytecode expression, it means something
2364 went wrong or we have a null command expression. */
2365 if (!loc->cmd_bytecode)
2366 {
2367 null_command_or_parse_error = 1;
2368 break;
2369 }
2370 }
2371 }
2372
2373 /* If anything failed, then we're not doing target-side commands,
2374 and so clean up. */
2375 if (null_command_or_parse_error)
2376 {
2377 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2378 {
2379 loc = (*loc2p);
2380 if (is_breakpoint (loc->owner)
2381 && loc->pspace->num == bl->pspace->num)
2382 {
2383 /* Only go as far as the first NULL bytecode is
2384 located. */
2385 if (loc->cmd_bytecode == NULL)
2386 return;
2387
2388 loc->cmd_bytecode.reset ();
2389 }
2390 }
2391 }
2392
2393 /* No NULL commands or failed bytecode generation. Build a command
2394 list for all duplicate locations at this location's address.
2395 Note that here we must care for whether the breakpoint location
2396 types are considered duplicates, otherwise, say, if we have a
2397 software and hardware location at the same address, the target
2398 could end up running the commands twice. For the moment, we only
2399 support targets-side commands with dprintf, but it doesn't hurt
2400 to be pedantically correct in case that changes. */
2401 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2402 {
2403 loc = (*loc2p);
2404 if (breakpoint_locations_match (bl, loc)
2405 && loc->owner->extra_string
2406 && is_breakpoint (loc->owner)
2407 && loc->pspace->num == bl->pspace->num
2408 && loc->owner->enable_state == bp_enabled
2409 && loc->enabled)
2410 {
2411 /* Add the command to the vector. This will be used later
2412 to send the commands to the target. */
2413 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2414 }
2415 }
2416
2417 bl->target_info.persist = 0;
2418 /* Maybe flag this location as persistent. */
2419 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2420 bl->target_info.persist = 1;
2421 }
2422
2423 /* Return the kind of breakpoint on address *ADDR. Get the kind
2424 of breakpoint according to ADDR except single-step breakpoint.
2425 Get the kind of single-step breakpoint according to the current
2426 registers state. */
2427
2428 static int
2429 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2430 {
2431 if (bl->owner->type == bp_single_step)
2432 {
2433 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2434 struct regcache *regcache;
2435
2436 regcache = get_thread_regcache (thr);
2437
2438 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2439 regcache, addr);
2440 }
2441 else
2442 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2443 }
2444
2445 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2446 location. Any error messages are printed to TMP_ERROR_STREAM; and
2447 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2448 Returns 0 for success, 1 if the bp_location type is not supported or
2449 -1 for failure.
2450
2451 NOTE drow/2003-09-09: This routine could be broken down to an
2452 object-style method for each breakpoint or catchpoint type. */
2453 static int
2454 insert_bp_location (struct bp_location *bl,
2455 struct ui_file *tmp_error_stream,
2456 int *disabled_breaks,
2457 int *hw_breakpoint_error,
2458 int *hw_bp_error_explained_already)
2459 {
2460 gdb_exception bp_excpt;
2461
2462 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2463 return 0;
2464
2465 /* Note we don't initialize bl->target_info, as that wipes out
2466 the breakpoint location's shadow_contents if the breakpoint
2467 is still inserted at that location. This in turn breaks
2468 target_read_memory which depends on these buffers when
2469 a memory read is requested at the breakpoint location:
2470 Once the target_info has been wiped, we fail to see that
2471 we have a breakpoint inserted at that address and thus
2472 read the breakpoint instead of returning the data saved in
2473 the breakpoint location's shadow contents. */
2474 bl->target_info.reqstd_address = bl->address;
2475 bl->target_info.placed_address_space = bl->pspace->aspace;
2476 bl->target_info.length = bl->length;
2477
2478 /* When working with target-side conditions, we must pass all the conditions
2479 for the same breakpoint address down to the target since GDB will not
2480 insert those locations. With a list of breakpoint conditions, the target
2481 can decide when to stop and notify GDB. */
2482
2483 if (is_breakpoint (bl->owner))
2484 {
2485 build_target_condition_list (bl);
2486 build_target_command_list (bl);
2487 /* Reset the modification marker. */
2488 bl->needs_update = 0;
2489 }
2490
2491 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2492 set at a read-only address, then a breakpoint location will have
2493 been changed to hardware breakpoint before we get here. If it is
2494 "off" however, error out before actually trying to insert the
2495 breakpoint, with a nicer error message. */
2496 if (bl->loc_type == bp_loc_software_breakpoint
2497 && !automatic_hardware_breakpoints)
2498 {
2499 mem_region *mr = lookup_mem_region (bl->address);
2500
2501 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2502 {
2503 fprintf_unfiltered (tmp_error_stream,
2504 _("Cannot insert breakpoint %d.\n"
2505 "Cannot set software breakpoint "
2506 "at read-only address %s\n"),
2507 bl->owner->number,
2508 paddress (bl->gdbarch, bl->address));
2509 return 1;
2510 }
2511 }
2512
2513 if (bl->loc_type == bp_loc_software_breakpoint
2514 || bl->loc_type == bp_loc_hardware_breakpoint)
2515 {
2516 /* First check to see if we have to handle an overlay. */
2517 if (overlay_debugging == ovly_off
2518 || bl->section == NULL
2519 || !(section_is_overlay (bl->section)))
2520 {
2521 /* No overlay handling: just set the breakpoint. */
2522 try
2523 {
2524 int val;
2525
2526 val = bl->owner->ops->insert_location (bl);
2527 if (val)
2528 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2529 }
2530 catch (gdb_exception &e)
2531 {
2532 bp_excpt = std::move (e);
2533 }
2534 }
2535 else
2536 {
2537 /* This breakpoint is in an overlay section.
2538 Shall we set a breakpoint at the LMA? */
2539 if (!overlay_events_enabled)
2540 {
2541 /* Yes -- overlay event support is not active,
2542 so we must try to set a breakpoint at the LMA.
2543 This will not work for a hardware breakpoint. */
2544 if (bl->loc_type == bp_loc_hardware_breakpoint)
2545 warning (_("hardware breakpoint %d not supported in overlay!"),
2546 bl->owner->number);
2547 else
2548 {
2549 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2550 bl->section);
2551 /* Set a software (trap) breakpoint at the LMA. */
2552 bl->overlay_target_info = bl->target_info;
2553 bl->overlay_target_info.reqstd_address = addr;
2554
2555 /* No overlay handling: just set the breakpoint. */
2556 try
2557 {
2558 int val;
2559
2560 bl->overlay_target_info.kind
2561 = breakpoint_kind (bl, &addr);
2562 bl->overlay_target_info.placed_address = addr;
2563 val = target_insert_breakpoint (bl->gdbarch,
2564 &bl->overlay_target_info);
2565 if (val)
2566 bp_excpt
2567 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2568 }
2569 catch (gdb_exception &e)
2570 {
2571 bp_excpt = std::move (e);
2572 }
2573
2574 if (bp_excpt.reason != 0)
2575 fprintf_unfiltered (tmp_error_stream,
2576 "Overlay breakpoint %d "
2577 "failed: in ROM?\n",
2578 bl->owner->number);
2579 }
2580 }
2581 /* Shall we set a breakpoint at the VMA? */
2582 if (section_is_mapped (bl->section))
2583 {
2584 /* Yes. This overlay section is mapped into memory. */
2585 try
2586 {
2587 int val;
2588
2589 val = bl->owner->ops->insert_location (bl);
2590 if (val)
2591 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2592 }
2593 catch (gdb_exception &e)
2594 {
2595 bp_excpt = std::move (e);
2596 }
2597 }
2598 else
2599 {
2600 /* No. This breakpoint will not be inserted.
2601 No error, but do not mark the bp as 'inserted'. */
2602 return 0;
2603 }
2604 }
2605
2606 if (bp_excpt.reason != 0)
2607 {
2608 /* Can't set the breakpoint. */
2609
2610 /* In some cases, we might not be able to insert a
2611 breakpoint in a shared library that has already been
2612 removed, but we have not yet processed the shlib unload
2613 event. Unfortunately, some targets that implement
2614 breakpoint insertion themselves can't tell why the
2615 breakpoint insertion failed (e.g., the remote target
2616 doesn't define error codes), so we must treat generic
2617 errors as memory errors. */
2618 if (bp_excpt.reason == RETURN_ERROR
2619 && (bp_excpt.error == GENERIC_ERROR
2620 || bp_excpt.error == MEMORY_ERROR)
2621 && bl->loc_type == bp_loc_software_breakpoint
2622 && (solib_name_from_address (bl->pspace, bl->address)
2623 || shared_objfile_contains_address_p (bl->pspace,
2624 bl->address)))
2625 {
2626 /* See also: disable_breakpoints_in_shlibs. */
2627 bl->shlib_disabled = 1;
2628 gdb::observers::breakpoint_modified.notify (bl->owner);
2629 if (!*disabled_breaks)
2630 {
2631 fprintf_unfiltered (tmp_error_stream,
2632 "Cannot insert breakpoint %d.\n",
2633 bl->owner->number);
2634 fprintf_unfiltered (tmp_error_stream,
2635 "Temporarily disabling shared "
2636 "library breakpoints:\n");
2637 }
2638 *disabled_breaks = 1;
2639 fprintf_unfiltered (tmp_error_stream,
2640 "breakpoint #%d\n", bl->owner->number);
2641 return 0;
2642 }
2643 else
2644 {
2645 if (bl->loc_type == bp_loc_hardware_breakpoint)
2646 {
2647 *hw_breakpoint_error = 1;
2648 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2649 fprintf_unfiltered (tmp_error_stream,
2650 "Cannot insert hardware breakpoint %d%s",
2651 bl->owner->number,
2652 bp_excpt.message ? ":" : ".\n");
2653 if (bp_excpt.message != NULL)
2654 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2655 bp_excpt.what ());
2656 }
2657 else
2658 {
2659 if (bp_excpt.message == NULL)
2660 {
2661 std::string message
2662 = memory_error_message (TARGET_XFER_E_IO,
2663 bl->gdbarch, bl->address);
2664
2665 fprintf_unfiltered (tmp_error_stream,
2666 "Cannot insert breakpoint %d.\n"
2667 "%s\n",
2668 bl->owner->number, message.c_str ());
2669 }
2670 else
2671 {
2672 fprintf_unfiltered (tmp_error_stream,
2673 "Cannot insert breakpoint %d: %s\n",
2674 bl->owner->number,
2675 bp_excpt.what ());
2676 }
2677 }
2678 return 1;
2679
2680 }
2681 }
2682 else
2683 bl->inserted = 1;
2684
2685 return 0;
2686 }
2687
2688 else if (bl->loc_type == bp_loc_hardware_watchpoint
2689 /* NOTE drow/2003-09-08: This state only exists for removing
2690 watchpoints. It's not clear that it's necessary... */
2691 && bl->owner->disposition != disp_del_at_next_stop)
2692 {
2693 int val;
2694
2695 gdb_assert (bl->owner->ops != NULL
2696 && bl->owner->ops->insert_location != NULL);
2697
2698 val = bl->owner->ops->insert_location (bl);
2699
2700 /* If trying to set a read-watchpoint, and it turns out it's not
2701 supported, try emulating one with an access watchpoint. */
2702 if (val == 1 && bl->watchpoint_type == hw_read)
2703 {
2704 struct bp_location *loc, **loc_temp;
2705
2706 /* But don't try to insert it, if there's already another
2707 hw_access location that would be considered a duplicate
2708 of this one. */
2709 ALL_BP_LOCATIONS (loc, loc_temp)
2710 if (loc != bl
2711 && loc->watchpoint_type == hw_access
2712 && watchpoint_locations_match (bl, loc))
2713 {
2714 bl->duplicate = 1;
2715 bl->inserted = 1;
2716 bl->target_info = loc->target_info;
2717 bl->watchpoint_type = hw_access;
2718 val = 0;
2719 break;
2720 }
2721
2722 if (val == 1)
2723 {
2724 bl->watchpoint_type = hw_access;
2725 val = bl->owner->ops->insert_location (bl);
2726
2727 if (val)
2728 /* Back to the original value. */
2729 bl->watchpoint_type = hw_read;
2730 }
2731 }
2732
2733 bl->inserted = (val == 0);
2734 }
2735
2736 else if (bl->owner->type == bp_catchpoint)
2737 {
2738 int val;
2739
2740 gdb_assert (bl->owner->ops != NULL
2741 && bl->owner->ops->insert_location != NULL);
2742
2743 val = bl->owner->ops->insert_location (bl);
2744 if (val)
2745 {
2746 bl->owner->enable_state = bp_disabled;
2747
2748 if (val == 1)
2749 warning (_("\
2750 Error inserting catchpoint %d: Your system does not support this type\n\
2751 of catchpoint."), bl->owner->number);
2752 else
2753 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2754 }
2755
2756 bl->inserted = (val == 0);
2757
2758 /* We've already printed an error message if there was a problem
2759 inserting this catchpoint, and we've disabled the catchpoint,
2760 so just return success. */
2761 return 0;
2762 }
2763
2764 return 0;
2765 }
2766
2767 /* This function is called when program space PSPACE is about to be
2768 deleted. It takes care of updating breakpoints to not reference
2769 PSPACE anymore. */
2770
2771 void
2772 breakpoint_program_space_exit (struct program_space *pspace)
2773 {
2774 struct breakpoint *b, *b_temp;
2775 struct bp_location *loc, **loc_temp;
2776
2777 /* Remove any breakpoint that was set through this program space. */
2778 ALL_BREAKPOINTS_SAFE (b, b_temp)
2779 {
2780 if (b->pspace == pspace)
2781 delete_breakpoint (b);
2782 }
2783
2784 /* Breakpoints set through other program spaces could have locations
2785 bound to PSPACE as well. Remove those. */
2786 ALL_BP_LOCATIONS (loc, loc_temp)
2787 {
2788 struct bp_location *tmp;
2789
2790 if (loc->pspace == pspace)
2791 {
2792 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2793 if (loc->owner->loc == loc)
2794 loc->owner->loc = loc->next;
2795 else
2796 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2797 if (tmp->next == loc)
2798 {
2799 tmp->next = loc->next;
2800 break;
2801 }
2802 }
2803 }
2804
2805 /* Now update the global location list to permanently delete the
2806 removed locations above. */
2807 update_global_location_list (UGLL_DONT_INSERT);
2808 }
2809
2810 /* Make sure all breakpoints are inserted in inferior.
2811 Throws exception on any error.
2812 A breakpoint that is already inserted won't be inserted
2813 again, so calling this function twice is safe. */
2814 void
2815 insert_breakpoints (void)
2816 {
2817 struct breakpoint *bpt;
2818
2819 ALL_BREAKPOINTS (bpt)
2820 if (is_hardware_watchpoint (bpt))
2821 {
2822 struct watchpoint *w = (struct watchpoint *) bpt;
2823
2824 update_watchpoint (w, 0 /* don't reparse. */);
2825 }
2826
2827 /* Updating watchpoints creates new locations, so update the global
2828 location list. Explicitly tell ugll to insert locations and
2829 ignore breakpoints_always_inserted_mode. Also,
2830 update_global_location_list tries to "upgrade" software
2831 breakpoints to hardware breakpoints to handle "set breakpoint
2832 auto-hw", so we need to call it even if we don't have new
2833 locations. */
2834 update_global_location_list (UGLL_INSERT);
2835 }
2836
2837 /* Invoke CALLBACK for each of bp_location. */
2838
2839 void
2840 iterate_over_bp_locations (walk_bp_location_callback callback)
2841 {
2842 struct bp_location *loc, **loc_tmp;
2843
2844 ALL_BP_LOCATIONS (loc, loc_tmp)
2845 {
2846 callback (loc, NULL);
2847 }
2848 }
2849
2850 /* This is used when we need to synch breakpoint conditions between GDB and the
2851 target. It is the case with deleting and disabling of breakpoints when using
2852 always-inserted mode. */
2853
2854 static void
2855 update_inserted_breakpoint_locations (void)
2856 {
2857 struct bp_location *bl, **blp_tmp;
2858 int error_flag = 0;
2859 int val = 0;
2860 int disabled_breaks = 0;
2861 int hw_breakpoint_error = 0;
2862 int hw_bp_details_reported = 0;
2863
2864 string_file tmp_error_stream;
2865
2866 /* Explicitly mark the warning -- this will only be printed if
2867 there was an error. */
2868 tmp_error_stream.puts ("Warning:\n");
2869
2870 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2871
2872 ALL_BP_LOCATIONS (bl, blp_tmp)
2873 {
2874 /* We only want to update software breakpoints and hardware
2875 breakpoints. */
2876 if (!is_breakpoint (bl->owner))
2877 continue;
2878
2879 /* We only want to update locations that are already inserted
2880 and need updating. This is to avoid unwanted insertion during
2881 deletion of breakpoints. */
2882 if (!bl->inserted || !bl->needs_update)
2883 continue;
2884
2885 switch_to_program_space_and_thread (bl->pspace);
2886
2887 /* For targets that support global breakpoints, there's no need
2888 to select an inferior to insert breakpoint to. In fact, even
2889 if we aren't attached to any process yet, we should still
2890 insert breakpoints. */
2891 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2892 && (inferior_ptid == null_ptid || !target_has_execution))
2893 continue;
2894
2895 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2896 &hw_breakpoint_error, &hw_bp_details_reported);
2897 if (val)
2898 error_flag = val;
2899 }
2900
2901 if (error_flag)
2902 {
2903 target_terminal::ours_for_output ();
2904 error_stream (tmp_error_stream);
2905 }
2906 }
2907
2908 /* Used when starting or continuing the program. */
2909
2910 static void
2911 insert_breakpoint_locations (void)
2912 {
2913 struct breakpoint *bpt;
2914 struct bp_location *bl, **blp_tmp;
2915 int error_flag = 0;
2916 int val = 0;
2917 int disabled_breaks = 0;
2918 int hw_breakpoint_error = 0;
2919 int hw_bp_error_explained_already = 0;
2920
2921 string_file tmp_error_stream;
2922
2923 /* Explicitly mark the warning -- this will only be printed if
2924 there was an error. */
2925 tmp_error_stream.puts ("Warning:\n");
2926
2927 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2928
2929 ALL_BP_LOCATIONS (bl, blp_tmp)
2930 {
2931 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2932 continue;
2933
2934 /* There is no point inserting thread-specific breakpoints if
2935 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2936 has BL->OWNER always non-NULL. */
2937 if (bl->owner->thread != -1
2938 && !valid_global_thread_id (bl->owner->thread))
2939 continue;
2940
2941 switch_to_program_space_and_thread (bl->pspace);
2942
2943 /* For targets that support global breakpoints, there's no need
2944 to select an inferior to insert breakpoint to. In fact, even
2945 if we aren't attached to any process yet, we should still
2946 insert breakpoints. */
2947 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2948 && (inferior_ptid == null_ptid || !target_has_execution))
2949 continue;
2950
2951 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2952 &hw_breakpoint_error, &hw_bp_error_explained_already);
2953 if (val)
2954 error_flag = val;
2955 }
2956
2957 /* If we failed to insert all locations of a watchpoint, remove
2958 them, as half-inserted watchpoint is of limited use. */
2959 ALL_BREAKPOINTS (bpt)
2960 {
2961 int some_failed = 0;
2962 struct bp_location *loc;
2963
2964 if (!is_hardware_watchpoint (bpt))
2965 continue;
2966
2967 if (!breakpoint_enabled (bpt))
2968 continue;
2969
2970 if (bpt->disposition == disp_del_at_next_stop)
2971 continue;
2972
2973 for (loc = bpt->loc; loc; loc = loc->next)
2974 if (!loc->inserted && should_be_inserted (loc))
2975 {
2976 some_failed = 1;
2977 break;
2978 }
2979 if (some_failed)
2980 {
2981 for (loc = bpt->loc; loc; loc = loc->next)
2982 if (loc->inserted)
2983 remove_breakpoint (loc);
2984
2985 hw_breakpoint_error = 1;
2986 tmp_error_stream.printf ("Could not insert "
2987 "hardware watchpoint %d.\n",
2988 bpt->number);
2989 error_flag = -1;
2990 }
2991 }
2992
2993 if (error_flag)
2994 {
2995 /* If a hardware breakpoint or watchpoint was inserted, add a
2996 message about possibly exhausted resources. */
2997 if (hw_breakpoint_error && !hw_bp_error_explained_already)
2998 {
2999 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3000 You may have requested too many hardware breakpoints/watchpoints.\n");
3001 }
3002 target_terminal::ours_for_output ();
3003 error_stream (tmp_error_stream);
3004 }
3005 }
3006
3007 /* Used when the program stops.
3008 Returns zero if successful, or non-zero if there was a problem
3009 removing a breakpoint location. */
3010
3011 int
3012 remove_breakpoints (void)
3013 {
3014 struct bp_location *bl, **blp_tmp;
3015 int val = 0;
3016
3017 ALL_BP_LOCATIONS (bl, blp_tmp)
3018 {
3019 if (bl->inserted && !is_tracepoint (bl->owner))
3020 val |= remove_breakpoint (bl);
3021 }
3022 return val;
3023 }
3024
3025 /* When a thread exits, remove breakpoints that are related to
3026 that thread. */
3027
3028 static void
3029 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3030 {
3031 struct breakpoint *b, *b_tmp;
3032
3033 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3034 {
3035 if (b->thread == tp->global_num && user_breakpoint_p (b))
3036 {
3037 b->disposition = disp_del_at_next_stop;
3038
3039 printf_filtered (_("\
3040 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3041 b->number, print_thread_id (tp));
3042
3043 /* Hide it from the user. */
3044 b->number = 0;
3045 }
3046 }
3047 }
3048
3049 /* See breakpoint.h. */
3050
3051 void
3052 remove_breakpoints_inf (inferior *inf)
3053 {
3054 struct bp_location *bl, **blp_tmp;
3055 int val;
3056
3057 ALL_BP_LOCATIONS (bl, blp_tmp)
3058 {
3059 if (bl->pspace != inf->pspace)
3060 continue;
3061
3062 if (bl->inserted && !bl->target_info.persist)
3063 {
3064 val = remove_breakpoint (bl);
3065 if (val != 0)
3066 return;
3067 }
3068 }
3069 }
3070
3071 static int internal_breakpoint_number = -1;
3072
3073 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3074 If INTERNAL is non-zero, the breakpoint number will be populated
3075 from internal_breakpoint_number and that variable decremented.
3076 Otherwise the breakpoint number will be populated from
3077 breakpoint_count and that value incremented. Internal breakpoints
3078 do not set the internal var bpnum. */
3079 static void
3080 set_breakpoint_number (int internal, struct breakpoint *b)
3081 {
3082 if (internal)
3083 b->number = internal_breakpoint_number--;
3084 else
3085 {
3086 set_breakpoint_count (breakpoint_count + 1);
3087 b->number = breakpoint_count;
3088 }
3089 }
3090
3091 static struct breakpoint *
3092 create_internal_breakpoint (struct gdbarch *gdbarch,
3093 CORE_ADDR address, enum bptype type,
3094 const struct breakpoint_ops *ops)
3095 {
3096 symtab_and_line sal;
3097 sal.pc = address;
3098 sal.section = find_pc_overlay (sal.pc);
3099 sal.pspace = current_program_space;
3100
3101 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3102 b->number = internal_breakpoint_number--;
3103 b->disposition = disp_donttouch;
3104
3105 return b;
3106 }
3107
3108 static const char *const longjmp_names[] =
3109 {
3110 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3111 };
3112 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3113
3114 /* Per-objfile data private to breakpoint.c. */
3115 struct breakpoint_objfile_data
3116 {
3117 /* Minimal symbol for "_ovly_debug_event" (if any). */
3118 struct bound_minimal_symbol overlay_msym {};
3119
3120 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3121 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3122
3123 /* True if we have looked for longjmp probes. */
3124 int longjmp_searched = 0;
3125
3126 /* SystemTap probe points for longjmp (if any). These are non-owning
3127 references. */
3128 std::vector<probe *> longjmp_probes;
3129
3130 /* Minimal symbol for "std::terminate()" (if any). */
3131 struct bound_minimal_symbol terminate_msym {};
3132
3133 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3134 struct bound_minimal_symbol exception_msym {};
3135
3136 /* True if we have looked for exception probes. */
3137 int exception_searched = 0;
3138
3139 /* SystemTap probe points for unwinding (if any). These are non-owning
3140 references. */
3141 std::vector<probe *> exception_probes;
3142 };
3143
3144 static const struct objfile_key<breakpoint_objfile_data>
3145 breakpoint_objfile_key;
3146
3147 /* Minimal symbol not found sentinel. */
3148 static struct minimal_symbol msym_not_found;
3149
3150 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3151
3152 static int
3153 msym_not_found_p (const struct minimal_symbol *msym)
3154 {
3155 return msym == &msym_not_found;
3156 }
3157
3158 /* Return per-objfile data needed by breakpoint.c.
3159 Allocate the data if necessary. */
3160
3161 static struct breakpoint_objfile_data *
3162 get_breakpoint_objfile_data (struct objfile *objfile)
3163 {
3164 struct breakpoint_objfile_data *bp_objfile_data;
3165
3166 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3167 if (bp_objfile_data == NULL)
3168 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3169 return bp_objfile_data;
3170 }
3171
3172 static void
3173 create_overlay_event_breakpoint (void)
3174 {
3175 const char *const func_name = "_ovly_debug_event";
3176
3177 for (objfile *objfile : current_program_space->objfiles ())
3178 {
3179 struct breakpoint *b;
3180 struct breakpoint_objfile_data *bp_objfile_data;
3181 CORE_ADDR addr;
3182 struct explicit_location explicit_loc;
3183
3184 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3185
3186 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3187 continue;
3188
3189 if (bp_objfile_data->overlay_msym.minsym == NULL)
3190 {
3191 struct bound_minimal_symbol m;
3192
3193 m = lookup_minimal_symbol_text (func_name, objfile);
3194 if (m.minsym == NULL)
3195 {
3196 /* Avoid future lookups in this objfile. */
3197 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3198 continue;
3199 }
3200 bp_objfile_data->overlay_msym = m;
3201 }
3202
3203 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3204 b = create_internal_breakpoint (objfile->arch (), addr,
3205 bp_overlay_event,
3206 &internal_breakpoint_ops);
3207 initialize_explicit_location (&explicit_loc);
3208 explicit_loc.function_name = ASTRDUP (func_name);
3209 b->location = new_explicit_location (&explicit_loc);
3210
3211 if (overlay_debugging == ovly_auto)
3212 {
3213 b->enable_state = bp_enabled;
3214 overlay_events_enabled = 1;
3215 }
3216 else
3217 {
3218 b->enable_state = bp_disabled;
3219 overlay_events_enabled = 0;
3220 }
3221 }
3222 }
3223
3224 static void
3225 create_longjmp_master_breakpoint (void)
3226 {
3227 scoped_restore_current_program_space restore_pspace;
3228
3229 for (struct program_space *pspace : program_spaces)
3230 {
3231 set_current_program_space (pspace);
3232
3233 for (objfile *objfile : current_program_space->objfiles ())
3234 {
3235 int i;
3236 struct gdbarch *gdbarch;
3237 struct breakpoint_objfile_data *bp_objfile_data;
3238
3239 gdbarch = objfile->arch ();
3240
3241 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3242
3243 if (!bp_objfile_data->longjmp_searched)
3244 {
3245 std::vector<probe *> ret
3246 = find_probes_in_objfile (objfile, "libc", "longjmp");
3247
3248 if (!ret.empty ())
3249 {
3250 /* We are only interested in checking one element. */
3251 probe *p = ret[0];
3252
3253 if (!p->can_evaluate_arguments ())
3254 {
3255 /* We cannot use the probe interface here,
3256 because it does not know how to evaluate
3257 arguments. */
3258 ret.clear ();
3259 }
3260 }
3261 bp_objfile_data->longjmp_probes = ret;
3262 bp_objfile_data->longjmp_searched = 1;
3263 }
3264
3265 if (!bp_objfile_data->longjmp_probes.empty ())
3266 {
3267 for (probe *p : bp_objfile_data->longjmp_probes)
3268 {
3269 struct breakpoint *b;
3270
3271 b = create_internal_breakpoint (gdbarch,
3272 p->get_relocated_address (objfile),
3273 bp_longjmp_master,
3274 &internal_breakpoint_ops);
3275 b->location = new_probe_location ("-probe-stap libc:longjmp");
3276 b->enable_state = bp_disabled;
3277 }
3278
3279 continue;
3280 }
3281
3282 if (!gdbarch_get_longjmp_target_p (gdbarch))
3283 continue;
3284
3285 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3286 {
3287 struct breakpoint *b;
3288 const char *func_name;
3289 CORE_ADDR addr;
3290 struct explicit_location explicit_loc;
3291
3292 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3293 continue;
3294
3295 func_name = longjmp_names[i];
3296 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3297 {
3298 struct bound_minimal_symbol m;
3299
3300 m = lookup_minimal_symbol_text (func_name, objfile);
3301 if (m.minsym == NULL)
3302 {
3303 /* Prevent future lookups in this objfile. */
3304 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3305 continue;
3306 }
3307 bp_objfile_data->longjmp_msym[i] = m;
3308 }
3309
3310 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3311 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3312 &internal_breakpoint_ops);
3313 initialize_explicit_location (&explicit_loc);
3314 explicit_loc.function_name = ASTRDUP (func_name);
3315 b->location = new_explicit_location (&explicit_loc);
3316 b->enable_state = bp_disabled;
3317 }
3318 }
3319 }
3320 }
3321
3322 /* Create a master std::terminate breakpoint. */
3323 static void
3324 create_std_terminate_master_breakpoint (void)
3325 {
3326 const char *const func_name = "std::terminate()";
3327
3328 scoped_restore_current_program_space restore_pspace;
3329
3330 for (struct program_space *pspace : program_spaces)
3331 {
3332 CORE_ADDR addr;
3333
3334 set_current_program_space (pspace);
3335
3336 for (objfile *objfile : current_program_space->objfiles ())
3337 {
3338 struct breakpoint *b;
3339 struct breakpoint_objfile_data *bp_objfile_data;
3340 struct explicit_location explicit_loc;
3341
3342 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3343
3344 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3345 continue;
3346
3347 if (bp_objfile_data->terminate_msym.minsym == NULL)
3348 {
3349 struct bound_minimal_symbol m;
3350
3351 m = lookup_minimal_symbol (func_name, NULL, objfile);
3352 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3353 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3354 {
3355 /* Prevent future lookups in this objfile. */
3356 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3357 continue;
3358 }
3359 bp_objfile_data->terminate_msym = m;
3360 }
3361
3362 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3363 b = create_internal_breakpoint (objfile->arch (), addr,
3364 bp_std_terminate_master,
3365 &internal_breakpoint_ops);
3366 initialize_explicit_location (&explicit_loc);
3367 explicit_loc.function_name = ASTRDUP (func_name);
3368 b->location = new_explicit_location (&explicit_loc);
3369 b->enable_state = bp_disabled;
3370 }
3371 }
3372 }
3373
3374 /* Install a master breakpoint on the unwinder's debug hook. */
3375
3376 static void
3377 create_exception_master_breakpoint (void)
3378 {
3379 const char *const func_name = "_Unwind_DebugHook";
3380
3381 for (objfile *objfile : current_program_space->objfiles ())
3382 {
3383 struct breakpoint *b;
3384 struct gdbarch *gdbarch;
3385 struct breakpoint_objfile_data *bp_objfile_data;
3386 CORE_ADDR addr;
3387 struct explicit_location explicit_loc;
3388
3389 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3390
3391 /* We prefer the SystemTap probe point if it exists. */
3392 if (!bp_objfile_data->exception_searched)
3393 {
3394 std::vector<probe *> ret
3395 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3396
3397 if (!ret.empty ())
3398 {
3399 /* We are only interested in checking one element. */
3400 probe *p = ret[0];
3401
3402 if (!p->can_evaluate_arguments ())
3403 {
3404 /* We cannot use the probe interface here, because it does
3405 not know how to evaluate arguments. */
3406 ret.clear ();
3407 }
3408 }
3409 bp_objfile_data->exception_probes = ret;
3410 bp_objfile_data->exception_searched = 1;
3411 }
3412
3413 if (!bp_objfile_data->exception_probes.empty ())
3414 {
3415 gdbarch = objfile->arch ();
3416
3417 for (probe *p : bp_objfile_data->exception_probes)
3418 {
3419 b = create_internal_breakpoint (gdbarch,
3420 p->get_relocated_address (objfile),
3421 bp_exception_master,
3422 &internal_breakpoint_ops);
3423 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3424 b->enable_state = bp_disabled;
3425 }
3426
3427 continue;
3428 }
3429
3430 /* Otherwise, try the hook function. */
3431
3432 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3433 continue;
3434
3435 gdbarch = objfile->arch ();
3436
3437 if (bp_objfile_data->exception_msym.minsym == NULL)
3438 {
3439 struct bound_minimal_symbol debug_hook;
3440
3441 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3442 if (debug_hook.minsym == NULL)
3443 {
3444 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3445 continue;
3446 }
3447
3448 bp_objfile_data->exception_msym = debug_hook;
3449 }
3450
3451 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3452 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3453 current_top_target ());
3454 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3455 &internal_breakpoint_ops);
3456 initialize_explicit_location (&explicit_loc);
3457 explicit_loc.function_name = ASTRDUP (func_name);
3458 b->location = new_explicit_location (&explicit_loc);
3459 b->enable_state = bp_disabled;
3460 }
3461 }
3462
3463 /* Does B have a location spec? */
3464
3465 static int
3466 breakpoint_event_location_empty_p (const struct breakpoint *b)
3467 {
3468 return b->location != NULL && event_location_empty_p (b->location.get ());
3469 }
3470
3471 void
3472 update_breakpoints_after_exec (void)
3473 {
3474 struct breakpoint *b, *b_tmp;
3475 struct bp_location *bploc, **bplocp_tmp;
3476
3477 /* We're about to delete breakpoints from GDB's lists. If the
3478 INSERTED flag is true, GDB will try to lift the breakpoints by
3479 writing the breakpoints' "shadow contents" back into memory. The
3480 "shadow contents" are NOT valid after an exec, so GDB should not
3481 do that. Instead, the target is responsible from marking
3482 breakpoints out as soon as it detects an exec. We don't do that
3483 here instead, because there may be other attempts to delete
3484 breakpoints after detecting an exec and before reaching here. */
3485 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3486 if (bploc->pspace == current_program_space)
3487 gdb_assert (!bploc->inserted);
3488
3489 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3490 {
3491 if (b->pspace != current_program_space)
3492 continue;
3493
3494 /* Solib breakpoints must be explicitly reset after an exec(). */
3495 if (b->type == bp_shlib_event)
3496 {
3497 delete_breakpoint (b);
3498 continue;
3499 }
3500
3501 /* JIT breakpoints must be explicitly reset after an exec(). */
3502 if (b->type == bp_jit_event)
3503 {
3504 delete_breakpoint (b);
3505 continue;
3506 }
3507
3508 /* Thread event breakpoints must be set anew after an exec(),
3509 as must overlay event and longjmp master breakpoints. */
3510 if (b->type == bp_thread_event || b->type == bp_overlay_event
3511 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3512 || b->type == bp_exception_master)
3513 {
3514 delete_breakpoint (b);
3515 continue;
3516 }
3517
3518 /* Step-resume breakpoints are meaningless after an exec(). */
3519 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3520 {
3521 delete_breakpoint (b);
3522 continue;
3523 }
3524
3525 /* Just like single-step breakpoints. */
3526 if (b->type == bp_single_step)
3527 {
3528 delete_breakpoint (b);
3529 continue;
3530 }
3531
3532 /* Longjmp and longjmp-resume breakpoints are also meaningless
3533 after an exec. */
3534 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3535 || b->type == bp_longjmp_call_dummy
3536 || b->type == bp_exception || b->type == bp_exception_resume)
3537 {
3538 delete_breakpoint (b);
3539 continue;
3540 }
3541
3542 if (b->type == bp_catchpoint)
3543 {
3544 /* For now, none of the bp_catchpoint breakpoints need to
3545 do anything at this point. In the future, if some of
3546 the catchpoints need to something, we will need to add
3547 a new method, and call this method from here. */
3548 continue;
3549 }
3550
3551 /* bp_finish is a special case. The only way we ought to be able
3552 to see one of these when an exec() has happened, is if the user
3553 caught a vfork, and then said "finish". Ordinarily a finish just
3554 carries them to the call-site of the current callee, by setting
3555 a temporary bp there and resuming. But in this case, the finish
3556 will carry them entirely through the vfork & exec.
3557
3558 We don't want to allow a bp_finish to remain inserted now. But
3559 we can't safely delete it, 'cause finish_command has a handle to
3560 the bp on a bpstat, and will later want to delete it. There's a
3561 chance (and I've seen it happen) that if we delete the bp_finish
3562 here, that its storage will get reused by the time finish_command
3563 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3564 We really must allow finish_command to delete a bp_finish.
3565
3566 In the absence of a general solution for the "how do we know
3567 it's safe to delete something others may have handles to?"
3568 problem, what we'll do here is just uninsert the bp_finish, and
3569 let finish_command delete it.
3570
3571 (We know the bp_finish is "doomed" in the sense that it's
3572 momentary, and will be deleted as soon as finish_command sees
3573 the inferior stopped. So it doesn't matter that the bp's
3574 address is probably bogus in the new a.out, unlike e.g., the
3575 solib breakpoints.) */
3576
3577 if (b->type == bp_finish)
3578 {
3579 continue;
3580 }
3581
3582 /* Without a symbolic address, we have little hope of the
3583 pre-exec() address meaning the same thing in the post-exec()
3584 a.out. */
3585 if (breakpoint_event_location_empty_p (b))
3586 {
3587 delete_breakpoint (b);
3588 continue;
3589 }
3590 }
3591 }
3592
3593 int
3594 detach_breakpoints (ptid_t ptid)
3595 {
3596 struct bp_location *bl, **blp_tmp;
3597 int val = 0;
3598 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3599 struct inferior *inf = current_inferior ();
3600
3601 if (ptid.pid () == inferior_ptid.pid ())
3602 error (_("Cannot detach breakpoints of inferior_ptid"));
3603
3604 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3605 inferior_ptid = ptid;
3606 ALL_BP_LOCATIONS (bl, blp_tmp)
3607 {
3608 if (bl->pspace != inf->pspace)
3609 continue;
3610
3611 /* This function must physically remove breakpoints locations
3612 from the specified ptid, without modifying the breakpoint
3613 package's state. Locations of type bp_loc_other are only
3614 maintained at GDB side. So, there is no need to remove
3615 these bp_loc_other locations. Moreover, removing these
3616 would modify the breakpoint package's state. */
3617 if (bl->loc_type == bp_loc_other)
3618 continue;
3619
3620 if (bl->inserted)
3621 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3622 }
3623
3624 return val;
3625 }
3626
3627 /* Remove the breakpoint location BL from the current address space.
3628 Note that this is used to detach breakpoints from a child fork.
3629 When we get here, the child isn't in the inferior list, and neither
3630 do we have objects to represent its address space --- we should
3631 *not* look at bl->pspace->aspace here. */
3632
3633 static int
3634 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3635 {
3636 int val;
3637
3638 /* BL is never in moribund_locations by our callers. */
3639 gdb_assert (bl->owner != NULL);
3640
3641 /* The type of none suggests that owner is actually deleted.
3642 This should not ever happen. */
3643 gdb_assert (bl->owner->type != bp_none);
3644
3645 if (bl->loc_type == bp_loc_software_breakpoint
3646 || bl->loc_type == bp_loc_hardware_breakpoint)
3647 {
3648 /* "Normal" instruction breakpoint: either the standard
3649 trap-instruction bp (bp_breakpoint), or a
3650 bp_hardware_breakpoint. */
3651
3652 /* First check to see if we have to handle an overlay. */
3653 if (overlay_debugging == ovly_off
3654 || bl->section == NULL
3655 || !(section_is_overlay (bl->section)))
3656 {
3657 /* No overlay handling: just remove the breakpoint. */
3658
3659 /* If we're trying to uninsert a memory breakpoint that we
3660 know is set in a dynamic object that is marked
3661 shlib_disabled, then either the dynamic object was
3662 removed with "remove-symbol-file" or with
3663 "nosharedlibrary". In the former case, we don't know
3664 whether another dynamic object might have loaded over the
3665 breakpoint's address -- the user might well let us know
3666 about it next with add-symbol-file (the whole point of
3667 add-symbol-file is letting the user manually maintain a
3668 list of dynamically loaded objects). If we have the
3669 breakpoint's shadow memory, that is, this is a software
3670 breakpoint managed by GDB, check whether the breakpoint
3671 is still inserted in memory, to avoid overwriting wrong
3672 code with stale saved shadow contents. Note that HW
3673 breakpoints don't have shadow memory, as they're
3674 implemented using a mechanism that is not dependent on
3675 being able to modify the target's memory, and as such
3676 they should always be removed. */
3677 if (bl->shlib_disabled
3678 && bl->target_info.shadow_len != 0
3679 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3680 val = 0;
3681 else
3682 val = bl->owner->ops->remove_location (bl, reason);
3683 }
3684 else
3685 {
3686 /* This breakpoint is in an overlay section.
3687 Did we set a breakpoint at the LMA? */
3688 if (!overlay_events_enabled)
3689 {
3690 /* Yes -- overlay event support is not active, so we
3691 should have set a breakpoint at the LMA. Remove it.
3692 */
3693 /* Ignore any failures: if the LMA is in ROM, we will
3694 have already warned when we failed to insert it. */
3695 if (bl->loc_type == bp_loc_hardware_breakpoint)
3696 target_remove_hw_breakpoint (bl->gdbarch,
3697 &bl->overlay_target_info);
3698 else
3699 target_remove_breakpoint (bl->gdbarch,
3700 &bl->overlay_target_info,
3701 reason);
3702 }
3703 /* Did we set a breakpoint at the VMA?
3704 If so, we will have marked the breakpoint 'inserted'. */
3705 if (bl->inserted)
3706 {
3707 /* Yes -- remove it. Previously we did not bother to
3708 remove the breakpoint if the section had been
3709 unmapped, but let's not rely on that being safe. We
3710 don't know what the overlay manager might do. */
3711
3712 /* However, we should remove *software* breakpoints only
3713 if the section is still mapped, or else we overwrite
3714 wrong code with the saved shadow contents. */
3715 if (bl->loc_type == bp_loc_hardware_breakpoint
3716 || section_is_mapped (bl->section))
3717 val = bl->owner->ops->remove_location (bl, reason);
3718 else
3719 val = 0;
3720 }
3721 else
3722 {
3723 /* No -- not inserted, so no need to remove. No error. */
3724 val = 0;
3725 }
3726 }
3727
3728 /* In some cases, we might not be able to remove a breakpoint in
3729 a shared library that has already been removed, but we have
3730 not yet processed the shlib unload event. Similarly for an
3731 unloaded add-symbol-file object - the user might not yet have
3732 had the chance to remove-symbol-file it. shlib_disabled will
3733 be set if the library/object has already been removed, but
3734 the breakpoint hasn't been uninserted yet, e.g., after
3735 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3736 always-inserted mode. */
3737 if (val
3738 && (bl->loc_type == bp_loc_software_breakpoint
3739 && (bl->shlib_disabled
3740 || solib_name_from_address (bl->pspace, bl->address)
3741 || shared_objfile_contains_address_p (bl->pspace,
3742 bl->address))))
3743 val = 0;
3744
3745 if (val)
3746 return val;
3747 bl->inserted = (reason == DETACH_BREAKPOINT);
3748 }
3749 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3750 {
3751 gdb_assert (bl->owner->ops != NULL
3752 && bl->owner->ops->remove_location != NULL);
3753
3754 bl->inserted = (reason == DETACH_BREAKPOINT);
3755 bl->owner->ops->remove_location (bl, reason);
3756
3757 /* Failure to remove any of the hardware watchpoints comes here. */
3758 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3759 warning (_("Could not remove hardware watchpoint %d."),
3760 bl->owner->number);
3761 }
3762 else if (bl->owner->type == bp_catchpoint
3763 && breakpoint_enabled (bl->owner)
3764 && !bl->duplicate)
3765 {
3766 gdb_assert (bl->owner->ops != NULL
3767 && bl->owner->ops->remove_location != NULL);
3768
3769 val = bl->owner->ops->remove_location (bl, reason);
3770 if (val)
3771 return val;
3772
3773 bl->inserted = (reason == DETACH_BREAKPOINT);
3774 }
3775
3776 return 0;
3777 }
3778
3779 static int
3780 remove_breakpoint (struct bp_location *bl)
3781 {
3782 /* BL is never in moribund_locations by our callers. */
3783 gdb_assert (bl->owner != NULL);
3784
3785 /* The type of none suggests that owner is actually deleted.
3786 This should not ever happen. */
3787 gdb_assert (bl->owner->type != bp_none);
3788
3789 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3790
3791 switch_to_program_space_and_thread (bl->pspace);
3792
3793 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3794 }
3795
3796 /* Clear the "inserted" flag in all breakpoints. */
3797
3798 void
3799 mark_breakpoints_out (void)
3800 {
3801 struct bp_location *bl, **blp_tmp;
3802
3803 ALL_BP_LOCATIONS (bl, blp_tmp)
3804 if (bl->pspace == current_program_space)
3805 bl->inserted = 0;
3806 }
3807
3808 /* Clear the "inserted" flag in all breakpoints and delete any
3809 breakpoints which should go away between runs of the program.
3810
3811 Plus other such housekeeping that has to be done for breakpoints
3812 between runs.
3813
3814 Note: this function gets called at the end of a run (by
3815 generic_mourn_inferior) and when a run begins (by
3816 init_wait_for_inferior). */
3817
3818
3819
3820 void
3821 breakpoint_init_inferior (enum inf_context context)
3822 {
3823 struct breakpoint *b, *b_tmp;
3824 struct program_space *pspace = current_program_space;
3825
3826 /* If breakpoint locations are shared across processes, then there's
3827 nothing to do. */
3828 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3829 return;
3830
3831 mark_breakpoints_out ();
3832
3833 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3834 {
3835 if (b->loc && b->loc->pspace != pspace)
3836 continue;
3837
3838 switch (b->type)
3839 {
3840 case bp_call_dummy:
3841 case bp_longjmp_call_dummy:
3842
3843 /* If the call dummy breakpoint is at the entry point it will
3844 cause problems when the inferior is rerun, so we better get
3845 rid of it. */
3846
3847 case bp_watchpoint_scope:
3848
3849 /* Also get rid of scope breakpoints. */
3850
3851 case bp_shlib_event:
3852
3853 /* Also remove solib event breakpoints. Their addresses may
3854 have changed since the last time we ran the program.
3855 Actually we may now be debugging against different target;
3856 and so the solib backend that installed this breakpoint may
3857 not be used in by the target. E.g.,
3858
3859 (gdb) file prog-linux
3860 (gdb) run # native linux target
3861 ...
3862 (gdb) kill
3863 (gdb) file prog-win.exe
3864 (gdb) tar rem :9999 # remote Windows gdbserver.
3865 */
3866
3867 case bp_step_resume:
3868
3869 /* Also remove step-resume breakpoints. */
3870
3871 case bp_single_step:
3872
3873 /* Also remove single-step breakpoints. */
3874
3875 delete_breakpoint (b);
3876 break;
3877
3878 case bp_watchpoint:
3879 case bp_hardware_watchpoint:
3880 case bp_read_watchpoint:
3881 case bp_access_watchpoint:
3882 {
3883 struct watchpoint *w = (struct watchpoint *) b;
3884
3885 /* Likewise for watchpoints on local expressions. */
3886 if (w->exp_valid_block != NULL)
3887 delete_breakpoint (b);
3888 else
3889 {
3890 /* Get rid of existing locations, which are no longer
3891 valid. New ones will be created in
3892 update_watchpoint, when the inferior is restarted.
3893 The next update_global_location_list call will
3894 garbage collect them. */
3895 b->loc = NULL;
3896
3897 if (context == inf_starting)
3898 {
3899 /* Reset val field to force reread of starting value in
3900 insert_breakpoints. */
3901 w->val.reset (nullptr);
3902 w->val_valid = false;
3903 }
3904 }
3905 }
3906 break;
3907 default:
3908 break;
3909 }
3910 }
3911
3912 /* Get rid of the moribund locations. */
3913 for (bp_location *bl : moribund_locations)
3914 decref_bp_location (&bl);
3915 moribund_locations.clear ();
3916 }
3917
3918 /* These functions concern about actual breakpoints inserted in the
3919 target --- to e.g. check if we need to do decr_pc adjustment or if
3920 we need to hop over the bkpt --- so we check for address space
3921 match, not program space. */
3922
3923 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3924 exists at PC. It returns ordinary_breakpoint_here if it's an
3925 ordinary breakpoint, or permanent_breakpoint_here if it's a
3926 permanent breakpoint.
3927 - When continuing from a location with an ordinary breakpoint, we
3928 actually single step once before calling insert_breakpoints.
3929 - When continuing from a location with a permanent breakpoint, we
3930 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3931 the target, to advance the PC past the breakpoint. */
3932
3933 enum breakpoint_here
3934 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3935 {
3936 struct bp_location *bl, **blp_tmp;
3937 int any_breakpoint_here = 0;
3938
3939 ALL_BP_LOCATIONS (bl, blp_tmp)
3940 {
3941 if (bl->loc_type != bp_loc_software_breakpoint
3942 && bl->loc_type != bp_loc_hardware_breakpoint)
3943 continue;
3944
3945 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3946 if ((breakpoint_enabled (bl->owner)
3947 || bl->permanent)
3948 && breakpoint_location_address_match (bl, aspace, pc))
3949 {
3950 if (overlay_debugging
3951 && section_is_overlay (bl->section)
3952 && !section_is_mapped (bl->section))
3953 continue; /* unmapped overlay -- can't be a match */
3954 else if (bl->permanent)
3955 return permanent_breakpoint_here;
3956 else
3957 any_breakpoint_here = 1;
3958 }
3959 }
3960
3961 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
3962 }
3963
3964 /* See breakpoint.h. */
3965
3966 int
3967 breakpoint_in_range_p (const address_space *aspace,
3968 CORE_ADDR addr, ULONGEST len)
3969 {
3970 struct bp_location *bl, **blp_tmp;
3971
3972 ALL_BP_LOCATIONS (bl, blp_tmp)
3973 {
3974 if (bl->loc_type != bp_loc_software_breakpoint
3975 && bl->loc_type != bp_loc_hardware_breakpoint)
3976 continue;
3977
3978 if ((breakpoint_enabled (bl->owner)
3979 || bl->permanent)
3980 && breakpoint_location_address_range_overlap (bl, aspace,
3981 addr, len))
3982 {
3983 if (overlay_debugging
3984 && section_is_overlay (bl->section)
3985 && !section_is_mapped (bl->section))
3986 {
3987 /* Unmapped overlay -- can't be a match. */
3988 continue;
3989 }
3990
3991 return 1;
3992 }
3993 }
3994
3995 return 0;
3996 }
3997
3998 /* Return true if there's a moribund breakpoint at PC. */
3999
4000 int
4001 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4002 {
4003 for (bp_location *loc : moribund_locations)
4004 if (breakpoint_location_address_match (loc, aspace, pc))
4005 return 1;
4006
4007 return 0;
4008 }
4009
4010 /* Returns non-zero iff BL is inserted at PC, in address space
4011 ASPACE. */
4012
4013 static int
4014 bp_location_inserted_here_p (struct bp_location *bl,
4015 const address_space *aspace, CORE_ADDR pc)
4016 {
4017 if (bl->inserted
4018 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4019 aspace, pc))
4020 {
4021 if (overlay_debugging
4022 && section_is_overlay (bl->section)
4023 && !section_is_mapped (bl->section))
4024 return 0; /* unmapped overlay -- can't be a match */
4025 else
4026 return 1;
4027 }
4028 return 0;
4029 }
4030
4031 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4032
4033 int
4034 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4035 {
4036 struct bp_location **blp, **blp_tmp = NULL;
4037
4038 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4039 {
4040 struct bp_location *bl = *blp;
4041
4042 if (bl->loc_type != bp_loc_software_breakpoint
4043 && bl->loc_type != bp_loc_hardware_breakpoint)
4044 continue;
4045
4046 if (bp_location_inserted_here_p (bl, aspace, pc))
4047 return 1;
4048 }
4049 return 0;
4050 }
4051
4052 /* This function returns non-zero iff there is a software breakpoint
4053 inserted at PC. */
4054
4055 int
4056 software_breakpoint_inserted_here_p (const address_space *aspace,
4057 CORE_ADDR pc)
4058 {
4059 struct bp_location **blp, **blp_tmp = NULL;
4060
4061 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4062 {
4063 struct bp_location *bl = *blp;
4064
4065 if (bl->loc_type != bp_loc_software_breakpoint)
4066 continue;
4067
4068 if (bp_location_inserted_here_p (bl, aspace, pc))
4069 return 1;
4070 }
4071
4072 return 0;
4073 }
4074
4075 /* See breakpoint.h. */
4076
4077 int
4078 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4079 CORE_ADDR pc)
4080 {
4081 struct bp_location **blp, **blp_tmp = NULL;
4082
4083 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4084 {
4085 struct bp_location *bl = *blp;
4086
4087 if (bl->loc_type != bp_loc_hardware_breakpoint)
4088 continue;
4089
4090 if (bp_location_inserted_here_p (bl, aspace, pc))
4091 return 1;
4092 }
4093
4094 return 0;
4095 }
4096
4097 int
4098 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4099 CORE_ADDR addr, ULONGEST len)
4100 {
4101 struct breakpoint *bpt;
4102
4103 ALL_BREAKPOINTS (bpt)
4104 {
4105 struct bp_location *loc;
4106
4107 if (bpt->type != bp_hardware_watchpoint
4108 && bpt->type != bp_access_watchpoint)
4109 continue;
4110
4111 if (!breakpoint_enabled (bpt))
4112 continue;
4113
4114 for (loc = bpt->loc; loc; loc = loc->next)
4115 if (loc->pspace->aspace == aspace && loc->inserted)
4116 {
4117 CORE_ADDR l, h;
4118
4119 /* Check for intersection. */
4120 l = std::max<CORE_ADDR> (loc->address, addr);
4121 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4122 if (l < h)
4123 return 1;
4124 }
4125 }
4126 return 0;
4127 }
4128
4129 /* See breakpoint.h. */
4130
4131 bool
4132 is_catchpoint (struct breakpoint *b)
4133 {
4134 return (b->type == bp_catchpoint);
4135 }
4136
4137 /* Frees any storage that is part of a bpstat. Does not walk the
4138 'next' chain. */
4139
4140 bpstats::~bpstats ()
4141 {
4142 if (bp_location_at != NULL)
4143 decref_bp_location (&bp_location_at);
4144 }
4145
4146 /* Clear a bpstat so that it says we are not at any breakpoint.
4147 Also free any storage that is part of a bpstat. */
4148
4149 void
4150 bpstat_clear (bpstat *bsp)
4151 {
4152 bpstat p;
4153 bpstat q;
4154
4155 if (bsp == 0)
4156 return;
4157 p = *bsp;
4158 while (p != NULL)
4159 {
4160 q = p->next;
4161 delete p;
4162 p = q;
4163 }
4164 *bsp = NULL;
4165 }
4166
4167 bpstats::bpstats (const bpstats &other)
4168 : next (NULL),
4169 bp_location_at (other.bp_location_at),
4170 breakpoint_at (other.breakpoint_at),
4171 commands (other.commands),
4172 print (other.print),
4173 stop (other.stop),
4174 print_it (other.print_it)
4175 {
4176 if (other.old_val != NULL)
4177 old_val = release_value (value_copy (other.old_val.get ()));
4178 incref_bp_location (bp_location_at);
4179 }
4180
4181 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4182 is part of the bpstat is copied as well. */
4183
4184 bpstat
4185 bpstat_copy (bpstat bs)
4186 {
4187 bpstat p = NULL;
4188 bpstat tmp;
4189 bpstat retval = NULL;
4190
4191 if (bs == NULL)
4192 return bs;
4193
4194 for (; bs != NULL; bs = bs->next)
4195 {
4196 tmp = new bpstats (*bs);
4197
4198 if (p == NULL)
4199 /* This is the first thing in the chain. */
4200 retval = tmp;
4201 else
4202 p->next = tmp;
4203 p = tmp;
4204 }
4205 p->next = NULL;
4206 return retval;
4207 }
4208
4209 /* Find the bpstat associated with this breakpoint. */
4210
4211 bpstat
4212 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4213 {
4214 if (bsp == NULL)
4215 return NULL;
4216
4217 for (; bsp != NULL; bsp = bsp->next)
4218 {
4219 if (bsp->breakpoint_at == breakpoint)
4220 return bsp;
4221 }
4222 return NULL;
4223 }
4224
4225 /* See breakpoint.h. */
4226
4227 bool
4228 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4229 {
4230 for (; bsp != NULL; bsp = bsp->next)
4231 {
4232 if (bsp->breakpoint_at == NULL)
4233 {
4234 /* A moribund location can never explain a signal other than
4235 GDB_SIGNAL_TRAP. */
4236 if (sig == GDB_SIGNAL_TRAP)
4237 return true;
4238 }
4239 else
4240 {
4241 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4242 sig))
4243 return true;
4244 }
4245 }
4246
4247 return false;
4248 }
4249
4250 /* Put in *NUM the breakpoint number of the first breakpoint we are
4251 stopped at. *BSP upon return is a bpstat which points to the
4252 remaining breakpoints stopped at (but which is not guaranteed to be
4253 good for anything but further calls to bpstat_num).
4254
4255 Return 0 if passed a bpstat which does not indicate any breakpoints.
4256 Return -1 if stopped at a breakpoint that has been deleted since
4257 we set it.
4258 Return 1 otherwise. */
4259
4260 int
4261 bpstat_num (bpstat *bsp, int *num)
4262 {
4263 struct breakpoint *b;
4264
4265 if ((*bsp) == NULL)
4266 return 0; /* No more breakpoint values */
4267
4268 /* We assume we'll never have several bpstats that correspond to a
4269 single breakpoint -- otherwise, this function might return the
4270 same number more than once and this will look ugly. */
4271 b = (*bsp)->breakpoint_at;
4272 *bsp = (*bsp)->next;
4273 if (b == NULL)
4274 return -1; /* breakpoint that's been deleted since */
4275
4276 *num = b->number; /* We have its number */
4277 return 1;
4278 }
4279
4280 /* See breakpoint.h. */
4281
4282 void
4283 bpstat_clear_actions (void)
4284 {
4285 bpstat bs;
4286
4287 if (inferior_ptid == null_ptid)
4288 return;
4289
4290 thread_info *tp = inferior_thread ();
4291 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4292 {
4293 bs->commands = NULL;
4294 bs->old_val.reset (nullptr);
4295 }
4296 }
4297
4298 /* Called when a command is about to proceed the inferior. */
4299
4300 static void
4301 breakpoint_about_to_proceed (void)
4302 {
4303 if (inferior_ptid != null_ptid)
4304 {
4305 struct thread_info *tp = inferior_thread ();
4306
4307 /* Allow inferior function calls in breakpoint commands to not
4308 interrupt the command list. When the call finishes
4309 successfully, the inferior will be standing at the same
4310 breakpoint as if nothing happened. */
4311 if (tp->control.in_infcall)
4312 return;
4313 }
4314
4315 breakpoint_proceeded = 1;
4316 }
4317
4318 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4319 or its equivalent. */
4320
4321 static int
4322 command_line_is_silent (struct command_line *cmd)
4323 {
4324 return cmd && (strcmp ("silent", cmd->line) == 0);
4325 }
4326
4327 /* Execute all the commands associated with all the breakpoints at
4328 this location. Any of these commands could cause the process to
4329 proceed beyond this point, etc. We look out for such changes by
4330 checking the global "breakpoint_proceeded" after each command.
4331
4332 Returns true if a breakpoint command resumed the inferior. In that
4333 case, it is the caller's responsibility to recall it again with the
4334 bpstat of the current thread. */
4335
4336 static int
4337 bpstat_do_actions_1 (bpstat *bsp)
4338 {
4339 bpstat bs;
4340 int again = 0;
4341
4342 /* Avoid endless recursion if a `source' command is contained
4343 in bs->commands. */
4344 if (executing_breakpoint_commands)
4345 return 0;
4346
4347 scoped_restore save_executing
4348 = make_scoped_restore (&executing_breakpoint_commands, 1);
4349
4350 scoped_restore preventer = prevent_dont_repeat ();
4351
4352 /* This pointer will iterate over the list of bpstat's. */
4353 bs = *bsp;
4354
4355 breakpoint_proceeded = 0;
4356 for (; bs != NULL; bs = bs->next)
4357 {
4358 struct command_line *cmd = NULL;
4359
4360 /* Take ownership of the BSP's command tree, if it has one.
4361
4362 The command tree could legitimately contain commands like
4363 'step' and 'next', which call clear_proceed_status, which
4364 frees stop_bpstat's command tree. To make sure this doesn't
4365 free the tree we're executing out from under us, we need to
4366 take ownership of the tree ourselves. Since a given bpstat's
4367 commands are only executed once, we don't need to copy it; we
4368 can clear the pointer in the bpstat, and make sure we free
4369 the tree when we're done. */
4370 counted_command_line ccmd = bs->commands;
4371 bs->commands = NULL;
4372 if (ccmd != NULL)
4373 cmd = ccmd.get ();
4374 if (command_line_is_silent (cmd))
4375 {
4376 /* The action has been already done by bpstat_stop_status. */
4377 cmd = cmd->next;
4378 }
4379
4380 while (cmd != NULL)
4381 {
4382 execute_control_command (cmd);
4383
4384 if (breakpoint_proceeded)
4385 break;
4386 else
4387 cmd = cmd->next;
4388 }
4389
4390 if (breakpoint_proceeded)
4391 {
4392 if (current_ui->async)
4393 /* If we are in async mode, then the target might be still
4394 running, not stopped at any breakpoint, so nothing for
4395 us to do here -- just return to the event loop. */
4396 ;
4397 else
4398 /* In sync mode, when execute_control_command returns
4399 we're already standing on the next breakpoint.
4400 Breakpoint commands for that stop were not run, since
4401 execute_command does not run breakpoint commands --
4402 only command_line_handler does, but that one is not
4403 involved in execution of breakpoint commands. So, we
4404 can now execute breakpoint commands. It should be
4405 noted that making execute_command do bpstat actions is
4406 not an option -- in this case we'll have recursive
4407 invocation of bpstat for each breakpoint with a
4408 command, and can easily blow up GDB stack. Instead, we
4409 return true, which will trigger the caller to recall us
4410 with the new stop_bpstat. */
4411 again = 1;
4412 break;
4413 }
4414 }
4415 return again;
4416 }
4417
4418 /* Helper for bpstat_do_actions. Get the current thread, if there's
4419 one, is alive and has execution. Return NULL otherwise. */
4420
4421 static thread_info *
4422 get_bpstat_thread ()
4423 {
4424 if (inferior_ptid == null_ptid || !target_has_execution)
4425 return NULL;
4426
4427 thread_info *tp = inferior_thread ();
4428 if (tp->state == THREAD_EXITED || tp->executing)
4429 return NULL;
4430 return tp;
4431 }
4432
4433 void
4434 bpstat_do_actions (void)
4435 {
4436 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4437 thread_info *tp;
4438
4439 /* Do any commands attached to breakpoint we are stopped at. */
4440 while ((tp = get_bpstat_thread ()) != NULL)
4441 {
4442 /* Since in sync mode, bpstat_do_actions may resume the
4443 inferior, and only return when it is stopped at the next
4444 breakpoint, we keep doing breakpoint actions until it returns
4445 false to indicate the inferior was not resumed. */
4446 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4447 break;
4448 }
4449
4450 cleanup_if_error.release ();
4451 }
4452
4453 /* Print out the (old or new) value associated with a watchpoint. */
4454
4455 static void
4456 watchpoint_value_print (struct value *val, struct ui_file *stream)
4457 {
4458 if (val == NULL)
4459 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4460 else
4461 {
4462 struct value_print_options opts;
4463 get_user_print_options (&opts);
4464 value_print (val, stream, &opts);
4465 }
4466 }
4467
4468 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4469 debugging multiple threads. */
4470
4471 void
4472 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4473 {
4474 if (uiout->is_mi_like_p ())
4475 return;
4476
4477 uiout->text ("\n");
4478
4479 if (show_thread_that_caused_stop ())
4480 {
4481 const char *name;
4482 struct thread_info *thr = inferior_thread ();
4483
4484 uiout->text ("Thread ");
4485 uiout->field_string ("thread-id", print_thread_id (thr));
4486
4487 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4488 if (name != NULL)
4489 {
4490 uiout->text (" \"");
4491 uiout->field_string ("name", name);
4492 uiout->text ("\"");
4493 }
4494
4495 uiout->text (" hit ");
4496 }
4497 }
4498
4499 /* Generic routine for printing messages indicating why we
4500 stopped. The behavior of this function depends on the value
4501 'print_it' in the bpstat structure. Under some circumstances we
4502 may decide not to print anything here and delegate the task to
4503 normal_stop(). */
4504
4505 static enum print_stop_action
4506 print_bp_stop_message (bpstat bs)
4507 {
4508 switch (bs->print_it)
4509 {
4510 case print_it_noop:
4511 /* Nothing should be printed for this bpstat entry. */
4512 return PRINT_UNKNOWN;
4513 break;
4514
4515 case print_it_done:
4516 /* We still want to print the frame, but we already printed the
4517 relevant messages. */
4518 return PRINT_SRC_AND_LOC;
4519 break;
4520
4521 case print_it_normal:
4522 {
4523 struct breakpoint *b = bs->breakpoint_at;
4524
4525 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4526 which has since been deleted. */
4527 if (b == NULL)
4528 return PRINT_UNKNOWN;
4529
4530 /* Normal case. Call the breakpoint's print_it method. */
4531 return b->ops->print_it (bs);
4532 }
4533 break;
4534
4535 default:
4536 internal_error (__FILE__, __LINE__,
4537 _("print_bp_stop_message: unrecognized enum value"));
4538 break;
4539 }
4540 }
4541
4542 /* A helper function that prints a shared library stopped event. */
4543
4544 static void
4545 print_solib_event (int is_catchpoint)
4546 {
4547 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4548 bool any_added = !current_program_space->added_solibs.empty ();
4549
4550 if (!is_catchpoint)
4551 {
4552 if (any_added || any_deleted)
4553 current_uiout->text (_("Stopped due to shared library event:\n"));
4554 else
4555 current_uiout->text (_("Stopped due to shared library event (no "
4556 "libraries added or removed)\n"));
4557 }
4558
4559 if (current_uiout->is_mi_like_p ())
4560 current_uiout->field_string ("reason",
4561 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4562
4563 if (any_deleted)
4564 {
4565 current_uiout->text (_(" Inferior unloaded "));
4566 ui_out_emit_list list_emitter (current_uiout, "removed");
4567 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4568 {
4569 const std::string &name = current_program_space->deleted_solibs[ix];
4570
4571 if (ix > 0)
4572 current_uiout->text (" ");
4573 current_uiout->field_string ("library", name);
4574 current_uiout->text ("\n");
4575 }
4576 }
4577
4578 if (any_added)
4579 {
4580 current_uiout->text (_(" Inferior loaded "));
4581 ui_out_emit_list list_emitter (current_uiout, "added");
4582 bool first = true;
4583 for (so_list *iter : current_program_space->added_solibs)
4584 {
4585 if (!first)
4586 current_uiout->text (" ");
4587 first = false;
4588 current_uiout->field_string ("library", iter->so_name);
4589 current_uiout->text ("\n");
4590 }
4591 }
4592 }
4593
4594 /* Print a message indicating what happened. This is called from
4595 normal_stop(). The input to this routine is the head of the bpstat
4596 list - a list of the eventpoints that caused this stop. KIND is
4597 the target_waitkind for the stopping event. This
4598 routine calls the generic print routine for printing a message
4599 about reasons for stopping. This will print (for example) the
4600 "Breakpoint n," part of the output. The return value of this
4601 routine is one of:
4602
4603 PRINT_UNKNOWN: Means we printed nothing.
4604 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4605 code to print the location. An example is
4606 "Breakpoint 1, " which should be followed by
4607 the location.
4608 PRINT_SRC_ONLY: Means we printed something, but there is no need
4609 to also print the location part of the message.
4610 An example is the catch/throw messages, which
4611 don't require a location appended to the end.
4612 PRINT_NOTHING: We have done some printing and we don't need any
4613 further info to be printed. */
4614
4615 enum print_stop_action
4616 bpstat_print (bpstat bs, int kind)
4617 {
4618 enum print_stop_action val;
4619
4620 /* Maybe another breakpoint in the chain caused us to stop.
4621 (Currently all watchpoints go on the bpstat whether hit or not.
4622 That probably could (should) be changed, provided care is taken
4623 with respect to bpstat_explains_signal). */
4624 for (; bs; bs = bs->next)
4625 {
4626 val = print_bp_stop_message (bs);
4627 if (val == PRINT_SRC_ONLY
4628 || val == PRINT_SRC_AND_LOC
4629 || val == PRINT_NOTHING)
4630 return val;
4631 }
4632
4633 /* If we had hit a shared library event breakpoint,
4634 print_bp_stop_message would print out this message. If we hit an
4635 OS-level shared library event, do the same thing. */
4636 if (kind == TARGET_WAITKIND_LOADED)
4637 {
4638 print_solib_event (0);
4639 return PRINT_NOTHING;
4640 }
4641
4642 /* We reached the end of the chain, or we got a null BS to start
4643 with and nothing was printed. */
4644 return PRINT_UNKNOWN;
4645 }
4646
4647 /* Evaluate the boolean expression EXP and return the result. */
4648
4649 static bool
4650 breakpoint_cond_eval (expression *exp)
4651 {
4652 struct value *mark = value_mark ();
4653 bool res = value_true (evaluate_expression (exp));
4654
4655 value_free_to_mark (mark);
4656 return res;
4657 }
4658
4659 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4660
4661 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4662 : next (NULL),
4663 bp_location_at (bl),
4664 breakpoint_at (bl->owner),
4665 commands (NULL),
4666 print (0),
4667 stop (0),
4668 print_it (print_it_normal)
4669 {
4670 incref_bp_location (bl);
4671 **bs_link_pointer = this;
4672 *bs_link_pointer = &next;
4673 }
4674
4675 bpstats::bpstats ()
4676 : next (NULL),
4677 bp_location_at (NULL),
4678 breakpoint_at (NULL),
4679 commands (NULL),
4680 print (0),
4681 stop (0),
4682 print_it (print_it_normal)
4683 {
4684 }
4685 \f
4686 /* The target has stopped with waitstatus WS. Check if any hardware
4687 watchpoints have triggered, according to the target. */
4688
4689 int
4690 watchpoints_triggered (struct target_waitstatus *ws)
4691 {
4692 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4693 CORE_ADDR addr;
4694 struct breakpoint *b;
4695
4696 if (!stopped_by_watchpoint)
4697 {
4698 /* We were not stopped by a watchpoint. Mark all watchpoints
4699 as not triggered. */
4700 ALL_BREAKPOINTS (b)
4701 if (is_hardware_watchpoint (b))
4702 {
4703 struct watchpoint *w = (struct watchpoint *) b;
4704
4705 w->watchpoint_triggered = watch_triggered_no;
4706 }
4707
4708 return 0;
4709 }
4710
4711 if (!target_stopped_data_address (current_top_target (), &addr))
4712 {
4713 /* We were stopped by a watchpoint, but we don't know where.
4714 Mark all watchpoints as unknown. */
4715 ALL_BREAKPOINTS (b)
4716 if (is_hardware_watchpoint (b))
4717 {
4718 struct watchpoint *w = (struct watchpoint *) b;
4719
4720 w->watchpoint_triggered = watch_triggered_unknown;
4721 }
4722
4723 return 1;
4724 }
4725
4726 /* The target could report the data address. Mark watchpoints
4727 affected by this data address as triggered, and all others as not
4728 triggered. */
4729
4730 ALL_BREAKPOINTS (b)
4731 if (is_hardware_watchpoint (b))
4732 {
4733 struct watchpoint *w = (struct watchpoint *) b;
4734 struct bp_location *loc;
4735
4736 w->watchpoint_triggered = watch_triggered_no;
4737 for (loc = b->loc; loc; loc = loc->next)
4738 {
4739 if (is_masked_watchpoint (b))
4740 {
4741 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4742 CORE_ADDR start = loc->address & w->hw_wp_mask;
4743
4744 if (newaddr == start)
4745 {
4746 w->watchpoint_triggered = watch_triggered_yes;
4747 break;
4748 }
4749 }
4750 /* Exact match not required. Within range is sufficient. */
4751 else if (target_watchpoint_addr_within_range (current_top_target (),
4752 addr, loc->address,
4753 loc->length))
4754 {
4755 w->watchpoint_triggered = watch_triggered_yes;
4756 break;
4757 }
4758 }
4759 }
4760
4761 return 1;
4762 }
4763
4764 /* Possible return values for watchpoint_check. */
4765 enum wp_check_result
4766 {
4767 /* The watchpoint has been deleted. */
4768 WP_DELETED = 1,
4769
4770 /* The value has changed. */
4771 WP_VALUE_CHANGED = 2,
4772
4773 /* The value has not changed. */
4774 WP_VALUE_NOT_CHANGED = 3,
4775
4776 /* Ignore this watchpoint, no matter if the value changed or not. */
4777 WP_IGNORE = 4,
4778 };
4779
4780 #define BP_TEMPFLAG 1
4781 #define BP_HARDWAREFLAG 2
4782
4783 /* Evaluate watchpoint condition expression and check if its value
4784 changed. */
4785
4786 static wp_check_result
4787 watchpoint_check (bpstat bs)
4788 {
4789 struct watchpoint *b;
4790 struct frame_info *fr;
4791 int within_current_scope;
4792
4793 /* BS is built from an existing struct breakpoint. */
4794 gdb_assert (bs->breakpoint_at != NULL);
4795 b = (struct watchpoint *) bs->breakpoint_at;
4796
4797 /* If this is a local watchpoint, we only want to check if the
4798 watchpoint frame is in scope if the current thread is the thread
4799 that was used to create the watchpoint. */
4800 if (!watchpoint_in_thread_scope (b))
4801 return WP_IGNORE;
4802
4803 if (b->exp_valid_block == NULL)
4804 within_current_scope = 1;
4805 else
4806 {
4807 struct frame_info *frame = get_current_frame ();
4808 struct gdbarch *frame_arch = get_frame_arch (frame);
4809 CORE_ADDR frame_pc = get_frame_pc (frame);
4810
4811 /* stack_frame_destroyed_p() returns a non-zero value if we're
4812 still in the function but the stack frame has already been
4813 invalidated. Since we can't rely on the values of local
4814 variables after the stack has been destroyed, we are treating
4815 the watchpoint in that state as `not changed' without further
4816 checking. Don't mark watchpoints as changed if the current
4817 frame is in an epilogue - even if they are in some other
4818 frame, our view of the stack is likely to be wrong and
4819 frame_find_by_id could error out. */
4820 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4821 return WP_IGNORE;
4822
4823 fr = frame_find_by_id (b->watchpoint_frame);
4824 within_current_scope = (fr != NULL);
4825
4826 /* If we've gotten confused in the unwinder, we might have
4827 returned a frame that can't describe this variable. */
4828 if (within_current_scope)
4829 {
4830 struct symbol *function;
4831
4832 function = get_frame_function (fr);
4833 if (function == NULL
4834 || !contained_in (b->exp_valid_block,
4835 SYMBOL_BLOCK_VALUE (function)))
4836 within_current_scope = 0;
4837 }
4838
4839 if (within_current_scope)
4840 /* If we end up stopping, the current frame will get selected
4841 in normal_stop. So this call to select_frame won't affect
4842 the user. */
4843 select_frame (fr);
4844 }
4845
4846 if (within_current_scope)
4847 {
4848 /* We use value_{,free_to_}mark because it could be a *long*
4849 time before we return to the command level and call
4850 free_all_values. We can't call free_all_values because we
4851 might be in the middle of evaluating a function call. */
4852
4853 int pc = 0;
4854 struct value *mark;
4855 struct value *new_val;
4856
4857 if (is_masked_watchpoint (b))
4858 /* Since we don't know the exact trigger address (from
4859 stopped_data_address), just tell the user we've triggered
4860 a mask watchpoint. */
4861 return WP_VALUE_CHANGED;
4862
4863 mark = value_mark ();
4864 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4865
4866 if (b->val_bitsize != 0)
4867 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4868
4869 /* We use value_equal_contents instead of value_equal because
4870 the latter coerces an array to a pointer, thus comparing just
4871 the address of the array instead of its contents. This is
4872 not what we want. */
4873 if ((b->val != NULL) != (new_val != NULL)
4874 || (b->val != NULL && !value_equal_contents (b->val.get (),
4875 new_val)))
4876 {
4877 bs->old_val = b->val;
4878 b->val = release_value (new_val);
4879 b->val_valid = true;
4880 if (new_val != NULL)
4881 value_free_to_mark (mark);
4882 return WP_VALUE_CHANGED;
4883 }
4884 else
4885 {
4886 /* Nothing changed. */
4887 value_free_to_mark (mark);
4888 return WP_VALUE_NOT_CHANGED;
4889 }
4890 }
4891 else
4892 {
4893 /* This seems like the only logical thing to do because
4894 if we temporarily ignored the watchpoint, then when
4895 we reenter the block in which it is valid it contains
4896 garbage (in the case of a function, it may have two
4897 garbage values, one before and one after the prologue).
4898 So we can't even detect the first assignment to it and
4899 watch after that (since the garbage may or may not equal
4900 the first value assigned). */
4901 /* We print all the stop information in
4902 breakpoint_ops->print_it, but in this case, by the time we
4903 call breakpoint_ops->print_it this bp will be deleted
4904 already. So we have no choice but print the information
4905 here. */
4906
4907 SWITCH_THRU_ALL_UIS ()
4908 {
4909 struct ui_out *uiout = current_uiout;
4910
4911 if (uiout->is_mi_like_p ())
4912 uiout->field_string
4913 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4914 uiout->message ("\nWatchpoint %pF deleted because the program has "
4915 "left the block in\n"
4916 "which its expression is valid.\n",
4917 signed_field ("wpnum", b->number));
4918 }
4919
4920 /* Make sure the watchpoint's commands aren't executed. */
4921 b->commands = NULL;
4922 watchpoint_del_at_next_stop (b);
4923
4924 return WP_DELETED;
4925 }
4926 }
4927
4928 /* Return true if it looks like target has stopped due to hitting
4929 breakpoint location BL. This function does not check if we should
4930 stop, only if BL explains the stop. */
4931
4932 static int
4933 bpstat_check_location (const struct bp_location *bl,
4934 const address_space *aspace, CORE_ADDR bp_addr,
4935 const struct target_waitstatus *ws)
4936 {
4937 struct breakpoint *b = bl->owner;
4938
4939 /* BL is from an existing breakpoint. */
4940 gdb_assert (b != NULL);
4941
4942 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4943 }
4944
4945 /* Determine if the watched values have actually changed, and we
4946 should stop. If not, set BS->stop to 0. */
4947
4948 static void
4949 bpstat_check_watchpoint (bpstat bs)
4950 {
4951 const struct bp_location *bl;
4952 struct watchpoint *b;
4953
4954 /* BS is built for existing struct breakpoint. */
4955 bl = bs->bp_location_at;
4956 gdb_assert (bl != NULL);
4957 b = (struct watchpoint *) bs->breakpoint_at;
4958 gdb_assert (b != NULL);
4959
4960 {
4961 int must_check_value = 0;
4962
4963 if (b->type == bp_watchpoint)
4964 /* For a software watchpoint, we must always check the
4965 watched value. */
4966 must_check_value = 1;
4967 else if (b->watchpoint_triggered == watch_triggered_yes)
4968 /* We have a hardware watchpoint (read, write, or access)
4969 and the target earlier reported an address watched by
4970 this watchpoint. */
4971 must_check_value = 1;
4972 else if (b->watchpoint_triggered == watch_triggered_unknown
4973 && b->type == bp_hardware_watchpoint)
4974 /* We were stopped by a hardware watchpoint, but the target could
4975 not report the data address. We must check the watchpoint's
4976 value. Access and read watchpoints are out of luck; without
4977 a data address, we can't figure it out. */
4978 must_check_value = 1;
4979
4980 if (must_check_value)
4981 {
4982 wp_check_result e;
4983
4984 try
4985 {
4986 e = watchpoint_check (bs);
4987 }
4988 catch (const gdb_exception &ex)
4989 {
4990 exception_fprintf (gdb_stderr, ex,
4991 "Error evaluating expression "
4992 "for watchpoint %d\n",
4993 b->number);
4994
4995 SWITCH_THRU_ALL_UIS ()
4996 {
4997 printf_filtered (_("Watchpoint %d deleted.\n"),
4998 b->number);
4999 }
5000 watchpoint_del_at_next_stop (b);
5001 e = WP_DELETED;
5002 }
5003
5004 switch (e)
5005 {
5006 case WP_DELETED:
5007 /* We've already printed what needs to be printed. */
5008 bs->print_it = print_it_done;
5009 /* Stop. */
5010 break;
5011 case WP_IGNORE:
5012 bs->print_it = print_it_noop;
5013 bs->stop = 0;
5014 break;
5015 case WP_VALUE_CHANGED:
5016 if (b->type == bp_read_watchpoint)
5017 {
5018 /* There are two cases to consider here:
5019
5020 1. We're watching the triggered memory for reads.
5021 In that case, trust the target, and always report
5022 the watchpoint hit to the user. Even though
5023 reads don't cause value changes, the value may
5024 have changed since the last time it was read, and
5025 since we're not trapping writes, we will not see
5026 those, and as such we should ignore our notion of
5027 old value.
5028
5029 2. We're watching the triggered memory for both
5030 reads and writes. There are two ways this may
5031 happen:
5032
5033 2.1. This is a target that can't break on data
5034 reads only, but can break on accesses (reads or
5035 writes), such as e.g., x86. We detect this case
5036 at the time we try to insert read watchpoints.
5037
5038 2.2. Otherwise, the target supports read
5039 watchpoints, but, the user set an access or write
5040 watchpoint watching the same memory as this read
5041 watchpoint.
5042
5043 If we're watching memory writes as well as reads,
5044 ignore watchpoint hits when we find that the
5045 value hasn't changed, as reads don't cause
5046 changes. This still gives false positives when
5047 the program writes the same value to memory as
5048 what there was already in memory (we will confuse
5049 it for a read), but it's much better than
5050 nothing. */
5051
5052 int other_write_watchpoint = 0;
5053
5054 if (bl->watchpoint_type == hw_read)
5055 {
5056 struct breakpoint *other_b;
5057
5058 ALL_BREAKPOINTS (other_b)
5059 if (other_b->type == bp_hardware_watchpoint
5060 || other_b->type == bp_access_watchpoint)
5061 {
5062 struct watchpoint *other_w =
5063 (struct watchpoint *) other_b;
5064
5065 if (other_w->watchpoint_triggered
5066 == watch_triggered_yes)
5067 {
5068 other_write_watchpoint = 1;
5069 break;
5070 }
5071 }
5072 }
5073
5074 if (other_write_watchpoint
5075 || bl->watchpoint_type == hw_access)
5076 {
5077 /* We're watching the same memory for writes,
5078 and the value changed since the last time we
5079 updated it, so this trap must be for a write.
5080 Ignore it. */
5081 bs->print_it = print_it_noop;
5082 bs->stop = 0;
5083 }
5084 }
5085 break;
5086 case WP_VALUE_NOT_CHANGED:
5087 if (b->type == bp_hardware_watchpoint
5088 || b->type == bp_watchpoint)
5089 {
5090 /* Don't stop: write watchpoints shouldn't fire if
5091 the value hasn't changed. */
5092 bs->print_it = print_it_noop;
5093 bs->stop = 0;
5094 }
5095 /* Stop. */
5096 break;
5097 default:
5098 /* Can't happen. */
5099 break;
5100 }
5101 }
5102 else /* must_check_value == 0 */
5103 {
5104 /* This is a case where some watchpoint(s) triggered, but
5105 not at the address of this watchpoint, or else no
5106 watchpoint triggered after all. So don't print
5107 anything for this watchpoint. */
5108 bs->print_it = print_it_noop;
5109 bs->stop = 0;
5110 }
5111 }
5112 }
5113
5114 /* For breakpoints that are currently marked as telling gdb to stop,
5115 check conditions (condition proper, frame, thread and ignore count)
5116 of breakpoint referred to by BS. If we should not stop for this
5117 breakpoint, set BS->stop to 0. */
5118
5119 static void
5120 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5121 {
5122 const struct bp_location *bl;
5123 struct breakpoint *b;
5124 /* Assume stop. */
5125 bool condition_result = true;
5126 struct expression *cond;
5127
5128 gdb_assert (bs->stop);
5129
5130 /* BS is built for existing struct breakpoint. */
5131 bl = bs->bp_location_at;
5132 gdb_assert (bl != NULL);
5133 b = bs->breakpoint_at;
5134 gdb_assert (b != NULL);
5135
5136 /* Even if the target evaluated the condition on its end and notified GDB, we
5137 need to do so again since GDB does not know if we stopped due to a
5138 breakpoint or a single step breakpoint. */
5139
5140 if (frame_id_p (b->frame_id)
5141 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5142 {
5143 bs->stop = 0;
5144 return;
5145 }
5146
5147 /* If this is a thread/task-specific breakpoint, don't waste cpu
5148 evaluating the condition if this isn't the specified
5149 thread/task. */
5150 if ((b->thread != -1 && b->thread != thread->global_num)
5151 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5152 {
5153 bs->stop = 0;
5154 return;
5155 }
5156
5157 /* Evaluate extension language breakpoints that have a "stop" method
5158 implemented. */
5159 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5160
5161 if (is_watchpoint (b))
5162 {
5163 struct watchpoint *w = (struct watchpoint *) b;
5164
5165 cond = w->cond_exp.get ();
5166 }
5167 else
5168 cond = bl->cond.get ();
5169
5170 if (cond && b->disposition != disp_del_at_next_stop)
5171 {
5172 int within_current_scope = 1;
5173 struct watchpoint * w;
5174
5175 /* We use value_mark and value_free_to_mark because it could
5176 be a long time before we return to the command level and
5177 call free_all_values. We can't call free_all_values
5178 because we might be in the middle of evaluating a
5179 function call. */
5180 struct value *mark = value_mark ();
5181
5182 if (is_watchpoint (b))
5183 w = (struct watchpoint *) b;
5184 else
5185 w = NULL;
5186
5187 /* Need to select the frame, with all that implies so that
5188 the conditions will have the right context. Because we
5189 use the frame, we will not see an inlined function's
5190 variables when we arrive at a breakpoint at the start
5191 of the inlined function; the current frame will be the
5192 call site. */
5193 if (w == NULL || w->cond_exp_valid_block == NULL)
5194 select_frame (get_current_frame ());
5195 else
5196 {
5197 struct frame_info *frame;
5198
5199 /* For local watchpoint expressions, which particular
5200 instance of a local is being watched matters, so we
5201 keep track of the frame to evaluate the expression
5202 in. To evaluate the condition however, it doesn't
5203 really matter which instantiation of the function
5204 where the condition makes sense triggers the
5205 watchpoint. This allows an expression like "watch
5206 global if q > 10" set in `func', catch writes to
5207 global on all threads that call `func', or catch
5208 writes on all recursive calls of `func' by a single
5209 thread. We simply always evaluate the condition in
5210 the innermost frame that's executing where it makes
5211 sense to evaluate the condition. It seems
5212 intuitive. */
5213 frame = block_innermost_frame (w->cond_exp_valid_block);
5214 if (frame != NULL)
5215 select_frame (frame);
5216 else
5217 within_current_scope = 0;
5218 }
5219 if (within_current_scope)
5220 {
5221 try
5222 {
5223 condition_result = breakpoint_cond_eval (cond);
5224 }
5225 catch (const gdb_exception &ex)
5226 {
5227 exception_fprintf (gdb_stderr, ex,
5228 "Error in testing breakpoint condition:\n");
5229 }
5230 }
5231 else
5232 {
5233 warning (_("Watchpoint condition cannot be tested "
5234 "in the current scope"));
5235 /* If we failed to set the right context for this
5236 watchpoint, unconditionally report it. */
5237 }
5238 /* FIXME-someday, should give breakpoint #. */
5239 value_free_to_mark (mark);
5240 }
5241
5242 if (cond && !condition_result)
5243 {
5244 bs->stop = 0;
5245 }
5246 else if (b->ignore_count > 0)
5247 {
5248 b->ignore_count--;
5249 bs->stop = 0;
5250 /* Increase the hit count even though we don't stop. */
5251 ++(b->hit_count);
5252 gdb::observers::breakpoint_modified.notify (b);
5253 }
5254 }
5255
5256 /* Returns true if we need to track moribund locations of LOC's type
5257 on the current target. */
5258
5259 static int
5260 need_moribund_for_location_type (struct bp_location *loc)
5261 {
5262 return ((loc->loc_type == bp_loc_software_breakpoint
5263 && !target_supports_stopped_by_sw_breakpoint ())
5264 || (loc->loc_type == bp_loc_hardware_breakpoint
5265 && !target_supports_stopped_by_hw_breakpoint ()));
5266 }
5267
5268 /* See breakpoint.h. */
5269
5270 bpstat
5271 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5272 const struct target_waitstatus *ws)
5273 {
5274 struct breakpoint *b;
5275 bpstat bs_head = NULL, *bs_link = &bs_head;
5276
5277 ALL_BREAKPOINTS (b)
5278 {
5279 if (!breakpoint_enabled (b))
5280 continue;
5281
5282 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5283 {
5284 /* For hardware watchpoints, we look only at the first
5285 location. The watchpoint_check function will work on the
5286 entire expression, not the individual locations. For
5287 read watchpoints, the watchpoints_triggered function has
5288 checked all locations already. */
5289 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5290 break;
5291
5292 if (!bl->enabled || bl->shlib_disabled)
5293 continue;
5294
5295 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5296 continue;
5297
5298 /* Come here if it's a watchpoint, or if the break address
5299 matches. */
5300
5301 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5302 explain stop. */
5303
5304 /* Assume we stop. Should we find a watchpoint that is not
5305 actually triggered, or if the condition of the breakpoint
5306 evaluates as false, we'll reset 'stop' to 0. */
5307 bs->stop = 1;
5308 bs->print = 1;
5309
5310 /* If this is a scope breakpoint, mark the associated
5311 watchpoint as triggered so that we will handle the
5312 out-of-scope event. We'll get to the watchpoint next
5313 iteration. */
5314 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5315 {
5316 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5317
5318 w->watchpoint_triggered = watch_triggered_yes;
5319 }
5320 }
5321 }
5322
5323 /* Check if a moribund breakpoint explains the stop. */
5324 if (!target_supports_stopped_by_sw_breakpoint ()
5325 || !target_supports_stopped_by_hw_breakpoint ())
5326 {
5327 for (bp_location *loc : moribund_locations)
5328 {
5329 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5330 && need_moribund_for_location_type (loc))
5331 {
5332 bpstat bs = new bpstats (loc, &bs_link);
5333 /* For hits of moribund locations, we should just proceed. */
5334 bs->stop = 0;
5335 bs->print = 0;
5336 bs->print_it = print_it_noop;
5337 }
5338 }
5339 }
5340
5341 return bs_head;
5342 }
5343
5344 /* See breakpoint.h. */
5345
5346 bpstat
5347 bpstat_stop_status (const address_space *aspace,
5348 CORE_ADDR bp_addr, thread_info *thread,
5349 const struct target_waitstatus *ws,
5350 bpstat stop_chain)
5351 {
5352 struct breakpoint *b = NULL;
5353 /* First item of allocated bpstat's. */
5354 bpstat bs_head = stop_chain;
5355 bpstat bs;
5356 int need_remove_insert;
5357 int removed_any;
5358
5359 /* First, build the bpstat chain with locations that explain a
5360 target stop, while being careful to not set the target running,
5361 as that may invalidate locations (in particular watchpoint
5362 locations are recreated). Resuming will happen here with
5363 breakpoint conditions or watchpoint expressions that include
5364 inferior function calls. */
5365 if (bs_head == NULL)
5366 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5367
5368 /* A bit of special processing for shlib breakpoints. We need to
5369 process solib loading here, so that the lists of loaded and
5370 unloaded libraries are correct before we handle "catch load" and
5371 "catch unload". */
5372 for (bs = bs_head; bs != NULL; bs = bs->next)
5373 {
5374 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5375 {
5376 handle_solib_event ();
5377 break;
5378 }
5379 }
5380
5381 /* Now go through the locations that caused the target to stop, and
5382 check whether we're interested in reporting this stop to higher
5383 layers, or whether we should resume the target transparently. */
5384
5385 removed_any = 0;
5386
5387 for (bs = bs_head; bs != NULL; bs = bs->next)
5388 {
5389 if (!bs->stop)
5390 continue;
5391
5392 b = bs->breakpoint_at;
5393 b->ops->check_status (bs);
5394 if (bs->stop)
5395 {
5396 bpstat_check_breakpoint_conditions (bs, thread);
5397
5398 if (bs->stop)
5399 {
5400 ++(b->hit_count);
5401 gdb::observers::breakpoint_modified.notify (b);
5402
5403 /* We will stop here. */
5404 if (b->disposition == disp_disable)
5405 {
5406 --(b->enable_count);
5407 if (b->enable_count <= 0)
5408 b->enable_state = bp_disabled;
5409 removed_any = 1;
5410 }
5411 if (b->silent)
5412 bs->print = 0;
5413 bs->commands = b->commands;
5414 if (command_line_is_silent (bs->commands
5415 ? bs->commands.get () : NULL))
5416 bs->print = 0;
5417
5418 b->ops->after_condition_true (bs);
5419 }
5420
5421 }
5422
5423 /* Print nothing for this entry if we don't stop or don't
5424 print. */
5425 if (!bs->stop || !bs->print)
5426 bs->print_it = print_it_noop;
5427 }
5428
5429 /* If we aren't stopping, the value of some hardware watchpoint may
5430 not have changed, but the intermediate memory locations we are
5431 watching may have. Don't bother if we're stopping; this will get
5432 done later. */
5433 need_remove_insert = 0;
5434 if (! bpstat_causes_stop (bs_head))
5435 for (bs = bs_head; bs != NULL; bs = bs->next)
5436 if (!bs->stop
5437 && bs->breakpoint_at
5438 && is_hardware_watchpoint (bs->breakpoint_at))
5439 {
5440 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5441
5442 update_watchpoint (w, 0 /* don't reparse. */);
5443 need_remove_insert = 1;
5444 }
5445
5446 if (need_remove_insert)
5447 update_global_location_list (UGLL_MAY_INSERT);
5448 else if (removed_any)
5449 update_global_location_list (UGLL_DONT_INSERT);
5450
5451 return bs_head;
5452 }
5453
5454 static void
5455 handle_jit_event (void)
5456 {
5457 struct frame_info *frame;
5458 struct gdbarch *gdbarch;
5459
5460 if (debug_infrun)
5461 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5462
5463 /* Switch terminal for any messages produced by
5464 breakpoint_re_set. */
5465 target_terminal::ours_for_output ();
5466
5467 frame = get_current_frame ();
5468 gdbarch = get_frame_arch (frame);
5469 objfile *jiter = symbol_objfile (get_frame_function (frame));
5470
5471 jit_event_handler (gdbarch, jiter);
5472
5473 target_terminal::inferior ();
5474 }
5475
5476 /* Prepare WHAT final decision for infrun. */
5477
5478 /* Decide what infrun needs to do with this bpstat. */
5479
5480 struct bpstat_what
5481 bpstat_what (bpstat bs_head)
5482 {
5483 struct bpstat_what retval;
5484 bpstat bs;
5485
5486 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5487 retval.call_dummy = STOP_NONE;
5488 retval.is_longjmp = false;
5489
5490 for (bs = bs_head; bs != NULL; bs = bs->next)
5491 {
5492 /* Extract this BS's action. After processing each BS, we check
5493 if its action overrides all we've seem so far. */
5494 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5495 enum bptype bptype;
5496
5497 if (bs->breakpoint_at == NULL)
5498 {
5499 /* I suspect this can happen if it was a momentary
5500 breakpoint which has since been deleted. */
5501 bptype = bp_none;
5502 }
5503 else
5504 bptype = bs->breakpoint_at->type;
5505
5506 switch (bptype)
5507 {
5508 case bp_none:
5509 break;
5510 case bp_breakpoint:
5511 case bp_hardware_breakpoint:
5512 case bp_single_step:
5513 case bp_until:
5514 case bp_finish:
5515 case bp_shlib_event:
5516 if (bs->stop)
5517 {
5518 if (bs->print)
5519 this_action = BPSTAT_WHAT_STOP_NOISY;
5520 else
5521 this_action = BPSTAT_WHAT_STOP_SILENT;
5522 }
5523 else
5524 this_action = BPSTAT_WHAT_SINGLE;
5525 break;
5526 case bp_watchpoint:
5527 case bp_hardware_watchpoint:
5528 case bp_read_watchpoint:
5529 case bp_access_watchpoint:
5530 if (bs->stop)
5531 {
5532 if (bs->print)
5533 this_action = BPSTAT_WHAT_STOP_NOISY;
5534 else
5535 this_action = BPSTAT_WHAT_STOP_SILENT;
5536 }
5537 else
5538 {
5539 /* There was a watchpoint, but we're not stopping.
5540 This requires no further action. */
5541 }
5542 break;
5543 case bp_longjmp:
5544 case bp_longjmp_call_dummy:
5545 case bp_exception:
5546 if (bs->stop)
5547 {
5548 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5549 retval.is_longjmp = bptype != bp_exception;
5550 }
5551 else
5552 this_action = BPSTAT_WHAT_SINGLE;
5553 break;
5554 case bp_longjmp_resume:
5555 case bp_exception_resume:
5556 if (bs->stop)
5557 {
5558 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5559 retval.is_longjmp = bptype == bp_longjmp_resume;
5560 }
5561 else
5562 this_action = BPSTAT_WHAT_SINGLE;
5563 break;
5564 case bp_step_resume:
5565 if (bs->stop)
5566 this_action = BPSTAT_WHAT_STEP_RESUME;
5567 else
5568 {
5569 /* It is for the wrong frame. */
5570 this_action = BPSTAT_WHAT_SINGLE;
5571 }
5572 break;
5573 case bp_hp_step_resume:
5574 if (bs->stop)
5575 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5576 else
5577 {
5578 /* It is for the wrong frame. */
5579 this_action = BPSTAT_WHAT_SINGLE;
5580 }
5581 break;
5582 case bp_watchpoint_scope:
5583 case bp_thread_event:
5584 case bp_overlay_event:
5585 case bp_longjmp_master:
5586 case bp_std_terminate_master:
5587 case bp_exception_master:
5588 this_action = BPSTAT_WHAT_SINGLE;
5589 break;
5590 case bp_catchpoint:
5591 if (bs->stop)
5592 {
5593 if (bs->print)
5594 this_action = BPSTAT_WHAT_STOP_NOISY;
5595 else
5596 this_action = BPSTAT_WHAT_STOP_SILENT;
5597 }
5598 else
5599 {
5600 /* Some catchpoints are implemented with breakpoints.
5601 For those, we need to step over the breakpoint. */
5602 if (bs->bp_location_at->loc_type != bp_loc_other)
5603 this_action = BPSTAT_WHAT_SINGLE;
5604 }
5605 break;
5606 case bp_jit_event:
5607 this_action = BPSTAT_WHAT_SINGLE;
5608 break;
5609 case bp_call_dummy:
5610 /* Make sure the action is stop (silent or noisy),
5611 so infrun.c pops the dummy frame. */
5612 retval.call_dummy = STOP_STACK_DUMMY;
5613 this_action = BPSTAT_WHAT_STOP_SILENT;
5614 break;
5615 case bp_std_terminate:
5616 /* Make sure the action is stop (silent or noisy),
5617 so infrun.c pops the dummy frame. */
5618 retval.call_dummy = STOP_STD_TERMINATE;
5619 this_action = BPSTAT_WHAT_STOP_SILENT;
5620 break;
5621 case bp_tracepoint:
5622 case bp_fast_tracepoint:
5623 case bp_static_tracepoint:
5624 /* Tracepoint hits should not be reported back to GDB, and
5625 if one got through somehow, it should have been filtered
5626 out already. */
5627 internal_error (__FILE__, __LINE__,
5628 _("bpstat_what: tracepoint encountered"));
5629 break;
5630 case bp_gnu_ifunc_resolver:
5631 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5632 this_action = BPSTAT_WHAT_SINGLE;
5633 break;
5634 case bp_gnu_ifunc_resolver_return:
5635 /* The breakpoint will be removed, execution will restart from the
5636 PC of the former breakpoint. */
5637 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5638 break;
5639
5640 case bp_dprintf:
5641 if (bs->stop)
5642 this_action = BPSTAT_WHAT_STOP_SILENT;
5643 else
5644 this_action = BPSTAT_WHAT_SINGLE;
5645 break;
5646
5647 default:
5648 internal_error (__FILE__, __LINE__,
5649 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5650 }
5651
5652 retval.main_action = std::max (retval.main_action, this_action);
5653 }
5654
5655 return retval;
5656 }
5657
5658 void
5659 bpstat_run_callbacks (bpstat bs_head)
5660 {
5661 bpstat bs;
5662
5663 for (bs = bs_head; bs != NULL; bs = bs->next)
5664 {
5665 struct breakpoint *b = bs->breakpoint_at;
5666
5667 if (b == NULL)
5668 continue;
5669 switch (b->type)
5670 {
5671 case bp_jit_event:
5672 handle_jit_event ();
5673 break;
5674 case bp_gnu_ifunc_resolver:
5675 gnu_ifunc_resolver_stop (b);
5676 break;
5677 case bp_gnu_ifunc_resolver_return:
5678 gnu_ifunc_resolver_return_stop (b);
5679 break;
5680 }
5681 }
5682 }
5683
5684 /* See breakpoint.h. */
5685
5686 bool
5687 bpstat_should_step ()
5688 {
5689 struct breakpoint *b;
5690
5691 ALL_BREAKPOINTS (b)
5692 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5693 return true;
5694 return false;
5695 }
5696
5697 /* See breakpoint.h. */
5698
5699 bool
5700 bpstat_causes_stop (bpstat bs)
5701 {
5702 for (; bs != NULL; bs = bs->next)
5703 if (bs->stop)
5704 return true;
5705
5706 return false;
5707 }
5708
5709 \f
5710
5711 /* Compute a string of spaces suitable to indent the next line
5712 so it starts at the position corresponding to the table column
5713 named COL_NAME in the currently active table of UIOUT. */
5714
5715 static char *
5716 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5717 {
5718 static char wrap_indent[80];
5719 int i, total_width, width, align;
5720 const char *text;
5721
5722 total_width = 0;
5723 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5724 {
5725 if (strcmp (text, col_name) == 0)
5726 {
5727 gdb_assert (total_width < sizeof wrap_indent);
5728 memset (wrap_indent, ' ', total_width);
5729 wrap_indent[total_width] = 0;
5730
5731 return wrap_indent;
5732 }
5733
5734 total_width += width + 1;
5735 }
5736
5737 return NULL;
5738 }
5739
5740 /* Determine if the locations of this breakpoint will have their conditions
5741 evaluated by the target, host or a mix of both. Returns the following:
5742
5743 "host": Host evals condition.
5744 "host or target": Host or Target evals condition.
5745 "target": Target evals condition.
5746 */
5747
5748 static const char *
5749 bp_condition_evaluator (struct breakpoint *b)
5750 {
5751 struct bp_location *bl;
5752 char host_evals = 0;
5753 char target_evals = 0;
5754
5755 if (!b)
5756 return NULL;
5757
5758 if (!is_breakpoint (b))
5759 return NULL;
5760
5761 if (gdb_evaluates_breakpoint_condition_p ()
5762 || !target_supports_evaluation_of_breakpoint_conditions ())
5763 return condition_evaluation_host;
5764
5765 for (bl = b->loc; bl; bl = bl->next)
5766 {
5767 if (bl->cond_bytecode)
5768 target_evals++;
5769 else
5770 host_evals++;
5771 }
5772
5773 if (host_evals && target_evals)
5774 return condition_evaluation_both;
5775 else if (target_evals)
5776 return condition_evaluation_target;
5777 else
5778 return condition_evaluation_host;
5779 }
5780
5781 /* Determine the breakpoint location's condition evaluator. This is
5782 similar to bp_condition_evaluator, but for locations. */
5783
5784 static const char *
5785 bp_location_condition_evaluator (struct bp_location *bl)
5786 {
5787 if (bl && !is_breakpoint (bl->owner))
5788 return NULL;
5789
5790 if (gdb_evaluates_breakpoint_condition_p ()
5791 || !target_supports_evaluation_of_breakpoint_conditions ())
5792 return condition_evaluation_host;
5793
5794 if (bl && bl->cond_bytecode)
5795 return condition_evaluation_target;
5796 else
5797 return condition_evaluation_host;
5798 }
5799
5800 /* Print the LOC location out of the list of B->LOC locations. */
5801
5802 static void
5803 print_breakpoint_location (struct breakpoint *b,
5804 struct bp_location *loc)
5805 {
5806 struct ui_out *uiout = current_uiout;
5807
5808 scoped_restore_current_program_space restore_pspace;
5809
5810 if (loc != NULL && loc->shlib_disabled)
5811 loc = NULL;
5812
5813 if (loc != NULL)
5814 set_current_program_space (loc->pspace);
5815
5816 if (b->display_canonical)
5817 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5818 else if (loc && loc->symtab)
5819 {
5820 const struct symbol *sym = loc->symbol;
5821
5822 if (sym)
5823 {
5824 uiout->text ("in ");
5825 uiout->field_string ("func", sym->print_name (),
5826 function_name_style.style ());
5827 uiout->text (" ");
5828 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5829 uiout->text ("at ");
5830 }
5831 uiout->field_string ("file",
5832 symtab_to_filename_for_display (loc->symtab),
5833 file_name_style.style ());
5834 uiout->text (":");
5835
5836 if (uiout->is_mi_like_p ())
5837 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5838
5839 uiout->field_signed ("line", loc->line_number);
5840 }
5841 else if (loc)
5842 {
5843 string_file stb;
5844
5845 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5846 demangle, "");
5847 uiout->field_stream ("at", stb);
5848 }
5849 else
5850 {
5851 uiout->field_string ("pending",
5852 event_location_to_string (b->location.get ()));
5853 /* If extra_string is available, it could be holding a condition
5854 or dprintf arguments. In either case, make sure it is printed,
5855 too, but only for non-MI streams. */
5856 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5857 {
5858 if (b->type == bp_dprintf)
5859 uiout->text (",");
5860 else
5861 uiout->text (" ");
5862 uiout->text (b->extra_string);
5863 }
5864 }
5865
5866 if (loc && is_breakpoint (b)
5867 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5868 && bp_condition_evaluator (b) == condition_evaluation_both)
5869 {
5870 uiout->text (" (");
5871 uiout->field_string ("evaluated-by",
5872 bp_location_condition_evaluator (loc));
5873 uiout->text (")");
5874 }
5875 }
5876
5877 static const char *
5878 bptype_string (enum bptype type)
5879 {
5880 struct ep_type_description
5881 {
5882 enum bptype type;
5883 const char *description;
5884 };
5885 static struct ep_type_description bptypes[] =
5886 {
5887 {bp_none, "?deleted?"},
5888 {bp_breakpoint, "breakpoint"},
5889 {bp_hardware_breakpoint, "hw breakpoint"},
5890 {bp_single_step, "sw single-step"},
5891 {bp_until, "until"},
5892 {bp_finish, "finish"},
5893 {bp_watchpoint, "watchpoint"},
5894 {bp_hardware_watchpoint, "hw watchpoint"},
5895 {bp_read_watchpoint, "read watchpoint"},
5896 {bp_access_watchpoint, "acc watchpoint"},
5897 {bp_longjmp, "longjmp"},
5898 {bp_longjmp_resume, "longjmp resume"},
5899 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5900 {bp_exception, "exception"},
5901 {bp_exception_resume, "exception resume"},
5902 {bp_step_resume, "step resume"},
5903 {bp_hp_step_resume, "high-priority step resume"},
5904 {bp_watchpoint_scope, "watchpoint scope"},
5905 {bp_call_dummy, "call dummy"},
5906 {bp_std_terminate, "std::terminate"},
5907 {bp_shlib_event, "shlib events"},
5908 {bp_thread_event, "thread events"},
5909 {bp_overlay_event, "overlay events"},
5910 {bp_longjmp_master, "longjmp master"},
5911 {bp_std_terminate_master, "std::terminate master"},
5912 {bp_exception_master, "exception master"},
5913 {bp_catchpoint, "catchpoint"},
5914 {bp_tracepoint, "tracepoint"},
5915 {bp_fast_tracepoint, "fast tracepoint"},
5916 {bp_static_tracepoint, "static tracepoint"},
5917 {bp_dprintf, "dprintf"},
5918 {bp_jit_event, "jit events"},
5919 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5920 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5921 };
5922
5923 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5924 || ((int) type != bptypes[(int) type].type))
5925 internal_error (__FILE__, __LINE__,
5926 _("bptypes table does not describe type #%d."),
5927 (int) type);
5928
5929 return bptypes[(int) type].description;
5930 }
5931
5932 /* For MI, output a field named 'thread-groups' with a list as the value.
5933 For CLI, prefix the list with the string 'inf'. */
5934
5935 static void
5936 output_thread_groups (struct ui_out *uiout,
5937 const char *field_name,
5938 const std::vector<int> &inf_nums,
5939 int mi_only)
5940 {
5941 int is_mi = uiout->is_mi_like_p ();
5942
5943 /* For backward compatibility, don't display inferiors in CLI unless
5944 there are several. Always display them for MI. */
5945 if (!is_mi && mi_only)
5946 return;
5947
5948 ui_out_emit_list list_emitter (uiout, field_name);
5949
5950 for (size_t i = 0; i < inf_nums.size (); i++)
5951 {
5952 if (is_mi)
5953 {
5954 char mi_group[10];
5955
5956 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
5957 uiout->field_string (NULL, mi_group);
5958 }
5959 else
5960 {
5961 if (i == 0)
5962 uiout->text (" inf ");
5963 else
5964 uiout->text (", ");
5965
5966 uiout->text (plongest (inf_nums[i]));
5967 }
5968 }
5969 }
5970
5971 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5972 instead of going via breakpoint_ops::print_one. This makes "maint
5973 info breakpoints" show the software breakpoint locations of
5974 catchpoints, which are considered internal implementation
5975 detail. */
5976
5977 static void
5978 print_one_breakpoint_location (struct breakpoint *b,
5979 struct bp_location *loc,
5980 int loc_number,
5981 struct bp_location **last_loc,
5982 int allflag, bool raw_loc)
5983 {
5984 struct command_line *l;
5985 static char bpenables[] = "nynny";
5986
5987 struct ui_out *uiout = current_uiout;
5988 int header_of_multiple = 0;
5989 int part_of_multiple = (loc != NULL);
5990 struct value_print_options opts;
5991
5992 get_user_print_options (&opts);
5993
5994 gdb_assert (!loc || loc_number != 0);
5995 /* See comment in print_one_breakpoint concerning treatment of
5996 breakpoints with single disabled location. */
5997 if (loc == NULL
5998 && (b->loc != NULL
5999 && (b->loc->next != NULL || !b->loc->enabled)))
6000 header_of_multiple = 1;
6001 if (loc == NULL)
6002 loc = b->loc;
6003
6004 annotate_record ();
6005
6006 /* 1 */
6007 annotate_field (0);
6008 if (part_of_multiple)
6009 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6010 else
6011 uiout->field_signed ("number", b->number);
6012
6013 /* 2 */
6014 annotate_field (1);
6015 if (part_of_multiple)
6016 uiout->field_skip ("type");
6017 else
6018 uiout->field_string ("type", bptype_string (b->type));
6019
6020 /* 3 */
6021 annotate_field (2);
6022 if (part_of_multiple)
6023 uiout->field_skip ("disp");
6024 else
6025 uiout->field_string ("disp", bpdisp_text (b->disposition));
6026
6027 /* 4 */
6028 annotate_field (3);
6029 if (part_of_multiple)
6030 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6031 else
6032 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6033
6034 /* 5 and 6 */
6035 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6036 b->ops->print_one (b, last_loc);
6037 else
6038 {
6039 if (is_watchpoint (b))
6040 {
6041 struct watchpoint *w = (struct watchpoint *) b;
6042
6043 /* Field 4, the address, is omitted (which makes the columns
6044 not line up too nicely with the headers, but the effect
6045 is relatively readable). */
6046 if (opts.addressprint)
6047 uiout->field_skip ("addr");
6048 annotate_field (5);
6049 uiout->field_string ("what", w->exp_string);
6050 }
6051 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6052 || is_ada_exception_catchpoint (b))
6053 {
6054 if (opts.addressprint)
6055 {
6056 annotate_field (4);
6057 if (header_of_multiple)
6058 uiout->field_string ("addr", "<MULTIPLE>",
6059 metadata_style.style ());
6060 else if (b->loc == NULL || loc->shlib_disabled)
6061 uiout->field_string ("addr", "<PENDING>",
6062 metadata_style.style ());
6063 else
6064 uiout->field_core_addr ("addr",
6065 loc->gdbarch, loc->address);
6066 }
6067 annotate_field (5);
6068 if (!header_of_multiple)
6069 print_breakpoint_location (b, loc);
6070 if (b->loc)
6071 *last_loc = b->loc;
6072 }
6073 }
6074
6075 if (loc != NULL && !header_of_multiple)
6076 {
6077 std::vector<int> inf_nums;
6078 int mi_only = 1;
6079
6080 for (inferior *inf : all_inferiors ())
6081 {
6082 if (inf->pspace == loc->pspace)
6083 inf_nums.push_back (inf->num);
6084 }
6085
6086 /* For backward compatibility, don't display inferiors in CLI unless
6087 there are several. Always display for MI. */
6088 if (allflag
6089 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6090 && (program_spaces.size () > 1
6091 || number_of_inferiors () > 1)
6092 /* LOC is for existing B, it cannot be in
6093 moribund_locations and thus having NULL OWNER. */
6094 && loc->owner->type != bp_catchpoint))
6095 mi_only = 0;
6096 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6097 }
6098
6099 if (!part_of_multiple)
6100 {
6101 if (b->thread != -1)
6102 {
6103 /* FIXME: This seems to be redundant and lost here; see the
6104 "stop only in" line a little further down. */
6105 uiout->text (" thread ");
6106 uiout->field_signed ("thread", b->thread);
6107 }
6108 else if (b->task != 0)
6109 {
6110 uiout->text (" task ");
6111 uiout->field_signed ("task", b->task);
6112 }
6113 }
6114
6115 uiout->text ("\n");
6116
6117 if (!part_of_multiple)
6118 b->ops->print_one_detail (b, uiout);
6119
6120 if (part_of_multiple && frame_id_p (b->frame_id))
6121 {
6122 annotate_field (6);
6123 uiout->text ("\tstop only in stack frame at ");
6124 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6125 the frame ID. */
6126 uiout->field_core_addr ("frame",
6127 b->gdbarch, b->frame_id.stack_addr);
6128 uiout->text ("\n");
6129 }
6130
6131 if (!part_of_multiple && b->cond_string)
6132 {
6133 annotate_field (7);
6134 if (is_tracepoint (b))
6135 uiout->text ("\ttrace only if ");
6136 else
6137 uiout->text ("\tstop only if ");
6138 uiout->field_string ("cond", b->cond_string);
6139
6140 /* Print whether the target is doing the breakpoint's condition
6141 evaluation. If GDB is doing the evaluation, don't print anything. */
6142 if (is_breakpoint (b)
6143 && breakpoint_condition_evaluation_mode ()
6144 == condition_evaluation_target)
6145 {
6146 uiout->message (" (%pF evals)",
6147 string_field ("evaluated-by",
6148 bp_condition_evaluator (b)));
6149 }
6150 uiout->text ("\n");
6151 }
6152
6153 if (!part_of_multiple && b->thread != -1)
6154 {
6155 /* FIXME should make an annotation for this. */
6156 uiout->text ("\tstop only in thread ");
6157 if (uiout->is_mi_like_p ())
6158 uiout->field_signed ("thread", b->thread);
6159 else
6160 {
6161 struct thread_info *thr = find_thread_global_id (b->thread);
6162
6163 uiout->field_string ("thread", print_thread_id (thr));
6164 }
6165 uiout->text ("\n");
6166 }
6167
6168 if (!part_of_multiple)
6169 {
6170 if (b->hit_count)
6171 {
6172 /* FIXME should make an annotation for this. */
6173 if (is_catchpoint (b))
6174 uiout->text ("\tcatchpoint");
6175 else if (is_tracepoint (b))
6176 uiout->text ("\ttracepoint");
6177 else
6178 uiout->text ("\tbreakpoint");
6179 uiout->text (" already hit ");
6180 uiout->field_signed ("times", b->hit_count);
6181 if (b->hit_count == 1)
6182 uiout->text (" time\n");
6183 else
6184 uiout->text (" times\n");
6185 }
6186 else
6187 {
6188 /* Output the count also if it is zero, but only if this is mi. */
6189 if (uiout->is_mi_like_p ())
6190 uiout->field_signed ("times", b->hit_count);
6191 }
6192 }
6193
6194 if (!part_of_multiple && b->ignore_count)
6195 {
6196 annotate_field (8);
6197 uiout->message ("\tignore next %pF hits\n",
6198 signed_field ("ignore", b->ignore_count));
6199 }
6200
6201 /* Note that an enable count of 1 corresponds to "enable once"
6202 behavior, which is reported by the combination of enablement and
6203 disposition, so we don't need to mention it here. */
6204 if (!part_of_multiple && b->enable_count > 1)
6205 {
6206 annotate_field (8);
6207 uiout->text ("\tdisable after ");
6208 /* Tweak the wording to clarify that ignore and enable counts
6209 are distinct, and have additive effect. */
6210 if (b->ignore_count)
6211 uiout->text ("additional ");
6212 else
6213 uiout->text ("next ");
6214 uiout->field_signed ("enable", b->enable_count);
6215 uiout->text (" hits\n");
6216 }
6217
6218 if (!part_of_multiple && is_tracepoint (b))
6219 {
6220 struct tracepoint *tp = (struct tracepoint *) b;
6221
6222 if (tp->traceframe_usage)
6223 {
6224 uiout->text ("\ttrace buffer usage ");
6225 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6226 uiout->text (" bytes\n");
6227 }
6228 }
6229
6230 l = b->commands ? b->commands.get () : NULL;
6231 if (!part_of_multiple && l)
6232 {
6233 annotate_field (9);
6234 ui_out_emit_tuple tuple_emitter (uiout, "script");
6235 print_command_lines (uiout, l, 4);
6236 }
6237
6238 if (is_tracepoint (b))
6239 {
6240 struct tracepoint *t = (struct tracepoint *) b;
6241
6242 if (!part_of_multiple && t->pass_count)
6243 {
6244 annotate_field (10);
6245 uiout->text ("\tpass count ");
6246 uiout->field_signed ("pass", t->pass_count);
6247 uiout->text (" \n");
6248 }
6249
6250 /* Don't display it when tracepoint or tracepoint location is
6251 pending. */
6252 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6253 {
6254 annotate_field (11);
6255
6256 if (uiout->is_mi_like_p ())
6257 uiout->field_string ("installed",
6258 loc->inserted ? "y" : "n");
6259 else
6260 {
6261 if (loc->inserted)
6262 uiout->text ("\t");
6263 else
6264 uiout->text ("\tnot ");
6265 uiout->text ("installed on target\n");
6266 }
6267 }
6268 }
6269
6270 if (uiout->is_mi_like_p () && !part_of_multiple)
6271 {
6272 if (is_watchpoint (b))
6273 {
6274 struct watchpoint *w = (struct watchpoint *) b;
6275
6276 uiout->field_string ("original-location", w->exp_string);
6277 }
6278 else if (b->location != NULL
6279 && event_location_to_string (b->location.get ()) != NULL)
6280 uiout->field_string ("original-location",
6281 event_location_to_string (b->location.get ()));
6282 }
6283 }
6284
6285 /* See breakpoint.h. */
6286
6287 bool fix_multi_location_breakpoint_output_globally = false;
6288
6289 static void
6290 print_one_breakpoint (struct breakpoint *b,
6291 struct bp_location **last_loc,
6292 int allflag)
6293 {
6294 struct ui_out *uiout = current_uiout;
6295 bool use_fixed_output
6296 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6297 || fix_multi_location_breakpoint_output_globally);
6298
6299 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6300 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6301
6302 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6303 are outside. */
6304 if (!use_fixed_output)
6305 bkpt_tuple_emitter.reset ();
6306
6307 /* If this breakpoint has custom print function,
6308 it's already printed. Otherwise, print individual
6309 locations, if any. */
6310 if (b->ops == NULL
6311 || b->ops->print_one == NULL
6312 || allflag)
6313 {
6314 /* If breakpoint has a single location that is disabled, we
6315 print it as if it had several locations, since otherwise it's
6316 hard to represent "breakpoint enabled, location disabled"
6317 situation.
6318
6319 Note that while hardware watchpoints have several locations
6320 internally, that's not a property exposed to users.
6321
6322 Likewise, while catchpoints may be implemented with
6323 breakpoints (e.g., catch throw), that's not a property
6324 exposed to users. We do however display the internal
6325 breakpoint locations with "maint info breakpoints". */
6326 if (!is_hardware_watchpoint (b)
6327 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6328 || is_ada_exception_catchpoint (b))
6329 && (allflag
6330 || (b->loc && (b->loc->next || !b->loc->enabled))))
6331 {
6332 gdb::optional<ui_out_emit_list> locations_list;
6333
6334 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6335 MI record. For later versions, place breakpoint locations in a
6336 list. */
6337 if (uiout->is_mi_like_p () && use_fixed_output)
6338 locations_list.emplace (uiout, "locations");
6339
6340 int n = 1;
6341 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6342 {
6343 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6344 print_one_breakpoint_location (b, loc, n, last_loc,
6345 allflag, allflag);
6346 }
6347 }
6348 }
6349 }
6350
6351 static int
6352 breakpoint_address_bits (struct breakpoint *b)
6353 {
6354 int print_address_bits = 0;
6355 struct bp_location *loc;
6356
6357 /* Software watchpoints that aren't watching memory don't have an
6358 address to print. */
6359 if (is_no_memory_software_watchpoint (b))
6360 return 0;
6361
6362 for (loc = b->loc; loc; loc = loc->next)
6363 {
6364 int addr_bit;
6365
6366 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6367 if (addr_bit > print_address_bits)
6368 print_address_bits = addr_bit;
6369 }
6370
6371 return print_address_bits;
6372 }
6373
6374 /* See breakpoint.h. */
6375
6376 void
6377 print_breakpoint (breakpoint *b)
6378 {
6379 struct bp_location *dummy_loc = NULL;
6380 print_one_breakpoint (b, &dummy_loc, 0);
6381 }
6382
6383 /* Return true if this breakpoint was set by the user, false if it is
6384 internal or momentary. */
6385
6386 int
6387 user_breakpoint_p (struct breakpoint *b)
6388 {
6389 return b->number > 0;
6390 }
6391
6392 /* See breakpoint.h. */
6393
6394 int
6395 pending_breakpoint_p (struct breakpoint *b)
6396 {
6397 return b->loc == NULL;
6398 }
6399
6400 /* Print information on breakpoints (including watchpoints and tracepoints).
6401
6402 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6403 understood by number_or_range_parser. Only breakpoints included in this
6404 list are then printed.
6405
6406 If SHOW_INTERNAL is true, print internal breakpoints.
6407
6408 If FILTER is non-NULL, call it on each breakpoint and only include the
6409 ones for which it returns true.
6410
6411 Return the total number of breakpoints listed. */
6412
6413 static int
6414 breakpoint_1 (const char *bp_num_list, bool show_internal,
6415 bool (*filter) (const struct breakpoint *))
6416 {
6417 struct breakpoint *b;
6418 struct bp_location *last_loc = NULL;
6419 int nr_printable_breakpoints;
6420 struct value_print_options opts;
6421 int print_address_bits = 0;
6422 int print_type_col_width = 14;
6423 struct ui_out *uiout = current_uiout;
6424
6425 get_user_print_options (&opts);
6426
6427 /* Compute the number of rows in the table, as well as the size
6428 required for address fields. */
6429 nr_printable_breakpoints = 0;
6430 ALL_BREAKPOINTS (b)
6431 {
6432 /* If we have a filter, only list the breakpoints it accepts. */
6433 if (filter && !filter (b))
6434 continue;
6435
6436 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6437 accept. Skip the others. */
6438 if (bp_num_list != NULL && *bp_num_list != '\0')
6439 {
6440 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6441 continue;
6442 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6443 continue;
6444 }
6445
6446 if (show_internal || user_breakpoint_p (b))
6447 {
6448 int addr_bit, type_len;
6449
6450 addr_bit = breakpoint_address_bits (b);
6451 if (addr_bit > print_address_bits)
6452 print_address_bits = addr_bit;
6453
6454 type_len = strlen (bptype_string (b->type));
6455 if (type_len > print_type_col_width)
6456 print_type_col_width = type_len;
6457
6458 nr_printable_breakpoints++;
6459 }
6460 }
6461
6462 {
6463 ui_out_emit_table table_emitter (uiout,
6464 opts.addressprint ? 6 : 5,
6465 nr_printable_breakpoints,
6466 "BreakpointTable");
6467
6468 if (nr_printable_breakpoints > 0)
6469 annotate_breakpoints_headers ();
6470 if (nr_printable_breakpoints > 0)
6471 annotate_field (0);
6472 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6473 if (nr_printable_breakpoints > 0)
6474 annotate_field (1);
6475 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6476 if (nr_printable_breakpoints > 0)
6477 annotate_field (2);
6478 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6479 if (nr_printable_breakpoints > 0)
6480 annotate_field (3);
6481 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6482 if (opts.addressprint)
6483 {
6484 if (nr_printable_breakpoints > 0)
6485 annotate_field (4);
6486 if (print_address_bits <= 32)
6487 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6488 else
6489 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6490 }
6491 if (nr_printable_breakpoints > 0)
6492 annotate_field (5);
6493 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6494 uiout->table_body ();
6495 if (nr_printable_breakpoints > 0)
6496 annotate_breakpoints_table ();
6497
6498 ALL_BREAKPOINTS (b)
6499 {
6500 QUIT;
6501 /* If we have a filter, only list the breakpoints it accepts. */
6502 if (filter && !filter (b))
6503 continue;
6504
6505 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6506 accept. Skip the others. */
6507
6508 if (bp_num_list != NULL && *bp_num_list != '\0')
6509 {
6510 if (show_internal) /* maintenance info breakpoint */
6511 {
6512 if (parse_and_eval_long (bp_num_list) != b->number)
6513 continue;
6514 }
6515 else /* all others */
6516 {
6517 if (!number_is_in_list (bp_num_list, b->number))
6518 continue;
6519 }
6520 }
6521 /* We only print out user settable breakpoints unless the
6522 show_internal is set. */
6523 if (show_internal || user_breakpoint_p (b))
6524 print_one_breakpoint (b, &last_loc, show_internal);
6525 }
6526 }
6527
6528 if (nr_printable_breakpoints == 0)
6529 {
6530 /* If there's a filter, let the caller decide how to report
6531 empty list. */
6532 if (!filter)
6533 {
6534 if (bp_num_list == NULL || *bp_num_list == '\0')
6535 uiout->message ("No breakpoints or watchpoints.\n");
6536 else
6537 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6538 bp_num_list);
6539 }
6540 }
6541 else
6542 {
6543 if (last_loc && !server_command)
6544 set_next_address (last_loc->gdbarch, last_loc->address);
6545 }
6546
6547 /* FIXME? Should this be moved up so that it is only called when
6548 there have been breakpoints? */
6549 annotate_breakpoints_table_end ();
6550
6551 return nr_printable_breakpoints;
6552 }
6553
6554 /* Display the value of default-collect in a way that is generally
6555 compatible with the breakpoint list. */
6556
6557 static void
6558 default_collect_info (void)
6559 {
6560 struct ui_out *uiout = current_uiout;
6561
6562 /* If it has no value (which is frequently the case), say nothing; a
6563 message like "No default-collect." gets in user's face when it's
6564 not wanted. */
6565 if (!*default_collect)
6566 return;
6567
6568 /* The following phrase lines up nicely with per-tracepoint collect
6569 actions. */
6570 uiout->text ("default collect ");
6571 uiout->field_string ("default-collect", default_collect);
6572 uiout->text (" \n");
6573 }
6574
6575 static void
6576 info_breakpoints_command (const char *args, int from_tty)
6577 {
6578 breakpoint_1 (args, false, NULL);
6579
6580 default_collect_info ();
6581 }
6582
6583 static void
6584 info_watchpoints_command (const char *args, int from_tty)
6585 {
6586 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6587 struct ui_out *uiout = current_uiout;
6588
6589 if (num_printed == 0)
6590 {
6591 if (args == NULL || *args == '\0')
6592 uiout->message ("No watchpoints.\n");
6593 else
6594 uiout->message ("No watchpoint matching '%s'.\n", args);
6595 }
6596 }
6597
6598 static void
6599 maintenance_info_breakpoints (const char *args, int from_tty)
6600 {
6601 breakpoint_1 (args, true, NULL);
6602
6603 default_collect_info ();
6604 }
6605
6606 static int
6607 breakpoint_has_pc (struct breakpoint *b,
6608 struct program_space *pspace,
6609 CORE_ADDR pc, struct obj_section *section)
6610 {
6611 struct bp_location *bl = b->loc;
6612
6613 for (; bl; bl = bl->next)
6614 {
6615 if (bl->pspace == pspace
6616 && bl->address == pc
6617 && (!overlay_debugging || bl->section == section))
6618 return 1;
6619 }
6620 return 0;
6621 }
6622
6623 /* Print a message describing any user-breakpoints set at PC. This
6624 concerns with logical breakpoints, so we match program spaces, not
6625 address spaces. */
6626
6627 static void
6628 describe_other_breakpoints (struct gdbarch *gdbarch,
6629 struct program_space *pspace, CORE_ADDR pc,
6630 struct obj_section *section, int thread)
6631 {
6632 int others = 0;
6633 struct breakpoint *b;
6634
6635 ALL_BREAKPOINTS (b)
6636 others += (user_breakpoint_p (b)
6637 && breakpoint_has_pc (b, pspace, pc, section));
6638 if (others > 0)
6639 {
6640 if (others == 1)
6641 printf_filtered (_("Note: breakpoint "));
6642 else /* if (others == ???) */
6643 printf_filtered (_("Note: breakpoints "));
6644 ALL_BREAKPOINTS (b)
6645 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6646 {
6647 others--;
6648 printf_filtered ("%d", b->number);
6649 if (b->thread == -1 && thread != -1)
6650 printf_filtered (" (all threads)");
6651 else if (b->thread != -1)
6652 printf_filtered (" (thread %d)", b->thread);
6653 printf_filtered ("%s%s ",
6654 ((b->enable_state == bp_disabled
6655 || b->enable_state == bp_call_disabled)
6656 ? " (disabled)"
6657 : ""),
6658 (others > 1) ? ","
6659 : ((others == 1) ? " and" : ""));
6660 }
6661 current_uiout->message (_("also set at pc %ps.\n"),
6662 styled_string (address_style.style (),
6663 paddress (gdbarch, pc)));
6664 }
6665 }
6666 \f
6667
6668 /* Return true iff it is meaningful to use the address member of LOC.
6669 For some breakpoint types, the locations' address members are
6670 irrelevant and it makes no sense to attempt to compare them to
6671 other addresses (or use them for any other purpose either).
6672
6673 More specifically, software watchpoints and catchpoints that are
6674 not backed by breakpoints always have a zero valued location
6675 address and we don't want to mark breakpoints of any of these types
6676 to be a duplicate of an actual breakpoint location at address
6677 zero. */
6678
6679 static bool
6680 bl_address_is_meaningful (bp_location *loc)
6681 {
6682 return loc->loc_type != bp_loc_other;
6683 }
6684
6685 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6686 true if LOC1 and LOC2 represent the same watchpoint location. */
6687
6688 static int
6689 watchpoint_locations_match (struct bp_location *loc1,
6690 struct bp_location *loc2)
6691 {
6692 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6693 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6694
6695 /* Both of them must exist. */
6696 gdb_assert (w1 != NULL);
6697 gdb_assert (w2 != NULL);
6698
6699 /* If the target can evaluate the condition expression in hardware,
6700 then we we need to insert both watchpoints even if they are at
6701 the same place. Otherwise the watchpoint will only trigger when
6702 the condition of whichever watchpoint was inserted evaluates to
6703 true, not giving a chance for GDB to check the condition of the
6704 other watchpoint. */
6705 if ((w1->cond_exp
6706 && target_can_accel_watchpoint_condition (loc1->address,
6707 loc1->length,
6708 loc1->watchpoint_type,
6709 w1->cond_exp.get ()))
6710 || (w2->cond_exp
6711 && target_can_accel_watchpoint_condition (loc2->address,
6712 loc2->length,
6713 loc2->watchpoint_type,
6714 w2->cond_exp.get ())))
6715 return 0;
6716
6717 /* Note that this checks the owner's type, not the location's. In
6718 case the target does not support read watchpoints, but does
6719 support access watchpoints, we'll have bp_read_watchpoint
6720 watchpoints with hw_access locations. Those should be considered
6721 duplicates of hw_read locations. The hw_read locations will
6722 become hw_access locations later. */
6723 return (loc1->owner->type == loc2->owner->type
6724 && loc1->pspace->aspace == loc2->pspace->aspace
6725 && loc1->address == loc2->address
6726 && loc1->length == loc2->length);
6727 }
6728
6729 /* See breakpoint.h. */
6730
6731 int
6732 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6733 const address_space *aspace2, CORE_ADDR addr2)
6734 {
6735 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6736 || aspace1 == aspace2)
6737 && addr1 == addr2);
6738 }
6739
6740 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6741 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6742 matches ASPACE2. On targets that have global breakpoints, the address
6743 space doesn't really matter. */
6744
6745 static int
6746 breakpoint_address_match_range (const address_space *aspace1,
6747 CORE_ADDR addr1,
6748 int len1, const address_space *aspace2,
6749 CORE_ADDR addr2)
6750 {
6751 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6752 || aspace1 == aspace2)
6753 && addr2 >= addr1 && addr2 < addr1 + len1);
6754 }
6755
6756 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6757 a ranged breakpoint. In most targets, a match happens only if ASPACE
6758 matches the breakpoint's address space. On targets that have global
6759 breakpoints, the address space doesn't really matter. */
6760
6761 static int
6762 breakpoint_location_address_match (struct bp_location *bl,
6763 const address_space *aspace,
6764 CORE_ADDR addr)
6765 {
6766 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6767 aspace, addr)
6768 || (bl->length
6769 && breakpoint_address_match_range (bl->pspace->aspace,
6770 bl->address, bl->length,
6771 aspace, addr)));
6772 }
6773
6774 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6775 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6776 match happens only if ASPACE matches the breakpoint's address
6777 space. On targets that have global breakpoints, the address space
6778 doesn't really matter. */
6779
6780 static int
6781 breakpoint_location_address_range_overlap (struct bp_location *bl,
6782 const address_space *aspace,
6783 CORE_ADDR addr, int len)
6784 {
6785 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6786 || bl->pspace->aspace == aspace)
6787 {
6788 int bl_len = bl->length != 0 ? bl->length : 1;
6789
6790 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6791 return 1;
6792 }
6793 return 0;
6794 }
6795
6796 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6797 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6798 true, otherwise returns false. */
6799
6800 static int
6801 tracepoint_locations_match (struct bp_location *loc1,
6802 struct bp_location *loc2)
6803 {
6804 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6805 /* Since tracepoint locations are never duplicated with others', tracepoint
6806 locations at the same address of different tracepoints are regarded as
6807 different locations. */
6808 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6809 else
6810 return 0;
6811 }
6812
6813 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6814 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6815 the same location. If SW_HW_BPS_MATCH is true, then software
6816 breakpoint locations and hardware breakpoint locations match,
6817 otherwise they don't. */
6818
6819 static int
6820 breakpoint_locations_match (struct bp_location *loc1,
6821 struct bp_location *loc2,
6822 bool sw_hw_bps_match)
6823 {
6824 int hw_point1, hw_point2;
6825
6826 /* Both of them must not be in moribund_locations. */
6827 gdb_assert (loc1->owner != NULL);
6828 gdb_assert (loc2->owner != NULL);
6829
6830 hw_point1 = is_hardware_watchpoint (loc1->owner);
6831 hw_point2 = is_hardware_watchpoint (loc2->owner);
6832
6833 if (hw_point1 != hw_point2)
6834 return 0;
6835 else if (hw_point1)
6836 return watchpoint_locations_match (loc1, loc2);
6837 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6838 return tracepoint_locations_match (loc1, loc2);
6839 else
6840 /* We compare bp_location.length in order to cover ranged
6841 breakpoints. Keep this in sync with
6842 bp_location_is_less_than. */
6843 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6844 loc2->pspace->aspace, loc2->address)
6845 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
6846 && loc1->length == loc2->length);
6847 }
6848
6849 static void
6850 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6851 int bnum, int have_bnum)
6852 {
6853 /* The longest string possibly returned by hex_string_custom
6854 is 50 chars. These must be at least that big for safety. */
6855 char astr1[64];
6856 char astr2[64];
6857
6858 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6859 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6860 if (have_bnum)
6861 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6862 bnum, astr1, astr2);
6863 else
6864 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6865 }
6866
6867 /* Adjust a breakpoint's address to account for architectural
6868 constraints on breakpoint placement. Return the adjusted address.
6869 Note: Very few targets require this kind of adjustment. For most
6870 targets, this function is simply the identity function. */
6871
6872 static CORE_ADDR
6873 adjust_breakpoint_address (struct gdbarch *gdbarch,
6874 CORE_ADDR bpaddr, enum bptype bptype)
6875 {
6876 if (bptype == bp_watchpoint
6877 || bptype == bp_hardware_watchpoint
6878 || bptype == bp_read_watchpoint
6879 || bptype == bp_access_watchpoint
6880 || bptype == bp_catchpoint)
6881 {
6882 /* Watchpoints and the various bp_catch_* eventpoints should not
6883 have their addresses modified. */
6884 return bpaddr;
6885 }
6886 else if (bptype == bp_single_step)
6887 {
6888 /* Single-step breakpoints should not have their addresses
6889 modified. If there's any architectural constrain that
6890 applies to this address, then it should have already been
6891 taken into account when the breakpoint was created in the
6892 first place. If we didn't do this, stepping through e.g.,
6893 Thumb-2 IT blocks would break. */
6894 return bpaddr;
6895 }
6896 else
6897 {
6898 CORE_ADDR adjusted_bpaddr = bpaddr;
6899
6900 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6901 {
6902 /* Some targets have architectural constraints on the placement
6903 of breakpoint instructions. Obtain the adjusted address. */
6904 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6905 }
6906
6907 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
6908
6909 /* An adjusted breakpoint address can significantly alter
6910 a user's expectations. Print a warning if an adjustment
6911 is required. */
6912 if (adjusted_bpaddr != bpaddr)
6913 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6914
6915 return adjusted_bpaddr;
6916 }
6917 }
6918
6919 static bp_loc_type
6920 bp_location_from_bp_type (bptype type)
6921 {
6922 switch (type)
6923 {
6924 case bp_breakpoint:
6925 case bp_single_step:
6926 case bp_until:
6927 case bp_finish:
6928 case bp_longjmp:
6929 case bp_longjmp_resume:
6930 case bp_longjmp_call_dummy:
6931 case bp_exception:
6932 case bp_exception_resume:
6933 case bp_step_resume:
6934 case bp_hp_step_resume:
6935 case bp_watchpoint_scope:
6936 case bp_call_dummy:
6937 case bp_std_terminate:
6938 case bp_shlib_event:
6939 case bp_thread_event:
6940 case bp_overlay_event:
6941 case bp_jit_event:
6942 case bp_longjmp_master:
6943 case bp_std_terminate_master:
6944 case bp_exception_master:
6945 case bp_gnu_ifunc_resolver:
6946 case bp_gnu_ifunc_resolver_return:
6947 case bp_dprintf:
6948 return bp_loc_software_breakpoint;
6949 case bp_hardware_breakpoint:
6950 return bp_loc_hardware_breakpoint;
6951 case bp_hardware_watchpoint:
6952 case bp_read_watchpoint:
6953 case bp_access_watchpoint:
6954 return bp_loc_hardware_watchpoint;
6955 case bp_watchpoint:
6956 case bp_catchpoint:
6957 case bp_tracepoint:
6958 case bp_fast_tracepoint:
6959 case bp_static_tracepoint:
6960 return bp_loc_other;
6961 default:
6962 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6963 }
6964 }
6965
6966 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6967 {
6968 this->owner = owner;
6969 this->cond_bytecode = NULL;
6970 this->shlib_disabled = 0;
6971 this->enabled = 1;
6972
6973 this->loc_type = type;
6974
6975 if (this->loc_type == bp_loc_software_breakpoint
6976 || this->loc_type == bp_loc_hardware_breakpoint)
6977 mark_breakpoint_location_modified (this);
6978
6979 this->refc = 1;
6980 }
6981
6982 bp_location::bp_location (breakpoint *owner)
6983 : bp_location::bp_location (owner,
6984 bp_location_from_bp_type (owner->type))
6985 {
6986 }
6987
6988 /* Allocate a struct bp_location. */
6989
6990 static struct bp_location *
6991 allocate_bp_location (struct breakpoint *bpt)
6992 {
6993 return bpt->ops->allocate_location (bpt);
6994 }
6995
6996 static void
6997 free_bp_location (struct bp_location *loc)
6998 {
6999 delete loc;
7000 }
7001
7002 /* Increment reference count. */
7003
7004 static void
7005 incref_bp_location (struct bp_location *bl)
7006 {
7007 ++bl->refc;
7008 }
7009
7010 /* Decrement reference count. If the reference count reaches 0,
7011 destroy the bp_location. Sets *BLP to NULL. */
7012
7013 static void
7014 decref_bp_location (struct bp_location **blp)
7015 {
7016 gdb_assert ((*blp)->refc > 0);
7017
7018 if (--(*blp)->refc == 0)
7019 free_bp_location (*blp);
7020 *blp = NULL;
7021 }
7022
7023 /* Add breakpoint B at the end of the global breakpoint chain. */
7024
7025 static breakpoint *
7026 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7027 {
7028 struct breakpoint *b1;
7029 struct breakpoint *result = b.get ();
7030
7031 /* Add this breakpoint to the end of the chain so that a list of
7032 breakpoints will come out in order of increasing numbers. */
7033
7034 b1 = breakpoint_chain;
7035 if (b1 == 0)
7036 breakpoint_chain = b.release ();
7037 else
7038 {
7039 while (b1->next)
7040 b1 = b1->next;
7041 b1->next = b.release ();
7042 }
7043
7044 return result;
7045 }
7046
7047 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7048
7049 static void
7050 init_raw_breakpoint_without_location (struct breakpoint *b,
7051 struct gdbarch *gdbarch,
7052 enum bptype bptype,
7053 const struct breakpoint_ops *ops)
7054 {
7055 gdb_assert (ops != NULL);
7056
7057 b->ops = ops;
7058 b->type = bptype;
7059 b->gdbarch = gdbarch;
7060 b->language = current_language->la_language;
7061 b->input_radix = input_radix;
7062 b->related_breakpoint = b;
7063 }
7064
7065 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7066 that has type BPTYPE and has no locations as yet. */
7067
7068 static struct breakpoint *
7069 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7070 enum bptype bptype,
7071 const struct breakpoint_ops *ops)
7072 {
7073 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7074
7075 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7076 return add_to_breakpoint_chain (std::move (b));
7077 }
7078
7079 /* Initialize loc->function_name. */
7080
7081 static void
7082 set_breakpoint_location_function (struct bp_location *loc)
7083 {
7084 gdb_assert (loc->owner != NULL);
7085
7086 if (loc->owner->type == bp_breakpoint
7087 || loc->owner->type == bp_hardware_breakpoint
7088 || is_tracepoint (loc->owner))
7089 {
7090 const char *function_name;
7091
7092 if (loc->msymbol != NULL
7093 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7094 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7095 {
7096 struct breakpoint *b = loc->owner;
7097
7098 function_name = loc->msymbol->linkage_name ();
7099
7100 if (b->type == bp_breakpoint && b->loc == loc
7101 && loc->next == NULL && b->related_breakpoint == b)
7102 {
7103 /* Create only the whole new breakpoint of this type but do not
7104 mess more complicated breakpoints with multiple locations. */
7105 b->type = bp_gnu_ifunc_resolver;
7106 /* Remember the resolver's address for use by the return
7107 breakpoint. */
7108 loc->related_address = loc->address;
7109 }
7110 }
7111 else
7112 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7113
7114 if (function_name)
7115 loc->function_name = xstrdup (function_name);
7116 }
7117 }
7118
7119 /* Attempt to determine architecture of location identified by SAL. */
7120 struct gdbarch *
7121 get_sal_arch (struct symtab_and_line sal)
7122 {
7123 if (sal.section)
7124 return sal.section->objfile->arch ();
7125 if (sal.symtab)
7126 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7127
7128 return NULL;
7129 }
7130
7131 /* Low level routine for partially initializing a breakpoint of type
7132 BPTYPE. The newly created breakpoint's address, section, source
7133 file name, and line number are provided by SAL.
7134
7135 It is expected that the caller will complete the initialization of
7136 the newly created breakpoint struct as well as output any status
7137 information regarding the creation of a new breakpoint. */
7138
7139 static void
7140 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7141 struct symtab_and_line sal, enum bptype bptype,
7142 const struct breakpoint_ops *ops)
7143 {
7144 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7145
7146 add_location_to_breakpoint (b, &sal);
7147
7148 if (bptype != bp_catchpoint)
7149 gdb_assert (sal.pspace != NULL);
7150
7151 /* Store the program space that was used to set the breakpoint,
7152 except for ordinary breakpoints, which are independent of the
7153 program space. */
7154 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7155 b->pspace = sal.pspace;
7156 }
7157
7158 /* set_raw_breakpoint is a low level routine for allocating and
7159 partially initializing a breakpoint of type BPTYPE. The newly
7160 created breakpoint's address, section, source file name, and line
7161 number are provided by SAL. The newly created and partially
7162 initialized breakpoint is added to the breakpoint chain and
7163 is also returned as the value of this function.
7164
7165 It is expected that the caller will complete the initialization of
7166 the newly created breakpoint struct as well as output any status
7167 information regarding the creation of a new breakpoint. In
7168 particular, set_raw_breakpoint does NOT set the breakpoint
7169 number! Care should be taken to not allow an error to occur
7170 prior to completing the initialization of the breakpoint. If this
7171 should happen, a bogus breakpoint will be left on the chain. */
7172
7173 struct breakpoint *
7174 set_raw_breakpoint (struct gdbarch *gdbarch,
7175 struct symtab_and_line sal, enum bptype bptype,
7176 const struct breakpoint_ops *ops)
7177 {
7178 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7179
7180 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7181 return add_to_breakpoint_chain (std::move (b));
7182 }
7183
7184 /* Call this routine when stepping and nexting to enable a breakpoint
7185 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7186 initiated the operation. */
7187
7188 void
7189 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7190 {
7191 struct breakpoint *b, *b_tmp;
7192 int thread = tp->global_num;
7193
7194 /* To avoid having to rescan all objfile symbols at every step,
7195 we maintain a list of continually-inserted but always disabled
7196 longjmp "master" breakpoints. Here, we simply create momentary
7197 clones of those and enable them for the requested thread. */
7198 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7199 if (b->pspace == current_program_space
7200 && (b->type == bp_longjmp_master
7201 || b->type == bp_exception_master))
7202 {
7203 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7204 struct breakpoint *clone;
7205
7206 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7207 after their removal. */
7208 clone = momentary_breakpoint_from_master (b, type,
7209 &momentary_breakpoint_ops, 1);
7210 clone->thread = thread;
7211 }
7212
7213 tp->initiating_frame = frame;
7214 }
7215
7216 /* Delete all longjmp breakpoints from THREAD. */
7217 void
7218 delete_longjmp_breakpoint (int thread)
7219 {
7220 struct breakpoint *b, *b_tmp;
7221
7222 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7223 if (b->type == bp_longjmp || b->type == bp_exception)
7224 {
7225 if (b->thread == thread)
7226 delete_breakpoint (b);
7227 }
7228 }
7229
7230 void
7231 delete_longjmp_breakpoint_at_next_stop (int thread)
7232 {
7233 struct breakpoint *b, *b_tmp;
7234
7235 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7236 if (b->type == bp_longjmp || b->type == bp_exception)
7237 {
7238 if (b->thread == thread)
7239 b->disposition = disp_del_at_next_stop;
7240 }
7241 }
7242
7243 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7244 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7245 pointer to any of them. Return NULL if this system cannot place longjmp
7246 breakpoints. */
7247
7248 struct breakpoint *
7249 set_longjmp_breakpoint_for_call_dummy (void)
7250 {
7251 struct breakpoint *b, *retval = NULL;
7252
7253 ALL_BREAKPOINTS (b)
7254 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7255 {
7256 struct breakpoint *new_b;
7257
7258 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7259 &momentary_breakpoint_ops,
7260 1);
7261 new_b->thread = inferior_thread ()->global_num;
7262
7263 /* Link NEW_B into the chain of RETVAL breakpoints. */
7264
7265 gdb_assert (new_b->related_breakpoint == new_b);
7266 if (retval == NULL)
7267 retval = new_b;
7268 new_b->related_breakpoint = retval;
7269 while (retval->related_breakpoint != new_b->related_breakpoint)
7270 retval = retval->related_breakpoint;
7271 retval->related_breakpoint = new_b;
7272 }
7273
7274 return retval;
7275 }
7276
7277 /* Verify all existing dummy frames and their associated breakpoints for
7278 TP. Remove those which can no longer be found in the current frame
7279 stack.
7280
7281 You should call this function only at places where it is safe to currently
7282 unwind the whole stack. Failed stack unwind would discard live dummy
7283 frames. */
7284
7285 void
7286 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7287 {
7288 struct breakpoint *b, *b_tmp;
7289
7290 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7291 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7292 {
7293 struct breakpoint *dummy_b = b->related_breakpoint;
7294
7295 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7296 dummy_b = dummy_b->related_breakpoint;
7297 if (dummy_b->type != bp_call_dummy
7298 || frame_find_by_id (dummy_b->frame_id) != NULL)
7299 continue;
7300
7301 dummy_frame_discard (dummy_b->frame_id, tp);
7302
7303 while (b->related_breakpoint != b)
7304 {
7305 if (b_tmp == b->related_breakpoint)
7306 b_tmp = b->related_breakpoint->next;
7307 delete_breakpoint (b->related_breakpoint);
7308 }
7309 delete_breakpoint (b);
7310 }
7311 }
7312
7313 void
7314 enable_overlay_breakpoints (void)
7315 {
7316 struct breakpoint *b;
7317
7318 ALL_BREAKPOINTS (b)
7319 if (b->type == bp_overlay_event)
7320 {
7321 b->enable_state = bp_enabled;
7322 update_global_location_list (UGLL_MAY_INSERT);
7323 overlay_events_enabled = 1;
7324 }
7325 }
7326
7327 void
7328 disable_overlay_breakpoints (void)
7329 {
7330 struct breakpoint *b;
7331
7332 ALL_BREAKPOINTS (b)
7333 if (b->type == bp_overlay_event)
7334 {
7335 b->enable_state = bp_disabled;
7336 update_global_location_list (UGLL_DONT_INSERT);
7337 overlay_events_enabled = 0;
7338 }
7339 }
7340
7341 /* Set an active std::terminate breakpoint for each std::terminate
7342 master breakpoint. */
7343 void
7344 set_std_terminate_breakpoint (void)
7345 {
7346 struct breakpoint *b, *b_tmp;
7347
7348 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7349 if (b->pspace == current_program_space
7350 && b->type == bp_std_terminate_master)
7351 {
7352 momentary_breakpoint_from_master (b, bp_std_terminate,
7353 &momentary_breakpoint_ops, 1);
7354 }
7355 }
7356
7357 /* Delete all the std::terminate breakpoints. */
7358 void
7359 delete_std_terminate_breakpoint (void)
7360 {
7361 struct breakpoint *b, *b_tmp;
7362
7363 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7364 if (b->type == bp_std_terminate)
7365 delete_breakpoint (b);
7366 }
7367
7368 struct breakpoint *
7369 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7370 {
7371 struct breakpoint *b;
7372
7373 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7374 &internal_breakpoint_ops);
7375
7376 b->enable_state = bp_enabled;
7377 /* location has to be used or breakpoint_re_set will delete me. */
7378 b->location = new_address_location (b->loc->address, NULL, 0);
7379
7380 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7381
7382 return b;
7383 }
7384
7385 struct lang_and_radix
7386 {
7387 enum language lang;
7388 int radix;
7389 };
7390
7391 /* Create a breakpoint for JIT code registration and unregistration. */
7392
7393 struct breakpoint *
7394 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7395 {
7396 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7397 &internal_breakpoint_ops);
7398 }
7399
7400 /* Remove JIT code registration and unregistration breakpoint(s). */
7401
7402 void
7403 remove_jit_event_breakpoints (void)
7404 {
7405 struct breakpoint *b, *b_tmp;
7406
7407 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7408 if (b->type == bp_jit_event
7409 && b->loc->pspace == current_program_space)
7410 delete_breakpoint (b);
7411 }
7412
7413 void
7414 remove_solib_event_breakpoints (void)
7415 {
7416 struct breakpoint *b, *b_tmp;
7417
7418 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7419 if (b->type == bp_shlib_event
7420 && b->loc->pspace == current_program_space)
7421 delete_breakpoint (b);
7422 }
7423
7424 /* See breakpoint.h. */
7425
7426 void
7427 remove_solib_event_breakpoints_at_next_stop (void)
7428 {
7429 struct breakpoint *b, *b_tmp;
7430
7431 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7432 if (b->type == bp_shlib_event
7433 && b->loc->pspace == current_program_space)
7434 b->disposition = disp_del_at_next_stop;
7435 }
7436
7437 /* Helper for create_solib_event_breakpoint /
7438 create_and_insert_solib_event_breakpoint. Allows specifying which
7439 INSERT_MODE to pass through to update_global_location_list. */
7440
7441 static struct breakpoint *
7442 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7443 enum ugll_insert_mode insert_mode)
7444 {
7445 struct breakpoint *b;
7446
7447 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7448 &internal_breakpoint_ops);
7449 update_global_location_list_nothrow (insert_mode);
7450 return b;
7451 }
7452
7453 struct breakpoint *
7454 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7455 {
7456 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7457 }
7458
7459 /* See breakpoint.h. */
7460
7461 struct breakpoint *
7462 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7463 {
7464 struct breakpoint *b;
7465
7466 /* Explicitly tell update_global_location_list to insert
7467 locations. */
7468 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7469 if (!b->loc->inserted)
7470 {
7471 delete_breakpoint (b);
7472 return NULL;
7473 }
7474 return b;
7475 }
7476
7477 /* Disable any breakpoints that are on code in shared libraries. Only
7478 apply to enabled breakpoints, disabled ones can just stay disabled. */
7479
7480 void
7481 disable_breakpoints_in_shlibs (void)
7482 {
7483 struct bp_location *loc, **locp_tmp;
7484
7485 ALL_BP_LOCATIONS (loc, locp_tmp)
7486 {
7487 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7488 struct breakpoint *b = loc->owner;
7489
7490 /* We apply the check to all breakpoints, including disabled for
7491 those with loc->duplicate set. This is so that when breakpoint
7492 becomes enabled, or the duplicate is removed, gdb will try to
7493 insert all breakpoints. If we don't set shlib_disabled here,
7494 we'll try to insert those breakpoints and fail. */
7495 if (((b->type == bp_breakpoint)
7496 || (b->type == bp_jit_event)
7497 || (b->type == bp_hardware_breakpoint)
7498 || (is_tracepoint (b)))
7499 && loc->pspace == current_program_space
7500 && !loc->shlib_disabled
7501 && solib_name_from_address (loc->pspace, loc->address)
7502 )
7503 {
7504 loc->shlib_disabled = 1;
7505 }
7506 }
7507 }
7508
7509 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7510 notification of unloaded_shlib. Only apply to enabled breakpoints,
7511 disabled ones can just stay disabled. */
7512
7513 static void
7514 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7515 {
7516 struct bp_location *loc, **locp_tmp;
7517 int disabled_shlib_breaks = 0;
7518
7519 ALL_BP_LOCATIONS (loc, locp_tmp)
7520 {
7521 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7522 struct breakpoint *b = loc->owner;
7523
7524 if (solib->pspace == loc->pspace
7525 && !loc->shlib_disabled
7526 && (((b->type == bp_breakpoint
7527 || b->type == bp_jit_event
7528 || b->type == bp_hardware_breakpoint)
7529 && (loc->loc_type == bp_loc_hardware_breakpoint
7530 || loc->loc_type == bp_loc_software_breakpoint))
7531 || is_tracepoint (b))
7532 && solib_contains_address_p (solib, loc->address))
7533 {
7534 loc->shlib_disabled = 1;
7535 /* At this point, we cannot rely on remove_breakpoint
7536 succeeding so we must mark the breakpoint as not inserted
7537 to prevent future errors occurring in remove_breakpoints. */
7538 loc->inserted = 0;
7539
7540 /* This may cause duplicate notifications for the same breakpoint. */
7541 gdb::observers::breakpoint_modified.notify (b);
7542
7543 if (!disabled_shlib_breaks)
7544 {
7545 target_terminal::ours_for_output ();
7546 warning (_("Temporarily disabling breakpoints "
7547 "for unloaded shared library \"%s\""),
7548 solib->so_name);
7549 }
7550 disabled_shlib_breaks = 1;
7551 }
7552 }
7553 }
7554
7555 /* Disable any breakpoints and tracepoints in OBJFILE upon
7556 notification of free_objfile. Only apply to enabled breakpoints,
7557 disabled ones can just stay disabled. */
7558
7559 static void
7560 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7561 {
7562 struct breakpoint *b;
7563
7564 if (objfile == NULL)
7565 return;
7566
7567 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7568 managed by the user with add-symbol-file/remove-symbol-file.
7569 Similarly to how breakpoints in shared libraries are handled in
7570 response to "nosharedlibrary", mark breakpoints in such modules
7571 shlib_disabled so they end up uninserted on the next global
7572 location list update. Shared libraries not loaded by the user
7573 aren't handled here -- they're already handled in
7574 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7575 solib_unloaded observer. We skip objfiles that are not
7576 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7577 main objfile). */
7578 if ((objfile->flags & OBJF_SHARED) == 0
7579 || (objfile->flags & OBJF_USERLOADED) == 0)
7580 return;
7581
7582 ALL_BREAKPOINTS (b)
7583 {
7584 struct bp_location *loc;
7585 int bp_modified = 0;
7586
7587 if (!is_breakpoint (b) && !is_tracepoint (b))
7588 continue;
7589
7590 for (loc = b->loc; loc != NULL; loc = loc->next)
7591 {
7592 CORE_ADDR loc_addr = loc->address;
7593
7594 if (loc->loc_type != bp_loc_hardware_breakpoint
7595 && loc->loc_type != bp_loc_software_breakpoint)
7596 continue;
7597
7598 if (loc->shlib_disabled != 0)
7599 continue;
7600
7601 if (objfile->pspace != loc->pspace)
7602 continue;
7603
7604 if (loc->loc_type != bp_loc_hardware_breakpoint
7605 && loc->loc_type != bp_loc_software_breakpoint)
7606 continue;
7607
7608 if (is_addr_in_objfile (loc_addr, objfile))
7609 {
7610 loc->shlib_disabled = 1;
7611 /* At this point, we don't know whether the object was
7612 unmapped from the inferior or not, so leave the
7613 inserted flag alone. We'll handle failure to
7614 uninsert quietly, in case the object was indeed
7615 unmapped. */
7616
7617 mark_breakpoint_location_modified (loc);
7618
7619 bp_modified = 1;
7620 }
7621 }
7622
7623 if (bp_modified)
7624 gdb::observers::breakpoint_modified.notify (b);
7625 }
7626 }
7627
7628 /* FORK & VFORK catchpoints. */
7629
7630 /* An instance of this type is used to represent a fork or vfork
7631 catchpoint. A breakpoint is really of this type iff its ops pointer points
7632 to CATCH_FORK_BREAKPOINT_OPS. */
7633
7634 struct fork_catchpoint : public breakpoint
7635 {
7636 /* Process id of a child process whose forking triggered this
7637 catchpoint. This field is only valid immediately after this
7638 catchpoint has triggered. */
7639 ptid_t forked_inferior_pid;
7640 };
7641
7642 /* Implement the "insert" breakpoint_ops method for fork
7643 catchpoints. */
7644
7645 static int
7646 insert_catch_fork (struct bp_location *bl)
7647 {
7648 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7649 }
7650
7651 /* Implement the "remove" breakpoint_ops method for fork
7652 catchpoints. */
7653
7654 static int
7655 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7656 {
7657 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7658 }
7659
7660 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7661 catchpoints. */
7662
7663 static int
7664 breakpoint_hit_catch_fork (const struct bp_location *bl,
7665 const address_space *aspace, CORE_ADDR bp_addr,
7666 const struct target_waitstatus *ws)
7667 {
7668 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7669
7670 if (ws->kind != TARGET_WAITKIND_FORKED)
7671 return 0;
7672
7673 c->forked_inferior_pid = ws->value.related_pid;
7674 return 1;
7675 }
7676
7677 /* Implement the "print_it" breakpoint_ops method for fork
7678 catchpoints. */
7679
7680 static enum print_stop_action
7681 print_it_catch_fork (bpstat bs)
7682 {
7683 struct ui_out *uiout = current_uiout;
7684 struct breakpoint *b = bs->breakpoint_at;
7685 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7686
7687 annotate_catchpoint (b->number);
7688 maybe_print_thread_hit_breakpoint (uiout);
7689 if (b->disposition == disp_del)
7690 uiout->text ("Temporary catchpoint ");
7691 else
7692 uiout->text ("Catchpoint ");
7693 if (uiout->is_mi_like_p ())
7694 {
7695 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7696 uiout->field_string ("disp", bpdisp_text (b->disposition));
7697 }
7698 uiout->field_signed ("bkptno", b->number);
7699 uiout->text (" (forked process ");
7700 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7701 uiout->text ("), ");
7702 return PRINT_SRC_AND_LOC;
7703 }
7704
7705 /* Implement the "print_one" breakpoint_ops method for fork
7706 catchpoints. */
7707
7708 static void
7709 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7710 {
7711 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7712 struct value_print_options opts;
7713 struct ui_out *uiout = current_uiout;
7714
7715 get_user_print_options (&opts);
7716
7717 /* Field 4, the address, is omitted (which makes the columns not
7718 line up too nicely with the headers, but the effect is relatively
7719 readable). */
7720 if (opts.addressprint)
7721 uiout->field_skip ("addr");
7722 annotate_field (5);
7723 uiout->text ("fork");
7724 if (c->forked_inferior_pid != null_ptid)
7725 {
7726 uiout->text (", process ");
7727 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7728 uiout->spaces (1);
7729 }
7730
7731 if (uiout->is_mi_like_p ())
7732 uiout->field_string ("catch-type", "fork");
7733 }
7734
7735 /* Implement the "print_mention" breakpoint_ops method for fork
7736 catchpoints. */
7737
7738 static void
7739 print_mention_catch_fork (struct breakpoint *b)
7740 {
7741 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7742 }
7743
7744 /* Implement the "print_recreate" breakpoint_ops method for fork
7745 catchpoints. */
7746
7747 static void
7748 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7749 {
7750 fprintf_unfiltered (fp, "catch fork");
7751 print_recreate_thread (b, fp);
7752 }
7753
7754 /* The breakpoint_ops structure to be used in fork catchpoints. */
7755
7756 static struct breakpoint_ops catch_fork_breakpoint_ops;
7757
7758 /* Implement the "insert" breakpoint_ops method for vfork
7759 catchpoints. */
7760
7761 static int
7762 insert_catch_vfork (struct bp_location *bl)
7763 {
7764 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7765 }
7766
7767 /* Implement the "remove" breakpoint_ops method for vfork
7768 catchpoints. */
7769
7770 static int
7771 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7772 {
7773 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7774 }
7775
7776 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7777 catchpoints. */
7778
7779 static int
7780 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7781 const address_space *aspace, CORE_ADDR bp_addr,
7782 const struct target_waitstatus *ws)
7783 {
7784 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7785
7786 if (ws->kind != TARGET_WAITKIND_VFORKED)
7787 return 0;
7788
7789 c->forked_inferior_pid = ws->value.related_pid;
7790 return 1;
7791 }
7792
7793 /* Implement the "print_it" breakpoint_ops method for vfork
7794 catchpoints. */
7795
7796 static enum print_stop_action
7797 print_it_catch_vfork (bpstat bs)
7798 {
7799 struct ui_out *uiout = current_uiout;
7800 struct breakpoint *b = bs->breakpoint_at;
7801 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7802
7803 annotate_catchpoint (b->number);
7804 maybe_print_thread_hit_breakpoint (uiout);
7805 if (b->disposition == disp_del)
7806 uiout->text ("Temporary catchpoint ");
7807 else
7808 uiout->text ("Catchpoint ");
7809 if (uiout->is_mi_like_p ())
7810 {
7811 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7812 uiout->field_string ("disp", bpdisp_text (b->disposition));
7813 }
7814 uiout->field_signed ("bkptno", b->number);
7815 uiout->text (" (vforked process ");
7816 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7817 uiout->text ("), ");
7818 return PRINT_SRC_AND_LOC;
7819 }
7820
7821 /* Implement the "print_one" breakpoint_ops method for vfork
7822 catchpoints. */
7823
7824 static void
7825 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7826 {
7827 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7828 struct value_print_options opts;
7829 struct ui_out *uiout = current_uiout;
7830
7831 get_user_print_options (&opts);
7832 /* Field 4, the address, is omitted (which makes the columns not
7833 line up too nicely with the headers, but the effect is relatively
7834 readable). */
7835 if (opts.addressprint)
7836 uiout->field_skip ("addr");
7837 annotate_field (5);
7838 uiout->text ("vfork");
7839 if (c->forked_inferior_pid != null_ptid)
7840 {
7841 uiout->text (", process ");
7842 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7843 uiout->spaces (1);
7844 }
7845
7846 if (uiout->is_mi_like_p ())
7847 uiout->field_string ("catch-type", "vfork");
7848 }
7849
7850 /* Implement the "print_mention" breakpoint_ops method for vfork
7851 catchpoints. */
7852
7853 static void
7854 print_mention_catch_vfork (struct breakpoint *b)
7855 {
7856 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7857 }
7858
7859 /* Implement the "print_recreate" breakpoint_ops method for vfork
7860 catchpoints. */
7861
7862 static void
7863 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7864 {
7865 fprintf_unfiltered (fp, "catch vfork");
7866 print_recreate_thread (b, fp);
7867 }
7868
7869 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7870
7871 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7872
7873 /* An instance of this type is used to represent an solib catchpoint.
7874 A breakpoint is really of this type iff its ops pointer points to
7875 CATCH_SOLIB_BREAKPOINT_OPS. */
7876
7877 struct solib_catchpoint : public breakpoint
7878 {
7879 ~solib_catchpoint () override;
7880
7881 /* True for "catch load", false for "catch unload". */
7882 unsigned char is_load;
7883
7884 /* Regular expression to match, if any. COMPILED is only valid when
7885 REGEX is non-NULL. */
7886 char *regex;
7887 std::unique_ptr<compiled_regex> compiled;
7888 };
7889
7890 solib_catchpoint::~solib_catchpoint ()
7891 {
7892 xfree (this->regex);
7893 }
7894
7895 static int
7896 insert_catch_solib (struct bp_location *ignore)
7897 {
7898 return 0;
7899 }
7900
7901 static int
7902 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7903 {
7904 return 0;
7905 }
7906
7907 static int
7908 breakpoint_hit_catch_solib (const struct bp_location *bl,
7909 const address_space *aspace,
7910 CORE_ADDR bp_addr,
7911 const struct target_waitstatus *ws)
7912 {
7913 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7914 struct breakpoint *other;
7915
7916 if (ws->kind == TARGET_WAITKIND_LOADED)
7917 return 1;
7918
7919 ALL_BREAKPOINTS (other)
7920 {
7921 struct bp_location *other_bl;
7922
7923 if (other == bl->owner)
7924 continue;
7925
7926 if (other->type != bp_shlib_event)
7927 continue;
7928
7929 if (self->pspace != NULL && other->pspace != self->pspace)
7930 continue;
7931
7932 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7933 {
7934 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7935 return 1;
7936 }
7937 }
7938
7939 return 0;
7940 }
7941
7942 static void
7943 check_status_catch_solib (struct bpstats *bs)
7944 {
7945 struct solib_catchpoint *self
7946 = (struct solib_catchpoint *) bs->breakpoint_at;
7947
7948 if (self->is_load)
7949 {
7950 for (so_list *iter : current_program_space->added_solibs)
7951 {
7952 if (!self->regex
7953 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
7954 return;
7955 }
7956 }
7957 else
7958 {
7959 for (const std::string &iter : current_program_space->deleted_solibs)
7960 {
7961 if (!self->regex
7962 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
7963 return;
7964 }
7965 }
7966
7967 bs->stop = 0;
7968 bs->print_it = print_it_noop;
7969 }
7970
7971 static enum print_stop_action
7972 print_it_catch_solib (bpstat bs)
7973 {
7974 struct breakpoint *b = bs->breakpoint_at;
7975 struct ui_out *uiout = current_uiout;
7976
7977 annotate_catchpoint (b->number);
7978 maybe_print_thread_hit_breakpoint (uiout);
7979 if (b->disposition == disp_del)
7980 uiout->text ("Temporary catchpoint ");
7981 else
7982 uiout->text ("Catchpoint ");
7983 uiout->field_signed ("bkptno", b->number);
7984 uiout->text ("\n");
7985 if (uiout->is_mi_like_p ())
7986 uiout->field_string ("disp", bpdisp_text (b->disposition));
7987 print_solib_event (1);
7988 return PRINT_SRC_AND_LOC;
7989 }
7990
7991 static void
7992 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7993 {
7994 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7995 struct value_print_options opts;
7996 struct ui_out *uiout = current_uiout;
7997
7998 get_user_print_options (&opts);
7999 /* Field 4, the address, is omitted (which makes the columns not
8000 line up too nicely with the headers, but the effect is relatively
8001 readable). */
8002 if (opts.addressprint)
8003 {
8004 annotate_field (4);
8005 uiout->field_skip ("addr");
8006 }
8007
8008 std::string msg;
8009 annotate_field (5);
8010 if (self->is_load)
8011 {
8012 if (self->regex)
8013 msg = string_printf (_("load of library matching %s"), self->regex);
8014 else
8015 msg = _("load of library");
8016 }
8017 else
8018 {
8019 if (self->regex)
8020 msg = string_printf (_("unload of library matching %s"), self->regex);
8021 else
8022 msg = _("unload of library");
8023 }
8024 uiout->field_string ("what", msg);
8025
8026 if (uiout->is_mi_like_p ())
8027 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8028 }
8029
8030 static void
8031 print_mention_catch_solib (struct breakpoint *b)
8032 {
8033 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8034
8035 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8036 self->is_load ? "load" : "unload");
8037 }
8038
8039 static void
8040 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8041 {
8042 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8043
8044 fprintf_unfiltered (fp, "%s %s",
8045 b->disposition == disp_del ? "tcatch" : "catch",
8046 self->is_load ? "load" : "unload");
8047 if (self->regex)
8048 fprintf_unfiltered (fp, " %s", self->regex);
8049 fprintf_unfiltered (fp, "\n");
8050 }
8051
8052 static struct breakpoint_ops catch_solib_breakpoint_ops;
8053
8054 /* Shared helper function (MI and CLI) for creating and installing
8055 a shared object event catchpoint. If IS_LOAD is non-zero then
8056 the events to be caught are load events, otherwise they are
8057 unload events. If IS_TEMP is non-zero the catchpoint is a
8058 temporary one. If ENABLED is non-zero the catchpoint is
8059 created in an enabled state. */
8060
8061 void
8062 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8063 {
8064 struct gdbarch *gdbarch = get_current_arch ();
8065
8066 if (!arg)
8067 arg = "";
8068 arg = skip_spaces (arg);
8069
8070 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8071
8072 if (*arg != '\0')
8073 {
8074 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8075 _("Invalid regexp")));
8076 c->regex = xstrdup (arg);
8077 }
8078
8079 c->is_load = is_load;
8080 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8081 &catch_solib_breakpoint_ops);
8082
8083 c->enable_state = enabled ? bp_enabled : bp_disabled;
8084
8085 install_breakpoint (0, std::move (c), 1);
8086 }
8087
8088 /* A helper function that does all the work for "catch load" and
8089 "catch unload". */
8090
8091 static void
8092 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8093 struct cmd_list_element *command)
8094 {
8095 int tempflag;
8096 const int enabled = 1;
8097
8098 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8099
8100 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8101 }
8102
8103 static void
8104 catch_load_command_1 (const char *arg, int from_tty,
8105 struct cmd_list_element *command)
8106 {
8107 catch_load_or_unload (arg, from_tty, 1, command);
8108 }
8109
8110 static void
8111 catch_unload_command_1 (const char *arg, int from_tty,
8112 struct cmd_list_element *command)
8113 {
8114 catch_load_or_unload (arg, from_tty, 0, command);
8115 }
8116
8117 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8118 is non-zero, then make the breakpoint temporary. If COND_STRING is
8119 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8120 the breakpoint_ops structure associated to the catchpoint. */
8121
8122 void
8123 init_catchpoint (struct breakpoint *b,
8124 struct gdbarch *gdbarch, int tempflag,
8125 const char *cond_string,
8126 const struct breakpoint_ops *ops)
8127 {
8128 symtab_and_line sal;
8129 sal.pspace = current_program_space;
8130
8131 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8132
8133 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8134 b->disposition = tempflag ? disp_del : disp_donttouch;
8135 }
8136
8137 void
8138 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8139 {
8140 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8141 set_breakpoint_number (internal, b);
8142 if (is_tracepoint (b))
8143 set_tracepoint_count (breakpoint_count);
8144 if (!internal)
8145 mention (b);
8146 gdb::observers::breakpoint_created.notify (b);
8147
8148 if (update_gll)
8149 update_global_location_list (UGLL_MAY_INSERT);
8150 }
8151
8152 static void
8153 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8154 int tempflag, const char *cond_string,
8155 const struct breakpoint_ops *ops)
8156 {
8157 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8158
8159 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8160
8161 c->forked_inferior_pid = null_ptid;
8162
8163 install_breakpoint (0, std::move (c), 1);
8164 }
8165
8166 /* Exec catchpoints. */
8167
8168 /* An instance of this type is used to represent an exec catchpoint.
8169 A breakpoint is really of this type iff its ops pointer points to
8170 CATCH_EXEC_BREAKPOINT_OPS. */
8171
8172 struct exec_catchpoint : public breakpoint
8173 {
8174 ~exec_catchpoint () override;
8175
8176 /* Filename of a program whose exec triggered this catchpoint.
8177 This field is only valid immediately after this catchpoint has
8178 triggered. */
8179 char *exec_pathname;
8180 };
8181
8182 /* Exec catchpoint destructor. */
8183
8184 exec_catchpoint::~exec_catchpoint ()
8185 {
8186 xfree (this->exec_pathname);
8187 }
8188
8189 static int
8190 insert_catch_exec (struct bp_location *bl)
8191 {
8192 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8193 }
8194
8195 static int
8196 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8197 {
8198 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8199 }
8200
8201 static int
8202 breakpoint_hit_catch_exec (const struct bp_location *bl,
8203 const address_space *aspace, CORE_ADDR bp_addr,
8204 const struct target_waitstatus *ws)
8205 {
8206 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8207
8208 if (ws->kind != TARGET_WAITKIND_EXECD)
8209 return 0;
8210
8211 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8212 return 1;
8213 }
8214
8215 static enum print_stop_action
8216 print_it_catch_exec (bpstat bs)
8217 {
8218 struct ui_out *uiout = current_uiout;
8219 struct breakpoint *b = bs->breakpoint_at;
8220 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8221
8222 annotate_catchpoint (b->number);
8223 maybe_print_thread_hit_breakpoint (uiout);
8224 if (b->disposition == disp_del)
8225 uiout->text ("Temporary catchpoint ");
8226 else
8227 uiout->text ("Catchpoint ");
8228 if (uiout->is_mi_like_p ())
8229 {
8230 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8231 uiout->field_string ("disp", bpdisp_text (b->disposition));
8232 }
8233 uiout->field_signed ("bkptno", b->number);
8234 uiout->text (" (exec'd ");
8235 uiout->field_string ("new-exec", c->exec_pathname);
8236 uiout->text ("), ");
8237
8238 return PRINT_SRC_AND_LOC;
8239 }
8240
8241 static void
8242 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8243 {
8244 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8245 struct value_print_options opts;
8246 struct ui_out *uiout = current_uiout;
8247
8248 get_user_print_options (&opts);
8249
8250 /* Field 4, the address, is omitted (which makes the columns
8251 not line up too nicely with the headers, but the effect
8252 is relatively readable). */
8253 if (opts.addressprint)
8254 uiout->field_skip ("addr");
8255 annotate_field (5);
8256 uiout->text ("exec");
8257 if (c->exec_pathname != NULL)
8258 {
8259 uiout->text (", program \"");
8260 uiout->field_string ("what", c->exec_pathname);
8261 uiout->text ("\" ");
8262 }
8263
8264 if (uiout->is_mi_like_p ())
8265 uiout->field_string ("catch-type", "exec");
8266 }
8267
8268 static void
8269 print_mention_catch_exec (struct breakpoint *b)
8270 {
8271 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8272 }
8273
8274 /* Implement the "print_recreate" breakpoint_ops method for exec
8275 catchpoints. */
8276
8277 static void
8278 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8279 {
8280 fprintf_unfiltered (fp, "catch exec");
8281 print_recreate_thread (b, fp);
8282 }
8283
8284 static struct breakpoint_ops catch_exec_breakpoint_ops;
8285
8286 static int
8287 hw_breakpoint_used_count (void)
8288 {
8289 int i = 0;
8290 struct breakpoint *b;
8291 struct bp_location *bl;
8292
8293 ALL_BREAKPOINTS (b)
8294 {
8295 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8296 for (bl = b->loc; bl; bl = bl->next)
8297 {
8298 /* Special types of hardware breakpoints may use more than
8299 one register. */
8300 i += b->ops->resources_needed (bl);
8301 }
8302 }
8303
8304 return i;
8305 }
8306
8307 /* Returns the resources B would use if it were a hardware
8308 watchpoint. */
8309
8310 static int
8311 hw_watchpoint_use_count (struct breakpoint *b)
8312 {
8313 int i = 0;
8314 struct bp_location *bl;
8315
8316 if (!breakpoint_enabled (b))
8317 return 0;
8318
8319 for (bl = b->loc; bl; bl = bl->next)
8320 {
8321 /* Special types of hardware watchpoints may use more than
8322 one register. */
8323 i += b->ops->resources_needed (bl);
8324 }
8325
8326 return i;
8327 }
8328
8329 /* Returns the sum the used resources of all hardware watchpoints of
8330 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8331 the sum of the used resources of all hardware watchpoints of other
8332 types _not_ TYPE. */
8333
8334 static int
8335 hw_watchpoint_used_count_others (struct breakpoint *except,
8336 enum bptype type, int *other_type_used)
8337 {
8338 int i = 0;
8339 struct breakpoint *b;
8340
8341 *other_type_used = 0;
8342 ALL_BREAKPOINTS (b)
8343 {
8344 if (b == except)
8345 continue;
8346 if (!breakpoint_enabled (b))
8347 continue;
8348
8349 if (b->type == type)
8350 i += hw_watchpoint_use_count (b);
8351 else if (is_hardware_watchpoint (b))
8352 *other_type_used = 1;
8353 }
8354
8355 return i;
8356 }
8357
8358 void
8359 disable_watchpoints_before_interactive_call_start (void)
8360 {
8361 struct breakpoint *b;
8362
8363 ALL_BREAKPOINTS (b)
8364 {
8365 if (is_watchpoint (b) && breakpoint_enabled (b))
8366 {
8367 b->enable_state = bp_call_disabled;
8368 update_global_location_list (UGLL_DONT_INSERT);
8369 }
8370 }
8371 }
8372
8373 void
8374 enable_watchpoints_after_interactive_call_stop (void)
8375 {
8376 struct breakpoint *b;
8377
8378 ALL_BREAKPOINTS (b)
8379 {
8380 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8381 {
8382 b->enable_state = bp_enabled;
8383 update_global_location_list (UGLL_MAY_INSERT);
8384 }
8385 }
8386 }
8387
8388 void
8389 disable_breakpoints_before_startup (void)
8390 {
8391 current_program_space->executing_startup = 1;
8392 update_global_location_list (UGLL_DONT_INSERT);
8393 }
8394
8395 void
8396 enable_breakpoints_after_startup (void)
8397 {
8398 current_program_space->executing_startup = 0;
8399 breakpoint_re_set ();
8400 }
8401
8402 /* Create a new single-step breakpoint for thread THREAD, with no
8403 locations. */
8404
8405 static struct breakpoint *
8406 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8407 {
8408 std::unique_ptr<breakpoint> b (new breakpoint ());
8409
8410 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8411 &momentary_breakpoint_ops);
8412
8413 b->disposition = disp_donttouch;
8414 b->frame_id = null_frame_id;
8415
8416 b->thread = thread;
8417 gdb_assert (b->thread != 0);
8418
8419 return add_to_breakpoint_chain (std::move (b));
8420 }
8421
8422 /* Set a momentary breakpoint of type TYPE at address specified by
8423 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8424 frame. */
8425
8426 breakpoint_up
8427 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8428 struct frame_id frame_id, enum bptype type)
8429 {
8430 struct breakpoint *b;
8431
8432 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8433 tail-called one. */
8434 gdb_assert (!frame_id_artificial_p (frame_id));
8435
8436 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8437 b->enable_state = bp_enabled;
8438 b->disposition = disp_donttouch;
8439 b->frame_id = frame_id;
8440
8441 b->thread = inferior_thread ()->global_num;
8442
8443 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8444
8445 return breakpoint_up (b);
8446 }
8447
8448 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8449 The new breakpoint will have type TYPE, use OPS as its
8450 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8451
8452 static struct breakpoint *
8453 momentary_breakpoint_from_master (struct breakpoint *orig,
8454 enum bptype type,
8455 const struct breakpoint_ops *ops,
8456 int loc_enabled)
8457 {
8458 struct breakpoint *copy;
8459
8460 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8461 copy->loc = allocate_bp_location (copy);
8462 set_breakpoint_location_function (copy->loc);
8463
8464 copy->loc->gdbarch = orig->loc->gdbarch;
8465 copy->loc->requested_address = orig->loc->requested_address;
8466 copy->loc->address = orig->loc->address;
8467 copy->loc->section = orig->loc->section;
8468 copy->loc->pspace = orig->loc->pspace;
8469 copy->loc->probe = orig->loc->probe;
8470 copy->loc->line_number = orig->loc->line_number;
8471 copy->loc->symtab = orig->loc->symtab;
8472 copy->loc->enabled = loc_enabled;
8473 copy->frame_id = orig->frame_id;
8474 copy->thread = orig->thread;
8475 copy->pspace = orig->pspace;
8476
8477 copy->enable_state = bp_enabled;
8478 copy->disposition = disp_donttouch;
8479 copy->number = internal_breakpoint_number--;
8480
8481 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8482 return copy;
8483 }
8484
8485 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8486 ORIG is NULL. */
8487
8488 struct breakpoint *
8489 clone_momentary_breakpoint (struct breakpoint *orig)
8490 {
8491 /* If there's nothing to clone, then return nothing. */
8492 if (orig == NULL)
8493 return NULL;
8494
8495 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8496 }
8497
8498 breakpoint_up
8499 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8500 enum bptype type)
8501 {
8502 struct symtab_and_line sal;
8503
8504 sal = find_pc_line (pc, 0);
8505 sal.pc = pc;
8506 sal.section = find_pc_overlay (pc);
8507 sal.explicit_pc = 1;
8508
8509 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8510 }
8511 \f
8512
8513 /* Tell the user we have just set a breakpoint B. */
8514
8515 static void
8516 mention (struct breakpoint *b)
8517 {
8518 b->ops->print_mention (b);
8519 current_uiout->text ("\n");
8520 }
8521 \f
8522
8523 static bool bp_loc_is_permanent (struct bp_location *loc);
8524
8525 /* Handle "set breakpoint auto-hw on".
8526
8527 If the explicitly specified breakpoint type is not hardware
8528 breakpoint, check the memory map to see whether the breakpoint
8529 address is in read-only memory.
8530
8531 - location type is not hardware breakpoint, memory is read-only.
8532 We change the type of the location to hardware breakpoint.
8533
8534 - location type is hardware breakpoint, memory is read-write. This
8535 means we've previously made the location hardware one, but then the
8536 memory map changed, so we undo.
8537 */
8538
8539 static void
8540 handle_automatic_hardware_breakpoints (bp_location *bl)
8541 {
8542 if (automatic_hardware_breakpoints
8543 && bl->owner->type != bp_hardware_breakpoint
8544 && (bl->loc_type == bp_loc_software_breakpoint
8545 || bl->loc_type == bp_loc_hardware_breakpoint))
8546 {
8547 /* When breakpoints are removed, remove_breakpoints will use
8548 location types we've just set here, the only possible problem
8549 is that memory map has changed during running program, but
8550 it's not going to work anyway with current gdb. */
8551 mem_region *mr = lookup_mem_region (bl->address);
8552
8553 if (mr != nullptr)
8554 {
8555 enum bp_loc_type new_type;
8556
8557 if (mr->attrib.mode != MEM_RW)
8558 new_type = bp_loc_hardware_breakpoint;
8559 else
8560 new_type = bp_loc_software_breakpoint;
8561
8562 if (new_type != bl->loc_type)
8563 {
8564 static bool said = false;
8565
8566 bl->loc_type = new_type;
8567 if (!said)
8568 {
8569 fprintf_filtered (gdb_stdout,
8570 _("Note: automatically using "
8571 "hardware breakpoints for "
8572 "read-only addresses.\n"));
8573 said = true;
8574 }
8575 }
8576 }
8577 }
8578 }
8579
8580 static struct bp_location *
8581 add_location_to_breakpoint (struct breakpoint *b,
8582 const struct symtab_and_line *sal)
8583 {
8584 struct bp_location *loc, **tmp;
8585 CORE_ADDR adjusted_address;
8586 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8587
8588 if (loc_gdbarch == NULL)
8589 loc_gdbarch = b->gdbarch;
8590
8591 /* Adjust the breakpoint's address prior to allocating a location.
8592 Once we call allocate_bp_location(), that mostly uninitialized
8593 location will be placed on the location chain. Adjustment of the
8594 breakpoint may cause target_read_memory() to be called and we do
8595 not want its scan of the location chain to find a breakpoint and
8596 location that's only been partially initialized. */
8597 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8598 sal->pc, b->type);
8599
8600 /* Sort the locations by their ADDRESS. */
8601 loc = allocate_bp_location (b);
8602 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8603 tmp = &((*tmp)->next))
8604 ;
8605 loc->next = *tmp;
8606 *tmp = loc;
8607
8608 loc->requested_address = sal->pc;
8609 loc->address = adjusted_address;
8610 loc->pspace = sal->pspace;
8611 loc->probe.prob = sal->prob;
8612 loc->probe.objfile = sal->objfile;
8613 gdb_assert (loc->pspace != NULL);
8614 loc->section = sal->section;
8615 loc->gdbarch = loc_gdbarch;
8616 loc->line_number = sal->line;
8617 loc->symtab = sal->symtab;
8618 loc->symbol = sal->symbol;
8619 loc->msymbol = sal->msymbol;
8620 loc->objfile = sal->objfile;
8621
8622 set_breakpoint_location_function (loc);
8623
8624 /* While by definition, permanent breakpoints are already present in the
8625 code, we don't mark the location as inserted. Normally one would expect
8626 that GDB could rely on that breakpoint instruction to stop the program,
8627 thus removing the need to insert its own breakpoint, except that executing
8628 the breakpoint instruction can kill the target instead of reporting a
8629 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8630 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8631 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8632 breakpoint be inserted normally results in QEMU knowing about the GDB
8633 breakpoint, and thus trap before the breakpoint instruction is executed.
8634 (If GDB later needs to continue execution past the permanent breakpoint,
8635 it manually increments the PC, thus avoiding executing the breakpoint
8636 instruction.) */
8637 if (bp_loc_is_permanent (loc))
8638 loc->permanent = 1;
8639
8640 return loc;
8641 }
8642 \f
8643
8644 /* Return true if LOC is pointing to a permanent breakpoint,
8645 return false otherwise. */
8646
8647 static bool
8648 bp_loc_is_permanent (struct bp_location *loc)
8649 {
8650 gdb_assert (loc != NULL);
8651
8652 /* If we have a non-breakpoint-backed catchpoint or a software
8653 watchpoint, just return 0. We should not attempt to read from
8654 the addresses the locations of these breakpoint types point to.
8655 gdbarch_program_breakpoint_here_p, below, will attempt to read
8656 memory. */
8657 if (!bl_address_is_meaningful (loc))
8658 return false;
8659
8660 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8661 switch_to_program_space_and_thread (loc->pspace);
8662 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8663 }
8664
8665 /* Build a command list for the dprintf corresponding to the current
8666 settings of the dprintf style options. */
8667
8668 static void
8669 update_dprintf_command_list (struct breakpoint *b)
8670 {
8671 char *dprintf_args = b->extra_string;
8672 char *printf_line = NULL;
8673
8674 if (!dprintf_args)
8675 return;
8676
8677 dprintf_args = skip_spaces (dprintf_args);
8678
8679 /* Allow a comma, as it may have terminated a location, but don't
8680 insist on it. */
8681 if (*dprintf_args == ',')
8682 ++dprintf_args;
8683 dprintf_args = skip_spaces (dprintf_args);
8684
8685 if (*dprintf_args != '"')
8686 error (_("Bad format string, missing '\"'."));
8687
8688 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8689 printf_line = xstrprintf ("printf %s", dprintf_args);
8690 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8691 {
8692 if (!dprintf_function)
8693 error (_("No function supplied for dprintf call"));
8694
8695 if (dprintf_channel && strlen (dprintf_channel) > 0)
8696 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8697 dprintf_function,
8698 dprintf_channel,
8699 dprintf_args);
8700 else
8701 printf_line = xstrprintf ("call (void) %s (%s)",
8702 dprintf_function,
8703 dprintf_args);
8704 }
8705 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8706 {
8707 if (target_can_run_breakpoint_commands ())
8708 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8709 else
8710 {
8711 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8712 printf_line = xstrprintf ("printf %s", dprintf_args);
8713 }
8714 }
8715 else
8716 internal_error (__FILE__, __LINE__,
8717 _("Invalid dprintf style."));
8718
8719 gdb_assert (printf_line != NULL);
8720
8721 /* Manufacture a printf sequence. */
8722 struct command_line *printf_cmd_line
8723 = new struct command_line (simple_control, printf_line);
8724 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8725 command_lines_deleter ()));
8726 }
8727
8728 /* Update all dprintf commands, making their command lists reflect
8729 current style settings. */
8730
8731 static void
8732 update_dprintf_commands (const char *args, int from_tty,
8733 struct cmd_list_element *c)
8734 {
8735 struct breakpoint *b;
8736
8737 ALL_BREAKPOINTS (b)
8738 {
8739 if (b->type == bp_dprintf)
8740 update_dprintf_command_list (b);
8741 }
8742 }
8743
8744 /* Create a breakpoint with SAL as location. Use LOCATION
8745 as a description of the location, and COND_STRING
8746 as condition expression. If LOCATION is NULL then create an
8747 "address location" from the address in the SAL. */
8748
8749 static void
8750 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8751 gdb::array_view<const symtab_and_line> sals,
8752 event_location_up &&location,
8753 gdb::unique_xmalloc_ptr<char> filter,
8754 gdb::unique_xmalloc_ptr<char> cond_string,
8755 gdb::unique_xmalloc_ptr<char> extra_string,
8756 enum bptype type, enum bpdisp disposition,
8757 int thread, int task, int ignore_count,
8758 const struct breakpoint_ops *ops, int from_tty,
8759 int enabled, int internal, unsigned flags,
8760 int display_canonical)
8761 {
8762 int i;
8763
8764 if (type == bp_hardware_breakpoint)
8765 {
8766 int target_resources_ok;
8767
8768 i = hw_breakpoint_used_count ();
8769 target_resources_ok =
8770 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8771 i + 1, 0);
8772 if (target_resources_ok == 0)
8773 error (_("No hardware breakpoint support in the target."));
8774 else if (target_resources_ok < 0)
8775 error (_("Hardware breakpoints used exceeds limit."));
8776 }
8777
8778 gdb_assert (!sals.empty ());
8779
8780 for (const auto &sal : sals)
8781 {
8782 struct bp_location *loc;
8783
8784 if (from_tty)
8785 {
8786 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8787 if (!loc_gdbarch)
8788 loc_gdbarch = gdbarch;
8789
8790 describe_other_breakpoints (loc_gdbarch,
8791 sal.pspace, sal.pc, sal.section, thread);
8792 }
8793
8794 if (&sal == &sals[0])
8795 {
8796 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8797 b->thread = thread;
8798 b->task = task;
8799
8800 b->cond_string = cond_string.release ();
8801 b->extra_string = extra_string.release ();
8802 b->ignore_count = ignore_count;
8803 b->enable_state = enabled ? bp_enabled : bp_disabled;
8804 b->disposition = disposition;
8805
8806 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8807 b->loc->inserted = 1;
8808
8809 if (type == bp_static_tracepoint)
8810 {
8811 struct tracepoint *t = (struct tracepoint *) b;
8812 struct static_tracepoint_marker marker;
8813
8814 if (strace_marker_p (b))
8815 {
8816 /* We already know the marker exists, otherwise, we
8817 wouldn't see a sal for it. */
8818 const char *p
8819 = &event_location_to_string (b->location.get ())[3];
8820 const char *endp;
8821
8822 p = skip_spaces (p);
8823
8824 endp = skip_to_space (p);
8825
8826 t->static_trace_marker_id.assign (p, endp - p);
8827
8828 printf_filtered (_("Probed static tracepoint "
8829 "marker \"%s\"\n"),
8830 t->static_trace_marker_id.c_str ());
8831 }
8832 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8833 {
8834 t->static_trace_marker_id = std::move (marker.str_id);
8835
8836 printf_filtered (_("Probed static tracepoint "
8837 "marker \"%s\"\n"),
8838 t->static_trace_marker_id.c_str ());
8839 }
8840 else
8841 warning (_("Couldn't determine the static "
8842 "tracepoint marker to probe"));
8843 }
8844
8845 loc = b->loc;
8846 }
8847 else
8848 {
8849 loc = add_location_to_breakpoint (b, &sal);
8850 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8851 loc->inserted = 1;
8852 }
8853
8854 if (b->cond_string)
8855 {
8856 const char *arg = b->cond_string;
8857
8858 loc->cond = parse_exp_1 (&arg, loc->address,
8859 block_for_pc (loc->address), 0);
8860 if (*arg)
8861 error (_("Garbage '%s' follows condition"), arg);
8862 }
8863
8864 /* Dynamic printf requires and uses additional arguments on the
8865 command line, otherwise it's an error. */
8866 if (type == bp_dprintf)
8867 {
8868 if (b->extra_string)
8869 update_dprintf_command_list (b);
8870 else
8871 error (_("Format string required"));
8872 }
8873 else if (b->extra_string)
8874 error (_("Garbage '%s' at end of command"), b->extra_string);
8875 }
8876
8877 b->display_canonical = display_canonical;
8878 if (location != NULL)
8879 b->location = std::move (location);
8880 else
8881 b->location = new_address_location (b->loc->address, NULL, 0);
8882 b->filter = std::move (filter);
8883 }
8884
8885 static void
8886 create_breakpoint_sal (struct gdbarch *gdbarch,
8887 gdb::array_view<const symtab_and_line> sals,
8888 event_location_up &&location,
8889 gdb::unique_xmalloc_ptr<char> filter,
8890 gdb::unique_xmalloc_ptr<char> cond_string,
8891 gdb::unique_xmalloc_ptr<char> extra_string,
8892 enum bptype type, enum bpdisp disposition,
8893 int thread, int task, int ignore_count,
8894 const struct breakpoint_ops *ops, int from_tty,
8895 int enabled, int internal, unsigned flags,
8896 int display_canonical)
8897 {
8898 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8899
8900 init_breakpoint_sal (b.get (), gdbarch,
8901 sals, std::move (location),
8902 std::move (filter),
8903 std::move (cond_string),
8904 std::move (extra_string),
8905 type, disposition,
8906 thread, task, ignore_count,
8907 ops, from_tty,
8908 enabled, internal, flags,
8909 display_canonical);
8910
8911 install_breakpoint (internal, std::move (b), 0);
8912 }
8913
8914 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8915 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8916 value. COND_STRING, if not NULL, specified the condition to be
8917 used for all breakpoints. Essentially the only case where
8918 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8919 function. In that case, it's still not possible to specify
8920 separate conditions for different overloaded functions, so
8921 we take just a single condition string.
8922
8923 NOTE: If the function succeeds, the caller is expected to cleanup
8924 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8925 array contents). If the function fails (error() is called), the
8926 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8927 COND and SALS arrays and each of those arrays contents. */
8928
8929 static void
8930 create_breakpoints_sal (struct gdbarch *gdbarch,
8931 struct linespec_result *canonical,
8932 gdb::unique_xmalloc_ptr<char> cond_string,
8933 gdb::unique_xmalloc_ptr<char> extra_string,
8934 enum bptype type, enum bpdisp disposition,
8935 int thread, int task, int ignore_count,
8936 const struct breakpoint_ops *ops, int from_tty,
8937 int enabled, int internal, unsigned flags)
8938 {
8939 if (canonical->pre_expanded)
8940 gdb_assert (canonical->lsals.size () == 1);
8941
8942 for (const auto &lsal : canonical->lsals)
8943 {
8944 /* Note that 'location' can be NULL in the case of a plain
8945 'break', without arguments. */
8946 event_location_up location
8947 = (canonical->location != NULL
8948 ? copy_event_location (canonical->location.get ()) : NULL);
8949 gdb::unique_xmalloc_ptr<char> filter_string
8950 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8951
8952 create_breakpoint_sal (gdbarch, lsal.sals,
8953 std::move (location),
8954 std::move (filter_string),
8955 std::move (cond_string),
8956 std::move (extra_string),
8957 type, disposition,
8958 thread, task, ignore_count, ops,
8959 from_tty, enabled, internal, flags,
8960 canonical->special_display);
8961 }
8962 }
8963
8964 /* Parse LOCATION which is assumed to be a SAL specification possibly
8965 followed by conditionals. On return, SALS contains an array of SAL
8966 addresses found. LOCATION points to the end of the SAL (for
8967 linespec locations).
8968
8969 The array and the line spec strings are allocated on the heap, it is
8970 the caller's responsibility to free them. */
8971
8972 static void
8973 parse_breakpoint_sals (const struct event_location *location,
8974 struct linespec_result *canonical)
8975 {
8976 struct symtab_and_line cursal;
8977
8978 if (event_location_type (location) == LINESPEC_LOCATION)
8979 {
8980 const char *spec = get_linespec_location (location)->spec_string;
8981
8982 if (spec == NULL)
8983 {
8984 /* The last displayed codepoint, if it's valid, is our default
8985 breakpoint address. */
8986 if (last_displayed_sal_is_valid ())
8987 {
8988 /* Set sal's pspace, pc, symtab, and line to the values
8989 corresponding to the last call to print_frame_info.
8990 Be sure to reinitialize LINE with NOTCURRENT == 0
8991 as the breakpoint line number is inappropriate otherwise.
8992 find_pc_line would adjust PC, re-set it back. */
8993 symtab_and_line sal = get_last_displayed_sal ();
8994 CORE_ADDR pc = sal.pc;
8995
8996 sal = find_pc_line (pc, 0);
8997
8998 /* "break" without arguments is equivalent to "break *PC"
8999 where PC is the last displayed codepoint's address. So
9000 make sure to set sal.explicit_pc to prevent GDB from
9001 trying to expand the list of sals to include all other
9002 instances with the same symtab and line. */
9003 sal.pc = pc;
9004 sal.explicit_pc = 1;
9005
9006 struct linespec_sals lsal;
9007 lsal.sals = {sal};
9008 lsal.canonical = NULL;
9009
9010 canonical->lsals.push_back (std::move (lsal));
9011 return;
9012 }
9013 else
9014 error (_("No default breakpoint address now."));
9015 }
9016 }
9017
9018 /* Force almost all breakpoints to be in terms of the
9019 current_source_symtab (which is decode_line_1's default).
9020 This should produce the results we want almost all of the
9021 time while leaving default_breakpoint_* alone.
9022
9023 ObjC: However, don't match an Objective-C method name which
9024 may have a '+' or '-' succeeded by a '['. */
9025 cursal = get_current_source_symtab_and_line ();
9026 if (last_displayed_sal_is_valid ())
9027 {
9028 const char *spec = NULL;
9029
9030 if (event_location_type (location) == LINESPEC_LOCATION)
9031 spec = get_linespec_location (location)->spec_string;
9032
9033 if (!cursal.symtab
9034 || (spec != NULL
9035 && strchr ("+-", spec[0]) != NULL
9036 && spec[1] != '['))
9037 {
9038 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9039 get_last_displayed_symtab (),
9040 get_last_displayed_line (),
9041 canonical, NULL, NULL);
9042 return;
9043 }
9044 }
9045
9046 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9047 cursal.symtab, cursal.line, canonical, NULL, NULL);
9048 }
9049
9050
9051 /* Convert each SAL into a real PC. Verify that the PC can be
9052 inserted as a breakpoint. If it can't throw an error. */
9053
9054 static void
9055 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9056 {
9057 for (auto &sal : sals)
9058 resolve_sal_pc (&sal);
9059 }
9060
9061 /* Fast tracepoints may have restrictions on valid locations. For
9062 instance, a fast tracepoint using a jump instead of a trap will
9063 likely have to overwrite more bytes than a trap would, and so can
9064 only be placed where the instruction is longer than the jump, or a
9065 multi-instruction sequence does not have a jump into the middle of
9066 it, etc. */
9067
9068 static void
9069 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9070 gdb::array_view<const symtab_and_line> sals)
9071 {
9072 for (const auto &sal : sals)
9073 {
9074 struct gdbarch *sarch;
9075
9076 sarch = get_sal_arch (sal);
9077 /* We fall back to GDBARCH if there is no architecture
9078 associated with SAL. */
9079 if (sarch == NULL)
9080 sarch = gdbarch;
9081 std::string msg;
9082 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9083 error (_("May not have a fast tracepoint at %s%s"),
9084 paddress (sarch, sal.pc), msg.c_str ());
9085 }
9086 }
9087
9088 /* Given TOK, a string specification of condition and thread, as
9089 accepted by the 'break' command, extract the condition
9090 string and thread number and set *COND_STRING and *THREAD.
9091 PC identifies the context at which the condition should be parsed.
9092 If no condition is found, *COND_STRING is set to NULL.
9093 If no thread is found, *THREAD is set to -1. */
9094
9095 static void
9096 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9097 char **cond_string, int *thread, int *task,
9098 char **rest)
9099 {
9100 *cond_string = NULL;
9101 *thread = -1;
9102 *task = 0;
9103 *rest = NULL;
9104
9105 while (tok && *tok)
9106 {
9107 const char *end_tok;
9108 int toklen;
9109 const char *cond_start = NULL;
9110 const char *cond_end = NULL;
9111
9112 tok = skip_spaces (tok);
9113
9114 if ((*tok == '"' || *tok == ',') && rest)
9115 {
9116 *rest = savestring (tok, strlen (tok));
9117 return;
9118 }
9119
9120 end_tok = skip_to_space (tok);
9121
9122 toklen = end_tok - tok;
9123
9124 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9125 {
9126 tok = cond_start = end_tok + 1;
9127 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9128 cond_end = tok;
9129 *cond_string = savestring (cond_start, cond_end - cond_start);
9130 }
9131 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9132 {
9133 const char *tmptok;
9134 struct thread_info *thr;
9135
9136 tok = end_tok + 1;
9137 thr = parse_thread_id (tok, &tmptok);
9138 if (tok == tmptok)
9139 error (_("Junk after thread keyword."));
9140 *thread = thr->global_num;
9141 tok = tmptok;
9142 }
9143 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9144 {
9145 char *tmptok;
9146
9147 tok = end_tok + 1;
9148 *task = strtol (tok, &tmptok, 0);
9149 if (tok == tmptok)
9150 error (_("Junk after task keyword."));
9151 if (!valid_task_id (*task))
9152 error (_("Unknown task %d."), *task);
9153 tok = tmptok;
9154 }
9155 else if (rest)
9156 {
9157 *rest = savestring (tok, strlen (tok));
9158 return;
9159 }
9160 else
9161 error (_("Junk at end of arguments."));
9162 }
9163 }
9164
9165 /* Decode a static tracepoint marker spec. */
9166
9167 static std::vector<symtab_and_line>
9168 decode_static_tracepoint_spec (const char **arg_p)
9169 {
9170 const char *p = &(*arg_p)[3];
9171 const char *endp;
9172
9173 p = skip_spaces (p);
9174
9175 endp = skip_to_space (p);
9176
9177 std::string marker_str (p, endp - p);
9178
9179 std::vector<static_tracepoint_marker> markers
9180 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9181 if (markers.empty ())
9182 error (_("No known static tracepoint marker named %s"),
9183 marker_str.c_str ());
9184
9185 std::vector<symtab_and_line> sals;
9186 sals.reserve (markers.size ());
9187
9188 for (const static_tracepoint_marker &marker : markers)
9189 {
9190 symtab_and_line sal = find_pc_line (marker.address, 0);
9191 sal.pc = marker.address;
9192 sals.push_back (sal);
9193 }
9194
9195 *arg_p = endp;
9196 return sals;
9197 }
9198
9199 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9200 according to IS_TRACEPOINT. */
9201
9202 static const struct breakpoint_ops *
9203 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9204 bool is_tracepoint)
9205 {
9206 if (is_tracepoint)
9207 {
9208 if (location_type == PROBE_LOCATION)
9209 return &tracepoint_probe_breakpoint_ops;
9210 else
9211 return &tracepoint_breakpoint_ops;
9212 }
9213 else
9214 {
9215 if (location_type == PROBE_LOCATION)
9216 return &bkpt_probe_breakpoint_ops;
9217 else
9218 return &bkpt_breakpoint_ops;
9219 }
9220 }
9221
9222 /* See breakpoint.h. */
9223
9224 const struct breakpoint_ops *
9225 breakpoint_ops_for_event_location (const struct event_location *location,
9226 bool is_tracepoint)
9227 {
9228 if (location != nullptr)
9229 return breakpoint_ops_for_event_location_type
9230 (event_location_type (location), is_tracepoint);
9231 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9232 }
9233
9234 /* See breakpoint.h. */
9235
9236 int
9237 create_breakpoint (struct gdbarch *gdbarch,
9238 const struct event_location *location,
9239 const char *cond_string,
9240 int thread, const char *extra_string,
9241 int parse_extra,
9242 int tempflag, enum bptype type_wanted,
9243 int ignore_count,
9244 enum auto_boolean pending_break_support,
9245 const struct breakpoint_ops *ops,
9246 int from_tty, int enabled, int internal,
9247 unsigned flags)
9248 {
9249 struct linespec_result canonical;
9250 int pending = 0;
9251 int task = 0;
9252 int prev_bkpt_count = breakpoint_count;
9253
9254 gdb_assert (ops != NULL);
9255
9256 /* If extra_string isn't useful, set it to NULL. */
9257 if (extra_string != NULL && *extra_string == '\0')
9258 extra_string = NULL;
9259
9260 try
9261 {
9262 ops->create_sals_from_location (location, &canonical, type_wanted);
9263 }
9264 catch (const gdb_exception_error &e)
9265 {
9266 /* If caller is interested in rc value from parse, set
9267 value. */
9268 if (e.error == NOT_FOUND_ERROR)
9269 {
9270 /* If pending breakpoint support is turned off, throw
9271 error. */
9272
9273 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9274 throw;
9275
9276 exception_print (gdb_stderr, e);
9277
9278 /* If pending breakpoint support is auto query and the user
9279 selects no, then simply return the error code. */
9280 if (pending_break_support == AUTO_BOOLEAN_AUTO
9281 && !nquery (_("Make %s pending on future shared library load? "),
9282 bptype_string (type_wanted)))
9283 return 0;
9284
9285 /* At this point, either the user was queried about setting
9286 a pending breakpoint and selected yes, or pending
9287 breakpoint behavior is on and thus a pending breakpoint
9288 is defaulted on behalf of the user. */
9289 pending = 1;
9290 }
9291 else
9292 throw;
9293 }
9294
9295 if (!pending && canonical.lsals.empty ())
9296 return 0;
9297
9298 /* Resolve all line numbers to PC's and verify that the addresses
9299 are ok for the target. */
9300 if (!pending)
9301 {
9302 for (auto &lsal : canonical.lsals)
9303 breakpoint_sals_to_pc (lsal.sals);
9304 }
9305
9306 /* Fast tracepoints may have additional restrictions on location. */
9307 if (!pending && type_wanted == bp_fast_tracepoint)
9308 {
9309 for (const auto &lsal : canonical.lsals)
9310 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9311 }
9312
9313 /* Verify that condition can be parsed, before setting any
9314 breakpoints. Allocate a separate condition expression for each
9315 breakpoint. */
9316 if (!pending)
9317 {
9318 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9319 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9320
9321 if (parse_extra)
9322 {
9323 char *rest;
9324 char *cond;
9325
9326 const linespec_sals &lsal = canonical.lsals[0];
9327
9328 /* Here we only parse 'arg' to separate condition
9329 from thread number, so parsing in context of first
9330 sal is OK. When setting the breakpoint we'll
9331 re-parse it in context of each sal. */
9332
9333 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9334 &cond, &thread, &task, &rest);
9335 cond_string_copy.reset (cond);
9336 extra_string_copy.reset (rest);
9337 }
9338 else
9339 {
9340 if (type_wanted != bp_dprintf
9341 && extra_string != NULL && *extra_string != '\0')
9342 error (_("Garbage '%s' at end of location"), extra_string);
9343
9344 /* Create a private copy of condition string. */
9345 if (cond_string)
9346 cond_string_copy.reset (xstrdup (cond_string));
9347 /* Create a private copy of any extra string. */
9348 if (extra_string)
9349 extra_string_copy.reset (xstrdup (extra_string));
9350 }
9351
9352 ops->create_breakpoints_sal (gdbarch, &canonical,
9353 std::move (cond_string_copy),
9354 std::move (extra_string_copy),
9355 type_wanted,
9356 tempflag ? disp_del : disp_donttouch,
9357 thread, task, ignore_count, ops,
9358 from_tty, enabled, internal, flags);
9359 }
9360 else
9361 {
9362 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9363
9364 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9365 b->location = copy_event_location (location);
9366
9367 if (parse_extra)
9368 b->cond_string = NULL;
9369 else
9370 {
9371 /* Create a private copy of condition string. */
9372 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9373 b->thread = thread;
9374 }
9375
9376 /* Create a private copy of any extra string. */
9377 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9378 b->ignore_count = ignore_count;
9379 b->disposition = tempflag ? disp_del : disp_donttouch;
9380 b->condition_not_parsed = 1;
9381 b->enable_state = enabled ? bp_enabled : bp_disabled;
9382 if ((type_wanted != bp_breakpoint
9383 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9384 b->pspace = current_program_space;
9385
9386 install_breakpoint (internal, std::move (b), 0);
9387 }
9388
9389 if (canonical.lsals.size () > 1)
9390 {
9391 warning (_("Multiple breakpoints were set.\nUse the "
9392 "\"delete\" command to delete unwanted breakpoints."));
9393 prev_breakpoint_count = prev_bkpt_count;
9394 }
9395
9396 update_global_location_list (UGLL_MAY_INSERT);
9397
9398 return 1;
9399 }
9400
9401 /* Set a breakpoint.
9402 ARG is a string describing breakpoint address,
9403 condition, and thread.
9404 FLAG specifies if a breakpoint is hardware on,
9405 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9406 and BP_TEMPFLAG. */
9407
9408 static void
9409 break_command_1 (const char *arg, int flag, int from_tty)
9410 {
9411 int tempflag = flag & BP_TEMPFLAG;
9412 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9413 ? bp_hardware_breakpoint
9414 : bp_breakpoint);
9415
9416 event_location_up location = string_to_event_location (&arg, current_language);
9417 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9418 (location.get (), false /* is_tracepoint */);
9419
9420 create_breakpoint (get_current_arch (),
9421 location.get (),
9422 NULL, 0, arg, 1 /* parse arg */,
9423 tempflag, type_wanted,
9424 0 /* Ignore count */,
9425 pending_break_support,
9426 ops,
9427 from_tty,
9428 1 /* enabled */,
9429 0 /* internal */,
9430 0);
9431 }
9432
9433 /* Helper function for break_command_1 and disassemble_command. */
9434
9435 void
9436 resolve_sal_pc (struct symtab_and_line *sal)
9437 {
9438 CORE_ADDR pc;
9439
9440 if (sal->pc == 0 && sal->symtab != NULL)
9441 {
9442 if (!find_line_pc (sal->symtab, sal->line, &pc))
9443 error (_("No line %d in file \"%s\"."),
9444 sal->line, symtab_to_filename_for_display (sal->symtab));
9445 sal->pc = pc;
9446
9447 /* If this SAL corresponds to a breakpoint inserted using a line
9448 number, then skip the function prologue if necessary. */
9449 if (sal->explicit_line)
9450 skip_prologue_sal (sal);
9451 }
9452
9453 if (sal->section == 0 && sal->symtab != NULL)
9454 {
9455 const struct blockvector *bv;
9456 const struct block *b;
9457 struct symbol *sym;
9458
9459 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9460 SYMTAB_COMPUNIT (sal->symtab));
9461 if (bv != NULL)
9462 {
9463 sym = block_linkage_function (b);
9464 if (sym != NULL)
9465 {
9466 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9467 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9468 sym);
9469 }
9470 else
9471 {
9472 /* It really is worthwhile to have the section, so we'll
9473 just have to look harder. This case can be executed
9474 if we have line numbers but no functions (as can
9475 happen in assembly source). */
9476
9477 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9478 switch_to_program_space_and_thread (sal->pspace);
9479
9480 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9481 if (msym.minsym)
9482 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9483 }
9484 }
9485 }
9486 }
9487
9488 void
9489 break_command (const char *arg, int from_tty)
9490 {
9491 break_command_1 (arg, 0, from_tty);
9492 }
9493
9494 void
9495 tbreak_command (const char *arg, int from_tty)
9496 {
9497 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9498 }
9499
9500 static void
9501 hbreak_command (const char *arg, int from_tty)
9502 {
9503 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9504 }
9505
9506 static void
9507 thbreak_command (const char *arg, int from_tty)
9508 {
9509 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9510 }
9511
9512 static void
9513 stop_command (const char *arg, int from_tty)
9514 {
9515 printf_filtered (_("Specify the type of breakpoint to set.\n\
9516 Usage: stop in <function | address>\n\
9517 stop at <line>\n"));
9518 }
9519
9520 static void
9521 stopin_command (const char *arg, int from_tty)
9522 {
9523 int badInput = 0;
9524
9525 if (arg == NULL)
9526 badInput = 1;
9527 else if (*arg != '*')
9528 {
9529 const char *argptr = arg;
9530 int hasColon = 0;
9531
9532 /* Look for a ':'. If this is a line number specification, then
9533 say it is bad, otherwise, it should be an address or
9534 function/method name. */
9535 while (*argptr && !hasColon)
9536 {
9537 hasColon = (*argptr == ':');
9538 argptr++;
9539 }
9540
9541 if (hasColon)
9542 badInput = (*argptr != ':'); /* Not a class::method */
9543 else
9544 badInput = isdigit (*arg); /* a simple line number */
9545 }
9546
9547 if (badInput)
9548 printf_filtered (_("Usage: stop in <function | address>\n"));
9549 else
9550 break_command_1 (arg, 0, from_tty);
9551 }
9552
9553 static void
9554 stopat_command (const char *arg, int from_tty)
9555 {
9556 int badInput = 0;
9557
9558 if (arg == NULL || *arg == '*') /* no line number */
9559 badInput = 1;
9560 else
9561 {
9562 const char *argptr = arg;
9563 int hasColon = 0;
9564
9565 /* Look for a ':'. If there is a '::' then get out, otherwise
9566 it is probably a line number. */
9567 while (*argptr && !hasColon)
9568 {
9569 hasColon = (*argptr == ':');
9570 argptr++;
9571 }
9572
9573 if (hasColon)
9574 badInput = (*argptr == ':'); /* we have class::method */
9575 else
9576 badInput = !isdigit (*arg); /* not a line number */
9577 }
9578
9579 if (badInput)
9580 printf_filtered (_("Usage: stop at LINE\n"));
9581 else
9582 break_command_1 (arg, 0, from_tty);
9583 }
9584
9585 /* The dynamic printf command is mostly like a regular breakpoint, but
9586 with a prewired command list consisting of a single output command,
9587 built from extra arguments supplied on the dprintf command
9588 line. */
9589
9590 static void
9591 dprintf_command (const char *arg, int from_tty)
9592 {
9593 event_location_up location = string_to_event_location (&arg, current_language);
9594
9595 /* If non-NULL, ARG should have been advanced past the location;
9596 the next character must be ','. */
9597 if (arg != NULL)
9598 {
9599 if (arg[0] != ',' || arg[1] == '\0')
9600 error (_("Format string required"));
9601 else
9602 {
9603 /* Skip the comma. */
9604 ++arg;
9605 }
9606 }
9607
9608 create_breakpoint (get_current_arch (),
9609 location.get (),
9610 NULL, 0, arg, 1 /* parse arg */,
9611 0, bp_dprintf,
9612 0 /* Ignore count */,
9613 pending_break_support,
9614 &dprintf_breakpoint_ops,
9615 from_tty,
9616 1 /* enabled */,
9617 0 /* internal */,
9618 0);
9619 }
9620
9621 static void
9622 agent_printf_command (const char *arg, int from_tty)
9623 {
9624 error (_("May only run agent-printf on the target"));
9625 }
9626
9627 /* Implement the "breakpoint_hit" breakpoint_ops method for
9628 ranged breakpoints. */
9629
9630 static int
9631 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9632 const address_space *aspace,
9633 CORE_ADDR bp_addr,
9634 const struct target_waitstatus *ws)
9635 {
9636 if (ws->kind != TARGET_WAITKIND_STOPPED
9637 || ws->value.sig != GDB_SIGNAL_TRAP)
9638 return 0;
9639
9640 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9641 bl->length, aspace, bp_addr);
9642 }
9643
9644 /* Implement the "resources_needed" breakpoint_ops method for
9645 ranged breakpoints. */
9646
9647 static int
9648 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9649 {
9650 return target_ranged_break_num_registers ();
9651 }
9652
9653 /* Implement the "print_it" breakpoint_ops method for
9654 ranged breakpoints. */
9655
9656 static enum print_stop_action
9657 print_it_ranged_breakpoint (bpstat bs)
9658 {
9659 struct breakpoint *b = bs->breakpoint_at;
9660 struct bp_location *bl = b->loc;
9661 struct ui_out *uiout = current_uiout;
9662
9663 gdb_assert (b->type == bp_hardware_breakpoint);
9664
9665 /* Ranged breakpoints have only one location. */
9666 gdb_assert (bl && bl->next == NULL);
9667
9668 annotate_breakpoint (b->number);
9669
9670 maybe_print_thread_hit_breakpoint (uiout);
9671
9672 if (b->disposition == disp_del)
9673 uiout->text ("Temporary ranged breakpoint ");
9674 else
9675 uiout->text ("Ranged breakpoint ");
9676 if (uiout->is_mi_like_p ())
9677 {
9678 uiout->field_string ("reason",
9679 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9680 uiout->field_string ("disp", bpdisp_text (b->disposition));
9681 }
9682 uiout->field_signed ("bkptno", b->number);
9683 uiout->text (", ");
9684
9685 return PRINT_SRC_AND_LOC;
9686 }
9687
9688 /* Implement the "print_one" breakpoint_ops method for
9689 ranged breakpoints. */
9690
9691 static void
9692 print_one_ranged_breakpoint (struct breakpoint *b,
9693 struct bp_location **last_loc)
9694 {
9695 struct bp_location *bl = b->loc;
9696 struct value_print_options opts;
9697 struct ui_out *uiout = current_uiout;
9698
9699 /* Ranged breakpoints have only one location. */
9700 gdb_assert (bl && bl->next == NULL);
9701
9702 get_user_print_options (&opts);
9703
9704 if (opts.addressprint)
9705 /* We don't print the address range here, it will be printed later
9706 by print_one_detail_ranged_breakpoint. */
9707 uiout->field_skip ("addr");
9708 annotate_field (5);
9709 print_breakpoint_location (b, bl);
9710 *last_loc = bl;
9711 }
9712
9713 /* Implement the "print_one_detail" breakpoint_ops method for
9714 ranged breakpoints. */
9715
9716 static void
9717 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9718 struct ui_out *uiout)
9719 {
9720 CORE_ADDR address_start, address_end;
9721 struct bp_location *bl = b->loc;
9722 string_file stb;
9723
9724 gdb_assert (bl);
9725
9726 address_start = bl->address;
9727 address_end = address_start + bl->length - 1;
9728
9729 uiout->text ("\taddress range: ");
9730 stb.printf ("[%s, %s]",
9731 print_core_address (bl->gdbarch, address_start),
9732 print_core_address (bl->gdbarch, address_end));
9733 uiout->field_stream ("addr", stb);
9734 uiout->text ("\n");
9735 }
9736
9737 /* Implement the "print_mention" breakpoint_ops method for
9738 ranged breakpoints. */
9739
9740 static void
9741 print_mention_ranged_breakpoint (struct breakpoint *b)
9742 {
9743 struct bp_location *bl = b->loc;
9744 struct ui_out *uiout = current_uiout;
9745
9746 gdb_assert (bl);
9747 gdb_assert (b->type == bp_hardware_breakpoint);
9748
9749 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9750 b->number, paddress (bl->gdbarch, bl->address),
9751 paddress (bl->gdbarch, bl->address + bl->length - 1));
9752 }
9753
9754 /* Implement the "print_recreate" breakpoint_ops method for
9755 ranged breakpoints. */
9756
9757 static void
9758 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9759 {
9760 fprintf_unfiltered (fp, "break-range %s, %s",
9761 event_location_to_string (b->location.get ()),
9762 event_location_to_string (b->location_range_end.get ()));
9763 print_recreate_thread (b, fp);
9764 }
9765
9766 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9767
9768 static struct breakpoint_ops ranged_breakpoint_ops;
9769
9770 /* Find the address where the end of the breakpoint range should be
9771 placed, given the SAL of the end of the range. This is so that if
9772 the user provides a line number, the end of the range is set to the
9773 last instruction of the given line. */
9774
9775 static CORE_ADDR
9776 find_breakpoint_range_end (struct symtab_and_line sal)
9777 {
9778 CORE_ADDR end;
9779
9780 /* If the user provided a PC value, use it. Otherwise,
9781 find the address of the end of the given location. */
9782 if (sal.explicit_pc)
9783 end = sal.pc;
9784 else
9785 {
9786 int ret;
9787 CORE_ADDR start;
9788
9789 ret = find_line_pc_range (sal, &start, &end);
9790 if (!ret)
9791 error (_("Could not find location of the end of the range."));
9792
9793 /* find_line_pc_range returns the start of the next line. */
9794 end--;
9795 }
9796
9797 return end;
9798 }
9799
9800 /* Implement the "break-range" CLI command. */
9801
9802 static void
9803 break_range_command (const char *arg, int from_tty)
9804 {
9805 const char *arg_start;
9806 struct linespec_result canonical_start, canonical_end;
9807 int bp_count, can_use_bp, length;
9808 CORE_ADDR end;
9809 struct breakpoint *b;
9810
9811 /* We don't support software ranged breakpoints. */
9812 if (target_ranged_break_num_registers () < 0)
9813 error (_("This target does not support hardware ranged breakpoints."));
9814
9815 bp_count = hw_breakpoint_used_count ();
9816 bp_count += target_ranged_break_num_registers ();
9817 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9818 bp_count, 0);
9819 if (can_use_bp < 0)
9820 error (_("Hardware breakpoints used exceeds limit."));
9821
9822 arg = skip_spaces (arg);
9823 if (arg == NULL || arg[0] == '\0')
9824 error(_("No address range specified."));
9825
9826 arg_start = arg;
9827 event_location_up start_location = string_to_event_location (&arg,
9828 current_language);
9829 parse_breakpoint_sals (start_location.get (), &canonical_start);
9830
9831 if (arg[0] != ',')
9832 error (_("Too few arguments."));
9833 else if (canonical_start.lsals.empty ())
9834 error (_("Could not find location of the beginning of the range."));
9835
9836 const linespec_sals &lsal_start = canonical_start.lsals[0];
9837
9838 if (canonical_start.lsals.size () > 1
9839 || lsal_start.sals.size () != 1)
9840 error (_("Cannot create a ranged breakpoint with multiple locations."));
9841
9842 const symtab_and_line &sal_start = lsal_start.sals[0];
9843 std::string addr_string_start (arg_start, arg - arg_start);
9844
9845 arg++; /* Skip the comma. */
9846 arg = skip_spaces (arg);
9847
9848 /* Parse the end location. */
9849
9850 arg_start = arg;
9851
9852 /* We call decode_line_full directly here instead of using
9853 parse_breakpoint_sals because we need to specify the start location's
9854 symtab and line as the default symtab and line for the end of the
9855 range. This makes it possible to have ranges like "foo.c:27, +14",
9856 where +14 means 14 lines from the start location. */
9857 event_location_up end_location = string_to_event_location (&arg,
9858 current_language);
9859 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9860 sal_start.symtab, sal_start.line,
9861 &canonical_end, NULL, NULL);
9862
9863 if (canonical_end.lsals.empty ())
9864 error (_("Could not find location of the end of the range."));
9865
9866 const linespec_sals &lsal_end = canonical_end.lsals[0];
9867 if (canonical_end.lsals.size () > 1
9868 || lsal_end.sals.size () != 1)
9869 error (_("Cannot create a ranged breakpoint with multiple locations."));
9870
9871 const symtab_and_line &sal_end = lsal_end.sals[0];
9872
9873 end = find_breakpoint_range_end (sal_end);
9874 if (sal_start.pc > end)
9875 error (_("Invalid address range, end precedes start."));
9876
9877 length = end - sal_start.pc + 1;
9878 if (length < 0)
9879 /* Length overflowed. */
9880 error (_("Address range too large."));
9881 else if (length == 1)
9882 {
9883 /* This range is simple enough to be handled by
9884 the `hbreak' command. */
9885 hbreak_command (&addr_string_start[0], 1);
9886
9887 return;
9888 }
9889
9890 /* Now set up the breakpoint. */
9891 b = set_raw_breakpoint (get_current_arch (), sal_start,
9892 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9893 set_breakpoint_count (breakpoint_count + 1);
9894 b->number = breakpoint_count;
9895 b->disposition = disp_donttouch;
9896 b->location = std::move (start_location);
9897 b->location_range_end = std::move (end_location);
9898 b->loc->length = length;
9899
9900 mention (b);
9901 gdb::observers::breakpoint_created.notify (b);
9902 update_global_location_list (UGLL_MAY_INSERT);
9903 }
9904
9905 /* Return non-zero if EXP is verified as constant. Returned zero
9906 means EXP is variable. Also the constant detection may fail for
9907 some constant expressions and in such case still falsely return
9908 zero. */
9909
9910 static int
9911 watchpoint_exp_is_const (const struct expression *exp)
9912 {
9913 int i = exp->nelts;
9914
9915 while (i > 0)
9916 {
9917 int oplenp, argsp;
9918
9919 /* We are only interested in the descriptor of each element. */
9920 operator_length (exp, i, &oplenp, &argsp);
9921 i -= oplenp;
9922
9923 switch (exp->elts[i].opcode)
9924 {
9925 case BINOP_ADD:
9926 case BINOP_SUB:
9927 case BINOP_MUL:
9928 case BINOP_DIV:
9929 case BINOP_REM:
9930 case BINOP_MOD:
9931 case BINOP_LSH:
9932 case BINOP_RSH:
9933 case BINOP_LOGICAL_AND:
9934 case BINOP_LOGICAL_OR:
9935 case BINOP_BITWISE_AND:
9936 case BINOP_BITWISE_IOR:
9937 case BINOP_BITWISE_XOR:
9938 case BINOP_EQUAL:
9939 case BINOP_NOTEQUAL:
9940 case BINOP_LESS:
9941 case BINOP_GTR:
9942 case BINOP_LEQ:
9943 case BINOP_GEQ:
9944 case BINOP_REPEAT:
9945 case BINOP_COMMA:
9946 case BINOP_EXP:
9947 case BINOP_MIN:
9948 case BINOP_MAX:
9949 case BINOP_INTDIV:
9950 case BINOP_CONCAT:
9951 case TERNOP_COND:
9952 case TERNOP_SLICE:
9953
9954 case OP_LONG:
9955 case OP_FLOAT:
9956 case OP_LAST:
9957 case OP_COMPLEX:
9958 case OP_STRING:
9959 case OP_ARRAY:
9960 case OP_TYPE:
9961 case OP_TYPEOF:
9962 case OP_DECLTYPE:
9963 case OP_TYPEID:
9964 case OP_NAME:
9965 case OP_OBJC_NSSTRING:
9966
9967 case UNOP_NEG:
9968 case UNOP_LOGICAL_NOT:
9969 case UNOP_COMPLEMENT:
9970 case UNOP_ADDR:
9971 case UNOP_HIGH:
9972 case UNOP_CAST:
9973
9974 case UNOP_CAST_TYPE:
9975 case UNOP_REINTERPRET_CAST:
9976 case UNOP_DYNAMIC_CAST:
9977 /* Unary, binary and ternary operators: We have to check
9978 their operands. If they are constant, then so is the
9979 result of that operation. For instance, if A and B are
9980 determined to be constants, then so is "A + B".
9981
9982 UNOP_IND is one exception to the rule above, because the
9983 value of *ADDR is not necessarily a constant, even when
9984 ADDR is. */
9985 break;
9986
9987 case OP_VAR_VALUE:
9988 /* Check whether the associated symbol is a constant.
9989
9990 We use SYMBOL_CLASS rather than TYPE_CONST because it's
9991 possible that a buggy compiler could mark a variable as
9992 constant even when it is not, and TYPE_CONST would return
9993 true in this case, while SYMBOL_CLASS wouldn't.
9994
9995 We also have to check for function symbols because they
9996 are always constant. */
9997 {
9998 struct symbol *s = exp->elts[i + 2].symbol;
9999
10000 if (SYMBOL_CLASS (s) != LOC_BLOCK
10001 && SYMBOL_CLASS (s) != LOC_CONST
10002 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10003 return 0;
10004 break;
10005 }
10006
10007 /* The default action is to return 0 because we are using
10008 the optimistic approach here: If we don't know something,
10009 then it is not a constant. */
10010 default:
10011 return 0;
10012 }
10013 }
10014
10015 return 1;
10016 }
10017
10018 /* Watchpoint destructor. */
10019
10020 watchpoint::~watchpoint ()
10021 {
10022 xfree (this->exp_string);
10023 xfree (this->exp_string_reparse);
10024 }
10025
10026 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10027
10028 static void
10029 re_set_watchpoint (struct breakpoint *b)
10030 {
10031 struct watchpoint *w = (struct watchpoint *) b;
10032
10033 /* Watchpoint can be either on expression using entirely global
10034 variables, or it can be on local variables.
10035
10036 Watchpoints of the first kind are never auto-deleted, and even
10037 persist across program restarts. Since they can use variables
10038 from shared libraries, we need to reparse expression as libraries
10039 are loaded and unloaded.
10040
10041 Watchpoints on local variables can also change meaning as result
10042 of solib event. For example, if a watchpoint uses both a local
10043 and a global variables in expression, it's a local watchpoint,
10044 but unloading of a shared library will make the expression
10045 invalid. This is not a very common use case, but we still
10046 re-evaluate expression, to avoid surprises to the user.
10047
10048 Note that for local watchpoints, we re-evaluate it only if
10049 watchpoints frame id is still valid. If it's not, it means the
10050 watchpoint is out of scope and will be deleted soon. In fact,
10051 I'm not sure we'll ever be called in this case.
10052
10053 If a local watchpoint's frame id is still valid, then
10054 w->exp_valid_block is likewise valid, and we can safely use it.
10055
10056 Don't do anything about disabled watchpoints, since they will be
10057 reevaluated again when enabled. */
10058 update_watchpoint (w, 1 /* reparse */);
10059 }
10060
10061 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10062
10063 static int
10064 insert_watchpoint (struct bp_location *bl)
10065 {
10066 struct watchpoint *w = (struct watchpoint *) bl->owner;
10067 int length = w->exact ? 1 : bl->length;
10068
10069 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10070 w->cond_exp.get ());
10071 }
10072
10073 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10074
10075 static int
10076 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10077 {
10078 struct watchpoint *w = (struct watchpoint *) bl->owner;
10079 int length = w->exact ? 1 : bl->length;
10080
10081 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10082 w->cond_exp.get ());
10083 }
10084
10085 static int
10086 breakpoint_hit_watchpoint (const struct bp_location *bl,
10087 const address_space *aspace, CORE_ADDR bp_addr,
10088 const struct target_waitstatus *ws)
10089 {
10090 struct breakpoint *b = bl->owner;
10091 struct watchpoint *w = (struct watchpoint *) b;
10092
10093 /* Continuable hardware watchpoints are treated as non-existent if the
10094 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10095 some data address). Otherwise gdb won't stop on a break instruction
10096 in the code (not from a breakpoint) when a hardware watchpoint has
10097 been defined. Also skip watchpoints which we know did not trigger
10098 (did not match the data address). */
10099 if (is_hardware_watchpoint (b)
10100 && w->watchpoint_triggered == watch_triggered_no)
10101 return 0;
10102
10103 return 1;
10104 }
10105
10106 static void
10107 check_status_watchpoint (bpstat bs)
10108 {
10109 gdb_assert (is_watchpoint (bs->breakpoint_at));
10110
10111 bpstat_check_watchpoint (bs);
10112 }
10113
10114 /* Implement the "resources_needed" breakpoint_ops method for
10115 hardware watchpoints. */
10116
10117 static int
10118 resources_needed_watchpoint (const struct bp_location *bl)
10119 {
10120 struct watchpoint *w = (struct watchpoint *) bl->owner;
10121 int length = w->exact? 1 : bl->length;
10122
10123 return target_region_ok_for_hw_watchpoint (bl->address, length);
10124 }
10125
10126 /* Implement the "works_in_software_mode" breakpoint_ops method for
10127 hardware watchpoints. */
10128
10129 static int
10130 works_in_software_mode_watchpoint (const struct breakpoint *b)
10131 {
10132 /* Read and access watchpoints only work with hardware support. */
10133 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10134 }
10135
10136 static enum print_stop_action
10137 print_it_watchpoint (bpstat bs)
10138 {
10139 struct breakpoint *b;
10140 enum print_stop_action result;
10141 struct watchpoint *w;
10142 struct ui_out *uiout = current_uiout;
10143
10144 gdb_assert (bs->bp_location_at != NULL);
10145
10146 b = bs->breakpoint_at;
10147 w = (struct watchpoint *) b;
10148
10149 annotate_watchpoint (b->number);
10150 maybe_print_thread_hit_breakpoint (uiout);
10151
10152 string_file stb;
10153
10154 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10155 switch (b->type)
10156 {
10157 case bp_watchpoint:
10158 case bp_hardware_watchpoint:
10159 if (uiout->is_mi_like_p ())
10160 uiout->field_string
10161 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10162 mention (b);
10163 tuple_emitter.emplace (uiout, "value");
10164 uiout->text ("\nOld value = ");
10165 watchpoint_value_print (bs->old_val.get (), &stb);
10166 uiout->field_stream ("old", stb);
10167 uiout->text ("\nNew value = ");
10168 watchpoint_value_print (w->val.get (), &stb);
10169 uiout->field_stream ("new", stb);
10170 uiout->text ("\n");
10171 /* More than one watchpoint may have been triggered. */
10172 result = PRINT_UNKNOWN;
10173 break;
10174
10175 case bp_read_watchpoint:
10176 if (uiout->is_mi_like_p ())
10177 uiout->field_string
10178 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10179 mention (b);
10180 tuple_emitter.emplace (uiout, "value");
10181 uiout->text ("\nValue = ");
10182 watchpoint_value_print (w->val.get (), &stb);
10183 uiout->field_stream ("value", stb);
10184 uiout->text ("\n");
10185 result = PRINT_UNKNOWN;
10186 break;
10187
10188 case bp_access_watchpoint:
10189 if (bs->old_val != NULL)
10190 {
10191 if (uiout->is_mi_like_p ())
10192 uiout->field_string
10193 ("reason",
10194 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10195 mention (b);
10196 tuple_emitter.emplace (uiout, "value");
10197 uiout->text ("\nOld value = ");
10198 watchpoint_value_print (bs->old_val.get (), &stb);
10199 uiout->field_stream ("old", stb);
10200 uiout->text ("\nNew value = ");
10201 }
10202 else
10203 {
10204 mention (b);
10205 if (uiout->is_mi_like_p ())
10206 uiout->field_string
10207 ("reason",
10208 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10209 tuple_emitter.emplace (uiout, "value");
10210 uiout->text ("\nValue = ");
10211 }
10212 watchpoint_value_print (w->val.get (), &stb);
10213 uiout->field_stream ("new", stb);
10214 uiout->text ("\n");
10215 result = PRINT_UNKNOWN;
10216 break;
10217 default:
10218 result = PRINT_UNKNOWN;
10219 }
10220
10221 return result;
10222 }
10223
10224 /* Implement the "print_mention" breakpoint_ops method for hardware
10225 watchpoints. */
10226
10227 static void
10228 print_mention_watchpoint (struct breakpoint *b)
10229 {
10230 struct watchpoint *w = (struct watchpoint *) b;
10231 struct ui_out *uiout = current_uiout;
10232 const char *tuple_name;
10233
10234 switch (b->type)
10235 {
10236 case bp_watchpoint:
10237 uiout->text ("Watchpoint ");
10238 tuple_name = "wpt";
10239 break;
10240 case bp_hardware_watchpoint:
10241 uiout->text ("Hardware watchpoint ");
10242 tuple_name = "wpt";
10243 break;
10244 case bp_read_watchpoint:
10245 uiout->text ("Hardware read watchpoint ");
10246 tuple_name = "hw-rwpt";
10247 break;
10248 case bp_access_watchpoint:
10249 uiout->text ("Hardware access (read/write) watchpoint ");
10250 tuple_name = "hw-awpt";
10251 break;
10252 default:
10253 internal_error (__FILE__, __LINE__,
10254 _("Invalid hardware watchpoint type."));
10255 }
10256
10257 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10258 uiout->field_signed ("number", b->number);
10259 uiout->text (": ");
10260 uiout->field_string ("exp", w->exp_string);
10261 }
10262
10263 /* Implement the "print_recreate" breakpoint_ops method for
10264 watchpoints. */
10265
10266 static void
10267 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10268 {
10269 struct watchpoint *w = (struct watchpoint *) b;
10270
10271 switch (b->type)
10272 {
10273 case bp_watchpoint:
10274 case bp_hardware_watchpoint:
10275 fprintf_unfiltered (fp, "watch");
10276 break;
10277 case bp_read_watchpoint:
10278 fprintf_unfiltered (fp, "rwatch");
10279 break;
10280 case bp_access_watchpoint:
10281 fprintf_unfiltered (fp, "awatch");
10282 break;
10283 default:
10284 internal_error (__FILE__, __LINE__,
10285 _("Invalid watchpoint type."));
10286 }
10287
10288 fprintf_unfiltered (fp, " %s", w->exp_string);
10289 print_recreate_thread (b, fp);
10290 }
10291
10292 /* Implement the "explains_signal" breakpoint_ops method for
10293 watchpoints. */
10294
10295 static int
10296 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10297 {
10298 /* A software watchpoint cannot cause a signal other than
10299 GDB_SIGNAL_TRAP. */
10300 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10301 return 0;
10302
10303 return 1;
10304 }
10305
10306 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10307
10308 static struct breakpoint_ops watchpoint_breakpoint_ops;
10309
10310 /* Implement the "insert" breakpoint_ops method for
10311 masked hardware watchpoints. */
10312
10313 static int
10314 insert_masked_watchpoint (struct bp_location *bl)
10315 {
10316 struct watchpoint *w = (struct watchpoint *) bl->owner;
10317
10318 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10319 bl->watchpoint_type);
10320 }
10321
10322 /* Implement the "remove" breakpoint_ops method for
10323 masked hardware watchpoints. */
10324
10325 static int
10326 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10327 {
10328 struct watchpoint *w = (struct watchpoint *) bl->owner;
10329
10330 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10331 bl->watchpoint_type);
10332 }
10333
10334 /* Implement the "resources_needed" breakpoint_ops method for
10335 masked hardware watchpoints. */
10336
10337 static int
10338 resources_needed_masked_watchpoint (const struct bp_location *bl)
10339 {
10340 struct watchpoint *w = (struct watchpoint *) bl->owner;
10341
10342 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10343 }
10344
10345 /* Implement the "works_in_software_mode" breakpoint_ops method for
10346 masked hardware watchpoints. */
10347
10348 static int
10349 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10350 {
10351 return 0;
10352 }
10353
10354 /* Implement the "print_it" breakpoint_ops method for
10355 masked hardware watchpoints. */
10356
10357 static enum print_stop_action
10358 print_it_masked_watchpoint (bpstat bs)
10359 {
10360 struct breakpoint *b = bs->breakpoint_at;
10361 struct ui_out *uiout = current_uiout;
10362
10363 /* Masked watchpoints have only one location. */
10364 gdb_assert (b->loc && b->loc->next == NULL);
10365
10366 annotate_watchpoint (b->number);
10367 maybe_print_thread_hit_breakpoint (uiout);
10368
10369 switch (b->type)
10370 {
10371 case bp_hardware_watchpoint:
10372 if (uiout->is_mi_like_p ())
10373 uiout->field_string
10374 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10375 break;
10376
10377 case bp_read_watchpoint:
10378 if (uiout->is_mi_like_p ())
10379 uiout->field_string
10380 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10381 break;
10382
10383 case bp_access_watchpoint:
10384 if (uiout->is_mi_like_p ())
10385 uiout->field_string
10386 ("reason",
10387 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10388 break;
10389 default:
10390 internal_error (__FILE__, __LINE__,
10391 _("Invalid hardware watchpoint type."));
10392 }
10393
10394 mention (b);
10395 uiout->text (_("\n\
10396 Check the underlying instruction at PC for the memory\n\
10397 address and value which triggered this watchpoint.\n"));
10398 uiout->text ("\n");
10399
10400 /* More than one watchpoint may have been triggered. */
10401 return PRINT_UNKNOWN;
10402 }
10403
10404 /* Implement the "print_one_detail" breakpoint_ops method for
10405 masked hardware watchpoints. */
10406
10407 static void
10408 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10409 struct ui_out *uiout)
10410 {
10411 struct watchpoint *w = (struct watchpoint *) b;
10412
10413 /* Masked watchpoints have only one location. */
10414 gdb_assert (b->loc && b->loc->next == NULL);
10415
10416 uiout->text ("\tmask ");
10417 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10418 uiout->text ("\n");
10419 }
10420
10421 /* Implement the "print_mention" breakpoint_ops method for
10422 masked hardware watchpoints. */
10423
10424 static void
10425 print_mention_masked_watchpoint (struct breakpoint *b)
10426 {
10427 struct watchpoint *w = (struct watchpoint *) b;
10428 struct ui_out *uiout = current_uiout;
10429 const char *tuple_name;
10430
10431 switch (b->type)
10432 {
10433 case bp_hardware_watchpoint:
10434 uiout->text ("Masked hardware watchpoint ");
10435 tuple_name = "wpt";
10436 break;
10437 case bp_read_watchpoint:
10438 uiout->text ("Masked hardware read watchpoint ");
10439 tuple_name = "hw-rwpt";
10440 break;
10441 case bp_access_watchpoint:
10442 uiout->text ("Masked hardware access (read/write) watchpoint ");
10443 tuple_name = "hw-awpt";
10444 break;
10445 default:
10446 internal_error (__FILE__, __LINE__,
10447 _("Invalid hardware watchpoint type."));
10448 }
10449
10450 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10451 uiout->field_signed ("number", b->number);
10452 uiout->text (": ");
10453 uiout->field_string ("exp", w->exp_string);
10454 }
10455
10456 /* Implement the "print_recreate" breakpoint_ops method for
10457 masked hardware watchpoints. */
10458
10459 static void
10460 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10461 {
10462 struct watchpoint *w = (struct watchpoint *) b;
10463
10464 switch (b->type)
10465 {
10466 case bp_hardware_watchpoint:
10467 fprintf_unfiltered (fp, "watch");
10468 break;
10469 case bp_read_watchpoint:
10470 fprintf_unfiltered (fp, "rwatch");
10471 break;
10472 case bp_access_watchpoint:
10473 fprintf_unfiltered (fp, "awatch");
10474 break;
10475 default:
10476 internal_error (__FILE__, __LINE__,
10477 _("Invalid hardware watchpoint type."));
10478 }
10479
10480 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10481 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10482 print_recreate_thread (b, fp);
10483 }
10484
10485 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10486
10487 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10488
10489 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10490
10491 static bool
10492 is_masked_watchpoint (const struct breakpoint *b)
10493 {
10494 return b->ops == &masked_watchpoint_breakpoint_ops;
10495 }
10496
10497 /* accessflag: hw_write: watch write,
10498 hw_read: watch read,
10499 hw_access: watch access (read or write) */
10500 static void
10501 watch_command_1 (const char *arg, int accessflag, int from_tty,
10502 int just_location, int internal)
10503 {
10504 struct breakpoint *scope_breakpoint = NULL;
10505 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10506 struct value *result;
10507 int saved_bitpos = 0, saved_bitsize = 0;
10508 const char *exp_start = NULL;
10509 const char *exp_end = NULL;
10510 const char *tok, *end_tok;
10511 int toklen = -1;
10512 const char *cond_start = NULL;
10513 const char *cond_end = NULL;
10514 enum bptype bp_type;
10515 int thread = -1;
10516 int pc = 0;
10517 /* Flag to indicate whether we are going to use masks for
10518 the hardware watchpoint. */
10519 int use_mask = 0;
10520 CORE_ADDR mask = 0;
10521
10522 /* Make sure that we actually have parameters to parse. */
10523 if (arg != NULL && arg[0] != '\0')
10524 {
10525 const char *value_start;
10526
10527 exp_end = arg + strlen (arg);
10528
10529 /* Look for "parameter value" pairs at the end
10530 of the arguments string. */
10531 for (tok = exp_end - 1; tok > arg; tok--)
10532 {
10533 /* Skip whitespace at the end of the argument list. */
10534 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10535 tok--;
10536
10537 /* Find the beginning of the last token.
10538 This is the value of the parameter. */
10539 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10540 tok--;
10541 value_start = tok + 1;
10542
10543 /* Skip whitespace. */
10544 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10545 tok--;
10546
10547 end_tok = tok;
10548
10549 /* Find the beginning of the second to last token.
10550 This is the parameter itself. */
10551 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10552 tok--;
10553 tok++;
10554 toklen = end_tok - tok + 1;
10555
10556 if (toklen == 6 && startswith (tok, "thread"))
10557 {
10558 struct thread_info *thr;
10559 /* At this point we've found a "thread" token, which means
10560 the user is trying to set a watchpoint that triggers
10561 only in a specific thread. */
10562 const char *endp;
10563
10564 if (thread != -1)
10565 error(_("You can specify only one thread."));
10566
10567 /* Extract the thread ID from the next token. */
10568 thr = parse_thread_id (value_start, &endp);
10569
10570 /* Check if the user provided a valid thread ID. */
10571 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10572 invalid_thread_id_error (value_start);
10573
10574 thread = thr->global_num;
10575 }
10576 else if (toklen == 4 && startswith (tok, "mask"))
10577 {
10578 /* We've found a "mask" token, which means the user wants to
10579 create a hardware watchpoint that is going to have the mask
10580 facility. */
10581 struct value *mask_value, *mark;
10582
10583 if (use_mask)
10584 error(_("You can specify only one mask."));
10585
10586 use_mask = just_location = 1;
10587
10588 mark = value_mark ();
10589 mask_value = parse_to_comma_and_eval (&value_start);
10590 mask = value_as_address (mask_value);
10591 value_free_to_mark (mark);
10592 }
10593 else
10594 /* We didn't recognize what we found. We should stop here. */
10595 break;
10596
10597 /* Truncate the string and get rid of the "parameter value" pair before
10598 the arguments string is parsed by the parse_exp_1 function. */
10599 exp_end = tok;
10600 }
10601 }
10602 else
10603 exp_end = arg;
10604
10605 /* Parse the rest of the arguments. From here on out, everything
10606 is in terms of a newly allocated string instead of the original
10607 ARG. */
10608 std::string expression (arg, exp_end - arg);
10609 exp_start = arg = expression.c_str ();
10610 innermost_block_tracker tracker;
10611 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10612 exp_end = arg;
10613 /* Remove trailing whitespace from the expression before saving it.
10614 This makes the eventual display of the expression string a bit
10615 prettier. */
10616 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10617 --exp_end;
10618
10619 /* Checking if the expression is not constant. */
10620 if (watchpoint_exp_is_const (exp.get ()))
10621 {
10622 int len;
10623
10624 len = exp_end - exp_start;
10625 while (len > 0 && isspace (exp_start[len - 1]))
10626 len--;
10627 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10628 }
10629
10630 exp_valid_block = tracker.block ();
10631 struct value *mark = value_mark ();
10632 struct value *val_as_value = nullptr;
10633 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10634 just_location);
10635
10636 if (val_as_value != NULL && just_location)
10637 {
10638 saved_bitpos = value_bitpos (val_as_value);
10639 saved_bitsize = value_bitsize (val_as_value);
10640 }
10641
10642 value_ref_ptr val;
10643 if (just_location)
10644 {
10645 int ret;
10646
10647 exp_valid_block = NULL;
10648 val = release_value (value_addr (result));
10649 value_free_to_mark (mark);
10650
10651 if (use_mask)
10652 {
10653 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10654 mask);
10655 if (ret == -1)
10656 error (_("This target does not support masked watchpoints."));
10657 else if (ret == -2)
10658 error (_("Invalid mask or memory region."));
10659 }
10660 }
10661 else if (val_as_value != NULL)
10662 val = release_value (val_as_value);
10663
10664 tok = skip_spaces (arg);
10665 end_tok = skip_to_space (tok);
10666
10667 toklen = end_tok - tok;
10668 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10669 {
10670 tok = cond_start = end_tok + 1;
10671 innermost_block_tracker if_tracker;
10672 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10673
10674 /* The watchpoint expression may not be local, but the condition
10675 may still be. E.g.: `watch global if local > 0'. */
10676 cond_exp_valid_block = if_tracker.block ();
10677
10678 cond_end = tok;
10679 }
10680 if (*tok)
10681 error (_("Junk at end of command."));
10682
10683 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10684
10685 /* Save this because create_internal_breakpoint below invalidates
10686 'wp_frame'. */
10687 frame_id watchpoint_frame = get_frame_id (wp_frame);
10688
10689 /* If the expression is "local", then set up a "watchpoint scope"
10690 breakpoint at the point where we've left the scope of the watchpoint
10691 expression. Create the scope breakpoint before the watchpoint, so
10692 that we will encounter it first in bpstat_stop_status. */
10693 if (exp_valid_block != NULL && wp_frame != NULL)
10694 {
10695 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10696
10697 if (frame_id_p (caller_frame_id))
10698 {
10699 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10700 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10701
10702 scope_breakpoint
10703 = create_internal_breakpoint (caller_arch, caller_pc,
10704 bp_watchpoint_scope,
10705 &momentary_breakpoint_ops);
10706
10707 /* create_internal_breakpoint could invalidate WP_FRAME. */
10708 wp_frame = NULL;
10709
10710 scope_breakpoint->enable_state = bp_enabled;
10711
10712 /* Automatically delete the breakpoint when it hits. */
10713 scope_breakpoint->disposition = disp_del;
10714
10715 /* Only break in the proper frame (help with recursion). */
10716 scope_breakpoint->frame_id = caller_frame_id;
10717
10718 /* Set the address at which we will stop. */
10719 scope_breakpoint->loc->gdbarch = caller_arch;
10720 scope_breakpoint->loc->requested_address = caller_pc;
10721 scope_breakpoint->loc->address
10722 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10723 scope_breakpoint->loc->requested_address,
10724 scope_breakpoint->type);
10725 }
10726 }
10727
10728 /* Now set up the breakpoint. We create all watchpoints as hardware
10729 watchpoints here even if hardware watchpoints are turned off, a call
10730 to update_watchpoint later in this function will cause the type to
10731 drop back to bp_watchpoint (software watchpoint) if required. */
10732
10733 if (accessflag == hw_read)
10734 bp_type = bp_read_watchpoint;
10735 else if (accessflag == hw_access)
10736 bp_type = bp_access_watchpoint;
10737 else
10738 bp_type = bp_hardware_watchpoint;
10739
10740 std::unique_ptr<watchpoint> w (new watchpoint ());
10741
10742 if (use_mask)
10743 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10744 &masked_watchpoint_breakpoint_ops);
10745 else
10746 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10747 &watchpoint_breakpoint_ops);
10748 w->thread = thread;
10749 w->disposition = disp_donttouch;
10750 w->pspace = current_program_space;
10751 w->exp = std::move (exp);
10752 w->exp_valid_block = exp_valid_block;
10753 w->cond_exp_valid_block = cond_exp_valid_block;
10754 if (just_location)
10755 {
10756 struct type *t = value_type (val.get ());
10757 CORE_ADDR addr = value_as_address (val.get ());
10758
10759 w->exp_string_reparse
10760 = current_language->watch_location_expression (t, addr).release ();
10761
10762 w->exp_string = xstrprintf ("-location %.*s",
10763 (int) (exp_end - exp_start), exp_start);
10764 }
10765 else
10766 w->exp_string = savestring (exp_start, exp_end - exp_start);
10767
10768 if (use_mask)
10769 {
10770 w->hw_wp_mask = mask;
10771 }
10772 else
10773 {
10774 w->val = val;
10775 w->val_bitpos = saved_bitpos;
10776 w->val_bitsize = saved_bitsize;
10777 w->val_valid = true;
10778 }
10779
10780 if (cond_start)
10781 w->cond_string = savestring (cond_start, cond_end - cond_start);
10782 else
10783 w->cond_string = 0;
10784
10785 if (frame_id_p (watchpoint_frame))
10786 {
10787 w->watchpoint_frame = watchpoint_frame;
10788 w->watchpoint_thread = inferior_ptid;
10789 }
10790 else
10791 {
10792 w->watchpoint_frame = null_frame_id;
10793 w->watchpoint_thread = null_ptid;
10794 }
10795
10796 if (scope_breakpoint != NULL)
10797 {
10798 /* The scope breakpoint is related to the watchpoint. We will
10799 need to act on them together. */
10800 w->related_breakpoint = scope_breakpoint;
10801 scope_breakpoint->related_breakpoint = w.get ();
10802 }
10803
10804 if (!just_location)
10805 value_free_to_mark (mark);
10806
10807 /* Finally update the new watchpoint. This creates the locations
10808 that should be inserted. */
10809 update_watchpoint (w.get (), 1);
10810
10811 install_breakpoint (internal, std::move (w), 1);
10812 }
10813
10814 /* Return count of debug registers needed to watch the given expression.
10815 If the watchpoint cannot be handled in hardware return zero. */
10816
10817 static int
10818 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10819 {
10820 int found_memory_cnt = 0;
10821
10822 /* Did the user specifically forbid us to use hardware watchpoints? */
10823 if (!can_use_hw_watchpoints)
10824 return 0;
10825
10826 gdb_assert (!vals.empty ());
10827 struct value *head = vals[0].get ();
10828
10829 /* Make sure that the value of the expression depends only upon
10830 memory contents, and values computed from them within GDB. If we
10831 find any register references or function calls, we can't use a
10832 hardware watchpoint.
10833
10834 The idea here is that evaluating an expression generates a series
10835 of values, one holding the value of every subexpression. (The
10836 expression a*b+c has five subexpressions: a, b, a*b, c, and
10837 a*b+c.) GDB's values hold almost enough information to establish
10838 the criteria given above --- they identify memory lvalues,
10839 register lvalues, computed values, etcetera. So we can evaluate
10840 the expression, and then scan the chain of values that leaves
10841 behind to decide whether we can detect any possible change to the
10842 expression's final value using only hardware watchpoints.
10843
10844 However, I don't think that the values returned by inferior
10845 function calls are special in any way. So this function may not
10846 notice that an expression involving an inferior function call
10847 can't be watched with hardware watchpoints. FIXME. */
10848 for (const value_ref_ptr &iter : vals)
10849 {
10850 struct value *v = iter.get ();
10851
10852 if (VALUE_LVAL (v) == lval_memory)
10853 {
10854 if (v != head && value_lazy (v))
10855 /* A lazy memory lvalue in the chain is one that GDB never
10856 needed to fetch; we either just used its address (e.g.,
10857 `a' in `a.b') or we never needed it at all (e.g., `a'
10858 in `a,b'). This doesn't apply to HEAD; if that is
10859 lazy then it was not readable, but watch it anyway. */
10860 ;
10861 else
10862 {
10863 /* Ahh, memory we actually used! Check if we can cover
10864 it with hardware watchpoints. */
10865 struct type *vtype = check_typedef (value_type (v));
10866
10867 /* We only watch structs and arrays if user asked for it
10868 explicitly, never if they just happen to appear in a
10869 middle of some value chain. */
10870 if (v == head
10871 || (vtype->code () != TYPE_CODE_STRUCT
10872 && vtype->code () != TYPE_CODE_ARRAY))
10873 {
10874 CORE_ADDR vaddr = value_address (v);
10875 int len;
10876 int num_regs;
10877
10878 len = (target_exact_watchpoints
10879 && is_scalar_type_recursive (vtype))?
10880 1 : TYPE_LENGTH (value_type (v));
10881
10882 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10883 if (!num_regs)
10884 return 0;
10885 else
10886 found_memory_cnt += num_regs;
10887 }
10888 }
10889 }
10890 else if (VALUE_LVAL (v) != not_lval
10891 && deprecated_value_modifiable (v) == 0)
10892 return 0; /* These are values from the history (e.g., $1). */
10893 else if (VALUE_LVAL (v) == lval_register)
10894 return 0; /* Cannot watch a register with a HW watchpoint. */
10895 }
10896
10897 /* The expression itself looks suitable for using a hardware
10898 watchpoint, but give the target machine a chance to reject it. */
10899 return found_memory_cnt;
10900 }
10901
10902 void
10903 watch_command_wrapper (const char *arg, int from_tty, int internal)
10904 {
10905 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10906 }
10907
10908 /* A helper function that looks for the "-location" argument and then
10909 calls watch_command_1. */
10910
10911 static void
10912 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10913 {
10914 int just_location = 0;
10915
10916 if (arg
10917 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10918 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10919 just_location = 1;
10920
10921 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10922 }
10923
10924 static void
10925 watch_command (const char *arg, int from_tty)
10926 {
10927 watch_maybe_just_location (arg, hw_write, from_tty);
10928 }
10929
10930 void
10931 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
10932 {
10933 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10934 }
10935
10936 static void
10937 rwatch_command (const char *arg, int from_tty)
10938 {
10939 watch_maybe_just_location (arg, hw_read, from_tty);
10940 }
10941
10942 void
10943 awatch_command_wrapper (const char *arg, int from_tty, int internal)
10944 {
10945 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10946 }
10947
10948 static void
10949 awatch_command (const char *arg, int from_tty)
10950 {
10951 watch_maybe_just_location (arg, hw_access, from_tty);
10952 }
10953 \f
10954
10955 /* Data for the FSM that manages the until(location)/advance commands
10956 in infcmd.c. Here because it uses the mechanisms of
10957 breakpoints. */
10958
10959 struct until_break_fsm : public thread_fsm
10960 {
10961 /* The thread that was current when the command was executed. */
10962 int thread;
10963
10964 /* The breakpoint set at the destination location. */
10965 breakpoint_up location_breakpoint;
10966
10967 /* Breakpoint set at the return address in the caller frame. May be
10968 NULL. */
10969 breakpoint_up caller_breakpoint;
10970
10971 until_break_fsm (struct interp *cmd_interp, int thread,
10972 breakpoint_up &&location_breakpoint,
10973 breakpoint_up &&caller_breakpoint)
10974 : thread_fsm (cmd_interp),
10975 thread (thread),
10976 location_breakpoint (std::move (location_breakpoint)),
10977 caller_breakpoint (std::move (caller_breakpoint))
10978 {
10979 }
10980
10981 void clean_up (struct thread_info *thread) override;
10982 bool should_stop (struct thread_info *thread) override;
10983 enum async_reply_reason do_async_reply_reason () override;
10984 };
10985
10986 /* Implementation of the 'should_stop' FSM method for the
10987 until(location)/advance commands. */
10988
10989 bool
10990 until_break_fsm::should_stop (struct thread_info *tp)
10991 {
10992 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10993 location_breakpoint.get ()) != NULL
10994 || (caller_breakpoint != NULL
10995 && bpstat_find_breakpoint (tp->control.stop_bpstat,
10996 caller_breakpoint.get ()) != NULL))
10997 set_finished ();
10998
10999 return true;
11000 }
11001
11002 /* Implementation of the 'clean_up' FSM method for the
11003 until(location)/advance commands. */
11004
11005 void
11006 until_break_fsm::clean_up (struct thread_info *)
11007 {
11008 /* Clean up our temporary breakpoints. */
11009 location_breakpoint.reset ();
11010 caller_breakpoint.reset ();
11011 delete_longjmp_breakpoint (thread);
11012 }
11013
11014 /* Implementation of the 'async_reply_reason' FSM method for the
11015 until(location)/advance commands. */
11016
11017 enum async_reply_reason
11018 until_break_fsm::do_async_reply_reason ()
11019 {
11020 return EXEC_ASYNC_LOCATION_REACHED;
11021 }
11022
11023 void
11024 until_break_command (const char *arg, int from_tty, int anywhere)
11025 {
11026 struct frame_info *frame;
11027 struct gdbarch *frame_gdbarch;
11028 struct frame_id stack_frame_id;
11029 struct frame_id caller_frame_id;
11030 int thread;
11031 struct thread_info *tp;
11032
11033 clear_proceed_status (0);
11034
11035 /* Set a breakpoint where the user wants it and at return from
11036 this function. */
11037
11038 event_location_up location = string_to_event_location (&arg, current_language);
11039
11040 std::vector<symtab_and_line> sals
11041 = (last_displayed_sal_is_valid ()
11042 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11043 get_last_displayed_symtab (),
11044 get_last_displayed_line ())
11045 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11046 NULL, NULL, 0));
11047
11048 if (sals.size () != 1)
11049 error (_("Couldn't get information on specified line."));
11050
11051 symtab_and_line &sal = sals[0];
11052
11053 if (*arg)
11054 error (_("Junk at end of arguments."));
11055
11056 resolve_sal_pc (&sal);
11057
11058 tp = inferior_thread ();
11059 thread = tp->global_num;
11060
11061 /* Note linespec handling above invalidates the frame chain.
11062 Installing a breakpoint also invalidates the frame chain (as it
11063 may need to switch threads), so do any frame handling before
11064 that. */
11065
11066 frame = get_selected_frame (NULL);
11067 frame_gdbarch = get_frame_arch (frame);
11068 stack_frame_id = get_stack_frame_id (frame);
11069 caller_frame_id = frame_unwind_caller_id (frame);
11070
11071 /* Keep within the current frame, or in frames called by the current
11072 one. */
11073
11074 breakpoint_up caller_breakpoint;
11075
11076 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11077
11078 if (frame_id_p (caller_frame_id))
11079 {
11080 struct symtab_and_line sal2;
11081 struct gdbarch *caller_gdbarch;
11082
11083 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11084 sal2.pc = frame_unwind_caller_pc (frame);
11085 caller_gdbarch = frame_unwind_caller_arch (frame);
11086 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11087 sal2,
11088 caller_frame_id,
11089 bp_until);
11090
11091 set_longjmp_breakpoint (tp, caller_frame_id);
11092 lj_deleter.emplace (thread);
11093 }
11094
11095 /* set_momentary_breakpoint could invalidate FRAME. */
11096 frame = NULL;
11097
11098 breakpoint_up location_breakpoint;
11099 if (anywhere)
11100 /* If the user told us to continue until a specified location,
11101 we don't specify a frame at which we need to stop. */
11102 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11103 null_frame_id, bp_until);
11104 else
11105 /* Otherwise, specify the selected frame, because we want to stop
11106 only at the very same frame. */
11107 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11108 stack_frame_id, bp_until);
11109
11110 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11111 std::move (location_breakpoint),
11112 std::move (caller_breakpoint));
11113
11114 if (lj_deleter)
11115 lj_deleter->release ();
11116
11117 proceed (-1, GDB_SIGNAL_DEFAULT);
11118 }
11119
11120 /* This function attempts to parse an optional "if <cond>" clause
11121 from the arg string. If one is not found, it returns NULL.
11122
11123 Else, it returns a pointer to the condition string. (It does not
11124 attempt to evaluate the string against a particular block.) And,
11125 it updates arg to point to the first character following the parsed
11126 if clause in the arg string. */
11127
11128 const char *
11129 ep_parse_optional_if_clause (const char **arg)
11130 {
11131 const char *cond_string;
11132
11133 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11134 return NULL;
11135
11136 /* Skip the "if" keyword. */
11137 (*arg) += 2;
11138
11139 /* Skip any extra leading whitespace, and record the start of the
11140 condition string. */
11141 *arg = skip_spaces (*arg);
11142 cond_string = *arg;
11143
11144 /* Assume that the condition occupies the remainder of the arg
11145 string. */
11146 (*arg) += strlen (cond_string);
11147
11148 return cond_string;
11149 }
11150
11151 /* Commands to deal with catching events, such as signals, exceptions,
11152 process start/exit, etc. */
11153
11154 typedef enum
11155 {
11156 catch_fork_temporary, catch_vfork_temporary,
11157 catch_fork_permanent, catch_vfork_permanent
11158 }
11159 catch_fork_kind;
11160
11161 static void
11162 catch_fork_command_1 (const char *arg, int from_tty,
11163 struct cmd_list_element *command)
11164 {
11165 struct gdbarch *gdbarch = get_current_arch ();
11166 const char *cond_string = NULL;
11167 catch_fork_kind fork_kind;
11168 int tempflag;
11169
11170 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11171 tempflag = (fork_kind == catch_fork_temporary
11172 || fork_kind == catch_vfork_temporary);
11173
11174 if (!arg)
11175 arg = "";
11176 arg = skip_spaces (arg);
11177
11178 /* The allowed syntax is:
11179 catch [v]fork
11180 catch [v]fork if <cond>
11181
11182 First, check if there's an if clause. */
11183 cond_string = ep_parse_optional_if_clause (&arg);
11184
11185 if ((*arg != '\0') && !isspace (*arg))
11186 error (_("Junk at end of arguments."));
11187
11188 /* If this target supports it, create a fork or vfork catchpoint
11189 and enable reporting of such events. */
11190 switch (fork_kind)
11191 {
11192 case catch_fork_temporary:
11193 case catch_fork_permanent:
11194 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11195 &catch_fork_breakpoint_ops);
11196 break;
11197 case catch_vfork_temporary:
11198 case catch_vfork_permanent:
11199 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11200 &catch_vfork_breakpoint_ops);
11201 break;
11202 default:
11203 error (_("unsupported or unknown fork kind; cannot catch it"));
11204 break;
11205 }
11206 }
11207
11208 static void
11209 catch_exec_command_1 (const char *arg, int from_tty,
11210 struct cmd_list_element *command)
11211 {
11212 struct gdbarch *gdbarch = get_current_arch ();
11213 int tempflag;
11214 const char *cond_string = NULL;
11215
11216 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11217
11218 if (!arg)
11219 arg = "";
11220 arg = skip_spaces (arg);
11221
11222 /* The allowed syntax is:
11223 catch exec
11224 catch exec if <cond>
11225
11226 First, check if there's an if clause. */
11227 cond_string = ep_parse_optional_if_clause (&arg);
11228
11229 if ((*arg != '\0') && !isspace (*arg))
11230 error (_("Junk at end of arguments."));
11231
11232 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11233 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11234 &catch_exec_breakpoint_ops);
11235 c->exec_pathname = NULL;
11236
11237 install_breakpoint (0, std::move (c), 1);
11238 }
11239
11240 void
11241 init_ada_exception_breakpoint (struct breakpoint *b,
11242 struct gdbarch *gdbarch,
11243 struct symtab_and_line sal,
11244 const char *addr_string,
11245 const struct breakpoint_ops *ops,
11246 int tempflag,
11247 int enabled,
11248 int from_tty)
11249 {
11250 if (from_tty)
11251 {
11252 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11253 if (!loc_gdbarch)
11254 loc_gdbarch = gdbarch;
11255
11256 describe_other_breakpoints (loc_gdbarch,
11257 sal.pspace, sal.pc, sal.section, -1);
11258 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11259 version for exception catchpoints, because two catchpoints
11260 used for different exception names will use the same address.
11261 In this case, a "breakpoint ... also set at..." warning is
11262 unproductive. Besides, the warning phrasing is also a bit
11263 inappropriate, we should use the word catchpoint, and tell
11264 the user what type of catchpoint it is. The above is good
11265 enough for now, though. */
11266 }
11267
11268 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11269
11270 b->enable_state = enabled ? bp_enabled : bp_disabled;
11271 b->disposition = tempflag ? disp_del : disp_donttouch;
11272 b->location = string_to_event_location (&addr_string,
11273 language_def (language_ada));
11274 b->language = language_ada;
11275 }
11276
11277 \f
11278
11279 /* Compare two breakpoints and return a strcmp-like result. */
11280
11281 static int
11282 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11283 {
11284 uintptr_t ua = (uintptr_t) a;
11285 uintptr_t ub = (uintptr_t) b;
11286
11287 if (a->number < b->number)
11288 return -1;
11289 else if (a->number > b->number)
11290 return 1;
11291
11292 /* Now sort by address, in case we see, e..g, two breakpoints with
11293 the number 0. */
11294 if (ua < ub)
11295 return -1;
11296 return ua > ub ? 1 : 0;
11297 }
11298
11299 /* Delete breakpoints by address or line. */
11300
11301 static void
11302 clear_command (const char *arg, int from_tty)
11303 {
11304 struct breakpoint *b;
11305 int default_match;
11306
11307 std::vector<symtab_and_line> decoded_sals;
11308 symtab_and_line last_sal;
11309 gdb::array_view<symtab_and_line> sals;
11310 if (arg)
11311 {
11312 decoded_sals
11313 = decode_line_with_current_source (arg,
11314 (DECODE_LINE_FUNFIRSTLINE
11315 | DECODE_LINE_LIST_MODE));
11316 default_match = 0;
11317 sals = decoded_sals;
11318 }
11319 else
11320 {
11321 /* Set sal's line, symtab, pc, and pspace to the values
11322 corresponding to the last call to print_frame_info. If the
11323 codepoint is not valid, this will set all the fields to 0. */
11324 last_sal = get_last_displayed_sal ();
11325 if (last_sal.symtab == 0)
11326 error (_("No source file specified."));
11327
11328 default_match = 1;
11329 sals = last_sal;
11330 }
11331
11332 /* We don't call resolve_sal_pc here. That's not as bad as it
11333 seems, because all existing breakpoints typically have both
11334 file/line and pc set. So, if clear is given file/line, we can
11335 match this to existing breakpoint without obtaining pc at all.
11336
11337 We only support clearing given the address explicitly
11338 present in breakpoint table. Say, we've set breakpoint
11339 at file:line. There were several PC values for that file:line,
11340 due to optimization, all in one block.
11341
11342 We've picked one PC value. If "clear" is issued with another
11343 PC corresponding to the same file:line, the breakpoint won't
11344 be cleared. We probably can still clear the breakpoint, but
11345 since the other PC value is never presented to user, user
11346 can only find it by guessing, and it does not seem important
11347 to support that. */
11348
11349 /* For each line spec given, delete bps which correspond to it. Do
11350 it in two passes, solely to preserve the current behavior that
11351 from_tty is forced true if we delete more than one
11352 breakpoint. */
11353
11354 std::vector<struct breakpoint *> found;
11355 for (const auto &sal : sals)
11356 {
11357 const char *sal_fullname;
11358
11359 /* If exact pc given, clear bpts at that pc.
11360 If line given (pc == 0), clear all bpts on specified line.
11361 If defaulting, clear all bpts on default line
11362 or at default pc.
11363
11364 defaulting sal.pc != 0 tests to do
11365
11366 0 1 pc
11367 1 1 pc _and_ line
11368 0 0 line
11369 1 0 <can't happen> */
11370
11371 sal_fullname = (sal.symtab == NULL
11372 ? NULL : symtab_to_fullname (sal.symtab));
11373
11374 /* Find all matching breakpoints and add them to 'found'. */
11375 ALL_BREAKPOINTS (b)
11376 {
11377 int match = 0;
11378 /* Are we going to delete b? */
11379 if (b->type != bp_none && !is_watchpoint (b))
11380 {
11381 struct bp_location *loc = b->loc;
11382 for (; loc; loc = loc->next)
11383 {
11384 /* If the user specified file:line, don't allow a PC
11385 match. This matches historical gdb behavior. */
11386 int pc_match = (!sal.explicit_line
11387 && sal.pc
11388 && (loc->pspace == sal.pspace)
11389 && (loc->address == sal.pc)
11390 && (!section_is_overlay (loc->section)
11391 || loc->section == sal.section));
11392 int line_match = 0;
11393
11394 if ((default_match || sal.explicit_line)
11395 && loc->symtab != NULL
11396 && sal_fullname != NULL
11397 && sal.pspace == loc->pspace
11398 && loc->line_number == sal.line
11399 && filename_cmp (symtab_to_fullname (loc->symtab),
11400 sal_fullname) == 0)
11401 line_match = 1;
11402
11403 if (pc_match || line_match)
11404 {
11405 match = 1;
11406 break;
11407 }
11408 }
11409 }
11410
11411 if (match)
11412 found.push_back (b);
11413 }
11414 }
11415
11416 /* Now go thru the 'found' chain and delete them. */
11417 if (found.empty ())
11418 {
11419 if (arg)
11420 error (_("No breakpoint at %s."), arg);
11421 else
11422 error (_("No breakpoint at this line."));
11423 }
11424
11425 /* Remove duplicates from the vec. */
11426 std::sort (found.begin (), found.end (),
11427 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11428 {
11429 return compare_breakpoints (bp_a, bp_b) < 0;
11430 });
11431 found.erase (std::unique (found.begin (), found.end (),
11432 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11433 {
11434 return compare_breakpoints (bp_a, bp_b) == 0;
11435 }),
11436 found.end ());
11437
11438 if (found.size () > 1)
11439 from_tty = 1; /* Always report if deleted more than one. */
11440 if (from_tty)
11441 {
11442 if (found.size () == 1)
11443 printf_unfiltered (_("Deleted breakpoint "));
11444 else
11445 printf_unfiltered (_("Deleted breakpoints "));
11446 }
11447
11448 for (breakpoint *iter : found)
11449 {
11450 if (from_tty)
11451 printf_unfiltered ("%d ", iter->number);
11452 delete_breakpoint (iter);
11453 }
11454 if (from_tty)
11455 putchar_unfiltered ('\n');
11456 }
11457 \f
11458 /* Delete breakpoint in BS if they are `delete' breakpoints and
11459 all breakpoints that are marked for deletion, whether hit or not.
11460 This is called after any breakpoint is hit, or after errors. */
11461
11462 void
11463 breakpoint_auto_delete (bpstat bs)
11464 {
11465 struct breakpoint *b, *b_tmp;
11466
11467 for (; bs; bs = bs->next)
11468 if (bs->breakpoint_at
11469 && bs->breakpoint_at->disposition == disp_del
11470 && bs->stop)
11471 delete_breakpoint (bs->breakpoint_at);
11472
11473 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11474 {
11475 if (b->disposition == disp_del_at_next_stop)
11476 delete_breakpoint (b);
11477 }
11478 }
11479
11480 /* A comparison function for bp_location AP and BP being interfaced to
11481 std::sort. Sort elements primarily by their ADDRESS (no matter what
11482 bl_address_is_meaningful says), secondarily by ordering first
11483 permanent elements and terciarily just ensuring the array is sorted
11484 stable way despite std::sort being an unstable algorithm. */
11485
11486 static int
11487 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11488 {
11489 if (a->address != b->address)
11490 return a->address < b->address;
11491
11492 /* Sort locations at the same address by their pspace number, keeping
11493 locations of the same inferior (in a multi-inferior environment)
11494 grouped. */
11495
11496 if (a->pspace->num != b->pspace->num)
11497 return a->pspace->num < b->pspace->num;
11498
11499 /* Sort permanent breakpoints first. */
11500 if (a->permanent != b->permanent)
11501 return a->permanent > b->permanent;
11502
11503 /* Sort by type in order to make duplicate determination easier.
11504 See update_global_location_list. This is kept in sync with
11505 breakpoint_locations_match. */
11506 if (a->loc_type < b->loc_type)
11507 return true;
11508
11509 /* Likewise, for range-breakpoints, sort by length. */
11510 if (a->loc_type == bp_loc_hardware_breakpoint
11511 && b->loc_type == bp_loc_hardware_breakpoint
11512 && a->length < b->length)
11513 return true;
11514
11515 /* Make the internal GDB representation stable across GDB runs
11516 where A and B memory inside GDB can differ. Breakpoint locations of
11517 the same type at the same address can be sorted in arbitrary order. */
11518
11519 if (a->owner->number != b->owner->number)
11520 return a->owner->number < b->owner->number;
11521
11522 return a < b;
11523 }
11524
11525 /* Set bp_locations_placed_address_before_address_max and
11526 bp_locations_shadow_len_after_address_max according to the current
11527 content of the bp_locations array. */
11528
11529 static void
11530 bp_locations_target_extensions_update (void)
11531 {
11532 struct bp_location *bl, **blp_tmp;
11533
11534 bp_locations_placed_address_before_address_max = 0;
11535 bp_locations_shadow_len_after_address_max = 0;
11536
11537 ALL_BP_LOCATIONS (bl, blp_tmp)
11538 {
11539 CORE_ADDR start, end, addr;
11540
11541 if (!bp_location_has_shadow (bl))
11542 continue;
11543
11544 start = bl->target_info.placed_address;
11545 end = start + bl->target_info.shadow_len;
11546
11547 gdb_assert (bl->address >= start);
11548 addr = bl->address - start;
11549 if (addr > bp_locations_placed_address_before_address_max)
11550 bp_locations_placed_address_before_address_max = addr;
11551
11552 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11553
11554 gdb_assert (bl->address < end);
11555 addr = end - bl->address;
11556 if (addr > bp_locations_shadow_len_after_address_max)
11557 bp_locations_shadow_len_after_address_max = addr;
11558 }
11559 }
11560
11561 /* Download tracepoint locations if they haven't been. */
11562
11563 static void
11564 download_tracepoint_locations (void)
11565 {
11566 struct breakpoint *b;
11567 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11568
11569 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11570
11571 ALL_TRACEPOINTS (b)
11572 {
11573 struct bp_location *bl;
11574 struct tracepoint *t;
11575 int bp_location_downloaded = 0;
11576
11577 if ((b->type == bp_fast_tracepoint
11578 ? !may_insert_fast_tracepoints
11579 : !may_insert_tracepoints))
11580 continue;
11581
11582 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11583 {
11584 if (target_can_download_tracepoint ())
11585 can_download_tracepoint = TRIBOOL_TRUE;
11586 else
11587 can_download_tracepoint = TRIBOOL_FALSE;
11588 }
11589
11590 if (can_download_tracepoint == TRIBOOL_FALSE)
11591 break;
11592
11593 for (bl = b->loc; bl; bl = bl->next)
11594 {
11595 /* In tracepoint, locations are _never_ duplicated, so
11596 should_be_inserted is equivalent to
11597 unduplicated_should_be_inserted. */
11598 if (!should_be_inserted (bl) || bl->inserted)
11599 continue;
11600
11601 switch_to_program_space_and_thread (bl->pspace);
11602
11603 target_download_tracepoint (bl);
11604
11605 bl->inserted = 1;
11606 bp_location_downloaded = 1;
11607 }
11608 t = (struct tracepoint *) b;
11609 t->number_on_target = b->number;
11610 if (bp_location_downloaded)
11611 gdb::observers::breakpoint_modified.notify (b);
11612 }
11613 }
11614
11615 /* Swap the insertion/duplication state between two locations. */
11616
11617 static void
11618 swap_insertion (struct bp_location *left, struct bp_location *right)
11619 {
11620 const int left_inserted = left->inserted;
11621 const int left_duplicate = left->duplicate;
11622 const int left_needs_update = left->needs_update;
11623 const struct bp_target_info left_target_info = left->target_info;
11624
11625 /* Locations of tracepoints can never be duplicated. */
11626 if (is_tracepoint (left->owner))
11627 gdb_assert (!left->duplicate);
11628 if (is_tracepoint (right->owner))
11629 gdb_assert (!right->duplicate);
11630
11631 left->inserted = right->inserted;
11632 left->duplicate = right->duplicate;
11633 left->needs_update = right->needs_update;
11634 left->target_info = right->target_info;
11635 right->inserted = left_inserted;
11636 right->duplicate = left_duplicate;
11637 right->needs_update = left_needs_update;
11638 right->target_info = left_target_info;
11639 }
11640
11641 /* Force the re-insertion of the locations at ADDRESS. This is called
11642 once a new/deleted/modified duplicate location is found and we are evaluating
11643 conditions on the target's side. Such conditions need to be updated on
11644 the target. */
11645
11646 static void
11647 force_breakpoint_reinsertion (struct bp_location *bl)
11648 {
11649 struct bp_location **locp = NULL, **loc2p;
11650 struct bp_location *loc;
11651 CORE_ADDR address = 0;
11652 int pspace_num;
11653
11654 address = bl->address;
11655 pspace_num = bl->pspace->num;
11656
11657 /* This is only meaningful if the target is
11658 evaluating conditions and if the user has
11659 opted for condition evaluation on the target's
11660 side. */
11661 if (gdb_evaluates_breakpoint_condition_p ()
11662 || !target_supports_evaluation_of_breakpoint_conditions ())
11663 return;
11664
11665 /* Flag all breakpoint locations with this address and
11666 the same program space as the location
11667 as "its condition has changed". We need to
11668 update the conditions on the target's side. */
11669 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11670 {
11671 loc = *loc2p;
11672
11673 if (!is_breakpoint (loc->owner)
11674 || pspace_num != loc->pspace->num)
11675 continue;
11676
11677 /* Flag the location appropriately. We use a different state to
11678 let everyone know that we already updated the set of locations
11679 with addr bl->address and program space bl->pspace. This is so
11680 we don't have to keep calling these functions just to mark locations
11681 that have already been marked. */
11682 loc->condition_changed = condition_updated;
11683
11684 /* Free the agent expression bytecode as well. We will compute
11685 it later on. */
11686 loc->cond_bytecode.reset ();
11687 }
11688 }
11689
11690 /* Called whether new breakpoints are created, or existing breakpoints
11691 deleted, to update the global location list and recompute which
11692 locations are duplicate of which.
11693
11694 The INSERT_MODE flag determines whether locations may not, may, or
11695 shall be inserted now. See 'enum ugll_insert_mode' for more
11696 info. */
11697
11698 static void
11699 update_global_location_list (enum ugll_insert_mode insert_mode)
11700 {
11701 struct breakpoint *b;
11702 struct bp_location **locp, *loc;
11703 /* Last breakpoint location address that was marked for update. */
11704 CORE_ADDR last_addr = 0;
11705 /* Last breakpoint location program space that was marked for update. */
11706 int last_pspace_num = -1;
11707
11708 /* Used in the duplicates detection below. When iterating over all
11709 bp_locations, points to the first bp_location of a given address.
11710 Breakpoints and watchpoints of different types are never
11711 duplicates of each other. Keep one pointer for each type of
11712 breakpoint/watchpoint, so we only need to loop over all locations
11713 once. */
11714 struct bp_location *bp_loc_first; /* breakpoint */
11715 struct bp_location *wp_loc_first; /* hardware watchpoint */
11716 struct bp_location *awp_loc_first; /* access watchpoint */
11717 struct bp_location *rwp_loc_first; /* read watchpoint */
11718
11719 /* Saved former bp_locations array which we compare against the newly
11720 built bp_locations from the current state of ALL_BREAKPOINTS. */
11721 struct bp_location **old_locp;
11722 unsigned old_locations_count;
11723 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11724
11725 old_locations_count = bp_locations_count;
11726 bp_locations = NULL;
11727 bp_locations_count = 0;
11728
11729 ALL_BREAKPOINTS (b)
11730 for (loc = b->loc; loc; loc = loc->next)
11731 bp_locations_count++;
11732
11733 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11734 locp = bp_locations;
11735 ALL_BREAKPOINTS (b)
11736 for (loc = b->loc; loc; loc = loc->next)
11737 *locp++ = loc;
11738
11739 /* See if we need to "upgrade" a software breakpoint to a hardware
11740 breakpoint. Do this before deciding whether locations are
11741 duplicates. Also do this before sorting because sorting order
11742 depends on location type. */
11743 for (locp = bp_locations;
11744 locp < bp_locations + bp_locations_count;
11745 locp++)
11746 {
11747 loc = *locp;
11748 if (!loc->inserted && should_be_inserted (loc))
11749 handle_automatic_hardware_breakpoints (loc);
11750 }
11751
11752 std::sort (bp_locations, bp_locations + bp_locations_count,
11753 bp_location_is_less_than);
11754
11755 bp_locations_target_extensions_update ();
11756
11757 /* Identify bp_location instances that are no longer present in the
11758 new list, and therefore should be freed. Note that it's not
11759 necessary that those locations should be removed from inferior --
11760 if there's another location at the same address (previously
11761 marked as duplicate), we don't need to remove/insert the
11762 location.
11763
11764 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11765 and former bp_location array state respectively. */
11766
11767 locp = bp_locations;
11768 for (old_locp = old_locations.get ();
11769 old_locp < old_locations.get () + old_locations_count;
11770 old_locp++)
11771 {
11772 struct bp_location *old_loc = *old_locp;
11773 struct bp_location **loc2p;
11774
11775 /* Tells if 'old_loc' is found among the new locations. If
11776 not, we have to free it. */
11777 int found_object = 0;
11778 /* Tells if the location should remain inserted in the target. */
11779 int keep_in_target = 0;
11780 int removed = 0;
11781
11782 /* Skip LOCP entries which will definitely never be needed.
11783 Stop either at or being the one matching OLD_LOC. */
11784 while (locp < bp_locations + bp_locations_count
11785 && (*locp)->address < old_loc->address)
11786 locp++;
11787
11788 for (loc2p = locp;
11789 (loc2p < bp_locations + bp_locations_count
11790 && (*loc2p)->address == old_loc->address);
11791 loc2p++)
11792 {
11793 /* Check if this is a new/duplicated location or a duplicated
11794 location that had its condition modified. If so, we want to send
11795 its condition to the target if evaluation of conditions is taking
11796 place there. */
11797 if ((*loc2p)->condition_changed == condition_modified
11798 && (last_addr != old_loc->address
11799 || last_pspace_num != old_loc->pspace->num))
11800 {
11801 force_breakpoint_reinsertion (*loc2p);
11802 last_pspace_num = old_loc->pspace->num;
11803 }
11804
11805 if (*loc2p == old_loc)
11806 found_object = 1;
11807 }
11808
11809 /* We have already handled this address, update it so that we don't
11810 have to go through updates again. */
11811 last_addr = old_loc->address;
11812
11813 /* Target-side condition evaluation: Handle deleted locations. */
11814 if (!found_object)
11815 force_breakpoint_reinsertion (old_loc);
11816
11817 /* If this location is no longer present, and inserted, look if
11818 there's maybe a new location at the same address. If so,
11819 mark that one inserted, and don't remove this one. This is
11820 needed so that we don't have a time window where a breakpoint
11821 at certain location is not inserted. */
11822
11823 if (old_loc->inserted)
11824 {
11825 /* If the location is inserted now, we might have to remove
11826 it. */
11827
11828 if (found_object && should_be_inserted (old_loc))
11829 {
11830 /* The location is still present in the location list,
11831 and still should be inserted. Don't do anything. */
11832 keep_in_target = 1;
11833 }
11834 else
11835 {
11836 /* This location still exists, but it won't be kept in the
11837 target since it may have been disabled. We proceed to
11838 remove its target-side condition. */
11839
11840 /* The location is either no longer present, or got
11841 disabled. See if there's another location at the
11842 same address, in which case we don't need to remove
11843 this one from the target. */
11844
11845 /* OLD_LOC comes from existing struct breakpoint. */
11846 if (bl_address_is_meaningful (old_loc))
11847 {
11848 for (loc2p = locp;
11849 (loc2p < bp_locations + bp_locations_count
11850 && (*loc2p)->address == old_loc->address);
11851 loc2p++)
11852 {
11853 struct bp_location *loc2 = *loc2p;
11854
11855 if (loc2 == old_loc)
11856 continue;
11857
11858 if (breakpoint_locations_match (loc2, old_loc))
11859 {
11860 /* Read watchpoint locations are switched to
11861 access watchpoints, if the former are not
11862 supported, but the latter are. */
11863 if (is_hardware_watchpoint (old_loc->owner))
11864 {
11865 gdb_assert (is_hardware_watchpoint (loc2->owner));
11866 loc2->watchpoint_type = old_loc->watchpoint_type;
11867 }
11868
11869 /* loc2 is a duplicated location. We need to check
11870 if it should be inserted in case it will be
11871 unduplicated. */
11872 if (unduplicated_should_be_inserted (loc2))
11873 {
11874 swap_insertion (old_loc, loc2);
11875 keep_in_target = 1;
11876 break;
11877 }
11878 }
11879 }
11880 }
11881 }
11882
11883 if (!keep_in_target)
11884 {
11885 if (remove_breakpoint (old_loc))
11886 {
11887 /* This is just about all we can do. We could keep
11888 this location on the global list, and try to
11889 remove it next time, but there's no particular
11890 reason why we will succeed next time.
11891
11892 Note that at this point, old_loc->owner is still
11893 valid, as delete_breakpoint frees the breakpoint
11894 only after calling us. */
11895 printf_filtered (_("warning: Error removing "
11896 "breakpoint %d\n"),
11897 old_loc->owner->number);
11898 }
11899 removed = 1;
11900 }
11901 }
11902
11903 if (!found_object)
11904 {
11905 if (removed && target_is_non_stop_p ()
11906 && need_moribund_for_location_type (old_loc))
11907 {
11908 /* This location was removed from the target. In
11909 non-stop mode, a race condition is possible where
11910 we've removed a breakpoint, but stop events for that
11911 breakpoint are already queued and will arrive later.
11912 We apply an heuristic to be able to distinguish such
11913 SIGTRAPs from other random SIGTRAPs: we keep this
11914 breakpoint location for a bit, and will retire it
11915 after we see some number of events. The theory here
11916 is that reporting of events should, "on the average",
11917 be fair, so after a while we'll see events from all
11918 threads that have anything of interest, and no longer
11919 need to keep this breakpoint location around. We
11920 don't hold locations forever so to reduce chances of
11921 mistaking a non-breakpoint SIGTRAP for a breakpoint
11922 SIGTRAP.
11923
11924 The heuristic failing can be disastrous on
11925 decr_pc_after_break targets.
11926
11927 On decr_pc_after_break targets, like e.g., x86-linux,
11928 if we fail to recognize a late breakpoint SIGTRAP,
11929 because events_till_retirement has reached 0 too
11930 soon, we'll fail to do the PC adjustment, and report
11931 a random SIGTRAP to the user. When the user resumes
11932 the inferior, it will most likely immediately crash
11933 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11934 corrupted, because of being resumed e.g., in the
11935 middle of a multi-byte instruction, or skipped a
11936 one-byte instruction. This was actually seen happen
11937 on native x86-linux, and should be less rare on
11938 targets that do not support new thread events, like
11939 remote, due to the heuristic depending on
11940 thread_count.
11941
11942 Mistaking a random SIGTRAP for a breakpoint trap
11943 causes similar symptoms (PC adjustment applied when
11944 it shouldn't), but then again, playing with SIGTRAPs
11945 behind the debugger's back is asking for trouble.
11946
11947 Since hardware watchpoint traps are always
11948 distinguishable from other traps, so we don't need to
11949 apply keep hardware watchpoint moribund locations
11950 around. We simply always ignore hardware watchpoint
11951 traps we can no longer explain. */
11952
11953 process_stratum_target *proc_target = nullptr;
11954 for (inferior *inf : all_inferiors ())
11955 if (inf->pspace == old_loc->pspace)
11956 {
11957 proc_target = inf->process_target ();
11958 break;
11959 }
11960 if (proc_target != nullptr)
11961 old_loc->events_till_retirement
11962 = 3 * (thread_count (proc_target) + 1);
11963 else
11964 old_loc->events_till_retirement = 1;
11965 old_loc->owner = NULL;
11966
11967 moribund_locations.push_back (old_loc);
11968 }
11969 else
11970 {
11971 old_loc->owner = NULL;
11972 decref_bp_location (&old_loc);
11973 }
11974 }
11975 }
11976
11977 /* Rescan breakpoints at the same address and section, marking the
11978 first one as "first" and any others as "duplicates". This is so
11979 that the bpt instruction is only inserted once. If we have a
11980 permanent breakpoint at the same place as BPT, make that one the
11981 official one, and the rest as duplicates. Permanent breakpoints
11982 are sorted first for the same address.
11983
11984 Do the same for hardware watchpoints, but also considering the
11985 watchpoint's type (regular/access/read) and length. */
11986
11987 bp_loc_first = NULL;
11988 wp_loc_first = NULL;
11989 awp_loc_first = NULL;
11990 rwp_loc_first = NULL;
11991 ALL_BP_LOCATIONS (loc, locp)
11992 {
11993 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11994 non-NULL. */
11995 struct bp_location **loc_first_p;
11996 b = loc->owner;
11997
11998 if (!unduplicated_should_be_inserted (loc)
11999 || !bl_address_is_meaningful (loc)
12000 /* Don't detect duplicate for tracepoint locations because they are
12001 never duplicated. See the comments in field `duplicate' of
12002 `struct bp_location'. */
12003 || is_tracepoint (b))
12004 {
12005 /* Clear the condition modification flag. */
12006 loc->condition_changed = condition_unchanged;
12007 continue;
12008 }
12009
12010 if (b->type == bp_hardware_watchpoint)
12011 loc_first_p = &wp_loc_first;
12012 else if (b->type == bp_read_watchpoint)
12013 loc_first_p = &rwp_loc_first;
12014 else if (b->type == bp_access_watchpoint)
12015 loc_first_p = &awp_loc_first;
12016 else
12017 loc_first_p = &bp_loc_first;
12018
12019 if (*loc_first_p == NULL
12020 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12021 || !breakpoint_locations_match (loc, *loc_first_p))
12022 {
12023 *loc_first_p = loc;
12024 loc->duplicate = 0;
12025
12026 if (is_breakpoint (loc->owner) && loc->condition_changed)
12027 {
12028 loc->needs_update = 1;
12029 /* Clear the condition modification flag. */
12030 loc->condition_changed = condition_unchanged;
12031 }
12032 continue;
12033 }
12034
12035
12036 /* This and the above ensure the invariant that the first location
12037 is not duplicated, and is the inserted one.
12038 All following are marked as duplicated, and are not inserted. */
12039 if (loc->inserted)
12040 swap_insertion (loc, *loc_first_p);
12041 loc->duplicate = 1;
12042
12043 /* Clear the condition modification flag. */
12044 loc->condition_changed = condition_unchanged;
12045 }
12046
12047 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12048 {
12049 if (insert_mode != UGLL_DONT_INSERT)
12050 insert_breakpoint_locations ();
12051 else
12052 {
12053 /* Even though the caller told us to not insert new
12054 locations, we may still need to update conditions on the
12055 target's side of breakpoints that were already inserted
12056 if the target is evaluating breakpoint conditions. We
12057 only update conditions for locations that are marked
12058 "needs_update". */
12059 update_inserted_breakpoint_locations ();
12060 }
12061 }
12062
12063 if (insert_mode != UGLL_DONT_INSERT)
12064 download_tracepoint_locations ();
12065 }
12066
12067 void
12068 breakpoint_retire_moribund (void)
12069 {
12070 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12071 {
12072 struct bp_location *loc = moribund_locations[ix];
12073 if (--(loc->events_till_retirement) == 0)
12074 {
12075 decref_bp_location (&loc);
12076 unordered_remove (moribund_locations, ix);
12077 --ix;
12078 }
12079 }
12080 }
12081
12082 static void
12083 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12084 {
12085
12086 try
12087 {
12088 update_global_location_list (insert_mode);
12089 }
12090 catch (const gdb_exception_error &e)
12091 {
12092 }
12093 }
12094
12095 /* Clear BKP from a BPS. */
12096
12097 static void
12098 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12099 {
12100 bpstat bs;
12101
12102 for (bs = bps; bs; bs = bs->next)
12103 if (bs->breakpoint_at == bpt)
12104 {
12105 bs->breakpoint_at = NULL;
12106 bs->old_val = NULL;
12107 /* bs->commands will be freed later. */
12108 }
12109 }
12110
12111 /* Callback for iterate_over_threads. */
12112 static int
12113 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12114 {
12115 struct breakpoint *bpt = (struct breakpoint *) data;
12116
12117 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12118 return 0;
12119 }
12120
12121 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12122 callbacks. */
12123
12124 static void
12125 say_where (struct breakpoint *b)
12126 {
12127 struct value_print_options opts;
12128
12129 get_user_print_options (&opts);
12130
12131 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12132 single string. */
12133 if (b->loc == NULL)
12134 {
12135 /* For pending locations, the output differs slightly based
12136 on b->extra_string. If this is non-NULL, it contains either
12137 a condition or dprintf arguments. */
12138 if (b->extra_string == NULL)
12139 {
12140 printf_filtered (_(" (%s) pending."),
12141 event_location_to_string (b->location.get ()));
12142 }
12143 else if (b->type == bp_dprintf)
12144 {
12145 printf_filtered (_(" (%s,%s) pending."),
12146 event_location_to_string (b->location.get ()),
12147 b->extra_string);
12148 }
12149 else
12150 {
12151 printf_filtered (_(" (%s %s) pending."),
12152 event_location_to_string (b->location.get ()),
12153 b->extra_string);
12154 }
12155 }
12156 else
12157 {
12158 if (opts.addressprint || b->loc->symtab == NULL)
12159 printf_filtered (" at %ps",
12160 styled_string (address_style.style (),
12161 paddress (b->loc->gdbarch,
12162 b->loc->address)));
12163 if (b->loc->symtab != NULL)
12164 {
12165 /* If there is a single location, we can print the location
12166 more nicely. */
12167 if (b->loc->next == NULL)
12168 {
12169 const char *filename
12170 = symtab_to_filename_for_display (b->loc->symtab);
12171 printf_filtered (": file %ps, line %d.",
12172 styled_string (file_name_style.style (),
12173 filename),
12174 b->loc->line_number);
12175 }
12176 else
12177 /* This is not ideal, but each location may have a
12178 different file name, and this at least reflects the
12179 real situation somewhat. */
12180 printf_filtered (": %s.",
12181 event_location_to_string (b->location.get ()));
12182 }
12183
12184 if (b->loc->next)
12185 {
12186 struct bp_location *loc = b->loc;
12187 int n = 0;
12188 for (; loc; loc = loc->next)
12189 ++n;
12190 printf_filtered (" (%d locations)", n);
12191 }
12192 }
12193 }
12194
12195 bp_location::~bp_location ()
12196 {
12197 xfree (function_name);
12198 }
12199
12200 /* Destructor for the breakpoint base class. */
12201
12202 breakpoint::~breakpoint ()
12203 {
12204 xfree (this->cond_string);
12205 xfree (this->extra_string);
12206 }
12207
12208 static struct bp_location *
12209 base_breakpoint_allocate_location (struct breakpoint *self)
12210 {
12211 return new bp_location (self);
12212 }
12213
12214 static void
12215 base_breakpoint_re_set (struct breakpoint *b)
12216 {
12217 /* Nothing to re-set. */
12218 }
12219
12220 #define internal_error_pure_virtual_called() \
12221 gdb_assert_not_reached ("pure virtual function called")
12222
12223 static int
12224 base_breakpoint_insert_location (struct bp_location *bl)
12225 {
12226 internal_error_pure_virtual_called ();
12227 }
12228
12229 static int
12230 base_breakpoint_remove_location (struct bp_location *bl,
12231 enum remove_bp_reason reason)
12232 {
12233 internal_error_pure_virtual_called ();
12234 }
12235
12236 static int
12237 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12238 const address_space *aspace,
12239 CORE_ADDR bp_addr,
12240 const struct target_waitstatus *ws)
12241 {
12242 internal_error_pure_virtual_called ();
12243 }
12244
12245 static void
12246 base_breakpoint_check_status (bpstat bs)
12247 {
12248 /* Always stop. */
12249 }
12250
12251 /* A "works_in_software_mode" breakpoint_ops method that just internal
12252 errors. */
12253
12254 static int
12255 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12256 {
12257 internal_error_pure_virtual_called ();
12258 }
12259
12260 /* A "resources_needed" breakpoint_ops method that just internal
12261 errors. */
12262
12263 static int
12264 base_breakpoint_resources_needed (const struct bp_location *bl)
12265 {
12266 internal_error_pure_virtual_called ();
12267 }
12268
12269 static enum print_stop_action
12270 base_breakpoint_print_it (bpstat bs)
12271 {
12272 internal_error_pure_virtual_called ();
12273 }
12274
12275 static void
12276 base_breakpoint_print_one_detail (const struct breakpoint *self,
12277 struct ui_out *uiout)
12278 {
12279 /* nothing */
12280 }
12281
12282 static void
12283 base_breakpoint_print_mention (struct breakpoint *b)
12284 {
12285 internal_error_pure_virtual_called ();
12286 }
12287
12288 static void
12289 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12290 {
12291 internal_error_pure_virtual_called ();
12292 }
12293
12294 static void
12295 base_breakpoint_create_sals_from_location
12296 (const struct event_location *location,
12297 struct linespec_result *canonical,
12298 enum bptype type_wanted)
12299 {
12300 internal_error_pure_virtual_called ();
12301 }
12302
12303 static void
12304 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12305 struct linespec_result *c,
12306 gdb::unique_xmalloc_ptr<char> cond_string,
12307 gdb::unique_xmalloc_ptr<char> extra_string,
12308 enum bptype type_wanted,
12309 enum bpdisp disposition,
12310 int thread,
12311 int task, int ignore_count,
12312 const struct breakpoint_ops *o,
12313 int from_tty, int enabled,
12314 int internal, unsigned flags)
12315 {
12316 internal_error_pure_virtual_called ();
12317 }
12318
12319 static std::vector<symtab_and_line>
12320 base_breakpoint_decode_location (struct breakpoint *b,
12321 const struct event_location *location,
12322 struct program_space *search_pspace)
12323 {
12324 internal_error_pure_virtual_called ();
12325 }
12326
12327 /* The default 'explains_signal' method. */
12328
12329 static int
12330 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12331 {
12332 return 1;
12333 }
12334
12335 /* The default "after_condition_true" method. */
12336
12337 static void
12338 base_breakpoint_after_condition_true (struct bpstats *bs)
12339 {
12340 /* Nothing to do. */
12341 }
12342
12343 struct breakpoint_ops base_breakpoint_ops =
12344 {
12345 base_breakpoint_allocate_location,
12346 base_breakpoint_re_set,
12347 base_breakpoint_insert_location,
12348 base_breakpoint_remove_location,
12349 base_breakpoint_breakpoint_hit,
12350 base_breakpoint_check_status,
12351 base_breakpoint_resources_needed,
12352 base_breakpoint_works_in_software_mode,
12353 base_breakpoint_print_it,
12354 NULL,
12355 base_breakpoint_print_one_detail,
12356 base_breakpoint_print_mention,
12357 base_breakpoint_print_recreate,
12358 base_breakpoint_create_sals_from_location,
12359 base_breakpoint_create_breakpoints_sal,
12360 base_breakpoint_decode_location,
12361 base_breakpoint_explains_signal,
12362 base_breakpoint_after_condition_true,
12363 };
12364
12365 /* Default breakpoint_ops methods. */
12366
12367 static void
12368 bkpt_re_set (struct breakpoint *b)
12369 {
12370 /* FIXME: is this still reachable? */
12371 if (breakpoint_event_location_empty_p (b))
12372 {
12373 /* Anything without a location can't be re-set. */
12374 delete_breakpoint (b);
12375 return;
12376 }
12377
12378 breakpoint_re_set_default (b);
12379 }
12380
12381 static int
12382 bkpt_insert_location (struct bp_location *bl)
12383 {
12384 CORE_ADDR addr = bl->target_info.reqstd_address;
12385
12386 bl->target_info.kind = breakpoint_kind (bl, &addr);
12387 bl->target_info.placed_address = addr;
12388
12389 if (bl->loc_type == bp_loc_hardware_breakpoint)
12390 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12391 else
12392 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12393 }
12394
12395 static int
12396 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12397 {
12398 if (bl->loc_type == bp_loc_hardware_breakpoint)
12399 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12400 else
12401 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12402 }
12403
12404 static int
12405 bkpt_breakpoint_hit (const struct bp_location *bl,
12406 const address_space *aspace, CORE_ADDR bp_addr,
12407 const struct target_waitstatus *ws)
12408 {
12409 if (ws->kind != TARGET_WAITKIND_STOPPED
12410 || ws->value.sig != GDB_SIGNAL_TRAP)
12411 return 0;
12412
12413 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12414 aspace, bp_addr))
12415 return 0;
12416
12417 if (overlay_debugging /* unmapped overlay section */
12418 && section_is_overlay (bl->section)
12419 && !section_is_mapped (bl->section))
12420 return 0;
12421
12422 return 1;
12423 }
12424
12425 static int
12426 dprintf_breakpoint_hit (const struct bp_location *bl,
12427 const address_space *aspace, CORE_ADDR bp_addr,
12428 const struct target_waitstatus *ws)
12429 {
12430 if (dprintf_style == dprintf_style_agent
12431 && target_can_run_breakpoint_commands ())
12432 {
12433 /* An agent-style dprintf never causes a stop. If we see a trap
12434 for this address it must be for a breakpoint that happens to
12435 be set at the same address. */
12436 return 0;
12437 }
12438
12439 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12440 }
12441
12442 static int
12443 bkpt_resources_needed (const struct bp_location *bl)
12444 {
12445 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12446
12447 return 1;
12448 }
12449
12450 static enum print_stop_action
12451 bkpt_print_it (bpstat bs)
12452 {
12453 struct breakpoint *b;
12454 const struct bp_location *bl;
12455 int bp_temp;
12456 struct ui_out *uiout = current_uiout;
12457
12458 gdb_assert (bs->bp_location_at != NULL);
12459
12460 bl = bs->bp_location_at;
12461 b = bs->breakpoint_at;
12462
12463 bp_temp = b->disposition == disp_del;
12464 if (bl->address != bl->requested_address)
12465 breakpoint_adjustment_warning (bl->requested_address,
12466 bl->address,
12467 b->number, 1);
12468 annotate_breakpoint (b->number);
12469 maybe_print_thread_hit_breakpoint (uiout);
12470
12471 if (uiout->is_mi_like_p ())
12472 {
12473 uiout->field_string ("reason",
12474 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12475 uiout->field_string ("disp", bpdisp_text (b->disposition));
12476 }
12477 if (bp_temp)
12478 uiout->message ("Temporary breakpoint %pF, ",
12479 signed_field ("bkptno", b->number));
12480 else
12481 uiout->message ("Breakpoint %pF, ",
12482 signed_field ("bkptno", b->number));
12483
12484 return PRINT_SRC_AND_LOC;
12485 }
12486
12487 static void
12488 bkpt_print_mention (struct breakpoint *b)
12489 {
12490 if (current_uiout->is_mi_like_p ())
12491 return;
12492
12493 switch (b->type)
12494 {
12495 case bp_breakpoint:
12496 case bp_gnu_ifunc_resolver:
12497 if (b->disposition == disp_del)
12498 printf_filtered (_("Temporary breakpoint"));
12499 else
12500 printf_filtered (_("Breakpoint"));
12501 printf_filtered (_(" %d"), b->number);
12502 if (b->type == bp_gnu_ifunc_resolver)
12503 printf_filtered (_(" at gnu-indirect-function resolver"));
12504 break;
12505 case bp_hardware_breakpoint:
12506 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12507 break;
12508 case bp_dprintf:
12509 printf_filtered (_("Dprintf %d"), b->number);
12510 break;
12511 }
12512
12513 say_where (b);
12514 }
12515
12516 static void
12517 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12518 {
12519 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12520 fprintf_unfiltered (fp, "tbreak");
12521 else if (tp->type == bp_breakpoint)
12522 fprintf_unfiltered (fp, "break");
12523 else if (tp->type == bp_hardware_breakpoint
12524 && tp->disposition == disp_del)
12525 fprintf_unfiltered (fp, "thbreak");
12526 else if (tp->type == bp_hardware_breakpoint)
12527 fprintf_unfiltered (fp, "hbreak");
12528 else
12529 internal_error (__FILE__, __LINE__,
12530 _("unhandled breakpoint type %d"), (int) tp->type);
12531
12532 fprintf_unfiltered (fp, " %s",
12533 event_location_to_string (tp->location.get ()));
12534
12535 /* Print out extra_string if this breakpoint is pending. It might
12536 contain, for example, conditions that were set by the user. */
12537 if (tp->loc == NULL && tp->extra_string != NULL)
12538 fprintf_unfiltered (fp, " %s", tp->extra_string);
12539
12540 print_recreate_thread (tp, fp);
12541 }
12542
12543 static void
12544 bkpt_create_sals_from_location (const struct event_location *location,
12545 struct linespec_result *canonical,
12546 enum bptype type_wanted)
12547 {
12548 create_sals_from_location_default (location, canonical, type_wanted);
12549 }
12550
12551 static void
12552 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12553 struct linespec_result *canonical,
12554 gdb::unique_xmalloc_ptr<char> cond_string,
12555 gdb::unique_xmalloc_ptr<char> extra_string,
12556 enum bptype type_wanted,
12557 enum bpdisp disposition,
12558 int thread,
12559 int task, int ignore_count,
12560 const struct breakpoint_ops *ops,
12561 int from_tty, int enabled,
12562 int internal, unsigned flags)
12563 {
12564 create_breakpoints_sal_default (gdbarch, canonical,
12565 std::move (cond_string),
12566 std::move (extra_string),
12567 type_wanted,
12568 disposition, thread, task,
12569 ignore_count, ops, from_tty,
12570 enabled, internal, flags);
12571 }
12572
12573 static std::vector<symtab_and_line>
12574 bkpt_decode_location (struct breakpoint *b,
12575 const struct event_location *location,
12576 struct program_space *search_pspace)
12577 {
12578 return decode_location_default (b, location, search_pspace);
12579 }
12580
12581 /* Virtual table for internal breakpoints. */
12582
12583 static void
12584 internal_bkpt_re_set (struct breakpoint *b)
12585 {
12586 switch (b->type)
12587 {
12588 /* Delete overlay event and longjmp master breakpoints; they
12589 will be reset later by breakpoint_re_set. */
12590 case bp_overlay_event:
12591 case bp_longjmp_master:
12592 case bp_std_terminate_master:
12593 case bp_exception_master:
12594 delete_breakpoint (b);
12595 break;
12596
12597 /* This breakpoint is special, it's set up when the inferior
12598 starts and we really don't want to touch it. */
12599 case bp_shlib_event:
12600
12601 /* Like bp_shlib_event, this breakpoint type is special. Once
12602 it is set up, we do not want to touch it. */
12603 case bp_thread_event:
12604 break;
12605 }
12606 }
12607
12608 static void
12609 internal_bkpt_check_status (bpstat bs)
12610 {
12611 if (bs->breakpoint_at->type == bp_shlib_event)
12612 {
12613 /* If requested, stop when the dynamic linker notifies GDB of
12614 events. This allows the user to get control and place
12615 breakpoints in initializer routines for dynamically loaded
12616 objects (among other things). */
12617 bs->stop = stop_on_solib_events;
12618 bs->print = stop_on_solib_events;
12619 }
12620 else
12621 bs->stop = 0;
12622 }
12623
12624 static enum print_stop_action
12625 internal_bkpt_print_it (bpstat bs)
12626 {
12627 struct breakpoint *b;
12628
12629 b = bs->breakpoint_at;
12630
12631 switch (b->type)
12632 {
12633 case bp_shlib_event:
12634 /* Did we stop because the user set the stop_on_solib_events
12635 variable? (If so, we report this as a generic, "Stopped due
12636 to shlib event" message.) */
12637 print_solib_event (0);
12638 break;
12639
12640 case bp_thread_event:
12641 /* Not sure how we will get here.
12642 GDB should not stop for these breakpoints. */
12643 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12644 break;
12645
12646 case bp_overlay_event:
12647 /* By analogy with the thread event, GDB should not stop for these. */
12648 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12649 break;
12650
12651 case bp_longjmp_master:
12652 /* These should never be enabled. */
12653 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12654 break;
12655
12656 case bp_std_terminate_master:
12657 /* These should never be enabled. */
12658 printf_filtered (_("std::terminate Master Breakpoint: "
12659 "gdb should not stop!\n"));
12660 break;
12661
12662 case bp_exception_master:
12663 /* These should never be enabled. */
12664 printf_filtered (_("Exception Master Breakpoint: "
12665 "gdb should not stop!\n"));
12666 break;
12667 }
12668
12669 return PRINT_NOTHING;
12670 }
12671
12672 static void
12673 internal_bkpt_print_mention (struct breakpoint *b)
12674 {
12675 /* Nothing to mention. These breakpoints are internal. */
12676 }
12677
12678 /* Virtual table for momentary breakpoints */
12679
12680 static void
12681 momentary_bkpt_re_set (struct breakpoint *b)
12682 {
12683 /* Keep temporary breakpoints, which can be encountered when we step
12684 over a dlopen call and solib_add is resetting the breakpoints.
12685 Otherwise these should have been blown away via the cleanup chain
12686 or by breakpoint_init_inferior when we rerun the executable. */
12687 }
12688
12689 static void
12690 momentary_bkpt_check_status (bpstat bs)
12691 {
12692 /* Nothing. The point of these breakpoints is causing a stop. */
12693 }
12694
12695 static enum print_stop_action
12696 momentary_bkpt_print_it (bpstat bs)
12697 {
12698 return PRINT_UNKNOWN;
12699 }
12700
12701 static void
12702 momentary_bkpt_print_mention (struct breakpoint *b)
12703 {
12704 /* Nothing to mention. These breakpoints are internal. */
12705 }
12706
12707 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12708
12709 It gets cleared already on the removal of the first one of such placed
12710 breakpoints. This is OK as they get all removed altogether. */
12711
12712 longjmp_breakpoint::~longjmp_breakpoint ()
12713 {
12714 thread_info *tp = find_thread_global_id (this->thread);
12715
12716 if (tp != NULL)
12717 tp->initiating_frame = null_frame_id;
12718 }
12719
12720 /* Specific methods for probe breakpoints. */
12721
12722 static int
12723 bkpt_probe_insert_location (struct bp_location *bl)
12724 {
12725 int v = bkpt_insert_location (bl);
12726
12727 if (v == 0)
12728 {
12729 /* The insertion was successful, now let's set the probe's semaphore
12730 if needed. */
12731 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12732 }
12733
12734 return v;
12735 }
12736
12737 static int
12738 bkpt_probe_remove_location (struct bp_location *bl,
12739 enum remove_bp_reason reason)
12740 {
12741 /* Let's clear the semaphore before removing the location. */
12742 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12743
12744 return bkpt_remove_location (bl, reason);
12745 }
12746
12747 static void
12748 bkpt_probe_create_sals_from_location (const struct event_location *location,
12749 struct linespec_result *canonical,
12750 enum bptype type_wanted)
12751 {
12752 struct linespec_sals lsal;
12753
12754 lsal.sals = parse_probes (location, NULL, canonical);
12755 lsal.canonical
12756 = xstrdup (event_location_to_string (canonical->location.get ()));
12757 canonical->lsals.push_back (std::move (lsal));
12758 }
12759
12760 static std::vector<symtab_and_line>
12761 bkpt_probe_decode_location (struct breakpoint *b,
12762 const struct event_location *location,
12763 struct program_space *search_pspace)
12764 {
12765 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12766 if (sals.empty ())
12767 error (_("probe not found"));
12768 return sals;
12769 }
12770
12771 /* The breakpoint_ops structure to be used in tracepoints. */
12772
12773 static void
12774 tracepoint_re_set (struct breakpoint *b)
12775 {
12776 breakpoint_re_set_default (b);
12777 }
12778
12779 static int
12780 tracepoint_breakpoint_hit (const struct bp_location *bl,
12781 const address_space *aspace, CORE_ADDR bp_addr,
12782 const struct target_waitstatus *ws)
12783 {
12784 /* By definition, the inferior does not report stops at
12785 tracepoints. */
12786 return 0;
12787 }
12788
12789 static void
12790 tracepoint_print_one_detail (const struct breakpoint *self,
12791 struct ui_out *uiout)
12792 {
12793 struct tracepoint *tp = (struct tracepoint *) self;
12794 if (!tp->static_trace_marker_id.empty ())
12795 {
12796 gdb_assert (self->type == bp_static_tracepoint);
12797
12798 uiout->message ("\tmarker id is %pF\n",
12799 string_field ("static-tracepoint-marker-string-id",
12800 tp->static_trace_marker_id.c_str ()));
12801 }
12802 }
12803
12804 static void
12805 tracepoint_print_mention (struct breakpoint *b)
12806 {
12807 if (current_uiout->is_mi_like_p ())
12808 return;
12809
12810 switch (b->type)
12811 {
12812 case bp_tracepoint:
12813 printf_filtered (_("Tracepoint"));
12814 printf_filtered (_(" %d"), b->number);
12815 break;
12816 case bp_fast_tracepoint:
12817 printf_filtered (_("Fast tracepoint"));
12818 printf_filtered (_(" %d"), b->number);
12819 break;
12820 case bp_static_tracepoint:
12821 printf_filtered (_("Static tracepoint"));
12822 printf_filtered (_(" %d"), b->number);
12823 break;
12824 default:
12825 internal_error (__FILE__, __LINE__,
12826 _("unhandled tracepoint type %d"), (int) b->type);
12827 }
12828
12829 say_where (b);
12830 }
12831
12832 static void
12833 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12834 {
12835 struct tracepoint *tp = (struct tracepoint *) self;
12836
12837 if (self->type == bp_fast_tracepoint)
12838 fprintf_unfiltered (fp, "ftrace");
12839 else if (self->type == bp_static_tracepoint)
12840 fprintf_unfiltered (fp, "strace");
12841 else if (self->type == bp_tracepoint)
12842 fprintf_unfiltered (fp, "trace");
12843 else
12844 internal_error (__FILE__, __LINE__,
12845 _("unhandled tracepoint type %d"), (int) self->type);
12846
12847 fprintf_unfiltered (fp, " %s",
12848 event_location_to_string (self->location.get ()));
12849 print_recreate_thread (self, fp);
12850
12851 if (tp->pass_count)
12852 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12853 }
12854
12855 static void
12856 tracepoint_create_sals_from_location (const struct event_location *location,
12857 struct linespec_result *canonical,
12858 enum bptype type_wanted)
12859 {
12860 create_sals_from_location_default (location, canonical, type_wanted);
12861 }
12862
12863 static void
12864 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12865 struct linespec_result *canonical,
12866 gdb::unique_xmalloc_ptr<char> cond_string,
12867 gdb::unique_xmalloc_ptr<char> extra_string,
12868 enum bptype type_wanted,
12869 enum bpdisp disposition,
12870 int thread,
12871 int task, int ignore_count,
12872 const struct breakpoint_ops *ops,
12873 int from_tty, int enabled,
12874 int internal, unsigned flags)
12875 {
12876 create_breakpoints_sal_default (gdbarch, canonical,
12877 std::move (cond_string),
12878 std::move (extra_string),
12879 type_wanted,
12880 disposition, thread, task,
12881 ignore_count, ops, from_tty,
12882 enabled, internal, flags);
12883 }
12884
12885 static std::vector<symtab_and_line>
12886 tracepoint_decode_location (struct breakpoint *b,
12887 const struct event_location *location,
12888 struct program_space *search_pspace)
12889 {
12890 return decode_location_default (b, location, search_pspace);
12891 }
12892
12893 struct breakpoint_ops tracepoint_breakpoint_ops;
12894
12895 /* Virtual table for tracepoints on static probes. */
12896
12897 static void
12898 tracepoint_probe_create_sals_from_location
12899 (const struct event_location *location,
12900 struct linespec_result *canonical,
12901 enum bptype type_wanted)
12902 {
12903 /* We use the same method for breakpoint on probes. */
12904 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12905 }
12906
12907 static std::vector<symtab_and_line>
12908 tracepoint_probe_decode_location (struct breakpoint *b,
12909 const struct event_location *location,
12910 struct program_space *search_pspace)
12911 {
12912 /* We use the same method for breakpoint on probes. */
12913 return bkpt_probe_decode_location (b, location, search_pspace);
12914 }
12915
12916 /* Dprintf breakpoint_ops methods. */
12917
12918 static void
12919 dprintf_re_set (struct breakpoint *b)
12920 {
12921 breakpoint_re_set_default (b);
12922
12923 /* extra_string should never be non-NULL for dprintf. */
12924 gdb_assert (b->extra_string != NULL);
12925
12926 /* 1 - connect to target 1, that can run breakpoint commands.
12927 2 - create a dprintf, which resolves fine.
12928 3 - disconnect from target 1
12929 4 - connect to target 2, that can NOT run breakpoint commands.
12930
12931 After steps #3/#4, you'll want the dprintf command list to
12932 be updated, because target 1 and 2 may well return different
12933 answers for target_can_run_breakpoint_commands().
12934 Given absence of finer grained resetting, we get to do
12935 it all the time. */
12936 if (b->extra_string != NULL)
12937 update_dprintf_command_list (b);
12938 }
12939
12940 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12941
12942 static void
12943 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12944 {
12945 fprintf_unfiltered (fp, "dprintf %s,%s",
12946 event_location_to_string (tp->location.get ()),
12947 tp->extra_string);
12948 print_recreate_thread (tp, fp);
12949 }
12950
12951 /* Implement the "after_condition_true" breakpoint_ops method for
12952 dprintf.
12953
12954 dprintf's are implemented with regular commands in their command
12955 list, but we run the commands here instead of before presenting the
12956 stop to the user, as dprintf's don't actually cause a stop. This
12957 also makes it so that the commands of multiple dprintfs at the same
12958 address are all handled. */
12959
12960 static void
12961 dprintf_after_condition_true (struct bpstats *bs)
12962 {
12963 struct bpstats tmp_bs;
12964 struct bpstats *tmp_bs_p = &tmp_bs;
12965
12966 /* dprintf's never cause a stop. This wasn't set in the
12967 check_status hook instead because that would make the dprintf's
12968 condition not be evaluated. */
12969 bs->stop = 0;
12970
12971 /* Run the command list here. Take ownership of it instead of
12972 copying. We never want these commands to run later in
12973 bpstat_do_actions, if a breakpoint that causes a stop happens to
12974 be set at same address as this dprintf, or even if running the
12975 commands here throws. */
12976 tmp_bs.commands = bs->commands;
12977 bs->commands = NULL;
12978
12979 bpstat_do_actions_1 (&tmp_bs_p);
12980
12981 /* 'tmp_bs.commands' will usually be NULL by now, but
12982 bpstat_do_actions_1 may return early without processing the whole
12983 list. */
12984 }
12985
12986 /* The breakpoint_ops structure to be used on static tracepoints with
12987 markers (`-m'). */
12988
12989 static void
12990 strace_marker_create_sals_from_location (const struct event_location *location,
12991 struct linespec_result *canonical,
12992 enum bptype type_wanted)
12993 {
12994 struct linespec_sals lsal;
12995 const char *arg_start, *arg;
12996
12997 arg = arg_start = get_linespec_location (location)->spec_string;
12998 lsal.sals = decode_static_tracepoint_spec (&arg);
12999
13000 std::string str (arg_start, arg - arg_start);
13001 const char *ptr = str.c_str ();
13002 canonical->location
13003 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13004
13005 lsal.canonical
13006 = xstrdup (event_location_to_string (canonical->location.get ()));
13007 canonical->lsals.push_back (std::move (lsal));
13008 }
13009
13010 static void
13011 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13012 struct linespec_result *canonical,
13013 gdb::unique_xmalloc_ptr<char> cond_string,
13014 gdb::unique_xmalloc_ptr<char> extra_string,
13015 enum bptype type_wanted,
13016 enum bpdisp disposition,
13017 int thread,
13018 int task, int ignore_count,
13019 const struct breakpoint_ops *ops,
13020 int from_tty, int enabled,
13021 int internal, unsigned flags)
13022 {
13023 const linespec_sals &lsal = canonical->lsals[0];
13024
13025 /* If the user is creating a static tracepoint by marker id
13026 (strace -m MARKER_ID), then store the sals index, so that
13027 breakpoint_re_set can try to match up which of the newly
13028 found markers corresponds to this one, and, don't try to
13029 expand multiple locations for each sal, given than SALS
13030 already should contain all sals for MARKER_ID. */
13031
13032 for (size_t i = 0; i < lsal.sals.size (); i++)
13033 {
13034 event_location_up location
13035 = copy_event_location (canonical->location.get ());
13036
13037 std::unique_ptr<tracepoint> tp (new tracepoint ());
13038 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13039 std::move (location), NULL,
13040 std::move (cond_string),
13041 std::move (extra_string),
13042 type_wanted, disposition,
13043 thread, task, ignore_count, ops,
13044 from_tty, enabled, internal, flags,
13045 canonical->special_display);
13046 /* Given that its possible to have multiple markers with
13047 the same string id, if the user is creating a static
13048 tracepoint by marker id ("strace -m MARKER_ID"), then
13049 store the sals index, so that breakpoint_re_set can
13050 try to match up which of the newly found markers
13051 corresponds to this one */
13052 tp->static_trace_marker_id_idx = i;
13053
13054 install_breakpoint (internal, std::move (tp), 0);
13055 }
13056 }
13057
13058 static std::vector<symtab_and_line>
13059 strace_marker_decode_location (struct breakpoint *b,
13060 const struct event_location *location,
13061 struct program_space *search_pspace)
13062 {
13063 struct tracepoint *tp = (struct tracepoint *) b;
13064 const char *s = get_linespec_location (location)->spec_string;
13065
13066 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13067 if (sals.size () > tp->static_trace_marker_id_idx)
13068 {
13069 sals[0] = sals[tp->static_trace_marker_id_idx];
13070 sals.resize (1);
13071 return sals;
13072 }
13073 else
13074 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13075 }
13076
13077 static struct breakpoint_ops strace_marker_breakpoint_ops;
13078
13079 static int
13080 strace_marker_p (struct breakpoint *b)
13081 {
13082 return b->ops == &strace_marker_breakpoint_ops;
13083 }
13084
13085 /* Delete a breakpoint and clean up all traces of it in the data
13086 structures. */
13087
13088 void
13089 delete_breakpoint (struct breakpoint *bpt)
13090 {
13091 struct breakpoint *b;
13092
13093 gdb_assert (bpt != NULL);
13094
13095 /* Has this bp already been deleted? This can happen because
13096 multiple lists can hold pointers to bp's. bpstat lists are
13097 especial culprits.
13098
13099 One example of this happening is a watchpoint's scope bp. When
13100 the scope bp triggers, we notice that the watchpoint is out of
13101 scope, and delete it. We also delete its scope bp. But the
13102 scope bp is marked "auto-deleting", and is already on a bpstat.
13103 That bpstat is then checked for auto-deleting bp's, which are
13104 deleted.
13105
13106 A real solution to this problem might involve reference counts in
13107 bp's, and/or giving them pointers back to their referencing
13108 bpstat's, and teaching delete_breakpoint to only free a bp's
13109 storage when no more references were extent. A cheaper bandaid
13110 was chosen. */
13111 if (bpt->type == bp_none)
13112 return;
13113
13114 /* At least avoid this stale reference until the reference counting
13115 of breakpoints gets resolved. */
13116 if (bpt->related_breakpoint != bpt)
13117 {
13118 struct breakpoint *related;
13119 struct watchpoint *w;
13120
13121 if (bpt->type == bp_watchpoint_scope)
13122 w = (struct watchpoint *) bpt->related_breakpoint;
13123 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13124 w = (struct watchpoint *) bpt;
13125 else
13126 w = NULL;
13127 if (w != NULL)
13128 watchpoint_del_at_next_stop (w);
13129
13130 /* Unlink bpt from the bpt->related_breakpoint ring. */
13131 for (related = bpt; related->related_breakpoint != bpt;
13132 related = related->related_breakpoint);
13133 related->related_breakpoint = bpt->related_breakpoint;
13134 bpt->related_breakpoint = bpt;
13135 }
13136
13137 /* watch_command_1 creates a watchpoint but only sets its number if
13138 update_watchpoint succeeds in creating its bp_locations. If there's
13139 a problem in that process, we'll be asked to delete the half-created
13140 watchpoint. In that case, don't announce the deletion. */
13141 if (bpt->number)
13142 gdb::observers::breakpoint_deleted.notify (bpt);
13143
13144 if (breakpoint_chain == bpt)
13145 breakpoint_chain = bpt->next;
13146
13147 ALL_BREAKPOINTS (b)
13148 if (b->next == bpt)
13149 {
13150 b->next = bpt->next;
13151 break;
13152 }
13153
13154 /* Be sure no bpstat's are pointing at the breakpoint after it's
13155 been freed. */
13156 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13157 in all threads for now. Note that we cannot just remove bpstats
13158 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13159 commands are associated with the bpstat; if we remove it here,
13160 then the later call to bpstat_do_actions (&stop_bpstat); in
13161 event-top.c won't do anything, and temporary breakpoints with
13162 commands won't work. */
13163
13164 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13165
13166 /* Now that breakpoint is removed from breakpoint list, update the
13167 global location list. This will remove locations that used to
13168 belong to this breakpoint. Do this before freeing the breakpoint
13169 itself, since remove_breakpoint looks at location's owner. It
13170 might be better design to have location completely
13171 self-contained, but it's not the case now. */
13172 update_global_location_list (UGLL_DONT_INSERT);
13173
13174 /* On the chance that someone will soon try again to delete this
13175 same bp, we mark it as deleted before freeing its storage. */
13176 bpt->type = bp_none;
13177 delete bpt;
13178 }
13179
13180 /* Iterator function to call a user-provided callback function once
13181 for each of B and its related breakpoints. */
13182
13183 static void
13184 iterate_over_related_breakpoints (struct breakpoint *b,
13185 gdb::function_view<void (breakpoint *)> function)
13186 {
13187 struct breakpoint *related;
13188
13189 related = b;
13190 do
13191 {
13192 struct breakpoint *next;
13193
13194 /* FUNCTION may delete RELATED. */
13195 next = related->related_breakpoint;
13196
13197 if (next == related)
13198 {
13199 /* RELATED is the last ring entry. */
13200 function (related);
13201
13202 /* FUNCTION may have deleted it, so we'd never reach back to
13203 B. There's nothing left to do anyway, so just break
13204 out. */
13205 break;
13206 }
13207 else
13208 function (related);
13209
13210 related = next;
13211 }
13212 while (related != b);
13213 }
13214
13215 static void
13216 delete_command (const char *arg, int from_tty)
13217 {
13218 struct breakpoint *b, *b_tmp;
13219
13220 dont_repeat ();
13221
13222 if (arg == 0)
13223 {
13224 int breaks_to_delete = 0;
13225
13226 /* Delete all breakpoints if no argument. Do not delete
13227 internal breakpoints, these have to be deleted with an
13228 explicit breakpoint number argument. */
13229 ALL_BREAKPOINTS (b)
13230 if (user_breakpoint_p (b))
13231 {
13232 breaks_to_delete = 1;
13233 break;
13234 }
13235
13236 /* Ask user only if there are some breakpoints to delete. */
13237 if (!from_tty
13238 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13239 {
13240 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13241 if (user_breakpoint_p (b))
13242 delete_breakpoint (b);
13243 }
13244 }
13245 else
13246 map_breakpoint_numbers
13247 (arg, [&] (breakpoint *br)
13248 {
13249 iterate_over_related_breakpoints (br, delete_breakpoint);
13250 });
13251 }
13252
13253 /* Return true if all locations of B bound to PSPACE are pending. If
13254 PSPACE is NULL, all locations of all program spaces are
13255 considered. */
13256
13257 static int
13258 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13259 {
13260 struct bp_location *loc;
13261
13262 for (loc = b->loc; loc != NULL; loc = loc->next)
13263 if ((pspace == NULL
13264 || loc->pspace == pspace)
13265 && !loc->shlib_disabled
13266 && !loc->pspace->executing_startup)
13267 return 0;
13268 return 1;
13269 }
13270
13271 /* Subroutine of update_breakpoint_locations to simplify it.
13272 Return non-zero if multiple fns in list LOC have the same name.
13273 Null names are ignored. */
13274
13275 static int
13276 ambiguous_names_p (struct bp_location *loc)
13277 {
13278 struct bp_location *l;
13279 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13280 xcalloc, xfree);
13281
13282 for (l = loc; l != NULL; l = l->next)
13283 {
13284 const char **slot;
13285 const char *name = l->function_name;
13286
13287 /* Allow for some names to be NULL, ignore them. */
13288 if (name == NULL)
13289 continue;
13290
13291 slot = (const char **) htab_find_slot (htab, (const void *) name,
13292 INSERT);
13293 /* NOTE: We can assume slot != NULL here because xcalloc never
13294 returns NULL. */
13295 if (*slot != NULL)
13296 {
13297 htab_delete (htab);
13298 return 1;
13299 }
13300 *slot = name;
13301 }
13302
13303 htab_delete (htab);
13304 return 0;
13305 }
13306
13307 /* When symbols change, it probably means the sources changed as well,
13308 and it might mean the static tracepoint markers are no longer at
13309 the same address or line numbers they used to be at last we
13310 checked. Losing your static tracepoints whenever you rebuild is
13311 undesirable. This function tries to resync/rematch gdb static
13312 tracepoints with the markers on the target, for static tracepoints
13313 that have not been set by marker id. Static tracepoint that have
13314 been set by marker id are reset by marker id in breakpoint_re_set.
13315 The heuristic is:
13316
13317 1) For a tracepoint set at a specific address, look for a marker at
13318 the old PC. If one is found there, assume to be the same marker.
13319 If the name / string id of the marker found is different from the
13320 previous known name, assume that means the user renamed the marker
13321 in the sources, and output a warning.
13322
13323 2) For a tracepoint set at a given line number, look for a marker
13324 at the new address of the old line number. If one is found there,
13325 assume to be the same marker. If the name / string id of the
13326 marker found is different from the previous known name, assume that
13327 means the user renamed the marker in the sources, and output a
13328 warning.
13329
13330 3) If a marker is no longer found at the same address or line, it
13331 may mean the marker no longer exists. But it may also just mean
13332 the code changed a bit. Maybe the user added a few lines of code
13333 that made the marker move up or down (in line number terms). Ask
13334 the target for info about the marker with the string id as we knew
13335 it. If found, update line number and address in the matching
13336 static tracepoint. This will get confused if there's more than one
13337 marker with the same ID (possible in UST, although unadvised
13338 precisely because it confuses tools). */
13339
13340 static struct symtab_and_line
13341 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13342 {
13343 struct tracepoint *tp = (struct tracepoint *) b;
13344 struct static_tracepoint_marker marker;
13345 CORE_ADDR pc;
13346
13347 pc = sal.pc;
13348 if (sal.line)
13349 find_line_pc (sal.symtab, sal.line, &pc);
13350
13351 if (target_static_tracepoint_marker_at (pc, &marker))
13352 {
13353 if (tp->static_trace_marker_id != marker.str_id)
13354 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13355 b->number, tp->static_trace_marker_id.c_str (),
13356 marker.str_id.c_str ());
13357
13358 tp->static_trace_marker_id = std::move (marker.str_id);
13359
13360 return sal;
13361 }
13362
13363 /* Old marker wasn't found on target at lineno. Try looking it up
13364 by string ID. */
13365 if (!sal.explicit_pc
13366 && sal.line != 0
13367 && sal.symtab != NULL
13368 && !tp->static_trace_marker_id.empty ())
13369 {
13370 std::vector<static_tracepoint_marker> markers
13371 = target_static_tracepoint_markers_by_strid
13372 (tp->static_trace_marker_id.c_str ());
13373
13374 if (!markers.empty ())
13375 {
13376 struct symbol *sym;
13377 struct static_tracepoint_marker *tpmarker;
13378 struct ui_out *uiout = current_uiout;
13379 struct explicit_location explicit_loc;
13380
13381 tpmarker = &markers[0];
13382
13383 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13384
13385 warning (_("marker for static tracepoint %d (%s) not "
13386 "found at previous line number"),
13387 b->number, tp->static_trace_marker_id.c_str ());
13388
13389 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13390 sym = find_pc_sect_function (tpmarker->address, NULL);
13391 uiout->text ("Now in ");
13392 if (sym)
13393 {
13394 uiout->field_string ("func", sym->print_name (),
13395 function_name_style.style ());
13396 uiout->text (" at ");
13397 }
13398 uiout->field_string ("file",
13399 symtab_to_filename_for_display (sal2.symtab),
13400 file_name_style.style ());
13401 uiout->text (":");
13402
13403 if (uiout->is_mi_like_p ())
13404 {
13405 const char *fullname = symtab_to_fullname (sal2.symtab);
13406
13407 uiout->field_string ("fullname", fullname);
13408 }
13409
13410 uiout->field_signed ("line", sal2.line);
13411 uiout->text ("\n");
13412
13413 b->loc->line_number = sal2.line;
13414 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13415
13416 b->location.reset (NULL);
13417 initialize_explicit_location (&explicit_loc);
13418 explicit_loc.source_filename
13419 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13420 explicit_loc.line_offset.offset = b->loc->line_number;
13421 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13422 b->location = new_explicit_location (&explicit_loc);
13423
13424 /* Might be nice to check if function changed, and warn if
13425 so. */
13426 }
13427 }
13428 return sal;
13429 }
13430
13431 /* Returns 1 iff locations A and B are sufficiently same that
13432 we don't need to report breakpoint as changed. */
13433
13434 static int
13435 locations_are_equal (struct bp_location *a, struct bp_location *b)
13436 {
13437 while (a && b)
13438 {
13439 if (a->address != b->address)
13440 return 0;
13441
13442 if (a->shlib_disabled != b->shlib_disabled)
13443 return 0;
13444
13445 if (a->enabled != b->enabled)
13446 return 0;
13447
13448 a = a->next;
13449 b = b->next;
13450 }
13451
13452 if ((a == NULL) != (b == NULL))
13453 return 0;
13454
13455 return 1;
13456 }
13457
13458 /* Split all locations of B that are bound to PSPACE out of B's
13459 location list to a separate list and return that list's head. If
13460 PSPACE is NULL, hoist out all locations of B. */
13461
13462 static struct bp_location *
13463 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13464 {
13465 struct bp_location head;
13466 struct bp_location *i = b->loc;
13467 struct bp_location **i_link = &b->loc;
13468 struct bp_location *hoisted = &head;
13469
13470 if (pspace == NULL)
13471 {
13472 i = b->loc;
13473 b->loc = NULL;
13474 return i;
13475 }
13476
13477 head.next = NULL;
13478
13479 while (i != NULL)
13480 {
13481 if (i->pspace == pspace)
13482 {
13483 *i_link = i->next;
13484 i->next = NULL;
13485 hoisted->next = i;
13486 hoisted = i;
13487 }
13488 else
13489 i_link = &i->next;
13490 i = *i_link;
13491 }
13492
13493 return head.next;
13494 }
13495
13496 /* Create new breakpoint locations for B (a hardware or software
13497 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13498 zero, then B is a ranged breakpoint. Only recreates locations for
13499 FILTER_PSPACE. Locations of other program spaces are left
13500 untouched. */
13501
13502 void
13503 update_breakpoint_locations (struct breakpoint *b,
13504 struct program_space *filter_pspace,
13505 gdb::array_view<const symtab_and_line> sals,
13506 gdb::array_view<const symtab_and_line> sals_end)
13507 {
13508 struct bp_location *existing_locations;
13509
13510 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13511 {
13512 /* Ranged breakpoints have only one start location and one end
13513 location. */
13514 b->enable_state = bp_disabled;
13515 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13516 "multiple locations found\n"),
13517 b->number);
13518 return;
13519 }
13520
13521 /* If there's no new locations, and all existing locations are
13522 pending, don't do anything. This optimizes the common case where
13523 all locations are in the same shared library, that was unloaded.
13524 We'd like to retain the location, so that when the library is
13525 loaded again, we don't loose the enabled/disabled status of the
13526 individual locations. */
13527 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13528 return;
13529
13530 existing_locations = hoist_existing_locations (b, filter_pspace);
13531
13532 for (const auto &sal : sals)
13533 {
13534 struct bp_location *new_loc;
13535
13536 switch_to_program_space_and_thread (sal.pspace);
13537
13538 new_loc = add_location_to_breakpoint (b, &sal);
13539
13540 /* Reparse conditions, they might contain references to the
13541 old symtab. */
13542 if (b->cond_string != NULL)
13543 {
13544 const char *s;
13545
13546 s = b->cond_string;
13547 try
13548 {
13549 new_loc->cond = parse_exp_1 (&s, sal.pc,
13550 block_for_pc (sal.pc),
13551 0);
13552 }
13553 catch (const gdb_exception_error &e)
13554 {
13555 warning (_("failed to reevaluate condition "
13556 "for breakpoint %d: %s"),
13557 b->number, e.what ());
13558 new_loc->enabled = 0;
13559 }
13560 }
13561
13562 if (!sals_end.empty ())
13563 {
13564 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13565
13566 new_loc->length = end - sals[0].pc + 1;
13567 }
13568 }
13569
13570 /* If possible, carry over 'disable' status from existing
13571 breakpoints. */
13572 {
13573 struct bp_location *e = existing_locations;
13574 /* If there are multiple breakpoints with the same function name,
13575 e.g. for inline functions, comparing function names won't work.
13576 Instead compare pc addresses; this is just a heuristic as things
13577 may have moved, but in practice it gives the correct answer
13578 often enough until a better solution is found. */
13579 int have_ambiguous_names = ambiguous_names_p (b->loc);
13580
13581 for (; e; e = e->next)
13582 {
13583 if (!e->enabled && e->function_name)
13584 {
13585 struct bp_location *l = b->loc;
13586 if (have_ambiguous_names)
13587 {
13588 for (; l; l = l->next)
13589 {
13590 /* Ignore software vs hardware location type at
13591 this point, because with "set breakpoint
13592 auto-hw", after a re-set, locations that were
13593 hardware can end up as software, or vice versa.
13594 As mentioned above, this is an heuristic and in
13595 practice should give the correct answer often
13596 enough. */
13597 if (breakpoint_locations_match (e, l, true))
13598 {
13599 l->enabled = 0;
13600 break;
13601 }
13602 }
13603 }
13604 else
13605 {
13606 for (; l; l = l->next)
13607 if (l->function_name
13608 && strcmp (e->function_name, l->function_name) == 0)
13609 {
13610 l->enabled = 0;
13611 break;
13612 }
13613 }
13614 }
13615 }
13616 }
13617
13618 if (!locations_are_equal (existing_locations, b->loc))
13619 gdb::observers::breakpoint_modified.notify (b);
13620 }
13621
13622 /* Find the SaL locations corresponding to the given LOCATION.
13623 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13624
13625 static std::vector<symtab_and_line>
13626 location_to_sals (struct breakpoint *b, struct event_location *location,
13627 struct program_space *search_pspace, int *found)
13628 {
13629 struct gdb_exception exception;
13630
13631 gdb_assert (b->ops != NULL);
13632
13633 std::vector<symtab_and_line> sals;
13634
13635 try
13636 {
13637 sals = b->ops->decode_location (b, location, search_pspace);
13638 }
13639 catch (gdb_exception_error &e)
13640 {
13641 int not_found_and_ok = 0;
13642
13643 /* For pending breakpoints, it's expected that parsing will
13644 fail until the right shared library is loaded. User has
13645 already told to create pending breakpoints and don't need
13646 extra messages. If breakpoint is in bp_shlib_disabled
13647 state, then user already saw the message about that
13648 breakpoint being disabled, and don't want to see more
13649 errors. */
13650 if (e.error == NOT_FOUND_ERROR
13651 && (b->condition_not_parsed
13652 || (b->loc != NULL
13653 && search_pspace != NULL
13654 && b->loc->pspace != search_pspace)
13655 || (b->loc && b->loc->shlib_disabled)
13656 || (b->loc && b->loc->pspace->executing_startup)
13657 || b->enable_state == bp_disabled))
13658 not_found_and_ok = 1;
13659
13660 if (!not_found_and_ok)
13661 {
13662 /* We surely don't want to warn about the same breakpoint
13663 10 times. One solution, implemented here, is disable
13664 the breakpoint on error. Another solution would be to
13665 have separate 'warning emitted' flag. Since this
13666 happens only when a binary has changed, I don't know
13667 which approach is better. */
13668 b->enable_state = bp_disabled;
13669 throw;
13670 }
13671
13672 exception = std::move (e);
13673 }
13674
13675 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13676 {
13677 for (auto &sal : sals)
13678 resolve_sal_pc (&sal);
13679 if (b->condition_not_parsed && b->extra_string != NULL)
13680 {
13681 char *cond_string, *extra_string;
13682 int thread, task;
13683
13684 find_condition_and_thread (b->extra_string, sals[0].pc,
13685 &cond_string, &thread, &task,
13686 &extra_string);
13687 gdb_assert (b->cond_string == NULL);
13688 if (cond_string)
13689 b->cond_string = cond_string;
13690 b->thread = thread;
13691 b->task = task;
13692 if (extra_string)
13693 {
13694 xfree (b->extra_string);
13695 b->extra_string = extra_string;
13696 }
13697 b->condition_not_parsed = 0;
13698 }
13699
13700 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13701 sals[0] = update_static_tracepoint (b, sals[0]);
13702
13703 *found = 1;
13704 }
13705 else
13706 *found = 0;
13707
13708 return sals;
13709 }
13710
13711 /* The default re_set method, for typical hardware or software
13712 breakpoints. Reevaluate the breakpoint and recreate its
13713 locations. */
13714
13715 static void
13716 breakpoint_re_set_default (struct breakpoint *b)
13717 {
13718 struct program_space *filter_pspace = current_program_space;
13719 std::vector<symtab_and_line> expanded, expanded_end;
13720
13721 int found;
13722 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13723 filter_pspace, &found);
13724 if (found)
13725 expanded = std::move (sals);
13726
13727 if (b->location_range_end != NULL)
13728 {
13729 std::vector<symtab_and_line> sals_end
13730 = location_to_sals (b, b->location_range_end.get (),
13731 filter_pspace, &found);
13732 if (found)
13733 expanded_end = std::move (sals_end);
13734 }
13735
13736 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13737 }
13738
13739 /* Default method for creating SALs from an address string. It basically
13740 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13741
13742 static void
13743 create_sals_from_location_default (const struct event_location *location,
13744 struct linespec_result *canonical,
13745 enum bptype type_wanted)
13746 {
13747 parse_breakpoint_sals (location, canonical);
13748 }
13749
13750 /* Call create_breakpoints_sal for the given arguments. This is the default
13751 function for the `create_breakpoints_sal' method of
13752 breakpoint_ops. */
13753
13754 static void
13755 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13756 struct linespec_result *canonical,
13757 gdb::unique_xmalloc_ptr<char> cond_string,
13758 gdb::unique_xmalloc_ptr<char> extra_string,
13759 enum bptype type_wanted,
13760 enum bpdisp disposition,
13761 int thread,
13762 int task, int ignore_count,
13763 const struct breakpoint_ops *ops,
13764 int from_tty, int enabled,
13765 int internal, unsigned flags)
13766 {
13767 create_breakpoints_sal (gdbarch, canonical,
13768 std::move (cond_string),
13769 std::move (extra_string),
13770 type_wanted, disposition,
13771 thread, task, ignore_count, ops, from_tty,
13772 enabled, internal, flags);
13773 }
13774
13775 /* Decode the line represented by S by calling decode_line_full. This is the
13776 default function for the `decode_location' method of breakpoint_ops. */
13777
13778 static std::vector<symtab_and_line>
13779 decode_location_default (struct breakpoint *b,
13780 const struct event_location *location,
13781 struct program_space *search_pspace)
13782 {
13783 struct linespec_result canonical;
13784
13785 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13786 NULL, 0, &canonical, multiple_symbols_all,
13787 b->filter.get ());
13788
13789 /* We should get 0 or 1 resulting SALs. */
13790 gdb_assert (canonical.lsals.size () < 2);
13791
13792 if (!canonical.lsals.empty ())
13793 {
13794 const linespec_sals &lsal = canonical.lsals[0];
13795 return std::move (lsal.sals);
13796 }
13797 return {};
13798 }
13799
13800 /* Reset a breakpoint. */
13801
13802 static void
13803 breakpoint_re_set_one (breakpoint *b)
13804 {
13805 input_radix = b->input_radix;
13806 set_language (b->language);
13807
13808 b->ops->re_set (b);
13809 }
13810
13811 /* Re-set breakpoint locations for the current program space.
13812 Locations bound to other program spaces are left untouched. */
13813
13814 void
13815 breakpoint_re_set (void)
13816 {
13817 struct breakpoint *b, *b_tmp;
13818
13819 {
13820 scoped_restore_current_language save_language;
13821 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13822 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13823
13824 /* breakpoint_re_set_one sets the current_language to the language
13825 of the breakpoint it is resetting (see prepare_re_set_context)
13826 before re-evaluating the breakpoint's location. This change can
13827 unfortunately get undone by accident if the language_mode is set
13828 to auto, and we either switch frames, or more likely in this context,
13829 we select the current frame.
13830
13831 We prevent this by temporarily turning the language_mode to
13832 language_mode_manual. We restore it once all breakpoints
13833 have been reset. */
13834 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13835 language_mode = language_mode_manual;
13836
13837 /* Note: we must not try to insert locations until after all
13838 breakpoints have been re-set. Otherwise, e.g., when re-setting
13839 breakpoint 1, we'd insert the locations of breakpoint 2, which
13840 hadn't been re-set yet, and thus may have stale locations. */
13841
13842 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13843 {
13844 try
13845 {
13846 breakpoint_re_set_one (b);
13847 }
13848 catch (const gdb_exception &ex)
13849 {
13850 exception_fprintf (gdb_stderr, ex,
13851 "Error in re-setting breakpoint %d: ",
13852 b->number);
13853 }
13854 }
13855
13856 jit_breakpoint_re_set ();
13857 }
13858
13859 create_overlay_event_breakpoint ();
13860 create_longjmp_master_breakpoint ();
13861 create_std_terminate_master_breakpoint ();
13862 create_exception_master_breakpoint ();
13863
13864 /* Now we can insert. */
13865 update_global_location_list (UGLL_MAY_INSERT);
13866 }
13867 \f
13868 /* Reset the thread number of this breakpoint:
13869
13870 - If the breakpoint is for all threads, leave it as-is.
13871 - Else, reset it to the current thread for inferior_ptid. */
13872 void
13873 breakpoint_re_set_thread (struct breakpoint *b)
13874 {
13875 if (b->thread != -1)
13876 {
13877 b->thread = inferior_thread ()->global_num;
13878
13879 /* We're being called after following a fork. The new fork is
13880 selected as current, and unless this was a vfork will have a
13881 different program space from the original thread. Reset that
13882 as well. */
13883 b->loc->pspace = current_program_space;
13884 }
13885 }
13886
13887 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13888 If from_tty is nonzero, it prints a message to that effect,
13889 which ends with a period (no newline). */
13890
13891 void
13892 set_ignore_count (int bptnum, int count, int from_tty)
13893 {
13894 struct breakpoint *b;
13895
13896 if (count < 0)
13897 count = 0;
13898
13899 ALL_BREAKPOINTS (b)
13900 if (b->number == bptnum)
13901 {
13902 if (is_tracepoint (b))
13903 {
13904 if (from_tty && count != 0)
13905 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13906 bptnum);
13907 return;
13908 }
13909
13910 b->ignore_count = count;
13911 if (from_tty)
13912 {
13913 if (count == 0)
13914 printf_filtered (_("Will stop next time "
13915 "breakpoint %d is reached."),
13916 bptnum);
13917 else if (count == 1)
13918 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13919 bptnum);
13920 else
13921 printf_filtered (_("Will ignore next %d "
13922 "crossings of breakpoint %d."),
13923 count, bptnum);
13924 }
13925 gdb::observers::breakpoint_modified.notify (b);
13926 return;
13927 }
13928
13929 error (_("No breakpoint number %d."), bptnum);
13930 }
13931
13932 /* Command to set ignore-count of breakpoint N to COUNT. */
13933
13934 static void
13935 ignore_command (const char *args, int from_tty)
13936 {
13937 const char *p = args;
13938 int num;
13939
13940 if (p == 0)
13941 error_no_arg (_("a breakpoint number"));
13942
13943 num = get_number (&p);
13944 if (num == 0)
13945 error (_("bad breakpoint number: '%s'"), args);
13946 if (*p == 0)
13947 error (_("Second argument (specified ignore-count) is missing."));
13948
13949 set_ignore_count (num,
13950 longest_to_int (value_as_long (parse_and_eval (p))),
13951 from_tty);
13952 if (from_tty)
13953 printf_filtered ("\n");
13954 }
13955 \f
13956
13957 /* Call FUNCTION on each of the breakpoints with numbers in the range
13958 defined by BP_NUM_RANGE (an inclusive range). */
13959
13960 static void
13961 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13962 gdb::function_view<void (breakpoint *)> function)
13963 {
13964 if (bp_num_range.first == 0)
13965 {
13966 warning (_("bad breakpoint number at or near '%d'"),
13967 bp_num_range.first);
13968 }
13969 else
13970 {
13971 struct breakpoint *b, *tmp;
13972
13973 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13974 {
13975 bool match = false;
13976
13977 ALL_BREAKPOINTS_SAFE (b, tmp)
13978 if (b->number == i)
13979 {
13980 match = true;
13981 function (b);
13982 break;
13983 }
13984 if (!match)
13985 printf_unfiltered (_("No breakpoint number %d.\n"), i);
13986 }
13987 }
13988 }
13989
13990 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13991 ARGS. */
13992
13993 static void
13994 map_breakpoint_numbers (const char *args,
13995 gdb::function_view<void (breakpoint *)> function)
13996 {
13997 if (args == NULL || *args == '\0')
13998 error_no_arg (_("one or more breakpoint numbers"));
13999
14000 number_or_range_parser parser (args);
14001
14002 while (!parser.finished ())
14003 {
14004 int num = parser.get_number ();
14005 map_breakpoint_number_range (std::make_pair (num, num), function);
14006 }
14007 }
14008
14009 /* Return the breakpoint location structure corresponding to the
14010 BP_NUM and LOC_NUM values. */
14011
14012 static struct bp_location *
14013 find_location_by_number (int bp_num, int loc_num)
14014 {
14015 struct breakpoint *b;
14016
14017 ALL_BREAKPOINTS (b)
14018 if (b->number == bp_num)
14019 {
14020 break;
14021 }
14022
14023 if (!b || b->number != bp_num)
14024 error (_("Bad breakpoint number '%d'"), bp_num);
14025
14026 if (loc_num == 0)
14027 error (_("Bad breakpoint location number '%d'"), loc_num);
14028
14029 int n = 0;
14030 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14031 if (++n == loc_num)
14032 return loc;
14033
14034 error (_("Bad breakpoint location number '%d'"), loc_num);
14035 }
14036
14037 /* Modes of operation for extract_bp_num. */
14038 enum class extract_bp_kind
14039 {
14040 /* Extracting a breakpoint number. */
14041 bp,
14042
14043 /* Extracting a location number. */
14044 loc,
14045 };
14046
14047 /* Extract a breakpoint or location number (as determined by KIND)
14048 from the string starting at START. TRAILER is a character which
14049 can be found after the number. If you don't want a trailer, use
14050 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14051 string. This always returns a positive integer. */
14052
14053 static int
14054 extract_bp_num (extract_bp_kind kind, const char *start,
14055 int trailer, const char **end_out = NULL)
14056 {
14057 const char *end = start;
14058 int num = get_number_trailer (&end, trailer);
14059 if (num < 0)
14060 error (kind == extract_bp_kind::bp
14061 ? _("Negative breakpoint number '%.*s'")
14062 : _("Negative breakpoint location number '%.*s'"),
14063 int (end - start), start);
14064 if (num == 0)
14065 error (kind == extract_bp_kind::bp
14066 ? _("Bad breakpoint number '%.*s'")
14067 : _("Bad breakpoint location number '%.*s'"),
14068 int (end - start), start);
14069
14070 if (end_out != NULL)
14071 *end_out = end;
14072 return num;
14073 }
14074
14075 /* Extract a breakpoint or location range (as determined by KIND) in
14076 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14077 representing the (inclusive) range. The returned pair's elements
14078 are always positive integers. */
14079
14080 static std::pair<int, int>
14081 extract_bp_or_bp_range (extract_bp_kind kind,
14082 const std::string &arg,
14083 std::string::size_type arg_offset)
14084 {
14085 std::pair<int, int> range;
14086 const char *bp_loc = &arg[arg_offset];
14087 std::string::size_type dash = arg.find ('-', arg_offset);
14088 if (dash != std::string::npos)
14089 {
14090 /* bp_loc is a range (x-z). */
14091 if (arg.length () == dash + 1)
14092 error (kind == extract_bp_kind::bp
14093 ? _("Bad breakpoint number at or near: '%s'")
14094 : _("Bad breakpoint location number at or near: '%s'"),
14095 bp_loc);
14096
14097 const char *end;
14098 const char *start_first = bp_loc;
14099 const char *start_second = &arg[dash + 1];
14100 range.first = extract_bp_num (kind, start_first, '-');
14101 range.second = extract_bp_num (kind, start_second, '\0', &end);
14102
14103 if (range.first > range.second)
14104 error (kind == extract_bp_kind::bp
14105 ? _("Inverted breakpoint range at '%.*s'")
14106 : _("Inverted breakpoint location range at '%.*s'"),
14107 int (end - start_first), start_first);
14108 }
14109 else
14110 {
14111 /* bp_loc is a single value. */
14112 range.first = extract_bp_num (kind, bp_loc, '\0');
14113 range.second = range.first;
14114 }
14115 return range;
14116 }
14117
14118 /* Extract the breakpoint/location range specified by ARG. Returns
14119 the breakpoint range in BP_NUM_RANGE, and the location range in
14120 BP_LOC_RANGE.
14121
14122 ARG may be in any of the following forms:
14123
14124 x where 'x' is a breakpoint number.
14125 x-y where 'x' and 'y' specify a breakpoint numbers range.
14126 x.y where 'x' is a breakpoint number and 'y' a location number.
14127 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14128 location number range.
14129 */
14130
14131 static void
14132 extract_bp_number_and_location (const std::string &arg,
14133 std::pair<int, int> &bp_num_range,
14134 std::pair<int, int> &bp_loc_range)
14135 {
14136 std::string::size_type dot = arg.find ('.');
14137
14138 if (dot != std::string::npos)
14139 {
14140 /* Handle 'x.y' and 'x.y-z' cases. */
14141
14142 if (arg.length () == dot + 1 || dot == 0)
14143 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14144
14145 bp_num_range.first
14146 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14147 bp_num_range.second = bp_num_range.first;
14148
14149 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14150 arg, dot + 1);
14151 }
14152 else
14153 {
14154 /* Handle x and x-y cases. */
14155
14156 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14157 bp_loc_range.first = 0;
14158 bp_loc_range.second = 0;
14159 }
14160 }
14161
14162 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14163 specifies whether to enable or disable. */
14164
14165 static void
14166 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14167 {
14168 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14169 if (loc != NULL)
14170 {
14171 if (loc->enabled != enable)
14172 {
14173 loc->enabled = enable;
14174 mark_breakpoint_location_modified (loc);
14175 }
14176 if (target_supports_enable_disable_tracepoint ()
14177 && current_trace_status ()->running && loc->owner
14178 && is_tracepoint (loc->owner))
14179 target_disable_tracepoint (loc);
14180 }
14181 update_global_location_list (UGLL_DONT_INSERT);
14182
14183 gdb::observers::breakpoint_modified.notify (loc->owner);
14184 }
14185
14186 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14187 number of the breakpoint, and BP_LOC_RANGE specifies the
14188 (inclusive) range of location numbers of that breakpoint to
14189 enable/disable. ENABLE specifies whether to enable or disable the
14190 location. */
14191
14192 static void
14193 enable_disable_breakpoint_location_range (int bp_num,
14194 std::pair<int, int> &bp_loc_range,
14195 bool enable)
14196 {
14197 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14198 enable_disable_bp_num_loc (bp_num, i, enable);
14199 }
14200
14201 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14202 If from_tty is nonzero, it prints a message to that effect,
14203 which ends with a period (no newline). */
14204
14205 void
14206 disable_breakpoint (struct breakpoint *bpt)
14207 {
14208 /* Never disable a watchpoint scope breakpoint; we want to
14209 hit them when we leave scope so we can delete both the
14210 watchpoint and its scope breakpoint at that time. */
14211 if (bpt->type == bp_watchpoint_scope)
14212 return;
14213
14214 bpt->enable_state = bp_disabled;
14215
14216 /* Mark breakpoint locations modified. */
14217 mark_breakpoint_modified (bpt);
14218
14219 if (target_supports_enable_disable_tracepoint ()
14220 && current_trace_status ()->running && is_tracepoint (bpt))
14221 {
14222 struct bp_location *location;
14223
14224 for (location = bpt->loc; location; location = location->next)
14225 target_disable_tracepoint (location);
14226 }
14227
14228 update_global_location_list (UGLL_DONT_INSERT);
14229
14230 gdb::observers::breakpoint_modified.notify (bpt);
14231 }
14232
14233 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14234 specified in ARGS. ARGS may be in any of the formats handled by
14235 extract_bp_number_and_location. ENABLE specifies whether to enable
14236 or disable the breakpoints/locations. */
14237
14238 static void
14239 enable_disable_command (const char *args, int from_tty, bool enable)
14240 {
14241 if (args == 0)
14242 {
14243 struct breakpoint *bpt;
14244
14245 ALL_BREAKPOINTS (bpt)
14246 if (user_breakpoint_p (bpt))
14247 {
14248 if (enable)
14249 enable_breakpoint (bpt);
14250 else
14251 disable_breakpoint (bpt);
14252 }
14253 }
14254 else
14255 {
14256 std::string num = extract_arg (&args);
14257
14258 while (!num.empty ())
14259 {
14260 std::pair<int, int> bp_num_range, bp_loc_range;
14261
14262 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14263
14264 if (bp_loc_range.first == bp_loc_range.second
14265 && bp_loc_range.first == 0)
14266 {
14267 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14268 map_breakpoint_number_range (bp_num_range,
14269 enable
14270 ? enable_breakpoint
14271 : disable_breakpoint);
14272 }
14273 else
14274 {
14275 /* Handle breakpoint ids with formats 'x.y' or
14276 'x.y-z'. */
14277 enable_disable_breakpoint_location_range
14278 (bp_num_range.first, bp_loc_range, enable);
14279 }
14280 num = extract_arg (&args);
14281 }
14282 }
14283 }
14284
14285 /* The disable command disables the specified breakpoints/locations
14286 (or all defined breakpoints) so they're no longer effective in
14287 stopping the inferior. ARGS may be in any of the forms defined in
14288 extract_bp_number_and_location. */
14289
14290 static void
14291 disable_command (const char *args, int from_tty)
14292 {
14293 enable_disable_command (args, from_tty, false);
14294 }
14295
14296 static void
14297 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14298 int count)
14299 {
14300 int target_resources_ok;
14301
14302 if (bpt->type == bp_hardware_breakpoint)
14303 {
14304 int i;
14305 i = hw_breakpoint_used_count ();
14306 target_resources_ok =
14307 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14308 i + 1, 0);
14309 if (target_resources_ok == 0)
14310 error (_("No hardware breakpoint support in the target."));
14311 else if (target_resources_ok < 0)
14312 error (_("Hardware breakpoints used exceeds limit."));
14313 }
14314
14315 if (is_watchpoint (bpt))
14316 {
14317 /* Initialize it just to avoid a GCC false warning. */
14318 enum enable_state orig_enable_state = bp_disabled;
14319
14320 try
14321 {
14322 struct watchpoint *w = (struct watchpoint *) bpt;
14323
14324 orig_enable_state = bpt->enable_state;
14325 bpt->enable_state = bp_enabled;
14326 update_watchpoint (w, 1 /* reparse */);
14327 }
14328 catch (const gdb_exception &e)
14329 {
14330 bpt->enable_state = orig_enable_state;
14331 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14332 bpt->number);
14333 return;
14334 }
14335 }
14336
14337 bpt->enable_state = bp_enabled;
14338
14339 /* Mark breakpoint locations modified. */
14340 mark_breakpoint_modified (bpt);
14341
14342 if (target_supports_enable_disable_tracepoint ()
14343 && current_trace_status ()->running && is_tracepoint (bpt))
14344 {
14345 struct bp_location *location;
14346
14347 for (location = bpt->loc; location; location = location->next)
14348 target_enable_tracepoint (location);
14349 }
14350
14351 bpt->disposition = disposition;
14352 bpt->enable_count = count;
14353 update_global_location_list (UGLL_MAY_INSERT);
14354
14355 gdb::observers::breakpoint_modified.notify (bpt);
14356 }
14357
14358
14359 void
14360 enable_breakpoint (struct breakpoint *bpt)
14361 {
14362 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14363 }
14364
14365 /* The enable command enables the specified breakpoints/locations (or
14366 all defined breakpoints) so they once again become (or continue to
14367 be) effective in stopping the inferior. ARGS may be in any of the
14368 forms defined in extract_bp_number_and_location. */
14369
14370 static void
14371 enable_command (const char *args, int from_tty)
14372 {
14373 enable_disable_command (args, from_tty, true);
14374 }
14375
14376 static void
14377 enable_once_command (const char *args, int from_tty)
14378 {
14379 map_breakpoint_numbers
14380 (args, [&] (breakpoint *b)
14381 {
14382 iterate_over_related_breakpoints
14383 (b, [&] (breakpoint *bpt)
14384 {
14385 enable_breakpoint_disp (bpt, disp_disable, 1);
14386 });
14387 });
14388 }
14389
14390 static void
14391 enable_count_command (const char *args, int from_tty)
14392 {
14393 int count;
14394
14395 if (args == NULL)
14396 error_no_arg (_("hit count"));
14397
14398 count = get_number (&args);
14399
14400 map_breakpoint_numbers
14401 (args, [&] (breakpoint *b)
14402 {
14403 iterate_over_related_breakpoints
14404 (b, [&] (breakpoint *bpt)
14405 {
14406 enable_breakpoint_disp (bpt, disp_disable, count);
14407 });
14408 });
14409 }
14410
14411 static void
14412 enable_delete_command (const char *args, int from_tty)
14413 {
14414 map_breakpoint_numbers
14415 (args, [&] (breakpoint *b)
14416 {
14417 iterate_over_related_breakpoints
14418 (b, [&] (breakpoint *bpt)
14419 {
14420 enable_breakpoint_disp (bpt, disp_del, 1);
14421 });
14422 });
14423 }
14424 \f
14425 /* Invalidate last known value of any hardware watchpoint if
14426 the memory which that value represents has been written to by
14427 GDB itself. */
14428
14429 static void
14430 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14431 CORE_ADDR addr, ssize_t len,
14432 const bfd_byte *data)
14433 {
14434 struct breakpoint *bp;
14435
14436 ALL_BREAKPOINTS (bp)
14437 if (bp->enable_state == bp_enabled
14438 && bp->type == bp_hardware_watchpoint)
14439 {
14440 struct watchpoint *wp = (struct watchpoint *) bp;
14441
14442 if (wp->val_valid && wp->val != nullptr)
14443 {
14444 struct bp_location *loc;
14445
14446 for (loc = bp->loc; loc != NULL; loc = loc->next)
14447 if (loc->loc_type == bp_loc_hardware_watchpoint
14448 && loc->address + loc->length > addr
14449 && addr + len > loc->address)
14450 {
14451 wp->val = NULL;
14452 wp->val_valid = false;
14453 }
14454 }
14455 }
14456 }
14457
14458 /* Create and insert a breakpoint for software single step. */
14459
14460 void
14461 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14462 const address_space *aspace,
14463 CORE_ADDR next_pc)
14464 {
14465 struct thread_info *tp = inferior_thread ();
14466 struct symtab_and_line sal;
14467 CORE_ADDR pc = next_pc;
14468
14469 if (tp->control.single_step_breakpoints == NULL)
14470 {
14471 tp->control.single_step_breakpoints
14472 = new_single_step_breakpoint (tp->global_num, gdbarch);
14473 }
14474
14475 sal = find_pc_line (pc, 0);
14476 sal.pc = pc;
14477 sal.section = find_pc_overlay (pc);
14478 sal.explicit_pc = 1;
14479 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14480
14481 update_global_location_list (UGLL_INSERT);
14482 }
14483
14484 /* Insert single step breakpoints according to the current state. */
14485
14486 int
14487 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14488 {
14489 struct regcache *regcache = get_current_regcache ();
14490 std::vector<CORE_ADDR> next_pcs;
14491
14492 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14493
14494 if (!next_pcs.empty ())
14495 {
14496 struct frame_info *frame = get_current_frame ();
14497 const address_space *aspace = get_frame_address_space (frame);
14498
14499 for (CORE_ADDR pc : next_pcs)
14500 insert_single_step_breakpoint (gdbarch, aspace, pc);
14501
14502 return 1;
14503 }
14504 else
14505 return 0;
14506 }
14507
14508 /* See breakpoint.h. */
14509
14510 int
14511 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14512 const address_space *aspace,
14513 CORE_ADDR pc)
14514 {
14515 struct bp_location *loc;
14516
14517 for (loc = bp->loc; loc != NULL; loc = loc->next)
14518 if (loc->inserted
14519 && breakpoint_location_address_match (loc, aspace, pc))
14520 return 1;
14521
14522 return 0;
14523 }
14524
14525 /* Check whether a software single-step breakpoint is inserted at
14526 PC. */
14527
14528 int
14529 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14530 CORE_ADDR pc)
14531 {
14532 struct breakpoint *bpt;
14533
14534 ALL_BREAKPOINTS (bpt)
14535 {
14536 if (bpt->type == bp_single_step
14537 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14538 return 1;
14539 }
14540 return 0;
14541 }
14542
14543 /* Tracepoint-specific operations. */
14544
14545 /* Set tracepoint count to NUM. */
14546 static void
14547 set_tracepoint_count (int num)
14548 {
14549 tracepoint_count = num;
14550 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14551 }
14552
14553 static void
14554 trace_command (const char *arg, int from_tty)
14555 {
14556 event_location_up location = string_to_event_location (&arg,
14557 current_language);
14558 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14559 (location.get (), true /* is_tracepoint */);
14560
14561 create_breakpoint (get_current_arch (),
14562 location.get (),
14563 NULL, 0, arg, 1 /* parse arg */,
14564 0 /* tempflag */,
14565 bp_tracepoint /* type_wanted */,
14566 0 /* Ignore count */,
14567 pending_break_support,
14568 ops,
14569 from_tty,
14570 1 /* enabled */,
14571 0 /* internal */, 0);
14572 }
14573
14574 static void
14575 ftrace_command (const char *arg, int from_tty)
14576 {
14577 event_location_up location = string_to_event_location (&arg,
14578 current_language);
14579 create_breakpoint (get_current_arch (),
14580 location.get (),
14581 NULL, 0, arg, 1 /* parse arg */,
14582 0 /* tempflag */,
14583 bp_fast_tracepoint /* type_wanted */,
14584 0 /* Ignore count */,
14585 pending_break_support,
14586 &tracepoint_breakpoint_ops,
14587 from_tty,
14588 1 /* enabled */,
14589 0 /* internal */, 0);
14590 }
14591
14592 /* strace command implementation. Creates a static tracepoint. */
14593
14594 static void
14595 strace_command (const char *arg, int from_tty)
14596 {
14597 struct breakpoint_ops *ops;
14598 event_location_up location;
14599
14600 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14601 or with a normal static tracepoint. */
14602 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14603 {
14604 ops = &strace_marker_breakpoint_ops;
14605 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14606 }
14607 else
14608 {
14609 ops = &tracepoint_breakpoint_ops;
14610 location = string_to_event_location (&arg, current_language);
14611 }
14612
14613 create_breakpoint (get_current_arch (),
14614 location.get (),
14615 NULL, 0, arg, 1 /* parse arg */,
14616 0 /* tempflag */,
14617 bp_static_tracepoint /* type_wanted */,
14618 0 /* Ignore count */,
14619 pending_break_support,
14620 ops,
14621 from_tty,
14622 1 /* enabled */,
14623 0 /* internal */, 0);
14624 }
14625
14626 /* Set up a fake reader function that gets command lines from a linked
14627 list that was acquired during tracepoint uploading. */
14628
14629 static struct uploaded_tp *this_utp;
14630 static int next_cmd;
14631
14632 static char *
14633 read_uploaded_action (void)
14634 {
14635 char *rslt = nullptr;
14636
14637 if (next_cmd < this_utp->cmd_strings.size ())
14638 {
14639 rslt = this_utp->cmd_strings[next_cmd].get ();
14640 next_cmd++;
14641 }
14642
14643 return rslt;
14644 }
14645
14646 /* Given information about a tracepoint as recorded on a target (which
14647 can be either a live system or a trace file), attempt to create an
14648 equivalent GDB tracepoint. This is not a reliable process, since
14649 the target does not necessarily have all the information used when
14650 the tracepoint was originally defined. */
14651
14652 struct tracepoint *
14653 create_tracepoint_from_upload (struct uploaded_tp *utp)
14654 {
14655 const char *addr_str;
14656 char small_buf[100];
14657 struct tracepoint *tp;
14658
14659 if (utp->at_string)
14660 addr_str = utp->at_string.get ();
14661 else
14662 {
14663 /* In the absence of a source location, fall back to raw
14664 address. Since there is no way to confirm that the address
14665 means the same thing as when the trace was started, warn the
14666 user. */
14667 warning (_("Uploaded tracepoint %d has no "
14668 "source location, using raw address"),
14669 utp->number);
14670 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14671 addr_str = small_buf;
14672 }
14673
14674 /* There's not much we can do with a sequence of bytecodes. */
14675 if (utp->cond && !utp->cond_string)
14676 warning (_("Uploaded tracepoint %d condition "
14677 "has no source form, ignoring it"),
14678 utp->number);
14679
14680 event_location_up location = string_to_event_location (&addr_str,
14681 current_language);
14682 if (!create_breakpoint (get_current_arch (),
14683 location.get (),
14684 utp->cond_string.get (), -1, addr_str,
14685 0 /* parse cond/thread */,
14686 0 /* tempflag */,
14687 utp->type /* type_wanted */,
14688 0 /* Ignore count */,
14689 pending_break_support,
14690 &tracepoint_breakpoint_ops,
14691 0 /* from_tty */,
14692 utp->enabled /* enabled */,
14693 0 /* internal */,
14694 CREATE_BREAKPOINT_FLAGS_INSERTED))
14695 return NULL;
14696
14697 /* Get the tracepoint we just created. */
14698 tp = get_tracepoint (tracepoint_count);
14699 gdb_assert (tp != NULL);
14700
14701 if (utp->pass > 0)
14702 {
14703 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14704 tp->number);
14705
14706 trace_pass_command (small_buf, 0);
14707 }
14708
14709 /* If we have uploaded versions of the original commands, set up a
14710 special-purpose "reader" function and call the usual command line
14711 reader, then pass the result to the breakpoint command-setting
14712 function. */
14713 if (!utp->cmd_strings.empty ())
14714 {
14715 counted_command_line cmd_list;
14716
14717 this_utp = utp;
14718 next_cmd = 0;
14719
14720 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14721
14722 breakpoint_set_commands (tp, std::move (cmd_list));
14723 }
14724 else if (!utp->actions.empty ()
14725 || !utp->step_actions.empty ())
14726 warning (_("Uploaded tracepoint %d actions "
14727 "have no source form, ignoring them"),
14728 utp->number);
14729
14730 /* Copy any status information that might be available. */
14731 tp->hit_count = utp->hit_count;
14732 tp->traceframe_usage = utp->traceframe_usage;
14733
14734 return tp;
14735 }
14736
14737 /* Print information on tracepoint number TPNUM_EXP, or all if
14738 omitted. */
14739
14740 static void
14741 info_tracepoints_command (const char *args, int from_tty)
14742 {
14743 struct ui_out *uiout = current_uiout;
14744 int num_printed;
14745
14746 num_printed = breakpoint_1 (args, false, is_tracepoint);
14747
14748 if (num_printed == 0)
14749 {
14750 if (args == NULL || *args == '\0')
14751 uiout->message ("No tracepoints.\n");
14752 else
14753 uiout->message ("No tracepoint matching '%s'.\n", args);
14754 }
14755
14756 default_collect_info ();
14757 }
14758
14759 /* The 'enable trace' command enables tracepoints.
14760 Not supported by all targets. */
14761 static void
14762 enable_trace_command (const char *args, int from_tty)
14763 {
14764 enable_command (args, from_tty);
14765 }
14766
14767 /* The 'disable trace' command disables tracepoints.
14768 Not supported by all targets. */
14769 static void
14770 disable_trace_command (const char *args, int from_tty)
14771 {
14772 disable_command (args, from_tty);
14773 }
14774
14775 /* Remove a tracepoint (or all if no argument). */
14776 static void
14777 delete_trace_command (const char *arg, int from_tty)
14778 {
14779 struct breakpoint *b, *b_tmp;
14780
14781 dont_repeat ();
14782
14783 if (arg == 0)
14784 {
14785 int breaks_to_delete = 0;
14786
14787 /* Delete all breakpoints if no argument.
14788 Do not delete internal or call-dummy breakpoints, these
14789 have to be deleted with an explicit breakpoint number
14790 argument. */
14791 ALL_TRACEPOINTS (b)
14792 if (is_tracepoint (b) && user_breakpoint_p (b))
14793 {
14794 breaks_to_delete = 1;
14795 break;
14796 }
14797
14798 /* Ask user only if there are some breakpoints to delete. */
14799 if (!from_tty
14800 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14801 {
14802 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14803 if (is_tracepoint (b) && user_breakpoint_p (b))
14804 delete_breakpoint (b);
14805 }
14806 }
14807 else
14808 map_breakpoint_numbers
14809 (arg, [&] (breakpoint *br)
14810 {
14811 iterate_over_related_breakpoints (br, delete_breakpoint);
14812 });
14813 }
14814
14815 /* Helper function for trace_pass_command. */
14816
14817 static void
14818 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14819 {
14820 tp->pass_count = count;
14821 gdb::observers::breakpoint_modified.notify (tp);
14822 if (from_tty)
14823 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14824 tp->number, count);
14825 }
14826
14827 /* Set passcount for tracepoint.
14828
14829 First command argument is passcount, second is tracepoint number.
14830 If tracepoint number omitted, apply to most recently defined.
14831 Also accepts special argument "all". */
14832
14833 static void
14834 trace_pass_command (const char *args, int from_tty)
14835 {
14836 struct tracepoint *t1;
14837 ULONGEST count;
14838
14839 if (args == 0 || *args == 0)
14840 error (_("passcount command requires an "
14841 "argument (count + optional TP num)"));
14842
14843 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14844
14845 args = skip_spaces (args);
14846 if (*args && strncasecmp (args, "all", 3) == 0)
14847 {
14848 struct breakpoint *b;
14849
14850 args += 3; /* Skip special argument "all". */
14851 if (*args)
14852 error (_("Junk at end of arguments."));
14853
14854 ALL_TRACEPOINTS (b)
14855 {
14856 t1 = (struct tracepoint *) b;
14857 trace_pass_set_count (t1, count, from_tty);
14858 }
14859 }
14860 else if (*args == '\0')
14861 {
14862 t1 = get_tracepoint_by_number (&args, NULL);
14863 if (t1)
14864 trace_pass_set_count (t1, count, from_tty);
14865 }
14866 else
14867 {
14868 number_or_range_parser parser (args);
14869 while (!parser.finished ())
14870 {
14871 t1 = get_tracepoint_by_number (&args, &parser);
14872 if (t1)
14873 trace_pass_set_count (t1, count, from_tty);
14874 }
14875 }
14876 }
14877
14878 struct tracepoint *
14879 get_tracepoint (int num)
14880 {
14881 struct breakpoint *t;
14882
14883 ALL_TRACEPOINTS (t)
14884 if (t->number == num)
14885 return (struct tracepoint *) t;
14886
14887 return NULL;
14888 }
14889
14890 /* Find the tracepoint with the given target-side number (which may be
14891 different from the tracepoint number after disconnecting and
14892 reconnecting). */
14893
14894 struct tracepoint *
14895 get_tracepoint_by_number_on_target (int num)
14896 {
14897 struct breakpoint *b;
14898
14899 ALL_TRACEPOINTS (b)
14900 {
14901 struct tracepoint *t = (struct tracepoint *) b;
14902
14903 if (t->number_on_target == num)
14904 return t;
14905 }
14906
14907 return NULL;
14908 }
14909
14910 /* Utility: parse a tracepoint number and look it up in the list.
14911 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14912 If the argument is missing, the most recent tracepoint
14913 (tracepoint_count) is returned. */
14914
14915 struct tracepoint *
14916 get_tracepoint_by_number (const char **arg,
14917 number_or_range_parser *parser)
14918 {
14919 struct breakpoint *t;
14920 int tpnum;
14921 const char *instring = arg == NULL ? NULL : *arg;
14922
14923 if (parser != NULL)
14924 {
14925 gdb_assert (!parser->finished ());
14926 tpnum = parser->get_number ();
14927 }
14928 else if (arg == NULL || *arg == NULL || ! **arg)
14929 tpnum = tracepoint_count;
14930 else
14931 tpnum = get_number (arg);
14932
14933 if (tpnum <= 0)
14934 {
14935 if (instring && *instring)
14936 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14937 instring);
14938 else
14939 printf_filtered (_("No previous tracepoint\n"));
14940 return NULL;
14941 }
14942
14943 ALL_TRACEPOINTS (t)
14944 if (t->number == tpnum)
14945 {
14946 return (struct tracepoint *) t;
14947 }
14948
14949 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14950 return NULL;
14951 }
14952
14953 void
14954 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14955 {
14956 if (b->thread != -1)
14957 fprintf_unfiltered (fp, " thread %d", b->thread);
14958
14959 if (b->task != 0)
14960 fprintf_unfiltered (fp, " task %d", b->task);
14961
14962 fprintf_unfiltered (fp, "\n");
14963 }
14964
14965 /* Save information on user settable breakpoints (watchpoints, etc) to
14966 a new script file named FILENAME. If FILTER is non-NULL, call it
14967 on each breakpoint and only include the ones for which it returns
14968 true. */
14969
14970 static void
14971 save_breakpoints (const char *filename, int from_tty,
14972 bool (*filter) (const struct breakpoint *))
14973 {
14974 struct breakpoint *tp;
14975 int any = 0;
14976 int extra_trace_bits = 0;
14977
14978 if (filename == 0 || *filename == 0)
14979 error (_("Argument required (file name in which to save)"));
14980
14981 /* See if we have anything to save. */
14982 ALL_BREAKPOINTS (tp)
14983 {
14984 /* Skip internal and momentary breakpoints. */
14985 if (!user_breakpoint_p (tp))
14986 continue;
14987
14988 /* If we have a filter, only save the breakpoints it accepts. */
14989 if (filter && !filter (tp))
14990 continue;
14991
14992 any = 1;
14993
14994 if (is_tracepoint (tp))
14995 {
14996 extra_trace_bits = 1;
14997
14998 /* We can stop searching. */
14999 break;
15000 }
15001 }
15002
15003 if (!any)
15004 {
15005 warning (_("Nothing to save."));
15006 return;
15007 }
15008
15009 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15010
15011 stdio_file fp;
15012
15013 if (!fp.open (expanded_filename.get (), "w"))
15014 error (_("Unable to open file '%s' for saving (%s)"),
15015 expanded_filename.get (), safe_strerror (errno));
15016
15017 if (extra_trace_bits)
15018 save_trace_state_variables (&fp);
15019
15020 ALL_BREAKPOINTS (tp)
15021 {
15022 /* Skip internal and momentary breakpoints. */
15023 if (!user_breakpoint_p (tp))
15024 continue;
15025
15026 /* If we have a filter, only save the breakpoints it accepts. */
15027 if (filter && !filter (tp))
15028 continue;
15029
15030 tp->ops->print_recreate (tp, &fp);
15031
15032 /* Note, we can't rely on tp->number for anything, as we can't
15033 assume the recreated breakpoint numbers will match. Use $bpnum
15034 instead. */
15035
15036 if (tp->cond_string)
15037 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15038
15039 if (tp->ignore_count)
15040 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15041
15042 if (tp->type != bp_dprintf && tp->commands)
15043 {
15044 fp.puts (" commands\n");
15045
15046 current_uiout->redirect (&fp);
15047 try
15048 {
15049 print_command_lines (current_uiout, tp->commands.get (), 2);
15050 }
15051 catch (const gdb_exception &ex)
15052 {
15053 current_uiout->redirect (NULL);
15054 throw;
15055 }
15056
15057 current_uiout->redirect (NULL);
15058 fp.puts (" end\n");
15059 }
15060
15061 if (tp->enable_state == bp_disabled)
15062 fp.puts ("disable $bpnum\n");
15063
15064 /* If this is a multi-location breakpoint, check if the locations
15065 should be individually disabled. Watchpoint locations are
15066 special, and not user visible. */
15067 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15068 {
15069 struct bp_location *loc;
15070 int n = 1;
15071
15072 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15073 if (!loc->enabled)
15074 fp.printf ("disable $bpnum.%d\n", n);
15075 }
15076 }
15077
15078 if (extra_trace_bits && *default_collect)
15079 fp.printf ("set default-collect %s\n", default_collect);
15080
15081 if (from_tty)
15082 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15083 }
15084
15085 /* The `save breakpoints' command. */
15086
15087 static void
15088 save_breakpoints_command (const char *args, int from_tty)
15089 {
15090 save_breakpoints (args, from_tty, NULL);
15091 }
15092
15093 /* The `save tracepoints' command. */
15094
15095 static void
15096 save_tracepoints_command (const char *args, int from_tty)
15097 {
15098 save_breakpoints (args, from_tty, is_tracepoint);
15099 }
15100
15101 /* Create a vector of all tracepoints. */
15102
15103 std::vector<breakpoint *>
15104 all_tracepoints (void)
15105 {
15106 std::vector<breakpoint *> tp_vec;
15107 struct breakpoint *tp;
15108
15109 ALL_TRACEPOINTS (tp)
15110 {
15111 tp_vec.push_back (tp);
15112 }
15113
15114 return tp_vec;
15115 }
15116
15117 \f
15118 /* This help string is used to consolidate all the help string for specifying
15119 locations used by several commands. */
15120
15121 #define LOCATION_HELP_STRING \
15122 "Linespecs are colon-separated lists of location parameters, such as\n\
15123 source filename, function name, label name, and line number.\n\
15124 Example: To specify the start of a label named \"the_top\" in the\n\
15125 function \"fact\" in the file \"factorial.c\", use\n\
15126 \"factorial.c:fact:the_top\".\n\
15127 \n\
15128 Address locations begin with \"*\" and specify an exact address in the\n\
15129 program. Example: To specify the fourth byte past the start function\n\
15130 \"main\", use \"*main + 4\".\n\
15131 \n\
15132 Explicit locations are similar to linespecs but use an option/argument\n\
15133 syntax to specify location parameters.\n\
15134 Example: To specify the start of the label named \"the_top\" in the\n\
15135 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15136 -function fact -label the_top\".\n\
15137 \n\
15138 By default, a specified function is matched against the program's\n\
15139 functions in all scopes. For C++, this means in all namespaces and\n\
15140 classes. For Ada, this means in all packages. E.g., in C++,\n\
15141 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15142 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15143 specified name as a complete fully-qualified name instead."
15144
15145 /* This help string is used for the break, hbreak, tbreak and thbreak
15146 commands. It is defined as a macro to prevent duplication.
15147 COMMAND should be a string constant containing the name of the
15148 command. */
15149
15150 #define BREAK_ARGS_HELP(command) \
15151 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15152 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15153 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15154 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15155 `-probe-dtrace' (for a DTrace probe).\n\
15156 LOCATION may be a linespec, address, or explicit location as described\n\
15157 below.\n\
15158 \n\
15159 With no LOCATION, uses current execution address of the selected\n\
15160 stack frame. This is useful for breaking on return to a stack frame.\n\
15161 \n\
15162 THREADNUM is the number from \"info threads\".\n\
15163 CONDITION is a boolean expression.\n\
15164 \n" LOCATION_HELP_STRING "\n\n\
15165 Multiple breakpoints at one place are permitted, and useful if their\n\
15166 conditions are different.\n\
15167 \n\
15168 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15169
15170 /* List of subcommands for "catch". */
15171 static struct cmd_list_element *catch_cmdlist;
15172
15173 /* List of subcommands for "tcatch". */
15174 static struct cmd_list_element *tcatch_cmdlist;
15175
15176 void
15177 add_catch_command (const char *name, const char *docstring,
15178 cmd_const_sfunc_ftype *sfunc,
15179 completer_ftype *completer,
15180 void *user_data_catch,
15181 void *user_data_tcatch)
15182 {
15183 struct cmd_list_element *command;
15184
15185 command = add_cmd (name, class_breakpoint, docstring,
15186 &catch_cmdlist);
15187 set_cmd_sfunc (command, sfunc);
15188 set_cmd_context (command, user_data_catch);
15189 set_cmd_completer (command, completer);
15190
15191 command = add_cmd (name, class_breakpoint, docstring,
15192 &tcatch_cmdlist);
15193 set_cmd_sfunc (command, sfunc);
15194 set_cmd_context (command, user_data_tcatch);
15195 set_cmd_completer (command, completer);
15196 }
15197
15198 struct breakpoint *
15199 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15200 {
15201 struct breakpoint *b, *b_tmp;
15202
15203 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15204 {
15205 if (callback (b))
15206 return b;
15207 }
15208
15209 return NULL;
15210 }
15211
15212 /* Zero if any of the breakpoint's locations could be a location where
15213 functions have been inlined, nonzero otherwise. */
15214
15215 static int
15216 is_non_inline_function (struct breakpoint *b)
15217 {
15218 /* The shared library event breakpoint is set on the address of a
15219 non-inline function. */
15220 if (b->type == bp_shlib_event)
15221 return 1;
15222
15223 return 0;
15224 }
15225
15226 /* Nonzero if the specified PC cannot be a location where functions
15227 have been inlined. */
15228
15229 int
15230 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15231 const struct target_waitstatus *ws)
15232 {
15233 struct breakpoint *b;
15234 struct bp_location *bl;
15235
15236 ALL_BREAKPOINTS (b)
15237 {
15238 if (!is_non_inline_function (b))
15239 continue;
15240
15241 for (bl = b->loc; bl != NULL; bl = bl->next)
15242 {
15243 if (!bl->shlib_disabled
15244 && bpstat_check_location (bl, aspace, pc, ws))
15245 return 1;
15246 }
15247 }
15248
15249 return 0;
15250 }
15251
15252 /* Remove any references to OBJFILE which is going to be freed. */
15253
15254 void
15255 breakpoint_free_objfile (struct objfile *objfile)
15256 {
15257 struct bp_location **locp, *loc;
15258
15259 ALL_BP_LOCATIONS (loc, locp)
15260 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15261 loc->symtab = NULL;
15262 }
15263
15264 void
15265 initialize_breakpoint_ops (void)
15266 {
15267 static int initialized = 0;
15268
15269 struct breakpoint_ops *ops;
15270
15271 if (initialized)
15272 return;
15273 initialized = 1;
15274
15275 /* The breakpoint_ops structure to be inherit by all kinds of
15276 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15277 internal and momentary breakpoints, etc.). */
15278 ops = &bkpt_base_breakpoint_ops;
15279 *ops = base_breakpoint_ops;
15280 ops->re_set = bkpt_re_set;
15281 ops->insert_location = bkpt_insert_location;
15282 ops->remove_location = bkpt_remove_location;
15283 ops->breakpoint_hit = bkpt_breakpoint_hit;
15284 ops->create_sals_from_location = bkpt_create_sals_from_location;
15285 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15286 ops->decode_location = bkpt_decode_location;
15287
15288 /* The breakpoint_ops structure to be used in regular breakpoints. */
15289 ops = &bkpt_breakpoint_ops;
15290 *ops = bkpt_base_breakpoint_ops;
15291 ops->re_set = bkpt_re_set;
15292 ops->resources_needed = bkpt_resources_needed;
15293 ops->print_it = bkpt_print_it;
15294 ops->print_mention = bkpt_print_mention;
15295 ops->print_recreate = bkpt_print_recreate;
15296
15297 /* Ranged breakpoints. */
15298 ops = &ranged_breakpoint_ops;
15299 *ops = bkpt_breakpoint_ops;
15300 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15301 ops->resources_needed = resources_needed_ranged_breakpoint;
15302 ops->print_it = print_it_ranged_breakpoint;
15303 ops->print_one = print_one_ranged_breakpoint;
15304 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15305 ops->print_mention = print_mention_ranged_breakpoint;
15306 ops->print_recreate = print_recreate_ranged_breakpoint;
15307
15308 /* Internal breakpoints. */
15309 ops = &internal_breakpoint_ops;
15310 *ops = bkpt_base_breakpoint_ops;
15311 ops->re_set = internal_bkpt_re_set;
15312 ops->check_status = internal_bkpt_check_status;
15313 ops->print_it = internal_bkpt_print_it;
15314 ops->print_mention = internal_bkpt_print_mention;
15315
15316 /* Momentary breakpoints. */
15317 ops = &momentary_breakpoint_ops;
15318 *ops = bkpt_base_breakpoint_ops;
15319 ops->re_set = momentary_bkpt_re_set;
15320 ops->check_status = momentary_bkpt_check_status;
15321 ops->print_it = momentary_bkpt_print_it;
15322 ops->print_mention = momentary_bkpt_print_mention;
15323
15324 /* Probe breakpoints. */
15325 ops = &bkpt_probe_breakpoint_ops;
15326 *ops = bkpt_breakpoint_ops;
15327 ops->insert_location = bkpt_probe_insert_location;
15328 ops->remove_location = bkpt_probe_remove_location;
15329 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15330 ops->decode_location = bkpt_probe_decode_location;
15331
15332 /* Watchpoints. */
15333 ops = &watchpoint_breakpoint_ops;
15334 *ops = base_breakpoint_ops;
15335 ops->re_set = re_set_watchpoint;
15336 ops->insert_location = insert_watchpoint;
15337 ops->remove_location = remove_watchpoint;
15338 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15339 ops->check_status = check_status_watchpoint;
15340 ops->resources_needed = resources_needed_watchpoint;
15341 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15342 ops->print_it = print_it_watchpoint;
15343 ops->print_mention = print_mention_watchpoint;
15344 ops->print_recreate = print_recreate_watchpoint;
15345 ops->explains_signal = explains_signal_watchpoint;
15346
15347 /* Masked watchpoints. */
15348 ops = &masked_watchpoint_breakpoint_ops;
15349 *ops = watchpoint_breakpoint_ops;
15350 ops->insert_location = insert_masked_watchpoint;
15351 ops->remove_location = remove_masked_watchpoint;
15352 ops->resources_needed = resources_needed_masked_watchpoint;
15353 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15354 ops->print_it = print_it_masked_watchpoint;
15355 ops->print_one_detail = print_one_detail_masked_watchpoint;
15356 ops->print_mention = print_mention_masked_watchpoint;
15357 ops->print_recreate = print_recreate_masked_watchpoint;
15358
15359 /* Tracepoints. */
15360 ops = &tracepoint_breakpoint_ops;
15361 *ops = base_breakpoint_ops;
15362 ops->re_set = tracepoint_re_set;
15363 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15364 ops->print_one_detail = tracepoint_print_one_detail;
15365 ops->print_mention = tracepoint_print_mention;
15366 ops->print_recreate = tracepoint_print_recreate;
15367 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15368 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15369 ops->decode_location = tracepoint_decode_location;
15370
15371 /* Probe tracepoints. */
15372 ops = &tracepoint_probe_breakpoint_ops;
15373 *ops = tracepoint_breakpoint_ops;
15374 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15375 ops->decode_location = tracepoint_probe_decode_location;
15376
15377 /* Static tracepoints with marker (`-m'). */
15378 ops = &strace_marker_breakpoint_ops;
15379 *ops = tracepoint_breakpoint_ops;
15380 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15381 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15382 ops->decode_location = strace_marker_decode_location;
15383
15384 /* Fork catchpoints. */
15385 ops = &catch_fork_breakpoint_ops;
15386 *ops = base_breakpoint_ops;
15387 ops->insert_location = insert_catch_fork;
15388 ops->remove_location = remove_catch_fork;
15389 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15390 ops->print_it = print_it_catch_fork;
15391 ops->print_one = print_one_catch_fork;
15392 ops->print_mention = print_mention_catch_fork;
15393 ops->print_recreate = print_recreate_catch_fork;
15394
15395 /* Vfork catchpoints. */
15396 ops = &catch_vfork_breakpoint_ops;
15397 *ops = base_breakpoint_ops;
15398 ops->insert_location = insert_catch_vfork;
15399 ops->remove_location = remove_catch_vfork;
15400 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15401 ops->print_it = print_it_catch_vfork;
15402 ops->print_one = print_one_catch_vfork;
15403 ops->print_mention = print_mention_catch_vfork;
15404 ops->print_recreate = print_recreate_catch_vfork;
15405
15406 /* Exec catchpoints. */
15407 ops = &catch_exec_breakpoint_ops;
15408 *ops = base_breakpoint_ops;
15409 ops->insert_location = insert_catch_exec;
15410 ops->remove_location = remove_catch_exec;
15411 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15412 ops->print_it = print_it_catch_exec;
15413 ops->print_one = print_one_catch_exec;
15414 ops->print_mention = print_mention_catch_exec;
15415 ops->print_recreate = print_recreate_catch_exec;
15416
15417 /* Solib-related catchpoints. */
15418 ops = &catch_solib_breakpoint_ops;
15419 *ops = base_breakpoint_ops;
15420 ops->insert_location = insert_catch_solib;
15421 ops->remove_location = remove_catch_solib;
15422 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15423 ops->check_status = check_status_catch_solib;
15424 ops->print_it = print_it_catch_solib;
15425 ops->print_one = print_one_catch_solib;
15426 ops->print_mention = print_mention_catch_solib;
15427 ops->print_recreate = print_recreate_catch_solib;
15428
15429 ops = &dprintf_breakpoint_ops;
15430 *ops = bkpt_base_breakpoint_ops;
15431 ops->re_set = dprintf_re_set;
15432 ops->resources_needed = bkpt_resources_needed;
15433 ops->print_it = bkpt_print_it;
15434 ops->print_mention = bkpt_print_mention;
15435 ops->print_recreate = dprintf_print_recreate;
15436 ops->after_condition_true = dprintf_after_condition_true;
15437 ops->breakpoint_hit = dprintf_breakpoint_hit;
15438 }
15439
15440 /* Chain containing all defined "enable breakpoint" subcommands. */
15441
15442 static struct cmd_list_element *enablebreaklist = NULL;
15443
15444 /* See breakpoint.h. */
15445
15446 cmd_list_element *commands_cmd_element = nullptr;
15447
15448 void _initialize_breakpoint ();
15449 void
15450 _initialize_breakpoint ()
15451 {
15452 struct cmd_list_element *c;
15453
15454 initialize_breakpoint_ops ();
15455
15456 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15457 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15458 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15459
15460 breakpoint_chain = 0;
15461 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15462 before a breakpoint is set. */
15463 breakpoint_count = 0;
15464
15465 tracepoint_count = 0;
15466
15467 add_com ("ignore", class_breakpoint, ignore_command, _("\
15468 Set ignore-count of breakpoint number N to COUNT.\n\
15469 Usage is `ignore N COUNT'."));
15470
15471 commands_cmd_element = add_com ("commands", class_breakpoint,
15472 commands_command, _("\
15473 Set commands to be executed when the given breakpoints are hit.\n\
15474 Give a space-separated breakpoint list as argument after \"commands\".\n\
15475 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15476 (e.g. `5-7').\n\
15477 With no argument, the targeted breakpoint is the last one set.\n\
15478 The commands themselves follow starting on the next line.\n\
15479 Type a line containing \"end\" to indicate the end of them.\n\
15480 Give \"silent\" as the first line to make the breakpoint silent;\n\
15481 then no output is printed when it is hit, except what the commands print."));
15482
15483 c = add_com ("condition", class_breakpoint, condition_command, _("\
15484 Specify breakpoint number N to break only if COND is true.\n\
15485 Usage is `condition N COND', where N is an integer and COND is an\n\
15486 expression to be evaluated whenever breakpoint N is reached."));
15487 set_cmd_completer (c, condition_completer);
15488
15489 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15490 Set a temporary breakpoint.\n\
15491 Like \"break\" except the breakpoint is only temporary,\n\
15492 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15493 by using \"enable delete\" on the breakpoint number.\n\
15494 \n"
15495 BREAK_ARGS_HELP ("tbreak")));
15496 set_cmd_completer (c, location_completer);
15497
15498 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15499 Set a hardware assisted breakpoint.\n\
15500 Like \"break\" except the breakpoint requires hardware support,\n\
15501 some target hardware may not have this support.\n\
15502 \n"
15503 BREAK_ARGS_HELP ("hbreak")));
15504 set_cmd_completer (c, location_completer);
15505
15506 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15507 Set a temporary hardware assisted breakpoint.\n\
15508 Like \"hbreak\" except the breakpoint is only temporary,\n\
15509 so it will be deleted when hit.\n\
15510 \n"
15511 BREAK_ARGS_HELP ("thbreak")));
15512 set_cmd_completer (c, location_completer);
15513
15514 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15515 Enable all or some breakpoints.\n\
15516 Usage: enable [BREAKPOINTNUM]...\n\
15517 Give breakpoint numbers (separated by spaces) as arguments.\n\
15518 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15519 This is used to cancel the effect of the \"disable\" command.\n\
15520 With a subcommand you can enable temporarily."),
15521 &enablelist, "enable ", 1, &cmdlist);
15522
15523 add_com_alias ("en", "enable", class_breakpoint, 1);
15524
15525 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15526 Enable all or some breakpoints.\n\
15527 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15528 Give breakpoint numbers (separated by spaces) as arguments.\n\
15529 This is used to cancel the effect of the \"disable\" command.\n\
15530 May be abbreviated to simply \"enable\"."),
15531 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15532
15533 add_cmd ("once", no_class, enable_once_command, _("\
15534 Enable some breakpoints for one hit.\n\
15535 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15536 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15537 &enablebreaklist);
15538
15539 add_cmd ("delete", no_class, enable_delete_command, _("\
15540 Enable some breakpoints and delete when hit.\n\
15541 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15542 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15543 &enablebreaklist);
15544
15545 add_cmd ("count", no_class, enable_count_command, _("\
15546 Enable some breakpoints for COUNT hits.\n\
15547 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15548 If a breakpoint is hit while enabled in this fashion,\n\
15549 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15550 &enablebreaklist);
15551
15552 add_cmd ("delete", no_class, enable_delete_command, _("\
15553 Enable some breakpoints and delete when hit.\n\
15554 Usage: enable delete BREAKPOINTNUM...\n\
15555 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15556 &enablelist);
15557
15558 add_cmd ("once", no_class, enable_once_command, _("\
15559 Enable some breakpoints for one hit.\n\
15560 Usage: enable once BREAKPOINTNUM...\n\
15561 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15562 &enablelist);
15563
15564 add_cmd ("count", no_class, enable_count_command, _("\
15565 Enable some breakpoints for COUNT hits.\n\
15566 Usage: enable count COUNT BREAKPOINTNUM...\n\
15567 If a breakpoint is hit while enabled in this fashion,\n\
15568 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15569 &enablelist);
15570
15571 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15572 Disable all or some breakpoints.\n\
15573 Usage: disable [BREAKPOINTNUM]...\n\
15574 Arguments are breakpoint numbers with spaces in between.\n\
15575 To disable all breakpoints, give no argument.\n\
15576 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15577 &disablelist, "disable ", 1, &cmdlist);
15578 add_com_alias ("dis", "disable", class_breakpoint, 1);
15579 add_com_alias ("disa", "disable", class_breakpoint, 1);
15580
15581 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15582 Disable all or some breakpoints.\n\
15583 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15584 Arguments are breakpoint numbers with spaces in between.\n\
15585 To disable all breakpoints, give no argument.\n\
15586 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15587 This command may be abbreviated \"disable\"."),
15588 &disablelist);
15589
15590 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15591 Delete all or some breakpoints.\n\
15592 Usage: delete [BREAKPOINTNUM]...\n\
15593 Arguments are breakpoint numbers with spaces in between.\n\
15594 To delete all breakpoints, give no argument.\n\
15595 \n\
15596 Also a prefix command for deletion of other GDB objects."),
15597 &deletelist, "delete ", 1, &cmdlist);
15598 add_com_alias ("d", "delete", class_breakpoint, 1);
15599 add_com_alias ("del", "delete", class_breakpoint, 1);
15600
15601 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15602 Delete all or some breakpoints or auto-display expressions.\n\
15603 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15604 Arguments are breakpoint numbers with spaces in between.\n\
15605 To delete all breakpoints, give no argument.\n\
15606 This command may be abbreviated \"delete\"."),
15607 &deletelist);
15608
15609 add_com ("clear", class_breakpoint, clear_command, _("\
15610 Clear breakpoint at specified location.\n\
15611 Argument may be a linespec, explicit, or address location as described below.\n\
15612 \n\
15613 With no argument, clears all breakpoints in the line that the selected frame\n\
15614 is executing in.\n"
15615 "\n" LOCATION_HELP_STRING "\n\n\
15616 See also the \"delete\" command which clears breakpoints by number."));
15617 add_com_alias ("cl", "clear", class_breakpoint, 1);
15618
15619 c = add_com ("break", class_breakpoint, break_command, _("\
15620 Set breakpoint at specified location.\n"
15621 BREAK_ARGS_HELP ("break")));
15622 set_cmd_completer (c, location_completer);
15623
15624 add_com_alias ("b", "break", class_run, 1);
15625 add_com_alias ("br", "break", class_run, 1);
15626 add_com_alias ("bre", "break", class_run, 1);
15627 add_com_alias ("brea", "break", class_run, 1);
15628
15629 if (dbx_commands)
15630 {
15631 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15632 Break in function/address or break at a line in the current file."),
15633 &stoplist, "stop ", 1, &cmdlist);
15634 add_cmd ("in", class_breakpoint, stopin_command,
15635 _("Break in function or address."), &stoplist);
15636 add_cmd ("at", class_breakpoint, stopat_command,
15637 _("Break at a line in the current file."), &stoplist);
15638 add_com ("status", class_info, info_breakpoints_command, _("\
15639 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15640 The \"Type\" column indicates one of:\n\
15641 \tbreakpoint - normal breakpoint\n\
15642 \twatchpoint - watchpoint\n\
15643 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15644 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15645 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15646 address and file/line number respectively.\n\
15647 \n\
15648 Convenience variable \"$_\" and default examine address for \"x\"\n\
15649 are set to the address of the last breakpoint listed unless the command\n\
15650 is prefixed with \"server \".\n\n\
15651 Convenience variable \"$bpnum\" contains the number of the last\n\
15652 breakpoint set."));
15653 }
15654
15655 add_info ("breakpoints", info_breakpoints_command, _("\
15656 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15657 The \"Type\" column indicates one of:\n\
15658 \tbreakpoint - normal breakpoint\n\
15659 \twatchpoint - watchpoint\n\
15660 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15661 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15662 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15663 address and file/line number respectively.\n\
15664 \n\
15665 Convenience variable \"$_\" and default examine address for \"x\"\n\
15666 are set to the address of the last breakpoint listed unless the command\n\
15667 is prefixed with \"server \".\n\n\
15668 Convenience variable \"$bpnum\" contains the number of the last\n\
15669 breakpoint set."));
15670
15671 add_info_alias ("b", "breakpoints", 1);
15672
15673 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15674 Status of all breakpoints, or breakpoint number NUMBER.\n\
15675 The \"Type\" column indicates one of:\n\
15676 \tbreakpoint - normal breakpoint\n\
15677 \twatchpoint - watchpoint\n\
15678 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15679 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15680 \tuntil - internal breakpoint used by the \"until\" command\n\
15681 \tfinish - internal breakpoint used by the \"finish\" command\n\
15682 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15683 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15684 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15685 address and file/line number respectively.\n\
15686 \n\
15687 Convenience variable \"$_\" and default examine address for \"x\"\n\
15688 are set to the address of the last breakpoint listed unless the command\n\
15689 is prefixed with \"server \".\n\n\
15690 Convenience variable \"$bpnum\" contains the number of the last\n\
15691 breakpoint set."),
15692 &maintenanceinfolist);
15693
15694 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15695 Set catchpoints to catch events."),
15696 &catch_cmdlist, "catch ",
15697 0/*allow-unknown*/, &cmdlist);
15698
15699 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15700 Set temporary catchpoints to catch events."),
15701 &tcatch_cmdlist, "tcatch ",
15702 0/*allow-unknown*/, &cmdlist);
15703
15704 add_catch_command ("fork", _("Catch calls to fork."),
15705 catch_fork_command_1,
15706 NULL,
15707 (void *) (uintptr_t) catch_fork_permanent,
15708 (void *) (uintptr_t) catch_fork_temporary);
15709 add_catch_command ("vfork", _("Catch calls to vfork."),
15710 catch_fork_command_1,
15711 NULL,
15712 (void *) (uintptr_t) catch_vfork_permanent,
15713 (void *) (uintptr_t) catch_vfork_temporary);
15714 add_catch_command ("exec", _("Catch calls to exec."),
15715 catch_exec_command_1,
15716 NULL,
15717 CATCH_PERMANENT,
15718 CATCH_TEMPORARY);
15719 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15720 Usage: catch load [REGEX]\n\
15721 If REGEX is given, only stop for libraries matching the regular expression."),
15722 catch_load_command_1,
15723 NULL,
15724 CATCH_PERMANENT,
15725 CATCH_TEMPORARY);
15726 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15727 Usage: catch unload [REGEX]\n\
15728 If REGEX is given, only stop for libraries matching the regular expression."),
15729 catch_unload_command_1,
15730 NULL,
15731 CATCH_PERMANENT,
15732 CATCH_TEMPORARY);
15733
15734 c = add_com ("watch", class_breakpoint, watch_command, _("\
15735 Set a watchpoint for an expression.\n\
15736 Usage: watch [-l|-location] EXPRESSION\n\
15737 A watchpoint stops execution of your program whenever the value of\n\
15738 an expression changes.\n\
15739 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15740 the memory to which it refers."));
15741 set_cmd_completer (c, expression_completer);
15742
15743 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15744 Set a read watchpoint for an expression.\n\
15745 Usage: rwatch [-l|-location] EXPRESSION\n\
15746 A watchpoint stops execution of your program whenever the value of\n\
15747 an expression is read.\n\
15748 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15749 the memory to which it refers."));
15750 set_cmd_completer (c, expression_completer);
15751
15752 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15753 Set a watchpoint for an expression.\n\
15754 Usage: awatch [-l|-location] EXPRESSION\n\
15755 A watchpoint stops execution of your program whenever the value of\n\
15756 an expression is either read or written.\n\
15757 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15758 the memory to which it refers."));
15759 set_cmd_completer (c, expression_completer);
15760
15761 add_info ("watchpoints", info_watchpoints_command, _("\
15762 Status of specified watchpoints (all watchpoints if no argument)."));
15763
15764 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15765 respond to changes - contrary to the description. */
15766 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15767 &can_use_hw_watchpoints, _("\
15768 Set debugger's willingness to use watchpoint hardware."), _("\
15769 Show debugger's willingness to use watchpoint hardware."), _("\
15770 If zero, gdb will not use hardware for new watchpoints, even if\n\
15771 such is available. (However, any hardware watchpoints that were\n\
15772 created before setting this to nonzero, will continue to use watchpoint\n\
15773 hardware.)"),
15774 NULL,
15775 show_can_use_hw_watchpoints,
15776 &setlist, &showlist);
15777
15778 can_use_hw_watchpoints = 1;
15779
15780 /* Tracepoint manipulation commands. */
15781
15782 c = add_com ("trace", class_breakpoint, trace_command, _("\
15783 Set a tracepoint at specified location.\n\
15784 \n"
15785 BREAK_ARGS_HELP ("trace") "\n\
15786 Do \"help tracepoints\" for info on other tracepoint commands."));
15787 set_cmd_completer (c, location_completer);
15788
15789 add_com_alias ("tp", "trace", class_breakpoint, 0);
15790 add_com_alias ("tr", "trace", class_breakpoint, 1);
15791 add_com_alias ("tra", "trace", class_breakpoint, 1);
15792 add_com_alias ("trac", "trace", class_breakpoint, 1);
15793
15794 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15795 Set a fast tracepoint at specified location.\n\
15796 \n"
15797 BREAK_ARGS_HELP ("ftrace") "\n\
15798 Do \"help tracepoints\" for info on other tracepoint commands."));
15799 set_cmd_completer (c, location_completer);
15800
15801 c = add_com ("strace", class_breakpoint, strace_command, _("\
15802 Set a static tracepoint at location or marker.\n\
15803 \n\
15804 strace [LOCATION] [if CONDITION]\n\
15805 LOCATION may be a linespec, explicit, or address location (described below) \n\
15806 or -m MARKER_ID.\n\n\
15807 If a marker id is specified, probe the marker with that name. With\n\
15808 no LOCATION, uses current execution address of the selected stack frame.\n\
15809 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15810 This collects arbitrary user data passed in the probe point call to the\n\
15811 tracing library. You can inspect it when analyzing the trace buffer,\n\
15812 by printing the $_sdata variable like any other convenience variable.\n\
15813 \n\
15814 CONDITION is a boolean expression.\n\
15815 \n" LOCATION_HELP_STRING "\n\n\
15816 Multiple tracepoints at one place are permitted, and useful if their\n\
15817 conditions are different.\n\
15818 \n\
15819 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15820 Do \"help tracepoints\" for info on other tracepoint commands."));
15821 set_cmd_completer (c, location_completer);
15822
15823 add_info ("tracepoints", info_tracepoints_command, _("\
15824 Status of specified tracepoints (all tracepoints if no argument).\n\
15825 Convenience variable \"$tpnum\" contains the number of the\n\
15826 last tracepoint set."));
15827
15828 add_info_alias ("tp", "tracepoints", 1);
15829
15830 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15831 Delete specified tracepoints.\n\
15832 Arguments are tracepoint numbers, separated by spaces.\n\
15833 No argument means delete all tracepoints."),
15834 &deletelist);
15835 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15836
15837 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15838 Disable specified tracepoints.\n\
15839 Arguments are tracepoint numbers, separated by spaces.\n\
15840 No argument means disable all tracepoints."),
15841 &disablelist);
15842 deprecate_cmd (c, "disable");
15843
15844 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15845 Enable specified tracepoints.\n\
15846 Arguments are tracepoint numbers, separated by spaces.\n\
15847 No argument means enable all tracepoints."),
15848 &enablelist);
15849 deprecate_cmd (c, "enable");
15850
15851 add_com ("passcount", class_trace, trace_pass_command, _("\
15852 Set the passcount for a tracepoint.\n\
15853 The trace will end when the tracepoint has been passed 'count' times.\n\
15854 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15855 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15856
15857 add_basic_prefix_cmd ("save", class_breakpoint,
15858 _("Save breakpoint definitions as a script."),
15859 &save_cmdlist, "save ",
15860 0/*allow-unknown*/, &cmdlist);
15861
15862 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15863 Save current breakpoint definitions as a script.\n\
15864 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15865 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15866 session to restore them."),
15867 &save_cmdlist);
15868 set_cmd_completer (c, filename_completer);
15869
15870 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15871 Save current tracepoint definitions as a script.\n\
15872 Use the 'source' command in another debug session to restore them."),
15873 &save_cmdlist);
15874 set_cmd_completer (c, filename_completer);
15875
15876 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15877 deprecate_cmd (c, "save tracepoints");
15878
15879 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
15880 Breakpoint specific settings.\n\
15881 Configure various breakpoint-specific variables such as\n\
15882 pending breakpoint behavior."),
15883 &breakpoint_set_cmdlist, "set breakpoint ",
15884 0/*allow-unknown*/, &setlist);
15885 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
15886 Breakpoint specific settings.\n\
15887 Configure various breakpoint-specific variables such as\n\
15888 pending breakpoint behavior."),
15889 &breakpoint_show_cmdlist, "show breakpoint ",
15890 0/*allow-unknown*/, &showlist);
15891
15892 add_setshow_auto_boolean_cmd ("pending", no_class,
15893 &pending_break_support, _("\
15894 Set debugger's behavior regarding pending breakpoints."), _("\
15895 Show debugger's behavior regarding pending breakpoints."), _("\
15896 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15897 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15898 an error. If auto, an unrecognized breakpoint location results in a\n\
15899 user-query to see if a pending breakpoint should be created."),
15900 NULL,
15901 show_pending_break_support,
15902 &breakpoint_set_cmdlist,
15903 &breakpoint_show_cmdlist);
15904
15905 pending_break_support = AUTO_BOOLEAN_AUTO;
15906
15907 add_setshow_boolean_cmd ("auto-hw", no_class,
15908 &automatic_hardware_breakpoints, _("\
15909 Set automatic usage of hardware breakpoints."), _("\
15910 Show automatic usage of hardware breakpoints."), _("\
15911 If set, the debugger will automatically use hardware breakpoints for\n\
15912 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15913 a warning will be emitted for such breakpoints."),
15914 NULL,
15915 show_automatic_hardware_breakpoints,
15916 &breakpoint_set_cmdlist,
15917 &breakpoint_show_cmdlist);
15918
15919 add_setshow_boolean_cmd ("always-inserted", class_support,
15920 &always_inserted_mode, _("\
15921 Set mode for inserting breakpoints."), _("\
15922 Show mode for inserting breakpoints."), _("\
15923 When this mode is on, breakpoints are inserted immediately as soon as\n\
15924 they're created, kept inserted even when execution stops, and removed\n\
15925 only when the user deletes them. When this mode is off (the default),\n\
15926 breakpoints are inserted only when execution continues, and removed\n\
15927 when execution stops."),
15928 NULL,
15929 &show_always_inserted_mode,
15930 &breakpoint_set_cmdlist,
15931 &breakpoint_show_cmdlist);
15932
15933 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15934 condition_evaluation_enums,
15935 &condition_evaluation_mode_1, _("\
15936 Set mode of breakpoint condition evaluation."), _("\
15937 Show mode of breakpoint condition evaluation."), _("\
15938 When this is set to \"host\", breakpoint conditions will be\n\
15939 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15940 breakpoint conditions will be downloaded to the target (if the target\n\
15941 supports such feature) and conditions will be evaluated on the target's side.\n\
15942 If this is set to \"auto\" (default), this will be automatically set to\n\
15943 \"target\" if it supports condition evaluation, otherwise it will\n\
15944 be set to \"gdb\""),
15945 &set_condition_evaluation_mode,
15946 &show_condition_evaluation_mode,
15947 &breakpoint_set_cmdlist,
15948 &breakpoint_show_cmdlist);
15949
15950 add_com ("break-range", class_breakpoint, break_range_command, _("\
15951 Set a breakpoint for an address range.\n\
15952 break-range START-LOCATION, END-LOCATION\n\
15953 where START-LOCATION and END-LOCATION can be one of the following:\n\
15954 LINENUM, for that line in the current file,\n\
15955 FILE:LINENUM, for that line in that file,\n\
15956 +OFFSET, for that number of lines after the current line\n\
15957 or the start of the range\n\
15958 FUNCTION, for the first line in that function,\n\
15959 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15960 *ADDRESS, for the instruction at that address.\n\
15961 \n\
15962 The breakpoint will stop execution of the inferior whenever it executes\n\
15963 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15964 range (including START-LOCATION and END-LOCATION)."));
15965
15966 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15967 Set a dynamic printf at specified location.\n\
15968 dprintf location,format string,arg1,arg2,...\n\
15969 location may be a linespec, explicit, or address location.\n"
15970 "\n" LOCATION_HELP_STRING));
15971 set_cmd_completer (c, location_completer);
15972
15973 add_setshow_enum_cmd ("dprintf-style", class_support,
15974 dprintf_style_enums, &dprintf_style, _("\
15975 Set the style of usage for dynamic printf."), _("\
15976 Show the style of usage for dynamic printf."), _("\
15977 This setting chooses how GDB will do a dynamic printf.\n\
15978 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15979 console, as with the \"printf\" command.\n\
15980 If the value is \"call\", the print is done by calling a function in your\n\
15981 program; by default printf(), but you can choose a different function or\n\
15982 output stream by setting dprintf-function and dprintf-channel."),
15983 update_dprintf_commands, NULL,
15984 &setlist, &showlist);
15985
15986 dprintf_function = xstrdup ("printf");
15987 add_setshow_string_cmd ("dprintf-function", class_support,
15988 &dprintf_function, _("\
15989 Set the function to use for dynamic printf."), _("\
15990 Show the function to use for dynamic printf."), NULL,
15991 update_dprintf_commands, NULL,
15992 &setlist, &showlist);
15993
15994 dprintf_channel = xstrdup ("");
15995 add_setshow_string_cmd ("dprintf-channel", class_support,
15996 &dprintf_channel, _("\
15997 Set the channel to use for dynamic printf."), _("\
15998 Show the channel to use for dynamic printf."), NULL,
15999 update_dprintf_commands, NULL,
16000 &setlist, &showlist);
16001
16002 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16003 &disconnected_dprintf, _("\
16004 Set whether dprintf continues after GDB disconnects."), _("\
16005 Show whether dprintf continues after GDB disconnects."), _("\
16006 Use this to let dprintf commands continue to hit and produce output\n\
16007 even if GDB disconnects or detaches from the target."),
16008 NULL,
16009 NULL,
16010 &setlist, &showlist);
16011
16012 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16013 Target agent only formatted printing, like the C \"printf\" function.\n\
16014 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16015 This supports most C printf format specifications, like %s, %d, etc.\n\
16016 This is useful for formatted output in user-defined commands."));
16017
16018 automatic_hardware_breakpoints = true;
16019
16020 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
16021 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
16022 }
This page took 0.354564 seconds and 5 git commands to generate.