Constify some linespec functions
[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 "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observer.h"
54 #include "memattr.h"
55 #include "ada-lang.h"
56 #include "top.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "parser-defs.h"
60 #include "gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "continuations.h"
64 #include "stack.h"
65 #include "skip.h"
66 #include "ax-gdb.h"
67 #include "dummy-frame.h"
68 #include "interps.h"
69 #include "format.h"
70 #include "thread-fsm.h"
71 #include "tid-parse.h"
72
73 /* readline include files */
74 #include "readline/readline.h"
75 #include "readline/history.h"
76
77 /* readline defines this. */
78 #undef savestring
79
80 #include "mi/mi-common.h"
81 #include "extension.h"
82 #include <algorithm>
83 #include "progspace-and-thread.h"
84 #include "common/array-view.h"
85 #include "common/gdb_optional.h"
86
87 /* Enums for exception-handling support. */
88 enum exception_event_kind
89 {
90 EX_EVENT_THROW,
91 EX_EVENT_RETHROW,
92 EX_EVENT_CATCH
93 };
94
95 /* Prototypes for local functions. */
96
97 static void enable_delete_command (char *, int);
98
99 static void enable_once_command (char *, int);
100
101 static void enable_count_command (char *, int);
102
103 static void disable_command (char *, int);
104
105 static void enable_command (char *, int);
106
107 static void map_breakpoint_numbers (const char *,
108 gdb::function_view<void (breakpoint *)>);
109
110 static void ignore_command (char *, int);
111
112 static int breakpoint_re_set_one (void *);
113
114 static void breakpoint_re_set_default (struct breakpoint *);
115
116 static void
117 create_sals_from_location_default (const struct event_location *location,
118 struct linespec_result *canonical,
119 enum bptype type_wanted);
120
121 static void create_breakpoints_sal_default (struct gdbarch *,
122 struct linespec_result *,
123 gdb::unique_xmalloc_ptr<char>,
124 gdb::unique_xmalloc_ptr<char>,
125 enum bptype,
126 enum bpdisp, int, int,
127 int,
128 const struct breakpoint_ops *,
129 int, int, int, unsigned);
130
131 static std::vector<symtab_and_line> decode_location_default
132 (struct breakpoint *b, const struct event_location *location,
133 struct program_space *search_pspace);
134
135 static void clear_command (char *, int);
136
137 static void catch_command (char *, int);
138
139 static int can_use_hardware_watchpoint (struct value *);
140
141 static void mention (struct breakpoint *);
142
143 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
144 enum bptype,
145 const struct breakpoint_ops *);
146 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
147 const struct symtab_and_line *);
148
149 /* This function is used in gdbtk sources and thus can not be made
150 static. */
151 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
152 struct symtab_and_line,
153 enum bptype,
154 const struct breakpoint_ops *);
155
156 static struct breakpoint *
157 momentary_breakpoint_from_master (struct breakpoint *orig,
158 enum bptype type,
159 const struct breakpoint_ops *ops,
160 int loc_enabled);
161
162 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
163
164 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
165 CORE_ADDR bpaddr,
166 enum bptype bptype);
167
168 static void describe_other_breakpoints (struct gdbarch *,
169 struct program_space *, CORE_ADDR,
170 struct obj_section *, int);
171
172 static int watchpoint_locations_match (struct bp_location *loc1,
173 struct bp_location *loc2);
174
175 static int breakpoint_location_address_match (struct bp_location *bl,
176 struct address_space *aspace,
177 CORE_ADDR addr);
178
179 static int breakpoint_location_address_range_overlap (struct bp_location *,
180 struct address_space *,
181 CORE_ADDR, int);
182
183 static void info_breakpoints_command (char *, int);
184
185 static void info_watchpoints_command (char *, int);
186
187 static int breakpoint_1 (char *, int,
188 int (*) (const struct breakpoint *));
189
190 static int breakpoint_cond_eval (void *);
191
192 static void cleanup_executing_breakpoints (void *);
193
194 static void commands_command (char *, int);
195
196 static void condition_command (char *, int);
197
198 static int remove_breakpoint (struct bp_location *);
199 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
200
201 static enum print_stop_action print_bp_stop_message (bpstat bs);
202
203 static int watchpoint_check (void *);
204
205 static void maintenance_info_breakpoints (char *, int);
206
207 static int hw_breakpoint_used_count (void);
208
209 static int hw_watchpoint_use_count (struct breakpoint *);
210
211 static int hw_watchpoint_used_count_others (struct breakpoint *except,
212 enum bptype type,
213 int *other_type_used);
214
215 static void hbreak_command (char *, int);
216
217 static void thbreak_command (char *, int);
218
219 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
220 int count);
221
222 static void stop_command (char *arg, int from_tty);
223
224 static void stopin_command (char *arg, int from_tty);
225
226 static void stopat_command (char *arg, int from_tty);
227
228 static void tcatch_command (char *arg, int from_tty);
229
230 static void free_bp_location (struct bp_location *loc);
231 static void incref_bp_location (struct bp_location *loc);
232 static void decref_bp_location (struct bp_location **loc);
233
234 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
235
236 /* update_global_location_list's modes of operation wrt to whether to
237 insert locations now. */
238 enum ugll_insert_mode
239 {
240 /* Don't insert any breakpoint locations into the inferior, only
241 remove already-inserted locations that no longer should be
242 inserted. Functions that delete a breakpoint or breakpoints
243 should specify this mode, so that deleting a breakpoint doesn't
244 have the side effect of inserting the locations of other
245 breakpoints that are marked not-inserted, but should_be_inserted
246 returns true on them.
247
248 This behavior is useful is situations close to tear-down -- e.g.,
249 after an exec, while the target still has execution, but
250 breakpoint shadows of the previous executable image should *NOT*
251 be restored to the new image; or before detaching, where the
252 target still has execution and wants to delete breakpoints from
253 GDB's lists, and all breakpoints had already been removed from
254 the inferior. */
255 UGLL_DONT_INSERT,
256
257 /* May insert breakpoints iff breakpoints_should_be_inserted_now
258 claims breakpoints should be inserted now. */
259 UGLL_MAY_INSERT,
260
261 /* Insert locations now, irrespective of
262 breakpoints_should_be_inserted_now. E.g., say all threads are
263 stopped right now, and the user did "continue". We need to
264 insert breakpoints _before_ resuming the target, but
265 UGLL_MAY_INSERT wouldn't insert them, because
266 breakpoints_should_be_inserted_now returns false at that point,
267 as no thread is running yet. */
268 UGLL_INSERT
269 };
270
271 static void update_global_location_list (enum ugll_insert_mode);
272
273 static void update_global_location_list_nothrow (enum ugll_insert_mode);
274
275 static int is_hardware_watchpoint (const struct breakpoint *bpt);
276
277 static void insert_breakpoint_locations (void);
278
279 static void info_tracepoints_command (char *, int);
280
281 static void delete_trace_command (char *, int);
282
283 static void enable_trace_command (char *, int);
284
285 static void disable_trace_command (char *, int);
286
287 static void trace_pass_command (char *, int);
288
289 static void set_tracepoint_count (int num);
290
291 static int is_masked_watchpoint (const struct breakpoint *b);
292
293 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
295 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298 static int strace_marker_p (struct breakpoint *b);
299
300 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
301 that are implemented on top of software or hardware breakpoints
302 (user breakpoints, internal and momentary breakpoints, etc.). */
303 static struct breakpoint_ops bkpt_base_breakpoint_ops;
304
305 /* Internal breakpoints class type. */
306 static struct breakpoint_ops internal_breakpoint_ops;
307
308 /* Momentary breakpoints class type. */
309 static struct breakpoint_ops momentary_breakpoint_ops;
310
311 /* The breakpoint_ops structure to be used in regular user created
312 breakpoints. */
313 struct breakpoint_ops bkpt_breakpoint_ops;
314
315 /* Breakpoints set on probes. */
316 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
317
318 /* Dynamic printf class type. */
319 struct breakpoint_ops dprintf_breakpoint_ops;
320
321 /* The style in which to perform a dynamic printf. This is a user
322 option because different output options have different tradeoffs;
323 if GDB does the printing, there is better error handling if there
324 is a problem with any of the arguments, but using an inferior
325 function lets you have special-purpose printers and sending of
326 output to the same place as compiled-in print functions. */
327
328 static const char dprintf_style_gdb[] = "gdb";
329 static const char dprintf_style_call[] = "call";
330 static const char dprintf_style_agent[] = "agent";
331 static const char *const dprintf_style_enums[] = {
332 dprintf_style_gdb,
333 dprintf_style_call,
334 dprintf_style_agent,
335 NULL
336 };
337 static const char *dprintf_style = dprintf_style_gdb;
338
339 /* The function to use for dynamic printf if the preferred style is to
340 call into the inferior. The value is simply a string that is
341 copied into the command, so it can be anything that GDB can
342 evaluate to a callable address, not necessarily a function name. */
343
344 static char *dprintf_function;
345
346 /* The channel to use for dynamic printf if the preferred style is to
347 call into the inferior; if a nonempty string, it will be passed to
348 the call as the first argument, with the format string as the
349 second. As with the dprintf function, this can be anything that
350 GDB knows how to evaluate, so in addition to common choices like
351 "stderr", this could be an app-specific expression like
352 "mystreams[curlogger]". */
353
354 static char *dprintf_channel;
355
356 /* True if dprintf commands should continue to operate even if GDB
357 has disconnected. */
358 static int disconnected_dprintf = 1;
359
360 struct command_line *
361 breakpoint_commands (struct breakpoint *b)
362 {
363 return b->commands ? b->commands.get () : NULL;
364 }
365
366 /* Flag indicating that a command has proceeded the inferior past the
367 current breakpoint. */
368
369 static int breakpoint_proceeded;
370
371 const char *
372 bpdisp_text (enum bpdisp disp)
373 {
374 /* NOTE: the following values are a part of MI protocol and
375 represent values of 'disp' field returned when inferior stops at
376 a breakpoint. */
377 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
378
379 return bpdisps[(int) disp];
380 }
381
382 /* Prototypes for exported functions. */
383 /* If FALSE, gdb will not use hardware support for watchpoints, even
384 if such is available. */
385 static int can_use_hw_watchpoints;
386
387 static void
388 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
389 struct cmd_list_element *c,
390 const char *value)
391 {
392 fprintf_filtered (file,
393 _("Debugger's willingness to use "
394 "watchpoint hardware is %s.\n"),
395 value);
396 }
397
398 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
399 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
400 for unrecognized breakpoint locations.
401 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
402 static enum auto_boolean pending_break_support;
403 static void
404 show_pending_break_support (struct ui_file *file, int from_tty,
405 struct cmd_list_element *c,
406 const char *value)
407 {
408 fprintf_filtered (file,
409 _("Debugger's behavior regarding "
410 "pending breakpoints is %s.\n"),
411 value);
412 }
413
414 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
415 set with "break" but falling in read-only memory.
416 If 0, gdb will warn about such breakpoints, but won't automatically
417 use hardware breakpoints. */
418 static int automatic_hardware_breakpoints;
419 static void
420 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
421 struct cmd_list_element *c,
422 const char *value)
423 {
424 fprintf_filtered (file,
425 _("Automatic usage of hardware breakpoints is %s.\n"),
426 value);
427 }
428
429 /* If on, GDB keeps breakpoints inserted even if the inferior is
430 stopped, and immediately inserts any new breakpoints as soon as
431 they're created. If off (default), GDB keeps breakpoints off of
432 the target as long as possible. That is, it delays inserting
433 breakpoints until the next resume, and removes them again when the
434 target fully stops. This is a bit safer in case GDB crashes while
435 processing user input. */
436 static int always_inserted_mode = 0;
437
438 static void
439 show_always_inserted_mode (struct ui_file *file, int from_tty,
440 struct cmd_list_element *c, const char *value)
441 {
442 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
443 value);
444 }
445
446 /* See breakpoint.h. */
447
448 int
449 breakpoints_should_be_inserted_now (void)
450 {
451 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
452 {
453 /* If breakpoints are global, they should be inserted even if no
454 thread under gdb's control is running, or even if there are
455 no threads under GDB's control yet. */
456 return 1;
457 }
458 else if (target_has_execution)
459 {
460 struct thread_info *tp;
461
462 if (always_inserted_mode)
463 {
464 /* The user wants breakpoints inserted even if all threads
465 are stopped. */
466 return 1;
467 }
468
469 if (threads_are_executing ())
470 return 1;
471
472 /* Don't remove breakpoints yet if, even though all threads are
473 stopped, we still have events to process. */
474 ALL_NON_EXITED_THREADS (tp)
475 if (tp->resumed
476 && tp->suspend.waitstatus_pending_p)
477 return 1;
478 }
479 return 0;
480 }
481
482 static const char condition_evaluation_both[] = "host or target";
483
484 /* Modes for breakpoint condition evaluation. */
485 static const char condition_evaluation_auto[] = "auto";
486 static const char condition_evaluation_host[] = "host";
487 static const char condition_evaluation_target[] = "target";
488 static const char *const condition_evaluation_enums[] = {
489 condition_evaluation_auto,
490 condition_evaluation_host,
491 condition_evaluation_target,
492 NULL
493 };
494
495 /* Global that holds the current mode for breakpoint condition evaluation. */
496 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
497
498 /* Global that we use to display information to the user (gets its value from
499 condition_evaluation_mode_1. */
500 static const char *condition_evaluation_mode = condition_evaluation_auto;
501
502 /* Translate a condition evaluation mode MODE into either "host"
503 or "target". This is used mostly to translate from "auto" to the
504 real setting that is being used. It returns the translated
505 evaluation mode. */
506
507 static const char *
508 translate_condition_evaluation_mode (const char *mode)
509 {
510 if (mode == condition_evaluation_auto)
511 {
512 if (target_supports_evaluation_of_breakpoint_conditions ())
513 return condition_evaluation_target;
514 else
515 return condition_evaluation_host;
516 }
517 else
518 return mode;
519 }
520
521 /* Discovers what condition_evaluation_auto translates to. */
522
523 static const char *
524 breakpoint_condition_evaluation_mode (void)
525 {
526 return translate_condition_evaluation_mode (condition_evaluation_mode);
527 }
528
529 /* Return true if GDB should evaluate breakpoint conditions or false
530 otherwise. */
531
532 static int
533 gdb_evaluates_breakpoint_condition_p (void)
534 {
535 const char *mode = breakpoint_condition_evaluation_mode ();
536
537 return (mode == condition_evaluation_host);
538 }
539
540 /* Are we executing breakpoint commands? */
541 static int executing_breakpoint_commands;
542
543 /* Are overlay event breakpoints enabled? */
544 static int overlay_events_enabled;
545
546 /* See description in breakpoint.h. */
547 int target_exact_watchpoints = 0;
548
549 /* Walk the following statement or block through all breakpoints.
550 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
551 current breakpoint. */
552
553 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
554
555 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
556 for (B = breakpoint_chain; \
557 B ? (TMP=B->next, 1): 0; \
558 B = TMP)
559
560 /* Similar iterator for the low-level breakpoints. SAFE variant is
561 not provided so update_global_location_list must not be called
562 while executing the block of ALL_BP_LOCATIONS. */
563
564 #define ALL_BP_LOCATIONS(B,BP_TMP) \
565 for (BP_TMP = bp_locations; \
566 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
567 BP_TMP++)
568
569 /* Iterates through locations with address ADDRESS for the currently selected
570 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
571 to where the loop should start from.
572 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
573 appropriate location to start with. */
574
575 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
576 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
577 BP_LOCP_TMP = BP_LOCP_START; \
578 BP_LOCP_START \
579 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
580 && (*BP_LOCP_TMP)->address == ADDRESS); \
581 BP_LOCP_TMP++)
582
583 /* Iterator for tracepoints only. */
584
585 #define ALL_TRACEPOINTS(B) \
586 for (B = breakpoint_chain; B; B = B->next) \
587 if (is_tracepoint (B))
588
589 /* Chains of all breakpoints defined. */
590
591 struct breakpoint *breakpoint_chain;
592
593 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
594
595 static struct bp_location **bp_locations;
596
597 /* Number of elements of BP_LOCATIONS. */
598
599 static unsigned bp_locations_count;
600
601 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
602 ADDRESS for the current elements of BP_LOCATIONS which get a valid
603 result from bp_location_has_shadow. You can use it for roughly
604 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
605 an address you need to read. */
606
607 static CORE_ADDR bp_locations_placed_address_before_address_max;
608
609 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
610 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
611 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
612 You can use it for roughly limiting the subrange of BP_LOCATIONS to
613 scan for shadow bytes for an address you need to read. */
614
615 static CORE_ADDR bp_locations_shadow_len_after_address_max;
616
617 /* The locations that no longer correspond to any breakpoint, unlinked
618 from the bp_locations array, but for which a hit may still be
619 reported by a target. */
620 VEC(bp_location_p) *moribund_locations = NULL;
621
622 /* Number of last breakpoint made. */
623
624 static int breakpoint_count;
625
626 /* The value of `breakpoint_count' before the last command that
627 created breakpoints. If the last (break-like) command created more
628 than one breakpoint, then the difference between BREAKPOINT_COUNT
629 and PREV_BREAKPOINT_COUNT is more than one. */
630 static int prev_breakpoint_count;
631
632 /* Number of last tracepoint made. */
633
634 static int tracepoint_count;
635
636 static struct cmd_list_element *breakpoint_set_cmdlist;
637 static struct cmd_list_element *breakpoint_show_cmdlist;
638 struct cmd_list_element *save_cmdlist;
639
640 /* See declaration at breakpoint.h. */
641
642 struct breakpoint *
643 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
644 void *user_data)
645 {
646 struct breakpoint *b = NULL;
647
648 ALL_BREAKPOINTS (b)
649 {
650 if (func (b, user_data) != 0)
651 break;
652 }
653
654 return b;
655 }
656
657 /* Return whether a breakpoint is an active enabled breakpoint. */
658 static int
659 breakpoint_enabled (struct breakpoint *b)
660 {
661 return (b->enable_state == bp_enabled);
662 }
663
664 /* Set breakpoint count to NUM. */
665
666 static void
667 set_breakpoint_count (int num)
668 {
669 prev_breakpoint_count = breakpoint_count;
670 breakpoint_count = num;
671 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
672 }
673
674 /* Used by `start_rbreak_breakpoints' below, to record the current
675 breakpoint count before "rbreak" creates any breakpoint. */
676 static int rbreak_start_breakpoint_count;
677
678 /* Called at the start an "rbreak" command to record the first
679 breakpoint made. */
680
681 void
682 start_rbreak_breakpoints (void)
683 {
684 rbreak_start_breakpoint_count = breakpoint_count;
685 }
686
687 /* Called at the end of an "rbreak" command to record the last
688 breakpoint made. */
689
690 void
691 end_rbreak_breakpoints (void)
692 {
693 prev_breakpoint_count = rbreak_start_breakpoint_count;
694 }
695
696 /* Used in run_command to zero the hit count when a new run starts. */
697
698 void
699 clear_breakpoint_hit_counts (void)
700 {
701 struct breakpoint *b;
702
703 ALL_BREAKPOINTS (b)
704 b->hit_count = 0;
705 }
706
707 \f
708 /* Return the breakpoint with the specified number, or NULL
709 if the number does not refer to an existing breakpoint. */
710
711 struct breakpoint *
712 get_breakpoint (int num)
713 {
714 struct breakpoint *b;
715
716 ALL_BREAKPOINTS (b)
717 if (b->number == num)
718 return b;
719
720 return NULL;
721 }
722
723 \f
724
725 /* Mark locations as "conditions have changed" in case the target supports
726 evaluating conditions on its side. */
727
728 static void
729 mark_breakpoint_modified (struct breakpoint *b)
730 {
731 struct bp_location *loc;
732
733 /* This is only meaningful if the target is
734 evaluating conditions and if the user has
735 opted for condition evaluation on the target's
736 side. */
737 if (gdb_evaluates_breakpoint_condition_p ()
738 || !target_supports_evaluation_of_breakpoint_conditions ())
739 return;
740
741 if (!is_breakpoint (b))
742 return;
743
744 for (loc = b->loc; loc; loc = loc->next)
745 loc->condition_changed = condition_modified;
746 }
747
748 /* Mark location as "conditions have changed" in case the target supports
749 evaluating conditions on its side. */
750
751 static void
752 mark_breakpoint_location_modified (struct bp_location *loc)
753 {
754 /* This is only meaningful if the target is
755 evaluating conditions and if the user has
756 opted for condition evaluation on the target's
757 side. */
758 if (gdb_evaluates_breakpoint_condition_p ()
759 || !target_supports_evaluation_of_breakpoint_conditions ())
760
761 return;
762
763 if (!is_breakpoint (loc->owner))
764 return;
765
766 loc->condition_changed = condition_modified;
767 }
768
769 /* Sets the condition-evaluation mode using the static global
770 condition_evaluation_mode. */
771
772 static void
773 set_condition_evaluation_mode (char *args, int from_tty,
774 struct cmd_list_element *c)
775 {
776 const char *old_mode, *new_mode;
777
778 if ((condition_evaluation_mode_1 == condition_evaluation_target)
779 && !target_supports_evaluation_of_breakpoint_conditions ())
780 {
781 condition_evaluation_mode_1 = condition_evaluation_mode;
782 warning (_("Target does not support breakpoint condition evaluation.\n"
783 "Using host evaluation mode instead."));
784 return;
785 }
786
787 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
788 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
789
790 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
791 settings was "auto". */
792 condition_evaluation_mode = condition_evaluation_mode_1;
793
794 /* Only update the mode if the user picked a different one. */
795 if (new_mode != old_mode)
796 {
797 struct bp_location *loc, **loc_tmp;
798 /* If the user switched to a different evaluation mode, we
799 need to synch the changes with the target as follows:
800
801 "host" -> "target": Send all (valid) conditions to the target.
802 "target" -> "host": Remove all the conditions from the target.
803 */
804
805 if (new_mode == condition_evaluation_target)
806 {
807 /* Mark everything modified and synch conditions with the
808 target. */
809 ALL_BP_LOCATIONS (loc, loc_tmp)
810 mark_breakpoint_location_modified (loc);
811 }
812 else
813 {
814 /* Manually mark non-duplicate locations to synch conditions
815 with the target. We do this to remove all the conditions the
816 target knows about. */
817 ALL_BP_LOCATIONS (loc, loc_tmp)
818 if (is_breakpoint (loc->owner) && loc->inserted)
819 loc->needs_update = 1;
820 }
821
822 /* Do the update. */
823 update_global_location_list (UGLL_MAY_INSERT);
824 }
825
826 return;
827 }
828
829 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
830 what "auto" is translating to. */
831
832 static void
833 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
834 struct cmd_list_element *c, const char *value)
835 {
836 if (condition_evaluation_mode == condition_evaluation_auto)
837 fprintf_filtered (file,
838 _("Breakpoint condition evaluation "
839 "mode is %s (currently %s).\n"),
840 value,
841 breakpoint_condition_evaluation_mode ());
842 else
843 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
844 value);
845 }
846
847 /* A comparison function for bp_location AP and BP that is used by
848 bsearch. This comparison function only cares about addresses, unlike
849 the more general bp_locations_compare function. */
850
851 static int
852 bp_locations_compare_addrs (const void *ap, const void *bp)
853 {
854 const struct bp_location *a = *(const struct bp_location **) ap;
855 const struct bp_location *b = *(const struct bp_location **) bp;
856
857 if (a->address == b->address)
858 return 0;
859 else
860 return ((a->address > b->address) - (a->address < b->address));
861 }
862
863 /* Helper function to skip all bp_locations with addresses
864 less than ADDRESS. It returns the first bp_location that
865 is greater than or equal to ADDRESS. If none is found, just
866 return NULL. */
867
868 static struct bp_location **
869 get_first_locp_gte_addr (CORE_ADDR address)
870 {
871 struct bp_location dummy_loc;
872 struct bp_location *dummy_locp = &dummy_loc;
873 struct bp_location **locp_found = NULL;
874
875 /* Initialize the dummy location's address field. */
876 dummy_loc.address = address;
877
878 /* Find a close match to the first location at ADDRESS. */
879 locp_found = ((struct bp_location **)
880 bsearch (&dummy_locp, bp_locations, bp_locations_count,
881 sizeof (struct bp_location **),
882 bp_locations_compare_addrs));
883
884 /* Nothing was found, nothing left to do. */
885 if (locp_found == NULL)
886 return NULL;
887
888 /* We may have found a location that is at ADDRESS but is not the first in the
889 location's list. Go backwards (if possible) and locate the first one. */
890 while ((locp_found - 1) >= bp_locations
891 && (*(locp_found - 1))->address == address)
892 locp_found--;
893
894 return locp_found;
895 }
896
897 void
898 set_breakpoint_condition (struct breakpoint *b, const char *exp,
899 int from_tty)
900 {
901 xfree (b->cond_string);
902 b->cond_string = NULL;
903
904 if (is_watchpoint (b))
905 {
906 struct watchpoint *w = (struct watchpoint *) b;
907
908 w->cond_exp.reset ();
909 }
910 else
911 {
912 struct bp_location *loc;
913
914 for (loc = b->loc; loc; loc = loc->next)
915 {
916 loc->cond.reset ();
917
918 /* No need to free the condition agent expression
919 bytecode (if we have one). We will handle this
920 when we go through update_global_location_list. */
921 }
922 }
923
924 if (*exp == 0)
925 {
926 if (from_tty)
927 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
928 }
929 else
930 {
931 const char *arg = exp;
932
933 /* I don't know if it matters whether this is the string the user
934 typed in or the decompiled expression. */
935 b->cond_string = xstrdup (arg);
936 b->condition_not_parsed = 0;
937
938 if (is_watchpoint (b))
939 {
940 struct watchpoint *w = (struct watchpoint *) b;
941
942 innermost_block = NULL;
943 arg = exp;
944 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
945 if (*arg)
946 error (_("Junk at end of expression"));
947 w->cond_exp_valid_block = innermost_block;
948 }
949 else
950 {
951 struct bp_location *loc;
952
953 for (loc = b->loc; loc; loc = loc->next)
954 {
955 arg = exp;
956 loc->cond =
957 parse_exp_1 (&arg, loc->address,
958 block_for_pc (loc->address), 0);
959 if (*arg)
960 error (_("Junk at end of expression"));
961 }
962 }
963 }
964 mark_breakpoint_modified (b);
965
966 observer_notify_breakpoint_modified (b);
967 }
968
969 /* Completion for the "condition" command. */
970
971 static void
972 condition_completer (struct cmd_list_element *cmd,
973 completion_tracker &tracker,
974 const char *text, const char *word)
975 {
976 const char *space;
977
978 text = skip_spaces (text);
979 space = skip_to_space (text);
980 if (*space == '\0')
981 {
982 int len;
983 struct breakpoint *b;
984 VEC (char_ptr) *result = NULL;
985
986 if (text[0] == '$')
987 {
988 /* We don't support completion of history indices. */
989 if (!isdigit (text[1]))
990 complete_internalvar (tracker, &text[1]);
991 return;
992 }
993
994 /* We're completing the breakpoint number. */
995 len = strlen (text);
996
997 ALL_BREAKPOINTS (b)
998 {
999 char number[50];
1000
1001 xsnprintf (number, sizeof (number), "%d", b->number);
1002
1003 if (strncmp (number, text, len) == 0)
1004 {
1005 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
1006 tracker.add_completion (std::move (copy));
1007 }
1008 }
1009
1010 return;
1011 }
1012
1013 /* We're completing the expression part. */
1014 text = skip_spaces (space);
1015 expression_completer (cmd, tracker, text, word);
1016 }
1017
1018 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1019
1020 static void
1021 condition_command (char *arg, int from_tty)
1022 {
1023 struct breakpoint *b;
1024 char *p;
1025 int bnum;
1026
1027 if (arg == 0)
1028 error_no_arg (_("breakpoint number"));
1029
1030 p = arg;
1031 bnum = get_number (&p);
1032 if (bnum == 0)
1033 error (_("Bad breakpoint argument: '%s'"), arg);
1034
1035 ALL_BREAKPOINTS (b)
1036 if (b->number == bnum)
1037 {
1038 /* Check if this breakpoint has a "stop" method implemented in an
1039 extension language. This method and conditions entered into GDB
1040 from the CLI are mutually exclusive. */
1041 const struct extension_language_defn *extlang
1042 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1043
1044 if (extlang != NULL)
1045 {
1046 error (_("Only one stop condition allowed. There is currently"
1047 " a %s stop condition defined for this breakpoint."),
1048 ext_lang_capitalized_name (extlang));
1049 }
1050 set_breakpoint_condition (b, p, from_tty);
1051
1052 if (is_breakpoint (b))
1053 update_global_location_list (UGLL_MAY_INSERT);
1054
1055 return;
1056 }
1057
1058 error (_("No breakpoint number %d."), bnum);
1059 }
1060
1061 /* Check that COMMAND do not contain commands that are suitable
1062 only for tracepoints and not suitable for ordinary breakpoints.
1063 Throw if any such commands is found. */
1064
1065 static void
1066 check_no_tracepoint_commands (struct command_line *commands)
1067 {
1068 struct command_line *c;
1069
1070 for (c = commands; c; c = c->next)
1071 {
1072 int i;
1073
1074 if (c->control_type == while_stepping_control)
1075 error (_("The 'while-stepping' command can "
1076 "only be used for tracepoints"));
1077
1078 for (i = 0; i < c->body_count; ++i)
1079 check_no_tracepoint_commands ((c->body_list)[i]);
1080
1081 /* Not that command parsing removes leading whitespace and comment
1082 lines and also empty lines. So, we only need to check for
1083 command directly. */
1084 if (strstr (c->line, "collect ") == c->line)
1085 error (_("The 'collect' command can only be used for tracepoints"));
1086
1087 if (strstr (c->line, "teval ") == c->line)
1088 error (_("The 'teval' command can only be used for tracepoints"));
1089 }
1090 }
1091
1092 struct longjmp_breakpoint : public breakpoint
1093 {
1094 ~longjmp_breakpoint () override;
1095 };
1096
1097 /* Encapsulate tests for different types of tracepoints. */
1098
1099 static bool
1100 is_tracepoint_type (bptype type)
1101 {
1102 return (type == bp_tracepoint
1103 || type == bp_fast_tracepoint
1104 || type == bp_static_tracepoint);
1105 }
1106
1107 static bool
1108 is_longjmp_type (bptype type)
1109 {
1110 return type == bp_longjmp || type == bp_exception;
1111 }
1112
1113 int
1114 is_tracepoint (const struct breakpoint *b)
1115 {
1116 return is_tracepoint_type (b->type);
1117 }
1118
1119 /* Factory function to create an appropriate instance of breakpoint given
1120 TYPE. */
1121
1122 static std::unique_ptr<breakpoint>
1123 new_breakpoint_from_type (bptype type)
1124 {
1125 breakpoint *b;
1126
1127 if (is_tracepoint_type (type))
1128 b = new tracepoint ();
1129 else if (is_longjmp_type (type))
1130 b = new longjmp_breakpoint ();
1131 else
1132 b = new breakpoint ();
1133
1134 return std::unique_ptr<breakpoint> (b);
1135 }
1136
1137 /* A helper function that validates that COMMANDS are valid for a
1138 breakpoint. This function will throw an exception if a problem is
1139 found. */
1140
1141 static void
1142 validate_commands_for_breakpoint (struct breakpoint *b,
1143 struct command_line *commands)
1144 {
1145 if (is_tracepoint (b))
1146 {
1147 struct tracepoint *t = (struct tracepoint *) b;
1148 struct command_line *c;
1149 struct command_line *while_stepping = 0;
1150
1151 /* Reset the while-stepping step count. The previous commands
1152 might have included a while-stepping action, while the new
1153 ones might not. */
1154 t->step_count = 0;
1155
1156 /* We need to verify that each top-level element of commands is
1157 valid for tracepoints, that there's at most one
1158 while-stepping element, and that the while-stepping's body
1159 has valid tracing commands excluding nested while-stepping.
1160 We also need to validate the tracepoint action line in the
1161 context of the tracepoint --- validate_actionline actually
1162 has side effects, like setting the tracepoint's
1163 while-stepping STEP_COUNT, in addition to checking if the
1164 collect/teval actions parse and make sense in the
1165 tracepoint's context. */
1166 for (c = commands; c; c = c->next)
1167 {
1168 if (c->control_type == while_stepping_control)
1169 {
1170 if (b->type == bp_fast_tracepoint)
1171 error (_("The 'while-stepping' command "
1172 "cannot be used for fast tracepoint"));
1173 else if (b->type == bp_static_tracepoint)
1174 error (_("The 'while-stepping' command "
1175 "cannot be used for static tracepoint"));
1176
1177 if (while_stepping)
1178 error (_("The 'while-stepping' command "
1179 "can be used only once"));
1180 else
1181 while_stepping = c;
1182 }
1183
1184 validate_actionline (c->line, b);
1185 }
1186 if (while_stepping)
1187 {
1188 struct command_line *c2;
1189
1190 gdb_assert (while_stepping->body_count == 1);
1191 c2 = while_stepping->body_list[0];
1192 for (; c2; c2 = c2->next)
1193 {
1194 if (c2->control_type == while_stepping_control)
1195 error (_("The 'while-stepping' command cannot be nested"));
1196 }
1197 }
1198 }
1199 else
1200 {
1201 check_no_tracepoint_commands (commands);
1202 }
1203 }
1204
1205 /* Return a vector of all the static tracepoints set at ADDR. The
1206 caller is responsible for releasing the vector. */
1207
1208 VEC(breakpoint_p) *
1209 static_tracepoints_here (CORE_ADDR addr)
1210 {
1211 struct breakpoint *b;
1212 VEC(breakpoint_p) *found = 0;
1213 struct bp_location *loc;
1214
1215 ALL_BREAKPOINTS (b)
1216 if (b->type == bp_static_tracepoint)
1217 {
1218 for (loc = b->loc; loc; loc = loc->next)
1219 if (loc->address == addr)
1220 VEC_safe_push(breakpoint_p, found, b);
1221 }
1222
1223 return found;
1224 }
1225
1226 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1227 validate that only allowed commands are included. */
1228
1229 void
1230 breakpoint_set_commands (struct breakpoint *b,
1231 command_line_up &&commands)
1232 {
1233 validate_commands_for_breakpoint (b, commands.get ());
1234
1235 b->commands = std::move (commands);
1236 observer_notify_breakpoint_modified (b);
1237 }
1238
1239 /* Set the internal `silent' flag on the breakpoint. Note that this
1240 is not the same as the "silent" that may appear in the breakpoint's
1241 commands. */
1242
1243 void
1244 breakpoint_set_silent (struct breakpoint *b, int silent)
1245 {
1246 int old_silent = b->silent;
1247
1248 b->silent = silent;
1249 if (old_silent != silent)
1250 observer_notify_breakpoint_modified (b);
1251 }
1252
1253 /* Set the thread for this breakpoint. If THREAD is -1, make the
1254 breakpoint work for any thread. */
1255
1256 void
1257 breakpoint_set_thread (struct breakpoint *b, int thread)
1258 {
1259 int old_thread = b->thread;
1260
1261 b->thread = thread;
1262 if (old_thread != thread)
1263 observer_notify_breakpoint_modified (b);
1264 }
1265
1266 /* Set the task for this breakpoint. If TASK is 0, make the
1267 breakpoint work for any task. */
1268
1269 void
1270 breakpoint_set_task (struct breakpoint *b, int task)
1271 {
1272 int old_task = b->task;
1273
1274 b->task = task;
1275 if (old_task != task)
1276 observer_notify_breakpoint_modified (b);
1277 }
1278
1279 void
1280 check_tracepoint_command (char *line, void *closure)
1281 {
1282 struct breakpoint *b = (struct breakpoint *) closure;
1283
1284 validate_actionline (line, b);
1285 }
1286
1287 static void
1288 commands_command_1 (const char *arg, int from_tty,
1289 struct command_line *control)
1290 {
1291 counted_command_line cmd;
1292
1293 std::string new_arg;
1294
1295 if (arg == NULL || !*arg)
1296 {
1297 if (breakpoint_count - prev_breakpoint_count > 1)
1298 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1299 breakpoint_count);
1300 else if (breakpoint_count > 0)
1301 new_arg = string_printf ("%d", breakpoint_count);
1302 arg = new_arg.c_str ();
1303 }
1304
1305 map_breakpoint_numbers
1306 (arg, [&] (breakpoint *b)
1307 {
1308 if (cmd == NULL)
1309 {
1310 if (control != NULL)
1311 cmd = copy_command_lines (control->body_list[0]);
1312 else
1313 {
1314 struct cleanup *old_chain;
1315 char *str;
1316
1317 str = xstrprintf (_("Type commands for breakpoint(s) "
1318 "%s, one per line."),
1319 arg);
1320
1321 old_chain = make_cleanup (xfree, str);
1322
1323 cmd = read_command_lines (str,
1324 from_tty, 1,
1325 (is_tracepoint (b)
1326 ? check_tracepoint_command : 0),
1327 b);
1328
1329 do_cleanups (old_chain);
1330 }
1331 }
1332
1333 /* If a breakpoint was on the list more than once, we don't need to
1334 do anything. */
1335 if (b->commands != cmd)
1336 {
1337 validate_commands_for_breakpoint (b, cmd.get ());
1338 b->commands = cmd;
1339 observer_notify_breakpoint_modified (b);
1340 }
1341 });
1342
1343 if (cmd == NULL)
1344 error (_("No breakpoints specified."));
1345 }
1346
1347 static void
1348 commands_command (char *arg, int from_tty)
1349 {
1350 commands_command_1 (arg, from_tty, NULL);
1351 }
1352
1353 /* Like commands_command, but instead of reading the commands from
1354 input stream, takes them from an already parsed command structure.
1355
1356 This is used by cli-script.c to DTRT with breakpoint commands
1357 that are part of if and while bodies. */
1358 enum command_control_type
1359 commands_from_control_command (const char *arg, struct command_line *cmd)
1360 {
1361 commands_command_1 (arg, 0, cmd);
1362 return simple_control;
1363 }
1364
1365 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1366
1367 static int
1368 bp_location_has_shadow (struct bp_location *bl)
1369 {
1370 if (bl->loc_type != bp_loc_software_breakpoint)
1371 return 0;
1372 if (!bl->inserted)
1373 return 0;
1374 if (bl->target_info.shadow_len == 0)
1375 /* BL isn't valid, or doesn't shadow memory. */
1376 return 0;
1377 return 1;
1378 }
1379
1380 /* Update BUF, which is LEN bytes read from the target address
1381 MEMADDR, by replacing a memory breakpoint with its shadowed
1382 contents.
1383
1384 If READBUF is not NULL, this buffer must not overlap with the of
1385 the breakpoint location's shadow_contents buffer. Otherwise, a
1386 failed assertion internal error will be raised. */
1387
1388 static void
1389 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1390 const gdb_byte *writebuf_org,
1391 ULONGEST memaddr, LONGEST len,
1392 struct bp_target_info *target_info,
1393 struct gdbarch *gdbarch)
1394 {
1395 /* Now do full processing of the found relevant range of elements. */
1396 CORE_ADDR bp_addr = 0;
1397 int bp_size = 0;
1398 int bptoffset = 0;
1399
1400 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1401 current_program_space->aspace, 0))
1402 {
1403 /* The breakpoint is inserted in a different address space. */
1404 return;
1405 }
1406
1407 /* Addresses and length of the part of the breakpoint that
1408 we need to copy. */
1409 bp_addr = target_info->placed_address;
1410 bp_size = target_info->shadow_len;
1411
1412 if (bp_addr + bp_size <= memaddr)
1413 {
1414 /* The breakpoint is entirely before the chunk of memory we are
1415 reading. */
1416 return;
1417 }
1418
1419 if (bp_addr >= memaddr + len)
1420 {
1421 /* The breakpoint is entirely after the chunk of memory we are
1422 reading. */
1423 return;
1424 }
1425
1426 /* Offset within shadow_contents. */
1427 if (bp_addr < memaddr)
1428 {
1429 /* Only copy the second part of the breakpoint. */
1430 bp_size -= memaddr - bp_addr;
1431 bptoffset = memaddr - bp_addr;
1432 bp_addr = memaddr;
1433 }
1434
1435 if (bp_addr + bp_size > memaddr + len)
1436 {
1437 /* Only copy the first part of the breakpoint. */
1438 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1439 }
1440
1441 if (readbuf != NULL)
1442 {
1443 /* Verify that the readbuf buffer does not overlap with the
1444 shadow_contents buffer. */
1445 gdb_assert (target_info->shadow_contents >= readbuf + len
1446 || readbuf >= (target_info->shadow_contents
1447 + target_info->shadow_len));
1448
1449 /* Update the read buffer with this inserted breakpoint's
1450 shadow. */
1451 memcpy (readbuf + bp_addr - memaddr,
1452 target_info->shadow_contents + bptoffset, bp_size);
1453 }
1454 else
1455 {
1456 const unsigned char *bp;
1457 CORE_ADDR addr = target_info->reqstd_address;
1458 int placed_size;
1459
1460 /* Update the shadow with what we want to write to memory. */
1461 memcpy (target_info->shadow_contents + bptoffset,
1462 writebuf_org + bp_addr - memaddr, bp_size);
1463
1464 /* Determine appropriate breakpoint contents and size for this
1465 address. */
1466 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1467
1468 /* Update the final write buffer with this inserted
1469 breakpoint's INSN. */
1470 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1471 }
1472 }
1473
1474 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1475 by replacing any memory breakpoints with their shadowed contents.
1476
1477 If READBUF is not NULL, this buffer must not overlap with any of
1478 the breakpoint location's shadow_contents buffers. Otherwise,
1479 a failed assertion internal error will be raised.
1480
1481 The range of shadowed area by each bp_location is:
1482 bl->address - bp_locations_placed_address_before_address_max
1483 up to bl->address + bp_locations_shadow_len_after_address_max
1484 The range we were requested to resolve shadows for is:
1485 memaddr ... memaddr + len
1486 Thus the safe cutoff boundaries for performance optimization are
1487 memaddr + len <= (bl->address
1488 - bp_locations_placed_address_before_address_max)
1489 and:
1490 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1491
1492 void
1493 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1494 const gdb_byte *writebuf_org,
1495 ULONGEST memaddr, LONGEST len)
1496 {
1497 /* Left boundary, right boundary and median element of our binary
1498 search. */
1499 unsigned bc_l, bc_r, bc;
1500
1501 /* Find BC_L which is a leftmost element which may affect BUF
1502 content. It is safe to report lower value but a failure to
1503 report higher one. */
1504
1505 bc_l = 0;
1506 bc_r = bp_locations_count;
1507 while (bc_l + 1 < bc_r)
1508 {
1509 struct bp_location *bl;
1510
1511 bc = (bc_l + bc_r) / 2;
1512 bl = bp_locations[bc];
1513
1514 /* Check first BL->ADDRESS will not overflow due to the added
1515 constant. Then advance the left boundary only if we are sure
1516 the BC element can in no way affect the BUF content (MEMADDR
1517 to MEMADDR + LEN range).
1518
1519 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1520 offset so that we cannot miss a breakpoint with its shadow
1521 range tail still reaching MEMADDR. */
1522
1523 if ((bl->address + bp_locations_shadow_len_after_address_max
1524 >= bl->address)
1525 && (bl->address + bp_locations_shadow_len_after_address_max
1526 <= memaddr))
1527 bc_l = bc;
1528 else
1529 bc_r = bc;
1530 }
1531
1532 /* Due to the binary search above, we need to make sure we pick the
1533 first location that's at BC_L's address. E.g., if there are
1534 multiple locations at the same address, BC_L may end up pointing
1535 at a duplicate location, and miss the "master"/"inserted"
1536 location. Say, given locations L1, L2 and L3 at addresses A and
1537 B:
1538
1539 L1@A, L2@A, L3@B, ...
1540
1541 BC_L could end up pointing at location L2, while the "master"
1542 location could be L1. Since the `loc->inserted' flag is only set
1543 on "master" locations, we'd forget to restore the shadow of L1
1544 and L2. */
1545 while (bc_l > 0
1546 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1547 bc_l--;
1548
1549 /* Now do full processing of the found relevant range of elements. */
1550
1551 for (bc = bc_l; bc < bp_locations_count; bc++)
1552 {
1553 struct bp_location *bl = bp_locations[bc];
1554
1555 /* bp_location array has BL->OWNER always non-NULL. */
1556 if (bl->owner->type == bp_none)
1557 warning (_("reading through apparently deleted breakpoint #%d?"),
1558 bl->owner->number);
1559
1560 /* Performance optimization: any further element can no longer affect BUF
1561 content. */
1562
1563 if (bl->address >= bp_locations_placed_address_before_address_max
1564 && memaddr + len <= (bl->address
1565 - bp_locations_placed_address_before_address_max))
1566 break;
1567
1568 if (!bp_location_has_shadow (bl))
1569 continue;
1570
1571 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1572 memaddr, len, &bl->target_info, bl->gdbarch);
1573 }
1574 }
1575
1576 \f
1577
1578 /* Return true if BPT is either a software breakpoint or a hardware
1579 breakpoint. */
1580
1581 int
1582 is_breakpoint (const struct breakpoint *bpt)
1583 {
1584 return (bpt->type == bp_breakpoint
1585 || bpt->type == bp_hardware_breakpoint
1586 || bpt->type == bp_dprintf);
1587 }
1588
1589 /* Return true if BPT is of any hardware watchpoint kind. */
1590
1591 static int
1592 is_hardware_watchpoint (const struct breakpoint *bpt)
1593 {
1594 return (bpt->type == bp_hardware_watchpoint
1595 || bpt->type == bp_read_watchpoint
1596 || bpt->type == bp_access_watchpoint);
1597 }
1598
1599 /* Return true if BPT is of any watchpoint kind, hardware or
1600 software. */
1601
1602 int
1603 is_watchpoint (const struct breakpoint *bpt)
1604 {
1605 return (is_hardware_watchpoint (bpt)
1606 || bpt->type == bp_watchpoint);
1607 }
1608
1609 /* Returns true if the current thread and its running state are safe
1610 to evaluate or update watchpoint B. Watchpoints on local
1611 expressions need to be evaluated in the context of the thread that
1612 was current when the watchpoint was created, and, that thread needs
1613 to be stopped to be able to select the correct frame context.
1614 Watchpoints on global expressions can be evaluated on any thread,
1615 and in any state. It is presently left to the target allowing
1616 memory accesses when threads are running. */
1617
1618 static int
1619 watchpoint_in_thread_scope (struct watchpoint *b)
1620 {
1621 return (b->pspace == current_program_space
1622 && (ptid_equal (b->watchpoint_thread, null_ptid)
1623 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1624 && !is_executing (inferior_ptid))));
1625 }
1626
1627 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1628 associated bp_watchpoint_scope breakpoint. */
1629
1630 static void
1631 watchpoint_del_at_next_stop (struct watchpoint *w)
1632 {
1633 if (w->related_breakpoint != w)
1634 {
1635 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1636 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1637 w->related_breakpoint->disposition = disp_del_at_next_stop;
1638 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1639 w->related_breakpoint = w;
1640 }
1641 w->disposition = disp_del_at_next_stop;
1642 }
1643
1644 /* Extract a bitfield value from value VAL using the bit parameters contained in
1645 watchpoint W. */
1646
1647 static struct value *
1648 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1649 {
1650 struct value *bit_val;
1651
1652 if (val == NULL)
1653 return NULL;
1654
1655 bit_val = allocate_value (value_type (val));
1656
1657 unpack_value_bitfield (bit_val,
1658 w->val_bitpos,
1659 w->val_bitsize,
1660 value_contents_for_printing (val),
1661 value_offset (val),
1662 val);
1663
1664 return bit_val;
1665 }
1666
1667 /* Allocate a dummy location and add it to B, which must be a software
1668 watchpoint. This is required because even if a software watchpoint
1669 is not watching any memory, bpstat_stop_status requires a location
1670 to be able to report stops. */
1671
1672 static void
1673 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1674 struct program_space *pspace)
1675 {
1676 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1677
1678 b->loc = allocate_bp_location (b);
1679 b->loc->pspace = pspace;
1680 b->loc->address = -1;
1681 b->loc->length = -1;
1682 }
1683
1684 /* Returns true if B is a software watchpoint that is not watching any
1685 memory (e.g., "watch $pc"). */
1686
1687 static int
1688 is_no_memory_software_watchpoint (struct breakpoint *b)
1689 {
1690 return (b->type == bp_watchpoint
1691 && b->loc != NULL
1692 && b->loc->next == NULL
1693 && b->loc->address == -1
1694 && b->loc->length == -1);
1695 }
1696
1697 /* Assuming that B is a watchpoint:
1698 - Reparse watchpoint expression, if REPARSE is non-zero
1699 - Evaluate expression and store the result in B->val
1700 - Evaluate the condition if there is one, and store the result
1701 in b->loc->cond.
1702 - Update the list of values that must be watched in B->loc.
1703
1704 If the watchpoint disposition is disp_del_at_next_stop, then do
1705 nothing. If this is local watchpoint that is out of scope, delete
1706 it.
1707
1708 Even with `set breakpoint always-inserted on' the watchpoints are
1709 removed + inserted on each stop here. Normal breakpoints must
1710 never be removed because they might be missed by a running thread
1711 when debugging in non-stop mode. On the other hand, hardware
1712 watchpoints (is_hardware_watchpoint; processed here) are specific
1713 to each LWP since they are stored in each LWP's hardware debug
1714 registers. Therefore, such LWP must be stopped first in order to
1715 be able to modify its hardware watchpoints.
1716
1717 Hardware watchpoints must be reset exactly once after being
1718 presented to the user. It cannot be done sooner, because it would
1719 reset the data used to present the watchpoint hit to the user. And
1720 it must not be done later because it could display the same single
1721 watchpoint hit during multiple GDB stops. Note that the latter is
1722 relevant only to the hardware watchpoint types bp_read_watchpoint
1723 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1724 not user-visible - its hit is suppressed if the memory content has
1725 not changed.
1726
1727 The following constraints influence the location where we can reset
1728 hardware watchpoints:
1729
1730 * target_stopped_by_watchpoint and target_stopped_data_address are
1731 called several times when GDB stops.
1732
1733 [linux]
1734 * Multiple hardware watchpoints can be hit at the same time,
1735 causing GDB to stop. GDB only presents one hardware watchpoint
1736 hit at a time as the reason for stopping, and all the other hits
1737 are presented later, one after the other, each time the user
1738 requests the execution to be resumed. Execution is not resumed
1739 for the threads still having pending hit event stored in
1740 LWP_INFO->STATUS. While the watchpoint is already removed from
1741 the inferior on the first stop the thread hit event is kept being
1742 reported from its cached value by linux_nat_stopped_data_address
1743 until the real thread resume happens after the watchpoint gets
1744 presented and thus its LWP_INFO->STATUS gets reset.
1745
1746 Therefore the hardware watchpoint hit can get safely reset on the
1747 watchpoint removal from inferior. */
1748
1749 static void
1750 update_watchpoint (struct watchpoint *b, int reparse)
1751 {
1752 int within_current_scope;
1753 struct frame_id saved_frame_id;
1754 int frame_saved;
1755
1756 /* If this is a local watchpoint, we only want to check if the
1757 watchpoint frame is in scope if the current thread is the thread
1758 that was used to create the watchpoint. */
1759 if (!watchpoint_in_thread_scope (b))
1760 return;
1761
1762 if (b->disposition == disp_del_at_next_stop)
1763 return;
1764
1765 frame_saved = 0;
1766
1767 /* Determine if the watchpoint is within scope. */
1768 if (b->exp_valid_block == NULL)
1769 within_current_scope = 1;
1770 else
1771 {
1772 struct frame_info *fi = get_current_frame ();
1773 struct gdbarch *frame_arch = get_frame_arch (fi);
1774 CORE_ADDR frame_pc = get_frame_pc (fi);
1775
1776 /* If we're at a point where the stack has been destroyed
1777 (e.g. in a function epilogue), unwinding may not work
1778 properly. Do not attempt to recreate locations at this
1779 point. See similar comments in watchpoint_check. */
1780 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1781 return;
1782
1783 /* Save the current frame's ID so we can restore it after
1784 evaluating the watchpoint expression on its own frame. */
1785 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1786 took a frame parameter, so that we didn't have to change the
1787 selected frame. */
1788 frame_saved = 1;
1789 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1790
1791 fi = frame_find_by_id (b->watchpoint_frame);
1792 within_current_scope = (fi != NULL);
1793 if (within_current_scope)
1794 select_frame (fi);
1795 }
1796
1797 /* We don't free locations. They are stored in the bp_location array
1798 and update_global_location_list will eventually delete them and
1799 remove breakpoints if needed. */
1800 b->loc = NULL;
1801
1802 if (within_current_scope && reparse)
1803 {
1804 const char *s;
1805
1806 b->exp.reset ();
1807 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1808 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1809 /* If the meaning of expression itself changed, the old value is
1810 no longer relevant. We don't want to report a watchpoint hit
1811 to the user when the old value and the new value may actually
1812 be completely different objects. */
1813 value_free (b->val);
1814 b->val = NULL;
1815 b->val_valid = 0;
1816
1817 /* Note that unlike with breakpoints, the watchpoint's condition
1818 expression is stored in the breakpoint object, not in the
1819 locations (re)created below. */
1820 if (b->cond_string != NULL)
1821 {
1822 b->cond_exp.reset ();
1823
1824 s = b->cond_string;
1825 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1826 }
1827 }
1828
1829 /* If we failed to parse the expression, for example because
1830 it refers to a global variable in a not-yet-loaded shared library,
1831 don't try to insert watchpoint. We don't automatically delete
1832 such watchpoint, though, since failure to parse expression
1833 is different from out-of-scope watchpoint. */
1834 if (!target_has_execution)
1835 {
1836 /* Without execution, memory can't change. No use to try and
1837 set watchpoint locations. The watchpoint will be reset when
1838 the target gains execution, through breakpoint_re_set. */
1839 if (!can_use_hw_watchpoints)
1840 {
1841 if (b->ops->works_in_software_mode (b))
1842 b->type = bp_watchpoint;
1843 else
1844 error (_("Can't set read/access watchpoint when "
1845 "hardware watchpoints are disabled."));
1846 }
1847 }
1848 else if (within_current_scope && b->exp)
1849 {
1850 int pc = 0;
1851 struct value *val_chain, *v, *result, *next;
1852 struct program_space *frame_pspace;
1853
1854 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1855
1856 /* Avoid setting b->val if it's already set. The meaning of
1857 b->val is 'the last value' user saw, and we should update
1858 it only if we reported that last value to user. As it
1859 happens, the code that reports it updates b->val directly.
1860 We don't keep track of the memory value for masked
1861 watchpoints. */
1862 if (!b->val_valid && !is_masked_watchpoint (b))
1863 {
1864 if (b->val_bitsize != 0)
1865 {
1866 v = extract_bitfield_from_watchpoint_value (b, v);
1867 if (v != NULL)
1868 release_value (v);
1869 }
1870 b->val = v;
1871 b->val_valid = 1;
1872 }
1873
1874 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1875
1876 /* Look at each value on the value chain. */
1877 for (v = val_chain; v; v = value_next (v))
1878 {
1879 /* If it's a memory location, and GDB actually needed
1880 its contents to evaluate the expression, then we
1881 must watch it. If the first value returned is
1882 still lazy, that means an error occurred reading it;
1883 watch it anyway in case it becomes readable. */
1884 if (VALUE_LVAL (v) == lval_memory
1885 && (v == val_chain || ! value_lazy (v)))
1886 {
1887 struct type *vtype = check_typedef (value_type (v));
1888
1889 /* We only watch structs and arrays if user asked
1890 for it explicitly, never if they just happen to
1891 appear in the middle of some value chain. */
1892 if (v == result
1893 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1894 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1895 {
1896 CORE_ADDR addr;
1897 enum target_hw_bp_type type;
1898 struct bp_location *loc, **tmp;
1899 int bitpos = 0, bitsize = 0;
1900
1901 if (value_bitsize (v) != 0)
1902 {
1903 /* Extract the bit parameters out from the bitfield
1904 sub-expression. */
1905 bitpos = value_bitpos (v);
1906 bitsize = value_bitsize (v);
1907 }
1908 else if (v == result && b->val_bitsize != 0)
1909 {
1910 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1911 lvalue whose bit parameters are saved in the fields
1912 VAL_BITPOS and VAL_BITSIZE. */
1913 bitpos = b->val_bitpos;
1914 bitsize = b->val_bitsize;
1915 }
1916
1917 addr = value_address (v);
1918 if (bitsize != 0)
1919 {
1920 /* Skip the bytes that don't contain the bitfield. */
1921 addr += bitpos / 8;
1922 }
1923
1924 type = hw_write;
1925 if (b->type == bp_read_watchpoint)
1926 type = hw_read;
1927 else if (b->type == bp_access_watchpoint)
1928 type = hw_access;
1929
1930 loc = allocate_bp_location (b);
1931 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1932 ;
1933 *tmp = loc;
1934 loc->gdbarch = get_type_arch (value_type (v));
1935
1936 loc->pspace = frame_pspace;
1937 loc->address = addr;
1938
1939 if (bitsize != 0)
1940 {
1941 /* Just cover the bytes that make up the bitfield. */
1942 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1943 }
1944 else
1945 loc->length = TYPE_LENGTH (value_type (v));
1946
1947 loc->watchpoint_type = type;
1948 }
1949 }
1950 }
1951
1952 /* Change the type of breakpoint between hardware assisted or
1953 an ordinary watchpoint depending on the hardware support
1954 and free hardware slots. REPARSE is set when the inferior
1955 is started. */
1956 if (reparse)
1957 {
1958 int reg_cnt;
1959 enum bp_loc_type loc_type;
1960 struct bp_location *bl;
1961
1962 reg_cnt = can_use_hardware_watchpoint (val_chain);
1963
1964 if (reg_cnt)
1965 {
1966 int i, target_resources_ok, other_type_used;
1967 enum bptype type;
1968
1969 /* Use an exact watchpoint when there's only one memory region to be
1970 watched, and only one debug register is needed to watch it. */
1971 b->exact = target_exact_watchpoints && reg_cnt == 1;
1972
1973 /* We need to determine how many resources are already
1974 used for all other hardware watchpoints plus this one
1975 to see if we still have enough resources to also fit
1976 this watchpoint in as well. */
1977
1978 /* If this is a software watchpoint, we try to turn it
1979 to a hardware one -- count resources as if B was of
1980 hardware watchpoint type. */
1981 type = b->type;
1982 if (type == bp_watchpoint)
1983 type = bp_hardware_watchpoint;
1984
1985 /* This watchpoint may or may not have been placed on
1986 the list yet at this point (it won't be in the list
1987 if we're trying to create it for the first time,
1988 through watch_command), so always account for it
1989 manually. */
1990
1991 /* Count resources used by all watchpoints except B. */
1992 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1993
1994 /* Add in the resources needed for B. */
1995 i += hw_watchpoint_use_count (b);
1996
1997 target_resources_ok
1998 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1999 if (target_resources_ok <= 0)
2000 {
2001 int sw_mode = b->ops->works_in_software_mode (b);
2002
2003 if (target_resources_ok == 0 && !sw_mode)
2004 error (_("Target does not support this type of "
2005 "hardware watchpoint."));
2006 else if (target_resources_ok < 0 && !sw_mode)
2007 error (_("There are not enough available hardware "
2008 "resources for this watchpoint."));
2009
2010 /* Downgrade to software watchpoint. */
2011 b->type = bp_watchpoint;
2012 }
2013 else
2014 {
2015 /* If this was a software watchpoint, we've just
2016 found we have enough resources to turn it to a
2017 hardware watchpoint. Otherwise, this is a
2018 nop. */
2019 b->type = type;
2020 }
2021 }
2022 else if (!b->ops->works_in_software_mode (b))
2023 {
2024 if (!can_use_hw_watchpoints)
2025 error (_("Can't set read/access watchpoint when "
2026 "hardware watchpoints are disabled."));
2027 else
2028 error (_("Expression cannot be implemented with "
2029 "read/access watchpoint."));
2030 }
2031 else
2032 b->type = bp_watchpoint;
2033
2034 loc_type = (b->type == bp_watchpoint? bp_loc_other
2035 : bp_loc_hardware_watchpoint);
2036 for (bl = b->loc; bl; bl = bl->next)
2037 bl->loc_type = loc_type;
2038 }
2039
2040 for (v = val_chain; v; v = next)
2041 {
2042 next = value_next (v);
2043 if (v != b->val)
2044 value_free (v);
2045 }
2046
2047 /* If a software watchpoint is not watching any memory, then the
2048 above left it without any location set up. But,
2049 bpstat_stop_status requires a location to be able to report
2050 stops, so make sure there's at least a dummy one. */
2051 if (b->type == bp_watchpoint && b->loc == NULL)
2052 software_watchpoint_add_no_memory_location (b, frame_pspace);
2053 }
2054 else if (!within_current_scope)
2055 {
2056 printf_filtered (_("\
2057 Watchpoint %d deleted because the program has left the block\n\
2058 in which its expression is valid.\n"),
2059 b->number);
2060 watchpoint_del_at_next_stop (b);
2061 }
2062
2063 /* Restore the selected frame. */
2064 if (frame_saved)
2065 select_frame (frame_find_by_id (saved_frame_id));
2066 }
2067
2068
2069 /* Returns 1 iff breakpoint location should be
2070 inserted in the inferior. We don't differentiate the type of BL's owner
2071 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2072 breakpoint_ops is not defined, because in insert_bp_location,
2073 tracepoint's insert_location will not be called. */
2074 static int
2075 should_be_inserted (struct bp_location *bl)
2076 {
2077 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2078 return 0;
2079
2080 if (bl->owner->disposition == disp_del_at_next_stop)
2081 return 0;
2082
2083 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2084 return 0;
2085
2086 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2087 return 0;
2088
2089 /* This is set for example, when we're attached to the parent of a
2090 vfork, and have detached from the child. The child is running
2091 free, and we expect it to do an exec or exit, at which point the
2092 OS makes the parent schedulable again (and the target reports
2093 that the vfork is done). Until the child is done with the shared
2094 memory region, do not insert breakpoints in the parent, otherwise
2095 the child could still trip on the parent's breakpoints. Since
2096 the parent is blocked anyway, it won't miss any breakpoint. */
2097 if (bl->pspace->breakpoints_not_allowed)
2098 return 0;
2099
2100 /* Don't insert a breakpoint if we're trying to step past its
2101 location, except if the breakpoint is a single-step breakpoint,
2102 and the breakpoint's thread is the thread which is stepping past
2103 a breakpoint. */
2104 if ((bl->loc_type == bp_loc_software_breakpoint
2105 || bl->loc_type == bp_loc_hardware_breakpoint)
2106 && stepping_past_instruction_at (bl->pspace->aspace,
2107 bl->address)
2108 /* The single-step breakpoint may be inserted at the location
2109 we're trying to step if the instruction branches to itself.
2110 However, the instruction won't be executed at all and it may
2111 break the semantics of the instruction, for example, the
2112 instruction is a conditional branch or updates some flags.
2113 We can't fix it unless GDB is able to emulate the instruction
2114 or switch to displaced stepping. */
2115 && !(bl->owner->type == bp_single_step
2116 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2117 {
2118 if (debug_infrun)
2119 {
2120 fprintf_unfiltered (gdb_stdlog,
2121 "infrun: skipping breakpoint: "
2122 "stepping past insn at: %s\n",
2123 paddress (bl->gdbarch, bl->address));
2124 }
2125 return 0;
2126 }
2127
2128 /* Don't insert watchpoints if we're trying to step past the
2129 instruction that triggered one. */
2130 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2131 && stepping_past_nonsteppable_watchpoint ())
2132 {
2133 if (debug_infrun)
2134 {
2135 fprintf_unfiltered (gdb_stdlog,
2136 "infrun: stepping past non-steppable watchpoint. "
2137 "skipping watchpoint at %s:%d\n",
2138 paddress (bl->gdbarch, bl->address),
2139 bl->length);
2140 }
2141 return 0;
2142 }
2143
2144 return 1;
2145 }
2146
2147 /* Same as should_be_inserted but does the check assuming
2148 that the location is not duplicated. */
2149
2150 static int
2151 unduplicated_should_be_inserted (struct bp_location *bl)
2152 {
2153 int result;
2154 const int save_duplicate = bl->duplicate;
2155
2156 bl->duplicate = 0;
2157 result = should_be_inserted (bl);
2158 bl->duplicate = save_duplicate;
2159 return result;
2160 }
2161
2162 /* Parses a conditional described by an expression COND into an
2163 agent expression bytecode suitable for evaluation
2164 by the bytecode interpreter. Return NULL if there was
2165 any error during parsing. */
2166
2167 static agent_expr_up
2168 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2169 {
2170 if (cond == NULL)
2171 return NULL;
2172
2173 agent_expr_up aexpr;
2174
2175 /* We don't want to stop processing, so catch any errors
2176 that may show up. */
2177 TRY
2178 {
2179 aexpr = gen_eval_for_expr (scope, cond);
2180 }
2181
2182 CATCH (ex, RETURN_MASK_ERROR)
2183 {
2184 /* If we got here, it means the condition could not be parsed to a valid
2185 bytecode expression and thus can't be evaluated on the target's side.
2186 It's no use iterating through the conditions. */
2187 }
2188 END_CATCH
2189
2190 /* We have a valid agent expression. */
2191 return aexpr;
2192 }
2193
2194 /* Based on location BL, create a list of breakpoint conditions to be
2195 passed on to the target. If we have duplicated locations with different
2196 conditions, we will add such conditions to the list. The idea is that the
2197 target will evaluate the list of conditions and will only notify GDB when
2198 one of them is true. */
2199
2200 static void
2201 build_target_condition_list (struct bp_location *bl)
2202 {
2203 struct bp_location **locp = NULL, **loc2p;
2204 int null_condition_or_parse_error = 0;
2205 int modified = bl->needs_update;
2206 struct bp_location *loc;
2207
2208 /* Release conditions left over from a previous insert. */
2209 bl->target_info.conditions.clear ();
2210
2211 /* This is only meaningful if the target is
2212 evaluating conditions and if the user has
2213 opted for condition evaluation on the target's
2214 side. */
2215 if (gdb_evaluates_breakpoint_condition_p ()
2216 || !target_supports_evaluation_of_breakpoint_conditions ())
2217 return;
2218
2219 /* Do a first pass to check for locations with no assigned
2220 conditions or conditions that fail to parse to a valid agent expression
2221 bytecode. If any of these happen, then it's no use to send conditions
2222 to the target since this location will always trigger and generate a
2223 response back to GDB. */
2224 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2225 {
2226 loc = (*loc2p);
2227 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2228 {
2229 if (modified)
2230 {
2231 /* Re-parse the conditions since something changed. In that
2232 case we already freed the condition bytecodes (see
2233 force_breakpoint_reinsertion). We just
2234 need to parse the condition to bytecodes again. */
2235 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2236 loc->cond.get ());
2237 }
2238
2239 /* If we have a NULL bytecode expression, it means something
2240 went wrong or we have a null condition expression. */
2241 if (!loc->cond_bytecode)
2242 {
2243 null_condition_or_parse_error = 1;
2244 break;
2245 }
2246 }
2247 }
2248
2249 /* If any of these happened, it means we will have to evaluate the conditions
2250 for the location's address on gdb's side. It is no use keeping bytecodes
2251 for all the other duplicate locations, thus we free all of them here.
2252
2253 This is so we have a finer control over which locations' conditions are
2254 being evaluated by GDB or the remote stub. */
2255 if (null_condition_or_parse_error)
2256 {
2257 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2258 {
2259 loc = (*loc2p);
2260 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2261 {
2262 /* Only go as far as the first NULL bytecode is
2263 located. */
2264 if (!loc->cond_bytecode)
2265 return;
2266
2267 loc->cond_bytecode.reset ();
2268 }
2269 }
2270 }
2271
2272 /* No NULL conditions or failed bytecode generation. Build a condition list
2273 for this location's address. */
2274 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2275 {
2276 loc = (*loc2p);
2277 if (loc->cond
2278 && is_breakpoint (loc->owner)
2279 && loc->pspace->num == bl->pspace->num
2280 && loc->owner->enable_state == bp_enabled
2281 && loc->enabled)
2282 {
2283 /* Add the condition to the vector. This will be used later
2284 to send the conditions to the target. */
2285 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2286 }
2287 }
2288
2289 return;
2290 }
2291
2292 /* Parses a command described by string CMD into an agent expression
2293 bytecode suitable for evaluation by the bytecode interpreter.
2294 Return NULL if there was any error during parsing. */
2295
2296 static agent_expr_up
2297 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2298 {
2299 struct cleanup *old_cleanups = 0;
2300 struct expression **argvec;
2301 const char *cmdrest;
2302 const char *format_start, *format_end;
2303 struct format_piece *fpieces;
2304 int nargs;
2305 struct gdbarch *gdbarch = get_current_arch ();
2306
2307 if (cmd == NULL)
2308 return NULL;
2309
2310 cmdrest = cmd;
2311
2312 if (*cmdrest == ',')
2313 ++cmdrest;
2314 cmdrest = skip_spaces (cmdrest);
2315
2316 if (*cmdrest++ != '"')
2317 error (_("No format string following the location"));
2318
2319 format_start = cmdrest;
2320
2321 fpieces = parse_format_string (&cmdrest);
2322
2323 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2324
2325 format_end = cmdrest;
2326
2327 if (*cmdrest++ != '"')
2328 error (_("Bad format string, non-terminated '\"'."));
2329
2330 cmdrest = skip_spaces (cmdrest);
2331
2332 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2333 error (_("Invalid argument syntax"));
2334
2335 if (*cmdrest == ',')
2336 cmdrest++;
2337 cmdrest = skip_spaces (cmdrest);
2338
2339 /* For each argument, make an expression. */
2340
2341 argvec = (struct expression **) alloca (strlen (cmd)
2342 * sizeof (struct expression *));
2343
2344 nargs = 0;
2345 while (*cmdrest != '\0')
2346 {
2347 const char *cmd1;
2348
2349 cmd1 = cmdrest;
2350 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2351 argvec[nargs++] = expr.release ();
2352 cmdrest = cmd1;
2353 if (*cmdrest == ',')
2354 ++cmdrest;
2355 }
2356
2357 agent_expr_up aexpr;
2358
2359 /* We don't want to stop processing, so catch any errors
2360 that may show up. */
2361 TRY
2362 {
2363 aexpr = gen_printf (scope, gdbarch, 0, 0,
2364 format_start, format_end - format_start,
2365 fpieces, nargs, argvec);
2366 }
2367 CATCH (ex, RETURN_MASK_ERROR)
2368 {
2369 /* If we got here, it means the command could not be parsed to a valid
2370 bytecode expression and thus can't be evaluated on the target's side.
2371 It's no use iterating through the other commands. */
2372 }
2373 END_CATCH
2374
2375 do_cleanups (old_cleanups);
2376
2377 /* We have a valid agent expression, return it. */
2378 return aexpr;
2379 }
2380
2381 /* Based on location BL, create a list of breakpoint commands to be
2382 passed on to the target. If we have duplicated locations with
2383 different commands, we will add any such to the list. */
2384
2385 static void
2386 build_target_command_list (struct bp_location *bl)
2387 {
2388 struct bp_location **locp = NULL, **loc2p;
2389 int null_command_or_parse_error = 0;
2390 int modified = bl->needs_update;
2391 struct bp_location *loc;
2392
2393 /* Clear commands left over from a previous insert. */
2394 bl->target_info.tcommands.clear ();
2395
2396 if (!target_can_run_breakpoint_commands ())
2397 return;
2398
2399 /* For now, limit to agent-style dprintf breakpoints. */
2400 if (dprintf_style != dprintf_style_agent)
2401 return;
2402
2403 /* For now, if we have any duplicate location that isn't a dprintf,
2404 don't install the target-side commands, as that would make the
2405 breakpoint not be reported to the core, and we'd lose
2406 control. */
2407 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2408 {
2409 loc = (*loc2p);
2410 if (is_breakpoint (loc->owner)
2411 && loc->pspace->num == bl->pspace->num
2412 && loc->owner->type != bp_dprintf)
2413 return;
2414 }
2415
2416 /* Do a first pass to check for locations with no assigned
2417 conditions or conditions that fail to parse to a valid agent expression
2418 bytecode. If any of these happen, then it's no use to send conditions
2419 to the target since this location will always trigger and generate a
2420 response back to GDB. */
2421 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2422 {
2423 loc = (*loc2p);
2424 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2425 {
2426 if (modified)
2427 {
2428 /* Re-parse the commands since something changed. In that
2429 case we already freed the command bytecodes (see
2430 force_breakpoint_reinsertion). We just
2431 need to parse the command to bytecodes again. */
2432 loc->cmd_bytecode
2433 = parse_cmd_to_aexpr (bl->address,
2434 loc->owner->extra_string);
2435 }
2436
2437 /* If we have a NULL bytecode expression, it means something
2438 went wrong or we have a null command expression. */
2439 if (!loc->cmd_bytecode)
2440 {
2441 null_command_or_parse_error = 1;
2442 break;
2443 }
2444 }
2445 }
2446
2447 /* If anything failed, then we're not doing target-side commands,
2448 and so clean up. */
2449 if (null_command_or_parse_error)
2450 {
2451 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2452 {
2453 loc = (*loc2p);
2454 if (is_breakpoint (loc->owner)
2455 && loc->pspace->num == bl->pspace->num)
2456 {
2457 /* Only go as far as the first NULL bytecode is
2458 located. */
2459 if (loc->cmd_bytecode == NULL)
2460 return;
2461
2462 loc->cmd_bytecode.reset ();
2463 }
2464 }
2465 }
2466
2467 /* No NULL commands or failed bytecode generation. Build a command list
2468 for this location's address. */
2469 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2470 {
2471 loc = (*loc2p);
2472 if (loc->owner->extra_string
2473 && is_breakpoint (loc->owner)
2474 && loc->pspace->num == bl->pspace->num
2475 && loc->owner->enable_state == bp_enabled
2476 && loc->enabled)
2477 {
2478 /* Add the command to the vector. This will be used later
2479 to send the commands to the target. */
2480 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2481 }
2482 }
2483
2484 bl->target_info.persist = 0;
2485 /* Maybe flag this location as persistent. */
2486 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2487 bl->target_info.persist = 1;
2488 }
2489
2490 /* Return the kind of breakpoint on address *ADDR. Get the kind
2491 of breakpoint according to ADDR except single-step breakpoint.
2492 Get the kind of single-step breakpoint according to the current
2493 registers state. */
2494
2495 static int
2496 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2497 {
2498 if (bl->owner->type == bp_single_step)
2499 {
2500 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2501 struct regcache *regcache;
2502
2503 regcache = get_thread_regcache (thr->ptid);
2504
2505 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2506 regcache, addr);
2507 }
2508 else
2509 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2510 }
2511
2512 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2513 location. Any error messages are printed to TMP_ERROR_STREAM; and
2514 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2515 Returns 0 for success, 1 if the bp_location type is not supported or
2516 -1 for failure.
2517
2518 NOTE drow/2003-09-09: This routine could be broken down to an
2519 object-style method for each breakpoint or catchpoint type. */
2520 static int
2521 insert_bp_location (struct bp_location *bl,
2522 struct ui_file *tmp_error_stream,
2523 int *disabled_breaks,
2524 int *hw_breakpoint_error,
2525 int *hw_bp_error_explained_already)
2526 {
2527 enum errors bp_err = GDB_NO_ERROR;
2528 const char *bp_err_message = NULL;
2529
2530 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2531 return 0;
2532
2533 /* Note we don't initialize bl->target_info, as that wipes out
2534 the breakpoint location's shadow_contents if the breakpoint
2535 is still inserted at that location. This in turn breaks
2536 target_read_memory which depends on these buffers when
2537 a memory read is requested at the breakpoint location:
2538 Once the target_info has been wiped, we fail to see that
2539 we have a breakpoint inserted at that address and thus
2540 read the breakpoint instead of returning the data saved in
2541 the breakpoint location's shadow contents. */
2542 bl->target_info.reqstd_address = bl->address;
2543 bl->target_info.placed_address_space = bl->pspace->aspace;
2544 bl->target_info.length = bl->length;
2545
2546 /* When working with target-side conditions, we must pass all the conditions
2547 for the same breakpoint address down to the target since GDB will not
2548 insert those locations. With a list of breakpoint conditions, the target
2549 can decide when to stop and notify GDB. */
2550
2551 if (is_breakpoint (bl->owner))
2552 {
2553 build_target_condition_list (bl);
2554 build_target_command_list (bl);
2555 /* Reset the modification marker. */
2556 bl->needs_update = 0;
2557 }
2558
2559 if (bl->loc_type == bp_loc_software_breakpoint
2560 || bl->loc_type == bp_loc_hardware_breakpoint)
2561 {
2562 if (bl->owner->type != bp_hardware_breakpoint)
2563 {
2564 /* If the explicitly specified breakpoint type
2565 is not hardware breakpoint, check the memory map to see
2566 if the breakpoint address is in read only memory or not.
2567
2568 Two important cases are:
2569 - location type is not hardware breakpoint, memory
2570 is readonly. We change the type of the location to
2571 hardware breakpoint.
2572 - location type is hardware breakpoint, memory is
2573 read-write. This means we've previously made the
2574 location hardware one, but then the memory map changed,
2575 so we undo.
2576
2577 When breakpoints are removed, remove_breakpoints will use
2578 location types we've just set here, the only possible
2579 problem is that memory map has changed during running
2580 program, but it's not going to work anyway with current
2581 gdb. */
2582 struct mem_region *mr
2583 = lookup_mem_region (bl->target_info.reqstd_address);
2584
2585 if (mr)
2586 {
2587 if (automatic_hardware_breakpoints)
2588 {
2589 enum bp_loc_type new_type;
2590
2591 if (mr->attrib.mode != MEM_RW)
2592 new_type = bp_loc_hardware_breakpoint;
2593 else
2594 new_type = bp_loc_software_breakpoint;
2595
2596 if (new_type != bl->loc_type)
2597 {
2598 static int said = 0;
2599
2600 bl->loc_type = new_type;
2601 if (!said)
2602 {
2603 fprintf_filtered (gdb_stdout,
2604 _("Note: automatically using "
2605 "hardware breakpoints for "
2606 "read-only addresses.\n"));
2607 said = 1;
2608 }
2609 }
2610 }
2611 else if (bl->loc_type == bp_loc_software_breakpoint
2612 && mr->attrib.mode != MEM_RW)
2613 {
2614 fprintf_unfiltered (tmp_error_stream,
2615 _("Cannot insert breakpoint %d.\n"
2616 "Cannot set software breakpoint "
2617 "at read-only address %s\n"),
2618 bl->owner->number,
2619 paddress (bl->gdbarch, bl->address));
2620 return 1;
2621 }
2622 }
2623 }
2624
2625 /* First check to see if we have to handle an overlay. */
2626 if (overlay_debugging == ovly_off
2627 || bl->section == NULL
2628 || !(section_is_overlay (bl->section)))
2629 {
2630 /* No overlay handling: just set the breakpoint. */
2631 TRY
2632 {
2633 int val;
2634
2635 val = bl->owner->ops->insert_location (bl);
2636 if (val)
2637 bp_err = GENERIC_ERROR;
2638 }
2639 CATCH (e, RETURN_MASK_ALL)
2640 {
2641 bp_err = e.error;
2642 bp_err_message = e.message;
2643 }
2644 END_CATCH
2645 }
2646 else
2647 {
2648 /* This breakpoint is in an overlay section.
2649 Shall we set a breakpoint at the LMA? */
2650 if (!overlay_events_enabled)
2651 {
2652 /* Yes -- overlay event support is not active,
2653 so we must try to set a breakpoint at the LMA.
2654 This will not work for a hardware breakpoint. */
2655 if (bl->loc_type == bp_loc_hardware_breakpoint)
2656 warning (_("hardware breakpoint %d not supported in overlay!"),
2657 bl->owner->number);
2658 else
2659 {
2660 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2661 bl->section);
2662 /* Set a software (trap) breakpoint at the LMA. */
2663 bl->overlay_target_info = bl->target_info;
2664 bl->overlay_target_info.reqstd_address = addr;
2665
2666 /* No overlay handling: just set the breakpoint. */
2667 TRY
2668 {
2669 int val;
2670
2671 bl->overlay_target_info.kind
2672 = breakpoint_kind (bl, &addr);
2673 bl->overlay_target_info.placed_address = addr;
2674 val = target_insert_breakpoint (bl->gdbarch,
2675 &bl->overlay_target_info);
2676 if (val)
2677 bp_err = GENERIC_ERROR;
2678 }
2679 CATCH (e, RETURN_MASK_ALL)
2680 {
2681 bp_err = e.error;
2682 bp_err_message = e.message;
2683 }
2684 END_CATCH
2685
2686 if (bp_err != GDB_NO_ERROR)
2687 fprintf_unfiltered (tmp_error_stream,
2688 "Overlay breakpoint %d "
2689 "failed: in ROM?\n",
2690 bl->owner->number);
2691 }
2692 }
2693 /* Shall we set a breakpoint at the VMA? */
2694 if (section_is_mapped (bl->section))
2695 {
2696 /* Yes. This overlay section is mapped into memory. */
2697 TRY
2698 {
2699 int val;
2700
2701 val = bl->owner->ops->insert_location (bl);
2702 if (val)
2703 bp_err = GENERIC_ERROR;
2704 }
2705 CATCH (e, RETURN_MASK_ALL)
2706 {
2707 bp_err = e.error;
2708 bp_err_message = e.message;
2709 }
2710 END_CATCH
2711 }
2712 else
2713 {
2714 /* No. This breakpoint will not be inserted.
2715 No error, but do not mark the bp as 'inserted'. */
2716 return 0;
2717 }
2718 }
2719
2720 if (bp_err != GDB_NO_ERROR)
2721 {
2722 /* Can't set the breakpoint. */
2723
2724 /* In some cases, we might not be able to insert a
2725 breakpoint in a shared library that has already been
2726 removed, but we have not yet processed the shlib unload
2727 event. Unfortunately, some targets that implement
2728 breakpoint insertion themselves can't tell why the
2729 breakpoint insertion failed (e.g., the remote target
2730 doesn't define error codes), so we must treat generic
2731 errors as memory errors. */
2732 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2733 && bl->loc_type == bp_loc_software_breakpoint
2734 && (solib_name_from_address (bl->pspace, bl->address)
2735 || shared_objfile_contains_address_p (bl->pspace,
2736 bl->address)))
2737 {
2738 /* See also: disable_breakpoints_in_shlibs. */
2739 bl->shlib_disabled = 1;
2740 observer_notify_breakpoint_modified (bl->owner);
2741 if (!*disabled_breaks)
2742 {
2743 fprintf_unfiltered (tmp_error_stream,
2744 "Cannot insert breakpoint %d.\n",
2745 bl->owner->number);
2746 fprintf_unfiltered (tmp_error_stream,
2747 "Temporarily disabling shared "
2748 "library breakpoints:\n");
2749 }
2750 *disabled_breaks = 1;
2751 fprintf_unfiltered (tmp_error_stream,
2752 "breakpoint #%d\n", bl->owner->number);
2753 return 0;
2754 }
2755 else
2756 {
2757 if (bl->loc_type == bp_loc_hardware_breakpoint)
2758 {
2759 *hw_breakpoint_error = 1;
2760 *hw_bp_error_explained_already = bp_err_message != NULL;
2761 fprintf_unfiltered (tmp_error_stream,
2762 "Cannot insert hardware breakpoint %d%s",
2763 bl->owner->number, bp_err_message ? ":" : ".\n");
2764 if (bp_err_message != NULL)
2765 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2766 }
2767 else
2768 {
2769 if (bp_err_message == NULL)
2770 {
2771 std::string message
2772 = memory_error_message (TARGET_XFER_E_IO,
2773 bl->gdbarch, bl->address);
2774
2775 fprintf_unfiltered (tmp_error_stream,
2776 "Cannot insert breakpoint %d.\n"
2777 "%s\n",
2778 bl->owner->number, message.c_str ());
2779 }
2780 else
2781 {
2782 fprintf_unfiltered (tmp_error_stream,
2783 "Cannot insert breakpoint %d: %s\n",
2784 bl->owner->number,
2785 bp_err_message);
2786 }
2787 }
2788 return 1;
2789
2790 }
2791 }
2792 else
2793 bl->inserted = 1;
2794
2795 return 0;
2796 }
2797
2798 else if (bl->loc_type == bp_loc_hardware_watchpoint
2799 /* NOTE drow/2003-09-08: This state only exists for removing
2800 watchpoints. It's not clear that it's necessary... */
2801 && bl->owner->disposition != disp_del_at_next_stop)
2802 {
2803 int val;
2804
2805 gdb_assert (bl->owner->ops != NULL
2806 && bl->owner->ops->insert_location != NULL);
2807
2808 val = bl->owner->ops->insert_location (bl);
2809
2810 /* If trying to set a read-watchpoint, and it turns out it's not
2811 supported, try emulating one with an access watchpoint. */
2812 if (val == 1 && bl->watchpoint_type == hw_read)
2813 {
2814 struct bp_location *loc, **loc_temp;
2815
2816 /* But don't try to insert it, if there's already another
2817 hw_access location that would be considered a duplicate
2818 of this one. */
2819 ALL_BP_LOCATIONS (loc, loc_temp)
2820 if (loc != bl
2821 && loc->watchpoint_type == hw_access
2822 && watchpoint_locations_match (bl, loc))
2823 {
2824 bl->duplicate = 1;
2825 bl->inserted = 1;
2826 bl->target_info = loc->target_info;
2827 bl->watchpoint_type = hw_access;
2828 val = 0;
2829 break;
2830 }
2831
2832 if (val == 1)
2833 {
2834 bl->watchpoint_type = hw_access;
2835 val = bl->owner->ops->insert_location (bl);
2836
2837 if (val)
2838 /* Back to the original value. */
2839 bl->watchpoint_type = hw_read;
2840 }
2841 }
2842
2843 bl->inserted = (val == 0);
2844 }
2845
2846 else if (bl->owner->type == bp_catchpoint)
2847 {
2848 int val;
2849
2850 gdb_assert (bl->owner->ops != NULL
2851 && bl->owner->ops->insert_location != NULL);
2852
2853 val = bl->owner->ops->insert_location (bl);
2854 if (val)
2855 {
2856 bl->owner->enable_state = bp_disabled;
2857
2858 if (val == 1)
2859 warning (_("\
2860 Error inserting catchpoint %d: Your system does not support this type\n\
2861 of catchpoint."), bl->owner->number);
2862 else
2863 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2864 }
2865
2866 bl->inserted = (val == 0);
2867
2868 /* We've already printed an error message if there was a problem
2869 inserting this catchpoint, and we've disabled the catchpoint,
2870 so just return success. */
2871 return 0;
2872 }
2873
2874 return 0;
2875 }
2876
2877 /* This function is called when program space PSPACE is about to be
2878 deleted. It takes care of updating breakpoints to not reference
2879 PSPACE anymore. */
2880
2881 void
2882 breakpoint_program_space_exit (struct program_space *pspace)
2883 {
2884 struct breakpoint *b, *b_temp;
2885 struct bp_location *loc, **loc_temp;
2886
2887 /* Remove any breakpoint that was set through this program space. */
2888 ALL_BREAKPOINTS_SAFE (b, b_temp)
2889 {
2890 if (b->pspace == pspace)
2891 delete_breakpoint (b);
2892 }
2893
2894 /* Breakpoints set through other program spaces could have locations
2895 bound to PSPACE as well. Remove those. */
2896 ALL_BP_LOCATIONS (loc, loc_temp)
2897 {
2898 struct bp_location *tmp;
2899
2900 if (loc->pspace == pspace)
2901 {
2902 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2903 if (loc->owner->loc == loc)
2904 loc->owner->loc = loc->next;
2905 else
2906 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2907 if (tmp->next == loc)
2908 {
2909 tmp->next = loc->next;
2910 break;
2911 }
2912 }
2913 }
2914
2915 /* Now update the global location list to permanently delete the
2916 removed locations above. */
2917 update_global_location_list (UGLL_DONT_INSERT);
2918 }
2919
2920 /* Make sure all breakpoints are inserted in inferior.
2921 Throws exception on any error.
2922 A breakpoint that is already inserted won't be inserted
2923 again, so calling this function twice is safe. */
2924 void
2925 insert_breakpoints (void)
2926 {
2927 struct breakpoint *bpt;
2928
2929 ALL_BREAKPOINTS (bpt)
2930 if (is_hardware_watchpoint (bpt))
2931 {
2932 struct watchpoint *w = (struct watchpoint *) bpt;
2933
2934 update_watchpoint (w, 0 /* don't reparse. */);
2935 }
2936
2937 /* Updating watchpoints creates new locations, so update the global
2938 location list. Explicitly tell ugll to insert locations and
2939 ignore breakpoints_always_inserted_mode. */
2940 update_global_location_list (UGLL_INSERT);
2941 }
2942
2943 /* Invoke CALLBACK for each of bp_location. */
2944
2945 void
2946 iterate_over_bp_locations (walk_bp_location_callback callback)
2947 {
2948 struct bp_location *loc, **loc_tmp;
2949
2950 ALL_BP_LOCATIONS (loc, loc_tmp)
2951 {
2952 callback (loc, NULL);
2953 }
2954 }
2955
2956 /* This is used when we need to synch breakpoint conditions between GDB and the
2957 target. It is the case with deleting and disabling of breakpoints when using
2958 always-inserted mode. */
2959
2960 static void
2961 update_inserted_breakpoint_locations (void)
2962 {
2963 struct bp_location *bl, **blp_tmp;
2964 int error_flag = 0;
2965 int val = 0;
2966 int disabled_breaks = 0;
2967 int hw_breakpoint_error = 0;
2968 int hw_bp_details_reported = 0;
2969
2970 string_file tmp_error_stream;
2971
2972 /* Explicitly mark the warning -- this will only be printed if
2973 there was an error. */
2974 tmp_error_stream.puts ("Warning:\n");
2975
2976 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2977
2978 ALL_BP_LOCATIONS (bl, blp_tmp)
2979 {
2980 /* We only want to update software breakpoints and hardware
2981 breakpoints. */
2982 if (!is_breakpoint (bl->owner))
2983 continue;
2984
2985 /* We only want to update locations that are already inserted
2986 and need updating. This is to avoid unwanted insertion during
2987 deletion of breakpoints. */
2988 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2989 continue;
2990
2991 switch_to_program_space_and_thread (bl->pspace);
2992
2993 /* For targets that support global breakpoints, there's no need
2994 to select an inferior to insert breakpoint to. In fact, even
2995 if we aren't attached to any process yet, we should still
2996 insert breakpoints. */
2997 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2998 && ptid_equal (inferior_ptid, null_ptid))
2999 continue;
3000
3001 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3002 &hw_breakpoint_error, &hw_bp_details_reported);
3003 if (val)
3004 error_flag = val;
3005 }
3006
3007 if (error_flag)
3008 {
3009 target_terminal::ours_for_output ();
3010 error_stream (tmp_error_stream);
3011 }
3012 }
3013
3014 /* Used when starting or continuing the program. */
3015
3016 static void
3017 insert_breakpoint_locations (void)
3018 {
3019 struct breakpoint *bpt;
3020 struct bp_location *bl, **blp_tmp;
3021 int error_flag = 0;
3022 int val = 0;
3023 int disabled_breaks = 0;
3024 int hw_breakpoint_error = 0;
3025 int hw_bp_error_explained_already = 0;
3026
3027 string_file tmp_error_stream;
3028
3029 /* Explicitly mark the warning -- this will only be printed if
3030 there was an error. */
3031 tmp_error_stream.puts ("Warning:\n");
3032
3033 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3034
3035 ALL_BP_LOCATIONS (bl, blp_tmp)
3036 {
3037 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3038 continue;
3039
3040 /* There is no point inserting thread-specific breakpoints if
3041 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3042 has BL->OWNER always non-NULL. */
3043 if (bl->owner->thread != -1
3044 && !valid_global_thread_id (bl->owner->thread))
3045 continue;
3046
3047 switch_to_program_space_and_thread (bl->pspace);
3048
3049 /* For targets that support global breakpoints, there's no need
3050 to select an inferior to insert breakpoint to. In fact, even
3051 if we aren't attached to any process yet, we should still
3052 insert breakpoints. */
3053 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3054 && ptid_equal (inferior_ptid, null_ptid))
3055 continue;
3056
3057 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3058 &hw_breakpoint_error, &hw_bp_error_explained_already);
3059 if (val)
3060 error_flag = val;
3061 }
3062
3063 /* If we failed to insert all locations of a watchpoint, remove
3064 them, as half-inserted watchpoint is of limited use. */
3065 ALL_BREAKPOINTS (bpt)
3066 {
3067 int some_failed = 0;
3068 struct bp_location *loc;
3069
3070 if (!is_hardware_watchpoint (bpt))
3071 continue;
3072
3073 if (!breakpoint_enabled (bpt))
3074 continue;
3075
3076 if (bpt->disposition == disp_del_at_next_stop)
3077 continue;
3078
3079 for (loc = bpt->loc; loc; loc = loc->next)
3080 if (!loc->inserted && should_be_inserted (loc))
3081 {
3082 some_failed = 1;
3083 break;
3084 }
3085 if (some_failed)
3086 {
3087 for (loc = bpt->loc; loc; loc = loc->next)
3088 if (loc->inserted)
3089 remove_breakpoint (loc);
3090
3091 hw_breakpoint_error = 1;
3092 tmp_error_stream.printf ("Could not insert "
3093 "hardware watchpoint %d.\n",
3094 bpt->number);
3095 error_flag = -1;
3096 }
3097 }
3098
3099 if (error_flag)
3100 {
3101 /* If a hardware breakpoint or watchpoint was inserted, add a
3102 message about possibly exhausted resources. */
3103 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3104 {
3105 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3106 You may have requested too many hardware breakpoints/watchpoints.\n");
3107 }
3108 target_terminal::ours_for_output ();
3109 error_stream (tmp_error_stream);
3110 }
3111 }
3112
3113 /* Used when the program stops.
3114 Returns zero if successful, or non-zero if there was a problem
3115 removing a breakpoint location. */
3116
3117 int
3118 remove_breakpoints (void)
3119 {
3120 struct bp_location *bl, **blp_tmp;
3121 int val = 0;
3122
3123 ALL_BP_LOCATIONS (bl, blp_tmp)
3124 {
3125 if (bl->inserted && !is_tracepoint (bl->owner))
3126 val |= remove_breakpoint (bl);
3127 }
3128 return val;
3129 }
3130
3131 /* When a thread exits, remove breakpoints that are related to
3132 that thread. */
3133
3134 static void
3135 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3136 {
3137 struct breakpoint *b, *b_tmp;
3138
3139 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3140 {
3141 if (b->thread == tp->global_num && user_breakpoint_p (b))
3142 {
3143 b->disposition = disp_del_at_next_stop;
3144
3145 printf_filtered (_("\
3146 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3147 b->number, print_thread_id (tp));
3148
3149 /* Hide it from the user. */
3150 b->number = 0;
3151 }
3152 }
3153 }
3154
3155 /* Remove breakpoints of process PID. */
3156
3157 int
3158 remove_breakpoints_pid (int pid)
3159 {
3160 struct bp_location *bl, **blp_tmp;
3161 int val;
3162 struct inferior *inf = find_inferior_pid (pid);
3163
3164 ALL_BP_LOCATIONS (bl, blp_tmp)
3165 {
3166 if (bl->pspace != inf->pspace)
3167 continue;
3168
3169 if (bl->inserted && !bl->target_info.persist)
3170 {
3171 val = remove_breakpoint (bl);
3172 if (val != 0)
3173 return val;
3174 }
3175 }
3176 return 0;
3177 }
3178
3179 int
3180 reattach_breakpoints (int pid)
3181 {
3182 struct bp_location *bl, **blp_tmp;
3183 int val;
3184 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3185 struct inferior *inf;
3186 struct thread_info *tp;
3187
3188 tp = any_live_thread_of_process (pid);
3189 if (tp == NULL)
3190 return 1;
3191
3192 inf = find_inferior_pid (pid);
3193
3194 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3195 inferior_ptid = tp->ptid;
3196
3197 string_file tmp_error_stream;
3198
3199 ALL_BP_LOCATIONS (bl, blp_tmp)
3200 {
3201 if (bl->pspace != inf->pspace)
3202 continue;
3203
3204 if (bl->inserted)
3205 {
3206 bl->inserted = 0;
3207 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
3208 if (val != 0)
3209 return val;
3210 }
3211 }
3212 return 0;
3213 }
3214
3215 static int internal_breakpoint_number = -1;
3216
3217 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3218 If INTERNAL is non-zero, the breakpoint number will be populated
3219 from internal_breakpoint_number and that variable decremented.
3220 Otherwise the breakpoint number will be populated from
3221 breakpoint_count and that value incremented. Internal breakpoints
3222 do not set the internal var bpnum. */
3223 static void
3224 set_breakpoint_number (int internal, struct breakpoint *b)
3225 {
3226 if (internal)
3227 b->number = internal_breakpoint_number--;
3228 else
3229 {
3230 set_breakpoint_count (breakpoint_count + 1);
3231 b->number = breakpoint_count;
3232 }
3233 }
3234
3235 static struct breakpoint *
3236 create_internal_breakpoint (struct gdbarch *gdbarch,
3237 CORE_ADDR address, enum bptype type,
3238 const struct breakpoint_ops *ops)
3239 {
3240 symtab_and_line sal;
3241 sal.pc = address;
3242 sal.section = find_pc_overlay (sal.pc);
3243 sal.pspace = current_program_space;
3244
3245 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3246 b->number = internal_breakpoint_number--;
3247 b->disposition = disp_donttouch;
3248
3249 return b;
3250 }
3251
3252 static const char *const longjmp_names[] =
3253 {
3254 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3255 };
3256 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3257
3258 /* Per-objfile data private to breakpoint.c. */
3259 struct breakpoint_objfile_data
3260 {
3261 /* Minimal symbol for "_ovly_debug_event" (if any). */
3262 struct bound_minimal_symbol overlay_msym;
3263
3264 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3265 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3266
3267 /* True if we have looked for longjmp probes. */
3268 int longjmp_searched;
3269
3270 /* SystemTap probe points for longjmp (if any). */
3271 VEC (probe_p) *longjmp_probes;
3272
3273 /* Minimal symbol for "std::terminate()" (if any). */
3274 struct bound_minimal_symbol terminate_msym;
3275
3276 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3277 struct bound_minimal_symbol exception_msym;
3278
3279 /* True if we have looked for exception probes. */
3280 int exception_searched;
3281
3282 /* SystemTap probe points for unwinding (if any). */
3283 VEC (probe_p) *exception_probes;
3284 };
3285
3286 static const struct objfile_data *breakpoint_objfile_key;
3287
3288 /* Minimal symbol not found sentinel. */
3289 static struct minimal_symbol msym_not_found;
3290
3291 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3292
3293 static int
3294 msym_not_found_p (const struct minimal_symbol *msym)
3295 {
3296 return msym == &msym_not_found;
3297 }
3298
3299 /* Return per-objfile data needed by breakpoint.c.
3300 Allocate the data if necessary. */
3301
3302 static struct breakpoint_objfile_data *
3303 get_breakpoint_objfile_data (struct objfile *objfile)
3304 {
3305 struct breakpoint_objfile_data *bp_objfile_data;
3306
3307 bp_objfile_data = ((struct breakpoint_objfile_data *)
3308 objfile_data (objfile, breakpoint_objfile_key));
3309 if (bp_objfile_data == NULL)
3310 {
3311 bp_objfile_data =
3312 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3313
3314 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3315 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3316 }
3317 return bp_objfile_data;
3318 }
3319
3320 static void
3321 free_breakpoint_probes (struct objfile *obj, void *data)
3322 {
3323 struct breakpoint_objfile_data *bp_objfile_data
3324 = (struct breakpoint_objfile_data *) data;
3325
3326 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3327 VEC_free (probe_p, bp_objfile_data->exception_probes);
3328 }
3329
3330 static void
3331 create_overlay_event_breakpoint (void)
3332 {
3333 struct objfile *objfile;
3334 const char *const func_name = "_ovly_debug_event";
3335
3336 ALL_OBJFILES (objfile)
3337 {
3338 struct breakpoint *b;
3339 struct breakpoint_objfile_data *bp_objfile_data;
3340 CORE_ADDR addr;
3341 struct explicit_location explicit_loc;
3342
3343 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3344
3345 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3346 continue;
3347
3348 if (bp_objfile_data->overlay_msym.minsym == NULL)
3349 {
3350 struct bound_minimal_symbol m;
3351
3352 m = lookup_minimal_symbol_text (func_name, objfile);
3353 if (m.minsym == NULL)
3354 {
3355 /* Avoid future lookups in this objfile. */
3356 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3357 continue;
3358 }
3359 bp_objfile_data->overlay_msym = m;
3360 }
3361
3362 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3363 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3364 bp_overlay_event,
3365 &internal_breakpoint_ops);
3366 initialize_explicit_location (&explicit_loc);
3367 explicit_loc.function_name = ASTRDUP (func_name);
3368 b->location = new_explicit_location (&explicit_loc);
3369
3370 if (overlay_debugging == ovly_auto)
3371 {
3372 b->enable_state = bp_enabled;
3373 overlay_events_enabled = 1;
3374 }
3375 else
3376 {
3377 b->enable_state = bp_disabled;
3378 overlay_events_enabled = 0;
3379 }
3380 }
3381 }
3382
3383 static void
3384 create_longjmp_master_breakpoint (void)
3385 {
3386 struct program_space *pspace;
3387
3388 scoped_restore_current_program_space restore_pspace;
3389
3390 ALL_PSPACES (pspace)
3391 {
3392 struct objfile *objfile;
3393
3394 set_current_program_space (pspace);
3395
3396 ALL_OBJFILES (objfile)
3397 {
3398 int i;
3399 struct gdbarch *gdbarch;
3400 struct breakpoint_objfile_data *bp_objfile_data;
3401
3402 gdbarch = get_objfile_arch (objfile);
3403
3404 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3405
3406 if (!bp_objfile_data->longjmp_searched)
3407 {
3408 VEC (probe_p) *ret;
3409
3410 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3411 if (ret != NULL)
3412 {
3413 /* We are only interested in checking one element. */
3414 struct probe *p = VEC_index (probe_p, ret, 0);
3415
3416 if (!can_evaluate_probe_arguments (p))
3417 {
3418 /* We cannot use the probe interface here, because it does
3419 not know how to evaluate arguments. */
3420 VEC_free (probe_p, ret);
3421 ret = NULL;
3422 }
3423 }
3424 bp_objfile_data->longjmp_probes = ret;
3425 bp_objfile_data->longjmp_searched = 1;
3426 }
3427
3428 if (bp_objfile_data->longjmp_probes != NULL)
3429 {
3430 int i;
3431 struct probe *probe;
3432 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3433
3434 for (i = 0;
3435 VEC_iterate (probe_p,
3436 bp_objfile_data->longjmp_probes,
3437 i, probe);
3438 ++i)
3439 {
3440 struct breakpoint *b;
3441
3442 b = create_internal_breakpoint (gdbarch,
3443 get_probe_address (probe,
3444 objfile),
3445 bp_longjmp_master,
3446 &internal_breakpoint_ops);
3447 b->location = new_probe_location ("-probe-stap libc:longjmp");
3448 b->enable_state = bp_disabled;
3449 }
3450
3451 continue;
3452 }
3453
3454 if (!gdbarch_get_longjmp_target_p (gdbarch))
3455 continue;
3456
3457 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3458 {
3459 struct breakpoint *b;
3460 const char *func_name;
3461 CORE_ADDR addr;
3462 struct explicit_location explicit_loc;
3463
3464 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3465 continue;
3466
3467 func_name = longjmp_names[i];
3468 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3469 {
3470 struct bound_minimal_symbol m;
3471
3472 m = lookup_minimal_symbol_text (func_name, objfile);
3473 if (m.minsym == NULL)
3474 {
3475 /* Prevent future lookups in this objfile. */
3476 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3477 continue;
3478 }
3479 bp_objfile_data->longjmp_msym[i] = m;
3480 }
3481
3482 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3483 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3484 &internal_breakpoint_ops);
3485 initialize_explicit_location (&explicit_loc);
3486 explicit_loc.function_name = ASTRDUP (func_name);
3487 b->location = new_explicit_location (&explicit_loc);
3488 b->enable_state = bp_disabled;
3489 }
3490 }
3491 }
3492 }
3493
3494 /* Create a master std::terminate breakpoint. */
3495 static void
3496 create_std_terminate_master_breakpoint (void)
3497 {
3498 struct program_space *pspace;
3499 const char *const func_name = "std::terminate()";
3500
3501 scoped_restore_current_program_space restore_pspace;
3502
3503 ALL_PSPACES (pspace)
3504 {
3505 struct objfile *objfile;
3506 CORE_ADDR addr;
3507
3508 set_current_program_space (pspace);
3509
3510 ALL_OBJFILES (objfile)
3511 {
3512 struct breakpoint *b;
3513 struct breakpoint_objfile_data *bp_objfile_data;
3514 struct explicit_location explicit_loc;
3515
3516 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3517
3518 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3519 continue;
3520
3521 if (bp_objfile_data->terminate_msym.minsym == NULL)
3522 {
3523 struct bound_minimal_symbol m;
3524
3525 m = lookup_minimal_symbol (func_name, NULL, objfile);
3526 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3527 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3528 {
3529 /* Prevent future lookups in this objfile. */
3530 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3531 continue;
3532 }
3533 bp_objfile_data->terminate_msym = m;
3534 }
3535
3536 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3537 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3538 bp_std_terminate_master,
3539 &internal_breakpoint_ops);
3540 initialize_explicit_location (&explicit_loc);
3541 explicit_loc.function_name = ASTRDUP (func_name);
3542 b->location = new_explicit_location (&explicit_loc);
3543 b->enable_state = bp_disabled;
3544 }
3545 }
3546 }
3547
3548 /* Install a master breakpoint on the unwinder's debug hook. */
3549
3550 static void
3551 create_exception_master_breakpoint (void)
3552 {
3553 struct objfile *objfile;
3554 const char *const func_name = "_Unwind_DebugHook";
3555
3556 ALL_OBJFILES (objfile)
3557 {
3558 struct breakpoint *b;
3559 struct gdbarch *gdbarch;
3560 struct breakpoint_objfile_data *bp_objfile_data;
3561 CORE_ADDR addr;
3562 struct explicit_location explicit_loc;
3563
3564 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3565
3566 /* We prefer the SystemTap probe point if it exists. */
3567 if (!bp_objfile_data->exception_searched)
3568 {
3569 VEC (probe_p) *ret;
3570
3571 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3572
3573 if (ret != NULL)
3574 {
3575 /* We are only interested in checking one element. */
3576 struct probe *p = VEC_index (probe_p, ret, 0);
3577
3578 if (!can_evaluate_probe_arguments (p))
3579 {
3580 /* We cannot use the probe interface here, because it does
3581 not know how to evaluate arguments. */
3582 VEC_free (probe_p, ret);
3583 ret = NULL;
3584 }
3585 }
3586 bp_objfile_data->exception_probes = ret;
3587 bp_objfile_data->exception_searched = 1;
3588 }
3589
3590 if (bp_objfile_data->exception_probes != NULL)
3591 {
3592 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3593 int i;
3594 struct probe *probe;
3595
3596 for (i = 0;
3597 VEC_iterate (probe_p,
3598 bp_objfile_data->exception_probes,
3599 i, probe);
3600 ++i)
3601 {
3602 struct breakpoint *b;
3603
3604 b = create_internal_breakpoint (gdbarch,
3605 get_probe_address (probe,
3606 objfile),
3607 bp_exception_master,
3608 &internal_breakpoint_ops);
3609 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3610 b->enable_state = bp_disabled;
3611 }
3612
3613 continue;
3614 }
3615
3616 /* Otherwise, try the hook function. */
3617
3618 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3619 continue;
3620
3621 gdbarch = get_objfile_arch (objfile);
3622
3623 if (bp_objfile_data->exception_msym.minsym == NULL)
3624 {
3625 struct bound_minimal_symbol debug_hook;
3626
3627 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3628 if (debug_hook.minsym == NULL)
3629 {
3630 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3631 continue;
3632 }
3633
3634 bp_objfile_data->exception_msym = debug_hook;
3635 }
3636
3637 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3638 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3639 &current_target);
3640 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3641 &internal_breakpoint_ops);
3642 initialize_explicit_location (&explicit_loc);
3643 explicit_loc.function_name = ASTRDUP (func_name);
3644 b->location = new_explicit_location (&explicit_loc);
3645 b->enable_state = bp_disabled;
3646 }
3647 }
3648
3649 /* Does B have a location spec? */
3650
3651 static int
3652 breakpoint_event_location_empty_p (const struct breakpoint *b)
3653 {
3654 return b->location != NULL && event_location_empty_p (b->location.get ());
3655 }
3656
3657 void
3658 update_breakpoints_after_exec (void)
3659 {
3660 struct breakpoint *b, *b_tmp;
3661 struct bp_location *bploc, **bplocp_tmp;
3662
3663 /* We're about to delete breakpoints from GDB's lists. If the
3664 INSERTED flag is true, GDB will try to lift the breakpoints by
3665 writing the breakpoints' "shadow contents" back into memory. The
3666 "shadow contents" are NOT valid after an exec, so GDB should not
3667 do that. Instead, the target is responsible from marking
3668 breakpoints out as soon as it detects an exec. We don't do that
3669 here instead, because there may be other attempts to delete
3670 breakpoints after detecting an exec and before reaching here. */
3671 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3672 if (bploc->pspace == current_program_space)
3673 gdb_assert (!bploc->inserted);
3674
3675 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3676 {
3677 if (b->pspace != current_program_space)
3678 continue;
3679
3680 /* Solib breakpoints must be explicitly reset after an exec(). */
3681 if (b->type == bp_shlib_event)
3682 {
3683 delete_breakpoint (b);
3684 continue;
3685 }
3686
3687 /* JIT breakpoints must be explicitly reset after an exec(). */
3688 if (b->type == bp_jit_event)
3689 {
3690 delete_breakpoint (b);
3691 continue;
3692 }
3693
3694 /* Thread event breakpoints must be set anew after an exec(),
3695 as must overlay event and longjmp master breakpoints. */
3696 if (b->type == bp_thread_event || b->type == bp_overlay_event
3697 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3698 || b->type == bp_exception_master)
3699 {
3700 delete_breakpoint (b);
3701 continue;
3702 }
3703
3704 /* Step-resume breakpoints are meaningless after an exec(). */
3705 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3706 {
3707 delete_breakpoint (b);
3708 continue;
3709 }
3710
3711 /* Just like single-step breakpoints. */
3712 if (b->type == bp_single_step)
3713 {
3714 delete_breakpoint (b);
3715 continue;
3716 }
3717
3718 /* Longjmp and longjmp-resume breakpoints are also meaningless
3719 after an exec. */
3720 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3721 || b->type == bp_longjmp_call_dummy
3722 || b->type == bp_exception || b->type == bp_exception_resume)
3723 {
3724 delete_breakpoint (b);
3725 continue;
3726 }
3727
3728 if (b->type == bp_catchpoint)
3729 {
3730 /* For now, none of the bp_catchpoint breakpoints need to
3731 do anything at this point. In the future, if some of
3732 the catchpoints need to something, we will need to add
3733 a new method, and call this method from here. */
3734 continue;
3735 }
3736
3737 /* bp_finish is a special case. The only way we ought to be able
3738 to see one of these when an exec() has happened, is if the user
3739 caught a vfork, and then said "finish". Ordinarily a finish just
3740 carries them to the call-site of the current callee, by setting
3741 a temporary bp there and resuming. But in this case, the finish
3742 will carry them entirely through the vfork & exec.
3743
3744 We don't want to allow a bp_finish to remain inserted now. But
3745 we can't safely delete it, 'cause finish_command has a handle to
3746 the bp on a bpstat, and will later want to delete it. There's a
3747 chance (and I've seen it happen) that if we delete the bp_finish
3748 here, that its storage will get reused by the time finish_command
3749 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3750 We really must allow finish_command to delete a bp_finish.
3751
3752 In the absence of a general solution for the "how do we know
3753 it's safe to delete something others may have handles to?"
3754 problem, what we'll do here is just uninsert the bp_finish, and
3755 let finish_command delete it.
3756
3757 (We know the bp_finish is "doomed" in the sense that it's
3758 momentary, and will be deleted as soon as finish_command sees
3759 the inferior stopped. So it doesn't matter that the bp's
3760 address is probably bogus in the new a.out, unlike e.g., the
3761 solib breakpoints.) */
3762
3763 if (b->type == bp_finish)
3764 {
3765 continue;
3766 }
3767
3768 /* Without a symbolic address, we have little hope of the
3769 pre-exec() address meaning the same thing in the post-exec()
3770 a.out. */
3771 if (breakpoint_event_location_empty_p (b))
3772 {
3773 delete_breakpoint (b);
3774 continue;
3775 }
3776 }
3777 }
3778
3779 int
3780 detach_breakpoints (ptid_t ptid)
3781 {
3782 struct bp_location *bl, **blp_tmp;
3783 int val = 0;
3784 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3785 struct inferior *inf = current_inferior ();
3786
3787 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3788 error (_("Cannot detach breakpoints of inferior_ptid"));
3789
3790 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3791 inferior_ptid = ptid;
3792 ALL_BP_LOCATIONS (bl, blp_tmp)
3793 {
3794 if (bl->pspace != inf->pspace)
3795 continue;
3796
3797 /* This function must physically remove breakpoints locations
3798 from the specified ptid, without modifying the breakpoint
3799 package's state. Locations of type bp_loc_other are only
3800 maintained at GDB side. So, there is no need to remove
3801 these bp_loc_other locations. Moreover, removing these
3802 would modify the breakpoint package's state. */
3803 if (bl->loc_type == bp_loc_other)
3804 continue;
3805
3806 if (bl->inserted)
3807 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3808 }
3809
3810 return val;
3811 }
3812
3813 /* Remove the breakpoint location BL from the current address space.
3814 Note that this is used to detach breakpoints from a child fork.
3815 When we get here, the child isn't in the inferior list, and neither
3816 do we have objects to represent its address space --- we should
3817 *not* look at bl->pspace->aspace here. */
3818
3819 static int
3820 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3821 {
3822 int val;
3823
3824 /* BL is never in moribund_locations by our callers. */
3825 gdb_assert (bl->owner != NULL);
3826
3827 /* The type of none suggests that owner is actually deleted.
3828 This should not ever happen. */
3829 gdb_assert (bl->owner->type != bp_none);
3830
3831 if (bl->loc_type == bp_loc_software_breakpoint
3832 || bl->loc_type == bp_loc_hardware_breakpoint)
3833 {
3834 /* "Normal" instruction breakpoint: either the standard
3835 trap-instruction bp (bp_breakpoint), or a
3836 bp_hardware_breakpoint. */
3837
3838 /* First check to see if we have to handle an overlay. */
3839 if (overlay_debugging == ovly_off
3840 || bl->section == NULL
3841 || !(section_is_overlay (bl->section)))
3842 {
3843 /* No overlay handling: just remove the breakpoint. */
3844
3845 /* If we're trying to uninsert a memory breakpoint that we
3846 know is set in a dynamic object that is marked
3847 shlib_disabled, then either the dynamic object was
3848 removed with "remove-symbol-file" or with
3849 "nosharedlibrary". In the former case, we don't know
3850 whether another dynamic object might have loaded over the
3851 breakpoint's address -- the user might well let us know
3852 about it next with add-symbol-file (the whole point of
3853 add-symbol-file is letting the user manually maintain a
3854 list of dynamically loaded objects). If we have the
3855 breakpoint's shadow memory, that is, this is a software
3856 breakpoint managed by GDB, check whether the breakpoint
3857 is still inserted in memory, to avoid overwriting wrong
3858 code with stale saved shadow contents. Note that HW
3859 breakpoints don't have shadow memory, as they're
3860 implemented using a mechanism that is not dependent on
3861 being able to modify the target's memory, and as such
3862 they should always be removed. */
3863 if (bl->shlib_disabled
3864 && bl->target_info.shadow_len != 0
3865 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3866 val = 0;
3867 else
3868 val = bl->owner->ops->remove_location (bl, reason);
3869 }
3870 else
3871 {
3872 /* This breakpoint is in an overlay section.
3873 Did we set a breakpoint at the LMA? */
3874 if (!overlay_events_enabled)
3875 {
3876 /* Yes -- overlay event support is not active, so we
3877 should have set a breakpoint at the LMA. Remove it.
3878 */
3879 /* Ignore any failures: if the LMA is in ROM, we will
3880 have already warned when we failed to insert it. */
3881 if (bl->loc_type == bp_loc_hardware_breakpoint)
3882 target_remove_hw_breakpoint (bl->gdbarch,
3883 &bl->overlay_target_info);
3884 else
3885 target_remove_breakpoint (bl->gdbarch,
3886 &bl->overlay_target_info,
3887 reason);
3888 }
3889 /* Did we set a breakpoint at the VMA?
3890 If so, we will have marked the breakpoint 'inserted'. */
3891 if (bl->inserted)
3892 {
3893 /* Yes -- remove it. Previously we did not bother to
3894 remove the breakpoint if the section had been
3895 unmapped, but let's not rely on that being safe. We
3896 don't know what the overlay manager might do. */
3897
3898 /* However, we should remove *software* breakpoints only
3899 if the section is still mapped, or else we overwrite
3900 wrong code with the saved shadow contents. */
3901 if (bl->loc_type == bp_loc_hardware_breakpoint
3902 || section_is_mapped (bl->section))
3903 val = bl->owner->ops->remove_location (bl, reason);
3904 else
3905 val = 0;
3906 }
3907 else
3908 {
3909 /* No -- not inserted, so no need to remove. No error. */
3910 val = 0;
3911 }
3912 }
3913
3914 /* In some cases, we might not be able to remove a breakpoint in
3915 a shared library that has already been removed, but we have
3916 not yet processed the shlib unload event. Similarly for an
3917 unloaded add-symbol-file object - the user might not yet have
3918 had the chance to remove-symbol-file it. shlib_disabled will
3919 be set if the library/object has already been removed, but
3920 the breakpoint hasn't been uninserted yet, e.g., after
3921 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3922 always-inserted mode. */
3923 if (val
3924 && (bl->loc_type == bp_loc_software_breakpoint
3925 && (bl->shlib_disabled
3926 || solib_name_from_address (bl->pspace, bl->address)
3927 || shared_objfile_contains_address_p (bl->pspace,
3928 bl->address))))
3929 val = 0;
3930
3931 if (val)
3932 return val;
3933 bl->inserted = (reason == DETACH_BREAKPOINT);
3934 }
3935 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3936 {
3937 gdb_assert (bl->owner->ops != NULL
3938 && bl->owner->ops->remove_location != NULL);
3939
3940 bl->inserted = (reason == DETACH_BREAKPOINT);
3941 bl->owner->ops->remove_location (bl, reason);
3942
3943 /* Failure to remove any of the hardware watchpoints comes here. */
3944 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3945 warning (_("Could not remove hardware watchpoint %d."),
3946 bl->owner->number);
3947 }
3948 else if (bl->owner->type == bp_catchpoint
3949 && breakpoint_enabled (bl->owner)
3950 && !bl->duplicate)
3951 {
3952 gdb_assert (bl->owner->ops != NULL
3953 && bl->owner->ops->remove_location != NULL);
3954
3955 val = bl->owner->ops->remove_location (bl, reason);
3956 if (val)
3957 return val;
3958
3959 bl->inserted = (reason == DETACH_BREAKPOINT);
3960 }
3961
3962 return 0;
3963 }
3964
3965 static int
3966 remove_breakpoint (struct bp_location *bl)
3967 {
3968 /* BL is never in moribund_locations by our callers. */
3969 gdb_assert (bl->owner != NULL);
3970
3971 /* The type of none suggests that owner is actually deleted.
3972 This should not ever happen. */
3973 gdb_assert (bl->owner->type != bp_none);
3974
3975 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3976
3977 switch_to_program_space_and_thread (bl->pspace);
3978
3979 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3980 }
3981
3982 /* Clear the "inserted" flag in all breakpoints. */
3983
3984 void
3985 mark_breakpoints_out (void)
3986 {
3987 struct bp_location *bl, **blp_tmp;
3988
3989 ALL_BP_LOCATIONS (bl, blp_tmp)
3990 if (bl->pspace == current_program_space)
3991 bl->inserted = 0;
3992 }
3993
3994 /* Clear the "inserted" flag in all breakpoints and delete any
3995 breakpoints which should go away between runs of the program.
3996
3997 Plus other such housekeeping that has to be done for breakpoints
3998 between runs.
3999
4000 Note: this function gets called at the end of a run (by
4001 generic_mourn_inferior) and when a run begins (by
4002 init_wait_for_inferior). */
4003
4004
4005
4006 void
4007 breakpoint_init_inferior (enum inf_context context)
4008 {
4009 struct breakpoint *b, *b_tmp;
4010 struct bp_location *bl;
4011 int ix;
4012 struct program_space *pspace = current_program_space;
4013
4014 /* If breakpoint locations are shared across processes, then there's
4015 nothing to do. */
4016 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4017 return;
4018
4019 mark_breakpoints_out ();
4020
4021 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4022 {
4023 if (b->loc && b->loc->pspace != pspace)
4024 continue;
4025
4026 switch (b->type)
4027 {
4028 case bp_call_dummy:
4029 case bp_longjmp_call_dummy:
4030
4031 /* If the call dummy breakpoint is at the entry point it will
4032 cause problems when the inferior is rerun, so we better get
4033 rid of it. */
4034
4035 case bp_watchpoint_scope:
4036
4037 /* Also get rid of scope breakpoints. */
4038
4039 case bp_shlib_event:
4040
4041 /* Also remove solib event breakpoints. Their addresses may
4042 have changed since the last time we ran the program.
4043 Actually we may now be debugging against different target;
4044 and so the solib backend that installed this breakpoint may
4045 not be used in by the target. E.g.,
4046
4047 (gdb) file prog-linux
4048 (gdb) run # native linux target
4049 ...
4050 (gdb) kill
4051 (gdb) file prog-win.exe
4052 (gdb) tar rem :9999 # remote Windows gdbserver.
4053 */
4054
4055 case bp_step_resume:
4056
4057 /* Also remove step-resume breakpoints. */
4058
4059 case bp_single_step:
4060
4061 /* Also remove single-step breakpoints. */
4062
4063 delete_breakpoint (b);
4064 break;
4065
4066 case bp_watchpoint:
4067 case bp_hardware_watchpoint:
4068 case bp_read_watchpoint:
4069 case bp_access_watchpoint:
4070 {
4071 struct watchpoint *w = (struct watchpoint *) b;
4072
4073 /* Likewise for watchpoints on local expressions. */
4074 if (w->exp_valid_block != NULL)
4075 delete_breakpoint (b);
4076 else
4077 {
4078 /* Get rid of existing locations, which are no longer
4079 valid. New ones will be created in
4080 update_watchpoint, when the inferior is restarted.
4081 The next update_global_location_list call will
4082 garbage collect them. */
4083 b->loc = NULL;
4084
4085 if (context == inf_starting)
4086 {
4087 /* Reset val field to force reread of starting value in
4088 insert_breakpoints. */
4089 if (w->val)
4090 value_free (w->val);
4091 w->val = NULL;
4092 w->val_valid = 0;
4093 }
4094 }
4095 }
4096 break;
4097 default:
4098 break;
4099 }
4100 }
4101
4102 /* Get rid of the moribund locations. */
4103 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4104 decref_bp_location (&bl);
4105 VEC_free (bp_location_p, moribund_locations);
4106 }
4107
4108 /* These functions concern about actual breakpoints inserted in the
4109 target --- to e.g. check if we need to do decr_pc adjustment or if
4110 we need to hop over the bkpt --- so we check for address space
4111 match, not program space. */
4112
4113 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4114 exists at PC. It returns ordinary_breakpoint_here if it's an
4115 ordinary breakpoint, or permanent_breakpoint_here if it's a
4116 permanent breakpoint.
4117 - When continuing from a location with an ordinary breakpoint, we
4118 actually single step once before calling insert_breakpoints.
4119 - When continuing from a location with a permanent breakpoint, we
4120 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4121 the target, to advance the PC past the breakpoint. */
4122
4123 enum breakpoint_here
4124 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4125 {
4126 struct bp_location *bl, **blp_tmp;
4127 int any_breakpoint_here = 0;
4128
4129 ALL_BP_LOCATIONS (bl, blp_tmp)
4130 {
4131 if (bl->loc_type != bp_loc_software_breakpoint
4132 && bl->loc_type != bp_loc_hardware_breakpoint)
4133 continue;
4134
4135 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4136 if ((breakpoint_enabled (bl->owner)
4137 || bl->permanent)
4138 && breakpoint_location_address_match (bl, aspace, pc))
4139 {
4140 if (overlay_debugging
4141 && section_is_overlay (bl->section)
4142 && !section_is_mapped (bl->section))
4143 continue; /* unmapped overlay -- can't be a match */
4144 else if (bl->permanent)
4145 return permanent_breakpoint_here;
4146 else
4147 any_breakpoint_here = 1;
4148 }
4149 }
4150
4151 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4152 }
4153
4154 /* See breakpoint.h. */
4155
4156 int
4157 breakpoint_in_range_p (struct address_space *aspace,
4158 CORE_ADDR addr, ULONGEST len)
4159 {
4160 struct bp_location *bl, **blp_tmp;
4161
4162 ALL_BP_LOCATIONS (bl, blp_tmp)
4163 {
4164 if (bl->loc_type != bp_loc_software_breakpoint
4165 && bl->loc_type != bp_loc_hardware_breakpoint)
4166 continue;
4167
4168 if ((breakpoint_enabled (bl->owner)
4169 || bl->permanent)
4170 && breakpoint_location_address_range_overlap (bl, aspace,
4171 addr, len))
4172 {
4173 if (overlay_debugging
4174 && section_is_overlay (bl->section)
4175 && !section_is_mapped (bl->section))
4176 {
4177 /* Unmapped overlay -- can't be a match. */
4178 continue;
4179 }
4180
4181 return 1;
4182 }
4183 }
4184
4185 return 0;
4186 }
4187
4188 /* Return true if there's a moribund breakpoint at PC. */
4189
4190 int
4191 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4192 {
4193 struct bp_location *loc;
4194 int ix;
4195
4196 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4197 if (breakpoint_location_address_match (loc, aspace, pc))
4198 return 1;
4199
4200 return 0;
4201 }
4202
4203 /* Returns non-zero iff BL is inserted at PC, in address space
4204 ASPACE. */
4205
4206 static int
4207 bp_location_inserted_here_p (struct bp_location *bl,
4208 struct address_space *aspace, CORE_ADDR pc)
4209 {
4210 if (bl->inserted
4211 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4212 aspace, pc))
4213 {
4214 if (overlay_debugging
4215 && section_is_overlay (bl->section)
4216 && !section_is_mapped (bl->section))
4217 return 0; /* unmapped overlay -- can't be a match */
4218 else
4219 return 1;
4220 }
4221 return 0;
4222 }
4223
4224 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4225
4226 int
4227 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4228 {
4229 struct bp_location **blp, **blp_tmp = NULL;
4230
4231 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4232 {
4233 struct bp_location *bl = *blp;
4234
4235 if (bl->loc_type != bp_loc_software_breakpoint
4236 && bl->loc_type != bp_loc_hardware_breakpoint)
4237 continue;
4238
4239 if (bp_location_inserted_here_p (bl, aspace, pc))
4240 return 1;
4241 }
4242 return 0;
4243 }
4244
4245 /* This function returns non-zero iff there is a software breakpoint
4246 inserted at PC. */
4247
4248 int
4249 software_breakpoint_inserted_here_p (struct address_space *aspace,
4250 CORE_ADDR pc)
4251 {
4252 struct bp_location **blp, **blp_tmp = NULL;
4253
4254 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4255 {
4256 struct bp_location *bl = *blp;
4257
4258 if (bl->loc_type != bp_loc_software_breakpoint)
4259 continue;
4260
4261 if (bp_location_inserted_here_p (bl, aspace, pc))
4262 return 1;
4263 }
4264
4265 return 0;
4266 }
4267
4268 /* See breakpoint.h. */
4269
4270 int
4271 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4272 CORE_ADDR pc)
4273 {
4274 struct bp_location **blp, **blp_tmp = NULL;
4275
4276 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4277 {
4278 struct bp_location *bl = *blp;
4279
4280 if (bl->loc_type != bp_loc_hardware_breakpoint)
4281 continue;
4282
4283 if (bp_location_inserted_here_p (bl, aspace, pc))
4284 return 1;
4285 }
4286
4287 return 0;
4288 }
4289
4290 int
4291 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4292 CORE_ADDR addr, ULONGEST len)
4293 {
4294 struct breakpoint *bpt;
4295
4296 ALL_BREAKPOINTS (bpt)
4297 {
4298 struct bp_location *loc;
4299
4300 if (bpt->type != bp_hardware_watchpoint
4301 && bpt->type != bp_access_watchpoint)
4302 continue;
4303
4304 if (!breakpoint_enabled (bpt))
4305 continue;
4306
4307 for (loc = bpt->loc; loc; loc = loc->next)
4308 if (loc->pspace->aspace == aspace && loc->inserted)
4309 {
4310 CORE_ADDR l, h;
4311
4312 /* Check for intersection. */
4313 l = std::max<CORE_ADDR> (loc->address, addr);
4314 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4315 if (l < h)
4316 return 1;
4317 }
4318 }
4319 return 0;
4320 }
4321 \f
4322
4323 /* bpstat stuff. External routines' interfaces are documented
4324 in breakpoint.h. */
4325
4326 int
4327 is_catchpoint (struct breakpoint *ep)
4328 {
4329 return (ep->type == bp_catchpoint);
4330 }
4331
4332 /* Frees any storage that is part of a bpstat. Does not walk the
4333 'next' chain. */
4334
4335 bpstats::~bpstats ()
4336 {
4337 if (old_val != NULL)
4338 value_free (old_val);
4339 if (bp_location_at != NULL)
4340 decref_bp_location (&bp_location_at);
4341 }
4342
4343 /* Clear a bpstat so that it says we are not at any breakpoint.
4344 Also free any storage that is part of a bpstat. */
4345
4346 void
4347 bpstat_clear (bpstat *bsp)
4348 {
4349 bpstat p;
4350 bpstat q;
4351
4352 if (bsp == 0)
4353 return;
4354 p = *bsp;
4355 while (p != NULL)
4356 {
4357 q = p->next;
4358 delete p;
4359 p = q;
4360 }
4361 *bsp = NULL;
4362 }
4363
4364 bpstats::bpstats (const bpstats &other)
4365 : next (NULL),
4366 bp_location_at (other.bp_location_at),
4367 breakpoint_at (other.breakpoint_at),
4368 commands (other.commands),
4369 old_val (other.old_val),
4370 print (other.print),
4371 stop (other.stop),
4372 print_it (other.print_it)
4373 {
4374 if (old_val != NULL)
4375 {
4376 old_val = value_copy (old_val);
4377 release_value (old_val);
4378 }
4379 incref_bp_location (bp_location_at);
4380 }
4381
4382 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4383 is part of the bpstat is copied as well. */
4384
4385 bpstat
4386 bpstat_copy (bpstat bs)
4387 {
4388 bpstat p = NULL;
4389 bpstat tmp;
4390 bpstat retval = NULL;
4391
4392 if (bs == NULL)
4393 return bs;
4394
4395 for (; bs != NULL; bs = bs->next)
4396 {
4397 tmp = new bpstats (*bs);
4398
4399 if (p == NULL)
4400 /* This is the first thing in the chain. */
4401 retval = tmp;
4402 else
4403 p->next = tmp;
4404 p = tmp;
4405 }
4406 p->next = NULL;
4407 return retval;
4408 }
4409
4410 /* Find the bpstat associated with this breakpoint. */
4411
4412 bpstat
4413 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4414 {
4415 if (bsp == NULL)
4416 return NULL;
4417
4418 for (; bsp != NULL; bsp = bsp->next)
4419 {
4420 if (bsp->breakpoint_at == breakpoint)
4421 return bsp;
4422 }
4423 return NULL;
4424 }
4425
4426 /* See breakpoint.h. */
4427
4428 int
4429 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4430 {
4431 for (; bsp != NULL; bsp = bsp->next)
4432 {
4433 if (bsp->breakpoint_at == NULL)
4434 {
4435 /* A moribund location can never explain a signal other than
4436 GDB_SIGNAL_TRAP. */
4437 if (sig == GDB_SIGNAL_TRAP)
4438 return 1;
4439 }
4440 else
4441 {
4442 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4443 sig))
4444 return 1;
4445 }
4446 }
4447
4448 return 0;
4449 }
4450
4451 /* Put in *NUM the breakpoint number of the first breakpoint we are
4452 stopped at. *BSP upon return is a bpstat which points to the
4453 remaining breakpoints stopped at (but which is not guaranteed to be
4454 good for anything but further calls to bpstat_num).
4455
4456 Return 0 if passed a bpstat which does not indicate any breakpoints.
4457 Return -1 if stopped at a breakpoint that has been deleted since
4458 we set it.
4459 Return 1 otherwise. */
4460
4461 int
4462 bpstat_num (bpstat *bsp, int *num)
4463 {
4464 struct breakpoint *b;
4465
4466 if ((*bsp) == NULL)
4467 return 0; /* No more breakpoint values */
4468
4469 /* We assume we'll never have several bpstats that correspond to a
4470 single breakpoint -- otherwise, this function might return the
4471 same number more than once and this will look ugly. */
4472 b = (*bsp)->breakpoint_at;
4473 *bsp = (*bsp)->next;
4474 if (b == NULL)
4475 return -1; /* breakpoint that's been deleted since */
4476
4477 *num = b->number; /* We have its number */
4478 return 1;
4479 }
4480
4481 /* See breakpoint.h. */
4482
4483 void
4484 bpstat_clear_actions (void)
4485 {
4486 struct thread_info *tp;
4487 bpstat bs;
4488
4489 if (ptid_equal (inferior_ptid, null_ptid))
4490 return;
4491
4492 tp = find_thread_ptid (inferior_ptid);
4493 if (tp == NULL)
4494 return;
4495
4496 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4497 {
4498 bs->commands = NULL;
4499
4500 if (bs->old_val != NULL)
4501 {
4502 value_free (bs->old_val);
4503 bs->old_val = NULL;
4504 }
4505 }
4506 }
4507
4508 /* Called when a command is about to proceed the inferior. */
4509
4510 static void
4511 breakpoint_about_to_proceed (void)
4512 {
4513 if (!ptid_equal (inferior_ptid, null_ptid))
4514 {
4515 struct thread_info *tp = inferior_thread ();
4516
4517 /* Allow inferior function calls in breakpoint commands to not
4518 interrupt the command list. When the call finishes
4519 successfully, the inferior will be standing at the same
4520 breakpoint as if nothing happened. */
4521 if (tp->control.in_infcall)
4522 return;
4523 }
4524
4525 breakpoint_proceeded = 1;
4526 }
4527
4528 /* Stub for cleaning up our state if we error-out of a breakpoint
4529 command. */
4530 static void
4531 cleanup_executing_breakpoints (void *ignore)
4532 {
4533 executing_breakpoint_commands = 0;
4534 }
4535
4536 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4537 or its equivalent. */
4538
4539 static int
4540 command_line_is_silent (struct command_line *cmd)
4541 {
4542 return cmd && (strcmp ("silent", cmd->line) == 0);
4543 }
4544
4545 /* Execute all the commands associated with all the breakpoints at
4546 this location. Any of these commands could cause the process to
4547 proceed beyond this point, etc. We look out for such changes by
4548 checking the global "breakpoint_proceeded" after each command.
4549
4550 Returns true if a breakpoint command resumed the inferior. In that
4551 case, it is the caller's responsibility to recall it again with the
4552 bpstat of the current thread. */
4553
4554 static int
4555 bpstat_do_actions_1 (bpstat *bsp)
4556 {
4557 bpstat bs;
4558 struct cleanup *old_chain;
4559 int again = 0;
4560
4561 /* Avoid endless recursion if a `source' command is contained
4562 in bs->commands. */
4563 if (executing_breakpoint_commands)
4564 return 0;
4565
4566 executing_breakpoint_commands = 1;
4567 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4568
4569 scoped_restore preventer = prevent_dont_repeat ();
4570
4571 /* This pointer will iterate over the list of bpstat's. */
4572 bs = *bsp;
4573
4574 breakpoint_proceeded = 0;
4575 for (; bs != NULL; bs = bs->next)
4576 {
4577 struct command_line *cmd = NULL;
4578
4579 /* Take ownership of the BSP's command tree, if it has one.
4580
4581 The command tree could legitimately contain commands like
4582 'step' and 'next', which call clear_proceed_status, which
4583 frees stop_bpstat's command tree. To make sure this doesn't
4584 free the tree we're executing out from under us, we need to
4585 take ownership of the tree ourselves. Since a given bpstat's
4586 commands are only executed once, we don't need to copy it; we
4587 can clear the pointer in the bpstat, and make sure we free
4588 the tree when we're done. */
4589 counted_command_line ccmd = bs->commands;
4590 bs->commands = NULL;
4591 if (ccmd != NULL)
4592 cmd = ccmd.get ();
4593 if (command_line_is_silent (cmd))
4594 {
4595 /* The action has been already done by bpstat_stop_status. */
4596 cmd = cmd->next;
4597 }
4598
4599 while (cmd != NULL)
4600 {
4601 execute_control_command (cmd);
4602
4603 if (breakpoint_proceeded)
4604 break;
4605 else
4606 cmd = cmd->next;
4607 }
4608
4609 if (breakpoint_proceeded)
4610 {
4611 if (current_ui->async)
4612 /* If we are in async mode, then the target might be still
4613 running, not stopped at any breakpoint, so nothing for
4614 us to do here -- just return to the event loop. */
4615 ;
4616 else
4617 /* In sync mode, when execute_control_command returns
4618 we're already standing on the next breakpoint.
4619 Breakpoint commands for that stop were not run, since
4620 execute_command does not run breakpoint commands --
4621 only command_line_handler does, but that one is not
4622 involved in execution of breakpoint commands. So, we
4623 can now execute breakpoint commands. It should be
4624 noted that making execute_command do bpstat actions is
4625 not an option -- in this case we'll have recursive
4626 invocation of bpstat for each breakpoint with a
4627 command, and can easily blow up GDB stack. Instead, we
4628 return true, which will trigger the caller to recall us
4629 with the new stop_bpstat. */
4630 again = 1;
4631 break;
4632 }
4633 }
4634 do_cleanups (old_chain);
4635 return again;
4636 }
4637
4638 void
4639 bpstat_do_actions (void)
4640 {
4641 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4642
4643 /* Do any commands attached to breakpoint we are stopped at. */
4644 while (!ptid_equal (inferior_ptid, null_ptid)
4645 && target_has_execution
4646 && !is_exited (inferior_ptid)
4647 && !is_executing (inferior_ptid))
4648 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4649 and only return when it is stopped at the next breakpoint, we
4650 keep doing breakpoint actions until it returns false to
4651 indicate the inferior was not resumed. */
4652 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4653 break;
4654
4655 discard_cleanups (cleanup_if_error);
4656 }
4657
4658 /* Print out the (old or new) value associated with a watchpoint. */
4659
4660 static void
4661 watchpoint_value_print (struct value *val, struct ui_file *stream)
4662 {
4663 if (val == NULL)
4664 fprintf_unfiltered (stream, _("<unreadable>"));
4665 else
4666 {
4667 struct value_print_options opts;
4668 get_user_print_options (&opts);
4669 value_print (val, stream, &opts);
4670 }
4671 }
4672
4673 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4674 debugging multiple threads. */
4675
4676 void
4677 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4678 {
4679 if (uiout->is_mi_like_p ())
4680 return;
4681
4682 uiout->text ("\n");
4683
4684 if (show_thread_that_caused_stop ())
4685 {
4686 const char *name;
4687 struct thread_info *thr = inferior_thread ();
4688
4689 uiout->text ("Thread ");
4690 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4691
4692 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4693 if (name != NULL)
4694 {
4695 uiout->text (" \"");
4696 uiout->field_fmt ("name", "%s", name);
4697 uiout->text ("\"");
4698 }
4699
4700 uiout->text (" hit ");
4701 }
4702 }
4703
4704 /* Generic routine for printing messages indicating why we
4705 stopped. The behavior of this function depends on the value
4706 'print_it' in the bpstat structure. Under some circumstances we
4707 may decide not to print anything here and delegate the task to
4708 normal_stop(). */
4709
4710 static enum print_stop_action
4711 print_bp_stop_message (bpstat bs)
4712 {
4713 switch (bs->print_it)
4714 {
4715 case print_it_noop:
4716 /* Nothing should be printed for this bpstat entry. */
4717 return PRINT_UNKNOWN;
4718 break;
4719
4720 case print_it_done:
4721 /* We still want to print the frame, but we already printed the
4722 relevant messages. */
4723 return PRINT_SRC_AND_LOC;
4724 break;
4725
4726 case print_it_normal:
4727 {
4728 struct breakpoint *b = bs->breakpoint_at;
4729
4730 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4731 which has since been deleted. */
4732 if (b == NULL)
4733 return PRINT_UNKNOWN;
4734
4735 /* Normal case. Call the breakpoint's print_it method. */
4736 return b->ops->print_it (bs);
4737 }
4738 break;
4739
4740 default:
4741 internal_error (__FILE__, __LINE__,
4742 _("print_bp_stop_message: unrecognized enum value"));
4743 break;
4744 }
4745 }
4746
4747 /* A helper function that prints a shared library stopped event. */
4748
4749 static void
4750 print_solib_event (int is_catchpoint)
4751 {
4752 int any_deleted
4753 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4754 int any_added
4755 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4756
4757 if (!is_catchpoint)
4758 {
4759 if (any_added || any_deleted)
4760 current_uiout->text (_("Stopped due to shared library event:\n"));
4761 else
4762 current_uiout->text (_("Stopped due to shared library event (no "
4763 "libraries added or removed)\n"));
4764 }
4765
4766 if (current_uiout->is_mi_like_p ())
4767 current_uiout->field_string ("reason",
4768 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4769
4770 if (any_deleted)
4771 {
4772 char *name;
4773 int ix;
4774
4775 current_uiout->text (_(" Inferior unloaded "));
4776 ui_out_emit_list list_emitter (current_uiout, "removed");
4777 for (ix = 0;
4778 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4779 ix, name);
4780 ++ix)
4781 {
4782 if (ix > 0)
4783 current_uiout->text (" ");
4784 current_uiout->field_string ("library", name);
4785 current_uiout->text ("\n");
4786 }
4787 }
4788
4789 if (any_added)
4790 {
4791 struct so_list *iter;
4792 int ix;
4793
4794 current_uiout->text (_(" Inferior loaded "));
4795 ui_out_emit_list list_emitter (current_uiout, "added");
4796 for (ix = 0;
4797 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4798 ix, iter);
4799 ++ix)
4800 {
4801 if (ix > 0)
4802 current_uiout->text (" ");
4803 current_uiout->field_string ("library", iter->so_name);
4804 current_uiout->text ("\n");
4805 }
4806 }
4807 }
4808
4809 /* Print a message indicating what happened. This is called from
4810 normal_stop(). The input to this routine is the head of the bpstat
4811 list - a list of the eventpoints that caused this stop. KIND is
4812 the target_waitkind for the stopping event. This
4813 routine calls the generic print routine for printing a message
4814 about reasons for stopping. This will print (for example) the
4815 "Breakpoint n," part of the output. The return value of this
4816 routine is one of:
4817
4818 PRINT_UNKNOWN: Means we printed nothing.
4819 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4820 code to print the location. An example is
4821 "Breakpoint 1, " which should be followed by
4822 the location.
4823 PRINT_SRC_ONLY: Means we printed something, but there is no need
4824 to also print the location part of the message.
4825 An example is the catch/throw messages, which
4826 don't require a location appended to the end.
4827 PRINT_NOTHING: We have done some printing and we don't need any
4828 further info to be printed. */
4829
4830 enum print_stop_action
4831 bpstat_print (bpstat bs, int kind)
4832 {
4833 enum print_stop_action val;
4834
4835 /* Maybe another breakpoint in the chain caused us to stop.
4836 (Currently all watchpoints go on the bpstat whether hit or not.
4837 That probably could (should) be changed, provided care is taken
4838 with respect to bpstat_explains_signal). */
4839 for (; bs; bs = bs->next)
4840 {
4841 val = print_bp_stop_message (bs);
4842 if (val == PRINT_SRC_ONLY
4843 || val == PRINT_SRC_AND_LOC
4844 || val == PRINT_NOTHING)
4845 return val;
4846 }
4847
4848 /* If we had hit a shared library event breakpoint,
4849 print_bp_stop_message would print out this message. If we hit an
4850 OS-level shared library event, do the same thing. */
4851 if (kind == TARGET_WAITKIND_LOADED)
4852 {
4853 print_solib_event (0);
4854 return PRINT_NOTHING;
4855 }
4856
4857 /* We reached the end of the chain, or we got a null BS to start
4858 with and nothing was printed. */
4859 return PRINT_UNKNOWN;
4860 }
4861
4862 /* Evaluate the expression EXP and return 1 if value is zero.
4863 This returns the inverse of the condition because it is called
4864 from catch_errors which returns 0 if an exception happened, and if an
4865 exception happens we want execution to stop.
4866 The argument is a "struct expression *" that has been cast to a
4867 "void *" to make it pass through catch_errors. */
4868
4869 static int
4870 breakpoint_cond_eval (void *exp)
4871 {
4872 struct value *mark = value_mark ();
4873 int i = !value_true (evaluate_expression ((struct expression *) exp));
4874
4875 value_free_to_mark (mark);
4876 return i;
4877 }
4878
4879 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4880
4881 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4882 : next (NULL),
4883 bp_location_at (bl),
4884 breakpoint_at (bl->owner),
4885 commands (NULL),
4886 old_val (NULL),
4887 print (0),
4888 stop (0),
4889 print_it (print_it_normal)
4890 {
4891 incref_bp_location (bl);
4892 **bs_link_pointer = this;
4893 *bs_link_pointer = &next;
4894 }
4895
4896 bpstats::bpstats ()
4897 : next (NULL),
4898 bp_location_at (NULL),
4899 breakpoint_at (NULL),
4900 commands (NULL),
4901 old_val (NULL),
4902 print (0),
4903 stop (0),
4904 print_it (print_it_normal)
4905 {
4906 }
4907 \f
4908 /* The target has stopped with waitstatus WS. Check if any hardware
4909 watchpoints have triggered, according to the target. */
4910
4911 int
4912 watchpoints_triggered (struct target_waitstatus *ws)
4913 {
4914 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4915 CORE_ADDR addr;
4916 struct breakpoint *b;
4917
4918 if (!stopped_by_watchpoint)
4919 {
4920 /* We were not stopped by a watchpoint. Mark all watchpoints
4921 as not triggered. */
4922 ALL_BREAKPOINTS (b)
4923 if (is_hardware_watchpoint (b))
4924 {
4925 struct watchpoint *w = (struct watchpoint *) b;
4926
4927 w->watchpoint_triggered = watch_triggered_no;
4928 }
4929
4930 return 0;
4931 }
4932
4933 if (!target_stopped_data_address (&current_target, &addr))
4934 {
4935 /* We were stopped by a watchpoint, but we don't know where.
4936 Mark all watchpoints as unknown. */
4937 ALL_BREAKPOINTS (b)
4938 if (is_hardware_watchpoint (b))
4939 {
4940 struct watchpoint *w = (struct watchpoint *) b;
4941
4942 w->watchpoint_triggered = watch_triggered_unknown;
4943 }
4944
4945 return 1;
4946 }
4947
4948 /* The target could report the data address. Mark watchpoints
4949 affected by this data address as triggered, and all others as not
4950 triggered. */
4951
4952 ALL_BREAKPOINTS (b)
4953 if (is_hardware_watchpoint (b))
4954 {
4955 struct watchpoint *w = (struct watchpoint *) b;
4956 struct bp_location *loc;
4957
4958 w->watchpoint_triggered = watch_triggered_no;
4959 for (loc = b->loc; loc; loc = loc->next)
4960 {
4961 if (is_masked_watchpoint (b))
4962 {
4963 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4964 CORE_ADDR start = loc->address & w->hw_wp_mask;
4965
4966 if (newaddr == start)
4967 {
4968 w->watchpoint_triggered = watch_triggered_yes;
4969 break;
4970 }
4971 }
4972 /* Exact match not required. Within range is sufficient. */
4973 else if (target_watchpoint_addr_within_range (&current_target,
4974 addr, loc->address,
4975 loc->length))
4976 {
4977 w->watchpoint_triggered = watch_triggered_yes;
4978 break;
4979 }
4980 }
4981 }
4982
4983 return 1;
4984 }
4985
4986 /* Possible return values for watchpoint_check (this can't be an enum
4987 because of check_errors). */
4988 /* The watchpoint has been deleted. */
4989 #define WP_DELETED 1
4990 /* The value has changed. */
4991 #define WP_VALUE_CHANGED 2
4992 /* The value has not changed. */
4993 #define WP_VALUE_NOT_CHANGED 3
4994 /* Ignore this watchpoint, no matter if the value changed or not. */
4995 #define WP_IGNORE 4
4996
4997 #define BP_TEMPFLAG 1
4998 #define BP_HARDWAREFLAG 2
4999
5000 /* Evaluate watchpoint condition expression and check if its value
5001 changed.
5002
5003 P should be a pointer to struct bpstat, but is defined as a void *
5004 in order for this function to be usable with catch_errors. */
5005
5006 static int
5007 watchpoint_check (void *p)
5008 {
5009 bpstat bs = (bpstat) p;
5010 struct watchpoint *b;
5011 struct frame_info *fr;
5012 int within_current_scope;
5013
5014 /* BS is built from an existing struct breakpoint. */
5015 gdb_assert (bs->breakpoint_at != NULL);
5016 b = (struct watchpoint *) bs->breakpoint_at;
5017
5018 /* If this is a local watchpoint, we only want to check if the
5019 watchpoint frame is in scope if the current thread is the thread
5020 that was used to create the watchpoint. */
5021 if (!watchpoint_in_thread_scope (b))
5022 return WP_IGNORE;
5023
5024 if (b->exp_valid_block == NULL)
5025 within_current_scope = 1;
5026 else
5027 {
5028 struct frame_info *frame = get_current_frame ();
5029 struct gdbarch *frame_arch = get_frame_arch (frame);
5030 CORE_ADDR frame_pc = get_frame_pc (frame);
5031
5032 /* stack_frame_destroyed_p() returns a non-zero value if we're
5033 still in the function but the stack frame has already been
5034 invalidated. Since we can't rely on the values of local
5035 variables after the stack has been destroyed, we are treating
5036 the watchpoint in that state as `not changed' without further
5037 checking. Don't mark watchpoints as changed if the current
5038 frame is in an epilogue - even if they are in some other
5039 frame, our view of the stack is likely to be wrong and
5040 frame_find_by_id could error out. */
5041 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5042 return WP_IGNORE;
5043
5044 fr = frame_find_by_id (b->watchpoint_frame);
5045 within_current_scope = (fr != NULL);
5046
5047 /* If we've gotten confused in the unwinder, we might have
5048 returned a frame that can't describe this variable. */
5049 if (within_current_scope)
5050 {
5051 struct symbol *function;
5052
5053 function = get_frame_function (fr);
5054 if (function == NULL
5055 || !contained_in (b->exp_valid_block,
5056 SYMBOL_BLOCK_VALUE (function)))
5057 within_current_scope = 0;
5058 }
5059
5060 if (within_current_scope)
5061 /* If we end up stopping, the current frame will get selected
5062 in normal_stop. So this call to select_frame won't affect
5063 the user. */
5064 select_frame (fr);
5065 }
5066
5067 if (within_current_scope)
5068 {
5069 /* We use value_{,free_to_}mark because it could be a *long*
5070 time before we return to the command level and call
5071 free_all_values. We can't call free_all_values because we
5072 might be in the middle of evaluating a function call. */
5073
5074 int pc = 0;
5075 struct value *mark;
5076 struct value *new_val;
5077
5078 if (is_masked_watchpoint (b))
5079 /* Since we don't know the exact trigger address (from
5080 stopped_data_address), just tell the user we've triggered
5081 a mask watchpoint. */
5082 return WP_VALUE_CHANGED;
5083
5084 mark = value_mark ();
5085 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5086
5087 if (b->val_bitsize != 0)
5088 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5089
5090 /* We use value_equal_contents instead of value_equal because
5091 the latter coerces an array to a pointer, thus comparing just
5092 the address of the array instead of its contents. This is
5093 not what we want. */
5094 if ((b->val != NULL) != (new_val != NULL)
5095 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5096 {
5097 if (new_val != NULL)
5098 {
5099 release_value (new_val);
5100 value_free_to_mark (mark);
5101 }
5102 bs->old_val = b->val;
5103 b->val = new_val;
5104 b->val_valid = 1;
5105 return WP_VALUE_CHANGED;
5106 }
5107 else
5108 {
5109 /* Nothing changed. */
5110 value_free_to_mark (mark);
5111 return WP_VALUE_NOT_CHANGED;
5112 }
5113 }
5114 else
5115 {
5116 /* This seems like the only logical thing to do because
5117 if we temporarily ignored the watchpoint, then when
5118 we reenter the block in which it is valid it contains
5119 garbage (in the case of a function, it may have two
5120 garbage values, one before and one after the prologue).
5121 So we can't even detect the first assignment to it and
5122 watch after that (since the garbage may or may not equal
5123 the first value assigned). */
5124 /* We print all the stop information in
5125 breakpoint_ops->print_it, but in this case, by the time we
5126 call breakpoint_ops->print_it this bp will be deleted
5127 already. So we have no choice but print the information
5128 here. */
5129
5130 SWITCH_THRU_ALL_UIS ()
5131 {
5132 struct ui_out *uiout = current_uiout;
5133
5134 if (uiout->is_mi_like_p ())
5135 uiout->field_string
5136 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5137 uiout->text ("\nWatchpoint ");
5138 uiout->field_int ("wpnum", b->number);
5139 uiout->text (" deleted because the program has left the block in\n"
5140 "which its expression is valid.\n");
5141 }
5142
5143 /* Make sure the watchpoint's commands aren't executed. */
5144 b->commands = NULL;
5145 watchpoint_del_at_next_stop (b);
5146
5147 return WP_DELETED;
5148 }
5149 }
5150
5151 /* Return true if it looks like target has stopped due to hitting
5152 breakpoint location BL. This function does not check if we should
5153 stop, only if BL explains the stop. */
5154
5155 static int
5156 bpstat_check_location (const struct bp_location *bl,
5157 struct address_space *aspace, CORE_ADDR bp_addr,
5158 const struct target_waitstatus *ws)
5159 {
5160 struct breakpoint *b = bl->owner;
5161
5162 /* BL is from an existing breakpoint. */
5163 gdb_assert (b != NULL);
5164
5165 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5166 }
5167
5168 /* Determine if the watched values have actually changed, and we
5169 should stop. If not, set BS->stop to 0. */
5170
5171 static void
5172 bpstat_check_watchpoint (bpstat bs)
5173 {
5174 const struct bp_location *bl;
5175 struct watchpoint *b;
5176
5177 /* BS is built for existing struct breakpoint. */
5178 bl = bs->bp_location_at;
5179 gdb_assert (bl != NULL);
5180 b = (struct watchpoint *) bs->breakpoint_at;
5181 gdb_assert (b != NULL);
5182
5183 {
5184 int must_check_value = 0;
5185
5186 if (b->type == bp_watchpoint)
5187 /* For a software watchpoint, we must always check the
5188 watched value. */
5189 must_check_value = 1;
5190 else if (b->watchpoint_triggered == watch_triggered_yes)
5191 /* We have a hardware watchpoint (read, write, or access)
5192 and the target earlier reported an address watched by
5193 this watchpoint. */
5194 must_check_value = 1;
5195 else if (b->watchpoint_triggered == watch_triggered_unknown
5196 && b->type == bp_hardware_watchpoint)
5197 /* We were stopped by a hardware watchpoint, but the target could
5198 not report the data address. We must check the watchpoint's
5199 value. Access and read watchpoints are out of luck; without
5200 a data address, we can't figure it out. */
5201 must_check_value = 1;
5202
5203 if (must_check_value)
5204 {
5205 char *message
5206 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5207 b->number);
5208 struct cleanup *cleanups = make_cleanup (xfree, message);
5209 int e = catch_errors (watchpoint_check, bs, message,
5210 RETURN_MASK_ALL);
5211 do_cleanups (cleanups);
5212 switch (e)
5213 {
5214 case WP_DELETED:
5215 /* We've already printed what needs to be printed. */
5216 bs->print_it = print_it_done;
5217 /* Stop. */
5218 break;
5219 case WP_IGNORE:
5220 bs->print_it = print_it_noop;
5221 bs->stop = 0;
5222 break;
5223 case WP_VALUE_CHANGED:
5224 if (b->type == bp_read_watchpoint)
5225 {
5226 /* There are two cases to consider here:
5227
5228 1. We're watching the triggered memory for reads.
5229 In that case, trust the target, and always report
5230 the watchpoint hit to the user. Even though
5231 reads don't cause value changes, the value may
5232 have changed since the last time it was read, and
5233 since we're not trapping writes, we will not see
5234 those, and as such we should ignore our notion of
5235 old value.
5236
5237 2. We're watching the triggered memory for both
5238 reads and writes. There are two ways this may
5239 happen:
5240
5241 2.1. This is a target that can't break on data
5242 reads only, but can break on accesses (reads or
5243 writes), such as e.g., x86. We detect this case
5244 at the time we try to insert read watchpoints.
5245
5246 2.2. Otherwise, the target supports read
5247 watchpoints, but, the user set an access or write
5248 watchpoint watching the same memory as this read
5249 watchpoint.
5250
5251 If we're watching memory writes as well as reads,
5252 ignore watchpoint hits when we find that the
5253 value hasn't changed, as reads don't cause
5254 changes. This still gives false positives when
5255 the program writes the same value to memory as
5256 what there was already in memory (we will confuse
5257 it for a read), but it's much better than
5258 nothing. */
5259
5260 int other_write_watchpoint = 0;
5261
5262 if (bl->watchpoint_type == hw_read)
5263 {
5264 struct breakpoint *other_b;
5265
5266 ALL_BREAKPOINTS (other_b)
5267 if (other_b->type == bp_hardware_watchpoint
5268 || other_b->type == bp_access_watchpoint)
5269 {
5270 struct watchpoint *other_w =
5271 (struct watchpoint *) other_b;
5272
5273 if (other_w->watchpoint_triggered
5274 == watch_triggered_yes)
5275 {
5276 other_write_watchpoint = 1;
5277 break;
5278 }
5279 }
5280 }
5281
5282 if (other_write_watchpoint
5283 || bl->watchpoint_type == hw_access)
5284 {
5285 /* We're watching the same memory for writes,
5286 and the value changed since the last time we
5287 updated it, so this trap must be for a write.
5288 Ignore it. */
5289 bs->print_it = print_it_noop;
5290 bs->stop = 0;
5291 }
5292 }
5293 break;
5294 case WP_VALUE_NOT_CHANGED:
5295 if (b->type == bp_hardware_watchpoint
5296 || b->type == bp_watchpoint)
5297 {
5298 /* Don't stop: write watchpoints shouldn't fire if
5299 the value hasn't changed. */
5300 bs->print_it = print_it_noop;
5301 bs->stop = 0;
5302 }
5303 /* Stop. */
5304 break;
5305 default:
5306 /* Can't happen. */
5307 case 0:
5308 /* Error from catch_errors. */
5309 {
5310 SWITCH_THRU_ALL_UIS ()
5311 {
5312 printf_filtered (_("Watchpoint %d deleted.\n"),
5313 b->number);
5314 }
5315 watchpoint_del_at_next_stop (b);
5316 /* We've already printed what needs to be printed. */
5317 bs->print_it = print_it_done;
5318 }
5319 break;
5320 }
5321 }
5322 else /* must_check_value == 0 */
5323 {
5324 /* This is a case where some watchpoint(s) triggered, but
5325 not at the address of this watchpoint, or else no
5326 watchpoint triggered after all. So don't print
5327 anything for this watchpoint. */
5328 bs->print_it = print_it_noop;
5329 bs->stop = 0;
5330 }
5331 }
5332 }
5333
5334 /* For breakpoints that are currently marked as telling gdb to stop,
5335 check conditions (condition proper, frame, thread and ignore count)
5336 of breakpoint referred to by BS. If we should not stop for this
5337 breakpoint, set BS->stop to 0. */
5338
5339 static void
5340 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5341 {
5342 const struct bp_location *bl;
5343 struct breakpoint *b;
5344 int value_is_zero = 0;
5345 struct expression *cond;
5346
5347 gdb_assert (bs->stop);
5348
5349 /* BS is built for existing struct breakpoint. */
5350 bl = bs->bp_location_at;
5351 gdb_assert (bl != NULL);
5352 b = bs->breakpoint_at;
5353 gdb_assert (b != NULL);
5354
5355 /* Even if the target evaluated the condition on its end and notified GDB, we
5356 need to do so again since GDB does not know if we stopped due to a
5357 breakpoint or a single step breakpoint. */
5358
5359 if (frame_id_p (b->frame_id)
5360 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5361 {
5362 bs->stop = 0;
5363 return;
5364 }
5365
5366 /* If this is a thread/task-specific breakpoint, don't waste cpu
5367 evaluating the condition if this isn't the specified
5368 thread/task. */
5369 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5370 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5371
5372 {
5373 bs->stop = 0;
5374 return;
5375 }
5376
5377 /* Evaluate extension language breakpoints that have a "stop" method
5378 implemented. */
5379 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5380
5381 if (is_watchpoint (b))
5382 {
5383 struct watchpoint *w = (struct watchpoint *) b;
5384
5385 cond = w->cond_exp.get ();
5386 }
5387 else
5388 cond = bl->cond.get ();
5389
5390 if (cond && b->disposition != disp_del_at_next_stop)
5391 {
5392 int within_current_scope = 1;
5393 struct watchpoint * w;
5394
5395 /* We use value_mark and value_free_to_mark because it could
5396 be a long time before we return to the command level and
5397 call free_all_values. We can't call free_all_values
5398 because we might be in the middle of evaluating a
5399 function call. */
5400 struct value *mark = value_mark ();
5401
5402 if (is_watchpoint (b))
5403 w = (struct watchpoint *) b;
5404 else
5405 w = NULL;
5406
5407 /* Need to select the frame, with all that implies so that
5408 the conditions will have the right context. Because we
5409 use the frame, we will not see an inlined function's
5410 variables when we arrive at a breakpoint at the start
5411 of the inlined function; the current frame will be the
5412 call site. */
5413 if (w == NULL || w->cond_exp_valid_block == NULL)
5414 select_frame (get_current_frame ());
5415 else
5416 {
5417 struct frame_info *frame;
5418
5419 /* For local watchpoint expressions, which particular
5420 instance of a local is being watched matters, so we
5421 keep track of the frame to evaluate the expression
5422 in. To evaluate the condition however, it doesn't
5423 really matter which instantiation of the function
5424 where the condition makes sense triggers the
5425 watchpoint. This allows an expression like "watch
5426 global if q > 10" set in `func', catch writes to
5427 global on all threads that call `func', or catch
5428 writes on all recursive calls of `func' by a single
5429 thread. We simply always evaluate the condition in
5430 the innermost frame that's executing where it makes
5431 sense to evaluate the condition. It seems
5432 intuitive. */
5433 frame = block_innermost_frame (w->cond_exp_valid_block);
5434 if (frame != NULL)
5435 select_frame (frame);
5436 else
5437 within_current_scope = 0;
5438 }
5439 if (within_current_scope)
5440 value_is_zero
5441 = catch_errors (breakpoint_cond_eval, cond,
5442 "Error in testing breakpoint condition:\n",
5443 RETURN_MASK_ALL);
5444 else
5445 {
5446 warning (_("Watchpoint condition cannot be tested "
5447 "in the current scope"));
5448 /* If we failed to set the right context for this
5449 watchpoint, unconditionally report it. */
5450 value_is_zero = 0;
5451 }
5452 /* FIXME-someday, should give breakpoint #. */
5453 value_free_to_mark (mark);
5454 }
5455
5456 if (cond && value_is_zero)
5457 {
5458 bs->stop = 0;
5459 }
5460 else if (b->ignore_count > 0)
5461 {
5462 b->ignore_count--;
5463 bs->stop = 0;
5464 /* Increase the hit count even though we don't stop. */
5465 ++(b->hit_count);
5466 observer_notify_breakpoint_modified (b);
5467 }
5468 }
5469
5470 /* Returns true if we need to track moribund locations of LOC's type
5471 on the current target. */
5472
5473 static int
5474 need_moribund_for_location_type (struct bp_location *loc)
5475 {
5476 return ((loc->loc_type == bp_loc_software_breakpoint
5477 && !target_supports_stopped_by_sw_breakpoint ())
5478 || (loc->loc_type == bp_loc_hardware_breakpoint
5479 && !target_supports_stopped_by_hw_breakpoint ()));
5480 }
5481
5482
5483 /* Get a bpstat associated with having just stopped at address
5484 BP_ADDR in thread PTID.
5485
5486 Determine whether we stopped at a breakpoint, etc, or whether we
5487 don't understand this stop. Result is a chain of bpstat's such
5488 that:
5489
5490 if we don't understand the stop, the result is a null pointer.
5491
5492 if we understand why we stopped, the result is not null.
5493
5494 Each element of the chain refers to a particular breakpoint or
5495 watchpoint at which we have stopped. (We may have stopped for
5496 several reasons concurrently.)
5497
5498 Each element of the chain has valid next, breakpoint_at,
5499 commands, FIXME??? fields. */
5500
5501 bpstat
5502 bpstat_stop_status (struct address_space *aspace,
5503 CORE_ADDR bp_addr, ptid_t ptid,
5504 const struct target_waitstatus *ws)
5505 {
5506 struct breakpoint *b = NULL;
5507 struct bp_location *bl;
5508 struct bp_location *loc;
5509 /* First item of allocated bpstat's. */
5510 bpstat bs_head = NULL, *bs_link = &bs_head;
5511 /* Pointer to the last thing in the chain currently. */
5512 bpstat bs;
5513 int ix;
5514 int need_remove_insert;
5515 int removed_any;
5516
5517 /* First, build the bpstat chain with locations that explain a
5518 target stop, while being careful to not set the target running,
5519 as that may invalidate locations (in particular watchpoint
5520 locations are recreated). Resuming will happen here with
5521 breakpoint conditions or watchpoint expressions that include
5522 inferior function calls. */
5523
5524 ALL_BREAKPOINTS (b)
5525 {
5526 if (!breakpoint_enabled (b))
5527 continue;
5528
5529 for (bl = b->loc; bl != NULL; bl = bl->next)
5530 {
5531 /* For hardware watchpoints, we look only at the first
5532 location. The watchpoint_check function will work on the
5533 entire expression, not the individual locations. For
5534 read watchpoints, the watchpoints_triggered function has
5535 checked all locations already. */
5536 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5537 break;
5538
5539 if (!bl->enabled || bl->shlib_disabled)
5540 continue;
5541
5542 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5543 continue;
5544
5545 /* Come here if it's a watchpoint, or if the break address
5546 matches. */
5547
5548 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5549 explain stop. */
5550
5551 /* Assume we stop. Should we find a watchpoint that is not
5552 actually triggered, or if the condition of the breakpoint
5553 evaluates as false, we'll reset 'stop' to 0. */
5554 bs->stop = 1;
5555 bs->print = 1;
5556
5557 /* If this is a scope breakpoint, mark the associated
5558 watchpoint as triggered so that we will handle the
5559 out-of-scope event. We'll get to the watchpoint next
5560 iteration. */
5561 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5562 {
5563 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5564
5565 w->watchpoint_triggered = watch_triggered_yes;
5566 }
5567 }
5568 }
5569
5570 /* Check if a moribund breakpoint explains the stop. */
5571 if (!target_supports_stopped_by_sw_breakpoint ()
5572 || !target_supports_stopped_by_hw_breakpoint ())
5573 {
5574 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5575 {
5576 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5577 && need_moribund_for_location_type (loc))
5578 {
5579 bs = new bpstats (loc, &bs_link);
5580 /* For hits of moribund locations, we should just proceed. */
5581 bs->stop = 0;
5582 bs->print = 0;
5583 bs->print_it = print_it_noop;
5584 }
5585 }
5586 }
5587
5588 /* A bit of special processing for shlib breakpoints. We need to
5589 process solib loading here, so that the lists of loaded and
5590 unloaded libraries are correct before we handle "catch load" and
5591 "catch unload". */
5592 for (bs = bs_head; bs != NULL; bs = bs->next)
5593 {
5594 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5595 {
5596 handle_solib_event ();
5597 break;
5598 }
5599 }
5600
5601 /* Now go through the locations that caused the target to stop, and
5602 check whether we're interested in reporting this stop to higher
5603 layers, or whether we should resume the target transparently. */
5604
5605 removed_any = 0;
5606
5607 for (bs = bs_head; bs != NULL; bs = bs->next)
5608 {
5609 if (!bs->stop)
5610 continue;
5611
5612 b = bs->breakpoint_at;
5613 b->ops->check_status (bs);
5614 if (bs->stop)
5615 {
5616 bpstat_check_breakpoint_conditions (bs, ptid);
5617
5618 if (bs->stop)
5619 {
5620 ++(b->hit_count);
5621 observer_notify_breakpoint_modified (b);
5622
5623 /* We will stop here. */
5624 if (b->disposition == disp_disable)
5625 {
5626 --(b->enable_count);
5627 if (b->enable_count <= 0)
5628 b->enable_state = bp_disabled;
5629 removed_any = 1;
5630 }
5631 if (b->silent)
5632 bs->print = 0;
5633 bs->commands = b->commands;
5634 if (command_line_is_silent (bs->commands
5635 ? bs->commands.get () : NULL))
5636 bs->print = 0;
5637
5638 b->ops->after_condition_true (bs);
5639 }
5640
5641 }
5642
5643 /* Print nothing for this entry if we don't stop or don't
5644 print. */
5645 if (!bs->stop || !bs->print)
5646 bs->print_it = print_it_noop;
5647 }
5648
5649 /* If we aren't stopping, the value of some hardware watchpoint may
5650 not have changed, but the intermediate memory locations we are
5651 watching may have. Don't bother if we're stopping; this will get
5652 done later. */
5653 need_remove_insert = 0;
5654 if (! bpstat_causes_stop (bs_head))
5655 for (bs = bs_head; bs != NULL; bs = bs->next)
5656 if (!bs->stop
5657 && bs->breakpoint_at
5658 && is_hardware_watchpoint (bs->breakpoint_at))
5659 {
5660 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5661
5662 update_watchpoint (w, 0 /* don't reparse. */);
5663 need_remove_insert = 1;
5664 }
5665
5666 if (need_remove_insert)
5667 update_global_location_list (UGLL_MAY_INSERT);
5668 else if (removed_any)
5669 update_global_location_list (UGLL_DONT_INSERT);
5670
5671 return bs_head;
5672 }
5673
5674 static void
5675 handle_jit_event (void)
5676 {
5677 struct frame_info *frame;
5678 struct gdbarch *gdbarch;
5679
5680 if (debug_infrun)
5681 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5682
5683 /* Switch terminal for any messages produced by
5684 breakpoint_re_set. */
5685 target_terminal::ours_for_output ();
5686
5687 frame = get_current_frame ();
5688 gdbarch = get_frame_arch (frame);
5689
5690 jit_event_handler (gdbarch);
5691
5692 target_terminal::inferior ();
5693 }
5694
5695 /* Prepare WHAT final decision for infrun. */
5696
5697 /* Decide what infrun needs to do with this bpstat. */
5698
5699 struct bpstat_what
5700 bpstat_what (bpstat bs_head)
5701 {
5702 struct bpstat_what retval;
5703 bpstat bs;
5704
5705 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5706 retval.call_dummy = STOP_NONE;
5707 retval.is_longjmp = 0;
5708
5709 for (bs = bs_head; bs != NULL; bs = bs->next)
5710 {
5711 /* Extract this BS's action. After processing each BS, we check
5712 if its action overrides all we've seem so far. */
5713 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5714 enum bptype bptype;
5715
5716 if (bs->breakpoint_at == NULL)
5717 {
5718 /* I suspect this can happen if it was a momentary
5719 breakpoint which has since been deleted. */
5720 bptype = bp_none;
5721 }
5722 else
5723 bptype = bs->breakpoint_at->type;
5724
5725 switch (bptype)
5726 {
5727 case bp_none:
5728 break;
5729 case bp_breakpoint:
5730 case bp_hardware_breakpoint:
5731 case bp_single_step:
5732 case bp_until:
5733 case bp_finish:
5734 case bp_shlib_event:
5735 if (bs->stop)
5736 {
5737 if (bs->print)
5738 this_action = BPSTAT_WHAT_STOP_NOISY;
5739 else
5740 this_action = BPSTAT_WHAT_STOP_SILENT;
5741 }
5742 else
5743 this_action = BPSTAT_WHAT_SINGLE;
5744 break;
5745 case bp_watchpoint:
5746 case bp_hardware_watchpoint:
5747 case bp_read_watchpoint:
5748 case bp_access_watchpoint:
5749 if (bs->stop)
5750 {
5751 if (bs->print)
5752 this_action = BPSTAT_WHAT_STOP_NOISY;
5753 else
5754 this_action = BPSTAT_WHAT_STOP_SILENT;
5755 }
5756 else
5757 {
5758 /* There was a watchpoint, but we're not stopping.
5759 This requires no further action. */
5760 }
5761 break;
5762 case bp_longjmp:
5763 case bp_longjmp_call_dummy:
5764 case bp_exception:
5765 if (bs->stop)
5766 {
5767 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5768 retval.is_longjmp = bptype != bp_exception;
5769 }
5770 else
5771 this_action = BPSTAT_WHAT_SINGLE;
5772 break;
5773 case bp_longjmp_resume:
5774 case bp_exception_resume:
5775 if (bs->stop)
5776 {
5777 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5778 retval.is_longjmp = bptype == bp_longjmp_resume;
5779 }
5780 else
5781 this_action = BPSTAT_WHAT_SINGLE;
5782 break;
5783 case bp_step_resume:
5784 if (bs->stop)
5785 this_action = BPSTAT_WHAT_STEP_RESUME;
5786 else
5787 {
5788 /* It is for the wrong frame. */
5789 this_action = BPSTAT_WHAT_SINGLE;
5790 }
5791 break;
5792 case bp_hp_step_resume:
5793 if (bs->stop)
5794 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5795 else
5796 {
5797 /* It is for the wrong frame. */
5798 this_action = BPSTAT_WHAT_SINGLE;
5799 }
5800 break;
5801 case bp_watchpoint_scope:
5802 case bp_thread_event:
5803 case bp_overlay_event:
5804 case bp_longjmp_master:
5805 case bp_std_terminate_master:
5806 case bp_exception_master:
5807 this_action = BPSTAT_WHAT_SINGLE;
5808 break;
5809 case bp_catchpoint:
5810 if (bs->stop)
5811 {
5812 if (bs->print)
5813 this_action = BPSTAT_WHAT_STOP_NOISY;
5814 else
5815 this_action = BPSTAT_WHAT_STOP_SILENT;
5816 }
5817 else
5818 {
5819 /* There was a catchpoint, but we're not stopping.
5820 This requires no further action. */
5821 }
5822 break;
5823 case bp_jit_event:
5824 this_action = BPSTAT_WHAT_SINGLE;
5825 break;
5826 case bp_call_dummy:
5827 /* Make sure the action is stop (silent or noisy),
5828 so infrun.c pops the dummy frame. */
5829 retval.call_dummy = STOP_STACK_DUMMY;
5830 this_action = BPSTAT_WHAT_STOP_SILENT;
5831 break;
5832 case bp_std_terminate:
5833 /* Make sure the action is stop (silent or noisy),
5834 so infrun.c pops the dummy frame. */
5835 retval.call_dummy = STOP_STD_TERMINATE;
5836 this_action = BPSTAT_WHAT_STOP_SILENT;
5837 break;
5838 case bp_tracepoint:
5839 case bp_fast_tracepoint:
5840 case bp_static_tracepoint:
5841 /* Tracepoint hits should not be reported back to GDB, and
5842 if one got through somehow, it should have been filtered
5843 out already. */
5844 internal_error (__FILE__, __LINE__,
5845 _("bpstat_what: tracepoint encountered"));
5846 break;
5847 case bp_gnu_ifunc_resolver:
5848 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5849 this_action = BPSTAT_WHAT_SINGLE;
5850 break;
5851 case bp_gnu_ifunc_resolver_return:
5852 /* The breakpoint will be removed, execution will restart from the
5853 PC of the former breakpoint. */
5854 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5855 break;
5856
5857 case bp_dprintf:
5858 if (bs->stop)
5859 this_action = BPSTAT_WHAT_STOP_SILENT;
5860 else
5861 this_action = BPSTAT_WHAT_SINGLE;
5862 break;
5863
5864 default:
5865 internal_error (__FILE__, __LINE__,
5866 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5867 }
5868
5869 retval.main_action = std::max (retval.main_action, this_action);
5870 }
5871
5872 return retval;
5873 }
5874
5875 void
5876 bpstat_run_callbacks (bpstat bs_head)
5877 {
5878 bpstat bs;
5879
5880 for (bs = bs_head; bs != NULL; bs = bs->next)
5881 {
5882 struct breakpoint *b = bs->breakpoint_at;
5883
5884 if (b == NULL)
5885 continue;
5886 switch (b->type)
5887 {
5888 case bp_jit_event:
5889 handle_jit_event ();
5890 break;
5891 case bp_gnu_ifunc_resolver:
5892 gnu_ifunc_resolver_stop (b);
5893 break;
5894 case bp_gnu_ifunc_resolver_return:
5895 gnu_ifunc_resolver_return_stop (b);
5896 break;
5897 }
5898 }
5899 }
5900
5901 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5902 without hardware support). This isn't related to a specific bpstat,
5903 just to things like whether watchpoints are set. */
5904
5905 int
5906 bpstat_should_step (void)
5907 {
5908 struct breakpoint *b;
5909
5910 ALL_BREAKPOINTS (b)
5911 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5912 return 1;
5913 return 0;
5914 }
5915
5916 int
5917 bpstat_causes_stop (bpstat bs)
5918 {
5919 for (; bs != NULL; bs = bs->next)
5920 if (bs->stop)
5921 return 1;
5922
5923 return 0;
5924 }
5925
5926 \f
5927
5928 /* Compute a string of spaces suitable to indent the next line
5929 so it starts at the position corresponding to the table column
5930 named COL_NAME in the currently active table of UIOUT. */
5931
5932 static char *
5933 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5934 {
5935 static char wrap_indent[80];
5936 int i, total_width, width, align;
5937 const char *text;
5938
5939 total_width = 0;
5940 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5941 {
5942 if (strcmp (text, col_name) == 0)
5943 {
5944 gdb_assert (total_width < sizeof wrap_indent);
5945 memset (wrap_indent, ' ', total_width);
5946 wrap_indent[total_width] = 0;
5947
5948 return wrap_indent;
5949 }
5950
5951 total_width += width + 1;
5952 }
5953
5954 return NULL;
5955 }
5956
5957 /* Determine if the locations of this breakpoint will have their conditions
5958 evaluated by the target, host or a mix of both. Returns the following:
5959
5960 "host": Host evals condition.
5961 "host or target": Host or Target evals condition.
5962 "target": Target evals condition.
5963 */
5964
5965 static const char *
5966 bp_condition_evaluator (struct breakpoint *b)
5967 {
5968 struct bp_location *bl;
5969 char host_evals = 0;
5970 char target_evals = 0;
5971
5972 if (!b)
5973 return NULL;
5974
5975 if (!is_breakpoint (b))
5976 return NULL;
5977
5978 if (gdb_evaluates_breakpoint_condition_p ()
5979 || !target_supports_evaluation_of_breakpoint_conditions ())
5980 return condition_evaluation_host;
5981
5982 for (bl = b->loc; bl; bl = bl->next)
5983 {
5984 if (bl->cond_bytecode)
5985 target_evals++;
5986 else
5987 host_evals++;
5988 }
5989
5990 if (host_evals && target_evals)
5991 return condition_evaluation_both;
5992 else if (target_evals)
5993 return condition_evaluation_target;
5994 else
5995 return condition_evaluation_host;
5996 }
5997
5998 /* Determine the breakpoint location's condition evaluator. This is
5999 similar to bp_condition_evaluator, but for locations. */
6000
6001 static const char *
6002 bp_location_condition_evaluator (struct bp_location *bl)
6003 {
6004 if (bl && !is_breakpoint (bl->owner))
6005 return NULL;
6006
6007 if (gdb_evaluates_breakpoint_condition_p ()
6008 || !target_supports_evaluation_of_breakpoint_conditions ())
6009 return condition_evaluation_host;
6010
6011 if (bl && bl->cond_bytecode)
6012 return condition_evaluation_target;
6013 else
6014 return condition_evaluation_host;
6015 }
6016
6017 /* Print the LOC location out of the list of B->LOC locations. */
6018
6019 static void
6020 print_breakpoint_location (struct breakpoint *b,
6021 struct bp_location *loc)
6022 {
6023 struct ui_out *uiout = current_uiout;
6024
6025 scoped_restore_current_program_space restore_pspace;
6026
6027 if (loc != NULL && loc->shlib_disabled)
6028 loc = NULL;
6029
6030 if (loc != NULL)
6031 set_current_program_space (loc->pspace);
6032
6033 if (b->display_canonical)
6034 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6035 else if (loc && loc->symtab)
6036 {
6037 struct symbol *sym
6038 = find_pc_sect_function (loc->address, loc->section);
6039 if (sym)
6040 {
6041 uiout->text ("in ");
6042 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6043 uiout->text (" ");
6044 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6045 uiout->text ("at ");
6046 }
6047 uiout->field_string ("file",
6048 symtab_to_filename_for_display (loc->symtab));
6049 uiout->text (":");
6050
6051 if (uiout->is_mi_like_p ())
6052 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6053
6054 uiout->field_int ("line", loc->line_number);
6055 }
6056 else if (loc)
6057 {
6058 string_file stb;
6059
6060 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6061 demangle, "");
6062 uiout->field_stream ("at", stb);
6063 }
6064 else
6065 {
6066 uiout->field_string ("pending",
6067 event_location_to_string (b->location.get ()));
6068 /* If extra_string is available, it could be holding a condition
6069 or dprintf arguments. In either case, make sure it is printed,
6070 too, but only for non-MI streams. */
6071 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6072 {
6073 if (b->type == bp_dprintf)
6074 uiout->text (",");
6075 else
6076 uiout->text (" ");
6077 uiout->text (b->extra_string);
6078 }
6079 }
6080
6081 if (loc && is_breakpoint (b)
6082 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6083 && bp_condition_evaluator (b) == condition_evaluation_both)
6084 {
6085 uiout->text (" (");
6086 uiout->field_string ("evaluated-by",
6087 bp_location_condition_evaluator (loc));
6088 uiout->text (")");
6089 }
6090 }
6091
6092 static const char *
6093 bptype_string (enum bptype type)
6094 {
6095 struct ep_type_description
6096 {
6097 enum bptype type;
6098 const char *description;
6099 };
6100 static struct ep_type_description bptypes[] =
6101 {
6102 {bp_none, "?deleted?"},
6103 {bp_breakpoint, "breakpoint"},
6104 {bp_hardware_breakpoint, "hw breakpoint"},
6105 {bp_single_step, "sw single-step"},
6106 {bp_until, "until"},
6107 {bp_finish, "finish"},
6108 {bp_watchpoint, "watchpoint"},
6109 {bp_hardware_watchpoint, "hw watchpoint"},
6110 {bp_read_watchpoint, "read watchpoint"},
6111 {bp_access_watchpoint, "acc watchpoint"},
6112 {bp_longjmp, "longjmp"},
6113 {bp_longjmp_resume, "longjmp resume"},
6114 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6115 {bp_exception, "exception"},
6116 {bp_exception_resume, "exception resume"},
6117 {bp_step_resume, "step resume"},
6118 {bp_hp_step_resume, "high-priority step resume"},
6119 {bp_watchpoint_scope, "watchpoint scope"},
6120 {bp_call_dummy, "call dummy"},
6121 {bp_std_terminate, "std::terminate"},
6122 {bp_shlib_event, "shlib events"},
6123 {bp_thread_event, "thread events"},
6124 {bp_overlay_event, "overlay events"},
6125 {bp_longjmp_master, "longjmp master"},
6126 {bp_std_terminate_master, "std::terminate master"},
6127 {bp_exception_master, "exception master"},
6128 {bp_catchpoint, "catchpoint"},
6129 {bp_tracepoint, "tracepoint"},
6130 {bp_fast_tracepoint, "fast tracepoint"},
6131 {bp_static_tracepoint, "static tracepoint"},
6132 {bp_dprintf, "dprintf"},
6133 {bp_jit_event, "jit events"},
6134 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6135 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6136 };
6137
6138 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6139 || ((int) type != bptypes[(int) type].type))
6140 internal_error (__FILE__, __LINE__,
6141 _("bptypes table does not describe type #%d."),
6142 (int) type);
6143
6144 return bptypes[(int) type].description;
6145 }
6146
6147 /* For MI, output a field named 'thread-groups' with a list as the value.
6148 For CLI, prefix the list with the string 'inf'. */
6149
6150 static void
6151 output_thread_groups (struct ui_out *uiout,
6152 const char *field_name,
6153 VEC(int) *inf_num,
6154 int mi_only)
6155 {
6156 int is_mi = uiout->is_mi_like_p ();
6157 int inf;
6158 int i;
6159
6160 /* For backward compatibility, don't display inferiors in CLI unless
6161 there are several. Always display them for MI. */
6162 if (!is_mi && mi_only)
6163 return;
6164
6165 ui_out_emit_list list_emitter (uiout, field_name);
6166
6167 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6168 {
6169 if (is_mi)
6170 {
6171 char mi_group[10];
6172
6173 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6174 uiout->field_string (NULL, mi_group);
6175 }
6176 else
6177 {
6178 if (i == 0)
6179 uiout->text (" inf ");
6180 else
6181 uiout->text (", ");
6182
6183 uiout->text (plongest (inf));
6184 }
6185 }
6186 }
6187
6188 /* Print B to gdb_stdout. */
6189
6190 static void
6191 print_one_breakpoint_location (struct breakpoint *b,
6192 struct bp_location *loc,
6193 int loc_number,
6194 struct bp_location **last_loc,
6195 int allflag)
6196 {
6197 struct command_line *l;
6198 static char bpenables[] = "nynny";
6199
6200 struct ui_out *uiout = current_uiout;
6201 int header_of_multiple = 0;
6202 int part_of_multiple = (loc != NULL);
6203 struct value_print_options opts;
6204
6205 get_user_print_options (&opts);
6206
6207 gdb_assert (!loc || loc_number != 0);
6208 /* See comment in print_one_breakpoint concerning treatment of
6209 breakpoints with single disabled location. */
6210 if (loc == NULL
6211 && (b->loc != NULL
6212 && (b->loc->next != NULL || !b->loc->enabled)))
6213 header_of_multiple = 1;
6214 if (loc == NULL)
6215 loc = b->loc;
6216
6217 annotate_record ();
6218
6219 /* 1 */
6220 annotate_field (0);
6221 if (part_of_multiple)
6222 {
6223 char *formatted;
6224 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6225 uiout->field_string ("number", formatted);
6226 xfree (formatted);
6227 }
6228 else
6229 {
6230 uiout->field_int ("number", b->number);
6231 }
6232
6233 /* 2 */
6234 annotate_field (1);
6235 if (part_of_multiple)
6236 uiout->field_skip ("type");
6237 else
6238 uiout->field_string ("type", bptype_string (b->type));
6239
6240 /* 3 */
6241 annotate_field (2);
6242 if (part_of_multiple)
6243 uiout->field_skip ("disp");
6244 else
6245 uiout->field_string ("disp", bpdisp_text (b->disposition));
6246
6247
6248 /* 4 */
6249 annotate_field (3);
6250 if (part_of_multiple)
6251 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6252 else
6253 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6254 uiout->spaces (2);
6255
6256
6257 /* 5 and 6 */
6258 if (b->ops != NULL && b->ops->print_one != NULL)
6259 {
6260 /* Although the print_one can possibly print all locations,
6261 calling it here is not likely to get any nice result. So,
6262 make sure there's just one location. */
6263 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6264 b->ops->print_one (b, last_loc);
6265 }
6266 else
6267 switch (b->type)
6268 {
6269 case bp_none:
6270 internal_error (__FILE__, __LINE__,
6271 _("print_one_breakpoint: bp_none encountered\n"));
6272 break;
6273
6274 case bp_watchpoint:
6275 case bp_hardware_watchpoint:
6276 case bp_read_watchpoint:
6277 case bp_access_watchpoint:
6278 {
6279 struct watchpoint *w = (struct watchpoint *) b;
6280
6281 /* Field 4, the address, is omitted (which makes the columns
6282 not line up too nicely with the headers, but the effect
6283 is relatively readable). */
6284 if (opts.addressprint)
6285 uiout->field_skip ("addr");
6286 annotate_field (5);
6287 uiout->field_string ("what", w->exp_string);
6288 }
6289 break;
6290
6291 case bp_breakpoint:
6292 case bp_hardware_breakpoint:
6293 case bp_single_step:
6294 case bp_until:
6295 case bp_finish:
6296 case bp_longjmp:
6297 case bp_longjmp_resume:
6298 case bp_longjmp_call_dummy:
6299 case bp_exception:
6300 case bp_exception_resume:
6301 case bp_step_resume:
6302 case bp_hp_step_resume:
6303 case bp_watchpoint_scope:
6304 case bp_call_dummy:
6305 case bp_std_terminate:
6306 case bp_shlib_event:
6307 case bp_thread_event:
6308 case bp_overlay_event:
6309 case bp_longjmp_master:
6310 case bp_std_terminate_master:
6311 case bp_exception_master:
6312 case bp_tracepoint:
6313 case bp_fast_tracepoint:
6314 case bp_static_tracepoint:
6315 case bp_dprintf:
6316 case bp_jit_event:
6317 case bp_gnu_ifunc_resolver:
6318 case bp_gnu_ifunc_resolver_return:
6319 if (opts.addressprint)
6320 {
6321 annotate_field (4);
6322 if (header_of_multiple)
6323 uiout->field_string ("addr", "<MULTIPLE>");
6324 else if (b->loc == NULL || loc->shlib_disabled)
6325 uiout->field_string ("addr", "<PENDING>");
6326 else
6327 uiout->field_core_addr ("addr",
6328 loc->gdbarch, loc->address);
6329 }
6330 annotate_field (5);
6331 if (!header_of_multiple)
6332 print_breakpoint_location (b, loc);
6333 if (b->loc)
6334 *last_loc = b->loc;
6335 break;
6336 }
6337
6338
6339 if (loc != NULL && !header_of_multiple)
6340 {
6341 struct inferior *inf;
6342 VEC(int) *inf_num = NULL;
6343 int mi_only = 1;
6344
6345 ALL_INFERIORS (inf)
6346 {
6347 if (inf->pspace == loc->pspace)
6348 VEC_safe_push (int, inf_num, inf->num);
6349 }
6350
6351 /* For backward compatibility, don't display inferiors in CLI unless
6352 there are several. Always display for MI. */
6353 if (allflag
6354 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6355 && (number_of_program_spaces () > 1
6356 || number_of_inferiors () > 1)
6357 /* LOC is for existing B, it cannot be in
6358 moribund_locations and thus having NULL OWNER. */
6359 && loc->owner->type != bp_catchpoint))
6360 mi_only = 0;
6361 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6362 VEC_free (int, inf_num);
6363 }
6364
6365 if (!part_of_multiple)
6366 {
6367 if (b->thread != -1)
6368 {
6369 /* FIXME: This seems to be redundant and lost here; see the
6370 "stop only in" line a little further down. */
6371 uiout->text (" thread ");
6372 uiout->field_int ("thread", b->thread);
6373 }
6374 else if (b->task != 0)
6375 {
6376 uiout->text (" task ");
6377 uiout->field_int ("task", b->task);
6378 }
6379 }
6380
6381 uiout->text ("\n");
6382
6383 if (!part_of_multiple)
6384 b->ops->print_one_detail (b, uiout);
6385
6386 if (part_of_multiple && frame_id_p (b->frame_id))
6387 {
6388 annotate_field (6);
6389 uiout->text ("\tstop only in stack frame at ");
6390 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6391 the frame ID. */
6392 uiout->field_core_addr ("frame",
6393 b->gdbarch, b->frame_id.stack_addr);
6394 uiout->text ("\n");
6395 }
6396
6397 if (!part_of_multiple && b->cond_string)
6398 {
6399 annotate_field (7);
6400 if (is_tracepoint (b))
6401 uiout->text ("\ttrace only if ");
6402 else
6403 uiout->text ("\tstop only if ");
6404 uiout->field_string ("cond", b->cond_string);
6405
6406 /* Print whether the target is doing the breakpoint's condition
6407 evaluation. If GDB is doing the evaluation, don't print anything. */
6408 if (is_breakpoint (b)
6409 && breakpoint_condition_evaluation_mode ()
6410 == condition_evaluation_target)
6411 {
6412 uiout->text (" (");
6413 uiout->field_string ("evaluated-by",
6414 bp_condition_evaluator (b));
6415 uiout->text (" evals)");
6416 }
6417 uiout->text ("\n");
6418 }
6419
6420 if (!part_of_multiple && b->thread != -1)
6421 {
6422 /* FIXME should make an annotation for this. */
6423 uiout->text ("\tstop only in thread ");
6424 if (uiout->is_mi_like_p ())
6425 uiout->field_int ("thread", b->thread);
6426 else
6427 {
6428 struct thread_info *thr = find_thread_global_id (b->thread);
6429
6430 uiout->field_string ("thread", print_thread_id (thr));
6431 }
6432 uiout->text ("\n");
6433 }
6434
6435 if (!part_of_multiple)
6436 {
6437 if (b->hit_count)
6438 {
6439 /* FIXME should make an annotation for this. */
6440 if (is_catchpoint (b))
6441 uiout->text ("\tcatchpoint");
6442 else if (is_tracepoint (b))
6443 uiout->text ("\ttracepoint");
6444 else
6445 uiout->text ("\tbreakpoint");
6446 uiout->text (" already hit ");
6447 uiout->field_int ("times", b->hit_count);
6448 if (b->hit_count == 1)
6449 uiout->text (" time\n");
6450 else
6451 uiout->text (" times\n");
6452 }
6453 else
6454 {
6455 /* Output the count also if it is zero, but only if this is mi. */
6456 if (uiout->is_mi_like_p ())
6457 uiout->field_int ("times", b->hit_count);
6458 }
6459 }
6460
6461 if (!part_of_multiple && b->ignore_count)
6462 {
6463 annotate_field (8);
6464 uiout->text ("\tignore next ");
6465 uiout->field_int ("ignore", b->ignore_count);
6466 uiout->text (" hits\n");
6467 }
6468
6469 /* Note that an enable count of 1 corresponds to "enable once"
6470 behavior, which is reported by the combination of enablement and
6471 disposition, so we don't need to mention it here. */
6472 if (!part_of_multiple && b->enable_count > 1)
6473 {
6474 annotate_field (8);
6475 uiout->text ("\tdisable after ");
6476 /* Tweak the wording to clarify that ignore and enable counts
6477 are distinct, and have additive effect. */
6478 if (b->ignore_count)
6479 uiout->text ("additional ");
6480 else
6481 uiout->text ("next ");
6482 uiout->field_int ("enable", b->enable_count);
6483 uiout->text (" hits\n");
6484 }
6485
6486 if (!part_of_multiple && is_tracepoint (b))
6487 {
6488 struct tracepoint *tp = (struct tracepoint *) b;
6489
6490 if (tp->traceframe_usage)
6491 {
6492 uiout->text ("\ttrace buffer usage ");
6493 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6494 uiout->text (" bytes\n");
6495 }
6496 }
6497
6498 l = b->commands ? b->commands.get () : NULL;
6499 if (!part_of_multiple && l)
6500 {
6501 annotate_field (9);
6502 ui_out_emit_tuple tuple_emitter (uiout, "script");
6503 print_command_lines (uiout, l, 4);
6504 }
6505
6506 if (is_tracepoint (b))
6507 {
6508 struct tracepoint *t = (struct tracepoint *) b;
6509
6510 if (!part_of_multiple && t->pass_count)
6511 {
6512 annotate_field (10);
6513 uiout->text ("\tpass count ");
6514 uiout->field_int ("pass", t->pass_count);
6515 uiout->text (" \n");
6516 }
6517
6518 /* Don't display it when tracepoint or tracepoint location is
6519 pending. */
6520 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6521 {
6522 annotate_field (11);
6523
6524 if (uiout->is_mi_like_p ())
6525 uiout->field_string ("installed",
6526 loc->inserted ? "y" : "n");
6527 else
6528 {
6529 if (loc->inserted)
6530 uiout->text ("\t");
6531 else
6532 uiout->text ("\tnot ");
6533 uiout->text ("installed on target\n");
6534 }
6535 }
6536 }
6537
6538 if (uiout->is_mi_like_p () && !part_of_multiple)
6539 {
6540 if (is_watchpoint (b))
6541 {
6542 struct watchpoint *w = (struct watchpoint *) b;
6543
6544 uiout->field_string ("original-location", w->exp_string);
6545 }
6546 else if (b->location != NULL
6547 && event_location_to_string (b->location.get ()) != NULL)
6548 uiout->field_string ("original-location",
6549 event_location_to_string (b->location.get ()));
6550 }
6551 }
6552
6553 static void
6554 print_one_breakpoint (struct breakpoint *b,
6555 struct bp_location **last_loc,
6556 int allflag)
6557 {
6558 struct ui_out *uiout = current_uiout;
6559
6560 {
6561 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6562
6563 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6564 }
6565
6566 /* If this breakpoint has custom print function,
6567 it's already printed. Otherwise, print individual
6568 locations, if any. */
6569 if (b->ops == NULL || b->ops->print_one == NULL)
6570 {
6571 /* If breakpoint has a single location that is disabled, we
6572 print it as if it had several locations, since otherwise it's
6573 hard to represent "breakpoint enabled, location disabled"
6574 situation.
6575
6576 Note that while hardware watchpoints have several locations
6577 internally, that's not a property exposed to user. */
6578 if (b->loc
6579 && !is_hardware_watchpoint (b)
6580 && (b->loc->next || !b->loc->enabled))
6581 {
6582 struct bp_location *loc;
6583 int n = 1;
6584
6585 for (loc = b->loc; loc; loc = loc->next, ++n)
6586 {
6587 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6588 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6589 }
6590 }
6591 }
6592 }
6593
6594 static int
6595 breakpoint_address_bits (struct breakpoint *b)
6596 {
6597 int print_address_bits = 0;
6598 struct bp_location *loc;
6599
6600 /* Software watchpoints that aren't watching memory don't have an
6601 address to print. */
6602 if (is_no_memory_software_watchpoint (b))
6603 return 0;
6604
6605 for (loc = b->loc; loc; loc = loc->next)
6606 {
6607 int addr_bit;
6608
6609 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6610 if (addr_bit > print_address_bits)
6611 print_address_bits = addr_bit;
6612 }
6613
6614 return print_address_bits;
6615 }
6616
6617 struct captured_breakpoint_query_args
6618 {
6619 int bnum;
6620 };
6621
6622 static int
6623 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6624 {
6625 struct captured_breakpoint_query_args *args
6626 = (struct captured_breakpoint_query_args *) data;
6627 struct breakpoint *b;
6628 struct bp_location *dummy_loc = NULL;
6629
6630 ALL_BREAKPOINTS (b)
6631 {
6632 if (args->bnum == b->number)
6633 {
6634 print_one_breakpoint (b, &dummy_loc, 0);
6635 return GDB_RC_OK;
6636 }
6637 }
6638 return GDB_RC_NONE;
6639 }
6640
6641 enum gdb_rc
6642 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6643 char **error_message)
6644 {
6645 struct captured_breakpoint_query_args args;
6646
6647 args.bnum = bnum;
6648 /* For the moment we don't trust print_one_breakpoint() to not throw
6649 an error. */
6650 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6651 error_message, RETURN_MASK_ALL) < 0)
6652 return GDB_RC_FAIL;
6653 else
6654 return GDB_RC_OK;
6655 }
6656
6657 /* Return true if this breakpoint was set by the user, false if it is
6658 internal or momentary. */
6659
6660 int
6661 user_breakpoint_p (struct breakpoint *b)
6662 {
6663 return b->number > 0;
6664 }
6665
6666 /* See breakpoint.h. */
6667
6668 int
6669 pending_breakpoint_p (struct breakpoint *b)
6670 {
6671 return b->loc == NULL;
6672 }
6673
6674 /* Print information on user settable breakpoint (watchpoint, etc)
6675 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6676 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6677 FILTER is non-NULL, call it on each breakpoint and only include the
6678 ones for which it returns non-zero. Return the total number of
6679 breakpoints listed. */
6680
6681 static int
6682 breakpoint_1 (char *args, int allflag,
6683 int (*filter) (const struct breakpoint *))
6684 {
6685 struct breakpoint *b;
6686 struct bp_location *last_loc = NULL;
6687 int nr_printable_breakpoints;
6688 struct value_print_options opts;
6689 int print_address_bits = 0;
6690 int print_type_col_width = 14;
6691 struct ui_out *uiout = current_uiout;
6692
6693 get_user_print_options (&opts);
6694
6695 /* Compute the number of rows in the table, as well as the size
6696 required for address fields. */
6697 nr_printable_breakpoints = 0;
6698 ALL_BREAKPOINTS (b)
6699 {
6700 /* If we have a filter, only list the breakpoints it accepts. */
6701 if (filter && !filter (b))
6702 continue;
6703
6704 /* If we have an "args" string, it is a list of breakpoints to
6705 accept. Skip the others. */
6706 if (args != NULL && *args != '\0')
6707 {
6708 if (allflag && parse_and_eval_long (args) != b->number)
6709 continue;
6710 if (!allflag && !number_is_in_list (args, b->number))
6711 continue;
6712 }
6713
6714 if (allflag || user_breakpoint_p (b))
6715 {
6716 int addr_bit, type_len;
6717
6718 addr_bit = breakpoint_address_bits (b);
6719 if (addr_bit > print_address_bits)
6720 print_address_bits = addr_bit;
6721
6722 type_len = strlen (bptype_string (b->type));
6723 if (type_len > print_type_col_width)
6724 print_type_col_width = type_len;
6725
6726 nr_printable_breakpoints++;
6727 }
6728 }
6729
6730 {
6731 ui_out_emit_table table_emitter (uiout,
6732 opts.addressprint ? 6 : 5,
6733 nr_printable_breakpoints,
6734 "BreakpointTable");
6735
6736 if (nr_printable_breakpoints > 0)
6737 annotate_breakpoints_headers ();
6738 if (nr_printable_breakpoints > 0)
6739 annotate_field (0);
6740 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6741 if (nr_printable_breakpoints > 0)
6742 annotate_field (1);
6743 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6744 if (nr_printable_breakpoints > 0)
6745 annotate_field (2);
6746 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6747 if (nr_printable_breakpoints > 0)
6748 annotate_field (3);
6749 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6750 if (opts.addressprint)
6751 {
6752 if (nr_printable_breakpoints > 0)
6753 annotate_field (4);
6754 if (print_address_bits <= 32)
6755 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6756 else
6757 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6758 }
6759 if (nr_printable_breakpoints > 0)
6760 annotate_field (5);
6761 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6762 uiout->table_body ();
6763 if (nr_printable_breakpoints > 0)
6764 annotate_breakpoints_table ();
6765
6766 ALL_BREAKPOINTS (b)
6767 {
6768 QUIT;
6769 /* If we have a filter, only list the breakpoints it accepts. */
6770 if (filter && !filter (b))
6771 continue;
6772
6773 /* If we have an "args" string, it is a list of breakpoints to
6774 accept. Skip the others. */
6775
6776 if (args != NULL && *args != '\0')
6777 {
6778 if (allflag) /* maintenance info breakpoint */
6779 {
6780 if (parse_and_eval_long (args) != b->number)
6781 continue;
6782 }
6783 else /* all others */
6784 {
6785 if (!number_is_in_list (args, b->number))
6786 continue;
6787 }
6788 }
6789 /* We only print out user settable breakpoints unless the
6790 allflag is set. */
6791 if (allflag || user_breakpoint_p (b))
6792 print_one_breakpoint (b, &last_loc, allflag);
6793 }
6794 }
6795
6796 if (nr_printable_breakpoints == 0)
6797 {
6798 /* If there's a filter, let the caller decide how to report
6799 empty list. */
6800 if (!filter)
6801 {
6802 if (args == NULL || *args == '\0')
6803 uiout->message ("No breakpoints or watchpoints.\n");
6804 else
6805 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6806 args);
6807 }
6808 }
6809 else
6810 {
6811 if (last_loc && !server_command)
6812 set_next_address (last_loc->gdbarch, last_loc->address);
6813 }
6814
6815 /* FIXME? Should this be moved up so that it is only called when
6816 there have been breakpoints? */
6817 annotate_breakpoints_table_end ();
6818
6819 return nr_printable_breakpoints;
6820 }
6821
6822 /* Display the value of default-collect in a way that is generally
6823 compatible with the breakpoint list. */
6824
6825 static void
6826 default_collect_info (void)
6827 {
6828 struct ui_out *uiout = current_uiout;
6829
6830 /* If it has no value (which is frequently the case), say nothing; a
6831 message like "No default-collect." gets in user's face when it's
6832 not wanted. */
6833 if (!*default_collect)
6834 return;
6835
6836 /* The following phrase lines up nicely with per-tracepoint collect
6837 actions. */
6838 uiout->text ("default collect ");
6839 uiout->field_string ("default-collect", default_collect);
6840 uiout->text (" \n");
6841 }
6842
6843 static void
6844 info_breakpoints_command (char *args, int from_tty)
6845 {
6846 breakpoint_1 (args, 0, NULL);
6847
6848 default_collect_info ();
6849 }
6850
6851 static void
6852 info_watchpoints_command (char *args, int from_tty)
6853 {
6854 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6855 struct ui_out *uiout = current_uiout;
6856
6857 if (num_printed == 0)
6858 {
6859 if (args == NULL || *args == '\0')
6860 uiout->message ("No watchpoints.\n");
6861 else
6862 uiout->message ("No watchpoint matching '%s'.\n", args);
6863 }
6864 }
6865
6866 static void
6867 maintenance_info_breakpoints (char *args, int from_tty)
6868 {
6869 breakpoint_1 (args, 1, NULL);
6870
6871 default_collect_info ();
6872 }
6873
6874 static int
6875 breakpoint_has_pc (struct breakpoint *b,
6876 struct program_space *pspace,
6877 CORE_ADDR pc, struct obj_section *section)
6878 {
6879 struct bp_location *bl = b->loc;
6880
6881 for (; bl; bl = bl->next)
6882 {
6883 if (bl->pspace == pspace
6884 && bl->address == pc
6885 && (!overlay_debugging || bl->section == section))
6886 return 1;
6887 }
6888 return 0;
6889 }
6890
6891 /* Print a message describing any user-breakpoints set at PC. This
6892 concerns with logical breakpoints, so we match program spaces, not
6893 address spaces. */
6894
6895 static void
6896 describe_other_breakpoints (struct gdbarch *gdbarch,
6897 struct program_space *pspace, CORE_ADDR pc,
6898 struct obj_section *section, int thread)
6899 {
6900 int others = 0;
6901 struct breakpoint *b;
6902
6903 ALL_BREAKPOINTS (b)
6904 others += (user_breakpoint_p (b)
6905 && breakpoint_has_pc (b, pspace, pc, section));
6906 if (others > 0)
6907 {
6908 if (others == 1)
6909 printf_filtered (_("Note: breakpoint "));
6910 else /* if (others == ???) */
6911 printf_filtered (_("Note: breakpoints "));
6912 ALL_BREAKPOINTS (b)
6913 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6914 {
6915 others--;
6916 printf_filtered ("%d", b->number);
6917 if (b->thread == -1 && thread != -1)
6918 printf_filtered (" (all threads)");
6919 else if (b->thread != -1)
6920 printf_filtered (" (thread %d)", b->thread);
6921 printf_filtered ("%s%s ",
6922 ((b->enable_state == bp_disabled
6923 || b->enable_state == bp_call_disabled)
6924 ? " (disabled)"
6925 : ""),
6926 (others > 1) ? ","
6927 : ((others == 1) ? " and" : ""));
6928 }
6929 printf_filtered (_("also set at pc "));
6930 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6931 printf_filtered (".\n");
6932 }
6933 }
6934 \f
6935
6936 /* Return true iff it is meaningful to use the address member of
6937 BPT locations. For some breakpoint types, the locations' address members
6938 are irrelevant and it makes no sense to attempt to compare them to other
6939 addresses (or use them for any other purpose either).
6940
6941 More specifically, each of the following breakpoint types will
6942 always have a zero valued location address and we don't want to mark
6943 breakpoints of any of these types to be a duplicate of an actual
6944 breakpoint location at address zero:
6945
6946 bp_watchpoint
6947 bp_catchpoint
6948
6949 */
6950
6951 static int
6952 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6953 {
6954 enum bptype type = bpt->type;
6955
6956 return (type != bp_watchpoint && type != bp_catchpoint);
6957 }
6958
6959 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6960 true if LOC1 and LOC2 represent the same watchpoint location. */
6961
6962 static int
6963 watchpoint_locations_match (struct bp_location *loc1,
6964 struct bp_location *loc2)
6965 {
6966 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6967 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6968
6969 /* Both of them must exist. */
6970 gdb_assert (w1 != NULL);
6971 gdb_assert (w2 != NULL);
6972
6973 /* If the target can evaluate the condition expression in hardware,
6974 then we we need to insert both watchpoints even if they are at
6975 the same place. Otherwise the watchpoint will only trigger when
6976 the condition of whichever watchpoint was inserted evaluates to
6977 true, not giving a chance for GDB to check the condition of the
6978 other watchpoint. */
6979 if ((w1->cond_exp
6980 && target_can_accel_watchpoint_condition (loc1->address,
6981 loc1->length,
6982 loc1->watchpoint_type,
6983 w1->cond_exp.get ()))
6984 || (w2->cond_exp
6985 && target_can_accel_watchpoint_condition (loc2->address,
6986 loc2->length,
6987 loc2->watchpoint_type,
6988 w2->cond_exp.get ())))
6989 return 0;
6990
6991 /* Note that this checks the owner's type, not the location's. In
6992 case the target does not support read watchpoints, but does
6993 support access watchpoints, we'll have bp_read_watchpoint
6994 watchpoints with hw_access locations. Those should be considered
6995 duplicates of hw_read locations. The hw_read locations will
6996 become hw_access locations later. */
6997 return (loc1->owner->type == loc2->owner->type
6998 && loc1->pspace->aspace == loc2->pspace->aspace
6999 && loc1->address == loc2->address
7000 && loc1->length == loc2->length);
7001 }
7002
7003 /* See breakpoint.h. */
7004
7005 int
7006 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7007 struct address_space *aspace2, CORE_ADDR addr2)
7008 {
7009 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7010 || aspace1 == aspace2)
7011 && addr1 == addr2);
7012 }
7013
7014 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7015 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7016 matches ASPACE2. On targets that have global breakpoints, the address
7017 space doesn't really matter. */
7018
7019 static int
7020 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7021 int len1, struct address_space *aspace2,
7022 CORE_ADDR addr2)
7023 {
7024 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7025 || aspace1 == aspace2)
7026 && addr2 >= addr1 && addr2 < addr1 + len1);
7027 }
7028
7029 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7030 a ranged breakpoint. In most targets, a match happens only if ASPACE
7031 matches the breakpoint's address space. On targets that have global
7032 breakpoints, the address space doesn't really matter. */
7033
7034 static int
7035 breakpoint_location_address_match (struct bp_location *bl,
7036 struct address_space *aspace,
7037 CORE_ADDR addr)
7038 {
7039 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7040 aspace, addr)
7041 || (bl->length
7042 && breakpoint_address_match_range (bl->pspace->aspace,
7043 bl->address, bl->length,
7044 aspace, addr)));
7045 }
7046
7047 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7048 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7049 match happens only if ASPACE matches the breakpoint's address
7050 space. On targets that have global breakpoints, the address space
7051 doesn't really matter. */
7052
7053 static int
7054 breakpoint_location_address_range_overlap (struct bp_location *bl,
7055 struct address_space *aspace,
7056 CORE_ADDR addr, int len)
7057 {
7058 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7059 || bl->pspace->aspace == aspace)
7060 {
7061 int bl_len = bl->length != 0 ? bl->length : 1;
7062
7063 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7064 return 1;
7065 }
7066 return 0;
7067 }
7068
7069 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7070 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7071 true, otherwise returns false. */
7072
7073 static int
7074 tracepoint_locations_match (struct bp_location *loc1,
7075 struct bp_location *loc2)
7076 {
7077 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7078 /* Since tracepoint locations are never duplicated with others', tracepoint
7079 locations at the same address of different tracepoints are regarded as
7080 different locations. */
7081 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7082 else
7083 return 0;
7084 }
7085
7086 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7087 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7088 represent the same location. */
7089
7090 static int
7091 breakpoint_locations_match (struct bp_location *loc1,
7092 struct bp_location *loc2)
7093 {
7094 int hw_point1, hw_point2;
7095
7096 /* Both of them must not be in moribund_locations. */
7097 gdb_assert (loc1->owner != NULL);
7098 gdb_assert (loc2->owner != NULL);
7099
7100 hw_point1 = is_hardware_watchpoint (loc1->owner);
7101 hw_point2 = is_hardware_watchpoint (loc2->owner);
7102
7103 if (hw_point1 != hw_point2)
7104 return 0;
7105 else if (hw_point1)
7106 return watchpoint_locations_match (loc1, loc2);
7107 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7108 return tracepoint_locations_match (loc1, loc2);
7109 else
7110 /* We compare bp_location.length in order to cover ranged breakpoints. */
7111 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7112 loc2->pspace->aspace, loc2->address)
7113 && loc1->length == loc2->length);
7114 }
7115
7116 static void
7117 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7118 int bnum, int have_bnum)
7119 {
7120 /* The longest string possibly returned by hex_string_custom
7121 is 50 chars. These must be at least that big for safety. */
7122 char astr1[64];
7123 char astr2[64];
7124
7125 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7126 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7127 if (have_bnum)
7128 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7129 bnum, astr1, astr2);
7130 else
7131 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7132 }
7133
7134 /* Adjust a breakpoint's address to account for architectural
7135 constraints on breakpoint placement. Return the adjusted address.
7136 Note: Very few targets require this kind of adjustment. For most
7137 targets, this function is simply the identity function. */
7138
7139 static CORE_ADDR
7140 adjust_breakpoint_address (struct gdbarch *gdbarch,
7141 CORE_ADDR bpaddr, enum bptype bptype)
7142 {
7143 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7144 {
7145 /* Very few targets need any kind of breakpoint adjustment. */
7146 return bpaddr;
7147 }
7148 else if (bptype == bp_watchpoint
7149 || bptype == bp_hardware_watchpoint
7150 || bptype == bp_read_watchpoint
7151 || bptype == bp_access_watchpoint
7152 || bptype == bp_catchpoint)
7153 {
7154 /* Watchpoints and the various bp_catch_* eventpoints should not
7155 have their addresses modified. */
7156 return bpaddr;
7157 }
7158 else if (bptype == bp_single_step)
7159 {
7160 /* Single-step breakpoints should not have their addresses
7161 modified. If there's any architectural constrain that
7162 applies to this address, then it should have already been
7163 taken into account when the breakpoint was created in the
7164 first place. If we didn't do this, stepping through e.g.,
7165 Thumb-2 IT blocks would break. */
7166 return bpaddr;
7167 }
7168 else
7169 {
7170 CORE_ADDR adjusted_bpaddr;
7171
7172 /* Some targets have architectural constraints on the placement
7173 of breakpoint instructions. Obtain the adjusted address. */
7174 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7175
7176 /* An adjusted breakpoint address can significantly alter
7177 a user's expectations. Print a warning if an adjustment
7178 is required. */
7179 if (adjusted_bpaddr != bpaddr)
7180 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7181
7182 return adjusted_bpaddr;
7183 }
7184 }
7185
7186 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7187 {
7188 bp_location *loc = this;
7189
7190 gdb_assert (ops != NULL);
7191
7192 loc->ops = ops;
7193 loc->owner = owner;
7194 loc->cond_bytecode = NULL;
7195 loc->shlib_disabled = 0;
7196 loc->enabled = 1;
7197
7198 switch (owner->type)
7199 {
7200 case bp_breakpoint:
7201 case bp_single_step:
7202 case bp_until:
7203 case bp_finish:
7204 case bp_longjmp:
7205 case bp_longjmp_resume:
7206 case bp_longjmp_call_dummy:
7207 case bp_exception:
7208 case bp_exception_resume:
7209 case bp_step_resume:
7210 case bp_hp_step_resume:
7211 case bp_watchpoint_scope:
7212 case bp_call_dummy:
7213 case bp_std_terminate:
7214 case bp_shlib_event:
7215 case bp_thread_event:
7216 case bp_overlay_event:
7217 case bp_jit_event:
7218 case bp_longjmp_master:
7219 case bp_std_terminate_master:
7220 case bp_exception_master:
7221 case bp_gnu_ifunc_resolver:
7222 case bp_gnu_ifunc_resolver_return:
7223 case bp_dprintf:
7224 loc->loc_type = bp_loc_software_breakpoint;
7225 mark_breakpoint_location_modified (loc);
7226 break;
7227 case bp_hardware_breakpoint:
7228 loc->loc_type = bp_loc_hardware_breakpoint;
7229 mark_breakpoint_location_modified (loc);
7230 break;
7231 case bp_hardware_watchpoint:
7232 case bp_read_watchpoint:
7233 case bp_access_watchpoint:
7234 loc->loc_type = bp_loc_hardware_watchpoint;
7235 break;
7236 case bp_watchpoint:
7237 case bp_catchpoint:
7238 case bp_tracepoint:
7239 case bp_fast_tracepoint:
7240 case bp_static_tracepoint:
7241 loc->loc_type = bp_loc_other;
7242 break;
7243 default:
7244 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7245 }
7246
7247 loc->refc = 1;
7248 }
7249
7250 /* Allocate a struct bp_location. */
7251
7252 static struct bp_location *
7253 allocate_bp_location (struct breakpoint *bpt)
7254 {
7255 return bpt->ops->allocate_location (bpt);
7256 }
7257
7258 static void
7259 free_bp_location (struct bp_location *loc)
7260 {
7261 loc->ops->dtor (loc);
7262 delete loc;
7263 }
7264
7265 /* Increment reference count. */
7266
7267 static void
7268 incref_bp_location (struct bp_location *bl)
7269 {
7270 ++bl->refc;
7271 }
7272
7273 /* Decrement reference count. If the reference count reaches 0,
7274 destroy the bp_location. Sets *BLP to NULL. */
7275
7276 static void
7277 decref_bp_location (struct bp_location **blp)
7278 {
7279 gdb_assert ((*blp)->refc > 0);
7280
7281 if (--(*blp)->refc == 0)
7282 free_bp_location (*blp);
7283 *blp = NULL;
7284 }
7285
7286 /* Add breakpoint B at the end of the global breakpoint chain. */
7287
7288 static breakpoint *
7289 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7290 {
7291 struct breakpoint *b1;
7292 struct breakpoint *result = b.get ();
7293
7294 /* Add this breakpoint to the end of the chain so that a list of
7295 breakpoints will come out in order of increasing numbers. */
7296
7297 b1 = breakpoint_chain;
7298 if (b1 == 0)
7299 breakpoint_chain = b.release ();
7300 else
7301 {
7302 while (b1->next)
7303 b1 = b1->next;
7304 b1->next = b.release ();
7305 }
7306
7307 return result;
7308 }
7309
7310 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7311
7312 static void
7313 init_raw_breakpoint_without_location (struct breakpoint *b,
7314 struct gdbarch *gdbarch,
7315 enum bptype bptype,
7316 const struct breakpoint_ops *ops)
7317 {
7318 gdb_assert (ops != NULL);
7319
7320 b->ops = ops;
7321 b->type = bptype;
7322 b->gdbarch = gdbarch;
7323 b->language = current_language->la_language;
7324 b->input_radix = input_radix;
7325 b->related_breakpoint = b;
7326 }
7327
7328 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7329 that has type BPTYPE and has no locations as yet. */
7330
7331 static struct breakpoint *
7332 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7333 enum bptype bptype,
7334 const struct breakpoint_ops *ops)
7335 {
7336 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7337
7338 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7339 return add_to_breakpoint_chain (std::move (b));
7340 }
7341
7342 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7343 resolutions should be made as the user specified the location explicitly
7344 enough. */
7345
7346 static void
7347 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7348 {
7349 gdb_assert (loc->owner != NULL);
7350
7351 if (loc->owner->type == bp_breakpoint
7352 || loc->owner->type == bp_hardware_breakpoint
7353 || is_tracepoint (loc->owner))
7354 {
7355 int is_gnu_ifunc;
7356 const char *function_name;
7357 CORE_ADDR func_addr;
7358
7359 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7360 &func_addr, NULL, &is_gnu_ifunc);
7361
7362 if (is_gnu_ifunc && !explicit_loc)
7363 {
7364 struct breakpoint *b = loc->owner;
7365
7366 gdb_assert (loc->pspace == current_program_space);
7367 if (gnu_ifunc_resolve_name (function_name,
7368 &loc->requested_address))
7369 {
7370 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7371 loc->address = adjust_breakpoint_address (loc->gdbarch,
7372 loc->requested_address,
7373 b->type);
7374 }
7375 else if (b->type == bp_breakpoint && b->loc == loc
7376 && loc->next == NULL && b->related_breakpoint == b)
7377 {
7378 /* Create only the whole new breakpoint of this type but do not
7379 mess more complicated breakpoints with multiple locations. */
7380 b->type = bp_gnu_ifunc_resolver;
7381 /* Remember the resolver's address for use by the return
7382 breakpoint. */
7383 loc->related_address = func_addr;
7384 }
7385 }
7386
7387 if (function_name)
7388 loc->function_name = xstrdup (function_name);
7389 }
7390 }
7391
7392 /* Attempt to determine architecture of location identified by SAL. */
7393 struct gdbarch *
7394 get_sal_arch (struct symtab_and_line sal)
7395 {
7396 if (sal.section)
7397 return get_objfile_arch (sal.section->objfile);
7398 if (sal.symtab)
7399 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7400
7401 return NULL;
7402 }
7403
7404 /* Low level routine for partially initializing a breakpoint of type
7405 BPTYPE. The newly created breakpoint's address, section, source
7406 file name, and line number are provided by SAL.
7407
7408 It is expected that the caller will complete the initialization of
7409 the newly created breakpoint struct as well as output any status
7410 information regarding the creation of a new breakpoint. */
7411
7412 static void
7413 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7414 struct symtab_and_line sal, enum bptype bptype,
7415 const struct breakpoint_ops *ops)
7416 {
7417 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7418
7419 add_location_to_breakpoint (b, &sal);
7420
7421 if (bptype != bp_catchpoint)
7422 gdb_assert (sal.pspace != NULL);
7423
7424 /* Store the program space that was used to set the breakpoint,
7425 except for ordinary breakpoints, which are independent of the
7426 program space. */
7427 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7428 b->pspace = sal.pspace;
7429 }
7430
7431 /* set_raw_breakpoint is a low level routine for allocating and
7432 partially initializing a breakpoint of type BPTYPE. The newly
7433 created breakpoint's address, section, source file name, and line
7434 number are provided by SAL. The newly created and partially
7435 initialized breakpoint is added to the breakpoint chain and
7436 is also returned as the value of this function.
7437
7438 It is expected that the caller will complete the initialization of
7439 the newly created breakpoint struct as well as output any status
7440 information regarding the creation of a new breakpoint. In
7441 particular, set_raw_breakpoint does NOT set the breakpoint
7442 number! Care should be taken to not allow an error to occur
7443 prior to completing the initialization of the breakpoint. If this
7444 should happen, a bogus breakpoint will be left on the chain. */
7445
7446 struct breakpoint *
7447 set_raw_breakpoint (struct gdbarch *gdbarch,
7448 struct symtab_and_line sal, enum bptype bptype,
7449 const struct breakpoint_ops *ops)
7450 {
7451 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7452
7453 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7454 return add_to_breakpoint_chain (std::move (b));
7455 }
7456
7457 /* Call this routine when stepping and nexting to enable a breakpoint
7458 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7459 initiated the operation. */
7460
7461 void
7462 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7463 {
7464 struct breakpoint *b, *b_tmp;
7465 int thread = tp->global_num;
7466
7467 /* To avoid having to rescan all objfile symbols at every step,
7468 we maintain a list of continually-inserted but always disabled
7469 longjmp "master" breakpoints. Here, we simply create momentary
7470 clones of those and enable them for the requested thread. */
7471 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7472 if (b->pspace == current_program_space
7473 && (b->type == bp_longjmp_master
7474 || b->type == bp_exception_master))
7475 {
7476 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7477 struct breakpoint *clone;
7478
7479 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7480 after their removal. */
7481 clone = momentary_breakpoint_from_master (b, type,
7482 &momentary_breakpoint_ops, 1);
7483 clone->thread = thread;
7484 }
7485
7486 tp->initiating_frame = frame;
7487 }
7488
7489 /* Delete all longjmp breakpoints from THREAD. */
7490 void
7491 delete_longjmp_breakpoint (int thread)
7492 {
7493 struct breakpoint *b, *b_tmp;
7494
7495 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7496 if (b->type == bp_longjmp || b->type == bp_exception)
7497 {
7498 if (b->thread == thread)
7499 delete_breakpoint (b);
7500 }
7501 }
7502
7503 void
7504 delete_longjmp_breakpoint_at_next_stop (int thread)
7505 {
7506 struct breakpoint *b, *b_tmp;
7507
7508 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7509 if (b->type == bp_longjmp || b->type == bp_exception)
7510 {
7511 if (b->thread == thread)
7512 b->disposition = disp_del_at_next_stop;
7513 }
7514 }
7515
7516 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7517 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7518 pointer to any of them. Return NULL if this system cannot place longjmp
7519 breakpoints. */
7520
7521 struct breakpoint *
7522 set_longjmp_breakpoint_for_call_dummy (void)
7523 {
7524 struct breakpoint *b, *retval = NULL;
7525
7526 ALL_BREAKPOINTS (b)
7527 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7528 {
7529 struct breakpoint *new_b;
7530
7531 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7532 &momentary_breakpoint_ops,
7533 1);
7534 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7535
7536 /* Link NEW_B into the chain of RETVAL breakpoints. */
7537
7538 gdb_assert (new_b->related_breakpoint == new_b);
7539 if (retval == NULL)
7540 retval = new_b;
7541 new_b->related_breakpoint = retval;
7542 while (retval->related_breakpoint != new_b->related_breakpoint)
7543 retval = retval->related_breakpoint;
7544 retval->related_breakpoint = new_b;
7545 }
7546
7547 return retval;
7548 }
7549
7550 /* Verify all existing dummy frames and their associated breakpoints for
7551 TP. Remove those which can no longer be found in the current frame
7552 stack.
7553
7554 You should call this function only at places where it is safe to currently
7555 unwind the whole stack. Failed stack unwind would discard live dummy
7556 frames. */
7557
7558 void
7559 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7560 {
7561 struct breakpoint *b, *b_tmp;
7562
7563 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7564 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7565 {
7566 struct breakpoint *dummy_b = b->related_breakpoint;
7567
7568 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7569 dummy_b = dummy_b->related_breakpoint;
7570 if (dummy_b->type != bp_call_dummy
7571 || frame_find_by_id (dummy_b->frame_id) != NULL)
7572 continue;
7573
7574 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7575
7576 while (b->related_breakpoint != b)
7577 {
7578 if (b_tmp == b->related_breakpoint)
7579 b_tmp = b->related_breakpoint->next;
7580 delete_breakpoint (b->related_breakpoint);
7581 }
7582 delete_breakpoint (b);
7583 }
7584 }
7585
7586 void
7587 enable_overlay_breakpoints (void)
7588 {
7589 struct breakpoint *b;
7590
7591 ALL_BREAKPOINTS (b)
7592 if (b->type == bp_overlay_event)
7593 {
7594 b->enable_state = bp_enabled;
7595 update_global_location_list (UGLL_MAY_INSERT);
7596 overlay_events_enabled = 1;
7597 }
7598 }
7599
7600 void
7601 disable_overlay_breakpoints (void)
7602 {
7603 struct breakpoint *b;
7604
7605 ALL_BREAKPOINTS (b)
7606 if (b->type == bp_overlay_event)
7607 {
7608 b->enable_state = bp_disabled;
7609 update_global_location_list (UGLL_DONT_INSERT);
7610 overlay_events_enabled = 0;
7611 }
7612 }
7613
7614 /* Set an active std::terminate breakpoint for each std::terminate
7615 master breakpoint. */
7616 void
7617 set_std_terminate_breakpoint (void)
7618 {
7619 struct breakpoint *b, *b_tmp;
7620
7621 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7622 if (b->pspace == current_program_space
7623 && b->type == bp_std_terminate_master)
7624 {
7625 momentary_breakpoint_from_master (b, bp_std_terminate,
7626 &momentary_breakpoint_ops, 1);
7627 }
7628 }
7629
7630 /* Delete all the std::terminate breakpoints. */
7631 void
7632 delete_std_terminate_breakpoint (void)
7633 {
7634 struct breakpoint *b, *b_tmp;
7635
7636 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7637 if (b->type == bp_std_terminate)
7638 delete_breakpoint (b);
7639 }
7640
7641 struct breakpoint *
7642 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7643 {
7644 struct breakpoint *b;
7645
7646 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7647 &internal_breakpoint_ops);
7648
7649 b->enable_state = bp_enabled;
7650 /* location has to be used or breakpoint_re_set will delete me. */
7651 b->location = new_address_location (b->loc->address, NULL, 0);
7652
7653 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7654
7655 return b;
7656 }
7657
7658 struct lang_and_radix
7659 {
7660 enum language lang;
7661 int radix;
7662 };
7663
7664 /* Create a breakpoint for JIT code registration and unregistration. */
7665
7666 struct breakpoint *
7667 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7668 {
7669 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7670 &internal_breakpoint_ops);
7671 }
7672
7673 /* Remove JIT code registration and unregistration breakpoint(s). */
7674
7675 void
7676 remove_jit_event_breakpoints (void)
7677 {
7678 struct breakpoint *b, *b_tmp;
7679
7680 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7681 if (b->type == bp_jit_event
7682 && b->loc->pspace == current_program_space)
7683 delete_breakpoint (b);
7684 }
7685
7686 void
7687 remove_solib_event_breakpoints (void)
7688 {
7689 struct breakpoint *b, *b_tmp;
7690
7691 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7692 if (b->type == bp_shlib_event
7693 && b->loc->pspace == current_program_space)
7694 delete_breakpoint (b);
7695 }
7696
7697 /* See breakpoint.h. */
7698
7699 void
7700 remove_solib_event_breakpoints_at_next_stop (void)
7701 {
7702 struct breakpoint *b, *b_tmp;
7703
7704 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7705 if (b->type == bp_shlib_event
7706 && b->loc->pspace == current_program_space)
7707 b->disposition = disp_del_at_next_stop;
7708 }
7709
7710 /* Helper for create_solib_event_breakpoint /
7711 create_and_insert_solib_event_breakpoint. Allows specifying which
7712 INSERT_MODE to pass through to update_global_location_list. */
7713
7714 static struct breakpoint *
7715 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7716 enum ugll_insert_mode insert_mode)
7717 {
7718 struct breakpoint *b;
7719
7720 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7721 &internal_breakpoint_ops);
7722 update_global_location_list_nothrow (insert_mode);
7723 return b;
7724 }
7725
7726 struct breakpoint *
7727 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7728 {
7729 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7730 }
7731
7732 /* See breakpoint.h. */
7733
7734 struct breakpoint *
7735 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7736 {
7737 struct breakpoint *b;
7738
7739 /* Explicitly tell update_global_location_list to insert
7740 locations. */
7741 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7742 if (!b->loc->inserted)
7743 {
7744 delete_breakpoint (b);
7745 return NULL;
7746 }
7747 return b;
7748 }
7749
7750 /* Disable any breakpoints that are on code in shared libraries. Only
7751 apply to enabled breakpoints, disabled ones can just stay disabled. */
7752
7753 void
7754 disable_breakpoints_in_shlibs (void)
7755 {
7756 struct bp_location *loc, **locp_tmp;
7757
7758 ALL_BP_LOCATIONS (loc, locp_tmp)
7759 {
7760 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7761 struct breakpoint *b = loc->owner;
7762
7763 /* We apply the check to all breakpoints, including disabled for
7764 those with loc->duplicate set. This is so that when breakpoint
7765 becomes enabled, or the duplicate is removed, gdb will try to
7766 insert all breakpoints. If we don't set shlib_disabled here,
7767 we'll try to insert those breakpoints and fail. */
7768 if (((b->type == bp_breakpoint)
7769 || (b->type == bp_jit_event)
7770 || (b->type == bp_hardware_breakpoint)
7771 || (is_tracepoint (b)))
7772 && loc->pspace == current_program_space
7773 && !loc->shlib_disabled
7774 && solib_name_from_address (loc->pspace, loc->address)
7775 )
7776 {
7777 loc->shlib_disabled = 1;
7778 }
7779 }
7780 }
7781
7782 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7783 notification of unloaded_shlib. Only apply to enabled breakpoints,
7784 disabled ones can just stay disabled. */
7785
7786 static void
7787 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7788 {
7789 struct bp_location *loc, **locp_tmp;
7790 int disabled_shlib_breaks = 0;
7791
7792 ALL_BP_LOCATIONS (loc, locp_tmp)
7793 {
7794 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7795 struct breakpoint *b = loc->owner;
7796
7797 if (solib->pspace == loc->pspace
7798 && !loc->shlib_disabled
7799 && (((b->type == bp_breakpoint
7800 || b->type == bp_jit_event
7801 || b->type == bp_hardware_breakpoint)
7802 && (loc->loc_type == bp_loc_hardware_breakpoint
7803 || loc->loc_type == bp_loc_software_breakpoint))
7804 || is_tracepoint (b))
7805 && solib_contains_address_p (solib, loc->address))
7806 {
7807 loc->shlib_disabled = 1;
7808 /* At this point, we cannot rely on remove_breakpoint
7809 succeeding so we must mark the breakpoint as not inserted
7810 to prevent future errors occurring in remove_breakpoints. */
7811 loc->inserted = 0;
7812
7813 /* This may cause duplicate notifications for the same breakpoint. */
7814 observer_notify_breakpoint_modified (b);
7815
7816 if (!disabled_shlib_breaks)
7817 {
7818 target_terminal::ours_for_output ();
7819 warning (_("Temporarily disabling breakpoints "
7820 "for unloaded shared library \"%s\""),
7821 solib->so_name);
7822 }
7823 disabled_shlib_breaks = 1;
7824 }
7825 }
7826 }
7827
7828 /* Disable any breakpoints and tracepoints in OBJFILE upon
7829 notification of free_objfile. Only apply to enabled breakpoints,
7830 disabled ones can just stay disabled. */
7831
7832 static void
7833 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7834 {
7835 struct breakpoint *b;
7836
7837 if (objfile == NULL)
7838 return;
7839
7840 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7841 managed by the user with add-symbol-file/remove-symbol-file.
7842 Similarly to how breakpoints in shared libraries are handled in
7843 response to "nosharedlibrary", mark breakpoints in such modules
7844 shlib_disabled so they end up uninserted on the next global
7845 location list update. Shared libraries not loaded by the user
7846 aren't handled here -- they're already handled in
7847 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7848 solib_unloaded observer. We skip objfiles that are not
7849 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7850 main objfile). */
7851 if ((objfile->flags & OBJF_SHARED) == 0
7852 || (objfile->flags & OBJF_USERLOADED) == 0)
7853 return;
7854
7855 ALL_BREAKPOINTS (b)
7856 {
7857 struct bp_location *loc;
7858 int bp_modified = 0;
7859
7860 if (!is_breakpoint (b) && !is_tracepoint (b))
7861 continue;
7862
7863 for (loc = b->loc; loc != NULL; loc = loc->next)
7864 {
7865 CORE_ADDR loc_addr = loc->address;
7866
7867 if (loc->loc_type != bp_loc_hardware_breakpoint
7868 && loc->loc_type != bp_loc_software_breakpoint)
7869 continue;
7870
7871 if (loc->shlib_disabled != 0)
7872 continue;
7873
7874 if (objfile->pspace != loc->pspace)
7875 continue;
7876
7877 if (loc->loc_type != bp_loc_hardware_breakpoint
7878 && loc->loc_type != bp_loc_software_breakpoint)
7879 continue;
7880
7881 if (is_addr_in_objfile (loc_addr, objfile))
7882 {
7883 loc->shlib_disabled = 1;
7884 /* At this point, we don't know whether the object was
7885 unmapped from the inferior or not, so leave the
7886 inserted flag alone. We'll handle failure to
7887 uninsert quietly, in case the object was indeed
7888 unmapped. */
7889
7890 mark_breakpoint_location_modified (loc);
7891
7892 bp_modified = 1;
7893 }
7894 }
7895
7896 if (bp_modified)
7897 observer_notify_breakpoint_modified (b);
7898 }
7899 }
7900
7901 /* FORK & VFORK catchpoints. */
7902
7903 /* An instance of this type is used to represent a fork or vfork
7904 catchpoint. A breakpoint is really of this type iff its ops pointer points
7905 to CATCH_FORK_BREAKPOINT_OPS. */
7906
7907 struct fork_catchpoint : public breakpoint
7908 {
7909 /* Process id of a child process whose forking triggered this
7910 catchpoint. This field is only valid immediately after this
7911 catchpoint has triggered. */
7912 ptid_t forked_inferior_pid;
7913 };
7914
7915 /* Implement the "insert" breakpoint_ops method for fork
7916 catchpoints. */
7917
7918 static int
7919 insert_catch_fork (struct bp_location *bl)
7920 {
7921 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7922 }
7923
7924 /* Implement the "remove" breakpoint_ops method for fork
7925 catchpoints. */
7926
7927 static int
7928 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7929 {
7930 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7931 }
7932
7933 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7934 catchpoints. */
7935
7936 static int
7937 breakpoint_hit_catch_fork (const struct bp_location *bl,
7938 struct address_space *aspace, CORE_ADDR bp_addr,
7939 const struct target_waitstatus *ws)
7940 {
7941 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7942
7943 if (ws->kind != TARGET_WAITKIND_FORKED)
7944 return 0;
7945
7946 c->forked_inferior_pid = ws->value.related_pid;
7947 return 1;
7948 }
7949
7950 /* Implement the "print_it" breakpoint_ops method for fork
7951 catchpoints. */
7952
7953 static enum print_stop_action
7954 print_it_catch_fork (bpstat bs)
7955 {
7956 struct ui_out *uiout = current_uiout;
7957 struct breakpoint *b = bs->breakpoint_at;
7958 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7959
7960 annotate_catchpoint (b->number);
7961 maybe_print_thread_hit_breakpoint (uiout);
7962 if (b->disposition == disp_del)
7963 uiout->text ("Temporary catchpoint ");
7964 else
7965 uiout->text ("Catchpoint ");
7966 if (uiout->is_mi_like_p ())
7967 {
7968 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7969 uiout->field_string ("disp", bpdisp_text (b->disposition));
7970 }
7971 uiout->field_int ("bkptno", b->number);
7972 uiout->text (" (forked process ");
7973 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7974 uiout->text ("), ");
7975 return PRINT_SRC_AND_LOC;
7976 }
7977
7978 /* Implement the "print_one" breakpoint_ops method for fork
7979 catchpoints. */
7980
7981 static void
7982 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7983 {
7984 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7985 struct value_print_options opts;
7986 struct ui_out *uiout = current_uiout;
7987
7988 get_user_print_options (&opts);
7989
7990 /* Field 4, the address, is omitted (which makes the columns not
7991 line up too nicely with the headers, but the effect is relatively
7992 readable). */
7993 if (opts.addressprint)
7994 uiout->field_skip ("addr");
7995 annotate_field (5);
7996 uiout->text ("fork");
7997 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7998 {
7999 uiout->text (", process ");
8000 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8001 uiout->spaces (1);
8002 }
8003
8004 if (uiout->is_mi_like_p ())
8005 uiout->field_string ("catch-type", "fork");
8006 }
8007
8008 /* Implement the "print_mention" breakpoint_ops method for fork
8009 catchpoints. */
8010
8011 static void
8012 print_mention_catch_fork (struct breakpoint *b)
8013 {
8014 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8015 }
8016
8017 /* Implement the "print_recreate" breakpoint_ops method for fork
8018 catchpoints. */
8019
8020 static void
8021 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8022 {
8023 fprintf_unfiltered (fp, "catch fork");
8024 print_recreate_thread (b, fp);
8025 }
8026
8027 /* The breakpoint_ops structure to be used in fork catchpoints. */
8028
8029 static struct breakpoint_ops catch_fork_breakpoint_ops;
8030
8031 /* Implement the "insert" breakpoint_ops method for vfork
8032 catchpoints. */
8033
8034 static int
8035 insert_catch_vfork (struct bp_location *bl)
8036 {
8037 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8038 }
8039
8040 /* Implement the "remove" breakpoint_ops method for vfork
8041 catchpoints. */
8042
8043 static int
8044 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8045 {
8046 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8047 }
8048
8049 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8050 catchpoints. */
8051
8052 static int
8053 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8054 struct address_space *aspace, CORE_ADDR bp_addr,
8055 const struct target_waitstatus *ws)
8056 {
8057 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8058
8059 if (ws->kind != TARGET_WAITKIND_VFORKED)
8060 return 0;
8061
8062 c->forked_inferior_pid = ws->value.related_pid;
8063 return 1;
8064 }
8065
8066 /* Implement the "print_it" breakpoint_ops method for vfork
8067 catchpoints. */
8068
8069 static enum print_stop_action
8070 print_it_catch_vfork (bpstat bs)
8071 {
8072 struct ui_out *uiout = current_uiout;
8073 struct breakpoint *b = bs->breakpoint_at;
8074 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8075
8076 annotate_catchpoint (b->number);
8077 maybe_print_thread_hit_breakpoint (uiout);
8078 if (b->disposition == disp_del)
8079 uiout->text ("Temporary catchpoint ");
8080 else
8081 uiout->text ("Catchpoint ");
8082 if (uiout->is_mi_like_p ())
8083 {
8084 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8085 uiout->field_string ("disp", bpdisp_text (b->disposition));
8086 }
8087 uiout->field_int ("bkptno", b->number);
8088 uiout->text (" (vforked process ");
8089 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8090 uiout->text ("), ");
8091 return PRINT_SRC_AND_LOC;
8092 }
8093
8094 /* Implement the "print_one" breakpoint_ops method for vfork
8095 catchpoints. */
8096
8097 static void
8098 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8099 {
8100 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8101 struct value_print_options opts;
8102 struct ui_out *uiout = current_uiout;
8103
8104 get_user_print_options (&opts);
8105 /* Field 4, the address, is omitted (which makes the columns not
8106 line up too nicely with the headers, but the effect is relatively
8107 readable). */
8108 if (opts.addressprint)
8109 uiout->field_skip ("addr");
8110 annotate_field (5);
8111 uiout->text ("vfork");
8112 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8113 {
8114 uiout->text (", process ");
8115 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8116 uiout->spaces (1);
8117 }
8118
8119 if (uiout->is_mi_like_p ())
8120 uiout->field_string ("catch-type", "vfork");
8121 }
8122
8123 /* Implement the "print_mention" breakpoint_ops method for vfork
8124 catchpoints. */
8125
8126 static void
8127 print_mention_catch_vfork (struct breakpoint *b)
8128 {
8129 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8130 }
8131
8132 /* Implement the "print_recreate" breakpoint_ops method for vfork
8133 catchpoints. */
8134
8135 static void
8136 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8137 {
8138 fprintf_unfiltered (fp, "catch vfork");
8139 print_recreate_thread (b, fp);
8140 }
8141
8142 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8143
8144 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8145
8146 /* An instance of this type is used to represent an solib catchpoint.
8147 A breakpoint is really of this type iff its ops pointer points to
8148 CATCH_SOLIB_BREAKPOINT_OPS. */
8149
8150 struct solib_catchpoint : public breakpoint
8151 {
8152 ~solib_catchpoint () override;
8153
8154 /* True for "catch load", false for "catch unload". */
8155 unsigned char is_load;
8156
8157 /* Regular expression to match, if any. COMPILED is only valid when
8158 REGEX is non-NULL. */
8159 char *regex;
8160 std::unique_ptr<compiled_regex> compiled;
8161 };
8162
8163 solib_catchpoint::~solib_catchpoint ()
8164 {
8165 xfree (this->regex);
8166 }
8167
8168 static int
8169 insert_catch_solib (struct bp_location *ignore)
8170 {
8171 return 0;
8172 }
8173
8174 static int
8175 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8176 {
8177 return 0;
8178 }
8179
8180 static int
8181 breakpoint_hit_catch_solib (const struct bp_location *bl,
8182 struct address_space *aspace,
8183 CORE_ADDR bp_addr,
8184 const struct target_waitstatus *ws)
8185 {
8186 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8187 struct breakpoint *other;
8188
8189 if (ws->kind == TARGET_WAITKIND_LOADED)
8190 return 1;
8191
8192 ALL_BREAKPOINTS (other)
8193 {
8194 struct bp_location *other_bl;
8195
8196 if (other == bl->owner)
8197 continue;
8198
8199 if (other->type != bp_shlib_event)
8200 continue;
8201
8202 if (self->pspace != NULL && other->pspace != self->pspace)
8203 continue;
8204
8205 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8206 {
8207 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8208 return 1;
8209 }
8210 }
8211
8212 return 0;
8213 }
8214
8215 static void
8216 check_status_catch_solib (struct bpstats *bs)
8217 {
8218 struct solib_catchpoint *self
8219 = (struct solib_catchpoint *) bs->breakpoint_at;
8220 int ix;
8221
8222 if (self->is_load)
8223 {
8224 struct so_list *iter;
8225
8226 for (ix = 0;
8227 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8228 ix, iter);
8229 ++ix)
8230 {
8231 if (!self->regex
8232 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8233 return;
8234 }
8235 }
8236 else
8237 {
8238 char *iter;
8239
8240 for (ix = 0;
8241 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8242 ix, iter);
8243 ++ix)
8244 {
8245 if (!self->regex
8246 || self->compiled->exec (iter, 0, NULL, 0) == 0)
8247 return;
8248 }
8249 }
8250
8251 bs->stop = 0;
8252 bs->print_it = print_it_noop;
8253 }
8254
8255 static enum print_stop_action
8256 print_it_catch_solib (bpstat bs)
8257 {
8258 struct breakpoint *b = bs->breakpoint_at;
8259 struct ui_out *uiout = current_uiout;
8260
8261 annotate_catchpoint (b->number);
8262 maybe_print_thread_hit_breakpoint (uiout);
8263 if (b->disposition == disp_del)
8264 uiout->text ("Temporary catchpoint ");
8265 else
8266 uiout->text ("Catchpoint ");
8267 uiout->field_int ("bkptno", b->number);
8268 uiout->text ("\n");
8269 if (uiout->is_mi_like_p ())
8270 uiout->field_string ("disp", bpdisp_text (b->disposition));
8271 print_solib_event (1);
8272 return PRINT_SRC_AND_LOC;
8273 }
8274
8275 static void
8276 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8277 {
8278 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8279 struct value_print_options opts;
8280 struct ui_out *uiout = current_uiout;
8281 char *msg;
8282
8283 get_user_print_options (&opts);
8284 /* Field 4, the address, is omitted (which makes the columns not
8285 line up too nicely with the headers, but the effect is relatively
8286 readable). */
8287 if (opts.addressprint)
8288 {
8289 annotate_field (4);
8290 uiout->field_skip ("addr");
8291 }
8292
8293 annotate_field (5);
8294 if (self->is_load)
8295 {
8296 if (self->regex)
8297 msg = xstrprintf (_("load of library matching %s"), self->regex);
8298 else
8299 msg = xstrdup (_("load of library"));
8300 }
8301 else
8302 {
8303 if (self->regex)
8304 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8305 else
8306 msg = xstrdup (_("unload of library"));
8307 }
8308 uiout->field_string ("what", msg);
8309 xfree (msg);
8310
8311 if (uiout->is_mi_like_p ())
8312 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8313 }
8314
8315 static void
8316 print_mention_catch_solib (struct breakpoint *b)
8317 {
8318 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8319
8320 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8321 self->is_load ? "load" : "unload");
8322 }
8323
8324 static void
8325 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8326 {
8327 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8328
8329 fprintf_unfiltered (fp, "%s %s",
8330 b->disposition == disp_del ? "tcatch" : "catch",
8331 self->is_load ? "load" : "unload");
8332 if (self->regex)
8333 fprintf_unfiltered (fp, " %s", self->regex);
8334 fprintf_unfiltered (fp, "\n");
8335 }
8336
8337 static struct breakpoint_ops catch_solib_breakpoint_ops;
8338
8339 /* Shared helper function (MI and CLI) for creating and installing
8340 a shared object event catchpoint. If IS_LOAD is non-zero then
8341 the events to be caught are load events, otherwise they are
8342 unload events. If IS_TEMP is non-zero the catchpoint is a
8343 temporary one. If ENABLED is non-zero the catchpoint is
8344 created in an enabled state. */
8345
8346 void
8347 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8348 {
8349 struct gdbarch *gdbarch = get_current_arch ();
8350
8351 if (!arg)
8352 arg = "";
8353 arg = skip_spaces (arg);
8354
8355 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8356
8357 if (*arg != '\0')
8358 {
8359 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8360 _("Invalid regexp")));
8361 c->regex = xstrdup (arg);
8362 }
8363
8364 c->is_load = is_load;
8365 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8366 &catch_solib_breakpoint_ops);
8367
8368 c->enable_state = enabled ? bp_enabled : bp_disabled;
8369
8370 install_breakpoint (0, std::move (c), 1);
8371 }
8372
8373 /* A helper function that does all the work for "catch load" and
8374 "catch unload". */
8375
8376 static void
8377 catch_load_or_unload (char *arg, int from_tty, int is_load,
8378 struct cmd_list_element *command)
8379 {
8380 int tempflag;
8381 const int enabled = 1;
8382
8383 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8384
8385 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8386 }
8387
8388 static void
8389 catch_load_command_1 (char *arg, int from_tty,
8390 struct cmd_list_element *command)
8391 {
8392 catch_load_or_unload (arg, from_tty, 1, command);
8393 }
8394
8395 static void
8396 catch_unload_command_1 (char *arg, int from_tty,
8397 struct cmd_list_element *command)
8398 {
8399 catch_load_or_unload (arg, from_tty, 0, command);
8400 }
8401
8402 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8403 is non-zero, then make the breakpoint temporary. If COND_STRING is
8404 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8405 the breakpoint_ops structure associated to the catchpoint. */
8406
8407 void
8408 init_catchpoint (struct breakpoint *b,
8409 struct gdbarch *gdbarch, int tempflag,
8410 const char *cond_string,
8411 const struct breakpoint_ops *ops)
8412 {
8413 symtab_and_line sal;
8414 sal.pspace = current_program_space;
8415
8416 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8417
8418 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8419 b->disposition = tempflag ? disp_del : disp_donttouch;
8420 }
8421
8422 void
8423 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8424 {
8425 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8426 set_breakpoint_number (internal, b);
8427 if (is_tracepoint (b))
8428 set_tracepoint_count (breakpoint_count);
8429 if (!internal)
8430 mention (b);
8431 observer_notify_breakpoint_created (b);
8432
8433 if (update_gll)
8434 update_global_location_list (UGLL_MAY_INSERT);
8435 }
8436
8437 static void
8438 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8439 int tempflag, const char *cond_string,
8440 const struct breakpoint_ops *ops)
8441 {
8442 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8443
8444 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8445
8446 c->forked_inferior_pid = null_ptid;
8447
8448 install_breakpoint (0, std::move (c), 1);
8449 }
8450
8451 /* Exec catchpoints. */
8452
8453 /* An instance of this type is used to represent an exec catchpoint.
8454 A breakpoint is really of this type iff its ops pointer points to
8455 CATCH_EXEC_BREAKPOINT_OPS. */
8456
8457 struct exec_catchpoint : public breakpoint
8458 {
8459 ~exec_catchpoint () override;
8460
8461 /* Filename of a program whose exec triggered this catchpoint.
8462 This field is only valid immediately after this catchpoint has
8463 triggered. */
8464 char *exec_pathname;
8465 };
8466
8467 /* Exec catchpoint destructor. */
8468
8469 exec_catchpoint::~exec_catchpoint ()
8470 {
8471 xfree (this->exec_pathname);
8472 }
8473
8474 static int
8475 insert_catch_exec (struct bp_location *bl)
8476 {
8477 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8478 }
8479
8480 static int
8481 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8482 {
8483 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8484 }
8485
8486 static int
8487 breakpoint_hit_catch_exec (const struct bp_location *bl,
8488 struct address_space *aspace, CORE_ADDR bp_addr,
8489 const struct target_waitstatus *ws)
8490 {
8491 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8492
8493 if (ws->kind != TARGET_WAITKIND_EXECD)
8494 return 0;
8495
8496 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8497 return 1;
8498 }
8499
8500 static enum print_stop_action
8501 print_it_catch_exec (bpstat bs)
8502 {
8503 struct ui_out *uiout = current_uiout;
8504 struct breakpoint *b = bs->breakpoint_at;
8505 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8506
8507 annotate_catchpoint (b->number);
8508 maybe_print_thread_hit_breakpoint (uiout);
8509 if (b->disposition == disp_del)
8510 uiout->text ("Temporary catchpoint ");
8511 else
8512 uiout->text ("Catchpoint ");
8513 if (uiout->is_mi_like_p ())
8514 {
8515 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8516 uiout->field_string ("disp", bpdisp_text (b->disposition));
8517 }
8518 uiout->field_int ("bkptno", b->number);
8519 uiout->text (" (exec'd ");
8520 uiout->field_string ("new-exec", c->exec_pathname);
8521 uiout->text ("), ");
8522
8523 return PRINT_SRC_AND_LOC;
8524 }
8525
8526 static void
8527 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8528 {
8529 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8530 struct value_print_options opts;
8531 struct ui_out *uiout = current_uiout;
8532
8533 get_user_print_options (&opts);
8534
8535 /* Field 4, the address, is omitted (which makes the columns
8536 not line up too nicely with the headers, but the effect
8537 is relatively readable). */
8538 if (opts.addressprint)
8539 uiout->field_skip ("addr");
8540 annotate_field (5);
8541 uiout->text ("exec");
8542 if (c->exec_pathname != NULL)
8543 {
8544 uiout->text (", program \"");
8545 uiout->field_string ("what", c->exec_pathname);
8546 uiout->text ("\" ");
8547 }
8548
8549 if (uiout->is_mi_like_p ())
8550 uiout->field_string ("catch-type", "exec");
8551 }
8552
8553 static void
8554 print_mention_catch_exec (struct breakpoint *b)
8555 {
8556 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8557 }
8558
8559 /* Implement the "print_recreate" breakpoint_ops method for exec
8560 catchpoints. */
8561
8562 static void
8563 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8564 {
8565 fprintf_unfiltered (fp, "catch exec");
8566 print_recreate_thread (b, fp);
8567 }
8568
8569 static struct breakpoint_ops catch_exec_breakpoint_ops;
8570
8571 static int
8572 hw_breakpoint_used_count (void)
8573 {
8574 int i = 0;
8575 struct breakpoint *b;
8576 struct bp_location *bl;
8577
8578 ALL_BREAKPOINTS (b)
8579 {
8580 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8581 for (bl = b->loc; bl; bl = bl->next)
8582 {
8583 /* Special types of hardware breakpoints may use more than
8584 one register. */
8585 i += b->ops->resources_needed (bl);
8586 }
8587 }
8588
8589 return i;
8590 }
8591
8592 /* Returns the resources B would use if it were a hardware
8593 watchpoint. */
8594
8595 static int
8596 hw_watchpoint_use_count (struct breakpoint *b)
8597 {
8598 int i = 0;
8599 struct bp_location *bl;
8600
8601 if (!breakpoint_enabled (b))
8602 return 0;
8603
8604 for (bl = b->loc; bl; bl = bl->next)
8605 {
8606 /* Special types of hardware watchpoints may use more than
8607 one register. */
8608 i += b->ops->resources_needed (bl);
8609 }
8610
8611 return i;
8612 }
8613
8614 /* Returns the sum the used resources of all hardware watchpoints of
8615 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8616 the sum of the used resources of all hardware watchpoints of other
8617 types _not_ TYPE. */
8618
8619 static int
8620 hw_watchpoint_used_count_others (struct breakpoint *except,
8621 enum bptype type, int *other_type_used)
8622 {
8623 int i = 0;
8624 struct breakpoint *b;
8625
8626 *other_type_used = 0;
8627 ALL_BREAKPOINTS (b)
8628 {
8629 if (b == except)
8630 continue;
8631 if (!breakpoint_enabled (b))
8632 continue;
8633
8634 if (b->type == type)
8635 i += hw_watchpoint_use_count (b);
8636 else if (is_hardware_watchpoint (b))
8637 *other_type_used = 1;
8638 }
8639
8640 return i;
8641 }
8642
8643 void
8644 disable_watchpoints_before_interactive_call_start (void)
8645 {
8646 struct breakpoint *b;
8647
8648 ALL_BREAKPOINTS (b)
8649 {
8650 if (is_watchpoint (b) && breakpoint_enabled (b))
8651 {
8652 b->enable_state = bp_call_disabled;
8653 update_global_location_list (UGLL_DONT_INSERT);
8654 }
8655 }
8656 }
8657
8658 void
8659 enable_watchpoints_after_interactive_call_stop (void)
8660 {
8661 struct breakpoint *b;
8662
8663 ALL_BREAKPOINTS (b)
8664 {
8665 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8666 {
8667 b->enable_state = bp_enabled;
8668 update_global_location_list (UGLL_MAY_INSERT);
8669 }
8670 }
8671 }
8672
8673 void
8674 disable_breakpoints_before_startup (void)
8675 {
8676 current_program_space->executing_startup = 1;
8677 update_global_location_list (UGLL_DONT_INSERT);
8678 }
8679
8680 void
8681 enable_breakpoints_after_startup (void)
8682 {
8683 current_program_space->executing_startup = 0;
8684 breakpoint_re_set ();
8685 }
8686
8687 /* Create a new single-step breakpoint for thread THREAD, with no
8688 locations. */
8689
8690 static struct breakpoint *
8691 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8692 {
8693 std::unique_ptr<breakpoint> b (new breakpoint ());
8694
8695 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8696 &momentary_breakpoint_ops);
8697
8698 b->disposition = disp_donttouch;
8699 b->frame_id = null_frame_id;
8700
8701 b->thread = thread;
8702 gdb_assert (b->thread != 0);
8703
8704 return add_to_breakpoint_chain (std::move (b));
8705 }
8706
8707 /* Set a momentary breakpoint of type TYPE at address specified by
8708 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8709 frame. */
8710
8711 struct breakpoint *
8712 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8713 struct frame_id frame_id, enum bptype type)
8714 {
8715 struct breakpoint *b;
8716
8717 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8718 tail-called one. */
8719 gdb_assert (!frame_id_artificial_p (frame_id));
8720
8721 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8722 b->enable_state = bp_enabled;
8723 b->disposition = disp_donttouch;
8724 b->frame_id = frame_id;
8725
8726 /* If we're debugging a multi-threaded program, then we want
8727 momentary breakpoints to be active in only a single thread of
8728 control. */
8729 if (in_thread_list (inferior_ptid))
8730 b->thread = ptid_to_global_thread_id (inferior_ptid);
8731
8732 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8733
8734 return b;
8735 }
8736
8737 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8738 The new breakpoint will have type TYPE, use OPS as its
8739 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8740
8741 static struct breakpoint *
8742 momentary_breakpoint_from_master (struct breakpoint *orig,
8743 enum bptype type,
8744 const struct breakpoint_ops *ops,
8745 int loc_enabled)
8746 {
8747 struct breakpoint *copy;
8748
8749 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8750 copy->loc = allocate_bp_location (copy);
8751 set_breakpoint_location_function (copy->loc, 1);
8752
8753 copy->loc->gdbarch = orig->loc->gdbarch;
8754 copy->loc->requested_address = orig->loc->requested_address;
8755 copy->loc->address = orig->loc->address;
8756 copy->loc->section = orig->loc->section;
8757 copy->loc->pspace = orig->loc->pspace;
8758 copy->loc->probe = orig->loc->probe;
8759 copy->loc->line_number = orig->loc->line_number;
8760 copy->loc->symtab = orig->loc->symtab;
8761 copy->loc->enabled = loc_enabled;
8762 copy->frame_id = orig->frame_id;
8763 copy->thread = orig->thread;
8764 copy->pspace = orig->pspace;
8765
8766 copy->enable_state = bp_enabled;
8767 copy->disposition = disp_donttouch;
8768 copy->number = internal_breakpoint_number--;
8769
8770 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8771 return copy;
8772 }
8773
8774 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8775 ORIG is NULL. */
8776
8777 struct breakpoint *
8778 clone_momentary_breakpoint (struct breakpoint *orig)
8779 {
8780 /* If there's nothing to clone, then return nothing. */
8781 if (orig == NULL)
8782 return NULL;
8783
8784 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8785 }
8786
8787 struct breakpoint *
8788 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8789 enum bptype type)
8790 {
8791 struct symtab_and_line sal;
8792
8793 sal = find_pc_line (pc, 0);
8794 sal.pc = pc;
8795 sal.section = find_pc_overlay (pc);
8796 sal.explicit_pc = 1;
8797
8798 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8799 }
8800 \f
8801
8802 /* Tell the user we have just set a breakpoint B. */
8803
8804 static void
8805 mention (struct breakpoint *b)
8806 {
8807 b->ops->print_mention (b);
8808 if (current_uiout->is_mi_like_p ())
8809 return;
8810 printf_filtered ("\n");
8811 }
8812 \f
8813
8814 static int bp_loc_is_permanent (struct bp_location *loc);
8815
8816 static struct bp_location *
8817 add_location_to_breakpoint (struct breakpoint *b,
8818 const struct symtab_and_line *sal)
8819 {
8820 struct bp_location *loc, **tmp;
8821 CORE_ADDR adjusted_address;
8822 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8823
8824 if (loc_gdbarch == NULL)
8825 loc_gdbarch = b->gdbarch;
8826
8827 /* Adjust the breakpoint's address prior to allocating a location.
8828 Once we call allocate_bp_location(), that mostly uninitialized
8829 location will be placed on the location chain. Adjustment of the
8830 breakpoint may cause target_read_memory() to be called and we do
8831 not want its scan of the location chain to find a breakpoint and
8832 location that's only been partially initialized. */
8833 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8834 sal->pc, b->type);
8835
8836 /* Sort the locations by their ADDRESS. */
8837 loc = allocate_bp_location (b);
8838 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8839 tmp = &((*tmp)->next))
8840 ;
8841 loc->next = *tmp;
8842 *tmp = loc;
8843
8844 loc->requested_address = sal->pc;
8845 loc->address = adjusted_address;
8846 loc->pspace = sal->pspace;
8847 loc->probe.probe = sal->probe;
8848 loc->probe.objfile = sal->objfile;
8849 gdb_assert (loc->pspace != NULL);
8850 loc->section = sal->section;
8851 loc->gdbarch = loc_gdbarch;
8852 loc->line_number = sal->line;
8853 loc->symtab = sal->symtab;
8854
8855 set_breakpoint_location_function (loc,
8856 sal->explicit_pc || sal->explicit_line);
8857
8858 /* While by definition, permanent breakpoints are already present in the
8859 code, we don't mark the location as inserted. Normally one would expect
8860 that GDB could rely on that breakpoint instruction to stop the program,
8861 thus removing the need to insert its own breakpoint, except that executing
8862 the breakpoint instruction can kill the target instead of reporting a
8863 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8864 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8865 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8866 breakpoint be inserted normally results in QEMU knowing about the GDB
8867 breakpoint, and thus trap before the breakpoint instruction is executed.
8868 (If GDB later needs to continue execution past the permanent breakpoint,
8869 it manually increments the PC, thus avoiding executing the breakpoint
8870 instruction.) */
8871 if (bp_loc_is_permanent (loc))
8872 loc->permanent = 1;
8873
8874 return loc;
8875 }
8876 \f
8877
8878 /* See breakpoint.h. */
8879
8880 int
8881 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8882 {
8883 int len;
8884 CORE_ADDR addr;
8885 const gdb_byte *bpoint;
8886 gdb_byte *target_mem;
8887
8888 addr = address;
8889 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8890
8891 /* Software breakpoints unsupported? */
8892 if (bpoint == NULL)
8893 return 0;
8894
8895 target_mem = (gdb_byte *) alloca (len);
8896
8897 /* Enable the automatic memory restoration from breakpoints while
8898 we read the memory. Otherwise we could say about our temporary
8899 breakpoints they are permanent. */
8900 scoped_restore restore_memory
8901 = make_scoped_restore_show_memory_breakpoints (0);
8902
8903 if (target_read_memory (address, target_mem, len) == 0
8904 && memcmp (target_mem, bpoint, len) == 0)
8905 return 1;
8906
8907 return 0;
8908 }
8909
8910 /* Return 1 if LOC is pointing to a permanent breakpoint,
8911 return 0 otherwise. */
8912
8913 static int
8914 bp_loc_is_permanent (struct bp_location *loc)
8915 {
8916 gdb_assert (loc != NULL);
8917
8918 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8919 attempt to read from the addresses the locations of these breakpoint types
8920 point to. program_breakpoint_here_p, below, will attempt to read
8921 memory. */
8922 if (!breakpoint_address_is_meaningful (loc->owner))
8923 return 0;
8924
8925 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8926 switch_to_program_space_and_thread (loc->pspace);
8927 return program_breakpoint_here_p (loc->gdbarch, loc->address);
8928 }
8929
8930 /* Build a command list for the dprintf corresponding to the current
8931 settings of the dprintf style options. */
8932
8933 static void
8934 update_dprintf_command_list (struct breakpoint *b)
8935 {
8936 char *dprintf_args = b->extra_string;
8937 char *printf_line = NULL;
8938
8939 if (!dprintf_args)
8940 return;
8941
8942 dprintf_args = skip_spaces (dprintf_args);
8943
8944 /* Allow a comma, as it may have terminated a location, but don't
8945 insist on it. */
8946 if (*dprintf_args == ',')
8947 ++dprintf_args;
8948 dprintf_args = skip_spaces (dprintf_args);
8949
8950 if (*dprintf_args != '"')
8951 error (_("Bad format string, missing '\"'."));
8952
8953 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8954 printf_line = xstrprintf ("printf %s", dprintf_args);
8955 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8956 {
8957 if (!dprintf_function)
8958 error (_("No function supplied for dprintf call"));
8959
8960 if (dprintf_channel && strlen (dprintf_channel) > 0)
8961 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8962 dprintf_function,
8963 dprintf_channel,
8964 dprintf_args);
8965 else
8966 printf_line = xstrprintf ("call (void) %s (%s)",
8967 dprintf_function,
8968 dprintf_args);
8969 }
8970 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8971 {
8972 if (target_can_run_breakpoint_commands ())
8973 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8974 else
8975 {
8976 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8977 printf_line = xstrprintf ("printf %s", dprintf_args);
8978 }
8979 }
8980 else
8981 internal_error (__FILE__, __LINE__,
8982 _("Invalid dprintf style."));
8983
8984 gdb_assert (printf_line != NULL);
8985 /* Manufacture a printf sequence. */
8986 {
8987 struct command_line *printf_cmd_line = XNEW (struct command_line);
8988
8989 printf_cmd_line->control_type = simple_control;
8990 printf_cmd_line->body_count = 0;
8991 printf_cmd_line->body_list = NULL;
8992 printf_cmd_line->next = NULL;
8993 printf_cmd_line->line = printf_line;
8994
8995 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
8996 }
8997 }
8998
8999 /* Update all dprintf commands, making their command lists reflect
9000 current style settings. */
9001
9002 static void
9003 update_dprintf_commands (char *args, int from_tty,
9004 struct cmd_list_element *c)
9005 {
9006 struct breakpoint *b;
9007
9008 ALL_BREAKPOINTS (b)
9009 {
9010 if (b->type == bp_dprintf)
9011 update_dprintf_command_list (b);
9012 }
9013 }
9014
9015 /* Create a breakpoint with SAL as location. Use LOCATION
9016 as a description of the location, and COND_STRING
9017 as condition expression. If LOCATION is NULL then create an
9018 "address location" from the address in the SAL. */
9019
9020 static void
9021 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9022 gdb::array_view<const symtab_and_line> sals,
9023 event_location_up &&location,
9024 gdb::unique_xmalloc_ptr<char> filter,
9025 gdb::unique_xmalloc_ptr<char> cond_string,
9026 gdb::unique_xmalloc_ptr<char> extra_string,
9027 enum bptype type, enum bpdisp disposition,
9028 int thread, int task, int ignore_count,
9029 const struct breakpoint_ops *ops, int from_tty,
9030 int enabled, int internal, unsigned flags,
9031 int display_canonical)
9032 {
9033 int i;
9034
9035 if (type == bp_hardware_breakpoint)
9036 {
9037 int target_resources_ok;
9038
9039 i = hw_breakpoint_used_count ();
9040 target_resources_ok =
9041 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9042 i + 1, 0);
9043 if (target_resources_ok == 0)
9044 error (_("No hardware breakpoint support in the target."));
9045 else if (target_resources_ok < 0)
9046 error (_("Hardware breakpoints used exceeds limit."));
9047 }
9048
9049 gdb_assert (!sals.empty ());
9050
9051 for (const auto &sal : sals)
9052 {
9053 struct bp_location *loc;
9054
9055 if (from_tty)
9056 {
9057 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9058 if (!loc_gdbarch)
9059 loc_gdbarch = gdbarch;
9060
9061 describe_other_breakpoints (loc_gdbarch,
9062 sal.pspace, sal.pc, sal.section, thread);
9063 }
9064
9065 if (&sal == &sals[0])
9066 {
9067 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9068 b->thread = thread;
9069 b->task = task;
9070
9071 b->cond_string = cond_string.release ();
9072 b->extra_string = extra_string.release ();
9073 b->ignore_count = ignore_count;
9074 b->enable_state = enabled ? bp_enabled : bp_disabled;
9075 b->disposition = disposition;
9076
9077 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9078 b->loc->inserted = 1;
9079
9080 if (type == bp_static_tracepoint)
9081 {
9082 struct tracepoint *t = (struct tracepoint *) b;
9083 struct static_tracepoint_marker marker;
9084
9085 if (strace_marker_p (b))
9086 {
9087 /* We already know the marker exists, otherwise, we
9088 wouldn't see a sal for it. */
9089 const char *p
9090 = &event_location_to_string (b->location.get ())[3];
9091 const char *endp;
9092 char *marker_str;
9093
9094 p = skip_spaces (p);
9095
9096 endp = skip_to_space (p);
9097
9098 marker_str = savestring (p, endp - p);
9099 t->static_trace_marker_id = marker_str;
9100
9101 printf_filtered (_("Probed static tracepoint "
9102 "marker \"%s\"\n"),
9103 t->static_trace_marker_id);
9104 }
9105 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9106 {
9107 t->static_trace_marker_id = xstrdup (marker.str_id);
9108 release_static_tracepoint_marker (&marker);
9109
9110 printf_filtered (_("Probed static tracepoint "
9111 "marker \"%s\"\n"),
9112 t->static_trace_marker_id);
9113 }
9114 else
9115 warning (_("Couldn't determine the static "
9116 "tracepoint marker to probe"));
9117 }
9118
9119 loc = b->loc;
9120 }
9121 else
9122 {
9123 loc = add_location_to_breakpoint (b, &sal);
9124 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9125 loc->inserted = 1;
9126 }
9127
9128 if (b->cond_string)
9129 {
9130 const char *arg = b->cond_string;
9131
9132 loc->cond = parse_exp_1 (&arg, loc->address,
9133 block_for_pc (loc->address), 0);
9134 if (*arg)
9135 error (_("Garbage '%s' follows condition"), arg);
9136 }
9137
9138 /* Dynamic printf requires and uses additional arguments on the
9139 command line, otherwise it's an error. */
9140 if (type == bp_dprintf)
9141 {
9142 if (b->extra_string)
9143 update_dprintf_command_list (b);
9144 else
9145 error (_("Format string required"));
9146 }
9147 else if (b->extra_string)
9148 error (_("Garbage '%s' at end of command"), b->extra_string);
9149 }
9150
9151 b->display_canonical = display_canonical;
9152 if (location != NULL)
9153 b->location = std::move (location);
9154 else
9155 b->location = new_address_location (b->loc->address, NULL, 0);
9156 b->filter = filter.release ();
9157 }
9158
9159 static void
9160 create_breakpoint_sal (struct gdbarch *gdbarch,
9161 gdb::array_view<const symtab_and_line> sals,
9162 event_location_up &&location,
9163 gdb::unique_xmalloc_ptr<char> filter,
9164 gdb::unique_xmalloc_ptr<char> cond_string,
9165 gdb::unique_xmalloc_ptr<char> extra_string,
9166 enum bptype type, enum bpdisp disposition,
9167 int thread, int task, int ignore_count,
9168 const struct breakpoint_ops *ops, int from_tty,
9169 int enabled, int internal, unsigned flags,
9170 int display_canonical)
9171 {
9172 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9173
9174 init_breakpoint_sal (b.get (), gdbarch,
9175 sals, std::move (location),
9176 std::move (filter),
9177 std::move (cond_string),
9178 std::move (extra_string),
9179 type, disposition,
9180 thread, task, ignore_count,
9181 ops, from_tty,
9182 enabled, internal, flags,
9183 display_canonical);
9184
9185 install_breakpoint (internal, std::move (b), 0);
9186 }
9187
9188 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9189 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9190 value. COND_STRING, if not NULL, specified the condition to be
9191 used for all breakpoints. Essentially the only case where
9192 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9193 function. In that case, it's still not possible to specify
9194 separate conditions for different overloaded functions, so
9195 we take just a single condition string.
9196
9197 NOTE: If the function succeeds, the caller is expected to cleanup
9198 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9199 array contents). If the function fails (error() is called), the
9200 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9201 COND and SALS arrays and each of those arrays contents. */
9202
9203 static void
9204 create_breakpoints_sal (struct gdbarch *gdbarch,
9205 struct linespec_result *canonical,
9206 gdb::unique_xmalloc_ptr<char> cond_string,
9207 gdb::unique_xmalloc_ptr<char> extra_string,
9208 enum bptype type, enum bpdisp disposition,
9209 int thread, int task, int ignore_count,
9210 const struct breakpoint_ops *ops, int from_tty,
9211 int enabled, int internal, unsigned flags)
9212 {
9213 if (canonical->pre_expanded)
9214 gdb_assert (canonical->lsals.size () == 1);
9215
9216 for (const auto &lsal : canonical->lsals)
9217 {
9218 /* Note that 'location' can be NULL in the case of a plain
9219 'break', without arguments. */
9220 event_location_up location
9221 = (canonical->location != NULL
9222 ? copy_event_location (canonical->location.get ()) : NULL);
9223 gdb::unique_xmalloc_ptr<char> filter_string
9224 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9225
9226 create_breakpoint_sal (gdbarch, lsal.sals,
9227 std::move (location),
9228 std::move (filter_string),
9229 std::move (cond_string),
9230 std::move (extra_string),
9231 type, disposition,
9232 thread, task, ignore_count, ops,
9233 from_tty, enabled, internal, flags,
9234 canonical->special_display);
9235 }
9236 }
9237
9238 /* Parse LOCATION which is assumed to be a SAL specification possibly
9239 followed by conditionals. On return, SALS contains an array of SAL
9240 addresses found. LOCATION points to the end of the SAL (for
9241 linespec locations).
9242
9243 The array and the line spec strings are allocated on the heap, it is
9244 the caller's responsibility to free them. */
9245
9246 static void
9247 parse_breakpoint_sals (const struct event_location *location,
9248 struct linespec_result *canonical)
9249 {
9250 struct symtab_and_line cursal;
9251
9252 if (event_location_type (location) == LINESPEC_LOCATION)
9253 {
9254 const char *address = get_linespec_location (location);
9255
9256 if (address == NULL)
9257 {
9258 /* The last displayed codepoint, if it's valid, is our default
9259 breakpoint address. */
9260 if (last_displayed_sal_is_valid ())
9261 {
9262 /* Set sal's pspace, pc, symtab, and line to the values
9263 corresponding to the last call to print_frame_info.
9264 Be sure to reinitialize LINE with NOTCURRENT == 0
9265 as the breakpoint line number is inappropriate otherwise.
9266 find_pc_line would adjust PC, re-set it back. */
9267 symtab_and_line sal = get_last_displayed_sal ();
9268 CORE_ADDR pc = sal.pc;
9269
9270 sal = find_pc_line (pc, 0);
9271
9272 /* "break" without arguments is equivalent to "break *PC"
9273 where PC is the last displayed codepoint's address. So
9274 make sure to set sal.explicit_pc to prevent GDB from
9275 trying to expand the list of sals to include all other
9276 instances with the same symtab and line. */
9277 sal.pc = pc;
9278 sal.explicit_pc = 1;
9279
9280 struct linespec_sals lsal;
9281 lsal.sals = {sal};
9282 lsal.canonical = NULL;
9283
9284 canonical->lsals.push_back (std::move (lsal));
9285 return;
9286 }
9287 else
9288 error (_("No default breakpoint address now."));
9289 }
9290 }
9291
9292 /* Force almost all breakpoints to be in terms of the
9293 current_source_symtab (which is decode_line_1's default).
9294 This should produce the results we want almost all of the
9295 time while leaving default_breakpoint_* alone.
9296
9297 ObjC: However, don't match an Objective-C method name which
9298 may have a '+' or '-' succeeded by a '['. */
9299 cursal = get_current_source_symtab_and_line ();
9300 if (last_displayed_sal_is_valid ())
9301 {
9302 const char *address = NULL;
9303
9304 if (event_location_type (location) == LINESPEC_LOCATION)
9305 address = get_linespec_location (location);
9306
9307 if (!cursal.symtab
9308 || (address != NULL
9309 && strchr ("+-", address[0]) != NULL
9310 && address[1] != '['))
9311 {
9312 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9313 get_last_displayed_symtab (),
9314 get_last_displayed_line (),
9315 canonical, NULL, NULL);
9316 return;
9317 }
9318 }
9319
9320 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9321 cursal.symtab, cursal.line, canonical, NULL, NULL);
9322 }
9323
9324
9325 /* Convert each SAL into a real PC. Verify that the PC can be
9326 inserted as a breakpoint. If it can't throw an error. */
9327
9328 static void
9329 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9330 {
9331 for (auto &sal : sals)
9332 resolve_sal_pc (&sal);
9333 }
9334
9335 /* Fast tracepoints may have restrictions on valid locations. For
9336 instance, a fast tracepoint using a jump instead of a trap will
9337 likely have to overwrite more bytes than a trap would, and so can
9338 only be placed where the instruction is longer than the jump, or a
9339 multi-instruction sequence does not have a jump into the middle of
9340 it, etc. */
9341
9342 static void
9343 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9344 gdb::array_view<const symtab_and_line> sals)
9345 {
9346 int rslt;
9347 char *msg;
9348 struct cleanup *old_chain;
9349
9350 for (const auto &sal : sals)
9351 {
9352 struct gdbarch *sarch;
9353
9354 sarch = get_sal_arch (sal);
9355 /* We fall back to GDBARCH if there is no architecture
9356 associated with SAL. */
9357 if (sarch == NULL)
9358 sarch = gdbarch;
9359 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
9360 old_chain = make_cleanup (xfree, msg);
9361
9362 if (!rslt)
9363 error (_("May not have a fast tracepoint at %s%s"),
9364 paddress (sarch, sal.pc), (msg ? msg : ""));
9365
9366 do_cleanups (old_chain);
9367 }
9368 }
9369
9370 /* Given TOK, a string specification of condition and thread, as
9371 accepted by the 'break' command, extract the condition
9372 string and thread number and set *COND_STRING and *THREAD.
9373 PC identifies the context at which the condition should be parsed.
9374 If no condition is found, *COND_STRING is set to NULL.
9375 If no thread is found, *THREAD is set to -1. */
9376
9377 static void
9378 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9379 char **cond_string, int *thread, int *task,
9380 char **rest)
9381 {
9382 *cond_string = NULL;
9383 *thread = -1;
9384 *task = 0;
9385 *rest = NULL;
9386
9387 while (tok && *tok)
9388 {
9389 const char *end_tok;
9390 int toklen;
9391 const char *cond_start = NULL;
9392 const char *cond_end = NULL;
9393
9394 tok = skip_spaces (tok);
9395
9396 if ((*tok == '"' || *tok == ',') && rest)
9397 {
9398 *rest = savestring (tok, strlen (tok));
9399 return;
9400 }
9401
9402 end_tok = skip_to_space (tok);
9403
9404 toklen = end_tok - tok;
9405
9406 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9407 {
9408 tok = cond_start = end_tok + 1;
9409 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9410 cond_end = tok;
9411 *cond_string = savestring (cond_start, cond_end - cond_start);
9412 }
9413 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9414 {
9415 const char *tmptok;
9416 struct thread_info *thr;
9417
9418 tok = end_tok + 1;
9419 thr = parse_thread_id (tok, &tmptok);
9420 if (tok == tmptok)
9421 error (_("Junk after thread keyword."));
9422 *thread = thr->global_num;
9423 tok = tmptok;
9424 }
9425 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9426 {
9427 char *tmptok;
9428
9429 tok = end_tok + 1;
9430 *task = strtol (tok, &tmptok, 0);
9431 if (tok == tmptok)
9432 error (_("Junk after task keyword."));
9433 if (!valid_task_id (*task))
9434 error (_("Unknown task %d."), *task);
9435 tok = tmptok;
9436 }
9437 else if (rest)
9438 {
9439 *rest = savestring (tok, strlen (tok));
9440 return;
9441 }
9442 else
9443 error (_("Junk at end of arguments."));
9444 }
9445 }
9446
9447 /* Decode a static tracepoint marker spec. */
9448
9449 static std::vector<symtab_and_line>
9450 decode_static_tracepoint_spec (const char **arg_p)
9451 {
9452 VEC(static_tracepoint_marker_p) *markers = NULL;
9453 struct cleanup *old_chain;
9454 const char *p = &(*arg_p)[3];
9455 const char *endp;
9456 char *marker_str;
9457 int i;
9458
9459 p = skip_spaces (p);
9460
9461 endp = skip_to_space (p);
9462
9463 marker_str = savestring (p, endp - p);
9464 old_chain = make_cleanup (xfree, marker_str);
9465
9466 markers = target_static_tracepoint_markers_by_strid (marker_str);
9467 if (VEC_empty(static_tracepoint_marker_p, markers))
9468 error (_("No known static tracepoint marker named %s"), marker_str);
9469
9470 std::vector<symtab_and_line> sals;
9471 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
9472
9473 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
9474 {
9475 struct static_tracepoint_marker *marker;
9476
9477 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9478
9479 symtab_and_line sal = find_pc_line (marker->address, 0);
9480 sal.pc = marker->address;
9481 sals.push_back (sal);
9482
9483 release_static_tracepoint_marker (marker);
9484 }
9485
9486 do_cleanups (old_chain);
9487
9488 *arg_p = endp;
9489 return sals;
9490 }
9491
9492 /* See breakpoint.h. */
9493
9494 int
9495 create_breakpoint (struct gdbarch *gdbarch,
9496 const struct event_location *location,
9497 const char *cond_string,
9498 int thread, const char *extra_string,
9499 int parse_extra,
9500 int tempflag, enum bptype type_wanted,
9501 int ignore_count,
9502 enum auto_boolean pending_break_support,
9503 const struct breakpoint_ops *ops,
9504 int from_tty, int enabled, int internal,
9505 unsigned flags)
9506 {
9507 struct linespec_result canonical;
9508 struct cleanup *bkpt_chain = NULL;
9509 int pending = 0;
9510 int task = 0;
9511 int prev_bkpt_count = breakpoint_count;
9512
9513 gdb_assert (ops != NULL);
9514
9515 /* If extra_string isn't useful, set it to NULL. */
9516 if (extra_string != NULL && *extra_string == '\0')
9517 extra_string = NULL;
9518
9519 TRY
9520 {
9521 ops->create_sals_from_location (location, &canonical, type_wanted);
9522 }
9523 CATCH (e, RETURN_MASK_ERROR)
9524 {
9525 /* If caller is interested in rc value from parse, set
9526 value. */
9527 if (e.error == NOT_FOUND_ERROR)
9528 {
9529 /* If pending breakpoint support is turned off, throw
9530 error. */
9531
9532 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9533 throw_exception (e);
9534
9535 exception_print (gdb_stderr, e);
9536
9537 /* If pending breakpoint support is auto query and the user
9538 selects no, then simply return the error code. */
9539 if (pending_break_support == AUTO_BOOLEAN_AUTO
9540 && !nquery (_("Make %s pending on future shared library load? "),
9541 bptype_string (type_wanted)))
9542 return 0;
9543
9544 /* At this point, either the user was queried about setting
9545 a pending breakpoint and selected yes, or pending
9546 breakpoint behavior is on and thus a pending breakpoint
9547 is defaulted on behalf of the user. */
9548 pending = 1;
9549 }
9550 else
9551 throw_exception (e);
9552 }
9553 END_CATCH
9554
9555 if (!pending && canonical.lsals.empty ())
9556 return 0;
9557
9558 /* ----------------------------- SNIP -----------------------------
9559 Anything added to the cleanup chain beyond this point is assumed
9560 to be part of a breakpoint. If the breakpoint create succeeds
9561 then the memory is not reclaimed. */
9562 bkpt_chain = make_cleanup (null_cleanup, 0);
9563
9564 /* Resolve all line numbers to PC's and verify that the addresses
9565 are ok for the target. */
9566 if (!pending)
9567 {
9568 for (auto &lsal : canonical.lsals)
9569 breakpoint_sals_to_pc (lsal.sals);
9570 }
9571
9572 /* Fast tracepoints may have additional restrictions on location. */
9573 if (!pending && type_wanted == bp_fast_tracepoint)
9574 {
9575 for (const auto &lsal : canonical.lsals)
9576 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9577 }
9578
9579 /* Verify that condition can be parsed, before setting any
9580 breakpoints. Allocate a separate condition expression for each
9581 breakpoint. */
9582 if (!pending)
9583 {
9584 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9585 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9586
9587 if (parse_extra)
9588 {
9589 char *rest;
9590 char *cond;
9591
9592 const linespec_sals &lsal = canonical.lsals[0];
9593
9594 /* Here we only parse 'arg' to separate condition
9595 from thread number, so parsing in context of first
9596 sal is OK. When setting the breakpoint we'll
9597 re-parse it in context of each sal. */
9598
9599 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9600 &cond, &thread, &task, &rest);
9601 cond_string_copy.reset (cond);
9602 extra_string_copy.reset (rest);
9603 }
9604 else
9605 {
9606 if (type_wanted != bp_dprintf
9607 && extra_string != NULL && *extra_string != '\0')
9608 error (_("Garbage '%s' at end of location"), extra_string);
9609
9610 /* Create a private copy of condition string. */
9611 if (cond_string)
9612 cond_string_copy.reset (xstrdup (cond_string));
9613 /* Create a private copy of any extra string. */
9614 if (extra_string)
9615 extra_string_copy.reset (xstrdup (extra_string));
9616 }
9617
9618 ops->create_breakpoints_sal (gdbarch, &canonical,
9619 std::move (cond_string_copy),
9620 std::move (extra_string_copy),
9621 type_wanted,
9622 tempflag ? disp_del : disp_donttouch,
9623 thread, task, ignore_count, ops,
9624 from_tty, enabled, internal, flags);
9625 }
9626 else
9627 {
9628 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9629
9630 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9631 b->location = copy_event_location (location);
9632
9633 if (parse_extra)
9634 b->cond_string = NULL;
9635 else
9636 {
9637 /* Create a private copy of condition string. */
9638 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9639 b->thread = thread;
9640 }
9641
9642 /* Create a private copy of any extra string. */
9643 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9644 b->ignore_count = ignore_count;
9645 b->disposition = tempflag ? disp_del : disp_donttouch;
9646 b->condition_not_parsed = 1;
9647 b->enable_state = enabled ? bp_enabled : bp_disabled;
9648 if ((type_wanted != bp_breakpoint
9649 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9650 b->pspace = current_program_space;
9651
9652 install_breakpoint (internal, std::move (b), 0);
9653 }
9654
9655 if (canonical.lsals.size () > 1)
9656 {
9657 warning (_("Multiple breakpoints were set.\nUse the "
9658 "\"delete\" command to delete unwanted breakpoints."));
9659 prev_breakpoint_count = prev_bkpt_count;
9660 }
9661
9662 /* That's it. Discard the cleanups for data inserted into the
9663 breakpoint. */
9664 discard_cleanups (bkpt_chain);
9665
9666 /* error call may happen here - have BKPT_CHAIN already discarded. */
9667 update_global_location_list (UGLL_MAY_INSERT);
9668
9669 return 1;
9670 }
9671
9672 /* Set a breakpoint.
9673 ARG is a string describing breakpoint address,
9674 condition, and thread.
9675 FLAG specifies if a breakpoint is hardware on,
9676 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9677 and BP_TEMPFLAG. */
9678
9679 static void
9680 break_command_1 (const char *arg, int flag, int from_tty)
9681 {
9682 int tempflag = flag & BP_TEMPFLAG;
9683 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9684 ? bp_hardware_breakpoint
9685 : bp_breakpoint);
9686 struct breakpoint_ops *ops;
9687
9688 event_location_up location = string_to_event_location (&arg, current_language);
9689
9690 /* Matching breakpoints on probes. */
9691 if (location != NULL
9692 && event_location_type (location.get ()) == PROBE_LOCATION)
9693 ops = &bkpt_probe_breakpoint_ops;
9694 else
9695 ops = &bkpt_breakpoint_ops;
9696
9697 create_breakpoint (get_current_arch (),
9698 location.get (),
9699 NULL, 0, arg, 1 /* parse arg */,
9700 tempflag, type_wanted,
9701 0 /* Ignore count */,
9702 pending_break_support,
9703 ops,
9704 from_tty,
9705 1 /* enabled */,
9706 0 /* internal */,
9707 0);
9708 }
9709
9710 /* Helper function for break_command_1 and disassemble_command. */
9711
9712 void
9713 resolve_sal_pc (struct symtab_and_line *sal)
9714 {
9715 CORE_ADDR pc;
9716
9717 if (sal->pc == 0 && sal->symtab != NULL)
9718 {
9719 if (!find_line_pc (sal->symtab, sal->line, &pc))
9720 error (_("No line %d in file \"%s\"."),
9721 sal->line, symtab_to_filename_for_display (sal->symtab));
9722 sal->pc = pc;
9723
9724 /* If this SAL corresponds to a breakpoint inserted using a line
9725 number, then skip the function prologue if necessary. */
9726 if (sal->explicit_line)
9727 skip_prologue_sal (sal);
9728 }
9729
9730 if (sal->section == 0 && sal->symtab != NULL)
9731 {
9732 const struct blockvector *bv;
9733 const struct block *b;
9734 struct symbol *sym;
9735
9736 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9737 SYMTAB_COMPUNIT (sal->symtab));
9738 if (bv != NULL)
9739 {
9740 sym = block_linkage_function (b);
9741 if (sym != NULL)
9742 {
9743 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9744 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9745 sym);
9746 }
9747 else
9748 {
9749 /* It really is worthwhile to have the section, so we'll
9750 just have to look harder. This case can be executed
9751 if we have line numbers but no functions (as can
9752 happen in assembly source). */
9753
9754 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9755 switch_to_program_space_and_thread (sal->pspace);
9756
9757 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9758 if (msym.minsym)
9759 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9760 }
9761 }
9762 }
9763 }
9764
9765 void
9766 break_command (char *arg, int from_tty)
9767 {
9768 break_command_1 (arg, 0, from_tty);
9769 }
9770
9771 void
9772 tbreak_command (char *arg, int from_tty)
9773 {
9774 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9775 }
9776
9777 static void
9778 hbreak_command (char *arg, int from_tty)
9779 {
9780 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9781 }
9782
9783 static void
9784 thbreak_command (char *arg, int from_tty)
9785 {
9786 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9787 }
9788
9789 static void
9790 stop_command (char *arg, int from_tty)
9791 {
9792 printf_filtered (_("Specify the type of breakpoint to set.\n\
9793 Usage: stop in <function | address>\n\
9794 stop at <line>\n"));
9795 }
9796
9797 static void
9798 stopin_command (char *arg, int from_tty)
9799 {
9800 int badInput = 0;
9801
9802 if (arg == (char *) NULL)
9803 badInput = 1;
9804 else if (*arg != '*')
9805 {
9806 char *argptr = arg;
9807 int hasColon = 0;
9808
9809 /* Look for a ':'. If this is a line number specification, then
9810 say it is bad, otherwise, it should be an address or
9811 function/method name. */
9812 while (*argptr && !hasColon)
9813 {
9814 hasColon = (*argptr == ':');
9815 argptr++;
9816 }
9817
9818 if (hasColon)
9819 badInput = (*argptr != ':'); /* Not a class::method */
9820 else
9821 badInput = isdigit (*arg); /* a simple line number */
9822 }
9823
9824 if (badInput)
9825 printf_filtered (_("Usage: stop in <function | address>\n"));
9826 else
9827 break_command_1 (arg, 0, from_tty);
9828 }
9829
9830 static void
9831 stopat_command (char *arg, int from_tty)
9832 {
9833 int badInput = 0;
9834
9835 if (arg == (char *) NULL || *arg == '*') /* no line number */
9836 badInput = 1;
9837 else
9838 {
9839 char *argptr = arg;
9840 int hasColon = 0;
9841
9842 /* Look for a ':'. If there is a '::' then get out, otherwise
9843 it is probably a line number. */
9844 while (*argptr && !hasColon)
9845 {
9846 hasColon = (*argptr == ':');
9847 argptr++;
9848 }
9849
9850 if (hasColon)
9851 badInput = (*argptr == ':'); /* we have class::method */
9852 else
9853 badInput = !isdigit (*arg); /* not a line number */
9854 }
9855
9856 if (badInput)
9857 printf_filtered (_("Usage: stop at <line>\n"));
9858 else
9859 break_command_1 (arg, 0, from_tty);
9860 }
9861
9862 /* The dynamic printf command is mostly like a regular breakpoint, but
9863 with a prewired command list consisting of a single output command,
9864 built from extra arguments supplied on the dprintf command
9865 line. */
9866
9867 static void
9868 dprintf_command (char *arg_in, int from_tty)
9869 {
9870 const char *arg = arg_in;
9871 event_location_up location = string_to_event_location (&arg, current_language);
9872
9873 /* If non-NULL, ARG should have been advanced past the location;
9874 the next character must be ','. */
9875 if (arg != NULL)
9876 {
9877 if (arg[0] != ',' || arg[1] == '\0')
9878 error (_("Format string required"));
9879 else
9880 {
9881 /* Skip the comma. */
9882 ++arg;
9883 }
9884 }
9885
9886 create_breakpoint (get_current_arch (),
9887 location.get (),
9888 NULL, 0, arg, 1 /* parse arg */,
9889 0, bp_dprintf,
9890 0 /* Ignore count */,
9891 pending_break_support,
9892 &dprintf_breakpoint_ops,
9893 from_tty,
9894 1 /* enabled */,
9895 0 /* internal */,
9896 0);
9897 }
9898
9899 static void
9900 agent_printf_command (char *arg, int from_tty)
9901 {
9902 error (_("May only run agent-printf on the target"));
9903 }
9904
9905 /* Implement the "breakpoint_hit" breakpoint_ops method for
9906 ranged breakpoints. */
9907
9908 static int
9909 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9910 struct address_space *aspace,
9911 CORE_ADDR bp_addr,
9912 const struct target_waitstatus *ws)
9913 {
9914 if (ws->kind != TARGET_WAITKIND_STOPPED
9915 || ws->value.sig != GDB_SIGNAL_TRAP)
9916 return 0;
9917
9918 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9919 bl->length, aspace, bp_addr);
9920 }
9921
9922 /* Implement the "resources_needed" breakpoint_ops method for
9923 ranged breakpoints. */
9924
9925 static int
9926 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9927 {
9928 return target_ranged_break_num_registers ();
9929 }
9930
9931 /* Implement the "print_it" breakpoint_ops method for
9932 ranged breakpoints. */
9933
9934 static enum print_stop_action
9935 print_it_ranged_breakpoint (bpstat bs)
9936 {
9937 struct breakpoint *b = bs->breakpoint_at;
9938 struct bp_location *bl = b->loc;
9939 struct ui_out *uiout = current_uiout;
9940
9941 gdb_assert (b->type == bp_hardware_breakpoint);
9942
9943 /* Ranged breakpoints have only one location. */
9944 gdb_assert (bl && bl->next == NULL);
9945
9946 annotate_breakpoint (b->number);
9947
9948 maybe_print_thread_hit_breakpoint (uiout);
9949
9950 if (b->disposition == disp_del)
9951 uiout->text ("Temporary ranged breakpoint ");
9952 else
9953 uiout->text ("Ranged breakpoint ");
9954 if (uiout->is_mi_like_p ())
9955 {
9956 uiout->field_string ("reason",
9957 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9958 uiout->field_string ("disp", bpdisp_text (b->disposition));
9959 }
9960 uiout->field_int ("bkptno", b->number);
9961 uiout->text (", ");
9962
9963 return PRINT_SRC_AND_LOC;
9964 }
9965
9966 /* Implement the "print_one" breakpoint_ops method for
9967 ranged breakpoints. */
9968
9969 static void
9970 print_one_ranged_breakpoint (struct breakpoint *b,
9971 struct bp_location **last_loc)
9972 {
9973 struct bp_location *bl = b->loc;
9974 struct value_print_options opts;
9975 struct ui_out *uiout = current_uiout;
9976
9977 /* Ranged breakpoints have only one location. */
9978 gdb_assert (bl && bl->next == NULL);
9979
9980 get_user_print_options (&opts);
9981
9982 if (opts.addressprint)
9983 /* We don't print the address range here, it will be printed later
9984 by print_one_detail_ranged_breakpoint. */
9985 uiout->field_skip ("addr");
9986 annotate_field (5);
9987 print_breakpoint_location (b, bl);
9988 *last_loc = bl;
9989 }
9990
9991 /* Implement the "print_one_detail" breakpoint_ops method for
9992 ranged breakpoints. */
9993
9994 static void
9995 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9996 struct ui_out *uiout)
9997 {
9998 CORE_ADDR address_start, address_end;
9999 struct bp_location *bl = b->loc;
10000 string_file stb;
10001
10002 gdb_assert (bl);
10003
10004 address_start = bl->address;
10005 address_end = address_start + bl->length - 1;
10006
10007 uiout->text ("\taddress range: ");
10008 stb.printf ("[%s, %s]",
10009 print_core_address (bl->gdbarch, address_start),
10010 print_core_address (bl->gdbarch, address_end));
10011 uiout->field_stream ("addr", stb);
10012 uiout->text ("\n");
10013 }
10014
10015 /* Implement the "print_mention" breakpoint_ops method for
10016 ranged breakpoints. */
10017
10018 static void
10019 print_mention_ranged_breakpoint (struct breakpoint *b)
10020 {
10021 struct bp_location *bl = b->loc;
10022 struct ui_out *uiout = current_uiout;
10023
10024 gdb_assert (bl);
10025 gdb_assert (b->type == bp_hardware_breakpoint);
10026
10027 if (uiout->is_mi_like_p ())
10028 return;
10029
10030 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10031 b->number, paddress (bl->gdbarch, bl->address),
10032 paddress (bl->gdbarch, bl->address + bl->length - 1));
10033 }
10034
10035 /* Implement the "print_recreate" breakpoint_ops method for
10036 ranged breakpoints. */
10037
10038 static void
10039 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10040 {
10041 fprintf_unfiltered (fp, "break-range %s, %s",
10042 event_location_to_string (b->location.get ()),
10043 event_location_to_string (b->location_range_end.get ()));
10044 print_recreate_thread (b, fp);
10045 }
10046
10047 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10048
10049 static struct breakpoint_ops ranged_breakpoint_ops;
10050
10051 /* Find the address where the end of the breakpoint range should be
10052 placed, given the SAL of the end of the range. This is so that if
10053 the user provides a line number, the end of the range is set to the
10054 last instruction of the given line. */
10055
10056 static CORE_ADDR
10057 find_breakpoint_range_end (struct symtab_and_line sal)
10058 {
10059 CORE_ADDR end;
10060
10061 /* If the user provided a PC value, use it. Otherwise,
10062 find the address of the end of the given location. */
10063 if (sal.explicit_pc)
10064 end = sal.pc;
10065 else
10066 {
10067 int ret;
10068 CORE_ADDR start;
10069
10070 ret = find_line_pc_range (sal, &start, &end);
10071 if (!ret)
10072 error (_("Could not find location of the end of the range."));
10073
10074 /* find_line_pc_range returns the start of the next line. */
10075 end--;
10076 }
10077
10078 return end;
10079 }
10080
10081 /* Implement the "break-range" CLI command. */
10082
10083 static void
10084 break_range_command (char *arg_in, int from_tty)
10085 {
10086 const char *arg = arg_in;
10087 const char *arg_start;
10088 char *addr_string_start;
10089 struct linespec_result canonical_start, canonical_end;
10090 int bp_count, can_use_bp, length;
10091 CORE_ADDR end;
10092 struct breakpoint *b;
10093 struct cleanup *cleanup_bkpt;
10094
10095 /* We don't support software ranged breakpoints. */
10096 if (target_ranged_break_num_registers () < 0)
10097 error (_("This target does not support hardware ranged breakpoints."));
10098
10099 bp_count = hw_breakpoint_used_count ();
10100 bp_count += target_ranged_break_num_registers ();
10101 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10102 bp_count, 0);
10103 if (can_use_bp < 0)
10104 error (_("Hardware breakpoints used exceeds limit."));
10105
10106 arg = skip_spaces (arg);
10107 if (arg == NULL || arg[0] == '\0')
10108 error(_("No address range specified."));
10109
10110 arg_start = arg;
10111 event_location_up start_location = string_to_event_location (&arg,
10112 current_language);
10113 parse_breakpoint_sals (start_location.get (), &canonical_start);
10114
10115 if (arg[0] != ',')
10116 error (_("Too few arguments."));
10117 else if (canonical_start.lsals.empty ())
10118 error (_("Could not find location of the beginning of the range."));
10119
10120 const linespec_sals &lsal_start = canonical_start.lsals[0];
10121
10122 if (canonical_start.lsals.size () > 1
10123 || lsal_start.sals.size () != 1)
10124 error (_("Cannot create a ranged breakpoint with multiple locations."));
10125
10126 const symtab_and_line &sal_start = lsal_start.sals[0];
10127 addr_string_start = savestring (arg_start, arg - arg_start);
10128 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
10129
10130 arg++; /* Skip the comma. */
10131 arg = skip_spaces (arg);
10132
10133 /* Parse the end location. */
10134
10135 arg_start = arg;
10136
10137 /* We call decode_line_full directly here instead of using
10138 parse_breakpoint_sals because we need to specify the start location's
10139 symtab and line as the default symtab and line for the end of the
10140 range. This makes it possible to have ranges like "foo.c:27, +14",
10141 where +14 means 14 lines from the start location. */
10142 event_location_up end_location = string_to_event_location (&arg,
10143 current_language);
10144 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10145 sal_start.symtab, sal_start.line,
10146 &canonical_end, NULL, NULL);
10147
10148 if (canonical_end.lsals.empty ())
10149 error (_("Could not find location of the end of the range."));
10150
10151 const linespec_sals &lsal_end = canonical_end.lsals[0];
10152 if (canonical_end.lsals.size () > 1
10153 || lsal_end.sals.size () != 1)
10154 error (_("Cannot create a ranged breakpoint with multiple locations."));
10155
10156 const symtab_and_line &sal_end = lsal_end.sals[0];
10157
10158 end = find_breakpoint_range_end (sal_end);
10159 if (sal_start.pc > end)
10160 error (_("Invalid address range, end precedes start."));
10161
10162 length = end - sal_start.pc + 1;
10163 if (length < 0)
10164 /* Length overflowed. */
10165 error (_("Address range too large."));
10166 else if (length == 1)
10167 {
10168 /* This range is simple enough to be handled by
10169 the `hbreak' command. */
10170 hbreak_command (addr_string_start, 1);
10171
10172 do_cleanups (cleanup_bkpt);
10173
10174 return;
10175 }
10176
10177 /* Now set up the breakpoint. */
10178 b = set_raw_breakpoint (get_current_arch (), sal_start,
10179 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10180 set_breakpoint_count (breakpoint_count + 1);
10181 b->number = breakpoint_count;
10182 b->disposition = disp_donttouch;
10183 b->location = std::move (start_location);
10184 b->location_range_end = std::move (end_location);
10185 b->loc->length = length;
10186
10187 do_cleanups (cleanup_bkpt);
10188
10189 mention (b);
10190 observer_notify_breakpoint_created (b);
10191 update_global_location_list (UGLL_MAY_INSERT);
10192 }
10193
10194 /* Return non-zero if EXP is verified as constant. Returned zero
10195 means EXP is variable. Also the constant detection may fail for
10196 some constant expressions and in such case still falsely return
10197 zero. */
10198
10199 static int
10200 watchpoint_exp_is_const (const struct expression *exp)
10201 {
10202 int i = exp->nelts;
10203
10204 while (i > 0)
10205 {
10206 int oplenp, argsp;
10207
10208 /* We are only interested in the descriptor of each element. */
10209 operator_length (exp, i, &oplenp, &argsp);
10210 i -= oplenp;
10211
10212 switch (exp->elts[i].opcode)
10213 {
10214 case BINOP_ADD:
10215 case BINOP_SUB:
10216 case BINOP_MUL:
10217 case BINOP_DIV:
10218 case BINOP_REM:
10219 case BINOP_MOD:
10220 case BINOP_LSH:
10221 case BINOP_RSH:
10222 case BINOP_LOGICAL_AND:
10223 case BINOP_LOGICAL_OR:
10224 case BINOP_BITWISE_AND:
10225 case BINOP_BITWISE_IOR:
10226 case BINOP_BITWISE_XOR:
10227 case BINOP_EQUAL:
10228 case BINOP_NOTEQUAL:
10229 case BINOP_LESS:
10230 case BINOP_GTR:
10231 case BINOP_LEQ:
10232 case BINOP_GEQ:
10233 case BINOP_REPEAT:
10234 case BINOP_COMMA:
10235 case BINOP_EXP:
10236 case BINOP_MIN:
10237 case BINOP_MAX:
10238 case BINOP_INTDIV:
10239 case BINOP_CONCAT:
10240 case TERNOP_COND:
10241 case TERNOP_SLICE:
10242
10243 case OP_LONG:
10244 case OP_DOUBLE:
10245 case OP_DECFLOAT:
10246 case OP_LAST:
10247 case OP_COMPLEX:
10248 case OP_STRING:
10249 case OP_ARRAY:
10250 case OP_TYPE:
10251 case OP_TYPEOF:
10252 case OP_DECLTYPE:
10253 case OP_TYPEID:
10254 case OP_NAME:
10255 case OP_OBJC_NSSTRING:
10256
10257 case UNOP_NEG:
10258 case UNOP_LOGICAL_NOT:
10259 case UNOP_COMPLEMENT:
10260 case UNOP_ADDR:
10261 case UNOP_HIGH:
10262 case UNOP_CAST:
10263
10264 case UNOP_CAST_TYPE:
10265 case UNOP_REINTERPRET_CAST:
10266 case UNOP_DYNAMIC_CAST:
10267 /* Unary, binary and ternary operators: We have to check
10268 their operands. If they are constant, then so is the
10269 result of that operation. For instance, if A and B are
10270 determined to be constants, then so is "A + B".
10271
10272 UNOP_IND is one exception to the rule above, because the
10273 value of *ADDR is not necessarily a constant, even when
10274 ADDR is. */
10275 break;
10276
10277 case OP_VAR_VALUE:
10278 /* Check whether the associated symbol is a constant.
10279
10280 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10281 possible that a buggy compiler could mark a variable as
10282 constant even when it is not, and TYPE_CONST would return
10283 true in this case, while SYMBOL_CLASS wouldn't.
10284
10285 We also have to check for function symbols because they
10286 are always constant. */
10287 {
10288 struct symbol *s = exp->elts[i + 2].symbol;
10289
10290 if (SYMBOL_CLASS (s) != LOC_BLOCK
10291 && SYMBOL_CLASS (s) != LOC_CONST
10292 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10293 return 0;
10294 break;
10295 }
10296
10297 /* The default action is to return 0 because we are using
10298 the optimistic approach here: If we don't know something,
10299 then it is not a constant. */
10300 default:
10301 return 0;
10302 }
10303 }
10304
10305 return 1;
10306 }
10307
10308 /* Watchpoint destructor. */
10309
10310 watchpoint::~watchpoint ()
10311 {
10312 xfree (this->exp_string);
10313 xfree (this->exp_string_reparse);
10314 value_free (this->val);
10315 }
10316
10317 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10318
10319 static void
10320 re_set_watchpoint (struct breakpoint *b)
10321 {
10322 struct watchpoint *w = (struct watchpoint *) b;
10323
10324 /* Watchpoint can be either on expression using entirely global
10325 variables, or it can be on local variables.
10326
10327 Watchpoints of the first kind are never auto-deleted, and even
10328 persist across program restarts. Since they can use variables
10329 from shared libraries, we need to reparse expression as libraries
10330 are loaded and unloaded.
10331
10332 Watchpoints on local variables can also change meaning as result
10333 of solib event. For example, if a watchpoint uses both a local
10334 and a global variables in expression, it's a local watchpoint,
10335 but unloading of a shared library will make the expression
10336 invalid. This is not a very common use case, but we still
10337 re-evaluate expression, to avoid surprises to the user.
10338
10339 Note that for local watchpoints, we re-evaluate it only if
10340 watchpoints frame id is still valid. If it's not, it means the
10341 watchpoint is out of scope and will be deleted soon. In fact,
10342 I'm not sure we'll ever be called in this case.
10343
10344 If a local watchpoint's frame id is still valid, then
10345 w->exp_valid_block is likewise valid, and we can safely use it.
10346
10347 Don't do anything about disabled watchpoints, since they will be
10348 reevaluated again when enabled. */
10349 update_watchpoint (w, 1 /* reparse */);
10350 }
10351
10352 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10353
10354 static int
10355 insert_watchpoint (struct bp_location *bl)
10356 {
10357 struct watchpoint *w = (struct watchpoint *) bl->owner;
10358 int length = w->exact ? 1 : bl->length;
10359
10360 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10361 w->cond_exp.get ());
10362 }
10363
10364 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10365
10366 static int
10367 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10368 {
10369 struct watchpoint *w = (struct watchpoint *) bl->owner;
10370 int length = w->exact ? 1 : bl->length;
10371
10372 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10373 w->cond_exp.get ());
10374 }
10375
10376 static int
10377 breakpoint_hit_watchpoint (const struct bp_location *bl,
10378 struct address_space *aspace, CORE_ADDR bp_addr,
10379 const struct target_waitstatus *ws)
10380 {
10381 struct breakpoint *b = bl->owner;
10382 struct watchpoint *w = (struct watchpoint *) b;
10383
10384 /* Continuable hardware watchpoints are treated as non-existent if the
10385 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10386 some data address). Otherwise gdb won't stop on a break instruction
10387 in the code (not from a breakpoint) when a hardware watchpoint has
10388 been defined. Also skip watchpoints which we know did not trigger
10389 (did not match the data address). */
10390 if (is_hardware_watchpoint (b)
10391 && w->watchpoint_triggered == watch_triggered_no)
10392 return 0;
10393
10394 return 1;
10395 }
10396
10397 static void
10398 check_status_watchpoint (bpstat bs)
10399 {
10400 gdb_assert (is_watchpoint (bs->breakpoint_at));
10401
10402 bpstat_check_watchpoint (bs);
10403 }
10404
10405 /* Implement the "resources_needed" breakpoint_ops method for
10406 hardware watchpoints. */
10407
10408 static int
10409 resources_needed_watchpoint (const struct bp_location *bl)
10410 {
10411 struct watchpoint *w = (struct watchpoint *) bl->owner;
10412 int length = w->exact? 1 : bl->length;
10413
10414 return target_region_ok_for_hw_watchpoint (bl->address, length);
10415 }
10416
10417 /* Implement the "works_in_software_mode" breakpoint_ops method for
10418 hardware watchpoints. */
10419
10420 static int
10421 works_in_software_mode_watchpoint (const struct breakpoint *b)
10422 {
10423 /* Read and access watchpoints only work with hardware support. */
10424 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10425 }
10426
10427 static enum print_stop_action
10428 print_it_watchpoint (bpstat bs)
10429 {
10430 struct breakpoint *b;
10431 enum print_stop_action result;
10432 struct watchpoint *w;
10433 struct ui_out *uiout = current_uiout;
10434
10435 gdb_assert (bs->bp_location_at != NULL);
10436
10437 b = bs->breakpoint_at;
10438 w = (struct watchpoint *) b;
10439
10440 annotate_watchpoint (b->number);
10441 maybe_print_thread_hit_breakpoint (uiout);
10442
10443 string_file stb;
10444
10445 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10446 switch (b->type)
10447 {
10448 case bp_watchpoint:
10449 case bp_hardware_watchpoint:
10450 if (uiout->is_mi_like_p ())
10451 uiout->field_string
10452 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10453 mention (b);
10454 tuple_emitter.emplace (uiout, "value");
10455 uiout->text ("\nOld value = ");
10456 watchpoint_value_print (bs->old_val, &stb);
10457 uiout->field_stream ("old", stb);
10458 uiout->text ("\nNew value = ");
10459 watchpoint_value_print (w->val, &stb);
10460 uiout->field_stream ("new", stb);
10461 uiout->text ("\n");
10462 /* More than one watchpoint may have been triggered. */
10463 result = PRINT_UNKNOWN;
10464 break;
10465
10466 case bp_read_watchpoint:
10467 if (uiout->is_mi_like_p ())
10468 uiout->field_string
10469 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10470 mention (b);
10471 tuple_emitter.emplace (uiout, "value");
10472 uiout->text ("\nValue = ");
10473 watchpoint_value_print (w->val, &stb);
10474 uiout->field_stream ("value", stb);
10475 uiout->text ("\n");
10476 result = PRINT_UNKNOWN;
10477 break;
10478
10479 case bp_access_watchpoint:
10480 if (bs->old_val != NULL)
10481 {
10482 if (uiout->is_mi_like_p ())
10483 uiout->field_string
10484 ("reason",
10485 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10486 mention (b);
10487 tuple_emitter.emplace (uiout, "value");
10488 uiout->text ("\nOld value = ");
10489 watchpoint_value_print (bs->old_val, &stb);
10490 uiout->field_stream ("old", stb);
10491 uiout->text ("\nNew value = ");
10492 }
10493 else
10494 {
10495 mention (b);
10496 if (uiout->is_mi_like_p ())
10497 uiout->field_string
10498 ("reason",
10499 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10500 tuple_emitter.emplace (uiout, "value");
10501 uiout->text ("\nValue = ");
10502 }
10503 watchpoint_value_print (w->val, &stb);
10504 uiout->field_stream ("new", stb);
10505 uiout->text ("\n");
10506 result = PRINT_UNKNOWN;
10507 break;
10508 default:
10509 result = PRINT_UNKNOWN;
10510 }
10511
10512 return result;
10513 }
10514
10515 /* Implement the "print_mention" breakpoint_ops method for hardware
10516 watchpoints. */
10517
10518 static void
10519 print_mention_watchpoint (struct breakpoint *b)
10520 {
10521 struct watchpoint *w = (struct watchpoint *) b;
10522 struct ui_out *uiout = current_uiout;
10523 const char *tuple_name;
10524
10525 switch (b->type)
10526 {
10527 case bp_watchpoint:
10528 uiout->text ("Watchpoint ");
10529 tuple_name = "wpt";
10530 break;
10531 case bp_hardware_watchpoint:
10532 uiout->text ("Hardware watchpoint ");
10533 tuple_name = "wpt";
10534 break;
10535 case bp_read_watchpoint:
10536 uiout->text ("Hardware read watchpoint ");
10537 tuple_name = "hw-rwpt";
10538 break;
10539 case bp_access_watchpoint:
10540 uiout->text ("Hardware access (read/write) watchpoint ");
10541 tuple_name = "hw-awpt";
10542 break;
10543 default:
10544 internal_error (__FILE__, __LINE__,
10545 _("Invalid hardware watchpoint type."));
10546 }
10547
10548 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10549 uiout->field_int ("number", b->number);
10550 uiout->text (": ");
10551 uiout->field_string ("exp", w->exp_string);
10552 }
10553
10554 /* Implement the "print_recreate" breakpoint_ops method for
10555 watchpoints. */
10556
10557 static void
10558 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10559 {
10560 struct watchpoint *w = (struct watchpoint *) b;
10561
10562 switch (b->type)
10563 {
10564 case bp_watchpoint:
10565 case bp_hardware_watchpoint:
10566 fprintf_unfiltered (fp, "watch");
10567 break;
10568 case bp_read_watchpoint:
10569 fprintf_unfiltered (fp, "rwatch");
10570 break;
10571 case bp_access_watchpoint:
10572 fprintf_unfiltered (fp, "awatch");
10573 break;
10574 default:
10575 internal_error (__FILE__, __LINE__,
10576 _("Invalid watchpoint type."));
10577 }
10578
10579 fprintf_unfiltered (fp, " %s", w->exp_string);
10580 print_recreate_thread (b, fp);
10581 }
10582
10583 /* Implement the "explains_signal" breakpoint_ops method for
10584 watchpoints. */
10585
10586 static int
10587 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10588 {
10589 /* A software watchpoint cannot cause a signal other than
10590 GDB_SIGNAL_TRAP. */
10591 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10592 return 0;
10593
10594 return 1;
10595 }
10596
10597 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10598
10599 static struct breakpoint_ops watchpoint_breakpoint_ops;
10600
10601 /* Implement the "insert" breakpoint_ops method for
10602 masked hardware watchpoints. */
10603
10604 static int
10605 insert_masked_watchpoint (struct bp_location *bl)
10606 {
10607 struct watchpoint *w = (struct watchpoint *) bl->owner;
10608
10609 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10610 bl->watchpoint_type);
10611 }
10612
10613 /* Implement the "remove" breakpoint_ops method for
10614 masked hardware watchpoints. */
10615
10616 static int
10617 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10618 {
10619 struct watchpoint *w = (struct watchpoint *) bl->owner;
10620
10621 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10622 bl->watchpoint_type);
10623 }
10624
10625 /* Implement the "resources_needed" breakpoint_ops method for
10626 masked hardware watchpoints. */
10627
10628 static int
10629 resources_needed_masked_watchpoint (const struct bp_location *bl)
10630 {
10631 struct watchpoint *w = (struct watchpoint *) bl->owner;
10632
10633 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10634 }
10635
10636 /* Implement the "works_in_software_mode" breakpoint_ops method for
10637 masked hardware watchpoints. */
10638
10639 static int
10640 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10641 {
10642 return 0;
10643 }
10644
10645 /* Implement the "print_it" breakpoint_ops method for
10646 masked hardware watchpoints. */
10647
10648 static enum print_stop_action
10649 print_it_masked_watchpoint (bpstat bs)
10650 {
10651 struct breakpoint *b = bs->breakpoint_at;
10652 struct ui_out *uiout = current_uiout;
10653
10654 /* Masked watchpoints have only one location. */
10655 gdb_assert (b->loc && b->loc->next == NULL);
10656
10657 annotate_watchpoint (b->number);
10658 maybe_print_thread_hit_breakpoint (uiout);
10659
10660 switch (b->type)
10661 {
10662 case bp_hardware_watchpoint:
10663 if (uiout->is_mi_like_p ())
10664 uiout->field_string
10665 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10666 break;
10667
10668 case bp_read_watchpoint:
10669 if (uiout->is_mi_like_p ())
10670 uiout->field_string
10671 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10672 break;
10673
10674 case bp_access_watchpoint:
10675 if (uiout->is_mi_like_p ())
10676 uiout->field_string
10677 ("reason",
10678 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10679 break;
10680 default:
10681 internal_error (__FILE__, __LINE__,
10682 _("Invalid hardware watchpoint type."));
10683 }
10684
10685 mention (b);
10686 uiout->text (_("\n\
10687 Check the underlying instruction at PC for the memory\n\
10688 address and value which triggered this watchpoint.\n"));
10689 uiout->text ("\n");
10690
10691 /* More than one watchpoint may have been triggered. */
10692 return PRINT_UNKNOWN;
10693 }
10694
10695 /* Implement the "print_one_detail" breakpoint_ops method for
10696 masked hardware watchpoints. */
10697
10698 static void
10699 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10700 struct ui_out *uiout)
10701 {
10702 struct watchpoint *w = (struct watchpoint *) b;
10703
10704 /* Masked watchpoints have only one location. */
10705 gdb_assert (b->loc && b->loc->next == NULL);
10706
10707 uiout->text ("\tmask ");
10708 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10709 uiout->text ("\n");
10710 }
10711
10712 /* Implement the "print_mention" breakpoint_ops method for
10713 masked hardware watchpoints. */
10714
10715 static void
10716 print_mention_masked_watchpoint (struct breakpoint *b)
10717 {
10718 struct watchpoint *w = (struct watchpoint *) b;
10719 struct ui_out *uiout = current_uiout;
10720 const char *tuple_name;
10721
10722 switch (b->type)
10723 {
10724 case bp_hardware_watchpoint:
10725 uiout->text ("Masked hardware watchpoint ");
10726 tuple_name = "wpt";
10727 break;
10728 case bp_read_watchpoint:
10729 uiout->text ("Masked hardware read watchpoint ");
10730 tuple_name = "hw-rwpt";
10731 break;
10732 case bp_access_watchpoint:
10733 uiout->text ("Masked hardware access (read/write) watchpoint ");
10734 tuple_name = "hw-awpt";
10735 break;
10736 default:
10737 internal_error (__FILE__, __LINE__,
10738 _("Invalid hardware watchpoint type."));
10739 }
10740
10741 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10742 uiout->field_int ("number", b->number);
10743 uiout->text (": ");
10744 uiout->field_string ("exp", w->exp_string);
10745 }
10746
10747 /* Implement the "print_recreate" breakpoint_ops method for
10748 masked hardware watchpoints. */
10749
10750 static void
10751 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10752 {
10753 struct watchpoint *w = (struct watchpoint *) b;
10754 char tmp[40];
10755
10756 switch (b->type)
10757 {
10758 case bp_hardware_watchpoint:
10759 fprintf_unfiltered (fp, "watch");
10760 break;
10761 case bp_read_watchpoint:
10762 fprintf_unfiltered (fp, "rwatch");
10763 break;
10764 case bp_access_watchpoint:
10765 fprintf_unfiltered (fp, "awatch");
10766 break;
10767 default:
10768 internal_error (__FILE__, __LINE__,
10769 _("Invalid hardware watchpoint type."));
10770 }
10771
10772 sprintf_vma (tmp, w->hw_wp_mask);
10773 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10774 print_recreate_thread (b, fp);
10775 }
10776
10777 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10778
10779 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10780
10781 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10782
10783 static int
10784 is_masked_watchpoint (const struct breakpoint *b)
10785 {
10786 return b->ops == &masked_watchpoint_breakpoint_ops;
10787 }
10788
10789 /* accessflag: hw_write: watch write,
10790 hw_read: watch read,
10791 hw_access: watch access (read or write) */
10792 static void
10793 watch_command_1 (const char *arg, int accessflag, int from_tty,
10794 int just_location, int internal)
10795 {
10796 struct breakpoint *scope_breakpoint = NULL;
10797 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10798 struct value *val, *mark, *result;
10799 int saved_bitpos = 0, saved_bitsize = 0;
10800 const char *exp_start = NULL;
10801 const char *exp_end = NULL;
10802 const char *tok, *end_tok;
10803 int toklen = -1;
10804 const char *cond_start = NULL;
10805 const char *cond_end = NULL;
10806 enum bptype bp_type;
10807 int thread = -1;
10808 int pc = 0;
10809 /* Flag to indicate whether we are going to use masks for
10810 the hardware watchpoint. */
10811 int use_mask = 0;
10812 CORE_ADDR mask = 0;
10813 char *expression;
10814 struct cleanup *back_to;
10815
10816 /* Make sure that we actually have parameters to parse. */
10817 if (arg != NULL && arg[0] != '\0')
10818 {
10819 const char *value_start;
10820
10821 exp_end = arg + strlen (arg);
10822
10823 /* Look for "parameter value" pairs at the end
10824 of the arguments string. */
10825 for (tok = exp_end - 1; tok > arg; tok--)
10826 {
10827 /* Skip whitespace at the end of the argument list. */
10828 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10829 tok--;
10830
10831 /* Find the beginning of the last token.
10832 This is the value of the parameter. */
10833 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10834 tok--;
10835 value_start = tok + 1;
10836
10837 /* Skip whitespace. */
10838 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10839 tok--;
10840
10841 end_tok = tok;
10842
10843 /* Find the beginning of the second to last token.
10844 This is the parameter itself. */
10845 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10846 tok--;
10847 tok++;
10848 toklen = end_tok - tok + 1;
10849
10850 if (toklen == 6 && startswith (tok, "thread"))
10851 {
10852 struct thread_info *thr;
10853 /* At this point we've found a "thread" token, which means
10854 the user is trying to set a watchpoint that triggers
10855 only in a specific thread. */
10856 const char *endp;
10857
10858 if (thread != -1)
10859 error(_("You can specify only one thread."));
10860
10861 /* Extract the thread ID from the next token. */
10862 thr = parse_thread_id (value_start, &endp);
10863
10864 /* Check if the user provided a valid thread ID. */
10865 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10866 invalid_thread_id_error (value_start);
10867
10868 thread = thr->global_num;
10869 }
10870 else if (toklen == 4 && startswith (tok, "mask"))
10871 {
10872 /* We've found a "mask" token, which means the user wants to
10873 create a hardware watchpoint that is going to have the mask
10874 facility. */
10875 struct value *mask_value, *mark;
10876
10877 if (use_mask)
10878 error(_("You can specify only one mask."));
10879
10880 use_mask = just_location = 1;
10881
10882 mark = value_mark ();
10883 mask_value = parse_to_comma_and_eval (&value_start);
10884 mask = value_as_address (mask_value);
10885 value_free_to_mark (mark);
10886 }
10887 else
10888 /* We didn't recognize what we found. We should stop here. */
10889 break;
10890
10891 /* Truncate the string and get rid of the "parameter value" pair before
10892 the arguments string is parsed by the parse_exp_1 function. */
10893 exp_end = tok;
10894 }
10895 }
10896 else
10897 exp_end = arg;
10898
10899 /* Parse the rest of the arguments. From here on out, everything
10900 is in terms of a newly allocated string instead of the original
10901 ARG. */
10902 innermost_block = NULL;
10903 expression = savestring (arg, exp_end - arg);
10904 back_to = make_cleanup (xfree, expression);
10905 exp_start = arg = expression;
10906 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
10907 exp_end = arg;
10908 /* Remove trailing whitespace from the expression before saving it.
10909 This makes the eventual display of the expression string a bit
10910 prettier. */
10911 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10912 --exp_end;
10913
10914 /* Checking if the expression is not constant. */
10915 if (watchpoint_exp_is_const (exp.get ()))
10916 {
10917 int len;
10918
10919 len = exp_end - exp_start;
10920 while (len > 0 && isspace (exp_start[len - 1]))
10921 len--;
10922 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10923 }
10924
10925 exp_valid_block = innermost_block;
10926 mark = value_mark ();
10927 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
10928
10929 if (val != NULL && just_location)
10930 {
10931 saved_bitpos = value_bitpos (val);
10932 saved_bitsize = value_bitsize (val);
10933 }
10934
10935 if (just_location)
10936 {
10937 int ret;
10938
10939 exp_valid_block = NULL;
10940 val = value_addr (result);
10941 release_value (val);
10942 value_free_to_mark (mark);
10943
10944 if (use_mask)
10945 {
10946 ret = target_masked_watch_num_registers (value_as_address (val),
10947 mask);
10948 if (ret == -1)
10949 error (_("This target does not support masked watchpoints."));
10950 else if (ret == -2)
10951 error (_("Invalid mask or memory region."));
10952 }
10953 }
10954 else if (val != NULL)
10955 release_value (val);
10956
10957 tok = skip_spaces (arg);
10958 end_tok = skip_to_space (tok);
10959
10960 toklen = end_tok - tok;
10961 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10962 {
10963 innermost_block = NULL;
10964 tok = cond_start = end_tok + 1;
10965 parse_exp_1 (&tok, 0, 0, 0);
10966
10967 /* The watchpoint expression may not be local, but the condition
10968 may still be. E.g.: `watch global if local > 0'. */
10969 cond_exp_valid_block = innermost_block;
10970
10971 cond_end = tok;
10972 }
10973 if (*tok)
10974 error (_("Junk at end of command."));
10975
10976 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10977
10978 /* Save this because create_internal_breakpoint below invalidates
10979 'wp_frame'. */
10980 frame_id watchpoint_frame = get_frame_id (wp_frame);
10981
10982 /* If the expression is "local", then set up a "watchpoint scope"
10983 breakpoint at the point where we've left the scope of the watchpoint
10984 expression. Create the scope breakpoint before the watchpoint, so
10985 that we will encounter it first in bpstat_stop_status. */
10986 if (exp_valid_block != NULL && wp_frame != NULL)
10987 {
10988 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10989
10990 if (frame_id_p (caller_frame_id))
10991 {
10992 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10993 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10994
10995 scope_breakpoint
10996 = create_internal_breakpoint (caller_arch, caller_pc,
10997 bp_watchpoint_scope,
10998 &momentary_breakpoint_ops);
10999
11000 /* create_internal_breakpoint could invalidate WP_FRAME. */
11001 wp_frame = NULL;
11002
11003 scope_breakpoint->enable_state = bp_enabled;
11004
11005 /* Automatically delete the breakpoint when it hits. */
11006 scope_breakpoint->disposition = disp_del;
11007
11008 /* Only break in the proper frame (help with recursion). */
11009 scope_breakpoint->frame_id = caller_frame_id;
11010
11011 /* Set the address at which we will stop. */
11012 scope_breakpoint->loc->gdbarch = caller_arch;
11013 scope_breakpoint->loc->requested_address = caller_pc;
11014 scope_breakpoint->loc->address
11015 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11016 scope_breakpoint->loc->requested_address,
11017 scope_breakpoint->type);
11018 }
11019 }
11020
11021 /* Now set up the breakpoint. We create all watchpoints as hardware
11022 watchpoints here even if hardware watchpoints are turned off, a call
11023 to update_watchpoint later in this function will cause the type to
11024 drop back to bp_watchpoint (software watchpoint) if required. */
11025
11026 if (accessflag == hw_read)
11027 bp_type = bp_read_watchpoint;
11028 else if (accessflag == hw_access)
11029 bp_type = bp_access_watchpoint;
11030 else
11031 bp_type = bp_hardware_watchpoint;
11032
11033 std::unique_ptr<watchpoint> w (new watchpoint ());
11034
11035 if (use_mask)
11036 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
11037 &masked_watchpoint_breakpoint_ops);
11038 else
11039 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
11040 &watchpoint_breakpoint_ops);
11041 w->thread = thread;
11042 w->disposition = disp_donttouch;
11043 w->pspace = current_program_space;
11044 w->exp = std::move (exp);
11045 w->exp_valid_block = exp_valid_block;
11046 w->cond_exp_valid_block = cond_exp_valid_block;
11047 if (just_location)
11048 {
11049 struct type *t = value_type (val);
11050 CORE_ADDR addr = value_as_address (val);
11051
11052 w->exp_string_reparse
11053 = current_language->la_watch_location_expression (t, addr).release ();
11054
11055 w->exp_string = xstrprintf ("-location %.*s",
11056 (int) (exp_end - exp_start), exp_start);
11057 }
11058 else
11059 w->exp_string = savestring (exp_start, exp_end - exp_start);
11060
11061 if (use_mask)
11062 {
11063 w->hw_wp_mask = mask;
11064 }
11065 else
11066 {
11067 w->val = val;
11068 w->val_bitpos = saved_bitpos;
11069 w->val_bitsize = saved_bitsize;
11070 w->val_valid = 1;
11071 }
11072
11073 if (cond_start)
11074 w->cond_string = savestring (cond_start, cond_end - cond_start);
11075 else
11076 w->cond_string = 0;
11077
11078 if (frame_id_p (watchpoint_frame))
11079 {
11080 w->watchpoint_frame = watchpoint_frame;
11081 w->watchpoint_thread = inferior_ptid;
11082 }
11083 else
11084 {
11085 w->watchpoint_frame = null_frame_id;
11086 w->watchpoint_thread = null_ptid;
11087 }
11088
11089 if (scope_breakpoint != NULL)
11090 {
11091 /* The scope breakpoint is related to the watchpoint. We will
11092 need to act on them together. */
11093 w->related_breakpoint = scope_breakpoint;
11094 scope_breakpoint->related_breakpoint = w.get ();
11095 }
11096
11097 if (!just_location)
11098 value_free_to_mark (mark);
11099
11100 /* Finally update the new watchpoint. This creates the locations
11101 that should be inserted. */
11102 update_watchpoint (w.get (), 1);
11103
11104 install_breakpoint (internal, std::move (w), 1);
11105 do_cleanups (back_to);
11106 }
11107
11108 /* Return count of debug registers needed to watch the given expression.
11109 If the watchpoint cannot be handled in hardware return zero. */
11110
11111 static int
11112 can_use_hardware_watchpoint (struct value *v)
11113 {
11114 int found_memory_cnt = 0;
11115 struct value *head = v;
11116
11117 /* Did the user specifically forbid us to use hardware watchpoints? */
11118 if (!can_use_hw_watchpoints)
11119 return 0;
11120
11121 /* Make sure that the value of the expression depends only upon
11122 memory contents, and values computed from them within GDB. If we
11123 find any register references or function calls, we can't use a
11124 hardware watchpoint.
11125
11126 The idea here is that evaluating an expression generates a series
11127 of values, one holding the value of every subexpression. (The
11128 expression a*b+c has five subexpressions: a, b, a*b, c, and
11129 a*b+c.) GDB's values hold almost enough information to establish
11130 the criteria given above --- they identify memory lvalues,
11131 register lvalues, computed values, etcetera. So we can evaluate
11132 the expression, and then scan the chain of values that leaves
11133 behind to decide whether we can detect any possible change to the
11134 expression's final value using only hardware watchpoints.
11135
11136 However, I don't think that the values returned by inferior
11137 function calls are special in any way. So this function may not
11138 notice that an expression involving an inferior function call
11139 can't be watched with hardware watchpoints. FIXME. */
11140 for (; v; v = value_next (v))
11141 {
11142 if (VALUE_LVAL (v) == lval_memory)
11143 {
11144 if (v != head && value_lazy (v))
11145 /* A lazy memory lvalue in the chain is one that GDB never
11146 needed to fetch; we either just used its address (e.g.,
11147 `a' in `a.b') or we never needed it at all (e.g., `a'
11148 in `a,b'). This doesn't apply to HEAD; if that is
11149 lazy then it was not readable, but watch it anyway. */
11150 ;
11151 else
11152 {
11153 /* Ahh, memory we actually used! Check if we can cover
11154 it with hardware watchpoints. */
11155 struct type *vtype = check_typedef (value_type (v));
11156
11157 /* We only watch structs and arrays if user asked for it
11158 explicitly, never if they just happen to appear in a
11159 middle of some value chain. */
11160 if (v == head
11161 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11162 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11163 {
11164 CORE_ADDR vaddr = value_address (v);
11165 int len;
11166 int num_regs;
11167
11168 len = (target_exact_watchpoints
11169 && is_scalar_type_recursive (vtype))?
11170 1 : TYPE_LENGTH (value_type (v));
11171
11172 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11173 if (!num_regs)
11174 return 0;
11175 else
11176 found_memory_cnt += num_regs;
11177 }
11178 }
11179 }
11180 else if (VALUE_LVAL (v) != not_lval
11181 && deprecated_value_modifiable (v) == 0)
11182 return 0; /* These are values from the history (e.g., $1). */
11183 else if (VALUE_LVAL (v) == lval_register)
11184 return 0; /* Cannot watch a register with a HW watchpoint. */
11185 }
11186
11187 /* The expression itself looks suitable for using a hardware
11188 watchpoint, but give the target machine a chance to reject it. */
11189 return found_memory_cnt;
11190 }
11191
11192 void
11193 watch_command_wrapper (const char *arg, int from_tty, int internal)
11194 {
11195 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11196 }
11197
11198 /* A helper function that looks for the "-location" argument and then
11199 calls watch_command_1. */
11200
11201 static void
11202 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11203 {
11204 int just_location = 0;
11205
11206 if (arg
11207 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11208 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11209 {
11210 arg = skip_spaces (arg);
11211 just_location = 1;
11212 }
11213
11214 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11215 }
11216
11217 static void
11218 watch_command (char *arg, int from_tty)
11219 {
11220 watch_maybe_just_location (arg, hw_write, from_tty);
11221 }
11222
11223 void
11224 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
11225 {
11226 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11227 }
11228
11229 static void
11230 rwatch_command (char *arg, int from_tty)
11231 {
11232 watch_maybe_just_location (arg, hw_read, from_tty);
11233 }
11234
11235 void
11236 awatch_command_wrapper (const char *arg, int from_tty, int internal)
11237 {
11238 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11239 }
11240
11241 static void
11242 awatch_command (char *arg, int from_tty)
11243 {
11244 watch_maybe_just_location (arg, hw_access, from_tty);
11245 }
11246 \f
11247
11248 /* Data for the FSM that manages the until(location)/advance commands
11249 in infcmd.c. Here because it uses the mechanisms of
11250 breakpoints. */
11251
11252 struct until_break_fsm
11253 {
11254 /* The base class. */
11255 struct thread_fsm thread_fsm;
11256
11257 /* The thread that as current when the command was executed. */
11258 int thread;
11259
11260 /* The breakpoint set at the destination location. */
11261 struct breakpoint *location_breakpoint;
11262
11263 /* Breakpoint set at the return address in the caller frame. May be
11264 NULL. */
11265 struct breakpoint *caller_breakpoint;
11266 };
11267
11268 static void until_break_fsm_clean_up (struct thread_fsm *self,
11269 struct thread_info *thread);
11270 static int until_break_fsm_should_stop (struct thread_fsm *self,
11271 struct thread_info *thread);
11272 static enum async_reply_reason
11273 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11274
11275 /* until_break_fsm's vtable. */
11276
11277 static struct thread_fsm_ops until_break_fsm_ops =
11278 {
11279 NULL, /* dtor */
11280 until_break_fsm_clean_up,
11281 until_break_fsm_should_stop,
11282 NULL, /* return_value */
11283 until_break_fsm_async_reply_reason,
11284 };
11285
11286 /* Allocate a new until_break_command_fsm. */
11287
11288 static struct until_break_fsm *
11289 new_until_break_fsm (struct interp *cmd_interp, int thread,
11290 struct breakpoint *location_breakpoint,
11291 struct breakpoint *caller_breakpoint)
11292 {
11293 struct until_break_fsm *sm;
11294
11295 sm = XCNEW (struct until_break_fsm);
11296 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11297
11298 sm->thread = thread;
11299 sm->location_breakpoint = location_breakpoint;
11300 sm->caller_breakpoint = caller_breakpoint;
11301
11302 return sm;
11303 }
11304
11305 /* Implementation of the 'should_stop' FSM method for the
11306 until(location)/advance commands. */
11307
11308 static int
11309 until_break_fsm_should_stop (struct thread_fsm *self,
11310 struct thread_info *tp)
11311 {
11312 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11313
11314 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11315 sm->location_breakpoint) != NULL
11316 || (sm->caller_breakpoint != NULL
11317 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11318 sm->caller_breakpoint) != NULL))
11319 thread_fsm_set_finished (self);
11320
11321 return 1;
11322 }
11323
11324 /* Implementation of the 'clean_up' FSM method for the
11325 until(location)/advance commands. */
11326
11327 static void
11328 until_break_fsm_clean_up (struct thread_fsm *self,
11329 struct thread_info *thread)
11330 {
11331 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11332
11333 /* Clean up our temporary breakpoints. */
11334 if (sm->location_breakpoint != NULL)
11335 {
11336 delete_breakpoint (sm->location_breakpoint);
11337 sm->location_breakpoint = NULL;
11338 }
11339 if (sm->caller_breakpoint != NULL)
11340 {
11341 delete_breakpoint (sm->caller_breakpoint);
11342 sm->caller_breakpoint = NULL;
11343 }
11344 delete_longjmp_breakpoint (sm->thread);
11345 }
11346
11347 /* Implementation of the 'async_reply_reason' FSM method for the
11348 until(location)/advance commands. */
11349
11350 static enum async_reply_reason
11351 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11352 {
11353 return EXEC_ASYNC_LOCATION_REACHED;
11354 }
11355
11356 void
11357 until_break_command (const char *arg, int from_tty, int anywhere)
11358 {
11359 struct frame_info *frame;
11360 struct gdbarch *frame_gdbarch;
11361 struct frame_id stack_frame_id;
11362 struct frame_id caller_frame_id;
11363 struct breakpoint *location_breakpoint;
11364 struct breakpoint *caller_breakpoint = NULL;
11365 struct cleanup *old_chain;
11366 int thread;
11367 struct thread_info *tp;
11368 struct until_break_fsm *sm;
11369
11370 clear_proceed_status (0);
11371
11372 /* Set a breakpoint where the user wants it and at return from
11373 this function. */
11374
11375 event_location_up location = string_to_event_location (&arg, current_language);
11376
11377 std::vector<symtab_and_line> sals
11378 = (last_displayed_sal_is_valid ()
11379 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11380 get_last_displayed_symtab (),
11381 get_last_displayed_line ())
11382 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11383 NULL, (struct symtab *) NULL, 0));
11384
11385 if (sals.size () != 1)
11386 error (_("Couldn't get information on specified line."));
11387
11388 symtab_and_line &sal = sals[0];
11389
11390 if (*arg)
11391 error (_("Junk at end of arguments."));
11392
11393 resolve_sal_pc (&sal);
11394
11395 tp = inferior_thread ();
11396 thread = tp->global_num;
11397
11398 old_chain = make_cleanup (null_cleanup, NULL);
11399
11400 /* Note linespec handling above invalidates the frame chain.
11401 Installing a breakpoint also invalidates the frame chain (as it
11402 may need to switch threads), so do any frame handling before
11403 that. */
11404
11405 frame = get_selected_frame (NULL);
11406 frame_gdbarch = get_frame_arch (frame);
11407 stack_frame_id = get_stack_frame_id (frame);
11408 caller_frame_id = frame_unwind_caller_id (frame);
11409
11410 /* Keep within the current frame, or in frames called by the current
11411 one. */
11412
11413 if (frame_id_p (caller_frame_id))
11414 {
11415 struct symtab_and_line sal2;
11416 struct gdbarch *caller_gdbarch;
11417
11418 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11419 sal2.pc = frame_unwind_caller_pc (frame);
11420 caller_gdbarch = frame_unwind_caller_arch (frame);
11421 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11422 sal2,
11423 caller_frame_id,
11424 bp_until);
11425 make_cleanup_delete_breakpoint (caller_breakpoint);
11426
11427 set_longjmp_breakpoint (tp, caller_frame_id);
11428 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11429 }
11430
11431 /* set_momentary_breakpoint could invalidate FRAME. */
11432 frame = NULL;
11433
11434 if (anywhere)
11435 /* If the user told us to continue until a specified location,
11436 we don't specify a frame at which we need to stop. */
11437 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11438 null_frame_id, bp_until);
11439 else
11440 /* Otherwise, specify the selected frame, because we want to stop
11441 only at the very same frame. */
11442 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11443 stack_frame_id, bp_until);
11444 make_cleanup_delete_breakpoint (location_breakpoint);
11445
11446 sm = new_until_break_fsm (command_interp (), tp->global_num,
11447 location_breakpoint, caller_breakpoint);
11448 tp->thread_fsm = &sm->thread_fsm;
11449
11450 discard_cleanups (old_chain);
11451
11452 proceed (-1, GDB_SIGNAL_DEFAULT);
11453 }
11454
11455 /* This function attempts to parse an optional "if <cond>" clause
11456 from the arg string. If one is not found, it returns NULL.
11457
11458 Else, it returns a pointer to the condition string. (It does not
11459 attempt to evaluate the string against a particular block.) And,
11460 it updates arg to point to the first character following the parsed
11461 if clause in the arg string. */
11462
11463 const char *
11464 ep_parse_optional_if_clause (const char **arg)
11465 {
11466 const char *cond_string;
11467
11468 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11469 return NULL;
11470
11471 /* Skip the "if" keyword. */
11472 (*arg) += 2;
11473
11474 /* Skip any extra leading whitespace, and record the start of the
11475 condition string. */
11476 *arg = skip_spaces (*arg);
11477 cond_string = *arg;
11478
11479 /* Assume that the condition occupies the remainder of the arg
11480 string. */
11481 (*arg) += strlen (cond_string);
11482
11483 return cond_string;
11484 }
11485
11486 /* Commands to deal with catching events, such as signals, exceptions,
11487 process start/exit, etc. */
11488
11489 typedef enum
11490 {
11491 catch_fork_temporary, catch_vfork_temporary,
11492 catch_fork_permanent, catch_vfork_permanent
11493 }
11494 catch_fork_kind;
11495
11496 static void
11497 catch_fork_command_1 (char *arg_entry, int from_tty,
11498 struct cmd_list_element *command)
11499 {
11500 const char *arg = arg_entry;
11501 struct gdbarch *gdbarch = get_current_arch ();
11502 const char *cond_string = NULL;
11503 catch_fork_kind fork_kind;
11504 int tempflag;
11505
11506 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11507 tempflag = (fork_kind == catch_fork_temporary
11508 || fork_kind == catch_vfork_temporary);
11509
11510 if (!arg)
11511 arg = "";
11512 arg = skip_spaces (arg);
11513
11514 /* The allowed syntax is:
11515 catch [v]fork
11516 catch [v]fork if <cond>
11517
11518 First, check if there's an if clause. */
11519 cond_string = ep_parse_optional_if_clause (&arg);
11520
11521 if ((*arg != '\0') && !isspace (*arg))
11522 error (_("Junk at end of arguments."));
11523
11524 /* If this target supports it, create a fork or vfork catchpoint
11525 and enable reporting of such events. */
11526 switch (fork_kind)
11527 {
11528 case catch_fork_temporary:
11529 case catch_fork_permanent:
11530 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11531 &catch_fork_breakpoint_ops);
11532 break;
11533 case catch_vfork_temporary:
11534 case catch_vfork_permanent:
11535 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11536 &catch_vfork_breakpoint_ops);
11537 break;
11538 default:
11539 error (_("unsupported or unknown fork kind; cannot catch it"));
11540 break;
11541 }
11542 }
11543
11544 static void
11545 catch_exec_command_1 (char *arg_entry, int from_tty,
11546 struct cmd_list_element *command)
11547 {
11548 const char *arg = arg_entry;
11549 struct gdbarch *gdbarch = get_current_arch ();
11550 int tempflag;
11551 const char *cond_string = NULL;
11552
11553 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11554
11555 if (!arg)
11556 arg = "";
11557 arg = skip_spaces (arg);
11558
11559 /* The allowed syntax is:
11560 catch exec
11561 catch exec if <cond>
11562
11563 First, check if there's an if clause. */
11564 cond_string = ep_parse_optional_if_clause (&arg);
11565
11566 if ((*arg != '\0') && !isspace (*arg))
11567 error (_("Junk at end of arguments."));
11568
11569 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11570 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11571 &catch_exec_breakpoint_ops);
11572 c->exec_pathname = NULL;
11573
11574 install_breakpoint (0, std::move (c), 1);
11575 }
11576
11577 void
11578 init_ada_exception_breakpoint (struct breakpoint *b,
11579 struct gdbarch *gdbarch,
11580 struct symtab_and_line sal,
11581 const char *addr_string,
11582 const struct breakpoint_ops *ops,
11583 int tempflag,
11584 int enabled,
11585 int from_tty)
11586 {
11587 if (from_tty)
11588 {
11589 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11590 if (!loc_gdbarch)
11591 loc_gdbarch = gdbarch;
11592
11593 describe_other_breakpoints (loc_gdbarch,
11594 sal.pspace, sal.pc, sal.section, -1);
11595 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11596 version for exception catchpoints, because two catchpoints
11597 used for different exception names will use the same address.
11598 In this case, a "breakpoint ... also set at..." warning is
11599 unproductive. Besides, the warning phrasing is also a bit
11600 inappropriate, we should use the word catchpoint, and tell
11601 the user what type of catchpoint it is. The above is good
11602 enough for now, though. */
11603 }
11604
11605 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11606
11607 b->enable_state = enabled ? bp_enabled : bp_disabled;
11608 b->disposition = tempflag ? disp_del : disp_donttouch;
11609 b->location = string_to_event_location (&addr_string,
11610 language_def (language_ada));
11611 b->language = language_ada;
11612 }
11613
11614 static void
11615 catch_command (char *arg, int from_tty)
11616 {
11617 error (_("Catch requires an event name."));
11618 }
11619 \f
11620
11621 static void
11622 tcatch_command (char *arg, int from_tty)
11623 {
11624 error (_("Catch requires an event name."));
11625 }
11626
11627 /* A qsort comparison function that sorts breakpoints in order. */
11628
11629 static int
11630 compare_breakpoints (const void *a, const void *b)
11631 {
11632 const breakpoint_p *ba = (const breakpoint_p *) a;
11633 uintptr_t ua = (uintptr_t) *ba;
11634 const breakpoint_p *bb = (const breakpoint_p *) b;
11635 uintptr_t ub = (uintptr_t) *bb;
11636
11637 if ((*ba)->number < (*bb)->number)
11638 return -1;
11639 else if ((*ba)->number > (*bb)->number)
11640 return 1;
11641
11642 /* Now sort by address, in case we see, e..g, two breakpoints with
11643 the number 0. */
11644 if (ua < ub)
11645 return -1;
11646 return ua > ub ? 1 : 0;
11647 }
11648
11649 /* Delete breakpoints by address or line. */
11650
11651 static void
11652 clear_command (char *arg, int from_tty)
11653 {
11654 struct breakpoint *b, *prev;
11655 VEC(breakpoint_p) *found = 0;
11656 int ix;
11657 int default_match;
11658 int i;
11659 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11660
11661 std::vector<symtab_and_line> decoded_sals;
11662 symtab_and_line last_sal;
11663 gdb::array_view<symtab_and_line> sals;
11664 if (arg)
11665 {
11666 decoded_sals
11667 = decode_line_with_current_source (arg,
11668 (DECODE_LINE_FUNFIRSTLINE
11669 | DECODE_LINE_LIST_MODE));
11670 default_match = 0;
11671 sals = decoded_sals;
11672 }
11673 else
11674 {
11675 /* Set sal's line, symtab, pc, and pspace to the values
11676 corresponding to the last call to print_frame_info. If the
11677 codepoint is not valid, this will set all the fields to 0. */
11678 last_sal = get_last_displayed_sal ();
11679 if (last_sal.symtab == 0)
11680 error (_("No source file specified."));
11681
11682 default_match = 1;
11683 sals = last_sal;
11684 }
11685
11686 /* We don't call resolve_sal_pc here. That's not as bad as it
11687 seems, because all existing breakpoints typically have both
11688 file/line and pc set. So, if clear is given file/line, we can
11689 match this to existing breakpoint without obtaining pc at all.
11690
11691 We only support clearing given the address explicitly
11692 present in breakpoint table. Say, we've set breakpoint
11693 at file:line. There were several PC values for that file:line,
11694 due to optimization, all in one block.
11695
11696 We've picked one PC value. If "clear" is issued with another
11697 PC corresponding to the same file:line, the breakpoint won't
11698 be cleared. We probably can still clear the breakpoint, but
11699 since the other PC value is never presented to user, user
11700 can only find it by guessing, and it does not seem important
11701 to support that. */
11702
11703 /* For each line spec given, delete bps which correspond to it. Do
11704 it in two passes, solely to preserve the current behavior that
11705 from_tty is forced true if we delete more than one
11706 breakpoint. */
11707
11708 found = NULL;
11709 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11710 for (const auto &sal : sals)
11711 {
11712 const char *sal_fullname;
11713
11714 /* If exact pc given, clear bpts at that pc.
11715 If line given (pc == 0), clear all bpts on specified line.
11716 If defaulting, clear all bpts on default line
11717 or at default pc.
11718
11719 defaulting sal.pc != 0 tests to do
11720
11721 0 1 pc
11722 1 1 pc _and_ line
11723 0 0 line
11724 1 0 <can't happen> */
11725
11726 sal_fullname = (sal.symtab == NULL
11727 ? NULL : symtab_to_fullname (sal.symtab));
11728
11729 /* Find all matching breakpoints and add them to 'found'. */
11730 ALL_BREAKPOINTS (b)
11731 {
11732 int match = 0;
11733 /* Are we going to delete b? */
11734 if (b->type != bp_none && !is_watchpoint (b))
11735 {
11736 struct bp_location *loc = b->loc;
11737 for (; loc; loc = loc->next)
11738 {
11739 /* If the user specified file:line, don't allow a PC
11740 match. This matches historical gdb behavior. */
11741 int pc_match = (!sal.explicit_line
11742 && sal.pc
11743 && (loc->pspace == sal.pspace)
11744 && (loc->address == sal.pc)
11745 && (!section_is_overlay (loc->section)
11746 || loc->section == sal.section));
11747 int line_match = 0;
11748
11749 if ((default_match || sal.explicit_line)
11750 && loc->symtab != NULL
11751 && sal_fullname != NULL
11752 && sal.pspace == loc->pspace
11753 && loc->line_number == sal.line
11754 && filename_cmp (symtab_to_fullname (loc->symtab),
11755 sal_fullname) == 0)
11756 line_match = 1;
11757
11758 if (pc_match || line_match)
11759 {
11760 match = 1;
11761 break;
11762 }
11763 }
11764 }
11765
11766 if (match)
11767 VEC_safe_push(breakpoint_p, found, b);
11768 }
11769 }
11770
11771 /* Now go thru the 'found' chain and delete them. */
11772 if (VEC_empty(breakpoint_p, found))
11773 {
11774 if (arg)
11775 error (_("No breakpoint at %s."), arg);
11776 else
11777 error (_("No breakpoint at this line."));
11778 }
11779
11780 /* Remove duplicates from the vec. */
11781 qsort (VEC_address (breakpoint_p, found),
11782 VEC_length (breakpoint_p, found),
11783 sizeof (breakpoint_p),
11784 compare_breakpoints);
11785 prev = VEC_index (breakpoint_p, found, 0);
11786 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11787 {
11788 if (b == prev)
11789 {
11790 VEC_ordered_remove (breakpoint_p, found, ix);
11791 --ix;
11792 }
11793 }
11794
11795 if (VEC_length(breakpoint_p, found) > 1)
11796 from_tty = 1; /* Always report if deleted more than one. */
11797 if (from_tty)
11798 {
11799 if (VEC_length(breakpoint_p, found) == 1)
11800 printf_unfiltered (_("Deleted breakpoint "));
11801 else
11802 printf_unfiltered (_("Deleted breakpoints "));
11803 }
11804
11805 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11806 {
11807 if (from_tty)
11808 printf_unfiltered ("%d ", b->number);
11809 delete_breakpoint (b);
11810 }
11811 if (from_tty)
11812 putchar_unfiltered ('\n');
11813
11814 do_cleanups (cleanups);
11815 }
11816 \f
11817 /* Delete breakpoint in BS if they are `delete' breakpoints and
11818 all breakpoints that are marked for deletion, whether hit or not.
11819 This is called after any breakpoint is hit, or after errors. */
11820
11821 void
11822 breakpoint_auto_delete (bpstat bs)
11823 {
11824 struct breakpoint *b, *b_tmp;
11825
11826 for (; bs; bs = bs->next)
11827 if (bs->breakpoint_at
11828 && bs->breakpoint_at->disposition == disp_del
11829 && bs->stop)
11830 delete_breakpoint (bs->breakpoint_at);
11831
11832 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11833 {
11834 if (b->disposition == disp_del_at_next_stop)
11835 delete_breakpoint (b);
11836 }
11837 }
11838
11839 /* A comparison function for bp_location AP and BP being interfaced to
11840 qsort. Sort elements primarily by their ADDRESS (no matter what
11841 does breakpoint_address_is_meaningful say for its OWNER),
11842 secondarily by ordering first permanent elements and
11843 terciarily just ensuring the array is sorted stable way despite
11844 qsort being an unstable algorithm. */
11845
11846 static int
11847 bp_locations_compare (const void *ap, const void *bp)
11848 {
11849 const struct bp_location *a = *(const struct bp_location **) ap;
11850 const struct bp_location *b = *(const struct bp_location **) bp;
11851
11852 if (a->address != b->address)
11853 return (a->address > b->address) - (a->address < b->address);
11854
11855 /* Sort locations at the same address by their pspace number, keeping
11856 locations of the same inferior (in a multi-inferior environment)
11857 grouped. */
11858
11859 if (a->pspace->num != b->pspace->num)
11860 return ((a->pspace->num > b->pspace->num)
11861 - (a->pspace->num < b->pspace->num));
11862
11863 /* Sort permanent breakpoints first. */
11864 if (a->permanent != b->permanent)
11865 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11866
11867 /* Make the internal GDB representation stable across GDB runs
11868 where A and B memory inside GDB can differ. Breakpoint locations of
11869 the same type at the same address can be sorted in arbitrary order. */
11870
11871 if (a->owner->number != b->owner->number)
11872 return ((a->owner->number > b->owner->number)
11873 - (a->owner->number < b->owner->number));
11874
11875 return (a > b) - (a < b);
11876 }
11877
11878 /* Set bp_locations_placed_address_before_address_max and
11879 bp_locations_shadow_len_after_address_max according to the current
11880 content of the bp_locations array. */
11881
11882 static void
11883 bp_locations_target_extensions_update (void)
11884 {
11885 struct bp_location *bl, **blp_tmp;
11886
11887 bp_locations_placed_address_before_address_max = 0;
11888 bp_locations_shadow_len_after_address_max = 0;
11889
11890 ALL_BP_LOCATIONS (bl, blp_tmp)
11891 {
11892 CORE_ADDR start, end, addr;
11893
11894 if (!bp_location_has_shadow (bl))
11895 continue;
11896
11897 start = bl->target_info.placed_address;
11898 end = start + bl->target_info.shadow_len;
11899
11900 gdb_assert (bl->address >= start);
11901 addr = bl->address - start;
11902 if (addr > bp_locations_placed_address_before_address_max)
11903 bp_locations_placed_address_before_address_max = addr;
11904
11905 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11906
11907 gdb_assert (bl->address < end);
11908 addr = end - bl->address;
11909 if (addr > bp_locations_shadow_len_after_address_max)
11910 bp_locations_shadow_len_after_address_max = addr;
11911 }
11912 }
11913
11914 /* Download tracepoint locations if they haven't been. */
11915
11916 static void
11917 download_tracepoint_locations (void)
11918 {
11919 struct breakpoint *b;
11920 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11921
11922 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11923
11924 ALL_TRACEPOINTS (b)
11925 {
11926 struct bp_location *bl;
11927 struct tracepoint *t;
11928 int bp_location_downloaded = 0;
11929
11930 if ((b->type == bp_fast_tracepoint
11931 ? !may_insert_fast_tracepoints
11932 : !may_insert_tracepoints))
11933 continue;
11934
11935 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11936 {
11937 if (target_can_download_tracepoint ())
11938 can_download_tracepoint = TRIBOOL_TRUE;
11939 else
11940 can_download_tracepoint = TRIBOOL_FALSE;
11941 }
11942
11943 if (can_download_tracepoint == TRIBOOL_FALSE)
11944 break;
11945
11946 for (bl = b->loc; bl; bl = bl->next)
11947 {
11948 /* In tracepoint, locations are _never_ duplicated, so
11949 should_be_inserted is equivalent to
11950 unduplicated_should_be_inserted. */
11951 if (!should_be_inserted (bl) || bl->inserted)
11952 continue;
11953
11954 switch_to_program_space_and_thread (bl->pspace);
11955
11956 target_download_tracepoint (bl);
11957
11958 bl->inserted = 1;
11959 bp_location_downloaded = 1;
11960 }
11961 t = (struct tracepoint *) b;
11962 t->number_on_target = b->number;
11963 if (bp_location_downloaded)
11964 observer_notify_breakpoint_modified (b);
11965 }
11966 }
11967
11968 /* Swap the insertion/duplication state between two locations. */
11969
11970 static void
11971 swap_insertion (struct bp_location *left, struct bp_location *right)
11972 {
11973 const int left_inserted = left->inserted;
11974 const int left_duplicate = left->duplicate;
11975 const int left_needs_update = left->needs_update;
11976 const struct bp_target_info left_target_info = left->target_info;
11977
11978 /* Locations of tracepoints can never be duplicated. */
11979 if (is_tracepoint (left->owner))
11980 gdb_assert (!left->duplicate);
11981 if (is_tracepoint (right->owner))
11982 gdb_assert (!right->duplicate);
11983
11984 left->inserted = right->inserted;
11985 left->duplicate = right->duplicate;
11986 left->needs_update = right->needs_update;
11987 left->target_info = right->target_info;
11988 right->inserted = left_inserted;
11989 right->duplicate = left_duplicate;
11990 right->needs_update = left_needs_update;
11991 right->target_info = left_target_info;
11992 }
11993
11994 /* Force the re-insertion of the locations at ADDRESS. This is called
11995 once a new/deleted/modified duplicate location is found and we are evaluating
11996 conditions on the target's side. Such conditions need to be updated on
11997 the target. */
11998
11999 static void
12000 force_breakpoint_reinsertion (struct bp_location *bl)
12001 {
12002 struct bp_location **locp = NULL, **loc2p;
12003 struct bp_location *loc;
12004 CORE_ADDR address = 0;
12005 int pspace_num;
12006
12007 address = bl->address;
12008 pspace_num = bl->pspace->num;
12009
12010 /* This is only meaningful if the target is
12011 evaluating conditions and if the user has
12012 opted for condition evaluation on the target's
12013 side. */
12014 if (gdb_evaluates_breakpoint_condition_p ()
12015 || !target_supports_evaluation_of_breakpoint_conditions ())
12016 return;
12017
12018 /* Flag all breakpoint locations with this address and
12019 the same program space as the location
12020 as "its condition has changed". We need to
12021 update the conditions on the target's side. */
12022 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12023 {
12024 loc = *loc2p;
12025
12026 if (!is_breakpoint (loc->owner)
12027 || pspace_num != loc->pspace->num)
12028 continue;
12029
12030 /* Flag the location appropriately. We use a different state to
12031 let everyone know that we already updated the set of locations
12032 with addr bl->address and program space bl->pspace. This is so
12033 we don't have to keep calling these functions just to mark locations
12034 that have already been marked. */
12035 loc->condition_changed = condition_updated;
12036
12037 /* Free the agent expression bytecode as well. We will compute
12038 it later on. */
12039 loc->cond_bytecode.reset ();
12040 }
12041 }
12042 /* Called whether new breakpoints are created, or existing breakpoints
12043 deleted, to update the global location list and recompute which
12044 locations are duplicate of which.
12045
12046 The INSERT_MODE flag determines whether locations may not, may, or
12047 shall be inserted now. See 'enum ugll_insert_mode' for more
12048 info. */
12049
12050 static void
12051 update_global_location_list (enum ugll_insert_mode insert_mode)
12052 {
12053 struct breakpoint *b;
12054 struct bp_location **locp, *loc;
12055 struct cleanup *cleanups;
12056 /* Last breakpoint location address that was marked for update. */
12057 CORE_ADDR last_addr = 0;
12058 /* Last breakpoint location program space that was marked for update. */
12059 int last_pspace_num = -1;
12060
12061 /* Used in the duplicates detection below. When iterating over all
12062 bp_locations, points to the first bp_location of a given address.
12063 Breakpoints and watchpoints of different types are never
12064 duplicates of each other. Keep one pointer for each type of
12065 breakpoint/watchpoint, so we only need to loop over all locations
12066 once. */
12067 struct bp_location *bp_loc_first; /* breakpoint */
12068 struct bp_location *wp_loc_first; /* hardware watchpoint */
12069 struct bp_location *awp_loc_first; /* access watchpoint */
12070 struct bp_location *rwp_loc_first; /* read watchpoint */
12071
12072 /* Saved former bp_locations array which we compare against the newly
12073 built bp_locations from the current state of ALL_BREAKPOINTS. */
12074 struct bp_location **old_locations, **old_locp;
12075 unsigned old_locations_count;
12076
12077 old_locations = bp_locations;
12078 old_locations_count = bp_locations_count;
12079 bp_locations = NULL;
12080 bp_locations_count = 0;
12081 cleanups = make_cleanup (xfree, old_locations);
12082
12083 ALL_BREAKPOINTS (b)
12084 for (loc = b->loc; loc; loc = loc->next)
12085 bp_locations_count++;
12086
12087 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12088 locp = bp_locations;
12089 ALL_BREAKPOINTS (b)
12090 for (loc = b->loc; loc; loc = loc->next)
12091 *locp++ = loc;
12092 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12093 bp_locations_compare);
12094
12095 bp_locations_target_extensions_update ();
12096
12097 /* Identify bp_location instances that are no longer present in the
12098 new list, and therefore should be freed. Note that it's not
12099 necessary that those locations should be removed from inferior --
12100 if there's another location at the same address (previously
12101 marked as duplicate), we don't need to remove/insert the
12102 location.
12103
12104 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12105 and former bp_location array state respectively. */
12106
12107 locp = bp_locations;
12108 for (old_locp = old_locations;
12109 old_locp < old_locations + old_locations_count;
12110 old_locp++)
12111 {
12112 struct bp_location *old_loc = *old_locp;
12113 struct bp_location **loc2p;
12114
12115 /* Tells if 'old_loc' is found among the new locations. If
12116 not, we have to free it. */
12117 int found_object = 0;
12118 /* Tells if the location should remain inserted in the target. */
12119 int keep_in_target = 0;
12120 int removed = 0;
12121
12122 /* Skip LOCP entries which will definitely never be needed.
12123 Stop either at or being the one matching OLD_LOC. */
12124 while (locp < bp_locations + bp_locations_count
12125 && (*locp)->address < old_loc->address)
12126 locp++;
12127
12128 for (loc2p = locp;
12129 (loc2p < bp_locations + bp_locations_count
12130 && (*loc2p)->address == old_loc->address);
12131 loc2p++)
12132 {
12133 /* Check if this is a new/duplicated location or a duplicated
12134 location that had its condition modified. If so, we want to send
12135 its condition to the target if evaluation of conditions is taking
12136 place there. */
12137 if ((*loc2p)->condition_changed == condition_modified
12138 && (last_addr != old_loc->address
12139 || last_pspace_num != old_loc->pspace->num))
12140 {
12141 force_breakpoint_reinsertion (*loc2p);
12142 last_pspace_num = old_loc->pspace->num;
12143 }
12144
12145 if (*loc2p == old_loc)
12146 found_object = 1;
12147 }
12148
12149 /* We have already handled this address, update it so that we don't
12150 have to go through updates again. */
12151 last_addr = old_loc->address;
12152
12153 /* Target-side condition evaluation: Handle deleted locations. */
12154 if (!found_object)
12155 force_breakpoint_reinsertion (old_loc);
12156
12157 /* If this location is no longer present, and inserted, look if
12158 there's maybe a new location at the same address. If so,
12159 mark that one inserted, and don't remove this one. This is
12160 needed so that we don't have a time window where a breakpoint
12161 at certain location is not inserted. */
12162
12163 if (old_loc->inserted)
12164 {
12165 /* If the location is inserted now, we might have to remove
12166 it. */
12167
12168 if (found_object && should_be_inserted (old_loc))
12169 {
12170 /* The location is still present in the location list,
12171 and still should be inserted. Don't do anything. */
12172 keep_in_target = 1;
12173 }
12174 else
12175 {
12176 /* This location still exists, but it won't be kept in the
12177 target since it may have been disabled. We proceed to
12178 remove its target-side condition. */
12179
12180 /* The location is either no longer present, or got
12181 disabled. See if there's another location at the
12182 same address, in which case we don't need to remove
12183 this one from the target. */
12184
12185 /* OLD_LOC comes from existing struct breakpoint. */
12186 if (breakpoint_address_is_meaningful (old_loc->owner))
12187 {
12188 for (loc2p = locp;
12189 (loc2p < bp_locations + bp_locations_count
12190 && (*loc2p)->address == old_loc->address);
12191 loc2p++)
12192 {
12193 struct bp_location *loc2 = *loc2p;
12194
12195 if (breakpoint_locations_match (loc2, old_loc))
12196 {
12197 /* Read watchpoint locations are switched to
12198 access watchpoints, if the former are not
12199 supported, but the latter are. */
12200 if (is_hardware_watchpoint (old_loc->owner))
12201 {
12202 gdb_assert (is_hardware_watchpoint (loc2->owner));
12203 loc2->watchpoint_type = old_loc->watchpoint_type;
12204 }
12205
12206 /* loc2 is a duplicated location. We need to check
12207 if it should be inserted in case it will be
12208 unduplicated. */
12209 if (loc2 != old_loc
12210 && unduplicated_should_be_inserted (loc2))
12211 {
12212 swap_insertion (old_loc, loc2);
12213 keep_in_target = 1;
12214 break;
12215 }
12216 }
12217 }
12218 }
12219 }
12220
12221 if (!keep_in_target)
12222 {
12223 if (remove_breakpoint (old_loc))
12224 {
12225 /* This is just about all we can do. We could keep
12226 this location on the global list, and try to
12227 remove it next time, but there's no particular
12228 reason why we will succeed next time.
12229
12230 Note that at this point, old_loc->owner is still
12231 valid, as delete_breakpoint frees the breakpoint
12232 only after calling us. */
12233 printf_filtered (_("warning: Error removing "
12234 "breakpoint %d\n"),
12235 old_loc->owner->number);
12236 }
12237 removed = 1;
12238 }
12239 }
12240
12241 if (!found_object)
12242 {
12243 if (removed && target_is_non_stop_p ()
12244 && need_moribund_for_location_type (old_loc))
12245 {
12246 /* This location was removed from the target. In
12247 non-stop mode, a race condition is possible where
12248 we've removed a breakpoint, but stop events for that
12249 breakpoint are already queued and will arrive later.
12250 We apply an heuristic to be able to distinguish such
12251 SIGTRAPs from other random SIGTRAPs: we keep this
12252 breakpoint location for a bit, and will retire it
12253 after we see some number of events. The theory here
12254 is that reporting of events should, "on the average",
12255 be fair, so after a while we'll see events from all
12256 threads that have anything of interest, and no longer
12257 need to keep this breakpoint location around. We
12258 don't hold locations forever so to reduce chances of
12259 mistaking a non-breakpoint SIGTRAP for a breakpoint
12260 SIGTRAP.
12261
12262 The heuristic failing can be disastrous on
12263 decr_pc_after_break targets.
12264
12265 On decr_pc_after_break targets, like e.g., x86-linux,
12266 if we fail to recognize a late breakpoint SIGTRAP,
12267 because events_till_retirement has reached 0 too
12268 soon, we'll fail to do the PC adjustment, and report
12269 a random SIGTRAP to the user. When the user resumes
12270 the inferior, it will most likely immediately crash
12271 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12272 corrupted, because of being resumed e.g., in the
12273 middle of a multi-byte instruction, or skipped a
12274 one-byte instruction. This was actually seen happen
12275 on native x86-linux, and should be less rare on
12276 targets that do not support new thread events, like
12277 remote, due to the heuristic depending on
12278 thread_count.
12279
12280 Mistaking a random SIGTRAP for a breakpoint trap
12281 causes similar symptoms (PC adjustment applied when
12282 it shouldn't), but then again, playing with SIGTRAPs
12283 behind the debugger's back is asking for trouble.
12284
12285 Since hardware watchpoint traps are always
12286 distinguishable from other traps, so we don't need to
12287 apply keep hardware watchpoint moribund locations
12288 around. We simply always ignore hardware watchpoint
12289 traps we can no longer explain. */
12290
12291 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12292 old_loc->owner = NULL;
12293
12294 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12295 }
12296 else
12297 {
12298 old_loc->owner = NULL;
12299 decref_bp_location (&old_loc);
12300 }
12301 }
12302 }
12303
12304 /* Rescan breakpoints at the same address and section, marking the
12305 first one as "first" and any others as "duplicates". This is so
12306 that the bpt instruction is only inserted once. If we have a
12307 permanent breakpoint at the same place as BPT, make that one the
12308 official one, and the rest as duplicates. Permanent breakpoints
12309 are sorted first for the same address.
12310
12311 Do the same for hardware watchpoints, but also considering the
12312 watchpoint's type (regular/access/read) and length. */
12313
12314 bp_loc_first = NULL;
12315 wp_loc_first = NULL;
12316 awp_loc_first = NULL;
12317 rwp_loc_first = NULL;
12318 ALL_BP_LOCATIONS (loc, locp)
12319 {
12320 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12321 non-NULL. */
12322 struct bp_location **loc_first_p;
12323 b = loc->owner;
12324
12325 if (!unduplicated_should_be_inserted (loc)
12326 || !breakpoint_address_is_meaningful (b)
12327 /* Don't detect duplicate for tracepoint locations because they are
12328 never duplicated. See the comments in field `duplicate' of
12329 `struct bp_location'. */
12330 || is_tracepoint (b))
12331 {
12332 /* Clear the condition modification flag. */
12333 loc->condition_changed = condition_unchanged;
12334 continue;
12335 }
12336
12337 if (b->type == bp_hardware_watchpoint)
12338 loc_first_p = &wp_loc_first;
12339 else if (b->type == bp_read_watchpoint)
12340 loc_first_p = &rwp_loc_first;
12341 else if (b->type == bp_access_watchpoint)
12342 loc_first_p = &awp_loc_first;
12343 else
12344 loc_first_p = &bp_loc_first;
12345
12346 if (*loc_first_p == NULL
12347 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12348 || !breakpoint_locations_match (loc, *loc_first_p))
12349 {
12350 *loc_first_p = loc;
12351 loc->duplicate = 0;
12352
12353 if (is_breakpoint (loc->owner) && loc->condition_changed)
12354 {
12355 loc->needs_update = 1;
12356 /* Clear the condition modification flag. */
12357 loc->condition_changed = condition_unchanged;
12358 }
12359 continue;
12360 }
12361
12362
12363 /* This and the above ensure the invariant that the first location
12364 is not duplicated, and is the inserted one.
12365 All following are marked as duplicated, and are not inserted. */
12366 if (loc->inserted)
12367 swap_insertion (loc, *loc_first_p);
12368 loc->duplicate = 1;
12369
12370 /* Clear the condition modification flag. */
12371 loc->condition_changed = condition_unchanged;
12372 }
12373
12374 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12375 {
12376 if (insert_mode != UGLL_DONT_INSERT)
12377 insert_breakpoint_locations ();
12378 else
12379 {
12380 /* Even though the caller told us to not insert new
12381 locations, we may still need to update conditions on the
12382 target's side of breakpoints that were already inserted
12383 if the target is evaluating breakpoint conditions. We
12384 only update conditions for locations that are marked
12385 "needs_update". */
12386 update_inserted_breakpoint_locations ();
12387 }
12388 }
12389
12390 if (insert_mode != UGLL_DONT_INSERT)
12391 download_tracepoint_locations ();
12392
12393 do_cleanups (cleanups);
12394 }
12395
12396 void
12397 breakpoint_retire_moribund (void)
12398 {
12399 struct bp_location *loc;
12400 int ix;
12401
12402 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12403 if (--(loc->events_till_retirement) == 0)
12404 {
12405 decref_bp_location (&loc);
12406 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12407 --ix;
12408 }
12409 }
12410
12411 static void
12412 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12413 {
12414
12415 TRY
12416 {
12417 update_global_location_list (insert_mode);
12418 }
12419 CATCH (e, RETURN_MASK_ERROR)
12420 {
12421 }
12422 END_CATCH
12423 }
12424
12425 /* Clear BKP from a BPS. */
12426
12427 static void
12428 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12429 {
12430 bpstat bs;
12431
12432 for (bs = bps; bs; bs = bs->next)
12433 if (bs->breakpoint_at == bpt)
12434 {
12435 bs->breakpoint_at = NULL;
12436 bs->old_val = NULL;
12437 /* bs->commands will be freed later. */
12438 }
12439 }
12440
12441 /* Callback for iterate_over_threads. */
12442 static int
12443 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12444 {
12445 struct breakpoint *bpt = (struct breakpoint *) data;
12446
12447 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12448 return 0;
12449 }
12450
12451 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12452 callbacks. */
12453
12454 static void
12455 say_where (struct breakpoint *b)
12456 {
12457 struct value_print_options opts;
12458
12459 get_user_print_options (&opts);
12460
12461 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12462 single string. */
12463 if (b->loc == NULL)
12464 {
12465 /* For pending locations, the output differs slightly based
12466 on b->extra_string. If this is non-NULL, it contains either
12467 a condition or dprintf arguments. */
12468 if (b->extra_string == NULL)
12469 {
12470 printf_filtered (_(" (%s) pending."),
12471 event_location_to_string (b->location.get ()));
12472 }
12473 else if (b->type == bp_dprintf)
12474 {
12475 printf_filtered (_(" (%s,%s) pending."),
12476 event_location_to_string (b->location.get ()),
12477 b->extra_string);
12478 }
12479 else
12480 {
12481 printf_filtered (_(" (%s %s) pending."),
12482 event_location_to_string (b->location.get ()),
12483 b->extra_string);
12484 }
12485 }
12486 else
12487 {
12488 if (opts.addressprint || b->loc->symtab == NULL)
12489 {
12490 printf_filtered (" at ");
12491 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12492 gdb_stdout);
12493 }
12494 if (b->loc->symtab != NULL)
12495 {
12496 /* If there is a single location, we can print the location
12497 more nicely. */
12498 if (b->loc->next == NULL)
12499 printf_filtered (": file %s, line %d.",
12500 symtab_to_filename_for_display (b->loc->symtab),
12501 b->loc->line_number);
12502 else
12503 /* This is not ideal, but each location may have a
12504 different file name, and this at least reflects the
12505 real situation somewhat. */
12506 printf_filtered (": %s.",
12507 event_location_to_string (b->location.get ()));
12508 }
12509
12510 if (b->loc->next)
12511 {
12512 struct bp_location *loc = b->loc;
12513 int n = 0;
12514 for (; loc; loc = loc->next)
12515 ++n;
12516 printf_filtered (" (%d locations)", n);
12517 }
12518 }
12519 }
12520
12521 /* Default bp_location_ops methods. */
12522
12523 static void
12524 bp_location_dtor (struct bp_location *self)
12525 {
12526 xfree (self->function_name);
12527 }
12528
12529 static const struct bp_location_ops bp_location_ops =
12530 {
12531 bp_location_dtor
12532 };
12533
12534 /* Destructor for the breakpoint base class. */
12535
12536 breakpoint::~breakpoint ()
12537 {
12538 xfree (this->cond_string);
12539 xfree (this->extra_string);
12540 xfree (this->filter);
12541 }
12542
12543 static struct bp_location *
12544 base_breakpoint_allocate_location (struct breakpoint *self)
12545 {
12546 return new bp_location (&bp_location_ops, self);
12547 }
12548
12549 static void
12550 base_breakpoint_re_set (struct breakpoint *b)
12551 {
12552 /* Nothing to re-set. */
12553 }
12554
12555 #define internal_error_pure_virtual_called() \
12556 gdb_assert_not_reached ("pure virtual function called")
12557
12558 static int
12559 base_breakpoint_insert_location (struct bp_location *bl)
12560 {
12561 internal_error_pure_virtual_called ();
12562 }
12563
12564 static int
12565 base_breakpoint_remove_location (struct bp_location *bl,
12566 enum remove_bp_reason reason)
12567 {
12568 internal_error_pure_virtual_called ();
12569 }
12570
12571 static int
12572 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12573 struct address_space *aspace,
12574 CORE_ADDR bp_addr,
12575 const struct target_waitstatus *ws)
12576 {
12577 internal_error_pure_virtual_called ();
12578 }
12579
12580 static void
12581 base_breakpoint_check_status (bpstat bs)
12582 {
12583 /* Always stop. */
12584 }
12585
12586 /* A "works_in_software_mode" breakpoint_ops method that just internal
12587 errors. */
12588
12589 static int
12590 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12591 {
12592 internal_error_pure_virtual_called ();
12593 }
12594
12595 /* A "resources_needed" breakpoint_ops method that just internal
12596 errors. */
12597
12598 static int
12599 base_breakpoint_resources_needed (const struct bp_location *bl)
12600 {
12601 internal_error_pure_virtual_called ();
12602 }
12603
12604 static enum print_stop_action
12605 base_breakpoint_print_it (bpstat bs)
12606 {
12607 internal_error_pure_virtual_called ();
12608 }
12609
12610 static void
12611 base_breakpoint_print_one_detail (const struct breakpoint *self,
12612 struct ui_out *uiout)
12613 {
12614 /* nothing */
12615 }
12616
12617 static void
12618 base_breakpoint_print_mention (struct breakpoint *b)
12619 {
12620 internal_error_pure_virtual_called ();
12621 }
12622
12623 static void
12624 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12625 {
12626 internal_error_pure_virtual_called ();
12627 }
12628
12629 static void
12630 base_breakpoint_create_sals_from_location
12631 (const struct event_location *location,
12632 struct linespec_result *canonical,
12633 enum bptype type_wanted)
12634 {
12635 internal_error_pure_virtual_called ();
12636 }
12637
12638 static void
12639 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12640 struct linespec_result *c,
12641 gdb::unique_xmalloc_ptr<char> cond_string,
12642 gdb::unique_xmalloc_ptr<char> extra_string,
12643 enum bptype type_wanted,
12644 enum bpdisp disposition,
12645 int thread,
12646 int task, int ignore_count,
12647 const struct breakpoint_ops *o,
12648 int from_tty, int enabled,
12649 int internal, unsigned flags)
12650 {
12651 internal_error_pure_virtual_called ();
12652 }
12653
12654 static std::vector<symtab_and_line>
12655 base_breakpoint_decode_location (struct breakpoint *b,
12656 const struct event_location *location,
12657 struct program_space *search_pspace)
12658 {
12659 internal_error_pure_virtual_called ();
12660 }
12661
12662 /* The default 'explains_signal' method. */
12663
12664 static int
12665 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12666 {
12667 return 1;
12668 }
12669
12670 /* The default "after_condition_true" method. */
12671
12672 static void
12673 base_breakpoint_after_condition_true (struct bpstats *bs)
12674 {
12675 /* Nothing to do. */
12676 }
12677
12678 struct breakpoint_ops base_breakpoint_ops =
12679 {
12680 base_breakpoint_allocate_location,
12681 base_breakpoint_re_set,
12682 base_breakpoint_insert_location,
12683 base_breakpoint_remove_location,
12684 base_breakpoint_breakpoint_hit,
12685 base_breakpoint_check_status,
12686 base_breakpoint_resources_needed,
12687 base_breakpoint_works_in_software_mode,
12688 base_breakpoint_print_it,
12689 NULL,
12690 base_breakpoint_print_one_detail,
12691 base_breakpoint_print_mention,
12692 base_breakpoint_print_recreate,
12693 base_breakpoint_create_sals_from_location,
12694 base_breakpoint_create_breakpoints_sal,
12695 base_breakpoint_decode_location,
12696 base_breakpoint_explains_signal,
12697 base_breakpoint_after_condition_true,
12698 };
12699
12700 /* Default breakpoint_ops methods. */
12701
12702 static void
12703 bkpt_re_set (struct breakpoint *b)
12704 {
12705 /* FIXME: is this still reachable? */
12706 if (breakpoint_event_location_empty_p (b))
12707 {
12708 /* Anything without a location can't be re-set. */
12709 delete_breakpoint (b);
12710 return;
12711 }
12712
12713 breakpoint_re_set_default (b);
12714 }
12715
12716 static int
12717 bkpt_insert_location (struct bp_location *bl)
12718 {
12719 CORE_ADDR addr = bl->target_info.reqstd_address;
12720
12721 bl->target_info.kind = breakpoint_kind (bl, &addr);
12722 bl->target_info.placed_address = addr;
12723
12724 if (bl->loc_type == bp_loc_hardware_breakpoint)
12725 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12726 else
12727 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12728 }
12729
12730 static int
12731 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12732 {
12733 if (bl->loc_type == bp_loc_hardware_breakpoint)
12734 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12735 else
12736 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12737 }
12738
12739 static int
12740 bkpt_breakpoint_hit (const struct bp_location *bl,
12741 struct address_space *aspace, CORE_ADDR bp_addr,
12742 const struct target_waitstatus *ws)
12743 {
12744 if (ws->kind != TARGET_WAITKIND_STOPPED
12745 || ws->value.sig != GDB_SIGNAL_TRAP)
12746 return 0;
12747
12748 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12749 aspace, bp_addr))
12750 return 0;
12751
12752 if (overlay_debugging /* unmapped overlay section */
12753 && section_is_overlay (bl->section)
12754 && !section_is_mapped (bl->section))
12755 return 0;
12756
12757 return 1;
12758 }
12759
12760 static int
12761 dprintf_breakpoint_hit (const struct bp_location *bl,
12762 struct address_space *aspace, CORE_ADDR bp_addr,
12763 const struct target_waitstatus *ws)
12764 {
12765 if (dprintf_style == dprintf_style_agent
12766 && target_can_run_breakpoint_commands ())
12767 {
12768 /* An agent-style dprintf never causes a stop. If we see a trap
12769 for this address it must be for a breakpoint that happens to
12770 be set at the same address. */
12771 return 0;
12772 }
12773
12774 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12775 }
12776
12777 static int
12778 bkpt_resources_needed (const struct bp_location *bl)
12779 {
12780 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12781
12782 return 1;
12783 }
12784
12785 static enum print_stop_action
12786 bkpt_print_it (bpstat bs)
12787 {
12788 struct breakpoint *b;
12789 const struct bp_location *bl;
12790 int bp_temp;
12791 struct ui_out *uiout = current_uiout;
12792
12793 gdb_assert (bs->bp_location_at != NULL);
12794
12795 bl = bs->bp_location_at;
12796 b = bs->breakpoint_at;
12797
12798 bp_temp = b->disposition == disp_del;
12799 if (bl->address != bl->requested_address)
12800 breakpoint_adjustment_warning (bl->requested_address,
12801 bl->address,
12802 b->number, 1);
12803 annotate_breakpoint (b->number);
12804 maybe_print_thread_hit_breakpoint (uiout);
12805
12806 if (bp_temp)
12807 uiout->text ("Temporary breakpoint ");
12808 else
12809 uiout->text ("Breakpoint ");
12810 if (uiout->is_mi_like_p ())
12811 {
12812 uiout->field_string ("reason",
12813 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12814 uiout->field_string ("disp", bpdisp_text (b->disposition));
12815 }
12816 uiout->field_int ("bkptno", b->number);
12817 uiout->text (", ");
12818
12819 return PRINT_SRC_AND_LOC;
12820 }
12821
12822 static void
12823 bkpt_print_mention (struct breakpoint *b)
12824 {
12825 if (current_uiout->is_mi_like_p ())
12826 return;
12827
12828 switch (b->type)
12829 {
12830 case bp_breakpoint:
12831 case bp_gnu_ifunc_resolver:
12832 if (b->disposition == disp_del)
12833 printf_filtered (_("Temporary breakpoint"));
12834 else
12835 printf_filtered (_("Breakpoint"));
12836 printf_filtered (_(" %d"), b->number);
12837 if (b->type == bp_gnu_ifunc_resolver)
12838 printf_filtered (_(" at gnu-indirect-function resolver"));
12839 break;
12840 case bp_hardware_breakpoint:
12841 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12842 break;
12843 case bp_dprintf:
12844 printf_filtered (_("Dprintf %d"), b->number);
12845 break;
12846 }
12847
12848 say_where (b);
12849 }
12850
12851 static void
12852 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12853 {
12854 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12855 fprintf_unfiltered (fp, "tbreak");
12856 else if (tp->type == bp_breakpoint)
12857 fprintf_unfiltered (fp, "break");
12858 else if (tp->type == bp_hardware_breakpoint
12859 && tp->disposition == disp_del)
12860 fprintf_unfiltered (fp, "thbreak");
12861 else if (tp->type == bp_hardware_breakpoint)
12862 fprintf_unfiltered (fp, "hbreak");
12863 else
12864 internal_error (__FILE__, __LINE__,
12865 _("unhandled breakpoint type %d"), (int) tp->type);
12866
12867 fprintf_unfiltered (fp, " %s",
12868 event_location_to_string (tp->location.get ()));
12869
12870 /* Print out extra_string if this breakpoint is pending. It might
12871 contain, for example, conditions that were set by the user. */
12872 if (tp->loc == NULL && tp->extra_string != NULL)
12873 fprintf_unfiltered (fp, " %s", tp->extra_string);
12874
12875 print_recreate_thread (tp, fp);
12876 }
12877
12878 static void
12879 bkpt_create_sals_from_location (const struct event_location *location,
12880 struct linespec_result *canonical,
12881 enum bptype type_wanted)
12882 {
12883 create_sals_from_location_default (location, canonical, type_wanted);
12884 }
12885
12886 static void
12887 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12888 struct linespec_result *canonical,
12889 gdb::unique_xmalloc_ptr<char> cond_string,
12890 gdb::unique_xmalloc_ptr<char> extra_string,
12891 enum bptype type_wanted,
12892 enum bpdisp disposition,
12893 int thread,
12894 int task, int ignore_count,
12895 const struct breakpoint_ops *ops,
12896 int from_tty, int enabled,
12897 int internal, unsigned flags)
12898 {
12899 create_breakpoints_sal_default (gdbarch, canonical,
12900 std::move (cond_string),
12901 std::move (extra_string),
12902 type_wanted,
12903 disposition, thread, task,
12904 ignore_count, ops, from_tty,
12905 enabled, internal, flags);
12906 }
12907
12908 static std::vector<symtab_and_line>
12909 bkpt_decode_location (struct breakpoint *b,
12910 const struct event_location *location,
12911 struct program_space *search_pspace)
12912 {
12913 return decode_location_default (b, location, search_pspace);
12914 }
12915
12916 /* Virtual table for internal breakpoints. */
12917
12918 static void
12919 internal_bkpt_re_set (struct breakpoint *b)
12920 {
12921 switch (b->type)
12922 {
12923 /* Delete overlay event and longjmp master breakpoints; they
12924 will be reset later by breakpoint_re_set. */
12925 case bp_overlay_event:
12926 case bp_longjmp_master:
12927 case bp_std_terminate_master:
12928 case bp_exception_master:
12929 delete_breakpoint (b);
12930 break;
12931
12932 /* This breakpoint is special, it's set up when the inferior
12933 starts and we really don't want to touch it. */
12934 case bp_shlib_event:
12935
12936 /* Like bp_shlib_event, this breakpoint type is special. Once
12937 it is set up, we do not want to touch it. */
12938 case bp_thread_event:
12939 break;
12940 }
12941 }
12942
12943 static void
12944 internal_bkpt_check_status (bpstat bs)
12945 {
12946 if (bs->breakpoint_at->type == bp_shlib_event)
12947 {
12948 /* If requested, stop when the dynamic linker notifies GDB of
12949 events. This allows the user to get control and place
12950 breakpoints in initializer routines for dynamically loaded
12951 objects (among other things). */
12952 bs->stop = stop_on_solib_events;
12953 bs->print = stop_on_solib_events;
12954 }
12955 else
12956 bs->stop = 0;
12957 }
12958
12959 static enum print_stop_action
12960 internal_bkpt_print_it (bpstat bs)
12961 {
12962 struct breakpoint *b;
12963
12964 b = bs->breakpoint_at;
12965
12966 switch (b->type)
12967 {
12968 case bp_shlib_event:
12969 /* Did we stop because the user set the stop_on_solib_events
12970 variable? (If so, we report this as a generic, "Stopped due
12971 to shlib event" message.) */
12972 print_solib_event (0);
12973 break;
12974
12975 case bp_thread_event:
12976 /* Not sure how we will get here.
12977 GDB should not stop for these breakpoints. */
12978 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12979 break;
12980
12981 case bp_overlay_event:
12982 /* By analogy with the thread event, GDB should not stop for these. */
12983 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12984 break;
12985
12986 case bp_longjmp_master:
12987 /* These should never be enabled. */
12988 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12989 break;
12990
12991 case bp_std_terminate_master:
12992 /* These should never be enabled. */
12993 printf_filtered (_("std::terminate Master Breakpoint: "
12994 "gdb should not stop!\n"));
12995 break;
12996
12997 case bp_exception_master:
12998 /* These should never be enabled. */
12999 printf_filtered (_("Exception Master Breakpoint: "
13000 "gdb should not stop!\n"));
13001 break;
13002 }
13003
13004 return PRINT_NOTHING;
13005 }
13006
13007 static void
13008 internal_bkpt_print_mention (struct breakpoint *b)
13009 {
13010 /* Nothing to mention. These breakpoints are internal. */
13011 }
13012
13013 /* Virtual table for momentary breakpoints */
13014
13015 static void
13016 momentary_bkpt_re_set (struct breakpoint *b)
13017 {
13018 /* Keep temporary breakpoints, which can be encountered when we step
13019 over a dlopen call and solib_add is resetting the breakpoints.
13020 Otherwise these should have been blown away via the cleanup chain
13021 or by breakpoint_init_inferior when we rerun the executable. */
13022 }
13023
13024 static void
13025 momentary_bkpt_check_status (bpstat bs)
13026 {
13027 /* Nothing. The point of these breakpoints is causing a stop. */
13028 }
13029
13030 static enum print_stop_action
13031 momentary_bkpt_print_it (bpstat bs)
13032 {
13033 return PRINT_UNKNOWN;
13034 }
13035
13036 static void
13037 momentary_bkpt_print_mention (struct breakpoint *b)
13038 {
13039 /* Nothing to mention. These breakpoints are internal. */
13040 }
13041
13042 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13043
13044 It gets cleared already on the removal of the first one of such placed
13045 breakpoints. This is OK as they get all removed altogether. */
13046
13047 longjmp_breakpoint::~longjmp_breakpoint ()
13048 {
13049 thread_info *tp = find_thread_global_id (this->thread);
13050
13051 if (tp != NULL)
13052 tp->initiating_frame = null_frame_id;
13053 }
13054
13055 /* Specific methods for probe breakpoints. */
13056
13057 static int
13058 bkpt_probe_insert_location (struct bp_location *bl)
13059 {
13060 int v = bkpt_insert_location (bl);
13061
13062 if (v == 0)
13063 {
13064 /* The insertion was successful, now let's set the probe's semaphore
13065 if needed. */
13066 if (bl->probe.probe->pops->set_semaphore != NULL)
13067 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13068 bl->probe.objfile,
13069 bl->gdbarch);
13070 }
13071
13072 return v;
13073 }
13074
13075 static int
13076 bkpt_probe_remove_location (struct bp_location *bl,
13077 enum remove_bp_reason reason)
13078 {
13079 /* Let's clear the semaphore before removing the location. */
13080 if (bl->probe.probe->pops->clear_semaphore != NULL)
13081 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13082 bl->probe.objfile,
13083 bl->gdbarch);
13084
13085 return bkpt_remove_location (bl, reason);
13086 }
13087
13088 static void
13089 bkpt_probe_create_sals_from_location (const struct event_location *location,
13090 struct linespec_result *canonical,
13091 enum bptype type_wanted)
13092 {
13093 struct linespec_sals lsal;
13094
13095 lsal.sals = parse_probes (location, NULL, canonical);
13096 lsal.canonical
13097 = xstrdup (event_location_to_string (canonical->location.get ()));
13098 canonical->lsals.push_back (std::move (lsal));
13099 }
13100
13101 static std::vector<symtab_and_line>
13102 bkpt_probe_decode_location (struct breakpoint *b,
13103 const struct event_location *location,
13104 struct program_space *search_pspace)
13105 {
13106 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
13107 if (sals.empty ())
13108 error (_("probe not found"));
13109 return sals;
13110 }
13111
13112 /* The breakpoint_ops structure to be used in tracepoints. */
13113
13114 static void
13115 tracepoint_re_set (struct breakpoint *b)
13116 {
13117 breakpoint_re_set_default (b);
13118 }
13119
13120 static int
13121 tracepoint_breakpoint_hit (const struct bp_location *bl,
13122 struct address_space *aspace, CORE_ADDR bp_addr,
13123 const struct target_waitstatus *ws)
13124 {
13125 /* By definition, the inferior does not report stops at
13126 tracepoints. */
13127 return 0;
13128 }
13129
13130 static void
13131 tracepoint_print_one_detail (const struct breakpoint *self,
13132 struct ui_out *uiout)
13133 {
13134 struct tracepoint *tp = (struct tracepoint *) self;
13135 if (tp->static_trace_marker_id)
13136 {
13137 gdb_assert (self->type == bp_static_tracepoint);
13138
13139 uiout->text ("\tmarker id is ");
13140 uiout->field_string ("static-tracepoint-marker-string-id",
13141 tp->static_trace_marker_id);
13142 uiout->text ("\n");
13143 }
13144 }
13145
13146 static void
13147 tracepoint_print_mention (struct breakpoint *b)
13148 {
13149 if (current_uiout->is_mi_like_p ())
13150 return;
13151
13152 switch (b->type)
13153 {
13154 case bp_tracepoint:
13155 printf_filtered (_("Tracepoint"));
13156 printf_filtered (_(" %d"), b->number);
13157 break;
13158 case bp_fast_tracepoint:
13159 printf_filtered (_("Fast tracepoint"));
13160 printf_filtered (_(" %d"), b->number);
13161 break;
13162 case bp_static_tracepoint:
13163 printf_filtered (_("Static tracepoint"));
13164 printf_filtered (_(" %d"), b->number);
13165 break;
13166 default:
13167 internal_error (__FILE__, __LINE__,
13168 _("unhandled tracepoint type %d"), (int) b->type);
13169 }
13170
13171 say_where (b);
13172 }
13173
13174 static void
13175 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13176 {
13177 struct tracepoint *tp = (struct tracepoint *) self;
13178
13179 if (self->type == bp_fast_tracepoint)
13180 fprintf_unfiltered (fp, "ftrace");
13181 else if (self->type == bp_static_tracepoint)
13182 fprintf_unfiltered (fp, "strace");
13183 else if (self->type == bp_tracepoint)
13184 fprintf_unfiltered (fp, "trace");
13185 else
13186 internal_error (__FILE__, __LINE__,
13187 _("unhandled tracepoint type %d"), (int) self->type);
13188
13189 fprintf_unfiltered (fp, " %s",
13190 event_location_to_string (self->location.get ()));
13191 print_recreate_thread (self, fp);
13192
13193 if (tp->pass_count)
13194 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13195 }
13196
13197 static void
13198 tracepoint_create_sals_from_location (const struct event_location *location,
13199 struct linespec_result *canonical,
13200 enum bptype type_wanted)
13201 {
13202 create_sals_from_location_default (location, canonical, type_wanted);
13203 }
13204
13205 static void
13206 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13207 struct linespec_result *canonical,
13208 gdb::unique_xmalloc_ptr<char> cond_string,
13209 gdb::unique_xmalloc_ptr<char> extra_string,
13210 enum bptype type_wanted,
13211 enum bpdisp disposition,
13212 int thread,
13213 int task, int ignore_count,
13214 const struct breakpoint_ops *ops,
13215 int from_tty, int enabled,
13216 int internal, unsigned flags)
13217 {
13218 create_breakpoints_sal_default (gdbarch, canonical,
13219 std::move (cond_string),
13220 std::move (extra_string),
13221 type_wanted,
13222 disposition, thread, task,
13223 ignore_count, ops, from_tty,
13224 enabled, internal, flags);
13225 }
13226
13227 static std::vector<symtab_and_line>
13228 tracepoint_decode_location (struct breakpoint *b,
13229 const struct event_location *location,
13230 struct program_space *search_pspace)
13231 {
13232 return decode_location_default (b, location, search_pspace);
13233 }
13234
13235 struct breakpoint_ops tracepoint_breakpoint_ops;
13236
13237 /* The breakpoint_ops structure to be use on tracepoints placed in a
13238 static probe. */
13239
13240 static void
13241 tracepoint_probe_create_sals_from_location
13242 (const struct event_location *location,
13243 struct linespec_result *canonical,
13244 enum bptype type_wanted)
13245 {
13246 /* We use the same method for breakpoint on probes. */
13247 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13248 }
13249
13250 static std::vector<symtab_and_line>
13251 tracepoint_probe_decode_location (struct breakpoint *b,
13252 const struct event_location *location,
13253 struct program_space *search_pspace)
13254 {
13255 /* We use the same method for breakpoint on probes. */
13256 return bkpt_probe_decode_location (b, location, search_pspace);
13257 }
13258
13259 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13260
13261 /* Dprintf breakpoint_ops methods. */
13262
13263 static void
13264 dprintf_re_set (struct breakpoint *b)
13265 {
13266 breakpoint_re_set_default (b);
13267
13268 /* extra_string should never be non-NULL for dprintf. */
13269 gdb_assert (b->extra_string != NULL);
13270
13271 /* 1 - connect to target 1, that can run breakpoint commands.
13272 2 - create a dprintf, which resolves fine.
13273 3 - disconnect from target 1
13274 4 - connect to target 2, that can NOT run breakpoint commands.
13275
13276 After steps #3/#4, you'll want the dprintf command list to
13277 be updated, because target 1 and 2 may well return different
13278 answers for target_can_run_breakpoint_commands().
13279 Given absence of finer grained resetting, we get to do
13280 it all the time. */
13281 if (b->extra_string != NULL)
13282 update_dprintf_command_list (b);
13283 }
13284
13285 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13286
13287 static void
13288 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13289 {
13290 fprintf_unfiltered (fp, "dprintf %s,%s",
13291 event_location_to_string (tp->location.get ()),
13292 tp->extra_string);
13293 print_recreate_thread (tp, fp);
13294 }
13295
13296 /* Implement the "after_condition_true" breakpoint_ops method for
13297 dprintf.
13298
13299 dprintf's are implemented with regular commands in their command
13300 list, but we run the commands here instead of before presenting the
13301 stop to the user, as dprintf's don't actually cause a stop. This
13302 also makes it so that the commands of multiple dprintfs at the same
13303 address are all handled. */
13304
13305 static void
13306 dprintf_after_condition_true (struct bpstats *bs)
13307 {
13308 struct bpstats tmp_bs;
13309 struct bpstats *tmp_bs_p = &tmp_bs;
13310
13311 /* dprintf's never cause a stop. This wasn't set in the
13312 check_status hook instead because that would make the dprintf's
13313 condition not be evaluated. */
13314 bs->stop = 0;
13315
13316 /* Run the command list here. Take ownership of it instead of
13317 copying. We never want these commands to run later in
13318 bpstat_do_actions, if a breakpoint that causes a stop happens to
13319 be set at same address as this dprintf, or even if running the
13320 commands here throws. */
13321 tmp_bs.commands = bs->commands;
13322 bs->commands = NULL;
13323
13324 bpstat_do_actions_1 (&tmp_bs_p);
13325
13326 /* 'tmp_bs.commands' will usually be NULL by now, but
13327 bpstat_do_actions_1 may return early without processing the whole
13328 list. */
13329 }
13330
13331 /* The breakpoint_ops structure to be used on static tracepoints with
13332 markers (`-m'). */
13333
13334 static void
13335 strace_marker_create_sals_from_location (const struct event_location *location,
13336 struct linespec_result *canonical,
13337 enum bptype type_wanted)
13338 {
13339 struct linespec_sals lsal;
13340 const char *arg_start, *arg;
13341
13342 arg = arg_start = get_linespec_location (location);
13343 lsal.sals = decode_static_tracepoint_spec (&arg);
13344
13345 std::string str (arg_start, arg - arg_start);
13346 const char *ptr = str.c_str ();
13347 canonical->location = new_linespec_location (&ptr);
13348
13349 lsal.canonical
13350 = xstrdup (event_location_to_string (canonical->location.get ()));
13351 canonical->lsals.push_back (std::move (lsal));
13352 }
13353
13354 static void
13355 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13356 struct linespec_result *canonical,
13357 gdb::unique_xmalloc_ptr<char> cond_string,
13358 gdb::unique_xmalloc_ptr<char> extra_string,
13359 enum bptype type_wanted,
13360 enum bpdisp disposition,
13361 int thread,
13362 int task, int ignore_count,
13363 const struct breakpoint_ops *ops,
13364 int from_tty, int enabled,
13365 int internal, unsigned flags)
13366 {
13367 const linespec_sals &lsal = canonical->lsals[0];
13368
13369 /* If the user is creating a static tracepoint by marker id
13370 (strace -m MARKER_ID), then store the sals index, so that
13371 breakpoint_re_set can try to match up which of the newly
13372 found markers corresponds to this one, and, don't try to
13373 expand multiple locations for each sal, given than SALS
13374 already should contain all sals for MARKER_ID. */
13375
13376 for (size_t i = 0; i < lsal.sals.size (); i++)
13377 {
13378 event_location_up location
13379 = copy_event_location (canonical->location.get ());
13380
13381 std::unique_ptr<tracepoint> tp (new tracepoint ());
13382 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13383 std::move (location), NULL,
13384 std::move (cond_string),
13385 std::move (extra_string),
13386 type_wanted, disposition,
13387 thread, task, ignore_count, ops,
13388 from_tty, enabled, internal, flags,
13389 canonical->special_display);
13390 /* Given that its possible to have multiple markers with
13391 the same string id, if the user is creating a static
13392 tracepoint by marker id ("strace -m MARKER_ID"), then
13393 store the sals index, so that breakpoint_re_set can
13394 try to match up which of the newly found markers
13395 corresponds to this one */
13396 tp->static_trace_marker_id_idx = i;
13397
13398 install_breakpoint (internal, std::move (tp), 0);
13399 }
13400 }
13401
13402 static std::vector<symtab_and_line>
13403 strace_marker_decode_location (struct breakpoint *b,
13404 const struct event_location *location,
13405 struct program_space *search_pspace)
13406 {
13407 struct tracepoint *tp = (struct tracepoint *) b;
13408 const char *s = get_linespec_location (location);
13409
13410 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13411 if (sals.size () > tp->static_trace_marker_id_idx)
13412 {
13413 sals[0] = sals[tp->static_trace_marker_id_idx];
13414 sals.resize (1);
13415 return sals;
13416 }
13417 else
13418 error (_("marker %s not found"), tp->static_trace_marker_id);
13419 }
13420
13421 static struct breakpoint_ops strace_marker_breakpoint_ops;
13422
13423 static int
13424 strace_marker_p (struct breakpoint *b)
13425 {
13426 return b->ops == &strace_marker_breakpoint_ops;
13427 }
13428
13429 /* Delete a breakpoint and clean up all traces of it in the data
13430 structures. */
13431
13432 void
13433 delete_breakpoint (struct breakpoint *bpt)
13434 {
13435 struct breakpoint *b;
13436
13437 gdb_assert (bpt != NULL);
13438
13439 /* Has this bp already been deleted? This can happen because
13440 multiple lists can hold pointers to bp's. bpstat lists are
13441 especial culprits.
13442
13443 One example of this happening is a watchpoint's scope bp. When
13444 the scope bp triggers, we notice that the watchpoint is out of
13445 scope, and delete it. We also delete its scope bp. But the
13446 scope bp is marked "auto-deleting", and is already on a bpstat.
13447 That bpstat is then checked for auto-deleting bp's, which are
13448 deleted.
13449
13450 A real solution to this problem might involve reference counts in
13451 bp's, and/or giving them pointers back to their referencing
13452 bpstat's, and teaching delete_breakpoint to only free a bp's
13453 storage when no more references were extent. A cheaper bandaid
13454 was chosen. */
13455 if (bpt->type == bp_none)
13456 return;
13457
13458 /* At least avoid this stale reference until the reference counting
13459 of breakpoints gets resolved. */
13460 if (bpt->related_breakpoint != bpt)
13461 {
13462 struct breakpoint *related;
13463 struct watchpoint *w;
13464
13465 if (bpt->type == bp_watchpoint_scope)
13466 w = (struct watchpoint *) bpt->related_breakpoint;
13467 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13468 w = (struct watchpoint *) bpt;
13469 else
13470 w = NULL;
13471 if (w != NULL)
13472 watchpoint_del_at_next_stop (w);
13473
13474 /* Unlink bpt from the bpt->related_breakpoint ring. */
13475 for (related = bpt; related->related_breakpoint != bpt;
13476 related = related->related_breakpoint);
13477 related->related_breakpoint = bpt->related_breakpoint;
13478 bpt->related_breakpoint = bpt;
13479 }
13480
13481 /* watch_command_1 creates a watchpoint but only sets its number if
13482 update_watchpoint succeeds in creating its bp_locations. If there's
13483 a problem in that process, we'll be asked to delete the half-created
13484 watchpoint. In that case, don't announce the deletion. */
13485 if (bpt->number)
13486 observer_notify_breakpoint_deleted (bpt);
13487
13488 if (breakpoint_chain == bpt)
13489 breakpoint_chain = bpt->next;
13490
13491 ALL_BREAKPOINTS (b)
13492 if (b->next == bpt)
13493 {
13494 b->next = bpt->next;
13495 break;
13496 }
13497
13498 /* Be sure no bpstat's are pointing at the breakpoint after it's
13499 been freed. */
13500 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13501 in all threads for now. Note that we cannot just remove bpstats
13502 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13503 commands are associated with the bpstat; if we remove it here,
13504 then the later call to bpstat_do_actions (&stop_bpstat); in
13505 event-top.c won't do anything, and temporary breakpoints with
13506 commands won't work. */
13507
13508 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13509
13510 /* Now that breakpoint is removed from breakpoint list, update the
13511 global location list. This will remove locations that used to
13512 belong to this breakpoint. Do this before freeing the breakpoint
13513 itself, since remove_breakpoint looks at location's owner. It
13514 might be better design to have location completely
13515 self-contained, but it's not the case now. */
13516 update_global_location_list (UGLL_DONT_INSERT);
13517
13518 /* On the chance that someone will soon try again to delete this
13519 same bp, we mark it as deleted before freeing its storage. */
13520 bpt->type = bp_none;
13521 delete bpt;
13522 }
13523
13524 static void
13525 do_delete_breakpoint_cleanup (void *b)
13526 {
13527 delete_breakpoint ((struct breakpoint *) b);
13528 }
13529
13530 struct cleanup *
13531 make_cleanup_delete_breakpoint (struct breakpoint *b)
13532 {
13533 return make_cleanup (do_delete_breakpoint_cleanup, b);
13534 }
13535
13536 /* Iterator function to call a user-provided callback function once
13537 for each of B and its related breakpoints. */
13538
13539 static void
13540 iterate_over_related_breakpoints (struct breakpoint *b,
13541 gdb::function_view<void (breakpoint *)> function)
13542 {
13543 struct breakpoint *related;
13544
13545 related = b;
13546 do
13547 {
13548 struct breakpoint *next;
13549
13550 /* FUNCTION may delete RELATED. */
13551 next = related->related_breakpoint;
13552
13553 if (next == related)
13554 {
13555 /* RELATED is the last ring entry. */
13556 function (related);
13557
13558 /* FUNCTION may have deleted it, so we'd never reach back to
13559 B. There's nothing left to do anyway, so just break
13560 out. */
13561 break;
13562 }
13563 else
13564 function (related);
13565
13566 related = next;
13567 }
13568 while (related != b);
13569 }
13570
13571 void
13572 delete_command (char *arg, int from_tty)
13573 {
13574 struct breakpoint *b, *b_tmp;
13575
13576 dont_repeat ();
13577
13578 if (arg == 0)
13579 {
13580 int breaks_to_delete = 0;
13581
13582 /* Delete all breakpoints if no argument. Do not delete
13583 internal breakpoints, these have to be deleted with an
13584 explicit breakpoint number argument. */
13585 ALL_BREAKPOINTS (b)
13586 if (user_breakpoint_p (b))
13587 {
13588 breaks_to_delete = 1;
13589 break;
13590 }
13591
13592 /* Ask user only if there are some breakpoints to delete. */
13593 if (!from_tty
13594 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13595 {
13596 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13597 if (user_breakpoint_p (b))
13598 delete_breakpoint (b);
13599 }
13600 }
13601 else
13602 map_breakpoint_numbers
13603 (arg, [&] (breakpoint *b)
13604 {
13605 iterate_over_related_breakpoints (b, delete_breakpoint);
13606 });
13607 }
13608
13609 /* Return true if all locations of B bound to PSPACE are pending. If
13610 PSPACE is NULL, all locations of all program spaces are
13611 considered. */
13612
13613 static int
13614 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13615 {
13616 struct bp_location *loc;
13617
13618 for (loc = b->loc; loc != NULL; loc = loc->next)
13619 if ((pspace == NULL
13620 || loc->pspace == pspace)
13621 && !loc->shlib_disabled
13622 && !loc->pspace->executing_startup)
13623 return 0;
13624 return 1;
13625 }
13626
13627 /* Subroutine of update_breakpoint_locations to simplify it.
13628 Return non-zero if multiple fns in list LOC have the same name.
13629 Null names are ignored. */
13630
13631 static int
13632 ambiguous_names_p (struct bp_location *loc)
13633 {
13634 struct bp_location *l;
13635 htab_t htab = htab_create_alloc (13, htab_hash_string,
13636 (int (*) (const void *,
13637 const void *)) streq,
13638 NULL, xcalloc, xfree);
13639
13640 for (l = loc; l != NULL; l = l->next)
13641 {
13642 const char **slot;
13643 const char *name = l->function_name;
13644
13645 /* Allow for some names to be NULL, ignore them. */
13646 if (name == NULL)
13647 continue;
13648
13649 slot = (const char **) htab_find_slot (htab, (const void *) name,
13650 INSERT);
13651 /* NOTE: We can assume slot != NULL here because xcalloc never
13652 returns NULL. */
13653 if (*slot != NULL)
13654 {
13655 htab_delete (htab);
13656 return 1;
13657 }
13658 *slot = name;
13659 }
13660
13661 htab_delete (htab);
13662 return 0;
13663 }
13664
13665 /* When symbols change, it probably means the sources changed as well,
13666 and it might mean the static tracepoint markers are no longer at
13667 the same address or line numbers they used to be at last we
13668 checked. Losing your static tracepoints whenever you rebuild is
13669 undesirable. This function tries to resync/rematch gdb static
13670 tracepoints with the markers on the target, for static tracepoints
13671 that have not been set by marker id. Static tracepoint that have
13672 been set by marker id are reset by marker id in breakpoint_re_set.
13673 The heuristic is:
13674
13675 1) For a tracepoint set at a specific address, look for a marker at
13676 the old PC. If one is found there, assume to be the same marker.
13677 If the name / string id of the marker found is different from the
13678 previous known name, assume that means the user renamed the marker
13679 in the sources, and output a warning.
13680
13681 2) For a tracepoint set at a given line number, look for a marker
13682 at the new address of the old line number. If one is found there,
13683 assume to be the same marker. If the name / string id of the
13684 marker found is different from the previous known name, assume that
13685 means the user renamed the marker in the sources, and output a
13686 warning.
13687
13688 3) If a marker is no longer found at the same address or line, it
13689 may mean the marker no longer exists. But it may also just mean
13690 the code changed a bit. Maybe the user added a few lines of code
13691 that made the marker move up or down (in line number terms). Ask
13692 the target for info about the marker with the string id as we knew
13693 it. If found, update line number and address in the matching
13694 static tracepoint. This will get confused if there's more than one
13695 marker with the same ID (possible in UST, although unadvised
13696 precisely because it confuses tools). */
13697
13698 static struct symtab_and_line
13699 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13700 {
13701 struct tracepoint *tp = (struct tracepoint *) b;
13702 struct static_tracepoint_marker marker;
13703 CORE_ADDR pc;
13704
13705 pc = sal.pc;
13706 if (sal.line)
13707 find_line_pc (sal.symtab, sal.line, &pc);
13708
13709 if (target_static_tracepoint_marker_at (pc, &marker))
13710 {
13711 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13712 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13713 b->number,
13714 tp->static_trace_marker_id, marker.str_id);
13715
13716 xfree (tp->static_trace_marker_id);
13717 tp->static_trace_marker_id = xstrdup (marker.str_id);
13718 release_static_tracepoint_marker (&marker);
13719
13720 return sal;
13721 }
13722
13723 /* Old marker wasn't found on target at lineno. Try looking it up
13724 by string ID. */
13725 if (!sal.explicit_pc
13726 && sal.line != 0
13727 && sal.symtab != NULL
13728 && tp->static_trace_marker_id != NULL)
13729 {
13730 VEC(static_tracepoint_marker_p) *markers;
13731
13732 markers
13733 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13734
13735 if (!VEC_empty(static_tracepoint_marker_p, markers))
13736 {
13737 struct symbol *sym;
13738 struct static_tracepoint_marker *tpmarker;
13739 struct ui_out *uiout = current_uiout;
13740 struct explicit_location explicit_loc;
13741
13742 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13743
13744 xfree (tp->static_trace_marker_id);
13745 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13746
13747 warning (_("marker for static tracepoint %d (%s) not "
13748 "found at previous line number"),
13749 b->number, tp->static_trace_marker_id);
13750
13751 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13752 sym = find_pc_sect_function (tpmarker->address, NULL);
13753 uiout->text ("Now in ");
13754 if (sym)
13755 {
13756 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13757 uiout->text (" at ");
13758 }
13759 uiout->field_string ("file",
13760 symtab_to_filename_for_display (sal2.symtab));
13761 uiout->text (":");
13762
13763 if (uiout->is_mi_like_p ())
13764 {
13765 const char *fullname = symtab_to_fullname (sal2.symtab);
13766
13767 uiout->field_string ("fullname", fullname);
13768 }
13769
13770 uiout->field_int ("line", sal2.line);
13771 uiout->text ("\n");
13772
13773 b->loc->line_number = sal2.line;
13774 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13775
13776 b->location.reset (NULL);
13777 initialize_explicit_location (&explicit_loc);
13778 explicit_loc.source_filename
13779 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13780 explicit_loc.line_offset.offset = b->loc->line_number;
13781 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13782 b->location = new_explicit_location (&explicit_loc);
13783
13784 /* Might be nice to check if function changed, and warn if
13785 so. */
13786
13787 release_static_tracepoint_marker (tpmarker);
13788 }
13789 }
13790 return sal;
13791 }
13792
13793 /* Returns 1 iff locations A and B are sufficiently same that
13794 we don't need to report breakpoint as changed. */
13795
13796 static int
13797 locations_are_equal (struct bp_location *a, struct bp_location *b)
13798 {
13799 while (a && b)
13800 {
13801 if (a->address != b->address)
13802 return 0;
13803
13804 if (a->shlib_disabled != b->shlib_disabled)
13805 return 0;
13806
13807 if (a->enabled != b->enabled)
13808 return 0;
13809
13810 a = a->next;
13811 b = b->next;
13812 }
13813
13814 if ((a == NULL) != (b == NULL))
13815 return 0;
13816
13817 return 1;
13818 }
13819
13820 /* Split all locations of B that are bound to PSPACE out of B's
13821 location list to a separate list and return that list's head. If
13822 PSPACE is NULL, hoist out all locations of B. */
13823
13824 static struct bp_location *
13825 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13826 {
13827 struct bp_location head;
13828 struct bp_location *i = b->loc;
13829 struct bp_location **i_link = &b->loc;
13830 struct bp_location *hoisted = &head;
13831
13832 if (pspace == NULL)
13833 {
13834 i = b->loc;
13835 b->loc = NULL;
13836 return i;
13837 }
13838
13839 head.next = NULL;
13840
13841 while (i != NULL)
13842 {
13843 if (i->pspace == pspace)
13844 {
13845 *i_link = i->next;
13846 i->next = NULL;
13847 hoisted->next = i;
13848 hoisted = i;
13849 }
13850 else
13851 i_link = &i->next;
13852 i = *i_link;
13853 }
13854
13855 return head.next;
13856 }
13857
13858 /* Create new breakpoint locations for B (a hardware or software
13859 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13860 zero, then B is a ranged breakpoint. Only recreates locations for
13861 FILTER_PSPACE. Locations of other program spaces are left
13862 untouched. */
13863
13864 void
13865 update_breakpoint_locations (struct breakpoint *b,
13866 struct program_space *filter_pspace,
13867 gdb::array_view<const symtab_and_line> sals,
13868 gdb::array_view<const symtab_and_line> sals_end)
13869 {
13870 int i;
13871 struct bp_location *existing_locations;
13872
13873 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13874 {
13875 /* Ranged breakpoints have only one start location and one end
13876 location. */
13877 b->enable_state = bp_disabled;
13878 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13879 "multiple locations found\n"),
13880 b->number);
13881 return;
13882 }
13883
13884 /* If there's no new locations, and all existing locations are
13885 pending, don't do anything. This optimizes the common case where
13886 all locations are in the same shared library, that was unloaded.
13887 We'd like to retain the location, so that when the library is
13888 loaded again, we don't loose the enabled/disabled status of the
13889 individual locations. */
13890 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13891 return;
13892
13893 existing_locations = hoist_existing_locations (b, filter_pspace);
13894
13895 for (const auto &sal : sals)
13896 {
13897 struct bp_location *new_loc;
13898
13899 switch_to_program_space_and_thread (sal.pspace);
13900
13901 new_loc = add_location_to_breakpoint (b, &sal);
13902
13903 /* Reparse conditions, they might contain references to the
13904 old symtab. */
13905 if (b->cond_string != NULL)
13906 {
13907 const char *s;
13908
13909 s = b->cond_string;
13910 TRY
13911 {
13912 new_loc->cond = parse_exp_1 (&s, sal.pc,
13913 block_for_pc (sal.pc),
13914 0);
13915 }
13916 CATCH (e, RETURN_MASK_ERROR)
13917 {
13918 warning (_("failed to reevaluate condition "
13919 "for breakpoint %d: %s"),
13920 b->number, e.message);
13921 new_loc->enabled = 0;
13922 }
13923 END_CATCH
13924 }
13925
13926 if (!sals_end.empty ())
13927 {
13928 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13929
13930 new_loc->length = end - sals[0].pc + 1;
13931 }
13932 }
13933
13934 /* If possible, carry over 'disable' status from existing
13935 breakpoints. */
13936 {
13937 struct bp_location *e = existing_locations;
13938 /* If there are multiple breakpoints with the same function name,
13939 e.g. for inline functions, comparing function names won't work.
13940 Instead compare pc addresses; this is just a heuristic as things
13941 may have moved, but in practice it gives the correct answer
13942 often enough until a better solution is found. */
13943 int have_ambiguous_names = ambiguous_names_p (b->loc);
13944
13945 for (; e; e = e->next)
13946 {
13947 if (!e->enabled && e->function_name)
13948 {
13949 struct bp_location *l = b->loc;
13950 if (have_ambiguous_names)
13951 {
13952 for (; l; l = l->next)
13953 if (breakpoint_locations_match (e, l))
13954 {
13955 l->enabled = 0;
13956 break;
13957 }
13958 }
13959 else
13960 {
13961 for (; l; l = l->next)
13962 if (l->function_name
13963 && strcmp (e->function_name, l->function_name) == 0)
13964 {
13965 l->enabled = 0;
13966 break;
13967 }
13968 }
13969 }
13970 }
13971 }
13972
13973 if (!locations_are_equal (existing_locations, b->loc))
13974 observer_notify_breakpoint_modified (b);
13975 }
13976
13977 /* Find the SaL locations corresponding to the given LOCATION.
13978 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13979
13980 static std::vector<symtab_and_line>
13981 location_to_sals (struct breakpoint *b, struct event_location *location,
13982 struct program_space *search_pspace, int *found)
13983 {
13984 struct gdb_exception exception = exception_none;
13985
13986 gdb_assert (b->ops != NULL);
13987
13988 std::vector<symtab_and_line> sals;
13989
13990 TRY
13991 {
13992 sals = b->ops->decode_location (b, location, search_pspace);
13993 }
13994 CATCH (e, RETURN_MASK_ERROR)
13995 {
13996 int not_found_and_ok = 0;
13997
13998 exception = e;
13999
14000 /* For pending breakpoints, it's expected that parsing will
14001 fail until the right shared library is loaded. User has
14002 already told to create pending breakpoints and don't need
14003 extra messages. If breakpoint is in bp_shlib_disabled
14004 state, then user already saw the message about that
14005 breakpoint being disabled, and don't want to see more
14006 errors. */
14007 if (e.error == NOT_FOUND_ERROR
14008 && (b->condition_not_parsed
14009 || (b->loc != NULL
14010 && search_pspace != NULL
14011 && b->loc->pspace != search_pspace)
14012 || (b->loc && b->loc->shlib_disabled)
14013 || (b->loc && b->loc->pspace->executing_startup)
14014 || b->enable_state == bp_disabled))
14015 not_found_and_ok = 1;
14016
14017 if (!not_found_and_ok)
14018 {
14019 /* We surely don't want to warn about the same breakpoint
14020 10 times. One solution, implemented here, is disable
14021 the breakpoint on error. Another solution would be to
14022 have separate 'warning emitted' flag. Since this
14023 happens only when a binary has changed, I don't know
14024 which approach is better. */
14025 b->enable_state = bp_disabled;
14026 throw_exception (e);
14027 }
14028 }
14029 END_CATCH
14030
14031 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14032 {
14033 for (auto &sal : sals)
14034 resolve_sal_pc (&sal);
14035 if (b->condition_not_parsed && b->extra_string != NULL)
14036 {
14037 char *cond_string, *extra_string;
14038 int thread, task;
14039
14040 find_condition_and_thread (b->extra_string, sals[0].pc,
14041 &cond_string, &thread, &task,
14042 &extra_string);
14043 gdb_assert (b->cond_string == NULL);
14044 if (cond_string)
14045 b->cond_string = cond_string;
14046 b->thread = thread;
14047 b->task = task;
14048 if (extra_string)
14049 {
14050 xfree (b->extra_string);
14051 b->extra_string = extra_string;
14052 }
14053 b->condition_not_parsed = 0;
14054 }
14055
14056 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14057 sals[0] = update_static_tracepoint (b, sals[0]);
14058
14059 *found = 1;
14060 }
14061 else
14062 *found = 0;
14063
14064 return sals;
14065 }
14066
14067 /* The default re_set method, for typical hardware or software
14068 breakpoints. Reevaluate the breakpoint and recreate its
14069 locations. */
14070
14071 static void
14072 breakpoint_re_set_default (struct breakpoint *b)
14073 {
14074 struct program_space *filter_pspace = current_program_space;
14075 std::vector<symtab_and_line> expanded, expanded_end;
14076
14077 int found;
14078 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
14079 filter_pspace, &found);
14080 if (found)
14081 expanded = std::move (sals);
14082
14083 if (b->location_range_end != NULL)
14084 {
14085 std::vector<symtab_and_line> sals_end
14086 = location_to_sals (b, b->location_range_end.get (),
14087 filter_pspace, &found);
14088 if (found)
14089 expanded_end = std::move (sals_end);
14090 }
14091
14092 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14093 }
14094
14095 /* Default method for creating SALs from an address string. It basically
14096 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14097
14098 static void
14099 create_sals_from_location_default (const struct event_location *location,
14100 struct linespec_result *canonical,
14101 enum bptype type_wanted)
14102 {
14103 parse_breakpoint_sals (location, canonical);
14104 }
14105
14106 /* Call create_breakpoints_sal for the given arguments. This is the default
14107 function for the `create_breakpoints_sal' method of
14108 breakpoint_ops. */
14109
14110 static void
14111 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14112 struct linespec_result *canonical,
14113 gdb::unique_xmalloc_ptr<char> cond_string,
14114 gdb::unique_xmalloc_ptr<char> extra_string,
14115 enum bptype type_wanted,
14116 enum bpdisp disposition,
14117 int thread,
14118 int task, int ignore_count,
14119 const struct breakpoint_ops *ops,
14120 int from_tty, int enabled,
14121 int internal, unsigned flags)
14122 {
14123 create_breakpoints_sal (gdbarch, canonical,
14124 std::move (cond_string),
14125 std::move (extra_string),
14126 type_wanted, disposition,
14127 thread, task, ignore_count, ops, from_tty,
14128 enabled, internal, flags);
14129 }
14130
14131 /* Decode the line represented by S by calling decode_line_full. This is the
14132 default function for the `decode_location' method of breakpoint_ops. */
14133
14134 static std::vector<symtab_and_line>
14135 decode_location_default (struct breakpoint *b,
14136 const struct event_location *location,
14137 struct program_space *search_pspace)
14138 {
14139 struct linespec_result canonical;
14140
14141 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14142 (struct symtab *) NULL, 0,
14143 &canonical, multiple_symbols_all,
14144 b->filter);
14145
14146 /* We should get 0 or 1 resulting SALs. */
14147 gdb_assert (canonical.lsals.size () < 2);
14148
14149 if (!canonical.lsals.empty ())
14150 {
14151 const linespec_sals &lsal = canonical.lsals[0];
14152 return std::move (lsal.sals);
14153 }
14154 return {};
14155 }
14156
14157 /* Prepare the global context for a re-set of breakpoint B. */
14158
14159 static struct cleanup *
14160 prepare_re_set_context (struct breakpoint *b)
14161 {
14162 input_radix = b->input_radix;
14163 set_language (b->language);
14164
14165 return make_cleanup (null_cleanup, NULL);
14166 }
14167
14168 /* Reset a breakpoint given it's struct breakpoint * BINT.
14169 The value we return ends up being the return value from catch_errors.
14170 Unused in this case. */
14171
14172 static int
14173 breakpoint_re_set_one (void *bint)
14174 {
14175 /* Get past catch_errs. */
14176 struct breakpoint *b = (struct breakpoint *) bint;
14177 struct cleanup *cleanups;
14178
14179 cleanups = prepare_re_set_context (b);
14180 b->ops->re_set (b);
14181 do_cleanups (cleanups);
14182 return 0;
14183 }
14184
14185 /* Re-set breakpoint locations for the current program space.
14186 Locations bound to other program spaces are left untouched. */
14187
14188 void
14189 breakpoint_re_set (void)
14190 {
14191 struct breakpoint *b, *b_tmp;
14192 enum language save_language;
14193 int save_input_radix;
14194
14195 save_language = current_language->la_language;
14196 save_input_radix = input_radix;
14197
14198 {
14199 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14200
14201 /* Note: we must not try to insert locations until after all
14202 breakpoints have been re-set. Otherwise, e.g., when re-setting
14203 breakpoint 1, we'd insert the locations of breakpoint 2, which
14204 hadn't been re-set yet, and thus may have stale locations. */
14205
14206 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14207 {
14208 /* Format possible error msg. */
14209 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14210 b->number);
14211 struct cleanup *cleanups = make_cleanup (xfree, message);
14212 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14213 do_cleanups (cleanups);
14214 }
14215 set_language (save_language);
14216 input_radix = save_input_radix;
14217
14218 jit_breakpoint_re_set ();
14219 }
14220
14221 create_overlay_event_breakpoint ();
14222 create_longjmp_master_breakpoint ();
14223 create_std_terminate_master_breakpoint ();
14224 create_exception_master_breakpoint ();
14225
14226 /* Now we can insert. */
14227 update_global_location_list (UGLL_MAY_INSERT);
14228 }
14229 \f
14230 /* Reset the thread number of this breakpoint:
14231
14232 - If the breakpoint is for all threads, leave it as-is.
14233 - Else, reset it to the current thread for inferior_ptid. */
14234 void
14235 breakpoint_re_set_thread (struct breakpoint *b)
14236 {
14237 if (b->thread != -1)
14238 {
14239 if (in_thread_list (inferior_ptid))
14240 b->thread = ptid_to_global_thread_id (inferior_ptid);
14241
14242 /* We're being called after following a fork. The new fork is
14243 selected as current, and unless this was a vfork will have a
14244 different program space from the original thread. Reset that
14245 as well. */
14246 b->loc->pspace = current_program_space;
14247 }
14248 }
14249
14250 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14251 If from_tty is nonzero, it prints a message to that effect,
14252 which ends with a period (no newline). */
14253
14254 void
14255 set_ignore_count (int bptnum, int count, int from_tty)
14256 {
14257 struct breakpoint *b;
14258
14259 if (count < 0)
14260 count = 0;
14261
14262 ALL_BREAKPOINTS (b)
14263 if (b->number == bptnum)
14264 {
14265 if (is_tracepoint (b))
14266 {
14267 if (from_tty && count != 0)
14268 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14269 bptnum);
14270 return;
14271 }
14272
14273 b->ignore_count = count;
14274 if (from_tty)
14275 {
14276 if (count == 0)
14277 printf_filtered (_("Will stop next time "
14278 "breakpoint %d is reached."),
14279 bptnum);
14280 else if (count == 1)
14281 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14282 bptnum);
14283 else
14284 printf_filtered (_("Will ignore next %d "
14285 "crossings of breakpoint %d."),
14286 count, bptnum);
14287 }
14288 observer_notify_breakpoint_modified (b);
14289 return;
14290 }
14291
14292 error (_("No breakpoint number %d."), bptnum);
14293 }
14294
14295 /* Command to set ignore-count of breakpoint N to COUNT. */
14296
14297 static void
14298 ignore_command (char *args, int from_tty)
14299 {
14300 char *p = args;
14301 int num;
14302
14303 if (p == 0)
14304 error_no_arg (_("a breakpoint number"));
14305
14306 num = get_number (&p);
14307 if (num == 0)
14308 error (_("bad breakpoint number: '%s'"), args);
14309 if (*p == 0)
14310 error (_("Second argument (specified ignore-count) is missing."));
14311
14312 set_ignore_count (num,
14313 longest_to_int (value_as_long (parse_and_eval (p))),
14314 from_tty);
14315 if (from_tty)
14316 printf_filtered ("\n");
14317 }
14318 \f
14319 /* Call FUNCTION on each of the breakpoints
14320 whose numbers are given in ARGS. */
14321
14322 static void
14323 map_breakpoint_numbers (const char *args,
14324 gdb::function_view<void (breakpoint *)> function)
14325 {
14326 int num;
14327 struct breakpoint *b, *tmp;
14328
14329 if (args == 0 || *args == '\0')
14330 error_no_arg (_("one or more breakpoint numbers"));
14331
14332 number_or_range_parser parser (args);
14333
14334 while (!parser.finished ())
14335 {
14336 const char *p = parser.cur_tok ();
14337 bool match = false;
14338
14339 num = parser.get_number ();
14340 if (num == 0)
14341 {
14342 warning (_("bad breakpoint number at or near '%s'"), p);
14343 }
14344 else
14345 {
14346 ALL_BREAKPOINTS_SAFE (b, tmp)
14347 if (b->number == num)
14348 {
14349 match = true;
14350 function (b);
14351 break;
14352 }
14353 if (!match)
14354 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14355 }
14356 }
14357 }
14358
14359 static struct bp_location *
14360 find_location_by_number (const char *number)
14361 {
14362 const char *p1;
14363 int bp_num;
14364 int loc_num;
14365 struct breakpoint *b;
14366 struct bp_location *loc;
14367
14368 p1 = number;
14369 bp_num = get_number_trailer (&p1, '.');
14370 if (bp_num == 0 || p1[0] != '.')
14371 error (_("Bad breakpoint number '%s'"), number);
14372
14373 ALL_BREAKPOINTS (b)
14374 if (b->number == bp_num)
14375 {
14376 break;
14377 }
14378
14379 if (!b || b->number != bp_num)
14380 error (_("Bad breakpoint number '%s'"), number);
14381
14382 /* Skip the dot. */
14383 ++p1;
14384 const char *save = p1;
14385 loc_num = get_number (&p1);
14386 if (loc_num == 0)
14387 error (_("Bad breakpoint location number '%s'"), number);
14388
14389 --loc_num;
14390 loc = b->loc;
14391 for (;loc_num && loc; --loc_num, loc = loc->next)
14392 ;
14393 if (!loc)
14394 error (_("Bad breakpoint location number '%s'"), save);
14395
14396 return loc;
14397 }
14398
14399
14400 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14401 If from_tty is nonzero, it prints a message to that effect,
14402 which ends with a period (no newline). */
14403
14404 void
14405 disable_breakpoint (struct breakpoint *bpt)
14406 {
14407 /* Never disable a watchpoint scope breakpoint; we want to
14408 hit them when we leave scope so we can delete both the
14409 watchpoint and its scope breakpoint at that time. */
14410 if (bpt->type == bp_watchpoint_scope)
14411 return;
14412
14413 bpt->enable_state = bp_disabled;
14414
14415 /* Mark breakpoint locations modified. */
14416 mark_breakpoint_modified (bpt);
14417
14418 if (target_supports_enable_disable_tracepoint ()
14419 && current_trace_status ()->running && is_tracepoint (bpt))
14420 {
14421 struct bp_location *location;
14422
14423 for (location = bpt->loc; location; location = location->next)
14424 target_disable_tracepoint (location);
14425 }
14426
14427 update_global_location_list (UGLL_DONT_INSERT);
14428
14429 observer_notify_breakpoint_modified (bpt);
14430 }
14431
14432 static void
14433 disable_command (char *args, int from_tty)
14434 {
14435 if (args == 0)
14436 {
14437 struct breakpoint *bpt;
14438
14439 ALL_BREAKPOINTS (bpt)
14440 if (user_breakpoint_p (bpt))
14441 disable_breakpoint (bpt);
14442 }
14443 else
14444 {
14445 std::string num = extract_arg (&args);
14446
14447 while (!num.empty ())
14448 {
14449 if (num.find ('.') != std::string::npos)
14450 {
14451 struct bp_location *loc = find_location_by_number (num.c_str ());
14452
14453 if (loc)
14454 {
14455 if (loc->enabled)
14456 {
14457 loc->enabled = 0;
14458 mark_breakpoint_location_modified (loc);
14459 }
14460 if (target_supports_enable_disable_tracepoint ()
14461 && current_trace_status ()->running && loc->owner
14462 && is_tracepoint (loc->owner))
14463 target_disable_tracepoint (loc);
14464 }
14465 update_global_location_list (UGLL_DONT_INSERT);
14466 }
14467 else
14468 map_breakpoint_numbers
14469 (num.c_str (), [&] (breakpoint *b)
14470 {
14471 iterate_over_related_breakpoints (b, disable_breakpoint);
14472 });
14473 num = extract_arg (&args);
14474 }
14475 }
14476 }
14477
14478 static void
14479 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14480 int count)
14481 {
14482 int target_resources_ok;
14483
14484 if (bpt->type == bp_hardware_breakpoint)
14485 {
14486 int i;
14487 i = hw_breakpoint_used_count ();
14488 target_resources_ok =
14489 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14490 i + 1, 0);
14491 if (target_resources_ok == 0)
14492 error (_("No hardware breakpoint support in the target."));
14493 else if (target_resources_ok < 0)
14494 error (_("Hardware breakpoints used exceeds limit."));
14495 }
14496
14497 if (is_watchpoint (bpt))
14498 {
14499 /* Initialize it just to avoid a GCC false warning. */
14500 enum enable_state orig_enable_state = bp_disabled;
14501
14502 TRY
14503 {
14504 struct watchpoint *w = (struct watchpoint *) bpt;
14505
14506 orig_enable_state = bpt->enable_state;
14507 bpt->enable_state = bp_enabled;
14508 update_watchpoint (w, 1 /* reparse */);
14509 }
14510 CATCH (e, RETURN_MASK_ALL)
14511 {
14512 bpt->enable_state = orig_enable_state;
14513 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14514 bpt->number);
14515 return;
14516 }
14517 END_CATCH
14518 }
14519
14520 bpt->enable_state = bp_enabled;
14521
14522 /* Mark breakpoint locations modified. */
14523 mark_breakpoint_modified (bpt);
14524
14525 if (target_supports_enable_disable_tracepoint ()
14526 && current_trace_status ()->running && is_tracepoint (bpt))
14527 {
14528 struct bp_location *location;
14529
14530 for (location = bpt->loc; location; location = location->next)
14531 target_enable_tracepoint (location);
14532 }
14533
14534 bpt->disposition = disposition;
14535 bpt->enable_count = count;
14536 update_global_location_list (UGLL_MAY_INSERT);
14537
14538 observer_notify_breakpoint_modified (bpt);
14539 }
14540
14541
14542 void
14543 enable_breakpoint (struct breakpoint *bpt)
14544 {
14545 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14546 }
14547
14548 /* The enable command enables the specified breakpoints (or all defined
14549 breakpoints) so they once again become (or continue to be) effective
14550 in stopping the inferior. */
14551
14552 static void
14553 enable_command (char *args, int from_tty)
14554 {
14555 if (args == 0)
14556 {
14557 struct breakpoint *bpt;
14558
14559 ALL_BREAKPOINTS (bpt)
14560 if (user_breakpoint_p (bpt))
14561 enable_breakpoint (bpt);
14562 }
14563 else
14564 {
14565 std::string num = extract_arg (&args);
14566
14567 while (!num.empty ())
14568 {
14569 if (num.find ('.') != std::string::npos)
14570 {
14571 struct bp_location *loc = find_location_by_number (num.c_str ());
14572
14573 if (loc)
14574 {
14575 if (!loc->enabled)
14576 {
14577 loc->enabled = 1;
14578 mark_breakpoint_location_modified (loc);
14579 }
14580 if (target_supports_enable_disable_tracepoint ()
14581 && current_trace_status ()->running && loc->owner
14582 && is_tracepoint (loc->owner))
14583 target_enable_tracepoint (loc);
14584 }
14585 update_global_location_list (UGLL_MAY_INSERT);
14586 }
14587 else
14588 map_breakpoint_numbers
14589 (num.c_str (), [&] (breakpoint *b)
14590 {
14591 iterate_over_related_breakpoints (b, enable_breakpoint);
14592 });
14593 num = extract_arg (&args);
14594 }
14595 }
14596 }
14597
14598 static void
14599 enable_once_command (char *args, int from_tty)
14600 {
14601 map_breakpoint_numbers
14602 (args, [&] (breakpoint *b)
14603 {
14604 iterate_over_related_breakpoints
14605 (b, [&] (breakpoint *bpt)
14606 {
14607 enable_breakpoint_disp (bpt, disp_disable, 1);
14608 });
14609 });
14610 }
14611
14612 static void
14613 enable_count_command (char *args, int from_tty)
14614 {
14615 int count;
14616
14617 if (args == NULL)
14618 error_no_arg (_("hit count"));
14619
14620 count = get_number (&args);
14621
14622 map_breakpoint_numbers
14623 (args, [&] (breakpoint *b)
14624 {
14625 iterate_over_related_breakpoints
14626 (b, [&] (breakpoint *bpt)
14627 {
14628 enable_breakpoint_disp (bpt, disp_disable, count);
14629 });
14630 });
14631 }
14632
14633 static void
14634 enable_delete_command (char *args, int from_tty)
14635 {
14636 map_breakpoint_numbers
14637 (args, [&] (breakpoint *b)
14638 {
14639 iterate_over_related_breakpoints
14640 (b, [&] (breakpoint *bpt)
14641 {
14642 enable_breakpoint_disp (bpt, disp_del, 1);
14643 });
14644 });
14645 }
14646 \f
14647 static void
14648 set_breakpoint_cmd (char *args, int from_tty)
14649 {
14650 }
14651
14652 static void
14653 show_breakpoint_cmd (char *args, int from_tty)
14654 {
14655 }
14656
14657 /* Invalidate last known value of any hardware watchpoint if
14658 the memory which that value represents has been written to by
14659 GDB itself. */
14660
14661 static void
14662 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14663 CORE_ADDR addr, ssize_t len,
14664 const bfd_byte *data)
14665 {
14666 struct breakpoint *bp;
14667
14668 ALL_BREAKPOINTS (bp)
14669 if (bp->enable_state == bp_enabled
14670 && bp->type == bp_hardware_watchpoint)
14671 {
14672 struct watchpoint *wp = (struct watchpoint *) bp;
14673
14674 if (wp->val_valid && wp->val)
14675 {
14676 struct bp_location *loc;
14677
14678 for (loc = bp->loc; loc != NULL; loc = loc->next)
14679 if (loc->loc_type == bp_loc_hardware_watchpoint
14680 && loc->address + loc->length > addr
14681 && addr + len > loc->address)
14682 {
14683 value_free (wp->val);
14684 wp->val = NULL;
14685 wp->val_valid = 0;
14686 }
14687 }
14688 }
14689 }
14690
14691 /* Create and insert a breakpoint for software single step. */
14692
14693 void
14694 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14695 struct address_space *aspace,
14696 CORE_ADDR next_pc)
14697 {
14698 struct thread_info *tp = inferior_thread ();
14699 struct symtab_and_line sal;
14700 CORE_ADDR pc = next_pc;
14701
14702 if (tp->control.single_step_breakpoints == NULL)
14703 {
14704 tp->control.single_step_breakpoints
14705 = new_single_step_breakpoint (tp->global_num, gdbarch);
14706 }
14707
14708 sal = find_pc_line (pc, 0);
14709 sal.pc = pc;
14710 sal.section = find_pc_overlay (pc);
14711 sal.explicit_pc = 1;
14712 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14713
14714 update_global_location_list (UGLL_INSERT);
14715 }
14716
14717 /* Insert single step breakpoints according to the current state. */
14718
14719 int
14720 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14721 {
14722 struct regcache *regcache = get_current_regcache ();
14723 std::vector<CORE_ADDR> next_pcs;
14724
14725 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14726
14727 if (!next_pcs.empty ())
14728 {
14729 struct frame_info *frame = get_current_frame ();
14730 struct address_space *aspace = get_frame_address_space (frame);
14731
14732 for (CORE_ADDR pc : next_pcs)
14733 insert_single_step_breakpoint (gdbarch, aspace, pc);
14734
14735 return 1;
14736 }
14737 else
14738 return 0;
14739 }
14740
14741 /* See breakpoint.h. */
14742
14743 int
14744 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14745 struct address_space *aspace,
14746 CORE_ADDR pc)
14747 {
14748 struct bp_location *loc;
14749
14750 for (loc = bp->loc; loc != NULL; loc = loc->next)
14751 if (loc->inserted
14752 && breakpoint_location_address_match (loc, aspace, pc))
14753 return 1;
14754
14755 return 0;
14756 }
14757
14758 /* Check whether a software single-step breakpoint is inserted at
14759 PC. */
14760
14761 int
14762 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14763 CORE_ADDR pc)
14764 {
14765 struct breakpoint *bpt;
14766
14767 ALL_BREAKPOINTS (bpt)
14768 {
14769 if (bpt->type == bp_single_step
14770 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14771 return 1;
14772 }
14773 return 0;
14774 }
14775
14776 /* Tracepoint-specific operations. */
14777
14778 /* Set tracepoint count to NUM. */
14779 static void
14780 set_tracepoint_count (int num)
14781 {
14782 tracepoint_count = num;
14783 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14784 }
14785
14786 static void
14787 trace_command (char *arg_in, int from_tty)
14788 {
14789 const char *arg = arg_in;
14790 struct breakpoint_ops *ops;
14791
14792 event_location_up location = string_to_event_location (&arg,
14793 current_language);
14794 if (location != NULL
14795 && event_location_type (location.get ()) == PROBE_LOCATION)
14796 ops = &tracepoint_probe_breakpoint_ops;
14797 else
14798 ops = &tracepoint_breakpoint_ops;
14799
14800 create_breakpoint (get_current_arch (),
14801 location.get (),
14802 NULL, 0, arg, 1 /* parse arg */,
14803 0 /* tempflag */,
14804 bp_tracepoint /* type_wanted */,
14805 0 /* Ignore count */,
14806 pending_break_support,
14807 ops,
14808 from_tty,
14809 1 /* enabled */,
14810 0 /* internal */, 0);
14811 }
14812
14813 static void
14814 ftrace_command (char *arg_in, int from_tty)
14815 {
14816 const char *arg = arg_in;
14817 event_location_up location = string_to_event_location (&arg,
14818 current_language);
14819 create_breakpoint (get_current_arch (),
14820 location.get (),
14821 NULL, 0, arg, 1 /* parse arg */,
14822 0 /* tempflag */,
14823 bp_fast_tracepoint /* type_wanted */,
14824 0 /* Ignore count */,
14825 pending_break_support,
14826 &tracepoint_breakpoint_ops,
14827 from_tty,
14828 1 /* enabled */,
14829 0 /* internal */, 0);
14830 }
14831
14832 /* strace command implementation. Creates a static tracepoint. */
14833
14834 static void
14835 strace_command (char *arg_in, int from_tty)
14836 {
14837 const char *arg = arg_in;
14838 struct breakpoint_ops *ops;
14839 event_location_up location;
14840 struct cleanup *back_to;
14841
14842 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14843 or with a normal static tracepoint. */
14844 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14845 {
14846 ops = &strace_marker_breakpoint_ops;
14847 location = new_linespec_location (&arg);
14848 }
14849 else
14850 {
14851 ops = &tracepoint_breakpoint_ops;
14852 location = string_to_event_location (&arg, current_language);
14853 }
14854
14855 create_breakpoint (get_current_arch (),
14856 location.get (),
14857 NULL, 0, arg, 1 /* parse arg */,
14858 0 /* tempflag */,
14859 bp_static_tracepoint /* type_wanted */,
14860 0 /* Ignore count */,
14861 pending_break_support,
14862 ops,
14863 from_tty,
14864 1 /* enabled */,
14865 0 /* internal */, 0);
14866 }
14867
14868 /* Set up a fake reader function that gets command lines from a linked
14869 list that was acquired during tracepoint uploading. */
14870
14871 static struct uploaded_tp *this_utp;
14872 static int next_cmd;
14873
14874 static char *
14875 read_uploaded_action (void)
14876 {
14877 char *rslt;
14878
14879 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14880
14881 next_cmd++;
14882
14883 return rslt;
14884 }
14885
14886 /* Given information about a tracepoint as recorded on a target (which
14887 can be either a live system or a trace file), attempt to create an
14888 equivalent GDB tracepoint. This is not a reliable process, since
14889 the target does not necessarily have all the information used when
14890 the tracepoint was originally defined. */
14891
14892 struct tracepoint *
14893 create_tracepoint_from_upload (struct uploaded_tp *utp)
14894 {
14895 const char *addr_str;
14896 char small_buf[100];
14897 struct tracepoint *tp;
14898
14899 if (utp->at_string)
14900 addr_str = utp->at_string;
14901 else
14902 {
14903 /* In the absence of a source location, fall back to raw
14904 address. Since there is no way to confirm that the address
14905 means the same thing as when the trace was started, warn the
14906 user. */
14907 warning (_("Uploaded tracepoint %d has no "
14908 "source location, using raw address"),
14909 utp->number);
14910 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14911 addr_str = small_buf;
14912 }
14913
14914 /* There's not much we can do with a sequence of bytecodes. */
14915 if (utp->cond && !utp->cond_string)
14916 warning (_("Uploaded tracepoint %d condition "
14917 "has no source form, ignoring it"),
14918 utp->number);
14919
14920 event_location_up location = string_to_event_location (&addr_str,
14921 current_language);
14922 if (!create_breakpoint (get_current_arch (),
14923 location.get (),
14924 utp->cond_string, -1, addr_str,
14925 0 /* parse cond/thread */,
14926 0 /* tempflag */,
14927 utp->type /* type_wanted */,
14928 0 /* Ignore count */,
14929 pending_break_support,
14930 &tracepoint_breakpoint_ops,
14931 0 /* from_tty */,
14932 utp->enabled /* enabled */,
14933 0 /* internal */,
14934 CREATE_BREAKPOINT_FLAGS_INSERTED))
14935 return NULL;
14936
14937 /* Get the tracepoint we just created. */
14938 tp = get_tracepoint (tracepoint_count);
14939 gdb_assert (tp != NULL);
14940
14941 if (utp->pass > 0)
14942 {
14943 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14944 tp->number);
14945
14946 trace_pass_command (small_buf, 0);
14947 }
14948
14949 /* If we have uploaded versions of the original commands, set up a
14950 special-purpose "reader" function and call the usual command line
14951 reader, then pass the result to the breakpoint command-setting
14952 function. */
14953 if (!VEC_empty (char_ptr, utp->cmd_strings))
14954 {
14955 command_line_up cmd_list;
14956
14957 this_utp = utp;
14958 next_cmd = 0;
14959
14960 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14961
14962 breakpoint_set_commands (tp, std::move (cmd_list));
14963 }
14964 else if (!VEC_empty (char_ptr, utp->actions)
14965 || !VEC_empty (char_ptr, utp->step_actions))
14966 warning (_("Uploaded tracepoint %d actions "
14967 "have no source form, ignoring them"),
14968 utp->number);
14969
14970 /* Copy any status information that might be available. */
14971 tp->hit_count = utp->hit_count;
14972 tp->traceframe_usage = utp->traceframe_usage;
14973
14974 return tp;
14975 }
14976
14977 /* Print information on tracepoint number TPNUM_EXP, or all if
14978 omitted. */
14979
14980 static void
14981 info_tracepoints_command (char *args, int from_tty)
14982 {
14983 struct ui_out *uiout = current_uiout;
14984 int num_printed;
14985
14986 num_printed = breakpoint_1 (args, 0, is_tracepoint);
14987
14988 if (num_printed == 0)
14989 {
14990 if (args == NULL || *args == '\0')
14991 uiout->message ("No tracepoints.\n");
14992 else
14993 uiout->message ("No tracepoint matching '%s'.\n", args);
14994 }
14995
14996 default_collect_info ();
14997 }
14998
14999 /* The 'enable trace' command enables tracepoints.
15000 Not supported by all targets. */
15001 static void
15002 enable_trace_command (char *args, int from_tty)
15003 {
15004 enable_command (args, from_tty);
15005 }
15006
15007 /* The 'disable trace' command disables tracepoints.
15008 Not supported by all targets. */
15009 static void
15010 disable_trace_command (char *args, int from_tty)
15011 {
15012 disable_command (args, from_tty);
15013 }
15014
15015 /* Remove a tracepoint (or all if no argument). */
15016 static void
15017 delete_trace_command (char *arg, int from_tty)
15018 {
15019 struct breakpoint *b, *b_tmp;
15020
15021 dont_repeat ();
15022
15023 if (arg == 0)
15024 {
15025 int breaks_to_delete = 0;
15026
15027 /* Delete all breakpoints if no argument.
15028 Do not delete internal or call-dummy breakpoints, these
15029 have to be deleted with an explicit breakpoint number
15030 argument. */
15031 ALL_TRACEPOINTS (b)
15032 if (is_tracepoint (b) && user_breakpoint_p (b))
15033 {
15034 breaks_to_delete = 1;
15035 break;
15036 }
15037
15038 /* Ask user only if there are some breakpoints to delete. */
15039 if (!from_tty
15040 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15041 {
15042 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15043 if (is_tracepoint (b) && user_breakpoint_p (b))
15044 delete_breakpoint (b);
15045 }
15046 }
15047 else
15048 map_breakpoint_numbers
15049 (arg, [&] (breakpoint *b)
15050 {
15051 iterate_over_related_breakpoints (b, delete_breakpoint);
15052 });
15053 }
15054
15055 /* Helper function for trace_pass_command. */
15056
15057 static void
15058 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15059 {
15060 tp->pass_count = count;
15061 observer_notify_breakpoint_modified (tp);
15062 if (from_tty)
15063 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15064 tp->number, count);
15065 }
15066
15067 /* Set passcount for tracepoint.
15068
15069 First command argument is passcount, second is tracepoint number.
15070 If tracepoint number omitted, apply to most recently defined.
15071 Also accepts special argument "all". */
15072
15073 static void
15074 trace_pass_command (char *args, int from_tty)
15075 {
15076 struct tracepoint *t1;
15077 unsigned int count;
15078
15079 if (args == 0 || *args == 0)
15080 error (_("passcount command requires an "
15081 "argument (count + optional TP num)"));
15082
15083 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15084
15085 args = skip_spaces (args);
15086 if (*args && strncasecmp (args, "all", 3) == 0)
15087 {
15088 struct breakpoint *b;
15089
15090 args += 3; /* Skip special argument "all". */
15091 if (*args)
15092 error (_("Junk at end of arguments."));
15093
15094 ALL_TRACEPOINTS (b)
15095 {
15096 t1 = (struct tracepoint *) b;
15097 trace_pass_set_count (t1, count, from_tty);
15098 }
15099 }
15100 else if (*args == '\0')
15101 {
15102 t1 = get_tracepoint_by_number (&args, NULL);
15103 if (t1)
15104 trace_pass_set_count (t1, count, from_tty);
15105 }
15106 else
15107 {
15108 number_or_range_parser parser (args);
15109 while (!parser.finished ())
15110 {
15111 t1 = get_tracepoint_by_number (&args, &parser);
15112 if (t1)
15113 trace_pass_set_count (t1, count, from_tty);
15114 }
15115 }
15116 }
15117
15118 struct tracepoint *
15119 get_tracepoint (int num)
15120 {
15121 struct breakpoint *t;
15122
15123 ALL_TRACEPOINTS (t)
15124 if (t->number == num)
15125 return (struct tracepoint *) t;
15126
15127 return NULL;
15128 }
15129
15130 /* Find the tracepoint with the given target-side number (which may be
15131 different from the tracepoint number after disconnecting and
15132 reconnecting). */
15133
15134 struct tracepoint *
15135 get_tracepoint_by_number_on_target (int num)
15136 {
15137 struct breakpoint *b;
15138
15139 ALL_TRACEPOINTS (b)
15140 {
15141 struct tracepoint *t = (struct tracepoint *) b;
15142
15143 if (t->number_on_target == num)
15144 return t;
15145 }
15146
15147 return NULL;
15148 }
15149
15150 /* Utility: parse a tracepoint number and look it up in the list.
15151 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15152 If the argument is missing, the most recent tracepoint
15153 (tracepoint_count) is returned. */
15154
15155 struct tracepoint *
15156 get_tracepoint_by_number (char **arg,
15157 number_or_range_parser *parser)
15158 {
15159 struct breakpoint *t;
15160 int tpnum;
15161 char *instring = arg == NULL ? NULL : *arg;
15162
15163 if (parser != NULL)
15164 {
15165 gdb_assert (!parser->finished ());
15166 tpnum = parser->get_number ();
15167 }
15168 else if (arg == NULL || *arg == NULL || ! **arg)
15169 tpnum = tracepoint_count;
15170 else
15171 tpnum = get_number (arg);
15172
15173 if (tpnum <= 0)
15174 {
15175 if (instring && *instring)
15176 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15177 instring);
15178 else
15179 printf_filtered (_("No previous tracepoint\n"));
15180 return NULL;
15181 }
15182
15183 ALL_TRACEPOINTS (t)
15184 if (t->number == tpnum)
15185 {
15186 return (struct tracepoint *) t;
15187 }
15188
15189 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15190 return NULL;
15191 }
15192
15193 void
15194 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15195 {
15196 if (b->thread != -1)
15197 fprintf_unfiltered (fp, " thread %d", b->thread);
15198
15199 if (b->task != 0)
15200 fprintf_unfiltered (fp, " task %d", b->task);
15201
15202 fprintf_unfiltered (fp, "\n");
15203 }
15204
15205 /* Save information on user settable breakpoints (watchpoints, etc) to
15206 a new script file named FILENAME. If FILTER is non-NULL, call it
15207 on each breakpoint and only include the ones for which it returns
15208 non-zero. */
15209
15210 static void
15211 save_breakpoints (char *filename, int from_tty,
15212 int (*filter) (const struct breakpoint *))
15213 {
15214 struct breakpoint *tp;
15215 int any = 0;
15216 int extra_trace_bits = 0;
15217
15218 if (filename == 0 || *filename == 0)
15219 error (_("Argument required (file name in which to save)"));
15220
15221 /* See if we have anything to save. */
15222 ALL_BREAKPOINTS (tp)
15223 {
15224 /* Skip internal and momentary breakpoints. */
15225 if (!user_breakpoint_p (tp))
15226 continue;
15227
15228 /* If we have a filter, only save the breakpoints it accepts. */
15229 if (filter && !filter (tp))
15230 continue;
15231
15232 any = 1;
15233
15234 if (is_tracepoint (tp))
15235 {
15236 extra_trace_bits = 1;
15237
15238 /* We can stop searching. */
15239 break;
15240 }
15241 }
15242
15243 if (!any)
15244 {
15245 warning (_("Nothing to save."));
15246 return;
15247 }
15248
15249 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15250
15251 stdio_file fp;
15252
15253 if (!fp.open (expanded_filename.get (), "w"))
15254 error (_("Unable to open file '%s' for saving (%s)"),
15255 expanded_filename.get (), safe_strerror (errno));
15256
15257 if (extra_trace_bits)
15258 save_trace_state_variables (&fp);
15259
15260 ALL_BREAKPOINTS (tp)
15261 {
15262 /* Skip internal and momentary breakpoints. */
15263 if (!user_breakpoint_p (tp))
15264 continue;
15265
15266 /* If we have a filter, only save the breakpoints it accepts. */
15267 if (filter && !filter (tp))
15268 continue;
15269
15270 tp->ops->print_recreate (tp, &fp);
15271
15272 /* Note, we can't rely on tp->number for anything, as we can't
15273 assume the recreated breakpoint numbers will match. Use $bpnum
15274 instead. */
15275
15276 if (tp->cond_string)
15277 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15278
15279 if (tp->ignore_count)
15280 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15281
15282 if (tp->type != bp_dprintf && tp->commands)
15283 {
15284 fp.puts (" commands\n");
15285
15286 current_uiout->redirect (&fp);
15287 TRY
15288 {
15289 print_command_lines (current_uiout, tp->commands.get (), 2);
15290 }
15291 CATCH (ex, RETURN_MASK_ALL)
15292 {
15293 current_uiout->redirect (NULL);
15294 throw_exception (ex);
15295 }
15296 END_CATCH
15297
15298 current_uiout->redirect (NULL);
15299 fp.puts (" end\n");
15300 }
15301
15302 if (tp->enable_state == bp_disabled)
15303 fp.puts ("disable $bpnum\n");
15304
15305 /* If this is a multi-location breakpoint, check if the locations
15306 should be individually disabled. Watchpoint locations are
15307 special, and not user visible. */
15308 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15309 {
15310 struct bp_location *loc;
15311 int n = 1;
15312
15313 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15314 if (!loc->enabled)
15315 fp.printf ("disable $bpnum.%d\n", n);
15316 }
15317 }
15318
15319 if (extra_trace_bits && *default_collect)
15320 fp.printf ("set default-collect %s\n", default_collect);
15321
15322 if (from_tty)
15323 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15324 }
15325
15326 /* The `save breakpoints' command. */
15327
15328 static void
15329 save_breakpoints_command (char *args, int from_tty)
15330 {
15331 save_breakpoints (args, from_tty, NULL);
15332 }
15333
15334 /* The `save tracepoints' command. */
15335
15336 static void
15337 save_tracepoints_command (char *args, int from_tty)
15338 {
15339 save_breakpoints (args, from_tty, is_tracepoint);
15340 }
15341
15342 /* Create a vector of all tracepoints. */
15343
15344 VEC(breakpoint_p) *
15345 all_tracepoints (void)
15346 {
15347 VEC(breakpoint_p) *tp_vec = 0;
15348 struct breakpoint *tp;
15349
15350 ALL_TRACEPOINTS (tp)
15351 {
15352 VEC_safe_push (breakpoint_p, tp_vec, tp);
15353 }
15354
15355 return tp_vec;
15356 }
15357
15358 \f
15359 /* This help string is used to consolidate all the help string for specifying
15360 locations used by several commands. */
15361
15362 #define LOCATION_HELP_STRING \
15363 "Linespecs are colon-separated lists of location parameters, such as\n\
15364 source filename, function name, label name, and line number.\n\
15365 Example: To specify the start of a label named \"the_top\" in the\n\
15366 function \"fact\" in the file \"factorial.c\", use\n\
15367 \"factorial.c:fact:the_top\".\n\
15368 \n\
15369 Address locations begin with \"*\" and specify an exact address in the\n\
15370 program. Example: To specify the fourth byte past the start function\n\
15371 \"main\", use \"*main + 4\".\n\
15372 \n\
15373 Explicit locations are similar to linespecs but use an option/argument\n\
15374 syntax to specify location parameters.\n\
15375 Example: To specify the start of the label named \"the_top\" in the\n\
15376 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15377 -function fact -label the_top\".\n"
15378
15379 /* This help string is used for the break, hbreak, tbreak and thbreak
15380 commands. It is defined as a macro to prevent duplication.
15381 COMMAND should be a string constant containing the name of the
15382 command. */
15383
15384 #define BREAK_ARGS_HELP(command) \
15385 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15386 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15387 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15388 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15389 `-probe-dtrace' (for a DTrace probe).\n\
15390 LOCATION may be a linespec, address, or explicit location as described\n\
15391 below.\n\
15392 \n\
15393 With no LOCATION, uses current execution address of the selected\n\
15394 stack frame. This is useful for breaking on return to a stack frame.\n\
15395 \n\
15396 THREADNUM is the number from \"info threads\".\n\
15397 CONDITION is a boolean expression.\n\
15398 \n" LOCATION_HELP_STRING "\n\
15399 Multiple breakpoints at one place are permitted, and useful if their\n\
15400 conditions are different.\n\
15401 \n\
15402 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15403
15404 /* List of subcommands for "catch". */
15405 static struct cmd_list_element *catch_cmdlist;
15406
15407 /* List of subcommands for "tcatch". */
15408 static struct cmd_list_element *tcatch_cmdlist;
15409
15410 void
15411 add_catch_command (const char *name, const char *docstring,
15412 cmd_sfunc_ftype *sfunc,
15413 completer_ftype *completer,
15414 void *user_data_catch,
15415 void *user_data_tcatch)
15416 {
15417 struct cmd_list_element *command;
15418
15419 command = add_cmd (name, class_breakpoint, docstring,
15420 &catch_cmdlist);
15421 set_cmd_sfunc (command, sfunc);
15422 set_cmd_context (command, user_data_catch);
15423 set_cmd_completer (command, completer);
15424
15425 command = add_cmd (name, class_breakpoint, docstring,
15426 &tcatch_cmdlist);
15427 set_cmd_sfunc (command, sfunc);
15428 set_cmd_context (command, user_data_tcatch);
15429 set_cmd_completer (command, completer);
15430 }
15431
15432 static void
15433 save_command (char *arg, int from_tty)
15434 {
15435 printf_unfiltered (_("\"save\" must be followed by "
15436 "the name of a save subcommand.\n"));
15437 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15438 }
15439
15440 struct breakpoint *
15441 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15442 void *data)
15443 {
15444 struct breakpoint *b, *b_tmp;
15445
15446 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15447 {
15448 if ((*callback) (b, data))
15449 return b;
15450 }
15451
15452 return NULL;
15453 }
15454
15455 /* Zero if any of the breakpoint's locations could be a location where
15456 functions have been inlined, nonzero otherwise. */
15457
15458 static int
15459 is_non_inline_function (struct breakpoint *b)
15460 {
15461 /* The shared library event breakpoint is set on the address of a
15462 non-inline function. */
15463 if (b->type == bp_shlib_event)
15464 return 1;
15465
15466 return 0;
15467 }
15468
15469 /* Nonzero if the specified PC cannot be a location where functions
15470 have been inlined. */
15471
15472 int
15473 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15474 const struct target_waitstatus *ws)
15475 {
15476 struct breakpoint *b;
15477 struct bp_location *bl;
15478
15479 ALL_BREAKPOINTS (b)
15480 {
15481 if (!is_non_inline_function (b))
15482 continue;
15483
15484 for (bl = b->loc; bl != NULL; bl = bl->next)
15485 {
15486 if (!bl->shlib_disabled
15487 && bpstat_check_location (bl, aspace, pc, ws))
15488 return 1;
15489 }
15490 }
15491
15492 return 0;
15493 }
15494
15495 /* Remove any references to OBJFILE which is going to be freed. */
15496
15497 void
15498 breakpoint_free_objfile (struct objfile *objfile)
15499 {
15500 struct bp_location **locp, *loc;
15501
15502 ALL_BP_LOCATIONS (loc, locp)
15503 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15504 loc->symtab = NULL;
15505 }
15506
15507 void
15508 initialize_breakpoint_ops (void)
15509 {
15510 static int initialized = 0;
15511
15512 struct breakpoint_ops *ops;
15513
15514 if (initialized)
15515 return;
15516 initialized = 1;
15517
15518 /* The breakpoint_ops structure to be inherit by all kinds of
15519 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15520 internal and momentary breakpoints, etc.). */
15521 ops = &bkpt_base_breakpoint_ops;
15522 *ops = base_breakpoint_ops;
15523 ops->re_set = bkpt_re_set;
15524 ops->insert_location = bkpt_insert_location;
15525 ops->remove_location = bkpt_remove_location;
15526 ops->breakpoint_hit = bkpt_breakpoint_hit;
15527 ops->create_sals_from_location = bkpt_create_sals_from_location;
15528 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15529 ops->decode_location = bkpt_decode_location;
15530
15531 /* The breakpoint_ops structure to be used in regular breakpoints. */
15532 ops = &bkpt_breakpoint_ops;
15533 *ops = bkpt_base_breakpoint_ops;
15534 ops->re_set = bkpt_re_set;
15535 ops->resources_needed = bkpt_resources_needed;
15536 ops->print_it = bkpt_print_it;
15537 ops->print_mention = bkpt_print_mention;
15538 ops->print_recreate = bkpt_print_recreate;
15539
15540 /* Ranged breakpoints. */
15541 ops = &ranged_breakpoint_ops;
15542 *ops = bkpt_breakpoint_ops;
15543 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15544 ops->resources_needed = resources_needed_ranged_breakpoint;
15545 ops->print_it = print_it_ranged_breakpoint;
15546 ops->print_one = print_one_ranged_breakpoint;
15547 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15548 ops->print_mention = print_mention_ranged_breakpoint;
15549 ops->print_recreate = print_recreate_ranged_breakpoint;
15550
15551 /* Internal breakpoints. */
15552 ops = &internal_breakpoint_ops;
15553 *ops = bkpt_base_breakpoint_ops;
15554 ops->re_set = internal_bkpt_re_set;
15555 ops->check_status = internal_bkpt_check_status;
15556 ops->print_it = internal_bkpt_print_it;
15557 ops->print_mention = internal_bkpt_print_mention;
15558
15559 /* Momentary breakpoints. */
15560 ops = &momentary_breakpoint_ops;
15561 *ops = bkpt_base_breakpoint_ops;
15562 ops->re_set = momentary_bkpt_re_set;
15563 ops->check_status = momentary_bkpt_check_status;
15564 ops->print_it = momentary_bkpt_print_it;
15565 ops->print_mention = momentary_bkpt_print_mention;
15566
15567 /* Probe breakpoints. */
15568 ops = &bkpt_probe_breakpoint_ops;
15569 *ops = bkpt_breakpoint_ops;
15570 ops->insert_location = bkpt_probe_insert_location;
15571 ops->remove_location = bkpt_probe_remove_location;
15572 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15573 ops->decode_location = bkpt_probe_decode_location;
15574
15575 /* Watchpoints. */
15576 ops = &watchpoint_breakpoint_ops;
15577 *ops = base_breakpoint_ops;
15578 ops->re_set = re_set_watchpoint;
15579 ops->insert_location = insert_watchpoint;
15580 ops->remove_location = remove_watchpoint;
15581 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15582 ops->check_status = check_status_watchpoint;
15583 ops->resources_needed = resources_needed_watchpoint;
15584 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15585 ops->print_it = print_it_watchpoint;
15586 ops->print_mention = print_mention_watchpoint;
15587 ops->print_recreate = print_recreate_watchpoint;
15588 ops->explains_signal = explains_signal_watchpoint;
15589
15590 /* Masked watchpoints. */
15591 ops = &masked_watchpoint_breakpoint_ops;
15592 *ops = watchpoint_breakpoint_ops;
15593 ops->insert_location = insert_masked_watchpoint;
15594 ops->remove_location = remove_masked_watchpoint;
15595 ops->resources_needed = resources_needed_masked_watchpoint;
15596 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15597 ops->print_it = print_it_masked_watchpoint;
15598 ops->print_one_detail = print_one_detail_masked_watchpoint;
15599 ops->print_mention = print_mention_masked_watchpoint;
15600 ops->print_recreate = print_recreate_masked_watchpoint;
15601
15602 /* Tracepoints. */
15603 ops = &tracepoint_breakpoint_ops;
15604 *ops = base_breakpoint_ops;
15605 ops->re_set = tracepoint_re_set;
15606 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15607 ops->print_one_detail = tracepoint_print_one_detail;
15608 ops->print_mention = tracepoint_print_mention;
15609 ops->print_recreate = tracepoint_print_recreate;
15610 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15611 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15612 ops->decode_location = tracepoint_decode_location;
15613
15614 /* Probe tracepoints. */
15615 ops = &tracepoint_probe_breakpoint_ops;
15616 *ops = tracepoint_breakpoint_ops;
15617 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15618 ops->decode_location = tracepoint_probe_decode_location;
15619
15620 /* Static tracepoints with marker (`-m'). */
15621 ops = &strace_marker_breakpoint_ops;
15622 *ops = tracepoint_breakpoint_ops;
15623 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15624 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15625 ops->decode_location = strace_marker_decode_location;
15626
15627 /* Fork catchpoints. */
15628 ops = &catch_fork_breakpoint_ops;
15629 *ops = base_breakpoint_ops;
15630 ops->insert_location = insert_catch_fork;
15631 ops->remove_location = remove_catch_fork;
15632 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15633 ops->print_it = print_it_catch_fork;
15634 ops->print_one = print_one_catch_fork;
15635 ops->print_mention = print_mention_catch_fork;
15636 ops->print_recreate = print_recreate_catch_fork;
15637
15638 /* Vfork catchpoints. */
15639 ops = &catch_vfork_breakpoint_ops;
15640 *ops = base_breakpoint_ops;
15641 ops->insert_location = insert_catch_vfork;
15642 ops->remove_location = remove_catch_vfork;
15643 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15644 ops->print_it = print_it_catch_vfork;
15645 ops->print_one = print_one_catch_vfork;
15646 ops->print_mention = print_mention_catch_vfork;
15647 ops->print_recreate = print_recreate_catch_vfork;
15648
15649 /* Exec catchpoints. */
15650 ops = &catch_exec_breakpoint_ops;
15651 *ops = base_breakpoint_ops;
15652 ops->insert_location = insert_catch_exec;
15653 ops->remove_location = remove_catch_exec;
15654 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15655 ops->print_it = print_it_catch_exec;
15656 ops->print_one = print_one_catch_exec;
15657 ops->print_mention = print_mention_catch_exec;
15658 ops->print_recreate = print_recreate_catch_exec;
15659
15660 /* Solib-related catchpoints. */
15661 ops = &catch_solib_breakpoint_ops;
15662 *ops = base_breakpoint_ops;
15663 ops->insert_location = insert_catch_solib;
15664 ops->remove_location = remove_catch_solib;
15665 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15666 ops->check_status = check_status_catch_solib;
15667 ops->print_it = print_it_catch_solib;
15668 ops->print_one = print_one_catch_solib;
15669 ops->print_mention = print_mention_catch_solib;
15670 ops->print_recreate = print_recreate_catch_solib;
15671
15672 ops = &dprintf_breakpoint_ops;
15673 *ops = bkpt_base_breakpoint_ops;
15674 ops->re_set = dprintf_re_set;
15675 ops->resources_needed = bkpt_resources_needed;
15676 ops->print_it = bkpt_print_it;
15677 ops->print_mention = bkpt_print_mention;
15678 ops->print_recreate = dprintf_print_recreate;
15679 ops->after_condition_true = dprintf_after_condition_true;
15680 ops->breakpoint_hit = dprintf_breakpoint_hit;
15681 }
15682
15683 /* Chain containing all defined "enable breakpoint" subcommands. */
15684
15685 static struct cmd_list_element *enablebreaklist = NULL;
15686
15687 void
15688 _initialize_breakpoint (void)
15689 {
15690 struct cmd_list_element *c;
15691
15692 initialize_breakpoint_ops ();
15693
15694 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15695 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15696 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15697
15698 breakpoint_objfile_key
15699 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15700
15701 breakpoint_chain = 0;
15702 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15703 before a breakpoint is set. */
15704 breakpoint_count = 0;
15705
15706 tracepoint_count = 0;
15707
15708 add_com ("ignore", class_breakpoint, ignore_command, _("\
15709 Set ignore-count of breakpoint number N to COUNT.\n\
15710 Usage is `ignore N COUNT'."));
15711
15712 add_com ("commands", class_breakpoint, commands_command, _("\
15713 Set commands to be executed when the given breakpoints are hit.\n\
15714 Give a space-separated breakpoint list as argument after \"commands\".\n\
15715 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15716 (e.g. `5-7').\n\
15717 With no argument, the targeted breakpoint is the last one set.\n\
15718 The commands themselves follow starting on the next line.\n\
15719 Type a line containing \"end\" to indicate the end of them.\n\
15720 Give \"silent\" as the first line to make the breakpoint silent;\n\
15721 then no output is printed when it is hit, except what the commands print."));
15722
15723 c = add_com ("condition", class_breakpoint, condition_command, _("\
15724 Specify breakpoint number N to break only if COND is true.\n\
15725 Usage is `condition N COND', where N is an integer and COND is an\n\
15726 expression to be evaluated whenever breakpoint N is reached."));
15727 set_cmd_completer (c, condition_completer);
15728
15729 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15730 Set a temporary breakpoint.\n\
15731 Like \"break\" except the breakpoint is only temporary,\n\
15732 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15733 by using \"enable delete\" on the breakpoint number.\n\
15734 \n"
15735 BREAK_ARGS_HELP ("tbreak")));
15736 set_cmd_completer (c, location_completer);
15737
15738 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15739 Set a hardware assisted breakpoint.\n\
15740 Like \"break\" except the breakpoint requires hardware support,\n\
15741 some target hardware may not have this support.\n\
15742 \n"
15743 BREAK_ARGS_HELP ("hbreak")));
15744 set_cmd_completer (c, location_completer);
15745
15746 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15747 Set a temporary hardware assisted breakpoint.\n\
15748 Like \"hbreak\" except the breakpoint is only temporary,\n\
15749 so it will be deleted when hit.\n\
15750 \n"
15751 BREAK_ARGS_HELP ("thbreak")));
15752 set_cmd_completer (c, location_completer);
15753
15754 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15755 Enable some breakpoints.\n\
15756 Give breakpoint numbers (separated by spaces) as arguments.\n\
15757 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15758 This is used to cancel the effect of the \"disable\" command.\n\
15759 With a subcommand you can enable temporarily."),
15760 &enablelist, "enable ", 1, &cmdlist);
15761
15762 add_com_alias ("en", "enable", class_breakpoint, 1);
15763
15764 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15765 Enable some breakpoints.\n\
15766 Give breakpoint numbers (separated by spaces) as arguments.\n\
15767 This is used to cancel the effect of the \"disable\" command.\n\
15768 May be abbreviated to simply \"enable\".\n"),
15769 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15770
15771 add_cmd ("once", no_class, enable_once_command, _("\
15772 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15773 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15774 &enablebreaklist);
15775
15776 add_cmd ("delete", no_class, enable_delete_command, _("\
15777 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15778 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15779 &enablebreaklist);
15780
15781 add_cmd ("count", no_class, enable_count_command, _("\
15782 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15783 If a breakpoint is hit while enabled in this fashion,\n\
15784 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15785 &enablebreaklist);
15786
15787 add_cmd ("delete", no_class, enable_delete_command, _("\
15788 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15789 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15790 &enablelist);
15791
15792 add_cmd ("once", no_class, enable_once_command, _("\
15793 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15794 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15795 &enablelist);
15796
15797 add_cmd ("count", no_class, enable_count_command, _("\
15798 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15799 If a breakpoint is hit while enabled in this fashion,\n\
15800 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15801 &enablelist);
15802
15803 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15804 Disable some breakpoints.\n\
15805 Arguments are breakpoint numbers with spaces in between.\n\
15806 To disable all breakpoints, give no argument.\n\
15807 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15808 &disablelist, "disable ", 1, &cmdlist);
15809 add_com_alias ("dis", "disable", class_breakpoint, 1);
15810 add_com_alias ("disa", "disable", class_breakpoint, 1);
15811
15812 add_cmd ("breakpoints", class_alias, disable_command, _("\
15813 Disable some breakpoints.\n\
15814 Arguments are breakpoint numbers with spaces in between.\n\
15815 To disable all breakpoints, give no argument.\n\
15816 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15817 This command may be abbreviated \"disable\"."),
15818 &disablelist);
15819
15820 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15821 Delete some breakpoints or auto-display expressions.\n\
15822 Arguments are breakpoint numbers with spaces in between.\n\
15823 To delete all breakpoints, give no argument.\n\
15824 \n\
15825 Also a prefix command for deletion of other GDB objects.\n\
15826 The \"unset\" command is also an alias for \"delete\"."),
15827 &deletelist, "delete ", 1, &cmdlist);
15828 add_com_alias ("d", "delete", class_breakpoint, 1);
15829 add_com_alias ("del", "delete", class_breakpoint, 1);
15830
15831 add_cmd ("breakpoints", class_alias, delete_command, _("\
15832 Delete some breakpoints or auto-display expressions.\n\
15833 Arguments are breakpoint numbers with spaces in between.\n\
15834 To delete all breakpoints, give no argument.\n\
15835 This command may be abbreviated \"delete\"."),
15836 &deletelist);
15837
15838 add_com ("clear", class_breakpoint, clear_command, _("\
15839 Clear breakpoint at specified location.\n\
15840 Argument may be a linespec, explicit, or address location as described below.\n\
15841 \n\
15842 With no argument, clears all breakpoints in the line that the selected frame\n\
15843 is executing in.\n"
15844 "\n" LOCATION_HELP_STRING "\n\
15845 See also the \"delete\" command which clears breakpoints by number."));
15846 add_com_alias ("cl", "clear", class_breakpoint, 1);
15847
15848 c = add_com ("break", class_breakpoint, break_command, _("\
15849 Set breakpoint at specified location.\n"
15850 BREAK_ARGS_HELP ("break")));
15851 set_cmd_completer (c, location_completer);
15852
15853 add_com_alias ("b", "break", class_run, 1);
15854 add_com_alias ("br", "break", class_run, 1);
15855 add_com_alias ("bre", "break", class_run, 1);
15856 add_com_alias ("brea", "break", class_run, 1);
15857
15858 if (dbx_commands)
15859 {
15860 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15861 Break in function/address or break at a line in the current file."),
15862 &stoplist, "stop ", 1, &cmdlist);
15863 add_cmd ("in", class_breakpoint, stopin_command,
15864 _("Break in function or address."), &stoplist);
15865 add_cmd ("at", class_breakpoint, stopat_command,
15866 _("Break at a line in the current file."), &stoplist);
15867 add_com ("status", class_info, info_breakpoints_command, _("\
15868 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15869 The \"Type\" column indicates one of:\n\
15870 \tbreakpoint - normal breakpoint\n\
15871 \twatchpoint - watchpoint\n\
15872 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15873 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15874 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15875 address and file/line number respectively.\n\
15876 \n\
15877 Convenience variable \"$_\" and default examine address for \"x\"\n\
15878 are set to the address of the last breakpoint listed unless the command\n\
15879 is prefixed with \"server \".\n\n\
15880 Convenience variable \"$bpnum\" contains the number of the last\n\
15881 breakpoint set."));
15882 }
15883
15884 add_info ("breakpoints", info_breakpoints_command, _("\
15885 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15886 The \"Type\" column indicates one of:\n\
15887 \tbreakpoint - normal breakpoint\n\
15888 \twatchpoint - watchpoint\n\
15889 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15890 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15891 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15892 address and file/line number respectively.\n\
15893 \n\
15894 Convenience variable \"$_\" and default examine address for \"x\"\n\
15895 are set to the address of the last breakpoint listed unless the command\n\
15896 is prefixed with \"server \".\n\n\
15897 Convenience variable \"$bpnum\" contains the number of the last\n\
15898 breakpoint set."));
15899
15900 add_info_alias ("b", "breakpoints", 1);
15901
15902 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15903 Status of all breakpoints, or breakpoint number NUMBER.\n\
15904 The \"Type\" column indicates one of:\n\
15905 \tbreakpoint - normal breakpoint\n\
15906 \twatchpoint - watchpoint\n\
15907 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15908 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15909 \tuntil - internal breakpoint used by the \"until\" command\n\
15910 \tfinish - internal breakpoint used by the \"finish\" command\n\
15911 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15912 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15913 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15914 address and file/line number respectively.\n\
15915 \n\
15916 Convenience variable \"$_\" and default examine address for \"x\"\n\
15917 are set to the address of the last breakpoint listed unless the command\n\
15918 is prefixed with \"server \".\n\n\
15919 Convenience variable \"$bpnum\" contains the number of the last\n\
15920 breakpoint set."),
15921 &maintenanceinfolist);
15922
15923 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15924 Set catchpoints to catch events."),
15925 &catch_cmdlist, "catch ",
15926 0/*allow-unknown*/, &cmdlist);
15927
15928 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15929 Set temporary catchpoints to catch events."),
15930 &tcatch_cmdlist, "tcatch ",
15931 0/*allow-unknown*/, &cmdlist);
15932
15933 add_catch_command ("fork", _("Catch calls to fork."),
15934 catch_fork_command_1,
15935 NULL,
15936 (void *) (uintptr_t) catch_fork_permanent,
15937 (void *) (uintptr_t) catch_fork_temporary);
15938 add_catch_command ("vfork", _("Catch calls to vfork."),
15939 catch_fork_command_1,
15940 NULL,
15941 (void *) (uintptr_t) catch_vfork_permanent,
15942 (void *) (uintptr_t) catch_vfork_temporary);
15943 add_catch_command ("exec", _("Catch calls to exec."),
15944 catch_exec_command_1,
15945 NULL,
15946 CATCH_PERMANENT,
15947 CATCH_TEMPORARY);
15948 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15949 Usage: catch load [REGEX]\n\
15950 If REGEX is given, only stop for libraries matching the regular expression."),
15951 catch_load_command_1,
15952 NULL,
15953 CATCH_PERMANENT,
15954 CATCH_TEMPORARY);
15955 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15956 Usage: catch unload [REGEX]\n\
15957 If REGEX is given, only stop for libraries matching the regular expression."),
15958 catch_unload_command_1,
15959 NULL,
15960 CATCH_PERMANENT,
15961 CATCH_TEMPORARY);
15962
15963 c = add_com ("watch", class_breakpoint, watch_command, _("\
15964 Set a watchpoint for an expression.\n\
15965 Usage: watch [-l|-location] EXPRESSION\n\
15966 A watchpoint stops execution of your program whenever the value of\n\
15967 an expression changes.\n\
15968 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15969 the memory to which it refers."));
15970 set_cmd_completer (c, expression_completer);
15971
15972 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15973 Set a read watchpoint for an expression.\n\
15974 Usage: rwatch [-l|-location] EXPRESSION\n\
15975 A watchpoint stops execution of your program whenever the value of\n\
15976 an expression is read.\n\
15977 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15978 the memory to which it refers."));
15979 set_cmd_completer (c, expression_completer);
15980
15981 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15982 Set a watchpoint for an expression.\n\
15983 Usage: awatch [-l|-location] EXPRESSION\n\
15984 A watchpoint stops execution of your program whenever the value of\n\
15985 an expression is either read or written.\n\
15986 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15987 the memory to which it refers."));
15988 set_cmd_completer (c, expression_completer);
15989
15990 add_info ("watchpoints", info_watchpoints_command, _("\
15991 Status of specified watchpoints (all watchpoints if no argument)."));
15992
15993 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15994 respond to changes - contrary to the description. */
15995 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15996 &can_use_hw_watchpoints, _("\
15997 Set debugger's willingness to use watchpoint hardware."), _("\
15998 Show debugger's willingness to use watchpoint hardware."), _("\
15999 If zero, gdb will not use hardware for new watchpoints, even if\n\
16000 such is available. (However, any hardware watchpoints that were\n\
16001 created before setting this to nonzero, will continue to use watchpoint\n\
16002 hardware.)"),
16003 NULL,
16004 show_can_use_hw_watchpoints,
16005 &setlist, &showlist);
16006
16007 can_use_hw_watchpoints = 1;
16008
16009 /* Tracepoint manipulation commands. */
16010
16011 c = add_com ("trace", class_breakpoint, trace_command, _("\
16012 Set a tracepoint at specified location.\n\
16013 \n"
16014 BREAK_ARGS_HELP ("trace") "\n\
16015 Do \"help tracepoints\" for info on other tracepoint commands."));
16016 set_cmd_completer (c, location_completer);
16017
16018 add_com_alias ("tp", "trace", class_alias, 0);
16019 add_com_alias ("tr", "trace", class_alias, 1);
16020 add_com_alias ("tra", "trace", class_alias, 1);
16021 add_com_alias ("trac", "trace", class_alias, 1);
16022
16023 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16024 Set a fast tracepoint at specified location.\n\
16025 \n"
16026 BREAK_ARGS_HELP ("ftrace") "\n\
16027 Do \"help tracepoints\" for info on other tracepoint commands."));
16028 set_cmd_completer (c, location_completer);
16029
16030 c = add_com ("strace", class_breakpoint, strace_command, _("\
16031 Set a static tracepoint at location or marker.\n\
16032 \n\
16033 strace [LOCATION] [if CONDITION]\n\
16034 LOCATION may be a linespec, explicit, or address location (described below) \n\
16035 or -m MARKER_ID.\n\n\
16036 If a marker id is specified, probe the marker with that name. With\n\
16037 no LOCATION, uses current execution address of the selected stack frame.\n\
16038 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16039 This collects arbitrary user data passed in the probe point call to the\n\
16040 tracing library. You can inspect it when analyzing the trace buffer,\n\
16041 by printing the $_sdata variable like any other convenience variable.\n\
16042 \n\
16043 CONDITION is a boolean expression.\n\
16044 \n" LOCATION_HELP_STRING "\n\
16045 Multiple tracepoints at one place are permitted, and useful if their\n\
16046 conditions are different.\n\
16047 \n\
16048 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16049 Do \"help tracepoints\" for info on other tracepoint commands."));
16050 set_cmd_completer (c, location_completer);
16051
16052 add_info ("tracepoints", info_tracepoints_command, _("\
16053 Status of specified tracepoints (all tracepoints if no argument).\n\
16054 Convenience variable \"$tpnum\" contains the number of the\n\
16055 last tracepoint set."));
16056
16057 add_info_alias ("tp", "tracepoints", 1);
16058
16059 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16060 Delete specified tracepoints.\n\
16061 Arguments are tracepoint numbers, separated by spaces.\n\
16062 No argument means delete all tracepoints."),
16063 &deletelist);
16064 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16065
16066 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16067 Disable specified tracepoints.\n\
16068 Arguments are tracepoint numbers, separated by spaces.\n\
16069 No argument means disable all tracepoints."),
16070 &disablelist);
16071 deprecate_cmd (c, "disable");
16072
16073 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16074 Enable specified tracepoints.\n\
16075 Arguments are tracepoint numbers, separated by spaces.\n\
16076 No argument means enable all tracepoints."),
16077 &enablelist);
16078 deprecate_cmd (c, "enable");
16079
16080 add_com ("passcount", class_trace, trace_pass_command, _("\
16081 Set the passcount for a tracepoint.\n\
16082 The trace will end when the tracepoint has been passed 'count' times.\n\
16083 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16084 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16085
16086 add_prefix_cmd ("save", class_breakpoint, save_command,
16087 _("Save breakpoint definitions as a script."),
16088 &save_cmdlist, "save ",
16089 0/*allow-unknown*/, &cmdlist);
16090
16091 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16092 Save current breakpoint definitions as a script.\n\
16093 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16094 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16095 session to restore them."),
16096 &save_cmdlist);
16097 set_cmd_completer (c, filename_completer);
16098
16099 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16100 Save current tracepoint definitions as a script.\n\
16101 Use the 'source' command in another debug session to restore them."),
16102 &save_cmdlist);
16103 set_cmd_completer (c, filename_completer);
16104
16105 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16106 deprecate_cmd (c, "save tracepoints");
16107
16108 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16109 Breakpoint specific settings\n\
16110 Configure various breakpoint-specific variables such as\n\
16111 pending breakpoint behavior"),
16112 &breakpoint_set_cmdlist, "set breakpoint ",
16113 0/*allow-unknown*/, &setlist);
16114 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16115 Breakpoint specific settings\n\
16116 Configure various breakpoint-specific variables such as\n\
16117 pending breakpoint behavior"),
16118 &breakpoint_show_cmdlist, "show breakpoint ",
16119 0/*allow-unknown*/, &showlist);
16120
16121 add_setshow_auto_boolean_cmd ("pending", no_class,
16122 &pending_break_support, _("\
16123 Set debugger's behavior regarding pending breakpoints."), _("\
16124 Show debugger's behavior regarding pending breakpoints."), _("\
16125 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16126 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16127 an error. If auto, an unrecognized breakpoint location results in a\n\
16128 user-query to see if a pending breakpoint should be created."),
16129 NULL,
16130 show_pending_break_support,
16131 &breakpoint_set_cmdlist,
16132 &breakpoint_show_cmdlist);
16133
16134 pending_break_support = AUTO_BOOLEAN_AUTO;
16135
16136 add_setshow_boolean_cmd ("auto-hw", no_class,
16137 &automatic_hardware_breakpoints, _("\
16138 Set automatic usage of hardware breakpoints."), _("\
16139 Show automatic usage of hardware breakpoints."), _("\
16140 If set, the debugger will automatically use hardware breakpoints for\n\
16141 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16142 a warning will be emitted for such breakpoints."),
16143 NULL,
16144 show_automatic_hardware_breakpoints,
16145 &breakpoint_set_cmdlist,
16146 &breakpoint_show_cmdlist);
16147
16148 add_setshow_boolean_cmd ("always-inserted", class_support,
16149 &always_inserted_mode, _("\
16150 Set mode for inserting breakpoints."), _("\
16151 Show mode for inserting breakpoints."), _("\
16152 When this mode is on, breakpoints are inserted immediately as soon as\n\
16153 they're created, kept inserted even when execution stops, and removed\n\
16154 only when the user deletes them. When this mode is off (the default),\n\
16155 breakpoints are inserted only when execution continues, and removed\n\
16156 when execution stops."),
16157 NULL,
16158 &show_always_inserted_mode,
16159 &breakpoint_set_cmdlist,
16160 &breakpoint_show_cmdlist);
16161
16162 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16163 condition_evaluation_enums,
16164 &condition_evaluation_mode_1, _("\
16165 Set mode of breakpoint condition evaluation."), _("\
16166 Show mode of breakpoint condition evaluation."), _("\
16167 When this is set to \"host\", breakpoint conditions will be\n\
16168 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16169 breakpoint conditions will be downloaded to the target (if the target\n\
16170 supports such feature) and conditions will be evaluated on the target's side.\n\
16171 If this is set to \"auto\" (default), this will be automatically set to\n\
16172 \"target\" if it supports condition evaluation, otherwise it will\n\
16173 be set to \"gdb\""),
16174 &set_condition_evaluation_mode,
16175 &show_condition_evaluation_mode,
16176 &breakpoint_set_cmdlist,
16177 &breakpoint_show_cmdlist);
16178
16179 add_com ("break-range", class_breakpoint, break_range_command, _("\
16180 Set a breakpoint for an address range.\n\
16181 break-range START-LOCATION, END-LOCATION\n\
16182 where START-LOCATION and END-LOCATION can be one of the following:\n\
16183 LINENUM, for that line in the current file,\n\
16184 FILE:LINENUM, for that line in that file,\n\
16185 +OFFSET, for that number of lines after the current line\n\
16186 or the start of the range\n\
16187 FUNCTION, for the first line in that function,\n\
16188 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16189 *ADDRESS, for the instruction at that address.\n\
16190 \n\
16191 The breakpoint will stop execution of the inferior whenever it executes\n\
16192 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16193 range (including START-LOCATION and END-LOCATION)."));
16194
16195 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16196 Set a dynamic printf at specified location.\n\
16197 dprintf location,format string,arg1,arg2,...\n\
16198 location may be a linespec, explicit, or address location.\n"
16199 "\n" LOCATION_HELP_STRING));
16200 set_cmd_completer (c, location_completer);
16201
16202 add_setshow_enum_cmd ("dprintf-style", class_support,
16203 dprintf_style_enums, &dprintf_style, _("\
16204 Set the style of usage for dynamic printf."), _("\
16205 Show the style of usage for dynamic printf."), _("\
16206 This setting chooses how GDB will do a dynamic printf.\n\
16207 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16208 console, as with the \"printf\" command.\n\
16209 If the value is \"call\", the print is done by calling a function in your\n\
16210 program; by default printf(), but you can choose a different function or\n\
16211 output stream by setting dprintf-function and dprintf-channel."),
16212 update_dprintf_commands, NULL,
16213 &setlist, &showlist);
16214
16215 dprintf_function = xstrdup ("printf");
16216 add_setshow_string_cmd ("dprintf-function", class_support,
16217 &dprintf_function, _("\
16218 Set the function to use for dynamic printf"), _("\
16219 Show the function to use for dynamic printf"), NULL,
16220 update_dprintf_commands, NULL,
16221 &setlist, &showlist);
16222
16223 dprintf_channel = xstrdup ("");
16224 add_setshow_string_cmd ("dprintf-channel", class_support,
16225 &dprintf_channel, _("\
16226 Set the channel to use for dynamic printf"), _("\
16227 Show the channel to use for dynamic printf"), NULL,
16228 update_dprintf_commands, NULL,
16229 &setlist, &showlist);
16230
16231 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16232 &disconnected_dprintf, _("\
16233 Set whether dprintf continues after GDB disconnects."), _("\
16234 Show whether dprintf continues after GDB disconnects."), _("\
16235 Use this to let dprintf commands continue to hit and produce output\n\
16236 even if GDB disconnects or detaches from the target."),
16237 NULL,
16238 NULL,
16239 &setlist, &showlist);
16240
16241 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16242 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16243 (target agent only) This is useful for formatted output in user-defined commands."));
16244
16245 automatic_hardware_breakpoints = 1;
16246
16247 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16248 observer_attach_thread_exit (remove_threaded_breakpoints);
16249 }
This page took 0.403232 seconds and 5 git commands to generate.