Remove prepare_re_set_context
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2017 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 "observer.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "continuations.h"
63 #include "stack.h"
64 #include "skip.h"
65 #include "ax-gdb.h"
66 #include "dummy-frame.h"
67 #include "interps.h"
68 #include "format.h"
69 #include "thread-fsm.h"
70 #include "tid-parse.h"
71
72 /* readline include files */
73 #include "readline/readline.h"
74 #include "readline/history.h"
75
76 /* readline defines this. */
77 #undef savestring
78
79 #include "mi/mi-common.h"
80 #include "extension.h"
81 #include <algorithm>
82 #include "progspace-and-thread.h"
83 #include "common/array-view.h"
84 #include "common/gdb_optional.h"
85
86 /* Enums for exception-handling support. */
87 enum exception_event_kind
88 {
89 EX_EVENT_THROW,
90 EX_EVENT_RETHROW,
91 EX_EVENT_CATCH
92 };
93
94 /* Prototypes for local functions. */
95
96 static void disable_command (char *, int);
97
98 static void enable_command (char *, int);
99
100 static void map_breakpoint_numbers (const char *,
101 gdb::function_view<void (breakpoint *)>);
102
103 static void ignore_command (char *, int);
104
105 static void breakpoint_re_set_default (struct breakpoint *);
106
107 static void
108 create_sals_from_location_default (const struct event_location *location,
109 struct linespec_result *canonical,
110 enum bptype type_wanted);
111
112 static void create_breakpoints_sal_default (struct gdbarch *,
113 struct linespec_result *,
114 gdb::unique_xmalloc_ptr<char>,
115 gdb::unique_xmalloc_ptr<char>,
116 enum bptype,
117 enum bpdisp, int, int,
118 int,
119 const struct breakpoint_ops *,
120 int, int, int, unsigned);
121
122 static std::vector<symtab_and_line> decode_location_default
123 (struct breakpoint *b, const struct event_location *location,
124 struct program_space *search_pspace);
125
126 static void clear_command (char *, int);
127
128 static void catch_command (char *, int);
129
130 static int can_use_hardware_watchpoint (struct value *);
131
132 static void mention (struct breakpoint *);
133
134 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
135 enum bptype,
136 const struct breakpoint_ops *);
137 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
138 const struct symtab_and_line *);
139
140 /* This function is used in gdbtk sources and thus can not be made
141 static. */
142 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
143 struct symtab_and_line,
144 enum bptype,
145 const struct breakpoint_ops *);
146
147 static struct breakpoint *
148 momentary_breakpoint_from_master (struct breakpoint *orig,
149 enum bptype type,
150 const struct breakpoint_ops *ops,
151 int loc_enabled);
152
153 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
154
155 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
156 CORE_ADDR bpaddr,
157 enum bptype bptype);
158
159 static void describe_other_breakpoints (struct gdbarch *,
160 struct program_space *, CORE_ADDR,
161 struct obj_section *, int);
162
163 static int watchpoint_locations_match (struct bp_location *loc1,
164 struct bp_location *loc2);
165
166 static int breakpoint_location_address_match (struct bp_location *bl,
167 struct address_space *aspace,
168 CORE_ADDR addr);
169
170 static int breakpoint_location_address_range_overlap (struct bp_location *,
171 struct address_space *,
172 CORE_ADDR, int);
173
174 static void info_breakpoints_command (char *, int);
175
176 static void info_watchpoints_command (char *, int);
177
178 static void commands_command (char *, int);
179
180 static void condition_command (char *, int);
181
182 static int remove_breakpoint (struct bp_location *);
183 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
184
185 static enum print_stop_action print_bp_stop_message (bpstat bs);
186
187 static int hw_breakpoint_used_count (void);
188
189 static int hw_watchpoint_use_count (struct breakpoint *);
190
191 static int hw_watchpoint_used_count_others (struct breakpoint *except,
192 enum bptype type,
193 int *other_type_used);
194
195 static void hbreak_command (char *, int);
196
197 static void thbreak_command (char *, int);
198
199 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
200 int count);
201
202 static void stop_command (char *arg, int from_tty);
203
204 static void tcatch_command (char *arg, int from_tty);
205
206 static void free_bp_location (struct bp_location *loc);
207 static void incref_bp_location (struct bp_location *loc);
208 static void decref_bp_location (struct bp_location **loc);
209
210 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
211
212 /* update_global_location_list's modes of operation wrt to whether to
213 insert locations now. */
214 enum ugll_insert_mode
215 {
216 /* Don't insert any breakpoint locations into the inferior, only
217 remove already-inserted locations that no longer should be
218 inserted. Functions that delete a breakpoint or breakpoints
219 should specify this mode, so that deleting a breakpoint doesn't
220 have the side effect of inserting the locations of other
221 breakpoints that are marked not-inserted, but should_be_inserted
222 returns true on them.
223
224 This behavior is useful is situations close to tear-down -- e.g.,
225 after an exec, while the target still has execution, but
226 breakpoint shadows of the previous executable image should *NOT*
227 be restored to the new image; or before detaching, where the
228 target still has execution and wants to delete breakpoints from
229 GDB's lists, and all breakpoints had already been removed from
230 the inferior. */
231 UGLL_DONT_INSERT,
232
233 /* May insert breakpoints iff breakpoints_should_be_inserted_now
234 claims breakpoints should be inserted now. */
235 UGLL_MAY_INSERT,
236
237 /* Insert locations now, irrespective of
238 breakpoints_should_be_inserted_now. E.g., say all threads are
239 stopped right now, and the user did "continue". We need to
240 insert breakpoints _before_ resuming the target, but
241 UGLL_MAY_INSERT wouldn't insert them, because
242 breakpoints_should_be_inserted_now returns false at that point,
243 as no thread is running yet. */
244 UGLL_INSERT
245 };
246
247 static void update_global_location_list (enum ugll_insert_mode);
248
249 static void update_global_location_list_nothrow (enum ugll_insert_mode);
250
251 static int is_hardware_watchpoint (const struct breakpoint *bpt);
252
253 static void insert_breakpoint_locations (void);
254
255 static void info_tracepoints_command (char *, int);
256
257 static void enable_trace_command (char *, int);
258
259 static void disable_trace_command (char *, int);
260
261 static void trace_pass_command (char *, int);
262
263 static void set_tracepoint_count (int num);
264
265 static int is_masked_watchpoint (const struct breakpoint *b);
266
267 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
268
269 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
270 otherwise. */
271
272 static int strace_marker_p (struct breakpoint *b);
273
274 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
275 that are implemented on top of software or hardware breakpoints
276 (user breakpoints, internal and momentary breakpoints, etc.). */
277 static struct breakpoint_ops bkpt_base_breakpoint_ops;
278
279 /* Internal breakpoints class type. */
280 static struct breakpoint_ops internal_breakpoint_ops;
281
282 /* Momentary breakpoints class type. */
283 static struct breakpoint_ops momentary_breakpoint_ops;
284
285 /* The breakpoint_ops structure to be used in regular user created
286 breakpoints. */
287 struct breakpoint_ops bkpt_breakpoint_ops;
288
289 /* Breakpoints set on probes. */
290 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
291
292 /* Dynamic printf class type. */
293 struct breakpoint_ops dprintf_breakpoint_ops;
294
295 /* The style in which to perform a dynamic printf. This is a user
296 option because different output options have different tradeoffs;
297 if GDB does the printing, there is better error handling if there
298 is a problem with any of the arguments, but using an inferior
299 function lets you have special-purpose printers and sending of
300 output to the same place as compiled-in print functions. */
301
302 static const char dprintf_style_gdb[] = "gdb";
303 static const char dprintf_style_call[] = "call";
304 static const char dprintf_style_agent[] = "agent";
305 static const char *const dprintf_style_enums[] = {
306 dprintf_style_gdb,
307 dprintf_style_call,
308 dprintf_style_agent,
309 NULL
310 };
311 static const char *dprintf_style = dprintf_style_gdb;
312
313 /* The function to use for dynamic printf if the preferred style is to
314 call into the inferior. The value is simply a string that is
315 copied into the command, so it can be anything that GDB can
316 evaluate to a callable address, not necessarily a function name. */
317
318 static char *dprintf_function;
319
320 /* The channel to use for dynamic printf if the preferred style is to
321 call into the inferior; if a nonempty string, it will be passed to
322 the call as the first argument, with the format string as the
323 second. As with the dprintf function, this can be anything that
324 GDB knows how to evaluate, so in addition to common choices like
325 "stderr", this could be an app-specific expression like
326 "mystreams[curlogger]". */
327
328 static char *dprintf_channel;
329
330 /* True if dprintf commands should continue to operate even if GDB
331 has disconnected. */
332 static int disconnected_dprintf = 1;
333
334 struct command_line *
335 breakpoint_commands (struct breakpoint *b)
336 {
337 return b->commands ? b->commands.get () : NULL;
338 }
339
340 /* Flag indicating that a command has proceeded the inferior past the
341 current breakpoint. */
342
343 static int breakpoint_proceeded;
344
345 const char *
346 bpdisp_text (enum bpdisp disp)
347 {
348 /* NOTE: the following values are a part of MI protocol and
349 represent values of 'disp' field returned when inferior stops at
350 a breakpoint. */
351 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
352
353 return bpdisps[(int) disp];
354 }
355
356 /* Prototypes for exported functions. */
357 /* If FALSE, gdb will not use hardware support for watchpoints, even
358 if such is available. */
359 static int can_use_hw_watchpoints;
360
361 static void
362 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
363 struct cmd_list_element *c,
364 const char *value)
365 {
366 fprintf_filtered (file,
367 _("Debugger's willingness to use "
368 "watchpoint hardware is %s.\n"),
369 value);
370 }
371
372 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
373 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
374 for unrecognized breakpoint locations.
375 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
376 static enum auto_boolean pending_break_support;
377 static void
378 show_pending_break_support (struct ui_file *file, int from_tty,
379 struct cmd_list_element *c,
380 const char *value)
381 {
382 fprintf_filtered (file,
383 _("Debugger's behavior regarding "
384 "pending breakpoints is %s.\n"),
385 value);
386 }
387
388 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
389 set with "break" but falling in read-only memory.
390 If 0, gdb will warn about such breakpoints, but won't automatically
391 use hardware breakpoints. */
392 static int automatic_hardware_breakpoints;
393 static void
394 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
395 struct cmd_list_element *c,
396 const char *value)
397 {
398 fprintf_filtered (file,
399 _("Automatic usage of hardware breakpoints is %s.\n"),
400 value);
401 }
402
403 /* If on, GDB keeps breakpoints inserted even if the inferior is
404 stopped, and immediately inserts any new breakpoints as soon as
405 they're created. If off (default), GDB keeps breakpoints off of
406 the target as long as possible. That is, it delays inserting
407 breakpoints until the next resume, and removes them again when the
408 target fully stops. This is a bit safer in case GDB crashes while
409 processing user input. */
410 static int always_inserted_mode = 0;
411
412 static void
413 show_always_inserted_mode (struct ui_file *file, int from_tty,
414 struct cmd_list_element *c, const char *value)
415 {
416 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
417 value);
418 }
419
420 /* See breakpoint.h. */
421
422 int
423 breakpoints_should_be_inserted_now (void)
424 {
425 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
426 {
427 /* If breakpoints are global, they should be inserted even if no
428 thread under gdb's control is running, or even if there are
429 no threads under GDB's control yet. */
430 return 1;
431 }
432 else if (target_has_execution)
433 {
434 struct thread_info *tp;
435
436 if (always_inserted_mode)
437 {
438 /* The user wants breakpoints inserted even if all threads
439 are stopped. */
440 return 1;
441 }
442
443 if (threads_are_executing ())
444 return 1;
445
446 /* Don't remove breakpoints yet if, even though all threads are
447 stopped, we still have events to process. */
448 ALL_NON_EXITED_THREADS (tp)
449 if (tp->resumed
450 && tp->suspend.waitstatus_pending_p)
451 return 1;
452 }
453 return 0;
454 }
455
456 static const char condition_evaluation_both[] = "host or target";
457
458 /* Modes for breakpoint condition evaluation. */
459 static const char condition_evaluation_auto[] = "auto";
460 static const char condition_evaluation_host[] = "host";
461 static const char condition_evaluation_target[] = "target";
462 static const char *const condition_evaluation_enums[] = {
463 condition_evaluation_auto,
464 condition_evaluation_host,
465 condition_evaluation_target,
466 NULL
467 };
468
469 /* Global that holds the current mode for breakpoint condition evaluation. */
470 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
471
472 /* Global that we use to display information to the user (gets its value from
473 condition_evaluation_mode_1. */
474 static const char *condition_evaluation_mode = condition_evaluation_auto;
475
476 /* Translate a condition evaluation mode MODE into either "host"
477 or "target". This is used mostly to translate from "auto" to the
478 real setting that is being used. It returns the translated
479 evaluation mode. */
480
481 static const char *
482 translate_condition_evaluation_mode (const char *mode)
483 {
484 if (mode == condition_evaluation_auto)
485 {
486 if (target_supports_evaluation_of_breakpoint_conditions ())
487 return condition_evaluation_target;
488 else
489 return condition_evaluation_host;
490 }
491 else
492 return mode;
493 }
494
495 /* Discovers what condition_evaluation_auto translates to. */
496
497 static const char *
498 breakpoint_condition_evaluation_mode (void)
499 {
500 return translate_condition_evaluation_mode (condition_evaluation_mode);
501 }
502
503 /* Return true if GDB should evaluate breakpoint conditions or false
504 otherwise. */
505
506 static int
507 gdb_evaluates_breakpoint_condition_p (void)
508 {
509 const char *mode = breakpoint_condition_evaluation_mode ();
510
511 return (mode == condition_evaluation_host);
512 }
513
514 /* Are we executing breakpoint commands? */
515 static int executing_breakpoint_commands;
516
517 /* Are overlay event breakpoints enabled? */
518 static int overlay_events_enabled;
519
520 /* See description in breakpoint.h. */
521 int target_exact_watchpoints = 0;
522
523 /* Walk the following statement or block through all breakpoints.
524 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
525 current breakpoint. */
526
527 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
528
529 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
530 for (B = breakpoint_chain; \
531 B ? (TMP=B->next, 1): 0; \
532 B = TMP)
533
534 /* Similar iterator for the low-level breakpoints. SAFE variant is
535 not provided so update_global_location_list must not be called
536 while executing the block of ALL_BP_LOCATIONS. */
537
538 #define ALL_BP_LOCATIONS(B,BP_TMP) \
539 for (BP_TMP = bp_locations; \
540 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
541 BP_TMP++)
542
543 /* Iterates through locations with address ADDRESS for the currently selected
544 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
545 to where the loop should start from.
546 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
547 appropriate location to start with. */
548
549 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
550 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
551 BP_LOCP_TMP = BP_LOCP_START; \
552 BP_LOCP_START \
553 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
554 && (*BP_LOCP_TMP)->address == ADDRESS); \
555 BP_LOCP_TMP++)
556
557 /* Iterator for tracepoints only. */
558
559 #define ALL_TRACEPOINTS(B) \
560 for (B = breakpoint_chain; B; B = B->next) \
561 if (is_tracepoint (B))
562
563 /* Chains of all breakpoints defined. */
564
565 struct breakpoint *breakpoint_chain;
566
567 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
568
569 static struct bp_location **bp_locations;
570
571 /* Number of elements of BP_LOCATIONS. */
572
573 static unsigned bp_locations_count;
574
575 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
576 ADDRESS for the current elements of BP_LOCATIONS which get a valid
577 result from bp_location_has_shadow. You can use it for roughly
578 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
579 an address you need to read. */
580
581 static CORE_ADDR bp_locations_placed_address_before_address_max;
582
583 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
584 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
585 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
586 You can use it for roughly limiting the subrange of BP_LOCATIONS to
587 scan for shadow bytes for an address you need to read. */
588
589 static CORE_ADDR bp_locations_shadow_len_after_address_max;
590
591 /* The locations that no longer correspond to any breakpoint, unlinked
592 from the bp_locations array, but for which a hit may still be
593 reported by a target. */
594 VEC(bp_location_p) *moribund_locations = NULL;
595
596 /* Number of last breakpoint made. */
597
598 static int breakpoint_count;
599
600 /* The value of `breakpoint_count' before the last command that
601 created breakpoints. If the last (break-like) command created more
602 than one breakpoint, then the difference between BREAKPOINT_COUNT
603 and PREV_BREAKPOINT_COUNT is more than one. */
604 static int prev_breakpoint_count;
605
606 /* Number of last tracepoint made. */
607
608 static int tracepoint_count;
609
610 static struct cmd_list_element *breakpoint_set_cmdlist;
611 static struct cmd_list_element *breakpoint_show_cmdlist;
612 struct cmd_list_element *save_cmdlist;
613
614 /* See declaration at breakpoint.h. */
615
616 struct breakpoint *
617 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
618 void *user_data)
619 {
620 struct breakpoint *b = NULL;
621
622 ALL_BREAKPOINTS (b)
623 {
624 if (func (b, user_data) != 0)
625 break;
626 }
627
628 return b;
629 }
630
631 /* Return whether a breakpoint is an active enabled breakpoint. */
632 static int
633 breakpoint_enabled (struct breakpoint *b)
634 {
635 return (b->enable_state == bp_enabled);
636 }
637
638 /* Set breakpoint count to NUM. */
639
640 static void
641 set_breakpoint_count (int num)
642 {
643 prev_breakpoint_count = breakpoint_count;
644 breakpoint_count = num;
645 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
646 }
647
648 /* Used by `start_rbreak_breakpoints' below, to record the current
649 breakpoint count before "rbreak" creates any breakpoint. */
650 static int rbreak_start_breakpoint_count;
651
652 /* Called at the start an "rbreak" command to record the first
653 breakpoint made. */
654
655 void
656 start_rbreak_breakpoints (void)
657 {
658 rbreak_start_breakpoint_count = breakpoint_count;
659 }
660
661 /* Called at the end of an "rbreak" command to record the last
662 breakpoint made. */
663
664 void
665 end_rbreak_breakpoints (void)
666 {
667 prev_breakpoint_count = rbreak_start_breakpoint_count;
668 }
669
670 /* Used in run_command to zero the hit count when a new run starts. */
671
672 void
673 clear_breakpoint_hit_counts (void)
674 {
675 struct breakpoint *b;
676
677 ALL_BREAKPOINTS (b)
678 b->hit_count = 0;
679 }
680
681 \f
682 /* Return the breakpoint with the specified number, or NULL
683 if the number does not refer to an existing breakpoint. */
684
685 struct breakpoint *
686 get_breakpoint (int num)
687 {
688 struct breakpoint *b;
689
690 ALL_BREAKPOINTS (b)
691 if (b->number == num)
692 return b;
693
694 return NULL;
695 }
696
697 \f
698
699 /* Mark locations as "conditions have changed" in case the target supports
700 evaluating conditions on its side. */
701
702 static void
703 mark_breakpoint_modified (struct breakpoint *b)
704 {
705 struct bp_location *loc;
706
707 /* This is only meaningful if the target is
708 evaluating conditions and if the user has
709 opted for condition evaluation on the target's
710 side. */
711 if (gdb_evaluates_breakpoint_condition_p ()
712 || !target_supports_evaluation_of_breakpoint_conditions ())
713 return;
714
715 if (!is_breakpoint (b))
716 return;
717
718 for (loc = b->loc; loc; loc = loc->next)
719 loc->condition_changed = condition_modified;
720 }
721
722 /* Mark location as "conditions have changed" in case the target supports
723 evaluating conditions on its side. */
724
725 static void
726 mark_breakpoint_location_modified (struct bp_location *loc)
727 {
728 /* This is only meaningful if the target is
729 evaluating conditions and if the user has
730 opted for condition evaluation on the target's
731 side. */
732 if (gdb_evaluates_breakpoint_condition_p ()
733 || !target_supports_evaluation_of_breakpoint_conditions ())
734
735 return;
736
737 if (!is_breakpoint (loc->owner))
738 return;
739
740 loc->condition_changed = condition_modified;
741 }
742
743 /* Sets the condition-evaluation mode using the static global
744 condition_evaluation_mode. */
745
746 static void
747 set_condition_evaluation_mode (char *args, int from_tty,
748 struct cmd_list_element *c)
749 {
750 const char *old_mode, *new_mode;
751
752 if ((condition_evaluation_mode_1 == condition_evaluation_target)
753 && !target_supports_evaluation_of_breakpoint_conditions ())
754 {
755 condition_evaluation_mode_1 = condition_evaluation_mode;
756 warning (_("Target does not support breakpoint condition evaluation.\n"
757 "Using host evaluation mode instead."));
758 return;
759 }
760
761 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
762 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
763
764 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
765 settings was "auto". */
766 condition_evaluation_mode = condition_evaluation_mode_1;
767
768 /* Only update the mode if the user picked a different one. */
769 if (new_mode != old_mode)
770 {
771 struct bp_location *loc, **loc_tmp;
772 /* If the user switched to a different evaluation mode, we
773 need to synch the changes with the target as follows:
774
775 "host" -> "target": Send all (valid) conditions to the target.
776 "target" -> "host": Remove all the conditions from the target.
777 */
778
779 if (new_mode == condition_evaluation_target)
780 {
781 /* Mark everything modified and synch conditions with the
782 target. */
783 ALL_BP_LOCATIONS (loc, loc_tmp)
784 mark_breakpoint_location_modified (loc);
785 }
786 else
787 {
788 /* Manually mark non-duplicate locations to synch conditions
789 with the target. We do this to remove all the conditions the
790 target knows about. */
791 ALL_BP_LOCATIONS (loc, loc_tmp)
792 if (is_breakpoint (loc->owner) && loc->inserted)
793 loc->needs_update = 1;
794 }
795
796 /* Do the update. */
797 update_global_location_list (UGLL_MAY_INSERT);
798 }
799
800 return;
801 }
802
803 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
804 what "auto" is translating to. */
805
806 static void
807 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
808 struct cmd_list_element *c, const char *value)
809 {
810 if (condition_evaluation_mode == condition_evaluation_auto)
811 fprintf_filtered (file,
812 _("Breakpoint condition evaluation "
813 "mode is %s (currently %s).\n"),
814 value,
815 breakpoint_condition_evaluation_mode ());
816 else
817 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
818 value);
819 }
820
821 /* A comparison function for bp_location AP and BP that is used by
822 bsearch. This comparison function only cares about addresses, unlike
823 the more general bp_locations_compare function. */
824
825 static int
826 bp_locations_compare_addrs (const void *ap, const void *bp)
827 {
828 const struct bp_location *a = *(const struct bp_location **) ap;
829 const struct bp_location *b = *(const struct bp_location **) bp;
830
831 if (a->address == b->address)
832 return 0;
833 else
834 return ((a->address > b->address) - (a->address < b->address));
835 }
836
837 /* Helper function to skip all bp_locations with addresses
838 less than ADDRESS. It returns the first bp_location that
839 is greater than or equal to ADDRESS. If none is found, just
840 return NULL. */
841
842 static struct bp_location **
843 get_first_locp_gte_addr (CORE_ADDR address)
844 {
845 struct bp_location dummy_loc;
846 struct bp_location *dummy_locp = &dummy_loc;
847 struct bp_location **locp_found = NULL;
848
849 /* Initialize the dummy location's address field. */
850 dummy_loc.address = address;
851
852 /* Find a close match to the first location at ADDRESS. */
853 locp_found = ((struct bp_location **)
854 bsearch (&dummy_locp, bp_locations, bp_locations_count,
855 sizeof (struct bp_location **),
856 bp_locations_compare_addrs));
857
858 /* Nothing was found, nothing left to do. */
859 if (locp_found == NULL)
860 return NULL;
861
862 /* We may have found a location that is at ADDRESS but is not the first in the
863 location's list. Go backwards (if possible) and locate the first one. */
864 while ((locp_found - 1) >= bp_locations
865 && (*(locp_found - 1))->address == address)
866 locp_found--;
867
868 return locp_found;
869 }
870
871 void
872 set_breakpoint_condition (struct breakpoint *b, const char *exp,
873 int from_tty)
874 {
875 xfree (b->cond_string);
876 b->cond_string = NULL;
877
878 if (is_watchpoint (b))
879 {
880 struct watchpoint *w = (struct watchpoint *) b;
881
882 w->cond_exp.reset ();
883 }
884 else
885 {
886 struct bp_location *loc;
887
888 for (loc = b->loc; loc; loc = loc->next)
889 {
890 loc->cond.reset ();
891
892 /* No need to free the condition agent expression
893 bytecode (if we have one). We will handle this
894 when we go through update_global_location_list. */
895 }
896 }
897
898 if (*exp == 0)
899 {
900 if (from_tty)
901 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
902 }
903 else
904 {
905 const char *arg = exp;
906
907 /* I don't know if it matters whether this is the string the user
908 typed in or the decompiled expression. */
909 b->cond_string = xstrdup (arg);
910 b->condition_not_parsed = 0;
911
912 if (is_watchpoint (b))
913 {
914 struct watchpoint *w = (struct watchpoint *) b;
915
916 innermost_block = NULL;
917 arg = exp;
918 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
919 if (*arg)
920 error (_("Junk at end of expression"));
921 w->cond_exp_valid_block = innermost_block;
922 }
923 else
924 {
925 struct bp_location *loc;
926
927 for (loc = b->loc; loc; loc = loc->next)
928 {
929 arg = exp;
930 loc->cond =
931 parse_exp_1 (&arg, loc->address,
932 block_for_pc (loc->address), 0);
933 if (*arg)
934 error (_("Junk at end of expression"));
935 }
936 }
937 }
938 mark_breakpoint_modified (b);
939
940 observer_notify_breakpoint_modified (b);
941 }
942
943 /* Completion for the "condition" command. */
944
945 static void
946 condition_completer (struct cmd_list_element *cmd,
947 completion_tracker &tracker,
948 const char *text, const char *word)
949 {
950 const char *space;
951
952 text = skip_spaces (text);
953 space = skip_to_space (text);
954 if (*space == '\0')
955 {
956 int len;
957 struct breakpoint *b;
958 VEC (char_ptr) *result = NULL;
959
960 if (text[0] == '$')
961 {
962 /* We don't support completion of history indices. */
963 if (!isdigit (text[1]))
964 complete_internalvar (tracker, &text[1]);
965 return;
966 }
967
968 /* We're completing the breakpoint number. */
969 len = strlen (text);
970
971 ALL_BREAKPOINTS (b)
972 {
973 char number[50];
974
975 xsnprintf (number, sizeof (number), "%d", b->number);
976
977 if (strncmp (number, text, len) == 0)
978 {
979 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
980 tracker.add_completion (std::move (copy));
981 }
982 }
983
984 return;
985 }
986
987 /* We're completing the expression part. */
988 text = skip_spaces (space);
989 expression_completer (cmd, tracker, text, word);
990 }
991
992 /* condition N EXP -- set break condition of breakpoint N to EXP. */
993
994 static void
995 condition_command (char *arg, int from_tty)
996 {
997 struct breakpoint *b;
998 char *p;
999 int bnum;
1000
1001 if (arg == 0)
1002 error_no_arg (_("breakpoint number"));
1003
1004 p = arg;
1005 bnum = get_number (&p);
1006 if (bnum == 0)
1007 error (_("Bad breakpoint argument: '%s'"), arg);
1008
1009 ALL_BREAKPOINTS (b)
1010 if (b->number == bnum)
1011 {
1012 /* Check if this breakpoint has a "stop" method implemented in an
1013 extension language. This method and conditions entered into GDB
1014 from the CLI are mutually exclusive. */
1015 const struct extension_language_defn *extlang
1016 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1017
1018 if (extlang != NULL)
1019 {
1020 error (_("Only one stop condition allowed. There is currently"
1021 " a %s stop condition defined for this breakpoint."),
1022 ext_lang_capitalized_name (extlang));
1023 }
1024 set_breakpoint_condition (b, p, from_tty);
1025
1026 if (is_breakpoint (b))
1027 update_global_location_list (UGLL_MAY_INSERT);
1028
1029 return;
1030 }
1031
1032 error (_("No breakpoint number %d."), bnum);
1033 }
1034
1035 /* Check that COMMAND do not contain commands that are suitable
1036 only for tracepoints and not suitable for ordinary breakpoints.
1037 Throw if any such commands is found. */
1038
1039 static void
1040 check_no_tracepoint_commands (struct command_line *commands)
1041 {
1042 struct command_line *c;
1043
1044 for (c = commands; c; c = c->next)
1045 {
1046 int i;
1047
1048 if (c->control_type == while_stepping_control)
1049 error (_("The 'while-stepping' command can "
1050 "only be used for tracepoints"));
1051
1052 for (i = 0; i < c->body_count; ++i)
1053 check_no_tracepoint_commands ((c->body_list)[i]);
1054
1055 /* Not that command parsing removes leading whitespace and comment
1056 lines and also empty lines. So, we only need to check for
1057 command directly. */
1058 if (strstr (c->line, "collect ") == c->line)
1059 error (_("The 'collect' command can only be used for tracepoints"));
1060
1061 if (strstr (c->line, "teval ") == c->line)
1062 error (_("The 'teval' command can only be used for tracepoints"));
1063 }
1064 }
1065
1066 struct longjmp_breakpoint : public breakpoint
1067 {
1068 ~longjmp_breakpoint () override;
1069 };
1070
1071 /* Encapsulate tests for different types of tracepoints. */
1072
1073 static bool
1074 is_tracepoint_type (bptype type)
1075 {
1076 return (type == bp_tracepoint
1077 || type == bp_fast_tracepoint
1078 || type == bp_static_tracepoint);
1079 }
1080
1081 static bool
1082 is_longjmp_type (bptype type)
1083 {
1084 return type == bp_longjmp || type == bp_exception;
1085 }
1086
1087 int
1088 is_tracepoint (const struct breakpoint *b)
1089 {
1090 return is_tracepoint_type (b->type);
1091 }
1092
1093 /* Factory function to create an appropriate instance of breakpoint given
1094 TYPE. */
1095
1096 static std::unique_ptr<breakpoint>
1097 new_breakpoint_from_type (bptype type)
1098 {
1099 breakpoint *b;
1100
1101 if (is_tracepoint_type (type))
1102 b = new tracepoint ();
1103 else if (is_longjmp_type (type))
1104 b = new longjmp_breakpoint ();
1105 else
1106 b = new breakpoint ();
1107
1108 return std::unique_ptr<breakpoint> (b);
1109 }
1110
1111 /* A helper function that validates that COMMANDS are valid for a
1112 breakpoint. This function will throw an exception if a problem is
1113 found. */
1114
1115 static void
1116 validate_commands_for_breakpoint (struct breakpoint *b,
1117 struct command_line *commands)
1118 {
1119 if (is_tracepoint (b))
1120 {
1121 struct tracepoint *t = (struct tracepoint *) b;
1122 struct command_line *c;
1123 struct command_line *while_stepping = 0;
1124
1125 /* Reset the while-stepping step count. The previous commands
1126 might have included a while-stepping action, while the new
1127 ones might not. */
1128 t->step_count = 0;
1129
1130 /* We need to verify that each top-level element of commands is
1131 valid for tracepoints, that there's at most one
1132 while-stepping element, and that the while-stepping's body
1133 has valid tracing commands excluding nested while-stepping.
1134 We also need to validate the tracepoint action line in the
1135 context of the tracepoint --- validate_actionline actually
1136 has side effects, like setting the tracepoint's
1137 while-stepping STEP_COUNT, in addition to checking if the
1138 collect/teval actions parse and make sense in the
1139 tracepoint's context. */
1140 for (c = commands; c; c = c->next)
1141 {
1142 if (c->control_type == while_stepping_control)
1143 {
1144 if (b->type == bp_fast_tracepoint)
1145 error (_("The 'while-stepping' command "
1146 "cannot be used for fast tracepoint"));
1147 else if (b->type == bp_static_tracepoint)
1148 error (_("The 'while-stepping' command "
1149 "cannot be used for static tracepoint"));
1150
1151 if (while_stepping)
1152 error (_("The 'while-stepping' command "
1153 "can be used only once"));
1154 else
1155 while_stepping = c;
1156 }
1157
1158 validate_actionline (c->line, b);
1159 }
1160 if (while_stepping)
1161 {
1162 struct command_line *c2;
1163
1164 gdb_assert (while_stepping->body_count == 1);
1165 c2 = while_stepping->body_list[0];
1166 for (; c2; c2 = c2->next)
1167 {
1168 if (c2->control_type == while_stepping_control)
1169 error (_("The 'while-stepping' command cannot be nested"));
1170 }
1171 }
1172 }
1173 else
1174 {
1175 check_no_tracepoint_commands (commands);
1176 }
1177 }
1178
1179 /* Return a vector of all the static tracepoints set at ADDR. The
1180 caller is responsible for releasing the vector. */
1181
1182 VEC(breakpoint_p) *
1183 static_tracepoints_here (CORE_ADDR addr)
1184 {
1185 struct breakpoint *b;
1186 VEC(breakpoint_p) *found = 0;
1187 struct bp_location *loc;
1188
1189 ALL_BREAKPOINTS (b)
1190 if (b->type == bp_static_tracepoint)
1191 {
1192 for (loc = b->loc; loc; loc = loc->next)
1193 if (loc->address == addr)
1194 VEC_safe_push(breakpoint_p, found, b);
1195 }
1196
1197 return found;
1198 }
1199
1200 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1201 validate that only allowed commands are included. */
1202
1203 void
1204 breakpoint_set_commands (struct breakpoint *b,
1205 command_line_up &&commands)
1206 {
1207 validate_commands_for_breakpoint (b, commands.get ());
1208
1209 b->commands = std::move (commands);
1210 observer_notify_breakpoint_modified (b);
1211 }
1212
1213 /* Set the internal `silent' flag on the breakpoint. Note that this
1214 is not the same as the "silent" that may appear in the breakpoint's
1215 commands. */
1216
1217 void
1218 breakpoint_set_silent (struct breakpoint *b, int silent)
1219 {
1220 int old_silent = b->silent;
1221
1222 b->silent = silent;
1223 if (old_silent != silent)
1224 observer_notify_breakpoint_modified (b);
1225 }
1226
1227 /* Set the thread for this breakpoint. If THREAD is -1, make the
1228 breakpoint work for any thread. */
1229
1230 void
1231 breakpoint_set_thread (struct breakpoint *b, int thread)
1232 {
1233 int old_thread = b->thread;
1234
1235 b->thread = thread;
1236 if (old_thread != thread)
1237 observer_notify_breakpoint_modified (b);
1238 }
1239
1240 /* Set the task for this breakpoint. If TASK is 0, make the
1241 breakpoint work for any task. */
1242
1243 void
1244 breakpoint_set_task (struct breakpoint *b, int task)
1245 {
1246 int old_task = b->task;
1247
1248 b->task = task;
1249 if (old_task != task)
1250 observer_notify_breakpoint_modified (b);
1251 }
1252
1253 void
1254 check_tracepoint_command (char *line, void *closure)
1255 {
1256 struct breakpoint *b = (struct breakpoint *) closure;
1257
1258 validate_actionline (line, b);
1259 }
1260
1261 static void
1262 commands_command_1 (const char *arg, int from_tty,
1263 struct command_line *control)
1264 {
1265 counted_command_line cmd;
1266
1267 std::string new_arg;
1268
1269 if (arg == NULL || !*arg)
1270 {
1271 if (breakpoint_count - prev_breakpoint_count > 1)
1272 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1273 breakpoint_count);
1274 else if (breakpoint_count > 0)
1275 new_arg = string_printf ("%d", breakpoint_count);
1276 arg = new_arg.c_str ();
1277 }
1278
1279 map_breakpoint_numbers
1280 (arg, [&] (breakpoint *b)
1281 {
1282 if (cmd == NULL)
1283 {
1284 if (control != NULL)
1285 cmd = copy_command_lines (control->body_list[0]);
1286 else
1287 {
1288 std::string str
1289 = string_printf (_("Type commands for breakpoint(s) "
1290 "%s, one per line."),
1291 arg);
1292
1293 cmd = read_command_lines (&str[0],
1294 from_tty, 1,
1295 (is_tracepoint (b)
1296 ? check_tracepoint_command : 0),
1297 b);
1298 }
1299 }
1300
1301 /* If a breakpoint was on the list more than once, we don't need to
1302 do anything. */
1303 if (b->commands != cmd)
1304 {
1305 validate_commands_for_breakpoint (b, cmd.get ());
1306 b->commands = cmd;
1307 observer_notify_breakpoint_modified (b);
1308 }
1309 });
1310
1311 if (cmd == NULL)
1312 error (_("No breakpoints specified."));
1313 }
1314
1315 static void
1316 commands_command (char *arg, int from_tty)
1317 {
1318 commands_command_1 (arg, from_tty, NULL);
1319 }
1320
1321 /* Like commands_command, but instead of reading the commands from
1322 input stream, takes them from an already parsed command structure.
1323
1324 This is used by cli-script.c to DTRT with breakpoint commands
1325 that are part of if and while bodies. */
1326 enum command_control_type
1327 commands_from_control_command (const char *arg, struct command_line *cmd)
1328 {
1329 commands_command_1 (arg, 0, cmd);
1330 return simple_control;
1331 }
1332
1333 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1334
1335 static int
1336 bp_location_has_shadow (struct bp_location *bl)
1337 {
1338 if (bl->loc_type != bp_loc_software_breakpoint)
1339 return 0;
1340 if (!bl->inserted)
1341 return 0;
1342 if (bl->target_info.shadow_len == 0)
1343 /* BL isn't valid, or doesn't shadow memory. */
1344 return 0;
1345 return 1;
1346 }
1347
1348 /* Update BUF, which is LEN bytes read from the target address
1349 MEMADDR, by replacing a memory breakpoint with its shadowed
1350 contents.
1351
1352 If READBUF is not NULL, this buffer must not overlap with the of
1353 the breakpoint location's shadow_contents buffer. Otherwise, a
1354 failed assertion internal error will be raised. */
1355
1356 static void
1357 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1358 const gdb_byte *writebuf_org,
1359 ULONGEST memaddr, LONGEST len,
1360 struct bp_target_info *target_info,
1361 struct gdbarch *gdbarch)
1362 {
1363 /* Now do full processing of the found relevant range of elements. */
1364 CORE_ADDR bp_addr = 0;
1365 int bp_size = 0;
1366 int bptoffset = 0;
1367
1368 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1369 current_program_space->aspace, 0))
1370 {
1371 /* The breakpoint is inserted in a different address space. */
1372 return;
1373 }
1374
1375 /* Addresses and length of the part of the breakpoint that
1376 we need to copy. */
1377 bp_addr = target_info->placed_address;
1378 bp_size = target_info->shadow_len;
1379
1380 if (bp_addr + bp_size <= memaddr)
1381 {
1382 /* The breakpoint is entirely before the chunk of memory we are
1383 reading. */
1384 return;
1385 }
1386
1387 if (bp_addr >= memaddr + len)
1388 {
1389 /* The breakpoint is entirely after the chunk of memory we are
1390 reading. */
1391 return;
1392 }
1393
1394 /* Offset within shadow_contents. */
1395 if (bp_addr < memaddr)
1396 {
1397 /* Only copy the second part of the breakpoint. */
1398 bp_size -= memaddr - bp_addr;
1399 bptoffset = memaddr - bp_addr;
1400 bp_addr = memaddr;
1401 }
1402
1403 if (bp_addr + bp_size > memaddr + len)
1404 {
1405 /* Only copy the first part of the breakpoint. */
1406 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1407 }
1408
1409 if (readbuf != NULL)
1410 {
1411 /* Verify that the readbuf buffer does not overlap with the
1412 shadow_contents buffer. */
1413 gdb_assert (target_info->shadow_contents >= readbuf + len
1414 || readbuf >= (target_info->shadow_contents
1415 + target_info->shadow_len));
1416
1417 /* Update the read buffer with this inserted breakpoint's
1418 shadow. */
1419 memcpy (readbuf + bp_addr - memaddr,
1420 target_info->shadow_contents + bptoffset, bp_size);
1421 }
1422 else
1423 {
1424 const unsigned char *bp;
1425 CORE_ADDR addr = target_info->reqstd_address;
1426 int placed_size;
1427
1428 /* Update the shadow with what we want to write to memory. */
1429 memcpy (target_info->shadow_contents + bptoffset,
1430 writebuf_org + bp_addr - memaddr, bp_size);
1431
1432 /* Determine appropriate breakpoint contents and size for this
1433 address. */
1434 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1435
1436 /* Update the final write buffer with this inserted
1437 breakpoint's INSN. */
1438 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1439 }
1440 }
1441
1442 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1443 by replacing any memory breakpoints with their shadowed contents.
1444
1445 If READBUF is not NULL, this buffer must not overlap with any of
1446 the breakpoint location's shadow_contents buffers. Otherwise,
1447 a failed assertion internal error will be raised.
1448
1449 The range of shadowed area by each bp_location is:
1450 bl->address - bp_locations_placed_address_before_address_max
1451 up to bl->address + bp_locations_shadow_len_after_address_max
1452 The range we were requested to resolve shadows for is:
1453 memaddr ... memaddr + len
1454 Thus the safe cutoff boundaries for performance optimization are
1455 memaddr + len <= (bl->address
1456 - bp_locations_placed_address_before_address_max)
1457 and:
1458 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1459
1460 void
1461 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1462 const gdb_byte *writebuf_org,
1463 ULONGEST memaddr, LONGEST len)
1464 {
1465 /* Left boundary, right boundary and median element of our binary
1466 search. */
1467 unsigned bc_l, bc_r, bc;
1468
1469 /* Find BC_L which is a leftmost element which may affect BUF
1470 content. It is safe to report lower value but a failure to
1471 report higher one. */
1472
1473 bc_l = 0;
1474 bc_r = bp_locations_count;
1475 while (bc_l + 1 < bc_r)
1476 {
1477 struct bp_location *bl;
1478
1479 bc = (bc_l + bc_r) / 2;
1480 bl = bp_locations[bc];
1481
1482 /* Check first BL->ADDRESS will not overflow due to the added
1483 constant. Then advance the left boundary only if we are sure
1484 the BC element can in no way affect the BUF content (MEMADDR
1485 to MEMADDR + LEN range).
1486
1487 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1488 offset so that we cannot miss a breakpoint with its shadow
1489 range tail still reaching MEMADDR. */
1490
1491 if ((bl->address + bp_locations_shadow_len_after_address_max
1492 >= bl->address)
1493 && (bl->address + bp_locations_shadow_len_after_address_max
1494 <= memaddr))
1495 bc_l = bc;
1496 else
1497 bc_r = bc;
1498 }
1499
1500 /* Due to the binary search above, we need to make sure we pick the
1501 first location that's at BC_L's address. E.g., if there are
1502 multiple locations at the same address, BC_L may end up pointing
1503 at a duplicate location, and miss the "master"/"inserted"
1504 location. Say, given locations L1, L2 and L3 at addresses A and
1505 B:
1506
1507 L1@A, L2@A, L3@B, ...
1508
1509 BC_L could end up pointing at location L2, while the "master"
1510 location could be L1. Since the `loc->inserted' flag is only set
1511 on "master" locations, we'd forget to restore the shadow of L1
1512 and L2. */
1513 while (bc_l > 0
1514 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1515 bc_l--;
1516
1517 /* Now do full processing of the found relevant range of elements. */
1518
1519 for (bc = bc_l; bc < bp_locations_count; bc++)
1520 {
1521 struct bp_location *bl = bp_locations[bc];
1522
1523 /* bp_location array has BL->OWNER always non-NULL. */
1524 if (bl->owner->type == bp_none)
1525 warning (_("reading through apparently deleted breakpoint #%d?"),
1526 bl->owner->number);
1527
1528 /* Performance optimization: any further element can no longer affect BUF
1529 content. */
1530
1531 if (bl->address >= bp_locations_placed_address_before_address_max
1532 && memaddr + len <= (bl->address
1533 - bp_locations_placed_address_before_address_max))
1534 break;
1535
1536 if (!bp_location_has_shadow (bl))
1537 continue;
1538
1539 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1540 memaddr, len, &bl->target_info, bl->gdbarch);
1541 }
1542 }
1543
1544 \f
1545
1546 /* Return true if BPT is either a software breakpoint or a hardware
1547 breakpoint. */
1548
1549 int
1550 is_breakpoint (const struct breakpoint *bpt)
1551 {
1552 return (bpt->type == bp_breakpoint
1553 || bpt->type == bp_hardware_breakpoint
1554 || bpt->type == bp_dprintf);
1555 }
1556
1557 /* Return true if BPT is of any hardware watchpoint kind. */
1558
1559 static int
1560 is_hardware_watchpoint (const struct breakpoint *bpt)
1561 {
1562 return (bpt->type == bp_hardware_watchpoint
1563 || bpt->type == bp_read_watchpoint
1564 || bpt->type == bp_access_watchpoint);
1565 }
1566
1567 /* Return true if BPT is of any watchpoint kind, hardware or
1568 software. */
1569
1570 int
1571 is_watchpoint (const struct breakpoint *bpt)
1572 {
1573 return (is_hardware_watchpoint (bpt)
1574 || bpt->type == bp_watchpoint);
1575 }
1576
1577 /* Returns true if the current thread and its running state are safe
1578 to evaluate or update watchpoint B. Watchpoints on local
1579 expressions need to be evaluated in the context of the thread that
1580 was current when the watchpoint was created, and, that thread needs
1581 to be stopped to be able to select the correct frame context.
1582 Watchpoints on global expressions can be evaluated on any thread,
1583 and in any state. It is presently left to the target allowing
1584 memory accesses when threads are running. */
1585
1586 static int
1587 watchpoint_in_thread_scope (struct watchpoint *b)
1588 {
1589 return (b->pspace == current_program_space
1590 && (ptid_equal (b->watchpoint_thread, null_ptid)
1591 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1592 && !is_executing (inferior_ptid))));
1593 }
1594
1595 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1596 associated bp_watchpoint_scope breakpoint. */
1597
1598 static void
1599 watchpoint_del_at_next_stop (struct watchpoint *w)
1600 {
1601 if (w->related_breakpoint != w)
1602 {
1603 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1604 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1605 w->related_breakpoint->disposition = disp_del_at_next_stop;
1606 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1607 w->related_breakpoint = w;
1608 }
1609 w->disposition = disp_del_at_next_stop;
1610 }
1611
1612 /* Extract a bitfield value from value VAL using the bit parameters contained in
1613 watchpoint W. */
1614
1615 static struct value *
1616 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1617 {
1618 struct value *bit_val;
1619
1620 if (val == NULL)
1621 return NULL;
1622
1623 bit_val = allocate_value (value_type (val));
1624
1625 unpack_value_bitfield (bit_val,
1626 w->val_bitpos,
1627 w->val_bitsize,
1628 value_contents_for_printing (val),
1629 value_offset (val),
1630 val);
1631
1632 return bit_val;
1633 }
1634
1635 /* Allocate a dummy location and add it to B, which must be a software
1636 watchpoint. This is required because even if a software watchpoint
1637 is not watching any memory, bpstat_stop_status requires a location
1638 to be able to report stops. */
1639
1640 static void
1641 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1642 struct program_space *pspace)
1643 {
1644 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1645
1646 b->loc = allocate_bp_location (b);
1647 b->loc->pspace = pspace;
1648 b->loc->address = -1;
1649 b->loc->length = -1;
1650 }
1651
1652 /* Returns true if B is a software watchpoint that is not watching any
1653 memory (e.g., "watch $pc"). */
1654
1655 static int
1656 is_no_memory_software_watchpoint (struct breakpoint *b)
1657 {
1658 return (b->type == bp_watchpoint
1659 && b->loc != NULL
1660 && b->loc->next == NULL
1661 && b->loc->address == -1
1662 && b->loc->length == -1);
1663 }
1664
1665 /* Assuming that B is a watchpoint:
1666 - Reparse watchpoint expression, if REPARSE is non-zero
1667 - Evaluate expression and store the result in B->val
1668 - Evaluate the condition if there is one, and store the result
1669 in b->loc->cond.
1670 - Update the list of values that must be watched in B->loc.
1671
1672 If the watchpoint disposition is disp_del_at_next_stop, then do
1673 nothing. If this is local watchpoint that is out of scope, delete
1674 it.
1675
1676 Even with `set breakpoint always-inserted on' the watchpoints are
1677 removed + inserted on each stop here. Normal breakpoints must
1678 never be removed because they might be missed by a running thread
1679 when debugging in non-stop mode. On the other hand, hardware
1680 watchpoints (is_hardware_watchpoint; processed here) are specific
1681 to each LWP since they are stored in each LWP's hardware debug
1682 registers. Therefore, such LWP must be stopped first in order to
1683 be able to modify its hardware watchpoints.
1684
1685 Hardware watchpoints must be reset exactly once after being
1686 presented to the user. It cannot be done sooner, because it would
1687 reset the data used to present the watchpoint hit to the user. And
1688 it must not be done later because it could display the same single
1689 watchpoint hit during multiple GDB stops. Note that the latter is
1690 relevant only to the hardware watchpoint types bp_read_watchpoint
1691 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1692 not user-visible - its hit is suppressed if the memory content has
1693 not changed.
1694
1695 The following constraints influence the location where we can reset
1696 hardware watchpoints:
1697
1698 * target_stopped_by_watchpoint and target_stopped_data_address are
1699 called several times when GDB stops.
1700
1701 [linux]
1702 * Multiple hardware watchpoints can be hit at the same time,
1703 causing GDB to stop. GDB only presents one hardware watchpoint
1704 hit at a time as the reason for stopping, and all the other hits
1705 are presented later, one after the other, each time the user
1706 requests the execution to be resumed. Execution is not resumed
1707 for the threads still having pending hit event stored in
1708 LWP_INFO->STATUS. While the watchpoint is already removed from
1709 the inferior on the first stop the thread hit event is kept being
1710 reported from its cached value by linux_nat_stopped_data_address
1711 until the real thread resume happens after the watchpoint gets
1712 presented and thus its LWP_INFO->STATUS gets reset.
1713
1714 Therefore the hardware watchpoint hit can get safely reset on the
1715 watchpoint removal from inferior. */
1716
1717 static void
1718 update_watchpoint (struct watchpoint *b, int reparse)
1719 {
1720 int within_current_scope;
1721 struct frame_id saved_frame_id;
1722 int frame_saved;
1723
1724 /* If this is a local watchpoint, we only want to check if the
1725 watchpoint frame is in scope if the current thread is the thread
1726 that was used to create the watchpoint. */
1727 if (!watchpoint_in_thread_scope (b))
1728 return;
1729
1730 if (b->disposition == disp_del_at_next_stop)
1731 return;
1732
1733 frame_saved = 0;
1734
1735 /* Determine if the watchpoint is within scope. */
1736 if (b->exp_valid_block == NULL)
1737 within_current_scope = 1;
1738 else
1739 {
1740 struct frame_info *fi = get_current_frame ();
1741 struct gdbarch *frame_arch = get_frame_arch (fi);
1742 CORE_ADDR frame_pc = get_frame_pc (fi);
1743
1744 /* If we're at a point where the stack has been destroyed
1745 (e.g. in a function epilogue), unwinding may not work
1746 properly. Do not attempt to recreate locations at this
1747 point. See similar comments in watchpoint_check. */
1748 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1749 return;
1750
1751 /* Save the current frame's ID so we can restore it after
1752 evaluating the watchpoint expression on its own frame. */
1753 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1754 took a frame parameter, so that we didn't have to change the
1755 selected frame. */
1756 frame_saved = 1;
1757 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1758
1759 fi = frame_find_by_id (b->watchpoint_frame);
1760 within_current_scope = (fi != NULL);
1761 if (within_current_scope)
1762 select_frame (fi);
1763 }
1764
1765 /* We don't free locations. They are stored in the bp_location array
1766 and update_global_location_list will eventually delete them and
1767 remove breakpoints if needed. */
1768 b->loc = NULL;
1769
1770 if (within_current_scope && reparse)
1771 {
1772 const char *s;
1773
1774 b->exp.reset ();
1775 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1776 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1777 /* If the meaning of expression itself changed, the old value is
1778 no longer relevant. We don't want to report a watchpoint hit
1779 to the user when the old value and the new value may actually
1780 be completely different objects. */
1781 value_free (b->val);
1782 b->val = NULL;
1783 b->val_valid = 0;
1784
1785 /* Note that unlike with breakpoints, the watchpoint's condition
1786 expression is stored in the breakpoint object, not in the
1787 locations (re)created below. */
1788 if (b->cond_string != NULL)
1789 {
1790 b->cond_exp.reset ();
1791
1792 s = b->cond_string;
1793 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1794 }
1795 }
1796
1797 /* If we failed to parse the expression, for example because
1798 it refers to a global variable in a not-yet-loaded shared library,
1799 don't try to insert watchpoint. We don't automatically delete
1800 such watchpoint, though, since failure to parse expression
1801 is different from out-of-scope watchpoint. */
1802 if (!target_has_execution)
1803 {
1804 /* Without execution, memory can't change. No use to try and
1805 set watchpoint locations. The watchpoint will be reset when
1806 the target gains execution, through breakpoint_re_set. */
1807 if (!can_use_hw_watchpoints)
1808 {
1809 if (b->ops->works_in_software_mode (b))
1810 b->type = bp_watchpoint;
1811 else
1812 error (_("Can't set read/access watchpoint when "
1813 "hardware watchpoints are disabled."));
1814 }
1815 }
1816 else if (within_current_scope && b->exp)
1817 {
1818 int pc = 0;
1819 struct value *val_chain, *v, *result, *next;
1820 struct program_space *frame_pspace;
1821
1822 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1823
1824 /* Avoid setting b->val if it's already set. The meaning of
1825 b->val is 'the last value' user saw, and we should update
1826 it only if we reported that last value to user. As it
1827 happens, the code that reports it updates b->val directly.
1828 We don't keep track of the memory value for masked
1829 watchpoints. */
1830 if (!b->val_valid && !is_masked_watchpoint (b))
1831 {
1832 if (b->val_bitsize != 0)
1833 {
1834 v = extract_bitfield_from_watchpoint_value (b, v);
1835 if (v != NULL)
1836 release_value (v);
1837 }
1838 b->val = v;
1839 b->val_valid = 1;
1840 }
1841
1842 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1843
1844 /* Look at each value on the value chain. */
1845 for (v = val_chain; v; v = value_next (v))
1846 {
1847 /* If it's a memory location, and GDB actually needed
1848 its contents to evaluate the expression, then we
1849 must watch it. If the first value returned is
1850 still lazy, that means an error occurred reading it;
1851 watch it anyway in case it becomes readable. */
1852 if (VALUE_LVAL (v) == lval_memory
1853 && (v == val_chain || ! value_lazy (v)))
1854 {
1855 struct type *vtype = check_typedef (value_type (v));
1856
1857 /* We only watch structs and arrays if user asked
1858 for it explicitly, never if they just happen to
1859 appear in the middle of some value chain. */
1860 if (v == result
1861 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1862 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1863 {
1864 CORE_ADDR addr;
1865 enum target_hw_bp_type type;
1866 struct bp_location *loc, **tmp;
1867 int bitpos = 0, bitsize = 0;
1868
1869 if (value_bitsize (v) != 0)
1870 {
1871 /* Extract the bit parameters out from the bitfield
1872 sub-expression. */
1873 bitpos = value_bitpos (v);
1874 bitsize = value_bitsize (v);
1875 }
1876 else if (v == result && b->val_bitsize != 0)
1877 {
1878 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1879 lvalue whose bit parameters are saved in the fields
1880 VAL_BITPOS and VAL_BITSIZE. */
1881 bitpos = b->val_bitpos;
1882 bitsize = b->val_bitsize;
1883 }
1884
1885 addr = value_address (v);
1886 if (bitsize != 0)
1887 {
1888 /* Skip the bytes that don't contain the bitfield. */
1889 addr += bitpos / 8;
1890 }
1891
1892 type = hw_write;
1893 if (b->type == bp_read_watchpoint)
1894 type = hw_read;
1895 else if (b->type == bp_access_watchpoint)
1896 type = hw_access;
1897
1898 loc = allocate_bp_location (b);
1899 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1900 ;
1901 *tmp = loc;
1902 loc->gdbarch = get_type_arch (value_type (v));
1903
1904 loc->pspace = frame_pspace;
1905 loc->address = addr;
1906
1907 if (bitsize != 0)
1908 {
1909 /* Just cover the bytes that make up the bitfield. */
1910 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1911 }
1912 else
1913 loc->length = TYPE_LENGTH (value_type (v));
1914
1915 loc->watchpoint_type = type;
1916 }
1917 }
1918 }
1919
1920 /* Change the type of breakpoint between hardware assisted or
1921 an ordinary watchpoint depending on the hardware support
1922 and free hardware slots. REPARSE is set when the inferior
1923 is started. */
1924 if (reparse)
1925 {
1926 int reg_cnt;
1927 enum bp_loc_type loc_type;
1928 struct bp_location *bl;
1929
1930 reg_cnt = can_use_hardware_watchpoint (val_chain);
1931
1932 if (reg_cnt)
1933 {
1934 int i, target_resources_ok, other_type_used;
1935 enum bptype type;
1936
1937 /* Use an exact watchpoint when there's only one memory region to be
1938 watched, and only one debug register is needed to watch it. */
1939 b->exact = target_exact_watchpoints && reg_cnt == 1;
1940
1941 /* We need to determine how many resources are already
1942 used for all other hardware watchpoints plus this one
1943 to see if we still have enough resources to also fit
1944 this watchpoint in as well. */
1945
1946 /* If this is a software watchpoint, we try to turn it
1947 to a hardware one -- count resources as if B was of
1948 hardware watchpoint type. */
1949 type = b->type;
1950 if (type == bp_watchpoint)
1951 type = bp_hardware_watchpoint;
1952
1953 /* This watchpoint may or may not have been placed on
1954 the list yet at this point (it won't be in the list
1955 if we're trying to create it for the first time,
1956 through watch_command), so always account for it
1957 manually. */
1958
1959 /* Count resources used by all watchpoints except B. */
1960 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1961
1962 /* Add in the resources needed for B. */
1963 i += hw_watchpoint_use_count (b);
1964
1965 target_resources_ok
1966 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1967 if (target_resources_ok <= 0)
1968 {
1969 int sw_mode = b->ops->works_in_software_mode (b);
1970
1971 if (target_resources_ok == 0 && !sw_mode)
1972 error (_("Target does not support this type of "
1973 "hardware watchpoint."));
1974 else if (target_resources_ok < 0 && !sw_mode)
1975 error (_("There are not enough available hardware "
1976 "resources for this watchpoint."));
1977
1978 /* Downgrade to software watchpoint. */
1979 b->type = bp_watchpoint;
1980 }
1981 else
1982 {
1983 /* If this was a software watchpoint, we've just
1984 found we have enough resources to turn it to a
1985 hardware watchpoint. Otherwise, this is a
1986 nop. */
1987 b->type = type;
1988 }
1989 }
1990 else if (!b->ops->works_in_software_mode (b))
1991 {
1992 if (!can_use_hw_watchpoints)
1993 error (_("Can't set read/access watchpoint when "
1994 "hardware watchpoints are disabled."));
1995 else
1996 error (_("Expression cannot be implemented with "
1997 "read/access watchpoint."));
1998 }
1999 else
2000 b->type = bp_watchpoint;
2001
2002 loc_type = (b->type == bp_watchpoint? bp_loc_other
2003 : bp_loc_hardware_watchpoint);
2004 for (bl = b->loc; bl; bl = bl->next)
2005 bl->loc_type = loc_type;
2006 }
2007
2008 for (v = val_chain; v; v = next)
2009 {
2010 next = value_next (v);
2011 if (v != b->val)
2012 value_free (v);
2013 }
2014
2015 /* If a software watchpoint is not watching any memory, then the
2016 above left it without any location set up. But,
2017 bpstat_stop_status requires a location to be able to report
2018 stops, so make sure there's at least a dummy one. */
2019 if (b->type == bp_watchpoint && b->loc == NULL)
2020 software_watchpoint_add_no_memory_location (b, frame_pspace);
2021 }
2022 else if (!within_current_scope)
2023 {
2024 printf_filtered (_("\
2025 Watchpoint %d deleted because the program has left the block\n\
2026 in which its expression is valid.\n"),
2027 b->number);
2028 watchpoint_del_at_next_stop (b);
2029 }
2030
2031 /* Restore the selected frame. */
2032 if (frame_saved)
2033 select_frame (frame_find_by_id (saved_frame_id));
2034 }
2035
2036
2037 /* Returns 1 iff breakpoint location should be
2038 inserted in the inferior. We don't differentiate the type of BL's owner
2039 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2040 breakpoint_ops is not defined, because in insert_bp_location,
2041 tracepoint's insert_location will not be called. */
2042 static int
2043 should_be_inserted (struct bp_location *bl)
2044 {
2045 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2046 return 0;
2047
2048 if (bl->owner->disposition == disp_del_at_next_stop)
2049 return 0;
2050
2051 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2052 return 0;
2053
2054 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2055 return 0;
2056
2057 /* This is set for example, when we're attached to the parent of a
2058 vfork, and have detached from the child. The child is running
2059 free, and we expect it to do an exec or exit, at which point the
2060 OS makes the parent schedulable again (and the target reports
2061 that the vfork is done). Until the child is done with the shared
2062 memory region, do not insert breakpoints in the parent, otherwise
2063 the child could still trip on the parent's breakpoints. Since
2064 the parent is blocked anyway, it won't miss any breakpoint. */
2065 if (bl->pspace->breakpoints_not_allowed)
2066 return 0;
2067
2068 /* Don't insert a breakpoint if we're trying to step past its
2069 location, except if the breakpoint is a single-step breakpoint,
2070 and the breakpoint's thread is the thread which is stepping past
2071 a breakpoint. */
2072 if ((bl->loc_type == bp_loc_software_breakpoint
2073 || bl->loc_type == bp_loc_hardware_breakpoint)
2074 && stepping_past_instruction_at (bl->pspace->aspace,
2075 bl->address)
2076 /* The single-step breakpoint may be inserted at the location
2077 we're trying to step if the instruction branches to itself.
2078 However, the instruction won't be executed at all and it may
2079 break the semantics of the instruction, for example, the
2080 instruction is a conditional branch or updates some flags.
2081 We can't fix it unless GDB is able to emulate the instruction
2082 or switch to displaced stepping. */
2083 && !(bl->owner->type == bp_single_step
2084 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2085 {
2086 if (debug_infrun)
2087 {
2088 fprintf_unfiltered (gdb_stdlog,
2089 "infrun: skipping breakpoint: "
2090 "stepping past insn at: %s\n",
2091 paddress (bl->gdbarch, bl->address));
2092 }
2093 return 0;
2094 }
2095
2096 /* Don't insert watchpoints if we're trying to step past the
2097 instruction that triggered one. */
2098 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2099 && stepping_past_nonsteppable_watchpoint ())
2100 {
2101 if (debug_infrun)
2102 {
2103 fprintf_unfiltered (gdb_stdlog,
2104 "infrun: stepping past non-steppable watchpoint. "
2105 "skipping watchpoint at %s:%d\n",
2106 paddress (bl->gdbarch, bl->address),
2107 bl->length);
2108 }
2109 return 0;
2110 }
2111
2112 return 1;
2113 }
2114
2115 /* Same as should_be_inserted but does the check assuming
2116 that the location is not duplicated. */
2117
2118 static int
2119 unduplicated_should_be_inserted (struct bp_location *bl)
2120 {
2121 int result;
2122 const int save_duplicate = bl->duplicate;
2123
2124 bl->duplicate = 0;
2125 result = should_be_inserted (bl);
2126 bl->duplicate = save_duplicate;
2127 return result;
2128 }
2129
2130 /* Parses a conditional described by an expression COND into an
2131 agent expression bytecode suitable for evaluation
2132 by the bytecode interpreter. Return NULL if there was
2133 any error during parsing. */
2134
2135 static agent_expr_up
2136 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2137 {
2138 if (cond == NULL)
2139 return NULL;
2140
2141 agent_expr_up aexpr;
2142
2143 /* We don't want to stop processing, so catch any errors
2144 that may show up. */
2145 TRY
2146 {
2147 aexpr = gen_eval_for_expr (scope, cond);
2148 }
2149
2150 CATCH (ex, RETURN_MASK_ERROR)
2151 {
2152 /* If we got here, it means the condition could not be parsed to a valid
2153 bytecode expression and thus can't be evaluated on the target's side.
2154 It's no use iterating through the conditions. */
2155 }
2156 END_CATCH
2157
2158 /* We have a valid agent expression. */
2159 return aexpr;
2160 }
2161
2162 /* Based on location BL, create a list of breakpoint conditions to be
2163 passed on to the target. If we have duplicated locations with different
2164 conditions, we will add such conditions to the list. The idea is that the
2165 target will evaluate the list of conditions and will only notify GDB when
2166 one of them is true. */
2167
2168 static void
2169 build_target_condition_list (struct bp_location *bl)
2170 {
2171 struct bp_location **locp = NULL, **loc2p;
2172 int null_condition_or_parse_error = 0;
2173 int modified = bl->needs_update;
2174 struct bp_location *loc;
2175
2176 /* Release conditions left over from a previous insert. */
2177 bl->target_info.conditions.clear ();
2178
2179 /* This is only meaningful if the target is
2180 evaluating conditions and if the user has
2181 opted for condition evaluation on the target's
2182 side. */
2183 if (gdb_evaluates_breakpoint_condition_p ()
2184 || !target_supports_evaluation_of_breakpoint_conditions ())
2185 return;
2186
2187 /* Do a first pass to check for locations with no assigned
2188 conditions or conditions that fail to parse to a valid agent expression
2189 bytecode. If any of these happen, then it's no use to send conditions
2190 to the target since this location will always trigger and generate a
2191 response back to GDB. */
2192 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2193 {
2194 loc = (*loc2p);
2195 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2196 {
2197 if (modified)
2198 {
2199 /* Re-parse the conditions since something changed. In that
2200 case we already freed the condition bytecodes (see
2201 force_breakpoint_reinsertion). We just
2202 need to parse the condition to bytecodes again. */
2203 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2204 loc->cond.get ());
2205 }
2206
2207 /* If we have a NULL bytecode expression, it means something
2208 went wrong or we have a null condition expression. */
2209 if (!loc->cond_bytecode)
2210 {
2211 null_condition_or_parse_error = 1;
2212 break;
2213 }
2214 }
2215 }
2216
2217 /* If any of these happened, it means we will have to evaluate the conditions
2218 for the location's address on gdb's side. It is no use keeping bytecodes
2219 for all the other duplicate locations, thus we free all of them here.
2220
2221 This is so we have a finer control over which locations' conditions are
2222 being evaluated by GDB or the remote stub. */
2223 if (null_condition_or_parse_error)
2224 {
2225 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2226 {
2227 loc = (*loc2p);
2228 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2229 {
2230 /* Only go as far as the first NULL bytecode is
2231 located. */
2232 if (!loc->cond_bytecode)
2233 return;
2234
2235 loc->cond_bytecode.reset ();
2236 }
2237 }
2238 }
2239
2240 /* No NULL conditions or failed bytecode generation. Build a condition list
2241 for this location's address. */
2242 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2243 {
2244 loc = (*loc2p);
2245 if (loc->cond
2246 && is_breakpoint (loc->owner)
2247 && loc->pspace->num == bl->pspace->num
2248 && loc->owner->enable_state == bp_enabled
2249 && loc->enabled)
2250 {
2251 /* Add the condition to the vector. This will be used later
2252 to send the conditions to the target. */
2253 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2254 }
2255 }
2256
2257 return;
2258 }
2259
2260 /* Parses a command described by string CMD into an agent expression
2261 bytecode suitable for evaluation by the bytecode interpreter.
2262 Return NULL if there was any error during parsing. */
2263
2264 static agent_expr_up
2265 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2266 {
2267 struct cleanup *old_cleanups = 0;
2268 struct expression **argvec;
2269 const char *cmdrest;
2270 const char *format_start, *format_end;
2271 struct format_piece *fpieces;
2272 int nargs;
2273 struct gdbarch *gdbarch = get_current_arch ();
2274
2275 if (cmd == NULL)
2276 return NULL;
2277
2278 cmdrest = cmd;
2279
2280 if (*cmdrest == ',')
2281 ++cmdrest;
2282 cmdrest = skip_spaces (cmdrest);
2283
2284 if (*cmdrest++ != '"')
2285 error (_("No format string following the location"));
2286
2287 format_start = cmdrest;
2288
2289 fpieces = parse_format_string (&cmdrest);
2290
2291 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2292
2293 format_end = cmdrest;
2294
2295 if (*cmdrest++ != '"')
2296 error (_("Bad format string, non-terminated '\"'."));
2297
2298 cmdrest = skip_spaces (cmdrest);
2299
2300 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2301 error (_("Invalid argument syntax"));
2302
2303 if (*cmdrest == ',')
2304 cmdrest++;
2305 cmdrest = skip_spaces (cmdrest);
2306
2307 /* For each argument, make an expression. */
2308
2309 argvec = (struct expression **) alloca (strlen (cmd)
2310 * sizeof (struct expression *));
2311
2312 nargs = 0;
2313 while (*cmdrest != '\0')
2314 {
2315 const char *cmd1;
2316
2317 cmd1 = cmdrest;
2318 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2319 argvec[nargs++] = expr.release ();
2320 cmdrest = cmd1;
2321 if (*cmdrest == ',')
2322 ++cmdrest;
2323 }
2324
2325 agent_expr_up aexpr;
2326
2327 /* We don't want to stop processing, so catch any errors
2328 that may show up. */
2329 TRY
2330 {
2331 aexpr = gen_printf (scope, gdbarch, 0, 0,
2332 format_start, format_end - format_start,
2333 fpieces, nargs, argvec);
2334 }
2335 CATCH (ex, RETURN_MASK_ERROR)
2336 {
2337 /* If we got here, it means the command could not be parsed to a valid
2338 bytecode expression and thus can't be evaluated on the target's side.
2339 It's no use iterating through the other commands. */
2340 }
2341 END_CATCH
2342
2343 do_cleanups (old_cleanups);
2344
2345 /* We have a valid agent expression, return it. */
2346 return aexpr;
2347 }
2348
2349 /* Based on location BL, create a list of breakpoint commands to be
2350 passed on to the target. If we have duplicated locations with
2351 different commands, we will add any such to the list. */
2352
2353 static void
2354 build_target_command_list (struct bp_location *bl)
2355 {
2356 struct bp_location **locp = NULL, **loc2p;
2357 int null_command_or_parse_error = 0;
2358 int modified = bl->needs_update;
2359 struct bp_location *loc;
2360
2361 /* Clear commands left over from a previous insert. */
2362 bl->target_info.tcommands.clear ();
2363
2364 if (!target_can_run_breakpoint_commands ())
2365 return;
2366
2367 /* For now, limit to agent-style dprintf breakpoints. */
2368 if (dprintf_style != dprintf_style_agent)
2369 return;
2370
2371 /* For now, if we have any duplicate location that isn't a dprintf,
2372 don't install the target-side commands, as that would make the
2373 breakpoint not be reported to the core, and we'd lose
2374 control. */
2375 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2376 {
2377 loc = (*loc2p);
2378 if (is_breakpoint (loc->owner)
2379 && loc->pspace->num == bl->pspace->num
2380 && loc->owner->type != bp_dprintf)
2381 return;
2382 }
2383
2384 /* Do a first pass to check for locations with no assigned
2385 conditions or conditions that fail to parse to a valid agent expression
2386 bytecode. If any of these happen, then it's no use to send conditions
2387 to the target since this location will always trigger and generate a
2388 response back to GDB. */
2389 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2390 {
2391 loc = (*loc2p);
2392 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2393 {
2394 if (modified)
2395 {
2396 /* Re-parse the commands since something changed. In that
2397 case we already freed the command bytecodes (see
2398 force_breakpoint_reinsertion). We just
2399 need to parse the command to bytecodes again. */
2400 loc->cmd_bytecode
2401 = parse_cmd_to_aexpr (bl->address,
2402 loc->owner->extra_string);
2403 }
2404
2405 /* If we have a NULL bytecode expression, it means something
2406 went wrong or we have a null command expression. */
2407 if (!loc->cmd_bytecode)
2408 {
2409 null_command_or_parse_error = 1;
2410 break;
2411 }
2412 }
2413 }
2414
2415 /* If anything failed, then we're not doing target-side commands,
2416 and so clean up. */
2417 if (null_command_or_parse_error)
2418 {
2419 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2420 {
2421 loc = (*loc2p);
2422 if (is_breakpoint (loc->owner)
2423 && loc->pspace->num == bl->pspace->num)
2424 {
2425 /* Only go as far as the first NULL bytecode is
2426 located. */
2427 if (loc->cmd_bytecode == NULL)
2428 return;
2429
2430 loc->cmd_bytecode.reset ();
2431 }
2432 }
2433 }
2434
2435 /* No NULL commands or failed bytecode generation. Build a command list
2436 for this location's address. */
2437 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2438 {
2439 loc = (*loc2p);
2440 if (loc->owner->extra_string
2441 && is_breakpoint (loc->owner)
2442 && loc->pspace->num == bl->pspace->num
2443 && loc->owner->enable_state == bp_enabled
2444 && loc->enabled)
2445 {
2446 /* Add the command to the vector. This will be used later
2447 to send the commands to the target. */
2448 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2449 }
2450 }
2451
2452 bl->target_info.persist = 0;
2453 /* Maybe flag this location as persistent. */
2454 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2455 bl->target_info.persist = 1;
2456 }
2457
2458 /* Return the kind of breakpoint on address *ADDR. Get the kind
2459 of breakpoint according to ADDR except single-step breakpoint.
2460 Get the kind of single-step breakpoint according to the current
2461 registers state. */
2462
2463 static int
2464 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2465 {
2466 if (bl->owner->type == bp_single_step)
2467 {
2468 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2469 struct regcache *regcache;
2470
2471 regcache = get_thread_regcache (thr->ptid);
2472
2473 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2474 regcache, addr);
2475 }
2476 else
2477 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2478 }
2479
2480 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2481 location. Any error messages are printed to TMP_ERROR_STREAM; and
2482 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2483 Returns 0 for success, 1 if the bp_location type is not supported or
2484 -1 for failure.
2485
2486 NOTE drow/2003-09-09: This routine could be broken down to an
2487 object-style method for each breakpoint or catchpoint type. */
2488 static int
2489 insert_bp_location (struct bp_location *bl,
2490 struct ui_file *tmp_error_stream,
2491 int *disabled_breaks,
2492 int *hw_breakpoint_error,
2493 int *hw_bp_error_explained_already)
2494 {
2495 enum errors bp_err = GDB_NO_ERROR;
2496 const char *bp_err_message = NULL;
2497
2498 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2499 return 0;
2500
2501 /* Note we don't initialize bl->target_info, as that wipes out
2502 the breakpoint location's shadow_contents if the breakpoint
2503 is still inserted at that location. This in turn breaks
2504 target_read_memory which depends on these buffers when
2505 a memory read is requested at the breakpoint location:
2506 Once the target_info has been wiped, we fail to see that
2507 we have a breakpoint inserted at that address and thus
2508 read the breakpoint instead of returning the data saved in
2509 the breakpoint location's shadow contents. */
2510 bl->target_info.reqstd_address = bl->address;
2511 bl->target_info.placed_address_space = bl->pspace->aspace;
2512 bl->target_info.length = bl->length;
2513
2514 /* When working with target-side conditions, we must pass all the conditions
2515 for the same breakpoint address down to the target since GDB will not
2516 insert those locations. With a list of breakpoint conditions, the target
2517 can decide when to stop and notify GDB. */
2518
2519 if (is_breakpoint (bl->owner))
2520 {
2521 build_target_condition_list (bl);
2522 build_target_command_list (bl);
2523 /* Reset the modification marker. */
2524 bl->needs_update = 0;
2525 }
2526
2527 if (bl->loc_type == bp_loc_software_breakpoint
2528 || bl->loc_type == bp_loc_hardware_breakpoint)
2529 {
2530 if (bl->owner->type != bp_hardware_breakpoint)
2531 {
2532 /* If the explicitly specified breakpoint type
2533 is not hardware breakpoint, check the memory map to see
2534 if the breakpoint address is in read only memory or not.
2535
2536 Two important cases are:
2537 - location type is not hardware breakpoint, memory
2538 is readonly. We change the type of the location to
2539 hardware breakpoint.
2540 - location type is hardware breakpoint, memory is
2541 read-write. This means we've previously made the
2542 location hardware one, but then the memory map changed,
2543 so we undo.
2544
2545 When breakpoints are removed, remove_breakpoints will use
2546 location types we've just set here, the only possible
2547 problem is that memory map has changed during running
2548 program, but it's not going to work anyway with current
2549 gdb. */
2550 struct mem_region *mr
2551 = lookup_mem_region (bl->target_info.reqstd_address);
2552
2553 if (mr)
2554 {
2555 if (automatic_hardware_breakpoints)
2556 {
2557 enum bp_loc_type new_type;
2558
2559 if (mr->attrib.mode != MEM_RW)
2560 new_type = bp_loc_hardware_breakpoint;
2561 else
2562 new_type = bp_loc_software_breakpoint;
2563
2564 if (new_type != bl->loc_type)
2565 {
2566 static int said = 0;
2567
2568 bl->loc_type = new_type;
2569 if (!said)
2570 {
2571 fprintf_filtered (gdb_stdout,
2572 _("Note: automatically using "
2573 "hardware breakpoints for "
2574 "read-only addresses.\n"));
2575 said = 1;
2576 }
2577 }
2578 }
2579 else if (bl->loc_type == bp_loc_software_breakpoint
2580 && mr->attrib.mode != MEM_RW)
2581 {
2582 fprintf_unfiltered (tmp_error_stream,
2583 _("Cannot insert breakpoint %d.\n"
2584 "Cannot set software breakpoint "
2585 "at read-only address %s\n"),
2586 bl->owner->number,
2587 paddress (bl->gdbarch, bl->address));
2588 return 1;
2589 }
2590 }
2591 }
2592
2593 /* First check to see if we have to handle an overlay. */
2594 if (overlay_debugging == ovly_off
2595 || bl->section == NULL
2596 || !(section_is_overlay (bl->section)))
2597 {
2598 /* No overlay handling: just set the breakpoint. */
2599 TRY
2600 {
2601 int val;
2602
2603 val = bl->owner->ops->insert_location (bl);
2604 if (val)
2605 bp_err = GENERIC_ERROR;
2606 }
2607 CATCH (e, RETURN_MASK_ALL)
2608 {
2609 bp_err = e.error;
2610 bp_err_message = e.message;
2611 }
2612 END_CATCH
2613 }
2614 else
2615 {
2616 /* This breakpoint is in an overlay section.
2617 Shall we set a breakpoint at the LMA? */
2618 if (!overlay_events_enabled)
2619 {
2620 /* Yes -- overlay event support is not active,
2621 so we must try to set a breakpoint at the LMA.
2622 This will not work for a hardware breakpoint. */
2623 if (bl->loc_type == bp_loc_hardware_breakpoint)
2624 warning (_("hardware breakpoint %d not supported in overlay!"),
2625 bl->owner->number);
2626 else
2627 {
2628 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2629 bl->section);
2630 /* Set a software (trap) breakpoint at the LMA. */
2631 bl->overlay_target_info = bl->target_info;
2632 bl->overlay_target_info.reqstd_address = addr;
2633
2634 /* No overlay handling: just set the breakpoint. */
2635 TRY
2636 {
2637 int val;
2638
2639 bl->overlay_target_info.kind
2640 = breakpoint_kind (bl, &addr);
2641 bl->overlay_target_info.placed_address = addr;
2642 val = target_insert_breakpoint (bl->gdbarch,
2643 &bl->overlay_target_info);
2644 if (val)
2645 bp_err = GENERIC_ERROR;
2646 }
2647 CATCH (e, RETURN_MASK_ALL)
2648 {
2649 bp_err = e.error;
2650 bp_err_message = e.message;
2651 }
2652 END_CATCH
2653
2654 if (bp_err != GDB_NO_ERROR)
2655 fprintf_unfiltered (tmp_error_stream,
2656 "Overlay breakpoint %d "
2657 "failed: in ROM?\n",
2658 bl->owner->number);
2659 }
2660 }
2661 /* Shall we set a breakpoint at the VMA? */
2662 if (section_is_mapped (bl->section))
2663 {
2664 /* Yes. This overlay section is mapped into memory. */
2665 TRY
2666 {
2667 int val;
2668
2669 val = bl->owner->ops->insert_location (bl);
2670 if (val)
2671 bp_err = GENERIC_ERROR;
2672 }
2673 CATCH (e, RETURN_MASK_ALL)
2674 {
2675 bp_err = e.error;
2676 bp_err_message = e.message;
2677 }
2678 END_CATCH
2679 }
2680 else
2681 {
2682 /* No. This breakpoint will not be inserted.
2683 No error, but do not mark the bp as 'inserted'. */
2684 return 0;
2685 }
2686 }
2687
2688 if (bp_err != GDB_NO_ERROR)
2689 {
2690 /* Can't set the breakpoint. */
2691
2692 /* In some cases, we might not be able to insert a
2693 breakpoint in a shared library that has already been
2694 removed, but we have not yet processed the shlib unload
2695 event. Unfortunately, some targets that implement
2696 breakpoint insertion themselves can't tell why the
2697 breakpoint insertion failed (e.g., the remote target
2698 doesn't define error codes), so we must treat generic
2699 errors as memory errors. */
2700 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2701 && bl->loc_type == bp_loc_software_breakpoint
2702 && (solib_name_from_address (bl->pspace, bl->address)
2703 || shared_objfile_contains_address_p (bl->pspace,
2704 bl->address)))
2705 {
2706 /* See also: disable_breakpoints_in_shlibs. */
2707 bl->shlib_disabled = 1;
2708 observer_notify_breakpoint_modified (bl->owner);
2709 if (!*disabled_breaks)
2710 {
2711 fprintf_unfiltered (tmp_error_stream,
2712 "Cannot insert breakpoint %d.\n",
2713 bl->owner->number);
2714 fprintf_unfiltered (tmp_error_stream,
2715 "Temporarily disabling shared "
2716 "library breakpoints:\n");
2717 }
2718 *disabled_breaks = 1;
2719 fprintf_unfiltered (tmp_error_stream,
2720 "breakpoint #%d\n", bl->owner->number);
2721 return 0;
2722 }
2723 else
2724 {
2725 if (bl->loc_type == bp_loc_hardware_breakpoint)
2726 {
2727 *hw_breakpoint_error = 1;
2728 *hw_bp_error_explained_already = bp_err_message != NULL;
2729 fprintf_unfiltered (tmp_error_stream,
2730 "Cannot insert hardware breakpoint %d%s",
2731 bl->owner->number, bp_err_message ? ":" : ".\n");
2732 if (bp_err_message != NULL)
2733 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2734 }
2735 else
2736 {
2737 if (bp_err_message == NULL)
2738 {
2739 std::string message
2740 = memory_error_message (TARGET_XFER_E_IO,
2741 bl->gdbarch, bl->address);
2742
2743 fprintf_unfiltered (tmp_error_stream,
2744 "Cannot insert breakpoint %d.\n"
2745 "%s\n",
2746 bl->owner->number, message.c_str ());
2747 }
2748 else
2749 {
2750 fprintf_unfiltered (tmp_error_stream,
2751 "Cannot insert breakpoint %d: %s\n",
2752 bl->owner->number,
2753 bp_err_message);
2754 }
2755 }
2756 return 1;
2757
2758 }
2759 }
2760 else
2761 bl->inserted = 1;
2762
2763 return 0;
2764 }
2765
2766 else if (bl->loc_type == bp_loc_hardware_watchpoint
2767 /* NOTE drow/2003-09-08: This state only exists for removing
2768 watchpoints. It's not clear that it's necessary... */
2769 && bl->owner->disposition != disp_del_at_next_stop)
2770 {
2771 int val;
2772
2773 gdb_assert (bl->owner->ops != NULL
2774 && bl->owner->ops->insert_location != NULL);
2775
2776 val = bl->owner->ops->insert_location (bl);
2777
2778 /* If trying to set a read-watchpoint, and it turns out it's not
2779 supported, try emulating one with an access watchpoint. */
2780 if (val == 1 && bl->watchpoint_type == hw_read)
2781 {
2782 struct bp_location *loc, **loc_temp;
2783
2784 /* But don't try to insert it, if there's already another
2785 hw_access location that would be considered a duplicate
2786 of this one. */
2787 ALL_BP_LOCATIONS (loc, loc_temp)
2788 if (loc != bl
2789 && loc->watchpoint_type == hw_access
2790 && watchpoint_locations_match (bl, loc))
2791 {
2792 bl->duplicate = 1;
2793 bl->inserted = 1;
2794 bl->target_info = loc->target_info;
2795 bl->watchpoint_type = hw_access;
2796 val = 0;
2797 break;
2798 }
2799
2800 if (val == 1)
2801 {
2802 bl->watchpoint_type = hw_access;
2803 val = bl->owner->ops->insert_location (bl);
2804
2805 if (val)
2806 /* Back to the original value. */
2807 bl->watchpoint_type = hw_read;
2808 }
2809 }
2810
2811 bl->inserted = (val == 0);
2812 }
2813
2814 else if (bl->owner->type == bp_catchpoint)
2815 {
2816 int val;
2817
2818 gdb_assert (bl->owner->ops != NULL
2819 && bl->owner->ops->insert_location != NULL);
2820
2821 val = bl->owner->ops->insert_location (bl);
2822 if (val)
2823 {
2824 bl->owner->enable_state = bp_disabled;
2825
2826 if (val == 1)
2827 warning (_("\
2828 Error inserting catchpoint %d: Your system does not support this type\n\
2829 of catchpoint."), bl->owner->number);
2830 else
2831 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2832 }
2833
2834 bl->inserted = (val == 0);
2835
2836 /* We've already printed an error message if there was a problem
2837 inserting this catchpoint, and we've disabled the catchpoint,
2838 so just return success. */
2839 return 0;
2840 }
2841
2842 return 0;
2843 }
2844
2845 /* This function is called when program space PSPACE is about to be
2846 deleted. It takes care of updating breakpoints to not reference
2847 PSPACE anymore. */
2848
2849 void
2850 breakpoint_program_space_exit (struct program_space *pspace)
2851 {
2852 struct breakpoint *b, *b_temp;
2853 struct bp_location *loc, **loc_temp;
2854
2855 /* Remove any breakpoint that was set through this program space. */
2856 ALL_BREAKPOINTS_SAFE (b, b_temp)
2857 {
2858 if (b->pspace == pspace)
2859 delete_breakpoint (b);
2860 }
2861
2862 /* Breakpoints set through other program spaces could have locations
2863 bound to PSPACE as well. Remove those. */
2864 ALL_BP_LOCATIONS (loc, loc_temp)
2865 {
2866 struct bp_location *tmp;
2867
2868 if (loc->pspace == pspace)
2869 {
2870 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2871 if (loc->owner->loc == loc)
2872 loc->owner->loc = loc->next;
2873 else
2874 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2875 if (tmp->next == loc)
2876 {
2877 tmp->next = loc->next;
2878 break;
2879 }
2880 }
2881 }
2882
2883 /* Now update the global location list to permanently delete the
2884 removed locations above. */
2885 update_global_location_list (UGLL_DONT_INSERT);
2886 }
2887
2888 /* Make sure all breakpoints are inserted in inferior.
2889 Throws exception on any error.
2890 A breakpoint that is already inserted won't be inserted
2891 again, so calling this function twice is safe. */
2892 void
2893 insert_breakpoints (void)
2894 {
2895 struct breakpoint *bpt;
2896
2897 ALL_BREAKPOINTS (bpt)
2898 if (is_hardware_watchpoint (bpt))
2899 {
2900 struct watchpoint *w = (struct watchpoint *) bpt;
2901
2902 update_watchpoint (w, 0 /* don't reparse. */);
2903 }
2904
2905 /* Updating watchpoints creates new locations, so update the global
2906 location list. Explicitly tell ugll to insert locations and
2907 ignore breakpoints_always_inserted_mode. */
2908 update_global_location_list (UGLL_INSERT);
2909 }
2910
2911 /* Invoke CALLBACK for each of bp_location. */
2912
2913 void
2914 iterate_over_bp_locations (walk_bp_location_callback callback)
2915 {
2916 struct bp_location *loc, **loc_tmp;
2917
2918 ALL_BP_LOCATIONS (loc, loc_tmp)
2919 {
2920 callback (loc, NULL);
2921 }
2922 }
2923
2924 /* This is used when we need to synch breakpoint conditions between GDB and the
2925 target. It is the case with deleting and disabling of breakpoints when using
2926 always-inserted mode. */
2927
2928 static void
2929 update_inserted_breakpoint_locations (void)
2930 {
2931 struct bp_location *bl, **blp_tmp;
2932 int error_flag = 0;
2933 int val = 0;
2934 int disabled_breaks = 0;
2935 int hw_breakpoint_error = 0;
2936 int hw_bp_details_reported = 0;
2937
2938 string_file tmp_error_stream;
2939
2940 /* Explicitly mark the warning -- this will only be printed if
2941 there was an error. */
2942 tmp_error_stream.puts ("Warning:\n");
2943
2944 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2945
2946 ALL_BP_LOCATIONS (bl, blp_tmp)
2947 {
2948 /* We only want to update software breakpoints and hardware
2949 breakpoints. */
2950 if (!is_breakpoint (bl->owner))
2951 continue;
2952
2953 /* We only want to update locations that are already inserted
2954 and need updating. This is to avoid unwanted insertion during
2955 deletion of breakpoints. */
2956 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2957 continue;
2958
2959 switch_to_program_space_and_thread (bl->pspace);
2960
2961 /* For targets that support global breakpoints, there's no need
2962 to select an inferior to insert breakpoint to. In fact, even
2963 if we aren't attached to any process yet, we should still
2964 insert breakpoints. */
2965 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2966 && ptid_equal (inferior_ptid, null_ptid))
2967 continue;
2968
2969 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2970 &hw_breakpoint_error, &hw_bp_details_reported);
2971 if (val)
2972 error_flag = val;
2973 }
2974
2975 if (error_flag)
2976 {
2977 target_terminal::ours_for_output ();
2978 error_stream (tmp_error_stream);
2979 }
2980 }
2981
2982 /* Used when starting or continuing the program. */
2983
2984 static void
2985 insert_breakpoint_locations (void)
2986 {
2987 struct breakpoint *bpt;
2988 struct bp_location *bl, **blp_tmp;
2989 int error_flag = 0;
2990 int val = 0;
2991 int disabled_breaks = 0;
2992 int hw_breakpoint_error = 0;
2993 int hw_bp_error_explained_already = 0;
2994
2995 string_file tmp_error_stream;
2996
2997 /* Explicitly mark the warning -- this will only be printed if
2998 there was an error. */
2999 tmp_error_stream.puts ("Warning:\n");
3000
3001 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3002
3003 ALL_BP_LOCATIONS (bl, blp_tmp)
3004 {
3005 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3006 continue;
3007
3008 /* There is no point inserting thread-specific breakpoints if
3009 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3010 has BL->OWNER always non-NULL. */
3011 if (bl->owner->thread != -1
3012 && !valid_global_thread_id (bl->owner->thread))
3013 continue;
3014
3015 switch_to_program_space_and_thread (bl->pspace);
3016
3017 /* For targets that support global breakpoints, there's no need
3018 to select an inferior to insert breakpoint to. In fact, even
3019 if we aren't attached to any process yet, we should still
3020 insert breakpoints. */
3021 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3022 && ptid_equal (inferior_ptid, null_ptid))
3023 continue;
3024
3025 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3026 &hw_breakpoint_error, &hw_bp_error_explained_already);
3027 if (val)
3028 error_flag = val;
3029 }
3030
3031 /* If we failed to insert all locations of a watchpoint, remove
3032 them, as half-inserted watchpoint is of limited use. */
3033 ALL_BREAKPOINTS (bpt)
3034 {
3035 int some_failed = 0;
3036 struct bp_location *loc;
3037
3038 if (!is_hardware_watchpoint (bpt))
3039 continue;
3040
3041 if (!breakpoint_enabled (bpt))
3042 continue;
3043
3044 if (bpt->disposition == disp_del_at_next_stop)
3045 continue;
3046
3047 for (loc = bpt->loc; loc; loc = loc->next)
3048 if (!loc->inserted && should_be_inserted (loc))
3049 {
3050 some_failed = 1;
3051 break;
3052 }
3053 if (some_failed)
3054 {
3055 for (loc = bpt->loc; loc; loc = loc->next)
3056 if (loc->inserted)
3057 remove_breakpoint (loc);
3058
3059 hw_breakpoint_error = 1;
3060 tmp_error_stream.printf ("Could not insert "
3061 "hardware watchpoint %d.\n",
3062 bpt->number);
3063 error_flag = -1;
3064 }
3065 }
3066
3067 if (error_flag)
3068 {
3069 /* If a hardware breakpoint or watchpoint was inserted, add a
3070 message about possibly exhausted resources. */
3071 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3072 {
3073 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3074 You may have requested too many hardware breakpoints/watchpoints.\n");
3075 }
3076 target_terminal::ours_for_output ();
3077 error_stream (tmp_error_stream);
3078 }
3079 }
3080
3081 /* Used when the program stops.
3082 Returns zero if successful, or non-zero if there was a problem
3083 removing a breakpoint location. */
3084
3085 int
3086 remove_breakpoints (void)
3087 {
3088 struct bp_location *bl, **blp_tmp;
3089 int val = 0;
3090
3091 ALL_BP_LOCATIONS (bl, blp_tmp)
3092 {
3093 if (bl->inserted && !is_tracepoint (bl->owner))
3094 val |= remove_breakpoint (bl);
3095 }
3096 return val;
3097 }
3098
3099 /* When a thread exits, remove breakpoints that are related to
3100 that thread. */
3101
3102 static void
3103 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3104 {
3105 struct breakpoint *b, *b_tmp;
3106
3107 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3108 {
3109 if (b->thread == tp->global_num && user_breakpoint_p (b))
3110 {
3111 b->disposition = disp_del_at_next_stop;
3112
3113 printf_filtered (_("\
3114 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3115 b->number, print_thread_id (tp));
3116
3117 /* Hide it from the user. */
3118 b->number = 0;
3119 }
3120 }
3121 }
3122
3123 /* Remove breakpoints of process PID. */
3124
3125 int
3126 remove_breakpoints_pid (int pid)
3127 {
3128 struct bp_location *bl, **blp_tmp;
3129 int val;
3130 struct inferior *inf = find_inferior_pid (pid);
3131
3132 ALL_BP_LOCATIONS (bl, blp_tmp)
3133 {
3134 if (bl->pspace != inf->pspace)
3135 continue;
3136
3137 if (bl->inserted && !bl->target_info.persist)
3138 {
3139 val = remove_breakpoint (bl);
3140 if (val != 0)
3141 return val;
3142 }
3143 }
3144 return 0;
3145 }
3146
3147 static int internal_breakpoint_number = -1;
3148
3149 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3150 If INTERNAL is non-zero, the breakpoint number will be populated
3151 from internal_breakpoint_number and that variable decremented.
3152 Otherwise the breakpoint number will be populated from
3153 breakpoint_count and that value incremented. Internal breakpoints
3154 do not set the internal var bpnum. */
3155 static void
3156 set_breakpoint_number (int internal, struct breakpoint *b)
3157 {
3158 if (internal)
3159 b->number = internal_breakpoint_number--;
3160 else
3161 {
3162 set_breakpoint_count (breakpoint_count + 1);
3163 b->number = breakpoint_count;
3164 }
3165 }
3166
3167 static struct breakpoint *
3168 create_internal_breakpoint (struct gdbarch *gdbarch,
3169 CORE_ADDR address, enum bptype type,
3170 const struct breakpoint_ops *ops)
3171 {
3172 symtab_and_line sal;
3173 sal.pc = address;
3174 sal.section = find_pc_overlay (sal.pc);
3175 sal.pspace = current_program_space;
3176
3177 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3178 b->number = internal_breakpoint_number--;
3179 b->disposition = disp_donttouch;
3180
3181 return b;
3182 }
3183
3184 static const char *const longjmp_names[] =
3185 {
3186 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3187 };
3188 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3189
3190 /* Per-objfile data private to breakpoint.c. */
3191 struct breakpoint_objfile_data
3192 {
3193 /* Minimal symbol for "_ovly_debug_event" (if any). */
3194 struct bound_minimal_symbol overlay_msym;
3195
3196 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3197 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3198
3199 /* True if we have looked for longjmp probes. */
3200 int longjmp_searched;
3201
3202 /* SystemTap probe points for longjmp (if any). */
3203 VEC (probe_p) *longjmp_probes;
3204
3205 /* Minimal symbol for "std::terminate()" (if any). */
3206 struct bound_minimal_symbol terminate_msym;
3207
3208 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3209 struct bound_minimal_symbol exception_msym;
3210
3211 /* True if we have looked for exception probes. */
3212 int exception_searched;
3213
3214 /* SystemTap probe points for unwinding (if any). */
3215 VEC (probe_p) *exception_probes;
3216 };
3217
3218 static const struct objfile_data *breakpoint_objfile_key;
3219
3220 /* Minimal symbol not found sentinel. */
3221 static struct minimal_symbol msym_not_found;
3222
3223 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3224
3225 static int
3226 msym_not_found_p (const struct minimal_symbol *msym)
3227 {
3228 return msym == &msym_not_found;
3229 }
3230
3231 /* Return per-objfile data needed by breakpoint.c.
3232 Allocate the data if necessary. */
3233
3234 static struct breakpoint_objfile_data *
3235 get_breakpoint_objfile_data (struct objfile *objfile)
3236 {
3237 struct breakpoint_objfile_data *bp_objfile_data;
3238
3239 bp_objfile_data = ((struct breakpoint_objfile_data *)
3240 objfile_data (objfile, breakpoint_objfile_key));
3241 if (bp_objfile_data == NULL)
3242 {
3243 bp_objfile_data =
3244 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3245
3246 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3247 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3248 }
3249 return bp_objfile_data;
3250 }
3251
3252 static void
3253 free_breakpoint_probes (struct objfile *obj, void *data)
3254 {
3255 struct breakpoint_objfile_data *bp_objfile_data
3256 = (struct breakpoint_objfile_data *) data;
3257
3258 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3259 VEC_free (probe_p, bp_objfile_data->exception_probes);
3260 }
3261
3262 static void
3263 create_overlay_event_breakpoint (void)
3264 {
3265 struct objfile *objfile;
3266 const char *const func_name = "_ovly_debug_event";
3267
3268 ALL_OBJFILES (objfile)
3269 {
3270 struct breakpoint *b;
3271 struct breakpoint_objfile_data *bp_objfile_data;
3272 CORE_ADDR addr;
3273 struct explicit_location explicit_loc;
3274
3275 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3276
3277 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3278 continue;
3279
3280 if (bp_objfile_data->overlay_msym.minsym == NULL)
3281 {
3282 struct bound_minimal_symbol m;
3283
3284 m = lookup_minimal_symbol_text (func_name, objfile);
3285 if (m.minsym == NULL)
3286 {
3287 /* Avoid future lookups in this objfile. */
3288 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3289 continue;
3290 }
3291 bp_objfile_data->overlay_msym = m;
3292 }
3293
3294 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3295 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3296 bp_overlay_event,
3297 &internal_breakpoint_ops);
3298 initialize_explicit_location (&explicit_loc);
3299 explicit_loc.function_name = ASTRDUP (func_name);
3300 b->location = new_explicit_location (&explicit_loc);
3301
3302 if (overlay_debugging == ovly_auto)
3303 {
3304 b->enable_state = bp_enabled;
3305 overlay_events_enabled = 1;
3306 }
3307 else
3308 {
3309 b->enable_state = bp_disabled;
3310 overlay_events_enabled = 0;
3311 }
3312 }
3313 }
3314
3315 static void
3316 create_longjmp_master_breakpoint (void)
3317 {
3318 struct program_space *pspace;
3319
3320 scoped_restore_current_program_space restore_pspace;
3321
3322 ALL_PSPACES (pspace)
3323 {
3324 struct objfile *objfile;
3325
3326 set_current_program_space (pspace);
3327
3328 ALL_OBJFILES (objfile)
3329 {
3330 int i;
3331 struct gdbarch *gdbarch;
3332 struct breakpoint_objfile_data *bp_objfile_data;
3333
3334 gdbarch = get_objfile_arch (objfile);
3335
3336 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3337
3338 if (!bp_objfile_data->longjmp_searched)
3339 {
3340 VEC (probe_p) *ret;
3341
3342 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3343 if (ret != NULL)
3344 {
3345 /* We are only interested in checking one element. */
3346 struct probe *p = VEC_index (probe_p, ret, 0);
3347
3348 if (!can_evaluate_probe_arguments (p))
3349 {
3350 /* We cannot use the probe interface here, because it does
3351 not know how to evaluate arguments. */
3352 VEC_free (probe_p, ret);
3353 ret = NULL;
3354 }
3355 }
3356 bp_objfile_data->longjmp_probes = ret;
3357 bp_objfile_data->longjmp_searched = 1;
3358 }
3359
3360 if (bp_objfile_data->longjmp_probes != NULL)
3361 {
3362 int i;
3363 struct probe *probe;
3364 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3365
3366 for (i = 0;
3367 VEC_iterate (probe_p,
3368 bp_objfile_data->longjmp_probes,
3369 i, probe);
3370 ++i)
3371 {
3372 struct breakpoint *b;
3373
3374 b = create_internal_breakpoint (gdbarch,
3375 get_probe_address (probe,
3376 objfile),
3377 bp_longjmp_master,
3378 &internal_breakpoint_ops);
3379 b->location = new_probe_location ("-probe-stap libc:longjmp");
3380 b->enable_state = bp_disabled;
3381 }
3382
3383 continue;
3384 }
3385
3386 if (!gdbarch_get_longjmp_target_p (gdbarch))
3387 continue;
3388
3389 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3390 {
3391 struct breakpoint *b;
3392 const char *func_name;
3393 CORE_ADDR addr;
3394 struct explicit_location explicit_loc;
3395
3396 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3397 continue;
3398
3399 func_name = longjmp_names[i];
3400 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3401 {
3402 struct bound_minimal_symbol m;
3403
3404 m = lookup_minimal_symbol_text (func_name, objfile);
3405 if (m.minsym == NULL)
3406 {
3407 /* Prevent future lookups in this objfile. */
3408 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3409 continue;
3410 }
3411 bp_objfile_data->longjmp_msym[i] = m;
3412 }
3413
3414 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3415 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3416 &internal_breakpoint_ops);
3417 initialize_explicit_location (&explicit_loc);
3418 explicit_loc.function_name = ASTRDUP (func_name);
3419 b->location = new_explicit_location (&explicit_loc);
3420 b->enable_state = bp_disabled;
3421 }
3422 }
3423 }
3424 }
3425
3426 /* Create a master std::terminate breakpoint. */
3427 static void
3428 create_std_terminate_master_breakpoint (void)
3429 {
3430 struct program_space *pspace;
3431 const char *const func_name = "std::terminate()";
3432
3433 scoped_restore_current_program_space restore_pspace;
3434
3435 ALL_PSPACES (pspace)
3436 {
3437 struct objfile *objfile;
3438 CORE_ADDR addr;
3439
3440 set_current_program_space (pspace);
3441
3442 ALL_OBJFILES (objfile)
3443 {
3444 struct breakpoint *b;
3445 struct breakpoint_objfile_data *bp_objfile_data;
3446 struct explicit_location explicit_loc;
3447
3448 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3449
3450 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3451 continue;
3452
3453 if (bp_objfile_data->terminate_msym.minsym == NULL)
3454 {
3455 struct bound_minimal_symbol m;
3456
3457 m = lookup_minimal_symbol (func_name, NULL, objfile);
3458 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3459 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3460 {
3461 /* Prevent future lookups in this objfile. */
3462 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3463 continue;
3464 }
3465 bp_objfile_data->terminate_msym = m;
3466 }
3467
3468 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3469 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3470 bp_std_terminate_master,
3471 &internal_breakpoint_ops);
3472 initialize_explicit_location (&explicit_loc);
3473 explicit_loc.function_name = ASTRDUP (func_name);
3474 b->location = new_explicit_location (&explicit_loc);
3475 b->enable_state = bp_disabled;
3476 }
3477 }
3478 }
3479
3480 /* Install a master breakpoint on the unwinder's debug hook. */
3481
3482 static void
3483 create_exception_master_breakpoint (void)
3484 {
3485 struct objfile *objfile;
3486 const char *const func_name = "_Unwind_DebugHook";
3487
3488 ALL_OBJFILES (objfile)
3489 {
3490 struct breakpoint *b;
3491 struct gdbarch *gdbarch;
3492 struct breakpoint_objfile_data *bp_objfile_data;
3493 CORE_ADDR addr;
3494 struct explicit_location explicit_loc;
3495
3496 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3497
3498 /* We prefer the SystemTap probe point if it exists. */
3499 if (!bp_objfile_data->exception_searched)
3500 {
3501 VEC (probe_p) *ret;
3502
3503 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3504
3505 if (ret != NULL)
3506 {
3507 /* We are only interested in checking one element. */
3508 struct probe *p = VEC_index (probe_p, ret, 0);
3509
3510 if (!can_evaluate_probe_arguments (p))
3511 {
3512 /* We cannot use the probe interface here, because it does
3513 not know how to evaluate arguments. */
3514 VEC_free (probe_p, ret);
3515 ret = NULL;
3516 }
3517 }
3518 bp_objfile_data->exception_probes = ret;
3519 bp_objfile_data->exception_searched = 1;
3520 }
3521
3522 if (bp_objfile_data->exception_probes != NULL)
3523 {
3524 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3525 int i;
3526 struct probe *probe;
3527
3528 for (i = 0;
3529 VEC_iterate (probe_p,
3530 bp_objfile_data->exception_probes,
3531 i, probe);
3532 ++i)
3533 {
3534 struct breakpoint *b;
3535
3536 b = create_internal_breakpoint (gdbarch,
3537 get_probe_address (probe,
3538 objfile),
3539 bp_exception_master,
3540 &internal_breakpoint_ops);
3541 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3542 b->enable_state = bp_disabled;
3543 }
3544
3545 continue;
3546 }
3547
3548 /* Otherwise, try the hook function. */
3549
3550 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3551 continue;
3552
3553 gdbarch = get_objfile_arch (objfile);
3554
3555 if (bp_objfile_data->exception_msym.minsym == NULL)
3556 {
3557 struct bound_minimal_symbol debug_hook;
3558
3559 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3560 if (debug_hook.minsym == NULL)
3561 {
3562 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3563 continue;
3564 }
3565
3566 bp_objfile_data->exception_msym = debug_hook;
3567 }
3568
3569 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3570 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3571 &current_target);
3572 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3573 &internal_breakpoint_ops);
3574 initialize_explicit_location (&explicit_loc);
3575 explicit_loc.function_name = ASTRDUP (func_name);
3576 b->location = new_explicit_location (&explicit_loc);
3577 b->enable_state = bp_disabled;
3578 }
3579 }
3580
3581 /* Does B have a location spec? */
3582
3583 static int
3584 breakpoint_event_location_empty_p (const struct breakpoint *b)
3585 {
3586 return b->location != NULL && event_location_empty_p (b->location.get ());
3587 }
3588
3589 void
3590 update_breakpoints_after_exec (void)
3591 {
3592 struct breakpoint *b, *b_tmp;
3593 struct bp_location *bploc, **bplocp_tmp;
3594
3595 /* We're about to delete breakpoints from GDB's lists. If the
3596 INSERTED flag is true, GDB will try to lift the breakpoints by
3597 writing the breakpoints' "shadow contents" back into memory. The
3598 "shadow contents" are NOT valid after an exec, so GDB should not
3599 do that. Instead, the target is responsible from marking
3600 breakpoints out as soon as it detects an exec. We don't do that
3601 here instead, because there may be other attempts to delete
3602 breakpoints after detecting an exec and before reaching here. */
3603 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3604 if (bploc->pspace == current_program_space)
3605 gdb_assert (!bploc->inserted);
3606
3607 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3608 {
3609 if (b->pspace != current_program_space)
3610 continue;
3611
3612 /* Solib breakpoints must be explicitly reset after an exec(). */
3613 if (b->type == bp_shlib_event)
3614 {
3615 delete_breakpoint (b);
3616 continue;
3617 }
3618
3619 /* JIT breakpoints must be explicitly reset after an exec(). */
3620 if (b->type == bp_jit_event)
3621 {
3622 delete_breakpoint (b);
3623 continue;
3624 }
3625
3626 /* Thread event breakpoints must be set anew after an exec(),
3627 as must overlay event and longjmp master breakpoints. */
3628 if (b->type == bp_thread_event || b->type == bp_overlay_event
3629 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3630 || b->type == bp_exception_master)
3631 {
3632 delete_breakpoint (b);
3633 continue;
3634 }
3635
3636 /* Step-resume breakpoints are meaningless after an exec(). */
3637 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3638 {
3639 delete_breakpoint (b);
3640 continue;
3641 }
3642
3643 /* Just like single-step breakpoints. */
3644 if (b->type == bp_single_step)
3645 {
3646 delete_breakpoint (b);
3647 continue;
3648 }
3649
3650 /* Longjmp and longjmp-resume breakpoints are also meaningless
3651 after an exec. */
3652 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3653 || b->type == bp_longjmp_call_dummy
3654 || b->type == bp_exception || b->type == bp_exception_resume)
3655 {
3656 delete_breakpoint (b);
3657 continue;
3658 }
3659
3660 if (b->type == bp_catchpoint)
3661 {
3662 /* For now, none of the bp_catchpoint breakpoints need to
3663 do anything at this point. In the future, if some of
3664 the catchpoints need to something, we will need to add
3665 a new method, and call this method from here. */
3666 continue;
3667 }
3668
3669 /* bp_finish is a special case. The only way we ought to be able
3670 to see one of these when an exec() has happened, is if the user
3671 caught a vfork, and then said "finish". Ordinarily a finish just
3672 carries them to the call-site of the current callee, by setting
3673 a temporary bp there and resuming. But in this case, the finish
3674 will carry them entirely through the vfork & exec.
3675
3676 We don't want to allow a bp_finish to remain inserted now. But
3677 we can't safely delete it, 'cause finish_command has a handle to
3678 the bp on a bpstat, and will later want to delete it. There's a
3679 chance (and I've seen it happen) that if we delete the bp_finish
3680 here, that its storage will get reused by the time finish_command
3681 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3682 We really must allow finish_command to delete a bp_finish.
3683
3684 In the absence of a general solution for the "how do we know
3685 it's safe to delete something others may have handles to?"
3686 problem, what we'll do here is just uninsert the bp_finish, and
3687 let finish_command delete it.
3688
3689 (We know the bp_finish is "doomed" in the sense that it's
3690 momentary, and will be deleted as soon as finish_command sees
3691 the inferior stopped. So it doesn't matter that the bp's
3692 address is probably bogus in the new a.out, unlike e.g., the
3693 solib breakpoints.) */
3694
3695 if (b->type == bp_finish)
3696 {
3697 continue;
3698 }
3699
3700 /* Without a symbolic address, we have little hope of the
3701 pre-exec() address meaning the same thing in the post-exec()
3702 a.out. */
3703 if (breakpoint_event_location_empty_p (b))
3704 {
3705 delete_breakpoint (b);
3706 continue;
3707 }
3708 }
3709 }
3710
3711 int
3712 detach_breakpoints (ptid_t ptid)
3713 {
3714 struct bp_location *bl, **blp_tmp;
3715 int val = 0;
3716 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3717 struct inferior *inf = current_inferior ();
3718
3719 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3720 error (_("Cannot detach breakpoints of inferior_ptid"));
3721
3722 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3723 inferior_ptid = ptid;
3724 ALL_BP_LOCATIONS (bl, blp_tmp)
3725 {
3726 if (bl->pspace != inf->pspace)
3727 continue;
3728
3729 /* This function must physically remove breakpoints locations
3730 from the specified ptid, without modifying the breakpoint
3731 package's state. Locations of type bp_loc_other are only
3732 maintained at GDB side. So, there is no need to remove
3733 these bp_loc_other locations. Moreover, removing these
3734 would modify the breakpoint package's state. */
3735 if (bl->loc_type == bp_loc_other)
3736 continue;
3737
3738 if (bl->inserted)
3739 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3740 }
3741
3742 return val;
3743 }
3744
3745 /* Remove the breakpoint location BL from the current address space.
3746 Note that this is used to detach breakpoints from a child fork.
3747 When we get here, the child isn't in the inferior list, and neither
3748 do we have objects to represent its address space --- we should
3749 *not* look at bl->pspace->aspace here. */
3750
3751 static int
3752 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3753 {
3754 int val;
3755
3756 /* BL is never in moribund_locations by our callers. */
3757 gdb_assert (bl->owner != NULL);
3758
3759 /* The type of none suggests that owner is actually deleted.
3760 This should not ever happen. */
3761 gdb_assert (bl->owner->type != bp_none);
3762
3763 if (bl->loc_type == bp_loc_software_breakpoint
3764 || bl->loc_type == bp_loc_hardware_breakpoint)
3765 {
3766 /* "Normal" instruction breakpoint: either the standard
3767 trap-instruction bp (bp_breakpoint), or a
3768 bp_hardware_breakpoint. */
3769
3770 /* First check to see if we have to handle an overlay. */
3771 if (overlay_debugging == ovly_off
3772 || bl->section == NULL
3773 || !(section_is_overlay (bl->section)))
3774 {
3775 /* No overlay handling: just remove the breakpoint. */
3776
3777 /* If we're trying to uninsert a memory breakpoint that we
3778 know is set in a dynamic object that is marked
3779 shlib_disabled, then either the dynamic object was
3780 removed with "remove-symbol-file" or with
3781 "nosharedlibrary". In the former case, we don't know
3782 whether another dynamic object might have loaded over the
3783 breakpoint's address -- the user might well let us know
3784 about it next with add-symbol-file (the whole point of
3785 add-symbol-file is letting the user manually maintain a
3786 list of dynamically loaded objects). If we have the
3787 breakpoint's shadow memory, that is, this is a software
3788 breakpoint managed by GDB, check whether the breakpoint
3789 is still inserted in memory, to avoid overwriting wrong
3790 code with stale saved shadow contents. Note that HW
3791 breakpoints don't have shadow memory, as they're
3792 implemented using a mechanism that is not dependent on
3793 being able to modify the target's memory, and as such
3794 they should always be removed. */
3795 if (bl->shlib_disabled
3796 && bl->target_info.shadow_len != 0
3797 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3798 val = 0;
3799 else
3800 val = bl->owner->ops->remove_location (bl, reason);
3801 }
3802 else
3803 {
3804 /* This breakpoint is in an overlay section.
3805 Did we set a breakpoint at the LMA? */
3806 if (!overlay_events_enabled)
3807 {
3808 /* Yes -- overlay event support is not active, so we
3809 should have set a breakpoint at the LMA. Remove it.
3810 */
3811 /* Ignore any failures: if the LMA is in ROM, we will
3812 have already warned when we failed to insert it. */
3813 if (bl->loc_type == bp_loc_hardware_breakpoint)
3814 target_remove_hw_breakpoint (bl->gdbarch,
3815 &bl->overlay_target_info);
3816 else
3817 target_remove_breakpoint (bl->gdbarch,
3818 &bl->overlay_target_info,
3819 reason);
3820 }
3821 /* Did we set a breakpoint at the VMA?
3822 If so, we will have marked the breakpoint 'inserted'. */
3823 if (bl->inserted)
3824 {
3825 /* Yes -- remove it. Previously we did not bother to
3826 remove the breakpoint if the section had been
3827 unmapped, but let's not rely on that being safe. We
3828 don't know what the overlay manager might do. */
3829
3830 /* However, we should remove *software* breakpoints only
3831 if the section is still mapped, or else we overwrite
3832 wrong code with the saved shadow contents. */
3833 if (bl->loc_type == bp_loc_hardware_breakpoint
3834 || section_is_mapped (bl->section))
3835 val = bl->owner->ops->remove_location (bl, reason);
3836 else
3837 val = 0;
3838 }
3839 else
3840 {
3841 /* No -- not inserted, so no need to remove. No error. */
3842 val = 0;
3843 }
3844 }
3845
3846 /* In some cases, we might not be able to remove a breakpoint in
3847 a shared library that has already been removed, but we have
3848 not yet processed the shlib unload event. Similarly for an
3849 unloaded add-symbol-file object - the user might not yet have
3850 had the chance to remove-symbol-file it. shlib_disabled will
3851 be set if the library/object has already been removed, but
3852 the breakpoint hasn't been uninserted yet, e.g., after
3853 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3854 always-inserted mode. */
3855 if (val
3856 && (bl->loc_type == bp_loc_software_breakpoint
3857 && (bl->shlib_disabled
3858 || solib_name_from_address (bl->pspace, bl->address)
3859 || shared_objfile_contains_address_p (bl->pspace,
3860 bl->address))))
3861 val = 0;
3862
3863 if (val)
3864 return val;
3865 bl->inserted = (reason == DETACH_BREAKPOINT);
3866 }
3867 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3868 {
3869 gdb_assert (bl->owner->ops != NULL
3870 && bl->owner->ops->remove_location != NULL);
3871
3872 bl->inserted = (reason == DETACH_BREAKPOINT);
3873 bl->owner->ops->remove_location (bl, reason);
3874
3875 /* Failure to remove any of the hardware watchpoints comes here. */
3876 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3877 warning (_("Could not remove hardware watchpoint %d."),
3878 bl->owner->number);
3879 }
3880 else if (bl->owner->type == bp_catchpoint
3881 && breakpoint_enabled (bl->owner)
3882 && !bl->duplicate)
3883 {
3884 gdb_assert (bl->owner->ops != NULL
3885 && bl->owner->ops->remove_location != NULL);
3886
3887 val = bl->owner->ops->remove_location (bl, reason);
3888 if (val)
3889 return val;
3890
3891 bl->inserted = (reason == DETACH_BREAKPOINT);
3892 }
3893
3894 return 0;
3895 }
3896
3897 static int
3898 remove_breakpoint (struct bp_location *bl)
3899 {
3900 /* BL is never in moribund_locations by our callers. */
3901 gdb_assert (bl->owner != NULL);
3902
3903 /* The type of none suggests that owner is actually deleted.
3904 This should not ever happen. */
3905 gdb_assert (bl->owner->type != bp_none);
3906
3907 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3908
3909 switch_to_program_space_and_thread (bl->pspace);
3910
3911 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3912 }
3913
3914 /* Clear the "inserted" flag in all breakpoints. */
3915
3916 void
3917 mark_breakpoints_out (void)
3918 {
3919 struct bp_location *bl, **blp_tmp;
3920
3921 ALL_BP_LOCATIONS (bl, blp_tmp)
3922 if (bl->pspace == current_program_space)
3923 bl->inserted = 0;
3924 }
3925
3926 /* Clear the "inserted" flag in all breakpoints and delete any
3927 breakpoints which should go away between runs of the program.
3928
3929 Plus other such housekeeping that has to be done for breakpoints
3930 between runs.
3931
3932 Note: this function gets called at the end of a run (by
3933 generic_mourn_inferior) and when a run begins (by
3934 init_wait_for_inferior). */
3935
3936
3937
3938 void
3939 breakpoint_init_inferior (enum inf_context context)
3940 {
3941 struct breakpoint *b, *b_tmp;
3942 struct bp_location *bl;
3943 int ix;
3944 struct program_space *pspace = current_program_space;
3945
3946 /* If breakpoint locations are shared across processes, then there's
3947 nothing to do. */
3948 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3949 return;
3950
3951 mark_breakpoints_out ();
3952
3953 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3954 {
3955 if (b->loc && b->loc->pspace != pspace)
3956 continue;
3957
3958 switch (b->type)
3959 {
3960 case bp_call_dummy:
3961 case bp_longjmp_call_dummy:
3962
3963 /* If the call dummy breakpoint is at the entry point it will
3964 cause problems when the inferior is rerun, so we better get
3965 rid of it. */
3966
3967 case bp_watchpoint_scope:
3968
3969 /* Also get rid of scope breakpoints. */
3970
3971 case bp_shlib_event:
3972
3973 /* Also remove solib event breakpoints. Their addresses may
3974 have changed since the last time we ran the program.
3975 Actually we may now be debugging against different target;
3976 and so the solib backend that installed this breakpoint may
3977 not be used in by the target. E.g.,
3978
3979 (gdb) file prog-linux
3980 (gdb) run # native linux target
3981 ...
3982 (gdb) kill
3983 (gdb) file prog-win.exe
3984 (gdb) tar rem :9999 # remote Windows gdbserver.
3985 */
3986
3987 case bp_step_resume:
3988
3989 /* Also remove step-resume breakpoints. */
3990
3991 case bp_single_step:
3992
3993 /* Also remove single-step breakpoints. */
3994
3995 delete_breakpoint (b);
3996 break;
3997
3998 case bp_watchpoint:
3999 case bp_hardware_watchpoint:
4000 case bp_read_watchpoint:
4001 case bp_access_watchpoint:
4002 {
4003 struct watchpoint *w = (struct watchpoint *) b;
4004
4005 /* Likewise for watchpoints on local expressions. */
4006 if (w->exp_valid_block != NULL)
4007 delete_breakpoint (b);
4008 else
4009 {
4010 /* Get rid of existing locations, which are no longer
4011 valid. New ones will be created in
4012 update_watchpoint, when the inferior is restarted.
4013 The next update_global_location_list call will
4014 garbage collect them. */
4015 b->loc = NULL;
4016
4017 if (context == inf_starting)
4018 {
4019 /* Reset val field to force reread of starting value in
4020 insert_breakpoints. */
4021 if (w->val)
4022 value_free (w->val);
4023 w->val = NULL;
4024 w->val_valid = 0;
4025 }
4026 }
4027 }
4028 break;
4029 default:
4030 break;
4031 }
4032 }
4033
4034 /* Get rid of the moribund locations. */
4035 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4036 decref_bp_location (&bl);
4037 VEC_free (bp_location_p, moribund_locations);
4038 }
4039
4040 /* These functions concern about actual breakpoints inserted in the
4041 target --- to e.g. check if we need to do decr_pc adjustment or if
4042 we need to hop over the bkpt --- so we check for address space
4043 match, not program space. */
4044
4045 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4046 exists at PC. It returns ordinary_breakpoint_here if it's an
4047 ordinary breakpoint, or permanent_breakpoint_here if it's a
4048 permanent breakpoint.
4049 - When continuing from a location with an ordinary breakpoint, we
4050 actually single step once before calling insert_breakpoints.
4051 - When continuing from a location with a permanent breakpoint, we
4052 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4053 the target, to advance the PC past the breakpoint. */
4054
4055 enum breakpoint_here
4056 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4057 {
4058 struct bp_location *bl, **blp_tmp;
4059 int any_breakpoint_here = 0;
4060
4061 ALL_BP_LOCATIONS (bl, blp_tmp)
4062 {
4063 if (bl->loc_type != bp_loc_software_breakpoint
4064 && bl->loc_type != bp_loc_hardware_breakpoint)
4065 continue;
4066
4067 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4068 if ((breakpoint_enabled (bl->owner)
4069 || bl->permanent)
4070 && breakpoint_location_address_match (bl, aspace, pc))
4071 {
4072 if (overlay_debugging
4073 && section_is_overlay (bl->section)
4074 && !section_is_mapped (bl->section))
4075 continue; /* unmapped overlay -- can't be a match */
4076 else if (bl->permanent)
4077 return permanent_breakpoint_here;
4078 else
4079 any_breakpoint_here = 1;
4080 }
4081 }
4082
4083 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4084 }
4085
4086 /* See breakpoint.h. */
4087
4088 int
4089 breakpoint_in_range_p (struct address_space *aspace,
4090 CORE_ADDR addr, ULONGEST len)
4091 {
4092 struct bp_location *bl, **blp_tmp;
4093
4094 ALL_BP_LOCATIONS (bl, blp_tmp)
4095 {
4096 if (bl->loc_type != bp_loc_software_breakpoint
4097 && bl->loc_type != bp_loc_hardware_breakpoint)
4098 continue;
4099
4100 if ((breakpoint_enabled (bl->owner)
4101 || bl->permanent)
4102 && breakpoint_location_address_range_overlap (bl, aspace,
4103 addr, len))
4104 {
4105 if (overlay_debugging
4106 && section_is_overlay (bl->section)
4107 && !section_is_mapped (bl->section))
4108 {
4109 /* Unmapped overlay -- can't be a match. */
4110 continue;
4111 }
4112
4113 return 1;
4114 }
4115 }
4116
4117 return 0;
4118 }
4119
4120 /* Return true if there's a moribund breakpoint at PC. */
4121
4122 int
4123 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4124 {
4125 struct bp_location *loc;
4126 int ix;
4127
4128 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4129 if (breakpoint_location_address_match (loc, aspace, pc))
4130 return 1;
4131
4132 return 0;
4133 }
4134
4135 /* Returns non-zero iff BL is inserted at PC, in address space
4136 ASPACE. */
4137
4138 static int
4139 bp_location_inserted_here_p (struct bp_location *bl,
4140 struct address_space *aspace, CORE_ADDR pc)
4141 {
4142 if (bl->inserted
4143 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4144 aspace, pc))
4145 {
4146 if (overlay_debugging
4147 && section_is_overlay (bl->section)
4148 && !section_is_mapped (bl->section))
4149 return 0; /* unmapped overlay -- can't be a match */
4150 else
4151 return 1;
4152 }
4153 return 0;
4154 }
4155
4156 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4157
4158 int
4159 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4160 {
4161 struct bp_location **blp, **blp_tmp = NULL;
4162
4163 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4164 {
4165 struct bp_location *bl = *blp;
4166
4167 if (bl->loc_type != bp_loc_software_breakpoint
4168 && bl->loc_type != bp_loc_hardware_breakpoint)
4169 continue;
4170
4171 if (bp_location_inserted_here_p (bl, aspace, pc))
4172 return 1;
4173 }
4174 return 0;
4175 }
4176
4177 /* This function returns non-zero iff there is a software breakpoint
4178 inserted at PC. */
4179
4180 int
4181 software_breakpoint_inserted_here_p (struct address_space *aspace,
4182 CORE_ADDR pc)
4183 {
4184 struct bp_location **blp, **blp_tmp = NULL;
4185
4186 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4187 {
4188 struct bp_location *bl = *blp;
4189
4190 if (bl->loc_type != bp_loc_software_breakpoint)
4191 continue;
4192
4193 if (bp_location_inserted_here_p (bl, aspace, pc))
4194 return 1;
4195 }
4196
4197 return 0;
4198 }
4199
4200 /* See breakpoint.h. */
4201
4202 int
4203 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4204 CORE_ADDR pc)
4205 {
4206 struct bp_location **blp, **blp_tmp = NULL;
4207
4208 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4209 {
4210 struct bp_location *bl = *blp;
4211
4212 if (bl->loc_type != bp_loc_hardware_breakpoint)
4213 continue;
4214
4215 if (bp_location_inserted_here_p (bl, aspace, pc))
4216 return 1;
4217 }
4218
4219 return 0;
4220 }
4221
4222 int
4223 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4224 CORE_ADDR addr, ULONGEST len)
4225 {
4226 struct breakpoint *bpt;
4227
4228 ALL_BREAKPOINTS (bpt)
4229 {
4230 struct bp_location *loc;
4231
4232 if (bpt->type != bp_hardware_watchpoint
4233 && bpt->type != bp_access_watchpoint)
4234 continue;
4235
4236 if (!breakpoint_enabled (bpt))
4237 continue;
4238
4239 for (loc = bpt->loc; loc; loc = loc->next)
4240 if (loc->pspace->aspace == aspace && loc->inserted)
4241 {
4242 CORE_ADDR l, h;
4243
4244 /* Check for intersection. */
4245 l = std::max<CORE_ADDR> (loc->address, addr);
4246 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4247 if (l < h)
4248 return 1;
4249 }
4250 }
4251 return 0;
4252 }
4253 \f
4254
4255 /* bpstat stuff. External routines' interfaces are documented
4256 in breakpoint.h. */
4257
4258 int
4259 is_catchpoint (struct breakpoint *ep)
4260 {
4261 return (ep->type == bp_catchpoint);
4262 }
4263
4264 /* Frees any storage that is part of a bpstat. Does not walk the
4265 'next' chain. */
4266
4267 bpstats::~bpstats ()
4268 {
4269 if (old_val != NULL)
4270 value_free (old_val);
4271 if (bp_location_at != NULL)
4272 decref_bp_location (&bp_location_at);
4273 }
4274
4275 /* Clear a bpstat so that it says we are not at any breakpoint.
4276 Also free any storage that is part of a bpstat. */
4277
4278 void
4279 bpstat_clear (bpstat *bsp)
4280 {
4281 bpstat p;
4282 bpstat q;
4283
4284 if (bsp == 0)
4285 return;
4286 p = *bsp;
4287 while (p != NULL)
4288 {
4289 q = p->next;
4290 delete p;
4291 p = q;
4292 }
4293 *bsp = NULL;
4294 }
4295
4296 bpstats::bpstats (const bpstats &other)
4297 : next (NULL),
4298 bp_location_at (other.bp_location_at),
4299 breakpoint_at (other.breakpoint_at),
4300 commands (other.commands),
4301 old_val (other.old_val),
4302 print (other.print),
4303 stop (other.stop),
4304 print_it (other.print_it)
4305 {
4306 if (old_val != NULL)
4307 {
4308 old_val = value_copy (old_val);
4309 release_value (old_val);
4310 }
4311 incref_bp_location (bp_location_at);
4312 }
4313
4314 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4315 is part of the bpstat is copied as well. */
4316
4317 bpstat
4318 bpstat_copy (bpstat bs)
4319 {
4320 bpstat p = NULL;
4321 bpstat tmp;
4322 bpstat retval = NULL;
4323
4324 if (bs == NULL)
4325 return bs;
4326
4327 for (; bs != NULL; bs = bs->next)
4328 {
4329 tmp = new bpstats (*bs);
4330
4331 if (p == NULL)
4332 /* This is the first thing in the chain. */
4333 retval = tmp;
4334 else
4335 p->next = tmp;
4336 p = tmp;
4337 }
4338 p->next = NULL;
4339 return retval;
4340 }
4341
4342 /* Find the bpstat associated with this breakpoint. */
4343
4344 bpstat
4345 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4346 {
4347 if (bsp == NULL)
4348 return NULL;
4349
4350 for (; bsp != NULL; bsp = bsp->next)
4351 {
4352 if (bsp->breakpoint_at == breakpoint)
4353 return bsp;
4354 }
4355 return NULL;
4356 }
4357
4358 /* See breakpoint.h. */
4359
4360 int
4361 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4362 {
4363 for (; bsp != NULL; bsp = bsp->next)
4364 {
4365 if (bsp->breakpoint_at == NULL)
4366 {
4367 /* A moribund location can never explain a signal other than
4368 GDB_SIGNAL_TRAP. */
4369 if (sig == GDB_SIGNAL_TRAP)
4370 return 1;
4371 }
4372 else
4373 {
4374 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4375 sig))
4376 return 1;
4377 }
4378 }
4379
4380 return 0;
4381 }
4382
4383 /* Put in *NUM the breakpoint number of the first breakpoint we are
4384 stopped at. *BSP upon return is a bpstat which points to the
4385 remaining breakpoints stopped at (but which is not guaranteed to be
4386 good for anything but further calls to bpstat_num).
4387
4388 Return 0 if passed a bpstat which does not indicate any breakpoints.
4389 Return -1 if stopped at a breakpoint that has been deleted since
4390 we set it.
4391 Return 1 otherwise. */
4392
4393 int
4394 bpstat_num (bpstat *bsp, int *num)
4395 {
4396 struct breakpoint *b;
4397
4398 if ((*bsp) == NULL)
4399 return 0; /* No more breakpoint values */
4400
4401 /* We assume we'll never have several bpstats that correspond to a
4402 single breakpoint -- otherwise, this function might return the
4403 same number more than once and this will look ugly. */
4404 b = (*bsp)->breakpoint_at;
4405 *bsp = (*bsp)->next;
4406 if (b == NULL)
4407 return -1; /* breakpoint that's been deleted since */
4408
4409 *num = b->number; /* We have its number */
4410 return 1;
4411 }
4412
4413 /* See breakpoint.h. */
4414
4415 void
4416 bpstat_clear_actions (void)
4417 {
4418 struct thread_info *tp;
4419 bpstat bs;
4420
4421 if (ptid_equal (inferior_ptid, null_ptid))
4422 return;
4423
4424 tp = find_thread_ptid (inferior_ptid);
4425 if (tp == NULL)
4426 return;
4427
4428 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4429 {
4430 bs->commands = NULL;
4431
4432 if (bs->old_val != NULL)
4433 {
4434 value_free (bs->old_val);
4435 bs->old_val = NULL;
4436 }
4437 }
4438 }
4439
4440 /* Called when a command is about to proceed the inferior. */
4441
4442 static void
4443 breakpoint_about_to_proceed (void)
4444 {
4445 if (!ptid_equal (inferior_ptid, null_ptid))
4446 {
4447 struct thread_info *tp = inferior_thread ();
4448
4449 /* Allow inferior function calls in breakpoint commands to not
4450 interrupt the command list. When the call finishes
4451 successfully, the inferior will be standing at the same
4452 breakpoint as if nothing happened. */
4453 if (tp->control.in_infcall)
4454 return;
4455 }
4456
4457 breakpoint_proceeded = 1;
4458 }
4459
4460 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4461 or its equivalent. */
4462
4463 static int
4464 command_line_is_silent (struct command_line *cmd)
4465 {
4466 return cmd && (strcmp ("silent", cmd->line) == 0);
4467 }
4468
4469 /* Execute all the commands associated with all the breakpoints at
4470 this location. Any of these commands could cause the process to
4471 proceed beyond this point, etc. We look out for such changes by
4472 checking the global "breakpoint_proceeded" after each command.
4473
4474 Returns true if a breakpoint command resumed the inferior. In that
4475 case, it is the caller's responsibility to recall it again with the
4476 bpstat of the current thread. */
4477
4478 static int
4479 bpstat_do_actions_1 (bpstat *bsp)
4480 {
4481 bpstat bs;
4482 int again = 0;
4483
4484 /* Avoid endless recursion if a `source' command is contained
4485 in bs->commands. */
4486 if (executing_breakpoint_commands)
4487 return 0;
4488
4489 scoped_restore save_executing
4490 = make_scoped_restore (&executing_breakpoint_commands, 1);
4491
4492 scoped_restore preventer = prevent_dont_repeat ();
4493
4494 /* This pointer will iterate over the list of bpstat's. */
4495 bs = *bsp;
4496
4497 breakpoint_proceeded = 0;
4498 for (; bs != NULL; bs = bs->next)
4499 {
4500 struct command_line *cmd = NULL;
4501
4502 /* Take ownership of the BSP's command tree, if it has one.
4503
4504 The command tree could legitimately contain commands like
4505 'step' and 'next', which call clear_proceed_status, which
4506 frees stop_bpstat's command tree. To make sure this doesn't
4507 free the tree we're executing out from under us, we need to
4508 take ownership of the tree ourselves. Since a given bpstat's
4509 commands are only executed once, we don't need to copy it; we
4510 can clear the pointer in the bpstat, and make sure we free
4511 the tree when we're done. */
4512 counted_command_line ccmd = bs->commands;
4513 bs->commands = NULL;
4514 if (ccmd != NULL)
4515 cmd = ccmd.get ();
4516 if (command_line_is_silent (cmd))
4517 {
4518 /* The action has been already done by bpstat_stop_status. */
4519 cmd = cmd->next;
4520 }
4521
4522 while (cmd != NULL)
4523 {
4524 execute_control_command (cmd);
4525
4526 if (breakpoint_proceeded)
4527 break;
4528 else
4529 cmd = cmd->next;
4530 }
4531
4532 if (breakpoint_proceeded)
4533 {
4534 if (current_ui->async)
4535 /* If we are in async mode, then the target might be still
4536 running, not stopped at any breakpoint, so nothing for
4537 us to do here -- just return to the event loop. */
4538 ;
4539 else
4540 /* In sync mode, when execute_control_command returns
4541 we're already standing on the next breakpoint.
4542 Breakpoint commands for that stop were not run, since
4543 execute_command does not run breakpoint commands --
4544 only command_line_handler does, but that one is not
4545 involved in execution of breakpoint commands. So, we
4546 can now execute breakpoint commands. It should be
4547 noted that making execute_command do bpstat actions is
4548 not an option -- in this case we'll have recursive
4549 invocation of bpstat for each breakpoint with a
4550 command, and can easily blow up GDB stack. Instead, we
4551 return true, which will trigger the caller to recall us
4552 with the new stop_bpstat. */
4553 again = 1;
4554 break;
4555 }
4556 }
4557 return again;
4558 }
4559
4560 void
4561 bpstat_do_actions (void)
4562 {
4563 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4564
4565 /* Do any commands attached to breakpoint we are stopped at. */
4566 while (!ptid_equal (inferior_ptid, null_ptid)
4567 && target_has_execution
4568 && !is_exited (inferior_ptid)
4569 && !is_executing (inferior_ptid))
4570 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4571 and only return when it is stopped at the next breakpoint, we
4572 keep doing breakpoint actions until it returns false to
4573 indicate the inferior was not resumed. */
4574 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4575 break;
4576
4577 discard_cleanups (cleanup_if_error);
4578 }
4579
4580 /* Print out the (old or new) value associated with a watchpoint. */
4581
4582 static void
4583 watchpoint_value_print (struct value *val, struct ui_file *stream)
4584 {
4585 if (val == NULL)
4586 fprintf_unfiltered (stream, _("<unreadable>"));
4587 else
4588 {
4589 struct value_print_options opts;
4590 get_user_print_options (&opts);
4591 value_print (val, stream, &opts);
4592 }
4593 }
4594
4595 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4596 debugging multiple threads. */
4597
4598 void
4599 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4600 {
4601 if (uiout->is_mi_like_p ())
4602 return;
4603
4604 uiout->text ("\n");
4605
4606 if (show_thread_that_caused_stop ())
4607 {
4608 const char *name;
4609 struct thread_info *thr = inferior_thread ();
4610
4611 uiout->text ("Thread ");
4612 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4613
4614 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4615 if (name != NULL)
4616 {
4617 uiout->text (" \"");
4618 uiout->field_fmt ("name", "%s", name);
4619 uiout->text ("\"");
4620 }
4621
4622 uiout->text (" hit ");
4623 }
4624 }
4625
4626 /* Generic routine for printing messages indicating why we
4627 stopped. The behavior of this function depends on the value
4628 'print_it' in the bpstat structure. Under some circumstances we
4629 may decide not to print anything here and delegate the task to
4630 normal_stop(). */
4631
4632 static enum print_stop_action
4633 print_bp_stop_message (bpstat bs)
4634 {
4635 switch (bs->print_it)
4636 {
4637 case print_it_noop:
4638 /* Nothing should be printed for this bpstat entry. */
4639 return PRINT_UNKNOWN;
4640 break;
4641
4642 case print_it_done:
4643 /* We still want to print the frame, but we already printed the
4644 relevant messages. */
4645 return PRINT_SRC_AND_LOC;
4646 break;
4647
4648 case print_it_normal:
4649 {
4650 struct breakpoint *b = bs->breakpoint_at;
4651
4652 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4653 which has since been deleted. */
4654 if (b == NULL)
4655 return PRINT_UNKNOWN;
4656
4657 /* Normal case. Call the breakpoint's print_it method. */
4658 return b->ops->print_it (bs);
4659 }
4660 break;
4661
4662 default:
4663 internal_error (__FILE__, __LINE__,
4664 _("print_bp_stop_message: unrecognized enum value"));
4665 break;
4666 }
4667 }
4668
4669 /* A helper function that prints a shared library stopped event. */
4670
4671 static void
4672 print_solib_event (int is_catchpoint)
4673 {
4674 int any_deleted
4675 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4676 int any_added
4677 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4678
4679 if (!is_catchpoint)
4680 {
4681 if (any_added || any_deleted)
4682 current_uiout->text (_("Stopped due to shared library event:\n"));
4683 else
4684 current_uiout->text (_("Stopped due to shared library event (no "
4685 "libraries added or removed)\n"));
4686 }
4687
4688 if (current_uiout->is_mi_like_p ())
4689 current_uiout->field_string ("reason",
4690 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4691
4692 if (any_deleted)
4693 {
4694 char *name;
4695 int ix;
4696
4697 current_uiout->text (_(" Inferior unloaded "));
4698 ui_out_emit_list list_emitter (current_uiout, "removed");
4699 for (ix = 0;
4700 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4701 ix, name);
4702 ++ix)
4703 {
4704 if (ix > 0)
4705 current_uiout->text (" ");
4706 current_uiout->field_string ("library", name);
4707 current_uiout->text ("\n");
4708 }
4709 }
4710
4711 if (any_added)
4712 {
4713 struct so_list *iter;
4714 int ix;
4715
4716 current_uiout->text (_(" Inferior loaded "));
4717 ui_out_emit_list list_emitter (current_uiout, "added");
4718 for (ix = 0;
4719 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4720 ix, iter);
4721 ++ix)
4722 {
4723 if (ix > 0)
4724 current_uiout->text (" ");
4725 current_uiout->field_string ("library", iter->so_name);
4726 current_uiout->text ("\n");
4727 }
4728 }
4729 }
4730
4731 /* Print a message indicating what happened. This is called from
4732 normal_stop(). The input to this routine is the head of the bpstat
4733 list - a list of the eventpoints that caused this stop. KIND is
4734 the target_waitkind for the stopping event. This
4735 routine calls the generic print routine for printing a message
4736 about reasons for stopping. This will print (for example) the
4737 "Breakpoint n," part of the output. The return value of this
4738 routine is one of:
4739
4740 PRINT_UNKNOWN: Means we printed nothing.
4741 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4742 code to print the location. An example is
4743 "Breakpoint 1, " which should be followed by
4744 the location.
4745 PRINT_SRC_ONLY: Means we printed something, but there is no need
4746 to also print the location part of the message.
4747 An example is the catch/throw messages, which
4748 don't require a location appended to the end.
4749 PRINT_NOTHING: We have done some printing and we don't need any
4750 further info to be printed. */
4751
4752 enum print_stop_action
4753 bpstat_print (bpstat bs, int kind)
4754 {
4755 enum print_stop_action val;
4756
4757 /* Maybe another breakpoint in the chain caused us to stop.
4758 (Currently all watchpoints go on the bpstat whether hit or not.
4759 That probably could (should) be changed, provided care is taken
4760 with respect to bpstat_explains_signal). */
4761 for (; bs; bs = bs->next)
4762 {
4763 val = print_bp_stop_message (bs);
4764 if (val == PRINT_SRC_ONLY
4765 || val == PRINT_SRC_AND_LOC
4766 || val == PRINT_NOTHING)
4767 return val;
4768 }
4769
4770 /* If we had hit a shared library event breakpoint,
4771 print_bp_stop_message would print out this message. If we hit an
4772 OS-level shared library event, do the same thing. */
4773 if (kind == TARGET_WAITKIND_LOADED)
4774 {
4775 print_solib_event (0);
4776 return PRINT_NOTHING;
4777 }
4778
4779 /* We reached the end of the chain, or we got a null BS to start
4780 with and nothing was printed. */
4781 return PRINT_UNKNOWN;
4782 }
4783
4784 /* Evaluate the boolean expression EXP and return the result. */
4785
4786 static bool
4787 breakpoint_cond_eval (expression *exp)
4788 {
4789 struct value *mark = value_mark ();
4790 bool res = value_true (evaluate_expression (exp));
4791
4792 value_free_to_mark (mark);
4793 return res;
4794 }
4795
4796 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4797
4798 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4799 : next (NULL),
4800 bp_location_at (bl),
4801 breakpoint_at (bl->owner),
4802 commands (NULL),
4803 old_val (NULL),
4804 print (0),
4805 stop (0),
4806 print_it (print_it_normal)
4807 {
4808 incref_bp_location (bl);
4809 **bs_link_pointer = this;
4810 *bs_link_pointer = &next;
4811 }
4812
4813 bpstats::bpstats ()
4814 : next (NULL),
4815 bp_location_at (NULL),
4816 breakpoint_at (NULL),
4817 commands (NULL),
4818 old_val (NULL),
4819 print (0),
4820 stop (0),
4821 print_it (print_it_normal)
4822 {
4823 }
4824 \f
4825 /* The target has stopped with waitstatus WS. Check if any hardware
4826 watchpoints have triggered, according to the target. */
4827
4828 int
4829 watchpoints_triggered (struct target_waitstatus *ws)
4830 {
4831 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4832 CORE_ADDR addr;
4833 struct breakpoint *b;
4834
4835 if (!stopped_by_watchpoint)
4836 {
4837 /* We were not stopped by a watchpoint. Mark all watchpoints
4838 as not triggered. */
4839 ALL_BREAKPOINTS (b)
4840 if (is_hardware_watchpoint (b))
4841 {
4842 struct watchpoint *w = (struct watchpoint *) b;
4843
4844 w->watchpoint_triggered = watch_triggered_no;
4845 }
4846
4847 return 0;
4848 }
4849
4850 if (!target_stopped_data_address (&current_target, &addr))
4851 {
4852 /* We were stopped by a watchpoint, but we don't know where.
4853 Mark all watchpoints as unknown. */
4854 ALL_BREAKPOINTS (b)
4855 if (is_hardware_watchpoint (b))
4856 {
4857 struct watchpoint *w = (struct watchpoint *) b;
4858
4859 w->watchpoint_triggered = watch_triggered_unknown;
4860 }
4861
4862 return 1;
4863 }
4864
4865 /* The target could report the data address. Mark watchpoints
4866 affected by this data address as triggered, and all others as not
4867 triggered. */
4868
4869 ALL_BREAKPOINTS (b)
4870 if (is_hardware_watchpoint (b))
4871 {
4872 struct watchpoint *w = (struct watchpoint *) b;
4873 struct bp_location *loc;
4874
4875 w->watchpoint_triggered = watch_triggered_no;
4876 for (loc = b->loc; loc; loc = loc->next)
4877 {
4878 if (is_masked_watchpoint (b))
4879 {
4880 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4881 CORE_ADDR start = loc->address & w->hw_wp_mask;
4882
4883 if (newaddr == start)
4884 {
4885 w->watchpoint_triggered = watch_triggered_yes;
4886 break;
4887 }
4888 }
4889 /* Exact match not required. Within range is sufficient. */
4890 else if (target_watchpoint_addr_within_range (&current_target,
4891 addr, loc->address,
4892 loc->length))
4893 {
4894 w->watchpoint_triggered = watch_triggered_yes;
4895 break;
4896 }
4897 }
4898 }
4899
4900 return 1;
4901 }
4902
4903 /* Possible return values for watchpoint_check. */
4904 enum wp_check_result
4905 {
4906 /* The watchpoint has been deleted. */
4907 WP_DELETED = 1,
4908
4909 /* The value has changed. */
4910 WP_VALUE_CHANGED = 2,
4911
4912 /* The value has not changed. */
4913 WP_VALUE_NOT_CHANGED = 3,
4914
4915 /* Ignore this watchpoint, no matter if the value changed or not. */
4916 WP_IGNORE = 4,
4917 };
4918
4919 #define BP_TEMPFLAG 1
4920 #define BP_HARDWAREFLAG 2
4921
4922 /* Evaluate watchpoint condition expression and check if its value
4923 changed. */
4924
4925 static wp_check_result
4926 watchpoint_check (bpstat bs)
4927 {
4928 struct watchpoint *b;
4929 struct frame_info *fr;
4930 int within_current_scope;
4931
4932 /* BS is built from an existing struct breakpoint. */
4933 gdb_assert (bs->breakpoint_at != NULL);
4934 b = (struct watchpoint *) bs->breakpoint_at;
4935
4936 /* If this is a local watchpoint, we only want to check if the
4937 watchpoint frame is in scope if the current thread is the thread
4938 that was used to create the watchpoint. */
4939 if (!watchpoint_in_thread_scope (b))
4940 return WP_IGNORE;
4941
4942 if (b->exp_valid_block == NULL)
4943 within_current_scope = 1;
4944 else
4945 {
4946 struct frame_info *frame = get_current_frame ();
4947 struct gdbarch *frame_arch = get_frame_arch (frame);
4948 CORE_ADDR frame_pc = get_frame_pc (frame);
4949
4950 /* stack_frame_destroyed_p() returns a non-zero value if we're
4951 still in the function but the stack frame has already been
4952 invalidated. Since we can't rely on the values of local
4953 variables after the stack has been destroyed, we are treating
4954 the watchpoint in that state as `not changed' without further
4955 checking. Don't mark watchpoints as changed if the current
4956 frame is in an epilogue - even if they are in some other
4957 frame, our view of the stack is likely to be wrong and
4958 frame_find_by_id could error out. */
4959 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4960 return WP_IGNORE;
4961
4962 fr = frame_find_by_id (b->watchpoint_frame);
4963 within_current_scope = (fr != NULL);
4964
4965 /* If we've gotten confused in the unwinder, we might have
4966 returned a frame that can't describe this variable. */
4967 if (within_current_scope)
4968 {
4969 struct symbol *function;
4970
4971 function = get_frame_function (fr);
4972 if (function == NULL
4973 || !contained_in (b->exp_valid_block,
4974 SYMBOL_BLOCK_VALUE (function)))
4975 within_current_scope = 0;
4976 }
4977
4978 if (within_current_scope)
4979 /* If we end up stopping, the current frame will get selected
4980 in normal_stop. So this call to select_frame won't affect
4981 the user. */
4982 select_frame (fr);
4983 }
4984
4985 if (within_current_scope)
4986 {
4987 /* We use value_{,free_to_}mark because it could be a *long*
4988 time before we return to the command level and call
4989 free_all_values. We can't call free_all_values because we
4990 might be in the middle of evaluating a function call. */
4991
4992 int pc = 0;
4993 struct value *mark;
4994 struct value *new_val;
4995
4996 if (is_masked_watchpoint (b))
4997 /* Since we don't know the exact trigger address (from
4998 stopped_data_address), just tell the user we've triggered
4999 a mask watchpoint. */
5000 return WP_VALUE_CHANGED;
5001
5002 mark = value_mark ();
5003 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5004
5005 if (b->val_bitsize != 0)
5006 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5007
5008 /* We use value_equal_contents instead of value_equal because
5009 the latter coerces an array to a pointer, thus comparing just
5010 the address of the array instead of its contents. This is
5011 not what we want. */
5012 if ((b->val != NULL) != (new_val != NULL)
5013 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5014 {
5015 if (new_val != NULL)
5016 {
5017 release_value (new_val);
5018 value_free_to_mark (mark);
5019 }
5020 bs->old_val = b->val;
5021 b->val = new_val;
5022 b->val_valid = 1;
5023 return WP_VALUE_CHANGED;
5024 }
5025 else
5026 {
5027 /* Nothing changed. */
5028 value_free_to_mark (mark);
5029 return WP_VALUE_NOT_CHANGED;
5030 }
5031 }
5032 else
5033 {
5034 /* This seems like the only logical thing to do because
5035 if we temporarily ignored the watchpoint, then when
5036 we reenter the block in which it is valid it contains
5037 garbage (in the case of a function, it may have two
5038 garbage values, one before and one after the prologue).
5039 So we can't even detect the first assignment to it and
5040 watch after that (since the garbage may or may not equal
5041 the first value assigned). */
5042 /* We print all the stop information in
5043 breakpoint_ops->print_it, but in this case, by the time we
5044 call breakpoint_ops->print_it this bp will be deleted
5045 already. So we have no choice but print the information
5046 here. */
5047
5048 SWITCH_THRU_ALL_UIS ()
5049 {
5050 struct ui_out *uiout = current_uiout;
5051
5052 if (uiout->is_mi_like_p ())
5053 uiout->field_string
5054 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5055 uiout->text ("\nWatchpoint ");
5056 uiout->field_int ("wpnum", b->number);
5057 uiout->text (" deleted because the program has left the block in\n"
5058 "which its expression is valid.\n");
5059 }
5060
5061 /* Make sure the watchpoint's commands aren't executed. */
5062 b->commands = NULL;
5063 watchpoint_del_at_next_stop (b);
5064
5065 return WP_DELETED;
5066 }
5067 }
5068
5069 /* Return true if it looks like target has stopped due to hitting
5070 breakpoint location BL. This function does not check if we should
5071 stop, only if BL explains the stop. */
5072
5073 static int
5074 bpstat_check_location (const struct bp_location *bl,
5075 struct address_space *aspace, CORE_ADDR bp_addr,
5076 const struct target_waitstatus *ws)
5077 {
5078 struct breakpoint *b = bl->owner;
5079
5080 /* BL is from an existing breakpoint. */
5081 gdb_assert (b != NULL);
5082
5083 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5084 }
5085
5086 /* Determine if the watched values have actually changed, and we
5087 should stop. If not, set BS->stop to 0. */
5088
5089 static void
5090 bpstat_check_watchpoint (bpstat bs)
5091 {
5092 const struct bp_location *bl;
5093 struct watchpoint *b;
5094
5095 /* BS is built for existing struct breakpoint. */
5096 bl = bs->bp_location_at;
5097 gdb_assert (bl != NULL);
5098 b = (struct watchpoint *) bs->breakpoint_at;
5099 gdb_assert (b != NULL);
5100
5101 {
5102 int must_check_value = 0;
5103
5104 if (b->type == bp_watchpoint)
5105 /* For a software watchpoint, we must always check the
5106 watched value. */
5107 must_check_value = 1;
5108 else if (b->watchpoint_triggered == watch_triggered_yes)
5109 /* We have a hardware watchpoint (read, write, or access)
5110 and the target earlier reported an address watched by
5111 this watchpoint. */
5112 must_check_value = 1;
5113 else if (b->watchpoint_triggered == watch_triggered_unknown
5114 && b->type == bp_hardware_watchpoint)
5115 /* We were stopped by a hardware watchpoint, but the target could
5116 not report the data address. We must check the watchpoint's
5117 value. Access and read watchpoints are out of luck; without
5118 a data address, we can't figure it out. */
5119 must_check_value = 1;
5120
5121 if (must_check_value)
5122 {
5123 wp_check_result e;
5124
5125 TRY
5126 {
5127 e = watchpoint_check (bs);
5128 }
5129 CATCH (ex, RETURN_MASK_ALL)
5130 {
5131 exception_fprintf (gdb_stderr, ex,
5132 "Error evaluating expression "
5133 "for watchpoint %d\n",
5134 b->number);
5135
5136 SWITCH_THRU_ALL_UIS ()
5137 {
5138 printf_filtered (_("Watchpoint %d deleted.\n"),
5139 b->number);
5140 }
5141 watchpoint_del_at_next_stop (b);
5142 e = WP_DELETED;
5143 }
5144 END_CATCH
5145
5146 switch (e)
5147 {
5148 case WP_DELETED:
5149 /* We've already printed what needs to be printed. */
5150 bs->print_it = print_it_done;
5151 /* Stop. */
5152 break;
5153 case WP_IGNORE:
5154 bs->print_it = print_it_noop;
5155 bs->stop = 0;
5156 break;
5157 case WP_VALUE_CHANGED:
5158 if (b->type == bp_read_watchpoint)
5159 {
5160 /* There are two cases to consider here:
5161
5162 1. We're watching the triggered memory for reads.
5163 In that case, trust the target, and always report
5164 the watchpoint hit to the user. Even though
5165 reads don't cause value changes, the value may
5166 have changed since the last time it was read, and
5167 since we're not trapping writes, we will not see
5168 those, and as such we should ignore our notion of
5169 old value.
5170
5171 2. We're watching the triggered memory for both
5172 reads and writes. There are two ways this may
5173 happen:
5174
5175 2.1. This is a target that can't break on data
5176 reads only, but can break on accesses (reads or
5177 writes), such as e.g., x86. We detect this case
5178 at the time we try to insert read watchpoints.
5179
5180 2.2. Otherwise, the target supports read
5181 watchpoints, but, the user set an access or write
5182 watchpoint watching the same memory as this read
5183 watchpoint.
5184
5185 If we're watching memory writes as well as reads,
5186 ignore watchpoint hits when we find that the
5187 value hasn't changed, as reads don't cause
5188 changes. This still gives false positives when
5189 the program writes the same value to memory as
5190 what there was already in memory (we will confuse
5191 it for a read), but it's much better than
5192 nothing. */
5193
5194 int other_write_watchpoint = 0;
5195
5196 if (bl->watchpoint_type == hw_read)
5197 {
5198 struct breakpoint *other_b;
5199
5200 ALL_BREAKPOINTS (other_b)
5201 if (other_b->type == bp_hardware_watchpoint
5202 || other_b->type == bp_access_watchpoint)
5203 {
5204 struct watchpoint *other_w =
5205 (struct watchpoint *) other_b;
5206
5207 if (other_w->watchpoint_triggered
5208 == watch_triggered_yes)
5209 {
5210 other_write_watchpoint = 1;
5211 break;
5212 }
5213 }
5214 }
5215
5216 if (other_write_watchpoint
5217 || bl->watchpoint_type == hw_access)
5218 {
5219 /* We're watching the same memory for writes,
5220 and the value changed since the last time we
5221 updated it, so this trap must be for a write.
5222 Ignore it. */
5223 bs->print_it = print_it_noop;
5224 bs->stop = 0;
5225 }
5226 }
5227 break;
5228 case WP_VALUE_NOT_CHANGED:
5229 if (b->type == bp_hardware_watchpoint
5230 || b->type == bp_watchpoint)
5231 {
5232 /* Don't stop: write watchpoints shouldn't fire if
5233 the value hasn't changed. */
5234 bs->print_it = print_it_noop;
5235 bs->stop = 0;
5236 }
5237 /* Stop. */
5238 break;
5239 default:
5240 /* Can't happen. */
5241 break;
5242 }
5243 }
5244 else /* must_check_value == 0 */
5245 {
5246 /* This is a case where some watchpoint(s) triggered, but
5247 not at the address of this watchpoint, or else no
5248 watchpoint triggered after all. So don't print
5249 anything for this watchpoint. */
5250 bs->print_it = print_it_noop;
5251 bs->stop = 0;
5252 }
5253 }
5254 }
5255
5256 /* For breakpoints that are currently marked as telling gdb to stop,
5257 check conditions (condition proper, frame, thread and ignore count)
5258 of breakpoint referred to by BS. If we should not stop for this
5259 breakpoint, set BS->stop to 0. */
5260
5261 static void
5262 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5263 {
5264 const struct bp_location *bl;
5265 struct breakpoint *b;
5266 /* Assume stop. */
5267 bool condition_result = true;
5268 struct expression *cond;
5269
5270 gdb_assert (bs->stop);
5271
5272 /* BS is built for existing struct breakpoint. */
5273 bl = bs->bp_location_at;
5274 gdb_assert (bl != NULL);
5275 b = bs->breakpoint_at;
5276 gdb_assert (b != NULL);
5277
5278 /* Even if the target evaluated the condition on its end and notified GDB, we
5279 need to do so again since GDB does not know if we stopped due to a
5280 breakpoint or a single step breakpoint. */
5281
5282 if (frame_id_p (b->frame_id)
5283 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5284 {
5285 bs->stop = 0;
5286 return;
5287 }
5288
5289 /* If this is a thread/task-specific breakpoint, don't waste cpu
5290 evaluating the condition if this isn't the specified
5291 thread/task. */
5292 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5293 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5294
5295 {
5296 bs->stop = 0;
5297 return;
5298 }
5299
5300 /* Evaluate extension language breakpoints that have a "stop" method
5301 implemented. */
5302 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5303
5304 if (is_watchpoint (b))
5305 {
5306 struct watchpoint *w = (struct watchpoint *) b;
5307
5308 cond = w->cond_exp.get ();
5309 }
5310 else
5311 cond = bl->cond.get ();
5312
5313 if (cond && b->disposition != disp_del_at_next_stop)
5314 {
5315 int within_current_scope = 1;
5316 struct watchpoint * w;
5317
5318 /* We use value_mark and value_free_to_mark because it could
5319 be a long time before we return to the command level and
5320 call free_all_values. We can't call free_all_values
5321 because we might be in the middle of evaluating a
5322 function call. */
5323 struct value *mark = value_mark ();
5324
5325 if (is_watchpoint (b))
5326 w = (struct watchpoint *) b;
5327 else
5328 w = NULL;
5329
5330 /* Need to select the frame, with all that implies so that
5331 the conditions will have the right context. Because we
5332 use the frame, we will not see an inlined function's
5333 variables when we arrive at a breakpoint at the start
5334 of the inlined function; the current frame will be the
5335 call site. */
5336 if (w == NULL || w->cond_exp_valid_block == NULL)
5337 select_frame (get_current_frame ());
5338 else
5339 {
5340 struct frame_info *frame;
5341
5342 /* For local watchpoint expressions, which particular
5343 instance of a local is being watched matters, so we
5344 keep track of the frame to evaluate the expression
5345 in. To evaluate the condition however, it doesn't
5346 really matter which instantiation of the function
5347 where the condition makes sense triggers the
5348 watchpoint. This allows an expression like "watch
5349 global if q > 10" set in `func', catch writes to
5350 global on all threads that call `func', or catch
5351 writes on all recursive calls of `func' by a single
5352 thread. We simply always evaluate the condition in
5353 the innermost frame that's executing where it makes
5354 sense to evaluate the condition. It seems
5355 intuitive. */
5356 frame = block_innermost_frame (w->cond_exp_valid_block);
5357 if (frame != NULL)
5358 select_frame (frame);
5359 else
5360 within_current_scope = 0;
5361 }
5362 if (within_current_scope)
5363 {
5364 TRY
5365 {
5366 condition_result = breakpoint_cond_eval (cond);
5367 }
5368 CATCH (ex, RETURN_MASK_ALL)
5369 {
5370 exception_fprintf (gdb_stderr, ex,
5371 "Error in testing breakpoint condition:\n");
5372 }
5373 END_CATCH
5374 }
5375 else
5376 {
5377 warning (_("Watchpoint condition cannot be tested "
5378 "in the current scope"));
5379 /* If we failed to set the right context for this
5380 watchpoint, unconditionally report it. */
5381 }
5382 /* FIXME-someday, should give breakpoint #. */
5383 value_free_to_mark (mark);
5384 }
5385
5386 if (cond && !condition_result)
5387 {
5388 bs->stop = 0;
5389 }
5390 else if (b->ignore_count > 0)
5391 {
5392 b->ignore_count--;
5393 bs->stop = 0;
5394 /* Increase the hit count even though we don't stop. */
5395 ++(b->hit_count);
5396 observer_notify_breakpoint_modified (b);
5397 }
5398 }
5399
5400 /* Returns true if we need to track moribund locations of LOC's type
5401 on the current target. */
5402
5403 static int
5404 need_moribund_for_location_type (struct bp_location *loc)
5405 {
5406 return ((loc->loc_type == bp_loc_software_breakpoint
5407 && !target_supports_stopped_by_sw_breakpoint ())
5408 || (loc->loc_type == bp_loc_hardware_breakpoint
5409 && !target_supports_stopped_by_hw_breakpoint ()));
5410 }
5411
5412
5413 /* Get a bpstat associated with having just stopped at address
5414 BP_ADDR in thread PTID.
5415
5416 Determine whether we stopped at a breakpoint, etc, or whether we
5417 don't understand this stop. Result is a chain of bpstat's such
5418 that:
5419
5420 if we don't understand the stop, the result is a null pointer.
5421
5422 if we understand why we stopped, the result is not null.
5423
5424 Each element of the chain refers to a particular breakpoint or
5425 watchpoint at which we have stopped. (We may have stopped for
5426 several reasons concurrently.)
5427
5428 Each element of the chain has valid next, breakpoint_at,
5429 commands, FIXME??? fields. */
5430
5431 bpstat
5432 bpstat_stop_status (struct address_space *aspace,
5433 CORE_ADDR bp_addr, ptid_t ptid,
5434 const struct target_waitstatus *ws)
5435 {
5436 struct breakpoint *b = NULL;
5437 struct bp_location *bl;
5438 struct bp_location *loc;
5439 /* First item of allocated bpstat's. */
5440 bpstat bs_head = NULL, *bs_link = &bs_head;
5441 /* Pointer to the last thing in the chain currently. */
5442 bpstat bs;
5443 int ix;
5444 int need_remove_insert;
5445 int removed_any;
5446
5447 /* First, build the bpstat chain with locations that explain a
5448 target stop, while being careful to not set the target running,
5449 as that may invalidate locations (in particular watchpoint
5450 locations are recreated). Resuming will happen here with
5451 breakpoint conditions or watchpoint expressions that include
5452 inferior function calls. */
5453
5454 ALL_BREAKPOINTS (b)
5455 {
5456 if (!breakpoint_enabled (b))
5457 continue;
5458
5459 for (bl = b->loc; bl != NULL; bl = bl->next)
5460 {
5461 /* For hardware watchpoints, we look only at the first
5462 location. The watchpoint_check function will work on the
5463 entire expression, not the individual locations. For
5464 read watchpoints, the watchpoints_triggered function has
5465 checked all locations already. */
5466 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5467 break;
5468
5469 if (!bl->enabled || bl->shlib_disabled)
5470 continue;
5471
5472 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5473 continue;
5474
5475 /* Come here if it's a watchpoint, or if the break address
5476 matches. */
5477
5478 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5479 explain stop. */
5480
5481 /* Assume we stop. Should we find a watchpoint that is not
5482 actually triggered, or if the condition of the breakpoint
5483 evaluates as false, we'll reset 'stop' to 0. */
5484 bs->stop = 1;
5485 bs->print = 1;
5486
5487 /* If this is a scope breakpoint, mark the associated
5488 watchpoint as triggered so that we will handle the
5489 out-of-scope event. We'll get to the watchpoint next
5490 iteration. */
5491 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5492 {
5493 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5494
5495 w->watchpoint_triggered = watch_triggered_yes;
5496 }
5497 }
5498 }
5499
5500 /* Check if a moribund breakpoint explains the stop. */
5501 if (!target_supports_stopped_by_sw_breakpoint ()
5502 || !target_supports_stopped_by_hw_breakpoint ())
5503 {
5504 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5505 {
5506 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5507 && need_moribund_for_location_type (loc))
5508 {
5509 bs = new bpstats (loc, &bs_link);
5510 /* For hits of moribund locations, we should just proceed. */
5511 bs->stop = 0;
5512 bs->print = 0;
5513 bs->print_it = print_it_noop;
5514 }
5515 }
5516 }
5517
5518 /* A bit of special processing for shlib breakpoints. We need to
5519 process solib loading here, so that the lists of loaded and
5520 unloaded libraries are correct before we handle "catch load" and
5521 "catch unload". */
5522 for (bs = bs_head; bs != NULL; bs = bs->next)
5523 {
5524 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5525 {
5526 handle_solib_event ();
5527 break;
5528 }
5529 }
5530
5531 /* Now go through the locations that caused the target to stop, and
5532 check whether we're interested in reporting this stop to higher
5533 layers, or whether we should resume the target transparently. */
5534
5535 removed_any = 0;
5536
5537 for (bs = bs_head; bs != NULL; bs = bs->next)
5538 {
5539 if (!bs->stop)
5540 continue;
5541
5542 b = bs->breakpoint_at;
5543 b->ops->check_status (bs);
5544 if (bs->stop)
5545 {
5546 bpstat_check_breakpoint_conditions (bs, ptid);
5547
5548 if (bs->stop)
5549 {
5550 ++(b->hit_count);
5551 observer_notify_breakpoint_modified (b);
5552
5553 /* We will stop here. */
5554 if (b->disposition == disp_disable)
5555 {
5556 --(b->enable_count);
5557 if (b->enable_count <= 0)
5558 b->enable_state = bp_disabled;
5559 removed_any = 1;
5560 }
5561 if (b->silent)
5562 bs->print = 0;
5563 bs->commands = b->commands;
5564 if (command_line_is_silent (bs->commands
5565 ? bs->commands.get () : NULL))
5566 bs->print = 0;
5567
5568 b->ops->after_condition_true (bs);
5569 }
5570
5571 }
5572
5573 /* Print nothing for this entry if we don't stop or don't
5574 print. */
5575 if (!bs->stop || !bs->print)
5576 bs->print_it = print_it_noop;
5577 }
5578
5579 /* If we aren't stopping, the value of some hardware watchpoint may
5580 not have changed, but the intermediate memory locations we are
5581 watching may have. Don't bother if we're stopping; this will get
5582 done later. */
5583 need_remove_insert = 0;
5584 if (! bpstat_causes_stop (bs_head))
5585 for (bs = bs_head; bs != NULL; bs = bs->next)
5586 if (!bs->stop
5587 && bs->breakpoint_at
5588 && is_hardware_watchpoint (bs->breakpoint_at))
5589 {
5590 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5591
5592 update_watchpoint (w, 0 /* don't reparse. */);
5593 need_remove_insert = 1;
5594 }
5595
5596 if (need_remove_insert)
5597 update_global_location_list (UGLL_MAY_INSERT);
5598 else if (removed_any)
5599 update_global_location_list (UGLL_DONT_INSERT);
5600
5601 return bs_head;
5602 }
5603
5604 static void
5605 handle_jit_event (void)
5606 {
5607 struct frame_info *frame;
5608 struct gdbarch *gdbarch;
5609
5610 if (debug_infrun)
5611 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5612
5613 /* Switch terminal for any messages produced by
5614 breakpoint_re_set. */
5615 target_terminal::ours_for_output ();
5616
5617 frame = get_current_frame ();
5618 gdbarch = get_frame_arch (frame);
5619
5620 jit_event_handler (gdbarch);
5621
5622 target_terminal::inferior ();
5623 }
5624
5625 /* Prepare WHAT final decision for infrun. */
5626
5627 /* Decide what infrun needs to do with this bpstat. */
5628
5629 struct bpstat_what
5630 bpstat_what (bpstat bs_head)
5631 {
5632 struct bpstat_what retval;
5633 bpstat bs;
5634
5635 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5636 retval.call_dummy = STOP_NONE;
5637 retval.is_longjmp = 0;
5638
5639 for (bs = bs_head; bs != NULL; bs = bs->next)
5640 {
5641 /* Extract this BS's action. After processing each BS, we check
5642 if its action overrides all we've seem so far. */
5643 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5644 enum bptype bptype;
5645
5646 if (bs->breakpoint_at == NULL)
5647 {
5648 /* I suspect this can happen if it was a momentary
5649 breakpoint which has since been deleted. */
5650 bptype = bp_none;
5651 }
5652 else
5653 bptype = bs->breakpoint_at->type;
5654
5655 switch (bptype)
5656 {
5657 case bp_none:
5658 break;
5659 case bp_breakpoint:
5660 case bp_hardware_breakpoint:
5661 case bp_single_step:
5662 case bp_until:
5663 case bp_finish:
5664 case bp_shlib_event:
5665 if (bs->stop)
5666 {
5667 if (bs->print)
5668 this_action = BPSTAT_WHAT_STOP_NOISY;
5669 else
5670 this_action = BPSTAT_WHAT_STOP_SILENT;
5671 }
5672 else
5673 this_action = BPSTAT_WHAT_SINGLE;
5674 break;
5675 case bp_watchpoint:
5676 case bp_hardware_watchpoint:
5677 case bp_read_watchpoint:
5678 case bp_access_watchpoint:
5679 if (bs->stop)
5680 {
5681 if (bs->print)
5682 this_action = BPSTAT_WHAT_STOP_NOISY;
5683 else
5684 this_action = BPSTAT_WHAT_STOP_SILENT;
5685 }
5686 else
5687 {
5688 /* There was a watchpoint, but we're not stopping.
5689 This requires no further action. */
5690 }
5691 break;
5692 case bp_longjmp:
5693 case bp_longjmp_call_dummy:
5694 case bp_exception:
5695 if (bs->stop)
5696 {
5697 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5698 retval.is_longjmp = bptype != bp_exception;
5699 }
5700 else
5701 this_action = BPSTAT_WHAT_SINGLE;
5702 break;
5703 case bp_longjmp_resume:
5704 case bp_exception_resume:
5705 if (bs->stop)
5706 {
5707 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5708 retval.is_longjmp = bptype == bp_longjmp_resume;
5709 }
5710 else
5711 this_action = BPSTAT_WHAT_SINGLE;
5712 break;
5713 case bp_step_resume:
5714 if (bs->stop)
5715 this_action = BPSTAT_WHAT_STEP_RESUME;
5716 else
5717 {
5718 /* It is for the wrong frame. */
5719 this_action = BPSTAT_WHAT_SINGLE;
5720 }
5721 break;
5722 case bp_hp_step_resume:
5723 if (bs->stop)
5724 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5725 else
5726 {
5727 /* It is for the wrong frame. */
5728 this_action = BPSTAT_WHAT_SINGLE;
5729 }
5730 break;
5731 case bp_watchpoint_scope:
5732 case bp_thread_event:
5733 case bp_overlay_event:
5734 case bp_longjmp_master:
5735 case bp_std_terminate_master:
5736 case bp_exception_master:
5737 this_action = BPSTAT_WHAT_SINGLE;
5738 break;
5739 case bp_catchpoint:
5740 if (bs->stop)
5741 {
5742 if (bs->print)
5743 this_action = BPSTAT_WHAT_STOP_NOISY;
5744 else
5745 this_action = BPSTAT_WHAT_STOP_SILENT;
5746 }
5747 else
5748 {
5749 /* There was a catchpoint, but we're not stopping.
5750 This requires no further action. */
5751 }
5752 break;
5753 case bp_jit_event:
5754 this_action = BPSTAT_WHAT_SINGLE;
5755 break;
5756 case bp_call_dummy:
5757 /* Make sure the action is stop (silent or noisy),
5758 so infrun.c pops the dummy frame. */
5759 retval.call_dummy = STOP_STACK_DUMMY;
5760 this_action = BPSTAT_WHAT_STOP_SILENT;
5761 break;
5762 case bp_std_terminate:
5763 /* Make sure the action is stop (silent or noisy),
5764 so infrun.c pops the dummy frame. */
5765 retval.call_dummy = STOP_STD_TERMINATE;
5766 this_action = BPSTAT_WHAT_STOP_SILENT;
5767 break;
5768 case bp_tracepoint:
5769 case bp_fast_tracepoint:
5770 case bp_static_tracepoint:
5771 /* Tracepoint hits should not be reported back to GDB, and
5772 if one got through somehow, it should have been filtered
5773 out already. */
5774 internal_error (__FILE__, __LINE__,
5775 _("bpstat_what: tracepoint encountered"));
5776 break;
5777 case bp_gnu_ifunc_resolver:
5778 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5779 this_action = BPSTAT_WHAT_SINGLE;
5780 break;
5781 case bp_gnu_ifunc_resolver_return:
5782 /* The breakpoint will be removed, execution will restart from the
5783 PC of the former breakpoint. */
5784 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5785 break;
5786
5787 case bp_dprintf:
5788 if (bs->stop)
5789 this_action = BPSTAT_WHAT_STOP_SILENT;
5790 else
5791 this_action = BPSTAT_WHAT_SINGLE;
5792 break;
5793
5794 default:
5795 internal_error (__FILE__, __LINE__,
5796 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5797 }
5798
5799 retval.main_action = std::max (retval.main_action, this_action);
5800 }
5801
5802 return retval;
5803 }
5804
5805 void
5806 bpstat_run_callbacks (bpstat bs_head)
5807 {
5808 bpstat bs;
5809
5810 for (bs = bs_head; bs != NULL; bs = bs->next)
5811 {
5812 struct breakpoint *b = bs->breakpoint_at;
5813
5814 if (b == NULL)
5815 continue;
5816 switch (b->type)
5817 {
5818 case bp_jit_event:
5819 handle_jit_event ();
5820 break;
5821 case bp_gnu_ifunc_resolver:
5822 gnu_ifunc_resolver_stop (b);
5823 break;
5824 case bp_gnu_ifunc_resolver_return:
5825 gnu_ifunc_resolver_return_stop (b);
5826 break;
5827 }
5828 }
5829 }
5830
5831 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5832 without hardware support). This isn't related to a specific bpstat,
5833 just to things like whether watchpoints are set. */
5834
5835 int
5836 bpstat_should_step (void)
5837 {
5838 struct breakpoint *b;
5839
5840 ALL_BREAKPOINTS (b)
5841 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5842 return 1;
5843 return 0;
5844 }
5845
5846 int
5847 bpstat_causes_stop (bpstat bs)
5848 {
5849 for (; bs != NULL; bs = bs->next)
5850 if (bs->stop)
5851 return 1;
5852
5853 return 0;
5854 }
5855
5856 \f
5857
5858 /* Compute a string of spaces suitable to indent the next line
5859 so it starts at the position corresponding to the table column
5860 named COL_NAME in the currently active table of UIOUT. */
5861
5862 static char *
5863 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5864 {
5865 static char wrap_indent[80];
5866 int i, total_width, width, align;
5867 const char *text;
5868
5869 total_width = 0;
5870 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5871 {
5872 if (strcmp (text, col_name) == 0)
5873 {
5874 gdb_assert (total_width < sizeof wrap_indent);
5875 memset (wrap_indent, ' ', total_width);
5876 wrap_indent[total_width] = 0;
5877
5878 return wrap_indent;
5879 }
5880
5881 total_width += width + 1;
5882 }
5883
5884 return NULL;
5885 }
5886
5887 /* Determine if the locations of this breakpoint will have their conditions
5888 evaluated by the target, host or a mix of both. Returns the following:
5889
5890 "host": Host evals condition.
5891 "host or target": Host or Target evals condition.
5892 "target": Target evals condition.
5893 */
5894
5895 static const char *
5896 bp_condition_evaluator (struct breakpoint *b)
5897 {
5898 struct bp_location *bl;
5899 char host_evals = 0;
5900 char target_evals = 0;
5901
5902 if (!b)
5903 return NULL;
5904
5905 if (!is_breakpoint (b))
5906 return NULL;
5907
5908 if (gdb_evaluates_breakpoint_condition_p ()
5909 || !target_supports_evaluation_of_breakpoint_conditions ())
5910 return condition_evaluation_host;
5911
5912 for (bl = b->loc; bl; bl = bl->next)
5913 {
5914 if (bl->cond_bytecode)
5915 target_evals++;
5916 else
5917 host_evals++;
5918 }
5919
5920 if (host_evals && target_evals)
5921 return condition_evaluation_both;
5922 else if (target_evals)
5923 return condition_evaluation_target;
5924 else
5925 return condition_evaluation_host;
5926 }
5927
5928 /* Determine the breakpoint location's condition evaluator. This is
5929 similar to bp_condition_evaluator, but for locations. */
5930
5931 static const char *
5932 bp_location_condition_evaluator (struct bp_location *bl)
5933 {
5934 if (bl && !is_breakpoint (bl->owner))
5935 return NULL;
5936
5937 if (gdb_evaluates_breakpoint_condition_p ()
5938 || !target_supports_evaluation_of_breakpoint_conditions ())
5939 return condition_evaluation_host;
5940
5941 if (bl && bl->cond_bytecode)
5942 return condition_evaluation_target;
5943 else
5944 return condition_evaluation_host;
5945 }
5946
5947 /* Print the LOC location out of the list of B->LOC locations. */
5948
5949 static void
5950 print_breakpoint_location (struct breakpoint *b,
5951 struct bp_location *loc)
5952 {
5953 struct ui_out *uiout = current_uiout;
5954
5955 scoped_restore_current_program_space restore_pspace;
5956
5957 if (loc != NULL && loc->shlib_disabled)
5958 loc = NULL;
5959
5960 if (loc != NULL)
5961 set_current_program_space (loc->pspace);
5962
5963 if (b->display_canonical)
5964 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5965 else if (loc && loc->symtab)
5966 {
5967 struct symbol *sym
5968 = find_pc_sect_function (loc->address, loc->section);
5969 if (sym)
5970 {
5971 uiout->text ("in ");
5972 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
5973 uiout->text (" ");
5974 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5975 uiout->text ("at ");
5976 }
5977 uiout->field_string ("file",
5978 symtab_to_filename_for_display (loc->symtab));
5979 uiout->text (":");
5980
5981 if (uiout->is_mi_like_p ())
5982 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5983
5984 uiout->field_int ("line", loc->line_number);
5985 }
5986 else if (loc)
5987 {
5988 string_file stb;
5989
5990 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5991 demangle, "");
5992 uiout->field_stream ("at", stb);
5993 }
5994 else
5995 {
5996 uiout->field_string ("pending",
5997 event_location_to_string (b->location.get ()));
5998 /* If extra_string is available, it could be holding a condition
5999 or dprintf arguments. In either case, make sure it is printed,
6000 too, but only for non-MI streams. */
6001 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6002 {
6003 if (b->type == bp_dprintf)
6004 uiout->text (",");
6005 else
6006 uiout->text (" ");
6007 uiout->text (b->extra_string);
6008 }
6009 }
6010
6011 if (loc && is_breakpoint (b)
6012 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6013 && bp_condition_evaluator (b) == condition_evaluation_both)
6014 {
6015 uiout->text (" (");
6016 uiout->field_string ("evaluated-by",
6017 bp_location_condition_evaluator (loc));
6018 uiout->text (")");
6019 }
6020 }
6021
6022 static const char *
6023 bptype_string (enum bptype type)
6024 {
6025 struct ep_type_description
6026 {
6027 enum bptype type;
6028 const char *description;
6029 };
6030 static struct ep_type_description bptypes[] =
6031 {
6032 {bp_none, "?deleted?"},
6033 {bp_breakpoint, "breakpoint"},
6034 {bp_hardware_breakpoint, "hw breakpoint"},
6035 {bp_single_step, "sw single-step"},
6036 {bp_until, "until"},
6037 {bp_finish, "finish"},
6038 {bp_watchpoint, "watchpoint"},
6039 {bp_hardware_watchpoint, "hw watchpoint"},
6040 {bp_read_watchpoint, "read watchpoint"},
6041 {bp_access_watchpoint, "acc watchpoint"},
6042 {bp_longjmp, "longjmp"},
6043 {bp_longjmp_resume, "longjmp resume"},
6044 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6045 {bp_exception, "exception"},
6046 {bp_exception_resume, "exception resume"},
6047 {bp_step_resume, "step resume"},
6048 {bp_hp_step_resume, "high-priority step resume"},
6049 {bp_watchpoint_scope, "watchpoint scope"},
6050 {bp_call_dummy, "call dummy"},
6051 {bp_std_terminate, "std::terminate"},
6052 {bp_shlib_event, "shlib events"},
6053 {bp_thread_event, "thread events"},
6054 {bp_overlay_event, "overlay events"},
6055 {bp_longjmp_master, "longjmp master"},
6056 {bp_std_terminate_master, "std::terminate master"},
6057 {bp_exception_master, "exception master"},
6058 {bp_catchpoint, "catchpoint"},
6059 {bp_tracepoint, "tracepoint"},
6060 {bp_fast_tracepoint, "fast tracepoint"},
6061 {bp_static_tracepoint, "static tracepoint"},
6062 {bp_dprintf, "dprintf"},
6063 {bp_jit_event, "jit events"},
6064 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6065 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6066 };
6067
6068 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6069 || ((int) type != bptypes[(int) type].type))
6070 internal_error (__FILE__, __LINE__,
6071 _("bptypes table does not describe type #%d."),
6072 (int) type);
6073
6074 return bptypes[(int) type].description;
6075 }
6076
6077 /* For MI, output a field named 'thread-groups' with a list as the value.
6078 For CLI, prefix the list with the string 'inf'. */
6079
6080 static void
6081 output_thread_groups (struct ui_out *uiout,
6082 const char *field_name,
6083 VEC(int) *inf_num,
6084 int mi_only)
6085 {
6086 int is_mi = uiout->is_mi_like_p ();
6087 int inf;
6088 int i;
6089
6090 /* For backward compatibility, don't display inferiors in CLI unless
6091 there are several. Always display them for MI. */
6092 if (!is_mi && mi_only)
6093 return;
6094
6095 ui_out_emit_list list_emitter (uiout, field_name);
6096
6097 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6098 {
6099 if (is_mi)
6100 {
6101 char mi_group[10];
6102
6103 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6104 uiout->field_string (NULL, mi_group);
6105 }
6106 else
6107 {
6108 if (i == 0)
6109 uiout->text (" inf ");
6110 else
6111 uiout->text (", ");
6112
6113 uiout->text (plongest (inf));
6114 }
6115 }
6116 }
6117
6118 /* Print B to gdb_stdout. */
6119
6120 static void
6121 print_one_breakpoint_location (struct breakpoint *b,
6122 struct bp_location *loc,
6123 int loc_number,
6124 struct bp_location **last_loc,
6125 int allflag)
6126 {
6127 struct command_line *l;
6128 static char bpenables[] = "nynny";
6129
6130 struct ui_out *uiout = current_uiout;
6131 int header_of_multiple = 0;
6132 int part_of_multiple = (loc != NULL);
6133 struct value_print_options opts;
6134
6135 get_user_print_options (&opts);
6136
6137 gdb_assert (!loc || loc_number != 0);
6138 /* See comment in print_one_breakpoint concerning treatment of
6139 breakpoints with single disabled location. */
6140 if (loc == NULL
6141 && (b->loc != NULL
6142 && (b->loc->next != NULL || !b->loc->enabled)))
6143 header_of_multiple = 1;
6144 if (loc == NULL)
6145 loc = b->loc;
6146
6147 annotate_record ();
6148
6149 /* 1 */
6150 annotate_field (0);
6151 if (part_of_multiple)
6152 {
6153 char *formatted;
6154 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6155 uiout->field_string ("number", formatted);
6156 xfree (formatted);
6157 }
6158 else
6159 {
6160 uiout->field_int ("number", b->number);
6161 }
6162
6163 /* 2 */
6164 annotate_field (1);
6165 if (part_of_multiple)
6166 uiout->field_skip ("type");
6167 else
6168 uiout->field_string ("type", bptype_string (b->type));
6169
6170 /* 3 */
6171 annotate_field (2);
6172 if (part_of_multiple)
6173 uiout->field_skip ("disp");
6174 else
6175 uiout->field_string ("disp", bpdisp_text (b->disposition));
6176
6177
6178 /* 4 */
6179 annotate_field (3);
6180 if (part_of_multiple)
6181 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6182 else
6183 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6184 uiout->spaces (2);
6185
6186
6187 /* 5 and 6 */
6188 if (b->ops != NULL && b->ops->print_one != NULL)
6189 {
6190 /* Although the print_one can possibly print all locations,
6191 calling it here is not likely to get any nice result. So,
6192 make sure there's just one location. */
6193 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6194 b->ops->print_one (b, last_loc);
6195 }
6196 else
6197 switch (b->type)
6198 {
6199 case bp_none:
6200 internal_error (__FILE__, __LINE__,
6201 _("print_one_breakpoint: bp_none encountered\n"));
6202 break;
6203
6204 case bp_watchpoint:
6205 case bp_hardware_watchpoint:
6206 case bp_read_watchpoint:
6207 case bp_access_watchpoint:
6208 {
6209 struct watchpoint *w = (struct watchpoint *) b;
6210
6211 /* Field 4, the address, is omitted (which makes the columns
6212 not line up too nicely with the headers, but the effect
6213 is relatively readable). */
6214 if (opts.addressprint)
6215 uiout->field_skip ("addr");
6216 annotate_field (5);
6217 uiout->field_string ("what", w->exp_string);
6218 }
6219 break;
6220
6221 case bp_breakpoint:
6222 case bp_hardware_breakpoint:
6223 case bp_single_step:
6224 case bp_until:
6225 case bp_finish:
6226 case bp_longjmp:
6227 case bp_longjmp_resume:
6228 case bp_longjmp_call_dummy:
6229 case bp_exception:
6230 case bp_exception_resume:
6231 case bp_step_resume:
6232 case bp_hp_step_resume:
6233 case bp_watchpoint_scope:
6234 case bp_call_dummy:
6235 case bp_std_terminate:
6236 case bp_shlib_event:
6237 case bp_thread_event:
6238 case bp_overlay_event:
6239 case bp_longjmp_master:
6240 case bp_std_terminate_master:
6241 case bp_exception_master:
6242 case bp_tracepoint:
6243 case bp_fast_tracepoint:
6244 case bp_static_tracepoint:
6245 case bp_dprintf:
6246 case bp_jit_event:
6247 case bp_gnu_ifunc_resolver:
6248 case bp_gnu_ifunc_resolver_return:
6249 if (opts.addressprint)
6250 {
6251 annotate_field (4);
6252 if (header_of_multiple)
6253 uiout->field_string ("addr", "<MULTIPLE>");
6254 else if (b->loc == NULL || loc->shlib_disabled)
6255 uiout->field_string ("addr", "<PENDING>");
6256 else
6257 uiout->field_core_addr ("addr",
6258 loc->gdbarch, loc->address);
6259 }
6260 annotate_field (5);
6261 if (!header_of_multiple)
6262 print_breakpoint_location (b, loc);
6263 if (b->loc)
6264 *last_loc = b->loc;
6265 break;
6266 }
6267
6268
6269 if (loc != NULL && !header_of_multiple)
6270 {
6271 struct inferior *inf;
6272 VEC(int) *inf_num = NULL;
6273 int mi_only = 1;
6274
6275 ALL_INFERIORS (inf)
6276 {
6277 if (inf->pspace == loc->pspace)
6278 VEC_safe_push (int, inf_num, inf->num);
6279 }
6280
6281 /* For backward compatibility, don't display inferiors in CLI unless
6282 there are several. Always display for MI. */
6283 if (allflag
6284 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6285 && (number_of_program_spaces () > 1
6286 || number_of_inferiors () > 1)
6287 /* LOC is for existing B, it cannot be in
6288 moribund_locations and thus having NULL OWNER. */
6289 && loc->owner->type != bp_catchpoint))
6290 mi_only = 0;
6291 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6292 VEC_free (int, inf_num);
6293 }
6294
6295 if (!part_of_multiple)
6296 {
6297 if (b->thread != -1)
6298 {
6299 /* FIXME: This seems to be redundant and lost here; see the
6300 "stop only in" line a little further down. */
6301 uiout->text (" thread ");
6302 uiout->field_int ("thread", b->thread);
6303 }
6304 else if (b->task != 0)
6305 {
6306 uiout->text (" task ");
6307 uiout->field_int ("task", b->task);
6308 }
6309 }
6310
6311 uiout->text ("\n");
6312
6313 if (!part_of_multiple)
6314 b->ops->print_one_detail (b, uiout);
6315
6316 if (part_of_multiple && frame_id_p (b->frame_id))
6317 {
6318 annotate_field (6);
6319 uiout->text ("\tstop only in stack frame at ");
6320 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6321 the frame ID. */
6322 uiout->field_core_addr ("frame",
6323 b->gdbarch, b->frame_id.stack_addr);
6324 uiout->text ("\n");
6325 }
6326
6327 if (!part_of_multiple && b->cond_string)
6328 {
6329 annotate_field (7);
6330 if (is_tracepoint (b))
6331 uiout->text ("\ttrace only if ");
6332 else
6333 uiout->text ("\tstop only if ");
6334 uiout->field_string ("cond", b->cond_string);
6335
6336 /* Print whether the target is doing the breakpoint's condition
6337 evaluation. If GDB is doing the evaluation, don't print anything. */
6338 if (is_breakpoint (b)
6339 && breakpoint_condition_evaluation_mode ()
6340 == condition_evaluation_target)
6341 {
6342 uiout->text (" (");
6343 uiout->field_string ("evaluated-by",
6344 bp_condition_evaluator (b));
6345 uiout->text (" evals)");
6346 }
6347 uiout->text ("\n");
6348 }
6349
6350 if (!part_of_multiple && b->thread != -1)
6351 {
6352 /* FIXME should make an annotation for this. */
6353 uiout->text ("\tstop only in thread ");
6354 if (uiout->is_mi_like_p ())
6355 uiout->field_int ("thread", b->thread);
6356 else
6357 {
6358 struct thread_info *thr = find_thread_global_id (b->thread);
6359
6360 uiout->field_string ("thread", print_thread_id (thr));
6361 }
6362 uiout->text ("\n");
6363 }
6364
6365 if (!part_of_multiple)
6366 {
6367 if (b->hit_count)
6368 {
6369 /* FIXME should make an annotation for this. */
6370 if (is_catchpoint (b))
6371 uiout->text ("\tcatchpoint");
6372 else if (is_tracepoint (b))
6373 uiout->text ("\ttracepoint");
6374 else
6375 uiout->text ("\tbreakpoint");
6376 uiout->text (" already hit ");
6377 uiout->field_int ("times", b->hit_count);
6378 if (b->hit_count == 1)
6379 uiout->text (" time\n");
6380 else
6381 uiout->text (" times\n");
6382 }
6383 else
6384 {
6385 /* Output the count also if it is zero, but only if this is mi. */
6386 if (uiout->is_mi_like_p ())
6387 uiout->field_int ("times", b->hit_count);
6388 }
6389 }
6390
6391 if (!part_of_multiple && b->ignore_count)
6392 {
6393 annotate_field (8);
6394 uiout->text ("\tignore next ");
6395 uiout->field_int ("ignore", b->ignore_count);
6396 uiout->text (" hits\n");
6397 }
6398
6399 /* Note that an enable count of 1 corresponds to "enable once"
6400 behavior, which is reported by the combination of enablement and
6401 disposition, so we don't need to mention it here. */
6402 if (!part_of_multiple && b->enable_count > 1)
6403 {
6404 annotate_field (8);
6405 uiout->text ("\tdisable after ");
6406 /* Tweak the wording to clarify that ignore and enable counts
6407 are distinct, and have additive effect. */
6408 if (b->ignore_count)
6409 uiout->text ("additional ");
6410 else
6411 uiout->text ("next ");
6412 uiout->field_int ("enable", b->enable_count);
6413 uiout->text (" hits\n");
6414 }
6415
6416 if (!part_of_multiple && is_tracepoint (b))
6417 {
6418 struct tracepoint *tp = (struct tracepoint *) b;
6419
6420 if (tp->traceframe_usage)
6421 {
6422 uiout->text ("\ttrace buffer usage ");
6423 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6424 uiout->text (" bytes\n");
6425 }
6426 }
6427
6428 l = b->commands ? b->commands.get () : NULL;
6429 if (!part_of_multiple && l)
6430 {
6431 annotate_field (9);
6432 ui_out_emit_tuple tuple_emitter (uiout, "script");
6433 print_command_lines (uiout, l, 4);
6434 }
6435
6436 if (is_tracepoint (b))
6437 {
6438 struct tracepoint *t = (struct tracepoint *) b;
6439
6440 if (!part_of_multiple && t->pass_count)
6441 {
6442 annotate_field (10);
6443 uiout->text ("\tpass count ");
6444 uiout->field_int ("pass", t->pass_count);
6445 uiout->text (" \n");
6446 }
6447
6448 /* Don't display it when tracepoint or tracepoint location is
6449 pending. */
6450 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6451 {
6452 annotate_field (11);
6453
6454 if (uiout->is_mi_like_p ())
6455 uiout->field_string ("installed",
6456 loc->inserted ? "y" : "n");
6457 else
6458 {
6459 if (loc->inserted)
6460 uiout->text ("\t");
6461 else
6462 uiout->text ("\tnot ");
6463 uiout->text ("installed on target\n");
6464 }
6465 }
6466 }
6467
6468 if (uiout->is_mi_like_p () && !part_of_multiple)
6469 {
6470 if (is_watchpoint (b))
6471 {
6472 struct watchpoint *w = (struct watchpoint *) b;
6473
6474 uiout->field_string ("original-location", w->exp_string);
6475 }
6476 else if (b->location != NULL
6477 && event_location_to_string (b->location.get ()) != NULL)
6478 uiout->field_string ("original-location",
6479 event_location_to_string (b->location.get ()));
6480 }
6481 }
6482
6483 static void
6484 print_one_breakpoint (struct breakpoint *b,
6485 struct bp_location **last_loc,
6486 int allflag)
6487 {
6488 struct ui_out *uiout = current_uiout;
6489
6490 {
6491 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6492
6493 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6494 }
6495
6496 /* If this breakpoint has custom print function,
6497 it's already printed. Otherwise, print individual
6498 locations, if any. */
6499 if (b->ops == NULL || b->ops->print_one == NULL)
6500 {
6501 /* If breakpoint has a single location that is disabled, we
6502 print it as if it had several locations, since otherwise it's
6503 hard to represent "breakpoint enabled, location disabled"
6504 situation.
6505
6506 Note that while hardware watchpoints have several locations
6507 internally, that's not a property exposed to user. */
6508 if (b->loc
6509 && !is_hardware_watchpoint (b)
6510 && (b->loc->next || !b->loc->enabled))
6511 {
6512 struct bp_location *loc;
6513 int n = 1;
6514
6515 for (loc = b->loc; loc; loc = loc->next, ++n)
6516 {
6517 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6518 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6519 }
6520 }
6521 }
6522 }
6523
6524 static int
6525 breakpoint_address_bits (struct breakpoint *b)
6526 {
6527 int print_address_bits = 0;
6528 struct bp_location *loc;
6529
6530 /* Software watchpoints that aren't watching memory don't have an
6531 address to print. */
6532 if (is_no_memory_software_watchpoint (b))
6533 return 0;
6534
6535 for (loc = b->loc; loc; loc = loc->next)
6536 {
6537 int addr_bit;
6538
6539 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6540 if (addr_bit > print_address_bits)
6541 print_address_bits = addr_bit;
6542 }
6543
6544 return print_address_bits;
6545 }
6546
6547 /* See breakpoint.h. */
6548
6549 void
6550 print_breakpoint (breakpoint *b)
6551 {
6552 struct bp_location *dummy_loc = NULL;
6553 print_one_breakpoint (b, &dummy_loc, 0);
6554 }
6555
6556 /* Return true if this breakpoint was set by the user, false if it is
6557 internal or momentary. */
6558
6559 int
6560 user_breakpoint_p (struct breakpoint *b)
6561 {
6562 return b->number > 0;
6563 }
6564
6565 /* See breakpoint.h. */
6566
6567 int
6568 pending_breakpoint_p (struct breakpoint *b)
6569 {
6570 return b->loc == NULL;
6571 }
6572
6573 /* Print information on user settable breakpoint (watchpoint, etc)
6574 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6575 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6576 FILTER is non-NULL, call it on each breakpoint and only include the
6577 ones for which it returns non-zero. Return the total number of
6578 breakpoints listed. */
6579
6580 static int
6581 breakpoint_1 (const char *args, int allflag,
6582 int (*filter) (const struct breakpoint *))
6583 {
6584 struct breakpoint *b;
6585 struct bp_location *last_loc = NULL;
6586 int nr_printable_breakpoints;
6587 struct value_print_options opts;
6588 int print_address_bits = 0;
6589 int print_type_col_width = 14;
6590 struct ui_out *uiout = current_uiout;
6591
6592 get_user_print_options (&opts);
6593
6594 /* Compute the number of rows in the table, as well as the size
6595 required for address fields. */
6596 nr_printable_breakpoints = 0;
6597 ALL_BREAKPOINTS (b)
6598 {
6599 /* If we have a filter, only list the breakpoints it accepts. */
6600 if (filter && !filter (b))
6601 continue;
6602
6603 /* If we have an "args" string, it is a list of breakpoints to
6604 accept. Skip the others. */
6605 if (args != NULL && *args != '\0')
6606 {
6607 if (allflag && parse_and_eval_long (args) != b->number)
6608 continue;
6609 if (!allflag && !number_is_in_list (args, b->number))
6610 continue;
6611 }
6612
6613 if (allflag || user_breakpoint_p (b))
6614 {
6615 int addr_bit, type_len;
6616
6617 addr_bit = breakpoint_address_bits (b);
6618 if (addr_bit > print_address_bits)
6619 print_address_bits = addr_bit;
6620
6621 type_len = strlen (bptype_string (b->type));
6622 if (type_len > print_type_col_width)
6623 print_type_col_width = type_len;
6624
6625 nr_printable_breakpoints++;
6626 }
6627 }
6628
6629 {
6630 ui_out_emit_table table_emitter (uiout,
6631 opts.addressprint ? 6 : 5,
6632 nr_printable_breakpoints,
6633 "BreakpointTable");
6634
6635 if (nr_printable_breakpoints > 0)
6636 annotate_breakpoints_headers ();
6637 if (nr_printable_breakpoints > 0)
6638 annotate_field (0);
6639 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6640 if (nr_printable_breakpoints > 0)
6641 annotate_field (1);
6642 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6643 if (nr_printable_breakpoints > 0)
6644 annotate_field (2);
6645 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6646 if (nr_printable_breakpoints > 0)
6647 annotate_field (3);
6648 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6649 if (opts.addressprint)
6650 {
6651 if (nr_printable_breakpoints > 0)
6652 annotate_field (4);
6653 if (print_address_bits <= 32)
6654 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6655 else
6656 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6657 }
6658 if (nr_printable_breakpoints > 0)
6659 annotate_field (5);
6660 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6661 uiout->table_body ();
6662 if (nr_printable_breakpoints > 0)
6663 annotate_breakpoints_table ();
6664
6665 ALL_BREAKPOINTS (b)
6666 {
6667 QUIT;
6668 /* If we have a filter, only list the breakpoints it accepts. */
6669 if (filter && !filter (b))
6670 continue;
6671
6672 /* If we have an "args" string, it is a list of breakpoints to
6673 accept. Skip the others. */
6674
6675 if (args != NULL && *args != '\0')
6676 {
6677 if (allflag) /* maintenance info breakpoint */
6678 {
6679 if (parse_and_eval_long (args) != b->number)
6680 continue;
6681 }
6682 else /* all others */
6683 {
6684 if (!number_is_in_list (args, b->number))
6685 continue;
6686 }
6687 }
6688 /* We only print out user settable breakpoints unless the
6689 allflag is set. */
6690 if (allflag || user_breakpoint_p (b))
6691 print_one_breakpoint (b, &last_loc, allflag);
6692 }
6693 }
6694
6695 if (nr_printable_breakpoints == 0)
6696 {
6697 /* If there's a filter, let the caller decide how to report
6698 empty list. */
6699 if (!filter)
6700 {
6701 if (args == NULL || *args == '\0')
6702 uiout->message ("No breakpoints or watchpoints.\n");
6703 else
6704 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6705 args);
6706 }
6707 }
6708 else
6709 {
6710 if (last_loc && !server_command)
6711 set_next_address (last_loc->gdbarch, last_loc->address);
6712 }
6713
6714 /* FIXME? Should this be moved up so that it is only called when
6715 there have been breakpoints? */
6716 annotate_breakpoints_table_end ();
6717
6718 return nr_printable_breakpoints;
6719 }
6720
6721 /* Display the value of default-collect in a way that is generally
6722 compatible with the breakpoint list. */
6723
6724 static void
6725 default_collect_info (void)
6726 {
6727 struct ui_out *uiout = current_uiout;
6728
6729 /* If it has no value (which is frequently the case), say nothing; a
6730 message like "No default-collect." gets in user's face when it's
6731 not wanted. */
6732 if (!*default_collect)
6733 return;
6734
6735 /* The following phrase lines up nicely with per-tracepoint collect
6736 actions. */
6737 uiout->text ("default collect ");
6738 uiout->field_string ("default-collect", default_collect);
6739 uiout->text (" \n");
6740 }
6741
6742 static void
6743 info_breakpoints_command (char *args, int from_tty)
6744 {
6745 breakpoint_1 (args, 0, NULL);
6746
6747 default_collect_info ();
6748 }
6749
6750 static void
6751 info_watchpoints_command (char *args, int from_tty)
6752 {
6753 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6754 struct ui_out *uiout = current_uiout;
6755
6756 if (num_printed == 0)
6757 {
6758 if (args == NULL || *args == '\0')
6759 uiout->message ("No watchpoints.\n");
6760 else
6761 uiout->message ("No watchpoint matching '%s'.\n", args);
6762 }
6763 }
6764
6765 static void
6766 maintenance_info_breakpoints (const char *args, int from_tty)
6767 {
6768 breakpoint_1 (args, 1, NULL);
6769
6770 default_collect_info ();
6771 }
6772
6773 static int
6774 breakpoint_has_pc (struct breakpoint *b,
6775 struct program_space *pspace,
6776 CORE_ADDR pc, struct obj_section *section)
6777 {
6778 struct bp_location *bl = b->loc;
6779
6780 for (; bl; bl = bl->next)
6781 {
6782 if (bl->pspace == pspace
6783 && bl->address == pc
6784 && (!overlay_debugging || bl->section == section))
6785 return 1;
6786 }
6787 return 0;
6788 }
6789
6790 /* Print a message describing any user-breakpoints set at PC. This
6791 concerns with logical breakpoints, so we match program spaces, not
6792 address spaces. */
6793
6794 static void
6795 describe_other_breakpoints (struct gdbarch *gdbarch,
6796 struct program_space *pspace, CORE_ADDR pc,
6797 struct obj_section *section, int thread)
6798 {
6799 int others = 0;
6800 struct breakpoint *b;
6801
6802 ALL_BREAKPOINTS (b)
6803 others += (user_breakpoint_p (b)
6804 && breakpoint_has_pc (b, pspace, pc, section));
6805 if (others > 0)
6806 {
6807 if (others == 1)
6808 printf_filtered (_("Note: breakpoint "));
6809 else /* if (others == ???) */
6810 printf_filtered (_("Note: breakpoints "));
6811 ALL_BREAKPOINTS (b)
6812 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6813 {
6814 others--;
6815 printf_filtered ("%d", b->number);
6816 if (b->thread == -1 && thread != -1)
6817 printf_filtered (" (all threads)");
6818 else if (b->thread != -1)
6819 printf_filtered (" (thread %d)", b->thread);
6820 printf_filtered ("%s%s ",
6821 ((b->enable_state == bp_disabled
6822 || b->enable_state == bp_call_disabled)
6823 ? " (disabled)"
6824 : ""),
6825 (others > 1) ? ","
6826 : ((others == 1) ? " and" : ""));
6827 }
6828 printf_filtered (_("also set at pc "));
6829 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6830 printf_filtered (".\n");
6831 }
6832 }
6833 \f
6834
6835 /* Return true iff it is meaningful to use the address member of
6836 BPT locations. For some breakpoint types, the locations' address members
6837 are irrelevant and it makes no sense to attempt to compare them to other
6838 addresses (or use them for any other purpose either).
6839
6840 More specifically, each of the following breakpoint types will
6841 always have a zero valued location address and we don't want to mark
6842 breakpoints of any of these types to be a duplicate of an actual
6843 breakpoint location at address zero:
6844
6845 bp_watchpoint
6846 bp_catchpoint
6847
6848 */
6849
6850 static int
6851 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6852 {
6853 enum bptype type = bpt->type;
6854
6855 return (type != bp_watchpoint && type != bp_catchpoint);
6856 }
6857
6858 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6859 true if LOC1 and LOC2 represent the same watchpoint location. */
6860
6861 static int
6862 watchpoint_locations_match (struct bp_location *loc1,
6863 struct bp_location *loc2)
6864 {
6865 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6866 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6867
6868 /* Both of them must exist. */
6869 gdb_assert (w1 != NULL);
6870 gdb_assert (w2 != NULL);
6871
6872 /* If the target can evaluate the condition expression in hardware,
6873 then we we need to insert both watchpoints even if they are at
6874 the same place. Otherwise the watchpoint will only trigger when
6875 the condition of whichever watchpoint was inserted evaluates to
6876 true, not giving a chance for GDB to check the condition of the
6877 other watchpoint. */
6878 if ((w1->cond_exp
6879 && target_can_accel_watchpoint_condition (loc1->address,
6880 loc1->length,
6881 loc1->watchpoint_type,
6882 w1->cond_exp.get ()))
6883 || (w2->cond_exp
6884 && target_can_accel_watchpoint_condition (loc2->address,
6885 loc2->length,
6886 loc2->watchpoint_type,
6887 w2->cond_exp.get ())))
6888 return 0;
6889
6890 /* Note that this checks the owner's type, not the location's. In
6891 case the target does not support read watchpoints, but does
6892 support access watchpoints, we'll have bp_read_watchpoint
6893 watchpoints with hw_access locations. Those should be considered
6894 duplicates of hw_read locations. The hw_read locations will
6895 become hw_access locations later. */
6896 return (loc1->owner->type == loc2->owner->type
6897 && loc1->pspace->aspace == loc2->pspace->aspace
6898 && loc1->address == loc2->address
6899 && loc1->length == loc2->length);
6900 }
6901
6902 /* See breakpoint.h. */
6903
6904 int
6905 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6906 struct address_space *aspace2, CORE_ADDR addr2)
6907 {
6908 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6909 || aspace1 == aspace2)
6910 && addr1 == addr2);
6911 }
6912
6913 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6914 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6915 matches ASPACE2. On targets that have global breakpoints, the address
6916 space doesn't really matter. */
6917
6918 static int
6919 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6920 int len1, struct address_space *aspace2,
6921 CORE_ADDR addr2)
6922 {
6923 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6924 || aspace1 == aspace2)
6925 && addr2 >= addr1 && addr2 < addr1 + len1);
6926 }
6927
6928 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6929 a ranged breakpoint. In most targets, a match happens only if ASPACE
6930 matches the breakpoint's address space. On targets that have global
6931 breakpoints, the address space doesn't really matter. */
6932
6933 static int
6934 breakpoint_location_address_match (struct bp_location *bl,
6935 struct address_space *aspace,
6936 CORE_ADDR addr)
6937 {
6938 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6939 aspace, addr)
6940 || (bl->length
6941 && breakpoint_address_match_range (bl->pspace->aspace,
6942 bl->address, bl->length,
6943 aspace, addr)));
6944 }
6945
6946 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6947 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6948 match happens only if ASPACE matches the breakpoint's address
6949 space. On targets that have global breakpoints, the address space
6950 doesn't really matter. */
6951
6952 static int
6953 breakpoint_location_address_range_overlap (struct bp_location *bl,
6954 struct address_space *aspace,
6955 CORE_ADDR addr, int len)
6956 {
6957 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6958 || bl->pspace->aspace == aspace)
6959 {
6960 int bl_len = bl->length != 0 ? bl->length : 1;
6961
6962 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6963 return 1;
6964 }
6965 return 0;
6966 }
6967
6968 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6969 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6970 true, otherwise returns false. */
6971
6972 static int
6973 tracepoint_locations_match (struct bp_location *loc1,
6974 struct bp_location *loc2)
6975 {
6976 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6977 /* Since tracepoint locations are never duplicated with others', tracepoint
6978 locations at the same address of different tracepoints are regarded as
6979 different locations. */
6980 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6981 else
6982 return 0;
6983 }
6984
6985 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6986 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6987 represent the same location. */
6988
6989 static int
6990 breakpoint_locations_match (struct bp_location *loc1,
6991 struct bp_location *loc2)
6992 {
6993 int hw_point1, hw_point2;
6994
6995 /* Both of them must not be in moribund_locations. */
6996 gdb_assert (loc1->owner != NULL);
6997 gdb_assert (loc2->owner != NULL);
6998
6999 hw_point1 = is_hardware_watchpoint (loc1->owner);
7000 hw_point2 = is_hardware_watchpoint (loc2->owner);
7001
7002 if (hw_point1 != hw_point2)
7003 return 0;
7004 else if (hw_point1)
7005 return watchpoint_locations_match (loc1, loc2);
7006 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7007 return tracepoint_locations_match (loc1, loc2);
7008 else
7009 /* We compare bp_location.length in order to cover ranged breakpoints. */
7010 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7011 loc2->pspace->aspace, loc2->address)
7012 && loc1->length == loc2->length);
7013 }
7014
7015 static void
7016 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7017 int bnum, int have_bnum)
7018 {
7019 /* The longest string possibly returned by hex_string_custom
7020 is 50 chars. These must be at least that big for safety. */
7021 char astr1[64];
7022 char astr2[64];
7023
7024 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7025 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7026 if (have_bnum)
7027 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7028 bnum, astr1, astr2);
7029 else
7030 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7031 }
7032
7033 /* Adjust a breakpoint's address to account for architectural
7034 constraints on breakpoint placement. Return the adjusted address.
7035 Note: Very few targets require this kind of adjustment. For most
7036 targets, this function is simply the identity function. */
7037
7038 static CORE_ADDR
7039 adjust_breakpoint_address (struct gdbarch *gdbarch,
7040 CORE_ADDR bpaddr, enum bptype bptype)
7041 {
7042 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7043 {
7044 /* Very few targets need any kind of breakpoint adjustment. */
7045 return bpaddr;
7046 }
7047 else if (bptype == bp_watchpoint
7048 || bptype == bp_hardware_watchpoint
7049 || bptype == bp_read_watchpoint
7050 || bptype == bp_access_watchpoint
7051 || bptype == bp_catchpoint)
7052 {
7053 /* Watchpoints and the various bp_catch_* eventpoints should not
7054 have their addresses modified. */
7055 return bpaddr;
7056 }
7057 else if (bptype == bp_single_step)
7058 {
7059 /* Single-step breakpoints should not have their addresses
7060 modified. If there's any architectural constrain that
7061 applies to this address, then it should have already been
7062 taken into account when the breakpoint was created in the
7063 first place. If we didn't do this, stepping through e.g.,
7064 Thumb-2 IT blocks would break. */
7065 return bpaddr;
7066 }
7067 else
7068 {
7069 CORE_ADDR adjusted_bpaddr;
7070
7071 /* Some targets have architectural constraints on the placement
7072 of breakpoint instructions. Obtain the adjusted address. */
7073 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7074
7075 /* An adjusted breakpoint address can significantly alter
7076 a user's expectations. Print a warning if an adjustment
7077 is required. */
7078 if (adjusted_bpaddr != bpaddr)
7079 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7080
7081 return adjusted_bpaddr;
7082 }
7083 }
7084
7085 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7086 {
7087 bp_location *loc = this;
7088
7089 gdb_assert (ops != NULL);
7090
7091 loc->ops = ops;
7092 loc->owner = owner;
7093 loc->cond_bytecode = NULL;
7094 loc->shlib_disabled = 0;
7095 loc->enabled = 1;
7096
7097 switch (owner->type)
7098 {
7099 case bp_breakpoint:
7100 case bp_single_step:
7101 case bp_until:
7102 case bp_finish:
7103 case bp_longjmp:
7104 case bp_longjmp_resume:
7105 case bp_longjmp_call_dummy:
7106 case bp_exception:
7107 case bp_exception_resume:
7108 case bp_step_resume:
7109 case bp_hp_step_resume:
7110 case bp_watchpoint_scope:
7111 case bp_call_dummy:
7112 case bp_std_terminate:
7113 case bp_shlib_event:
7114 case bp_thread_event:
7115 case bp_overlay_event:
7116 case bp_jit_event:
7117 case bp_longjmp_master:
7118 case bp_std_terminate_master:
7119 case bp_exception_master:
7120 case bp_gnu_ifunc_resolver:
7121 case bp_gnu_ifunc_resolver_return:
7122 case bp_dprintf:
7123 loc->loc_type = bp_loc_software_breakpoint;
7124 mark_breakpoint_location_modified (loc);
7125 break;
7126 case bp_hardware_breakpoint:
7127 loc->loc_type = bp_loc_hardware_breakpoint;
7128 mark_breakpoint_location_modified (loc);
7129 break;
7130 case bp_hardware_watchpoint:
7131 case bp_read_watchpoint:
7132 case bp_access_watchpoint:
7133 loc->loc_type = bp_loc_hardware_watchpoint;
7134 break;
7135 case bp_watchpoint:
7136 case bp_catchpoint:
7137 case bp_tracepoint:
7138 case bp_fast_tracepoint:
7139 case bp_static_tracepoint:
7140 loc->loc_type = bp_loc_other;
7141 break;
7142 default:
7143 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7144 }
7145
7146 loc->refc = 1;
7147 }
7148
7149 /* Allocate a struct bp_location. */
7150
7151 static struct bp_location *
7152 allocate_bp_location (struct breakpoint *bpt)
7153 {
7154 return bpt->ops->allocate_location (bpt);
7155 }
7156
7157 static void
7158 free_bp_location (struct bp_location *loc)
7159 {
7160 loc->ops->dtor (loc);
7161 delete loc;
7162 }
7163
7164 /* Increment reference count. */
7165
7166 static void
7167 incref_bp_location (struct bp_location *bl)
7168 {
7169 ++bl->refc;
7170 }
7171
7172 /* Decrement reference count. If the reference count reaches 0,
7173 destroy the bp_location. Sets *BLP to NULL. */
7174
7175 static void
7176 decref_bp_location (struct bp_location **blp)
7177 {
7178 gdb_assert ((*blp)->refc > 0);
7179
7180 if (--(*blp)->refc == 0)
7181 free_bp_location (*blp);
7182 *blp = NULL;
7183 }
7184
7185 /* Add breakpoint B at the end of the global breakpoint chain. */
7186
7187 static breakpoint *
7188 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7189 {
7190 struct breakpoint *b1;
7191 struct breakpoint *result = b.get ();
7192
7193 /* Add this breakpoint to the end of the chain so that a list of
7194 breakpoints will come out in order of increasing numbers. */
7195
7196 b1 = breakpoint_chain;
7197 if (b1 == 0)
7198 breakpoint_chain = b.release ();
7199 else
7200 {
7201 while (b1->next)
7202 b1 = b1->next;
7203 b1->next = b.release ();
7204 }
7205
7206 return result;
7207 }
7208
7209 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7210
7211 static void
7212 init_raw_breakpoint_without_location (struct breakpoint *b,
7213 struct gdbarch *gdbarch,
7214 enum bptype bptype,
7215 const struct breakpoint_ops *ops)
7216 {
7217 gdb_assert (ops != NULL);
7218
7219 b->ops = ops;
7220 b->type = bptype;
7221 b->gdbarch = gdbarch;
7222 b->language = current_language->la_language;
7223 b->input_radix = input_radix;
7224 b->related_breakpoint = b;
7225 }
7226
7227 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7228 that has type BPTYPE and has no locations as yet. */
7229
7230 static struct breakpoint *
7231 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7232 enum bptype bptype,
7233 const struct breakpoint_ops *ops)
7234 {
7235 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7236
7237 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7238 return add_to_breakpoint_chain (std::move (b));
7239 }
7240
7241 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7242 resolutions should be made as the user specified the location explicitly
7243 enough. */
7244
7245 static void
7246 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7247 {
7248 gdb_assert (loc->owner != NULL);
7249
7250 if (loc->owner->type == bp_breakpoint
7251 || loc->owner->type == bp_hardware_breakpoint
7252 || is_tracepoint (loc->owner))
7253 {
7254 int is_gnu_ifunc;
7255 const char *function_name;
7256 CORE_ADDR func_addr;
7257
7258 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7259 &func_addr, NULL, &is_gnu_ifunc);
7260
7261 if (is_gnu_ifunc && !explicit_loc)
7262 {
7263 struct breakpoint *b = loc->owner;
7264
7265 gdb_assert (loc->pspace == current_program_space);
7266 if (gnu_ifunc_resolve_name (function_name,
7267 &loc->requested_address))
7268 {
7269 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7270 loc->address = adjust_breakpoint_address (loc->gdbarch,
7271 loc->requested_address,
7272 b->type);
7273 }
7274 else if (b->type == bp_breakpoint && b->loc == loc
7275 && loc->next == NULL && b->related_breakpoint == b)
7276 {
7277 /* Create only the whole new breakpoint of this type but do not
7278 mess more complicated breakpoints with multiple locations. */
7279 b->type = bp_gnu_ifunc_resolver;
7280 /* Remember the resolver's address for use by the return
7281 breakpoint. */
7282 loc->related_address = func_addr;
7283 }
7284 }
7285
7286 if (function_name)
7287 loc->function_name = xstrdup (function_name);
7288 }
7289 }
7290
7291 /* Attempt to determine architecture of location identified by SAL. */
7292 struct gdbarch *
7293 get_sal_arch (struct symtab_and_line sal)
7294 {
7295 if (sal.section)
7296 return get_objfile_arch (sal.section->objfile);
7297 if (sal.symtab)
7298 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7299
7300 return NULL;
7301 }
7302
7303 /* Low level routine for partially initializing a breakpoint of type
7304 BPTYPE. The newly created breakpoint's address, section, source
7305 file name, and line number are provided by SAL.
7306
7307 It is expected that the caller will complete the initialization of
7308 the newly created breakpoint struct as well as output any status
7309 information regarding the creation of a new breakpoint. */
7310
7311 static void
7312 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7313 struct symtab_and_line sal, enum bptype bptype,
7314 const struct breakpoint_ops *ops)
7315 {
7316 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7317
7318 add_location_to_breakpoint (b, &sal);
7319
7320 if (bptype != bp_catchpoint)
7321 gdb_assert (sal.pspace != NULL);
7322
7323 /* Store the program space that was used to set the breakpoint,
7324 except for ordinary breakpoints, which are independent of the
7325 program space. */
7326 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7327 b->pspace = sal.pspace;
7328 }
7329
7330 /* set_raw_breakpoint is a low level routine for allocating and
7331 partially initializing a breakpoint of type BPTYPE. The newly
7332 created breakpoint's address, section, source file name, and line
7333 number are provided by SAL. The newly created and partially
7334 initialized breakpoint is added to the breakpoint chain and
7335 is also returned as the value of this function.
7336
7337 It is expected that the caller will complete the initialization of
7338 the newly created breakpoint struct as well as output any status
7339 information regarding the creation of a new breakpoint. In
7340 particular, set_raw_breakpoint does NOT set the breakpoint
7341 number! Care should be taken to not allow an error to occur
7342 prior to completing the initialization of the breakpoint. If this
7343 should happen, a bogus breakpoint will be left on the chain. */
7344
7345 struct breakpoint *
7346 set_raw_breakpoint (struct gdbarch *gdbarch,
7347 struct symtab_and_line sal, enum bptype bptype,
7348 const struct breakpoint_ops *ops)
7349 {
7350 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7351
7352 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7353 return add_to_breakpoint_chain (std::move (b));
7354 }
7355
7356 /* Call this routine when stepping and nexting to enable a breakpoint
7357 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7358 initiated the operation. */
7359
7360 void
7361 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7362 {
7363 struct breakpoint *b, *b_tmp;
7364 int thread = tp->global_num;
7365
7366 /* To avoid having to rescan all objfile symbols at every step,
7367 we maintain a list of continually-inserted but always disabled
7368 longjmp "master" breakpoints. Here, we simply create momentary
7369 clones of those and enable them for the requested thread. */
7370 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7371 if (b->pspace == current_program_space
7372 && (b->type == bp_longjmp_master
7373 || b->type == bp_exception_master))
7374 {
7375 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7376 struct breakpoint *clone;
7377
7378 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7379 after their removal. */
7380 clone = momentary_breakpoint_from_master (b, type,
7381 &momentary_breakpoint_ops, 1);
7382 clone->thread = thread;
7383 }
7384
7385 tp->initiating_frame = frame;
7386 }
7387
7388 /* Delete all longjmp breakpoints from THREAD. */
7389 void
7390 delete_longjmp_breakpoint (int thread)
7391 {
7392 struct breakpoint *b, *b_tmp;
7393
7394 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7395 if (b->type == bp_longjmp || b->type == bp_exception)
7396 {
7397 if (b->thread == thread)
7398 delete_breakpoint (b);
7399 }
7400 }
7401
7402 void
7403 delete_longjmp_breakpoint_at_next_stop (int thread)
7404 {
7405 struct breakpoint *b, *b_tmp;
7406
7407 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7408 if (b->type == bp_longjmp || b->type == bp_exception)
7409 {
7410 if (b->thread == thread)
7411 b->disposition = disp_del_at_next_stop;
7412 }
7413 }
7414
7415 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7416 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7417 pointer to any of them. Return NULL if this system cannot place longjmp
7418 breakpoints. */
7419
7420 struct breakpoint *
7421 set_longjmp_breakpoint_for_call_dummy (void)
7422 {
7423 struct breakpoint *b, *retval = NULL;
7424
7425 ALL_BREAKPOINTS (b)
7426 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7427 {
7428 struct breakpoint *new_b;
7429
7430 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7431 &momentary_breakpoint_ops,
7432 1);
7433 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7434
7435 /* Link NEW_B into the chain of RETVAL breakpoints. */
7436
7437 gdb_assert (new_b->related_breakpoint == new_b);
7438 if (retval == NULL)
7439 retval = new_b;
7440 new_b->related_breakpoint = retval;
7441 while (retval->related_breakpoint != new_b->related_breakpoint)
7442 retval = retval->related_breakpoint;
7443 retval->related_breakpoint = new_b;
7444 }
7445
7446 return retval;
7447 }
7448
7449 /* Verify all existing dummy frames and their associated breakpoints for
7450 TP. Remove those which can no longer be found in the current frame
7451 stack.
7452
7453 You should call this function only at places where it is safe to currently
7454 unwind the whole stack. Failed stack unwind would discard live dummy
7455 frames. */
7456
7457 void
7458 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7459 {
7460 struct breakpoint *b, *b_tmp;
7461
7462 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7463 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7464 {
7465 struct breakpoint *dummy_b = b->related_breakpoint;
7466
7467 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7468 dummy_b = dummy_b->related_breakpoint;
7469 if (dummy_b->type != bp_call_dummy
7470 || frame_find_by_id (dummy_b->frame_id) != NULL)
7471 continue;
7472
7473 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7474
7475 while (b->related_breakpoint != b)
7476 {
7477 if (b_tmp == b->related_breakpoint)
7478 b_tmp = b->related_breakpoint->next;
7479 delete_breakpoint (b->related_breakpoint);
7480 }
7481 delete_breakpoint (b);
7482 }
7483 }
7484
7485 void
7486 enable_overlay_breakpoints (void)
7487 {
7488 struct breakpoint *b;
7489
7490 ALL_BREAKPOINTS (b)
7491 if (b->type == bp_overlay_event)
7492 {
7493 b->enable_state = bp_enabled;
7494 update_global_location_list (UGLL_MAY_INSERT);
7495 overlay_events_enabled = 1;
7496 }
7497 }
7498
7499 void
7500 disable_overlay_breakpoints (void)
7501 {
7502 struct breakpoint *b;
7503
7504 ALL_BREAKPOINTS (b)
7505 if (b->type == bp_overlay_event)
7506 {
7507 b->enable_state = bp_disabled;
7508 update_global_location_list (UGLL_DONT_INSERT);
7509 overlay_events_enabled = 0;
7510 }
7511 }
7512
7513 /* Set an active std::terminate breakpoint for each std::terminate
7514 master breakpoint. */
7515 void
7516 set_std_terminate_breakpoint (void)
7517 {
7518 struct breakpoint *b, *b_tmp;
7519
7520 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7521 if (b->pspace == current_program_space
7522 && b->type == bp_std_terminate_master)
7523 {
7524 momentary_breakpoint_from_master (b, bp_std_terminate,
7525 &momentary_breakpoint_ops, 1);
7526 }
7527 }
7528
7529 /* Delete all the std::terminate breakpoints. */
7530 void
7531 delete_std_terminate_breakpoint (void)
7532 {
7533 struct breakpoint *b, *b_tmp;
7534
7535 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7536 if (b->type == bp_std_terminate)
7537 delete_breakpoint (b);
7538 }
7539
7540 struct breakpoint *
7541 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7542 {
7543 struct breakpoint *b;
7544
7545 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7546 &internal_breakpoint_ops);
7547
7548 b->enable_state = bp_enabled;
7549 /* location has to be used or breakpoint_re_set will delete me. */
7550 b->location = new_address_location (b->loc->address, NULL, 0);
7551
7552 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7553
7554 return b;
7555 }
7556
7557 struct lang_and_radix
7558 {
7559 enum language lang;
7560 int radix;
7561 };
7562
7563 /* Create a breakpoint for JIT code registration and unregistration. */
7564
7565 struct breakpoint *
7566 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7567 {
7568 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7569 &internal_breakpoint_ops);
7570 }
7571
7572 /* Remove JIT code registration and unregistration breakpoint(s). */
7573
7574 void
7575 remove_jit_event_breakpoints (void)
7576 {
7577 struct breakpoint *b, *b_tmp;
7578
7579 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7580 if (b->type == bp_jit_event
7581 && b->loc->pspace == current_program_space)
7582 delete_breakpoint (b);
7583 }
7584
7585 void
7586 remove_solib_event_breakpoints (void)
7587 {
7588 struct breakpoint *b, *b_tmp;
7589
7590 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7591 if (b->type == bp_shlib_event
7592 && b->loc->pspace == current_program_space)
7593 delete_breakpoint (b);
7594 }
7595
7596 /* See breakpoint.h. */
7597
7598 void
7599 remove_solib_event_breakpoints_at_next_stop (void)
7600 {
7601 struct breakpoint *b, *b_tmp;
7602
7603 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7604 if (b->type == bp_shlib_event
7605 && b->loc->pspace == current_program_space)
7606 b->disposition = disp_del_at_next_stop;
7607 }
7608
7609 /* Helper for create_solib_event_breakpoint /
7610 create_and_insert_solib_event_breakpoint. Allows specifying which
7611 INSERT_MODE to pass through to update_global_location_list. */
7612
7613 static struct breakpoint *
7614 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7615 enum ugll_insert_mode insert_mode)
7616 {
7617 struct breakpoint *b;
7618
7619 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7620 &internal_breakpoint_ops);
7621 update_global_location_list_nothrow (insert_mode);
7622 return b;
7623 }
7624
7625 struct breakpoint *
7626 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7627 {
7628 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7629 }
7630
7631 /* See breakpoint.h. */
7632
7633 struct breakpoint *
7634 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7635 {
7636 struct breakpoint *b;
7637
7638 /* Explicitly tell update_global_location_list to insert
7639 locations. */
7640 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7641 if (!b->loc->inserted)
7642 {
7643 delete_breakpoint (b);
7644 return NULL;
7645 }
7646 return b;
7647 }
7648
7649 /* Disable any breakpoints that are on code in shared libraries. Only
7650 apply to enabled breakpoints, disabled ones can just stay disabled. */
7651
7652 void
7653 disable_breakpoints_in_shlibs (void)
7654 {
7655 struct bp_location *loc, **locp_tmp;
7656
7657 ALL_BP_LOCATIONS (loc, locp_tmp)
7658 {
7659 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7660 struct breakpoint *b = loc->owner;
7661
7662 /* We apply the check to all breakpoints, including disabled for
7663 those with loc->duplicate set. This is so that when breakpoint
7664 becomes enabled, or the duplicate is removed, gdb will try to
7665 insert all breakpoints. If we don't set shlib_disabled here,
7666 we'll try to insert those breakpoints and fail. */
7667 if (((b->type == bp_breakpoint)
7668 || (b->type == bp_jit_event)
7669 || (b->type == bp_hardware_breakpoint)
7670 || (is_tracepoint (b)))
7671 && loc->pspace == current_program_space
7672 && !loc->shlib_disabled
7673 && solib_name_from_address (loc->pspace, loc->address)
7674 )
7675 {
7676 loc->shlib_disabled = 1;
7677 }
7678 }
7679 }
7680
7681 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7682 notification of unloaded_shlib. Only apply to enabled breakpoints,
7683 disabled ones can just stay disabled. */
7684
7685 static void
7686 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7687 {
7688 struct bp_location *loc, **locp_tmp;
7689 int disabled_shlib_breaks = 0;
7690
7691 ALL_BP_LOCATIONS (loc, locp_tmp)
7692 {
7693 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7694 struct breakpoint *b = loc->owner;
7695
7696 if (solib->pspace == loc->pspace
7697 && !loc->shlib_disabled
7698 && (((b->type == bp_breakpoint
7699 || b->type == bp_jit_event
7700 || b->type == bp_hardware_breakpoint)
7701 && (loc->loc_type == bp_loc_hardware_breakpoint
7702 || loc->loc_type == bp_loc_software_breakpoint))
7703 || is_tracepoint (b))
7704 && solib_contains_address_p (solib, loc->address))
7705 {
7706 loc->shlib_disabled = 1;
7707 /* At this point, we cannot rely on remove_breakpoint
7708 succeeding so we must mark the breakpoint as not inserted
7709 to prevent future errors occurring in remove_breakpoints. */
7710 loc->inserted = 0;
7711
7712 /* This may cause duplicate notifications for the same breakpoint. */
7713 observer_notify_breakpoint_modified (b);
7714
7715 if (!disabled_shlib_breaks)
7716 {
7717 target_terminal::ours_for_output ();
7718 warning (_("Temporarily disabling breakpoints "
7719 "for unloaded shared library \"%s\""),
7720 solib->so_name);
7721 }
7722 disabled_shlib_breaks = 1;
7723 }
7724 }
7725 }
7726
7727 /* Disable any breakpoints and tracepoints in OBJFILE upon
7728 notification of free_objfile. Only apply to enabled breakpoints,
7729 disabled ones can just stay disabled. */
7730
7731 static void
7732 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7733 {
7734 struct breakpoint *b;
7735
7736 if (objfile == NULL)
7737 return;
7738
7739 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7740 managed by the user with add-symbol-file/remove-symbol-file.
7741 Similarly to how breakpoints in shared libraries are handled in
7742 response to "nosharedlibrary", mark breakpoints in such modules
7743 shlib_disabled so they end up uninserted on the next global
7744 location list update. Shared libraries not loaded by the user
7745 aren't handled here -- they're already handled in
7746 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7747 solib_unloaded observer. We skip objfiles that are not
7748 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7749 main objfile). */
7750 if ((objfile->flags & OBJF_SHARED) == 0
7751 || (objfile->flags & OBJF_USERLOADED) == 0)
7752 return;
7753
7754 ALL_BREAKPOINTS (b)
7755 {
7756 struct bp_location *loc;
7757 int bp_modified = 0;
7758
7759 if (!is_breakpoint (b) && !is_tracepoint (b))
7760 continue;
7761
7762 for (loc = b->loc; loc != NULL; loc = loc->next)
7763 {
7764 CORE_ADDR loc_addr = loc->address;
7765
7766 if (loc->loc_type != bp_loc_hardware_breakpoint
7767 && loc->loc_type != bp_loc_software_breakpoint)
7768 continue;
7769
7770 if (loc->shlib_disabled != 0)
7771 continue;
7772
7773 if (objfile->pspace != loc->pspace)
7774 continue;
7775
7776 if (loc->loc_type != bp_loc_hardware_breakpoint
7777 && loc->loc_type != bp_loc_software_breakpoint)
7778 continue;
7779
7780 if (is_addr_in_objfile (loc_addr, objfile))
7781 {
7782 loc->shlib_disabled = 1;
7783 /* At this point, we don't know whether the object was
7784 unmapped from the inferior or not, so leave the
7785 inserted flag alone. We'll handle failure to
7786 uninsert quietly, in case the object was indeed
7787 unmapped. */
7788
7789 mark_breakpoint_location_modified (loc);
7790
7791 bp_modified = 1;
7792 }
7793 }
7794
7795 if (bp_modified)
7796 observer_notify_breakpoint_modified (b);
7797 }
7798 }
7799
7800 /* FORK & VFORK catchpoints. */
7801
7802 /* An instance of this type is used to represent a fork or vfork
7803 catchpoint. A breakpoint is really of this type iff its ops pointer points
7804 to CATCH_FORK_BREAKPOINT_OPS. */
7805
7806 struct fork_catchpoint : public breakpoint
7807 {
7808 /* Process id of a child process whose forking triggered this
7809 catchpoint. This field is only valid immediately after this
7810 catchpoint has triggered. */
7811 ptid_t forked_inferior_pid;
7812 };
7813
7814 /* Implement the "insert" breakpoint_ops method for fork
7815 catchpoints. */
7816
7817 static int
7818 insert_catch_fork (struct bp_location *bl)
7819 {
7820 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7821 }
7822
7823 /* Implement the "remove" breakpoint_ops method for fork
7824 catchpoints. */
7825
7826 static int
7827 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7828 {
7829 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7830 }
7831
7832 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7833 catchpoints. */
7834
7835 static int
7836 breakpoint_hit_catch_fork (const struct bp_location *bl,
7837 struct address_space *aspace, CORE_ADDR bp_addr,
7838 const struct target_waitstatus *ws)
7839 {
7840 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7841
7842 if (ws->kind != TARGET_WAITKIND_FORKED)
7843 return 0;
7844
7845 c->forked_inferior_pid = ws->value.related_pid;
7846 return 1;
7847 }
7848
7849 /* Implement the "print_it" breakpoint_ops method for fork
7850 catchpoints. */
7851
7852 static enum print_stop_action
7853 print_it_catch_fork (bpstat bs)
7854 {
7855 struct ui_out *uiout = current_uiout;
7856 struct breakpoint *b = bs->breakpoint_at;
7857 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7858
7859 annotate_catchpoint (b->number);
7860 maybe_print_thread_hit_breakpoint (uiout);
7861 if (b->disposition == disp_del)
7862 uiout->text ("Temporary catchpoint ");
7863 else
7864 uiout->text ("Catchpoint ");
7865 if (uiout->is_mi_like_p ())
7866 {
7867 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7868 uiout->field_string ("disp", bpdisp_text (b->disposition));
7869 }
7870 uiout->field_int ("bkptno", b->number);
7871 uiout->text (" (forked process ");
7872 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7873 uiout->text ("), ");
7874 return PRINT_SRC_AND_LOC;
7875 }
7876
7877 /* Implement the "print_one" breakpoint_ops method for fork
7878 catchpoints. */
7879
7880 static void
7881 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7882 {
7883 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7884 struct value_print_options opts;
7885 struct ui_out *uiout = current_uiout;
7886
7887 get_user_print_options (&opts);
7888
7889 /* Field 4, the address, is omitted (which makes the columns not
7890 line up too nicely with the headers, but the effect is relatively
7891 readable). */
7892 if (opts.addressprint)
7893 uiout->field_skip ("addr");
7894 annotate_field (5);
7895 uiout->text ("fork");
7896 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7897 {
7898 uiout->text (", process ");
7899 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7900 uiout->spaces (1);
7901 }
7902
7903 if (uiout->is_mi_like_p ())
7904 uiout->field_string ("catch-type", "fork");
7905 }
7906
7907 /* Implement the "print_mention" breakpoint_ops method for fork
7908 catchpoints. */
7909
7910 static void
7911 print_mention_catch_fork (struct breakpoint *b)
7912 {
7913 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7914 }
7915
7916 /* Implement the "print_recreate" breakpoint_ops method for fork
7917 catchpoints. */
7918
7919 static void
7920 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7921 {
7922 fprintf_unfiltered (fp, "catch fork");
7923 print_recreate_thread (b, fp);
7924 }
7925
7926 /* The breakpoint_ops structure to be used in fork catchpoints. */
7927
7928 static struct breakpoint_ops catch_fork_breakpoint_ops;
7929
7930 /* Implement the "insert" breakpoint_ops method for vfork
7931 catchpoints. */
7932
7933 static int
7934 insert_catch_vfork (struct bp_location *bl)
7935 {
7936 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7937 }
7938
7939 /* Implement the "remove" breakpoint_ops method for vfork
7940 catchpoints. */
7941
7942 static int
7943 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7944 {
7945 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7946 }
7947
7948 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7949 catchpoints. */
7950
7951 static int
7952 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7953 struct address_space *aspace, CORE_ADDR bp_addr,
7954 const struct target_waitstatus *ws)
7955 {
7956 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7957
7958 if (ws->kind != TARGET_WAITKIND_VFORKED)
7959 return 0;
7960
7961 c->forked_inferior_pid = ws->value.related_pid;
7962 return 1;
7963 }
7964
7965 /* Implement the "print_it" breakpoint_ops method for vfork
7966 catchpoints. */
7967
7968 static enum print_stop_action
7969 print_it_catch_vfork (bpstat bs)
7970 {
7971 struct ui_out *uiout = current_uiout;
7972 struct breakpoint *b = bs->breakpoint_at;
7973 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7974
7975 annotate_catchpoint (b->number);
7976 maybe_print_thread_hit_breakpoint (uiout);
7977 if (b->disposition == disp_del)
7978 uiout->text ("Temporary catchpoint ");
7979 else
7980 uiout->text ("Catchpoint ");
7981 if (uiout->is_mi_like_p ())
7982 {
7983 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7984 uiout->field_string ("disp", bpdisp_text (b->disposition));
7985 }
7986 uiout->field_int ("bkptno", b->number);
7987 uiout->text (" (vforked process ");
7988 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7989 uiout->text ("), ");
7990 return PRINT_SRC_AND_LOC;
7991 }
7992
7993 /* Implement the "print_one" breakpoint_ops method for vfork
7994 catchpoints. */
7995
7996 static void
7997 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7998 {
7999 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8000 struct value_print_options opts;
8001 struct ui_out *uiout = current_uiout;
8002
8003 get_user_print_options (&opts);
8004 /* Field 4, the address, is omitted (which makes the columns not
8005 line up too nicely with the headers, but the effect is relatively
8006 readable). */
8007 if (opts.addressprint)
8008 uiout->field_skip ("addr");
8009 annotate_field (5);
8010 uiout->text ("vfork");
8011 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8012 {
8013 uiout->text (", process ");
8014 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8015 uiout->spaces (1);
8016 }
8017
8018 if (uiout->is_mi_like_p ())
8019 uiout->field_string ("catch-type", "vfork");
8020 }
8021
8022 /* Implement the "print_mention" breakpoint_ops method for vfork
8023 catchpoints. */
8024
8025 static void
8026 print_mention_catch_vfork (struct breakpoint *b)
8027 {
8028 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8029 }
8030
8031 /* Implement the "print_recreate" breakpoint_ops method for vfork
8032 catchpoints. */
8033
8034 static void
8035 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8036 {
8037 fprintf_unfiltered (fp, "catch vfork");
8038 print_recreate_thread (b, fp);
8039 }
8040
8041 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8042
8043 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8044
8045 /* An instance of this type is used to represent an solib catchpoint.
8046 A breakpoint is really of this type iff its ops pointer points to
8047 CATCH_SOLIB_BREAKPOINT_OPS. */
8048
8049 struct solib_catchpoint : public breakpoint
8050 {
8051 ~solib_catchpoint () override;
8052
8053 /* True for "catch load", false for "catch unload". */
8054 unsigned char is_load;
8055
8056 /* Regular expression to match, if any. COMPILED is only valid when
8057 REGEX is non-NULL. */
8058 char *regex;
8059 std::unique_ptr<compiled_regex> compiled;
8060 };
8061
8062 solib_catchpoint::~solib_catchpoint ()
8063 {
8064 xfree (this->regex);
8065 }
8066
8067 static int
8068 insert_catch_solib (struct bp_location *ignore)
8069 {
8070 return 0;
8071 }
8072
8073 static int
8074 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8075 {
8076 return 0;
8077 }
8078
8079 static int
8080 breakpoint_hit_catch_solib (const struct bp_location *bl,
8081 struct address_space *aspace,
8082 CORE_ADDR bp_addr,
8083 const struct target_waitstatus *ws)
8084 {
8085 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8086 struct breakpoint *other;
8087
8088 if (ws->kind == TARGET_WAITKIND_LOADED)
8089 return 1;
8090
8091 ALL_BREAKPOINTS (other)
8092 {
8093 struct bp_location *other_bl;
8094
8095 if (other == bl->owner)
8096 continue;
8097
8098 if (other->type != bp_shlib_event)
8099 continue;
8100
8101 if (self->pspace != NULL && other->pspace != self->pspace)
8102 continue;
8103
8104 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8105 {
8106 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8107 return 1;
8108 }
8109 }
8110
8111 return 0;
8112 }
8113
8114 static void
8115 check_status_catch_solib (struct bpstats *bs)
8116 {
8117 struct solib_catchpoint *self
8118 = (struct solib_catchpoint *) bs->breakpoint_at;
8119 int ix;
8120
8121 if (self->is_load)
8122 {
8123 struct so_list *iter;
8124
8125 for (ix = 0;
8126 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8127 ix, iter);
8128 ++ix)
8129 {
8130 if (!self->regex
8131 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8132 return;
8133 }
8134 }
8135 else
8136 {
8137 char *iter;
8138
8139 for (ix = 0;
8140 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8141 ix, iter);
8142 ++ix)
8143 {
8144 if (!self->regex
8145 || self->compiled->exec (iter, 0, NULL, 0) == 0)
8146 return;
8147 }
8148 }
8149
8150 bs->stop = 0;
8151 bs->print_it = print_it_noop;
8152 }
8153
8154 static enum print_stop_action
8155 print_it_catch_solib (bpstat bs)
8156 {
8157 struct breakpoint *b = bs->breakpoint_at;
8158 struct ui_out *uiout = current_uiout;
8159
8160 annotate_catchpoint (b->number);
8161 maybe_print_thread_hit_breakpoint (uiout);
8162 if (b->disposition == disp_del)
8163 uiout->text ("Temporary catchpoint ");
8164 else
8165 uiout->text ("Catchpoint ");
8166 uiout->field_int ("bkptno", b->number);
8167 uiout->text ("\n");
8168 if (uiout->is_mi_like_p ())
8169 uiout->field_string ("disp", bpdisp_text (b->disposition));
8170 print_solib_event (1);
8171 return PRINT_SRC_AND_LOC;
8172 }
8173
8174 static void
8175 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8176 {
8177 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8178 struct value_print_options opts;
8179 struct ui_out *uiout = current_uiout;
8180 char *msg;
8181
8182 get_user_print_options (&opts);
8183 /* Field 4, the address, is omitted (which makes the columns not
8184 line up too nicely with the headers, but the effect is relatively
8185 readable). */
8186 if (opts.addressprint)
8187 {
8188 annotate_field (4);
8189 uiout->field_skip ("addr");
8190 }
8191
8192 annotate_field (5);
8193 if (self->is_load)
8194 {
8195 if (self->regex)
8196 msg = xstrprintf (_("load of library matching %s"), self->regex);
8197 else
8198 msg = xstrdup (_("load of library"));
8199 }
8200 else
8201 {
8202 if (self->regex)
8203 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8204 else
8205 msg = xstrdup (_("unload of library"));
8206 }
8207 uiout->field_string ("what", msg);
8208 xfree (msg);
8209
8210 if (uiout->is_mi_like_p ())
8211 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8212 }
8213
8214 static void
8215 print_mention_catch_solib (struct breakpoint *b)
8216 {
8217 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8218
8219 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8220 self->is_load ? "load" : "unload");
8221 }
8222
8223 static void
8224 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8225 {
8226 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8227
8228 fprintf_unfiltered (fp, "%s %s",
8229 b->disposition == disp_del ? "tcatch" : "catch",
8230 self->is_load ? "load" : "unload");
8231 if (self->regex)
8232 fprintf_unfiltered (fp, " %s", self->regex);
8233 fprintf_unfiltered (fp, "\n");
8234 }
8235
8236 static struct breakpoint_ops catch_solib_breakpoint_ops;
8237
8238 /* Shared helper function (MI and CLI) for creating and installing
8239 a shared object event catchpoint. If IS_LOAD is non-zero then
8240 the events to be caught are load events, otherwise they are
8241 unload events. If IS_TEMP is non-zero the catchpoint is a
8242 temporary one. If ENABLED is non-zero the catchpoint is
8243 created in an enabled state. */
8244
8245 void
8246 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8247 {
8248 struct gdbarch *gdbarch = get_current_arch ();
8249
8250 if (!arg)
8251 arg = "";
8252 arg = skip_spaces (arg);
8253
8254 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8255
8256 if (*arg != '\0')
8257 {
8258 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8259 _("Invalid regexp")));
8260 c->regex = xstrdup (arg);
8261 }
8262
8263 c->is_load = is_load;
8264 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8265 &catch_solib_breakpoint_ops);
8266
8267 c->enable_state = enabled ? bp_enabled : bp_disabled;
8268
8269 install_breakpoint (0, std::move (c), 1);
8270 }
8271
8272 /* A helper function that does all the work for "catch load" and
8273 "catch unload". */
8274
8275 static void
8276 catch_load_or_unload (char *arg, int from_tty, int is_load,
8277 struct cmd_list_element *command)
8278 {
8279 int tempflag;
8280 const int enabled = 1;
8281
8282 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8283
8284 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8285 }
8286
8287 static void
8288 catch_load_command_1 (char *arg, int from_tty,
8289 struct cmd_list_element *command)
8290 {
8291 catch_load_or_unload (arg, from_tty, 1, command);
8292 }
8293
8294 static void
8295 catch_unload_command_1 (char *arg, int from_tty,
8296 struct cmd_list_element *command)
8297 {
8298 catch_load_or_unload (arg, from_tty, 0, command);
8299 }
8300
8301 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8302 is non-zero, then make the breakpoint temporary. If COND_STRING is
8303 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8304 the breakpoint_ops structure associated to the catchpoint. */
8305
8306 void
8307 init_catchpoint (struct breakpoint *b,
8308 struct gdbarch *gdbarch, int tempflag,
8309 const char *cond_string,
8310 const struct breakpoint_ops *ops)
8311 {
8312 symtab_and_line sal;
8313 sal.pspace = current_program_space;
8314
8315 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8316
8317 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8318 b->disposition = tempflag ? disp_del : disp_donttouch;
8319 }
8320
8321 void
8322 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8323 {
8324 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8325 set_breakpoint_number (internal, b);
8326 if (is_tracepoint (b))
8327 set_tracepoint_count (breakpoint_count);
8328 if (!internal)
8329 mention (b);
8330 observer_notify_breakpoint_created (b);
8331
8332 if (update_gll)
8333 update_global_location_list (UGLL_MAY_INSERT);
8334 }
8335
8336 static void
8337 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8338 int tempflag, const char *cond_string,
8339 const struct breakpoint_ops *ops)
8340 {
8341 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8342
8343 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8344
8345 c->forked_inferior_pid = null_ptid;
8346
8347 install_breakpoint (0, std::move (c), 1);
8348 }
8349
8350 /* Exec catchpoints. */
8351
8352 /* An instance of this type is used to represent an exec catchpoint.
8353 A breakpoint is really of this type iff its ops pointer points to
8354 CATCH_EXEC_BREAKPOINT_OPS. */
8355
8356 struct exec_catchpoint : public breakpoint
8357 {
8358 ~exec_catchpoint () override;
8359
8360 /* Filename of a program whose exec triggered this catchpoint.
8361 This field is only valid immediately after this catchpoint has
8362 triggered. */
8363 char *exec_pathname;
8364 };
8365
8366 /* Exec catchpoint destructor. */
8367
8368 exec_catchpoint::~exec_catchpoint ()
8369 {
8370 xfree (this->exec_pathname);
8371 }
8372
8373 static int
8374 insert_catch_exec (struct bp_location *bl)
8375 {
8376 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8377 }
8378
8379 static int
8380 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8381 {
8382 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8383 }
8384
8385 static int
8386 breakpoint_hit_catch_exec (const struct bp_location *bl,
8387 struct address_space *aspace, CORE_ADDR bp_addr,
8388 const struct target_waitstatus *ws)
8389 {
8390 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8391
8392 if (ws->kind != TARGET_WAITKIND_EXECD)
8393 return 0;
8394
8395 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8396 return 1;
8397 }
8398
8399 static enum print_stop_action
8400 print_it_catch_exec (bpstat bs)
8401 {
8402 struct ui_out *uiout = current_uiout;
8403 struct breakpoint *b = bs->breakpoint_at;
8404 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8405
8406 annotate_catchpoint (b->number);
8407 maybe_print_thread_hit_breakpoint (uiout);
8408 if (b->disposition == disp_del)
8409 uiout->text ("Temporary catchpoint ");
8410 else
8411 uiout->text ("Catchpoint ");
8412 if (uiout->is_mi_like_p ())
8413 {
8414 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8415 uiout->field_string ("disp", bpdisp_text (b->disposition));
8416 }
8417 uiout->field_int ("bkptno", b->number);
8418 uiout->text (" (exec'd ");
8419 uiout->field_string ("new-exec", c->exec_pathname);
8420 uiout->text ("), ");
8421
8422 return PRINT_SRC_AND_LOC;
8423 }
8424
8425 static void
8426 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8427 {
8428 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8429 struct value_print_options opts;
8430 struct ui_out *uiout = current_uiout;
8431
8432 get_user_print_options (&opts);
8433
8434 /* Field 4, the address, is omitted (which makes the columns
8435 not line up too nicely with the headers, but the effect
8436 is relatively readable). */
8437 if (opts.addressprint)
8438 uiout->field_skip ("addr");
8439 annotate_field (5);
8440 uiout->text ("exec");
8441 if (c->exec_pathname != NULL)
8442 {
8443 uiout->text (", program \"");
8444 uiout->field_string ("what", c->exec_pathname);
8445 uiout->text ("\" ");
8446 }
8447
8448 if (uiout->is_mi_like_p ())
8449 uiout->field_string ("catch-type", "exec");
8450 }
8451
8452 static void
8453 print_mention_catch_exec (struct breakpoint *b)
8454 {
8455 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8456 }
8457
8458 /* Implement the "print_recreate" breakpoint_ops method for exec
8459 catchpoints. */
8460
8461 static void
8462 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8463 {
8464 fprintf_unfiltered (fp, "catch exec");
8465 print_recreate_thread (b, fp);
8466 }
8467
8468 static struct breakpoint_ops catch_exec_breakpoint_ops;
8469
8470 static int
8471 hw_breakpoint_used_count (void)
8472 {
8473 int i = 0;
8474 struct breakpoint *b;
8475 struct bp_location *bl;
8476
8477 ALL_BREAKPOINTS (b)
8478 {
8479 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8480 for (bl = b->loc; bl; bl = bl->next)
8481 {
8482 /* Special types of hardware breakpoints may use more than
8483 one register. */
8484 i += b->ops->resources_needed (bl);
8485 }
8486 }
8487
8488 return i;
8489 }
8490
8491 /* Returns the resources B would use if it were a hardware
8492 watchpoint. */
8493
8494 static int
8495 hw_watchpoint_use_count (struct breakpoint *b)
8496 {
8497 int i = 0;
8498 struct bp_location *bl;
8499
8500 if (!breakpoint_enabled (b))
8501 return 0;
8502
8503 for (bl = b->loc; bl; bl = bl->next)
8504 {
8505 /* Special types of hardware watchpoints may use more than
8506 one register. */
8507 i += b->ops->resources_needed (bl);
8508 }
8509
8510 return i;
8511 }
8512
8513 /* Returns the sum the used resources of all hardware watchpoints of
8514 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8515 the sum of the used resources of all hardware watchpoints of other
8516 types _not_ TYPE. */
8517
8518 static int
8519 hw_watchpoint_used_count_others (struct breakpoint *except,
8520 enum bptype type, int *other_type_used)
8521 {
8522 int i = 0;
8523 struct breakpoint *b;
8524
8525 *other_type_used = 0;
8526 ALL_BREAKPOINTS (b)
8527 {
8528 if (b == except)
8529 continue;
8530 if (!breakpoint_enabled (b))
8531 continue;
8532
8533 if (b->type == type)
8534 i += hw_watchpoint_use_count (b);
8535 else if (is_hardware_watchpoint (b))
8536 *other_type_used = 1;
8537 }
8538
8539 return i;
8540 }
8541
8542 void
8543 disable_watchpoints_before_interactive_call_start (void)
8544 {
8545 struct breakpoint *b;
8546
8547 ALL_BREAKPOINTS (b)
8548 {
8549 if (is_watchpoint (b) && breakpoint_enabled (b))
8550 {
8551 b->enable_state = bp_call_disabled;
8552 update_global_location_list (UGLL_DONT_INSERT);
8553 }
8554 }
8555 }
8556
8557 void
8558 enable_watchpoints_after_interactive_call_stop (void)
8559 {
8560 struct breakpoint *b;
8561
8562 ALL_BREAKPOINTS (b)
8563 {
8564 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8565 {
8566 b->enable_state = bp_enabled;
8567 update_global_location_list (UGLL_MAY_INSERT);
8568 }
8569 }
8570 }
8571
8572 void
8573 disable_breakpoints_before_startup (void)
8574 {
8575 current_program_space->executing_startup = 1;
8576 update_global_location_list (UGLL_DONT_INSERT);
8577 }
8578
8579 void
8580 enable_breakpoints_after_startup (void)
8581 {
8582 current_program_space->executing_startup = 0;
8583 breakpoint_re_set ();
8584 }
8585
8586 /* Create a new single-step breakpoint for thread THREAD, with no
8587 locations. */
8588
8589 static struct breakpoint *
8590 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8591 {
8592 std::unique_ptr<breakpoint> b (new breakpoint ());
8593
8594 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8595 &momentary_breakpoint_ops);
8596
8597 b->disposition = disp_donttouch;
8598 b->frame_id = null_frame_id;
8599
8600 b->thread = thread;
8601 gdb_assert (b->thread != 0);
8602
8603 return add_to_breakpoint_chain (std::move (b));
8604 }
8605
8606 /* Set a momentary breakpoint of type TYPE at address specified by
8607 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8608 frame. */
8609
8610 struct breakpoint *
8611 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8612 struct frame_id frame_id, enum bptype type)
8613 {
8614 struct breakpoint *b;
8615
8616 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8617 tail-called one. */
8618 gdb_assert (!frame_id_artificial_p (frame_id));
8619
8620 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8621 b->enable_state = bp_enabled;
8622 b->disposition = disp_donttouch;
8623 b->frame_id = frame_id;
8624
8625 /* If we're debugging a multi-threaded program, then we want
8626 momentary breakpoints to be active in only a single thread of
8627 control. */
8628 if (in_thread_list (inferior_ptid))
8629 b->thread = ptid_to_global_thread_id (inferior_ptid);
8630
8631 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8632
8633 return b;
8634 }
8635
8636 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8637 The new breakpoint will have type TYPE, use OPS as its
8638 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8639
8640 static struct breakpoint *
8641 momentary_breakpoint_from_master (struct breakpoint *orig,
8642 enum bptype type,
8643 const struct breakpoint_ops *ops,
8644 int loc_enabled)
8645 {
8646 struct breakpoint *copy;
8647
8648 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8649 copy->loc = allocate_bp_location (copy);
8650 set_breakpoint_location_function (copy->loc, 1);
8651
8652 copy->loc->gdbarch = orig->loc->gdbarch;
8653 copy->loc->requested_address = orig->loc->requested_address;
8654 copy->loc->address = orig->loc->address;
8655 copy->loc->section = orig->loc->section;
8656 copy->loc->pspace = orig->loc->pspace;
8657 copy->loc->probe = orig->loc->probe;
8658 copy->loc->line_number = orig->loc->line_number;
8659 copy->loc->symtab = orig->loc->symtab;
8660 copy->loc->enabled = loc_enabled;
8661 copy->frame_id = orig->frame_id;
8662 copy->thread = orig->thread;
8663 copy->pspace = orig->pspace;
8664
8665 copy->enable_state = bp_enabled;
8666 copy->disposition = disp_donttouch;
8667 copy->number = internal_breakpoint_number--;
8668
8669 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8670 return copy;
8671 }
8672
8673 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8674 ORIG is NULL. */
8675
8676 struct breakpoint *
8677 clone_momentary_breakpoint (struct breakpoint *orig)
8678 {
8679 /* If there's nothing to clone, then return nothing. */
8680 if (orig == NULL)
8681 return NULL;
8682
8683 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8684 }
8685
8686 struct breakpoint *
8687 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8688 enum bptype type)
8689 {
8690 struct symtab_and_line sal;
8691
8692 sal = find_pc_line (pc, 0);
8693 sal.pc = pc;
8694 sal.section = find_pc_overlay (pc);
8695 sal.explicit_pc = 1;
8696
8697 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8698 }
8699 \f
8700
8701 /* Tell the user we have just set a breakpoint B. */
8702
8703 static void
8704 mention (struct breakpoint *b)
8705 {
8706 b->ops->print_mention (b);
8707 if (current_uiout->is_mi_like_p ())
8708 return;
8709 printf_filtered ("\n");
8710 }
8711 \f
8712
8713 static int bp_loc_is_permanent (struct bp_location *loc);
8714
8715 static struct bp_location *
8716 add_location_to_breakpoint (struct breakpoint *b,
8717 const struct symtab_and_line *sal)
8718 {
8719 struct bp_location *loc, **tmp;
8720 CORE_ADDR adjusted_address;
8721 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8722
8723 if (loc_gdbarch == NULL)
8724 loc_gdbarch = b->gdbarch;
8725
8726 /* Adjust the breakpoint's address prior to allocating a location.
8727 Once we call allocate_bp_location(), that mostly uninitialized
8728 location will be placed on the location chain. Adjustment of the
8729 breakpoint may cause target_read_memory() to be called and we do
8730 not want its scan of the location chain to find a breakpoint and
8731 location that's only been partially initialized. */
8732 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8733 sal->pc, b->type);
8734
8735 /* Sort the locations by their ADDRESS. */
8736 loc = allocate_bp_location (b);
8737 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8738 tmp = &((*tmp)->next))
8739 ;
8740 loc->next = *tmp;
8741 *tmp = loc;
8742
8743 loc->requested_address = sal->pc;
8744 loc->address = adjusted_address;
8745 loc->pspace = sal->pspace;
8746 loc->probe.probe = sal->probe;
8747 loc->probe.objfile = sal->objfile;
8748 gdb_assert (loc->pspace != NULL);
8749 loc->section = sal->section;
8750 loc->gdbarch = loc_gdbarch;
8751 loc->line_number = sal->line;
8752 loc->symtab = sal->symtab;
8753
8754 set_breakpoint_location_function (loc,
8755 sal->explicit_pc || sal->explicit_line);
8756
8757 /* While by definition, permanent breakpoints are already present in the
8758 code, we don't mark the location as inserted. Normally one would expect
8759 that GDB could rely on that breakpoint instruction to stop the program,
8760 thus removing the need to insert its own breakpoint, except that executing
8761 the breakpoint instruction can kill the target instead of reporting a
8762 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8763 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8764 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8765 breakpoint be inserted normally results in QEMU knowing about the GDB
8766 breakpoint, and thus trap before the breakpoint instruction is executed.
8767 (If GDB later needs to continue execution past the permanent breakpoint,
8768 it manually increments the PC, thus avoiding executing the breakpoint
8769 instruction.) */
8770 if (bp_loc_is_permanent (loc))
8771 loc->permanent = 1;
8772
8773 return loc;
8774 }
8775 \f
8776
8777 /* See breakpoint.h. */
8778
8779 int
8780 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8781 {
8782 int len;
8783 CORE_ADDR addr;
8784 const gdb_byte *bpoint;
8785 gdb_byte *target_mem;
8786
8787 addr = address;
8788 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8789
8790 /* Software breakpoints unsupported? */
8791 if (bpoint == NULL)
8792 return 0;
8793
8794 target_mem = (gdb_byte *) alloca (len);
8795
8796 /* Enable the automatic memory restoration from breakpoints while
8797 we read the memory. Otherwise we could say about our temporary
8798 breakpoints they are permanent. */
8799 scoped_restore restore_memory
8800 = make_scoped_restore_show_memory_breakpoints (0);
8801
8802 if (target_read_memory (address, target_mem, len) == 0
8803 && memcmp (target_mem, bpoint, len) == 0)
8804 return 1;
8805
8806 return 0;
8807 }
8808
8809 /* Return 1 if LOC is pointing to a permanent breakpoint,
8810 return 0 otherwise. */
8811
8812 static int
8813 bp_loc_is_permanent (struct bp_location *loc)
8814 {
8815 gdb_assert (loc != NULL);
8816
8817 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8818 attempt to read from the addresses the locations of these breakpoint types
8819 point to. program_breakpoint_here_p, below, will attempt to read
8820 memory. */
8821 if (!breakpoint_address_is_meaningful (loc->owner))
8822 return 0;
8823
8824 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8825 switch_to_program_space_and_thread (loc->pspace);
8826 return program_breakpoint_here_p (loc->gdbarch, loc->address);
8827 }
8828
8829 /* Build a command list for the dprintf corresponding to the current
8830 settings of the dprintf style options. */
8831
8832 static void
8833 update_dprintf_command_list (struct breakpoint *b)
8834 {
8835 char *dprintf_args = b->extra_string;
8836 char *printf_line = NULL;
8837
8838 if (!dprintf_args)
8839 return;
8840
8841 dprintf_args = skip_spaces (dprintf_args);
8842
8843 /* Allow a comma, as it may have terminated a location, but don't
8844 insist on it. */
8845 if (*dprintf_args == ',')
8846 ++dprintf_args;
8847 dprintf_args = skip_spaces (dprintf_args);
8848
8849 if (*dprintf_args != '"')
8850 error (_("Bad format string, missing '\"'."));
8851
8852 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8853 printf_line = xstrprintf ("printf %s", dprintf_args);
8854 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8855 {
8856 if (!dprintf_function)
8857 error (_("No function supplied for dprintf call"));
8858
8859 if (dprintf_channel && strlen (dprintf_channel) > 0)
8860 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8861 dprintf_function,
8862 dprintf_channel,
8863 dprintf_args);
8864 else
8865 printf_line = xstrprintf ("call (void) %s (%s)",
8866 dprintf_function,
8867 dprintf_args);
8868 }
8869 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8870 {
8871 if (target_can_run_breakpoint_commands ())
8872 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8873 else
8874 {
8875 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8876 printf_line = xstrprintf ("printf %s", dprintf_args);
8877 }
8878 }
8879 else
8880 internal_error (__FILE__, __LINE__,
8881 _("Invalid dprintf style."));
8882
8883 gdb_assert (printf_line != NULL);
8884 /* Manufacture a printf sequence. */
8885 {
8886 struct command_line *printf_cmd_line = XNEW (struct command_line);
8887
8888 printf_cmd_line->control_type = simple_control;
8889 printf_cmd_line->body_count = 0;
8890 printf_cmd_line->body_list = NULL;
8891 printf_cmd_line->next = NULL;
8892 printf_cmd_line->line = printf_line;
8893
8894 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
8895 }
8896 }
8897
8898 /* Update all dprintf commands, making their command lists reflect
8899 current style settings. */
8900
8901 static void
8902 update_dprintf_commands (char *args, int from_tty,
8903 struct cmd_list_element *c)
8904 {
8905 struct breakpoint *b;
8906
8907 ALL_BREAKPOINTS (b)
8908 {
8909 if (b->type == bp_dprintf)
8910 update_dprintf_command_list (b);
8911 }
8912 }
8913
8914 /* Create a breakpoint with SAL as location. Use LOCATION
8915 as a description of the location, and COND_STRING
8916 as condition expression. If LOCATION is NULL then create an
8917 "address location" from the address in the SAL. */
8918
8919 static void
8920 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8921 gdb::array_view<const symtab_and_line> sals,
8922 event_location_up &&location,
8923 gdb::unique_xmalloc_ptr<char> filter,
8924 gdb::unique_xmalloc_ptr<char> cond_string,
8925 gdb::unique_xmalloc_ptr<char> extra_string,
8926 enum bptype type, enum bpdisp disposition,
8927 int thread, int task, int ignore_count,
8928 const struct breakpoint_ops *ops, int from_tty,
8929 int enabled, int internal, unsigned flags,
8930 int display_canonical)
8931 {
8932 int i;
8933
8934 if (type == bp_hardware_breakpoint)
8935 {
8936 int target_resources_ok;
8937
8938 i = hw_breakpoint_used_count ();
8939 target_resources_ok =
8940 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8941 i + 1, 0);
8942 if (target_resources_ok == 0)
8943 error (_("No hardware breakpoint support in the target."));
8944 else if (target_resources_ok < 0)
8945 error (_("Hardware breakpoints used exceeds limit."));
8946 }
8947
8948 gdb_assert (!sals.empty ());
8949
8950 for (const auto &sal : sals)
8951 {
8952 struct bp_location *loc;
8953
8954 if (from_tty)
8955 {
8956 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8957 if (!loc_gdbarch)
8958 loc_gdbarch = gdbarch;
8959
8960 describe_other_breakpoints (loc_gdbarch,
8961 sal.pspace, sal.pc, sal.section, thread);
8962 }
8963
8964 if (&sal == &sals[0])
8965 {
8966 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8967 b->thread = thread;
8968 b->task = task;
8969
8970 b->cond_string = cond_string.release ();
8971 b->extra_string = extra_string.release ();
8972 b->ignore_count = ignore_count;
8973 b->enable_state = enabled ? bp_enabled : bp_disabled;
8974 b->disposition = disposition;
8975
8976 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8977 b->loc->inserted = 1;
8978
8979 if (type == bp_static_tracepoint)
8980 {
8981 struct tracepoint *t = (struct tracepoint *) b;
8982 struct static_tracepoint_marker marker;
8983
8984 if (strace_marker_p (b))
8985 {
8986 /* We already know the marker exists, otherwise, we
8987 wouldn't see a sal for it. */
8988 const char *p
8989 = &event_location_to_string (b->location.get ())[3];
8990 const char *endp;
8991 char *marker_str;
8992
8993 p = skip_spaces (p);
8994
8995 endp = skip_to_space (p);
8996
8997 marker_str = savestring (p, endp - p);
8998 t->static_trace_marker_id = marker_str;
8999
9000 printf_filtered (_("Probed static tracepoint "
9001 "marker \"%s\"\n"),
9002 t->static_trace_marker_id);
9003 }
9004 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9005 {
9006 t->static_trace_marker_id = xstrdup (marker.str_id);
9007 release_static_tracepoint_marker (&marker);
9008
9009 printf_filtered (_("Probed static tracepoint "
9010 "marker \"%s\"\n"),
9011 t->static_trace_marker_id);
9012 }
9013 else
9014 warning (_("Couldn't determine the static "
9015 "tracepoint marker to probe"));
9016 }
9017
9018 loc = b->loc;
9019 }
9020 else
9021 {
9022 loc = add_location_to_breakpoint (b, &sal);
9023 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9024 loc->inserted = 1;
9025 }
9026
9027 if (b->cond_string)
9028 {
9029 const char *arg = b->cond_string;
9030
9031 loc->cond = parse_exp_1 (&arg, loc->address,
9032 block_for_pc (loc->address), 0);
9033 if (*arg)
9034 error (_("Garbage '%s' follows condition"), arg);
9035 }
9036
9037 /* Dynamic printf requires and uses additional arguments on the
9038 command line, otherwise it's an error. */
9039 if (type == bp_dprintf)
9040 {
9041 if (b->extra_string)
9042 update_dprintf_command_list (b);
9043 else
9044 error (_("Format string required"));
9045 }
9046 else if (b->extra_string)
9047 error (_("Garbage '%s' at end of command"), b->extra_string);
9048 }
9049
9050 b->display_canonical = display_canonical;
9051 if (location != NULL)
9052 b->location = std::move (location);
9053 else
9054 b->location = new_address_location (b->loc->address, NULL, 0);
9055 b->filter = filter.release ();
9056 }
9057
9058 static void
9059 create_breakpoint_sal (struct gdbarch *gdbarch,
9060 gdb::array_view<const symtab_and_line> sals,
9061 event_location_up &&location,
9062 gdb::unique_xmalloc_ptr<char> filter,
9063 gdb::unique_xmalloc_ptr<char> cond_string,
9064 gdb::unique_xmalloc_ptr<char> extra_string,
9065 enum bptype type, enum bpdisp disposition,
9066 int thread, int task, int ignore_count,
9067 const struct breakpoint_ops *ops, int from_tty,
9068 int enabled, int internal, unsigned flags,
9069 int display_canonical)
9070 {
9071 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9072
9073 init_breakpoint_sal (b.get (), gdbarch,
9074 sals, std::move (location),
9075 std::move (filter),
9076 std::move (cond_string),
9077 std::move (extra_string),
9078 type, disposition,
9079 thread, task, ignore_count,
9080 ops, from_tty,
9081 enabled, internal, flags,
9082 display_canonical);
9083
9084 install_breakpoint (internal, std::move (b), 0);
9085 }
9086
9087 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9088 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9089 value. COND_STRING, if not NULL, specified the condition to be
9090 used for all breakpoints. Essentially the only case where
9091 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9092 function. In that case, it's still not possible to specify
9093 separate conditions for different overloaded functions, so
9094 we take just a single condition string.
9095
9096 NOTE: If the function succeeds, the caller is expected to cleanup
9097 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9098 array contents). If the function fails (error() is called), the
9099 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9100 COND and SALS arrays and each of those arrays contents. */
9101
9102 static void
9103 create_breakpoints_sal (struct gdbarch *gdbarch,
9104 struct linespec_result *canonical,
9105 gdb::unique_xmalloc_ptr<char> cond_string,
9106 gdb::unique_xmalloc_ptr<char> extra_string,
9107 enum bptype type, enum bpdisp disposition,
9108 int thread, int task, int ignore_count,
9109 const struct breakpoint_ops *ops, int from_tty,
9110 int enabled, int internal, unsigned flags)
9111 {
9112 if (canonical->pre_expanded)
9113 gdb_assert (canonical->lsals.size () == 1);
9114
9115 for (const auto &lsal : canonical->lsals)
9116 {
9117 /* Note that 'location' can be NULL in the case of a plain
9118 'break', without arguments. */
9119 event_location_up location
9120 = (canonical->location != NULL
9121 ? copy_event_location (canonical->location.get ()) : NULL);
9122 gdb::unique_xmalloc_ptr<char> filter_string
9123 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9124
9125 create_breakpoint_sal (gdbarch, lsal.sals,
9126 std::move (location),
9127 std::move (filter_string),
9128 std::move (cond_string),
9129 std::move (extra_string),
9130 type, disposition,
9131 thread, task, ignore_count, ops,
9132 from_tty, enabled, internal, flags,
9133 canonical->special_display);
9134 }
9135 }
9136
9137 /* Parse LOCATION which is assumed to be a SAL specification possibly
9138 followed by conditionals. On return, SALS contains an array of SAL
9139 addresses found. LOCATION points to the end of the SAL (for
9140 linespec locations).
9141
9142 The array and the line spec strings are allocated on the heap, it is
9143 the caller's responsibility to free them. */
9144
9145 static void
9146 parse_breakpoint_sals (const struct event_location *location,
9147 struct linespec_result *canonical)
9148 {
9149 struct symtab_and_line cursal;
9150
9151 if (event_location_type (location) == LINESPEC_LOCATION)
9152 {
9153 const char *address = get_linespec_location (location);
9154
9155 if (address == NULL)
9156 {
9157 /* The last displayed codepoint, if it's valid, is our default
9158 breakpoint address. */
9159 if (last_displayed_sal_is_valid ())
9160 {
9161 /* Set sal's pspace, pc, symtab, and line to the values
9162 corresponding to the last call to print_frame_info.
9163 Be sure to reinitialize LINE with NOTCURRENT == 0
9164 as the breakpoint line number is inappropriate otherwise.
9165 find_pc_line would adjust PC, re-set it back. */
9166 symtab_and_line sal = get_last_displayed_sal ();
9167 CORE_ADDR pc = sal.pc;
9168
9169 sal = find_pc_line (pc, 0);
9170
9171 /* "break" without arguments is equivalent to "break *PC"
9172 where PC is the last displayed codepoint's address. So
9173 make sure to set sal.explicit_pc to prevent GDB from
9174 trying to expand the list of sals to include all other
9175 instances with the same symtab and line. */
9176 sal.pc = pc;
9177 sal.explicit_pc = 1;
9178
9179 struct linespec_sals lsal;
9180 lsal.sals = {sal};
9181 lsal.canonical = NULL;
9182
9183 canonical->lsals.push_back (std::move (lsal));
9184 return;
9185 }
9186 else
9187 error (_("No default breakpoint address now."));
9188 }
9189 }
9190
9191 /* Force almost all breakpoints to be in terms of the
9192 current_source_symtab (which is decode_line_1's default).
9193 This should produce the results we want almost all of the
9194 time while leaving default_breakpoint_* alone.
9195
9196 ObjC: However, don't match an Objective-C method name which
9197 may have a '+' or '-' succeeded by a '['. */
9198 cursal = get_current_source_symtab_and_line ();
9199 if (last_displayed_sal_is_valid ())
9200 {
9201 const char *address = NULL;
9202
9203 if (event_location_type (location) == LINESPEC_LOCATION)
9204 address = get_linespec_location (location);
9205
9206 if (!cursal.symtab
9207 || (address != NULL
9208 && strchr ("+-", address[0]) != NULL
9209 && address[1] != '['))
9210 {
9211 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9212 get_last_displayed_symtab (),
9213 get_last_displayed_line (),
9214 canonical, NULL, NULL);
9215 return;
9216 }
9217 }
9218
9219 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9220 cursal.symtab, cursal.line, canonical, NULL, NULL);
9221 }
9222
9223
9224 /* Convert each SAL into a real PC. Verify that the PC can be
9225 inserted as a breakpoint. If it can't throw an error. */
9226
9227 static void
9228 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9229 {
9230 for (auto &sal : sals)
9231 resolve_sal_pc (&sal);
9232 }
9233
9234 /* Fast tracepoints may have restrictions on valid locations. For
9235 instance, a fast tracepoint using a jump instead of a trap will
9236 likely have to overwrite more bytes than a trap would, and so can
9237 only be placed where the instruction is longer than the jump, or a
9238 multi-instruction sequence does not have a jump into the middle of
9239 it, etc. */
9240
9241 static void
9242 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9243 gdb::array_view<const symtab_and_line> sals)
9244 {
9245 int rslt;
9246 char *msg;
9247 struct cleanup *old_chain;
9248
9249 for (const auto &sal : sals)
9250 {
9251 struct gdbarch *sarch;
9252
9253 sarch = get_sal_arch (sal);
9254 /* We fall back to GDBARCH if there is no architecture
9255 associated with SAL. */
9256 if (sarch == NULL)
9257 sarch = gdbarch;
9258 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
9259 old_chain = make_cleanup (xfree, msg);
9260
9261 if (!rslt)
9262 error (_("May not have a fast tracepoint at %s%s"),
9263 paddress (sarch, sal.pc), (msg ? msg : ""));
9264
9265 do_cleanups (old_chain);
9266 }
9267 }
9268
9269 /* Given TOK, a string specification of condition and thread, as
9270 accepted by the 'break' command, extract the condition
9271 string and thread number and set *COND_STRING and *THREAD.
9272 PC identifies the context at which the condition should be parsed.
9273 If no condition is found, *COND_STRING is set to NULL.
9274 If no thread is found, *THREAD is set to -1. */
9275
9276 static void
9277 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9278 char **cond_string, int *thread, int *task,
9279 char **rest)
9280 {
9281 *cond_string = NULL;
9282 *thread = -1;
9283 *task = 0;
9284 *rest = NULL;
9285
9286 while (tok && *tok)
9287 {
9288 const char *end_tok;
9289 int toklen;
9290 const char *cond_start = NULL;
9291 const char *cond_end = NULL;
9292
9293 tok = skip_spaces (tok);
9294
9295 if ((*tok == '"' || *tok == ',') && rest)
9296 {
9297 *rest = savestring (tok, strlen (tok));
9298 return;
9299 }
9300
9301 end_tok = skip_to_space (tok);
9302
9303 toklen = end_tok - tok;
9304
9305 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9306 {
9307 tok = cond_start = end_tok + 1;
9308 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9309 cond_end = tok;
9310 *cond_string = savestring (cond_start, cond_end - cond_start);
9311 }
9312 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9313 {
9314 const char *tmptok;
9315 struct thread_info *thr;
9316
9317 tok = end_tok + 1;
9318 thr = parse_thread_id (tok, &tmptok);
9319 if (tok == tmptok)
9320 error (_("Junk after thread keyword."));
9321 *thread = thr->global_num;
9322 tok = tmptok;
9323 }
9324 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9325 {
9326 char *tmptok;
9327
9328 tok = end_tok + 1;
9329 *task = strtol (tok, &tmptok, 0);
9330 if (tok == tmptok)
9331 error (_("Junk after task keyword."));
9332 if (!valid_task_id (*task))
9333 error (_("Unknown task %d."), *task);
9334 tok = tmptok;
9335 }
9336 else if (rest)
9337 {
9338 *rest = savestring (tok, strlen (tok));
9339 return;
9340 }
9341 else
9342 error (_("Junk at end of arguments."));
9343 }
9344 }
9345
9346 /* Decode a static tracepoint marker spec. */
9347
9348 static std::vector<symtab_and_line>
9349 decode_static_tracepoint_spec (const char **arg_p)
9350 {
9351 VEC(static_tracepoint_marker_p) *markers = NULL;
9352 const char *p = &(*arg_p)[3];
9353 const char *endp;
9354 int i;
9355
9356 p = skip_spaces (p);
9357
9358 endp = skip_to_space (p);
9359
9360 std::string marker_str (p, endp - p);
9361
9362 markers = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9363 if (VEC_empty(static_tracepoint_marker_p, markers))
9364 error (_("No known static tracepoint marker named %s"),
9365 marker_str.c_str ());
9366
9367 std::vector<symtab_and_line> sals;
9368 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
9369
9370 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
9371 {
9372 struct static_tracepoint_marker *marker;
9373
9374 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9375
9376 symtab_and_line sal = find_pc_line (marker->address, 0);
9377 sal.pc = marker->address;
9378 sals.push_back (sal);
9379
9380 release_static_tracepoint_marker (marker);
9381 }
9382
9383 *arg_p = endp;
9384 return sals;
9385 }
9386
9387 /* See breakpoint.h. */
9388
9389 int
9390 create_breakpoint (struct gdbarch *gdbarch,
9391 const struct event_location *location,
9392 const char *cond_string,
9393 int thread, const char *extra_string,
9394 int parse_extra,
9395 int tempflag, enum bptype type_wanted,
9396 int ignore_count,
9397 enum auto_boolean pending_break_support,
9398 const struct breakpoint_ops *ops,
9399 int from_tty, int enabled, int internal,
9400 unsigned flags)
9401 {
9402 struct linespec_result canonical;
9403 struct cleanup *bkpt_chain = NULL;
9404 int pending = 0;
9405 int task = 0;
9406 int prev_bkpt_count = breakpoint_count;
9407
9408 gdb_assert (ops != NULL);
9409
9410 /* If extra_string isn't useful, set it to NULL. */
9411 if (extra_string != NULL && *extra_string == '\0')
9412 extra_string = NULL;
9413
9414 TRY
9415 {
9416 ops->create_sals_from_location (location, &canonical, type_wanted);
9417 }
9418 CATCH (e, RETURN_MASK_ERROR)
9419 {
9420 /* If caller is interested in rc value from parse, set
9421 value. */
9422 if (e.error == NOT_FOUND_ERROR)
9423 {
9424 /* If pending breakpoint support is turned off, throw
9425 error. */
9426
9427 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9428 throw_exception (e);
9429
9430 exception_print (gdb_stderr, e);
9431
9432 /* If pending breakpoint support is auto query and the user
9433 selects no, then simply return the error code. */
9434 if (pending_break_support == AUTO_BOOLEAN_AUTO
9435 && !nquery (_("Make %s pending on future shared library load? "),
9436 bptype_string (type_wanted)))
9437 return 0;
9438
9439 /* At this point, either the user was queried about setting
9440 a pending breakpoint and selected yes, or pending
9441 breakpoint behavior is on and thus a pending breakpoint
9442 is defaulted on behalf of the user. */
9443 pending = 1;
9444 }
9445 else
9446 throw_exception (e);
9447 }
9448 END_CATCH
9449
9450 if (!pending && canonical.lsals.empty ())
9451 return 0;
9452
9453 /* ----------------------------- SNIP -----------------------------
9454 Anything added to the cleanup chain beyond this point is assumed
9455 to be part of a breakpoint. If the breakpoint create succeeds
9456 then the memory is not reclaimed. */
9457 bkpt_chain = make_cleanup (null_cleanup, 0);
9458
9459 /* Resolve all line numbers to PC's and verify that the addresses
9460 are ok for the target. */
9461 if (!pending)
9462 {
9463 for (auto &lsal : canonical.lsals)
9464 breakpoint_sals_to_pc (lsal.sals);
9465 }
9466
9467 /* Fast tracepoints may have additional restrictions on location. */
9468 if (!pending && type_wanted == bp_fast_tracepoint)
9469 {
9470 for (const auto &lsal : canonical.lsals)
9471 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9472 }
9473
9474 /* Verify that condition can be parsed, before setting any
9475 breakpoints. Allocate a separate condition expression for each
9476 breakpoint. */
9477 if (!pending)
9478 {
9479 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9480 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9481
9482 if (parse_extra)
9483 {
9484 char *rest;
9485 char *cond;
9486
9487 const linespec_sals &lsal = canonical.lsals[0];
9488
9489 /* Here we only parse 'arg' to separate condition
9490 from thread number, so parsing in context of first
9491 sal is OK. When setting the breakpoint we'll
9492 re-parse it in context of each sal. */
9493
9494 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9495 &cond, &thread, &task, &rest);
9496 cond_string_copy.reset (cond);
9497 extra_string_copy.reset (rest);
9498 }
9499 else
9500 {
9501 if (type_wanted != bp_dprintf
9502 && extra_string != NULL && *extra_string != '\0')
9503 error (_("Garbage '%s' at end of location"), extra_string);
9504
9505 /* Create a private copy of condition string. */
9506 if (cond_string)
9507 cond_string_copy.reset (xstrdup (cond_string));
9508 /* Create a private copy of any extra string. */
9509 if (extra_string)
9510 extra_string_copy.reset (xstrdup (extra_string));
9511 }
9512
9513 ops->create_breakpoints_sal (gdbarch, &canonical,
9514 std::move (cond_string_copy),
9515 std::move (extra_string_copy),
9516 type_wanted,
9517 tempflag ? disp_del : disp_donttouch,
9518 thread, task, ignore_count, ops,
9519 from_tty, enabled, internal, flags);
9520 }
9521 else
9522 {
9523 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9524
9525 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9526 b->location = copy_event_location (location);
9527
9528 if (parse_extra)
9529 b->cond_string = NULL;
9530 else
9531 {
9532 /* Create a private copy of condition string. */
9533 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9534 b->thread = thread;
9535 }
9536
9537 /* Create a private copy of any extra string. */
9538 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9539 b->ignore_count = ignore_count;
9540 b->disposition = tempflag ? disp_del : disp_donttouch;
9541 b->condition_not_parsed = 1;
9542 b->enable_state = enabled ? bp_enabled : bp_disabled;
9543 if ((type_wanted != bp_breakpoint
9544 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9545 b->pspace = current_program_space;
9546
9547 install_breakpoint (internal, std::move (b), 0);
9548 }
9549
9550 if (canonical.lsals.size () > 1)
9551 {
9552 warning (_("Multiple breakpoints were set.\nUse the "
9553 "\"delete\" command to delete unwanted breakpoints."));
9554 prev_breakpoint_count = prev_bkpt_count;
9555 }
9556
9557 /* That's it. Discard the cleanups for data inserted into the
9558 breakpoint. */
9559 discard_cleanups (bkpt_chain);
9560
9561 /* error call may happen here - have BKPT_CHAIN already discarded. */
9562 update_global_location_list (UGLL_MAY_INSERT);
9563
9564 return 1;
9565 }
9566
9567 /* Set a breakpoint.
9568 ARG is a string describing breakpoint address,
9569 condition, and thread.
9570 FLAG specifies if a breakpoint is hardware on,
9571 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9572 and BP_TEMPFLAG. */
9573
9574 static void
9575 break_command_1 (const char *arg, int flag, int from_tty)
9576 {
9577 int tempflag = flag & BP_TEMPFLAG;
9578 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9579 ? bp_hardware_breakpoint
9580 : bp_breakpoint);
9581 struct breakpoint_ops *ops;
9582
9583 event_location_up location = string_to_event_location (&arg, current_language);
9584
9585 /* Matching breakpoints on probes. */
9586 if (location != NULL
9587 && event_location_type (location.get ()) == PROBE_LOCATION)
9588 ops = &bkpt_probe_breakpoint_ops;
9589 else
9590 ops = &bkpt_breakpoint_ops;
9591
9592 create_breakpoint (get_current_arch (),
9593 location.get (),
9594 NULL, 0, arg, 1 /* parse arg */,
9595 tempflag, type_wanted,
9596 0 /* Ignore count */,
9597 pending_break_support,
9598 ops,
9599 from_tty,
9600 1 /* enabled */,
9601 0 /* internal */,
9602 0);
9603 }
9604
9605 /* Helper function for break_command_1 and disassemble_command. */
9606
9607 void
9608 resolve_sal_pc (struct symtab_and_line *sal)
9609 {
9610 CORE_ADDR pc;
9611
9612 if (sal->pc == 0 && sal->symtab != NULL)
9613 {
9614 if (!find_line_pc (sal->symtab, sal->line, &pc))
9615 error (_("No line %d in file \"%s\"."),
9616 sal->line, symtab_to_filename_for_display (sal->symtab));
9617 sal->pc = pc;
9618
9619 /* If this SAL corresponds to a breakpoint inserted using a line
9620 number, then skip the function prologue if necessary. */
9621 if (sal->explicit_line)
9622 skip_prologue_sal (sal);
9623 }
9624
9625 if (sal->section == 0 && sal->symtab != NULL)
9626 {
9627 const struct blockvector *bv;
9628 const struct block *b;
9629 struct symbol *sym;
9630
9631 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9632 SYMTAB_COMPUNIT (sal->symtab));
9633 if (bv != NULL)
9634 {
9635 sym = block_linkage_function (b);
9636 if (sym != NULL)
9637 {
9638 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9639 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9640 sym);
9641 }
9642 else
9643 {
9644 /* It really is worthwhile to have the section, so we'll
9645 just have to look harder. This case can be executed
9646 if we have line numbers but no functions (as can
9647 happen in assembly source). */
9648
9649 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9650 switch_to_program_space_and_thread (sal->pspace);
9651
9652 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9653 if (msym.minsym)
9654 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9655 }
9656 }
9657 }
9658 }
9659
9660 void
9661 break_command (char *arg, int from_tty)
9662 {
9663 break_command_1 (arg, 0, from_tty);
9664 }
9665
9666 void
9667 tbreak_command (char *arg, int from_tty)
9668 {
9669 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9670 }
9671
9672 static void
9673 hbreak_command (char *arg, int from_tty)
9674 {
9675 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9676 }
9677
9678 static void
9679 thbreak_command (char *arg, int from_tty)
9680 {
9681 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9682 }
9683
9684 static void
9685 stop_command (char *arg, int from_tty)
9686 {
9687 printf_filtered (_("Specify the type of breakpoint to set.\n\
9688 Usage: stop in <function | address>\n\
9689 stop at <line>\n"));
9690 }
9691
9692 static void
9693 stopin_command (const char *arg, int from_tty)
9694 {
9695 int badInput = 0;
9696
9697 if (arg == (char *) NULL)
9698 badInput = 1;
9699 else if (*arg != '*')
9700 {
9701 const char *argptr = arg;
9702 int hasColon = 0;
9703
9704 /* Look for a ':'. If this is a line number specification, then
9705 say it is bad, otherwise, it should be an address or
9706 function/method name. */
9707 while (*argptr && !hasColon)
9708 {
9709 hasColon = (*argptr == ':');
9710 argptr++;
9711 }
9712
9713 if (hasColon)
9714 badInput = (*argptr != ':'); /* Not a class::method */
9715 else
9716 badInput = isdigit (*arg); /* a simple line number */
9717 }
9718
9719 if (badInput)
9720 printf_filtered (_("Usage: stop in <function | address>\n"));
9721 else
9722 break_command_1 (arg, 0, from_tty);
9723 }
9724
9725 static void
9726 stopat_command (const char *arg, int from_tty)
9727 {
9728 int badInput = 0;
9729
9730 if (arg == (char *) NULL || *arg == '*') /* no line number */
9731 badInput = 1;
9732 else
9733 {
9734 const char *argptr = arg;
9735 int hasColon = 0;
9736
9737 /* Look for a ':'. If there is a '::' then get out, otherwise
9738 it is probably a line number. */
9739 while (*argptr && !hasColon)
9740 {
9741 hasColon = (*argptr == ':');
9742 argptr++;
9743 }
9744
9745 if (hasColon)
9746 badInput = (*argptr == ':'); /* we have class::method */
9747 else
9748 badInput = !isdigit (*arg); /* not a line number */
9749 }
9750
9751 if (badInput)
9752 printf_filtered (_("Usage: stop at <line>\n"));
9753 else
9754 break_command_1 (arg, 0, from_tty);
9755 }
9756
9757 /* The dynamic printf command is mostly like a regular breakpoint, but
9758 with a prewired command list consisting of a single output command,
9759 built from extra arguments supplied on the dprintf command
9760 line. */
9761
9762 static void
9763 dprintf_command (char *arg_in, int from_tty)
9764 {
9765 const char *arg = arg_in;
9766 event_location_up location = string_to_event_location (&arg, current_language);
9767
9768 /* If non-NULL, ARG should have been advanced past the location;
9769 the next character must be ','. */
9770 if (arg != NULL)
9771 {
9772 if (arg[0] != ',' || arg[1] == '\0')
9773 error (_("Format string required"));
9774 else
9775 {
9776 /* Skip the comma. */
9777 ++arg;
9778 }
9779 }
9780
9781 create_breakpoint (get_current_arch (),
9782 location.get (),
9783 NULL, 0, arg, 1 /* parse arg */,
9784 0, bp_dprintf,
9785 0 /* Ignore count */,
9786 pending_break_support,
9787 &dprintf_breakpoint_ops,
9788 from_tty,
9789 1 /* enabled */,
9790 0 /* internal */,
9791 0);
9792 }
9793
9794 static void
9795 agent_printf_command (char *arg, int from_tty)
9796 {
9797 error (_("May only run agent-printf on the target"));
9798 }
9799
9800 /* Implement the "breakpoint_hit" breakpoint_ops method for
9801 ranged breakpoints. */
9802
9803 static int
9804 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9805 struct address_space *aspace,
9806 CORE_ADDR bp_addr,
9807 const struct target_waitstatus *ws)
9808 {
9809 if (ws->kind != TARGET_WAITKIND_STOPPED
9810 || ws->value.sig != GDB_SIGNAL_TRAP)
9811 return 0;
9812
9813 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9814 bl->length, aspace, bp_addr);
9815 }
9816
9817 /* Implement the "resources_needed" breakpoint_ops method for
9818 ranged breakpoints. */
9819
9820 static int
9821 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9822 {
9823 return target_ranged_break_num_registers ();
9824 }
9825
9826 /* Implement the "print_it" breakpoint_ops method for
9827 ranged breakpoints. */
9828
9829 static enum print_stop_action
9830 print_it_ranged_breakpoint (bpstat bs)
9831 {
9832 struct breakpoint *b = bs->breakpoint_at;
9833 struct bp_location *bl = b->loc;
9834 struct ui_out *uiout = current_uiout;
9835
9836 gdb_assert (b->type == bp_hardware_breakpoint);
9837
9838 /* Ranged breakpoints have only one location. */
9839 gdb_assert (bl && bl->next == NULL);
9840
9841 annotate_breakpoint (b->number);
9842
9843 maybe_print_thread_hit_breakpoint (uiout);
9844
9845 if (b->disposition == disp_del)
9846 uiout->text ("Temporary ranged breakpoint ");
9847 else
9848 uiout->text ("Ranged breakpoint ");
9849 if (uiout->is_mi_like_p ())
9850 {
9851 uiout->field_string ("reason",
9852 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9853 uiout->field_string ("disp", bpdisp_text (b->disposition));
9854 }
9855 uiout->field_int ("bkptno", b->number);
9856 uiout->text (", ");
9857
9858 return PRINT_SRC_AND_LOC;
9859 }
9860
9861 /* Implement the "print_one" breakpoint_ops method for
9862 ranged breakpoints. */
9863
9864 static void
9865 print_one_ranged_breakpoint (struct breakpoint *b,
9866 struct bp_location **last_loc)
9867 {
9868 struct bp_location *bl = b->loc;
9869 struct value_print_options opts;
9870 struct ui_out *uiout = current_uiout;
9871
9872 /* Ranged breakpoints have only one location. */
9873 gdb_assert (bl && bl->next == NULL);
9874
9875 get_user_print_options (&opts);
9876
9877 if (opts.addressprint)
9878 /* We don't print the address range here, it will be printed later
9879 by print_one_detail_ranged_breakpoint. */
9880 uiout->field_skip ("addr");
9881 annotate_field (5);
9882 print_breakpoint_location (b, bl);
9883 *last_loc = bl;
9884 }
9885
9886 /* Implement the "print_one_detail" breakpoint_ops method for
9887 ranged breakpoints. */
9888
9889 static void
9890 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9891 struct ui_out *uiout)
9892 {
9893 CORE_ADDR address_start, address_end;
9894 struct bp_location *bl = b->loc;
9895 string_file stb;
9896
9897 gdb_assert (bl);
9898
9899 address_start = bl->address;
9900 address_end = address_start + bl->length - 1;
9901
9902 uiout->text ("\taddress range: ");
9903 stb.printf ("[%s, %s]",
9904 print_core_address (bl->gdbarch, address_start),
9905 print_core_address (bl->gdbarch, address_end));
9906 uiout->field_stream ("addr", stb);
9907 uiout->text ("\n");
9908 }
9909
9910 /* Implement the "print_mention" breakpoint_ops method for
9911 ranged breakpoints. */
9912
9913 static void
9914 print_mention_ranged_breakpoint (struct breakpoint *b)
9915 {
9916 struct bp_location *bl = b->loc;
9917 struct ui_out *uiout = current_uiout;
9918
9919 gdb_assert (bl);
9920 gdb_assert (b->type == bp_hardware_breakpoint);
9921
9922 if (uiout->is_mi_like_p ())
9923 return;
9924
9925 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9926 b->number, paddress (bl->gdbarch, bl->address),
9927 paddress (bl->gdbarch, bl->address + bl->length - 1));
9928 }
9929
9930 /* Implement the "print_recreate" breakpoint_ops method for
9931 ranged breakpoints. */
9932
9933 static void
9934 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9935 {
9936 fprintf_unfiltered (fp, "break-range %s, %s",
9937 event_location_to_string (b->location.get ()),
9938 event_location_to_string (b->location_range_end.get ()));
9939 print_recreate_thread (b, fp);
9940 }
9941
9942 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9943
9944 static struct breakpoint_ops ranged_breakpoint_ops;
9945
9946 /* Find the address where the end of the breakpoint range should be
9947 placed, given the SAL of the end of the range. This is so that if
9948 the user provides a line number, the end of the range is set to the
9949 last instruction of the given line. */
9950
9951 static CORE_ADDR
9952 find_breakpoint_range_end (struct symtab_and_line sal)
9953 {
9954 CORE_ADDR end;
9955
9956 /* If the user provided a PC value, use it. Otherwise,
9957 find the address of the end of the given location. */
9958 if (sal.explicit_pc)
9959 end = sal.pc;
9960 else
9961 {
9962 int ret;
9963 CORE_ADDR start;
9964
9965 ret = find_line_pc_range (sal, &start, &end);
9966 if (!ret)
9967 error (_("Could not find location of the end of the range."));
9968
9969 /* find_line_pc_range returns the start of the next line. */
9970 end--;
9971 }
9972
9973 return end;
9974 }
9975
9976 /* Implement the "break-range" CLI command. */
9977
9978 static void
9979 break_range_command (char *arg_in, int from_tty)
9980 {
9981 const char *arg = arg_in;
9982 const char *arg_start;
9983 struct linespec_result canonical_start, canonical_end;
9984 int bp_count, can_use_bp, length;
9985 CORE_ADDR end;
9986 struct breakpoint *b;
9987
9988 /* We don't support software ranged breakpoints. */
9989 if (target_ranged_break_num_registers () < 0)
9990 error (_("This target does not support hardware ranged breakpoints."));
9991
9992 bp_count = hw_breakpoint_used_count ();
9993 bp_count += target_ranged_break_num_registers ();
9994 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9995 bp_count, 0);
9996 if (can_use_bp < 0)
9997 error (_("Hardware breakpoints used exceeds limit."));
9998
9999 arg = skip_spaces (arg);
10000 if (arg == NULL || arg[0] == '\0')
10001 error(_("No address range specified."));
10002
10003 arg_start = arg;
10004 event_location_up start_location = string_to_event_location (&arg,
10005 current_language);
10006 parse_breakpoint_sals (start_location.get (), &canonical_start);
10007
10008 if (arg[0] != ',')
10009 error (_("Too few arguments."));
10010 else if (canonical_start.lsals.empty ())
10011 error (_("Could not find location of the beginning of the range."));
10012
10013 const linespec_sals &lsal_start = canonical_start.lsals[0];
10014
10015 if (canonical_start.lsals.size () > 1
10016 || lsal_start.sals.size () != 1)
10017 error (_("Cannot create a ranged breakpoint with multiple locations."));
10018
10019 const symtab_and_line &sal_start = lsal_start.sals[0];
10020 std::string addr_string_start (arg_start, arg - arg_start);
10021
10022 arg++; /* Skip the comma. */
10023 arg = skip_spaces (arg);
10024
10025 /* Parse the end location. */
10026
10027 arg_start = arg;
10028
10029 /* We call decode_line_full directly here instead of using
10030 parse_breakpoint_sals because we need to specify the start location's
10031 symtab and line as the default symtab and line for the end of the
10032 range. This makes it possible to have ranges like "foo.c:27, +14",
10033 where +14 means 14 lines from the start location. */
10034 event_location_up end_location = string_to_event_location (&arg,
10035 current_language);
10036 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10037 sal_start.symtab, sal_start.line,
10038 &canonical_end, NULL, NULL);
10039
10040 if (canonical_end.lsals.empty ())
10041 error (_("Could not find location of the end of the range."));
10042
10043 const linespec_sals &lsal_end = canonical_end.lsals[0];
10044 if (canonical_end.lsals.size () > 1
10045 || lsal_end.sals.size () != 1)
10046 error (_("Cannot create a ranged breakpoint with multiple locations."));
10047
10048 const symtab_and_line &sal_end = lsal_end.sals[0];
10049
10050 end = find_breakpoint_range_end (sal_end);
10051 if (sal_start.pc > end)
10052 error (_("Invalid address range, end precedes start."));
10053
10054 length = end - sal_start.pc + 1;
10055 if (length < 0)
10056 /* Length overflowed. */
10057 error (_("Address range too large."));
10058 else if (length == 1)
10059 {
10060 /* This range is simple enough to be handled by
10061 the `hbreak' command. */
10062 hbreak_command (&addr_string_start[0], 1);
10063
10064 return;
10065 }
10066
10067 /* Now set up the breakpoint. */
10068 b = set_raw_breakpoint (get_current_arch (), sal_start,
10069 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10070 set_breakpoint_count (breakpoint_count + 1);
10071 b->number = breakpoint_count;
10072 b->disposition = disp_donttouch;
10073 b->location = std::move (start_location);
10074 b->location_range_end = std::move (end_location);
10075 b->loc->length = length;
10076
10077 mention (b);
10078 observer_notify_breakpoint_created (b);
10079 update_global_location_list (UGLL_MAY_INSERT);
10080 }
10081
10082 /* Return non-zero if EXP is verified as constant. Returned zero
10083 means EXP is variable. Also the constant detection may fail for
10084 some constant expressions and in such case still falsely return
10085 zero. */
10086
10087 static int
10088 watchpoint_exp_is_const (const struct expression *exp)
10089 {
10090 int i = exp->nelts;
10091
10092 while (i > 0)
10093 {
10094 int oplenp, argsp;
10095
10096 /* We are only interested in the descriptor of each element. */
10097 operator_length (exp, i, &oplenp, &argsp);
10098 i -= oplenp;
10099
10100 switch (exp->elts[i].opcode)
10101 {
10102 case BINOP_ADD:
10103 case BINOP_SUB:
10104 case BINOP_MUL:
10105 case BINOP_DIV:
10106 case BINOP_REM:
10107 case BINOP_MOD:
10108 case BINOP_LSH:
10109 case BINOP_RSH:
10110 case BINOP_LOGICAL_AND:
10111 case BINOP_LOGICAL_OR:
10112 case BINOP_BITWISE_AND:
10113 case BINOP_BITWISE_IOR:
10114 case BINOP_BITWISE_XOR:
10115 case BINOP_EQUAL:
10116 case BINOP_NOTEQUAL:
10117 case BINOP_LESS:
10118 case BINOP_GTR:
10119 case BINOP_LEQ:
10120 case BINOP_GEQ:
10121 case BINOP_REPEAT:
10122 case BINOP_COMMA:
10123 case BINOP_EXP:
10124 case BINOP_MIN:
10125 case BINOP_MAX:
10126 case BINOP_INTDIV:
10127 case BINOP_CONCAT:
10128 case TERNOP_COND:
10129 case TERNOP_SLICE:
10130
10131 case OP_LONG:
10132 case OP_DOUBLE:
10133 case OP_DECFLOAT:
10134 case OP_LAST:
10135 case OP_COMPLEX:
10136 case OP_STRING:
10137 case OP_ARRAY:
10138 case OP_TYPE:
10139 case OP_TYPEOF:
10140 case OP_DECLTYPE:
10141 case OP_TYPEID:
10142 case OP_NAME:
10143 case OP_OBJC_NSSTRING:
10144
10145 case UNOP_NEG:
10146 case UNOP_LOGICAL_NOT:
10147 case UNOP_COMPLEMENT:
10148 case UNOP_ADDR:
10149 case UNOP_HIGH:
10150 case UNOP_CAST:
10151
10152 case UNOP_CAST_TYPE:
10153 case UNOP_REINTERPRET_CAST:
10154 case UNOP_DYNAMIC_CAST:
10155 /* Unary, binary and ternary operators: We have to check
10156 their operands. If they are constant, then so is the
10157 result of that operation. For instance, if A and B are
10158 determined to be constants, then so is "A + B".
10159
10160 UNOP_IND is one exception to the rule above, because the
10161 value of *ADDR is not necessarily a constant, even when
10162 ADDR is. */
10163 break;
10164
10165 case OP_VAR_VALUE:
10166 /* Check whether the associated symbol is a constant.
10167
10168 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10169 possible that a buggy compiler could mark a variable as
10170 constant even when it is not, and TYPE_CONST would return
10171 true in this case, while SYMBOL_CLASS wouldn't.
10172
10173 We also have to check for function symbols because they
10174 are always constant. */
10175 {
10176 struct symbol *s = exp->elts[i + 2].symbol;
10177
10178 if (SYMBOL_CLASS (s) != LOC_BLOCK
10179 && SYMBOL_CLASS (s) != LOC_CONST
10180 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10181 return 0;
10182 break;
10183 }
10184
10185 /* The default action is to return 0 because we are using
10186 the optimistic approach here: If we don't know something,
10187 then it is not a constant. */
10188 default:
10189 return 0;
10190 }
10191 }
10192
10193 return 1;
10194 }
10195
10196 /* Watchpoint destructor. */
10197
10198 watchpoint::~watchpoint ()
10199 {
10200 xfree (this->exp_string);
10201 xfree (this->exp_string_reparse);
10202 value_free (this->val);
10203 }
10204
10205 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10206
10207 static void
10208 re_set_watchpoint (struct breakpoint *b)
10209 {
10210 struct watchpoint *w = (struct watchpoint *) b;
10211
10212 /* Watchpoint can be either on expression using entirely global
10213 variables, or it can be on local variables.
10214
10215 Watchpoints of the first kind are never auto-deleted, and even
10216 persist across program restarts. Since they can use variables
10217 from shared libraries, we need to reparse expression as libraries
10218 are loaded and unloaded.
10219
10220 Watchpoints on local variables can also change meaning as result
10221 of solib event. For example, if a watchpoint uses both a local
10222 and a global variables in expression, it's a local watchpoint,
10223 but unloading of a shared library will make the expression
10224 invalid. This is not a very common use case, but we still
10225 re-evaluate expression, to avoid surprises to the user.
10226
10227 Note that for local watchpoints, we re-evaluate it only if
10228 watchpoints frame id is still valid. If it's not, it means the
10229 watchpoint is out of scope and will be deleted soon. In fact,
10230 I'm not sure we'll ever be called in this case.
10231
10232 If a local watchpoint's frame id is still valid, then
10233 w->exp_valid_block is likewise valid, and we can safely use it.
10234
10235 Don't do anything about disabled watchpoints, since they will be
10236 reevaluated again when enabled. */
10237 update_watchpoint (w, 1 /* reparse */);
10238 }
10239
10240 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10241
10242 static int
10243 insert_watchpoint (struct bp_location *bl)
10244 {
10245 struct watchpoint *w = (struct watchpoint *) bl->owner;
10246 int length = w->exact ? 1 : bl->length;
10247
10248 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10249 w->cond_exp.get ());
10250 }
10251
10252 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10253
10254 static int
10255 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10256 {
10257 struct watchpoint *w = (struct watchpoint *) bl->owner;
10258 int length = w->exact ? 1 : bl->length;
10259
10260 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10261 w->cond_exp.get ());
10262 }
10263
10264 static int
10265 breakpoint_hit_watchpoint (const struct bp_location *bl,
10266 struct address_space *aspace, CORE_ADDR bp_addr,
10267 const struct target_waitstatus *ws)
10268 {
10269 struct breakpoint *b = bl->owner;
10270 struct watchpoint *w = (struct watchpoint *) b;
10271
10272 /* Continuable hardware watchpoints are treated as non-existent if the
10273 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10274 some data address). Otherwise gdb won't stop on a break instruction
10275 in the code (not from a breakpoint) when a hardware watchpoint has
10276 been defined. Also skip watchpoints which we know did not trigger
10277 (did not match the data address). */
10278 if (is_hardware_watchpoint (b)
10279 && w->watchpoint_triggered == watch_triggered_no)
10280 return 0;
10281
10282 return 1;
10283 }
10284
10285 static void
10286 check_status_watchpoint (bpstat bs)
10287 {
10288 gdb_assert (is_watchpoint (bs->breakpoint_at));
10289
10290 bpstat_check_watchpoint (bs);
10291 }
10292
10293 /* Implement the "resources_needed" breakpoint_ops method for
10294 hardware watchpoints. */
10295
10296 static int
10297 resources_needed_watchpoint (const struct bp_location *bl)
10298 {
10299 struct watchpoint *w = (struct watchpoint *) bl->owner;
10300 int length = w->exact? 1 : bl->length;
10301
10302 return target_region_ok_for_hw_watchpoint (bl->address, length);
10303 }
10304
10305 /* Implement the "works_in_software_mode" breakpoint_ops method for
10306 hardware watchpoints. */
10307
10308 static int
10309 works_in_software_mode_watchpoint (const struct breakpoint *b)
10310 {
10311 /* Read and access watchpoints only work with hardware support. */
10312 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10313 }
10314
10315 static enum print_stop_action
10316 print_it_watchpoint (bpstat bs)
10317 {
10318 struct breakpoint *b;
10319 enum print_stop_action result;
10320 struct watchpoint *w;
10321 struct ui_out *uiout = current_uiout;
10322
10323 gdb_assert (bs->bp_location_at != NULL);
10324
10325 b = bs->breakpoint_at;
10326 w = (struct watchpoint *) b;
10327
10328 annotate_watchpoint (b->number);
10329 maybe_print_thread_hit_breakpoint (uiout);
10330
10331 string_file stb;
10332
10333 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10334 switch (b->type)
10335 {
10336 case bp_watchpoint:
10337 case bp_hardware_watchpoint:
10338 if (uiout->is_mi_like_p ())
10339 uiout->field_string
10340 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10341 mention (b);
10342 tuple_emitter.emplace (uiout, "value");
10343 uiout->text ("\nOld value = ");
10344 watchpoint_value_print (bs->old_val, &stb);
10345 uiout->field_stream ("old", stb);
10346 uiout->text ("\nNew value = ");
10347 watchpoint_value_print (w->val, &stb);
10348 uiout->field_stream ("new", stb);
10349 uiout->text ("\n");
10350 /* More than one watchpoint may have been triggered. */
10351 result = PRINT_UNKNOWN;
10352 break;
10353
10354 case bp_read_watchpoint:
10355 if (uiout->is_mi_like_p ())
10356 uiout->field_string
10357 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10358 mention (b);
10359 tuple_emitter.emplace (uiout, "value");
10360 uiout->text ("\nValue = ");
10361 watchpoint_value_print (w->val, &stb);
10362 uiout->field_stream ("value", stb);
10363 uiout->text ("\n");
10364 result = PRINT_UNKNOWN;
10365 break;
10366
10367 case bp_access_watchpoint:
10368 if (bs->old_val != NULL)
10369 {
10370 if (uiout->is_mi_like_p ())
10371 uiout->field_string
10372 ("reason",
10373 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10374 mention (b);
10375 tuple_emitter.emplace (uiout, "value");
10376 uiout->text ("\nOld value = ");
10377 watchpoint_value_print (bs->old_val, &stb);
10378 uiout->field_stream ("old", stb);
10379 uiout->text ("\nNew value = ");
10380 }
10381 else
10382 {
10383 mention (b);
10384 if (uiout->is_mi_like_p ())
10385 uiout->field_string
10386 ("reason",
10387 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10388 tuple_emitter.emplace (uiout, "value");
10389 uiout->text ("\nValue = ");
10390 }
10391 watchpoint_value_print (w->val, &stb);
10392 uiout->field_stream ("new", stb);
10393 uiout->text ("\n");
10394 result = PRINT_UNKNOWN;
10395 break;
10396 default:
10397 result = PRINT_UNKNOWN;
10398 }
10399
10400 return result;
10401 }
10402
10403 /* Implement the "print_mention" breakpoint_ops method for hardware
10404 watchpoints. */
10405
10406 static void
10407 print_mention_watchpoint (struct breakpoint *b)
10408 {
10409 struct watchpoint *w = (struct watchpoint *) b;
10410 struct ui_out *uiout = current_uiout;
10411 const char *tuple_name;
10412
10413 switch (b->type)
10414 {
10415 case bp_watchpoint:
10416 uiout->text ("Watchpoint ");
10417 tuple_name = "wpt";
10418 break;
10419 case bp_hardware_watchpoint:
10420 uiout->text ("Hardware watchpoint ");
10421 tuple_name = "wpt";
10422 break;
10423 case bp_read_watchpoint:
10424 uiout->text ("Hardware read watchpoint ");
10425 tuple_name = "hw-rwpt";
10426 break;
10427 case bp_access_watchpoint:
10428 uiout->text ("Hardware access (read/write) watchpoint ");
10429 tuple_name = "hw-awpt";
10430 break;
10431 default:
10432 internal_error (__FILE__, __LINE__,
10433 _("Invalid hardware watchpoint type."));
10434 }
10435
10436 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10437 uiout->field_int ("number", b->number);
10438 uiout->text (": ");
10439 uiout->field_string ("exp", w->exp_string);
10440 }
10441
10442 /* Implement the "print_recreate" breakpoint_ops method for
10443 watchpoints. */
10444
10445 static void
10446 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10447 {
10448 struct watchpoint *w = (struct watchpoint *) b;
10449
10450 switch (b->type)
10451 {
10452 case bp_watchpoint:
10453 case bp_hardware_watchpoint:
10454 fprintf_unfiltered (fp, "watch");
10455 break;
10456 case bp_read_watchpoint:
10457 fprintf_unfiltered (fp, "rwatch");
10458 break;
10459 case bp_access_watchpoint:
10460 fprintf_unfiltered (fp, "awatch");
10461 break;
10462 default:
10463 internal_error (__FILE__, __LINE__,
10464 _("Invalid watchpoint type."));
10465 }
10466
10467 fprintf_unfiltered (fp, " %s", w->exp_string);
10468 print_recreate_thread (b, fp);
10469 }
10470
10471 /* Implement the "explains_signal" breakpoint_ops method for
10472 watchpoints. */
10473
10474 static int
10475 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10476 {
10477 /* A software watchpoint cannot cause a signal other than
10478 GDB_SIGNAL_TRAP. */
10479 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10480 return 0;
10481
10482 return 1;
10483 }
10484
10485 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10486
10487 static struct breakpoint_ops watchpoint_breakpoint_ops;
10488
10489 /* Implement the "insert" breakpoint_ops method for
10490 masked hardware watchpoints. */
10491
10492 static int
10493 insert_masked_watchpoint (struct bp_location *bl)
10494 {
10495 struct watchpoint *w = (struct watchpoint *) bl->owner;
10496
10497 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10498 bl->watchpoint_type);
10499 }
10500
10501 /* Implement the "remove" breakpoint_ops method for
10502 masked hardware watchpoints. */
10503
10504 static int
10505 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10506 {
10507 struct watchpoint *w = (struct watchpoint *) bl->owner;
10508
10509 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10510 bl->watchpoint_type);
10511 }
10512
10513 /* Implement the "resources_needed" breakpoint_ops method for
10514 masked hardware watchpoints. */
10515
10516 static int
10517 resources_needed_masked_watchpoint (const struct bp_location *bl)
10518 {
10519 struct watchpoint *w = (struct watchpoint *) bl->owner;
10520
10521 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10522 }
10523
10524 /* Implement the "works_in_software_mode" breakpoint_ops method for
10525 masked hardware watchpoints. */
10526
10527 static int
10528 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10529 {
10530 return 0;
10531 }
10532
10533 /* Implement the "print_it" breakpoint_ops method for
10534 masked hardware watchpoints. */
10535
10536 static enum print_stop_action
10537 print_it_masked_watchpoint (bpstat bs)
10538 {
10539 struct breakpoint *b = bs->breakpoint_at;
10540 struct ui_out *uiout = current_uiout;
10541
10542 /* Masked watchpoints have only one location. */
10543 gdb_assert (b->loc && b->loc->next == NULL);
10544
10545 annotate_watchpoint (b->number);
10546 maybe_print_thread_hit_breakpoint (uiout);
10547
10548 switch (b->type)
10549 {
10550 case bp_hardware_watchpoint:
10551 if (uiout->is_mi_like_p ())
10552 uiout->field_string
10553 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10554 break;
10555
10556 case bp_read_watchpoint:
10557 if (uiout->is_mi_like_p ())
10558 uiout->field_string
10559 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10560 break;
10561
10562 case bp_access_watchpoint:
10563 if (uiout->is_mi_like_p ())
10564 uiout->field_string
10565 ("reason",
10566 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10567 break;
10568 default:
10569 internal_error (__FILE__, __LINE__,
10570 _("Invalid hardware watchpoint type."));
10571 }
10572
10573 mention (b);
10574 uiout->text (_("\n\
10575 Check the underlying instruction at PC for the memory\n\
10576 address and value which triggered this watchpoint.\n"));
10577 uiout->text ("\n");
10578
10579 /* More than one watchpoint may have been triggered. */
10580 return PRINT_UNKNOWN;
10581 }
10582
10583 /* Implement the "print_one_detail" breakpoint_ops method for
10584 masked hardware watchpoints. */
10585
10586 static void
10587 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10588 struct ui_out *uiout)
10589 {
10590 struct watchpoint *w = (struct watchpoint *) b;
10591
10592 /* Masked watchpoints have only one location. */
10593 gdb_assert (b->loc && b->loc->next == NULL);
10594
10595 uiout->text ("\tmask ");
10596 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10597 uiout->text ("\n");
10598 }
10599
10600 /* Implement the "print_mention" breakpoint_ops method for
10601 masked hardware watchpoints. */
10602
10603 static void
10604 print_mention_masked_watchpoint (struct breakpoint *b)
10605 {
10606 struct watchpoint *w = (struct watchpoint *) b;
10607 struct ui_out *uiout = current_uiout;
10608 const char *tuple_name;
10609
10610 switch (b->type)
10611 {
10612 case bp_hardware_watchpoint:
10613 uiout->text ("Masked hardware watchpoint ");
10614 tuple_name = "wpt";
10615 break;
10616 case bp_read_watchpoint:
10617 uiout->text ("Masked hardware read watchpoint ");
10618 tuple_name = "hw-rwpt";
10619 break;
10620 case bp_access_watchpoint:
10621 uiout->text ("Masked hardware access (read/write) watchpoint ");
10622 tuple_name = "hw-awpt";
10623 break;
10624 default:
10625 internal_error (__FILE__, __LINE__,
10626 _("Invalid hardware watchpoint type."));
10627 }
10628
10629 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10630 uiout->field_int ("number", b->number);
10631 uiout->text (": ");
10632 uiout->field_string ("exp", w->exp_string);
10633 }
10634
10635 /* Implement the "print_recreate" breakpoint_ops method for
10636 masked hardware watchpoints. */
10637
10638 static void
10639 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10640 {
10641 struct watchpoint *w = (struct watchpoint *) b;
10642 char tmp[40];
10643
10644 switch (b->type)
10645 {
10646 case bp_hardware_watchpoint:
10647 fprintf_unfiltered (fp, "watch");
10648 break;
10649 case bp_read_watchpoint:
10650 fprintf_unfiltered (fp, "rwatch");
10651 break;
10652 case bp_access_watchpoint:
10653 fprintf_unfiltered (fp, "awatch");
10654 break;
10655 default:
10656 internal_error (__FILE__, __LINE__,
10657 _("Invalid hardware watchpoint type."));
10658 }
10659
10660 sprintf_vma (tmp, w->hw_wp_mask);
10661 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10662 print_recreate_thread (b, fp);
10663 }
10664
10665 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10666
10667 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10668
10669 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10670
10671 static int
10672 is_masked_watchpoint (const struct breakpoint *b)
10673 {
10674 return b->ops == &masked_watchpoint_breakpoint_ops;
10675 }
10676
10677 /* accessflag: hw_write: watch write,
10678 hw_read: watch read,
10679 hw_access: watch access (read or write) */
10680 static void
10681 watch_command_1 (const char *arg, int accessflag, int from_tty,
10682 int just_location, int internal)
10683 {
10684 struct breakpoint *scope_breakpoint = NULL;
10685 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10686 struct value *val, *mark, *result;
10687 int saved_bitpos = 0, saved_bitsize = 0;
10688 const char *exp_start = NULL;
10689 const char *exp_end = NULL;
10690 const char *tok, *end_tok;
10691 int toklen = -1;
10692 const char *cond_start = NULL;
10693 const char *cond_end = NULL;
10694 enum bptype bp_type;
10695 int thread = -1;
10696 int pc = 0;
10697 /* Flag to indicate whether we are going to use masks for
10698 the hardware watchpoint. */
10699 int use_mask = 0;
10700 CORE_ADDR mask = 0;
10701
10702 /* Make sure that we actually have parameters to parse. */
10703 if (arg != NULL && arg[0] != '\0')
10704 {
10705 const char *value_start;
10706
10707 exp_end = arg + strlen (arg);
10708
10709 /* Look for "parameter value" pairs at the end
10710 of the arguments string. */
10711 for (tok = exp_end - 1; tok > arg; tok--)
10712 {
10713 /* Skip whitespace at the end of the argument list. */
10714 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10715 tok--;
10716
10717 /* Find the beginning of the last token.
10718 This is the value of the parameter. */
10719 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10720 tok--;
10721 value_start = tok + 1;
10722
10723 /* Skip whitespace. */
10724 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10725 tok--;
10726
10727 end_tok = tok;
10728
10729 /* Find the beginning of the second to last token.
10730 This is the parameter itself. */
10731 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10732 tok--;
10733 tok++;
10734 toklen = end_tok - tok + 1;
10735
10736 if (toklen == 6 && startswith (tok, "thread"))
10737 {
10738 struct thread_info *thr;
10739 /* At this point we've found a "thread" token, which means
10740 the user is trying to set a watchpoint that triggers
10741 only in a specific thread. */
10742 const char *endp;
10743
10744 if (thread != -1)
10745 error(_("You can specify only one thread."));
10746
10747 /* Extract the thread ID from the next token. */
10748 thr = parse_thread_id (value_start, &endp);
10749
10750 /* Check if the user provided a valid thread ID. */
10751 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10752 invalid_thread_id_error (value_start);
10753
10754 thread = thr->global_num;
10755 }
10756 else if (toklen == 4 && startswith (tok, "mask"))
10757 {
10758 /* We've found a "mask" token, which means the user wants to
10759 create a hardware watchpoint that is going to have the mask
10760 facility. */
10761 struct value *mask_value, *mark;
10762
10763 if (use_mask)
10764 error(_("You can specify only one mask."));
10765
10766 use_mask = just_location = 1;
10767
10768 mark = value_mark ();
10769 mask_value = parse_to_comma_and_eval (&value_start);
10770 mask = value_as_address (mask_value);
10771 value_free_to_mark (mark);
10772 }
10773 else
10774 /* We didn't recognize what we found. We should stop here. */
10775 break;
10776
10777 /* Truncate the string and get rid of the "parameter value" pair before
10778 the arguments string is parsed by the parse_exp_1 function. */
10779 exp_end = tok;
10780 }
10781 }
10782 else
10783 exp_end = arg;
10784
10785 /* Parse the rest of the arguments. From here on out, everything
10786 is in terms of a newly allocated string instead of the original
10787 ARG. */
10788 innermost_block = NULL;
10789 std::string expression (arg, exp_end - arg);
10790 exp_start = arg = expression.c_str ();
10791 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
10792 exp_end = arg;
10793 /* Remove trailing whitespace from the expression before saving it.
10794 This makes the eventual display of the expression string a bit
10795 prettier. */
10796 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10797 --exp_end;
10798
10799 /* Checking if the expression is not constant. */
10800 if (watchpoint_exp_is_const (exp.get ()))
10801 {
10802 int len;
10803
10804 len = exp_end - exp_start;
10805 while (len > 0 && isspace (exp_start[len - 1]))
10806 len--;
10807 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10808 }
10809
10810 exp_valid_block = innermost_block;
10811 mark = value_mark ();
10812 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
10813
10814 if (val != NULL && just_location)
10815 {
10816 saved_bitpos = value_bitpos (val);
10817 saved_bitsize = value_bitsize (val);
10818 }
10819
10820 if (just_location)
10821 {
10822 int ret;
10823
10824 exp_valid_block = NULL;
10825 val = value_addr (result);
10826 release_value (val);
10827 value_free_to_mark (mark);
10828
10829 if (use_mask)
10830 {
10831 ret = target_masked_watch_num_registers (value_as_address (val),
10832 mask);
10833 if (ret == -1)
10834 error (_("This target does not support masked watchpoints."));
10835 else if (ret == -2)
10836 error (_("Invalid mask or memory region."));
10837 }
10838 }
10839 else if (val != NULL)
10840 release_value (val);
10841
10842 tok = skip_spaces (arg);
10843 end_tok = skip_to_space (tok);
10844
10845 toklen = end_tok - tok;
10846 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10847 {
10848 innermost_block = NULL;
10849 tok = cond_start = end_tok + 1;
10850 parse_exp_1 (&tok, 0, 0, 0);
10851
10852 /* The watchpoint expression may not be local, but the condition
10853 may still be. E.g.: `watch global if local > 0'. */
10854 cond_exp_valid_block = innermost_block;
10855
10856 cond_end = tok;
10857 }
10858 if (*tok)
10859 error (_("Junk at end of command."));
10860
10861 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10862
10863 /* Save this because create_internal_breakpoint below invalidates
10864 'wp_frame'. */
10865 frame_id watchpoint_frame = get_frame_id (wp_frame);
10866
10867 /* If the expression is "local", then set up a "watchpoint scope"
10868 breakpoint at the point where we've left the scope of the watchpoint
10869 expression. Create the scope breakpoint before the watchpoint, so
10870 that we will encounter it first in bpstat_stop_status. */
10871 if (exp_valid_block != NULL && wp_frame != NULL)
10872 {
10873 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10874
10875 if (frame_id_p (caller_frame_id))
10876 {
10877 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10878 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10879
10880 scope_breakpoint
10881 = create_internal_breakpoint (caller_arch, caller_pc,
10882 bp_watchpoint_scope,
10883 &momentary_breakpoint_ops);
10884
10885 /* create_internal_breakpoint could invalidate WP_FRAME. */
10886 wp_frame = NULL;
10887
10888 scope_breakpoint->enable_state = bp_enabled;
10889
10890 /* Automatically delete the breakpoint when it hits. */
10891 scope_breakpoint->disposition = disp_del;
10892
10893 /* Only break in the proper frame (help with recursion). */
10894 scope_breakpoint->frame_id = caller_frame_id;
10895
10896 /* Set the address at which we will stop. */
10897 scope_breakpoint->loc->gdbarch = caller_arch;
10898 scope_breakpoint->loc->requested_address = caller_pc;
10899 scope_breakpoint->loc->address
10900 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10901 scope_breakpoint->loc->requested_address,
10902 scope_breakpoint->type);
10903 }
10904 }
10905
10906 /* Now set up the breakpoint. We create all watchpoints as hardware
10907 watchpoints here even if hardware watchpoints are turned off, a call
10908 to update_watchpoint later in this function will cause the type to
10909 drop back to bp_watchpoint (software watchpoint) if required. */
10910
10911 if (accessflag == hw_read)
10912 bp_type = bp_read_watchpoint;
10913 else if (accessflag == hw_access)
10914 bp_type = bp_access_watchpoint;
10915 else
10916 bp_type = bp_hardware_watchpoint;
10917
10918 std::unique_ptr<watchpoint> w (new watchpoint ());
10919
10920 if (use_mask)
10921 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10922 &masked_watchpoint_breakpoint_ops);
10923 else
10924 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10925 &watchpoint_breakpoint_ops);
10926 w->thread = thread;
10927 w->disposition = disp_donttouch;
10928 w->pspace = current_program_space;
10929 w->exp = std::move (exp);
10930 w->exp_valid_block = exp_valid_block;
10931 w->cond_exp_valid_block = cond_exp_valid_block;
10932 if (just_location)
10933 {
10934 struct type *t = value_type (val);
10935 CORE_ADDR addr = value_as_address (val);
10936
10937 w->exp_string_reparse
10938 = current_language->la_watch_location_expression (t, addr).release ();
10939
10940 w->exp_string = xstrprintf ("-location %.*s",
10941 (int) (exp_end - exp_start), exp_start);
10942 }
10943 else
10944 w->exp_string = savestring (exp_start, exp_end - exp_start);
10945
10946 if (use_mask)
10947 {
10948 w->hw_wp_mask = mask;
10949 }
10950 else
10951 {
10952 w->val = val;
10953 w->val_bitpos = saved_bitpos;
10954 w->val_bitsize = saved_bitsize;
10955 w->val_valid = 1;
10956 }
10957
10958 if (cond_start)
10959 w->cond_string = savestring (cond_start, cond_end - cond_start);
10960 else
10961 w->cond_string = 0;
10962
10963 if (frame_id_p (watchpoint_frame))
10964 {
10965 w->watchpoint_frame = watchpoint_frame;
10966 w->watchpoint_thread = inferior_ptid;
10967 }
10968 else
10969 {
10970 w->watchpoint_frame = null_frame_id;
10971 w->watchpoint_thread = null_ptid;
10972 }
10973
10974 if (scope_breakpoint != NULL)
10975 {
10976 /* The scope breakpoint is related to the watchpoint. We will
10977 need to act on them together. */
10978 w->related_breakpoint = scope_breakpoint;
10979 scope_breakpoint->related_breakpoint = w.get ();
10980 }
10981
10982 if (!just_location)
10983 value_free_to_mark (mark);
10984
10985 /* Finally update the new watchpoint. This creates the locations
10986 that should be inserted. */
10987 update_watchpoint (w.get (), 1);
10988
10989 install_breakpoint (internal, std::move (w), 1);
10990 }
10991
10992 /* Return count of debug registers needed to watch the given expression.
10993 If the watchpoint cannot be handled in hardware return zero. */
10994
10995 static int
10996 can_use_hardware_watchpoint (struct value *v)
10997 {
10998 int found_memory_cnt = 0;
10999 struct value *head = v;
11000
11001 /* Did the user specifically forbid us to use hardware watchpoints? */
11002 if (!can_use_hw_watchpoints)
11003 return 0;
11004
11005 /* Make sure that the value of the expression depends only upon
11006 memory contents, and values computed from them within GDB. If we
11007 find any register references or function calls, we can't use a
11008 hardware watchpoint.
11009
11010 The idea here is that evaluating an expression generates a series
11011 of values, one holding the value of every subexpression. (The
11012 expression a*b+c has five subexpressions: a, b, a*b, c, and
11013 a*b+c.) GDB's values hold almost enough information to establish
11014 the criteria given above --- they identify memory lvalues,
11015 register lvalues, computed values, etcetera. So we can evaluate
11016 the expression, and then scan the chain of values that leaves
11017 behind to decide whether we can detect any possible change to the
11018 expression's final value using only hardware watchpoints.
11019
11020 However, I don't think that the values returned by inferior
11021 function calls are special in any way. So this function may not
11022 notice that an expression involving an inferior function call
11023 can't be watched with hardware watchpoints. FIXME. */
11024 for (; v; v = value_next (v))
11025 {
11026 if (VALUE_LVAL (v) == lval_memory)
11027 {
11028 if (v != head && value_lazy (v))
11029 /* A lazy memory lvalue in the chain is one that GDB never
11030 needed to fetch; we either just used its address (e.g.,
11031 `a' in `a.b') or we never needed it at all (e.g., `a'
11032 in `a,b'). This doesn't apply to HEAD; if that is
11033 lazy then it was not readable, but watch it anyway. */
11034 ;
11035 else
11036 {
11037 /* Ahh, memory we actually used! Check if we can cover
11038 it with hardware watchpoints. */
11039 struct type *vtype = check_typedef (value_type (v));
11040
11041 /* We only watch structs and arrays if user asked for it
11042 explicitly, never if they just happen to appear in a
11043 middle of some value chain. */
11044 if (v == head
11045 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11046 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11047 {
11048 CORE_ADDR vaddr = value_address (v);
11049 int len;
11050 int num_regs;
11051
11052 len = (target_exact_watchpoints
11053 && is_scalar_type_recursive (vtype))?
11054 1 : TYPE_LENGTH (value_type (v));
11055
11056 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11057 if (!num_regs)
11058 return 0;
11059 else
11060 found_memory_cnt += num_regs;
11061 }
11062 }
11063 }
11064 else if (VALUE_LVAL (v) != not_lval
11065 && deprecated_value_modifiable (v) == 0)
11066 return 0; /* These are values from the history (e.g., $1). */
11067 else if (VALUE_LVAL (v) == lval_register)
11068 return 0; /* Cannot watch a register with a HW watchpoint. */
11069 }
11070
11071 /* The expression itself looks suitable for using a hardware
11072 watchpoint, but give the target machine a chance to reject it. */
11073 return found_memory_cnt;
11074 }
11075
11076 void
11077 watch_command_wrapper (const char *arg, int from_tty, int internal)
11078 {
11079 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11080 }
11081
11082 /* A helper function that looks for the "-location" argument and then
11083 calls watch_command_1. */
11084
11085 static void
11086 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11087 {
11088 int just_location = 0;
11089
11090 if (arg
11091 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11092 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11093 {
11094 arg = skip_spaces (arg);
11095 just_location = 1;
11096 }
11097
11098 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11099 }
11100
11101 static void
11102 watch_command (char *arg, int from_tty)
11103 {
11104 watch_maybe_just_location (arg, hw_write, from_tty);
11105 }
11106
11107 void
11108 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
11109 {
11110 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11111 }
11112
11113 static void
11114 rwatch_command (char *arg, int from_tty)
11115 {
11116 watch_maybe_just_location (arg, hw_read, from_tty);
11117 }
11118
11119 void
11120 awatch_command_wrapper (const char *arg, int from_tty, int internal)
11121 {
11122 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11123 }
11124
11125 static void
11126 awatch_command (char *arg, int from_tty)
11127 {
11128 watch_maybe_just_location (arg, hw_access, from_tty);
11129 }
11130 \f
11131
11132 /* Data for the FSM that manages the until(location)/advance commands
11133 in infcmd.c. Here because it uses the mechanisms of
11134 breakpoints. */
11135
11136 struct until_break_fsm
11137 {
11138 /* The base class. */
11139 struct thread_fsm thread_fsm;
11140
11141 /* The thread that as current when the command was executed. */
11142 int thread;
11143
11144 /* The breakpoint set at the destination location. */
11145 struct breakpoint *location_breakpoint;
11146
11147 /* Breakpoint set at the return address in the caller frame. May be
11148 NULL. */
11149 struct breakpoint *caller_breakpoint;
11150 };
11151
11152 static void until_break_fsm_clean_up (struct thread_fsm *self,
11153 struct thread_info *thread);
11154 static int until_break_fsm_should_stop (struct thread_fsm *self,
11155 struct thread_info *thread);
11156 static enum async_reply_reason
11157 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11158
11159 /* until_break_fsm's vtable. */
11160
11161 static struct thread_fsm_ops until_break_fsm_ops =
11162 {
11163 NULL, /* dtor */
11164 until_break_fsm_clean_up,
11165 until_break_fsm_should_stop,
11166 NULL, /* return_value */
11167 until_break_fsm_async_reply_reason,
11168 };
11169
11170 /* Allocate a new until_break_command_fsm. */
11171
11172 static struct until_break_fsm *
11173 new_until_break_fsm (struct interp *cmd_interp, int thread,
11174 struct breakpoint *location_breakpoint,
11175 struct breakpoint *caller_breakpoint)
11176 {
11177 struct until_break_fsm *sm;
11178
11179 sm = XCNEW (struct until_break_fsm);
11180 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11181
11182 sm->thread = thread;
11183 sm->location_breakpoint = location_breakpoint;
11184 sm->caller_breakpoint = caller_breakpoint;
11185
11186 return sm;
11187 }
11188
11189 /* Implementation of the 'should_stop' FSM method for the
11190 until(location)/advance commands. */
11191
11192 static int
11193 until_break_fsm_should_stop (struct thread_fsm *self,
11194 struct thread_info *tp)
11195 {
11196 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11197
11198 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11199 sm->location_breakpoint) != NULL
11200 || (sm->caller_breakpoint != NULL
11201 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11202 sm->caller_breakpoint) != NULL))
11203 thread_fsm_set_finished (self);
11204
11205 return 1;
11206 }
11207
11208 /* Implementation of the 'clean_up' FSM method for the
11209 until(location)/advance commands. */
11210
11211 static void
11212 until_break_fsm_clean_up (struct thread_fsm *self,
11213 struct thread_info *thread)
11214 {
11215 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11216
11217 /* Clean up our temporary breakpoints. */
11218 if (sm->location_breakpoint != NULL)
11219 {
11220 delete_breakpoint (sm->location_breakpoint);
11221 sm->location_breakpoint = NULL;
11222 }
11223 if (sm->caller_breakpoint != NULL)
11224 {
11225 delete_breakpoint (sm->caller_breakpoint);
11226 sm->caller_breakpoint = NULL;
11227 }
11228 delete_longjmp_breakpoint (sm->thread);
11229 }
11230
11231 /* Implementation of the 'async_reply_reason' FSM method for the
11232 until(location)/advance commands. */
11233
11234 static enum async_reply_reason
11235 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11236 {
11237 return EXEC_ASYNC_LOCATION_REACHED;
11238 }
11239
11240 void
11241 until_break_command (const char *arg, int from_tty, int anywhere)
11242 {
11243 struct frame_info *frame;
11244 struct gdbarch *frame_gdbarch;
11245 struct frame_id stack_frame_id;
11246 struct frame_id caller_frame_id;
11247 struct breakpoint *location_breakpoint;
11248 struct breakpoint *caller_breakpoint = NULL;
11249 struct cleanup *old_chain;
11250 int thread;
11251 struct thread_info *tp;
11252 struct until_break_fsm *sm;
11253
11254 clear_proceed_status (0);
11255
11256 /* Set a breakpoint where the user wants it and at return from
11257 this function. */
11258
11259 event_location_up location = string_to_event_location (&arg, current_language);
11260
11261 std::vector<symtab_and_line> sals
11262 = (last_displayed_sal_is_valid ()
11263 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11264 get_last_displayed_symtab (),
11265 get_last_displayed_line ())
11266 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11267 NULL, (struct symtab *) NULL, 0));
11268
11269 if (sals.size () != 1)
11270 error (_("Couldn't get information on specified line."));
11271
11272 symtab_and_line &sal = sals[0];
11273
11274 if (*arg)
11275 error (_("Junk at end of arguments."));
11276
11277 resolve_sal_pc (&sal);
11278
11279 tp = inferior_thread ();
11280 thread = tp->global_num;
11281
11282 old_chain = make_cleanup (null_cleanup, NULL);
11283
11284 /* Note linespec handling above invalidates the frame chain.
11285 Installing a breakpoint also invalidates the frame chain (as it
11286 may need to switch threads), so do any frame handling before
11287 that. */
11288
11289 frame = get_selected_frame (NULL);
11290 frame_gdbarch = get_frame_arch (frame);
11291 stack_frame_id = get_stack_frame_id (frame);
11292 caller_frame_id = frame_unwind_caller_id (frame);
11293
11294 /* Keep within the current frame, or in frames called by the current
11295 one. */
11296
11297 if (frame_id_p (caller_frame_id))
11298 {
11299 struct symtab_and_line sal2;
11300 struct gdbarch *caller_gdbarch;
11301
11302 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11303 sal2.pc = frame_unwind_caller_pc (frame);
11304 caller_gdbarch = frame_unwind_caller_arch (frame);
11305 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11306 sal2,
11307 caller_frame_id,
11308 bp_until);
11309 make_cleanup_delete_breakpoint (caller_breakpoint);
11310
11311 set_longjmp_breakpoint (tp, caller_frame_id);
11312 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11313 }
11314
11315 /* set_momentary_breakpoint could invalidate FRAME. */
11316 frame = NULL;
11317
11318 if (anywhere)
11319 /* If the user told us to continue until a specified location,
11320 we don't specify a frame at which we need to stop. */
11321 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11322 null_frame_id, bp_until);
11323 else
11324 /* Otherwise, specify the selected frame, because we want to stop
11325 only at the very same frame. */
11326 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11327 stack_frame_id, bp_until);
11328 make_cleanup_delete_breakpoint (location_breakpoint);
11329
11330 sm = new_until_break_fsm (command_interp (), tp->global_num,
11331 location_breakpoint, caller_breakpoint);
11332 tp->thread_fsm = &sm->thread_fsm;
11333
11334 discard_cleanups (old_chain);
11335
11336 proceed (-1, GDB_SIGNAL_DEFAULT);
11337 }
11338
11339 /* This function attempts to parse an optional "if <cond>" clause
11340 from the arg string. If one is not found, it returns NULL.
11341
11342 Else, it returns a pointer to the condition string. (It does not
11343 attempt to evaluate the string against a particular block.) And,
11344 it updates arg to point to the first character following the parsed
11345 if clause in the arg string. */
11346
11347 const char *
11348 ep_parse_optional_if_clause (const char **arg)
11349 {
11350 const char *cond_string;
11351
11352 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11353 return NULL;
11354
11355 /* Skip the "if" keyword. */
11356 (*arg) += 2;
11357
11358 /* Skip any extra leading whitespace, and record the start of the
11359 condition string. */
11360 *arg = skip_spaces (*arg);
11361 cond_string = *arg;
11362
11363 /* Assume that the condition occupies the remainder of the arg
11364 string. */
11365 (*arg) += strlen (cond_string);
11366
11367 return cond_string;
11368 }
11369
11370 /* Commands to deal with catching events, such as signals, exceptions,
11371 process start/exit, etc. */
11372
11373 typedef enum
11374 {
11375 catch_fork_temporary, catch_vfork_temporary,
11376 catch_fork_permanent, catch_vfork_permanent
11377 }
11378 catch_fork_kind;
11379
11380 static void
11381 catch_fork_command_1 (char *arg_entry, int from_tty,
11382 struct cmd_list_element *command)
11383 {
11384 const char *arg = arg_entry;
11385 struct gdbarch *gdbarch = get_current_arch ();
11386 const char *cond_string = NULL;
11387 catch_fork_kind fork_kind;
11388 int tempflag;
11389
11390 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11391 tempflag = (fork_kind == catch_fork_temporary
11392 || fork_kind == catch_vfork_temporary);
11393
11394 if (!arg)
11395 arg = "";
11396 arg = skip_spaces (arg);
11397
11398 /* The allowed syntax is:
11399 catch [v]fork
11400 catch [v]fork if <cond>
11401
11402 First, check if there's an if clause. */
11403 cond_string = ep_parse_optional_if_clause (&arg);
11404
11405 if ((*arg != '\0') && !isspace (*arg))
11406 error (_("Junk at end of arguments."));
11407
11408 /* If this target supports it, create a fork or vfork catchpoint
11409 and enable reporting of such events. */
11410 switch (fork_kind)
11411 {
11412 case catch_fork_temporary:
11413 case catch_fork_permanent:
11414 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11415 &catch_fork_breakpoint_ops);
11416 break;
11417 case catch_vfork_temporary:
11418 case catch_vfork_permanent:
11419 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11420 &catch_vfork_breakpoint_ops);
11421 break;
11422 default:
11423 error (_("unsupported or unknown fork kind; cannot catch it"));
11424 break;
11425 }
11426 }
11427
11428 static void
11429 catch_exec_command_1 (char *arg_entry, int from_tty,
11430 struct cmd_list_element *command)
11431 {
11432 const char *arg = arg_entry;
11433 struct gdbarch *gdbarch = get_current_arch ();
11434 int tempflag;
11435 const char *cond_string = NULL;
11436
11437 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11438
11439 if (!arg)
11440 arg = "";
11441 arg = skip_spaces (arg);
11442
11443 /* The allowed syntax is:
11444 catch exec
11445 catch exec if <cond>
11446
11447 First, check if there's an if clause. */
11448 cond_string = ep_parse_optional_if_clause (&arg);
11449
11450 if ((*arg != '\0') && !isspace (*arg))
11451 error (_("Junk at end of arguments."));
11452
11453 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11454 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11455 &catch_exec_breakpoint_ops);
11456 c->exec_pathname = NULL;
11457
11458 install_breakpoint (0, std::move (c), 1);
11459 }
11460
11461 void
11462 init_ada_exception_breakpoint (struct breakpoint *b,
11463 struct gdbarch *gdbarch,
11464 struct symtab_and_line sal,
11465 const char *addr_string,
11466 const struct breakpoint_ops *ops,
11467 int tempflag,
11468 int enabled,
11469 int from_tty)
11470 {
11471 if (from_tty)
11472 {
11473 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11474 if (!loc_gdbarch)
11475 loc_gdbarch = gdbarch;
11476
11477 describe_other_breakpoints (loc_gdbarch,
11478 sal.pspace, sal.pc, sal.section, -1);
11479 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11480 version for exception catchpoints, because two catchpoints
11481 used for different exception names will use the same address.
11482 In this case, a "breakpoint ... also set at..." warning is
11483 unproductive. Besides, the warning phrasing is also a bit
11484 inappropriate, we should use the word catchpoint, and tell
11485 the user what type of catchpoint it is. The above is good
11486 enough for now, though. */
11487 }
11488
11489 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11490
11491 b->enable_state = enabled ? bp_enabled : bp_disabled;
11492 b->disposition = tempflag ? disp_del : disp_donttouch;
11493 b->location = string_to_event_location (&addr_string,
11494 language_def (language_ada));
11495 b->language = language_ada;
11496 }
11497
11498 static void
11499 catch_command (char *arg, int from_tty)
11500 {
11501 error (_("Catch requires an event name."));
11502 }
11503 \f
11504
11505 static void
11506 tcatch_command (char *arg, int from_tty)
11507 {
11508 error (_("Catch requires an event name."));
11509 }
11510
11511 /* Compare two breakpoints and return a strcmp-like result. */
11512
11513 static int
11514 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11515 {
11516 uintptr_t ua = (uintptr_t) a;
11517 uintptr_t ub = (uintptr_t) b;
11518
11519 if (a->number < b->number)
11520 return -1;
11521 else if (a->number > b->number)
11522 return 1;
11523
11524 /* Now sort by address, in case we see, e..g, two breakpoints with
11525 the number 0. */
11526 if (ua < ub)
11527 return -1;
11528 return ua > ub ? 1 : 0;
11529 }
11530
11531 /* Delete breakpoints by address or line. */
11532
11533 static void
11534 clear_command (char *arg, int from_tty)
11535 {
11536 struct breakpoint *b;
11537 int default_match;
11538 int i;
11539
11540 std::vector<symtab_and_line> decoded_sals;
11541 symtab_and_line last_sal;
11542 gdb::array_view<symtab_and_line> sals;
11543 if (arg)
11544 {
11545 decoded_sals
11546 = decode_line_with_current_source (arg,
11547 (DECODE_LINE_FUNFIRSTLINE
11548 | DECODE_LINE_LIST_MODE));
11549 default_match = 0;
11550 sals = decoded_sals;
11551 }
11552 else
11553 {
11554 /* Set sal's line, symtab, pc, and pspace to the values
11555 corresponding to the last call to print_frame_info. If the
11556 codepoint is not valid, this will set all the fields to 0. */
11557 last_sal = get_last_displayed_sal ();
11558 if (last_sal.symtab == 0)
11559 error (_("No source file specified."));
11560
11561 default_match = 1;
11562 sals = last_sal;
11563 }
11564
11565 /* We don't call resolve_sal_pc here. That's not as bad as it
11566 seems, because all existing breakpoints typically have both
11567 file/line and pc set. So, if clear is given file/line, we can
11568 match this to existing breakpoint without obtaining pc at all.
11569
11570 We only support clearing given the address explicitly
11571 present in breakpoint table. Say, we've set breakpoint
11572 at file:line. There were several PC values for that file:line,
11573 due to optimization, all in one block.
11574
11575 We've picked one PC value. If "clear" is issued with another
11576 PC corresponding to the same file:line, the breakpoint won't
11577 be cleared. We probably can still clear the breakpoint, but
11578 since the other PC value is never presented to user, user
11579 can only find it by guessing, and it does not seem important
11580 to support that. */
11581
11582 /* For each line spec given, delete bps which correspond to it. Do
11583 it in two passes, solely to preserve the current behavior that
11584 from_tty is forced true if we delete more than one
11585 breakpoint. */
11586
11587 std::vector<struct breakpoint *> found;
11588 for (const auto &sal : sals)
11589 {
11590 const char *sal_fullname;
11591
11592 /* If exact pc given, clear bpts at that pc.
11593 If line given (pc == 0), clear all bpts on specified line.
11594 If defaulting, clear all bpts on default line
11595 or at default pc.
11596
11597 defaulting sal.pc != 0 tests to do
11598
11599 0 1 pc
11600 1 1 pc _and_ line
11601 0 0 line
11602 1 0 <can't happen> */
11603
11604 sal_fullname = (sal.symtab == NULL
11605 ? NULL : symtab_to_fullname (sal.symtab));
11606
11607 /* Find all matching breakpoints and add them to 'found'. */
11608 ALL_BREAKPOINTS (b)
11609 {
11610 int match = 0;
11611 /* Are we going to delete b? */
11612 if (b->type != bp_none && !is_watchpoint (b))
11613 {
11614 struct bp_location *loc = b->loc;
11615 for (; loc; loc = loc->next)
11616 {
11617 /* If the user specified file:line, don't allow a PC
11618 match. This matches historical gdb behavior. */
11619 int pc_match = (!sal.explicit_line
11620 && sal.pc
11621 && (loc->pspace == sal.pspace)
11622 && (loc->address == sal.pc)
11623 && (!section_is_overlay (loc->section)
11624 || loc->section == sal.section));
11625 int line_match = 0;
11626
11627 if ((default_match || sal.explicit_line)
11628 && loc->symtab != NULL
11629 && sal_fullname != NULL
11630 && sal.pspace == loc->pspace
11631 && loc->line_number == sal.line
11632 && filename_cmp (symtab_to_fullname (loc->symtab),
11633 sal_fullname) == 0)
11634 line_match = 1;
11635
11636 if (pc_match || line_match)
11637 {
11638 match = 1;
11639 break;
11640 }
11641 }
11642 }
11643
11644 if (match)
11645 found.push_back (b);
11646 }
11647 }
11648
11649 /* Now go thru the 'found' chain and delete them. */
11650 if (found.empty ())
11651 {
11652 if (arg)
11653 error (_("No breakpoint at %s."), arg);
11654 else
11655 error (_("No breakpoint at this line."));
11656 }
11657
11658 /* Remove duplicates from the vec. */
11659 std::sort (found.begin (), found.end (),
11660 [] (const breakpoint *a, const breakpoint *b)
11661 {
11662 return compare_breakpoints (a, b) < 0;
11663 });
11664 found.erase (std::unique (found.begin (), found.end (),
11665 [] (const breakpoint *a, const breakpoint *b)
11666 {
11667 return compare_breakpoints (a, b) == 0;
11668 }),
11669 found.end ());
11670
11671 if (found.size () > 1)
11672 from_tty = 1; /* Always report if deleted more than one. */
11673 if (from_tty)
11674 {
11675 if (found.size () == 1)
11676 printf_unfiltered (_("Deleted breakpoint "));
11677 else
11678 printf_unfiltered (_("Deleted breakpoints "));
11679 }
11680
11681 for (breakpoint *iter : found)
11682 {
11683 if (from_tty)
11684 printf_unfiltered ("%d ", iter->number);
11685 delete_breakpoint (iter);
11686 }
11687 if (from_tty)
11688 putchar_unfiltered ('\n');
11689 }
11690 \f
11691 /* Delete breakpoint in BS if they are `delete' breakpoints and
11692 all breakpoints that are marked for deletion, whether hit or not.
11693 This is called after any breakpoint is hit, or after errors. */
11694
11695 void
11696 breakpoint_auto_delete (bpstat bs)
11697 {
11698 struct breakpoint *b, *b_tmp;
11699
11700 for (; bs; bs = bs->next)
11701 if (bs->breakpoint_at
11702 && bs->breakpoint_at->disposition == disp_del
11703 && bs->stop)
11704 delete_breakpoint (bs->breakpoint_at);
11705
11706 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11707 {
11708 if (b->disposition == disp_del_at_next_stop)
11709 delete_breakpoint (b);
11710 }
11711 }
11712
11713 /* A comparison function for bp_location AP and BP being interfaced to
11714 qsort. Sort elements primarily by their ADDRESS (no matter what
11715 does breakpoint_address_is_meaningful say for its OWNER),
11716 secondarily by ordering first permanent elements and
11717 terciarily just ensuring the array is sorted stable way despite
11718 qsort being an unstable algorithm. */
11719
11720 static int
11721 bp_locations_compare (const void *ap, const void *bp)
11722 {
11723 const struct bp_location *a = *(const struct bp_location **) ap;
11724 const struct bp_location *b = *(const struct bp_location **) bp;
11725
11726 if (a->address != b->address)
11727 return (a->address > b->address) - (a->address < b->address);
11728
11729 /* Sort locations at the same address by their pspace number, keeping
11730 locations of the same inferior (in a multi-inferior environment)
11731 grouped. */
11732
11733 if (a->pspace->num != b->pspace->num)
11734 return ((a->pspace->num > b->pspace->num)
11735 - (a->pspace->num < b->pspace->num));
11736
11737 /* Sort permanent breakpoints first. */
11738 if (a->permanent != b->permanent)
11739 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11740
11741 /* Make the internal GDB representation stable across GDB runs
11742 where A and B memory inside GDB can differ. Breakpoint locations of
11743 the same type at the same address can be sorted in arbitrary order. */
11744
11745 if (a->owner->number != b->owner->number)
11746 return ((a->owner->number > b->owner->number)
11747 - (a->owner->number < b->owner->number));
11748
11749 return (a > b) - (a < b);
11750 }
11751
11752 /* Set bp_locations_placed_address_before_address_max and
11753 bp_locations_shadow_len_after_address_max according to the current
11754 content of the bp_locations array. */
11755
11756 static void
11757 bp_locations_target_extensions_update (void)
11758 {
11759 struct bp_location *bl, **blp_tmp;
11760
11761 bp_locations_placed_address_before_address_max = 0;
11762 bp_locations_shadow_len_after_address_max = 0;
11763
11764 ALL_BP_LOCATIONS (bl, blp_tmp)
11765 {
11766 CORE_ADDR start, end, addr;
11767
11768 if (!bp_location_has_shadow (bl))
11769 continue;
11770
11771 start = bl->target_info.placed_address;
11772 end = start + bl->target_info.shadow_len;
11773
11774 gdb_assert (bl->address >= start);
11775 addr = bl->address - start;
11776 if (addr > bp_locations_placed_address_before_address_max)
11777 bp_locations_placed_address_before_address_max = addr;
11778
11779 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11780
11781 gdb_assert (bl->address < end);
11782 addr = end - bl->address;
11783 if (addr > bp_locations_shadow_len_after_address_max)
11784 bp_locations_shadow_len_after_address_max = addr;
11785 }
11786 }
11787
11788 /* Download tracepoint locations if they haven't been. */
11789
11790 static void
11791 download_tracepoint_locations (void)
11792 {
11793 struct breakpoint *b;
11794 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11795
11796 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11797
11798 ALL_TRACEPOINTS (b)
11799 {
11800 struct bp_location *bl;
11801 struct tracepoint *t;
11802 int bp_location_downloaded = 0;
11803
11804 if ((b->type == bp_fast_tracepoint
11805 ? !may_insert_fast_tracepoints
11806 : !may_insert_tracepoints))
11807 continue;
11808
11809 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11810 {
11811 if (target_can_download_tracepoint ())
11812 can_download_tracepoint = TRIBOOL_TRUE;
11813 else
11814 can_download_tracepoint = TRIBOOL_FALSE;
11815 }
11816
11817 if (can_download_tracepoint == TRIBOOL_FALSE)
11818 break;
11819
11820 for (bl = b->loc; bl; bl = bl->next)
11821 {
11822 /* In tracepoint, locations are _never_ duplicated, so
11823 should_be_inserted is equivalent to
11824 unduplicated_should_be_inserted. */
11825 if (!should_be_inserted (bl) || bl->inserted)
11826 continue;
11827
11828 switch_to_program_space_and_thread (bl->pspace);
11829
11830 target_download_tracepoint (bl);
11831
11832 bl->inserted = 1;
11833 bp_location_downloaded = 1;
11834 }
11835 t = (struct tracepoint *) b;
11836 t->number_on_target = b->number;
11837 if (bp_location_downloaded)
11838 observer_notify_breakpoint_modified (b);
11839 }
11840 }
11841
11842 /* Swap the insertion/duplication state between two locations. */
11843
11844 static void
11845 swap_insertion (struct bp_location *left, struct bp_location *right)
11846 {
11847 const int left_inserted = left->inserted;
11848 const int left_duplicate = left->duplicate;
11849 const int left_needs_update = left->needs_update;
11850 const struct bp_target_info left_target_info = left->target_info;
11851
11852 /* Locations of tracepoints can never be duplicated. */
11853 if (is_tracepoint (left->owner))
11854 gdb_assert (!left->duplicate);
11855 if (is_tracepoint (right->owner))
11856 gdb_assert (!right->duplicate);
11857
11858 left->inserted = right->inserted;
11859 left->duplicate = right->duplicate;
11860 left->needs_update = right->needs_update;
11861 left->target_info = right->target_info;
11862 right->inserted = left_inserted;
11863 right->duplicate = left_duplicate;
11864 right->needs_update = left_needs_update;
11865 right->target_info = left_target_info;
11866 }
11867
11868 /* Force the re-insertion of the locations at ADDRESS. This is called
11869 once a new/deleted/modified duplicate location is found and we are evaluating
11870 conditions on the target's side. Such conditions need to be updated on
11871 the target. */
11872
11873 static void
11874 force_breakpoint_reinsertion (struct bp_location *bl)
11875 {
11876 struct bp_location **locp = NULL, **loc2p;
11877 struct bp_location *loc;
11878 CORE_ADDR address = 0;
11879 int pspace_num;
11880
11881 address = bl->address;
11882 pspace_num = bl->pspace->num;
11883
11884 /* This is only meaningful if the target is
11885 evaluating conditions and if the user has
11886 opted for condition evaluation on the target's
11887 side. */
11888 if (gdb_evaluates_breakpoint_condition_p ()
11889 || !target_supports_evaluation_of_breakpoint_conditions ())
11890 return;
11891
11892 /* Flag all breakpoint locations with this address and
11893 the same program space as the location
11894 as "its condition has changed". We need to
11895 update the conditions on the target's side. */
11896 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11897 {
11898 loc = *loc2p;
11899
11900 if (!is_breakpoint (loc->owner)
11901 || pspace_num != loc->pspace->num)
11902 continue;
11903
11904 /* Flag the location appropriately. We use a different state to
11905 let everyone know that we already updated the set of locations
11906 with addr bl->address and program space bl->pspace. This is so
11907 we don't have to keep calling these functions just to mark locations
11908 that have already been marked. */
11909 loc->condition_changed = condition_updated;
11910
11911 /* Free the agent expression bytecode as well. We will compute
11912 it later on. */
11913 loc->cond_bytecode.reset ();
11914 }
11915 }
11916 /* Called whether new breakpoints are created, or existing breakpoints
11917 deleted, to update the global location list and recompute which
11918 locations are duplicate of which.
11919
11920 The INSERT_MODE flag determines whether locations may not, may, or
11921 shall be inserted now. See 'enum ugll_insert_mode' for more
11922 info. */
11923
11924 static void
11925 update_global_location_list (enum ugll_insert_mode insert_mode)
11926 {
11927 struct breakpoint *b;
11928 struct bp_location **locp, *loc;
11929 /* Last breakpoint location address that was marked for update. */
11930 CORE_ADDR last_addr = 0;
11931 /* Last breakpoint location program space that was marked for update. */
11932 int last_pspace_num = -1;
11933
11934 /* Used in the duplicates detection below. When iterating over all
11935 bp_locations, points to the first bp_location of a given address.
11936 Breakpoints and watchpoints of different types are never
11937 duplicates of each other. Keep one pointer for each type of
11938 breakpoint/watchpoint, so we only need to loop over all locations
11939 once. */
11940 struct bp_location *bp_loc_first; /* breakpoint */
11941 struct bp_location *wp_loc_first; /* hardware watchpoint */
11942 struct bp_location *awp_loc_first; /* access watchpoint */
11943 struct bp_location *rwp_loc_first; /* read watchpoint */
11944
11945 /* Saved former bp_locations array which we compare against the newly
11946 built bp_locations from the current state of ALL_BREAKPOINTS. */
11947 struct bp_location **old_locp;
11948 unsigned old_locations_count;
11949 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11950
11951 old_locations_count = bp_locations_count;
11952 bp_locations = NULL;
11953 bp_locations_count = 0;
11954
11955 ALL_BREAKPOINTS (b)
11956 for (loc = b->loc; loc; loc = loc->next)
11957 bp_locations_count++;
11958
11959 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11960 locp = bp_locations;
11961 ALL_BREAKPOINTS (b)
11962 for (loc = b->loc; loc; loc = loc->next)
11963 *locp++ = loc;
11964 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11965 bp_locations_compare);
11966
11967 bp_locations_target_extensions_update ();
11968
11969 /* Identify bp_location instances that are no longer present in the
11970 new list, and therefore should be freed. Note that it's not
11971 necessary that those locations should be removed from inferior --
11972 if there's another location at the same address (previously
11973 marked as duplicate), we don't need to remove/insert the
11974 location.
11975
11976 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11977 and former bp_location array state respectively. */
11978
11979 locp = bp_locations;
11980 for (old_locp = old_locations.get ();
11981 old_locp < old_locations.get () + old_locations_count;
11982 old_locp++)
11983 {
11984 struct bp_location *old_loc = *old_locp;
11985 struct bp_location **loc2p;
11986
11987 /* Tells if 'old_loc' is found among the new locations. If
11988 not, we have to free it. */
11989 int found_object = 0;
11990 /* Tells if the location should remain inserted in the target. */
11991 int keep_in_target = 0;
11992 int removed = 0;
11993
11994 /* Skip LOCP entries which will definitely never be needed.
11995 Stop either at or being the one matching OLD_LOC. */
11996 while (locp < bp_locations + bp_locations_count
11997 && (*locp)->address < old_loc->address)
11998 locp++;
11999
12000 for (loc2p = locp;
12001 (loc2p < bp_locations + bp_locations_count
12002 && (*loc2p)->address == old_loc->address);
12003 loc2p++)
12004 {
12005 /* Check if this is a new/duplicated location or a duplicated
12006 location that had its condition modified. If so, we want to send
12007 its condition to the target if evaluation of conditions is taking
12008 place there. */
12009 if ((*loc2p)->condition_changed == condition_modified
12010 && (last_addr != old_loc->address
12011 || last_pspace_num != old_loc->pspace->num))
12012 {
12013 force_breakpoint_reinsertion (*loc2p);
12014 last_pspace_num = old_loc->pspace->num;
12015 }
12016
12017 if (*loc2p == old_loc)
12018 found_object = 1;
12019 }
12020
12021 /* We have already handled this address, update it so that we don't
12022 have to go through updates again. */
12023 last_addr = old_loc->address;
12024
12025 /* Target-side condition evaluation: Handle deleted locations. */
12026 if (!found_object)
12027 force_breakpoint_reinsertion (old_loc);
12028
12029 /* If this location is no longer present, and inserted, look if
12030 there's maybe a new location at the same address. If so,
12031 mark that one inserted, and don't remove this one. This is
12032 needed so that we don't have a time window where a breakpoint
12033 at certain location is not inserted. */
12034
12035 if (old_loc->inserted)
12036 {
12037 /* If the location is inserted now, we might have to remove
12038 it. */
12039
12040 if (found_object && should_be_inserted (old_loc))
12041 {
12042 /* The location is still present in the location list,
12043 and still should be inserted. Don't do anything. */
12044 keep_in_target = 1;
12045 }
12046 else
12047 {
12048 /* This location still exists, but it won't be kept in the
12049 target since it may have been disabled. We proceed to
12050 remove its target-side condition. */
12051
12052 /* The location is either no longer present, or got
12053 disabled. See if there's another location at the
12054 same address, in which case we don't need to remove
12055 this one from the target. */
12056
12057 /* OLD_LOC comes from existing struct breakpoint. */
12058 if (breakpoint_address_is_meaningful (old_loc->owner))
12059 {
12060 for (loc2p = locp;
12061 (loc2p < bp_locations + bp_locations_count
12062 && (*loc2p)->address == old_loc->address);
12063 loc2p++)
12064 {
12065 struct bp_location *loc2 = *loc2p;
12066
12067 if (breakpoint_locations_match (loc2, old_loc))
12068 {
12069 /* Read watchpoint locations are switched to
12070 access watchpoints, if the former are not
12071 supported, but the latter are. */
12072 if (is_hardware_watchpoint (old_loc->owner))
12073 {
12074 gdb_assert (is_hardware_watchpoint (loc2->owner));
12075 loc2->watchpoint_type = old_loc->watchpoint_type;
12076 }
12077
12078 /* loc2 is a duplicated location. We need to check
12079 if it should be inserted in case it will be
12080 unduplicated. */
12081 if (loc2 != old_loc
12082 && unduplicated_should_be_inserted (loc2))
12083 {
12084 swap_insertion (old_loc, loc2);
12085 keep_in_target = 1;
12086 break;
12087 }
12088 }
12089 }
12090 }
12091 }
12092
12093 if (!keep_in_target)
12094 {
12095 if (remove_breakpoint (old_loc))
12096 {
12097 /* This is just about all we can do. We could keep
12098 this location on the global list, and try to
12099 remove it next time, but there's no particular
12100 reason why we will succeed next time.
12101
12102 Note that at this point, old_loc->owner is still
12103 valid, as delete_breakpoint frees the breakpoint
12104 only after calling us. */
12105 printf_filtered (_("warning: Error removing "
12106 "breakpoint %d\n"),
12107 old_loc->owner->number);
12108 }
12109 removed = 1;
12110 }
12111 }
12112
12113 if (!found_object)
12114 {
12115 if (removed && target_is_non_stop_p ()
12116 && need_moribund_for_location_type (old_loc))
12117 {
12118 /* This location was removed from the target. In
12119 non-stop mode, a race condition is possible where
12120 we've removed a breakpoint, but stop events for that
12121 breakpoint are already queued and will arrive later.
12122 We apply an heuristic to be able to distinguish such
12123 SIGTRAPs from other random SIGTRAPs: we keep this
12124 breakpoint location for a bit, and will retire it
12125 after we see some number of events. The theory here
12126 is that reporting of events should, "on the average",
12127 be fair, so after a while we'll see events from all
12128 threads that have anything of interest, and no longer
12129 need to keep this breakpoint location around. We
12130 don't hold locations forever so to reduce chances of
12131 mistaking a non-breakpoint SIGTRAP for a breakpoint
12132 SIGTRAP.
12133
12134 The heuristic failing can be disastrous on
12135 decr_pc_after_break targets.
12136
12137 On decr_pc_after_break targets, like e.g., x86-linux,
12138 if we fail to recognize a late breakpoint SIGTRAP,
12139 because events_till_retirement has reached 0 too
12140 soon, we'll fail to do the PC adjustment, and report
12141 a random SIGTRAP to the user. When the user resumes
12142 the inferior, it will most likely immediately crash
12143 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12144 corrupted, because of being resumed e.g., in the
12145 middle of a multi-byte instruction, or skipped a
12146 one-byte instruction. This was actually seen happen
12147 on native x86-linux, and should be less rare on
12148 targets that do not support new thread events, like
12149 remote, due to the heuristic depending on
12150 thread_count.
12151
12152 Mistaking a random SIGTRAP for a breakpoint trap
12153 causes similar symptoms (PC adjustment applied when
12154 it shouldn't), but then again, playing with SIGTRAPs
12155 behind the debugger's back is asking for trouble.
12156
12157 Since hardware watchpoint traps are always
12158 distinguishable from other traps, so we don't need to
12159 apply keep hardware watchpoint moribund locations
12160 around. We simply always ignore hardware watchpoint
12161 traps we can no longer explain. */
12162
12163 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12164 old_loc->owner = NULL;
12165
12166 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12167 }
12168 else
12169 {
12170 old_loc->owner = NULL;
12171 decref_bp_location (&old_loc);
12172 }
12173 }
12174 }
12175
12176 /* Rescan breakpoints at the same address and section, marking the
12177 first one as "first" and any others as "duplicates". This is so
12178 that the bpt instruction is only inserted once. If we have a
12179 permanent breakpoint at the same place as BPT, make that one the
12180 official one, and the rest as duplicates. Permanent breakpoints
12181 are sorted first for the same address.
12182
12183 Do the same for hardware watchpoints, but also considering the
12184 watchpoint's type (regular/access/read) and length. */
12185
12186 bp_loc_first = NULL;
12187 wp_loc_first = NULL;
12188 awp_loc_first = NULL;
12189 rwp_loc_first = NULL;
12190 ALL_BP_LOCATIONS (loc, locp)
12191 {
12192 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12193 non-NULL. */
12194 struct bp_location **loc_first_p;
12195 b = loc->owner;
12196
12197 if (!unduplicated_should_be_inserted (loc)
12198 || !breakpoint_address_is_meaningful (b)
12199 /* Don't detect duplicate for tracepoint locations because they are
12200 never duplicated. See the comments in field `duplicate' of
12201 `struct bp_location'. */
12202 || is_tracepoint (b))
12203 {
12204 /* Clear the condition modification flag. */
12205 loc->condition_changed = condition_unchanged;
12206 continue;
12207 }
12208
12209 if (b->type == bp_hardware_watchpoint)
12210 loc_first_p = &wp_loc_first;
12211 else if (b->type == bp_read_watchpoint)
12212 loc_first_p = &rwp_loc_first;
12213 else if (b->type == bp_access_watchpoint)
12214 loc_first_p = &awp_loc_first;
12215 else
12216 loc_first_p = &bp_loc_first;
12217
12218 if (*loc_first_p == NULL
12219 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12220 || !breakpoint_locations_match (loc, *loc_first_p))
12221 {
12222 *loc_first_p = loc;
12223 loc->duplicate = 0;
12224
12225 if (is_breakpoint (loc->owner) && loc->condition_changed)
12226 {
12227 loc->needs_update = 1;
12228 /* Clear the condition modification flag. */
12229 loc->condition_changed = condition_unchanged;
12230 }
12231 continue;
12232 }
12233
12234
12235 /* This and the above ensure the invariant that the first location
12236 is not duplicated, and is the inserted one.
12237 All following are marked as duplicated, and are not inserted. */
12238 if (loc->inserted)
12239 swap_insertion (loc, *loc_first_p);
12240 loc->duplicate = 1;
12241
12242 /* Clear the condition modification flag. */
12243 loc->condition_changed = condition_unchanged;
12244 }
12245
12246 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12247 {
12248 if (insert_mode != UGLL_DONT_INSERT)
12249 insert_breakpoint_locations ();
12250 else
12251 {
12252 /* Even though the caller told us to not insert new
12253 locations, we may still need to update conditions on the
12254 target's side of breakpoints that were already inserted
12255 if the target is evaluating breakpoint conditions. We
12256 only update conditions for locations that are marked
12257 "needs_update". */
12258 update_inserted_breakpoint_locations ();
12259 }
12260 }
12261
12262 if (insert_mode != UGLL_DONT_INSERT)
12263 download_tracepoint_locations ();
12264 }
12265
12266 void
12267 breakpoint_retire_moribund (void)
12268 {
12269 struct bp_location *loc;
12270 int ix;
12271
12272 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12273 if (--(loc->events_till_retirement) == 0)
12274 {
12275 decref_bp_location (&loc);
12276 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12277 --ix;
12278 }
12279 }
12280
12281 static void
12282 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12283 {
12284
12285 TRY
12286 {
12287 update_global_location_list (insert_mode);
12288 }
12289 CATCH (e, RETURN_MASK_ERROR)
12290 {
12291 }
12292 END_CATCH
12293 }
12294
12295 /* Clear BKP from a BPS. */
12296
12297 static void
12298 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12299 {
12300 bpstat bs;
12301
12302 for (bs = bps; bs; bs = bs->next)
12303 if (bs->breakpoint_at == bpt)
12304 {
12305 bs->breakpoint_at = NULL;
12306 bs->old_val = NULL;
12307 /* bs->commands will be freed later. */
12308 }
12309 }
12310
12311 /* Callback for iterate_over_threads. */
12312 static int
12313 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12314 {
12315 struct breakpoint *bpt = (struct breakpoint *) data;
12316
12317 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12318 return 0;
12319 }
12320
12321 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12322 callbacks. */
12323
12324 static void
12325 say_where (struct breakpoint *b)
12326 {
12327 struct value_print_options opts;
12328
12329 get_user_print_options (&opts);
12330
12331 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12332 single string. */
12333 if (b->loc == NULL)
12334 {
12335 /* For pending locations, the output differs slightly based
12336 on b->extra_string. If this is non-NULL, it contains either
12337 a condition or dprintf arguments. */
12338 if (b->extra_string == NULL)
12339 {
12340 printf_filtered (_(" (%s) pending."),
12341 event_location_to_string (b->location.get ()));
12342 }
12343 else if (b->type == bp_dprintf)
12344 {
12345 printf_filtered (_(" (%s,%s) pending."),
12346 event_location_to_string (b->location.get ()),
12347 b->extra_string);
12348 }
12349 else
12350 {
12351 printf_filtered (_(" (%s %s) pending."),
12352 event_location_to_string (b->location.get ()),
12353 b->extra_string);
12354 }
12355 }
12356 else
12357 {
12358 if (opts.addressprint || b->loc->symtab == NULL)
12359 {
12360 printf_filtered (" at ");
12361 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12362 gdb_stdout);
12363 }
12364 if (b->loc->symtab != NULL)
12365 {
12366 /* If there is a single location, we can print the location
12367 more nicely. */
12368 if (b->loc->next == NULL)
12369 printf_filtered (": file %s, line %d.",
12370 symtab_to_filename_for_display (b->loc->symtab),
12371 b->loc->line_number);
12372 else
12373 /* This is not ideal, but each location may have a
12374 different file name, and this at least reflects the
12375 real situation somewhat. */
12376 printf_filtered (": %s.",
12377 event_location_to_string (b->location.get ()));
12378 }
12379
12380 if (b->loc->next)
12381 {
12382 struct bp_location *loc = b->loc;
12383 int n = 0;
12384 for (; loc; loc = loc->next)
12385 ++n;
12386 printf_filtered (" (%d locations)", n);
12387 }
12388 }
12389 }
12390
12391 /* Default bp_location_ops methods. */
12392
12393 static void
12394 bp_location_dtor (struct bp_location *self)
12395 {
12396 xfree (self->function_name);
12397 }
12398
12399 static const struct bp_location_ops bp_location_ops =
12400 {
12401 bp_location_dtor
12402 };
12403
12404 /* Destructor for the breakpoint base class. */
12405
12406 breakpoint::~breakpoint ()
12407 {
12408 xfree (this->cond_string);
12409 xfree (this->extra_string);
12410 xfree (this->filter);
12411 }
12412
12413 static struct bp_location *
12414 base_breakpoint_allocate_location (struct breakpoint *self)
12415 {
12416 return new bp_location (&bp_location_ops, self);
12417 }
12418
12419 static void
12420 base_breakpoint_re_set (struct breakpoint *b)
12421 {
12422 /* Nothing to re-set. */
12423 }
12424
12425 #define internal_error_pure_virtual_called() \
12426 gdb_assert_not_reached ("pure virtual function called")
12427
12428 static int
12429 base_breakpoint_insert_location (struct bp_location *bl)
12430 {
12431 internal_error_pure_virtual_called ();
12432 }
12433
12434 static int
12435 base_breakpoint_remove_location (struct bp_location *bl,
12436 enum remove_bp_reason reason)
12437 {
12438 internal_error_pure_virtual_called ();
12439 }
12440
12441 static int
12442 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12443 struct address_space *aspace,
12444 CORE_ADDR bp_addr,
12445 const struct target_waitstatus *ws)
12446 {
12447 internal_error_pure_virtual_called ();
12448 }
12449
12450 static void
12451 base_breakpoint_check_status (bpstat bs)
12452 {
12453 /* Always stop. */
12454 }
12455
12456 /* A "works_in_software_mode" breakpoint_ops method that just internal
12457 errors. */
12458
12459 static int
12460 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12461 {
12462 internal_error_pure_virtual_called ();
12463 }
12464
12465 /* A "resources_needed" breakpoint_ops method that just internal
12466 errors. */
12467
12468 static int
12469 base_breakpoint_resources_needed (const struct bp_location *bl)
12470 {
12471 internal_error_pure_virtual_called ();
12472 }
12473
12474 static enum print_stop_action
12475 base_breakpoint_print_it (bpstat bs)
12476 {
12477 internal_error_pure_virtual_called ();
12478 }
12479
12480 static void
12481 base_breakpoint_print_one_detail (const struct breakpoint *self,
12482 struct ui_out *uiout)
12483 {
12484 /* nothing */
12485 }
12486
12487 static void
12488 base_breakpoint_print_mention (struct breakpoint *b)
12489 {
12490 internal_error_pure_virtual_called ();
12491 }
12492
12493 static void
12494 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12495 {
12496 internal_error_pure_virtual_called ();
12497 }
12498
12499 static void
12500 base_breakpoint_create_sals_from_location
12501 (const struct event_location *location,
12502 struct linespec_result *canonical,
12503 enum bptype type_wanted)
12504 {
12505 internal_error_pure_virtual_called ();
12506 }
12507
12508 static void
12509 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12510 struct linespec_result *c,
12511 gdb::unique_xmalloc_ptr<char> cond_string,
12512 gdb::unique_xmalloc_ptr<char> extra_string,
12513 enum bptype type_wanted,
12514 enum bpdisp disposition,
12515 int thread,
12516 int task, int ignore_count,
12517 const struct breakpoint_ops *o,
12518 int from_tty, int enabled,
12519 int internal, unsigned flags)
12520 {
12521 internal_error_pure_virtual_called ();
12522 }
12523
12524 static std::vector<symtab_and_line>
12525 base_breakpoint_decode_location (struct breakpoint *b,
12526 const struct event_location *location,
12527 struct program_space *search_pspace)
12528 {
12529 internal_error_pure_virtual_called ();
12530 }
12531
12532 /* The default 'explains_signal' method. */
12533
12534 static int
12535 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12536 {
12537 return 1;
12538 }
12539
12540 /* The default "after_condition_true" method. */
12541
12542 static void
12543 base_breakpoint_after_condition_true (struct bpstats *bs)
12544 {
12545 /* Nothing to do. */
12546 }
12547
12548 struct breakpoint_ops base_breakpoint_ops =
12549 {
12550 base_breakpoint_allocate_location,
12551 base_breakpoint_re_set,
12552 base_breakpoint_insert_location,
12553 base_breakpoint_remove_location,
12554 base_breakpoint_breakpoint_hit,
12555 base_breakpoint_check_status,
12556 base_breakpoint_resources_needed,
12557 base_breakpoint_works_in_software_mode,
12558 base_breakpoint_print_it,
12559 NULL,
12560 base_breakpoint_print_one_detail,
12561 base_breakpoint_print_mention,
12562 base_breakpoint_print_recreate,
12563 base_breakpoint_create_sals_from_location,
12564 base_breakpoint_create_breakpoints_sal,
12565 base_breakpoint_decode_location,
12566 base_breakpoint_explains_signal,
12567 base_breakpoint_after_condition_true,
12568 };
12569
12570 /* Default breakpoint_ops methods. */
12571
12572 static void
12573 bkpt_re_set (struct breakpoint *b)
12574 {
12575 /* FIXME: is this still reachable? */
12576 if (breakpoint_event_location_empty_p (b))
12577 {
12578 /* Anything without a location can't be re-set. */
12579 delete_breakpoint (b);
12580 return;
12581 }
12582
12583 breakpoint_re_set_default (b);
12584 }
12585
12586 static int
12587 bkpt_insert_location (struct bp_location *bl)
12588 {
12589 CORE_ADDR addr = bl->target_info.reqstd_address;
12590
12591 bl->target_info.kind = breakpoint_kind (bl, &addr);
12592 bl->target_info.placed_address = addr;
12593
12594 if (bl->loc_type == bp_loc_hardware_breakpoint)
12595 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12596 else
12597 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12598 }
12599
12600 static int
12601 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12602 {
12603 if (bl->loc_type == bp_loc_hardware_breakpoint)
12604 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12605 else
12606 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12607 }
12608
12609 static int
12610 bkpt_breakpoint_hit (const struct bp_location *bl,
12611 struct address_space *aspace, CORE_ADDR bp_addr,
12612 const struct target_waitstatus *ws)
12613 {
12614 if (ws->kind != TARGET_WAITKIND_STOPPED
12615 || ws->value.sig != GDB_SIGNAL_TRAP)
12616 return 0;
12617
12618 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12619 aspace, bp_addr))
12620 return 0;
12621
12622 if (overlay_debugging /* unmapped overlay section */
12623 && section_is_overlay (bl->section)
12624 && !section_is_mapped (bl->section))
12625 return 0;
12626
12627 return 1;
12628 }
12629
12630 static int
12631 dprintf_breakpoint_hit (const struct bp_location *bl,
12632 struct address_space *aspace, CORE_ADDR bp_addr,
12633 const struct target_waitstatus *ws)
12634 {
12635 if (dprintf_style == dprintf_style_agent
12636 && target_can_run_breakpoint_commands ())
12637 {
12638 /* An agent-style dprintf never causes a stop. If we see a trap
12639 for this address it must be for a breakpoint that happens to
12640 be set at the same address. */
12641 return 0;
12642 }
12643
12644 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12645 }
12646
12647 static int
12648 bkpt_resources_needed (const struct bp_location *bl)
12649 {
12650 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12651
12652 return 1;
12653 }
12654
12655 static enum print_stop_action
12656 bkpt_print_it (bpstat bs)
12657 {
12658 struct breakpoint *b;
12659 const struct bp_location *bl;
12660 int bp_temp;
12661 struct ui_out *uiout = current_uiout;
12662
12663 gdb_assert (bs->bp_location_at != NULL);
12664
12665 bl = bs->bp_location_at;
12666 b = bs->breakpoint_at;
12667
12668 bp_temp = b->disposition == disp_del;
12669 if (bl->address != bl->requested_address)
12670 breakpoint_adjustment_warning (bl->requested_address,
12671 bl->address,
12672 b->number, 1);
12673 annotate_breakpoint (b->number);
12674 maybe_print_thread_hit_breakpoint (uiout);
12675
12676 if (bp_temp)
12677 uiout->text ("Temporary breakpoint ");
12678 else
12679 uiout->text ("Breakpoint ");
12680 if (uiout->is_mi_like_p ())
12681 {
12682 uiout->field_string ("reason",
12683 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12684 uiout->field_string ("disp", bpdisp_text (b->disposition));
12685 }
12686 uiout->field_int ("bkptno", b->number);
12687 uiout->text (", ");
12688
12689 return PRINT_SRC_AND_LOC;
12690 }
12691
12692 static void
12693 bkpt_print_mention (struct breakpoint *b)
12694 {
12695 if (current_uiout->is_mi_like_p ())
12696 return;
12697
12698 switch (b->type)
12699 {
12700 case bp_breakpoint:
12701 case bp_gnu_ifunc_resolver:
12702 if (b->disposition == disp_del)
12703 printf_filtered (_("Temporary breakpoint"));
12704 else
12705 printf_filtered (_("Breakpoint"));
12706 printf_filtered (_(" %d"), b->number);
12707 if (b->type == bp_gnu_ifunc_resolver)
12708 printf_filtered (_(" at gnu-indirect-function resolver"));
12709 break;
12710 case bp_hardware_breakpoint:
12711 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12712 break;
12713 case bp_dprintf:
12714 printf_filtered (_("Dprintf %d"), b->number);
12715 break;
12716 }
12717
12718 say_where (b);
12719 }
12720
12721 static void
12722 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12723 {
12724 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12725 fprintf_unfiltered (fp, "tbreak");
12726 else if (tp->type == bp_breakpoint)
12727 fprintf_unfiltered (fp, "break");
12728 else if (tp->type == bp_hardware_breakpoint
12729 && tp->disposition == disp_del)
12730 fprintf_unfiltered (fp, "thbreak");
12731 else if (tp->type == bp_hardware_breakpoint)
12732 fprintf_unfiltered (fp, "hbreak");
12733 else
12734 internal_error (__FILE__, __LINE__,
12735 _("unhandled breakpoint type %d"), (int) tp->type);
12736
12737 fprintf_unfiltered (fp, " %s",
12738 event_location_to_string (tp->location.get ()));
12739
12740 /* Print out extra_string if this breakpoint is pending. It might
12741 contain, for example, conditions that were set by the user. */
12742 if (tp->loc == NULL && tp->extra_string != NULL)
12743 fprintf_unfiltered (fp, " %s", tp->extra_string);
12744
12745 print_recreate_thread (tp, fp);
12746 }
12747
12748 static void
12749 bkpt_create_sals_from_location (const struct event_location *location,
12750 struct linespec_result *canonical,
12751 enum bptype type_wanted)
12752 {
12753 create_sals_from_location_default (location, canonical, type_wanted);
12754 }
12755
12756 static void
12757 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12758 struct linespec_result *canonical,
12759 gdb::unique_xmalloc_ptr<char> cond_string,
12760 gdb::unique_xmalloc_ptr<char> extra_string,
12761 enum bptype type_wanted,
12762 enum bpdisp disposition,
12763 int thread,
12764 int task, int ignore_count,
12765 const struct breakpoint_ops *ops,
12766 int from_tty, int enabled,
12767 int internal, unsigned flags)
12768 {
12769 create_breakpoints_sal_default (gdbarch, canonical,
12770 std::move (cond_string),
12771 std::move (extra_string),
12772 type_wanted,
12773 disposition, thread, task,
12774 ignore_count, ops, from_tty,
12775 enabled, internal, flags);
12776 }
12777
12778 static std::vector<symtab_and_line>
12779 bkpt_decode_location (struct breakpoint *b,
12780 const struct event_location *location,
12781 struct program_space *search_pspace)
12782 {
12783 return decode_location_default (b, location, search_pspace);
12784 }
12785
12786 /* Virtual table for internal breakpoints. */
12787
12788 static void
12789 internal_bkpt_re_set (struct breakpoint *b)
12790 {
12791 switch (b->type)
12792 {
12793 /* Delete overlay event and longjmp master breakpoints; they
12794 will be reset later by breakpoint_re_set. */
12795 case bp_overlay_event:
12796 case bp_longjmp_master:
12797 case bp_std_terminate_master:
12798 case bp_exception_master:
12799 delete_breakpoint (b);
12800 break;
12801
12802 /* This breakpoint is special, it's set up when the inferior
12803 starts and we really don't want to touch it. */
12804 case bp_shlib_event:
12805
12806 /* Like bp_shlib_event, this breakpoint type is special. Once
12807 it is set up, we do not want to touch it. */
12808 case bp_thread_event:
12809 break;
12810 }
12811 }
12812
12813 static void
12814 internal_bkpt_check_status (bpstat bs)
12815 {
12816 if (bs->breakpoint_at->type == bp_shlib_event)
12817 {
12818 /* If requested, stop when the dynamic linker notifies GDB of
12819 events. This allows the user to get control and place
12820 breakpoints in initializer routines for dynamically loaded
12821 objects (among other things). */
12822 bs->stop = stop_on_solib_events;
12823 bs->print = stop_on_solib_events;
12824 }
12825 else
12826 bs->stop = 0;
12827 }
12828
12829 static enum print_stop_action
12830 internal_bkpt_print_it (bpstat bs)
12831 {
12832 struct breakpoint *b;
12833
12834 b = bs->breakpoint_at;
12835
12836 switch (b->type)
12837 {
12838 case bp_shlib_event:
12839 /* Did we stop because the user set the stop_on_solib_events
12840 variable? (If so, we report this as a generic, "Stopped due
12841 to shlib event" message.) */
12842 print_solib_event (0);
12843 break;
12844
12845 case bp_thread_event:
12846 /* Not sure how we will get here.
12847 GDB should not stop for these breakpoints. */
12848 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12849 break;
12850
12851 case bp_overlay_event:
12852 /* By analogy with the thread event, GDB should not stop for these. */
12853 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12854 break;
12855
12856 case bp_longjmp_master:
12857 /* These should never be enabled. */
12858 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12859 break;
12860
12861 case bp_std_terminate_master:
12862 /* These should never be enabled. */
12863 printf_filtered (_("std::terminate Master Breakpoint: "
12864 "gdb should not stop!\n"));
12865 break;
12866
12867 case bp_exception_master:
12868 /* These should never be enabled. */
12869 printf_filtered (_("Exception Master Breakpoint: "
12870 "gdb should not stop!\n"));
12871 break;
12872 }
12873
12874 return PRINT_NOTHING;
12875 }
12876
12877 static void
12878 internal_bkpt_print_mention (struct breakpoint *b)
12879 {
12880 /* Nothing to mention. These breakpoints are internal. */
12881 }
12882
12883 /* Virtual table for momentary breakpoints */
12884
12885 static void
12886 momentary_bkpt_re_set (struct breakpoint *b)
12887 {
12888 /* Keep temporary breakpoints, which can be encountered when we step
12889 over a dlopen call and solib_add is resetting the breakpoints.
12890 Otherwise these should have been blown away via the cleanup chain
12891 or by breakpoint_init_inferior when we rerun the executable. */
12892 }
12893
12894 static void
12895 momentary_bkpt_check_status (bpstat bs)
12896 {
12897 /* Nothing. The point of these breakpoints is causing a stop. */
12898 }
12899
12900 static enum print_stop_action
12901 momentary_bkpt_print_it (bpstat bs)
12902 {
12903 return PRINT_UNKNOWN;
12904 }
12905
12906 static void
12907 momentary_bkpt_print_mention (struct breakpoint *b)
12908 {
12909 /* Nothing to mention. These breakpoints are internal. */
12910 }
12911
12912 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12913
12914 It gets cleared already on the removal of the first one of such placed
12915 breakpoints. This is OK as they get all removed altogether. */
12916
12917 longjmp_breakpoint::~longjmp_breakpoint ()
12918 {
12919 thread_info *tp = find_thread_global_id (this->thread);
12920
12921 if (tp != NULL)
12922 tp->initiating_frame = null_frame_id;
12923 }
12924
12925 /* Specific methods for probe breakpoints. */
12926
12927 static int
12928 bkpt_probe_insert_location (struct bp_location *bl)
12929 {
12930 int v = bkpt_insert_location (bl);
12931
12932 if (v == 0)
12933 {
12934 /* The insertion was successful, now let's set the probe's semaphore
12935 if needed. */
12936 if (bl->probe.probe->pops->set_semaphore != NULL)
12937 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
12938 bl->probe.objfile,
12939 bl->gdbarch);
12940 }
12941
12942 return v;
12943 }
12944
12945 static int
12946 bkpt_probe_remove_location (struct bp_location *bl,
12947 enum remove_bp_reason reason)
12948 {
12949 /* Let's clear the semaphore before removing the location. */
12950 if (bl->probe.probe->pops->clear_semaphore != NULL)
12951 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
12952 bl->probe.objfile,
12953 bl->gdbarch);
12954
12955 return bkpt_remove_location (bl, reason);
12956 }
12957
12958 static void
12959 bkpt_probe_create_sals_from_location (const struct event_location *location,
12960 struct linespec_result *canonical,
12961 enum bptype type_wanted)
12962 {
12963 struct linespec_sals lsal;
12964
12965 lsal.sals = parse_probes (location, NULL, canonical);
12966 lsal.canonical
12967 = xstrdup (event_location_to_string (canonical->location.get ()));
12968 canonical->lsals.push_back (std::move (lsal));
12969 }
12970
12971 static std::vector<symtab_and_line>
12972 bkpt_probe_decode_location (struct breakpoint *b,
12973 const struct event_location *location,
12974 struct program_space *search_pspace)
12975 {
12976 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12977 if (sals.empty ())
12978 error (_("probe not found"));
12979 return sals;
12980 }
12981
12982 /* The breakpoint_ops structure to be used in tracepoints. */
12983
12984 static void
12985 tracepoint_re_set (struct breakpoint *b)
12986 {
12987 breakpoint_re_set_default (b);
12988 }
12989
12990 static int
12991 tracepoint_breakpoint_hit (const struct bp_location *bl,
12992 struct address_space *aspace, CORE_ADDR bp_addr,
12993 const struct target_waitstatus *ws)
12994 {
12995 /* By definition, the inferior does not report stops at
12996 tracepoints. */
12997 return 0;
12998 }
12999
13000 static void
13001 tracepoint_print_one_detail (const struct breakpoint *self,
13002 struct ui_out *uiout)
13003 {
13004 struct tracepoint *tp = (struct tracepoint *) self;
13005 if (tp->static_trace_marker_id)
13006 {
13007 gdb_assert (self->type == bp_static_tracepoint);
13008
13009 uiout->text ("\tmarker id is ");
13010 uiout->field_string ("static-tracepoint-marker-string-id",
13011 tp->static_trace_marker_id);
13012 uiout->text ("\n");
13013 }
13014 }
13015
13016 static void
13017 tracepoint_print_mention (struct breakpoint *b)
13018 {
13019 if (current_uiout->is_mi_like_p ())
13020 return;
13021
13022 switch (b->type)
13023 {
13024 case bp_tracepoint:
13025 printf_filtered (_("Tracepoint"));
13026 printf_filtered (_(" %d"), b->number);
13027 break;
13028 case bp_fast_tracepoint:
13029 printf_filtered (_("Fast tracepoint"));
13030 printf_filtered (_(" %d"), b->number);
13031 break;
13032 case bp_static_tracepoint:
13033 printf_filtered (_("Static tracepoint"));
13034 printf_filtered (_(" %d"), b->number);
13035 break;
13036 default:
13037 internal_error (__FILE__, __LINE__,
13038 _("unhandled tracepoint type %d"), (int) b->type);
13039 }
13040
13041 say_where (b);
13042 }
13043
13044 static void
13045 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13046 {
13047 struct tracepoint *tp = (struct tracepoint *) self;
13048
13049 if (self->type == bp_fast_tracepoint)
13050 fprintf_unfiltered (fp, "ftrace");
13051 else if (self->type == bp_static_tracepoint)
13052 fprintf_unfiltered (fp, "strace");
13053 else if (self->type == bp_tracepoint)
13054 fprintf_unfiltered (fp, "trace");
13055 else
13056 internal_error (__FILE__, __LINE__,
13057 _("unhandled tracepoint type %d"), (int) self->type);
13058
13059 fprintf_unfiltered (fp, " %s",
13060 event_location_to_string (self->location.get ()));
13061 print_recreate_thread (self, fp);
13062
13063 if (tp->pass_count)
13064 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13065 }
13066
13067 static void
13068 tracepoint_create_sals_from_location (const struct event_location *location,
13069 struct linespec_result *canonical,
13070 enum bptype type_wanted)
13071 {
13072 create_sals_from_location_default (location, canonical, type_wanted);
13073 }
13074
13075 static void
13076 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13077 struct linespec_result *canonical,
13078 gdb::unique_xmalloc_ptr<char> cond_string,
13079 gdb::unique_xmalloc_ptr<char> extra_string,
13080 enum bptype type_wanted,
13081 enum bpdisp disposition,
13082 int thread,
13083 int task, int ignore_count,
13084 const struct breakpoint_ops *ops,
13085 int from_tty, int enabled,
13086 int internal, unsigned flags)
13087 {
13088 create_breakpoints_sal_default (gdbarch, canonical,
13089 std::move (cond_string),
13090 std::move (extra_string),
13091 type_wanted,
13092 disposition, thread, task,
13093 ignore_count, ops, from_tty,
13094 enabled, internal, flags);
13095 }
13096
13097 static std::vector<symtab_and_line>
13098 tracepoint_decode_location (struct breakpoint *b,
13099 const struct event_location *location,
13100 struct program_space *search_pspace)
13101 {
13102 return decode_location_default (b, location, search_pspace);
13103 }
13104
13105 struct breakpoint_ops tracepoint_breakpoint_ops;
13106
13107 /* The breakpoint_ops structure to be use on tracepoints placed in a
13108 static probe. */
13109
13110 static void
13111 tracepoint_probe_create_sals_from_location
13112 (const struct event_location *location,
13113 struct linespec_result *canonical,
13114 enum bptype type_wanted)
13115 {
13116 /* We use the same method for breakpoint on probes. */
13117 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13118 }
13119
13120 static std::vector<symtab_and_line>
13121 tracepoint_probe_decode_location (struct breakpoint *b,
13122 const struct event_location *location,
13123 struct program_space *search_pspace)
13124 {
13125 /* We use the same method for breakpoint on probes. */
13126 return bkpt_probe_decode_location (b, location, search_pspace);
13127 }
13128
13129 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13130
13131 /* Dprintf breakpoint_ops methods. */
13132
13133 static void
13134 dprintf_re_set (struct breakpoint *b)
13135 {
13136 breakpoint_re_set_default (b);
13137
13138 /* extra_string should never be non-NULL for dprintf. */
13139 gdb_assert (b->extra_string != NULL);
13140
13141 /* 1 - connect to target 1, that can run breakpoint commands.
13142 2 - create a dprintf, which resolves fine.
13143 3 - disconnect from target 1
13144 4 - connect to target 2, that can NOT run breakpoint commands.
13145
13146 After steps #3/#4, you'll want the dprintf command list to
13147 be updated, because target 1 and 2 may well return different
13148 answers for target_can_run_breakpoint_commands().
13149 Given absence of finer grained resetting, we get to do
13150 it all the time. */
13151 if (b->extra_string != NULL)
13152 update_dprintf_command_list (b);
13153 }
13154
13155 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13156
13157 static void
13158 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13159 {
13160 fprintf_unfiltered (fp, "dprintf %s,%s",
13161 event_location_to_string (tp->location.get ()),
13162 tp->extra_string);
13163 print_recreate_thread (tp, fp);
13164 }
13165
13166 /* Implement the "after_condition_true" breakpoint_ops method for
13167 dprintf.
13168
13169 dprintf's are implemented with regular commands in their command
13170 list, but we run the commands here instead of before presenting the
13171 stop to the user, as dprintf's don't actually cause a stop. This
13172 also makes it so that the commands of multiple dprintfs at the same
13173 address are all handled. */
13174
13175 static void
13176 dprintf_after_condition_true (struct bpstats *bs)
13177 {
13178 struct bpstats tmp_bs;
13179 struct bpstats *tmp_bs_p = &tmp_bs;
13180
13181 /* dprintf's never cause a stop. This wasn't set in the
13182 check_status hook instead because that would make the dprintf's
13183 condition not be evaluated. */
13184 bs->stop = 0;
13185
13186 /* Run the command list here. Take ownership of it instead of
13187 copying. We never want these commands to run later in
13188 bpstat_do_actions, if a breakpoint that causes a stop happens to
13189 be set at same address as this dprintf, or even if running the
13190 commands here throws. */
13191 tmp_bs.commands = bs->commands;
13192 bs->commands = NULL;
13193
13194 bpstat_do_actions_1 (&tmp_bs_p);
13195
13196 /* 'tmp_bs.commands' will usually be NULL by now, but
13197 bpstat_do_actions_1 may return early without processing the whole
13198 list. */
13199 }
13200
13201 /* The breakpoint_ops structure to be used on static tracepoints with
13202 markers (`-m'). */
13203
13204 static void
13205 strace_marker_create_sals_from_location (const struct event_location *location,
13206 struct linespec_result *canonical,
13207 enum bptype type_wanted)
13208 {
13209 struct linespec_sals lsal;
13210 const char *arg_start, *arg;
13211
13212 arg = arg_start = get_linespec_location (location);
13213 lsal.sals = decode_static_tracepoint_spec (&arg);
13214
13215 std::string str (arg_start, arg - arg_start);
13216 const char *ptr = str.c_str ();
13217 canonical->location = new_linespec_location (&ptr);
13218
13219 lsal.canonical
13220 = xstrdup (event_location_to_string (canonical->location.get ()));
13221 canonical->lsals.push_back (std::move (lsal));
13222 }
13223
13224 static void
13225 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13226 struct linespec_result *canonical,
13227 gdb::unique_xmalloc_ptr<char> cond_string,
13228 gdb::unique_xmalloc_ptr<char> extra_string,
13229 enum bptype type_wanted,
13230 enum bpdisp disposition,
13231 int thread,
13232 int task, int ignore_count,
13233 const struct breakpoint_ops *ops,
13234 int from_tty, int enabled,
13235 int internal, unsigned flags)
13236 {
13237 const linespec_sals &lsal = canonical->lsals[0];
13238
13239 /* If the user is creating a static tracepoint by marker id
13240 (strace -m MARKER_ID), then store the sals index, so that
13241 breakpoint_re_set can try to match up which of the newly
13242 found markers corresponds to this one, and, don't try to
13243 expand multiple locations for each sal, given than SALS
13244 already should contain all sals for MARKER_ID. */
13245
13246 for (size_t i = 0; i < lsal.sals.size (); i++)
13247 {
13248 event_location_up location
13249 = copy_event_location (canonical->location.get ());
13250
13251 std::unique_ptr<tracepoint> tp (new tracepoint ());
13252 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13253 std::move (location), NULL,
13254 std::move (cond_string),
13255 std::move (extra_string),
13256 type_wanted, disposition,
13257 thread, task, ignore_count, ops,
13258 from_tty, enabled, internal, flags,
13259 canonical->special_display);
13260 /* Given that its possible to have multiple markers with
13261 the same string id, if the user is creating a static
13262 tracepoint by marker id ("strace -m MARKER_ID"), then
13263 store the sals index, so that breakpoint_re_set can
13264 try to match up which of the newly found markers
13265 corresponds to this one */
13266 tp->static_trace_marker_id_idx = i;
13267
13268 install_breakpoint (internal, std::move (tp), 0);
13269 }
13270 }
13271
13272 static std::vector<symtab_and_line>
13273 strace_marker_decode_location (struct breakpoint *b,
13274 const struct event_location *location,
13275 struct program_space *search_pspace)
13276 {
13277 struct tracepoint *tp = (struct tracepoint *) b;
13278 const char *s = get_linespec_location (location);
13279
13280 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13281 if (sals.size () > tp->static_trace_marker_id_idx)
13282 {
13283 sals[0] = sals[tp->static_trace_marker_id_idx];
13284 sals.resize (1);
13285 return sals;
13286 }
13287 else
13288 error (_("marker %s not found"), tp->static_trace_marker_id);
13289 }
13290
13291 static struct breakpoint_ops strace_marker_breakpoint_ops;
13292
13293 static int
13294 strace_marker_p (struct breakpoint *b)
13295 {
13296 return b->ops == &strace_marker_breakpoint_ops;
13297 }
13298
13299 /* Delete a breakpoint and clean up all traces of it in the data
13300 structures. */
13301
13302 void
13303 delete_breakpoint (struct breakpoint *bpt)
13304 {
13305 struct breakpoint *b;
13306
13307 gdb_assert (bpt != NULL);
13308
13309 /* Has this bp already been deleted? This can happen because
13310 multiple lists can hold pointers to bp's. bpstat lists are
13311 especial culprits.
13312
13313 One example of this happening is a watchpoint's scope bp. When
13314 the scope bp triggers, we notice that the watchpoint is out of
13315 scope, and delete it. We also delete its scope bp. But the
13316 scope bp is marked "auto-deleting", and is already on a bpstat.
13317 That bpstat is then checked for auto-deleting bp's, which are
13318 deleted.
13319
13320 A real solution to this problem might involve reference counts in
13321 bp's, and/or giving them pointers back to their referencing
13322 bpstat's, and teaching delete_breakpoint to only free a bp's
13323 storage when no more references were extent. A cheaper bandaid
13324 was chosen. */
13325 if (bpt->type == bp_none)
13326 return;
13327
13328 /* At least avoid this stale reference until the reference counting
13329 of breakpoints gets resolved. */
13330 if (bpt->related_breakpoint != bpt)
13331 {
13332 struct breakpoint *related;
13333 struct watchpoint *w;
13334
13335 if (bpt->type == bp_watchpoint_scope)
13336 w = (struct watchpoint *) bpt->related_breakpoint;
13337 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13338 w = (struct watchpoint *) bpt;
13339 else
13340 w = NULL;
13341 if (w != NULL)
13342 watchpoint_del_at_next_stop (w);
13343
13344 /* Unlink bpt from the bpt->related_breakpoint ring. */
13345 for (related = bpt; related->related_breakpoint != bpt;
13346 related = related->related_breakpoint);
13347 related->related_breakpoint = bpt->related_breakpoint;
13348 bpt->related_breakpoint = bpt;
13349 }
13350
13351 /* watch_command_1 creates a watchpoint but only sets its number if
13352 update_watchpoint succeeds in creating its bp_locations. If there's
13353 a problem in that process, we'll be asked to delete the half-created
13354 watchpoint. In that case, don't announce the deletion. */
13355 if (bpt->number)
13356 observer_notify_breakpoint_deleted (bpt);
13357
13358 if (breakpoint_chain == bpt)
13359 breakpoint_chain = bpt->next;
13360
13361 ALL_BREAKPOINTS (b)
13362 if (b->next == bpt)
13363 {
13364 b->next = bpt->next;
13365 break;
13366 }
13367
13368 /* Be sure no bpstat's are pointing at the breakpoint after it's
13369 been freed. */
13370 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13371 in all threads for now. Note that we cannot just remove bpstats
13372 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13373 commands are associated with the bpstat; if we remove it here,
13374 then the later call to bpstat_do_actions (&stop_bpstat); in
13375 event-top.c won't do anything, and temporary breakpoints with
13376 commands won't work. */
13377
13378 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13379
13380 /* Now that breakpoint is removed from breakpoint list, update the
13381 global location list. This will remove locations that used to
13382 belong to this breakpoint. Do this before freeing the breakpoint
13383 itself, since remove_breakpoint looks at location's owner. It
13384 might be better design to have location completely
13385 self-contained, but it's not the case now. */
13386 update_global_location_list (UGLL_DONT_INSERT);
13387
13388 /* On the chance that someone will soon try again to delete this
13389 same bp, we mark it as deleted before freeing its storage. */
13390 bpt->type = bp_none;
13391 delete bpt;
13392 }
13393
13394 static void
13395 do_delete_breakpoint_cleanup (void *b)
13396 {
13397 delete_breakpoint ((struct breakpoint *) b);
13398 }
13399
13400 struct cleanup *
13401 make_cleanup_delete_breakpoint (struct breakpoint *b)
13402 {
13403 return make_cleanup (do_delete_breakpoint_cleanup, b);
13404 }
13405
13406 /* Iterator function to call a user-provided callback function once
13407 for each of B and its related breakpoints. */
13408
13409 static void
13410 iterate_over_related_breakpoints (struct breakpoint *b,
13411 gdb::function_view<void (breakpoint *)> function)
13412 {
13413 struct breakpoint *related;
13414
13415 related = b;
13416 do
13417 {
13418 struct breakpoint *next;
13419
13420 /* FUNCTION may delete RELATED. */
13421 next = related->related_breakpoint;
13422
13423 if (next == related)
13424 {
13425 /* RELATED is the last ring entry. */
13426 function (related);
13427
13428 /* FUNCTION may have deleted it, so we'd never reach back to
13429 B. There's nothing left to do anyway, so just break
13430 out. */
13431 break;
13432 }
13433 else
13434 function (related);
13435
13436 related = next;
13437 }
13438 while (related != b);
13439 }
13440
13441 static void
13442 delete_command (char *arg, int from_tty)
13443 {
13444 struct breakpoint *b, *b_tmp;
13445
13446 dont_repeat ();
13447
13448 if (arg == 0)
13449 {
13450 int breaks_to_delete = 0;
13451
13452 /* Delete all breakpoints if no argument. Do not delete
13453 internal breakpoints, these have to be deleted with an
13454 explicit breakpoint number argument. */
13455 ALL_BREAKPOINTS (b)
13456 if (user_breakpoint_p (b))
13457 {
13458 breaks_to_delete = 1;
13459 break;
13460 }
13461
13462 /* Ask user only if there are some breakpoints to delete. */
13463 if (!from_tty
13464 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13465 {
13466 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13467 if (user_breakpoint_p (b))
13468 delete_breakpoint (b);
13469 }
13470 }
13471 else
13472 map_breakpoint_numbers
13473 (arg, [&] (breakpoint *b)
13474 {
13475 iterate_over_related_breakpoints (b, delete_breakpoint);
13476 });
13477 }
13478
13479 /* Return true if all locations of B bound to PSPACE are pending. If
13480 PSPACE is NULL, all locations of all program spaces are
13481 considered. */
13482
13483 static int
13484 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13485 {
13486 struct bp_location *loc;
13487
13488 for (loc = b->loc; loc != NULL; loc = loc->next)
13489 if ((pspace == NULL
13490 || loc->pspace == pspace)
13491 && !loc->shlib_disabled
13492 && !loc->pspace->executing_startup)
13493 return 0;
13494 return 1;
13495 }
13496
13497 /* Subroutine of update_breakpoint_locations to simplify it.
13498 Return non-zero if multiple fns in list LOC have the same name.
13499 Null names are ignored. */
13500
13501 static int
13502 ambiguous_names_p (struct bp_location *loc)
13503 {
13504 struct bp_location *l;
13505 htab_t htab = htab_create_alloc (13, htab_hash_string,
13506 (int (*) (const void *,
13507 const void *)) streq,
13508 NULL, xcalloc, xfree);
13509
13510 for (l = loc; l != NULL; l = l->next)
13511 {
13512 const char **slot;
13513 const char *name = l->function_name;
13514
13515 /* Allow for some names to be NULL, ignore them. */
13516 if (name == NULL)
13517 continue;
13518
13519 slot = (const char **) htab_find_slot (htab, (const void *) name,
13520 INSERT);
13521 /* NOTE: We can assume slot != NULL here because xcalloc never
13522 returns NULL. */
13523 if (*slot != NULL)
13524 {
13525 htab_delete (htab);
13526 return 1;
13527 }
13528 *slot = name;
13529 }
13530
13531 htab_delete (htab);
13532 return 0;
13533 }
13534
13535 /* When symbols change, it probably means the sources changed as well,
13536 and it might mean the static tracepoint markers are no longer at
13537 the same address or line numbers they used to be at last we
13538 checked. Losing your static tracepoints whenever you rebuild is
13539 undesirable. This function tries to resync/rematch gdb static
13540 tracepoints with the markers on the target, for static tracepoints
13541 that have not been set by marker id. Static tracepoint that have
13542 been set by marker id are reset by marker id in breakpoint_re_set.
13543 The heuristic is:
13544
13545 1) For a tracepoint set at a specific address, look for a marker at
13546 the old PC. If one is found there, assume to be the same marker.
13547 If the name / string id of the marker found is different from the
13548 previous known name, assume that means the user renamed the marker
13549 in the sources, and output a warning.
13550
13551 2) For a tracepoint set at a given line number, look for a marker
13552 at the new address of the old line number. If one is found there,
13553 assume to be the same marker. If the name / string id of the
13554 marker found is different from the previous known name, assume that
13555 means the user renamed the marker in the sources, and output a
13556 warning.
13557
13558 3) If a marker is no longer found at the same address or line, it
13559 may mean the marker no longer exists. But it may also just mean
13560 the code changed a bit. Maybe the user added a few lines of code
13561 that made the marker move up or down (in line number terms). Ask
13562 the target for info about the marker with the string id as we knew
13563 it. If found, update line number and address in the matching
13564 static tracepoint. This will get confused if there's more than one
13565 marker with the same ID (possible in UST, although unadvised
13566 precisely because it confuses tools). */
13567
13568 static struct symtab_and_line
13569 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13570 {
13571 struct tracepoint *tp = (struct tracepoint *) b;
13572 struct static_tracepoint_marker marker;
13573 CORE_ADDR pc;
13574
13575 pc = sal.pc;
13576 if (sal.line)
13577 find_line_pc (sal.symtab, sal.line, &pc);
13578
13579 if (target_static_tracepoint_marker_at (pc, &marker))
13580 {
13581 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13582 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13583 b->number,
13584 tp->static_trace_marker_id, marker.str_id);
13585
13586 xfree (tp->static_trace_marker_id);
13587 tp->static_trace_marker_id = xstrdup (marker.str_id);
13588 release_static_tracepoint_marker (&marker);
13589
13590 return sal;
13591 }
13592
13593 /* Old marker wasn't found on target at lineno. Try looking it up
13594 by string ID. */
13595 if (!sal.explicit_pc
13596 && sal.line != 0
13597 && sal.symtab != NULL
13598 && tp->static_trace_marker_id != NULL)
13599 {
13600 VEC(static_tracepoint_marker_p) *markers;
13601
13602 markers
13603 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13604
13605 if (!VEC_empty(static_tracepoint_marker_p, markers))
13606 {
13607 struct symbol *sym;
13608 struct static_tracepoint_marker *tpmarker;
13609 struct ui_out *uiout = current_uiout;
13610 struct explicit_location explicit_loc;
13611
13612 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13613
13614 xfree (tp->static_trace_marker_id);
13615 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13616
13617 warning (_("marker for static tracepoint %d (%s) not "
13618 "found at previous line number"),
13619 b->number, tp->static_trace_marker_id);
13620
13621 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13622 sym = find_pc_sect_function (tpmarker->address, NULL);
13623 uiout->text ("Now in ");
13624 if (sym)
13625 {
13626 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13627 uiout->text (" at ");
13628 }
13629 uiout->field_string ("file",
13630 symtab_to_filename_for_display (sal2.symtab));
13631 uiout->text (":");
13632
13633 if (uiout->is_mi_like_p ())
13634 {
13635 const char *fullname = symtab_to_fullname (sal2.symtab);
13636
13637 uiout->field_string ("fullname", fullname);
13638 }
13639
13640 uiout->field_int ("line", sal2.line);
13641 uiout->text ("\n");
13642
13643 b->loc->line_number = sal2.line;
13644 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13645
13646 b->location.reset (NULL);
13647 initialize_explicit_location (&explicit_loc);
13648 explicit_loc.source_filename
13649 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13650 explicit_loc.line_offset.offset = b->loc->line_number;
13651 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13652 b->location = new_explicit_location (&explicit_loc);
13653
13654 /* Might be nice to check if function changed, and warn if
13655 so. */
13656
13657 release_static_tracepoint_marker (tpmarker);
13658 }
13659 }
13660 return sal;
13661 }
13662
13663 /* Returns 1 iff locations A and B are sufficiently same that
13664 we don't need to report breakpoint as changed. */
13665
13666 static int
13667 locations_are_equal (struct bp_location *a, struct bp_location *b)
13668 {
13669 while (a && b)
13670 {
13671 if (a->address != b->address)
13672 return 0;
13673
13674 if (a->shlib_disabled != b->shlib_disabled)
13675 return 0;
13676
13677 if (a->enabled != b->enabled)
13678 return 0;
13679
13680 a = a->next;
13681 b = b->next;
13682 }
13683
13684 if ((a == NULL) != (b == NULL))
13685 return 0;
13686
13687 return 1;
13688 }
13689
13690 /* Split all locations of B that are bound to PSPACE out of B's
13691 location list to a separate list and return that list's head. If
13692 PSPACE is NULL, hoist out all locations of B. */
13693
13694 static struct bp_location *
13695 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13696 {
13697 struct bp_location head;
13698 struct bp_location *i = b->loc;
13699 struct bp_location **i_link = &b->loc;
13700 struct bp_location *hoisted = &head;
13701
13702 if (pspace == NULL)
13703 {
13704 i = b->loc;
13705 b->loc = NULL;
13706 return i;
13707 }
13708
13709 head.next = NULL;
13710
13711 while (i != NULL)
13712 {
13713 if (i->pspace == pspace)
13714 {
13715 *i_link = i->next;
13716 i->next = NULL;
13717 hoisted->next = i;
13718 hoisted = i;
13719 }
13720 else
13721 i_link = &i->next;
13722 i = *i_link;
13723 }
13724
13725 return head.next;
13726 }
13727
13728 /* Create new breakpoint locations for B (a hardware or software
13729 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13730 zero, then B is a ranged breakpoint. Only recreates locations for
13731 FILTER_PSPACE. Locations of other program spaces are left
13732 untouched. */
13733
13734 void
13735 update_breakpoint_locations (struct breakpoint *b,
13736 struct program_space *filter_pspace,
13737 gdb::array_view<const symtab_and_line> sals,
13738 gdb::array_view<const symtab_and_line> sals_end)
13739 {
13740 int i;
13741 struct bp_location *existing_locations;
13742
13743 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13744 {
13745 /* Ranged breakpoints have only one start location and one end
13746 location. */
13747 b->enable_state = bp_disabled;
13748 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13749 "multiple locations found\n"),
13750 b->number);
13751 return;
13752 }
13753
13754 /* If there's no new locations, and all existing locations are
13755 pending, don't do anything. This optimizes the common case where
13756 all locations are in the same shared library, that was unloaded.
13757 We'd like to retain the location, so that when the library is
13758 loaded again, we don't loose the enabled/disabled status of the
13759 individual locations. */
13760 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13761 return;
13762
13763 existing_locations = hoist_existing_locations (b, filter_pspace);
13764
13765 for (const auto &sal : sals)
13766 {
13767 struct bp_location *new_loc;
13768
13769 switch_to_program_space_and_thread (sal.pspace);
13770
13771 new_loc = add_location_to_breakpoint (b, &sal);
13772
13773 /* Reparse conditions, they might contain references to the
13774 old symtab. */
13775 if (b->cond_string != NULL)
13776 {
13777 const char *s;
13778
13779 s = b->cond_string;
13780 TRY
13781 {
13782 new_loc->cond = parse_exp_1 (&s, sal.pc,
13783 block_for_pc (sal.pc),
13784 0);
13785 }
13786 CATCH (e, RETURN_MASK_ERROR)
13787 {
13788 warning (_("failed to reevaluate condition "
13789 "for breakpoint %d: %s"),
13790 b->number, e.message);
13791 new_loc->enabled = 0;
13792 }
13793 END_CATCH
13794 }
13795
13796 if (!sals_end.empty ())
13797 {
13798 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13799
13800 new_loc->length = end - sals[0].pc + 1;
13801 }
13802 }
13803
13804 /* If possible, carry over 'disable' status from existing
13805 breakpoints. */
13806 {
13807 struct bp_location *e = existing_locations;
13808 /* If there are multiple breakpoints with the same function name,
13809 e.g. for inline functions, comparing function names won't work.
13810 Instead compare pc addresses; this is just a heuristic as things
13811 may have moved, but in practice it gives the correct answer
13812 often enough until a better solution is found. */
13813 int have_ambiguous_names = ambiguous_names_p (b->loc);
13814
13815 for (; e; e = e->next)
13816 {
13817 if (!e->enabled && e->function_name)
13818 {
13819 struct bp_location *l = b->loc;
13820 if (have_ambiguous_names)
13821 {
13822 for (; l; l = l->next)
13823 if (breakpoint_locations_match (e, l))
13824 {
13825 l->enabled = 0;
13826 break;
13827 }
13828 }
13829 else
13830 {
13831 for (; l; l = l->next)
13832 if (l->function_name
13833 && strcmp (e->function_name, l->function_name) == 0)
13834 {
13835 l->enabled = 0;
13836 break;
13837 }
13838 }
13839 }
13840 }
13841 }
13842
13843 if (!locations_are_equal (existing_locations, b->loc))
13844 observer_notify_breakpoint_modified (b);
13845 }
13846
13847 /* Find the SaL locations corresponding to the given LOCATION.
13848 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13849
13850 static std::vector<symtab_and_line>
13851 location_to_sals (struct breakpoint *b, struct event_location *location,
13852 struct program_space *search_pspace, int *found)
13853 {
13854 struct gdb_exception exception = exception_none;
13855
13856 gdb_assert (b->ops != NULL);
13857
13858 std::vector<symtab_and_line> sals;
13859
13860 TRY
13861 {
13862 sals = b->ops->decode_location (b, location, search_pspace);
13863 }
13864 CATCH (e, RETURN_MASK_ERROR)
13865 {
13866 int not_found_and_ok = 0;
13867
13868 exception = e;
13869
13870 /* For pending breakpoints, it's expected that parsing will
13871 fail until the right shared library is loaded. User has
13872 already told to create pending breakpoints and don't need
13873 extra messages. If breakpoint is in bp_shlib_disabled
13874 state, then user already saw the message about that
13875 breakpoint being disabled, and don't want to see more
13876 errors. */
13877 if (e.error == NOT_FOUND_ERROR
13878 && (b->condition_not_parsed
13879 || (b->loc != NULL
13880 && search_pspace != NULL
13881 && b->loc->pspace != search_pspace)
13882 || (b->loc && b->loc->shlib_disabled)
13883 || (b->loc && b->loc->pspace->executing_startup)
13884 || b->enable_state == bp_disabled))
13885 not_found_and_ok = 1;
13886
13887 if (!not_found_and_ok)
13888 {
13889 /* We surely don't want to warn about the same breakpoint
13890 10 times. One solution, implemented here, is disable
13891 the breakpoint on error. Another solution would be to
13892 have separate 'warning emitted' flag. Since this
13893 happens only when a binary has changed, I don't know
13894 which approach is better. */
13895 b->enable_state = bp_disabled;
13896 throw_exception (e);
13897 }
13898 }
13899 END_CATCH
13900
13901 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13902 {
13903 for (auto &sal : sals)
13904 resolve_sal_pc (&sal);
13905 if (b->condition_not_parsed && b->extra_string != NULL)
13906 {
13907 char *cond_string, *extra_string;
13908 int thread, task;
13909
13910 find_condition_and_thread (b->extra_string, sals[0].pc,
13911 &cond_string, &thread, &task,
13912 &extra_string);
13913 gdb_assert (b->cond_string == NULL);
13914 if (cond_string)
13915 b->cond_string = cond_string;
13916 b->thread = thread;
13917 b->task = task;
13918 if (extra_string)
13919 {
13920 xfree (b->extra_string);
13921 b->extra_string = extra_string;
13922 }
13923 b->condition_not_parsed = 0;
13924 }
13925
13926 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13927 sals[0] = update_static_tracepoint (b, sals[0]);
13928
13929 *found = 1;
13930 }
13931 else
13932 *found = 0;
13933
13934 return sals;
13935 }
13936
13937 /* The default re_set method, for typical hardware or software
13938 breakpoints. Reevaluate the breakpoint and recreate its
13939 locations. */
13940
13941 static void
13942 breakpoint_re_set_default (struct breakpoint *b)
13943 {
13944 struct program_space *filter_pspace = current_program_space;
13945 std::vector<symtab_and_line> expanded, expanded_end;
13946
13947 int found;
13948 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13949 filter_pspace, &found);
13950 if (found)
13951 expanded = std::move (sals);
13952
13953 if (b->location_range_end != NULL)
13954 {
13955 std::vector<symtab_and_line> sals_end
13956 = location_to_sals (b, b->location_range_end.get (),
13957 filter_pspace, &found);
13958 if (found)
13959 expanded_end = std::move (sals_end);
13960 }
13961
13962 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13963 }
13964
13965 /* Default method for creating SALs from an address string. It basically
13966 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13967
13968 static void
13969 create_sals_from_location_default (const struct event_location *location,
13970 struct linespec_result *canonical,
13971 enum bptype type_wanted)
13972 {
13973 parse_breakpoint_sals (location, canonical);
13974 }
13975
13976 /* Call create_breakpoints_sal for the given arguments. This is the default
13977 function for the `create_breakpoints_sal' method of
13978 breakpoint_ops. */
13979
13980 static void
13981 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13982 struct linespec_result *canonical,
13983 gdb::unique_xmalloc_ptr<char> cond_string,
13984 gdb::unique_xmalloc_ptr<char> extra_string,
13985 enum bptype type_wanted,
13986 enum bpdisp disposition,
13987 int thread,
13988 int task, int ignore_count,
13989 const struct breakpoint_ops *ops,
13990 int from_tty, int enabled,
13991 int internal, unsigned flags)
13992 {
13993 create_breakpoints_sal (gdbarch, canonical,
13994 std::move (cond_string),
13995 std::move (extra_string),
13996 type_wanted, disposition,
13997 thread, task, ignore_count, ops, from_tty,
13998 enabled, internal, flags);
13999 }
14000
14001 /* Decode the line represented by S by calling decode_line_full. This is the
14002 default function for the `decode_location' method of breakpoint_ops. */
14003
14004 static std::vector<symtab_and_line>
14005 decode_location_default (struct breakpoint *b,
14006 const struct event_location *location,
14007 struct program_space *search_pspace)
14008 {
14009 struct linespec_result canonical;
14010
14011 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14012 (struct symtab *) NULL, 0,
14013 &canonical, multiple_symbols_all,
14014 b->filter);
14015
14016 /* We should get 0 or 1 resulting SALs. */
14017 gdb_assert (canonical.lsals.size () < 2);
14018
14019 if (!canonical.lsals.empty ())
14020 {
14021 const linespec_sals &lsal = canonical.lsals[0];
14022 return std::move (lsal.sals);
14023 }
14024 return {};
14025 }
14026
14027 /* Reset a breakpoint. */
14028
14029 static void
14030 breakpoint_re_set_one (breakpoint *b)
14031 {
14032 input_radix = b->input_radix;
14033 set_language (b->language);
14034
14035 b->ops->re_set (b);
14036 }
14037
14038 /* Re-set breakpoint locations for the current program space.
14039 Locations bound to other program spaces are left untouched. */
14040
14041 void
14042 breakpoint_re_set (void)
14043 {
14044 struct breakpoint *b, *b_tmp;
14045
14046 {
14047 scoped_restore_current_language save_language;
14048 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
14049 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14050
14051 /* Note: we must not try to insert locations until after all
14052 breakpoints have been re-set. Otherwise, e.g., when re-setting
14053 breakpoint 1, we'd insert the locations of breakpoint 2, which
14054 hadn't been re-set yet, and thus may have stale locations. */
14055
14056 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14057 {
14058 TRY
14059 {
14060 breakpoint_re_set_one (b);
14061 }
14062 CATCH (ex, RETURN_MASK_ALL)
14063 {
14064 exception_fprintf (gdb_stderr, ex,
14065 "Error in re-setting breakpoint %d: ",
14066 b->number);
14067 }
14068 END_CATCH
14069 }
14070
14071 jit_breakpoint_re_set ();
14072 }
14073
14074 create_overlay_event_breakpoint ();
14075 create_longjmp_master_breakpoint ();
14076 create_std_terminate_master_breakpoint ();
14077 create_exception_master_breakpoint ();
14078
14079 /* Now we can insert. */
14080 update_global_location_list (UGLL_MAY_INSERT);
14081 }
14082 \f
14083 /* Reset the thread number of this breakpoint:
14084
14085 - If the breakpoint is for all threads, leave it as-is.
14086 - Else, reset it to the current thread for inferior_ptid. */
14087 void
14088 breakpoint_re_set_thread (struct breakpoint *b)
14089 {
14090 if (b->thread != -1)
14091 {
14092 if (in_thread_list (inferior_ptid))
14093 b->thread = ptid_to_global_thread_id (inferior_ptid);
14094
14095 /* We're being called after following a fork. The new fork is
14096 selected as current, and unless this was a vfork will have a
14097 different program space from the original thread. Reset that
14098 as well. */
14099 b->loc->pspace = current_program_space;
14100 }
14101 }
14102
14103 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14104 If from_tty is nonzero, it prints a message to that effect,
14105 which ends with a period (no newline). */
14106
14107 void
14108 set_ignore_count (int bptnum, int count, int from_tty)
14109 {
14110 struct breakpoint *b;
14111
14112 if (count < 0)
14113 count = 0;
14114
14115 ALL_BREAKPOINTS (b)
14116 if (b->number == bptnum)
14117 {
14118 if (is_tracepoint (b))
14119 {
14120 if (from_tty && count != 0)
14121 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14122 bptnum);
14123 return;
14124 }
14125
14126 b->ignore_count = count;
14127 if (from_tty)
14128 {
14129 if (count == 0)
14130 printf_filtered (_("Will stop next time "
14131 "breakpoint %d is reached."),
14132 bptnum);
14133 else if (count == 1)
14134 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14135 bptnum);
14136 else
14137 printf_filtered (_("Will ignore next %d "
14138 "crossings of breakpoint %d."),
14139 count, bptnum);
14140 }
14141 observer_notify_breakpoint_modified (b);
14142 return;
14143 }
14144
14145 error (_("No breakpoint number %d."), bptnum);
14146 }
14147
14148 /* Command to set ignore-count of breakpoint N to COUNT. */
14149
14150 static void
14151 ignore_command (char *args, int from_tty)
14152 {
14153 char *p = args;
14154 int num;
14155
14156 if (p == 0)
14157 error_no_arg (_("a breakpoint number"));
14158
14159 num = get_number (&p);
14160 if (num == 0)
14161 error (_("bad breakpoint number: '%s'"), args);
14162 if (*p == 0)
14163 error (_("Second argument (specified ignore-count) is missing."));
14164
14165 set_ignore_count (num,
14166 longest_to_int (value_as_long (parse_and_eval (p))),
14167 from_tty);
14168 if (from_tty)
14169 printf_filtered ("\n");
14170 }
14171 \f
14172 /* Call FUNCTION on each of the breakpoints
14173 whose numbers are given in ARGS. */
14174
14175 static void
14176 map_breakpoint_numbers (const char *args,
14177 gdb::function_view<void (breakpoint *)> function)
14178 {
14179 int num;
14180 struct breakpoint *b, *tmp;
14181
14182 if (args == 0 || *args == '\0')
14183 error_no_arg (_("one or more breakpoint numbers"));
14184
14185 number_or_range_parser parser (args);
14186
14187 while (!parser.finished ())
14188 {
14189 const char *p = parser.cur_tok ();
14190 bool match = false;
14191
14192 num = parser.get_number ();
14193 if (num == 0)
14194 {
14195 warning (_("bad breakpoint number at or near '%s'"), p);
14196 }
14197 else
14198 {
14199 ALL_BREAKPOINTS_SAFE (b, tmp)
14200 if (b->number == num)
14201 {
14202 match = true;
14203 function (b);
14204 break;
14205 }
14206 if (!match)
14207 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14208 }
14209 }
14210 }
14211
14212 static struct bp_location *
14213 find_location_by_number (const char *number)
14214 {
14215 const char *p1;
14216 int bp_num;
14217 int loc_num;
14218 struct breakpoint *b;
14219 struct bp_location *loc;
14220
14221 p1 = number;
14222 bp_num = get_number_trailer (&p1, '.');
14223 if (bp_num == 0 || p1[0] != '.')
14224 error (_("Bad breakpoint number '%s'"), number);
14225
14226 ALL_BREAKPOINTS (b)
14227 if (b->number == bp_num)
14228 {
14229 break;
14230 }
14231
14232 if (!b || b->number != bp_num)
14233 error (_("Bad breakpoint number '%s'"), number);
14234
14235 /* Skip the dot. */
14236 ++p1;
14237 const char *save = p1;
14238 loc_num = get_number (&p1);
14239 if (loc_num == 0)
14240 error (_("Bad breakpoint location number '%s'"), number);
14241
14242 --loc_num;
14243 loc = b->loc;
14244 for (;loc_num && loc; --loc_num, loc = loc->next)
14245 ;
14246 if (!loc)
14247 error (_("Bad breakpoint location number '%s'"), save);
14248
14249 return loc;
14250 }
14251
14252
14253 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14254 If from_tty is nonzero, it prints a message to that effect,
14255 which ends with a period (no newline). */
14256
14257 void
14258 disable_breakpoint (struct breakpoint *bpt)
14259 {
14260 /* Never disable a watchpoint scope breakpoint; we want to
14261 hit them when we leave scope so we can delete both the
14262 watchpoint and its scope breakpoint at that time. */
14263 if (bpt->type == bp_watchpoint_scope)
14264 return;
14265
14266 bpt->enable_state = bp_disabled;
14267
14268 /* Mark breakpoint locations modified. */
14269 mark_breakpoint_modified (bpt);
14270
14271 if (target_supports_enable_disable_tracepoint ()
14272 && current_trace_status ()->running && is_tracepoint (bpt))
14273 {
14274 struct bp_location *location;
14275
14276 for (location = bpt->loc; location; location = location->next)
14277 target_disable_tracepoint (location);
14278 }
14279
14280 update_global_location_list (UGLL_DONT_INSERT);
14281
14282 observer_notify_breakpoint_modified (bpt);
14283 }
14284
14285 static void
14286 disable_command (char *args, int from_tty)
14287 {
14288 if (args == 0)
14289 {
14290 struct breakpoint *bpt;
14291
14292 ALL_BREAKPOINTS (bpt)
14293 if (user_breakpoint_p (bpt))
14294 disable_breakpoint (bpt);
14295 }
14296 else
14297 {
14298 std::string num = extract_arg (&args);
14299
14300 while (!num.empty ())
14301 {
14302 if (num.find ('.') != std::string::npos)
14303 {
14304 struct bp_location *loc = find_location_by_number (num.c_str ());
14305
14306 if (loc)
14307 {
14308 if (loc->enabled)
14309 {
14310 loc->enabled = 0;
14311 mark_breakpoint_location_modified (loc);
14312 }
14313 if (target_supports_enable_disable_tracepoint ()
14314 && current_trace_status ()->running && loc->owner
14315 && is_tracepoint (loc->owner))
14316 target_disable_tracepoint (loc);
14317 }
14318 update_global_location_list (UGLL_DONT_INSERT);
14319 }
14320 else
14321 map_breakpoint_numbers
14322 (num.c_str (), [&] (breakpoint *b)
14323 {
14324 iterate_over_related_breakpoints (b, disable_breakpoint);
14325 });
14326 num = extract_arg (&args);
14327 }
14328 }
14329 }
14330
14331 static void
14332 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14333 int count)
14334 {
14335 int target_resources_ok;
14336
14337 if (bpt->type == bp_hardware_breakpoint)
14338 {
14339 int i;
14340 i = hw_breakpoint_used_count ();
14341 target_resources_ok =
14342 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14343 i + 1, 0);
14344 if (target_resources_ok == 0)
14345 error (_("No hardware breakpoint support in the target."));
14346 else if (target_resources_ok < 0)
14347 error (_("Hardware breakpoints used exceeds limit."));
14348 }
14349
14350 if (is_watchpoint (bpt))
14351 {
14352 /* Initialize it just to avoid a GCC false warning. */
14353 enum enable_state orig_enable_state = bp_disabled;
14354
14355 TRY
14356 {
14357 struct watchpoint *w = (struct watchpoint *) bpt;
14358
14359 orig_enable_state = bpt->enable_state;
14360 bpt->enable_state = bp_enabled;
14361 update_watchpoint (w, 1 /* reparse */);
14362 }
14363 CATCH (e, RETURN_MASK_ALL)
14364 {
14365 bpt->enable_state = orig_enable_state;
14366 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14367 bpt->number);
14368 return;
14369 }
14370 END_CATCH
14371 }
14372
14373 bpt->enable_state = bp_enabled;
14374
14375 /* Mark breakpoint locations modified. */
14376 mark_breakpoint_modified (bpt);
14377
14378 if (target_supports_enable_disable_tracepoint ()
14379 && current_trace_status ()->running && is_tracepoint (bpt))
14380 {
14381 struct bp_location *location;
14382
14383 for (location = bpt->loc; location; location = location->next)
14384 target_enable_tracepoint (location);
14385 }
14386
14387 bpt->disposition = disposition;
14388 bpt->enable_count = count;
14389 update_global_location_list (UGLL_MAY_INSERT);
14390
14391 observer_notify_breakpoint_modified (bpt);
14392 }
14393
14394
14395 void
14396 enable_breakpoint (struct breakpoint *bpt)
14397 {
14398 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14399 }
14400
14401 /* The enable command enables the specified breakpoints (or all defined
14402 breakpoints) so they once again become (or continue to be) effective
14403 in stopping the inferior. */
14404
14405 static void
14406 enable_command (char *args, int from_tty)
14407 {
14408 if (args == 0)
14409 {
14410 struct breakpoint *bpt;
14411
14412 ALL_BREAKPOINTS (bpt)
14413 if (user_breakpoint_p (bpt))
14414 enable_breakpoint (bpt);
14415 }
14416 else
14417 {
14418 std::string num = extract_arg (&args);
14419
14420 while (!num.empty ())
14421 {
14422 if (num.find ('.') != std::string::npos)
14423 {
14424 struct bp_location *loc = find_location_by_number (num.c_str ());
14425
14426 if (loc)
14427 {
14428 if (!loc->enabled)
14429 {
14430 loc->enabled = 1;
14431 mark_breakpoint_location_modified (loc);
14432 }
14433 if (target_supports_enable_disable_tracepoint ()
14434 && current_trace_status ()->running && loc->owner
14435 && is_tracepoint (loc->owner))
14436 target_enable_tracepoint (loc);
14437 }
14438 update_global_location_list (UGLL_MAY_INSERT);
14439 }
14440 else
14441 map_breakpoint_numbers
14442 (num.c_str (), [&] (breakpoint *b)
14443 {
14444 iterate_over_related_breakpoints (b, enable_breakpoint);
14445 });
14446 num = extract_arg (&args);
14447 }
14448 }
14449 }
14450
14451 static void
14452 enable_once_command (const char *args, int from_tty)
14453 {
14454 map_breakpoint_numbers
14455 (args, [&] (breakpoint *b)
14456 {
14457 iterate_over_related_breakpoints
14458 (b, [&] (breakpoint *bpt)
14459 {
14460 enable_breakpoint_disp (bpt, disp_disable, 1);
14461 });
14462 });
14463 }
14464
14465 static void
14466 enable_count_command (const char *args, int from_tty)
14467 {
14468 int count;
14469
14470 if (args == NULL)
14471 error_no_arg (_("hit count"));
14472
14473 count = get_number (&args);
14474
14475 map_breakpoint_numbers
14476 (args, [&] (breakpoint *b)
14477 {
14478 iterate_over_related_breakpoints
14479 (b, [&] (breakpoint *bpt)
14480 {
14481 enable_breakpoint_disp (bpt, disp_disable, count);
14482 });
14483 });
14484 }
14485
14486 static void
14487 enable_delete_command (const char *args, int from_tty)
14488 {
14489 map_breakpoint_numbers
14490 (args, [&] (breakpoint *b)
14491 {
14492 iterate_over_related_breakpoints
14493 (b, [&] (breakpoint *bpt)
14494 {
14495 enable_breakpoint_disp (bpt, disp_del, 1);
14496 });
14497 });
14498 }
14499 \f
14500 static void
14501 set_breakpoint_cmd (char *args, int from_tty)
14502 {
14503 }
14504
14505 static void
14506 show_breakpoint_cmd (char *args, int from_tty)
14507 {
14508 }
14509
14510 /* Invalidate last known value of any hardware watchpoint if
14511 the memory which that value represents has been written to by
14512 GDB itself. */
14513
14514 static void
14515 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14516 CORE_ADDR addr, ssize_t len,
14517 const bfd_byte *data)
14518 {
14519 struct breakpoint *bp;
14520
14521 ALL_BREAKPOINTS (bp)
14522 if (bp->enable_state == bp_enabled
14523 && bp->type == bp_hardware_watchpoint)
14524 {
14525 struct watchpoint *wp = (struct watchpoint *) bp;
14526
14527 if (wp->val_valid && wp->val)
14528 {
14529 struct bp_location *loc;
14530
14531 for (loc = bp->loc; loc != NULL; loc = loc->next)
14532 if (loc->loc_type == bp_loc_hardware_watchpoint
14533 && loc->address + loc->length > addr
14534 && addr + len > loc->address)
14535 {
14536 value_free (wp->val);
14537 wp->val = NULL;
14538 wp->val_valid = 0;
14539 }
14540 }
14541 }
14542 }
14543
14544 /* Create and insert a breakpoint for software single step. */
14545
14546 void
14547 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14548 struct address_space *aspace,
14549 CORE_ADDR next_pc)
14550 {
14551 struct thread_info *tp = inferior_thread ();
14552 struct symtab_and_line sal;
14553 CORE_ADDR pc = next_pc;
14554
14555 if (tp->control.single_step_breakpoints == NULL)
14556 {
14557 tp->control.single_step_breakpoints
14558 = new_single_step_breakpoint (tp->global_num, gdbarch);
14559 }
14560
14561 sal = find_pc_line (pc, 0);
14562 sal.pc = pc;
14563 sal.section = find_pc_overlay (pc);
14564 sal.explicit_pc = 1;
14565 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14566
14567 update_global_location_list (UGLL_INSERT);
14568 }
14569
14570 /* Insert single step breakpoints according to the current state. */
14571
14572 int
14573 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14574 {
14575 struct regcache *regcache = get_current_regcache ();
14576 std::vector<CORE_ADDR> next_pcs;
14577
14578 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14579
14580 if (!next_pcs.empty ())
14581 {
14582 struct frame_info *frame = get_current_frame ();
14583 struct address_space *aspace = get_frame_address_space (frame);
14584
14585 for (CORE_ADDR pc : next_pcs)
14586 insert_single_step_breakpoint (gdbarch, aspace, pc);
14587
14588 return 1;
14589 }
14590 else
14591 return 0;
14592 }
14593
14594 /* See breakpoint.h. */
14595
14596 int
14597 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14598 struct address_space *aspace,
14599 CORE_ADDR pc)
14600 {
14601 struct bp_location *loc;
14602
14603 for (loc = bp->loc; loc != NULL; loc = loc->next)
14604 if (loc->inserted
14605 && breakpoint_location_address_match (loc, aspace, pc))
14606 return 1;
14607
14608 return 0;
14609 }
14610
14611 /* Check whether a software single-step breakpoint is inserted at
14612 PC. */
14613
14614 int
14615 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14616 CORE_ADDR pc)
14617 {
14618 struct breakpoint *bpt;
14619
14620 ALL_BREAKPOINTS (bpt)
14621 {
14622 if (bpt->type == bp_single_step
14623 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14624 return 1;
14625 }
14626 return 0;
14627 }
14628
14629 /* Tracepoint-specific operations. */
14630
14631 /* Set tracepoint count to NUM. */
14632 static void
14633 set_tracepoint_count (int num)
14634 {
14635 tracepoint_count = num;
14636 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14637 }
14638
14639 static void
14640 trace_command (char *arg_in, int from_tty)
14641 {
14642 const char *arg = arg_in;
14643 struct breakpoint_ops *ops;
14644
14645 event_location_up location = string_to_event_location (&arg,
14646 current_language);
14647 if (location != NULL
14648 && event_location_type (location.get ()) == PROBE_LOCATION)
14649 ops = &tracepoint_probe_breakpoint_ops;
14650 else
14651 ops = &tracepoint_breakpoint_ops;
14652
14653 create_breakpoint (get_current_arch (),
14654 location.get (),
14655 NULL, 0, arg, 1 /* parse arg */,
14656 0 /* tempflag */,
14657 bp_tracepoint /* type_wanted */,
14658 0 /* Ignore count */,
14659 pending_break_support,
14660 ops,
14661 from_tty,
14662 1 /* enabled */,
14663 0 /* internal */, 0);
14664 }
14665
14666 static void
14667 ftrace_command (char *arg_in, int from_tty)
14668 {
14669 const char *arg = arg_in;
14670 event_location_up location = string_to_event_location (&arg,
14671 current_language);
14672 create_breakpoint (get_current_arch (),
14673 location.get (),
14674 NULL, 0, arg, 1 /* parse arg */,
14675 0 /* tempflag */,
14676 bp_fast_tracepoint /* type_wanted */,
14677 0 /* Ignore count */,
14678 pending_break_support,
14679 &tracepoint_breakpoint_ops,
14680 from_tty,
14681 1 /* enabled */,
14682 0 /* internal */, 0);
14683 }
14684
14685 /* strace command implementation. Creates a static tracepoint. */
14686
14687 static void
14688 strace_command (char *arg_in, int from_tty)
14689 {
14690 const char *arg = arg_in;
14691 struct breakpoint_ops *ops;
14692 event_location_up location;
14693
14694 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14695 or with a normal static tracepoint. */
14696 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14697 {
14698 ops = &strace_marker_breakpoint_ops;
14699 location = new_linespec_location (&arg);
14700 }
14701 else
14702 {
14703 ops = &tracepoint_breakpoint_ops;
14704 location = string_to_event_location (&arg, current_language);
14705 }
14706
14707 create_breakpoint (get_current_arch (),
14708 location.get (),
14709 NULL, 0, arg, 1 /* parse arg */,
14710 0 /* tempflag */,
14711 bp_static_tracepoint /* type_wanted */,
14712 0 /* Ignore count */,
14713 pending_break_support,
14714 ops,
14715 from_tty,
14716 1 /* enabled */,
14717 0 /* internal */, 0);
14718 }
14719
14720 /* Set up a fake reader function that gets command lines from a linked
14721 list that was acquired during tracepoint uploading. */
14722
14723 static struct uploaded_tp *this_utp;
14724 static int next_cmd;
14725
14726 static char *
14727 read_uploaded_action (void)
14728 {
14729 char *rslt;
14730
14731 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14732
14733 next_cmd++;
14734
14735 return rslt;
14736 }
14737
14738 /* Given information about a tracepoint as recorded on a target (which
14739 can be either a live system or a trace file), attempt to create an
14740 equivalent GDB tracepoint. This is not a reliable process, since
14741 the target does not necessarily have all the information used when
14742 the tracepoint was originally defined. */
14743
14744 struct tracepoint *
14745 create_tracepoint_from_upload (struct uploaded_tp *utp)
14746 {
14747 const char *addr_str;
14748 char small_buf[100];
14749 struct tracepoint *tp;
14750
14751 if (utp->at_string)
14752 addr_str = utp->at_string;
14753 else
14754 {
14755 /* In the absence of a source location, fall back to raw
14756 address. Since there is no way to confirm that the address
14757 means the same thing as when the trace was started, warn the
14758 user. */
14759 warning (_("Uploaded tracepoint %d has no "
14760 "source location, using raw address"),
14761 utp->number);
14762 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14763 addr_str = small_buf;
14764 }
14765
14766 /* There's not much we can do with a sequence of bytecodes. */
14767 if (utp->cond && !utp->cond_string)
14768 warning (_("Uploaded tracepoint %d condition "
14769 "has no source form, ignoring it"),
14770 utp->number);
14771
14772 event_location_up location = string_to_event_location (&addr_str,
14773 current_language);
14774 if (!create_breakpoint (get_current_arch (),
14775 location.get (),
14776 utp->cond_string, -1, addr_str,
14777 0 /* parse cond/thread */,
14778 0 /* tempflag */,
14779 utp->type /* type_wanted */,
14780 0 /* Ignore count */,
14781 pending_break_support,
14782 &tracepoint_breakpoint_ops,
14783 0 /* from_tty */,
14784 utp->enabled /* enabled */,
14785 0 /* internal */,
14786 CREATE_BREAKPOINT_FLAGS_INSERTED))
14787 return NULL;
14788
14789 /* Get the tracepoint we just created. */
14790 tp = get_tracepoint (tracepoint_count);
14791 gdb_assert (tp != NULL);
14792
14793 if (utp->pass > 0)
14794 {
14795 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14796 tp->number);
14797
14798 trace_pass_command (small_buf, 0);
14799 }
14800
14801 /* If we have uploaded versions of the original commands, set up a
14802 special-purpose "reader" function and call the usual command line
14803 reader, then pass the result to the breakpoint command-setting
14804 function. */
14805 if (!VEC_empty (char_ptr, utp->cmd_strings))
14806 {
14807 command_line_up cmd_list;
14808
14809 this_utp = utp;
14810 next_cmd = 0;
14811
14812 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14813
14814 breakpoint_set_commands (tp, std::move (cmd_list));
14815 }
14816 else if (!VEC_empty (char_ptr, utp->actions)
14817 || !VEC_empty (char_ptr, utp->step_actions))
14818 warning (_("Uploaded tracepoint %d actions "
14819 "have no source form, ignoring them"),
14820 utp->number);
14821
14822 /* Copy any status information that might be available. */
14823 tp->hit_count = utp->hit_count;
14824 tp->traceframe_usage = utp->traceframe_usage;
14825
14826 return tp;
14827 }
14828
14829 /* Print information on tracepoint number TPNUM_EXP, or all if
14830 omitted. */
14831
14832 static void
14833 info_tracepoints_command (char *args, int from_tty)
14834 {
14835 struct ui_out *uiout = current_uiout;
14836 int num_printed;
14837
14838 num_printed = breakpoint_1 (args, 0, is_tracepoint);
14839
14840 if (num_printed == 0)
14841 {
14842 if (args == NULL || *args == '\0')
14843 uiout->message ("No tracepoints.\n");
14844 else
14845 uiout->message ("No tracepoint matching '%s'.\n", args);
14846 }
14847
14848 default_collect_info ();
14849 }
14850
14851 /* The 'enable trace' command enables tracepoints.
14852 Not supported by all targets. */
14853 static void
14854 enable_trace_command (char *args, int from_tty)
14855 {
14856 enable_command (args, from_tty);
14857 }
14858
14859 /* The 'disable trace' command disables tracepoints.
14860 Not supported by all targets. */
14861 static void
14862 disable_trace_command (char *args, int from_tty)
14863 {
14864 disable_command (args, from_tty);
14865 }
14866
14867 /* Remove a tracepoint (or all if no argument). */
14868 static void
14869 delete_trace_command (const char *arg, int from_tty)
14870 {
14871 struct breakpoint *b, *b_tmp;
14872
14873 dont_repeat ();
14874
14875 if (arg == 0)
14876 {
14877 int breaks_to_delete = 0;
14878
14879 /* Delete all breakpoints if no argument.
14880 Do not delete internal or call-dummy breakpoints, these
14881 have to be deleted with an explicit breakpoint number
14882 argument. */
14883 ALL_TRACEPOINTS (b)
14884 if (is_tracepoint (b) && user_breakpoint_p (b))
14885 {
14886 breaks_to_delete = 1;
14887 break;
14888 }
14889
14890 /* Ask user only if there are some breakpoints to delete. */
14891 if (!from_tty
14892 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14893 {
14894 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14895 if (is_tracepoint (b) && user_breakpoint_p (b))
14896 delete_breakpoint (b);
14897 }
14898 }
14899 else
14900 map_breakpoint_numbers
14901 (arg, [&] (breakpoint *b)
14902 {
14903 iterate_over_related_breakpoints (b, delete_breakpoint);
14904 });
14905 }
14906
14907 /* Helper function for trace_pass_command. */
14908
14909 static void
14910 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14911 {
14912 tp->pass_count = count;
14913 observer_notify_breakpoint_modified (tp);
14914 if (from_tty)
14915 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14916 tp->number, count);
14917 }
14918
14919 /* Set passcount for tracepoint.
14920
14921 First command argument is passcount, second is tracepoint number.
14922 If tracepoint number omitted, apply to most recently defined.
14923 Also accepts special argument "all". */
14924
14925 static void
14926 trace_pass_command (char *args, int from_tty)
14927 {
14928 struct tracepoint *t1;
14929 unsigned int count;
14930
14931 if (args == 0 || *args == 0)
14932 error (_("passcount command requires an "
14933 "argument (count + optional TP num)"));
14934
14935 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
14936
14937 args = skip_spaces (args);
14938 if (*args && strncasecmp (args, "all", 3) == 0)
14939 {
14940 struct breakpoint *b;
14941
14942 args += 3; /* Skip special argument "all". */
14943 if (*args)
14944 error (_("Junk at end of arguments."));
14945
14946 ALL_TRACEPOINTS (b)
14947 {
14948 t1 = (struct tracepoint *) b;
14949 trace_pass_set_count (t1, count, from_tty);
14950 }
14951 }
14952 else if (*args == '\0')
14953 {
14954 t1 = get_tracepoint_by_number (&args, NULL);
14955 if (t1)
14956 trace_pass_set_count (t1, count, from_tty);
14957 }
14958 else
14959 {
14960 number_or_range_parser parser (args);
14961 while (!parser.finished ())
14962 {
14963 t1 = get_tracepoint_by_number (&args, &parser);
14964 if (t1)
14965 trace_pass_set_count (t1, count, from_tty);
14966 }
14967 }
14968 }
14969
14970 struct tracepoint *
14971 get_tracepoint (int num)
14972 {
14973 struct breakpoint *t;
14974
14975 ALL_TRACEPOINTS (t)
14976 if (t->number == num)
14977 return (struct tracepoint *) t;
14978
14979 return NULL;
14980 }
14981
14982 /* Find the tracepoint with the given target-side number (which may be
14983 different from the tracepoint number after disconnecting and
14984 reconnecting). */
14985
14986 struct tracepoint *
14987 get_tracepoint_by_number_on_target (int num)
14988 {
14989 struct breakpoint *b;
14990
14991 ALL_TRACEPOINTS (b)
14992 {
14993 struct tracepoint *t = (struct tracepoint *) b;
14994
14995 if (t->number_on_target == num)
14996 return t;
14997 }
14998
14999 return NULL;
15000 }
15001
15002 /* Utility: parse a tracepoint number and look it up in the list.
15003 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15004 If the argument is missing, the most recent tracepoint
15005 (tracepoint_count) is returned. */
15006
15007 struct tracepoint *
15008 get_tracepoint_by_number (char **arg,
15009 number_or_range_parser *parser)
15010 {
15011 struct breakpoint *t;
15012 int tpnum;
15013 char *instring = arg == NULL ? NULL : *arg;
15014
15015 if (parser != NULL)
15016 {
15017 gdb_assert (!parser->finished ());
15018 tpnum = parser->get_number ();
15019 }
15020 else if (arg == NULL || *arg == NULL || ! **arg)
15021 tpnum = tracepoint_count;
15022 else
15023 tpnum = get_number (arg);
15024
15025 if (tpnum <= 0)
15026 {
15027 if (instring && *instring)
15028 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15029 instring);
15030 else
15031 printf_filtered (_("No previous tracepoint\n"));
15032 return NULL;
15033 }
15034
15035 ALL_TRACEPOINTS (t)
15036 if (t->number == tpnum)
15037 {
15038 return (struct tracepoint *) t;
15039 }
15040
15041 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15042 return NULL;
15043 }
15044
15045 void
15046 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15047 {
15048 if (b->thread != -1)
15049 fprintf_unfiltered (fp, " thread %d", b->thread);
15050
15051 if (b->task != 0)
15052 fprintf_unfiltered (fp, " task %d", b->task);
15053
15054 fprintf_unfiltered (fp, "\n");
15055 }
15056
15057 /* Save information on user settable breakpoints (watchpoints, etc) to
15058 a new script file named FILENAME. If FILTER is non-NULL, call it
15059 on each breakpoint and only include the ones for which it returns
15060 non-zero. */
15061
15062 static void
15063 save_breakpoints (const char *filename, int from_tty,
15064 int (*filter) (const struct breakpoint *))
15065 {
15066 struct breakpoint *tp;
15067 int any = 0;
15068 int extra_trace_bits = 0;
15069
15070 if (filename == 0 || *filename == 0)
15071 error (_("Argument required (file name in which to save)"));
15072
15073 /* See if we have anything to save. */
15074 ALL_BREAKPOINTS (tp)
15075 {
15076 /* Skip internal and momentary breakpoints. */
15077 if (!user_breakpoint_p (tp))
15078 continue;
15079
15080 /* If we have a filter, only save the breakpoints it accepts. */
15081 if (filter && !filter (tp))
15082 continue;
15083
15084 any = 1;
15085
15086 if (is_tracepoint (tp))
15087 {
15088 extra_trace_bits = 1;
15089
15090 /* We can stop searching. */
15091 break;
15092 }
15093 }
15094
15095 if (!any)
15096 {
15097 warning (_("Nothing to save."));
15098 return;
15099 }
15100
15101 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15102
15103 stdio_file fp;
15104
15105 if (!fp.open (expanded_filename.get (), "w"))
15106 error (_("Unable to open file '%s' for saving (%s)"),
15107 expanded_filename.get (), safe_strerror (errno));
15108
15109 if (extra_trace_bits)
15110 save_trace_state_variables (&fp);
15111
15112 ALL_BREAKPOINTS (tp)
15113 {
15114 /* Skip internal and momentary breakpoints. */
15115 if (!user_breakpoint_p (tp))
15116 continue;
15117
15118 /* If we have a filter, only save the breakpoints it accepts. */
15119 if (filter && !filter (tp))
15120 continue;
15121
15122 tp->ops->print_recreate (tp, &fp);
15123
15124 /* Note, we can't rely on tp->number for anything, as we can't
15125 assume the recreated breakpoint numbers will match. Use $bpnum
15126 instead. */
15127
15128 if (tp->cond_string)
15129 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15130
15131 if (tp->ignore_count)
15132 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15133
15134 if (tp->type != bp_dprintf && tp->commands)
15135 {
15136 fp.puts (" commands\n");
15137
15138 current_uiout->redirect (&fp);
15139 TRY
15140 {
15141 print_command_lines (current_uiout, tp->commands.get (), 2);
15142 }
15143 CATCH (ex, RETURN_MASK_ALL)
15144 {
15145 current_uiout->redirect (NULL);
15146 throw_exception (ex);
15147 }
15148 END_CATCH
15149
15150 current_uiout->redirect (NULL);
15151 fp.puts (" end\n");
15152 }
15153
15154 if (tp->enable_state == bp_disabled)
15155 fp.puts ("disable $bpnum\n");
15156
15157 /* If this is a multi-location breakpoint, check if the locations
15158 should be individually disabled. Watchpoint locations are
15159 special, and not user visible. */
15160 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15161 {
15162 struct bp_location *loc;
15163 int n = 1;
15164
15165 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15166 if (!loc->enabled)
15167 fp.printf ("disable $bpnum.%d\n", n);
15168 }
15169 }
15170
15171 if (extra_trace_bits && *default_collect)
15172 fp.printf ("set default-collect %s\n", default_collect);
15173
15174 if (from_tty)
15175 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15176 }
15177
15178 /* The `save breakpoints' command. */
15179
15180 static void
15181 save_breakpoints_command (const char *args, int from_tty)
15182 {
15183 save_breakpoints (args, from_tty, NULL);
15184 }
15185
15186 /* The `save tracepoints' command. */
15187
15188 static void
15189 save_tracepoints_command (const char *args, int from_tty)
15190 {
15191 save_breakpoints (args, from_tty, is_tracepoint);
15192 }
15193
15194 /* Create a vector of all tracepoints. */
15195
15196 VEC(breakpoint_p) *
15197 all_tracepoints (void)
15198 {
15199 VEC(breakpoint_p) *tp_vec = 0;
15200 struct breakpoint *tp;
15201
15202 ALL_TRACEPOINTS (tp)
15203 {
15204 VEC_safe_push (breakpoint_p, tp_vec, tp);
15205 }
15206
15207 return tp_vec;
15208 }
15209
15210 \f
15211 /* This help string is used to consolidate all the help string for specifying
15212 locations used by several commands. */
15213
15214 #define LOCATION_HELP_STRING \
15215 "Linespecs are colon-separated lists of location parameters, such as\n\
15216 source filename, function name, label name, and line number.\n\
15217 Example: To specify the start of a label named \"the_top\" in the\n\
15218 function \"fact\" in the file \"factorial.c\", use\n\
15219 \"factorial.c:fact:the_top\".\n\
15220 \n\
15221 Address locations begin with \"*\" and specify an exact address in the\n\
15222 program. Example: To specify the fourth byte past the start function\n\
15223 \"main\", use \"*main + 4\".\n\
15224 \n\
15225 Explicit locations are similar to linespecs but use an option/argument\n\
15226 syntax to specify location parameters.\n\
15227 Example: To specify the start of the label named \"the_top\" in the\n\
15228 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15229 -function fact -label the_top\".\n"
15230
15231 /* This help string is used for the break, hbreak, tbreak and thbreak
15232 commands. It is defined as a macro to prevent duplication.
15233 COMMAND should be a string constant containing the name of the
15234 command. */
15235
15236 #define BREAK_ARGS_HELP(command) \
15237 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15238 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15239 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15240 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15241 `-probe-dtrace' (for a DTrace probe).\n\
15242 LOCATION may be a linespec, address, or explicit location as described\n\
15243 below.\n\
15244 \n\
15245 With no LOCATION, uses current execution address of the selected\n\
15246 stack frame. This is useful for breaking on return to a stack frame.\n\
15247 \n\
15248 THREADNUM is the number from \"info threads\".\n\
15249 CONDITION is a boolean expression.\n\
15250 \n" LOCATION_HELP_STRING "\n\
15251 Multiple breakpoints at one place are permitted, and useful if their\n\
15252 conditions are different.\n\
15253 \n\
15254 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15255
15256 /* List of subcommands for "catch". */
15257 static struct cmd_list_element *catch_cmdlist;
15258
15259 /* List of subcommands for "tcatch". */
15260 static struct cmd_list_element *tcatch_cmdlist;
15261
15262 void
15263 add_catch_command (const char *name, const char *docstring,
15264 cmd_sfunc_ftype *sfunc,
15265 completer_ftype *completer,
15266 void *user_data_catch,
15267 void *user_data_tcatch)
15268 {
15269 struct cmd_list_element *command;
15270
15271 command = add_cmd (name, class_breakpoint, docstring,
15272 &catch_cmdlist);
15273 set_cmd_sfunc (command, sfunc);
15274 set_cmd_context (command, user_data_catch);
15275 set_cmd_completer (command, completer);
15276
15277 command = add_cmd (name, class_breakpoint, docstring,
15278 &tcatch_cmdlist);
15279 set_cmd_sfunc (command, sfunc);
15280 set_cmd_context (command, user_data_tcatch);
15281 set_cmd_completer (command, completer);
15282 }
15283
15284 static void
15285 save_command (char *arg, int from_tty)
15286 {
15287 printf_unfiltered (_("\"save\" must be followed by "
15288 "the name of a save subcommand.\n"));
15289 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15290 }
15291
15292 struct breakpoint *
15293 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15294 void *data)
15295 {
15296 struct breakpoint *b, *b_tmp;
15297
15298 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15299 {
15300 if ((*callback) (b, data))
15301 return b;
15302 }
15303
15304 return NULL;
15305 }
15306
15307 /* Zero if any of the breakpoint's locations could be a location where
15308 functions have been inlined, nonzero otherwise. */
15309
15310 static int
15311 is_non_inline_function (struct breakpoint *b)
15312 {
15313 /* The shared library event breakpoint is set on the address of a
15314 non-inline function. */
15315 if (b->type == bp_shlib_event)
15316 return 1;
15317
15318 return 0;
15319 }
15320
15321 /* Nonzero if the specified PC cannot be a location where functions
15322 have been inlined. */
15323
15324 int
15325 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15326 const struct target_waitstatus *ws)
15327 {
15328 struct breakpoint *b;
15329 struct bp_location *bl;
15330
15331 ALL_BREAKPOINTS (b)
15332 {
15333 if (!is_non_inline_function (b))
15334 continue;
15335
15336 for (bl = b->loc; bl != NULL; bl = bl->next)
15337 {
15338 if (!bl->shlib_disabled
15339 && bpstat_check_location (bl, aspace, pc, ws))
15340 return 1;
15341 }
15342 }
15343
15344 return 0;
15345 }
15346
15347 /* Remove any references to OBJFILE which is going to be freed. */
15348
15349 void
15350 breakpoint_free_objfile (struct objfile *objfile)
15351 {
15352 struct bp_location **locp, *loc;
15353
15354 ALL_BP_LOCATIONS (loc, locp)
15355 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15356 loc->symtab = NULL;
15357 }
15358
15359 void
15360 initialize_breakpoint_ops (void)
15361 {
15362 static int initialized = 0;
15363
15364 struct breakpoint_ops *ops;
15365
15366 if (initialized)
15367 return;
15368 initialized = 1;
15369
15370 /* The breakpoint_ops structure to be inherit by all kinds of
15371 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15372 internal and momentary breakpoints, etc.). */
15373 ops = &bkpt_base_breakpoint_ops;
15374 *ops = base_breakpoint_ops;
15375 ops->re_set = bkpt_re_set;
15376 ops->insert_location = bkpt_insert_location;
15377 ops->remove_location = bkpt_remove_location;
15378 ops->breakpoint_hit = bkpt_breakpoint_hit;
15379 ops->create_sals_from_location = bkpt_create_sals_from_location;
15380 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15381 ops->decode_location = bkpt_decode_location;
15382
15383 /* The breakpoint_ops structure to be used in regular breakpoints. */
15384 ops = &bkpt_breakpoint_ops;
15385 *ops = bkpt_base_breakpoint_ops;
15386 ops->re_set = bkpt_re_set;
15387 ops->resources_needed = bkpt_resources_needed;
15388 ops->print_it = bkpt_print_it;
15389 ops->print_mention = bkpt_print_mention;
15390 ops->print_recreate = bkpt_print_recreate;
15391
15392 /* Ranged breakpoints. */
15393 ops = &ranged_breakpoint_ops;
15394 *ops = bkpt_breakpoint_ops;
15395 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15396 ops->resources_needed = resources_needed_ranged_breakpoint;
15397 ops->print_it = print_it_ranged_breakpoint;
15398 ops->print_one = print_one_ranged_breakpoint;
15399 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15400 ops->print_mention = print_mention_ranged_breakpoint;
15401 ops->print_recreate = print_recreate_ranged_breakpoint;
15402
15403 /* Internal breakpoints. */
15404 ops = &internal_breakpoint_ops;
15405 *ops = bkpt_base_breakpoint_ops;
15406 ops->re_set = internal_bkpt_re_set;
15407 ops->check_status = internal_bkpt_check_status;
15408 ops->print_it = internal_bkpt_print_it;
15409 ops->print_mention = internal_bkpt_print_mention;
15410
15411 /* Momentary breakpoints. */
15412 ops = &momentary_breakpoint_ops;
15413 *ops = bkpt_base_breakpoint_ops;
15414 ops->re_set = momentary_bkpt_re_set;
15415 ops->check_status = momentary_bkpt_check_status;
15416 ops->print_it = momentary_bkpt_print_it;
15417 ops->print_mention = momentary_bkpt_print_mention;
15418
15419 /* Probe breakpoints. */
15420 ops = &bkpt_probe_breakpoint_ops;
15421 *ops = bkpt_breakpoint_ops;
15422 ops->insert_location = bkpt_probe_insert_location;
15423 ops->remove_location = bkpt_probe_remove_location;
15424 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15425 ops->decode_location = bkpt_probe_decode_location;
15426
15427 /* Watchpoints. */
15428 ops = &watchpoint_breakpoint_ops;
15429 *ops = base_breakpoint_ops;
15430 ops->re_set = re_set_watchpoint;
15431 ops->insert_location = insert_watchpoint;
15432 ops->remove_location = remove_watchpoint;
15433 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15434 ops->check_status = check_status_watchpoint;
15435 ops->resources_needed = resources_needed_watchpoint;
15436 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15437 ops->print_it = print_it_watchpoint;
15438 ops->print_mention = print_mention_watchpoint;
15439 ops->print_recreate = print_recreate_watchpoint;
15440 ops->explains_signal = explains_signal_watchpoint;
15441
15442 /* Masked watchpoints. */
15443 ops = &masked_watchpoint_breakpoint_ops;
15444 *ops = watchpoint_breakpoint_ops;
15445 ops->insert_location = insert_masked_watchpoint;
15446 ops->remove_location = remove_masked_watchpoint;
15447 ops->resources_needed = resources_needed_masked_watchpoint;
15448 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15449 ops->print_it = print_it_masked_watchpoint;
15450 ops->print_one_detail = print_one_detail_masked_watchpoint;
15451 ops->print_mention = print_mention_masked_watchpoint;
15452 ops->print_recreate = print_recreate_masked_watchpoint;
15453
15454 /* Tracepoints. */
15455 ops = &tracepoint_breakpoint_ops;
15456 *ops = base_breakpoint_ops;
15457 ops->re_set = tracepoint_re_set;
15458 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15459 ops->print_one_detail = tracepoint_print_one_detail;
15460 ops->print_mention = tracepoint_print_mention;
15461 ops->print_recreate = tracepoint_print_recreate;
15462 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15463 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15464 ops->decode_location = tracepoint_decode_location;
15465
15466 /* Probe tracepoints. */
15467 ops = &tracepoint_probe_breakpoint_ops;
15468 *ops = tracepoint_breakpoint_ops;
15469 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15470 ops->decode_location = tracepoint_probe_decode_location;
15471
15472 /* Static tracepoints with marker (`-m'). */
15473 ops = &strace_marker_breakpoint_ops;
15474 *ops = tracepoint_breakpoint_ops;
15475 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15476 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15477 ops->decode_location = strace_marker_decode_location;
15478
15479 /* Fork catchpoints. */
15480 ops = &catch_fork_breakpoint_ops;
15481 *ops = base_breakpoint_ops;
15482 ops->insert_location = insert_catch_fork;
15483 ops->remove_location = remove_catch_fork;
15484 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15485 ops->print_it = print_it_catch_fork;
15486 ops->print_one = print_one_catch_fork;
15487 ops->print_mention = print_mention_catch_fork;
15488 ops->print_recreate = print_recreate_catch_fork;
15489
15490 /* Vfork catchpoints. */
15491 ops = &catch_vfork_breakpoint_ops;
15492 *ops = base_breakpoint_ops;
15493 ops->insert_location = insert_catch_vfork;
15494 ops->remove_location = remove_catch_vfork;
15495 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15496 ops->print_it = print_it_catch_vfork;
15497 ops->print_one = print_one_catch_vfork;
15498 ops->print_mention = print_mention_catch_vfork;
15499 ops->print_recreate = print_recreate_catch_vfork;
15500
15501 /* Exec catchpoints. */
15502 ops = &catch_exec_breakpoint_ops;
15503 *ops = base_breakpoint_ops;
15504 ops->insert_location = insert_catch_exec;
15505 ops->remove_location = remove_catch_exec;
15506 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15507 ops->print_it = print_it_catch_exec;
15508 ops->print_one = print_one_catch_exec;
15509 ops->print_mention = print_mention_catch_exec;
15510 ops->print_recreate = print_recreate_catch_exec;
15511
15512 /* Solib-related catchpoints. */
15513 ops = &catch_solib_breakpoint_ops;
15514 *ops = base_breakpoint_ops;
15515 ops->insert_location = insert_catch_solib;
15516 ops->remove_location = remove_catch_solib;
15517 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15518 ops->check_status = check_status_catch_solib;
15519 ops->print_it = print_it_catch_solib;
15520 ops->print_one = print_one_catch_solib;
15521 ops->print_mention = print_mention_catch_solib;
15522 ops->print_recreate = print_recreate_catch_solib;
15523
15524 ops = &dprintf_breakpoint_ops;
15525 *ops = bkpt_base_breakpoint_ops;
15526 ops->re_set = dprintf_re_set;
15527 ops->resources_needed = bkpt_resources_needed;
15528 ops->print_it = bkpt_print_it;
15529 ops->print_mention = bkpt_print_mention;
15530 ops->print_recreate = dprintf_print_recreate;
15531 ops->after_condition_true = dprintf_after_condition_true;
15532 ops->breakpoint_hit = dprintf_breakpoint_hit;
15533 }
15534
15535 /* Chain containing all defined "enable breakpoint" subcommands. */
15536
15537 static struct cmd_list_element *enablebreaklist = NULL;
15538
15539 void
15540 _initialize_breakpoint (void)
15541 {
15542 struct cmd_list_element *c;
15543
15544 initialize_breakpoint_ops ();
15545
15546 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15547 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15548 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15549
15550 breakpoint_objfile_key
15551 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15552
15553 breakpoint_chain = 0;
15554 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15555 before a breakpoint is set. */
15556 breakpoint_count = 0;
15557
15558 tracepoint_count = 0;
15559
15560 add_com ("ignore", class_breakpoint, ignore_command, _("\
15561 Set ignore-count of breakpoint number N to COUNT.\n\
15562 Usage is `ignore N COUNT'."));
15563
15564 add_com ("commands", class_breakpoint, commands_command, _("\
15565 Set commands to be executed when the given breakpoints are hit.\n\
15566 Give a space-separated breakpoint list as argument after \"commands\".\n\
15567 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15568 (e.g. `5-7').\n\
15569 With no argument, the targeted breakpoint is the last one set.\n\
15570 The commands themselves follow starting on the next line.\n\
15571 Type a line containing \"end\" to indicate the end of them.\n\
15572 Give \"silent\" as the first line to make the breakpoint silent;\n\
15573 then no output is printed when it is hit, except what the commands print."));
15574
15575 c = add_com ("condition", class_breakpoint, condition_command, _("\
15576 Specify breakpoint number N to break only if COND is true.\n\
15577 Usage is `condition N COND', where N is an integer and COND is an\n\
15578 expression to be evaluated whenever breakpoint N is reached."));
15579 set_cmd_completer (c, condition_completer);
15580
15581 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15582 Set a temporary breakpoint.\n\
15583 Like \"break\" except the breakpoint is only temporary,\n\
15584 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15585 by using \"enable delete\" on the breakpoint number.\n\
15586 \n"
15587 BREAK_ARGS_HELP ("tbreak")));
15588 set_cmd_completer (c, location_completer);
15589
15590 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15591 Set a hardware assisted breakpoint.\n\
15592 Like \"break\" except the breakpoint requires hardware support,\n\
15593 some target hardware may not have this support.\n\
15594 \n"
15595 BREAK_ARGS_HELP ("hbreak")));
15596 set_cmd_completer (c, location_completer);
15597
15598 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15599 Set a temporary hardware assisted breakpoint.\n\
15600 Like \"hbreak\" except the breakpoint is only temporary,\n\
15601 so it will be deleted when hit.\n\
15602 \n"
15603 BREAK_ARGS_HELP ("thbreak")));
15604 set_cmd_completer (c, location_completer);
15605
15606 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15607 Enable some breakpoints.\n\
15608 Give breakpoint numbers (separated by spaces) as arguments.\n\
15609 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15610 This is used to cancel the effect of the \"disable\" command.\n\
15611 With a subcommand you can enable temporarily."),
15612 &enablelist, "enable ", 1, &cmdlist);
15613
15614 add_com_alias ("en", "enable", class_breakpoint, 1);
15615
15616 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15617 Enable some breakpoints.\n\
15618 Give breakpoint numbers (separated by spaces) as arguments.\n\
15619 This is used to cancel the effect of the \"disable\" command.\n\
15620 May be abbreviated to simply \"enable\".\n"),
15621 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15622
15623 add_cmd ("once", no_class, enable_once_command, _("\
15624 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15625 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15626 &enablebreaklist);
15627
15628 add_cmd ("delete", no_class, enable_delete_command, _("\
15629 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15630 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15631 &enablebreaklist);
15632
15633 add_cmd ("count", no_class, enable_count_command, _("\
15634 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15635 If a breakpoint is hit while enabled in this fashion,\n\
15636 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15637 &enablebreaklist);
15638
15639 add_cmd ("delete", no_class, enable_delete_command, _("\
15640 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15641 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15642 &enablelist);
15643
15644 add_cmd ("once", no_class, enable_once_command, _("\
15645 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15646 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15647 &enablelist);
15648
15649 add_cmd ("count", no_class, enable_count_command, _("\
15650 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15651 If a breakpoint is hit while enabled in this fashion,\n\
15652 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15653 &enablelist);
15654
15655 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15656 Disable some breakpoints.\n\
15657 Arguments are breakpoint numbers with spaces in between.\n\
15658 To disable all breakpoints, give no argument.\n\
15659 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15660 &disablelist, "disable ", 1, &cmdlist);
15661 add_com_alias ("dis", "disable", class_breakpoint, 1);
15662 add_com_alias ("disa", "disable", class_breakpoint, 1);
15663
15664 add_cmd ("breakpoints", class_alias, disable_command, _("\
15665 Disable some breakpoints.\n\
15666 Arguments are breakpoint numbers with spaces in between.\n\
15667 To disable all breakpoints, give no argument.\n\
15668 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15669 This command may be abbreviated \"disable\"."),
15670 &disablelist);
15671
15672 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15673 Delete some breakpoints or auto-display expressions.\n\
15674 Arguments are breakpoint numbers with spaces in between.\n\
15675 To delete all breakpoints, give no argument.\n\
15676 \n\
15677 Also a prefix command for deletion of other GDB objects.\n\
15678 The \"unset\" command is also an alias for \"delete\"."),
15679 &deletelist, "delete ", 1, &cmdlist);
15680 add_com_alias ("d", "delete", class_breakpoint, 1);
15681 add_com_alias ("del", "delete", class_breakpoint, 1);
15682
15683 add_cmd ("breakpoints", class_alias, delete_command, _("\
15684 Delete some breakpoints or auto-display expressions.\n\
15685 Arguments are breakpoint numbers with spaces in between.\n\
15686 To delete all breakpoints, give no argument.\n\
15687 This command may be abbreviated \"delete\"."),
15688 &deletelist);
15689
15690 add_com ("clear", class_breakpoint, clear_command, _("\
15691 Clear breakpoint at specified location.\n\
15692 Argument may be a linespec, explicit, or address location as described below.\n\
15693 \n\
15694 With no argument, clears all breakpoints in the line that the selected frame\n\
15695 is executing in.\n"
15696 "\n" LOCATION_HELP_STRING "\n\
15697 See also the \"delete\" command which clears breakpoints by number."));
15698 add_com_alias ("cl", "clear", class_breakpoint, 1);
15699
15700 c = add_com ("break", class_breakpoint, break_command, _("\
15701 Set breakpoint at specified location.\n"
15702 BREAK_ARGS_HELP ("break")));
15703 set_cmd_completer (c, location_completer);
15704
15705 add_com_alias ("b", "break", class_run, 1);
15706 add_com_alias ("br", "break", class_run, 1);
15707 add_com_alias ("bre", "break", class_run, 1);
15708 add_com_alias ("brea", "break", class_run, 1);
15709
15710 if (dbx_commands)
15711 {
15712 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15713 Break in function/address or break at a line in the current file."),
15714 &stoplist, "stop ", 1, &cmdlist);
15715 add_cmd ("in", class_breakpoint, stopin_command,
15716 _("Break in function or address."), &stoplist);
15717 add_cmd ("at", class_breakpoint, stopat_command,
15718 _("Break at a line in the current file."), &stoplist);
15719 add_com ("status", class_info, info_breakpoints_command, _("\
15720 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15721 The \"Type\" column indicates one of:\n\
15722 \tbreakpoint - normal breakpoint\n\
15723 \twatchpoint - watchpoint\n\
15724 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15725 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15726 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15727 address and file/line number respectively.\n\
15728 \n\
15729 Convenience variable \"$_\" and default examine address for \"x\"\n\
15730 are set to the address of the last breakpoint listed unless the command\n\
15731 is prefixed with \"server \".\n\n\
15732 Convenience variable \"$bpnum\" contains the number of the last\n\
15733 breakpoint set."));
15734 }
15735
15736 add_info ("breakpoints", info_breakpoints_command, _("\
15737 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15738 The \"Type\" column indicates one of:\n\
15739 \tbreakpoint - normal breakpoint\n\
15740 \twatchpoint - watchpoint\n\
15741 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15742 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15743 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15744 address and file/line number respectively.\n\
15745 \n\
15746 Convenience variable \"$_\" and default examine address for \"x\"\n\
15747 are set to the address of the last breakpoint listed unless the command\n\
15748 is prefixed with \"server \".\n\n\
15749 Convenience variable \"$bpnum\" contains the number of the last\n\
15750 breakpoint set."));
15751
15752 add_info_alias ("b", "breakpoints", 1);
15753
15754 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15755 Status of all breakpoints, or breakpoint number NUMBER.\n\
15756 The \"Type\" column indicates one of:\n\
15757 \tbreakpoint - normal breakpoint\n\
15758 \twatchpoint - watchpoint\n\
15759 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15760 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15761 \tuntil - internal breakpoint used by the \"until\" command\n\
15762 \tfinish - internal breakpoint used by the \"finish\" command\n\
15763 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15764 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15765 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15766 address and file/line number respectively.\n\
15767 \n\
15768 Convenience variable \"$_\" and default examine address for \"x\"\n\
15769 are set to the address of the last breakpoint listed unless the command\n\
15770 is prefixed with \"server \".\n\n\
15771 Convenience variable \"$bpnum\" contains the number of the last\n\
15772 breakpoint set."),
15773 &maintenanceinfolist);
15774
15775 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15776 Set catchpoints to catch events."),
15777 &catch_cmdlist, "catch ",
15778 0/*allow-unknown*/, &cmdlist);
15779
15780 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15781 Set temporary catchpoints to catch events."),
15782 &tcatch_cmdlist, "tcatch ",
15783 0/*allow-unknown*/, &cmdlist);
15784
15785 add_catch_command ("fork", _("Catch calls to fork."),
15786 catch_fork_command_1,
15787 NULL,
15788 (void *) (uintptr_t) catch_fork_permanent,
15789 (void *) (uintptr_t) catch_fork_temporary);
15790 add_catch_command ("vfork", _("Catch calls to vfork."),
15791 catch_fork_command_1,
15792 NULL,
15793 (void *) (uintptr_t) catch_vfork_permanent,
15794 (void *) (uintptr_t) catch_vfork_temporary);
15795 add_catch_command ("exec", _("Catch calls to exec."),
15796 catch_exec_command_1,
15797 NULL,
15798 CATCH_PERMANENT,
15799 CATCH_TEMPORARY);
15800 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15801 Usage: catch load [REGEX]\n\
15802 If REGEX is given, only stop for libraries matching the regular expression."),
15803 catch_load_command_1,
15804 NULL,
15805 CATCH_PERMANENT,
15806 CATCH_TEMPORARY);
15807 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15808 Usage: catch unload [REGEX]\n\
15809 If REGEX is given, only stop for libraries matching the regular expression."),
15810 catch_unload_command_1,
15811 NULL,
15812 CATCH_PERMANENT,
15813 CATCH_TEMPORARY);
15814
15815 c = add_com ("watch", class_breakpoint, watch_command, _("\
15816 Set a watchpoint for an expression.\n\
15817 Usage: watch [-l|-location] EXPRESSION\n\
15818 A watchpoint stops execution of your program whenever the value of\n\
15819 an expression changes.\n\
15820 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15821 the memory to which it refers."));
15822 set_cmd_completer (c, expression_completer);
15823
15824 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15825 Set a read watchpoint for an expression.\n\
15826 Usage: rwatch [-l|-location] EXPRESSION\n\
15827 A watchpoint stops execution of your program whenever the value of\n\
15828 an expression is read.\n\
15829 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15830 the memory to which it refers."));
15831 set_cmd_completer (c, expression_completer);
15832
15833 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15834 Set a watchpoint for an expression.\n\
15835 Usage: awatch [-l|-location] EXPRESSION\n\
15836 A watchpoint stops execution of your program whenever the value of\n\
15837 an expression is either read or written.\n\
15838 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15839 the memory to which it refers."));
15840 set_cmd_completer (c, expression_completer);
15841
15842 add_info ("watchpoints", info_watchpoints_command, _("\
15843 Status of specified watchpoints (all watchpoints if no argument)."));
15844
15845 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15846 respond to changes - contrary to the description. */
15847 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15848 &can_use_hw_watchpoints, _("\
15849 Set debugger's willingness to use watchpoint hardware."), _("\
15850 Show debugger's willingness to use watchpoint hardware."), _("\
15851 If zero, gdb will not use hardware for new watchpoints, even if\n\
15852 such is available. (However, any hardware watchpoints that were\n\
15853 created before setting this to nonzero, will continue to use watchpoint\n\
15854 hardware.)"),
15855 NULL,
15856 show_can_use_hw_watchpoints,
15857 &setlist, &showlist);
15858
15859 can_use_hw_watchpoints = 1;
15860
15861 /* Tracepoint manipulation commands. */
15862
15863 c = add_com ("trace", class_breakpoint, trace_command, _("\
15864 Set a tracepoint at specified location.\n\
15865 \n"
15866 BREAK_ARGS_HELP ("trace") "\n\
15867 Do \"help tracepoints\" for info on other tracepoint commands."));
15868 set_cmd_completer (c, location_completer);
15869
15870 add_com_alias ("tp", "trace", class_alias, 0);
15871 add_com_alias ("tr", "trace", class_alias, 1);
15872 add_com_alias ("tra", "trace", class_alias, 1);
15873 add_com_alias ("trac", "trace", class_alias, 1);
15874
15875 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15876 Set a fast tracepoint at specified location.\n\
15877 \n"
15878 BREAK_ARGS_HELP ("ftrace") "\n\
15879 Do \"help tracepoints\" for info on other tracepoint commands."));
15880 set_cmd_completer (c, location_completer);
15881
15882 c = add_com ("strace", class_breakpoint, strace_command, _("\
15883 Set a static tracepoint at location or marker.\n\
15884 \n\
15885 strace [LOCATION] [if CONDITION]\n\
15886 LOCATION may be a linespec, explicit, or address location (described below) \n\
15887 or -m MARKER_ID.\n\n\
15888 If a marker id is specified, probe the marker with that name. With\n\
15889 no LOCATION, uses current execution address of the selected stack frame.\n\
15890 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15891 This collects arbitrary user data passed in the probe point call to the\n\
15892 tracing library. You can inspect it when analyzing the trace buffer,\n\
15893 by printing the $_sdata variable like any other convenience variable.\n\
15894 \n\
15895 CONDITION is a boolean expression.\n\
15896 \n" LOCATION_HELP_STRING "\n\
15897 Multiple tracepoints at one place are permitted, and useful if their\n\
15898 conditions are different.\n\
15899 \n\
15900 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15901 Do \"help tracepoints\" for info on other tracepoint commands."));
15902 set_cmd_completer (c, location_completer);
15903
15904 add_info ("tracepoints", info_tracepoints_command, _("\
15905 Status of specified tracepoints (all tracepoints if no argument).\n\
15906 Convenience variable \"$tpnum\" contains the number of the\n\
15907 last tracepoint set."));
15908
15909 add_info_alias ("tp", "tracepoints", 1);
15910
15911 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15912 Delete specified tracepoints.\n\
15913 Arguments are tracepoint numbers, separated by spaces.\n\
15914 No argument means delete all tracepoints."),
15915 &deletelist);
15916 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15917
15918 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15919 Disable specified tracepoints.\n\
15920 Arguments are tracepoint numbers, separated by spaces.\n\
15921 No argument means disable all tracepoints."),
15922 &disablelist);
15923 deprecate_cmd (c, "disable");
15924
15925 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15926 Enable specified tracepoints.\n\
15927 Arguments are tracepoint numbers, separated by spaces.\n\
15928 No argument means enable all tracepoints."),
15929 &enablelist);
15930 deprecate_cmd (c, "enable");
15931
15932 add_com ("passcount", class_trace, trace_pass_command, _("\
15933 Set the passcount for a tracepoint.\n\
15934 The trace will end when the tracepoint has been passed 'count' times.\n\
15935 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15936 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15937
15938 add_prefix_cmd ("save", class_breakpoint, save_command,
15939 _("Save breakpoint definitions as a script."),
15940 &save_cmdlist, "save ",
15941 0/*allow-unknown*/, &cmdlist);
15942
15943 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15944 Save current breakpoint definitions as a script.\n\
15945 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15946 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15947 session to restore them."),
15948 &save_cmdlist);
15949 set_cmd_completer (c, filename_completer);
15950
15951 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15952 Save current tracepoint definitions as a script.\n\
15953 Use the 'source' command in another debug session to restore them."),
15954 &save_cmdlist);
15955 set_cmd_completer (c, filename_completer);
15956
15957 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15958 deprecate_cmd (c, "save tracepoints");
15959
15960 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
15961 Breakpoint specific settings\n\
15962 Configure various breakpoint-specific variables such as\n\
15963 pending breakpoint behavior"),
15964 &breakpoint_set_cmdlist, "set breakpoint ",
15965 0/*allow-unknown*/, &setlist);
15966 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
15967 Breakpoint specific settings\n\
15968 Configure various breakpoint-specific variables such as\n\
15969 pending breakpoint behavior"),
15970 &breakpoint_show_cmdlist, "show breakpoint ",
15971 0/*allow-unknown*/, &showlist);
15972
15973 add_setshow_auto_boolean_cmd ("pending", no_class,
15974 &pending_break_support, _("\
15975 Set debugger's behavior regarding pending breakpoints."), _("\
15976 Show debugger's behavior regarding pending breakpoints."), _("\
15977 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15978 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15979 an error. If auto, an unrecognized breakpoint location results in a\n\
15980 user-query to see if a pending breakpoint should be created."),
15981 NULL,
15982 show_pending_break_support,
15983 &breakpoint_set_cmdlist,
15984 &breakpoint_show_cmdlist);
15985
15986 pending_break_support = AUTO_BOOLEAN_AUTO;
15987
15988 add_setshow_boolean_cmd ("auto-hw", no_class,
15989 &automatic_hardware_breakpoints, _("\
15990 Set automatic usage of hardware breakpoints."), _("\
15991 Show automatic usage of hardware breakpoints."), _("\
15992 If set, the debugger will automatically use hardware breakpoints for\n\
15993 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15994 a warning will be emitted for such breakpoints."),
15995 NULL,
15996 show_automatic_hardware_breakpoints,
15997 &breakpoint_set_cmdlist,
15998 &breakpoint_show_cmdlist);
15999
16000 add_setshow_boolean_cmd ("always-inserted", class_support,
16001 &always_inserted_mode, _("\
16002 Set mode for inserting breakpoints."), _("\
16003 Show mode for inserting breakpoints."), _("\
16004 When this mode is on, breakpoints are inserted immediately as soon as\n\
16005 they're created, kept inserted even when execution stops, and removed\n\
16006 only when the user deletes them. When this mode is off (the default),\n\
16007 breakpoints are inserted only when execution continues, and removed\n\
16008 when execution stops."),
16009 NULL,
16010 &show_always_inserted_mode,
16011 &breakpoint_set_cmdlist,
16012 &breakpoint_show_cmdlist);
16013
16014 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16015 condition_evaluation_enums,
16016 &condition_evaluation_mode_1, _("\
16017 Set mode of breakpoint condition evaluation."), _("\
16018 Show mode of breakpoint condition evaluation."), _("\
16019 When this is set to \"host\", breakpoint conditions will be\n\
16020 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16021 breakpoint conditions will be downloaded to the target (if the target\n\
16022 supports such feature) and conditions will be evaluated on the target's side.\n\
16023 If this is set to \"auto\" (default), this will be automatically set to\n\
16024 \"target\" if it supports condition evaluation, otherwise it will\n\
16025 be set to \"gdb\""),
16026 &set_condition_evaluation_mode,
16027 &show_condition_evaluation_mode,
16028 &breakpoint_set_cmdlist,
16029 &breakpoint_show_cmdlist);
16030
16031 add_com ("break-range", class_breakpoint, break_range_command, _("\
16032 Set a breakpoint for an address range.\n\
16033 break-range START-LOCATION, END-LOCATION\n\
16034 where START-LOCATION and END-LOCATION can be one of the following:\n\
16035 LINENUM, for that line in the current file,\n\
16036 FILE:LINENUM, for that line in that file,\n\
16037 +OFFSET, for that number of lines after the current line\n\
16038 or the start of the range\n\
16039 FUNCTION, for the first line in that function,\n\
16040 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16041 *ADDRESS, for the instruction at that address.\n\
16042 \n\
16043 The breakpoint will stop execution of the inferior whenever it executes\n\
16044 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16045 range (including START-LOCATION and END-LOCATION)."));
16046
16047 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16048 Set a dynamic printf at specified location.\n\
16049 dprintf location,format string,arg1,arg2,...\n\
16050 location may be a linespec, explicit, or address location.\n"
16051 "\n" LOCATION_HELP_STRING));
16052 set_cmd_completer (c, location_completer);
16053
16054 add_setshow_enum_cmd ("dprintf-style", class_support,
16055 dprintf_style_enums, &dprintf_style, _("\
16056 Set the style of usage for dynamic printf."), _("\
16057 Show the style of usage for dynamic printf."), _("\
16058 This setting chooses how GDB will do a dynamic printf.\n\
16059 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16060 console, as with the \"printf\" command.\n\
16061 If the value is \"call\", the print is done by calling a function in your\n\
16062 program; by default printf(), but you can choose a different function or\n\
16063 output stream by setting dprintf-function and dprintf-channel."),
16064 update_dprintf_commands, NULL,
16065 &setlist, &showlist);
16066
16067 dprintf_function = xstrdup ("printf");
16068 add_setshow_string_cmd ("dprintf-function", class_support,
16069 &dprintf_function, _("\
16070 Set the function to use for dynamic printf"), _("\
16071 Show the function to use for dynamic printf"), NULL,
16072 update_dprintf_commands, NULL,
16073 &setlist, &showlist);
16074
16075 dprintf_channel = xstrdup ("");
16076 add_setshow_string_cmd ("dprintf-channel", class_support,
16077 &dprintf_channel, _("\
16078 Set the channel to use for dynamic printf"), _("\
16079 Show the channel to use for dynamic printf"), NULL,
16080 update_dprintf_commands, NULL,
16081 &setlist, &showlist);
16082
16083 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16084 &disconnected_dprintf, _("\
16085 Set whether dprintf continues after GDB disconnects."), _("\
16086 Show whether dprintf continues after GDB disconnects."), _("\
16087 Use this to let dprintf commands continue to hit and produce output\n\
16088 even if GDB disconnects or detaches from the target."),
16089 NULL,
16090 NULL,
16091 &setlist, &showlist);
16092
16093 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16094 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16095 (target agent only) This is useful for formatted output in user-defined commands."));
16096
16097 automatic_hardware_breakpoints = 1;
16098
16099 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16100 observer_attach_thread_exit (remove_threaded_breakpoints);
16101 }
This page took 0.537313 seconds and 5 git commands to generate.