5549fe735810b03f6b2221b17ad12bf4e7f54dc6
[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 break_command_1 (char *, int, int);
142
143 static void mention (struct breakpoint *);
144
145 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
146 enum bptype,
147 const struct breakpoint_ops *);
148 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
149 const struct symtab_and_line *);
150
151 /* This function is used in gdbtk sources and thus can not be made
152 static. */
153 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
154 struct symtab_and_line,
155 enum bptype,
156 const struct breakpoint_ops *);
157
158 static struct breakpoint *
159 momentary_breakpoint_from_master (struct breakpoint *orig,
160 enum bptype type,
161 const struct breakpoint_ops *ops,
162 int loc_enabled);
163
164 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
165
166 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
167 CORE_ADDR bpaddr,
168 enum bptype bptype);
169
170 static void describe_other_breakpoints (struct gdbarch *,
171 struct program_space *, CORE_ADDR,
172 struct obj_section *, int);
173
174 static int watchpoint_locations_match (struct bp_location *loc1,
175 struct bp_location *loc2);
176
177 static int breakpoint_location_address_match (struct bp_location *bl,
178 struct address_space *aspace,
179 CORE_ADDR addr);
180
181 static int breakpoint_location_address_range_overlap (struct bp_location *,
182 struct address_space *,
183 CORE_ADDR, int);
184
185 static void info_breakpoints_command (char *, int);
186
187 static void info_watchpoints_command (char *, int);
188
189 static int breakpoint_1 (char *, int,
190 int (*) (const struct breakpoint *));
191
192 static int breakpoint_cond_eval (void *);
193
194 static void cleanup_executing_breakpoints (void *);
195
196 static void commands_command (char *, int);
197
198 static void condition_command (char *, int);
199
200 static int remove_breakpoint (struct bp_location *);
201 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
202
203 static enum print_stop_action print_bp_stop_message (bpstat bs);
204
205 static int watchpoint_check (void *);
206
207 static void maintenance_info_breakpoints (char *, int);
208
209 static int hw_breakpoint_used_count (void);
210
211 static int hw_watchpoint_use_count (struct breakpoint *);
212
213 static int hw_watchpoint_used_count_others (struct breakpoint *except,
214 enum bptype type,
215 int *other_type_used);
216
217 static void hbreak_command (char *, int);
218
219 static void thbreak_command (char *, int);
220
221 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
222 int count);
223
224 static void stop_command (char *arg, int from_tty);
225
226 static void stopin_command (char *arg, int from_tty);
227
228 static void stopat_command (char *arg, int from_tty);
229
230 static void tcatch_command (char *arg, int from_tty);
231
232 static void free_bp_location (struct bp_location *loc);
233 static void incref_bp_location (struct bp_location *loc);
234 static void decref_bp_location (struct bp_location **loc);
235
236 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
237
238 /* update_global_location_list's modes of operation wrt to whether to
239 insert locations now. */
240 enum ugll_insert_mode
241 {
242 /* Don't insert any breakpoint locations into the inferior, only
243 remove already-inserted locations that no longer should be
244 inserted. Functions that delete a breakpoint or breakpoints
245 should specify this mode, so that deleting a breakpoint doesn't
246 have the side effect of inserting the locations of other
247 breakpoints that are marked not-inserted, but should_be_inserted
248 returns true on them.
249
250 This behavior is useful is situations close to tear-down -- e.g.,
251 after an exec, while the target still has execution, but
252 breakpoint shadows of the previous executable image should *NOT*
253 be restored to the new image; or before detaching, where the
254 target still has execution and wants to delete breakpoints from
255 GDB's lists, and all breakpoints had already been removed from
256 the inferior. */
257 UGLL_DONT_INSERT,
258
259 /* May insert breakpoints iff breakpoints_should_be_inserted_now
260 claims breakpoints should be inserted now. */
261 UGLL_MAY_INSERT,
262
263 /* Insert locations now, irrespective of
264 breakpoints_should_be_inserted_now. E.g., say all threads are
265 stopped right now, and the user did "continue". We need to
266 insert breakpoints _before_ resuming the target, but
267 UGLL_MAY_INSERT wouldn't insert them, because
268 breakpoints_should_be_inserted_now returns false at that point,
269 as no thread is running yet. */
270 UGLL_INSERT
271 };
272
273 static void update_global_location_list (enum ugll_insert_mode);
274
275 static void update_global_location_list_nothrow (enum ugll_insert_mode);
276
277 static int is_hardware_watchpoint (const struct breakpoint *bpt);
278
279 static void insert_breakpoint_locations (void);
280
281 static void info_tracepoints_command (char *, int);
282
283 static void delete_trace_command (char *, int);
284
285 static void enable_trace_command (char *, int);
286
287 static void disable_trace_command (char *, int);
288
289 static void trace_pass_command (char *, int);
290
291 static void set_tracepoint_count (int num);
292
293 static int is_masked_watchpoint (const struct breakpoint *b);
294
295 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
296
297 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
298 otherwise. */
299
300 static int strace_marker_p (struct breakpoint *b);
301
302 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
303 that are implemented on top of software or hardware breakpoints
304 (user breakpoints, internal and momentary breakpoints, etc.). */
305 static struct breakpoint_ops bkpt_base_breakpoint_ops;
306
307 /* Internal breakpoints class type. */
308 static struct breakpoint_ops internal_breakpoint_ops;
309
310 /* Momentary breakpoints class type. */
311 static struct breakpoint_ops momentary_breakpoint_ops;
312
313 /* The breakpoint_ops structure to be used in regular user created
314 breakpoints. */
315 struct breakpoint_ops bkpt_breakpoint_ops;
316
317 /* Breakpoints set on probes. */
318 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
319
320 /* Dynamic printf class type. */
321 struct breakpoint_ops dprintf_breakpoint_ops;
322
323 /* The style in which to perform a dynamic printf. This is a user
324 option because different output options have different tradeoffs;
325 if GDB does the printing, there is better error handling if there
326 is a problem with any of the arguments, but using an inferior
327 function lets you have special-purpose printers and sending of
328 output to the same place as compiled-in print functions. */
329
330 static const char dprintf_style_gdb[] = "gdb";
331 static const char dprintf_style_call[] = "call";
332 static const char dprintf_style_agent[] = "agent";
333 static const char *const dprintf_style_enums[] = {
334 dprintf_style_gdb,
335 dprintf_style_call,
336 dprintf_style_agent,
337 NULL
338 };
339 static const char *dprintf_style = dprintf_style_gdb;
340
341 /* The function to use for dynamic printf if the preferred style is to
342 call into the inferior. The value is simply a string that is
343 copied into the command, so it can be anything that GDB can
344 evaluate to a callable address, not necessarily a function name. */
345
346 static char *dprintf_function;
347
348 /* The channel to use for dynamic printf if the preferred style is to
349 call into the inferior; if a nonempty string, it will be passed to
350 the call as the first argument, with the format string as the
351 second. As with the dprintf function, this can be anything that
352 GDB knows how to evaluate, so in addition to common choices like
353 "stderr", this could be an app-specific expression like
354 "mystreams[curlogger]". */
355
356 static char *dprintf_channel;
357
358 /* True if dprintf commands should continue to operate even if GDB
359 has disconnected. */
360 static int disconnected_dprintf = 1;
361
362 struct command_line *
363 breakpoint_commands (struct breakpoint *b)
364 {
365 return b->commands ? b->commands.get () : NULL;
366 }
367
368 /* Flag indicating that a command has proceeded the inferior past the
369 current breakpoint. */
370
371 static int breakpoint_proceeded;
372
373 const char *
374 bpdisp_text (enum bpdisp disp)
375 {
376 /* NOTE: the following values are a part of MI protocol and
377 represent values of 'disp' field returned when inferior stops at
378 a breakpoint. */
379 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
380
381 return bpdisps[(int) disp];
382 }
383
384 /* Prototypes for exported functions. */
385 /* If FALSE, gdb will not use hardware support for watchpoints, even
386 if such is available. */
387 static int can_use_hw_watchpoints;
388
389 static void
390 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
391 struct cmd_list_element *c,
392 const char *value)
393 {
394 fprintf_filtered (file,
395 _("Debugger's willingness to use "
396 "watchpoint hardware is %s.\n"),
397 value);
398 }
399
400 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
401 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
402 for unrecognized breakpoint locations.
403 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
404 static enum auto_boolean pending_break_support;
405 static void
406 show_pending_break_support (struct ui_file *file, int from_tty,
407 struct cmd_list_element *c,
408 const char *value)
409 {
410 fprintf_filtered (file,
411 _("Debugger's behavior regarding "
412 "pending breakpoints is %s.\n"),
413 value);
414 }
415
416 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
417 set with "break" but falling in read-only memory.
418 If 0, gdb will warn about such breakpoints, but won't automatically
419 use hardware breakpoints. */
420 static int automatic_hardware_breakpoints;
421 static void
422 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
423 struct cmd_list_element *c,
424 const char *value)
425 {
426 fprintf_filtered (file,
427 _("Automatic usage of hardware breakpoints is %s.\n"),
428 value);
429 }
430
431 /* If on, GDB keeps breakpoints inserted even if the inferior is
432 stopped, and immediately inserts any new breakpoints as soon as
433 they're created. If off (default), GDB keeps breakpoints off of
434 the target as long as possible. That is, it delays inserting
435 breakpoints until the next resume, and removes them again when the
436 target fully stops. This is a bit safer in case GDB crashes while
437 processing user input. */
438 static int always_inserted_mode = 0;
439
440 static void
441 show_always_inserted_mode (struct ui_file *file, int from_tty,
442 struct cmd_list_element *c, const char *value)
443 {
444 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
445 value);
446 }
447
448 /* See breakpoint.h. */
449
450 int
451 breakpoints_should_be_inserted_now (void)
452 {
453 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
454 {
455 /* If breakpoints are global, they should be inserted even if no
456 thread under gdb's control is running, or even if there are
457 no threads under GDB's control yet. */
458 return 1;
459 }
460 else if (target_has_execution)
461 {
462 struct thread_info *tp;
463
464 if (always_inserted_mode)
465 {
466 /* The user wants breakpoints inserted even if all threads
467 are stopped. */
468 return 1;
469 }
470
471 if (threads_are_executing ())
472 return 1;
473
474 /* Don't remove breakpoints yet if, even though all threads are
475 stopped, we still have events to process. */
476 ALL_NON_EXITED_THREADS (tp)
477 if (tp->resumed
478 && tp->suspend.waitstatus_pending_p)
479 return 1;
480 }
481 return 0;
482 }
483
484 static const char condition_evaluation_both[] = "host or target";
485
486 /* Modes for breakpoint condition evaluation. */
487 static const char condition_evaluation_auto[] = "auto";
488 static const char condition_evaluation_host[] = "host";
489 static const char condition_evaluation_target[] = "target";
490 static const char *const condition_evaluation_enums[] = {
491 condition_evaluation_auto,
492 condition_evaluation_host,
493 condition_evaluation_target,
494 NULL
495 };
496
497 /* Global that holds the current mode for breakpoint condition evaluation. */
498 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
499
500 /* Global that we use to display information to the user (gets its value from
501 condition_evaluation_mode_1. */
502 static const char *condition_evaluation_mode = condition_evaluation_auto;
503
504 /* Translate a condition evaluation mode MODE into either "host"
505 or "target". This is used mostly to translate from "auto" to the
506 real setting that is being used. It returns the translated
507 evaluation mode. */
508
509 static const char *
510 translate_condition_evaluation_mode (const char *mode)
511 {
512 if (mode == condition_evaluation_auto)
513 {
514 if (target_supports_evaluation_of_breakpoint_conditions ())
515 return condition_evaluation_target;
516 else
517 return condition_evaluation_host;
518 }
519 else
520 return mode;
521 }
522
523 /* Discovers what condition_evaluation_auto translates to. */
524
525 static const char *
526 breakpoint_condition_evaluation_mode (void)
527 {
528 return translate_condition_evaluation_mode (condition_evaluation_mode);
529 }
530
531 /* Return true if GDB should evaluate breakpoint conditions or false
532 otherwise. */
533
534 static int
535 gdb_evaluates_breakpoint_condition_p (void)
536 {
537 const char *mode = breakpoint_condition_evaluation_mode ();
538
539 return (mode == condition_evaluation_host);
540 }
541
542 /* Are we executing breakpoint commands? */
543 static int executing_breakpoint_commands;
544
545 /* Are overlay event breakpoints enabled? */
546 static int overlay_events_enabled;
547
548 /* See description in breakpoint.h. */
549 int target_exact_watchpoints = 0;
550
551 /* Walk the following statement or block through all breakpoints.
552 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
553 current breakpoint. */
554
555 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
556
557 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
558 for (B = breakpoint_chain; \
559 B ? (TMP=B->next, 1): 0; \
560 B = TMP)
561
562 /* Similar iterator for the low-level breakpoints. SAFE variant is
563 not provided so update_global_location_list must not be called
564 while executing the block of ALL_BP_LOCATIONS. */
565
566 #define ALL_BP_LOCATIONS(B,BP_TMP) \
567 for (BP_TMP = bp_locations; \
568 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
569 BP_TMP++)
570
571 /* Iterates through locations with address ADDRESS for the currently selected
572 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
573 to where the loop should start from.
574 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
575 appropriate location to start with. */
576
577 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
578 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
579 BP_LOCP_TMP = BP_LOCP_START; \
580 BP_LOCP_START \
581 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
582 && (*BP_LOCP_TMP)->address == ADDRESS); \
583 BP_LOCP_TMP++)
584
585 /* Iterator for tracepoints only. */
586
587 #define ALL_TRACEPOINTS(B) \
588 for (B = breakpoint_chain; B; B = B->next) \
589 if (is_tracepoint (B))
590
591 /* Chains of all breakpoints defined. */
592
593 struct breakpoint *breakpoint_chain;
594
595 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
596
597 static struct bp_location **bp_locations;
598
599 /* Number of elements of BP_LOCATIONS. */
600
601 static unsigned bp_locations_count;
602
603 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
604 ADDRESS for the current elements of BP_LOCATIONS which get a valid
605 result from bp_location_has_shadow. You can use it for roughly
606 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
607 an address you need to read. */
608
609 static CORE_ADDR bp_locations_placed_address_before_address_max;
610
611 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
612 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
613 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
614 You can use it for roughly limiting the subrange of BP_LOCATIONS to
615 scan for shadow bytes for an address you need to read. */
616
617 static CORE_ADDR bp_locations_shadow_len_after_address_max;
618
619 /* The locations that no longer correspond to any breakpoint, unlinked
620 from the bp_locations array, but for which a hit may still be
621 reported by a target. */
622 VEC(bp_location_p) *moribund_locations = NULL;
623
624 /* Number of last breakpoint made. */
625
626 static int breakpoint_count;
627
628 /* The value of `breakpoint_count' before the last command that
629 created breakpoints. If the last (break-like) command created more
630 than one breakpoint, then the difference between BREAKPOINT_COUNT
631 and PREV_BREAKPOINT_COUNT is more than one. */
632 static int prev_breakpoint_count;
633
634 /* Number of last tracepoint made. */
635
636 static int tracepoint_count;
637
638 static struct cmd_list_element *breakpoint_set_cmdlist;
639 static struct cmd_list_element *breakpoint_show_cmdlist;
640 struct cmd_list_element *save_cmdlist;
641
642 /* See declaration at breakpoint.h. */
643
644 struct breakpoint *
645 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
646 void *user_data)
647 {
648 struct breakpoint *b = NULL;
649
650 ALL_BREAKPOINTS (b)
651 {
652 if (func (b, user_data) != 0)
653 break;
654 }
655
656 return b;
657 }
658
659 /* Return whether a breakpoint is an active enabled breakpoint. */
660 static int
661 breakpoint_enabled (struct breakpoint *b)
662 {
663 return (b->enable_state == bp_enabled);
664 }
665
666 /* Set breakpoint count to NUM. */
667
668 static void
669 set_breakpoint_count (int num)
670 {
671 prev_breakpoint_count = breakpoint_count;
672 breakpoint_count = num;
673 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
674 }
675
676 /* Used by `start_rbreak_breakpoints' below, to record the current
677 breakpoint count before "rbreak" creates any breakpoint. */
678 static int rbreak_start_breakpoint_count;
679
680 /* Called at the start an "rbreak" command to record the first
681 breakpoint made. */
682
683 void
684 start_rbreak_breakpoints (void)
685 {
686 rbreak_start_breakpoint_count = breakpoint_count;
687 }
688
689 /* Called at the end of an "rbreak" command to record the last
690 breakpoint made. */
691
692 void
693 end_rbreak_breakpoints (void)
694 {
695 prev_breakpoint_count = rbreak_start_breakpoint_count;
696 }
697
698 /* Used in run_command to zero the hit count when a new run starts. */
699
700 void
701 clear_breakpoint_hit_counts (void)
702 {
703 struct breakpoint *b;
704
705 ALL_BREAKPOINTS (b)
706 b->hit_count = 0;
707 }
708
709 \f
710 /* Return the breakpoint with the specified number, or NULL
711 if the number does not refer to an existing breakpoint. */
712
713 struct breakpoint *
714 get_breakpoint (int num)
715 {
716 struct breakpoint *b;
717
718 ALL_BREAKPOINTS (b)
719 if (b->number == num)
720 return b;
721
722 return NULL;
723 }
724
725 \f
726
727 /* Mark locations as "conditions have changed" in case the target supports
728 evaluating conditions on its side. */
729
730 static void
731 mark_breakpoint_modified (struct breakpoint *b)
732 {
733 struct bp_location *loc;
734
735 /* This is only meaningful if the target is
736 evaluating conditions and if the user has
737 opted for condition evaluation on the target's
738 side. */
739 if (gdb_evaluates_breakpoint_condition_p ()
740 || !target_supports_evaluation_of_breakpoint_conditions ())
741 return;
742
743 if (!is_breakpoint (b))
744 return;
745
746 for (loc = b->loc; loc; loc = loc->next)
747 loc->condition_changed = condition_modified;
748 }
749
750 /* Mark location as "conditions have changed" in case the target supports
751 evaluating conditions on its side. */
752
753 static void
754 mark_breakpoint_location_modified (struct bp_location *loc)
755 {
756 /* This is only meaningful if the target is
757 evaluating conditions and if the user has
758 opted for condition evaluation on the target's
759 side. */
760 if (gdb_evaluates_breakpoint_condition_p ()
761 || !target_supports_evaluation_of_breakpoint_conditions ())
762
763 return;
764
765 if (!is_breakpoint (loc->owner))
766 return;
767
768 loc->condition_changed = condition_modified;
769 }
770
771 /* Sets the condition-evaluation mode using the static global
772 condition_evaluation_mode. */
773
774 static void
775 set_condition_evaluation_mode (char *args, int from_tty,
776 struct cmd_list_element *c)
777 {
778 const char *old_mode, *new_mode;
779
780 if ((condition_evaluation_mode_1 == condition_evaluation_target)
781 && !target_supports_evaluation_of_breakpoint_conditions ())
782 {
783 condition_evaluation_mode_1 = condition_evaluation_mode;
784 warning (_("Target does not support breakpoint condition evaluation.\n"
785 "Using host evaluation mode instead."));
786 return;
787 }
788
789 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
790 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
791
792 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
793 settings was "auto". */
794 condition_evaluation_mode = condition_evaluation_mode_1;
795
796 /* Only update the mode if the user picked a different one. */
797 if (new_mode != old_mode)
798 {
799 struct bp_location *loc, **loc_tmp;
800 /* If the user switched to a different evaluation mode, we
801 need to synch the changes with the target as follows:
802
803 "host" -> "target": Send all (valid) conditions to the target.
804 "target" -> "host": Remove all the conditions from the target.
805 */
806
807 if (new_mode == condition_evaluation_target)
808 {
809 /* Mark everything modified and synch conditions with the
810 target. */
811 ALL_BP_LOCATIONS (loc, loc_tmp)
812 mark_breakpoint_location_modified (loc);
813 }
814 else
815 {
816 /* Manually mark non-duplicate locations to synch conditions
817 with the target. We do this to remove all the conditions the
818 target knows about. */
819 ALL_BP_LOCATIONS (loc, loc_tmp)
820 if (is_breakpoint (loc->owner) && loc->inserted)
821 loc->needs_update = 1;
822 }
823
824 /* Do the update. */
825 update_global_location_list (UGLL_MAY_INSERT);
826 }
827
828 return;
829 }
830
831 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
832 what "auto" is translating to. */
833
834 static void
835 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
836 struct cmd_list_element *c, const char *value)
837 {
838 if (condition_evaluation_mode == condition_evaluation_auto)
839 fprintf_filtered (file,
840 _("Breakpoint condition evaluation "
841 "mode is %s (currently %s).\n"),
842 value,
843 breakpoint_condition_evaluation_mode ());
844 else
845 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
846 value);
847 }
848
849 /* A comparison function for bp_location AP and BP that is used by
850 bsearch. This comparison function only cares about addresses, unlike
851 the more general bp_locations_compare function. */
852
853 static int
854 bp_locations_compare_addrs (const void *ap, const void *bp)
855 {
856 const struct bp_location *a = *(const struct bp_location **) ap;
857 const struct bp_location *b = *(const struct bp_location **) bp;
858
859 if (a->address == b->address)
860 return 0;
861 else
862 return ((a->address > b->address) - (a->address < b->address));
863 }
864
865 /* Helper function to skip all bp_locations with addresses
866 less than ADDRESS. It returns the first bp_location that
867 is greater than or equal to ADDRESS. If none is found, just
868 return NULL. */
869
870 static struct bp_location **
871 get_first_locp_gte_addr (CORE_ADDR address)
872 {
873 struct bp_location dummy_loc;
874 struct bp_location *dummy_locp = &dummy_loc;
875 struct bp_location **locp_found = NULL;
876
877 /* Initialize the dummy location's address field. */
878 dummy_loc.address = address;
879
880 /* Find a close match to the first location at ADDRESS. */
881 locp_found = ((struct bp_location **)
882 bsearch (&dummy_locp, bp_locations, bp_locations_count,
883 sizeof (struct bp_location **),
884 bp_locations_compare_addrs));
885
886 /* Nothing was found, nothing left to do. */
887 if (locp_found == NULL)
888 return NULL;
889
890 /* We may have found a location that is at ADDRESS but is not the first in the
891 location's list. Go backwards (if possible) and locate the first one. */
892 while ((locp_found - 1) >= bp_locations
893 && (*(locp_found - 1))->address == address)
894 locp_found--;
895
896 return locp_found;
897 }
898
899 void
900 set_breakpoint_condition (struct breakpoint *b, const char *exp,
901 int from_tty)
902 {
903 xfree (b->cond_string);
904 b->cond_string = NULL;
905
906 if (is_watchpoint (b))
907 {
908 struct watchpoint *w = (struct watchpoint *) b;
909
910 w->cond_exp.reset ();
911 }
912 else
913 {
914 struct bp_location *loc;
915
916 for (loc = b->loc; loc; loc = loc->next)
917 {
918 loc->cond.reset ();
919
920 /* No need to free the condition agent expression
921 bytecode (if we have one). We will handle this
922 when we go through update_global_location_list. */
923 }
924 }
925
926 if (*exp == 0)
927 {
928 if (from_tty)
929 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
930 }
931 else
932 {
933 const char *arg = exp;
934
935 /* I don't know if it matters whether this is the string the user
936 typed in or the decompiled expression. */
937 b->cond_string = xstrdup (arg);
938 b->condition_not_parsed = 0;
939
940 if (is_watchpoint (b))
941 {
942 struct watchpoint *w = (struct watchpoint *) b;
943
944 innermost_block = NULL;
945 arg = exp;
946 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
947 if (*arg)
948 error (_("Junk at end of expression"));
949 w->cond_exp_valid_block = innermost_block;
950 }
951 else
952 {
953 struct bp_location *loc;
954
955 for (loc = b->loc; loc; loc = loc->next)
956 {
957 arg = exp;
958 loc->cond =
959 parse_exp_1 (&arg, loc->address,
960 block_for_pc (loc->address), 0);
961 if (*arg)
962 error (_("Junk at end of expression"));
963 }
964 }
965 }
966 mark_breakpoint_modified (b);
967
968 observer_notify_breakpoint_modified (b);
969 }
970
971 /* Completion for the "condition" command. */
972
973 static void
974 condition_completer (struct cmd_list_element *cmd,
975 completion_tracker &tracker,
976 const char *text, const char *word)
977 {
978 const char *space;
979
980 text = skip_spaces (text);
981 space = skip_to_space (text);
982 if (*space == '\0')
983 {
984 int len;
985 struct breakpoint *b;
986 VEC (char_ptr) *result = NULL;
987
988 if (text[0] == '$')
989 {
990 /* We don't support completion of history indices. */
991 if (!isdigit (text[1]))
992 complete_internalvar (tracker, &text[1]);
993 return;
994 }
995
996 /* We're completing the breakpoint number. */
997 len = strlen (text);
998
999 ALL_BREAKPOINTS (b)
1000 {
1001 char number[50];
1002
1003 xsnprintf (number, sizeof (number), "%d", b->number);
1004
1005 if (strncmp (number, text, len) == 0)
1006 {
1007 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
1008 tracker.add_completion (std::move (copy));
1009 }
1010 }
1011
1012 return;
1013 }
1014
1015 /* We're completing the expression part. */
1016 text = skip_spaces (space);
1017 expression_completer (cmd, tracker, text, word);
1018 }
1019
1020 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1021
1022 static void
1023 condition_command (char *arg, int from_tty)
1024 {
1025 struct breakpoint *b;
1026 char *p;
1027 int bnum;
1028
1029 if (arg == 0)
1030 error_no_arg (_("breakpoint number"));
1031
1032 p = arg;
1033 bnum = get_number (&p);
1034 if (bnum == 0)
1035 error (_("Bad breakpoint argument: '%s'"), arg);
1036
1037 ALL_BREAKPOINTS (b)
1038 if (b->number == bnum)
1039 {
1040 /* Check if this breakpoint has a "stop" method implemented in an
1041 extension language. This method and conditions entered into GDB
1042 from the CLI are mutually exclusive. */
1043 const struct extension_language_defn *extlang
1044 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1045
1046 if (extlang != NULL)
1047 {
1048 error (_("Only one stop condition allowed. There is currently"
1049 " a %s stop condition defined for this breakpoint."),
1050 ext_lang_capitalized_name (extlang));
1051 }
1052 set_breakpoint_condition (b, p, from_tty);
1053
1054 if (is_breakpoint (b))
1055 update_global_location_list (UGLL_MAY_INSERT);
1056
1057 return;
1058 }
1059
1060 error (_("No breakpoint number %d."), bnum);
1061 }
1062
1063 /* Check that COMMAND do not contain commands that are suitable
1064 only for tracepoints and not suitable for ordinary breakpoints.
1065 Throw if any such commands is found. */
1066
1067 static void
1068 check_no_tracepoint_commands (struct command_line *commands)
1069 {
1070 struct command_line *c;
1071
1072 for (c = commands; c; c = c->next)
1073 {
1074 int i;
1075
1076 if (c->control_type == while_stepping_control)
1077 error (_("The 'while-stepping' command can "
1078 "only be used for tracepoints"));
1079
1080 for (i = 0; i < c->body_count; ++i)
1081 check_no_tracepoint_commands ((c->body_list)[i]);
1082
1083 /* Not that command parsing removes leading whitespace and comment
1084 lines and also empty lines. So, we only need to check for
1085 command directly. */
1086 if (strstr (c->line, "collect ") == c->line)
1087 error (_("The 'collect' command can only be used for tracepoints"));
1088
1089 if (strstr (c->line, "teval ") == c->line)
1090 error (_("The 'teval' command can only be used for tracepoints"));
1091 }
1092 }
1093
1094 struct longjmp_breakpoint : public breakpoint
1095 {
1096 ~longjmp_breakpoint () override;
1097 };
1098
1099 /* Encapsulate tests for different types of tracepoints. */
1100
1101 static bool
1102 is_tracepoint_type (bptype type)
1103 {
1104 return (type == bp_tracepoint
1105 || type == bp_fast_tracepoint
1106 || type == bp_static_tracepoint);
1107 }
1108
1109 static bool
1110 is_longjmp_type (bptype type)
1111 {
1112 return type == bp_longjmp || type == bp_exception;
1113 }
1114
1115 int
1116 is_tracepoint (const struct breakpoint *b)
1117 {
1118 return is_tracepoint_type (b->type);
1119 }
1120
1121 /* Factory function to create an appropriate instance of breakpoint given
1122 TYPE. */
1123
1124 static std::unique_ptr<breakpoint>
1125 new_breakpoint_from_type (bptype type)
1126 {
1127 breakpoint *b;
1128
1129 if (is_tracepoint_type (type))
1130 b = new tracepoint ();
1131 else if (is_longjmp_type (type))
1132 b = new longjmp_breakpoint ();
1133 else
1134 b = new breakpoint ();
1135
1136 return std::unique_ptr<breakpoint> (b);
1137 }
1138
1139 /* A helper function that validates that COMMANDS are valid for a
1140 breakpoint. This function will throw an exception if a problem is
1141 found. */
1142
1143 static void
1144 validate_commands_for_breakpoint (struct breakpoint *b,
1145 struct command_line *commands)
1146 {
1147 if (is_tracepoint (b))
1148 {
1149 struct tracepoint *t = (struct tracepoint *) b;
1150 struct command_line *c;
1151 struct command_line *while_stepping = 0;
1152
1153 /* Reset the while-stepping step count. The previous commands
1154 might have included a while-stepping action, while the new
1155 ones might not. */
1156 t->step_count = 0;
1157
1158 /* We need to verify that each top-level element of commands is
1159 valid for tracepoints, that there's at most one
1160 while-stepping element, and that the while-stepping's body
1161 has valid tracing commands excluding nested while-stepping.
1162 We also need to validate the tracepoint action line in the
1163 context of the tracepoint --- validate_actionline actually
1164 has side effects, like setting the tracepoint's
1165 while-stepping STEP_COUNT, in addition to checking if the
1166 collect/teval actions parse and make sense in the
1167 tracepoint's context. */
1168 for (c = commands; c; c = c->next)
1169 {
1170 if (c->control_type == while_stepping_control)
1171 {
1172 if (b->type == bp_fast_tracepoint)
1173 error (_("The 'while-stepping' command "
1174 "cannot be used for fast tracepoint"));
1175 else if (b->type == bp_static_tracepoint)
1176 error (_("The 'while-stepping' command "
1177 "cannot be used for static tracepoint"));
1178
1179 if (while_stepping)
1180 error (_("The 'while-stepping' command "
1181 "can be used only once"));
1182 else
1183 while_stepping = c;
1184 }
1185
1186 validate_actionline (c->line, b);
1187 }
1188 if (while_stepping)
1189 {
1190 struct command_line *c2;
1191
1192 gdb_assert (while_stepping->body_count == 1);
1193 c2 = while_stepping->body_list[0];
1194 for (; c2; c2 = c2->next)
1195 {
1196 if (c2->control_type == while_stepping_control)
1197 error (_("The 'while-stepping' command cannot be nested"));
1198 }
1199 }
1200 }
1201 else
1202 {
1203 check_no_tracepoint_commands (commands);
1204 }
1205 }
1206
1207 /* Return a vector of all the static tracepoints set at ADDR. The
1208 caller is responsible for releasing the vector. */
1209
1210 VEC(breakpoint_p) *
1211 static_tracepoints_here (CORE_ADDR addr)
1212 {
1213 struct breakpoint *b;
1214 VEC(breakpoint_p) *found = 0;
1215 struct bp_location *loc;
1216
1217 ALL_BREAKPOINTS (b)
1218 if (b->type == bp_static_tracepoint)
1219 {
1220 for (loc = b->loc; loc; loc = loc->next)
1221 if (loc->address == addr)
1222 VEC_safe_push(breakpoint_p, found, b);
1223 }
1224
1225 return found;
1226 }
1227
1228 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1229 validate that only allowed commands are included. */
1230
1231 void
1232 breakpoint_set_commands (struct breakpoint *b,
1233 command_line_up &&commands)
1234 {
1235 validate_commands_for_breakpoint (b, commands.get ());
1236
1237 b->commands = std::move (commands);
1238 observer_notify_breakpoint_modified (b);
1239 }
1240
1241 /* Set the internal `silent' flag on the breakpoint. Note that this
1242 is not the same as the "silent" that may appear in the breakpoint's
1243 commands. */
1244
1245 void
1246 breakpoint_set_silent (struct breakpoint *b, int silent)
1247 {
1248 int old_silent = b->silent;
1249
1250 b->silent = silent;
1251 if (old_silent != silent)
1252 observer_notify_breakpoint_modified (b);
1253 }
1254
1255 /* Set the thread for this breakpoint. If THREAD is -1, make the
1256 breakpoint work for any thread. */
1257
1258 void
1259 breakpoint_set_thread (struct breakpoint *b, int thread)
1260 {
1261 int old_thread = b->thread;
1262
1263 b->thread = thread;
1264 if (old_thread != thread)
1265 observer_notify_breakpoint_modified (b);
1266 }
1267
1268 /* Set the task for this breakpoint. If TASK is 0, make the
1269 breakpoint work for any task. */
1270
1271 void
1272 breakpoint_set_task (struct breakpoint *b, int task)
1273 {
1274 int old_task = b->task;
1275
1276 b->task = task;
1277 if (old_task != task)
1278 observer_notify_breakpoint_modified (b);
1279 }
1280
1281 void
1282 check_tracepoint_command (char *line, void *closure)
1283 {
1284 struct breakpoint *b = (struct breakpoint *) closure;
1285
1286 validate_actionline (line, b);
1287 }
1288
1289 static void
1290 commands_command_1 (const char *arg, int from_tty,
1291 struct command_line *control)
1292 {
1293 counted_command_line cmd;
1294
1295 std::string new_arg;
1296
1297 if (arg == NULL || !*arg)
1298 {
1299 if (breakpoint_count - prev_breakpoint_count > 1)
1300 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1301 breakpoint_count);
1302 else if (breakpoint_count > 0)
1303 new_arg = string_printf ("%d", breakpoint_count);
1304 arg = new_arg.c_str ();
1305 }
1306
1307 map_breakpoint_numbers
1308 (arg, [&] (breakpoint *b)
1309 {
1310 if (cmd == NULL)
1311 {
1312 if (control != NULL)
1313 cmd = copy_command_lines (control->body_list[0]);
1314 else
1315 {
1316 struct cleanup *old_chain;
1317 char *str;
1318
1319 str = xstrprintf (_("Type commands for breakpoint(s) "
1320 "%s, one per line."),
1321 arg);
1322
1323 old_chain = make_cleanup (xfree, str);
1324
1325 cmd = read_command_lines (str,
1326 from_tty, 1,
1327 (is_tracepoint (b)
1328 ? check_tracepoint_command : 0),
1329 b);
1330
1331 do_cleanups (old_chain);
1332 }
1333 }
1334
1335 /* If a breakpoint was on the list more than once, we don't need to
1336 do anything. */
1337 if (b->commands != cmd)
1338 {
1339 validate_commands_for_breakpoint (b, cmd.get ());
1340 b->commands = cmd;
1341 observer_notify_breakpoint_modified (b);
1342 }
1343 });
1344
1345 if (cmd == NULL)
1346 error (_("No breakpoints specified."));
1347 }
1348
1349 static void
1350 commands_command (char *arg, int from_tty)
1351 {
1352 commands_command_1 (arg, from_tty, NULL);
1353 }
1354
1355 /* Like commands_command, but instead of reading the commands from
1356 input stream, takes them from an already parsed command structure.
1357
1358 This is used by cli-script.c to DTRT with breakpoint commands
1359 that are part of if and while bodies. */
1360 enum command_control_type
1361 commands_from_control_command (const char *arg, struct command_line *cmd)
1362 {
1363 commands_command_1 (arg, 0, cmd);
1364 return simple_control;
1365 }
1366
1367 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1368
1369 static int
1370 bp_location_has_shadow (struct bp_location *bl)
1371 {
1372 if (bl->loc_type != bp_loc_software_breakpoint)
1373 return 0;
1374 if (!bl->inserted)
1375 return 0;
1376 if (bl->target_info.shadow_len == 0)
1377 /* BL isn't valid, or doesn't shadow memory. */
1378 return 0;
1379 return 1;
1380 }
1381
1382 /* Update BUF, which is LEN bytes read from the target address
1383 MEMADDR, by replacing a memory breakpoint with its shadowed
1384 contents.
1385
1386 If READBUF is not NULL, this buffer must not overlap with the of
1387 the breakpoint location's shadow_contents buffer. Otherwise, a
1388 failed assertion internal error will be raised. */
1389
1390 static void
1391 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1392 const gdb_byte *writebuf_org,
1393 ULONGEST memaddr, LONGEST len,
1394 struct bp_target_info *target_info,
1395 struct gdbarch *gdbarch)
1396 {
1397 /* Now do full processing of the found relevant range of elements. */
1398 CORE_ADDR bp_addr = 0;
1399 int bp_size = 0;
1400 int bptoffset = 0;
1401
1402 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1403 current_program_space->aspace, 0))
1404 {
1405 /* The breakpoint is inserted in a different address space. */
1406 return;
1407 }
1408
1409 /* Addresses and length of the part of the breakpoint that
1410 we need to copy. */
1411 bp_addr = target_info->placed_address;
1412 bp_size = target_info->shadow_len;
1413
1414 if (bp_addr + bp_size <= memaddr)
1415 {
1416 /* The breakpoint is entirely before the chunk of memory we are
1417 reading. */
1418 return;
1419 }
1420
1421 if (bp_addr >= memaddr + len)
1422 {
1423 /* The breakpoint is entirely after the chunk of memory we are
1424 reading. */
1425 return;
1426 }
1427
1428 /* Offset within shadow_contents. */
1429 if (bp_addr < memaddr)
1430 {
1431 /* Only copy the second part of the breakpoint. */
1432 bp_size -= memaddr - bp_addr;
1433 bptoffset = memaddr - bp_addr;
1434 bp_addr = memaddr;
1435 }
1436
1437 if (bp_addr + bp_size > memaddr + len)
1438 {
1439 /* Only copy the first part of the breakpoint. */
1440 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1441 }
1442
1443 if (readbuf != NULL)
1444 {
1445 /* Verify that the readbuf buffer does not overlap with the
1446 shadow_contents buffer. */
1447 gdb_assert (target_info->shadow_contents >= readbuf + len
1448 || readbuf >= (target_info->shadow_contents
1449 + target_info->shadow_len));
1450
1451 /* Update the read buffer with this inserted breakpoint's
1452 shadow. */
1453 memcpy (readbuf + bp_addr - memaddr,
1454 target_info->shadow_contents + bptoffset, bp_size);
1455 }
1456 else
1457 {
1458 const unsigned char *bp;
1459 CORE_ADDR addr = target_info->reqstd_address;
1460 int placed_size;
1461
1462 /* Update the shadow with what we want to write to memory. */
1463 memcpy (target_info->shadow_contents + bptoffset,
1464 writebuf_org + bp_addr - memaddr, bp_size);
1465
1466 /* Determine appropriate breakpoint contents and size for this
1467 address. */
1468 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1469
1470 /* Update the final write buffer with this inserted
1471 breakpoint's INSN. */
1472 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1473 }
1474 }
1475
1476 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1477 by replacing any memory breakpoints with their shadowed contents.
1478
1479 If READBUF is not NULL, this buffer must not overlap with any of
1480 the breakpoint location's shadow_contents buffers. Otherwise,
1481 a failed assertion internal error will be raised.
1482
1483 The range of shadowed area by each bp_location is:
1484 bl->address - bp_locations_placed_address_before_address_max
1485 up to bl->address + bp_locations_shadow_len_after_address_max
1486 The range we were requested to resolve shadows for is:
1487 memaddr ... memaddr + len
1488 Thus the safe cutoff boundaries for performance optimization are
1489 memaddr + len <= (bl->address
1490 - bp_locations_placed_address_before_address_max)
1491 and:
1492 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1493
1494 void
1495 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1496 const gdb_byte *writebuf_org,
1497 ULONGEST memaddr, LONGEST len)
1498 {
1499 /* Left boundary, right boundary and median element of our binary
1500 search. */
1501 unsigned bc_l, bc_r, bc;
1502
1503 /* Find BC_L which is a leftmost element which may affect BUF
1504 content. It is safe to report lower value but a failure to
1505 report higher one. */
1506
1507 bc_l = 0;
1508 bc_r = bp_locations_count;
1509 while (bc_l + 1 < bc_r)
1510 {
1511 struct bp_location *bl;
1512
1513 bc = (bc_l + bc_r) / 2;
1514 bl = bp_locations[bc];
1515
1516 /* Check first BL->ADDRESS will not overflow due to the added
1517 constant. Then advance the left boundary only if we are sure
1518 the BC element can in no way affect the BUF content (MEMADDR
1519 to MEMADDR + LEN range).
1520
1521 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1522 offset so that we cannot miss a breakpoint with its shadow
1523 range tail still reaching MEMADDR. */
1524
1525 if ((bl->address + bp_locations_shadow_len_after_address_max
1526 >= bl->address)
1527 && (bl->address + bp_locations_shadow_len_after_address_max
1528 <= memaddr))
1529 bc_l = bc;
1530 else
1531 bc_r = bc;
1532 }
1533
1534 /* Due to the binary search above, we need to make sure we pick the
1535 first location that's at BC_L's address. E.g., if there are
1536 multiple locations at the same address, BC_L may end up pointing
1537 at a duplicate location, and miss the "master"/"inserted"
1538 location. Say, given locations L1, L2 and L3 at addresses A and
1539 B:
1540
1541 L1@A, L2@A, L3@B, ...
1542
1543 BC_L could end up pointing at location L2, while the "master"
1544 location could be L1. Since the `loc->inserted' flag is only set
1545 on "master" locations, we'd forget to restore the shadow of L1
1546 and L2. */
1547 while (bc_l > 0
1548 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1549 bc_l--;
1550
1551 /* Now do full processing of the found relevant range of elements. */
1552
1553 for (bc = bc_l; bc < bp_locations_count; bc++)
1554 {
1555 struct bp_location *bl = bp_locations[bc];
1556
1557 /* bp_location array has BL->OWNER always non-NULL. */
1558 if (bl->owner->type == bp_none)
1559 warning (_("reading through apparently deleted breakpoint #%d?"),
1560 bl->owner->number);
1561
1562 /* Performance optimization: any further element can no longer affect BUF
1563 content. */
1564
1565 if (bl->address >= bp_locations_placed_address_before_address_max
1566 && memaddr + len <= (bl->address
1567 - bp_locations_placed_address_before_address_max))
1568 break;
1569
1570 if (!bp_location_has_shadow (bl))
1571 continue;
1572
1573 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1574 memaddr, len, &bl->target_info, bl->gdbarch);
1575 }
1576 }
1577
1578 \f
1579
1580 /* Return true if BPT is either a software breakpoint or a hardware
1581 breakpoint. */
1582
1583 int
1584 is_breakpoint (const struct breakpoint *bpt)
1585 {
1586 return (bpt->type == bp_breakpoint
1587 || bpt->type == bp_hardware_breakpoint
1588 || bpt->type == bp_dprintf);
1589 }
1590
1591 /* Return true if BPT is of any hardware watchpoint kind. */
1592
1593 static int
1594 is_hardware_watchpoint (const struct breakpoint *bpt)
1595 {
1596 return (bpt->type == bp_hardware_watchpoint
1597 || bpt->type == bp_read_watchpoint
1598 || bpt->type == bp_access_watchpoint);
1599 }
1600
1601 /* Return true if BPT is of any watchpoint kind, hardware or
1602 software. */
1603
1604 int
1605 is_watchpoint (const struct breakpoint *bpt)
1606 {
1607 return (is_hardware_watchpoint (bpt)
1608 || bpt->type == bp_watchpoint);
1609 }
1610
1611 /* Returns true if the current thread and its running state are safe
1612 to evaluate or update watchpoint B. Watchpoints on local
1613 expressions need to be evaluated in the context of the thread that
1614 was current when the watchpoint was created, and, that thread needs
1615 to be stopped to be able to select the correct frame context.
1616 Watchpoints on global expressions can be evaluated on any thread,
1617 and in any state. It is presently left to the target allowing
1618 memory accesses when threads are running. */
1619
1620 static int
1621 watchpoint_in_thread_scope (struct watchpoint *b)
1622 {
1623 return (b->pspace == current_program_space
1624 && (ptid_equal (b->watchpoint_thread, null_ptid)
1625 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1626 && !is_executing (inferior_ptid))));
1627 }
1628
1629 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1630 associated bp_watchpoint_scope breakpoint. */
1631
1632 static void
1633 watchpoint_del_at_next_stop (struct watchpoint *w)
1634 {
1635 if (w->related_breakpoint != w)
1636 {
1637 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1638 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1639 w->related_breakpoint->disposition = disp_del_at_next_stop;
1640 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1641 w->related_breakpoint = w;
1642 }
1643 w->disposition = disp_del_at_next_stop;
1644 }
1645
1646 /* Extract a bitfield value from value VAL using the bit parameters contained in
1647 watchpoint W. */
1648
1649 static struct value *
1650 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1651 {
1652 struct value *bit_val;
1653
1654 if (val == NULL)
1655 return NULL;
1656
1657 bit_val = allocate_value (value_type (val));
1658
1659 unpack_value_bitfield (bit_val,
1660 w->val_bitpos,
1661 w->val_bitsize,
1662 value_contents_for_printing (val),
1663 value_offset (val),
1664 val);
1665
1666 return bit_val;
1667 }
1668
1669 /* Allocate a dummy location and add it to B, which must be a software
1670 watchpoint. This is required because even if a software watchpoint
1671 is not watching any memory, bpstat_stop_status requires a location
1672 to be able to report stops. */
1673
1674 static void
1675 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1676 struct program_space *pspace)
1677 {
1678 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1679
1680 b->loc = allocate_bp_location (b);
1681 b->loc->pspace = pspace;
1682 b->loc->address = -1;
1683 b->loc->length = -1;
1684 }
1685
1686 /* Returns true if B is a software watchpoint that is not watching any
1687 memory (e.g., "watch $pc"). */
1688
1689 static int
1690 is_no_memory_software_watchpoint (struct breakpoint *b)
1691 {
1692 return (b->type == bp_watchpoint
1693 && b->loc != NULL
1694 && b->loc->next == NULL
1695 && b->loc->address == -1
1696 && b->loc->length == -1);
1697 }
1698
1699 /* Assuming that B is a watchpoint:
1700 - Reparse watchpoint expression, if REPARSE is non-zero
1701 - Evaluate expression and store the result in B->val
1702 - Evaluate the condition if there is one, and store the result
1703 in b->loc->cond.
1704 - Update the list of values that must be watched in B->loc.
1705
1706 If the watchpoint disposition is disp_del_at_next_stop, then do
1707 nothing. If this is local watchpoint that is out of scope, delete
1708 it.
1709
1710 Even with `set breakpoint always-inserted on' the watchpoints are
1711 removed + inserted on each stop here. Normal breakpoints must
1712 never be removed because they might be missed by a running thread
1713 when debugging in non-stop mode. On the other hand, hardware
1714 watchpoints (is_hardware_watchpoint; processed here) are specific
1715 to each LWP since they are stored in each LWP's hardware debug
1716 registers. Therefore, such LWP must be stopped first in order to
1717 be able to modify its hardware watchpoints.
1718
1719 Hardware watchpoints must be reset exactly once after being
1720 presented to the user. It cannot be done sooner, because it would
1721 reset the data used to present the watchpoint hit to the user. And
1722 it must not be done later because it could display the same single
1723 watchpoint hit during multiple GDB stops. Note that the latter is
1724 relevant only to the hardware watchpoint types bp_read_watchpoint
1725 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1726 not user-visible - its hit is suppressed if the memory content has
1727 not changed.
1728
1729 The following constraints influence the location where we can reset
1730 hardware watchpoints:
1731
1732 * target_stopped_by_watchpoint and target_stopped_data_address are
1733 called several times when GDB stops.
1734
1735 [linux]
1736 * Multiple hardware watchpoints can be hit at the same time,
1737 causing GDB to stop. GDB only presents one hardware watchpoint
1738 hit at a time as the reason for stopping, and all the other hits
1739 are presented later, one after the other, each time the user
1740 requests the execution to be resumed. Execution is not resumed
1741 for the threads still having pending hit event stored in
1742 LWP_INFO->STATUS. While the watchpoint is already removed from
1743 the inferior on the first stop the thread hit event is kept being
1744 reported from its cached value by linux_nat_stopped_data_address
1745 until the real thread resume happens after the watchpoint gets
1746 presented and thus its LWP_INFO->STATUS gets reset.
1747
1748 Therefore the hardware watchpoint hit can get safely reset on the
1749 watchpoint removal from inferior. */
1750
1751 static void
1752 update_watchpoint (struct watchpoint *b, int reparse)
1753 {
1754 int within_current_scope;
1755 struct frame_id saved_frame_id;
1756 int frame_saved;
1757
1758 /* If this is a local watchpoint, we only want to check if the
1759 watchpoint frame is in scope if the current thread is the thread
1760 that was used to create the watchpoint. */
1761 if (!watchpoint_in_thread_scope (b))
1762 return;
1763
1764 if (b->disposition == disp_del_at_next_stop)
1765 return;
1766
1767 frame_saved = 0;
1768
1769 /* Determine if the watchpoint is within scope. */
1770 if (b->exp_valid_block == NULL)
1771 within_current_scope = 1;
1772 else
1773 {
1774 struct frame_info *fi = get_current_frame ();
1775 struct gdbarch *frame_arch = get_frame_arch (fi);
1776 CORE_ADDR frame_pc = get_frame_pc (fi);
1777
1778 /* If we're at a point where the stack has been destroyed
1779 (e.g. in a function epilogue), unwinding may not work
1780 properly. Do not attempt to recreate locations at this
1781 point. See similar comments in watchpoint_check. */
1782 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1783 return;
1784
1785 /* Save the current frame's ID so we can restore it after
1786 evaluating the watchpoint expression on its own frame. */
1787 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1788 took a frame parameter, so that we didn't have to change the
1789 selected frame. */
1790 frame_saved = 1;
1791 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1792
1793 fi = frame_find_by_id (b->watchpoint_frame);
1794 within_current_scope = (fi != NULL);
1795 if (within_current_scope)
1796 select_frame (fi);
1797 }
1798
1799 /* We don't free locations. They are stored in the bp_location array
1800 and update_global_location_list will eventually delete them and
1801 remove breakpoints if needed. */
1802 b->loc = NULL;
1803
1804 if (within_current_scope && reparse)
1805 {
1806 const char *s;
1807
1808 b->exp.reset ();
1809 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1810 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1811 /* If the meaning of expression itself changed, the old value is
1812 no longer relevant. We don't want to report a watchpoint hit
1813 to the user when the old value and the new value may actually
1814 be completely different objects. */
1815 value_free (b->val);
1816 b->val = NULL;
1817 b->val_valid = 0;
1818
1819 /* Note that unlike with breakpoints, the watchpoint's condition
1820 expression is stored in the breakpoint object, not in the
1821 locations (re)created below. */
1822 if (b->cond_string != NULL)
1823 {
1824 b->cond_exp.reset ();
1825
1826 s = b->cond_string;
1827 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1828 }
1829 }
1830
1831 /* If we failed to parse the expression, for example because
1832 it refers to a global variable in a not-yet-loaded shared library,
1833 don't try to insert watchpoint. We don't automatically delete
1834 such watchpoint, though, since failure to parse expression
1835 is different from out-of-scope watchpoint. */
1836 if (!target_has_execution)
1837 {
1838 /* Without execution, memory can't change. No use to try and
1839 set watchpoint locations. The watchpoint will be reset when
1840 the target gains execution, through breakpoint_re_set. */
1841 if (!can_use_hw_watchpoints)
1842 {
1843 if (b->ops->works_in_software_mode (b))
1844 b->type = bp_watchpoint;
1845 else
1846 error (_("Can't set read/access watchpoint when "
1847 "hardware watchpoints are disabled."));
1848 }
1849 }
1850 else if (within_current_scope && b->exp)
1851 {
1852 int pc = 0;
1853 struct value *val_chain, *v, *result, *next;
1854 struct program_space *frame_pspace;
1855
1856 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1857
1858 /* Avoid setting b->val if it's already set. The meaning of
1859 b->val is 'the last value' user saw, and we should update
1860 it only if we reported that last value to user. As it
1861 happens, the code that reports it updates b->val directly.
1862 We don't keep track of the memory value for masked
1863 watchpoints. */
1864 if (!b->val_valid && !is_masked_watchpoint (b))
1865 {
1866 if (b->val_bitsize != 0)
1867 {
1868 v = extract_bitfield_from_watchpoint_value (b, v);
1869 if (v != NULL)
1870 release_value (v);
1871 }
1872 b->val = v;
1873 b->val_valid = 1;
1874 }
1875
1876 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1877
1878 /* Look at each value on the value chain. */
1879 for (v = val_chain; v; v = value_next (v))
1880 {
1881 /* If it's a memory location, and GDB actually needed
1882 its contents to evaluate the expression, then we
1883 must watch it. If the first value returned is
1884 still lazy, that means an error occurred reading it;
1885 watch it anyway in case it becomes readable. */
1886 if (VALUE_LVAL (v) == lval_memory
1887 && (v == val_chain || ! value_lazy (v)))
1888 {
1889 struct type *vtype = check_typedef (value_type (v));
1890
1891 /* We only watch structs and arrays if user asked
1892 for it explicitly, never if they just happen to
1893 appear in the middle of some value chain. */
1894 if (v == result
1895 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1896 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1897 {
1898 CORE_ADDR addr;
1899 enum target_hw_bp_type type;
1900 struct bp_location *loc, **tmp;
1901 int bitpos = 0, bitsize = 0;
1902
1903 if (value_bitsize (v) != 0)
1904 {
1905 /* Extract the bit parameters out from the bitfield
1906 sub-expression. */
1907 bitpos = value_bitpos (v);
1908 bitsize = value_bitsize (v);
1909 }
1910 else if (v == result && b->val_bitsize != 0)
1911 {
1912 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1913 lvalue whose bit parameters are saved in the fields
1914 VAL_BITPOS and VAL_BITSIZE. */
1915 bitpos = b->val_bitpos;
1916 bitsize = b->val_bitsize;
1917 }
1918
1919 addr = value_address (v);
1920 if (bitsize != 0)
1921 {
1922 /* Skip the bytes that don't contain the bitfield. */
1923 addr += bitpos / 8;
1924 }
1925
1926 type = hw_write;
1927 if (b->type == bp_read_watchpoint)
1928 type = hw_read;
1929 else if (b->type == bp_access_watchpoint)
1930 type = hw_access;
1931
1932 loc = allocate_bp_location (b);
1933 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1934 ;
1935 *tmp = loc;
1936 loc->gdbarch = get_type_arch (value_type (v));
1937
1938 loc->pspace = frame_pspace;
1939 loc->address = addr;
1940
1941 if (bitsize != 0)
1942 {
1943 /* Just cover the bytes that make up the bitfield. */
1944 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1945 }
1946 else
1947 loc->length = TYPE_LENGTH (value_type (v));
1948
1949 loc->watchpoint_type = type;
1950 }
1951 }
1952 }
1953
1954 /* Change the type of breakpoint between hardware assisted or
1955 an ordinary watchpoint depending on the hardware support
1956 and free hardware slots. REPARSE is set when the inferior
1957 is started. */
1958 if (reparse)
1959 {
1960 int reg_cnt;
1961 enum bp_loc_type loc_type;
1962 struct bp_location *bl;
1963
1964 reg_cnt = can_use_hardware_watchpoint (val_chain);
1965
1966 if (reg_cnt)
1967 {
1968 int i, target_resources_ok, other_type_used;
1969 enum bptype type;
1970
1971 /* Use an exact watchpoint when there's only one memory region to be
1972 watched, and only one debug register is needed to watch it. */
1973 b->exact = target_exact_watchpoints && reg_cnt == 1;
1974
1975 /* We need to determine how many resources are already
1976 used for all other hardware watchpoints plus this one
1977 to see if we still have enough resources to also fit
1978 this watchpoint in as well. */
1979
1980 /* If this is a software watchpoint, we try to turn it
1981 to a hardware one -- count resources as if B was of
1982 hardware watchpoint type. */
1983 type = b->type;
1984 if (type == bp_watchpoint)
1985 type = bp_hardware_watchpoint;
1986
1987 /* This watchpoint may or may not have been placed on
1988 the list yet at this point (it won't be in the list
1989 if we're trying to create it for the first time,
1990 through watch_command), so always account for it
1991 manually. */
1992
1993 /* Count resources used by all watchpoints except B. */
1994 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1995
1996 /* Add in the resources needed for B. */
1997 i += hw_watchpoint_use_count (b);
1998
1999 target_resources_ok
2000 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2001 if (target_resources_ok <= 0)
2002 {
2003 int sw_mode = b->ops->works_in_software_mode (b);
2004
2005 if (target_resources_ok == 0 && !sw_mode)
2006 error (_("Target does not support this type of "
2007 "hardware watchpoint."));
2008 else if (target_resources_ok < 0 && !sw_mode)
2009 error (_("There are not enough available hardware "
2010 "resources for this watchpoint."));
2011
2012 /* Downgrade to software watchpoint. */
2013 b->type = bp_watchpoint;
2014 }
2015 else
2016 {
2017 /* If this was a software watchpoint, we've just
2018 found we have enough resources to turn it to a
2019 hardware watchpoint. Otherwise, this is a
2020 nop. */
2021 b->type = type;
2022 }
2023 }
2024 else if (!b->ops->works_in_software_mode (b))
2025 {
2026 if (!can_use_hw_watchpoints)
2027 error (_("Can't set read/access watchpoint when "
2028 "hardware watchpoints are disabled."));
2029 else
2030 error (_("Expression cannot be implemented with "
2031 "read/access watchpoint."));
2032 }
2033 else
2034 b->type = bp_watchpoint;
2035
2036 loc_type = (b->type == bp_watchpoint? bp_loc_other
2037 : bp_loc_hardware_watchpoint);
2038 for (bl = b->loc; bl; bl = bl->next)
2039 bl->loc_type = loc_type;
2040 }
2041
2042 for (v = val_chain; v; v = next)
2043 {
2044 next = value_next (v);
2045 if (v != b->val)
2046 value_free (v);
2047 }
2048
2049 /* If a software watchpoint is not watching any memory, then the
2050 above left it without any location set up. But,
2051 bpstat_stop_status requires a location to be able to report
2052 stops, so make sure there's at least a dummy one. */
2053 if (b->type == bp_watchpoint && b->loc == NULL)
2054 software_watchpoint_add_no_memory_location (b, frame_pspace);
2055 }
2056 else if (!within_current_scope)
2057 {
2058 printf_filtered (_("\
2059 Watchpoint %d deleted because the program has left the block\n\
2060 in which its expression is valid.\n"),
2061 b->number);
2062 watchpoint_del_at_next_stop (b);
2063 }
2064
2065 /* Restore the selected frame. */
2066 if (frame_saved)
2067 select_frame (frame_find_by_id (saved_frame_id));
2068 }
2069
2070
2071 /* Returns 1 iff breakpoint location should be
2072 inserted in the inferior. We don't differentiate the type of BL's owner
2073 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2074 breakpoint_ops is not defined, because in insert_bp_location,
2075 tracepoint's insert_location will not be called. */
2076 static int
2077 should_be_inserted (struct bp_location *bl)
2078 {
2079 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2080 return 0;
2081
2082 if (bl->owner->disposition == disp_del_at_next_stop)
2083 return 0;
2084
2085 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2086 return 0;
2087
2088 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2089 return 0;
2090
2091 /* This is set for example, when we're attached to the parent of a
2092 vfork, and have detached from the child. The child is running
2093 free, and we expect it to do an exec or exit, at which point the
2094 OS makes the parent schedulable again (and the target reports
2095 that the vfork is done). Until the child is done with the shared
2096 memory region, do not insert breakpoints in the parent, otherwise
2097 the child could still trip on the parent's breakpoints. Since
2098 the parent is blocked anyway, it won't miss any breakpoint. */
2099 if (bl->pspace->breakpoints_not_allowed)
2100 return 0;
2101
2102 /* Don't insert a breakpoint if we're trying to step past its
2103 location, except if the breakpoint is a single-step breakpoint,
2104 and the breakpoint's thread is the thread which is stepping past
2105 a breakpoint. */
2106 if ((bl->loc_type == bp_loc_software_breakpoint
2107 || bl->loc_type == bp_loc_hardware_breakpoint)
2108 && stepping_past_instruction_at (bl->pspace->aspace,
2109 bl->address)
2110 /* The single-step breakpoint may be inserted at the location
2111 we're trying to step if the instruction branches to itself.
2112 However, the instruction won't be executed at all and it may
2113 break the semantics of the instruction, for example, the
2114 instruction is a conditional branch or updates some flags.
2115 We can't fix it unless GDB is able to emulate the instruction
2116 or switch to displaced stepping. */
2117 && !(bl->owner->type == bp_single_step
2118 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2119 {
2120 if (debug_infrun)
2121 {
2122 fprintf_unfiltered (gdb_stdlog,
2123 "infrun: skipping breakpoint: "
2124 "stepping past insn at: %s\n",
2125 paddress (bl->gdbarch, bl->address));
2126 }
2127 return 0;
2128 }
2129
2130 /* Don't insert watchpoints if we're trying to step past the
2131 instruction that triggered one. */
2132 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2133 && stepping_past_nonsteppable_watchpoint ())
2134 {
2135 if (debug_infrun)
2136 {
2137 fprintf_unfiltered (gdb_stdlog,
2138 "infrun: stepping past non-steppable watchpoint. "
2139 "skipping watchpoint at %s:%d\n",
2140 paddress (bl->gdbarch, bl->address),
2141 bl->length);
2142 }
2143 return 0;
2144 }
2145
2146 return 1;
2147 }
2148
2149 /* Same as should_be_inserted but does the check assuming
2150 that the location is not duplicated. */
2151
2152 static int
2153 unduplicated_should_be_inserted (struct bp_location *bl)
2154 {
2155 int result;
2156 const int save_duplicate = bl->duplicate;
2157
2158 bl->duplicate = 0;
2159 result = should_be_inserted (bl);
2160 bl->duplicate = save_duplicate;
2161 return result;
2162 }
2163
2164 /* Parses a conditional described by an expression COND into an
2165 agent expression bytecode suitable for evaluation
2166 by the bytecode interpreter. Return NULL if there was
2167 any error during parsing. */
2168
2169 static agent_expr_up
2170 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2171 {
2172 if (cond == NULL)
2173 return NULL;
2174
2175 agent_expr_up aexpr;
2176
2177 /* We don't want to stop processing, so catch any errors
2178 that may show up. */
2179 TRY
2180 {
2181 aexpr = gen_eval_for_expr (scope, cond);
2182 }
2183
2184 CATCH (ex, RETURN_MASK_ERROR)
2185 {
2186 /* If we got here, it means the condition could not be parsed to a valid
2187 bytecode expression and thus can't be evaluated on the target's side.
2188 It's no use iterating through the conditions. */
2189 }
2190 END_CATCH
2191
2192 /* We have a valid agent expression. */
2193 return aexpr;
2194 }
2195
2196 /* Based on location BL, create a list of breakpoint conditions to be
2197 passed on to the target. If we have duplicated locations with different
2198 conditions, we will add such conditions to the list. The idea is that the
2199 target will evaluate the list of conditions and will only notify GDB when
2200 one of them is true. */
2201
2202 static void
2203 build_target_condition_list (struct bp_location *bl)
2204 {
2205 struct bp_location **locp = NULL, **loc2p;
2206 int null_condition_or_parse_error = 0;
2207 int modified = bl->needs_update;
2208 struct bp_location *loc;
2209
2210 /* Release conditions left over from a previous insert. */
2211 bl->target_info.conditions.clear ();
2212
2213 /* This is only meaningful if the target is
2214 evaluating conditions and if the user has
2215 opted for condition evaluation on the target's
2216 side. */
2217 if (gdb_evaluates_breakpoint_condition_p ()
2218 || !target_supports_evaluation_of_breakpoint_conditions ())
2219 return;
2220
2221 /* Do a first pass to check for locations with no assigned
2222 conditions or conditions that fail to parse to a valid agent expression
2223 bytecode. If any of these happen, then it's no use to send conditions
2224 to the target since this location will always trigger and generate a
2225 response back to GDB. */
2226 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2227 {
2228 loc = (*loc2p);
2229 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2230 {
2231 if (modified)
2232 {
2233 /* Re-parse the conditions since something changed. In that
2234 case we already freed the condition bytecodes (see
2235 force_breakpoint_reinsertion). We just
2236 need to parse the condition to bytecodes again. */
2237 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2238 loc->cond.get ());
2239 }
2240
2241 /* If we have a NULL bytecode expression, it means something
2242 went wrong or we have a null condition expression. */
2243 if (!loc->cond_bytecode)
2244 {
2245 null_condition_or_parse_error = 1;
2246 break;
2247 }
2248 }
2249 }
2250
2251 /* If any of these happened, it means we will have to evaluate the conditions
2252 for the location's address on gdb's side. It is no use keeping bytecodes
2253 for all the other duplicate locations, thus we free all of them here.
2254
2255 This is so we have a finer control over which locations' conditions are
2256 being evaluated by GDB or the remote stub. */
2257 if (null_condition_or_parse_error)
2258 {
2259 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2260 {
2261 loc = (*loc2p);
2262 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2263 {
2264 /* Only go as far as the first NULL bytecode is
2265 located. */
2266 if (!loc->cond_bytecode)
2267 return;
2268
2269 loc->cond_bytecode.reset ();
2270 }
2271 }
2272 }
2273
2274 /* No NULL conditions or failed bytecode generation. Build a condition list
2275 for this location's address. */
2276 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2277 {
2278 loc = (*loc2p);
2279 if (loc->cond
2280 && is_breakpoint (loc->owner)
2281 && loc->pspace->num == bl->pspace->num
2282 && loc->owner->enable_state == bp_enabled
2283 && loc->enabled)
2284 {
2285 /* Add the condition to the vector. This will be used later
2286 to send the conditions to the target. */
2287 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2288 }
2289 }
2290
2291 return;
2292 }
2293
2294 /* Parses a command described by string CMD into an agent expression
2295 bytecode suitable for evaluation by the bytecode interpreter.
2296 Return NULL if there was any error during parsing. */
2297
2298 static agent_expr_up
2299 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2300 {
2301 struct cleanup *old_cleanups = 0;
2302 struct expression **argvec;
2303 const char *cmdrest;
2304 const char *format_start, *format_end;
2305 struct format_piece *fpieces;
2306 int nargs;
2307 struct gdbarch *gdbarch = get_current_arch ();
2308
2309 if (cmd == NULL)
2310 return NULL;
2311
2312 cmdrest = cmd;
2313
2314 if (*cmdrest == ',')
2315 ++cmdrest;
2316 cmdrest = skip_spaces (cmdrest);
2317
2318 if (*cmdrest++ != '"')
2319 error (_("No format string following the location"));
2320
2321 format_start = cmdrest;
2322
2323 fpieces = parse_format_string (&cmdrest);
2324
2325 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2326
2327 format_end = cmdrest;
2328
2329 if (*cmdrest++ != '"')
2330 error (_("Bad format string, non-terminated '\"'."));
2331
2332 cmdrest = skip_spaces (cmdrest);
2333
2334 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2335 error (_("Invalid argument syntax"));
2336
2337 if (*cmdrest == ',')
2338 cmdrest++;
2339 cmdrest = skip_spaces (cmdrest);
2340
2341 /* For each argument, make an expression. */
2342
2343 argvec = (struct expression **) alloca (strlen (cmd)
2344 * sizeof (struct expression *));
2345
2346 nargs = 0;
2347 while (*cmdrest != '\0')
2348 {
2349 const char *cmd1;
2350
2351 cmd1 = cmdrest;
2352 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2353 argvec[nargs++] = expr.release ();
2354 cmdrest = cmd1;
2355 if (*cmdrest == ',')
2356 ++cmdrest;
2357 }
2358
2359 agent_expr_up aexpr;
2360
2361 /* We don't want to stop processing, so catch any errors
2362 that may show up. */
2363 TRY
2364 {
2365 aexpr = gen_printf (scope, gdbarch, 0, 0,
2366 format_start, format_end - format_start,
2367 fpieces, nargs, argvec);
2368 }
2369 CATCH (ex, RETURN_MASK_ERROR)
2370 {
2371 /* If we got here, it means the command could not be parsed to a valid
2372 bytecode expression and thus can't be evaluated on the target's side.
2373 It's no use iterating through the other commands. */
2374 }
2375 END_CATCH
2376
2377 do_cleanups (old_cleanups);
2378
2379 /* We have a valid agent expression, return it. */
2380 return aexpr;
2381 }
2382
2383 /* Based on location BL, create a list of breakpoint commands to be
2384 passed on to the target. If we have duplicated locations with
2385 different commands, we will add any such to the list. */
2386
2387 static void
2388 build_target_command_list (struct bp_location *bl)
2389 {
2390 struct bp_location **locp = NULL, **loc2p;
2391 int null_command_or_parse_error = 0;
2392 int modified = bl->needs_update;
2393 struct bp_location *loc;
2394
2395 /* Clear commands left over from a previous insert. */
2396 bl->target_info.tcommands.clear ();
2397
2398 if (!target_can_run_breakpoint_commands ())
2399 return;
2400
2401 /* For now, limit to agent-style dprintf breakpoints. */
2402 if (dprintf_style != dprintf_style_agent)
2403 return;
2404
2405 /* For now, if we have any duplicate location that isn't a dprintf,
2406 don't install the target-side commands, as that would make the
2407 breakpoint not be reported to the core, and we'd lose
2408 control. */
2409 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2410 {
2411 loc = (*loc2p);
2412 if (is_breakpoint (loc->owner)
2413 && loc->pspace->num == bl->pspace->num
2414 && loc->owner->type != bp_dprintf)
2415 return;
2416 }
2417
2418 /* Do a first pass to check for locations with no assigned
2419 conditions or conditions that fail to parse to a valid agent expression
2420 bytecode. If any of these happen, then it's no use to send conditions
2421 to the target since this location will always trigger and generate a
2422 response back to GDB. */
2423 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2424 {
2425 loc = (*loc2p);
2426 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2427 {
2428 if (modified)
2429 {
2430 /* Re-parse the commands since something changed. In that
2431 case we already freed the command bytecodes (see
2432 force_breakpoint_reinsertion). We just
2433 need to parse the command to bytecodes again. */
2434 loc->cmd_bytecode
2435 = parse_cmd_to_aexpr (bl->address,
2436 loc->owner->extra_string);
2437 }
2438
2439 /* If we have a NULL bytecode expression, it means something
2440 went wrong or we have a null command expression. */
2441 if (!loc->cmd_bytecode)
2442 {
2443 null_command_or_parse_error = 1;
2444 break;
2445 }
2446 }
2447 }
2448
2449 /* If anything failed, then we're not doing target-side commands,
2450 and so clean up. */
2451 if (null_command_or_parse_error)
2452 {
2453 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2454 {
2455 loc = (*loc2p);
2456 if (is_breakpoint (loc->owner)
2457 && loc->pspace->num == bl->pspace->num)
2458 {
2459 /* Only go as far as the first NULL bytecode is
2460 located. */
2461 if (loc->cmd_bytecode == NULL)
2462 return;
2463
2464 loc->cmd_bytecode.reset ();
2465 }
2466 }
2467 }
2468
2469 /* No NULL commands or failed bytecode generation. Build a command list
2470 for this location's address. */
2471 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2472 {
2473 loc = (*loc2p);
2474 if (loc->owner->extra_string
2475 && is_breakpoint (loc->owner)
2476 && loc->pspace->num == bl->pspace->num
2477 && loc->owner->enable_state == bp_enabled
2478 && loc->enabled)
2479 {
2480 /* Add the command to the vector. This will be used later
2481 to send the commands to the target. */
2482 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2483 }
2484 }
2485
2486 bl->target_info.persist = 0;
2487 /* Maybe flag this location as persistent. */
2488 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2489 bl->target_info.persist = 1;
2490 }
2491
2492 /* Return the kind of breakpoint on address *ADDR. Get the kind
2493 of breakpoint according to ADDR except single-step breakpoint.
2494 Get the kind of single-step breakpoint according to the current
2495 registers state. */
2496
2497 static int
2498 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2499 {
2500 if (bl->owner->type == bp_single_step)
2501 {
2502 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2503 struct regcache *regcache;
2504
2505 regcache = get_thread_regcache (thr->ptid);
2506
2507 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2508 regcache, addr);
2509 }
2510 else
2511 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2512 }
2513
2514 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2515 location. Any error messages are printed to TMP_ERROR_STREAM; and
2516 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2517 Returns 0 for success, 1 if the bp_location type is not supported or
2518 -1 for failure.
2519
2520 NOTE drow/2003-09-09: This routine could be broken down to an
2521 object-style method for each breakpoint or catchpoint type. */
2522 static int
2523 insert_bp_location (struct bp_location *bl,
2524 struct ui_file *tmp_error_stream,
2525 int *disabled_breaks,
2526 int *hw_breakpoint_error,
2527 int *hw_bp_error_explained_already)
2528 {
2529 enum errors bp_err = GDB_NO_ERROR;
2530 const char *bp_err_message = NULL;
2531
2532 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2533 return 0;
2534
2535 /* Note we don't initialize bl->target_info, as that wipes out
2536 the breakpoint location's shadow_contents if the breakpoint
2537 is still inserted at that location. This in turn breaks
2538 target_read_memory which depends on these buffers when
2539 a memory read is requested at the breakpoint location:
2540 Once the target_info has been wiped, we fail to see that
2541 we have a breakpoint inserted at that address and thus
2542 read the breakpoint instead of returning the data saved in
2543 the breakpoint location's shadow contents. */
2544 bl->target_info.reqstd_address = bl->address;
2545 bl->target_info.placed_address_space = bl->pspace->aspace;
2546 bl->target_info.length = bl->length;
2547
2548 /* When working with target-side conditions, we must pass all the conditions
2549 for the same breakpoint address down to the target since GDB will not
2550 insert those locations. With a list of breakpoint conditions, the target
2551 can decide when to stop and notify GDB. */
2552
2553 if (is_breakpoint (bl->owner))
2554 {
2555 build_target_condition_list (bl);
2556 build_target_command_list (bl);
2557 /* Reset the modification marker. */
2558 bl->needs_update = 0;
2559 }
2560
2561 if (bl->loc_type == bp_loc_software_breakpoint
2562 || bl->loc_type == bp_loc_hardware_breakpoint)
2563 {
2564 if (bl->owner->type != bp_hardware_breakpoint)
2565 {
2566 /* If the explicitly specified breakpoint type
2567 is not hardware breakpoint, check the memory map to see
2568 if the breakpoint address is in read only memory or not.
2569
2570 Two important cases are:
2571 - location type is not hardware breakpoint, memory
2572 is readonly. We change the type of the location to
2573 hardware breakpoint.
2574 - location type is hardware breakpoint, memory is
2575 read-write. This means we've previously made the
2576 location hardware one, but then the memory map changed,
2577 so we undo.
2578
2579 When breakpoints are removed, remove_breakpoints will use
2580 location types we've just set here, the only possible
2581 problem is that memory map has changed during running
2582 program, but it's not going to work anyway with current
2583 gdb. */
2584 struct mem_region *mr
2585 = lookup_mem_region (bl->target_info.reqstd_address);
2586
2587 if (mr)
2588 {
2589 if (automatic_hardware_breakpoints)
2590 {
2591 enum bp_loc_type new_type;
2592
2593 if (mr->attrib.mode != MEM_RW)
2594 new_type = bp_loc_hardware_breakpoint;
2595 else
2596 new_type = bp_loc_software_breakpoint;
2597
2598 if (new_type != bl->loc_type)
2599 {
2600 static int said = 0;
2601
2602 bl->loc_type = new_type;
2603 if (!said)
2604 {
2605 fprintf_filtered (gdb_stdout,
2606 _("Note: automatically using "
2607 "hardware breakpoints for "
2608 "read-only addresses.\n"));
2609 said = 1;
2610 }
2611 }
2612 }
2613 else if (bl->loc_type == bp_loc_software_breakpoint
2614 && mr->attrib.mode != MEM_RW)
2615 {
2616 fprintf_unfiltered (tmp_error_stream,
2617 _("Cannot insert breakpoint %d.\n"
2618 "Cannot set software breakpoint "
2619 "at read-only address %s\n"),
2620 bl->owner->number,
2621 paddress (bl->gdbarch, bl->address));
2622 return 1;
2623 }
2624 }
2625 }
2626
2627 /* First check to see if we have to handle an overlay. */
2628 if (overlay_debugging == ovly_off
2629 || bl->section == NULL
2630 || !(section_is_overlay (bl->section)))
2631 {
2632 /* No overlay handling: just set the breakpoint. */
2633 TRY
2634 {
2635 int val;
2636
2637 val = bl->owner->ops->insert_location (bl);
2638 if (val)
2639 bp_err = GENERIC_ERROR;
2640 }
2641 CATCH (e, RETURN_MASK_ALL)
2642 {
2643 bp_err = e.error;
2644 bp_err_message = e.message;
2645 }
2646 END_CATCH
2647 }
2648 else
2649 {
2650 /* This breakpoint is in an overlay section.
2651 Shall we set a breakpoint at the LMA? */
2652 if (!overlay_events_enabled)
2653 {
2654 /* Yes -- overlay event support is not active,
2655 so we must try to set a breakpoint at the LMA.
2656 This will not work for a hardware breakpoint. */
2657 if (bl->loc_type == bp_loc_hardware_breakpoint)
2658 warning (_("hardware breakpoint %d not supported in overlay!"),
2659 bl->owner->number);
2660 else
2661 {
2662 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2663 bl->section);
2664 /* Set a software (trap) breakpoint at the LMA. */
2665 bl->overlay_target_info = bl->target_info;
2666 bl->overlay_target_info.reqstd_address = addr;
2667
2668 /* No overlay handling: just set the breakpoint. */
2669 TRY
2670 {
2671 int val;
2672
2673 bl->overlay_target_info.kind
2674 = breakpoint_kind (bl, &addr);
2675 bl->overlay_target_info.placed_address = addr;
2676 val = target_insert_breakpoint (bl->gdbarch,
2677 &bl->overlay_target_info);
2678 if (val)
2679 bp_err = GENERIC_ERROR;
2680 }
2681 CATCH (e, RETURN_MASK_ALL)
2682 {
2683 bp_err = e.error;
2684 bp_err_message = e.message;
2685 }
2686 END_CATCH
2687
2688 if (bp_err != GDB_NO_ERROR)
2689 fprintf_unfiltered (tmp_error_stream,
2690 "Overlay breakpoint %d "
2691 "failed: in ROM?\n",
2692 bl->owner->number);
2693 }
2694 }
2695 /* Shall we set a breakpoint at the VMA? */
2696 if (section_is_mapped (bl->section))
2697 {
2698 /* Yes. This overlay section is mapped into memory. */
2699 TRY
2700 {
2701 int val;
2702
2703 val = bl->owner->ops->insert_location (bl);
2704 if (val)
2705 bp_err = GENERIC_ERROR;
2706 }
2707 CATCH (e, RETURN_MASK_ALL)
2708 {
2709 bp_err = e.error;
2710 bp_err_message = e.message;
2711 }
2712 END_CATCH
2713 }
2714 else
2715 {
2716 /* No. This breakpoint will not be inserted.
2717 No error, but do not mark the bp as 'inserted'. */
2718 return 0;
2719 }
2720 }
2721
2722 if (bp_err != GDB_NO_ERROR)
2723 {
2724 /* Can't set the breakpoint. */
2725
2726 /* In some cases, we might not be able to insert a
2727 breakpoint in a shared library that has already been
2728 removed, but we have not yet processed the shlib unload
2729 event. Unfortunately, some targets that implement
2730 breakpoint insertion themselves can't tell why the
2731 breakpoint insertion failed (e.g., the remote target
2732 doesn't define error codes), so we must treat generic
2733 errors as memory errors. */
2734 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2735 && bl->loc_type == bp_loc_software_breakpoint
2736 && (solib_name_from_address (bl->pspace, bl->address)
2737 || shared_objfile_contains_address_p (bl->pspace,
2738 bl->address)))
2739 {
2740 /* See also: disable_breakpoints_in_shlibs. */
2741 bl->shlib_disabled = 1;
2742 observer_notify_breakpoint_modified (bl->owner);
2743 if (!*disabled_breaks)
2744 {
2745 fprintf_unfiltered (tmp_error_stream,
2746 "Cannot insert breakpoint %d.\n",
2747 bl->owner->number);
2748 fprintf_unfiltered (tmp_error_stream,
2749 "Temporarily disabling shared "
2750 "library breakpoints:\n");
2751 }
2752 *disabled_breaks = 1;
2753 fprintf_unfiltered (tmp_error_stream,
2754 "breakpoint #%d\n", bl->owner->number);
2755 return 0;
2756 }
2757 else
2758 {
2759 if (bl->loc_type == bp_loc_hardware_breakpoint)
2760 {
2761 *hw_breakpoint_error = 1;
2762 *hw_bp_error_explained_already = bp_err_message != NULL;
2763 fprintf_unfiltered (tmp_error_stream,
2764 "Cannot insert hardware breakpoint %d%s",
2765 bl->owner->number, bp_err_message ? ":" : ".\n");
2766 if (bp_err_message != NULL)
2767 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2768 }
2769 else
2770 {
2771 if (bp_err_message == NULL)
2772 {
2773 std::string message
2774 = memory_error_message (TARGET_XFER_E_IO,
2775 bl->gdbarch, bl->address);
2776
2777 fprintf_unfiltered (tmp_error_stream,
2778 "Cannot insert breakpoint %d.\n"
2779 "%s\n",
2780 bl->owner->number, message.c_str ());
2781 }
2782 else
2783 {
2784 fprintf_unfiltered (tmp_error_stream,
2785 "Cannot insert breakpoint %d: %s\n",
2786 bl->owner->number,
2787 bp_err_message);
2788 }
2789 }
2790 return 1;
2791
2792 }
2793 }
2794 else
2795 bl->inserted = 1;
2796
2797 return 0;
2798 }
2799
2800 else if (bl->loc_type == bp_loc_hardware_watchpoint
2801 /* NOTE drow/2003-09-08: This state only exists for removing
2802 watchpoints. It's not clear that it's necessary... */
2803 && bl->owner->disposition != disp_del_at_next_stop)
2804 {
2805 int val;
2806
2807 gdb_assert (bl->owner->ops != NULL
2808 && bl->owner->ops->insert_location != NULL);
2809
2810 val = bl->owner->ops->insert_location (bl);
2811
2812 /* If trying to set a read-watchpoint, and it turns out it's not
2813 supported, try emulating one with an access watchpoint. */
2814 if (val == 1 && bl->watchpoint_type == hw_read)
2815 {
2816 struct bp_location *loc, **loc_temp;
2817
2818 /* But don't try to insert it, if there's already another
2819 hw_access location that would be considered a duplicate
2820 of this one. */
2821 ALL_BP_LOCATIONS (loc, loc_temp)
2822 if (loc != bl
2823 && loc->watchpoint_type == hw_access
2824 && watchpoint_locations_match (bl, loc))
2825 {
2826 bl->duplicate = 1;
2827 bl->inserted = 1;
2828 bl->target_info = loc->target_info;
2829 bl->watchpoint_type = hw_access;
2830 val = 0;
2831 break;
2832 }
2833
2834 if (val == 1)
2835 {
2836 bl->watchpoint_type = hw_access;
2837 val = bl->owner->ops->insert_location (bl);
2838
2839 if (val)
2840 /* Back to the original value. */
2841 bl->watchpoint_type = hw_read;
2842 }
2843 }
2844
2845 bl->inserted = (val == 0);
2846 }
2847
2848 else if (bl->owner->type == bp_catchpoint)
2849 {
2850 int val;
2851
2852 gdb_assert (bl->owner->ops != NULL
2853 && bl->owner->ops->insert_location != NULL);
2854
2855 val = bl->owner->ops->insert_location (bl);
2856 if (val)
2857 {
2858 bl->owner->enable_state = bp_disabled;
2859
2860 if (val == 1)
2861 warning (_("\
2862 Error inserting catchpoint %d: Your system does not support this type\n\
2863 of catchpoint."), bl->owner->number);
2864 else
2865 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2866 }
2867
2868 bl->inserted = (val == 0);
2869
2870 /* We've already printed an error message if there was a problem
2871 inserting this catchpoint, and we've disabled the catchpoint,
2872 so just return success. */
2873 return 0;
2874 }
2875
2876 return 0;
2877 }
2878
2879 /* This function is called when program space PSPACE is about to be
2880 deleted. It takes care of updating breakpoints to not reference
2881 PSPACE anymore. */
2882
2883 void
2884 breakpoint_program_space_exit (struct program_space *pspace)
2885 {
2886 struct breakpoint *b, *b_temp;
2887 struct bp_location *loc, **loc_temp;
2888
2889 /* Remove any breakpoint that was set through this program space. */
2890 ALL_BREAKPOINTS_SAFE (b, b_temp)
2891 {
2892 if (b->pspace == pspace)
2893 delete_breakpoint (b);
2894 }
2895
2896 /* Breakpoints set through other program spaces could have locations
2897 bound to PSPACE as well. Remove those. */
2898 ALL_BP_LOCATIONS (loc, loc_temp)
2899 {
2900 struct bp_location *tmp;
2901
2902 if (loc->pspace == pspace)
2903 {
2904 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2905 if (loc->owner->loc == loc)
2906 loc->owner->loc = loc->next;
2907 else
2908 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2909 if (tmp->next == loc)
2910 {
2911 tmp->next = loc->next;
2912 break;
2913 }
2914 }
2915 }
2916
2917 /* Now update the global location list to permanently delete the
2918 removed locations above. */
2919 update_global_location_list (UGLL_DONT_INSERT);
2920 }
2921
2922 /* Make sure all breakpoints are inserted in inferior.
2923 Throws exception on any error.
2924 A breakpoint that is already inserted won't be inserted
2925 again, so calling this function twice is safe. */
2926 void
2927 insert_breakpoints (void)
2928 {
2929 struct breakpoint *bpt;
2930
2931 ALL_BREAKPOINTS (bpt)
2932 if (is_hardware_watchpoint (bpt))
2933 {
2934 struct watchpoint *w = (struct watchpoint *) bpt;
2935
2936 update_watchpoint (w, 0 /* don't reparse. */);
2937 }
2938
2939 /* Updating watchpoints creates new locations, so update the global
2940 location list. Explicitly tell ugll to insert locations and
2941 ignore breakpoints_always_inserted_mode. */
2942 update_global_location_list (UGLL_INSERT);
2943 }
2944
2945 /* Invoke CALLBACK for each of bp_location. */
2946
2947 void
2948 iterate_over_bp_locations (walk_bp_location_callback callback)
2949 {
2950 struct bp_location *loc, **loc_tmp;
2951
2952 ALL_BP_LOCATIONS (loc, loc_tmp)
2953 {
2954 callback (loc, NULL);
2955 }
2956 }
2957
2958 /* This is used when we need to synch breakpoint conditions between GDB and the
2959 target. It is the case with deleting and disabling of breakpoints when using
2960 always-inserted mode. */
2961
2962 static void
2963 update_inserted_breakpoint_locations (void)
2964 {
2965 struct bp_location *bl, **blp_tmp;
2966 int error_flag = 0;
2967 int val = 0;
2968 int disabled_breaks = 0;
2969 int hw_breakpoint_error = 0;
2970 int hw_bp_details_reported = 0;
2971
2972 string_file tmp_error_stream;
2973
2974 /* Explicitly mark the warning -- this will only be printed if
2975 there was an error. */
2976 tmp_error_stream.puts ("Warning:\n");
2977
2978 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2979
2980 ALL_BP_LOCATIONS (bl, blp_tmp)
2981 {
2982 /* We only want to update software breakpoints and hardware
2983 breakpoints. */
2984 if (!is_breakpoint (bl->owner))
2985 continue;
2986
2987 /* We only want to update locations that are already inserted
2988 and need updating. This is to avoid unwanted insertion during
2989 deletion of breakpoints. */
2990 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2991 continue;
2992
2993 switch_to_program_space_and_thread (bl->pspace);
2994
2995 /* For targets that support global breakpoints, there's no need
2996 to select an inferior to insert breakpoint to. In fact, even
2997 if we aren't attached to any process yet, we should still
2998 insert breakpoints. */
2999 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3000 && ptid_equal (inferior_ptid, null_ptid))
3001 continue;
3002
3003 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3004 &hw_breakpoint_error, &hw_bp_details_reported);
3005 if (val)
3006 error_flag = val;
3007 }
3008
3009 if (error_flag)
3010 {
3011 target_terminal::ours_for_output ();
3012 error_stream (tmp_error_stream);
3013 }
3014 }
3015
3016 /* Used when starting or continuing the program. */
3017
3018 static void
3019 insert_breakpoint_locations (void)
3020 {
3021 struct breakpoint *bpt;
3022 struct bp_location *bl, **blp_tmp;
3023 int error_flag = 0;
3024 int val = 0;
3025 int disabled_breaks = 0;
3026 int hw_breakpoint_error = 0;
3027 int hw_bp_error_explained_already = 0;
3028
3029 string_file tmp_error_stream;
3030
3031 /* Explicitly mark the warning -- this will only be printed if
3032 there was an error. */
3033 tmp_error_stream.puts ("Warning:\n");
3034
3035 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3036
3037 ALL_BP_LOCATIONS (bl, blp_tmp)
3038 {
3039 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3040 continue;
3041
3042 /* There is no point inserting thread-specific breakpoints if
3043 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3044 has BL->OWNER always non-NULL. */
3045 if (bl->owner->thread != -1
3046 && !valid_global_thread_id (bl->owner->thread))
3047 continue;
3048
3049 switch_to_program_space_and_thread (bl->pspace);
3050
3051 /* For targets that support global breakpoints, there's no need
3052 to select an inferior to insert breakpoint to. In fact, even
3053 if we aren't attached to any process yet, we should still
3054 insert breakpoints. */
3055 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3056 && ptid_equal (inferior_ptid, null_ptid))
3057 continue;
3058
3059 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3060 &hw_breakpoint_error, &hw_bp_error_explained_already);
3061 if (val)
3062 error_flag = val;
3063 }
3064
3065 /* If we failed to insert all locations of a watchpoint, remove
3066 them, as half-inserted watchpoint is of limited use. */
3067 ALL_BREAKPOINTS (bpt)
3068 {
3069 int some_failed = 0;
3070 struct bp_location *loc;
3071
3072 if (!is_hardware_watchpoint (bpt))
3073 continue;
3074
3075 if (!breakpoint_enabled (bpt))
3076 continue;
3077
3078 if (bpt->disposition == disp_del_at_next_stop)
3079 continue;
3080
3081 for (loc = bpt->loc; loc; loc = loc->next)
3082 if (!loc->inserted && should_be_inserted (loc))
3083 {
3084 some_failed = 1;
3085 break;
3086 }
3087 if (some_failed)
3088 {
3089 for (loc = bpt->loc; loc; loc = loc->next)
3090 if (loc->inserted)
3091 remove_breakpoint (loc);
3092
3093 hw_breakpoint_error = 1;
3094 tmp_error_stream.printf ("Could not insert "
3095 "hardware watchpoint %d.\n",
3096 bpt->number);
3097 error_flag = -1;
3098 }
3099 }
3100
3101 if (error_flag)
3102 {
3103 /* If a hardware breakpoint or watchpoint was inserted, add a
3104 message about possibly exhausted resources. */
3105 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3106 {
3107 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3108 You may have requested too many hardware breakpoints/watchpoints.\n");
3109 }
3110 target_terminal::ours_for_output ();
3111 error_stream (tmp_error_stream);
3112 }
3113 }
3114
3115 /* Used when the program stops.
3116 Returns zero if successful, or non-zero if there was a problem
3117 removing a breakpoint location. */
3118
3119 int
3120 remove_breakpoints (void)
3121 {
3122 struct bp_location *bl, **blp_tmp;
3123 int val = 0;
3124
3125 ALL_BP_LOCATIONS (bl, blp_tmp)
3126 {
3127 if (bl->inserted && !is_tracepoint (bl->owner))
3128 val |= remove_breakpoint (bl);
3129 }
3130 return val;
3131 }
3132
3133 /* When a thread exits, remove breakpoints that are related to
3134 that thread. */
3135
3136 static void
3137 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3138 {
3139 struct breakpoint *b, *b_tmp;
3140
3141 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3142 {
3143 if (b->thread == tp->global_num && user_breakpoint_p (b))
3144 {
3145 b->disposition = disp_del_at_next_stop;
3146
3147 printf_filtered (_("\
3148 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3149 b->number, print_thread_id (tp));
3150
3151 /* Hide it from the user. */
3152 b->number = 0;
3153 }
3154 }
3155 }
3156
3157 /* Remove breakpoints of process PID. */
3158
3159 int
3160 remove_breakpoints_pid (int pid)
3161 {
3162 struct bp_location *bl, **blp_tmp;
3163 int val;
3164 struct inferior *inf = find_inferior_pid (pid);
3165
3166 ALL_BP_LOCATIONS (bl, blp_tmp)
3167 {
3168 if (bl->pspace != inf->pspace)
3169 continue;
3170
3171 if (bl->inserted && !bl->target_info.persist)
3172 {
3173 val = remove_breakpoint (bl);
3174 if (val != 0)
3175 return val;
3176 }
3177 }
3178 return 0;
3179 }
3180
3181 int
3182 reattach_breakpoints (int pid)
3183 {
3184 struct bp_location *bl, **blp_tmp;
3185 int val;
3186 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3187 struct inferior *inf;
3188 struct thread_info *tp;
3189
3190 tp = any_live_thread_of_process (pid);
3191 if (tp == NULL)
3192 return 1;
3193
3194 inf = find_inferior_pid (pid);
3195
3196 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3197 inferior_ptid = tp->ptid;
3198
3199 string_file tmp_error_stream;
3200
3201 ALL_BP_LOCATIONS (bl, blp_tmp)
3202 {
3203 if (bl->pspace != inf->pspace)
3204 continue;
3205
3206 if (bl->inserted)
3207 {
3208 bl->inserted = 0;
3209 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
3210 if (val != 0)
3211 return val;
3212 }
3213 }
3214 return 0;
3215 }
3216
3217 static int internal_breakpoint_number = -1;
3218
3219 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3220 If INTERNAL is non-zero, the breakpoint number will be populated
3221 from internal_breakpoint_number and that variable decremented.
3222 Otherwise the breakpoint number will be populated from
3223 breakpoint_count and that value incremented. Internal breakpoints
3224 do not set the internal var bpnum. */
3225 static void
3226 set_breakpoint_number (int internal, struct breakpoint *b)
3227 {
3228 if (internal)
3229 b->number = internal_breakpoint_number--;
3230 else
3231 {
3232 set_breakpoint_count (breakpoint_count + 1);
3233 b->number = breakpoint_count;
3234 }
3235 }
3236
3237 static struct breakpoint *
3238 create_internal_breakpoint (struct gdbarch *gdbarch,
3239 CORE_ADDR address, enum bptype type,
3240 const struct breakpoint_ops *ops)
3241 {
3242 symtab_and_line sal;
3243 sal.pc = address;
3244 sal.section = find_pc_overlay (sal.pc);
3245 sal.pspace = current_program_space;
3246
3247 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3248 b->number = internal_breakpoint_number--;
3249 b->disposition = disp_donttouch;
3250
3251 return b;
3252 }
3253
3254 static const char *const longjmp_names[] =
3255 {
3256 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3257 };
3258 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3259
3260 /* Per-objfile data private to breakpoint.c. */
3261 struct breakpoint_objfile_data
3262 {
3263 /* Minimal symbol for "_ovly_debug_event" (if any). */
3264 struct bound_minimal_symbol overlay_msym;
3265
3266 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3267 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3268
3269 /* True if we have looked for longjmp probes. */
3270 int longjmp_searched;
3271
3272 /* SystemTap probe points for longjmp (if any). */
3273 VEC (probe_p) *longjmp_probes;
3274
3275 /* Minimal symbol for "std::terminate()" (if any). */
3276 struct bound_minimal_symbol terminate_msym;
3277
3278 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3279 struct bound_minimal_symbol exception_msym;
3280
3281 /* True if we have looked for exception probes. */
3282 int exception_searched;
3283
3284 /* SystemTap probe points for unwinding (if any). */
3285 VEC (probe_p) *exception_probes;
3286 };
3287
3288 static const struct objfile_data *breakpoint_objfile_key;
3289
3290 /* Minimal symbol not found sentinel. */
3291 static struct minimal_symbol msym_not_found;
3292
3293 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3294
3295 static int
3296 msym_not_found_p (const struct minimal_symbol *msym)
3297 {
3298 return msym == &msym_not_found;
3299 }
3300
3301 /* Return per-objfile data needed by breakpoint.c.
3302 Allocate the data if necessary. */
3303
3304 static struct breakpoint_objfile_data *
3305 get_breakpoint_objfile_data (struct objfile *objfile)
3306 {
3307 struct breakpoint_objfile_data *bp_objfile_data;
3308
3309 bp_objfile_data = ((struct breakpoint_objfile_data *)
3310 objfile_data (objfile, breakpoint_objfile_key));
3311 if (bp_objfile_data == NULL)
3312 {
3313 bp_objfile_data =
3314 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3315
3316 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3317 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3318 }
3319 return bp_objfile_data;
3320 }
3321
3322 static void
3323 free_breakpoint_probes (struct objfile *obj, void *data)
3324 {
3325 struct breakpoint_objfile_data *bp_objfile_data
3326 = (struct breakpoint_objfile_data *) data;
3327
3328 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3329 VEC_free (probe_p, bp_objfile_data->exception_probes);
3330 }
3331
3332 static void
3333 create_overlay_event_breakpoint (void)
3334 {
3335 struct objfile *objfile;
3336 const char *const func_name = "_ovly_debug_event";
3337
3338 ALL_OBJFILES (objfile)
3339 {
3340 struct breakpoint *b;
3341 struct breakpoint_objfile_data *bp_objfile_data;
3342 CORE_ADDR addr;
3343 struct explicit_location explicit_loc;
3344
3345 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3346
3347 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3348 continue;
3349
3350 if (bp_objfile_data->overlay_msym.minsym == NULL)
3351 {
3352 struct bound_minimal_symbol m;
3353
3354 m = lookup_minimal_symbol_text (func_name, objfile);
3355 if (m.minsym == NULL)
3356 {
3357 /* Avoid future lookups in this objfile. */
3358 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3359 continue;
3360 }
3361 bp_objfile_data->overlay_msym = m;
3362 }
3363
3364 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3365 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3366 bp_overlay_event,
3367 &internal_breakpoint_ops);
3368 initialize_explicit_location (&explicit_loc);
3369 explicit_loc.function_name = ASTRDUP (func_name);
3370 b->location = new_explicit_location (&explicit_loc);
3371
3372 if (overlay_debugging == ovly_auto)
3373 {
3374 b->enable_state = bp_enabled;
3375 overlay_events_enabled = 1;
3376 }
3377 else
3378 {
3379 b->enable_state = bp_disabled;
3380 overlay_events_enabled = 0;
3381 }
3382 }
3383 }
3384
3385 static void
3386 create_longjmp_master_breakpoint (void)
3387 {
3388 struct program_space *pspace;
3389
3390 scoped_restore_current_program_space restore_pspace;
3391
3392 ALL_PSPACES (pspace)
3393 {
3394 struct objfile *objfile;
3395
3396 set_current_program_space (pspace);
3397
3398 ALL_OBJFILES (objfile)
3399 {
3400 int i;
3401 struct gdbarch *gdbarch;
3402 struct breakpoint_objfile_data *bp_objfile_data;
3403
3404 gdbarch = get_objfile_arch (objfile);
3405
3406 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3407
3408 if (!bp_objfile_data->longjmp_searched)
3409 {
3410 VEC (probe_p) *ret;
3411
3412 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3413 if (ret != NULL)
3414 {
3415 /* We are only interested in checking one element. */
3416 struct probe *p = VEC_index (probe_p, ret, 0);
3417
3418 if (!can_evaluate_probe_arguments (p))
3419 {
3420 /* We cannot use the probe interface here, because it does
3421 not know how to evaluate arguments. */
3422 VEC_free (probe_p, ret);
3423 ret = NULL;
3424 }
3425 }
3426 bp_objfile_data->longjmp_probes = ret;
3427 bp_objfile_data->longjmp_searched = 1;
3428 }
3429
3430 if (bp_objfile_data->longjmp_probes != NULL)
3431 {
3432 int i;
3433 struct probe *probe;
3434 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3435
3436 for (i = 0;
3437 VEC_iterate (probe_p,
3438 bp_objfile_data->longjmp_probes,
3439 i, probe);
3440 ++i)
3441 {
3442 struct breakpoint *b;
3443
3444 b = create_internal_breakpoint (gdbarch,
3445 get_probe_address (probe,
3446 objfile),
3447 bp_longjmp_master,
3448 &internal_breakpoint_ops);
3449 b->location = new_probe_location ("-probe-stap libc:longjmp");
3450 b->enable_state = bp_disabled;
3451 }
3452
3453 continue;
3454 }
3455
3456 if (!gdbarch_get_longjmp_target_p (gdbarch))
3457 continue;
3458
3459 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3460 {
3461 struct breakpoint *b;
3462 const char *func_name;
3463 CORE_ADDR addr;
3464 struct explicit_location explicit_loc;
3465
3466 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3467 continue;
3468
3469 func_name = longjmp_names[i];
3470 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3471 {
3472 struct bound_minimal_symbol m;
3473
3474 m = lookup_minimal_symbol_text (func_name, objfile);
3475 if (m.minsym == NULL)
3476 {
3477 /* Prevent future lookups in this objfile. */
3478 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3479 continue;
3480 }
3481 bp_objfile_data->longjmp_msym[i] = m;
3482 }
3483
3484 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3485 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3486 &internal_breakpoint_ops);
3487 initialize_explicit_location (&explicit_loc);
3488 explicit_loc.function_name = ASTRDUP (func_name);
3489 b->location = new_explicit_location (&explicit_loc);
3490 b->enable_state = bp_disabled;
3491 }
3492 }
3493 }
3494 }
3495
3496 /* Create a master std::terminate breakpoint. */
3497 static void
3498 create_std_terminate_master_breakpoint (void)
3499 {
3500 struct program_space *pspace;
3501 const char *const func_name = "std::terminate()";
3502
3503 scoped_restore_current_program_space restore_pspace;
3504
3505 ALL_PSPACES (pspace)
3506 {
3507 struct objfile *objfile;
3508 CORE_ADDR addr;
3509
3510 set_current_program_space (pspace);
3511
3512 ALL_OBJFILES (objfile)
3513 {
3514 struct breakpoint *b;
3515 struct breakpoint_objfile_data *bp_objfile_data;
3516 struct explicit_location explicit_loc;
3517
3518 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3519
3520 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3521 continue;
3522
3523 if (bp_objfile_data->terminate_msym.minsym == NULL)
3524 {
3525 struct bound_minimal_symbol m;
3526
3527 m = lookup_minimal_symbol (func_name, NULL, objfile);
3528 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3529 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3530 {
3531 /* Prevent future lookups in this objfile. */
3532 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3533 continue;
3534 }
3535 bp_objfile_data->terminate_msym = m;
3536 }
3537
3538 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3539 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3540 bp_std_terminate_master,
3541 &internal_breakpoint_ops);
3542 initialize_explicit_location (&explicit_loc);
3543 explicit_loc.function_name = ASTRDUP (func_name);
3544 b->location = new_explicit_location (&explicit_loc);
3545 b->enable_state = bp_disabled;
3546 }
3547 }
3548 }
3549
3550 /* Install a master breakpoint on the unwinder's debug hook. */
3551
3552 static void
3553 create_exception_master_breakpoint (void)
3554 {
3555 struct objfile *objfile;
3556 const char *const func_name = "_Unwind_DebugHook";
3557
3558 ALL_OBJFILES (objfile)
3559 {
3560 struct breakpoint *b;
3561 struct gdbarch *gdbarch;
3562 struct breakpoint_objfile_data *bp_objfile_data;
3563 CORE_ADDR addr;
3564 struct explicit_location explicit_loc;
3565
3566 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3567
3568 /* We prefer the SystemTap probe point if it exists. */
3569 if (!bp_objfile_data->exception_searched)
3570 {
3571 VEC (probe_p) *ret;
3572
3573 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3574
3575 if (ret != NULL)
3576 {
3577 /* We are only interested in checking one element. */
3578 struct probe *p = VEC_index (probe_p, ret, 0);
3579
3580 if (!can_evaluate_probe_arguments (p))
3581 {
3582 /* We cannot use the probe interface here, because it does
3583 not know how to evaluate arguments. */
3584 VEC_free (probe_p, ret);
3585 ret = NULL;
3586 }
3587 }
3588 bp_objfile_data->exception_probes = ret;
3589 bp_objfile_data->exception_searched = 1;
3590 }
3591
3592 if (bp_objfile_data->exception_probes != NULL)
3593 {
3594 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3595 int i;
3596 struct probe *probe;
3597
3598 for (i = 0;
3599 VEC_iterate (probe_p,
3600 bp_objfile_data->exception_probes,
3601 i, probe);
3602 ++i)
3603 {
3604 struct breakpoint *b;
3605
3606 b = create_internal_breakpoint (gdbarch,
3607 get_probe_address (probe,
3608 objfile),
3609 bp_exception_master,
3610 &internal_breakpoint_ops);
3611 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3612 b->enable_state = bp_disabled;
3613 }
3614
3615 continue;
3616 }
3617
3618 /* Otherwise, try the hook function. */
3619
3620 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3621 continue;
3622
3623 gdbarch = get_objfile_arch (objfile);
3624
3625 if (bp_objfile_data->exception_msym.minsym == NULL)
3626 {
3627 struct bound_minimal_symbol debug_hook;
3628
3629 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3630 if (debug_hook.minsym == NULL)
3631 {
3632 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3633 continue;
3634 }
3635
3636 bp_objfile_data->exception_msym = debug_hook;
3637 }
3638
3639 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3640 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3641 &current_target);
3642 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3643 &internal_breakpoint_ops);
3644 initialize_explicit_location (&explicit_loc);
3645 explicit_loc.function_name = ASTRDUP (func_name);
3646 b->location = new_explicit_location (&explicit_loc);
3647 b->enable_state = bp_disabled;
3648 }
3649 }
3650
3651 /* Does B have a location spec? */
3652
3653 static int
3654 breakpoint_event_location_empty_p (const struct breakpoint *b)
3655 {
3656 return b->location != NULL && event_location_empty_p (b->location.get ());
3657 }
3658
3659 void
3660 update_breakpoints_after_exec (void)
3661 {
3662 struct breakpoint *b, *b_tmp;
3663 struct bp_location *bploc, **bplocp_tmp;
3664
3665 /* We're about to delete breakpoints from GDB's lists. If the
3666 INSERTED flag is true, GDB will try to lift the breakpoints by
3667 writing the breakpoints' "shadow contents" back into memory. The
3668 "shadow contents" are NOT valid after an exec, so GDB should not
3669 do that. Instead, the target is responsible from marking
3670 breakpoints out as soon as it detects an exec. We don't do that
3671 here instead, because there may be other attempts to delete
3672 breakpoints after detecting an exec and before reaching here. */
3673 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3674 if (bploc->pspace == current_program_space)
3675 gdb_assert (!bploc->inserted);
3676
3677 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3678 {
3679 if (b->pspace != current_program_space)
3680 continue;
3681
3682 /* Solib breakpoints must be explicitly reset after an exec(). */
3683 if (b->type == bp_shlib_event)
3684 {
3685 delete_breakpoint (b);
3686 continue;
3687 }
3688
3689 /* JIT breakpoints must be explicitly reset after an exec(). */
3690 if (b->type == bp_jit_event)
3691 {
3692 delete_breakpoint (b);
3693 continue;
3694 }
3695
3696 /* Thread event breakpoints must be set anew after an exec(),
3697 as must overlay event and longjmp master breakpoints. */
3698 if (b->type == bp_thread_event || b->type == bp_overlay_event
3699 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3700 || b->type == bp_exception_master)
3701 {
3702 delete_breakpoint (b);
3703 continue;
3704 }
3705
3706 /* Step-resume breakpoints are meaningless after an exec(). */
3707 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3708 {
3709 delete_breakpoint (b);
3710 continue;
3711 }
3712
3713 /* Just like single-step breakpoints. */
3714 if (b->type == bp_single_step)
3715 {
3716 delete_breakpoint (b);
3717 continue;
3718 }
3719
3720 /* Longjmp and longjmp-resume breakpoints are also meaningless
3721 after an exec. */
3722 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3723 || b->type == bp_longjmp_call_dummy
3724 || b->type == bp_exception || b->type == bp_exception_resume)
3725 {
3726 delete_breakpoint (b);
3727 continue;
3728 }
3729
3730 if (b->type == bp_catchpoint)
3731 {
3732 /* For now, none of the bp_catchpoint breakpoints need to
3733 do anything at this point. In the future, if some of
3734 the catchpoints need to something, we will need to add
3735 a new method, and call this method from here. */
3736 continue;
3737 }
3738
3739 /* bp_finish is a special case. The only way we ought to be able
3740 to see one of these when an exec() has happened, is if the user
3741 caught a vfork, and then said "finish". Ordinarily a finish just
3742 carries them to the call-site of the current callee, by setting
3743 a temporary bp there and resuming. But in this case, the finish
3744 will carry them entirely through the vfork & exec.
3745
3746 We don't want to allow a bp_finish to remain inserted now. But
3747 we can't safely delete it, 'cause finish_command has a handle to
3748 the bp on a bpstat, and will later want to delete it. There's a
3749 chance (and I've seen it happen) that if we delete the bp_finish
3750 here, that its storage will get reused by the time finish_command
3751 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3752 We really must allow finish_command to delete a bp_finish.
3753
3754 In the absence of a general solution for the "how do we know
3755 it's safe to delete something others may have handles to?"
3756 problem, what we'll do here is just uninsert the bp_finish, and
3757 let finish_command delete it.
3758
3759 (We know the bp_finish is "doomed" in the sense that it's
3760 momentary, and will be deleted as soon as finish_command sees
3761 the inferior stopped. So it doesn't matter that the bp's
3762 address is probably bogus in the new a.out, unlike e.g., the
3763 solib breakpoints.) */
3764
3765 if (b->type == bp_finish)
3766 {
3767 continue;
3768 }
3769
3770 /* Without a symbolic address, we have little hope of the
3771 pre-exec() address meaning the same thing in the post-exec()
3772 a.out. */
3773 if (breakpoint_event_location_empty_p (b))
3774 {
3775 delete_breakpoint (b);
3776 continue;
3777 }
3778 }
3779 }
3780
3781 int
3782 detach_breakpoints (ptid_t ptid)
3783 {
3784 struct bp_location *bl, **blp_tmp;
3785 int val = 0;
3786 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3787 struct inferior *inf = current_inferior ();
3788
3789 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3790 error (_("Cannot detach breakpoints of inferior_ptid"));
3791
3792 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3793 inferior_ptid = ptid;
3794 ALL_BP_LOCATIONS (bl, blp_tmp)
3795 {
3796 if (bl->pspace != inf->pspace)
3797 continue;
3798
3799 /* This function must physically remove breakpoints locations
3800 from the specified ptid, without modifying the breakpoint
3801 package's state. Locations of type bp_loc_other are only
3802 maintained at GDB side. So, there is no need to remove
3803 these bp_loc_other locations. Moreover, removing these
3804 would modify the breakpoint package's state. */
3805 if (bl->loc_type == bp_loc_other)
3806 continue;
3807
3808 if (bl->inserted)
3809 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3810 }
3811
3812 return val;
3813 }
3814
3815 /* Remove the breakpoint location BL from the current address space.
3816 Note that this is used to detach breakpoints from a child fork.
3817 When we get here, the child isn't in the inferior list, and neither
3818 do we have objects to represent its address space --- we should
3819 *not* look at bl->pspace->aspace here. */
3820
3821 static int
3822 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3823 {
3824 int val;
3825
3826 /* BL is never in moribund_locations by our callers. */
3827 gdb_assert (bl->owner != NULL);
3828
3829 /* The type of none suggests that owner is actually deleted.
3830 This should not ever happen. */
3831 gdb_assert (bl->owner->type != bp_none);
3832
3833 if (bl->loc_type == bp_loc_software_breakpoint
3834 || bl->loc_type == bp_loc_hardware_breakpoint)
3835 {
3836 /* "Normal" instruction breakpoint: either the standard
3837 trap-instruction bp (bp_breakpoint), or a
3838 bp_hardware_breakpoint. */
3839
3840 /* First check to see if we have to handle an overlay. */
3841 if (overlay_debugging == ovly_off
3842 || bl->section == NULL
3843 || !(section_is_overlay (bl->section)))
3844 {
3845 /* No overlay handling: just remove the breakpoint. */
3846
3847 /* If we're trying to uninsert a memory breakpoint that we
3848 know is set in a dynamic object that is marked
3849 shlib_disabled, then either the dynamic object was
3850 removed with "remove-symbol-file" or with
3851 "nosharedlibrary". In the former case, we don't know
3852 whether another dynamic object might have loaded over the
3853 breakpoint's address -- the user might well let us know
3854 about it next with add-symbol-file (the whole point of
3855 add-symbol-file is letting the user manually maintain a
3856 list of dynamically loaded objects). If we have the
3857 breakpoint's shadow memory, that is, this is a software
3858 breakpoint managed by GDB, check whether the breakpoint
3859 is still inserted in memory, to avoid overwriting wrong
3860 code with stale saved shadow contents. Note that HW
3861 breakpoints don't have shadow memory, as they're
3862 implemented using a mechanism that is not dependent on
3863 being able to modify the target's memory, and as such
3864 they should always be removed. */
3865 if (bl->shlib_disabled
3866 && bl->target_info.shadow_len != 0
3867 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3868 val = 0;
3869 else
3870 val = bl->owner->ops->remove_location (bl, reason);
3871 }
3872 else
3873 {
3874 /* This breakpoint is in an overlay section.
3875 Did we set a breakpoint at the LMA? */
3876 if (!overlay_events_enabled)
3877 {
3878 /* Yes -- overlay event support is not active, so we
3879 should have set a breakpoint at the LMA. Remove it.
3880 */
3881 /* Ignore any failures: if the LMA is in ROM, we will
3882 have already warned when we failed to insert it. */
3883 if (bl->loc_type == bp_loc_hardware_breakpoint)
3884 target_remove_hw_breakpoint (bl->gdbarch,
3885 &bl->overlay_target_info);
3886 else
3887 target_remove_breakpoint (bl->gdbarch,
3888 &bl->overlay_target_info,
3889 reason);
3890 }
3891 /* Did we set a breakpoint at the VMA?
3892 If so, we will have marked the breakpoint 'inserted'. */
3893 if (bl->inserted)
3894 {
3895 /* Yes -- remove it. Previously we did not bother to
3896 remove the breakpoint if the section had been
3897 unmapped, but let's not rely on that being safe. We
3898 don't know what the overlay manager might do. */
3899
3900 /* However, we should remove *software* breakpoints only
3901 if the section is still mapped, or else we overwrite
3902 wrong code with the saved shadow contents. */
3903 if (bl->loc_type == bp_loc_hardware_breakpoint
3904 || section_is_mapped (bl->section))
3905 val = bl->owner->ops->remove_location (bl, reason);
3906 else
3907 val = 0;
3908 }
3909 else
3910 {
3911 /* No -- not inserted, so no need to remove. No error. */
3912 val = 0;
3913 }
3914 }
3915
3916 /* In some cases, we might not be able to remove a breakpoint in
3917 a shared library that has already been removed, but we have
3918 not yet processed the shlib unload event. Similarly for an
3919 unloaded add-symbol-file object - the user might not yet have
3920 had the chance to remove-symbol-file it. shlib_disabled will
3921 be set if the library/object has already been removed, but
3922 the breakpoint hasn't been uninserted yet, e.g., after
3923 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3924 always-inserted mode. */
3925 if (val
3926 && (bl->loc_type == bp_loc_software_breakpoint
3927 && (bl->shlib_disabled
3928 || solib_name_from_address (bl->pspace, bl->address)
3929 || shared_objfile_contains_address_p (bl->pspace,
3930 bl->address))))
3931 val = 0;
3932
3933 if (val)
3934 return val;
3935 bl->inserted = (reason == DETACH_BREAKPOINT);
3936 }
3937 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3938 {
3939 gdb_assert (bl->owner->ops != NULL
3940 && bl->owner->ops->remove_location != NULL);
3941
3942 bl->inserted = (reason == DETACH_BREAKPOINT);
3943 bl->owner->ops->remove_location (bl, reason);
3944
3945 /* Failure to remove any of the hardware watchpoints comes here. */
3946 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3947 warning (_("Could not remove hardware watchpoint %d."),
3948 bl->owner->number);
3949 }
3950 else if (bl->owner->type == bp_catchpoint
3951 && breakpoint_enabled (bl->owner)
3952 && !bl->duplicate)
3953 {
3954 gdb_assert (bl->owner->ops != NULL
3955 && bl->owner->ops->remove_location != NULL);
3956
3957 val = bl->owner->ops->remove_location (bl, reason);
3958 if (val)
3959 return val;
3960
3961 bl->inserted = (reason == DETACH_BREAKPOINT);
3962 }
3963
3964 return 0;
3965 }
3966
3967 static int
3968 remove_breakpoint (struct bp_location *bl)
3969 {
3970 /* BL is never in moribund_locations by our callers. */
3971 gdb_assert (bl->owner != NULL);
3972
3973 /* The type of none suggests that owner is actually deleted.
3974 This should not ever happen. */
3975 gdb_assert (bl->owner->type != bp_none);
3976
3977 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3978
3979 switch_to_program_space_and_thread (bl->pspace);
3980
3981 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3982 }
3983
3984 /* Clear the "inserted" flag in all breakpoints. */
3985
3986 void
3987 mark_breakpoints_out (void)
3988 {
3989 struct bp_location *bl, **blp_tmp;
3990
3991 ALL_BP_LOCATIONS (bl, blp_tmp)
3992 if (bl->pspace == current_program_space)
3993 bl->inserted = 0;
3994 }
3995
3996 /* Clear the "inserted" flag in all breakpoints and delete any
3997 breakpoints which should go away between runs of the program.
3998
3999 Plus other such housekeeping that has to be done for breakpoints
4000 between runs.
4001
4002 Note: this function gets called at the end of a run (by
4003 generic_mourn_inferior) and when a run begins (by
4004 init_wait_for_inferior). */
4005
4006
4007
4008 void
4009 breakpoint_init_inferior (enum inf_context context)
4010 {
4011 struct breakpoint *b, *b_tmp;
4012 struct bp_location *bl;
4013 int ix;
4014 struct program_space *pspace = current_program_space;
4015
4016 /* If breakpoint locations are shared across processes, then there's
4017 nothing to do. */
4018 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4019 return;
4020
4021 mark_breakpoints_out ();
4022
4023 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4024 {
4025 if (b->loc && b->loc->pspace != pspace)
4026 continue;
4027
4028 switch (b->type)
4029 {
4030 case bp_call_dummy:
4031 case bp_longjmp_call_dummy:
4032
4033 /* If the call dummy breakpoint is at the entry point it will
4034 cause problems when the inferior is rerun, so we better get
4035 rid of it. */
4036
4037 case bp_watchpoint_scope:
4038
4039 /* Also get rid of scope breakpoints. */
4040
4041 case bp_shlib_event:
4042
4043 /* Also remove solib event breakpoints. Their addresses may
4044 have changed since the last time we ran the program.
4045 Actually we may now be debugging against different target;
4046 and so the solib backend that installed this breakpoint may
4047 not be used in by the target. E.g.,
4048
4049 (gdb) file prog-linux
4050 (gdb) run # native linux target
4051 ...
4052 (gdb) kill
4053 (gdb) file prog-win.exe
4054 (gdb) tar rem :9999 # remote Windows gdbserver.
4055 */
4056
4057 case bp_step_resume:
4058
4059 /* Also remove step-resume breakpoints. */
4060
4061 case bp_single_step:
4062
4063 /* Also remove single-step breakpoints. */
4064
4065 delete_breakpoint (b);
4066 break;
4067
4068 case bp_watchpoint:
4069 case bp_hardware_watchpoint:
4070 case bp_read_watchpoint:
4071 case bp_access_watchpoint:
4072 {
4073 struct watchpoint *w = (struct watchpoint *) b;
4074
4075 /* Likewise for watchpoints on local expressions. */
4076 if (w->exp_valid_block != NULL)
4077 delete_breakpoint (b);
4078 else
4079 {
4080 /* Get rid of existing locations, which are no longer
4081 valid. New ones will be created in
4082 update_watchpoint, when the inferior is restarted.
4083 The next update_global_location_list call will
4084 garbage collect them. */
4085 b->loc = NULL;
4086
4087 if (context == inf_starting)
4088 {
4089 /* Reset val field to force reread of starting value in
4090 insert_breakpoints. */
4091 if (w->val)
4092 value_free (w->val);
4093 w->val = NULL;
4094 w->val_valid = 0;
4095 }
4096 }
4097 }
4098 break;
4099 default:
4100 break;
4101 }
4102 }
4103
4104 /* Get rid of the moribund locations. */
4105 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4106 decref_bp_location (&bl);
4107 VEC_free (bp_location_p, moribund_locations);
4108 }
4109
4110 /* These functions concern about actual breakpoints inserted in the
4111 target --- to e.g. check if we need to do decr_pc adjustment or if
4112 we need to hop over the bkpt --- so we check for address space
4113 match, not program space. */
4114
4115 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4116 exists at PC. It returns ordinary_breakpoint_here if it's an
4117 ordinary breakpoint, or permanent_breakpoint_here if it's a
4118 permanent breakpoint.
4119 - When continuing from a location with an ordinary breakpoint, we
4120 actually single step once before calling insert_breakpoints.
4121 - When continuing from a location with a permanent breakpoint, we
4122 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4123 the target, to advance the PC past the breakpoint. */
4124
4125 enum breakpoint_here
4126 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4127 {
4128 struct bp_location *bl, **blp_tmp;
4129 int any_breakpoint_here = 0;
4130
4131 ALL_BP_LOCATIONS (bl, blp_tmp)
4132 {
4133 if (bl->loc_type != bp_loc_software_breakpoint
4134 && bl->loc_type != bp_loc_hardware_breakpoint)
4135 continue;
4136
4137 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4138 if ((breakpoint_enabled (bl->owner)
4139 || bl->permanent)
4140 && breakpoint_location_address_match (bl, aspace, pc))
4141 {
4142 if (overlay_debugging
4143 && section_is_overlay (bl->section)
4144 && !section_is_mapped (bl->section))
4145 continue; /* unmapped overlay -- can't be a match */
4146 else if (bl->permanent)
4147 return permanent_breakpoint_here;
4148 else
4149 any_breakpoint_here = 1;
4150 }
4151 }
4152
4153 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4154 }
4155
4156 /* See breakpoint.h. */
4157
4158 int
4159 breakpoint_in_range_p (struct address_space *aspace,
4160 CORE_ADDR addr, ULONGEST len)
4161 {
4162 struct bp_location *bl, **blp_tmp;
4163
4164 ALL_BP_LOCATIONS (bl, blp_tmp)
4165 {
4166 if (bl->loc_type != bp_loc_software_breakpoint
4167 && bl->loc_type != bp_loc_hardware_breakpoint)
4168 continue;
4169
4170 if ((breakpoint_enabled (bl->owner)
4171 || bl->permanent)
4172 && breakpoint_location_address_range_overlap (bl, aspace,
4173 addr, len))
4174 {
4175 if (overlay_debugging
4176 && section_is_overlay (bl->section)
4177 && !section_is_mapped (bl->section))
4178 {
4179 /* Unmapped overlay -- can't be a match. */
4180 continue;
4181 }
4182
4183 return 1;
4184 }
4185 }
4186
4187 return 0;
4188 }
4189
4190 /* Return true if there's a moribund breakpoint at PC. */
4191
4192 int
4193 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4194 {
4195 struct bp_location *loc;
4196 int ix;
4197
4198 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4199 if (breakpoint_location_address_match (loc, aspace, pc))
4200 return 1;
4201
4202 return 0;
4203 }
4204
4205 /* Returns non-zero iff BL is inserted at PC, in address space
4206 ASPACE. */
4207
4208 static int
4209 bp_location_inserted_here_p (struct bp_location *bl,
4210 struct address_space *aspace, CORE_ADDR pc)
4211 {
4212 if (bl->inserted
4213 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4214 aspace, pc))
4215 {
4216 if (overlay_debugging
4217 && section_is_overlay (bl->section)
4218 && !section_is_mapped (bl->section))
4219 return 0; /* unmapped overlay -- can't be a match */
4220 else
4221 return 1;
4222 }
4223 return 0;
4224 }
4225
4226 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4227
4228 int
4229 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4230 {
4231 struct bp_location **blp, **blp_tmp = NULL;
4232
4233 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4234 {
4235 struct bp_location *bl = *blp;
4236
4237 if (bl->loc_type != bp_loc_software_breakpoint
4238 && bl->loc_type != bp_loc_hardware_breakpoint)
4239 continue;
4240
4241 if (bp_location_inserted_here_p (bl, aspace, pc))
4242 return 1;
4243 }
4244 return 0;
4245 }
4246
4247 /* This function returns non-zero iff there is a software breakpoint
4248 inserted at PC. */
4249
4250 int
4251 software_breakpoint_inserted_here_p (struct address_space *aspace,
4252 CORE_ADDR pc)
4253 {
4254 struct bp_location **blp, **blp_tmp = NULL;
4255
4256 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4257 {
4258 struct bp_location *bl = *blp;
4259
4260 if (bl->loc_type != bp_loc_software_breakpoint)
4261 continue;
4262
4263 if (bp_location_inserted_here_p (bl, aspace, pc))
4264 return 1;
4265 }
4266
4267 return 0;
4268 }
4269
4270 /* See breakpoint.h. */
4271
4272 int
4273 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4274 CORE_ADDR pc)
4275 {
4276 struct bp_location **blp, **blp_tmp = NULL;
4277
4278 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4279 {
4280 struct bp_location *bl = *blp;
4281
4282 if (bl->loc_type != bp_loc_hardware_breakpoint)
4283 continue;
4284
4285 if (bp_location_inserted_here_p (bl, aspace, pc))
4286 return 1;
4287 }
4288
4289 return 0;
4290 }
4291
4292 int
4293 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4294 CORE_ADDR addr, ULONGEST len)
4295 {
4296 struct breakpoint *bpt;
4297
4298 ALL_BREAKPOINTS (bpt)
4299 {
4300 struct bp_location *loc;
4301
4302 if (bpt->type != bp_hardware_watchpoint
4303 && bpt->type != bp_access_watchpoint)
4304 continue;
4305
4306 if (!breakpoint_enabled (bpt))
4307 continue;
4308
4309 for (loc = bpt->loc; loc; loc = loc->next)
4310 if (loc->pspace->aspace == aspace && loc->inserted)
4311 {
4312 CORE_ADDR l, h;
4313
4314 /* Check for intersection. */
4315 l = std::max<CORE_ADDR> (loc->address, addr);
4316 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4317 if (l < h)
4318 return 1;
4319 }
4320 }
4321 return 0;
4322 }
4323 \f
4324
4325 /* bpstat stuff. External routines' interfaces are documented
4326 in breakpoint.h. */
4327
4328 int
4329 is_catchpoint (struct breakpoint *ep)
4330 {
4331 return (ep->type == bp_catchpoint);
4332 }
4333
4334 /* Frees any storage that is part of a bpstat. Does not walk the
4335 'next' chain. */
4336
4337 bpstats::~bpstats ()
4338 {
4339 if (old_val != NULL)
4340 value_free (old_val);
4341 if (bp_location_at != NULL)
4342 decref_bp_location (&bp_location_at);
4343 }
4344
4345 /* Clear a bpstat so that it says we are not at any breakpoint.
4346 Also free any storage that is part of a bpstat. */
4347
4348 void
4349 bpstat_clear (bpstat *bsp)
4350 {
4351 bpstat p;
4352 bpstat q;
4353
4354 if (bsp == 0)
4355 return;
4356 p = *bsp;
4357 while (p != NULL)
4358 {
4359 q = p->next;
4360 delete p;
4361 p = q;
4362 }
4363 *bsp = NULL;
4364 }
4365
4366 bpstats::bpstats (const bpstats &other)
4367 : next (NULL),
4368 bp_location_at (other.bp_location_at),
4369 breakpoint_at (other.breakpoint_at),
4370 commands (other.commands),
4371 old_val (other.old_val),
4372 print (other.print),
4373 stop (other.stop),
4374 print_it (other.print_it)
4375 {
4376 if (old_val != NULL)
4377 {
4378 old_val = value_copy (old_val);
4379 release_value (old_val);
4380 }
4381 incref_bp_location (bp_location_at);
4382 }
4383
4384 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4385 is part of the bpstat is copied as well. */
4386
4387 bpstat
4388 bpstat_copy (bpstat bs)
4389 {
4390 bpstat p = NULL;
4391 bpstat tmp;
4392 bpstat retval = NULL;
4393
4394 if (bs == NULL)
4395 return bs;
4396
4397 for (; bs != NULL; bs = bs->next)
4398 {
4399 tmp = new bpstats (*bs);
4400
4401 if (p == NULL)
4402 /* This is the first thing in the chain. */
4403 retval = tmp;
4404 else
4405 p->next = tmp;
4406 p = tmp;
4407 }
4408 p->next = NULL;
4409 return retval;
4410 }
4411
4412 /* Find the bpstat associated with this breakpoint. */
4413
4414 bpstat
4415 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4416 {
4417 if (bsp == NULL)
4418 return NULL;
4419
4420 for (; bsp != NULL; bsp = bsp->next)
4421 {
4422 if (bsp->breakpoint_at == breakpoint)
4423 return bsp;
4424 }
4425 return NULL;
4426 }
4427
4428 /* See breakpoint.h. */
4429
4430 int
4431 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4432 {
4433 for (; bsp != NULL; bsp = bsp->next)
4434 {
4435 if (bsp->breakpoint_at == NULL)
4436 {
4437 /* A moribund location can never explain a signal other than
4438 GDB_SIGNAL_TRAP. */
4439 if (sig == GDB_SIGNAL_TRAP)
4440 return 1;
4441 }
4442 else
4443 {
4444 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4445 sig))
4446 return 1;
4447 }
4448 }
4449
4450 return 0;
4451 }
4452
4453 /* Put in *NUM the breakpoint number of the first breakpoint we are
4454 stopped at. *BSP upon return is a bpstat which points to the
4455 remaining breakpoints stopped at (but which is not guaranteed to be
4456 good for anything but further calls to bpstat_num).
4457
4458 Return 0 if passed a bpstat which does not indicate any breakpoints.
4459 Return -1 if stopped at a breakpoint that has been deleted since
4460 we set it.
4461 Return 1 otherwise. */
4462
4463 int
4464 bpstat_num (bpstat *bsp, int *num)
4465 {
4466 struct breakpoint *b;
4467
4468 if ((*bsp) == NULL)
4469 return 0; /* No more breakpoint values */
4470
4471 /* We assume we'll never have several bpstats that correspond to a
4472 single breakpoint -- otherwise, this function might return the
4473 same number more than once and this will look ugly. */
4474 b = (*bsp)->breakpoint_at;
4475 *bsp = (*bsp)->next;
4476 if (b == NULL)
4477 return -1; /* breakpoint that's been deleted since */
4478
4479 *num = b->number; /* We have its number */
4480 return 1;
4481 }
4482
4483 /* See breakpoint.h. */
4484
4485 void
4486 bpstat_clear_actions (void)
4487 {
4488 struct thread_info *tp;
4489 bpstat bs;
4490
4491 if (ptid_equal (inferior_ptid, null_ptid))
4492 return;
4493
4494 tp = find_thread_ptid (inferior_ptid);
4495 if (tp == NULL)
4496 return;
4497
4498 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4499 {
4500 bs->commands = NULL;
4501
4502 if (bs->old_val != NULL)
4503 {
4504 value_free (bs->old_val);
4505 bs->old_val = NULL;
4506 }
4507 }
4508 }
4509
4510 /* Called when a command is about to proceed the inferior. */
4511
4512 static void
4513 breakpoint_about_to_proceed (void)
4514 {
4515 if (!ptid_equal (inferior_ptid, null_ptid))
4516 {
4517 struct thread_info *tp = inferior_thread ();
4518
4519 /* Allow inferior function calls in breakpoint commands to not
4520 interrupt the command list. When the call finishes
4521 successfully, the inferior will be standing at the same
4522 breakpoint as if nothing happened. */
4523 if (tp->control.in_infcall)
4524 return;
4525 }
4526
4527 breakpoint_proceeded = 1;
4528 }
4529
4530 /* Stub for cleaning up our state if we error-out of a breakpoint
4531 command. */
4532 static void
4533 cleanup_executing_breakpoints (void *ignore)
4534 {
4535 executing_breakpoint_commands = 0;
4536 }
4537
4538 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4539 or its equivalent. */
4540
4541 static int
4542 command_line_is_silent (struct command_line *cmd)
4543 {
4544 return cmd && (strcmp ("silent", cmd->line) == 0);
4545 }
4546
4547 /* Execute all the commands associated with all the breakpoints at
4548 this location. Any of these commands could cause the process to
4549 proceed beyond this point, etc. We look out for such changes by
4550 checking the global "breakpoint_proceeded" after each command.
4551
4552 Returns true if a breakpoint command resumed the inferior. In that
4553 case, it is the caller's responsibility to recall it again with the
4554 bpstat of the current thread. */
4555
4556 static int
4557 bpstat_do_actions_1 (bpstat *bsp)
4558 {
4559 bpstat bs;
4560 struct cleanup *old_chain;
4561 int again = 0;
4562
4563 /* Avoid endless recursion if a `source' command is contained
4564 in bs->commands. */
4565 if (executing_breakpoint_commands)
4566 return 0;
4567
4568 executing_breakpoint_commands = 1;
4569 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4570
4571 scoped_restore preventer = prevent_dont_repeat ();
4572
4573 /* This pointer will iterate over the list of bpstat's. */
4574 bs = *bsp;
4575
4576 breakpoint_proceeded = 0;
4577 for (; bs != NULL; bs = bs->next)
4578 {
4579 struct command_line *cmd = NULL;
4580
4581 /* Take ownership of the BSP's command tree, if it has one.
4582
4583 The command tree could legitimately contain commands like
4584 'step' and 'next', which call clear_proceed_status, which
4585 frees stop_bpstat's command tree. To make sure this doesn't
4586 free the tree we're executing out from under us, we need to
4587 take ownership of the tree ourselves. Since a given bpstat's
4588 commands are only executed once, we don't need to copy it; we
4589 can clear the pointer in the bpstat, and make sure we free
4590 the tree when we're done. */
4591 counted_command_line ccmd = bs->commands;
4592 bs->commands = NULL;
4593 if (ccmd != NULL)
4594 cmd = ccmd.get ();
4595 if (command_line_is_silent (cmd))
4596 {
4597 /* The action has been already done by bpstat_stop_status. */
4598 cmd = cmd->next;
4599 }
4600
4601 while (cmd != NULL)
4602 {
4603 execute_control_command (cmd);
4604
4605 if (breakpoint_proceeded)
4606 break;
4607 else
4608 cmd = cmd->next;
4609 }
4610
4611 if (breakpoint_proceeded)
4612 {
4613 if (current_ui->async)
4614 /* If we are in async mode, then the target might be still
4615 running, not stopped at any breakpoint, so nothing for
4616 us to do here -- just return to the event loop. */
4617 ;
4618 else
4619 /* In sync mode, when execute_control_command returns
4620 we're already standing on the next breakpoint.
4621 Breakpoint commands for that stop were not run, since
4622 execute_command does not run breakpoint commands --
4623 only command_line_handler does, but that one is not
4624 involved in execution of breakpoint commands. So, we
4625 can now execute breakpoint commands. It should be
4626 noted that making execute_command do bpstat actions is
4627 not an option -- in this case we'll have recursive
4628 invocation of bpstat for each breakpoint with a
4629 command, and can easily blow up GDB stack. Instead, we
4630 return true, which will trigger the caller to recall us
4631 with the new stop_bpstat. */
4632 again = 1;
4633 break;
4634 }
4635 }
4636 do_cleanups (old_chain);
4637 return again;
4638 }
4639
4640 void
4641 bpstat_do_actions (void)
4642 {
4643 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4644
4645 /* Do any commands attached to breakpoint we are stopped at. */
4646 while (!ptid_equal (inferior_ptid, null_ptid)
4647 && target_has_execution
4648 && !is_exited (inferior_ptid)
4649 && !is_executing (inferior_ptid))
4650 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4651 and only return when it is stopped at the next breakpoint, we
4652 keep doing breakpoint actions until it returns false to
4653 indicate the inferior was not resumed. */
4654 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4655 break;
4656
4657 discard_cleanups (cleanup_if_error);
4658 }
4659
4660 /* Print out the (old or new) value associated with a watchpoint. */
4661
4662 static void
4663 watchpoint_value_print (struct value *val, struct ui_file *stream)
4664 {
4665 if (val == NULL)
4666 fprintf_unfiltered (stream, _("<unreadable>"));
4667 else
4668 {
4669 struct value_print_options opts;
4670 get_user_print_options (&opts);
4671 value_print (val, stream, &opts);
4672 }
4673 }
4674
4675 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4676 debugging multiple threads. */
4677
4678 void
4679 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4680 {
4681 if (uiout->is_mi_like_p ())
4682 return;
4683
4684 uiout->text ("\n");
4685
4686 if (show_thread_that_caused_stop ())
4687 {
4688 const char *name;
4689 struct thread_info *thr = inferior_thread ();
4690
4691 uiout->text ("Thread ");
4692 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4693
4694 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4695 if (name != NULL)
4696 {
4697 uiout->text (" \"");
4698 uiout->field_fmt ("name", "%s", name);
4699 uiout->text ("\"");
4700 }
4701
4702 uiout->text (" hit ");
4703 }
4704 }
4705
4706 /* Generic routine for printing messages indicating why we
4707 stopped. The behavior of this function depends on the value
4708 'print_it' in the bpstat structure. Under some circumstances we
4709 may decide not to print anything here and delegate the task to
4710 normal_stop(). */
4711
4712 static enum print_stop_action
4713 print_bp_stop_message (bpstat bs)
4714 {
4715 switch (bs->print_it)
4716 {
4717 case print_it_noop:
4718 /* Nothing should be printed for this bpstat entry. */
4719 return PRINT_UNKNOWN;
4720 break;
4721
4722 case print_it_done:
4723 /* We still want to print the frame, but we already printed the
4724 relevant messages. */
4725 return PRINT_SRC_AND_LOC;
4726 break;
4727
4728 case print_it_normal:
4729 {
4730 struct breakpoint *b = bs->breakpoint_at;
4731
4732 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4733 which has since been deleted. */
4734 if (b == NULL)
4735 return PRINT_UNKNOWN;
4736
4737 /* Normal case. Call the breakpoint's print_it method. */
4738 return b->ops->print_it (bs);
4739 }
4740 break;
4741
4742 default:
4743 internal_error (__FILE__, __LINE__,
4744 _("print_bp_stop_message: unrecognized enum value"));
4745 break;
4746 }
4747 }
4748
4749 /* A helper function that prints a shared library stopped event. */
4750
4751 static void
4752 print_solib_event (int is_catchpoint)
4753 {
4754 int any_deleted
4755 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4756 int any_added
4757 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4758
4759 if (!is_catchpoint)
4760 {
4761 if (any_added || any_deleted)
4762 current_uiout->text (_("Stopped due to shared library event:\n"));
4763 else
4764 current_uiout->text (_("Stopped due to shared library event (no "
4765 "libraries added or removed)\n"));
4766 }
4767
4768 if (current_uiout->is_mi_like_p ())
4769 current_uiout->field_string ("reason",
4770 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4771
4772 if (any_deleted)
4773 {
4774 char *name;
4775 int ix;
4776
4777 current_uiout->text (_(" Inferior unloaded "));
4778 ui_out_emit_list list_emitter (current_uiout, "removed");
4779 for (ix = 0;
4780 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4781 ix, name);
4782 ++ix)
4783 {
4784 if (ix > 0)
4785 current_uiout->text (" ");
4786 current_uiout->field_string ("library", name);
4787 current_uiout->text ("\n");
4788 }
4789 }
4790
4791 if (any_added)
4792 {
4793 struct so_list *iter;
4794 int ix;
4795
4796 current_uiout->text (_(" Inferior loaded "));
4797 ui_out_emit_list list_emitter (current_uiout, "added");
4798 for (ix = 0;
4799 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4800 ix, iter);
4801 ++ix)
4802 {
4803 if (ix > 0)
4804 current_uiout->text (" ");
4805 current_uiout->field_string ("library", iter->so_name);
4806 current_uiout->text ("\n");
4807 }
4808 }
4809 }
4810
4811 /* Print a message indicating what happened. This is called from
4812 normal_stop(). The input to this routine is the head of the bpstat
4813 list - a list of the eventpoints that caused this stop. KIND is
4814 the target_waitkind for the stopping event. This
4815 routine calls the generic print routine for printing a message
4816 about reasons for stopping. This will print (for example) the
4817 "Breakpoint n," part of the output. The return value of this
4818 routine is one of:
4819
4820 PRINT_UNKNOWN: Means we printed nothing.
4821 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4822 code to print the location. An example is
4823 "Breakpoint 1, " which should be followed by
4824 the location.
4825 PRINT_SRC_ONLY: Means we printed something, but there is no need
4826 to also print the location part of the message.
4827 An example is the catch/throw messages, which
4828 don't require a location appended to the end.
4829 PRINT_NOTHING: We have done some printing and we don't need any
4830 further info to be printed. */
4831
4832 enum print_stop_action
4833 bpstat_print (bpstat bs, int kind)
4834 {
4835 enum print_stop_action val;
4836
4837 /* Maybe another breakpoint in the chain caused us to stop.
4838 (Currently all watchpoints go on the bpstat whether hit or not.
4839 That probably could (should) be changed, provided care is taken
4840 with respect to bpstat_explains_signal). */
4841 for (; bs; bs = bs->next)
4842 {
4843 val = print_bp_stop_message (bs);
4844 if (val == PRINT_SRC_ONLY
4845 || val == PRINT_SRC_AND_LOC
4846 || val == PRINT_NOTHING)
4847 return val;
4848 }
4849
4850 /* If we had hit a shared library event breakpoint,
4851 print_bp_stop_message would print out this message. If we hit an
4852 OS-level shared library event, do the same thing. */
4853 if (kind == TARGET_WAITKIND_LOADED)
4854 {
4855 print_solib_event (0);
4856 return PRINT_NOTHING;
4857 }
4858
4859 /* We reached the end of the chain, or we got a null BS to start
4860 with and nothing was printed. */
4861 return PRINT_UNKNOWN;
4862 }
4863
4864 /* Evaluate the expression EXP and return 1 if value is zero.
4865 This returns the inverse of the condition because it is called
4866 from catch_errors which returns 0 if an exception happened, and if an
4867 exception happens we want execution to stop.
4868 The argument is a "struct expression *" that has been cast to a
4869 "void *" to make it pass through catch_errors. */
4870
4871 static int
4872 breakpoint_cond_eval (void *exp)
4873 {
4874 struct value *mark = value_mark ();
4875 int i = !value_true (evaluate_expression ((struct expression *) exp));
4876
4877 value_free_to_mark (mark);
4878 return i;
4879 }
4880
4881 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4882
4883 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4884 : next (NULL),
4885 bp_location_at (bl),
4886 breakpoint_at (bl->owner),
4887 commands (NULL),
4888 old_val (NULL),
4889 print (0),
4890 stop (0),
4891 print_it (print_it_normal)
4892 {
4893 incref_bp_location (bl);
4894 **bs_link_pointer = this;
4895 *bs_link_pointer = &next;
4896 }
4897
4898 bpstats::bpstats ()
4899 : next (NULL),
4900 bp_location_at (NULL),
4901 breakpoint_at (NULL),
4902 commands (NULL),
4903 old_val (NULL),
4904 print (0),
4905 stop (0),
4906 print_it (print_it_normal)
4907 {
4908 }
4909 \f
4910 /* The target has stopped with waitstatus WS. Check if any hardware
4911 watchpoints have triggered, according to the target. */
4912
4913 int
4914 watchpoints_triggered (struct target_waitstatus *ws)
4915 {
4916 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4917 CORE_ADDR addr;
4918 struct breakpoint *b;
4919
4920 if (!stopped_by_watchpoint)
4921 {
4922 /* We were not stopped by a watchpoint. Mark all watchpoints
4923 as not triggered. */
4924 ALL_BREAKPOINTS (b)
4925 if (is_hardware_watchpoint (b))
4926 {
4927 struct watchpoint *w = (struct watchpoint *) b;
4928
4929 w->watchpoint_triggered = watch_triggered_no;
4930 }
4931
4932 return 0;
4933 }
4934
4935 if (!target_stopped_data_address (&current_target, &addr))
4936 {
4937 /* We were stopped by a watchpoint, but we don't know where.
4938 Mark all watchpoints as unknown. */
4939 ALL_BREAKPOINTS (b)
4940 if (is_hardware_watchpoint (b))
4941 {
4942 struct watchpoint *w = (struct watchpoint *) b;
4943
4944 w->watchpoint_triggered = watch_triggered_unknown;
4945 }
4946
4947 return 1;
4948 }
4949
4950 /* The target could report the data address. Mark watchpoints
4951 affected by this data address as triggered, and all others as not
4952 triggered. */
4953
4954 ALL_BREAKPOINTS (b)
4955 if (is_hardware_watchpoint (b))
4956 {
4957 struct watchpoint *w = (struct watchpoint *) b;
4958 struct bp_location *loc;
4959
4960 w->watchpoint_triggered = watch_triggered_no;
4961 for (loc = b->loc; loc; loc = loc->next)
4962 {
4963 if (is_masked_watchpoint (b))
4964 {
4965 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4966 CORE_ADDR start = loc->address & w->hw_wp_mask;
4967
4968 if (newaddr == start)
4969 {
4970 w->watchpoint_triggered = watch_triggered_yes;
4971 break;
4972 }
4973 }
4974 /* Exact match not required. Within range is sufficient. */
4975 else if (target_watchpoint_addr_within_range (&current_target,
4976 addr, loc->address,
4977 loc->length))
4978 {
4979 w->watchpoint_triggered = watch_triggered_yes;
4980 break;
4981 }
4982 }
4983 }
4984
4985 return 1;
4986 }
4987
4988 /* Possible return values for watchpoint_check (this can't be an enum
4989 because of check_errors). */
4990 /* The watchpoint has been deleted. */
4991 #define WP_DELETED 1
4992 /* The value has changed. */
4993 #define WP_VALUE_CHANGED 2
4994 /* The value has not changed. */
4995 #define WP_VALUE_NOT_CHANGED 3
4996 /* Ignore this watchpoint, no matter if the value changed or not. */
4997 #define WP_IGNORE 4
4998
4999 #define BP_TEMPFLAG 1
5000 #define BP_HARDWAREFLAG 2
5001
5002 /* Evaluate watchpoint condition expression and check if its value
5003 changed.
5004
5005 P should be a pointer to struct bpstat, but is defined as a void *
5006 in order for this function to be usable with catch_errors. */
5007
5008 static int
5009 watchpoint_check (void *p)
5010 {
5011 bpstat bs = (bpstat) p;
5012 struct watchpoint *b;
5013 struct frame_info *fr;
5014 int within_current_scope;
5015
5016 /* BS is built from an existing struct breakpoint. */
5017 gdb_assert (bs->breakpoint_at != NULL);
5018 b = (struct watchpoint *) bs->breakpoint_at;
5019
5020 /* If this is a local watchpoint, we only want to check if the
5021 watchpoint frame is in scope if the current thread is the thread
5022 that was used to create the watchpoint. */
5023 if (!watchpoint_in_thread_scope (b))
5024 return WP_IGNORE;
5025
5026 if (b->exp_valid_block == NULL)
5027 within_current_scope = 1;
5028 else
5029 {
5030 struct frame_info *frame = get_current_frame ();
5031 struct gdbarch *frame_arch = get_frame_arch (frame);
5032 CORE_ADDR frame_pc = get_frame_pc (frame);
5033
5034 /* stack_frame_destroyed_p() returns a non-zero value if we're
5035 still in the function but the stack frame has already been
5036 invalidated. Since we can't rely on the values of local
5037 variables after the stack has been destroyed, we are treating
5038 the watchpoint in that state as `not changed' without further
5039 checking. Don't mark watchpoints as changed if the current
5040 frame is in an epilogue - even if they are in some other
5041 frame, our view of the stack is likely to be wrong and
5042 frame_find_by_id could error out. */
5043 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5044 return WP_IGNORE;
5045
5046 fr = frame_find_by_id (b->watchpoint_frame);
5047 within_current_scope = (fr != NULL);
5048
5049 /* If we've gotten confused in the unwinder, we might have
5050 returned a frame that can't describe this variable. */
5051 if (within_current_scope)
5052 {
5053 struct symbol *function;
5054
5055 function = get_frame_function (fr);
5056 if (function == NULL
5057 || !contained_in (b->exp_valid_block,
5058 SYMBOL_BLOCK_VALUE (function)))
5059 within_current_scope = 0;
5060 }
5061
5062 if (within_current_scope)
5063 /* If we end up stopping, the current frame will get selected
5064 in normal_stop. So this call to select_frame won't affect
5065 the user. */
5066 select_frame (fr);
5067 }
5068
5069 if (within_current_scope)
5070 {
5071 /* We use value_{,free_to_}mark because it could be a *long*
5072 time before we return to the command level and call
5073 free_all_values. We can't call free_all_values because we
5074 might be in the middle of evaluating a function call. */
5075
5076 int pc = 0;
5077 struct value *mark;
5078 struct value *new_val;
5079
5080 if (is_masked_watchpoint (b))
5081 /* Since we don't know the exact trigger address (from
5082 stopped_data_address), just tell the user we've triggered
5083 a mask watchpoint. */
5084 return WP_VALUE_CHANGED;
5085
5086 mark = value_mark ();
5087 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5088
5089 if (b->val_bitsize != 0)
5090 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5091
5092 /* We use value_equal_contents instead of value_equal because
5093 the latter coerces an array to a pointer, thus comparing just
5094 the address of the array instead of its contents. This is
5095 not what we want. */
5096 if ((b->val != NULL) != (new_val != NULL)
5097 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5098 {
5099 if (new_val != NULL)
5100 {
5101 release_value (new_val);
5102 value_free_to_mark (mark);
5103 }
5104 bs->old_val = b->val;
5105 b->val = new_val;
5106 b->val_valid = 1;
5107 return WP_VALUE_CHANGED;
5108 }
5109 else
5110 {
5111 /* Nothing changed. */
5112 value_free_to_mark (mark);
5113 return WP_VALUE_NOT_CHANGED;
5114 }
5115 }
5116 else
5117 {
5118 /* This seems like the only logical thing to do because
5119 if we temporarily ignored the watchpoint, then when
5120 we reenter the block in which it is valid it contains
5121 garbage (in the case of a function, it may have two
5122 garbage values, one before and one after the prologue).
5123 So we can't even detect the first assignment to it and
5124 watch after that (since the garbage may or may not equal
5125 the first value assigned). */
5126 /* We print all the stop information in
5127 breakpoint_ops->print_it, but in this case, by the time we
5128 call breakpoint_ops->print_it this bp will be deleted
5129 already. So we have no choice but print the information
5130 here. */
5131
5132 SWITCH_THRU_ALL_UIS ()
5133 {
5134 struct ui_out *uiout = current_uiout;
5135
5136 if (uiout->is_mi_like_p ())
5137 uiout->field_string
5138 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5139 uiout->text ("\nWatchpoint ");
5140 uiout->field_int ("wpnum", b->number);
5141 uiout->text (" deleted because the program has left the block in\n"
5142 "which its expression is valid.\n");
5143 }
5144
5145 /* Make sure the watchpoint's commands aren't executed. */
5146 b->commands = NULL;
5147 watchpoint_del_at_next_stop (b);
5148
5149 return WP_DELETED;
5150 }
5151 }
5152
5153 /* Return true if it looks like target has stopped due to hitting
5154 breakpoint location BL. This function does not check if we should
5155 stop, only if BL explains the stop. */
5156
5157 static int
5158 bpstat_check_location (const struct bp_location *bl,
5159 struct address_space *aspace, CORE_ADDR bp_addr,
5160 const struct target_waitstatus *ws)
5161 {
5162 struct breakpoint *b = bl->owner;
5163
5164 /* BL is from an existing breakpoint. */
5165 gdb_assert (b != NULL);
5166
5167 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5168 }
5169
5170 /* Determine if the watched values have actually changed, and we
5171 should stop. If not, set BS->stop to 0. */
5172
5173 static void
5174 bpstat_check_watchpoint (bpstat bs)
5175 {
5176 const struct bp_location *bl;
5177 struct watchpoint *b;
5178
5179 /* BS is built for existing struct breakpoint. */
5180 bl = bs->bp_location_at;
5181 gdb_assert (bl != NULL);
5182 b = (struct watchpoint *) bs->breakpoint_at;
5183 gdb_assert (b != NULL);
5184
5185 {
5186 int must_check_value = 0;
5187
5188 if (b->type == bp_watchpoint)
5189 /* For a software watchpoint, we must always check the
5190 watched value. */
5191 must_check_value = 1;
5192 else if (b->watchpoint_triggered == watch_triggered_yes)
5193 /* We have a hardware watchpoint (read, write, or access)
5194 and the target earlier reported an address watched by
5195 this watchpoint. */
5196 must_check_value = 1;
5197 else if (b->watchpoint_triggered == watch_triggered_unknown
5198 && b->type == bp_hardware_watchpoint)
5199 /* We were stopped by a hardware watchpoint, but the target could
5200 not report the data address. We must check the watchpoint's
5201 value. Access and read watchpoints are out of luck; without
5202 a data address, we can't figure it out. */
5203 must_check_value = 1;
5204
5205 if (must_check_value)
5206 {
5207 char *message
5208 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5209 b->number);
5210 struct cleanup *cleanups = make_cleanup (xfree, message);
5211 int e = catch_errors (watchpoint_check, bs, message,
5212 RETURN_MASK_ALL);
5213 do_cleanups (cleanups);
5214 switch (e)
5215 {
5216 case WP_DELETED:
5217 /* We've already printed what needs to be printed. */
5218 bs->print_it = print_it_done;
5219 /* Stop. */
5220 break;
5221 case WP_IGNORE:
5222 bs->print_it = print_it_noop;
5223 bs->stop = 0;
5224 break;
5225 case WP_VALUE_CHANGED:
5226 if (b->type == bp_read_watchpoint)
5227 {
5228 /* There are two cases to consider here:
5229
5230 1. We're watching the triggered memory for reads.
5231 In that case, trust the target, and always report
5232 the watchpoint hit to the user. Even though
5233 reads don't cause value changes, the value may
5234 have changed since the last time it was read, and
5235 since we're not trapping writes, we will not see
5236 those, and as such we should ignore our notion of
5237 old value.
5238
5239 2. We're watching the triggered memory for both
5240 reads and writes. There are two ways this may
5241 happen:
5242
5243 2.1. This is a target that can't break on data
5244 reads only, but can break on accesses (reads or
5245 writes), such as e.g., x86. We detect this case
5246 at the time we try to insert read watchpoints.
5247
5248 2.2. Otherwise, the target supports read
5249 watchpoints, but, the user set an access or write
5250 watchpoint watching the same memory as this read
5251 watchpoint.
5252
5253 If we're watching memory writes as well as reads,
5254 ignore watchpoint hits when we find that the
5255 value hasn't changed, as reads don't cause
5256 changes. This still gives false positives when
5257 the program writes the same value to memory as
5258 what there was already in memory (we will confuse
5259 it for a read), but it's much better than
5260 nothing. */
5261
5262 int other_write_watchpoint = 0;
5263
5264 if (bl->watchpoint_type == hw_read)
5265 {
5266 struct breakpoint *other_b;
5267
5268 ALL_BREAKPOINTS (other_b)
5269 if (other_b->type == bp_hardware_watchpoint
5270 || other_b->type == bp_access_watchpoint)
5271 {
5272 struct watchpoint *other_w =
5273 (struct watchpoint *) other_b;
5274
5275 if (other_w->watchpoint_triggered
5276 == watch_triggered_yes)
5277 {
5278 other_write_watchpoint = 1;
5279 break;
5280 }
5281 }
5282 }
5283
5284 if (other_write_watchpoint
5285 || bl->watchpoint_type == hw_access)
5286 {
5287 /* We're watching the same memory for writes,
5288 and the value changed since the last time we
5289 updated it, so this trap must be for a write.
5290 Ignore it. */
5291 bs->print_it = print_it_noop;
5292 bs->stop = 0;
5293 }
5294 }
5295 break;
5296 case WP_VALUE_NOT_CHANGED:
5297 if (b->type == bp_hardware_watchpoint
5298 || b->type == bp_watchpoint)
5299 {
5300 /* Don't stop: write watchpoints shouldn't fire if
5301 the value hasn't changed. */
5302 bs->print_it = print_it_noop;
5303 bs->stop = 0;
5304 }
5305 /* Stop. */
5306 break;
5307 default:
5308 /* Can't happen. */
5309 case 0:
5310 /* Error from catch_errors. */
5311 {
5312 SWITCH_THRU_ALL_UIS ()
5313 {
5314 printf_filtered (_("Watchpoint %d deleted.\n"),
5315 b->number);
5316 }
5317 watchpoint_del_at_next_stop (b);
5318 /* We've already printed what needs to be printed. */
5319 bs->print_it = print_it_done;
5320 }
5321 break;
5322 }
5323 }
5324 else /* must_check_value == 0 */
5325 {
5326 /* This is a case where some watchpoint(s) triggered, but
5327 not at the address of this watchpoint, or else no
5328 watchpoint triggered after all. So don't print
5329 anything for this watchpoint. */
5330 bs->print_it = print_it_noop;
5331 bs->stop = 0;
5332 }
5333 }
5334 }
5335
5336 /* For breakpoints that are currently marked as telling gdb to stop,
5337 check conditions (condition proper, frame, thread and ignore count)
5338 of breakpoint referred to by BS. If we should not stop for this
5339 breakpoint, set BS->stop to 0. */
5340
5341 static void
5342 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5343 {
5344 const struct bp_location *bl;
5345 struct breakpoint *b;
5346 int value_is_zero = 0;
5347 struct expression *cond;
5348
5349 gdb_assert (bs->stop);
5350
5351 /* BS is built for existing struct breakpoint. */
5352 bl = bs->bp_location_at;
5353 gdb_assert (bl != NULL);
5354 b = bs->breakpoint_at;
5355 gdb_assert (b != NULL);
5356
5357 /* Even if the target evaluated the condition on its end and notified GDB, we
5358 need to do so again since GDB does not know if we stopped due to a
5359 breakpoint or a single step breakpoint. */
5360
5361 if (frame_id_p (b->frame_id)
5362 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5363 {
5364 bs->stop = 0;
5365 return;
5366 }
5367
5368 /* If this is a thread/task-specific breakpoint, don't waste cpu
5369 evaluating the condition if this isn't the specified
5370 thread/task. */
5371 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5372 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5373
5374 {
5375 bs->stop = 0;
5376 return;
5377 }
5378
5379 /* Evaluate extension language breakpoints that have a "stop" method
5380 implemented. */
5381 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5382
5383 if (is_watchpoint (b))
5384 {
5385 struct watchpoint *w = (struct watchpoint *) b;
5386
5387 cond = w->cond_exp.get ();
5388 }
5389 else
5390 cond = bl->cond.get ();
5391
5392 if (cond && b->disposition != disp_del_at_next_stop)
5393 {
5394 int within_current_scope = 1;
5395 struct watchpoint * w;
5396
5397 /* We use value_mark and value_free_to_mark because it could
5398 be a long time before we return to the command level and
5399 call free_all_values. We can't call free_all_values
5400 because we might be in the middle of evaluating a
5401 function call. */
5402 struct value *mark = value_mark ();
5403
5404 if (is_watchpoint (b))
5405 w = (struct watchpoint *) b;
5406 else
5407 w = NULL;
5408
5409 /* Need to select the frame, with all that implies so that
5410 the conditions will have the right context. Because we
5411 use the frame, we will not see an inlined function's
5412 variables when we arrive at a breakpoint at the start
5413 of the inlined function; the current frame will be the
5414 call site. */
5415 if (w == NULL || w->cond_exp_valid_block == NULL)
5416 select_frame (get_current_frame ());
5417 else
5418 {
5419 struct frame_info *frame;
5420
5421 /* For local watchpoint expressions, which particular
5422 instance of a local is being watched matters, so we
5423 keep track of the frame to evaluate the expression
5424 in. To evaluate the condition however, it doesn't
5425 really matter which instantiation of the function
5426 where the condition makes sense triggers the
5427 watchpoint. This allows an expression like "watch
5428 global if q > 10" set in `func', catch writes to
5429 global on all threads that call `func', or catch
5430 writes on all recursive calls of `func' by a single
5431 thread. We simply always evaluate the condition in
5432 the innermost frame that's executing where it makes
5433 sense to evaluate the condition. It seems
5434 intuitive. */
5435 frame = block_innermost_frame (w->cond_exp_valid_block);
5436 if (frame != NULL)
5437 select_frame (frame);
5438 else
5439 within_current_scope = 0;
5440 }
5441 if (within_current_scope)
5442 value_is_zero
5443 = catch_errors (breakpoint_cond_eval, cond,
5444 "Error in testing breakpoint condition:\n",
5445 RETURN_MASK_ALL);
5446 else
5447 {
5448 warning (_("Watchpoint condition cannot be tested "
5449 "in the current scope"));
5450 /* If we failed to set the right context for this
5451 watchpoint, unconditionally report it. */
5452 value_is_zero = 0;
5453 }
5454 /* FIXME-someday, should give breakpoint #. */
5455 value_free_to_mark (mark);
5456 }
5457
5458 if (cond && value_is_zero)
5459 {
5460 bs->stop = 0;
5461 }
5462 else if (b->ignore_count > 0)
5463 {
5464 b->ignore_count--;
5465 bs->stop = 0;
5466 /* Increase the hit count even though we don't stop. */
5467 ++(b->hit_count);
5468 observer_notify_breakpoint_modified (b);
5469 }
5470 }
5471
5472 /* Returns true if we need to track moribund locations of LOC's type
5473 on the current target. */
5474
5475 static int
5476 need_moribund_for_location_type (struct bp_location *loc)
5477 {
5478 return ((loc->loc_type == bp_loc_software_breakpoint
5479 && !target_supports_stopped_by_sw_breakpoint ())
5480 || (loc->loc_type == bp_loc_hardware_breakpoint
5481 && !target_supports_stopped_by_hw_breakpoint ()));
5482 }
5483
5484
5485 /* Get a bpstat associated with having just stopped at address
5486 BP_ADDR in thread PTID.
5487
5488 Determine whether we stopped at a breakpoint, etc, or whether we
5489 don't understand this stop. Result is a chain of bpstat's such
5490 that:
5491
5492 if we don't understand the stop, the result is a null pointer.
5493
5494 if we understand why we stopped, the result is not null.
5495
5496 Each element of the chain refers to a particular breakpoint or
5497 watchpoint at which we have stopped. (We may have stopped for
5498 several reasons concurrently.)
5499
5500 Each element of the chain has valid next, breakpoint_at,
5501 commands, FIXME??? fields. */
5502
5503 bpstat
5504 bpstat_stop_status (struct address_space *aspace,
5505 CORE_ADDR bp_addr, ptid_t ptid,
5506 const struct target_waitstatus *ws)
5507 {
5508 struct breakpoint *b = NULL;
5509 struct bp_location *bl;
5510 struct bp_location *loc;
5511 /* First item of allocated bpstat's. */
5512 bpstat bs_head = NULL, *bs_link = &bs_head;
5513 /* Pointer to the last thing in the chain currently. */
5514 bpstat bs;
5515 int ix;
5516 int need_remove_insert;
5517 int removed_any;
5518
5519 /* First, build the bpstat chain with locations that explain a
5520 target stop, while being careful to not set the target running,
5521 as that may invalidate locations (in particular watchpoint
5522 locations are recreated). Resuming will happen here with
5523 breakpoint conditions or watchpoint expressions that include
5524 inferior function calls. */
5525
5526 ALL_BREAKPOINTS (b)
5527 {
5528 if (!breakpoint_enabled (b))
5529 continue;
5530
5531 for (bl = b->loc; bl != NULL; bl = bl->next)
5532 {
5533 /* For hardware watchpoints, we look only at the first
5534 location. The watchpoint_check function will work on the
5535 entire expression, not the individual locations. For
5536 read watchpoints, the watchpoints_triggered function has
5537 checked all locations already. */
5538 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5539 break;
5540
5541 if (!bl->enabled || bl->shlib_disabled)
5542 continue;
5543
5544 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5545 continue;
5546
5547 /* Come here if it's a watchpoint, or if the break address
5548 matches. */
5549
5550 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5551 explain stop. */
5552
5553 /* Assume we stop. Should we find a watchpoint that is not
5554 actually triggered, or if the condition of the breakpoint
5555 evaluates as false, we'll reset 'stop' to 0. */
5556 bs->stop = 1;
5557 bs->print = 1;
5558
5559 /* If this is a scope breakpoint, mark the associated
5560 watchpoint as triggered so that we will handle the
5561 out-of-scope event. We'll get to the watchpoint next
5562 iteration. */
5563 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5564 {
5565 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5566
5567 w->watchpoint_triggered = watch_triggered_yes;
5568 }
5569 }
5570 }
5571
5572 /* Check if a moribund breakpoint explains the stop. */
5573 if (!target_supports_stopped_by_sw_breakpoint ()
5574 || !target_supports_stopped_by_hw_breakpoint ())
5575 {
5576 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5577 {
5578 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5579 && need_moribund_for_location_type (loc))
5580 {
5581 bs = new bpstats (loc, &bs_link);
5582 /* For hits of moribund locations, we should just proceed. */
5583 bs->stop = 0;
5584 bs->print = 0;
5585 bs->print_it = print_it_noop;
5586 }
5587 }
5588 }
5589
5590 /* A bit of special processing for shlib breakpoints. We need to
5591 process solib loading here, so that the lists of loaded and
5592 unloaded libraries are correct before we handle "catch load" and
5593 "catch unload". */
5594 for (bs = bs_head; bs != NULL; bs = bs->next)
5595 {
5596 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5597 {
5598 handle_solib_event ();
5599 break;
5600 }
5601 }
5602
5603 /* Now go through the locations that caused the target to stop, and
5604 check whether we're interested in reporting this stop to higher
5605 layers, or whether we should resume the target transparently. */
5606
5607 removed_any = 0;
5608
5609 for (bs = bs_head; bs != NULL; bs = bs->next)
5610 {
5611 if (!bs->stop)
5612 continue;
5613
5614 b = bs->breakpoint_at;
5615 b->ops->check_status (bs);
5616 if (bs->stop)
5617 {
5618 bpstat_check_breakpoint_conditions (bs, ptid);
5619
5620 if (bs->stop)
5621 {
5622 ++(b->hit_count);
5623 observer_notify_breakpoint_modified (b);
5624
5625 /* We will stop here. */
5626 if (b->disposition == disp_disable)
5627 {
5628 --(b->enable_count);
5629 if (b->enable_count <= 0)
5630 b->enable_state = bp_disabled;
5631 removed_any = 1;
5632 }
5633 if (b->silent)
5634 bs->print = 0;
5635 bs->commands = b->commands;
5636 if (command_line_is_silent (bs->commands
5637 ? bs->commands.get () : NULL))
5638 bs->print = 0;
5639
5640 b->ops->after_condition_true (bs);
5641 }
5642
5643 }
5644
5645 /* Print nothing for this entry if we don't stop or don't
5646 print. */
5647 if (!bs->stop || !bs->print)
5648 bs->print_it = print_it_noop;
5649 }
5650
5651 /* If we aren't stopping, the value of some hardware watchpoint may
5652 not have changed, but the intermediate memory locations we are
5653 watching may have. Don't bother if we're stopping; this will get
5654 done later. */
5655 need_remove_insert = 0;
5656 if (! bpstat_causes_stop (bs_head))
5657 for (bs = bs_head; bs != NULL; bs = bs->next)
5658 if (!bs->stop
5659 && bs->breakpoint_at
5660 && is_hardware_watchpoint (bs->breakpoint_at))
5661 {
5662 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5663
5664 update_watchpoint (w, 0 /* don't reparse. */);
5665 need_remove_insert = 1;
5666 }
5667
5668 if (need_remove_insert)
5669 update_global_location_list (UGLL_MAY_INSERT);
5670 else if (removed_any)
5671 update_global_location_list (UGLL_DONT_INSERT);
5672
5673 return bs_head;
5674 }
5675
5676 static void
5677 handle_jit_event (void)
5678 {
5679 struct frame_info *frame;
5680 struct gdbarch *gdbarch;
5681
5682 if (debug_infrun)
5683 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5684
5685 /* Switch terminal for any messages produced by
5686 breakpoint_re_set. */
5687 target_terminal::ours_for_output ();
5688
5689 frame = get_current_frame ();
5690 gdbarch = get_frame_arch (frame);
5691
5692 jit_event_handler (gdbarch);
5693
5694 target_terminal::inferior ();
5695 }
5696
5697 /* Prepare WHAT final decision for infrun. */
5698
5699 /* Decide what infrun needs to do with this bpstat. */
5700
5701 struct bpstat_what
5702 bpstat_what (bpstat bs_head)
5703 {
5704 struct bpstat_what retval;
5705 bpstat bs;
5706
5707 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5708 retval.call_dummy = STOP_NONE;
5709 retval.is_longjmp = 0;
5710
5711 for (bs = bs_head; bs != NULL; bs = bs->next)
5712 {
5713 /* Extract this BS's action. After processing each BS, we check
5714 if its action overrides all we've seem so far. */
5715 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5716 enum bptype bptype;
5717
5718 if (bs->breakpoint_at == NULL)
5719 {
5720 /* I suspect this can happen if it was a momentary
5721 breakpoint which has since been deleted. */
5722 bptype = bp_none;
5723 }
5724 else
5725 bptype = bs->breakpoint_at->type;
5726
5727 switch (bptype)
5728 {
5729 case bp_none:
5730 break;
5731 case bp_breakpoint:
5732 case bp_hardware_breakpoint:
5733 case bp_single_step:
5734 case bp_until:
5735 case bp_finish:
5736 case bp_shlib_event:
5737 if (bs->stop)
5738 {
5739 if (bs->print)
5740 this_action = BPSTAT_WHAT_STOP_NOISY;
5741 else
5742 this_action = BPSTAT_WHAT_STOP_SILENT;
5743 }
5744 else
5745 this_action = BPSTAT_WHAT_SINGLE;
5746 break;
5747 case bp_watchpoint:
5748 case bp_hardware_watchpoint:
5749 case bp_read_watchpoint:
5750 case bp_access_watchpoint:
5751 if (bs->stop)
5752 {
5753 if (bs->print)
5754 this_action = BPSTAT_WHAT_STOP_NOISY;
5755 else
5756 this_action = BPSTAT_WHAT_STOP_SILENT;
5757 }
5758 else
5759 {
5760 /* There was a watchpoint, but we're not stopping.
5761 This requires no further action. */
5762 }
5763 break;
5764 case bp_longjmp:
5765 case bp_longjmp_call_dummy:
5766 case bp_exception:
5767 if (bs->stop)
5768 {
5769 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5770 retval.is_longjmp = bptype != bp_exception;
5771 }
5772 else
5773 this_action = BPSTAT_WHAT_SINGLE;
5774 break;
5775 case bp_longjmp_resume:
5776 case bp_exception_resume:
5777 if (bs->stop)
5778 {
5779 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5780 retval.is_longjmp = bptype == bp_longjmp_resume;
5781 }
5782 else
5783 this_action = BPSTAT_WHAT_SINGLE;
5784 break;
5785 case bp_step_resume:
5786 if (bs->stop)
5787 this_action = BPSTAT_WHAT_STEP_RESUME;
5788 else
5789 {
5790 /* It is for the wrong frame. */
5791 this_action = BPSTAT_WHAT_SINGLE;
5792 }
5793 break;
5794 case bp_hp_step_resume:
5795 if (bs->stop)
5796 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5797 else
5798 {
5799 /* It is for the wrong frame. */
5800 this_action = BPSTAT_WHAT_SINGLE;
5801 }
5802 break;
5803 case bp_watchpoint_scope:
5804 case bp_thread_event:
5805 case bp_overlay_event:
5806 case bp_longjmp_master:
5807 case bp_std_terminate_master:
5808 case bp_exception_master:
5809 this_action = BPSTAT_WHAT_SINGLE;
5810 break;
5811 case bp_catchpoint:
5812 if (bs->stop)
5813 {
5814 if (bs->print)
5815 this_action = BPSTAT_WHAT_STOP_NOISY;
5816 else
5817 this_action = BPSTAT_WHAT_STOP_SILENT;
5818 }
5819 else
5820 {
5821 /* There was a catchpoint, but we're not stopping.
5822 This requires no further action. */
5823 }
5824 break;
5825 case bp_jit_event:
5826 this_action = BPSTAT_WHAT_SINGLE;
5827 break;
5828 case bp_call_dummy:
5829 /* Make sure the action is stop (silent or noisy),
5830 so infrun.c pops the dummy frame. */
5831 retval.call_dummy = STOP_STACK_DUMMY;
5832 this_action = BPSTAT_WHAT_STOP_SILENT;
5833 break;
5834 case bp_std_terminate:
5835 /* Make sure the action is stop (silent or noisy),
5836 so infrun.c pops the dummy frame. */
5837 retval.call_dummy = STOP_STD_TERMINATE;
5838 this_action = BPSTAT_WHAT_STOP_SILENT;
5839 break;
5840 case bp_tracepoint:
5841 case bp_fast_tracepoint:
5842 case bp_static_tracepoint:
5843 /* Tracepoint hits should not be reported back to GDB, and
5844 if one got through somehow, it should have been filtered
5845 out already. */
5846 internal_error (__FILE__, __LINE__,
5847 _("bpstat_what: tracepoint encountered"));
5848 break;
5849 case bp_gnu_ifunc_resolver:
5850 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5851 this_action = BPSTAT_WHAT_SINGLE;
5852 break;
5853 case bp_gnu_ifunc_resolver_return:
5854 /* The breakpoint will be removed, execution will restart from the
5855 PC of the former breakpoint. */
5856 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5857 break;
5858
5859 case bp_dprintf:
5860 if (bs->stop)
5861 this_action = BPSTAT_WHAT_STOP_SILENT;
5862 else
5863 this_action = BPSTAT_WHAT_SINGLE;
5864 break;
5865
5866 default:
5867 internal_error (__FILE__, __LINE__,
5868 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5869 }
5870
5871 retval.main_action = std::max (retval.main_action, this_action);
5872 }
5873
5874 return retval;
5875 }
5876
5877 void
5878 bpstat_run_callbacks (bpstat bs_head)
5879 {
5880 bpstat bs;
5881
5882 for (bs = bs_head; bs != NULL; bs = bs->next)
5883 {
5884 struct breakpoint *b = bs->breakpoint_at;
5885
5886 if (b == NULL)
5887 continue;
5888 switch (b->type)
5889 {
5890 case bp_jit_event:
5891 handle_jit_event ();
5892 break;
5893 case bp_gnu_ifunc_resolver:
5894 gnu_ifunc_resolver_stop (b);
5895 break;
5896 case bp_gnu_ifunc_resolver_return:
5897 gnu_ifunc_resolver_return_stop (b);
5898 break;
5899 }
5900 }
5901 }
5902
5903 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5904 without hardware support). This isn't related to a specific bpstat,
5905 just to things like whether watchpoints are set. */
5906
5907 int
5908 bpstat_should_step (void)
5909 {
5910 struct breakpoint *b;
5911
5912 ALL_BREAKPOINTS (b)
5913 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5914 return 1;
5915 return 0;
5916 }
5917
5918 int
5919 bpstat_causes_stop (bpstat bs)
5920 {
5921 for (; bs != NULL; bs = bs->next)
5922 if (bs->stop)
5923 return 1;
5924
5925 return 0;
5926 }
5927
5928 \f
5929
5930 /* Compute a string of spaces suitable to indent the next line
5931 so it starts at the position corresponding to the table column
5932 named COL_NAME in the currently active table of UIOUT. */
5933
5934 static char *
5935 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5936 {
5937 static char wrap_indent[80];
5938 int i, total_width, width, align;
5939 const char *text;
5940
5941 total_width = 0;
5942 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5943 {
5944 if (strcmp (text, col_name) == 0)
5945 {
5946 gdb_assert (total_width < sizeof wrap_indent);
5947 memset (wrap_indent, ' ', total_width);
5948 wrap_indent[total_width] = 0;
5949
5950 return wrap_indent;
5951 }
5952
5953 total_width += width + 1;
5954 }
5955
5956 return NULL;
5957 }
5958
5959 /* Determine if the locations of this breakpoint will have their conditions
5960 evaluated by the target, host or a mix of both. Returns the following:
5961
5962 "host": Host evals condition.
5963 "host or target": Host or Target evals condition.
5964 "target": Target evals condition.
5965 */
5966
5967 static const char *
5968 bp_condition_evaluator (struct breakpoint *b)
5969 {
5970 struct bp_location *bl;
5971 char host_evals = 0;
5972 char target_evals = 0;
5973
5974 if (!b)
5975 return NULL;
5976
5977 if (!is_breakpoint (b))
5978 return NULL;
5979
5980 if (gdb_evaluates_breakpoint_condition_p ()
5981 || !target_supports_evaluation_of_breakpoint_conditions ())
5982 return condition_evaluation_host;
5983
5984 for (bl = b->loc; bl; bl = bl->next)
5985 {
5986 if (bl->cond_bytecode)
5987 target_evals++;
5988 else
5989 host_evals++;
5990 }
5991
5992 if (host_evals && target_evals)
5993 return condition_evaluation_both;
5994 else if (target_evals)
5995 return condition_evaluation_target;
5996 else
5997 return condition_evaluation_host;
5998 }
5999
6000 /* Determine the breakpoint location's condition evaluator. This is
6001 similar to bp_condition_evaluator, but for locations. */
6002
6003 static const char *
6004 bp_location_condition_evaluator (struct bp_location *bl)
6005 {
6006 if (bl && !is_breakpoint (bl->owner))
6007 return NULL;
6008
6009 if (gdb_evaluates_breakpoint_condition_p ()
6010 || !target_supports_evaluation_of_breakpoint_conditions ())
6011 return condition_evaluation_host;
6012
6013 if (bl && bl->cond_bytecode)
6014 return condition_evaluation_target;
6015 else
6016 return condition_evaluation_host;
6017 }
6018
6019 /* Print the LOC location out of the list of B->LOC locations. */
6020
6021 static void
6022 print_breakpoint_location (struct breakpoint *b,
6023 struct bp_location *loc)
6024 {
6025 struct ui_out *uiout = current_uiout;
6026
6027 scoped_restore_current_program_space restore_pspace;
6028
6029 if (loc != NULL && loc->shlib_disabled)
6030 loc = NULL;
6031
6032 if (loc != NULL)
6033 set_current_program_space (loc->pspace);
6034
6035 if (b->display_canonical)
6036 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6037 else if (loc && loc->symtab)
6038 {
6039 struct symbol *sym
6040 = find_pc_sect_function (loc->address, loc->section);
6041 if (sym)
6042 {
6043 uiout->text ("in ");
6044 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6045 uiout->text (" ");
6046 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6047 uiout->text ("at ");
6048 }
6049 uiout->field_string ("file",
6050 symtab_to_filename_for_display (loc->symtab));
6051 uiout->text (":");
6052
6053 if (uiout->is_mi_like_p ())
6054 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6055
6056 uiout->field_int ("line", loc->line_number);
6057 }
6058 else if (loc)
6059 {
6060 string_file stb;
6061
6062 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6063 demangle, "");
6064 uiout->field_stream ("at", stb);
6065 }
6066 else
6067 {
6068 uiout->field_string ("pending",
6069 event_location_to_string (b->location.get ()));
6070 /* If extra_string is available, it could be holding a condition
6071 or dprintf arguments. In either case, make sure it is printed,
6072 too, but only for non-MI streams. */
6073 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6074 {
6075 if (b->type == bp_dprintf)
6076 uiout->text (",");
6077 else
6078 uiout->text (" ");
6079 uiout->text (b->extra_string);
6080 }
6081 }
6082
6083 if (loc && is_breakpoint (b)
6084 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6085 && bp_condition_evaluator (b) == condition_evaluation_both)
6086 {
6087 uiout->text (" (");
6088 uiout->field_string ("evaluated-by",
6089 bp_location_condition_evaluator (loc));
6090 uiout->text (")");
6091 }
6092 }
6093
6094 static const char *
6095 bptype_string (enum bptype type)
6096 {
6097 struct ep_type_description
6098 {
6099 enum bptype type;
6100 const char *description;
6101 };
6102 static struct ep_type_description bptypes[] =
6103 {
6104 {bp_none, "?deleted?"},
6105 {bp_breakpoint, "breakpoint"},
6106 {bp_hardware_breakpoint, "hw breakpoint"},
6107 {bp_single_step, "sw single-step"},
6108 {bp_until, "until"},
6109 {bp_finish, "finish"},
6110 {bp_watchpoint, "watchpoint"},
6111 {bp_hardware_watchpoint, "hw watchpoint"},
6112 {bp_read_watchpoint, "read watchpoint"},
6113 {bp_access_watchpoint, "acc watchpoint"},
6114 {bp_longjmp, "longjmp"},
6115 {bp_longjmp_resume, "longjmp resume"},
6116 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6117 {bp_exception, "exception"},
6118 {bp_exception_resume, "exception resume"},
6119 {bp_step_resume, "step resume"},
6120 {bp_hp_step_resume, "high-priority step resume"},
6121 {bp_watchpoint_scope, "watchpoint scope"},
6122 {bp_call_dummy, "call dummy"},
6123 {bp_std_terminate, "std::terminate"},
6124 {bp_shlib_event, "shlib events"},
6125 {bp_thread_event, "thread events"},
6126 {bp_overlay_event, "overlay events"},
6127 {bp_longjmp_master, "longjmp master"},
6128 {bp_std_terminate_master, "std::terminate master"},
6129 {bp_exception_master, "exception master"},
6130 {bp_catchpoint, "catchpoint"},
6131 {bp_tracepoint, "tracepoint"},
6132 {bp_fast_tracepoint, "fast tracepoint"},
6133 {bp_static_tracepoint, "static tracepoint"},
6134 {bp_dprintf, "dprintf"},
6135 {bp_jit_event, "jit events"},
6136 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6137 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6138 };
6139
6140 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6141 || ((int) type != bptypes[(int) type].type))
6142 internal_error (__FILE__, __LINE__,
6143 _("bptypes table does not describe type #%d."),
6144 (int) type);
6145
6146 return bptypes[(int) type].description;
6147 }
6148
6149 /* For MI, output a field named 'thread-groups' with a list as the value.
6150 For CLI, prefix the list with the string 'inf'. */
6151
6152 static void
6153 output_thread_groups (struct ui_out *uiout,
6154 const char *field_name,
6155 VEC(int) *inf_num,
6156 int mi_only)
6157 {
6158 int is_mi = uiout->is_mi_like_p ();
6159 int inf;
6160 int i;
6161
6162 /* For backward compatibility, don't display inferiors in CLI unless
6163 there are several. Always display them for MI. */
6164 if (!is_mi && mi_only)
6165 return;
6166
6167 ui_out_emit_list list_emitter (uiout, field_name);
6168
6169 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6170 {
6171 if (is_mi)
6172 {
6173 char mi_group[10];
6174
6175 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6176 uiout->field_string (NULL, mi_group);
6177 }
6178 else
6179 {
6180 if (i == 0)
6181 uiout->text (" inf ");
6182 else
6183 uiout->text (", ");
6184
6185 uiout->text (plongest (inf));
6186 }
6187 }
6188 }
6189
6190 /* Print B to gdb_stdout. */
6191
6192 static void
6193 print_one_breakpoint_location (struct breakpoint *b,
6194 struct bp_location *loc,
6195 int loc_number,
6196 struct bp_location **last_loc,
6197 int allflag)
6198 {
6199 struct command_line *l;
6200 static char bpenables[] = "nynny";
6201
6202 struct ui_out *uiout = current_uiout;
6203 int header_of_multiple = 0;
6204 int part_of_multiple = (loc != NULL);
6205 struct value_print_options opts;
6206
6207 get_user_print_options (&opts);
6208
6209 gdb_assert (!loc || loc_number != 0);
6210 /* See comment in print_one_breakpoint concerning treatment of
6211 breakpoints with single disabled location. */
6212 if (loc == NULL
6213 && (b->loc != NULL
6214 && (b->loc->next != NULL || !b->loc->enabled)))
6215 header_of_multiple = 1;
6216 if (loc == NULL)
6217 loc = b->loc;
6218
6219 annotate_record ();
6220
6221 /* 1 */
6222 annotate_field (0);
6223 if (part_of_multiple)
6224 {
6225 char *formatted;
6226 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6227 uiout->field_string ("number", formatted);
6228 xfree (formatted);
6229 }
6230 else
6231 {
6232 uiout->field_int ("number", b->number);
6233 }
6234
6235 /* 2 */
6236 annotate_field (1);
6237 if (part_of_multiple)
6238 uiout->field_skip ("type");
6239 else
6240 uiout->field_string ("type", bptype_string (b->type));
6241
6242 /* 3 */
6243 annotate_field (2);
6244 if (part_of_multiple)
6245 uiout->field_skip ("disp");
6246 else
6247 uiout->field_string ("disp", bpdisp_text (b->disposition));
6248
6249
6250 /* 4 */
6251 annotate_field (3);
6252 if (part_of_multiple)
6253 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6254 else
6255 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6256 uiout->spaces (2);
6257
6258
6259 /* 5 and 6 */
6260 if (b->ops != NULL && b->ops->print_one != NULL)
6261 {
6262 /* Although the print_one can possibly print all locations,
6263 calling it here is not likely to get any nice result. So,
6264 make sure there's just one location. */
6265 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6266 b->ops->print_one (b, last_loc);
6267 }
6268 else
6269 switch (b->type)
6270 {
6271 case bp_none:
6272 internal_error (__FILE__, __LINE__,
6273 _("print_one_breakpoint: bp_none encountered\n"));
6274 break;
6275
6276 case bp_watchpoint:
6277 case bp_hardware_watchpoint:
6278 case bp_read_watchpoint:
6279 case bp_access_watchpoint:
6280 {
6281 struct watchpoint *w = (struct watchpoint *) b;
6282
6283 /* Field 4, the address, is omitted (which makes the columns
6284 not line up too nicely with the headers, but the effect
6285 is relatively readable). */
6286 if (opts.addressprint)
6287 uiout->field_skip ("addr");
6288 annotate_field (5);
6289 uiout->field_string ("what", w->exp_string);
6290 }
6291 break;
6292
6293 case bp_breakpoint:
6294 case bp_hardware_breakpoint:
6295 case bp_single_step:
6296 case bp_until:
6297 case bp_finish:
6298 case bp_longjmp:
6299 case bp_longjmp_resume:
6300 case bp_longjmp_call_dummy:
6301 case bp_exception:
6302 case bp_exception_resume:
6303 case bp_step_resume:
6304 case bp_hp_step_resume:
6305 case bp_watchpoint_scope:
6306 case bp_call_dummy:
6307 case bp_std_terminate:
6308 case bp_shlib_event:
6309 case bp_thread_event:
6310 case bp_overlay_event:
6311 case bp_longjmp_master:
6312 case bp_std_terminate_master:
6313 case bp_exception_master:
6314 case bp_tracepoint:
6315 case bp_fast_tracepoint:
6316 case bp_static_tracepoint:
6317 case bp_dprintf:
6318 case bp_jit_event:
6319 case bp_gnu_ifunc_resolver:
6320 case bp_gnu_ifunc_resolver_return:
6321 if (opts.addressprint)
6322 {
6323 annotate_field (4);
6324 if (header_of_multiple)
6325 uiout->field_string ("addr", "<MULTIPLE>");
6326 else if (b->loc == NULL || loc->shlib_disabled)
6327 uiout->field_string ("addr", "<PENDING>");
6328 else
6329 uiout->field_core_addr ("addr",
6330 loc->gdbarch, loc->address);
6331 }
6332 annotate_field (5);
6333 if (!header_of_multiple)
6334 print_breakpoint_location (b, loc);
6335 if (b->loc)
6336 *last_loc = b->loc;
6337 break;
6338 }
6339
6340
6341 if (loc != NULL && !header_of_multiple)
6342 {
6343 struct inferior *inf;
6344 VEC(int) *inf_num = NULL;
6345 int mi_only = 1;
6346
6347 ALL_INFERIORS (inf)
6348 {
6349 if (inf->pspace == loc->pspace)
6350 VEC_safe_push (int, inf_num, inf->num);
6351 }
6352
6353 /* For backward compatibility, don't display inferiors in CLI unless
6354 there are several. Always display for MI. */
6355 if (allflag
6356 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6357 && (number_of_program_spaces () > 1
6358 || number_of_inferiors () > 1)
6359 /* LOC is for existing B, it cannot be in
6360 moribund_locations and thus having NULL OWNER. */
6361 && loc->owner->type != bp_catchpoint))
6362 mi_only = 0;
6363 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6364 VEC_free (int, inf_num);
6365 }
6366
6367 if (!part_of_multiple)
6368 {
6369 if (b->thread != -1)
6370 {
6371 /* FIXME: This seems to be redundant and lost here; see the
6372 "stop only in" line a little further down. */
6373 uiout->text (" thread ");
6374 uiout->field_int ("thread", b->thread);
6375 }
6376 else if (b->task != 0)
6377 {
6378 uiout->text (" task ");
6379 uiout->field_int ("task", b->task);
6380 }
6381 }
6382
6383 uiout->text ("\n");
6384
6385 if (!part_of_multiple)
6386 b->ops->print_one_detail (b, uiout);
6387
6388 if (part_of_multiple && frame_id_p (b->frame_id))
6389 {
6390 annotate_field (6);
6391 uiout->text ("\tstop only in stack frame at ");
6392 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6393 the frame ID. */
6394 uiout->field_core_addr ("frame",
6395 b->gdbarch, b->frame_id.stack_addr);
6396 uiout->text ("\n");
6397 }
6398
6399 if (!part_of_multiple && b->cond_string)
6400 {
6401 annotate_field (7);
6402 if (is_tracepoint (b))
6403 uiout->text ("\ttrace only if ");
6404 else
6405 uiout->text ("\tstop only if ");
6406 uiout->field_string ("cond", b->cond_string);
6407
6408 /* Print whether the target is doing the breakpoint's condition
6409 evaluation. If GDB is doing the evaluation, don't print anything. */
6410 if (is_breakpoint (b)
6411 && breakpoint_condition_evaluation_mode ()
6412 == condition_evaluation_target)
6413 {
6414 uiout->text (" (");
6415 uiout->field_string ("evaluated-by",
6416 bp_condition_evaluator (b));
6417 uiout->text (" evals)");
6418 }
6419 uiout->text ("\n");
6420 }
6421
6422 if (!part_of_multiple && b->thread != -1)
6423 {
6424 /* FIXME should make an annotation for this. */
6425 uiout->text ("\tstop only in thread ");
6426 if (uiout->is_mi_like_p ())
6427 uiout->field_int ("thread", b->thread);
6428 else
6429 {
6430 struct thread_info *thr = find_thread_global_id (b->thread);
6431
6432 uiout->field_string ("thread", print_thread_id (thr));
6433 }
6434 uiout->text ("\n");
6435 }
6436
6437 if (!part_of_multiple)
6438 {
6439 if (b->hit_count)
6440 {
6441 /* FIXME should make an annotation for this. */
6442 if (is_catchpoint (b))
6443 uiout->text ("\tcatchpoint");
6444 else if (is_tracepoint (b))
6445 uiout->text ("\ttracepoint");
6446 else
6447 uiout->text ("\tbreakpoint");
6448 uiout->text (" already hit ");
6449 uiout->field_int ("times", b->hit_count);
6450 if (b->hit_count == 1)
6451 uiout->text (" time\n");
6452 else
6453 uiout->text (" times\n");
6454 }
6455 else
6456 {
6457 /* Output the count also if it is zero, but only if this is mi. */
6458 if (uiout->is_mi_like_p ())
6459 uiout->field_int ("times", b->hit_count);
6460 }
6461 }
6462
6463 if (!part_of_multiple && b->ignore_count)
6464 {
6465 annotate_field (8);
6466 uiout->text ("\tignore next ");
6467 uiout->field_int ("ignore", b->ignore_count);
6468 uiout->text (" hits\n");
6469 }
6470
6471 /* Note that an enable count of 1 corresponds to "enable once"
6472 behavior, which is reported by the combination of enablement and
6473 disposition, so we don't need to mention it here. */
6474 if (!part_of_multiple && b->enable_count > 1)
6475 {
6476 annotate_field (8);
6477 uiout->text ("\tdisable after ");
6478 /* Tweak the wording to clarify that ignore and enable counts
6479 are distinct, and have additive effect. */
6480 if (b->ignore_count)
6481 uiout->text ("additional ");
6482 else
6483 uiout->text ("next ");
6484 uiout->field_int ("enable", b->enable_count);
6485 uiout->text (" hits\n");
6486 }
6487
6488 if (!part_of_multiple && is_tracepoint (b))
6489 {
6490 struct tracepoint *tp = (struct tracepoint *) b;
6491
6492 if (tp->traceframe_usage)
6493 {
6494 uiout->text ("\ttrace buffer usage ");
6495 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6496 uiout->text (" bytes\n");
6497 }
6498 }
6499
6500 l = b->commands ? b->commands.get () : NULL;
6501 if (!part_of_multiple && l)
6502 {
6503 annotate_field (9);
6504 ui_out_emit_tuple tuple_emitter (uiout, "script");
6505 print_command_lines (uiout, l, 4);
6506 }
6507
6508 if (is_tracepoint (b))
6509 {
6510 struct tracepoint *t = (struct tracepoint *) b;
6511
6512 if (!part_of_multiple && t->pass_count)
6513 {
6514 annotate_field (10);
6515 uiout->text ("\tpass count ");
6516 uiout->field_int ("pass", t->pass_count);
6517 uiout->text (" \n");
6518 }
6519
6520 /* Don't display it when tracepoint or tracepoint location is
6521 pending. */
6522 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6523 {
6524 annotate_field (11);
6525
6526 if (uiout->is_mi_like_p ())
6527 uiout->field_string ("installed",
6528 loc->inserted ? "y" : "n");
6529 else
6530 {
6531 if (loc->inserted)
6532 uiout->text ("\t");
6533 else
6534 uiout->text ("\tnot ");
6535 uiout->text ("installed on target\n");
6536 }
6537 }
6538 }
6539
6540 if (uiout->is_mi_like_p () && !part_of_multiple)
6541 {
6542 if (is_watchpoint (b))
6543 {
6544 struct watchpoint *w = (struct watchpoint *) b;
6545
6546 uiout->field_string ("original-location", w->exp_string);
6547 }
6548 else if (b->location != NULL
6549 && event_location_to_string (b->location.get ()) != NULL)
6550 uiout->field_string ("original-location",
6551 event_location_to_string (b->location.get ()));
6552 }
6553 }
6554
6555 static void
6556 print_one_breakpoint (struct breakpoint *b,
6557 struct bp_location **last_loc,
6558 int allflag)
6559 {
6560 struct ui_out *uiout = current_uiout;
6561
6562 {
6563 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6564
6565 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6566 }
6567
6568 /* If this breakpoint has custom print function,
6569 it's already printed. Otherwise, print individual
6570 locations, if any. */
6571 if (b->ops == NULL || b->ops->print_one == NULL)
6572 {
6573 /* If breakpoint has a single location that is disabled, we
6574 print it as if it had several locations, since otherwise it's
6575 hard to represent "breakpoint enabled, location disabled"
6576 situation.
6577
6578 Note that while hardware watchpoints have several locations
6579 internally, that's not a property exposed to user. */
6580 if (b->loc
6581 && !is_hardware_watchpoint (b)
6582 && (b->loc->next || !b->loc->enabled))
6583 {
6584 struct bp_location *loc;
6585 int n = 1;
6586
6587 for (loc = b->loc; loc; loc = loc->next, ++n)
6588 {
6589 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6590 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6591 }
6592 }
6593 }
6594 }
6595
6596 static int
6597 breakpoint_address_bits (struct breakpoint *b)
6598 {
6599 int print_address_bits = 0;
6600 struct bp_location *loc;
6601
6602 /* Software watchpoints that aren't watching memory don't have an
6603 address to print. */
6604 if (is_no_memory_software_watchpoint (b))
6605 return 0;
6606
6607 for (loc = b->loc; loc; loc = loc->next)
6608 {
6609 int addr_bit;
6610
6611 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6612 if (addr_bit > print_address_bits)
6613 print_address_bits = addr_bit;
6614 }
6615
6616 return print_address_bits;
6617 }
6618
6619 struct captured_breakpoint_query_args
6620 {
6621 int bnum;
6622 };
6623
6624 static int
6625 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6626 {
6627 struct captured_breakpoint_query_args *args
6628 = (struct captured_breakpoint_query_args *) data;
6629 struct breakpoint *b;
6630 struct bp_location *dummy_loc = NULL;
6631
6632 ALL_BREAKPOINTS (b)
6633 {
6634 if (args->bnum == b->number)
6635 {
6636 print_one_breakpoint (b, &dummy_loc, 0);
6637 return GDB_RC_OK;
6638 }
6639 }
6640 return GDB_RC_NONE;
6641 }
6642
6643 enum gdb_rc
6644 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6645 char **error_message)
6646 {
6647 struct captured_breakpoint_query_args args;
6648
6649 args.bnum = bnum;
6650 /* For the moment we don't trust print_one_breakpoint() to not throw
6651 an error. */
6652 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6653 error_message, RETURN_MASK_ALL) < 0)
6654 return GDB_RC_FAIL;
6655 else
6656 return GDB_RC_OK;
6657 }
6658
6659 /* Return true if this breakpoint was set by the user, false if it is
6660 internal or momentary. */
6661
6662 int
6663 user_breakpoint_p (struct breakpoint *b)
6664 {
6665 return b->number > 0;
6666 }
6667
6668 /* See breakpoint.h. */
6669
6670 int
6671 pending_breakpoint_p (struct breakpoint *b)
6672 {
6673 return b->loc == NULL;
6674 }
6675
6676 /* Print information on user settable breakpoint (watchpoint, etc)
6677 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6678 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6679 FILTER is non-NULL, call it on each breakpoint and only include the
6680 ones for which it returns non-zero. Return the total number of
6681 breakpoints listed. */
6682
6683 static int
6684 breakpoint_1 (char *args, int allflag,
6685 int (*filter) (const struct breakpoint *))
6686 {
6687 struct breakpoint *b;
6688 struct bp_location *last_loc = NULL;
6689 int nr_printable_breakpoints;
6690 struct value_print_options opts;
6691 int print_address_bits = 0;
6692 int print_type_col_width = 14;
6693 struct ui_out *uiout = current_uiout;
6694
6695 get_user_print_options (&opts);
6696
6697 /* Compute the number of rows in the table, as well as the size
6698 required for address fields. */
6699 nr_printable_breakpoints = 0;
6700 ALL_BREAKPOINTS (b)
6701 {
6702 /* If we have a filter, only list the breakpoints it accepts. */
6703 if (filter && !filter (b))
6704 continue;
6705
6706 /* If we have an "args" string, it is a list of breakpoints to
6707 accept. Skip the others. */
6708 if (args != NULL && *args != '\0')
6709 {
6710 if (allflag && parse_and_eval_long (args) != b->number)
6711 continue;
6712 if (!allflag && !number_is_in_list (args, b->number))
6713 continue;
6714 }
6715
6716 if (allflag || user_breakpoint_p (b))
6717 {
6718 int addr_bit, type_len;
6719
6720 addr_bit = breakpoint_address_bits (b);
6721 if (addr_bit > print_address_bits)
6722 print_address_bits = addr_bit;
6723
6724 type_len = strlen (bptype_string (b->type));
6725 if (type_len > print_type_col_width)
6726 print_type_col_width = type_len;
6727
6728 nr_printable_breakpoints++;
6729 }
6730 }
6731
6732 {
6733 ui_out_emit_table table_emitter (uiout,
6734 opts.addressprint ? 6 : 5,
6735 nr_printable_breakpoints,
6736 "BreakpointTable");
6737
6738 if (nr_printable_breakpoints > 0)
6739 annotate_breakpoints_headers ();
6740 if (nr_printable_breakpoints > 0)
6741 annotate_field (0);
6742 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6743 if (nr_printable_breakpoints > 0)
6744 annotate_field (1);
6745 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6746 if (nr_printable_breakpoints > 0)
6747 annotate_field (2);
6748 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6749 if (nr_printable_breakpoints > 0)
6750 annotate_field (3);
6751 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6752 if (opts.addressprint)
6753 {
6754 if (nr_printable_breakpoints > 0)
6755 annotate_field (4);
6756 if (print_address_bits <= 32)
6757 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6758 else
6759 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6760 }
6761 if (nr_printable_breakpoints > 0)
6762 annotate_field (5);
6763 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6764 uiout->table_body ();
6765 if (nr_printable_breakpoints > 0)
6766 annotate_breakpoints_table ();
6767
6768 ALL_BREAKPOINTS (b)
6769 {
6770 QUIT;
6771 /* If we have a filter, only list the breakpoints it accepts. */
6772 if (filter && !filter (b))
6773 continue;
6774
6775 /* If we have an "args" string, it is a list of breakpoints to
6776 accept. Skip the others. */
6777
6778 if (args != NULL && *args != '\0')
6779 {
6780 if (allflag) /* maintenance info breakpoint */
6781 {
6782 if (parse_and_eval_long (args) != b->number)
6783 continue;
6784 }
6785 else /* all others */
6786 {
6787 if (!number_is_in_list (args, b->number))
6788 continue;
6789 }
6790 }
6791 /* We only print out user settable breakpoints unless the
6792 allflag is set. */
6793 if (allflag || user_breakpoint_p (b))
6794 print_one_breakpoint (b, &last_loc, allflag);
6795 }
6796 }
6797
6798 if (nr_printable_breakpoints == 0)
6799 {
6800 /* If there's a filter, let the caller decide how to report
6801 empty list. */
6802 if (!filter)
6803 {
6804 if (args == NULL || *args == '\0')
6805 uiout->message ("No breakpoints or watchpoints.\n");
6806 else
6807 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6808 args);
6809 }
6810 }
6811 else
6812 {
6813 if (last_loc && !server_command)
6814 set_next_address (last_loc->gdbarch, last_loc->address);
6815 }
6816
6817 /* FIXME? Should this be moved up so that it is only called when
6818 there have been breakpoints? */
6819 annotate_breakpoints_table_end ();
6820
6821 return nr_printable_breakpoints;
6822 }
6823
6824 /* Display the value of default-collect in a way that is generally
6825 compatible with the breakpoint list. */
6826
6827 static void
6828 default_collect_info (void)
6829 {
6830 struct ui_out *uiout = current_uiout;
6831
6832 /* If it has no value (which is frequently the case), say nothing; a
6833 message like "No default-collect." gets in user's face when it's
6834 not wanted. */
6835 if (!*default_collect)
6836 return;
6837
6838 /* The following phrase lines up nicely with per-tracepoint collect
6839 actions. */
6840 uiout->text ("default collect ");
6841 uiout->field_string ("default-collect", default_collect);
6842 uiout->text (" \n");
6843 }
6844
6845 static void
6846 info_breakpoints_command (char *args, int from_tty)
6847 {
6848 breakpoint_1 (args, 0, NULL);
6849
6850 default_collect_info ();
6851 }
6852
6853 static void
6854 info_watchpoints_command (char *args, int from_tty)
6855 {
6856 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6857 struct ui_out *uiout = current_uiout;
6858
6859 if (num_printed == 0)
6860 {
6861 if (args == NULL || *args == '\0')
6862 uiout->message ("No watchpoints.\n");
6863 else
6864 uiout->message ("No watchpoint matching '%s'.\n", args);
6865 }
6866 }
6867
6868 static void
6869 maintenance_info_breakpoints (char *args, int from_tty)
6870 {
6871 breakpoint_1 (args, 1, NULL);
6872
6873 default_collect_info ();
6874 }
6875
6876 static int
6877 breakpoint_has_pc (struct breakpoint *b,
6878 struct program_space *pspace,
6879 CORE_ADDR pc, struct obj_section *section)
6880 {
6881 struct bp_location *bl = b->loc;
6882
6883 for (; bl; bl = bl->next)
6884 {
6885 if (bl->pspace == pspace
6886 && bl->address == pc
6887 && (!overlay_debugging || bl->section == section))
6888 return 1;
6889 }
6890 return 0;
6891 }
6892
6893 /* Print a message describing any user-breakpoints set at PC. This
6894 concerns with logical breakpoints, so we match program spaces, not
6895 address spaces. */
6896
6897 static void
6898 describe_other_breakpoints (struct gdbarch *gdbarch,
6899 struct program_space *pspace, CORE_ADDR pc,
6900 struct obj_section *section, int thread)
6901 {
6902 int others = 0;
6903 struct breakpoint *b;
6904
6905 ALL_BREAKPOINTS (b)
6906 others += (user_breakpoint_p (b)
6907 && breakpoint_has_pc (b, pspace, pc, section));
6908 if (others > 0)
6909 {
6910 if (others == 1)
6911 printf_filtered (_("Note: breakpoint "));
6912 else /* if (others == ???) */
6913 printf_filtered (_("Note: breakpoints "));
6914 ALL_BREAKPOINTS (b)
6915 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6916 {
6917 others--;
6918 printf_filtered ("%d", b->number);
6919 if (b->thread == -1 && thread != -1)
6920 printf_filtered (" (all threads)");
6921 else if (b->thread != -1)
6922 printf_filtered (" (thread %d)", b->thread);
6923 printf_filtered ("%s%s ",
6924 ((b->enable_state == bp_disabled
6925 || b->enable_state == bp_call_disabled)
6926 ? " (disabled)"
6927 : ""),
6928 (others > 1) ? ","
6929 : ((others == 1) ? " and" : ""));
6930 }
6931 printf_filtered (_("also set at pc "));
6932 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6933 printf_filtered (".\n");
6934 }
6935 }
6936 \f
6937
6938 /* Return true iff it is meaningful to use the address member of
6939 BPT locations. For some breakpoint types, the locations' address members
6940 are irrelevant and it makes no sense to attempt to compare them to other
6941 addresses (or use them for any other purpose either).
6942
6943 More specifically, each of the following breakpoint types will
6944 always have a zero valued location address and we don't want to mark
6945 breakpoints of any of these types to be a duplicate of an actual
6946 breakpoint location at address zero:
6947
6948 bp_watchpoint
6949 bp_catchpoint
6950
6951 */
6952
6953 static int
6954 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6955 {
6956 enum bptype type = bpt->type;
6957
6958 return (type != bp_watchpoint && type != bp_catchpoint);
6959 }
6960
6961 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6962 true if LOC1 and LOC2 represent the same watchpoint location. */
6963
6964 static int
6965 watchpoint_locations_match (struct bp_location *loc1,
6966 struct bp_location *loc2)
6967 {
6968 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6969 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6970
6971 /* Both of them must exist. */
6972 gdb_assert (w1 != NULL);
6973 gdb_assert (w2 != NULL);
6974
6975 /* If the target can evaluate the condition expression in hardware,
6976 then we we need to insert both watchpoints even if they are at
6977 the same place. Otherwise the watchpoint will only trigger when
6978 the condition of whichever watchpoint was inserted evaluates to
6979 true, not giving a chance for GDB to check the condition of the
6980 other watchpoint. */
6981 if ((w1->cond_exp
6982 && target_can_accel_watchpoint_condition (loc1->address,
6983 loc1->length,
6984 loc1->watchpoint_type,
6985 w1->cond_exp.get ()))
6986 || (w2->cond_exp
6987 && target_can_accel_watchpoint_condition (loc2->address,
6988 loc2->length,
6989 loc2->watchpoint_type,
6990 w2->cond_exp.get ())))
6991 return 0;
6992
6993 /* Note that this checks the owner's type, not the location's. In
6994 case the target does not support read watchpoints, but does
6995 support access watchpoints, we'll have bp_read_watchpoint
6996 watchpoints with hw_access locations. Those should be considered
6997 duplicates of hw_read locations. The hw_read locations will
6998 become hw_access locations later. */
6999 return (loc1->owner->type == loc2->owner->type
7000 && loc1->pspace->aspace == loc2->pspace->aspace
7001 && loc1->address == loc2->address
7002 && loc1->length == loc2->length);
7003 }
7004
7005 /* See breakpoint.h. */
7006
7007 int
7008 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7009 struct address_space *aspace2, CORE_ADDR addr2)
7010 {
7011 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7012 || aspace1 == aspace2)
7013 && addr1 == addr2);
7014 }
7015
7016 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7017 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7018 matches ASPACE2. On targets that have global breakpoints, the address
7019 space doesn't really matter. */
7020
7021 static int
7022 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7023 int len1, struct address_space *aspace2,
7024 CORE_ADDR addr2)
7025 {
7026 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7027 || aspace1 == aspace2)
7028 && addr2 >= addr1 && addr2 < addr1 + len1);
7029 }
7030
7031 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7032 a ranged breakpoint. In most targets, a match happens only if ASPACE
7033 matches the breakpoint's address space. On targets that have global
7034 breakpoints, the address space doesn't really matter. */
7035
7036 static int
7037 breakpoint_location_address_match (struct bp_location *bl,
7038 struct address_space *aspace,
7039 CORE_ADDR addr)
7040 {
7041 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7042 aspace, addr)
7043 || (bl->length
7044 && breakpoint_address_match_range (bl->pspace->aspace,
7045 bl->address, bl->length,
7046 aspace, addr)));
7047 }
7048
7049 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7050 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7051 match happens only if ASPACE matches the breakpoint's address
7052 space. On targets that have global breakpoints, the address space
7053 doesn't really matter. */
7054
7055 static int
7056 breakpoint_location_address_range_overlap (struct bp_location *bl,
7057 struct address_space *aspace,
7058 CORE_ADDR addr, int len)
7059 {
7060 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7061 || bl->pspace->aspace == aspace)
7062 {
7063 int bl_len = bl->length != 0 ? bl->length : 1;
7064
7065 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7066 return 1;
7067 }
7068 return 0;
7069 }
7070
7071 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7072 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7073 true, otherwise returns false. */
7074
7075 static int
7076 tracepoint_locations_match (struct bp_location *loc1,
7077 struct bp_location *loc2)
7078 {
7079 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7080 /* Since tracepoint locations are never duplicated with others', tracepoint
7081 locations at the same address of different tracepoints are regarded as
7082 different locations. */
7083 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7084 else
7085 return 0;
7086 }
7087
7088 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7089 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7090 represent the same location. */
7091
7092 static int
7093 breakpoint_locations_match (struct bp_location *loc1,
7094 struct bp_location *loc2)
7095 {
7096 int hw_point1, hw_point2;
7097
7098 /* Both of them must not be in moribund_locations. */
7099 gdb_assert (loc1->owner != NULL);
7100 gdb_assert (loc2->owner != NULL);
7101
7102 hw_point1 = is_hardware_watchpoint (loc1->owner);
7103 hw_point2 = is_hardware_watchpoint (loc2->owner);
7104
7105 if (hw_point1 != hw_point2)
7106 return 0;
7107 else if (hw_point1)
7108 return watchpoint_locations_match (loc1, loc2);
7109 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7110 return tracepoint_locations_match (loc1, loc2);
7111 else
7112 /* We compare bp_location.length in order to cover ranged breakpoints. */
7113 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7114 loc2->pspace->aspace, loc2->address)
7115 && loc1->length == loc2->length);
7116 }
7117
7118 static void
7119 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7120 int bnum, int have_bnum)
7121 {
7122 /* The longest string possibly returned by hex_string_custom
7123 is 50 chars. These must be at least that big for safety. */
7124 char astr1[64];
7125 char astr2[64];
7126
7127 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7128 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7129 if (have_bnum)
7130 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7131 bnum, astr1, astr2);
7132 else
7133 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7134 }
7135
7136 /* Adjust a breakpoint's address to account for architectural
7137 constraints on breakpoint placement. Return the adjusted address.
7138 Note: Very few targets require this kind of adjustment. For most
7139 targets, this function is simply the identity function. */
7140
7141 static CORE_ADDR
7142 adjust_breakpoint_address (struct gdbarch *gdbarch,
7143 CORE_ADDR bpaddr, enum bptype bptype)
7144 {
7145 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7146 {
7147 /* Very few targets need any kind of breakpoint adjustment. */
7148 return bpaddr;
7149 }
7150 else if (bptype == bp_watchpoint
7151 || bptype == bp_hardware_watchpoint
7152 || bptype == bp_read_watchpoint
7153 || bptype == bp_access_watchpoint
7154 || bptype == bp_catchpoint)
7155 {
7156 /* Watchpoints and the various bp_catch_* eventpoints should not
7157 have their addresses modified. */
7158 return bpaddr;
7159 }
7160 else if (bptype == bp_single_step)
7161 {
7162 /* Single-step breakpoints should not have their addresses
7163 modified. If there's any architectural constrain that
7164 applies to this address, then it should have already been
7165 taken into account when the breakpoint was created in the
7166 first place. If we didn't do this, stepping through e.g.,
7167 Thumb-2 IT blocks would break. */
7168 return bpaddr;
7169 }
7170 else
7171 {
7172 CORE_ADDR adjusted_bpaddr;
7173
7174 /* Some targets have architectural constraints on the placement
7175 of breakpoint instructions. Obtain the adjusted address. */
7176 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7177
7178 /* An adjusted breakpoint address can significantly alter
7179 a user's expectations. Print a warning if an adjustment
7180 is required. */
7181 if (adjusted_bpaddr != bpaddr)
7182 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7183
7184 return adjusted_bpaddr;
7185 }
7186 }
7187
7188 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7189 {
7190 bp_location *loc = this;
7191
7192 gdb_assert (ops != NULL);
7193
7194 loc->ops = ops;
7195 loc->owner = owner;
7196 loc->cond_bytecode = NULL;
7197 loc->shlib_disabled = 0;
7198 loc->enabled = 1;
7199
7200 switch (owner->type)
7201 {
7202 case bp_breakpoint:
7203 case bp_single_step:
7204 case bp_until:
7205 case bp_finish:
7206 case bp_longjmp:
7207 case bp_longjmp_resume:
7208 case bp_longjmp_call_dummy:
7209 case bp_exception:
7210 case bp_exception_resume:
7211 case bp_step_resume:
7212 case bp_hp_step_resume:
7213 case bp_watchpoint_scope:
7214 case bp_call_dummy:
7215 case bp_std_terminate:
7216 case bp_shlib_event:
7217 case bp_thread_event:
7218 case bp_overlay_event:
7219 case bp_jit_event:
7220 case bp_longjmp_master:
7221 case bp_std_terminate_master:
7222 case bp_exception_master:
7223 case bp_gnu_ifunc_resolver:
7224 case bp_gnu_ifunc_resolver_return:
7225 case bp_dprintf:
7226 loc->loc_type = bp_loc_software_breakpoint;
7227 mark_breakpoint_location_modified (loc);
7228 break;
7229 case bp_hardware_breakpoint:
7230 loc->loc_type = bp_loc_hardware_breakpoint;
7231 mark_breakpoint_location_modified (loc);
7232 break;
7233 case bp_hardware_watchpoint:
7234 case bp_read_watchpoint:
7235 case bp_access_watchpoint:
7236 loc->loc_type = bp_loc_hardware_watchpoint;
7237 break;
7238 case bp_watchpoint:
7239 case bp_catchpoint:
7240 case bp_tracepoint:
7241 case bp_fast_tracepoint:
7242 case bp_static_tracepoint:
7243 loc->loc_type = bp_loc_other;
7244 break;
7245 default:
7246 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7247 }
7248
7249 loc->refc = 1;
7250 }
7251
7252 /* Allocate a struct bp_location. */
7253
7254 static struct bp_location *
7255 allocate_bp_location (struct breakpoint *bpt)
7256 {
7257 return bpt->ops->allocate_location (bpt);
7258 }
7259
7260 static void
7261 free_bp_location (struct bp_location *loc)
7262 {
7263 loc->ops->dtor (loc);
7264 delete loc;
7265 }
7266
7267 /* Increment reference count. */
7268
7269 static void
7270 incref_bp_location (struct bp_location *bl)
7271 {
7272 ++bl->refc;
7273 }
7274
7275 /* Decrement reference count. If the reference count reaches 0,
7276 destroy the bp_location. Sets *BLP to NULL. */
7277
7278 static void
7279 decref_bp_location (struct bp_location **blp)
7280 {
7281 gdb_assert ((*blp)->refc > 0);
7282
7283 if (--(*blp)->refc == 0)
7284 free_bp_location (*blp);
7285 *blp = NULL;
7286 }
7287
7288 /* Add breakpoint B at the end of the global breakpoint chain. */
7289
7290 static breakpoint *
7291 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7292 {
7293 struct breakpoint *b1;
7294 struct breakpoint *result = b.get ();
7295
7296 /* Add this breakpoint to the end of the chain so that a list of
7297 breakpoints will come out in order of increasing numbers. */
7298
7299 b1 = breakpoint_chain;
7300 if (b1 == 0)
7301 breakpoint_chain = b.release ();
7302 else
7303 {
7304 while (b1->next)
7305 b1 = b1->next;
7306 b1->next = b.release ();
7307 }
7308
7309 return result;
7310 }
7311
7312 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7313
7314 static void
7315 init_raw_breakpoint_without_location (struct breakpoint *b,
7316 struct gdbarch *gdbarch,
7317 enum bptype bptype,
7318 const struct breakpoint_ops *ops)
7319 {
7320 gdb_assert (ops != NULL);
7321
7322 b->ops = ops;
7323 b->type = bptype;
7324 b->gdbarch = gdbarch;
7325 b->language = current_language->la_language;
7326 b->input_radix = input_radix;
7327 b->related_breakpoint = b;
7328 }
7329
7330 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7331 that has type BPTYPE and has no locations as yet. */
7332
7333 static struct breakpoint *
7334 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7335 enum bptype bptype,
7336 const struct breakpoint_ops *ops)
7337 {
7338 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7339
7340 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7341 return add_to_breakpoint_chain (std::move (b));
7342 }
7343
7344 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7345 resolutions should be made as the user specified the location explicitly
7346 enough. */
7347
7348 static void
7349 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7350 {
7351 gdb_assert (loc->owner != NULL);
7352
7353 if (loc->owner->type == bp_breakpoint
7354 || loc->owner->type == bp_hardware_breakpoint
7355 || is_tracepoint (loc->owner))
7356 {
7357 int is_gnu_ifunc;
7358 const char *function_name;
7359 CORE_ADDR func_addr;
7360
7361 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7362 &func_addr, NULL, &is_gnu_ifunc);
7363
7364 if (is_gnu_ifunc && !explicit_loc)
7365 {
7366 struct breakpoint *b = loc->owner;
7367
7368 gdb_assert (loc->pspace == current_program_space);
7369 if (gnu_ifunc_resolve_name (function_name,
7370 &loc->requested_address))
7371 {
7372 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7373 loc->address = adjust_breakpoint_address (loc->gdbarch,
7374 loc->requested_address,
7375 b->type);
7376 }
7377 else if (b->type == bp_breakpoint && b->loc == loc
7378 && loc->next == NULL && b->related_breakpoint == b)
7379 {
7380 /* Create only the whole new breakpoint of this type but do not
7381 mess more complicated breakpoints with multiple locations. */
7382 b->type = bp_gnu_ifunc_resolver;
7383 /* Remember the resolver's address for use by the return
7384 breakpoint. */
7385 loc->related_address = func_addr;
7386 }
7387 }
7388
7389 if (function_name)
7390 loc->function_name = xstrdup (function_name);
7391 }
7392 }
7393
7394 /* Attempt to determine architecture of location identified by SAL. */
7395 struct gdbarch *
7396 get_sal_arch (struct symtab_and_line sal)
7397 {
7398 if (sal.section)
7399 return get_objfile_arch (sal.section->objfile);
7400 if (sal.symtab)
7401 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7402
7403 return NULL;
7404 }
7405
7406 /* Low level routine for partially initializing a breakpoint of type
7407 BPTYPE. The newly created breakpoint's address, section, source
7408 file name, and line number are provided by SAL.
7409
7410 It is expected that the caller will complete the initialization of
7411 the newly created breakpoint struct as well as output any status
7412 information regarding the creation of a new breakpoint. */
7413
7414 static void
7415 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7416 struct symtab_and_line sal, enum bptype bptype,
7417 const struct breakpoint_ops *ops)
7418 {
7419 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7420
7421 add_location_to_breakpoint (b, &sal);
7422
7423 if (bptype != bp_catchpoint)
7424 gdb_assert (sal.pspace != NULL);
7425
7426 /* Store the program space that was used to set the breakpoint,
7427 except for ordinary breakpoints, which are independent of the
7428 program space. */
7429 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7430 b->pspace = sal.pspace;
7431 }
7432
7433 /* set_raw_breakpoint is a low level routine for allocating and
7434 partially initializing a breakpoint of type BPTYPE. The newly
7435 created breakpoint's address, section, source file name, and line
7436 number are provided by SAL. The newly created and partially
7437 initialized breakpoint is added to the breakpoint chain and
7438 is also returned as the value of this function.
7439
7440 It is expected that the caller will complete the initialization of
7441 the newly created breakpoint struct as well as output any status
7442 information regarding the creation of a new breakpoint. In
7443 particular, set_raw_breakpoint does NOT set the breakpoint
7444 number! Care should be taken to not allow an error to occur
7445 prior to completing the initialization of the breakpoint. If this
7446 should happen, a bogus breakpoint will be left on the chain. */
7447
7448 struct breakpoint *
7449 set_raw_breakpoint (struct gdbarch *gdbarch,
7450 struct symtab_and_line sal, enum bptype bptype,
7451 const struct breakpoint_ops *ops)
7452 {
7453 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7454
7455 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7456 return add_to_breakpoint_chain (std::move (b));
7457 }
7458
7459 /* Call this routine when stepping and nexting to enable a breakpoint
7460 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7461 initiated the operation. */
7462
7463 void
7464 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7465 {
7466 struct breakpoint *b, *b_tmp;
7467 int thread = tp->global_num;
7468
7469 /* To avoid having to rescan all objfile symbols at every step,
7470 we maintain a list of continually-inserted but always disabled
7471 longjmp "master" breakpoints. Here, we simply create momentary
7472 clones of those and enable them for the requested thread. */
7473 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7474 if (b->pspace == current_program_space
7475 && (b->type == bp_longjmp_master
7476 || b->type == bp_exception_master))
7477 {
7478 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7479 struct breakpoint *clone;
7480
7481 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7482 after their removal. */
7483 clone = momentary_breakpoint_from_master (b, type,
7484 &momentary_breakpoint_ops, 1);
7485 clone->thread = thread;
7486 }
7487
7488 tp->initiating_frame = frame;
7489 }
7490
7491 /* Delete all longjmp breakpoints from THREAD. */
7492 void
7493 delete_longjmp_breakpoint (int thread)
7494 {
7495 struct breakpoint *b, *b_tmp;
7496
7497 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7498 if (b->type == bp_longjmp || b->type == bp_exception)
7499 {
7500 if (b->thread == thread)
7501 delete_breakpoint (b);
7502 }
7503 }
7504
7505 void
7506 delete_longjmp_breakpoint_at_next_stop (int thread)
7507 {
7508 struct breakpoint *b, *b_tmp;
7509
7510 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7511 if (b->type == bp_longjmp || b->type == bp_exception)
7512 {
7513 if (b->thread == thread)
7514 b->disposition = disp_del_at_next_stop;
7515 }
7516 }
7517
7518 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7519 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7520 pointer to any of them. Return NULL if this system cannot place longjmp
7521 breakpoints. */
7522
7523 struct breakpoint *
7524 set_longjmp_breakpoint_for_call_dummy (void)
7525 {
7526 struct breakpoint *b, *retval = NULL;
7527
7528 ALL_BREAKPOINTS (b)
7529 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7530 {
7531 struct breakpoint *new_b;
7532
7533 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7534 &momentary_breakpoint_ops,
7535 1);
7536 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7537
7538 /* Link NEW_B into the chain of RETVAL breakpoints. */
7539
7540 gdb_assert (new_b->related_breakpoint == new_b);
7541 if (retval == NULL)
7542 retval = new_b;
7543 new_b->related_breakpoint = retval;
7544 while (retval->related_breakpoint != new_b->related_breakpoint)
7545 retval = retval->related_breakpoint;
7546 retval->related_breakpoint = new_b;
7547 }
7548
7549 return retval;
7550 }
7551
7552 /* Verify all existing dummy frames and their associated breakpoints for
7553 TP. Remove those which can no longer be found in the current frame
7554 stack.
7555
7556 You should call this function only at places where it is safe to currently
7557 unwind the whole stack. Failed stack unwind would discard live dummy
7558 frames. */
7559
7560 void
7561 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7562 {
7563 struct breakpoint *b, *b_tmp;
7564
7565 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7566 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7567 {
7568 struct breakpoint *dummy_b = b->related_breakpoint;
7569
7570 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7571 dummy_b = dummy_b->related_breakpoint;
7572 if (dummy_b->type != bp_call_dummy
7573 || frame_find_by_id (dummy_b->frame_id) != NULL)
7574 continue;
7575
7576 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7577
7578 while (b->related_breakpoint != b)
7579 {
7580 if (b_tmp == b->related_breakpoint)
7581 b_tmp = b->related_breakpoint->next;
7582 delete_breakpoint (b->related_breakpoint);
7583 }
7584 delete_breakpoint (b);
7585 }
7586 }
7587
7588 void
7589 enable_overlay_breakpoints (void)
7590 {
7591 struct breakpoint *b;
7592
7593 ALL_BREAKPOINTS (b)
7594 if (b->type == bp_overlay_event)
7595 {
7596 b->enable_state = bp_enabled;
7597 update_global_location_list (UGLL_MAY_INSERT);
7598 overlay_events_enabled = 1;
7599 }
7600 }
7601
7602 void
7603 disable_overlay_breakpoints (void)
7604 {
7605 struct breakpoint *b;
7606
7607 ALL_BREAKPOINTS (b)
7608 if (b->type == bp_overlay_event)
7609 {
7610 b->enable_state = bp_disabled;
7611 update_global_location_list (UGLL_DONT_INSERT);
7612 overlay_events_enabled = 0;
7613 }
7614 }
7615
7616 /* Set an active std::terminate breakpoint for each std::terminate
7617 master breakpoint. */
7618 void
7619 set_std_terminate_breakpoint (void)
7620 {
7621 struct breakpoint *b, *b_tmp;
7622
7623 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7624 if (b->pspace == current_program_space
7625 && b->type == bp_std_terminate_master)
7626 {
7627 momentary_breakpoint_from_master (b, bp_std_terminate,
7628 &momentary_breakpoint_ops, 1);
7629 }
7630 }
7631
7632 /* Delete all the std::terminate breakpoints. */
7633 void
7634 delete_std_terminate_breakpoint (void)
7635 {
7636 struct breakpoint *b, *b_tmp;
7637
7638 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7639 if (b->type == bp_std_terminate)
7640 delete_breakpoint (b);
7641 }
7642
7643 struct breakpoint *
7644 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7645 {
7646 struct breakpoint *b;
7647
7648 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7649 &internal_breakpoint_ops);
7650
7651 b->enable_state = bp_enabled;
7652 /* location has to be used or breakpoint_re_set will delete me. */
7653 b->location = new_address_location (b->loc->address, NULL, 0);
7654
7655 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7656
7657 return b;
7658 }
7659
7660 struct lang_and_radix
7661 {
7662 enum language lang;
7663 int radix;
7664 };
7665
7666 /* Create a breakpoint for JIT code registration and unregistration. */
7667
7668 struct breakpoint *
7669 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7670 {
7671 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7672 &internal_breakpoint_ops);
7673 }
7674
7675 /* Remove JIT code registration and unregistration breakpoint(s). */
7676
7677 void
7678 remove_jit_event_breakpoints (void)
7679 {
7680 struct breakpoint *b, *b_tmp;
7681
7682 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7683 if (b->type == bp_jit_event
7684 && b->loc->pspace == current_program_space)
7685 delete_breakpoint (b);
7686 }
7687
7688 void
7689 remove_solib_event_breakpoints (void)
7690 {
7691 struct breakpoint *b, *b_tmp;
7692
7693 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7694 if (b->type == bp_shlib_event
7695 && b->loc->pspace == current_program_space)
7696 delete_breakpoint (b);
7697 }
7698
7699 /* See breakpoint.h. */
7700
7701 void
7702 remove_solib_event_breakpoints_at_next_stop (void)
7703 {
7704 struct breakpoint *b, *b_tmp;
7705
7706 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7707 if (b->type == bp_shlib_event
7708 && b->loc->pspace == current_program_space)
7709 b->disposition = disp_del_at_next_stop;
7710 }
7711
7712 /* Helper for create_solib_event_breakpoint /
7713 create_and_insert_solib_event_breakpoint. Allows specifying which
7714 INSERT_MODE to pass through to update_global_location_list. */
7715
7716 static struct breakpoint *
7717 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7718 enum ugll_insert_mode insert_mode)
7719 {
7720 struct breakpoint *b;
7721
7722 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7723 &internal_breakpoint_ops);
7724 update_global_location_list_nothrow (insert_mode);
7725 return b;
7726 }
7727
7728 struct breakpoint *
7729 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7730 {
7731 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7732 }
7733
7734 /* See breakpoint.h. */
7735
7736 struct breakpoint *
7737 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7738 {
7739 struct breakpoint *b;
7740
7741 /* Explicitly tell update_global_location_list to insert
7742 locations. */
7743 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7744 if (!b->loc->inserted)
7745 {
7746 delete_breakpoint (b);
7747 return NULL;
7748 }
7749 return b;
7750 }
7751
7752 /* Disable any breakpoints that are on code in shared libraries. Only
7753 apply to enabled breakpoints, disabled ones can just stay disabled. */
7754
7755 void
7756 disable_breakpoints_in_shlibs (void)
7757 {
7758 struct bp_location *loc, **locp_tmp;
7759
7760 ALL_BP_LOCATIONS (loc, locp_tmp)
7761 {
7762 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7763 struct breakpoint *b = loc->owner;
7764
7765 /* We apply the check to all breakpoints, including disabled for
7766 those with loc->duplicate set. This is so that when breakpoint
7767 becomes enabled, or the duplicate is removed, gdb will try to
7768 insert all breakpoints. If we don't set shlib_disabled here,
7769 we'll try to insert those breakpoints and fail. */
7770 if (((b->type == bp_breakpoint)
7771 || (b->type == bp_jit_event)
7772 || (b->type == bp_hardware_breakpoint)
7773 || (is_tracepoint (b)))
7774 && loc->pspace == current_program_space
7775 && !loc->shlib_disabled
7776 && solib_name_from_address (loc->pspace, loc->address)
7777 )
7778 {
7779 loc->shlib_disabled = 1;
7780 }
7781 }
7782 }
7783
7784 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7785 notification of unloaded_shlib. Only apply to enabled breakpoints,
7786 disabled ones can just stay disabled. */
7787
7788 static void
7789 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7790 {
7791 struct bp_location *loc, **locp_tmp;
7792 int disabled_shlib_breaks = 0;
7793
7794 ALL_BP_LOCATIONS (loc, locp_tmp)
7795 {
7796 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7797 struct breakpoint *b = loc->owner;
7798
7799 if (solib->pspace == loc->pspace
7800 && !loc->shlib_disabled
7801 && (((b->type == bp_breakpoint
7802 || b->type == bp_jit_event
7803 || b->type == bp_hardware_breakpoint)
7804 && (loc->loc_type == bp_loc_hardware_breakpoint
7805 || loc->loc_type == bp_loc_software_breakpoint))
7806 || is_tracepoint (b))
7807 && solib_contains_address_p (solib, loc->address))
7808 {
7809 loc->shlib_disabled = 1;
7810 /* At this point, we cannot rely on remove_breakpoint
7811 succeeding so we must mark the breakpoint as not inserted
7812 to prevent future errors occurring in remove_breakpoints. */
7813 loc->inserted = 0;
7814
7815 /* This may cause duplicate notifications for the same breakpoint. */
7816 observer_notify_breakpoint_modified (b);
7817
7818 if (!disabled_shlib_breaks)
7819 {
7820 target_terminal::ours_for_output ();
7821 warning (_("Temporarily disabling breakpoints "
7822 "for unloaded shared library \"%s\""),
7823 solib->so_name);
7824 }
7825 disabled_shlib_breaks = 1;
7826 }
7827 }
7828 }
7829
7830 /* Disable any breakpoints and tracepoints in OBJFILE upon
7831 notification of free_objfile. Only apply to enabled breakpoints,
7832 disabled ones can just stay disabled. */
7833
7834 static void
7835 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7836 {
7837 struct breakpoint *b;
7838
7839 if (objfile == NULL)
7840 return;
7841
7842 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7843 managed by the user with add-symbol-file/remove-symbol-file.
7844 Similarly to how breakpoints in shared libraries are handled in
7845 response to "nosharedlibrary", mark breakpoints in such modules
7846 shlib_disabled so they end up uninserted on the next global
7847 location list update. Shared libraries not loaded by the user
7848 aren't handled here -- they're already handled in
7849 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7850 solib_unloaded observer. We skip objfiles that are not
7851 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7852 main objfile). */
7853 if ((objfile->flags & OBJF_SHARED) == 0
7854 || (objfile->flags & OBJF_USERLOADED) == 0)
7855 return;
7856
7857 ALL_BREAKPOINTS (b)
7858 {
7859 struct bp_location *loc;
7860 int bp_modified = 0;
7861
7862 if (!is_breakpoint (b) && !is_tracepoint (b))
7863 continue;
7864
7865 for (loc = b->loc; loc != NULL; loc = loc->next)
7866 {
7867 CORE_ADDR loc_addr = loc->address;
7868
7869 if (loc->loc_type != bp_loc_hardware_breakpoint
7870 && loc->loc_type != bp_loc_software_breakpoint)
7871 continue;
7872
7873 if (loc->shlib_disabled != 0)
7874 continue;
7875
7876 if (objfile->pspace != loc->pspace)
7877 continue;
7878
7879 if (loc->loc_type != bp_loc_hardware_breakpoint
7880 && loc->loc_type != bp_loc_software_breakpoint)
7881 continue;
7882
7883 if (is_addr_in_objfile (loc_addr, objfile))
7884 {
7885 loc->shlib_disabled = 1;
7886 /* At this point, we don't know whether the object was
7887 unmapped from the inferior or not, so leave the
7888 inserted flag alone. We'll handle failure to
7889 uninsert quietly, in case the object was indeed
7890 unmapped. */
7891
7892 mark_breakpoint_location_modified (loc);
7893
7894 bp_modified = 1;
7895 }
7896 }
7897
7898 if (bp_modified)
7899 observer_notify_breakpoint_modified (b);
7900 }
7901 }
7902
7903 /* FORK & VFORK catchpoints. */
7904
7905 /* An instance of this type is used to represent a fork or vfork
7906 catchpoint. A breakpoint is really of this type iff its ops pointer points
7907 to CATCH_FORK_BREAKPOINT_OPS. */
7908
7909 struct fork_catchpoint : public breakpoint
7910 {
7911 /* Process id of a child process whose forking triggered this
7912 catchpoint. This field is only valid immediately after this
7913 catchpoint has triggered. */
7914 ptid_t forked_inferior_pid;
7915 };
7916
7917 /* Implement the "insert" breakpoint_ops method for fork
7918 catchpoints. */
7919
7920 static int
7921 insert_catch_fork (struct bp_location *bl)
7922 {
7923 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7924 }
7925
7926 /* Implement the "remove" breakpoint_ops method for fork
7927 catchpoints. */
7928
7929 static int
7930 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7931 {
7932 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7933 }
7934
7935 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7936 catchpoints. */
7937
7938 static int
7939 breakpoint_hit_catch_fork (const struct bp_location *bl,
7940 struct address_space *aspace, CORE_ADDR bp_addr,
7941 const struct target_waitstatus *ws)
7942 {
7943 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7944
7945 if (ws->kind != TARGET_WAITKIND_FORKED)
7946 return 0;
7947
7948 c->forked_inferior_pid = ws->value.related_pid;
7949 return 1;
7950 }
7951
7952 /* Implement the "print_it" breakpoint_ops method for fork
7953 catchpoints. */
7954
7955 static enum print_stop_action
7956 print_it_catch_fork (bpstat bs)
7957 {
7958 struct ui_out *uiout = current_uiout;
7959 struct breakpoint *b = bs->breakpoint_at;
7960 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7961
7962 annotate_catchpoint (b->number);
7963 maybe_print_thread_hit_breakpoint (uiout);
7964 if (b->disposition == disp_del)
7965 uiout->text ("Temporary catchpoint ");
7966 else
7967 uiout->text ("Catchpoint ");
7968 if (uiout->is_mi_like_p ())
7969 {
7970 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7971 uiout->field_string ("disp", bpdisp_text (b->disposition));
7972 }
7973 uiout->field_int ("bkptno", b->number);
7974 uiout->text (" (forked process ");
7975 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7976 uiout->text ("), ");
7977 return PRINT_SRC_AND_LOC;
7978 }
7979
7980 /* Implement the "print_one" breakpoint_ops method for fork
7981 catchpoints. */
7982
7983 static void
7984 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7985 {
7986 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7987 struct value_print_options opts;
7988 struct ui_out *uiout = current_uiout;
7989
7990 get_user_print_options (&opts);
7991
7992 /* Field 4, the address, is omitted (which makes the columns not
7993 line up too nicely with the headers, but the effect is relatively
7994 readable). */
7995 if (opts.addressprint)
7996 uiout->field_skip ("addr");
7997 annotate_field (5);
7998 uiout->text ("fork");
7999 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8000 {
8001 uiout->text (", process ");
8002 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8003 uiout->spaces (1);
8004 }
8005
8006 if (uiout->is_mi_like_p ())
8007 uiout->field_string ("catch-type", "fork");
8008 }
8009
8010 /* Implement the "print_mention" breakpoint_ops method for fork
8011 catchpoints. */
8012
8013 static void
8014 print_mention_catch_fork (struct breakpoint *b)
8015 {
8016 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8017 }
8018
8019 /* Implement the "print_recreate" breakpoint_ops method for fork
8020 catchpoints. */
8021
8022 static void
8023 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8024 {
8025 fprintf_unfiltered (fp, "catch fork");
8026 print_recreate_thread (b, fp);
8027 }
8028
8029 /* The breakpoint_ops structure to be used in fork catchpoints. */
8030
8031 static struct breakpoint_ops catch_fork_breakpoint_ops;
8032
8033 /* Implement the "insert" breakpoint_ops method for vfork
8034 catchpoints. */
8035
8036 static int
8037 insert_catch_vfork (struct bp_location *bl)
8038 {
8039 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8040 }
8041
8042 /* Implement the "remove" breakpoint_ops method for vfork
8043 catchpoints. */
8044
8045 static int
8046 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8047 {
8048 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8049 }
8050
8051 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8052 catchpoints. */
8053
8054 static int
8055 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8056 struct address_space *aspace, CORE_ADDR bp_addr,
8057 const struct target_waitstatus *ws)
8058 {
8059 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8060
8061 if (ws->kind != TARGET_WAITKIND_VFORKED)
8062 return 0;
8063
8064 c->forked_inferior_pid = ws->value.related_pid;
8065 return 1;
8066 }
8067
8068 /* Implement the "print_it" breakpoint_ops method for vfork
8069 catchpoints. */
8070
8071 static enum print_stop_action
8072 print_it_catch_vfork (bpstat bs)
8073 {
8074 struct ui_out *uiout = current_uiout;
8075 struct breakpoint *b = bs->breakpoint_at;
8076 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8077
8078 annotate_catchpoint (b->number);
8079 maybe_print_thread_hit_breakpoint (uiout);
8080 if (b->disposition == disp_del)
8081 uiout->text ("Temporary catchpoint ");
8082 else
8083 uiout->text ("Catchpoint ");
8084 if (uiout->is_mi_like_p ())
8085 {
8086 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8087 uiout->field_string ("disp", bpdisp_text (b->disposition));
8088 }
8089 uiout->field_int ("bkptno", b->number);
8090 uiout->text (" (vforked process ");
8091 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8092 uiout->text ("), ");
8093 return PRINT_SRC_AND_LOC;
8094 }
8095
8096 /* Implement the "print_one" breakpoint_ops method for vfork
8097 catchpoints. */
8098
8099 static void
8100 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8101 {
8102 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8103 struct value_print_options opts;
8104 struct ui_out *uiout = current_uiout;
8105
8106 get_user_print_options (&opts);
8107 /* Field 4, the address, is omitted (which makes the columns not
8108 line up too nicely with the headers, but the effect is relatively
8109 readable). */
8110 if (opts.addressprint)
8111 uiout->field_skip ("addr");
8112 annotate_field (5);
8113 uiout->text ("vfork");
8114 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8115 {
8116 uiout->text (", process ");
8117 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8118 uiout->spaces (1);
8119 }
8120
8121 if (uiout->is_mi_like_p ())
8122 uiout->field_string ("catch-type", "vfork");
8123 }
8124
8125 /* Implement the "print_mention" breakpoint_ops method for vfork
8126 catchpoints. */
8127
8128 static void
8129 print_mention_catch_vfork (struct breakpoint *b)
8130 {
8131 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8132 }
8133
8134 /* Implement the "print_recreate" breakpoint_ops method for vfork
8135 catchpoints. */
8136
8137 static void
8138 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8139 {
8140 fprintf_unfiltered (fp, "catch vfork");
8141 print_recreate_thread (b, fp);
8142 }
8143
8144 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8145
8146 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8147
8148 /* An instance of this type is used to represent an solib catchpoint.
8149 A breakpoint is really of this type iff its ops pointer points to
8150 CATCH_SOLIB_BREAKPOINT_OPS. */
8151
8152 struct solib_catchpoint : public breakpoint
8153 {
8154 ~solib_catchpoint () override;
8155
8156 /* True for "catch load", false for "catch unload". */
8157 unsigned char is_load;
8158
8159 /* Regular expression to match, if any. COMPILED is only valid when
8160 REGEX is non-NULL. */
8161 char *regex;
8162 std::unique_ptr<compiled_regex> compiled;
8163 };
8164
8165 solib_catchpoint::~solib_catchpoint ()
8166 {
8167 xfree (this->regex);
8168 }
8169
8170 static int
8171 insert_catch_solib (struct bp_location *ignore)
8172 {
8173 return 0;
8174 }
8175
8176 static int
8177 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8178 {
8179 return 0;
8180 }
8181
8182 static int
8183 breakpoint_hit_catch_solib (const struct bp_location *bl,
8184 struct address_space *aspace,
8185 CORE_ADDR bp_addr,
8186 const struct target_waitstatus *ws)
8187 {
8188 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8189 struct breakpoint *other;
8190
8191 if (ws->kind == TARGET_WAITKIND_LOADED)
8192 return 1;
8193
8194 ALL_BREAKPOINTS (other)
8195 {
8196 struct bp_location *other_bl;
8197
8198 if (other == bl->owner)
8199 continue;
8200
8201 if (other->type != bp_shlib_event)
8202 continue;
8203
8204 if (self->pspace != NULL && other->pspace != self->pspace)
8205 continue;
8206
8207 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8208 {
8209 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8210 return 1;
8211 }
8212 }
8213
8214 return 0;
8215 }
8216
8217 static void
8218 check_status_catch_solib (struct bpstats *bs)
8219 {
8220 struct solib_catchpoint *self
8221 = (struct solib_catchpoint *) bs->breakpoint_at;
8222 int ix;
8223
8224 if (self->is_load)
8225 {
8226 struct so_list *iter;
8227
8228 for (ix = 0;
8229 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8230 ix, iter);
8231 ++ix)
8232 {
8233 if (!self->regex
8234 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8235 return;
8236 }
8237 }
8238 else
8239 {
8240 char *iter;
8241
8242 for (ix = 0;
8243 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8244 ix, iter);
8245 ++ix)
8246 {
8247 if (!self->regex
8248 || self->compiled->exec (iter, 0, NULL, 0) == 0)
8249 return;
8250 }
8251 }
8252
8253 bs->stop = 0;
8254 bs->print_it = print_it_noop;
8255 }
8256
8257 static enum print_stop_action
8258 print_it_catch_solib (bpstat bs)
8259 {
8260 struct breakpoint *b = bs->breakpoint_at;
8261 struct ui_out *uiout = current_uiout;
8262
8263 annotate_catchpoint (b->number);
8264 maybe_print_thread_hit_breakpoint (uiout);
8265 if (b->disposition == disp_del)
8266 uiout->text ("Temporary catchpoint ");
8267 else
8268 uiout->text ("Catchpoint ");
8269 uiout->field_int ("bkptno", b->number);
8270 uiout->text ("\n");
8271 if (uiout->is_mi_like_p ())
8272 uiout->field_string ("disp", bpdisp_text (b->disposition));
8273 print_solib_event (1);
8274 return PRINT_SRC_AND_LOC;
8275 }
8276
8277 static void
8278 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8279 {
8280 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8281 struct value_print_options opts;
8282 struct ui_out *uiout = current_uiout;
8283 char *msg;
8284
8285 get_user_print_options (&opts);
8286 /* Field 4, the address, is omitted (which makes the columns not
8287 line up too nicely with the headers, but the effect is relatively
8288 readable). */
8289 if (opts.addressprint)
8290 {
8291 annotate_field (4);
8292 uiout->field_skip ("addr");
8293 }
8294
8295 annotate_field (5);
8296 if (self->is_load)
8297 {
8298 if (self->regex)
8299 msg = xstrprintf (_("load of library matching %s"), self->regex);
8300 else
8301 msg = xstrdup (_("load of library"));
8302 }
8303 else
8304 {
8305 if (self->regex)
8306 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8307 else
8308 msg = xstrdup (_("unload of library"));
8309 }
8310 uiout->field_string ("what", msg);
8311 xfree (msg);
8312
8313 if (uiout->is_mi_like_p ())
8314 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8315 }
8316
8317 static void
8318 print_mention_catch_solib (struct breakpoint *b)
8319 {
8320 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8321
8322 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8323 self->is_load ? "load" : "unload");
8324 }
8325
8326 static void
8327 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8328 {
8329 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8330
8331 fprintf_unfiltered (fp, "%s %s",
8332 b->disposition == disp_del ? "tcatch" : "catch",
8333 self->is_load ? "load" : "unload");
8334 if (self->regex)
8335 fprintf_unfiltered (fp, " %s", self->regex);
8336 fprintf_unfiltered (fp, "\n");
8337 }
8338
8339 static struct breakpoint_ops catch_solib_breakpoint_ops;
8340
8341 /* Shared helper function (MI and CLI) for creating and installing
8342 a shared object event catchpoint. If IS_LOAD is non-zero then
8343 the events to be caught are load events, otherwise they are
8344 unload events. If IS_TEMP is non-zero the catchpoint is a
8345 temporary one. If ENABLED is non-zero the catchpoint is
8346 created in an enabled state. */
8347
8348 void
8349 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8350 {
8351 struct gdbarch *gdbarch = get_current_arch ();
8352
8353 if (!arg)
8354 arg = "";
8355 arg = skip_spaces (arg);
8356
8357 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8358
8359 if (*arg != '\0')
8360 {
8361 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8362 _("Invalid regexp")));
8363 c->regex = xstrdup (arg);
8364 }
8365
8366 c->is_load = is_load;
8367 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8368 &catch_solib_breakpoint_ops);
8369
8370 c->enable_state = enabled ? bp_enabled : bp_disabled;
8371
8372 install_breakpoint (0, std::move (c), 1);
8373 }
8374
8375 /* A helper function that does all the work for "catch load" and
8376 "catch unload". */
8377
8378 static void
8379 catch_load_or_unload (char *arg, int from_tty, int is_load,
8380 struct cmd_list_element *command)
8381 {
8382 int tempflag;
8383 const int enabled = 1;
8384
8385 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8386
8387 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8388 }
8389
8390 static void
8391 catch_load_command_1 (char *arg, int from_tty,
8392 struct cmd_list_element *command)
8393 {
8394 catch_load_or_unload (arg, from_tty, 1, command);
8395 }
8396
8397 static void
8398 catch_unload_command_1 (char *arg, int from_tty,
8399 struct cmd_list_element *command)
8400 {
8401 catch_load_or_unload (arg, from_tty, 0, command);
8402 }
8403
8404 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8405 is non-zero, then make the breakpoint temporary. If COND_STRING is
8406 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8407 the breakpoint_ops structure associated to the catchpoint. */
8408
8409 void
8410 init_catchpoint (struct breakpoint *b,
8411 struct gdbarch *gdbarch, int tempflag,
8412 const char *cond_string,
8413 const struct breakpoint_ops *ops)
8414 {
8415 symtab_and_line sal;
8416 sal.pspace = current_program_space;
8417
8418 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8419
8420 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8421 b->disposition = tempflag ? disp_del : disp_donttouch;
8422 }
8423
8424 void
8425 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8426 {
8427 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8428 set_breakpoint_number (internal, b);
8429 if (is_tracepoint (b))
8430 set_tracepoint_count (breakpoint_count);
8431 if (!internal)
8432 mention (b);
8433 observer_notify_breakpoint_created (b);
8434
8435 if (update_gll)
8436 update_global_location_list (UGLL_MAY_INSERT);
8437 }
8438
8439 static void
8440 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8441 int tempflag, const char *cond_string,
8442 const struct breakpoint_ops *ops)
8443 {
8444 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8445
8446 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8447
8448 c->forked_inferior_pid = null_ptid;
8449
8450 install_breakpoint (0, std::move (c), 1);
8451 }
8452
8453 /* Exec catchpoints. */
8454
8455 /* An instance of this type is used to represent an exec catchpoint.
8456 A breakpoint is really of this type iff its ops pointer points to
8457 CATCH_EXEC_BREAKPOINT_OPS. */
8458
8459 struct exec_catchpoint : public breakpoint
8460 {
8461 ~exec_catchpoint () override;
8462
8463 /* Filename of a program whose exec triggered this catchpoint.
8464 This field is only valid immediately after this catchpoint has
8465 triggered. */
8466 char *exec_pathname;
8467 };
8468
8469 /* Exec catchpoint destructor. */
8470
8471 exec_catchpoint::~exec_catchpoint ()
8472 {
8473 xfree (this->exec_pathname);
8474 }
8475
8476 static int
8477 insert_catch_exec (struct bp_location *bl)
8478 {
8479 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8480 }
8481
8482 static int
8483 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8484 {
8485 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8486 }
8487
8488 static int
8489 breakpoint_hit_catch_exec (const struct bp_location *bl,
8490 struct address_space *aspace, CORE_ADDR bp_addr,
8491 const struct target_waitstatus *ws)
8492 {
8493 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8494
8495 if (ws->kind != TARGET_WAITKIND_EXECD)
8496 return 0;
8497
8498 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8499 return 1;
8500 }
8501
8502 static enum print_stop_action
8503 print_it_catch_exec (bpstat bs)
8504 {
8505 struct ui_out *uiout = current_uiout;
8506 struct breakpoint *b = bs->breakpoint_at;
8507 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8508
8509 annotate_catchpoint (b->number);
8510 maybe_print_thread_hit_breakpoint (uiout);
8511 if (b->disposition == disp_del)
8512 uiout->text ("Temporary catchpoint ");
8513 else
8514 uiout->text ("Catchpoint ");
8515 if (uiout->is_mi_like_p ())
8516 {
8517 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8518 uiout->field_string ("disp", bpdisp_text (b->disposition));
8519 }
8520 uiout->field_int ("bkptno", b->number);
8521 uiout->text (" (exec'd ");
8522 uiout->field_string ("new-exec", c->exec_pathname);
8523 uiout->text ("), ");
8524
8525 return PRINT_SRC_AND_LOC;
8526 }
8527
8528 static void
8529 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8530 {
8531 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8532 struct value_print_options opts;
8533 struct ui_out *uiout = current_uiout;
8534
8535 get_user_print_options (&opts);
8536
8537 /* Field 4, the address, is omitted (which makes the columns
8538 not line up too nicely with the headers, but the effect
8539 is relatively readable). */
8540 if (opts.addressprint)
8541 uiout->field_skip ("addr");
8542 annotate_field (5);
8543 uiout->text ("exec");
8544 if (c->exec_pathname != NULL)
8545 {
8546 uiout->text (", program \"");
8547 uiout->field_string ("what", c->exec_pathname);
8548 uiout->text ("\" ");
8549 }
8550
8551 if (uiout->is_mi_like_p ())
8552 uiout->field_string ("catch-type", "exec");
8553 }
8554
8555 static void
8556 print_mention_catch_exec (struct breakpoint *b)
8557 {
8558 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8559 }
8560
8561 /* Implement the "print_recreate" breakpoint_ops method for exec
8562 catchpoints. */
8563
8564 static void
8565 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8566 {
8567 fprintf_unfiltered (fp, "catch exec");
8568 print_recreate_thread (b, fp);
8569 }
8570
8571 static struct breakpoint_ops catch_exec_breakpoint_ops;
8572
8573 static int
8574 hw_breakpoint_used_count (void)
8575 {
8576 int i = 0;
8577 struct breakpoint *b;
8578 struct bp_location *bl;
8579
8580 ALL_BREAKPOINTS (b)
8581 {
8582 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8583 for (bl = b->loc; bl; bl = bl->next)
8584 {
8585 /* Special types of hardware breakpoints may use more than
8586 one register. */
8587 i += b->ops->resources_needed (bl);
8588 }
8589 }
8590
8591 return i;
8592 }
8593
8594 /* Returns the resources B would use if it were a hardware
8595 watchpoint. */
8596
8597 static int
8598 hw_watchpoint_use_count (struct breakpoint *b)
8599 {
8600 int i = 0;
8601 struct bp_location *bl;
8602
8603 if (!breakpoint_enabled (b))
8604 return 0;
8605
8606 for (bl = b->loc; bl; bl = bl->next)
8607 {
8608 /* Special types of hardware watchpoints may use more than
8609 one register. */
8610 i += b->ops->resources_needed (bl);
8611 }
8612
8613 return i;
8614 }
8615
8616 /* Returns the sum the used resources of all hardware watchpoints of
8617 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8618 the sum of the used resources of all hardware watchpoints of other
8619 types _not_ TYPE. */
8620
8621 static int
8622 hw_watchpoint_used_count_others (struct breakpoint *except,
8623 enum bptype type, int *other_type_used)
8624 {
8625 int i = 0;
8626 struct breakpoint *b;
8627
8628 *other_type_used = 0;
8629 ALL_BREAKPOINTS (b)
8630 {
8631 if (b == except)
8632 continue;
8633 if (!breakpoint_enabled (b))
8634 continue;
8635
8636 if (b->type == type)
8637 i += hw_watchpoint_use_count (b);
8638 else if (is_hardware_watchpoint (b))
8639 *other_type_used = 1;
8640 }
8641
8642 return i;
8643 }
8644
8645 void
8646 disable_watchpoints_before_interactive_call_start (void)
8647 {
8648 struct breakpoint *b;
8649
8650 ALL_BREAKPOINTS (b)
8651 {
8652 if (is_watchpoint (b) && breakpoint_enabled (b))
8653 {
8654 b->enable_state = bp_call_disabled;
8655 update_global_location_list (UGLL_DONT_INSERT);
8656 }
8657 }
8658 }
8659
8660 void
8661 enable_watchpoints_after_interactive_call_stop (void)
8662 {
8663 struct breakpoint *b;
8664
8665 ALL_BREAKPOINTS (b)
8666 {
8667 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8668 {
8669 b->enable_state = bp_enabled;
8670 update_global_location_list (UGLL_MAY_INSERT);
8671 }
8672 }
8673 }
8674
8675 void
8676 disable_breakpoints_before_startup (void)
8677 {
8678 current_program_space->executing_startup = 1;
8679 update_global_location_list (UGLL_DONT_INSERT);
8680 }
8681
8682 void
8683 enable_breakpoints_after_startup (void)
8684 {
8685 current_program_space->executing_startup = 0;
8686 breakpoint_re_set ();
8687 }
8688
8689 /* Create a new single-step breakpoint for thread THREAD, with no
8690 locations. */
8691
8692 static struct breakpoint *
8693 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8694 {
8695 std::unique_ptr<breakpoint> b (new breakpoint ());
8696
8697 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8698 &momentary_breakpoint_ops);
8699
8700 b->disposition = disp_donttouch;
8701 b->frame_id = null_frame_id;
8702
8703 b->thread = thread;
8704 gdb_assert (b->thread != 0);
8705
8706 return add_to_breakpoint_chain (std::move (b));
8707 }
8708
8709 /* Set a momentary breakpoint of type TYPE at address specified by
8710 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8711 frame. */
8712
8713 struct breakpoint *
8714 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8715 struct frame_id frame_id, enum bptype type)
8716 {
8717 struct breakpoint *b;
8718
8719 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8720 tail-called one. */
8721 gdb_assert (!frame_id_artificial_p (frame_id));
8722
8723 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8724 b->enable_state = bp_enabled;
8725 b->disposition = disp_donttouch;
8726 b->frame_id = frame_id;
8727
8728 /* If we're debugging a multi-threaded program, then we want
8729 momentary breakpoints to be active in only a single thread of
8730 control. */
8731 if (in_thread_list (inferior_ptid))
8732 b->thread = ptid_to_global_thread_id (inferior_ptid);
8733
8734 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8735
8736 return b;
8737 }
8738
8739 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8740 The new breakpoint will have type TYPE, use OPS as its
8741 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8742
8743 static struct breakpoint *
8744 momentary_breakpoint_from_master (struct breakpoint *orig,
8745 enum bptype type,
8746 const struct breakpoint_ops *ops,
8747 int loc_enabled)
8748 {
8749 struct breakpoint *copy;
8750
8751 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8752 copy->loc = allocate_bp_location (copy);
8753 set_breakpoint_location_function (copy->loc, 1);
8754
8755 copy->loc->gdbarch = orig->loc->gdbarch;
8756 copy->loc->requested_address = orig->loc->requested_address;
8757 copy->loc->address = orig->loc->address;
8758 copy->loc->section = orig->loc->section;
8759 copy->loc->pspace = orig->loc->pspace;
8760 copy->loc->probe = orig->loc->probe;
8761 copy->loc->line_number = orig->loc->line_number;
8762 copy->loc->symtab = orig->loc->symtab;
8763 copy->loc->enabled = loc_enabled;
8764 copy->frame_id = orig->frame_id;
8765 copy->thread = orig->thread;
8766 copy->pspace = orig->pspace;
8767
8768 copy->enable_state = bp_enabled;
8769 copy->disposition = disp_donttouch;
8770 copy->number = internal_breakpoint_number--;
8771
8772 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8773 return copy;
8774 }
8775
8776 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8777 ORIG is NULL. */
8778
8779 struct breakpoint *
8780 clone_momentary_breakpoint (struct breakpoint *orig)
8781 {
8782 /* If there's nothing to clone, then return nothing. */
8783 if (orig == NULL)
8784 return NULL;
8785
8786 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8787 }
8788
8789 struct breakpoint *
8790 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8791 enum bptype type)
8792 {
8793 struct symtab_and_line sal;
8794
8795 sal = find_pc_line (pc, 0);
8796 sal.pc = pc;
8797 sal.section = find_pc_overlay (pc);
8798 sal.explicit_pc = 1;
8799
8800 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8801 }
8802 \f
8803
8804 /* Tell the user we have just set a breakpoint B. */
8805
8806 static void
8807 mention (struct breakpoint *b)
8808 {
8809 b->ops->print_mention (b);
8810 if (current_uiout->is_mi_like_p ())
8811 return;
8812 printf_filtered ("\n");
8813 }
8814 \f
8815
8816 static int bp_loc_is_permanent (struct bp_location *loc);
8817
8818 static struct bp_location *
8819 add_location_to_breakpoint (struct breakpoint *b,
8820 const struct symtab_and_line *sal)
8821 {
8822 struct bp_location *loc, **tmp;
8823 CORE_ADDR adjusted_address;
8824 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8825
8826 if (loc_gdbarch == NULL)
8827 loc_gdbarch = b->gdbarch;
8828
8829 /* Adjust the breakpoint's address prior to allocating a location.
8830 Once we call allocate_bp_location(), that mostly uninitialized
8831 location will be placed on the location chain. Adjustment of the
8832 breakpoint may cause target_read_memory() to be called and we do
8833 not want its scan of the location chain to find a breakpoint and
8834 location that's only been partially initialized. */
8835 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8836 sal->pc, b->type);
8837
8838 /* Sort the locations by their ADDRESS. */
8839 loc = allocate_bp_location (b);
8840 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8841 tmp = &((*tmp)->next))
8842 ;
8843 loc->next = *tmp;
8844 *tmp = loc;
8845
8846 loc->requested_address = sal->pc;
8847 loc->address = adjusted_address;
8848 loc->pspace = sal->pspace;
8849 loc->probe.probe = sal->probe;
8850 loc->probe.objfile = sal->objfile;
8851 gdb_assert (loc->pspace != NULL);
8852 loc->section = sal->section;
8853 loc->gdbarch = loc_gdbarch;
8854 loc->line_number = sal->line;
8855 loc->symtab = sal->symtab;
8856
8857 set_breakpoint_location_function (loc,
8858 sal->explicit_pc || sal->explicit_line);
8859
8860 /* While by definition, permanent breakpoints are already present in the
8861 code, we don't mark the location as inserted. Normally one would expect
8862 that GDB could rely on that breakpoint instruction to stop the program,
8863 thus removing the need to insert its own breakpoint, except that executing
8864 the breakpoint instruction can kill the target instead of reporting a
8865 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8866 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8867 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8868 breakpoint be inserted normally results in QEMU knowing about the GDB
8869 breakpoint, and thus trap before the breakpoint instruction is executed.
8870 (If GDB later needs to continue execution past the permanent breakpoint,
8871 it manually increments the PC, thus avoiding executing the breakpoint
8872 instruction.) */
8873 if (bp_loc_is_permanent (loc))
8874 loc->permanent = 1;
8875
8876 return loc;
8877 }
8878 \f
8879
8880 /* See breakpoint.h. */
8881
8882 int
8883 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8884 {
8885 int len;
8886 CORE_ADDR addr;
8887 const gdb_byte *bpoint;
8888 gdb_byte *target_mem;
8889
8890 addr = address;
8891 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8892
8893 /* Software breakpoints unsupported? */
8894 if (bpoint == NULL)
8895 return 0;
8896
8897 target_mem = (gdb_byte *) alloca (len);
8898
8899 /* Enable the automatic memory restoration from breakpoints while
8900 we read the memory. Otherwise we could say about our temporary
8901 breakpoints they are permanent. */
8902 scoped_restore restore_memory
8903 = make_scoped_restore_show_memory_breakpoints (0);
8904
8905 if (target_read_memory (address, target_mem, len) == 0
8906 && memcmp (target_mem, bpoint, len) == 0)
8907 return 1;
8908
8909 return 0;
8910 }
8911
8912 /* Return 1 if LOC is pointing to a permanent breakpoint,
8913 return 0 otherwise. */
8914
8915 static int
8916 bp_loc_is_permanent (struct bp_location *loc)
8917 {
8918 gdb_assert (loc != NULL);
8919
8920 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8921 attempt to read from the addresses the locations of these breakpoint types
8922 point to. program_breakpoint_here_p, below, will attempt to read
8923 memory. */
8924 if (!breakpoint_address_is_meaningful (loc->owner))
8925 return 0;
8926
8927 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8928 switch_to_program_space_and_thread (loc->pspace);
8929 return program_breakpoint_here_p (loc->gdbarch, loc->address);
8930 }
8931
8932 /* Build a command list for the dprintf corresponding to the current
8933 settings of the dprintf style options. */
8934
8935 static void
8936 update_dprintf_command_list (struct breakpoint *b)
8937 {
8938 char *dprintf_args = b->extra_string;
8939 char *printf_line = NULL;
8940
8941 if (!dprintf_args)
8942 return;
8943
8944 dprintf_args = skip_spaces (dprintf_args);
8945
8946 /* Allow a comma, as it may have terminated a location, but don't
8947 insist on it. */
8948 if (*dprintf_args == ',')
8949 ++dprintf_args;
8950 dprintf_args = skip_spaces (dprintf_args);
8951
8952 if (*dprintf_args != '"')
8953 error (_("Bad format string, missing '\"'."));
8954
8955 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8956 printf_line = xstrprintf ("printf %s", dprintf_args);
8957 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8958 {
8959 if (!dprintf_function)
8960 error (_("No function supplied for dprintf call"));
8961
8962 if (dprintf_channel && strlen (dprintf_channel) > 0)
8963 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8964 dprintf_function,
8965 dprintf_channel,
8966 dprintf_args);
8967 else
8968 printf_line = xstrprintf ("call (void) %s (%s)",
8969 dprintf_function,
8970 dprintf_args);
8971 }
8972 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8973 {
8974 if (target_can_run_breakpoint_commands ())
8975 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8976 else
8977 {
8978 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8979 printf_line = xstrprintf ("printf %s", dprintf_args);
8980 }
8981 }
8982 else
8983 internal_error (__FILE__, __LINE__,
8984 _("Invalid dprintf style."));
8985
8986 gdb_assert (printf_line != NULL);
8987 /* Manufacture a printf sequence. */
8988 {
8989 struct command_line *printf_cmd_line = XNEW (struct command_line);
8990
8991 printf_cmd_line->control_type = simple_control;
8992 printf_cmd_line->body_count = 0;
8993 printf_cmd_line->body_list = NULL;
8994 printf_cmd_line->next = NULL;
8995 printf_cmd_line->line = printf_line;
8996
8997 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
8998 }
8999 }
9000
9001 /* Update all dprintf commands, making their command lists reflect
9002 current style settings. */
9003
9004 static void
9005 update_dprintf_commands (char *args, int from_tty,
9006 struct cmd_list_element *c)
9007 {
9008 struct breakpoint *b;
9009
9010 ALL_BREAKPOINTS (b)
9011 {
9012 if (b->type == bp_dprintf)
9013 update_dprintf_command_list (b);
9014 }
9015 }
9016
9017 /* Create a breakpoint with SAL as location. Use LOCATION
9018 as a description of the location, and COND_STRING
9019 as condition expression. If LOCATION is NULL then create an
9020 "address location" from the address in the SAL. */
9021
9022 static void
9023 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9024 gdb::array_view<const symtab_and_line> sals,
9025 event_location_up &&location,
9026 gdb::unique_xmalloc_ptr<char> filter,
9027 gdb::unique_xmalloc_ptr<char> cond_string,
9028 gdb::unique_xmalloc_ptr<char> extra_string,
9029 enum bptype type, enum bpdisp disposition,
9030 int thread, int task, int ignore_count,
9031 const struct breakpoint_ops *ops, int from_tty,
9032 int enabled, int internal, unsigned flags,
9033 int display_canonical)
9034 {
9035 int i;
9036
9037 if (type == bp_hardware_breakpoint)
9038 {
9039 int target_resources_ok;
9040
9041 i = hw_breakpoint_used_count ();
9042 target_resources_ok =
9043 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9044 i + 1, 0);
9045 if (target_resources_ok == 0)
9046 error (_("No hardware breakpoint support in the target."));
9047 else if (target_resources_ok < 0)
9048 error (_("Hardware breakpoints used exceeds limit."));
9049 }
9050
9051 gdb_assert (!sals.empty ());
9052
9053 for (const auto &sal : sals)
9054 {
9055 struct bp_location *loc;
9056
9057 if (from_tty)
9058 {
9059 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9060 if (!loc_gdbarch)
9061 loc_gdbarch = gdbarch;
9062
9063 describe_other_breakpoints (loc_gdbarch,
9064 sal.pspace, sal.pc, sal.section, thread);
9065 }
9066
9067 if (&sal == &sals[0])
9068 {
9069 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9070 b->thread = thread;
9071 b->task = task;
9072
9073 b->cond_string = cond_string.release ();
9074 b->extra_string = extra_string.release ();
9075 b->ignore_count = ignore_count;
9076 b->enable_state = enabled ? bp_enabled : bp_disabled;
9077 b->disposition = disposition;
9078
9079 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9080 b->loc->inserted = 1;
9081
9082 if (type == bp_static_tracepoint)
9083 {
9084 struct tracepoint *t = (struct tracepoint *) b;
9085 struct static_tracepoint_marker marker;
9086
9087 if (strace_marker_p (b))
9088 {
9089 /* We already know the marker exists, otherwise, we
9090 wouldn't see a sal for it. */
9091 const char *p
9092 = &event_location_to_string (b->location.get ())[3];
9093 const char *endp;
9094 char *marker_str;
9095
9096 p = skip_spaces (p);
9097
9098 endp = skip_to_space (p);
9099
9100 marker_str = savestring (p, endp - p);
9101 t->static_trace_marker_id = marker_str;
9102
9103 printf_filtered (_("Probed static tracepoint "
9104 "marker \"%s\"\n"),
9105 t->static_trace_marker_id);
9106 }
9107 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9108 {
9109 t->static_trace_marker_id = xstrdup (marker.str_id);
9110 release_static_tracepoint_marker (&marker);
9111
9112 printf_filtered (_("Probed static tracepoint "
9113 "marker \"%s\"\n"),
9114 t->static_trace_marker_id);
9115 }
9116 else
9117 warning (_("Couldn't determine the static "
9118 "tracepoint marker to probe"));
9119 }
9120
9121 loc = b->loc;
9122 }
9123 else
9124 {
9125 loc = add_location_to_breakpoint (b, &sal);
9126 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9127 loc->inserted = 1;
9128 }
9129
9130 if (b->cond_string)
9131 {
9132 const char *arg = b->cond_string;
9133
9134 loc->cond = parse_exp_1 (&arg, loc->address,
9135 block_for_pc (loc->address), 0);
9136 if (*arg)
9137 error (_("Garbage '%s' follows condition"), arg);
9138 }
9139
9140 /* Dynamic printf requires and uses additional arguments on the
9141 command line, otherwise it's an error. */
9142 if (type == bp_dprintf)
9143 {
9144 if (b->extra_string)
9145 update_dprintf_command_list (b);
9146 else
9147 error (_("Format string required"));
9148 }
9149 else if (b->extra_string)
9150 error (_("Garbage '%s' at end of command"), b->extra_string);
9151 }
9152
9153 b->display_canonical = display_canonical;
9154 if (location != NULL)
9155 b->location = std::move (location);
9156 else
9157 b->location = new_address_location (b->loc->address, NULL, 0);
9158 b->filter = filter.release ();
9159 }
9160
9161 static void
9162 create_breakpoint_sal (struct gdbarch *gdbarch,
9163 gdb::array_view<const symtab_and_line> sals,
9164 event_location_up &&location,
9165 gdb::unique_xmalloc_ptr<char> filter,
9166 gdb::unique_xmalloc_ptr<char> cond_string,
9167 gdb::unique_xmalloc_ptr<char> extra_string,
9168 enum bptype type, enum bpdisp disposition,
9169 int thread, int task, int ignore_count,
9170 const struct breakpoint_ops *ops, int from_tty,
9171 int enabled, int internal, unsigned flags,
9172 int display_canonical)
9173 {
9174 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9175
9176 init_breakpoint_sal (b.get (), gdbarch,
9177 sals, std::move (location),
9178 std::move (filter),
9179 std::move (cond_string),
9180 std::move (extra_string),
9181 type, disposition,
9182 thread, task, ignore_count,
9183 ops, from_tty,
9184 enabled, internal, flags,
9185 display_canonical);
9186
9187 install_breakpoint (internal, std::move (b), 0);
9188 }
9189
9190 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9191 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9192 value. COND_STRING, if not NULL, specified the condition to be
9193 used for all breakpoints. Essentially the only case where
9194 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9195 function. In that case, it's still not possible to specify
9196 separate conditions for different overloaded functions, so
9197 we take just a single condition string.
9198
9199 NOTE: If the function succeeds, the caller is expected to cleanup
9200 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9201 array contents). If the function fails (error() is called), the
9202 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9203 COND and SALS arrays and each of those arrays contents. */
9204
9205 static void
9206 create_breakpoints_sal (struct gdbarch *gdbarch,
9207 struct linespec_result *canonical,
9208 gdb::unique_xmalloc_ptr<char> cond_string,
9209 gdb::unique_xmalloc_ptr<char> extra_string,
9210 enum bptype type, enum bpdisp disposition,
9211 int thread, int task, int ignore_count,
9212 const struct breakpoint_ops *ops, int from_tty,
9213 int enabled, int internal, unsigned flags)
9214 {
9215 if (canonical->pre_expanded)
9216 gdb_assert (canonical->lsals.size () == 1);
9217
9218 for (const auto &lsal : canonical->lsals)
9219 {
9220 /* Note that 'location' can be NULL in the case of a plain
9221 'break', without arguments. */
9222 event_location_up location
9223 = (canonical->location != NULL
9224 ? copy_event_location (canonical->location.get ()) : NULL);
9225 gdb::unique_xmalloc_ptr<char> filter_string
9226 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9227
9228 create_breakpoint_sal (gdbarch, lsal.sals,
9229 std::move (location),
9230 std::move (filter_string),
9231 std::move (cond_string),
9232 std::move (extra_string),
9233 type, disposition,
9234 thread, task, ignore_count, ops,
9235 from_tty, enabled, internal, flags,
9236 canonical->special_display);
9237 }
9238 }
9239
9240 /* Parse LOCATION which is assumed to be a SAL specification possibly
9241 followed by conditionals. On return, SALS contains an array of SAL
9242 addresses found. LOCATION points to the end of the SAL (for
9243 linespec locations).
9244
9245 The array and the line spec strings are allocated on the heap, it is
9246 the caller's responsibility to free them. */
9247
9248 static void
9249 parse_breakpoint_sals (const struct event_location *location,
9250 struct linespec_result *canonical)
9251 {
9252 struct symtab_and_line cursal;
9253
9254 if (event_location_type (location) == LINESPEC_LOCATION)
9255 {
9256 const char *address = get_linespec_location (location);
9257
9258 if (address == NULL)
9259 {
9260 /* The last displayed codepoint, if it's valid, is our default
9261 breakpoint address. */
9262 if (last_displayed_sal_is_valid ())
9263 {
9264 /* Set sal's pspace, pc, symtab, and line to the values
9265 corresponding to the last call to print_frame_info.
9266 Be sure to reinitialize LINE with NOTCURRENT == 0
9267 as the breakpoint line number is inappropriate otherwise.
9268 find_pc_line would adjust PC, re-set it back. */
9269 symtab_and_line sal = get_last_displayed_sal ();
9270 CORE_ADDR pc = sal.pc;
9271
9272 sal = find_pc_line (pc, 0);
9273
9274 /* "break" without arguments is equivalent to "break *PC"
9275 where PC is the last displayed codepoint's address. So
9276 make sure to set sal.explicit_pc to prevent GDB from
9277 trying to expand the list of sals to include all other
9278 instances with the same symtab and line. */
9279 sal.pc = pc;
9280 sal.explicit_pc = 1;
9281
9282 struct linespec_sals lsal;
9283 lsal.sals = {sal};
9284 lsal.canonical = NULL;
9285
9286 canonical->lsals.push_back (std::move (lsal));
9287 return;
9288 }
9289 else
9290 error (_("No default breakpoint address now."));
9291 }
9292 }
9293
9294 /* Force almost all breakpoints to be in terms of the
9295 current_source_symtab (which is decode_line_1's default).
9296 This should produce the results we want almost all of the
9297 time while leaving default_breakpoint_* alone.
9298
9299 ObjC: However, don't match an Objective-C method name which
9300 may have a '+' or '-' succeeded by a '['. */
9301 cursal = get_current_source_symtab_and_line ();
9302 if (last_displayed_sal_is_valid ())
9303 {
9304 const char *address = NULL;
9305
9306 if (event_location_type (location) == LINESPEC_LOCATION)
9307 address = get_linespec_location (location);
9308
9309 if (!cursal.symtab
9310 || (address != NULL
9311 && strchr ("+-", address[0]) != NULL
9312 && address[1] != '['))
9313 {
9314 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9315 get_last_displayed_symtab (),
9316 get_last_displayed_line (),
9317 canonical, NULL, NULL);
9318 return;
9319 }
9320 }
9321
9322 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9323 cursal.symtab, cursal.line, canonical, NULL, NULL);
9324 }
9325
9326
9327 /* Convert each SAL into a real PC. Verify that the PC can be
9328 inserted as a breakpoint. If it can't throw an error. */
9329
9330 static void
9331 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9332 {
9333 for (auto &sal : sals)
9334 resolve_sal_pc (&sal);
9335 }
9336
9337 /* Fast tracepoints may have restrictions on valid locations. For
9338 instance, a fast tracepoint using a jump instead of a trap will
9339 likely have to overwrite more bytes than a trap would, and so can
9340 only be placed where the instruction is longer than the jump, or a
9341 multi-instruction sequence does not have a jump into the middle of
9342 it, etc. */
9343
9344 static void
9345 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9346 gdb::array_view<const symtab_and_line> sals)
9347 {
9348 int rslt;
9349 char *msg;
9350 struct cleanup *old_chain;
9351
9352 for (const auto &sal : sals)
9353 {
9354 struct gdbarch *sarch;
9355
9356 sarch = get_sal_arch (sal);
9357 /* We fall back to GDBARCH if there is no architecture
9358 associated with SAL. */
9359 if (sarch == NULL)
9360 sarch = gdbarch;
9361 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
9362 old_chain = make_cleanup (xfree, msg);
9363
9364 if (!rslt)
9365 error (_("May not have a fast tracepoint at %s%s"),
9366 paddress (sarch, sal.pc), (msg ? msg : ""));
9367
9368 do_cleanups (old_chain);
9369 }
9370 }
9371
9372 /* Given TOK, a string specification of condition and thread, as
9373 accepted by the 'break' command, extract the condition
9374 string and thread number and set *COND_STRING and *THREAD.
9375 PC identifies the context at which the condition should be parsed.
9376 If no condition is found, *COND_STRING is set to NULL.
9377 If no thread is found, *THREAD is set to -1. */
9378
9379 static void
9380 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9381 char **cond_string, int *thread, int *task,
9382 char **rest)
9383 {
9384 *cond_string = NULL;
9385 *thread = -1;
9386 *task = 0;
9387 *rest = NULL;
9388
9389 while (tok && *tok)
9390 {
9391 const char *end_tok;
9392 int toklen;
9393 const char *cond_start = NULL;
9394 const char *cond_end = NULL;
9395
9396 tok = skip_spaces (tok);
9397
9398 if ((*tok == '"' || *tok == ',') && rest)
9399 {
9400 *rest = savestring (tok, strlen (tok));
9401 return;
9402 }
9403
9404 end_tok = skip_to_space (tok);
9405
9406 toklen = end_tok - tok;
9407
9408 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9409 {
9410 tok = cond_start = end_tok + 1;
9411 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9412 cond_end = tok;
9413 *cond_string = savestring (cond_start, cond_end - cond_start);
9414 }
9415 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9416 {
9417 const char *tmptok;
9418 struct thread_info *thr;
9419
9420 tok = end_tok + 1;
9421 thr = parse_thread_id (tok, &tmptok);
9422 if (tok == tmptok)
9423 error (_("Junk after thread keyword."));
9424 *thread = thr->global_num;
9425 tok = tmptok;
9426 }
9427 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9428 {
9429 char *tmptok;
9430
9431 tok = end_tok + 1;
9432 *task = strtol (tok, &tmptok, 0);
9433 if (tok == tmptok)
9434 error (_("Junk after task keyword."));
9435 if (!valid_task_id (*task))
9436 error (_("Unknown task %d."), *task);
9437 tok = tmptok;
9438 }
9439 else if (rest)
9440 {
9441 *rest = savestring (tok, strlen (tok));
9442 return;
9443 }
9444 else
9445 error (_("Junk at end of arguments."));
9446 }
9447 }
9448
9449 /* Decode a static tracepoint marker spec. */
9450
9451 static std::vector<symtab_and_line>
9452 decode_static_tracepoint_spec (const char **arg_p)
9453 {
9454 VEC(static_tracepoint_marker_p) *markers = NULL;
9455 struct cleanup *old_chain;
9456 const char *p = &(*arg_p)[3];
9457 const char *endp;
9458 char *marker_str;
9459 int i;
9460
9461 p = skip_spaces (p);
9462
9463 endp = skip_to_space (p);
9464
9465 marker_str = savestring (p, endp - p);
9466 old_chain = make_cleanup (xfree, marker_str);
9467
9468 markers = target_static_tracepoint_markers_by_strid (marker_str);
9469 if (VEC_empty(static_tracepoint_marker_p, markers))
9470 error (_("No known static tracepoint marker named %s"), marker_str);
9471
9472 std::vector<symtab_and_line> sals;
9473 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
9474
9475 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
9476 {
9477 struct static_tracepoint_marker *marker;
9478
9479 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9480
9481 symtab_and_line sal = find_pc_line (marker->address, 0);
9482 sal.pc = marker->address;
9483 sals.push_back (sal);
9484
9485 release_static_tracepoint_marker (marker);
9486 }
9487
9488 do_cleanups (old_chain);
9489
9490 *arg_p = endp;
9491 return sals;
9492 }
9493
9494 /* See breakpoint.h. */
9495
9496 int
9497 create_breakpoint (struct gdbarch *gdbarch,
9498 const struct event_location *location,
9499 const char *cond_string,
9500 int thread, const char *extra_string,
9501 int parse_extra,
9502 int tempflag, enum bptype type_wanted,
9503 int ignore_count,
9504 enum auto_boolean pending_break_support,
9505 const struct breakpoint_ops *ops,
9506 int from_tty, int enabled, int internal,
9507 unsigned flags)
9508 {
9509 struct linespec_result canonical;
9510 struct cleanup *bkpt_chain = NULL;
9511 int pending = 0;
9512 int task = 0;
9513 int prev_bkpt_count = breakpoint_count;
9514
9515 gdb_assert (ops != NULL);
9516
9517 /* If extra_string isn't useful, set it to NULL. */
9518 if (extra_string != NULL && *extra_string == '\0')
9519 extra_string = NULL;
9520
9521 TRY
9522 {
9523 ops->create_sals_from_location (location, &canonical, type_wanted);
9524 }
9525 CATCH (e, RETURN_MASK_ERROR)
9526 {
9527 /* If caller is interested in rc value from parse, set
9528 value. */
9529 if (e.error == NOT_FOUND_ERROR)
9530 {
9531 /* If pending breakpoint support is turned off, throw
9532 error. */
9533
9534 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9535 throw_exception (e);
9536
9537 exception_print (gdb_stderr, e);
9538
9539 /* If pending breakpoint support is auto query and the user
9540 selects no, then simply return the error code. */
9541 if (pending_break_support == AUTO_BOOLEAN_AUTO
9542 && !nquery (_("Make %s pending on future shared library load? "),
9543 bptype_string (type_wanted)))
9544 return 0;
9545
9546 /* At this point, either the user was queried about setting
9547 a pending breakpoint and selected yes, or pending
9548 breakpoint behavior is on and thus a pending breakpoint
9549 is defaulted on behalf of the user. */
9550 pending = 1;
9551 }
9552 else
9553 throw_exception (e);
9554 }
9555 END_CATCH
9556
9557 if (!pending && canonical.lsals.empty ())
9558 return 0;
9559
9560 /* ----------------------------- SNIP -----------------------------
9561 Anything added to the cleanup chain beyond this point is assumed
9562 to be part of a breakpoint. If the breakpoint create succeeds
9563 then the memory is not reclaimed. */
9564 bkpt_chain = make_cleanup (null_cleanup, 0);
9565
9566 /* Resolve all line numbers to PC's and verify that the addresses
9567 are ok for the target. */
9568 if (!pending)
9569 {
9570 for (auto &lsal : canonical.lsals)
9571 breakpoint_sals_to_pc (lsal.sals);
9572 }
9573
9574 /* Fast tracepoints may have additional restrictions on location. */
9575 if (!pending && type_wanted == bp_fast_tracepoint)
9576 {
9577 for (const auto &lsal : canonical.lsals)
9578 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9579 }
9580
9581 /* Verify that condition can be parsed, before setting any
9582 breakpoints. Allocate a separate condition expression for each
9583 breakpoint. */
9584 if (!pending)
9585 {
9586 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9587 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9588
9589 if (parse_extra)
9590 {
9591 char *rest;
9592 char *cond;
9593
9594 const linespec_sals &lsal = canonical.lsals[0];
9595
9596 /* Here we only parse 'arg' to separate condition
9597 from thread number, so parsing in context of first
9598 sal is OK. When setting the breakpoint we'll
9599 re-parse it in context of each sal. */
9600
9601 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9602 &cond, &thread, &task, &rest);
9603 cond_string_copy.reset (cond);
9604 extra_string_copy.reset (rest);
9605 }
9606 else
9607 {
9608 if (type_wanted != bp_dprintf
9609 && extra_string != NULL && *extra_string != '\0')
9610 error (_("Garbage '%s' at end of location"), extra_string);
9611
9612 /* Create a private copy of condition string. */
9613 if (cond_string)
9614 cond_string_copy.reset (xstrdup (cond_string));
9615 /* Create a private copy of any extra string. */
9616 if (extra_string)
9617 extra_string_copy.reset (xstrdup (extra_string));
9618 }
9619
9620 ops->create_breakpoints_sal (gdbarch, &canonical,
9621 std::move (cond_string_copy),
9622 std::move (extra_string_copy),
9623 type_wanted,
9624 tempflag ? disp_del : disp_donttouch,
9625 thread, task, ignore_count, ops,
9626 from_tty, enabled, internal, flags);
9627 }
9628 else
9629 {
9630 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9631
9632 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9633 b->location = copy_event_location (location);
9634
9635 if (parse_extra)
9636 b->cond_string = NULL;
9637 else
9638 {
9639 /* Create a private copy of condition string. */
9640 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9641 b->thread = thread;
9642 }
9643
9644 /* Create a private copy of any extra string. */
9645 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9646 b->ignore_count = ignore_count;
9647 b->disposition = tempflag ? disp_del : disp_donttouch;
9648 b->condition_not_parsed = 1;
9649 b->enable_state = enabled ? bp_enabled : bp_disabled;
9650 if ((type_wanted != bp_breakpoint
9651 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9652 b->pspace = current_program_space;
9653
9654 install_breakpoint (internal, std::move (b), 0);
9655 }
9656
9657 if (canonical.lsals.size () > 1)
9658 {
9659 warning (_("Multiple breakpoints were set.\nUse the "
9660 "\"delete\" command to delete unwanted breakpoints."));
9661 prev_breakpoint_count = prev_bkpt_count;
9662 }
9663
9664 /* That's it. Discard the cleanups for data inserted into the
9665 breakpoint. */
9666 discard_cleanups (bkpt_chain);
9667
9668 /* error call may happen here - have BKPT_CHAIN already discarded. */
9669 update_global_location_list (UGLL_MAY_INSERT);
9670
9671 return 1;
9672 }
9673
9674 /* Set a breakpoint.
9675 ARG is a string describing breakpoint address,
9676 condition, and thread.
9677 FLAG specifies if a breakpoint is hardware on,
9678 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9679 and BP_TEMPFLAG. */
9680
9681 static void
9682 break_command_1 (char *arg, int flag, int from_tty)
9683 {
9684 int tempflag = flag & BP_TEMPFLAG;
9685 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9686 ? bp_hardware_breakpoint
9687 : bp_breakpoint);
9688 struct breakpoint_ops *ops;
9689
9690 event_location_up location = string_to_event_location (&arg, current_language);
9691
9692 /* Matching breakpoints on probes. */
9693 if (location != NULL
9694 && event_location_type (location.get ()) == PROBE_LOCATION)
9695 ops = &bkpt_probe_breakpoint_ops;
9696 else
9697 ops = &bkpt_breakpoint_ops;
9698
9699 create_breakpoint (get_current_arch (),
9700 location.get (),
9701 NULL, 0, arg, 1 /* parse arg */,
9702 tempflag, type_wanted,
9703 0 /* Ignore count */,
9704 pending_break_support,
9705 ops,
9706 from_tty,
9707 1 /* enabled */,
9708 0 /* internal */,
9709 0);
9710 }
9711
9712 /* Helper function for break_command_1 and disassemble_command. */
9713
9714 void
9715 resolve_sal_pc (struct symtab_and_line *sal)
9716 {
9717 CORE_ADDR pc;
9718
9719 if (sal->pc == 0 && sal->symtab != NULL)
9720 {
9721 if (!find_line_pc (sal->symtab, sal->line, &pc))
9722 error (_("No line %d in file \"%s\"."),
9723 sal->line, symtab_to_filename_for_display (sal->symtab));
9724 sal->pc = pc;
9725
9726 /* If this SAL corresponds to a breakpoint inserted using a line
9727 number, then skip the function prologue if necessary. */
9728 if (sal->explicit_line)
9729 skip_prologue_sal (sal);
9730 }
9731
9732 if (sal->section == 0 && sal->symtab != NULL)
9733 {
9734 const struct blockvector *bv;
9735 const struct block *b;
9736 struct symbol *sym;
9737
9738 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9739 SYMTAB_COMPUNIT (sal->symtab));
9740 if (bv != NULL)
9741 {
9742 sym = block_linkage_function (b);
9743 if (sym != NULL)
9744 {
9745 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9746 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9747 sym);
9748 }
9749 else
9750 {
9751 /* It really is worthwhile to have the section, so we'll
9752 just have to look harder. This case can be executed
9753 if we have line numbers but no functions (as can
9754 happen in assembly source). */
9755
9756 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9757 switch_to_program_space_and_thread (sal->pspace);
9758
9759 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9760 if (msym.minsym)
9761 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9762 }
9763 }
9764 }
9765 }
9766
9767 void
9768 break_command (char *arg, int from_tty)
9769 {
9770 break_command_1 (arg, 0, from_tty);
9771 }
9772
9773 void
9774 tbreak_command (char *arg, int from_tty)
9775 {
9776 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9777 }
9778
9779 static void
9780 hbreak_command (char *arg, int from_tty)
9781 {
9782 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9783 }
9784
9785 static void
9786 thbreak_command (char *arg, int from_tty)
9787 {
9788 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9789 }
9790
9791 static void
9792 stop_command (char *arg, int from_tty)
9793 {
9794 printf_filtered (_("Specify the type of breakpoint to set.\n\
9795 Usage: stop in <function | address>\n\
9796 stop at <line>\n"));
9797 }
9798
9799 static void
9800 stopin_command (char *arg, int from_tty)
9801 {
9802 int badInput = 0;
9803
9804 if (arg == (char *) NULL)
9805 badInput = 1;
9806 else if (*arg != '*')
9807 {
9808 char *argptr = arg;
9809 int hasColon = 0;
9810
9811 /* Look for a ':'. If this is a line number specification, then
9812 say it is bad, otherwise, it should be an address or
9813 function/method name. */
9814 while (*argptr && !hasColon)
9815 {
9816 hasColon = (*argptr == ':');
9817 argptr++;
9818 }
9819
9820 if (hasColon)
9821 badInput = (*argptr != ':'); /* Not a class::method */
9822 else
9823 badInput = isdigit (*arg); /* a simple line number */
9824 }
9825
9826 if (badInput)
9827 printf_filtered (_("Usage: stop in <function | address>\n"));
9828 else
9829 break_command_1 (arg, 0, from_tty);
9830 }
9831
9832 static void
9833 stopat_command (char *arg, int from_tty)
9834 {
9835 int badInput = 0;
9836
9837 if (arg == (char *) NULL || *arg == '*') /* no line number */
9838 badInput = 1;
9839 else
9840 {
9841 char *argptr = arg;
9842 int hasColon = 0;
9843
9844 /* Look for a ':'. If there is a '::' then get out, otherwise
9845 it is probably a line number. */
9846 while (*argptr && !hasColon)
9847 {
9848 hasColon = (*argptr == ':');
9849 argptr++;
9850 }
9851
9852 if (hasColon)
9853 badInput = (*argptr == ':'); /* we have class::method */
9854 else
9855 badInput = !isdigit (*arg); /* not a line number */
9856 }
9857
9858 if (badInput)
9859 printf_filtered (_("Usage: stop at <line>\n"));
9860 else
9861 break_command_1 (arg, 0, from_tty);
9862 }
9863
9864 /* The dynamic printf command is mostly like a regular breakpoint, but
9865 with a prewired command list consisting of a single output command,
9866 built from extra arguments supplied on the dprintf command
9867 line. */
9868
9869 static void
9870 dprintf_command (char *arg, int from_tty)
9871 {
9872 event_location_up location = string_to_event_location (&arg, current_language);
9873
9874 /* If non-NULL, ARG should have been advanced past the location;
9875 the next character must be ','. */
9876 if (arg != NULL)
9877 {
9878 if (arg[0] != ',' || arg[1] == '\0')
9879 error (_("Format string required"));
9880 else
9881 {
9882 /* Skip the comma. */
9883 ++arg;
9884 }
9885 }
9886
9887 create_breakpoint (get_current_arch (),
9888 location.get (),
9889 NULL, 0, arg, 1 /* parse arg */,
9890 0, bp_dprintf,
9891 0 /* Ignore count */,
9892 pending_break_support,
9893 &dprintf_breakpoint_ops,
9894 from_tty,
9895 1 /* enabled */,
9896 0 /* internal */,
9897 0);
9898 }
9899
9900 static void
9901 agent_printf_command (char *arg, int from_tty)
9902 {
9903 error (_("May only run agent-printf on the target"));
9904 }
9905
9906 /* Implement the "breakpoint_hit" breakpoint_ops method for
9907 ranged breakpoints. */
9908
9909 static int
9910 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9911 struct address_space *aspace,
9912 CORE_ADDR bp_addr,
9913 const struct target_waitstatus *ws)
9914 {
9915 if (ws->kind != TARGET_WAITKIND_STOPPED
9916 || ws->value.sig != GDB_SIGNAL_TRAP)
9917 return 0;
9918
9919 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9920 bl->length, aspace, bp_addr);
9921 }
9922
9923 /* Implement the "resources_needed" breakpoint_ops method for
9924 ranged breakpoints. */
9925
9926 static int
9927 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9928 {
9929 return target_ranged_break_num_registers ();
9930 }
9931
9932 /* Implement the "print_it" breakpoint_ops method for
9933 ranged breakpoints. */
9934
9935 static enum print_stop_action
9936 print_it_ranged_breakpoint (bpstat bs)
9937 {
9938 struct breakpoint *b = bs->breakpoint_at;
9939 struct bp_location *bl = b->loc;
9940 struct ui_out *uiout = current_uiout;
9941
9942 gdb_assert (b->type == bp_hardware_breakpoint);
9943
9944 /* Ranged breakpoints have only one location. */
9945 gdb_assert (bl && bl->next == NULL);
9946
9947 annotate_breakpoint (b->number);
9948
9949 maybe_print_thread_hit_breakpoint (uiout);
9950
9951 if (b->disposition == disp_del)
9952 uiout->text ("Temporary ranged breakpoint ");
9953 else
9954 uiout->text ("Ranged breakpoint ");
9955 if (uiout->is_mi_like_p ())
9956 {
9957 uiout->field_string ("reason",
9958 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9959 uiout->field_string ("disp", bpdisp_text (b->disposition));
9960 }
9961 uiout->field_int ("bkptno", b->number);
9962 uiout->text (", ");
9963
9964 return PRINT_SRC_AND_LOC;
9965 }
9966
9967 /* Implement the "print_one" breakpoint_ops method for
9968 ranged breakpoints. */
9969
9970 static void
9971 print_one_ranged_breakpoint (struct breakpoint *b,
9972 struct bp_location **last_loc)
9973 {
9974 struct bp_location *bl = b->loc;
9975 struct value_print_options opts;
9976 struct ui_out *uiout = current_uiout;
9977
9978 /* Ranged breakpoints have only one location. */
9979 gdb_assert (bl && bl->next == NULL);
9980
9981 get_user_print_options (&opts);
9982
9983 if (opts.addressprint)
9984 /* We don't print the address range here, it will be printed later
9985 by print_one_detail_ranged_breakpoint. */
9986 uiout->field_skip ("addr");
9987 annotate_field (5);
9988 print_breakpoint_location (b, bl);
9989 *last_loc = bl;
9990 }
9991
9992 /* Implement the "print_one_detail" breakpoint_ops method for
9993 ranged breakpoints. */
9994
9995 static void
9996 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9997 struct ui_out *uiout)
9998 {
9999 CORE_ADDR address_start, address_end;
10000 struct bp_location *bl = b->loc;
10001 string_file stb;
10002
10003 gdb_assert (bl);
10004
10005 address_start = bl->address;
10006 address_end = address_start + bl->length - 1;
10007
10008 uiout->text ("\taddress range: ");
10009 stb.printf ("[%s, %s]",
10010 print_core_address (bl->gdbarch, address_start),
10011 print_core_address (bl->gdbarch, address_end));
10012 uiout->field_stream ("addr", stb);
10013 uiout->text ("\n");
10014 }
10015
10016 /* Implement the "print_mention" breakpoint_ops method for
10017 ranged breakpoints. */
10018
10019 static void
10020 print_mention_ranged_breakpoint (struct breakpoint *b)
10021 {
10022 struct bp_location *bl = b->loc;
10023 struct ui_out *uiout = current_uiout;
10024
10025 gdb_assert (bl);
10026 gdb_assert (b->type == bp_hardware_breakpoint);
10027
10028 if (uiout->is_mi_like_p ())
10029 return;
10030
10031 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10032 b->number, paddress (bl->gdbarch, bl->address),
10033 paddress (bl->gdbarch, bl->address + bl->length - 1));
10034 }
10035
10036 /* Implement the "print_recreate" breakpoint_ops method for
10037 ranged breakpoints. */
10038
10039 static void
10040 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10041 {
10042 fprintf_unfiltered (fp, "break-range %s, %s",
10043 event_location_to_string (b->location.get ()),
10044 event_location_to_string (b->location_range_end.get ()));
10045 print_recreate_thread (b, fp);
10046 }
10047
10048 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10049
10050 static struct breakpoint_ops ranged_breakpoint_ops;
10051
10052 /* Find the address where the end of the breakpoint range should be
10053 placed, given the SAL of the end of the range. This is so that if
10054 the user provides a line number, the end of the range is set to the
10055 last instruction of the given line. */
10056
10057 static CORE_ADDR
10058 find_breakpoint_range_end (struct symtab_and_line sal)
10059 {
10060 CORE_ADDR end;
10061
10062 /* If the user provided a PC value, use it. Otherwise,
10063 find the address of the end of the given location. */
10064 if (sal.explicit_pc)
10065 end = sal.pc;
10066 else
10067 {
10068 int ret;
10069 CORE_ADDR start;
10070
10071 ret = find_line_pc_range (sal, &start, &end);
10072 if (!ret)
10073 error (_("Could not find location of the end of the range."));
10074
10075 /* find_line_pc_range returns the start of the next line. */
10076 end--;
10077 }
10078
10079 return end;
10080 }
10081
10082 /* Implement the "break-range" CLI command. */
10083
10084 static void
10085 break_range_command (char *arg, int from_tty)
10086 {
10087 char *arg_start, *addr_string_start;
10088 struct linespec_result canonical_start, canonical_end;
10089 int bp_count, can_use_bp, length;
10090 CORE_ADDR end;
10091 struct breakpoint *b;
10092 struct cleanup *cleanup_bkpt;
10093
10094 /* We don't support software ranged breakpoints. */
10095 if (target_ranged_break_num_registers () < 0)
10096 error (_("This target does not support hardware ranged breakpoints."));
10097
10098 bp_count = hw_breakpoint_used_count ();
10099 bp_count += target_ranged_break_num_registers ();
10100 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10101 bp_count, 0);
10102 if (can_use_bp < 0)
10103 error (_("Hardware breakpoints used exceeds limit."));
10104
10105 arg = skip_spaces (arg);
10106 if (arg == NULL || arg[0] == '\0')
10107 error(_("No address range specified."));
10108
10109 arg_start = arg;
10110 event_location_up start_location = string_to_event_location (&arg,
10111 current_language);
10112 parse_breakpoint_sals (start_location.get (), &canonical_start);
10113
10114 if (arg[0] != ',')
10115 error (_("Too few arguments."));
10116 else if (canonical_start.lsals.empty ())
10117 error (_("Could not find location of the beginning of the range."));
10118
10119 const linespec_sals &lsal_start = canonical_start.lsals[0];
10120
10121 if (canonical_start.lsals.size () > 1
10122 || lsal_start.sals.size () != 1)
10123 error (_("Cannot create a ranged breakpoint with multiple locations."));
10124
10125 const symtab_and_line &sal_start = lsal_start.sals[0];
10126 addr_string_start = savestring (arg_start, arg - arg_start);
10127 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
10128
10129 arg++; /* Skip the comma. */
10130 arg = skip_spaces (arg);
10131
10132 /* Parse the end location. */
10133
10134 arg_start = arg;
10135
10136 /* We call decode_line_full directly here instead of using
10137 parse_breakpoint_sals because we need to specify the start location's
10138 symtab and line as the default symtab and line for the end of the
10139 range. This makes it possible to have ranges like "foo.c:27, +14",
10140 where +14 means 14 lines from the start location. */
10141 event_location_up end_location = string_to_event_location (&arg,
10142 current_language);
10143 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10144 sal_start.symtab, sal_start.line,
10145 &canonical_end, NULL, NULL);
10146
10147 if (canonical_end.lsals.empty ())
10148 error (_("Could not find location of the end of the range."));
10149
10150 const linespec_sals &lsal_end = canonical_end.lsals[0];
10151 if (canonical_end.lsals.size () > 1
10152 || lsal_end.sals.size () != 1)
10153 error (_("Cannot create a ranged breakpoint with multiple locations."));
10154
10155 const symtab_and_line &sal_end = lsal_end.sals[0];
10156
10157 end = find_breakpoint_range_end (sal_end);
10158 if (sal_start.pc > end)
10159 error (_("Invalid address range, end precedes start."));
10160
10161 length = end - sal_start.pc + 1;
10162 if (length < 0)
10163 /* Length overflowed. */
10164 error (_("Address range too large."));
10165 else if (length == 1)
10166 {
10167 /* This range is simple enough to be handled by
10168 the `hbreak' command. */
10169 hbreak_command (addr_string_start, 1);
10170
10171 do_cleanups (cleanup_bkpt);
10172
10173 return;
10174 }
10175
10176 /* Now set up the breakpoint. */
10177 b = set_raw_breakpoint (get_current_arch (), sal_start,
10178 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10179 set_breakpoint_count (breakpoint_count + 1);
10180 b->number = breakpoint_count;
10181 b->disposition = disp_donttouch;
10182 b->location = std::move (start_location);
10183 b->location_range_end = std::move (end_location);
10184 b->loc->length = length;
10185
10186 do_cleanups (cleanup_bkpt);
10187
10188 mention (b);
10189 observer_notify_breakpoint_created (b);
10190 update_global_location_list (UGLL_MAY_INSERT);
10191 }
10192
10193 /* Return non-zero if EXP is verified as constant. Returned zero
10194 means EXP is variable. Also the constant detection may fail for
10195 some constant expressions and in such case still falsely return
10196 zero. */
10197
10198 static int
10199 watchpoint_exp_is_const (const struct expression *exp)
10200 {
10201 int i = exp->nelts;
10202
10203 while (i > 0)
10204 {
10205 int oplenp, argsp;
10206
10207 /* We are only interested in the descriptor of each element. */
10208 operator_length (exp, i, &oplenp, &argsp);
10209 i -= oplenp;
10210
10211 switch (exp->elts[i].opcode)
10212 {
10213 case BINOP_ADD:
10214 case BINOP_SUB:
10215 case BINOP_MUL:
10216 case BINOP_DIV:
10217 case BINOP_REM:
10218 case BINOP_MOD:
10219 case BINOP_LSH:
10220 case BINOP_RSH:
10221 case BINOP_LOGICAL_AND:
10222 case BINOP_LOGICAL_OR:
10223 case BINOP_BITWISE_AND:
10224 case BINOP_BITWISE_IOR:
10225 case BINOP_BITWISE_XOR:
10226 case BINOP_EQUAL:
10227 case BINOP_NOTEQUAL:
10228 case BINOP_LESS:
10229 case BINOP_GTR:
10230 case BINOP_LEQ:
10231 case BINOP_GEQ:
10232 case BINOP_REPEAT:
10233 case BINOP_COMMA:
10234 case BINOP_EXP:
10235 case BINOP_MIN:
10236 case BINOP_MAX:
10237 case BINOP_INTDIV:
10238 case BINOP_CONCAT:
10239 case TERNOP_COND:
10240 case TERNOP_SLICE:
10241
10242 case OP_LONG:
10243 case OP_DOUBLE:
10244 case OP_DECFLOAT:
10245 case OP_LAST:
10246 case OP_COMPLEX:
10247 case OP_STRING:
10248 case OP_ARRAY:
10249 case OP_TYPE:
10250 case OP_TYPEOF:
10251 case OP_DECLTYPE:
10252 case OP_TYPEID:
10253 case OP_NAME:
10254 case OP_OBJC_NSSTRING:
10255
10256 case UNOP_NEG:
10257 case UNOP_LOGICAL_NOT:
10258 case UNOP_COMPLEMENT:
10259 case UNOP_ADDR:
10260 case UNOP_HIGH:
10261 case UNOP_CAST:
10262
10263 case UNOP_CAST_TYPE:
10264 case UNOP_REINTERPRET_CAST:
10265 case UNOP_DYNAMIC_CAST:
10266 /* Unary, binary and ternary operators: We have to check
10267 their operands. If they are constant, then so is the
10268 result of that operation. For instance, if A and B are
10269 determined to be constants, then so is "A + B".
10270
10271 UNOP_IND is one exception to the rule above, because the
10272 value of *ADDR is not necessarily a constant, even when
10273 ADDR is. */
10274 break;
10275
10276 case OP_VAR_VALUE:
10277 /* Check whether the associated symbol is a constant.
10278
10279 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10280 possible that a buggy compiler could mark a variable as
10281 constant even when it is not, and TYPE_CONST would return
10282 true in this case, while SYMBOL_CLASS wouldn't.
10283
10284 We also have to check for function symbols because they
10285 are always constant. */
10286 {
10287 struct symbol *s = exp->elts[i + 2].symbol;
10288
10289 if (SYMBOL_CLASS (s) != LOC_BLOCK
10290 && SYMBOL_CLASS (s) != LOC_CONST
10291 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10292 return 0;
10293 break;
10294 }
10295
10296 /* The default action is to return 0 because we are using
10297 the optimistic approach here: If we don't know something,
10298 then it is not a constant. */
10299 default:
10300 return 0;
10301 }
10302 }
10303
10304 return 1;
10305 }
10306
10307 /* Watchpoint destructor. */
10308
10309 watchpoint::~watchpoint ()
10310 {
10311 xfree (this->exp_string);
10312 xfree (this->exp_string_reparse);
10313 value_free (this->val);
10314 }
10315
10316 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10317
10318 static void
10319 re_set_watchpoint (struct breakpoint *b)
10320 {
10321 struct watchpoint *w = (struct watchpoint *) b;
10322
10323 /* Watchpoint can be either on expression using entirely global
10324 variables, or it can be on local variables.
10325
10326 Watchpoints of the first kind are never auto-deleted, and even
10327 persist across program restarts. Since they can use variables
10328 from shared libraries, we need to reparse expression as libraries
10329 are loaded and unloaded.
10330
10331 Watchpoints on local variables can also change meaning as result
10332 of solib event. For example, if a watchpoint uses both a local
10333 and a global variables in expression, it's a local watchpoint,
10334 but unloading of a shared library will make the expression
10335 invalid. This is not a very common use case, but we still
10336 re-evaluate expression, to avoid surprises to the user.
10337
10338 Note that for local watchpoints, we re-evaluate it only if
10339 watchpoints frame id is still valid. If it's not, it means the
10340 watchpoint is out of scope and will be deleted soon. In fact,
10341 I'm not sure we'll ever be called in this case.
10342
10343 If a local watchpoint's frame id is still valid, then
10344 w->exp_valid_block is likewise valid, and we can safely use it.
10345
10346 Don't do anything about disabled watchpoints, since they will be
10347 reevaluated again when enabled. */
10348 update_watchpoint (w, 1 /* reparse */);
10349 }
10350
10351 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10352
10353 static int
10354 insert_watchpoint (struct bp_location *bl)
10355 {
10356 struct watchpoint *w = (struct watchpoint *) bl->owner;
10357 int length = w->exact ? 1 : bl->length;
10358
10359 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10360 w->cond_exp.get ());
10361 }
10362
10363 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10364
10365 static int
10366 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10367 {
10368 struct watchpoint *w = (struct watchpoint *) bl->owner;
10369 int length = w->exact ? 1 : bl->length;
10370
10371 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10372 w->cond_exp.get ());
10373 }
10374
10375 static int
10376 breakpoint_hit_watchpoint (const struct bp_location *bl,
10377 struct address_space *aspace, CORE_ADDR bp_addr,
10378 const struct target_waitstatus *ws)
10379 {
10380 struct breakpoint *b = bl->owner;
10381 struct watchpoint *w = (struct watchpoint *) b;
10382
10383 /* Continuable hardware watchpoints are treated as non-existent if the
10384 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10385 some data address). Otherwise gdb won't stop on a break instruction
10386 in the code (not from a breakpoint) when a hardware watchpoint has
10387 been defined. Also skip watchpoints which we know did not trigger
10388 (did not match the data address). */
10389 if (is_hardware_watchpoint (b)
10390 && w->watchpoint_triggered == watch_triggered_no)
10391 return 0;
10392
10393 return 1;
10394 }
10395
10396 static void
10397 check_status_watchpoint (bpstat bs)
10398 {
10399 gdb_assert (is_watchpoint (bs->breakpoint_at));
10400
10401 bpstat_check_watchpoint (bs);
10402 }
10403
10404 /* Implement the "resources_needed" breakpoint_ops method for
10405 hardware watchpoints. */
10406
10407 static int
10408 resources_needed_watchpoint (const struct bp_location *bl)
10409 {
10410 struct watchpoint *w = (struct watchpoint *) bl->owner;
10411 int length = w->exact? 1 : bl->length;
10412
10413 return target_region_ok_for_hw_watchpoint (bl->address, length);
10414 }
10415
10416 /* Implement the "works_in_software_mode" breakpoint_ops method for
10417 hardware watchpoints. */
10418
10419 static int
10420 works_in_software_mode_watchpoint (const struct breakpoint *b)
10421 {
10422 /* Read and access watchpoints only work with hardware support. */
10423 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10424 }
10425
10426 static enum print_stop_action
10427 print_it_watchpoint (bpstat bs)
10428 {
10429 struct breakpoint *b;
10430 enum print_stop_action result;
10431 struct watchpoint *w;
10432 struct ui_out *uiout = current_uiout;
10433
10434 gdb_assert (bs->bp_location_at != NULL);
10435
10436 b = bs->breakpoint_at;
10437 w = (struct watchpoint *) b;
10438
10439 annotate_watchpoint (b->number);
10440 maybe_print_thread_hit_breakpoint (uiout);
10441
10442 string_file stb;
10443
10444 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10445 switch (b->type)
10446 {
10447 case bp_watchpoint:
10448 case bp_hardware_watchpoint:
10449 if (uiout->is_mi_like_p ())
10450 uiout->field_string
10451 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10452 mention (b);
10453 tuple_emitter.emplace (uiout, "value");
10454 uiout->text ("\nOld value = ");
10455 watchpoint_value_print (bs->old_val, &stb);
10456 uiout->field_stream ("old", stb);
10457 uiout->text ("\nNew value = ");
10458 watchpoint_value_print (w->val, &stb);
10459 uiout->field_stream ("new", stb);
10460 uiout->text ("\n");
10461 /* More than one watchpoint may have been triggered. */
10462 result = PRINT_UNKNOWN;
10463 break;
10464
10465 case bp_read_watchpoint:
10466 if (uiout->is_mi_like_p ())
10467 uiout->field_string
10468 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10469 mention (b);
10470 tuple_emitter.emplace (uiout, "value");
10471 uiout->text ("\nValue = ");
10472 watchpoint_value_print (w->val, &stb);
10473 uiout->field_stream ("value", stb);
10474 uiout->text ("\n");
10475 result = PRINT_UNKNOWN;
10476 break;
10477
10478 case bp_access_watchpoint:
10479 if (bs->old_val != NULL)
10480 {
10481 if (uiout->is_mi_like_p ())
10482 uiout->field_string
10483 ("reason",
10484 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10485 mention (b);
10486 tuple_emitter.emplace (uiout, "value");
10487 uiout->text ("\nOld value = ");
10488 watchpoint_value_print (bs->old_val, &stb);
10489 uiout->field_stream ("old", stb);
10490 uiout->text ("\nNew value = ");
10491 }
10492 else
10493 {
10494 mention (b);
10495 if (uiout->is_mi_like_p ())
10496 uiout->field_string
10497 ("reason",
10498 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10499 tuple_emitter.emplace (uiout, "value");
10500 uiout->text ("\nValue = ");
10501 }
10502 watchpoint_value_print (w->val, &stb);
10503 uiout->field_stream ("new", stb);
10504 uiout->text ("\n");
10505 result = PRINT_UNKNOWN;
10506 break;
10507 default:
10508 result = PRINT_UNKNOWN;
10509 }
10510
10511 return result;
10512 }
10513
10514 /* Implement the "print_mention" breakpoint_ops method for hardware
10515 watchpoints. */
10516
10517 static void
10518 print_mention_watchpoint (struct breakpoint *b)
10519 {
10520 struct watchpoint *w = (struct watchpoint *) b;
10521 struct ui_out *uiout = current_uiout;
10522 const char *tuple_name;
10523
10524 switch (b->type)
10525 {
10526 case bp_watchpoint:
10527 uiout->text ("Watchpoint ");
10528 tuple_name = "wpt";
10529 break;
10530 case bp_hardware_watchpoint:
10531 uiout->text ("Hardware watchpoint ");
10532 tuple_name = "wpt";
10533 break;
10534 case bp_read_watchpoint:
10535 uiout->text ("Hardware read watchpoint ");
10536 tuple_name = "hw-rwpt";
10537 break;
10538 case bp_access_watchpoint:
10539 uiout->text ("Hardware access (read/write) watchpoint ");
10540 tuple_name = "hw-awpt";
10541 break;
10542 default:
10543 internal_error (__FILE__, __LINE__,
10544 _("Invalid hardware watchpoint type."));
10545 }
10546
10547 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10548 uiout->field_int ("number", b->number);
10549 uiout->text (": ");
10550 uiout->field_string ("exp", w->exp_string);
10551 }
10552
10553 /* Implement the "print_recreate" breakpoint_ops method for
10554 watchpoints. */
10555
10556 static void
10557 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10558 {
10559 struct watchpoint *w = (struct watchpoint *) b;
10560
10561 switch (b->type)
10562 {
10563 case bp_watchpoint:
10564 case bp_hardware_watchpoint:
10565 fprintf_unfiltered (fp, "watch");
10566 break;
10567 case bp_read_watchpoint:
10568 fprintf_unfiltered (fp, "rwatch");
10569 break;
10570 case bp_access_watchpoint:
10571 fprintf_unfiltered (fp, "awatch");
10572 break;
10573 default:
10574 internal_error (__FILE__, __LINE__,
10575 _("Invalid watchpoint type."));
10576 }
10577
10578 fprintf_unfiltered (fp, " %s", w->exp_string);
10579 print_recreate_thread (b, fp);
10580 }
10581
10582 /* Implement the "explains_signal" breakpoint_ops method for
10583 watchpoints. */
10584
10585 static int
10586 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10587 {
10588 /* A software watchpoint cannot cause a signal other than
10589 GDB_SIGNAL_TRAP. */
10590 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10591 return 0;
10592
10593 return 1;
10594 }
10595
10596 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10597
10598 static struct breakpoint_ops watchpoint_breakpoint_ops;
10599
10600 /* Implement the "insert" breakpoint_ops method for
10601 masked hardware watchpoints. */
10602
10603 static int
10604 insert_masked_watchpoint (struct bp_location *bl)
10605 {
10606 struct watchpoint *w = (struct watchpoint *) bl->owner;
10607
10608 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10609 bl->watchpoint_type);
10610 }
10611
10612 /* Implement the "remove" breakpoint_ops method for
10613 masked hardware watchpoints. */
10614
10615 static int
10616 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10617 {
10618 struct watchpoint *w = (struct watchpoint *) bl->owner;
10619
10620 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10621 bl->watchpoint_type);
10622 }
10623
10624 /* Implement the "resources_needed" breakpoint_ops method for
10625 masked hardware watchpoints. */
10626
10627 static int
10628 resources_needed_masked_watchpoint (const struct bp_location *bl)
10629 {
10630 struct watchpoint *w = (struct watchpoint *) bl->owner;
10631
10632 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10633 }
10634
10635 /* Implement the "works_in_software_mode" breakpoint_ops method for
10636 masked hardware watchpoints. */
10637
10638 static int
10639 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10640 {
10641 return 0;
10642 }
10643
10644 /* Implement the "print_it" breakpoint_ops method for
10645 masked hardware watchpoints. */
10646
10647 static enum print_stop_action
10648 print_it_masked_watchpoint (bpstat bs)
10649 {
10650 struct breakpoint *b = bs->breakpoint_at;
10651 struct ui_out *uiout = current_uiout;
10652
10653 /* Masked watchpoints have only one location. */
10654 gdb_assert (b->loc && b->loc->next == NULL);
10655
10656 annotate_watchpoint (b->number);
10657 maybe_print_thread_hit_breakpoint (uiout);
10658
10659 switch (b->type)
10660 {
10661 case bp_hardware_watchpoint:
10662 if (uiout->is_mi_like_p ())
10663 uiout->field_string
10664 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10665 break;
10666
10667 case bp_read_watchpoint:
10668 if (uiout->is_mi_like_p ())
10669 uiout->field_string
10670 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10671 break;
10672
10673 case bp_access_watchpoint:
10674 if (uiout->is_mi_like_p ())
10675 uiout->field_string
10676 ("reason",
10677 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10678 break;
10679 default:
10680 internal_error (__FILE__, __LINE__,
10681 _("Invalid hardware watchpoint type."));
10682 }
10683
10684 mention (b);
10685 uiout->text (_("\n\
10686 Check the underlying instruction at PC for the memory\n\
10687 address and value which triggered this watchpoint.\n"));
10688 uiout->text ("\n");
10689
10690 /* More than one watchpoint may have been triggered. */
10691 return PRINT_UNKNOWN;
10692 }
10693
10694 /* Implement the "print_one_detail" breakpoint_ops method for
10695 masked hardware watchpoints. */
10696
10697 static void
10698 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10699 struct ui_out *uiout)
10700 {
10701 struct watchpoint *w = (struct watchpoint *) b;
10702
10703 /* Masked watchpoints have only one location. */
10704 gdb_assert (b->loc && b->loc->next == NULL);
10705
10706 uiout->text ("\tmask ");
10707 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10708 uiout->text ("\n");
10709 }
10710
10711 /* Implement the "print_mention" breakpoint_ops method for
10712 masked hardware watchpoints. */
10713
10714 static void
10715 print_mention_masked_watchpoint (struct breakpoint *b)
10716 {
10717 struct watchpoint *w = (struct watchpoint *) b;
10718 struct ui_out *uiout = current_uiout;
10719 const char *tuple_name;
10720
10721 switch (b->type)
10722 {
10723 case bp_hardware_watchpoint:
10724 uiout->text ("Masked hardware watchpoint ");
10725 tuple_name = "wpt";
10726 break;
10727 case bp_read_watchpoint:
10728 uiout->text ("Masked hardware read watchpoint ");
10729 tuple_name = "hw-rwpt";
10730 break;
10731 case bp_access_watchpoint:
10732 uiout->text ("Masked hardware access (read/write) watchpoint ");
10733 tuple_name = "hw-awpt";
10734 break;
10735 default:
10736 internal_error (__FILE__, __LINE__,
10737 _("Invalid hardware watchpoint type."));
10738 }
10739
10740 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10741 uiout->field_int ("number", b->number);
10742 uiout->text (": ");
10743 uiout->field_string ("exp", w->exp_string);
10744 }
10745
10746 /* Implement the "print_recreate" breakpoint_ops method for
10747 masked hardware watchpoints. */
10748
10749 static void
10750 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10751 {
10752 struct watchpoint *w = (struct watchpoint *) b;
10753 char tmp[40];
10754
10755 switch (b->type)
10756 {
10757 case bp_hardware_watchpoint:
10758 fprintf_unfiltered (fp, "watch");
10759 break;
10760 case bp_read_watchpoint:
10761 fprintf_unfiltered (fp, "rwatch");
10762 break;
10763 case bp_access_watchpoint:
10764 fprintf_unfiltered (fp, "awatch");
10765 break;
10766 default:
10767 internal_error (__FILE__, __LINE__,
10768 _("Invalid hardware watchpoint type."));
10769 }
10770
10771 sprintf_vma (tmp, w->hw_wp_mask);
10772 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10773 print_recreate_thread (b, fp);
10774 }
10775
10776 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10777
10778 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10779
10780 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10781
10782 static int
10783 is_masked_watchpoint (const struct breakpoint *b)
10784 {
10785 return b->ops == &masked_watchpoint_breakpoint_ops;
10786 }
10787
10788 /* accessflag: hw_write: watch write,
10789 hw_read: watch read,
10790 hw_access: watch access (read or write) */
10791 static void
10792 watch_command_1 (const char *arg, int accessflag, int from_tty,
10793 int just_location, int internal)
10794 {
10795 struct breakpoint *scope_breakpoint = NULL;
10796 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10797 struct value *val, *mark, *result;
10798 int saved_bitpos = 0, saved_bitsize = 0;
10799 const char *exp_start = NULL;
10800 const char *exp_end = NULL;
10801 const char *tok, *end_tok;
10802 int toklen = -1;
10803 const char *cond_start = NULL;
10804 const char *cond_end = NULL;
10805 enum bptype bp_type;
10806 int thread = -1;
10807 int pc = 0;
10808 /* Flag to indicate whether we are going to use masks for
10809 the hardware watchpoint. */
10810 int use_mask = 0;
10811 CORE_ADDR mask = 0;
10812 char *expression;
10813 struct cleanup *back_to;
10814
10815 /* Make sure that we actually have parameters to parse. */
10816 if (arg != NULL && arg[0] != '\0')
10817 {
10818 const char *value_start;
10819
10820 exp_end = arg + strlen (arg);
10821
10822 /* Look for "parameter value" pairs at the end
10823 of the arguments string. */
10824 for (tok = exp_end - 1; tok > arg; tok--)
10825 {
10826 /* Skip whitespace at the end of the argument list. */
10827 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10828 tok--;
10829
10830 /* Find the beginning of the last token.
10831 This is the value of the parameter. */
10832 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10833 tok--;
10834 value_start = tok + 1;
10835
10836 /* Skip whitespace. */
10837 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10838 tok--;
10839
10840 end_tok = tok;
10841
10842 /* Find the beginning of the second to last token.
10843 This is the parameter itself. */
10844 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10845 tok--;
10846 tok++;
10847 toklen = end_tok - tok + 1;
10848
10849 if (toklen == 6 && startswith (tok, "thread"))
10850 {
10851 struct thread_info *thr;
10852 /* At this point we've found a "thread" token, which means
10853 the user is trying to set a watchpoint that triggers
10854 only in a specific thread. */
10855 const char *endp;
10856
10857 if (thread != -1)
10858 error(_("You can specify only one thread."));
10859
10860 /* Extract the thread ID from the next token. */
10861 thr = parse_thread_id (value_start, &endp);
10862
10863 /* Check if the user provided a valid thread ID. */
10864 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10865 invalid_thread_id_error (value_start);
10866
10867 thread = thr->global_num;
10868 }
10869 else if (toklen == 4 && startswith (tok, "mask"))
10870 {
10871 /* We've found a "mask" token, which means the user wants to
10872 create a hardware watchpoint that is going to have the mask
10873 facility. */
10874 struct value *mask_value, *mark;
10875
10876 if (use_mask)
10877 error(_("You can specify only one mask."));
10878
10879 use_mask = just_location = 1;
10880
10881 mark = value_mark ();
10882 mask_value = parse_to_comma_and_eval (&value_start);
10883 mask = value_as_address (mask_value);
10884 value_free_to_mark (mark);
10885 }
10886 else
10887 /* We didn't recognize what we found. We should stop here. */
10888 break;
10889
10890 /* Truncate the string and get rid of the "parameter value" pair before
10891 the arguments string is parsed by the parse_exp_1 function. */
10892 exp_end = tok;
10893 }
10894 }
10895 else
10896 exp_end = arg;
10897
10898 /* Parse the rest of the arguments. From here on out, everything
10899 is in terms of a newly allocated string instead of the original
10900 ARG. */
10901 innermost_block = NULL;
10902 expression = savestring (arg, exp_end - arg);
10903 back_to = make_cleanup (xfree, expression);
10904 exp_start = arg = expression;
10905 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
10906 exp_end = arg;
10907 /* Remove trailing whitespace from the expression before saving it.
10908 This makes the eventual display of the expression string a bit
10909 prettier. */
10910 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10911 --exp_end;
10912
10913 /* Checking if the expression is not constant. */
10914 if (watchpoint_exp_is_const (exp.get ()))
10915 {
10916 int len;
10917
10918 len = exp_end - exp_start;
10919 while (len > 0 && isspace (exp_start[len - 1]))
10920 len--;
10921 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10922 }
10923
10924 exp_valid_block = innermost_block;
10925 mark = value_mark ();
10926 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
10927
10928 if (val != NULL && just_location)
10929 {
10930 saved_bitpos = value_bitpos (val);
10931 saved_bitsize = value_bitsize (val);
10932 }
10933
10934 if (just_location)
10935 {
10936 int ret;
10937
10938 exp_valid_block = NULL;
10939 val = value_addr (result);
10940 release_value (val);
10941 value_free_to_mark (mark);
10942
10943 if (use_mask)
10944 {
10945 ret = target_masked_watch_num_registers (value_as_address (val),
10946 mask);
10947 if (ret == -1)
10948 error (_("This target does not support masked watchpoints."));
10949 else if (ret == -2)
10950 error (_("Invalid mask or memory region."));
10951 }
10952 }
10953 else if (val != NULL)
10954 release_value (val);
10955
10956 tok = skip_spaces (arg);
10957 end_tok = skip_to_space (tok);
10958
10959 toklen = end_tok - tok;
10960 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10961 {
10962 innermost_block = NULL;
10963 tok = cond_start = end_tok + 1;
10964 parse_exp_1 (&tok, 0, 0, 0);
10965
10966 /* The watchpoint expression may not be local, but the condition
10967 may still be. E.g.: `watch global if local > 0'. */
10968 cond_exp_valid_block = innermost_block;
10969
10970 cond_end = tok;
10971 }
10972 if (*tok)
10973 error (_("Junk at end of command."));
10974
10975 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10976
10977 /* Save this because create_internal_breakpoint below invalidates
10978 'wp_frame'. */
10979 frame_id watchpoint_frame = get_frame_id (wp_frame);
10980
10981 /* If the expression is "local", then set up a "watchpoint scope"
10982 breakpoint at the point where we've left the scope of the watchpoint
10983 expression. Create the scope breakpoint before the watchpoint, so
10984 that we will encounter it first in bpstat_stop_status. */
10985 if (exp_valid_block != NULL && wp_frame != NULL)
10986 {
10987 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10988
10989 if (frame_id_p (caller_frame_id))
10990 {
10991 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10992 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10993
10994 scope_breakpoint
10995 = create_internal_breakpoint (caller_arch, caller_pc,
10996 bp_watchpoint_scope,
10997 &momentary_breakpoint_ops);
10998
10999 /* create_internal_breakpoint could invalidate WP_FRAME. */
11000 wp_frame = NULL;
11001
11002 scope_breakpoint->enable_state = bp_enabled;
11003
11004 /* Automatically delete the breakpoint when it hits. */
11005 scope_breakpoint->disposition = disp_del;
11006
11007 /* Only break in the proper frame (help with recursion). */
11008 scope_breakpoint->frame_id = caller_frame_id;
11009
11010 /* Set the address at which we will stop. */
11011 scope_breakpoint->loc->gdbarch = caller_arch;
11012 scope_breakpoint->loc->requested_address = caller_pc;
11013 scope_breakpoint->loc->address
11014 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11015 scope_breakpoint->loc->requested_address,
11016 scope_breakpoint->type);
11017 }
11018 }
11019
11020 /* Now set up the breakpoint. We create all watchpoints as hardware
11021 watchpoints here even if hardware watchpoints are turned off, a call
11022 to update_watchpoint later in this function will cause the type to
11023 drop back to bp_watchpoint (software watchpoint) if required. */
11024
11025 if (accessflag == hw_read)
11026 bp_type = bp_read_watchpoint;
11027 else if (accessflag == hw_access)
11028 bp_type = bp_access_watchpoint;
11029 else
11030 bp_type = bp_hardware_watchpoint;
11031
11032 std::unique_ptr<watchpoint> w (new watchpoint ());
11033
11034 if (use_mask)
11035 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
11036 &masked_watchpoint_breakpoint_ops);
11037 else
11038 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
11039 &watchpoint_breakpoint_ops);
11040 w->thread = thread;
11041 w->disposition = disp_donttouch;
11042 w->pspace = current_program_space;
11043 w->exp = std::move (exp);
11044 w->exp_valid_block = exp_valid_block;
11045 w->cond_exp_valid_block = cond_exp_valid_block;
11046 if (just_location)
11047 {
11048 struct type *t = value_type (val);
11049 CORE_ADDR addr = value_as_address (val);
11050
11051 w->exp_string_reparse
11052 = current_language->la_watch_location_expression (t, addr).release ();
11053
11054 w->exp_string = xstrprintf ("-location %.*s",
11055 (int) (exp_end - exp_start), exp_start);
11056 }
11057 else
11058 w->exp_string = savestring (exp_start, exp_end - exp_start);
11059
11060 if (use_mask)
11061 {
11062 w->hw_wp_mask = mask;
11063 }
11064 else
11065 {
11066 w->val = val;
11067 w->val_bitpos = saved_bitpos;
11068 w->val_bitsize = saved_bitsize;
11069 w->val_valid = 1;
11070 }
11071
11072 if (cond_start)
11073 w->cond_string = savestring (cond_start, cond_end - cond_start);
11074 else
11075 w->cond_string = 0;
11076
11077 if (frame_id_p (watchpoint_frame))
11078 {
11079 w->watchpoint_frame = watchpoint_frame;
11080 w->watchpoint_thread = inferior_ptid;
11081 }
11082 else
11083 {
11084 w->watchpoint_frame = null_frame_id;
11085 w->watchpoint_thread = null_ptid;
11086 }
11087
11088 if (scope_breakpoint != NULL)
11089 {
11090 /* The scope breakpoint is related to the watchpoint. We will
11091 need to act on them together. */
11092 w->related_breakpoint = scope_breakpoint;
11093 scope_breakpoint->related_breakpoint = w.get ();
11094 }
11095
11096 if (!just_location)
11097 value_free_to_mark (mark);
11098
11099 /* Finally update the new watchpoint. This creates the locations
11100 that should be inserted. */
11101 update_watchpoint (w.get (), 1);
11102
11103 install_breakpoint (internal, std::move (w), 1);
11104 do_cleanups (back_to);
11105 }
11106
11107 /* Return count of debug registers needed to watch the given expression.
11108 If the watchpoint cannot be handled in hardware return zero. */
11109
11110 static int
11111 can_use_hardware_watchpoint (struct value *v)
11112 {
11113 int found_memory_cnt = 0;
11114 struct value *head = v;
11115
11116 /* Did the user specifically forbid us to use hardware watchpoints? */
11117 if (!can_use_hw_watchpoints)
11118 return 0;
11119
11120 /* Make sure that the value of the expression depends only upon
11121 memory contents, and values computed from them within GDB. If we
11122 find any register references or function calls, we can't use a
11123 hardware watchpoint.
11124
11125 The idea here is that evaluating an expression generates a series
11126 of values, one holding the value of every subexpression. (The
11127 expression a*b+c has five subexpressions: a, b, a*b, c, and
11128 a*b+c.) GDB's values hold almost enough information to establish
11129 the criteria given above --- they identify memory lvalues,
11130 register lvalues, computed values, etcetera. So we can evaluate
11131 the expression, and then scan the chain of values that leaves
11132 behind to decide whether we can detect any possible change to the
11133 expression's final value using only hardware watchpoints.
11134
11135 However, I don't think that the values returned by inferior
11136 function calls are special in any way. So this function may not
11137 notice that an expression involving an inferior function call
11138 can't be watched with hardware watchpoints. FIXME. */
11139 for (; v; v = value_next (v))
11140 {
11141 if (VALUE_LVAL (v) == lval_memory)
11142 {
11143 if (v != head && value_lazy (v))
11144 /* A lazy memory lvalue in the chain is one that GDB never
11145 needed to fetch; we either just used its address (e.g.,
11146 `a' in `a.b') or we never needed it at all (e.g., `a'
11147 in `a,b'). This doesn't apply to HEAD; if that is
11148 lazy then it was not readable, but watch it anyway. */
11149 ;
11150 else
11151 {
11152 /* Ahh, memory we actually used! Check if we can cover
11153 it with hardware watchpoints. */
11154 struct type *vtype = check_typedef (value_type (v));
11155
11156 /* We only watch structs and arrays if user asked for it
11157 explicitly, never if they just happen to appear in a
11158 middle of some value chain. */
11159 if (v == head
11160 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11161 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11162 {
11163 CORE_ADDR vaddr = value_address (v);
11164 int len;
11165 int num_regs;
11166
11167 len = (target_exact_watchpoints
11168 && is_scalar_type_recursive (vtype))?
11169 1 : TYPE_LENGTH (value_type (v));
11170
11171 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11172 if (!num_regs)
11173 return 0;
11174 else
11175 found_memory_cnt += num_regs;
11176 }
11177 }
11178 }
11179 else if (VALUE_LVAL (v) != not_lval
11180 && deprecated_value_modifiable (v) == 0)
11181 return 0; /* These are values from the history (e.g., $1). */
11182 else if (VALUE_LVAL (v) == lval_register)
11183 return 0; /* Cannot watch a register with a HW watchpoint. */
11184 }
11185
11186 /* The expression itself looks suitable for using a hardware
11187 watchpoint, but give the target machine a chance to reject it. */
11188 return found_memory_cnt;
11189 }
11190
11191 void
11192 watch_command_wrapper (char *arg, int from_tty, int internal)
11193 {
11194 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11195 }
11196
11197 /* A helper function that looks for the "-location" argument and then
11198 calls watch_command_1. */
11199
11200 static void
11201 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11202 {
11203 int just_location = 0;
11204
11205 if (arg
11206 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11207 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11208 {
11209 arg = skip_spaces (arg);
11210 just_location = 1;
11211 }
11212
11213 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11214 }
11215
11216 static void
11217 watch_command (char *arg, int from_tty)
11218 {
11219 watch_maybe_just_location (arg, hw_write, from_tty);
11220 }
11221
11222 void
11223 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11224 {
11225 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11226 }
11227
11228 static void
11229 rwatch_command (char *arg, int from_tty)
11230 {
11231 watch_maybe_just_location (arg, hw_read, from_tty);
11232 }
11233
11234 void
11235 awatch_command_wrapper (char *arg, int from_tty, int internal)
11236 {
11237 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11238 }
11239
11240 static void
11241 awatch_command (char *arg, int from_tty)
11242 {
11243 watch_maybe_just_location (arg, hw_access, from_tty);
11244 }
11245 \f
11246
11247 /* Data for the FSM that manages the until(location)/advance commands
11248 in infcmd.c. Here because it uses the mechanisms of
11249 breakpoints. */
11250
11251 struct until_break_fsm
11252 {
11253 /* The base class. */
11254 struct thread_fsm thread_fsm;
11255
11256 /* The thread that as current when the command was executed. */
11257 int thread;
11258
11259 /* The breakpoint set at the destination location. */
11260 struct breakpoint *location_breakpoint;
11261
11262 /* Breakpoint set at the return address in the caller frame. May be
11263 NULL. */
11264 struct breakpoint *caller_breakpoint;
11265 };
11266
11267 static void until_break_fsm_clean_up (struct thread_fsm *self,
11268 struct thread_info *thread);
11269 static int until_break_fsm_should_stop (struct thread_fsm *self,
11270 struct thread_info *thread);
11271 static enum async_reply_reason
11272 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11273
11274 /* until_break_fsm's vtable. */
11275
11276 static struct thread_fsm_ops until_break_fsm_ops =
11277 {
11278 NULL, /* dtor */
11279 until_break_fsm_clean_up,
11280 until_break_fsm_should_stop,
11281 NULL, /* return_value */
11282 until_break_fsm_async_reply_reason,
11283 };
11284
11285 /* Allocate a new until_break_command_fsm. */
11286
11287 static struct until_break_fsm *
11288 new_until_break_fsm (struct interp *cmd_interp, int thread,
11289 struct breakpoint *location_breakpoint,
11290 struct breakpoint *caller_breakpoint)
11291 {
11292 struct until_break_fsm *sm;
11293
11294 sm = XCNEW (struct until_break_fsm);
11295 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11296
11297 sm->thread = thread;
11298 sm->location_breakpoint = location_breakpoint;
11299 sm->caller_breakpoint = caller_breakpoint;
11300
11301 return sm;
11302 }
11303
11304 /* Implementation of the 'should_stop' FSM method for the
11305 until(location)/advance commands. */
11306
11307 static int
11308 until_break_fsm_should_stop (struct thread_fsm *self,
11309 struct thread_info *tp)
11310 {
11311 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11312
11313 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11314 sm->location_breakpoint) != NULL
11315 || (sm->caller_breakpoint != NULL
11316 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11317 sm->caller_breakpoint) != NULL))
11318 thread_fsm_set_finished (self);
11319
11320 return 1;
11321 }
11322
11323 /* Implementation of the 'clean_up' FSM method for the
11324 until(location)/advance commands. */
11325
11326 static void
11327 until_break_fsm_clean_up (struct thread_fsm *self,
11328 struct thread_info *thread)
11329 {
11330 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11331
11332 /* Clean up our temporary breakpoints. */
11333 if (sm->location_breakpoint != NULL)
11334 {
11335 delete_breakpoint (sm->location_breakpoint);
11336 sm->location_breakpoint = NULL;
11337 }
11338 if (sm->caller_breakpoint != NULL)
11339 {
11340 delete_breakpoint (sm->caller_breakpoint);
11341 sm->caller_breakpoint = NULL;
11342 }
11343 delete_longjmp_breakpoint (sm->thread);
11344 }
11345
11346 /* Implementation of the 'async_reply_reason' FSM method for the
11347 until(location)/advance commands. */
11348
11349 static enum async_reply_reason
11350 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11351 {
11352 return EXEC_ASYNC_LOCATION_REACHED;
11353 }
11354
11355 void
11356 until_break_command (char *arg, int from_tty, int anywhere)
11357 {
11358 struct frame_info *frame;
11359 struct gdbarch *frame_gdbarch;
11360 struct frame_id stack_frame_id;
11361 struct frame_id caller_frame_id;
11362 struct breakpoint *location_breakpoint;
11363 struct breakpoint *caller_breakpoint = NULL;
11364 struct cleanup *old_chain;
11365 int thread;
11366 struct thread_info *tp;
11367 struct until_break_fsm *sm;
11368
11369 clear_proceed_status (0);
11370
11371 /* Set a breakpoint where the user wants it and at return from
11372 this function. */
11373
11374 event_location_up location = string_to_event_location (&arg, current_language);
11375
11376 std::vector<symtab_and_line> sals
11377 = (last_displayed_sal_is_valid ()
11378 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11379 get_last_displayed_symtab (),
11380 get_last_displayed_line ())
11381 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11382 NULL, (struct symtab *) NULL, 0));
11383
11384 if (sals.size () != 1)
11385 error (_("Couldn't get information on specified line."));
11386
11387 symtab_and_line &sal = sals[0];
11388
11389 if (*arg)
11390 error (_("Junk at end of arguments."));
11391
11392 resolve_sal_pc (&sal);
11393
11394 tp = inferior_thread ();
11395 thread = tp->global_num;
11396
11397 old_chain = make_cleanup (null_cleanup, NULL);
11398
11399 /* Note linespec handling above invalidates the frame chain.
11400 Installing a breakpoint also invalidates the frame chain (as it
11401 may need to switch threads), so do any frame handling before
11402 that. */
11403
11404 frame = get_selected_frame (NULL);
11405 frame_gdbarch = get_frame_arch (frame);
11406 stack_frame_id = get_stack_frame_id (frame);
11407 caller_frame_id = frame_unwind_caller_id (frame);
11408
11409 /* Keep within the current frame, or in frames called by the current
11410 one. */
11411
11412 if (frame_id_p (caller_frame_id))
11413 {
11414 struct symtab_and_line sal2;
11415 struct gdbarch *caller_gdbarch;
11416
11417 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11418 sal2.pc = frame_unwind_caller_pc (frame);
11419 caller_gdbarch = frame_unwind_caller_arch (frame);
11420 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11421 sal2,
11422 caller_frame_id,
11423 bp_until);
11424 make_cleanup_delete_breakpoint (caller_breakpoint);
11425
11426 set_longjmp_breakpoint (tp, caller_frame_id);
11427 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11428 }
11429
11430 /* set_momentary_breakpoint could invalidate FRAME. */
11431 frame = NULL;
11432
11433 if (anywhere)
11434 /* If the user told us to continue until a specified location,
11435 we don't specify a frame at which we need to stop. */
11436 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11437 null_frame_id, bp_until);
11438 else
11439 /* Otherwise, specify the selected frame, because we want to stop
11440 only at the very same frame. */
11441 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11442 stack_frame_id, bp_until);
11443 make_cleanup_delete_breakpoint (location_breakpoint);
11444
11445 sm = new_until_break_fsm (command_interp (), tp->global_num,
11446 location_breakpoint, caller_breakpoint);
11447 tp->thread_fsm = &sm->thread_fsm;
11448
11449 discard_cleanups (old_chain);
11450
11451 proceed (-1, GDB_SIGNAL_DEFAULT);
11452 }
11453
11454 /* This function attempts to parse an optional "if <cond>" clause
11455 from the arg string. If one is not found, it returns NULL.
11456
11457 Else, it returns a pointer to the condition string. (It does not
11458 attempt to evaluate the string against a particular block.) And,
11459 it updates arg to point to the first character following the parsed
11460 if clause in the arg string. */
11461
11462 const char *
11463 ep_parse_optional_if_clause (const char **arg)
11464 {
11465 const char *cond_string;
11466
11467 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11468 return NULL;
11469
11470 /* Skip the "if" keyword. */
11471 (*arg) += 2;
11472
11473 /* Skip any extra leading whitespace, and record the start of the
11474 condition string. */
11475 *arg = skip_spaces (*arg);
11476 cond_string = *arg;
11477
11478 /* Assume that the condition occupies the remainder of the arg
11479 string. */
11480 (*arg) += strlen (cond_string);
11481
11482 return cond_string;
11483 }
11484
11485 /* Commands to deal with catching events, such as signals, exceptions,
11486 process start/exit, etc. */
11487
11488 typedef enum
11489 {
11490 catch_fork_temporary, catch_vfork_temporary,
11491 catch_fork_permanent, catch_vfork_permanent
11492 }
11493 catch_fork_kind;
11494
11495 static void
11496 catch_fork_command_1 (char *arg_entry, int from_tty,
11497 struct cmd_list_element *command)
11498 {
11499 const char *arg = arg_entry;
11500 struct gdbarch *gdbarch = get_current_arch ();
11501 const char *cond_string = NULL;
11502 catch_fork_kind fork_kind;
11503 int tempflag;
11504
11505 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11506 tempflag = (fork_kind == catch_fork_temporary
11507 || fork_kind == catch_vfork_temporary);
11508
11509 if (!arg)
11510 arg = "";
11511 arg = skip_spaces (arg);
11512
11513 /* The allowed syntax is:
11514 catch [v]fork
11515 catch [v]fork if <cond>
11516
11517 First, check if there's an if clause. */
11518 cond_string = ep_parse_optional_if_clause (&arg);
11519
11520 if ((*arg != '\0') && !isspace (*arg))
11521 error (_("Junk at end of arguments."));
11522
11523 /* If this target supports it, create a fork or vfork catchpoint
11524 and enable reporting of such events. */
11525 switch (fork_kind)
11526 {
11527 case catch_fork_temporary:
11528 case catch_fork_permanent:
11529 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11530 &catch_fork_breakpoint_ops);
11531 break;
11532 case catch_vfork_temporary:
11533 case catch_vfork_permanent:
11534 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11535 &catch_vfork_breakpoint_ops);
11536 break;
11537 default:
11538 error (_("unsupported or unknown fork kind; cannot catch it"));
11539 break;
11540 }
11541 }
11542
11543 static void
11544 catch_exec_command_1 (char *arg_entry, int from_tty,
11545 struct cmd_list_element *command)
11546 {
11547 const char *arg = arg_entry;
11548 struct gdbarch *gdbarch = get_current_arch ();
11549 int tempflag;
11550 const char *cond_string = NULL;
11551
11552 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11553
11554 if (!arg)
11555 arg = "";
11556 arg = skip_spaces (arg);
11557
11558 /* The allowed syntax is:
11559 catch exec
11560 catch exec if <cond>
11561
11562 First, check if there's an if clause. */
11563 cond_string = ep_parse_optional_if_clause (&arg);
11564
11565 if ((*arg != '\0') && !isspace (*arg))
11566 error (_("Junk at end of arguments."));
11567
11568 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11569 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11570 &catch_exec_breakpoint_ops);
11571 c->exec_pathname = NULL;
11572
11573 install_breakpoint (0, std::move (c), 1);
11574 }
11575
11576 void
11577 init_ada_exception_breakpoint (struct breakpoint *b,
11578 struct gdbarch *gdbarch,
11579 struct symtab_and_line sal,
11580 char *addr_string,
11581 const struct breakpoint_ops *ops,
11582 int tempflag,
11583 int enabled,
11584 int from_tty)
11585 {
11586 if (from_tty)
11587 {
11588 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11589 if (!loc_gdbarch)
11590 loc_gdbarch = gdbarch;
11591
11592 describe_other_breakpoints (loc_gdbarch,
11593 sal.pspace, sal.pc, sal.section, -1);
11594 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11595 version for exception catchpoints, because two catchpoints
11596 used for different exception names will use the same address.
11597 In this case, a "breakpoint ... also set at..." warning is
11598 unproductive. Besides, the warning phrasing is also a bit
11599 inappropriate, we should use the word catchpoint, and tell
11600 the user what type of catchpoint it is. The above is good
11601 enough for now, though. */
11602 }
11603
11604 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11605
11606 b->enable_state = enabled ? bp_enabled : bp_disabled;
11607 b->disposition = tempflag ? disp_del : disp_donttouch;
11608 b->location = string_to_event_location (&addr_string,
11609 language_def (language_ada));
11610 b->language = language_ada;
11611 }
11612
11613 static void
11614 catch_command (char *arg, int from_tty)
11615 {
11616 error (_("Catch requires an event name."));
11617 }
11618 \f
11619
11620 static void
11621 tcatch_command (char *arg, int from_tty)
11622 {
11623 error (_("Catch requires an event name."));
11624 }
11625
11626 /* A qsort comparison function that sorts breakpoints in order. */
11627
11628 static int
11629 compare_breakpoints (const void *a, const void *b)
11630 {
11631 const breakpoint_p *ba = (const breakpoint_p *) a;
11632 uintptr_t ua = (uintptr_t) *ba;
11633 const breakpoint_p *bb = (const breakpoint_p *) b;
11634 uintptr_t ub = (uintptr_t) *bb;
11635
11636 if ((*ba)->number < (*bb)->number)
11637 return -1;
11638 else if ((*ba)->number > (*bb)->number)
11639 return 1;
11640
11641 /* Now sort by address, in case we see, e..g, two breakpoints with
11642 the number 0. */
11643 if (ua < ub)
11644 return -1;
11645 return ua > ub ? 1 : 0;
11646 }
11647
11648 /* Delete breakpoints by address or line. */
11649
11650 static void
11651 clear_command (char *arg, int from_tty)
11652 {
11653 struct breakpoint *b, *prev;
11654 VEC(breakpoint_p) *found = 0;
11655 int ix;
11656 int default_match;
11657 int i;
11658 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11659
11660 std::vector<symtab_and_line> decoded_sals;
11661 symtab_and_line last_sal;
11662 gdb::array_view<symtab_and_line> sals;
11663 if (arg)
11664 {
11665 decoded_sals
11666 = decode_line_with_current_source (arg,
11667 (DECODE_LINE_FUNFIRSTLINE
11668 | DECODE_LINE_LIST_MODE));
11669 default_match = 0;
11670 sals = decoded_sals;
11671 }
11672 else
11673 {
11674 /* Set sal's line, symtab, pc, and pspace to the values
11675 corresponding to the last call to print_frame_info. If the
11676 codepoint is not valid, this will set all the fields to 0. */
11677 last_sal = get_last_displayed_sal ();
11678 if (last_sal.symtab == 0)
11679 error (_("No source file specified."));
11680
11681 default_match = 1;
11682 sals = last_sal;
11683 }
11684
11685 /* We don't call resolve_sal_pc here. That's not as bad as it
11686 seems, because all existing breakpoints typically have both
11687 file/line and pc set. So, if clear is given file/line, we can
11688 match this to existing breakpoint without obtaining pc at all.
11689
11690 We only support clearing given the address explicitly
11691 present in breakpoint table. Say, we've set breakpoint
11692 at file:line. There were several PC values for that file:line,
11693 due to optimization, all in one block.
11694
11695 We've picked one PC value. If "clear" is issued with another
11696 PC corresponding to the same file:line, the breakpoint won't
11697 be cleared. We probably can still clear the breakpoint, but
11698 since the other PC value is never presented to user, user
11699 can only find it by guessing, and it does not seem important
11700 to support that. */
11701
11702 /* For each line spec given, delete bps which correspond to it. Do
11703 it in two passes, solely to preserve the current behavior that
11704 from_tty is forced true if we delete more than one
11705 breakpoint. */
11706
11707 found = NULL;
11708 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11709 for (const auto &sal : sals)
11710 {
11711 const char *sal_fullname;
11712
11713 /* If exact pc given, clear bpts at that pc.
11714 If line given (pc == 0), clear all bpts on specified line.
11715 If defaulting, clear all bpts on default line
11716 or at default pc.
11717
11718 defaulting sal.pc != 0 tests to do
11719
11720 0 1 pc
11721 1 1 pc _and_ line
11722 0 0 line
11723 1 0 <can't happen> */
11724
11725 sal_fullname = (sal.symtab == NULL
11726 ? NULL : symtab_to_fullname (sal.symtab));
11727
11728 /* Find all matching breakpoints and add them to 'found'. */
11729 ALL_BREAKPOINTS (b)
11730 {
11731 int match = 0;
11732 /* Are we going to delete b? */
11733 if (b->type != bp_none && !is_watchpoint (b))
11734 {
11735 struct bp_location *loc = b->loc;
11736 for (; loc; loc = loc->next)
11737 {
11738 /* If the user specified file:line, don't allow a PC
11739 match. This matches historical gdb behavior. */
11740 int pc_match = (!sal.explicit_line
11741 && sal.pc
11742 && (loc->pspace == sal.pspace)
11743 && (loc->address == sal.pc)
11744 && (!section_is_overlay (loc->section)
11745 || loc->section == sal.section));
11746 int line_match = 0;
11747
11748 if ((default_match || sal.explicit_line)
11749 && loc->symtab != NULL
11750 && sal_fullname != NULL
11751 && sal.pspace == loc->pspace
11752 && loc->line_number == sal.line
11753 && filename_cmp (symtab_to_fullname (loc->symtab),
11754 sal_fullname) == 0)
11755 line_match = 1;
11756
11757 if (pc_match || line_match)
11758 {
11759 match = 1;
11760 break;
11761 }
11762 }
11763 }
11764
11765 if (match)
11766 VEC_safe_push(breakpoint_p, found, b);
11767 }
11768 }
11769
11770 /* Now go thru the 'found' chain and delete them. */
11771 if (VEC_empty(breakpoint_p, found))
11772 {
11773 if (arg)
11774 error (_("No breakpoint at %s."), arg);
11775 else
11776 error (_("No breakpoint at this line."));
11777 }
11778
11779 /* Remove duplicates from the vec. */
11780 qsort (VEC_address (breakpoint_p, found),
11781 VEC_length (breakpoint_p, found),
11782 sizeof (breakpoint_p),
11783 compare_breakpoints);
11784 prev = VEC_index (breakpoint_p, found, 0);
11785 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11786 {
11787 if (b == prev)
11788 {
11789 VEC_ordered_remove (breakpoint_p, found, ix);
11790 --ix;
11791 }
11792 }
11793
11794 if (VEC_length(breakpoint_p, found) > 1)
11795 from_tty = 1; /* Always report if deleted more than one. */
11796 if (from_tty)
11797 {
11798 if (VEC_length(breakpoint_p, found) == 1)
11799 printf_unfiltered (_("Deleted breakpoint "));
11800 else
11801 printf_unfiltered (_("Deleted breakpoints "));
11802 }
11803
11804 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11805 {
11806 if (from_tty)
11807 printf_unfiltered ("%d ", b->number);
11808 delete_breakpoint (b);
11809 }
11810 if (from_tty)
11811 putchar_unfiltered ('\n');
11812
11813 do_cleanups (cleanups);
11814 }
11815 \f
11816 /* Delete breakpoint in BS if they are `delete' breakpoints and
11817 all breakpoints that are marked for deletion, whether hit or not.
11818 This is called after any breakpoint is hit, or after errors. */
11819
11820 void
11821 breakpoint_auto_delete (bpstat bs)
11822 {
11823 struct breakpoint *b, *b_tmp;
11824
11825 for (; bs; bs = bs->next)
11826 if (bs->breakpoint_at
11827 && bs->breakpoint_at->disposition == disp_del
11828 && bs->stop)
11829 delete_breakpoint (bs->breakpoint_at);
11830
11831 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11832 {
11833 if (b->disposition == disp_del_at_next_stop)
11834 delete_breakpoint (b);
11835 }
11836 }
11837
11838 /* A comparison function for bp_location AP and BP being interfaced to
11839 qsort. Sort elements primarily by their ADDRESS (no matter what
11840 does breakpoint_address_is_meaningful say for its OWNER),
11841 secondarily by ordering first permanent elements and
11842 terciarily just ensuring the array is sorted stable way despite
11843 qsort being an unstable algorithm. */
11844
11845 static int
11846 bp_locations_compare (const void *ap, const void *bp)
11847 {
11848 const struct bp_location *a = *(const struct bp_location **) ap;
11849 const struct bp_location *b = *(const struct bp_location **) bp;
11850
11851 if (a->address != b->address)
11852 return (a->address > b->address) - (a->address < b->address);
11853
11854 /* Sort locations at the same address by their pspace number, keeping
11855 locations of the same inferior (in a multi-inferior environment)
11856 grouped. */
11857
11858 if (a->pspace->num != b->pspace->num)
11859 return ((a->pspace->num > b->pspace->num)
11860 - (a->pspace->num < b->pspace->num));
11861
11862 /* Sort permanent breakpoints first. */
11863 if (a->permanent != b->permanent)
11864 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11865
11866 /* Make the internal GDB representation stable across GDB runs
11867 where A and B memory inside GDB can differ. Breakpoint locations of
11868 the same type at the same address can be sorted in arbitrary order. */
11869
11870 if (a->owner->number != b->owner->number)
11871 return ((a->owner->number > b->owner->number)
11872 - (a->owner->number < b->owner->number));
11873
11874 return (a > b) - (a < b);
11875 }
11876
11877 /* Set bp_locations_placed_address_before_address_max and
11878 bp_locations_shadow_len_after_address_max according to the current
11879 content of the bp_locations array. */
11880
11881 static void
11882 bp_locations_target_extensions_update (void)
11883 {
11884 struct bp_location *bl, **blp_tmp;
11885
11886 bp_locations_placed_address_before_address_max = 0;
11887 bp_locations_shadow_len_after_address_max = 0;
11888
11889 ALL_BP_LOCATIONS (bl, blp_tmp)
11890 {
11891 CORE_ADDR start, end, addr;
11892
11893 if (!bp_location_has_shadow (bl))
11894 continue;
11895
11896 start = bl->target_info.placed_address;
11897 end = start + bl->target_info.shadow_len;
11898
11899 gdb_assert (bl->address >= start);
11900 addr = bl->address - start;
11901 if (addr > bp_locations_placed_address_before_address_max)
11902 bp_locations_placed_address_before_address_max = addr;
11903
11904 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11905
11906 gdb_assert (bl->address < end);
11907 addr = end - bl->address;
11908 if (addr > bp_locations_shadow_len_after_address_max)
11909 bp_locations_shadow_len_after_address_max = addr;
11910 }
11911 }
11912
11913 /* Download tracepoint locations if they haven't been. */
11914
11915 static void
11916 download_tracepoint_locations (void)
11917 {
11918 struct breakpoint *b;
11919 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11920
11921 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11922
11923 ALL_TRACEPOINTS (b)
11924 {
11925 struct bp_location *bl;
11926 struct tracepoint *t;
11927 int bp_location_downloaded = 0;
11928
11929 if ((b->type == bp_fast_tracepoint
11930 ? !may_insert_fast_tracepoints
11931 : !may_insert_tracepoints))
11932 continue;
11933
11934 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11935 {
11936 if (target_can_download_tracepoint ())
11937 can_download_tracepoint = TRIBOOL_TRUE;
11938 else
11939 can_download_tracepoint = TRIBOOL_FALSE;
11940 }
11941
11942 if (can_download_tracepoint == TRIBOOL_FALSE)
11943 break;
11944
11945 for (bl = b->loc; bl; bl = bl->next)
11946 {
11947 /* In tracepoint, locations are _never_ duplicated, so
11948 should_be_inserted is equivalent to
11949 unduplicated_should_be_inserted. */
11950 if (!should_be_inserted (bl) || bl->inserted)
11951 continue;
11952
11953 switch_to_program_space_and_thread (bl->pspace);
11954
11955 target_download_tracepoint (bl);
11956
11957 bl->inserted = 1;
11958 bp_location_downloaded = 1;
11959 }
11960 t = (struct tracepoint *) b;
11961 t->number_on_target = b->number;
11962 if (bp_location_downloaded)
11963 observer_notify_breakpoint_modified (b);
11964 }
11965 }
11966
11967 /* Swap the insertion/duplication state between two locations. */
11968
11969 static void
11970 swap_insertion (struct bp_location *left, struct bp_location *right)
11971 {
11972 const int left_inserted = left->inserted;
11973 const int left_duplicate = left->duplicate;
11974 const int left_needs_update = left->needs_update;
11975 const struct bp_target_info left_target_info = left->target_info;
11976
11977 /* Locations of tracepoints can never be duplicated. */
11978 if (is_tracepoint (left->owner))
11979 gdb_assert (!left->duplicate);
11980 if (is_tracepoint (right->owner))
11981 gdb_assert (!right->duplicate);
11982
11983 left->inserted = right->inserted;
11984 left->duplicate = right->duplicate;
11985 left->needs_update = right->needs_update;
11986 left->target_info = right->target_info;
11987 right->inserted = left_inserted;
11988 right->duplicate = left_duplicate;
11989 right->needs_update = left_needs_update;
11990 right->target_info = left_target_info;
11991 }
11992
11993 /* Force the re-insertion of the locations at ADDRESS. This is called
11994 once a new/deleted/modified duplicate location is found and we are evaluating
11995 conditions on the target's side. Such conditions need to be updated on
11996 the target. */
11997
11998 static void
11999 force_breakpoint_reinsertion (struct bp_location *bl)
12000 {
12001 struct bp_location **locp = NULL, **loc2p;
12002 struct bp_location *loc;
12003 CORE_ADDR address = 0;
12004 int pspace_num;
12005
12006 address = bl->address;
12007 pspace_num = bl->pspace->num;
12008
12009 /* This is only meaningful if the target is
12010 evaluating conditions and if the user has
12011 opted for condition evaluation on the target's
12012 side. */
12013 if (gdb_evaluates_breakpoint_condition_p ()
12014 || !target_supports_evaluation_of_breakpoint_conditions ())
12015 return;
12016
12017 /* Flag all breakpoint locations with this address and
12018 the same program space as the location
12019 as "its condition has changed". We need to
12020 update the conditions on the target's side. */
12021 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12022 {
12023 loc = *loc2p;
12024
12025 if (!is_breakpoint (loc->owner)
12026 || pspace_num != loc->pspace->num)
12027 continue;
12028
12029 /* Flag the location appropriately. We use a different state to
12030 let everyone know that we already updated the set of locations
12031 with addr bl->address and program space bl->pspace. This is so
12032 we don't have to keep calling these functions just to mark locations
12033 that have already been marked. */
12034 loc->condition_changed = condition_updated;
12035
12036 /* Free the agent expression bytecode as well. We will compute
12037 it later on. */
12038 loc->cond_bytecode.reset ();
12039 }
12040 }
12041 /* Called whether new breakpoints are created, or existing breakpoints
12042 deleted, to update the global location list and recompute which
12043 locations are duplicate of which.
12044
12045 The INSERT_MODE flag determines whether locations may not, may, or
12046 shall be inserted now. See 'enum ugll_insert_mode' for more
12047 info. */
12048
12049 static void
12050 update_global_location_list (enum ugll_insert_mode insert_mode)
12051 {
12052 struct breakpoint *b;
12053 struct bp_location **locp, *loc;
12054 struct cleanup *cleanups;
12055 /* Last breakpoint location address that was marked for update. */
12056 CORE_ADDR last_addr = 0;
12057 /* Last breakpoint location program space that was marked for update. */
12058 int last_pspace_num = -1;
12059
12060 /* Used in the duplicates detection below. When iterating over all
12061 bp_locations, points to the first bp_location of a given address.
12062 Breakpoints and watchpoints of different types are never
12063 duplicates of each other. Keep one pointer for each type of
12064 breakpoint/watchpoint, so we only need to loop over all locations
12065 once. */
12066 struct bp_location *bp_loc_first; /* breakpoint */
12067 struct bp_location *wp_loc_first; /* hardware watchpoint */
12068 struct bp_location *awp_loc_first; /* access watchpoint */
12069 struct bp_location *rwp_loc_first; /* read watchpoint */
12070
12071 /* Saved former bp_locations array which we compare against the newly
12072 built bp_locations from the current state of ALL_BREAKPOINTS. */
12073 struct bp_location **old_locations, **old_locp;
12074 unsigned old_locations_count;
12075
12076 old_locations = bp_locations;
12077 old_locations_count = bp_locations_count;
12078 bp_locations = NULL;
12079 bp_locations_count = 0;
12080 cleanups = make_cleanup (xfree, old_locations);
12081
12082 ALL_BREAKPOINTS (b)
12083 for (loc = b->loc; loc; loc = loc->next)
12084 bp_locations_count++;
12085
12086 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12087 locp = bp_locations;
12088 ALL_BREAKPOINTS (b)
12089 for (loc = b->loc; loc; loc = loc->next)
12090 *locp++ = loc;
12091 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12092 bp_locations_compare);
12093
12094 bp_locations_target_extensions_update ();
12095
12096 /* Identify bp_location instances that are no longer present in the
12097 new list, and therefore should be freed. Note that it's not
12098 necessary that those locations should be removed from inferior --
12099 if there's another location at the same address (previously
12100 marked as duplicate), we don't need to remove/insert the
12101 location.
12102
12103 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12104 and former bp_location array state respectively. */
12105
12106 locp = bp_locations;
12107 for (old_locp = old_locations;
12108 old_locp < old_locations + old_locations_count;
12109 old_locp++)
12110 {
12111 struct bp_location *old_loc = *old_locp;
12112 struct bp_location **loc2p;
12113
12114 /* Tells if 'old_loc' is found among the new locations. If
12115 not, we have to free it. */
12116 int found_object = 0;
12117 /* Tells if the location should remain inserted in the target. */
12118 int keep_in_target = 0;
12119 int removed = 0;
12120
12121 /* Skip LOCP entries which will definitely never be needed.
12122 Stop either at or being the one matching OLD_LOC. */
12123 while (locp < bp_locations + bp_locations_count
12124 && (*locp)->address < old_loc->address)
12125 locp++;
12126
12127 for (loc2p = locp;
12128 (loc2p < bp_locations + bp_locations_count
12129 && (*loc2p)->address == old_loc->address);
12130 loc2p++)
12131 {
12132 /* Check if this is a new/duplicated location or a duplicated
12133 location that had its condition modified. If so, we want to send
12134 its condition to the target if evaluation of conditions is taking
12135 place there. */
12136 if ((*loc2p)->condition_changed == condition_modified
12137 && (last_addr != old_loc->address
12138 || last_pspace_num != old_loc->pspace->num))
12139 {
12140 force_breakpoint_reinsertion (*loc2p);
12141 last_pspace_num = old_loc->pspace->num;
12142 }
12143
12144 if (*loc2p == old_loc)
12145 found_object = 1;
12146 }
12147
12148 /* We have already handled this address, update it so that we don't
12149 have to go through updates again. */
12150 last_addr = old_loc->address;
12151
12152 /* Target-side condition evaluation: Handle deleted locations. */
12153 if (!found_object)
12154 force_breakpoint_reinsertion (old_loc);
12155
12156 /* If this location is no longer present, and inserted, look if
12157 there's maybe a new location at the same address. If so,
12158 mark that one inserted, and don't remove this one. This is
12159 needed so that we don't have a time window where a breakpoint
12160 at certain location is not inserted. */
12161
12162 if (old_loc->inserted)
12163 {
12164 /* If the location is inserted now, we might have to remove
12165 it. */
12166
12167 if (found_object && should_be_inserted (old_loc))
12168 {
12169 /* The location is still present in the location list,
12170 and still should be inserted. Don't do anything. */
12171 keep_in_target = 1;
12172 }
12173 else
12174 {
12175 /* This location still exists, but it won't be kept in the
12176 target since it may have been disabled. We proceed to
12177 remove its target-side condition. */
12178
12179 /* The location is either no longer present, or got
12180 disabled. See if there's another location at the
12181 same address, in which case we don't need to remove
12182 this one from the target. */
12183
12184 /* OLD_LOC comes from existing struct breakpoint. */
12185 if (breakpoint_address_is_meaningful (old_loc->owner))
12186 {
12187 for (loc2p = locp;
12188 (loc2p < bp_locations + bp_locations_count
12189 && (*loc2p)->address == old_loc->address);
12190 loc2p++)
12191 {
12192 struct bp_location *loc2 = *loc2p;
12193
12194 if (breakpoint_locations_match (loc2, old_loc))
12195 {
12196 /* Read watchpoint locations are switched to
12197 access watchpoints, if the former are not
12198 supported, but the latter are. */
12199 if (is_hardware_watchpoint (old_loc->owner))
12200 {
12201 gdb_assert (is_hardware_watchpoint (loc2->owner));
12202 loc2->watchpoint_type = old_loc->watchpoint_type;
12203 }
12204
12205 /* loc2 is a duplicated location. We need to check
12206 if it should be inserted in case it will be
12207 unduplicated. */
12208 if (loc2 != old_loc
12209 && unduplicated_should_be_inserted (loc2))
12210 {
12211 swap_insertion (old_loc, loc2);
12212 keep_in_target = 1;
12213 break;
12214 }
12215 }
12216 }
12217 }
12218 }
12219
12220 if (!keep_in_target)
12221 {
12222 if (remove_breakpoint (old_loc))
12223 {
12224 /* This is just about all we can do. We could keep
12225 this location on the global list, and try to
12226 remove it next time, but there's no particular
12227 reason why we will succeed next time.
12228
12229 Note that at this point, old_loc->owner is still
12230 valid, as delete_breakpoint frees the breakpoint
12231 only after calling us. */
12232 printf_filtered (_("warning: Error removing "
12233 "breakpoint %d\n"),
12234 old_loc->owner->number);
12235 }
12236 removed = 1;
12237 }
12238 }
12239
12240 if (!found_object)
12241 {
12242 if (removed && target_is_non_stop_p ()
12243 && need_moribund_for_location_type (old_loc))
12244 {
12245 /* This location was removed from the target. In
12246 non-stop mode, a race condition is possible where
12247 we've removed a breakpoint, but stop events for that
12248 breakpoint are already queued and will arrive later.
12249 We apply an heuristic to be able to distinguish such
12250 SIGTRAPs from other random SIGTRAPs: we keep this
12251 breakpoint location for a bit, and will retire it
12252 after we see some number of events. The theory here
12253 is that reporting of events should, "on the average",
12254 be fair, so after a while we'll see events from all
12255 threads that have anything of interest, and no longer
12256 need to keep this breakpoint location around. We
12257 don't hold locations forever so to reduce chances of
12258 mistaking a non-breakpoint SIGTRAP for a breakpoint
12259 SIGTRAP.
12260
12261 The heuristic failing can be disastrous on
12262 decr_pc_after_break targets.
12263
12264 On decr_pc_after_break targets, like e.g., x86-linux,
12265 if we fail to recognize a late breakpoint SIGTRAP,
12266 because events_till_retirement has reached 0 too
12267 soon, we'll fail to do the PC adjustment, and report
12268 a random SIGTRAP to the user. When the user resumes
12269 the inferior, it will most likely immediately crash
12270 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12271 corrupted, because of being resumed e.g., in the
12272 middle of a multi-byte instruction, or skipped a
12273 one-byte instruction. This was actually seen happen
12274 on native x86-linux, and should be less rare on
12275 targets that do not support new thread events, like
12276 remote, due to the heuristic depending on
12277 thread_count.
12278
12279 Mistaking a random SIGTRAP for a breakpoint trap
12280 causes similar symptoms (PC adjustment applied when
12281 it shouldn't), but then again, playing with SIGTRAPs
12282 behind the debugger's back is asking for trouble.
12283
12284 Since hardware watchpoint traps are always
12285 distinguishable from other traps, so we don't need to
12286 apply keep hardware watchpoint moribund locations
12287 around. We simply always ignore hardware watchpoint
12288 traps we can no longer explain. */
12289
12290 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12291 old_loc->owner = NULL;
12292
12293 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12294 }
12295 else
12296 {
12297 old_loc->owner = NULL;
12298 decref_bp_location (&old_loc);
12299 }
12300 }
12301 }
12302
12303 /* Rescan breakpoints at the same address and section, marking the
12304 first one as "first" and any others as "duplicates". This is so
12305 that the bpt instruction is only inserted once. If we have a
12306 permanent breakpoint at the same place as BPT, make that one the
12307 official one, and the rest as duplicates. Permanent breakpoints
12308 are sorted first for the same address.
12309
12310 Do the same for hardware watchpoints, but also considering the
12311 watchpoint's type (regular/access/read) and length. */
12312
12313 bp_loc_first = NULL;
12314 wp_loc_first = NULL;
12315 awp_loc_first = NULL;
12316 rwp_loc_first = NULL;
12317 ALL_BP_LOCATIONS (loc, locp)
12318 {
12319 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12320 non-NULL. */
12321 struct bp_location **loc_first_p;
12322 b = loc->owner;
12323
12324 if (!unduplicated_should_be_inserted (loc)
12325 || !breakpoint_address_is_meaningful (b)
12326 /* Don't detect duplicate for tracepoint locations because they are
12327 never duplicated. See the comments in field `duplicate' of
12328 `struct bp_location'. */
12329 || is_tracepoint (b))
12330 {
12331 /* Clear the condition modification flag. */
12332 loc->condition_changed = condition_unchanged;
12333 continue;
12334 }
12335
12336 if (b->type == bp_hardware_watchpoint)
12337 loc_first_p = &wp_loc_first;
12338 else if (b->type == bp_read_watchpoint)
12339 loc_first_p = &rwp_loc_first;
12340 else if (b->type == bp_access_watchpoint)
12341 loc_first_p = &awp_loc_first;
12342 else
12343 loc_first_p = &bp_loc_first;
12344
12345 if (*loc_first_p == NULL
12346 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12347 || !breakpoint_locations_match (loc, *loc_first_p))
12348 {
12349 *loc_first_p = loc;
12350 loc->duplicate = 0;
12351
12352 if (is_breakpoint (loc->owner) && loc->condition_changed)
12353 {
12354 loc->needs_update = 1;
12355 /* Clear the condition modification flag. */
12356 loc->condition_changed = condition_unchanged;
12357 }
12358 continue;
12359 }
12360
12361
12362 /* This and the above ensure the invariant that the first location
12363 is not duplicated, and is the inserted one.
12364 All following are marked as duplicated, and are not inserted. */
12365 if (loc->inserted)
12366 swap_insertion (loc, *loc_first_p);
12367 loc->duplicate = 1;
12368
12369 /* Clear the condition modification flag. */
12370 loc->condition_changed = condition_unchanged;
12371 }
12372
12373 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12374 {
12375 if (insert_mode != UGLL_DONT_INSERT)
12376 insert_breakpoint_locations ();
12377 else
12378 {
12379 /* Even though the caller told us to not insert new
12380 locations, we may still need to update conditions on the
12381 target's side of breakpoints that were already inserted
12382 if the target is evaluating breakpoint conditions. We
12383 only update conditions for locations that are marked
12384 "needs_update". */
12385 update_inserted_breakpoint_locations ();
12386 }
12387 }
12388
12389 if (insert_mode != UGLL_DONT_INSERT)
12390 download_tracepoint_locations ();
12391
12392 do_cleanups (cleanups);
12393 }
12394
12395 void
12396 breakpoint_retire_moribund (void)
12397 {
12398 struct bp_location *loc;
12399 int ix;
12400
12401 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12402 if (--(loc->events_till_retirement) == 0)
12403 {
12404 decref_bp_location (&loc);
12405 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12406 --ix;
12407 }
12408 }
12409
12410 static void
12411 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12412 {
12413
12414 TRY
12415 {
12416 update_global_location_list (insert_mode);
12417 }
12418 CATCH (e, RETURN_MASK_ERROR)
12419 {
12420 }
12421 END_CATCH
12422 }
12423
12424 /* Clear BKP from a BPS. */
12425
12426 static void
12427 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12428 {
12429 bpstat bs;
12430
12431 for (bs = bps; bs; bs = bs->next)
12432 if (bs->breakpoint_at == bpt)
12433 {
12434 bs->breakpoint_at = NULL;
12435 bs->old_val = NULL;
12436 /* bs->commands will be freed later. */
12437 }
12438 }
12439
12440 /* Callback for iterate_over_threads. */
12441 static int
12442 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12443 {
12444 struct breakpoint *bpt = (struct breakpoint *) data;
12445
12446 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12447 return 0;
12448 }
12449
12450 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12451 callbacks. */
12452
12453 static void
12454 say_where (struct breakpoint *b)
12455 {
12456 struct value_print_options opts;
12457
12458 get_user_print_options (&opts);
12459
12460 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12461 single string. */
12462 if (b->loc == NULL)
12463 {
12464 /* For pending locations, the output differs slightly based
12465 on b->extra_string. If this is non-NULL, it contains either
12466 a condition or dprintf arguments. */
12467 if (b->extra_string == NULL)
12468 {
12469 printf_filtered (_(" (%s) pending."),
12470 event_location_to_string (b->location.get ()));
12471 }
12472 else if (b->type == bp_dprintf)
12473 {
12474 printf_filtered (_(" (%s,%s) pending."),
12475 event_location_to_string (b->location.get ()),
12476 b->extra_string);
12477 }
12478 else
12479 {
12480 printf_filtered (_(" (%s %s) pending."),
12481 event_location_to_string (b->location.get ()),
12482 b->extra_string);
12483 }
12484 }
12485 else
12486 {
12487 if (opts.addressprint || b->loc->symtab == NULL)
12488 {
12489 printf_filtered (" at ");
12490 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12491 gdb_stdout);
12492 }
12493 if (b->loc->symtab != NULL)
12494 {
12495 /* If there is a single location, we can print the location
12496 more nicely. */
12497 if (b->loc->next == NULL)
12498 printf_filtered (": file %s, line %d.",
12499 symtab_to_filename_for_display (b->loc->symtab),
12500 b->loc->line_number);
12501 else
12502 /* This is not ideal, but each location may have a
12503 different file name, and this at least reflects the
12504 real situation somewhat. */
12505 printf_filtered (": %s.",
12506 event_location_to_string (b->location.get ()));
12507 }
12508
12509 if (b->loc->next)
12510 {
12511 struct bp_location *loc = b->loc;
12512 int n = 0;
12513 for (; loc; loc = loc->next)
12514 ++n;
12515 printf_filtered (" (%d locations)", n);
12516 }
12517 }
12518 }
12519
12520 /* Default bp_location_ops methods. */
12521
12522 static void
12523 bp_location_dtor (struct bp_location *self)
12524 {
12525 xfree (self->function_name);
12526 }
12527
12528 static const struct bp_location_ops bp_location_ops =
12529 {
12530 bp_location_dtor
12531 };
12532
12533 /* Destructor for the breakpoint base class. */
12534
12535 breakpoint::~breakpoint ()
12536 {
12537 xfree (this->cond_string);
12538 xfree (this->extra_string);
12539 xfree (this->filter);
12540 }
12541
12542 static struct bp_location *
12543 base_breakpoint_allocate_location (struct breakpoint *self)
12544 {
12545 return new bp_location (&bp_location_ops, self);
12546 }
12547
12548 static void
12549 base_breakpoint_re_set (struct breakpoint *b)
12550 {
12551 /* Nothing to re-set. */
12552 }
12553
12554 #define internal_error_pure_virtual_called() \
12555 gdb_assert_not_reached ("pure virtual function called")
12556
12557 static int
12558 base_breakpoint_insert_location (struct bp_location *bl)
12559 {
12560 internal_error_pure_virtual_called ();
12561 }
12562
12563 static int
12564 base_breakpoint_remove_location (struct bp_location *bl,
12565 enum remove_bp_reason reason)
12566 {
12567 internal_error_pure_virtual_called ();
12568 }
12569
12570 static int
12571 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12572 struct address_space *aspace,
12573 CORE_ADDR bp_addr,
12574 const struct target_waitstatus *ws)
12575 {
12576 internal_error_pure_virtual_called ();
12577 }
12578
12579 static void
12580 base_breakpoint_check_status (bpstat bs)
12581 {
12582 /* Always stop. */
12583 }
12584
12585 /* A "works_in_software_mode" breakpoint_ops method that just internal
12586 errors. */
12587
12588 static int
12589 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12590 {
12591 internal_error_pure_virtual_called ();
12592 }
12593
12594 /* A "resources_needed" breakpoint_ops method that just internal
12595 errors. */
12596
12597 static int
12598 base_breakpoint_resources_needed (const struct bp_location *bl)
12599 {
12600 internal_error_pure_virtual_called ();
12601 }
12602
12603 static enum print_stop_action
12604 base_breakpoint_print_it (bpstat bs)
12605 {
12606 internal_error_pure_virtual_called ();
12607 }
12608
12609 static void
12610 base_breakpoint_print_one_detail (const struct breakpoint *self,
12611 struct ui_out *uiout)
12612 {
12613 /* nothing */
12614 }
12615
12616 static void
12617 base_breakpoint_print_mention (struct breakpoint *b)
12618 {
12619 internal_error_pure_virtual_called ();
12620 }
12621
12622 static void
12623 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12624 {
12625 internal_error_pure_virtual_called ();
12626 }
12627
12628 static void
12629 base_breakpoint_create_sals_from_location
12630 (const struct event_location *location,
12631 struct linespec_result *canonical,
12632 enum bptype type_wanted)
12633 {
12634 internal_error_pure_virtual_called ();
12635 }
12636
12637 static void
12638 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12639 struct linespec_result *c,
12640 gdb::unique_xmalloc_ptr<char> cond_string,
12641 gdb::unique_xmalloc_ptr<char> extra_string,
12642 enum bptype type_wanted,
12643 enum bpdisp disposition,
12644 int thread,
12645 int task, int ignore_count,
12646 const struct breakpoint_ops *o,
12647 int from_tty, int enabled,
12648 int internal, unsigned flags)
12649 {
12650 internal_error_pure_virtual_called ();
12651 }
12652
12653 static std::vector<symtab_and_line>
12654 base_breakpoint_decode_location (struct breakpoint *b,
12655 const struct event_location *location,
12656 struct program_space *search_pspace)
12657 {
12658 internal_error_pure_virtual_called ();
12659 }
12660
12661 /* The default 'explains_signal' method. */
12662
12663 static int
12664 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12665 {
12666 return 1;
12667 }
12668
12669 /* The default "after_condition_true" method. */
12670
12671 static void
12672 base_breakpoint_after_condition_true (struct bpstats *bs)
12673 {
12674 /* Nothing to do. */
12675 }
12676
12677 struct breakpoint_ops base_breakpoint_ops =
12678 {
12679 base_breakpoint_allocate_location,
12680 base_breakpoint_re_set,
12681 base_breakpoint_insert_location,
12682 base_breakpoint_remove_location,
12683 base_breakpoint_breakpoint_hit,
12684 base_breakpoint_check_status,
12685 base_breakpoint_resources_needed,
12686 base_breakpoint_works_in_software_mode,
12687 base_breakpoint_print_it,
12688 NULL,
12689 base_breakpoint_print_one_detail,
12690 base_breakpoint_print_mention,
12691 base_breakpoint_print_recreate,
12692 base_breakpoint_create_sals_from_location,
12693 base_breakpoint_create_breakpoints_sal,
12694 base_breakpoint_decode_location,
12695 base_breakpoint_explains_signal,
12696 base_breakpoint_after_condition_true,
12697 };
12698
12699 /* Default breakpoint_ops methods. */
12700
12701 static void
12702 bkpt_re_set (struct breakpoint *b)
12703 {
12704 /* FIXME: is this still reachable? */
12705 if (breakpoint_event_location_empty_p (b))
12706 {
12707 /* Anything without a location can't be re-set. */
12708 delete_breakpoint (b);
12709 return;
12710 }
12711
12712 breakpoint_re_set_default (b);
12713 }
12714
12715 static int
12716 bkpt_insert_location (struct bp_location *bl)
12717 {
12718 CORE_ADDR addr = bl->target_info.reqstd_address;
12719
12720 bl->target_info.kind = breakpoint_kind (bl, &addr);
12721 bl->target_info.placed_address = addr;
12722
12723 if (bl->loc_type == bp_loc_hardware_breakpoint)
12724 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12725 else
12726 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12727 }
12728
12729 static int
12730 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12731 {
12732 if (bl->loc_type == bp_loc_hardware_breakpoint)
12733 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12734 else
12735 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12736 }
12737
12738 static int
12739 bkpt_breakpoint_hit (const struct bp_location *bl,
12740 struct address_space *aspace, CORE_ADDR bp_addr,
12741 const struct target_waitstatus *ws)
12742 {
12743 if (ws->kind != TARGET_WAITKIND_STOPPED
12744 || ws->value.sig != GDB_SIGNAL_TRAP)
12745 return 0;
12746
12747 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12748 aspace, bp_addr))
12749 return 0;
12750
12751 if (overlay_debugging /* unmapped overlay section */
12752 && section_is_overlay (bl->section)
12753 && !section_is_mapped (bl->section))
12754 return 0;
12755
12756 return 1;
12757 }
12758
12759 static int
12760 dprintf_breakpoint_hit (const struct bp_location *bl,
12761 struct address_space *aspace, CORE_ADDR bp_addr,
12762 const struct target_waitstatus *ws)
12763 {
12764 if (dprintf_style == dprintf_style_agent
12765 && target_can_run_breakpoint_commands ())
12766 {
12767 /* An agent-style dprintf never causes a stop. If we see a trap
12768 for this address it must be for a breakpoint that happens to
12769 be set at the same address. */
12770 return 0;
12771 }
12772
12773 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12774 }
12775
12776 static int
12777 bkpt_resources_needed (const struct bp_location *bl)
12778 {
12779 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12780
12781 return 1;
12782 }
12783
12784 static enum print_stop_action
12785 bkpt_print_it (bpstat bs)
12786 {
12787 struct breakpoint *b;
12788 const struct bp_location *bl;
12789 int bp_temp;
12790 struct ui_out *uiout = current_uiout;
12791
12792 gdb_assert (bs->bp_location_at != NULL);
12793
12794 bl = bs->bp_location_at;
12795 b = bs->breakpoint_at;
12796
12797 bp_temp = b->disposition == disp_del;
12798 if (bl->address != bl->requested_address)
12799 breakpoint_adjustment_warning (bl->requested_address,
12800 bl->address,
12801 b->number, 1);
12802 annotate_breakpoint (b->number);
12803 maybe_print_thread_hit_breakpoint (uiout);
12804
12805 if (bp_temp)
12806 uiout->text ("Temporary breakpoint ");
12807 else
12808 uiout->text ("Breakpoint ");
12809 if (uiout->is_mi_like_p ())
12810 {
12811 uiout->field_string ("reason",
12812 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12813 uiout->field_string ("disp", bpdisp_text (b->disposition));
12814 }
12815 uiout->field_int ("bkptno", b->number);
12816 uiout->text (", ");
12817
12818 return PRINT_SRC_AND_LOC;
12819 }
12820
12821 static void
12822 bkpt_print_mention (struct breakpoint *b)
12823 {
12824 if (current_uiout->is_mi_like_p ())
12825 return;
12826
12827 switch (b->type)
12828 {
12829 case bp_breakpoint:
12830 case bp_gnu_ifunc_resolver:
12831 if (b->disposition == disp_del)
12832 printf_filtered (_("Temporary breakpoint"));
12833 else
12834 printf_filtered (_("Breakpoint"));
12835 printf_filtered (_(" %d"), b->number);
12836 if (b->type == bp_gnu_ifunc_resolver)
12837 printf_filtered (_(" at gnu-indirect-function resolver"));
12838 break;
12839 case bp_hardware_breakpoint:
12840 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12841 break;
12842 case bp_dprintf:
12843 printf_filtered (_("Dprintf %d"), b->number);
12844 break;
12845 }
12846
12847 say_where (b);
12848 }
12849
12850 static void
12851 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12852 {
12853 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12854 fprintf_unfiltered (fp, "tbreak");
12855 else if (tp->type == bp_breakpoint)
12856 fprintf_unfiltered (fp, "break");
12857 else if (tp->type == bp_hardware_breakpoint
12858 && tp->disposition == disp_del)
12859 fprintf_unfiltered (fp, "thbreak");
12860 else if (tp->type == bp_hardware_breakpoint)
12861 fprintf_unfiltered (fp, "hbreak");
12862 else
12863 internal_error (__FILE__, __LINE__,
12864 _("unhandled breakpoint type %d"), (int) tp->type);
12865
12866 fprintf_unfiltered (fp, " %s",
12867 event_location_to_string (tp->location.get ()));
12868
12869 /* Print out extra_string if this breakpoint is pending. It might
12870 contain, for example, conditions that were set by the user. */
12871 if (tp->loc == NULL && tp->extra_string != NULL)
12872 fprintf_unfiltered (fp, " %s", tp->extra_string);
12873
12874 print_recreate_thread (tp, fp);
12875 }
12876
12877 static void
12878 bkpt_create_sals_from_location (const struct event_location *location,
12879 struct linespec_result *canonical,
12880 enum bptype type_wanted)
12881 {
12882 create_sals_from_location_default (location, canonical, type_wanted);
12883 }
12884
12885 static void
12886 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12887 struct linespec_result *canonical,
12888 gdb::unique_xmalloc_ptr<char> cond_string,
12889 gdb::unique_xmalloc_ptr<char> extra_string,
12890 enum bptype type_wanted,
12891 enum bpdisp disposition,
12892 int thread,
12893 int task, int ignore_count,
12894 const struct breakpoint_ops *ops,
12895 int from_tty, int enabled,
12896 int internal, unsigned flags)
12897 {
12898 create_breakpoints_sal_default (gdbarch, canonical,
12899 std::move (cond_string),
12900 std::move (extra_string),
12901 type_wanted,
12902 disposition, thread, task,
12903 ignore_count, ops, from_tty,
12904 enabled, internal, flags);
12905 }
12906
12907 static std::vector<symtab_and_line>
12908 bkpt_decode_location (struct breakpoint *b,
12909 const struct event_location *location,
12910 struct program_space *search_pspace)
12911 {
12912 return decode_location_default (b, location, search_pspace);
12913 }
12914
12915 /* Virtual table for internal breakpoints. */
12916
12917 static void
12918 internal_bkpt_re_set (struct breakpoint *b)
12919 {
12920 switch (b->type)
12921 {
12922 /* Delete overlay event and longjmp master breakpoints; they
12923 will be reset later by breakpoint_re_set. */
12924 case bp_overlay_event:
12925 case bp_longjmp_master:
12926 case bp_std_terminate_master:
12927 case bp_exception_master:
12928 delete_breakpoint (b);
12929 break;
12930
12931 /* This breakpoint is special, it's set up when the inferior
12932 starts and we really don't want to touch it. */
12933 case bp_shlib_event:
12934
12935 /* Like bp_shlib_event, this breakpoint type is special. Once
12936 it is set up, we do not want to touch it. */
12937 case bp_thread_event:
12938 break;
12939 }
12940 }
12941
12942 static void
12943 internal_bkpt_check_status (bpstat bs)
12944 {
12945 if (bs->breakpoint_at->type == bp_shlib_event)
12946 {
12947 /* If requested, stop when the dynamic linker notifies GDB of
12948 events. This allows the user to get control and place
12949 breakpoints in initializer routines for dynamically loaded
12950 objects (among other things). */
12951 bs->stop = stop_on_solib_events;
12952 bs->print = stop_on_solib_events;
12953 }
12954 else
12955 bs->stop = 0;
12956 }
12957
12958 static enum print_stop_action
12959 internal_bkpt_print_it (bpstat bs)
12960 {
12961 struct breakpoint *b;
12962
12963 b = bs->breakpoint_at;
12964
12965 switch (b->type)
12966 {
12967 case bp_shlib_event:
12968 /* Did we stop because the user set the stop_on_solib_events
12969 variable? (If so, we report this as a generic, "Stopped due
12970 to shlib event" message.) */
12971 print_solib_event (0);
12972 break;
12973
12974 case bp_thread_event:
12975 /* Not sure how we will get here.
12976 GDB should not stop for these breakpoints. */
12977 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12978 break;
12979
12980 case bp_overlay_event:
12981 /* By analogy with the thread event, GDB should not stop for these. */
12982 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12983 break;
12984
12985 case bp_longjmp_master:
12986 /* These should never be enabled. */
12987 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12988 break;
12989
12990 case bp_std_terminate_master:
12991 /* These should never be enabled. */
12992 printf_filtered (_("std::terminate Master Breakpoint: "
12993 "gdb should not stop!\n"));
12994 break;
12995
12996 case bp_exception_master:
12997 /* These should never be enabled. */
12998 printf_filtered (_("Exception Master Breakpoint: "
12999 "gdb should not stop!\n"));
13000 break;
13001 }
13002
13003 return PRINT_NOTHING;
13004 }
13005
13006 static void
13007 internal_bkpt_print_mention (struct breakpoint *b)
13008 {
13009 /* Nothing to mention. These breakpoints are internal. */
13010 }
13011
13012 /* Virtual table for momentary breakpoints */
13013
13014 static void
13015 momentary_bkpt_re_set (struct breakpoint *b)
13016 {
13017 /* Keep temporary breakpoints, which can be encountered when we step
13018 over a dlopen call and solib_add is resetting the breakpoints.
13019 Otherwise these should have been blown away via the cleanup chain
13020 or by breakpoint_init_inferior when we rerun the executable. */
13021 }
13022
13023 static void
13024 momentary_bkpt_check_status (bpstat bs)
13025 {
13026 /* Nothing. The point of these breakpoints is causing a stop. */
13027 }
13028
13029 static enum print_stop_action
13030 momentary_bkpt_print_it (bpstat bs)
13031 {
13032 return PRINT_UNKNOWN;
13033 }
13034
13035 static void
13036 momentary_bkpt_print_mention (struct breakpoint *b)
13037 {
13038 /* Nothing to mention. These breakpoints are internal. */
13039 }
13040
13041 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13042
13043 It gets cleared already on the removal of the first one of such placed
13044 breakpoints. This is OK as they get all removed altogether. */
13045
13046 longjmp_breakpoint::~longjmp_breakpoint ()
13047 {
13048 thread_info *tp = find_thread_global_id (this->thread);
13049
13050 if (tp != NULL)
13051 tp->initiating_frame = null_frame_id;
13052 }
13053
13054 /* Specific methods for probe breakpoints. */
13055
13056 static int
13057 bkpt_probe_insert_location (struct bp_location *bl)
13058 {
13059 int v = bkpt_insert_location (bl);
13060
13061 if (v == 0)
13062 {
13063 /* The insertion was successful, now let's set the probe's semaphore
13064 if needed. */
13065 if (bl->probe.probe->pops->set_semaphore != NULL)
13066 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13067 bl->probe.objfile,
13068 bl->gdbarch);
13069 }
13070
13071 return v;
13072 }
13073
13074 static int
13075 bkpt_probe_remove_location (struct bp_location *bl,
13076 enum remove_bp_reason reason)
13077 {
13078 /* Let's clear the semaphore before removing the location. */
13079 if (bl->probe.probe->pops->clear_semaphore != NULL)
13080 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13081 bl->probe.objfile,
13082 bl->gdbarch);
13083
13084 return bkpt_remove_location (bl, reason);
13085 }
13086
13087 static void
13088 bkpt_probe_create_sals_from_location (const struct event_location *location,
13089 struct linespec_result *canonical,
13090 enum bptype type_wanted)
13091 {
13092 struct linespec_sals lsal;
13093
13094 lsal.sals = parse_probes (location, NULL, canonical);
13095 lsal.canonical
13096 = xstrdup (event_location_to_string (canonical->location.get ()));
13097 canonical->lsals.push_back (std::move (lsal));
13098 }
13099
13100 static std::vector<symtab_and_line>
13101 bkpt_probe_decode_location (struct breakpoint *b,
13102 const struct event_location *location,
13103 struct program_space *search_pspace)
13104 {
13105 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
13106 if (sals.empty ())
13107 error (_("probe not found"));
13108 return sals;
13109 }
13110
13111 /* The breakpoint_ops structure to be used in tracepoints. */
13112
13113 static void
13114 tracepoint_re_set (struct breakpoint *b)
13115 {
13116 breakpoint_re_set_default (b);
13117 }
13118
13119 static int
13120 tracepoint_breakpoint_hit (const struct bp_location *bl,
13121 struct address_space *aspace, CORE_ADDR bp_addr,
13122 const struct target_waitstatus *ws)
13123 {
13124 /* By definition, the inferior does not report stops at
13125 tracepoints. */
13126 return 0;
13127 }
13128
13129 static void
13130 tracepoint_print_one_detail (const struct breakpoint *self,
13131 struct ui_out *uiout)
13132 {
13133 struct tracepoint *tp = (struct tracepoint *) self;
13134 if (tp->static_trace_marker_id)
13135 {
13136 gdb_assert (self->type == bp_static_tracepoint);
13137
13138 uiout->text ("\tmarker id is ");
13139 uiout->field_string ("static-tracepoint-marker-string-id",
13140 tp->static_trace_marker_id);
13141 uiout->text ("\n");
13142 }
13143 }
13144
13145 static void
13146 tracepoint_print_mention (struct breakpoint *b)
13147 {
13148 if (current_uiout->is_mi_like_p ())
13149 return;
13150
13151 switch (b->type)
13152 {
13153 case bp_tracepoint:
13154 printf_filtered (_("Tracepoint"));
13155 printf_filtered (_(" %d"), b->number);
13156 break;
13157 case bp_fast_tracepoint:
13158 printf_filtered (_("Fast tracepoint"));
13159 printf_filtered (_(" %d"), b->number);
13160 break;
13161 case bp_static_tracepoint:
13162 printf_filtered (_("Static tracepoint"));
13163 printf_filtered (_(" %d"), b->number);
13164 break;
13165 default:
13166 internal_error (__FILE__, __LINE__,
13167 _("unhandled tracepoint type %d"), (int) b->type);
13168 }
13169
13170 say_where (b);
13171 }
13172
13173 static void
13174 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13175 {
13176 struct tracepoint *tp = (struct tracepoint *) self;
13177
13178 if (self->type == bp_fast_tracepoint)
13179 fprintf_unfiltered (fp, "ftrace");
13180 else if (self->type == bp_static_tracepoint)
13181 fprintf_unfiltered (fp, "strace");
13182 else if (self->type == bp_tracepoint)
13183 fprintf_unfiltered (fp, "trace");
13184 else
13185 internal_error (__FILE__, __LINE__,
13186 _("unhandled tracepoint type %d"), (int) self->type);
13187
13188 fprintf_unfiltered (fp, " %s",
13189 event_location_to_string (self->location.get ()));
13190 print_recreate_thread (self, fp);
13191
13192 if (tp->pass_count)
13193 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13194 }
13195
13196 static void
13197 tracepoint_create_sals_from_location (const struct event_location *location,
13198 struct linespec_result *canonical,
13199 enum bptype type_wanted)
13200 {
13201 create_sals_from_location_default (location, canonical, type_wanted);
13202 }
13203
13204 static void
13205 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13206 struct linespec_result *canonical,
13207 gdb::unique_xmalloc_ptr<char> cond_string,
13208 gdb::unique_xmalloc_ptr<char> extra_string,
13209 enum bptype type_wanted,
13210 enum bpdisp disposition,
13211 int thread,
13212 int task, int ignore_count,
13213 const struct breakpoint_ops *ops,
13214 int from_tty, int enabled,
13215 int internal, unsigned flags)
13216 {
13217 create_breakpoints_sal_default (gdbarch, canonical,
13218 std::move (cond_string),
13219 std::move (extra_string),
13220 type_wanted,
13221 disposition, thread, task,
13222 ignore_count, ops, from_tty,
13223 enabled, internal, flags);
13224 }
13225
13226 static std::vector<symtab_and_line>
13227 tracepoint_decode_location (struct breakpoint *b,
13228 const struct event_location *location,
13229 struct program_space *search_pspace)
13230 {
13231 return decode_location_default (b, location, search_pspace);
13232 }
13233
13234 struct breakpoint_ops tracepoint_breakpoint_ops;
13235
13236 /* The breakpoint_ops structure to be use on tracepoints placed in a
13237 static probe. */
13238
13239 static void
13240 tracepoint_probe_create_sals_from_location
13241 (const struct event_location *location,
13242 struct linespec_result *canonical,
13243 enum bptype type_wanted)
13244 {
13245 /* We use the same method for breakpoint on probes. */
13246 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13247 }
13248
13249 static std::vector<symtab_and_line>
13250 tracepoint_probe_decode_location (struct breakpoint *b,
13251 const struct event_location *location,
13252 struct program_space *search_pspace)
13253 {
13254 /* We use the same method for breakpoint on probes. */
13255 return bkpt_probe_decode_location (b, location, search_pspace);
13256 }
13257
13258 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13259
13260 /* Dprintf breakpoint_ops methods. */
13261
13262 static void
13263 dprintf_re_set (struct breakpoint *b)
13264 {
13265 breakpoint_re_set_default (b);
13266
13267 /* extra_string should never be non-NULL for dprintf. */
13268 gdb_assert (b->extra_string != NULL);
13269
13270 /* 1 - connect to target 1, that can run breakpoint commands.
13271 2 - create a dprintf, which resolves fine.
13272 3 - disconnect from target 1
13273 4 - connect to target 2, that can NOT run breakpoint commands.
13274
13275 After steps #3/#4, you'll want the dprintf command list to
13276 be updated, because target 1 and 2 may well return different
13277 answers for target_can_run_breakpoint_commands().
13278 Given absence of finer grained resetting, we get to do
13279 it all the time. */
13280 if (b->extra_string != NULL)
13281 update_dprintf_command_list (b);
13282 }
13283
13284 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13285
13286 static void
13287 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13288 {
13289 fprintf_unfiltered (fp, "dprintf %s,%s",
13290 event_location_to_string (tp->location.get ()),
13291 tp->extra_string);
13292 print_recreate_thread (tp, fp);
13293 }
13294
13295 /* Implement the "after_condition_true" breakpoint_ops method for
13296 dprintf.
13297
13298 dprintf's are implemented with regular commands in their command
13299 list, but we run the commands here instead of before presenting the
13300 stop to the user, as dprintf's don't actually cause a stop. This
13301 also makes it so that the commands of multiple dprintfs at the same
13302 address are all handled. */
13303
13304 static void
13305 dprintf_after_condition_true (struct bpstats *bs)
13306 {
13307 struct bpstats tmp_bs;
13308 struct bpstats *tmp_bs_p = &tmp_bs;
13309
13310 /* dprintf's never cause a stop. This wasn't set in the
13311 check_status hook instead because that would make the dprintf's
13312 condition not be evaluated. */
13313 bs->stop = 0;
13314
13315 /* Run the command list here. Take ownership of it instead of
13316 copying. We never want these commands to run later in
13317 bpstat_do_actions, if a breakpoint that causes a stop happens to
13318 be set at same address as this dprintf, or even if running the
13319 commands here throws. */
13320 tmp_bs.commands = bs->commands;
13321 bs->commands = NULL;
13322
13323 bpstat_do_actions_1 (&tmp_bs_p);
13324
13325 /* 'tmp_bs.commands' will usually be NULL by now, but
13326 bpstat_do_actions_1 may return early without processing the whole
13327 list. */
13328 }
13329
13330 /* The breakpoint_ops structure to be used on static tracepoints with
13331 markers (`-m'). */
13332
13333 static void
13334 strace_marker_create_sals_from_location (const struct event_location *location,
13335 struct linespec_result *canonical,
13336 enum bptype type_wanted)
13337 {
13338 struct linespec_sals lsal;
13339 const char *arg_start, *arg;
13340 char *str;
13341 struct cleanup *cleanup;
13342
13343 arg = arg_start = get_linespec_location (location);
13344 lsal.sals = decode_static_tracepoint_spec (&arg);
13345
13346 str = savestring (arg_start, arg - arg_start);
13347 cleanup = make_cleanup (xfree, str);
13348 canonical->location = new_linespec_location (&str);
13349 do_cleanups (cleanup);
13350
13351 lsal.canonical
13352 = xstrdup (event_location_to_string (canonical->location.get ()));
13353 canonical->lsals.push_back (std::move (lsal));
13354 }
13355
13356 static void
13357 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13358 struct linespec_result *canonical,
13359 gdb::unique_xmalloc_ptr<char> cond_string,
13360 gdb::unique_xmalloc_ptr<char> extra_string,
13361 enum bptype type_wanted,
13362 enum bpdisp disposition,
13363 int thread,
13364 int task, int ignore_count,
13365 const struct breakpoint_ops *ops,
13366 int from_tty, int enabled,
13367 int internal, unsigned flags)
13368 {
13369 const linespec_sals &lsal = canonical->lsals[0];
13370
13371 /* If the user is creating a static tracepoint by marker id
13372 (strace -m MARKER_ID), then store the sals index, so that
13373 breakpoint_re_set can try to match up which of the newly
13374 found markers corresponds to this one, and, don't try to
13375 expand multiple locations for each sal, given than SALS
13376 already should contain all sals for MARKER_ID. */
13377
13378 for (size_t i = 0; i < lsal.sals.size (); i++)
13379 {
13380 event_location_up location
13381 = copy_event_location (canonical->location.get ());
13382
13383 std::unique_ptr<tracepoint> tp (new tracepoint ());
13384 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13385 std::move (location), NULL,
13386 std::move (cond_string),
13387 std::move (extra_string),
13388 type_wanted, disposition,
13389 thread, task, ignore_count, ops,
13390 from_tty, enabled, internal, flags,
13391 canonical->special_display);
13392 /* Given that its possible to have multiple markers with
13393 the same string id, if the user is creating a static
13394 tracepoint by marker id ("strace -m MARKER_ID"), then
13395 store the sals index, so that breakpoint_re_set can
13396 try to match up which of the newly found markers
13397 corresponds to this one */
13398 tp->static_trace_marker_id_idx = i;
13399
13400 install_breakpoint (internal, std::move (tp), 0);
13401 }
13402 }
13403
13404 static std::vector<symtab_and_line>
13405 strace_marker_decode_location (struct breakpoint *b,
13406 const struct event_location *location,
13407 struct program_space *search_pspace)
13408 {
13409 struct tracepoint *tp = (struct tracepoint *) b;
13410 const char *s = get_linespec_location (location);
13411
13412 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13413 if (sals.size () > tp->static_trace_marker_id_idx)
13414 {
13415 sals[0] = sals[tp->static_trace_marker_id_idx];
13416 sals.resize (1);
13417 return sals;
13418 }
13419 else
13420 error (_("marker %s not found"), tp->static_trace_marker_id);
13421 }
13422
13423 static struct breakpoint_ops strace_marker_breakpoint_ops;
13424
13425 static int
13426 strace_marker_p (struct breakpoint *b)
13427 {
13428 return b->ops == &strace_marker_breakpoint_ops;
13429 }
13430
13431 /* Delete a breakpoint and clean up all traces of it in the data
13432 structures. */
13433
13434 void
13435 delete_breakpoint (struct breakpoint *bpt)
13436 {
13437 struct breakpoint *b;
13438
13439 gdb_assert (bpt != NULL);
13440
13441 /* Has this bp already been deleted? This can happen because
13442 multiple lists can hold pointers to bp's. bpstat lists are
13443 especial culprits.
13444
13445 One example of this happening is a watchpoint's scope bp. When
13446 the scope bp triggers, we notice that the watchpoint is out of
13447 scope, and delete it. We also delete its scope bp. But the
13448 scope bp is marked "auto-deleting", and is already on a bpstat.
13449 That bpstat is then checked for auto-deleting bp's, which are
13450 deleted.
13451
13452 A real solution to this problem might involve reference counts in
13453 bp's, and/or giving them pointers back to their referencing
13454 bpstat's, and teaching delete_breakpoint to only free a bp's
13455 storage when no more references were extent. A cheaper bandaid
13456 was chosen. */
13457 if (bpt->type == bp_none)
13458 return;
13459
13460 /* At least avoid this stale reference until the reference counting
13461 of breakpoints gets resolved. */
13462 if (bpt->related_breakpoint != bpt)
13463 {
13464 struct breakpoint *related;
13465 struct watchpoint *w;
13466
13467 if (bpt->type == bp_watchpoint_scope)
13468 w = (struct watchpoint *) bpt->related_breakpoint;
13469 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13470 w = (struct watchpoint *) bpt;
13471 else
13472 w = NULL;
13473 if (w != NULL)
13474 watchpoint_del_at_next_stop (w);
13475
13476 /* Unlink bpt from the bpt->related_breakpoint ring. */
13477 for (related = bpt; related->related_breakpoint != bpt;
13478 related = related->related_breakpoint);
13479 related->related_breakpoint = bpt->related_breakpoint;
13480 bpt->related_breakpoint = bpt;
13481 }
13482
13483 /* watch_command_1 creates a watchpoint but only sets its number if
13484 update_watchpoint succeeds in creating its bp_locations. If there's
13485 a problem in that process, we'll be asked to delete the half-created
13486 watchpoint. In that case, don't announce the deletion. */
13487 if (bpt->number)
13488 observer_notify_breakpoint_deleted (bpt);
13489
13490 if (breakpoint_chain == bpt)
13491 breakpoint_chain = bpt->next;
13492
13493 ALL_BREAKPOINTS (b)
13494 if (b->next == bpt)
13495 {
13496 b->next = bpt->next;
13497 break;
13498 }
13499
13500 /* Be sure no bpstat's are pointing at the breakpoint after it's
13501 been freed. */
13502 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13503 in all threads for now. Note that we cannot just remove bpstats
13504 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13505 commands are associated with the bpstat; if we remove it here,
13506 then the later call to bpstat_do_actions (&stop_bpstat); in
13507 event-top.c won't do anything, and temporary breakpoints with
13508 commands won't work. */
13509
13510 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13511
13512 /* Now that breakpoint is removed from breakpoint list, update the
13513 global location list. This will remove locations that used to
13514 belong to this breakpoint. Do this before freeing the breakpoint
13515 itself, since remove_breakpoint looks at location's owner. It
13516 might be better design to have location completely
13517 self-contained, but it's not the case now. */
13518 update_global_location_list (UGLL_DONT_INSERT);
13519
13520 /* On the chance that someone will soon try again to delete this
13521 same bp, we mark it as deleted before freeing its storage. */
13522 bpt->type = bp_none;
13523 delete bpt;
13524 }
13525
13526 static void
13527 do_delete_breakpoint_cleanup (void *b)
13528 {
13529 delete_breakpoint ((struct breakpoint *) b);
13530 }
13531
13532 struct cleanup *
13533 make_cleanup_delete_breakpoint (struct breakpoint *b)
13534 {
13535 return make_cleanup (do_delete_breakpoint_cleanup, b);
13536 }
13537
13538 /* Iterator function to call a user-provided callback function once
13539 for each of B and its related breakpoints. */
13540
13541 static void
13542 iterate_over_related_breakpoints (struct breakpoint *b,
13543 gdb::function_view<void (breakpoint *)> function)
13544 {
13545 struct breakpoint *related;
13546
13547 related = b;
13548 do
13549 {
13550 struct breakpoint *next;
13551
13552 /* FUNCTION may delete RELATED. */
13553 next = related->related_breakpoint;
13554
13555 if (next == related)
13556 {
13557 /* RELATED is the last ring entry. */
13558 function (related);
13559
13560 /* FUNCTION may have deleted it, so we'd never reach back to
13561 B. There's nothing left to do anyway, so just break
13562 out. */
13563 break;
13564 }
13565 else
13566 function (related);
13567
13568 related = next;
13569 }
13570 while (related != b);
13571 }
13572
13573 void
13574 delete_command (char *arg, int from_tty)
13575 {
13576 struct breakpoint *b, *b_tmp;
13577
13578 dont_repeat ();
13579
13580 if (arg == 0)
13581 {
13582 int breaks_to_delete = 0;
13583
13584 /* Delete all breakpoints if no argument. Do not delete
13585 internal breakpoints, these have to be deleted with an
13586 explicit breakpoint number argument. */
13587 ALL_BREAKPOINTS (b)
13588 if (user_breakpoint_p (b))
13589 {
13590 breaks_to_delete = 1;
13591 break;
13592 }
13593
13594 /* Ask user only if there are some breakpoints to delete. */
13595 if (!from_tty
13596 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13597 {
13598 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13599 if (user_breakpoint_p (b))
13600 delete_breakpoint (b);
13601 }
13602 }
13603 else
13604 map_breakpoint_numbers
13605 (arg, [&] (breakpoint *b)
13606 {
13607 iterate_over_related_breakpoints (b, delete_breakpoint);
13608 });
13609 }
13610
13611 /* Return true if all locations of B bound to PSPACE are pending. If
13612 PSPACE is NULL, all locations of all program spaces are
13613 considered. */
13614
13615 static int
13616 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13617 {
13618 struct bp_location *loc;
13619
13620 for (loc = b->loc; loc != NULL; loc = loc->next)
13621 if ((pspace == NULL
13622 || loc->pspace == pspace)
13623 && !loc->shlib_disabled
13624 && !loc->pspace->executing_startup)
13625 return 0;
13626 return 1;
13627 }
13628
13629 /* Subroutine of update_breakpoint_locations to simplify it.
13630 Return non-zero if multiple fns in list LOC have the same name.
13631 Null names are ignored. */
13632
13633 static int
13634 ambiguous_names_p (struct bp_location *loc)
13635 {
13636 struct bp_location *l;
13637 htab_t htab = htab_create_alloc (13, htab_hash_string,
13638 (int (*) (const void *,
13639 const void *)) streq,
13640 NULL, xcalloc, xfree);
13641
13642 for (l = loc; l != NULL; l = l->next)
13643 {
13644 const char **slot;
13645 const char *name = l->function_name;
13646
13647 /* Allow for some names to be NULL, ignore them. */
13648 if (name == NULL)
13649 continue;
13650
13651 slot = (const char **) htab_find_slot (htab, (const void *) name,
13652 INSERT);
13653 /* NOTE: We can assume slot != NULL here because xcalloc never
13654 returns NULL. */
13655 if (*slot != NULL)
13656 {
13657 htab_delete (htab);
13658 return 1;
13659 }
13660 *slot = name;
13661 }
13662
13663 htab_delete (htab);
13664 return 0;
13665 }
13666
13667 /* When symbols change, it probably means the sources changed as well,
13668 and it might mean the static tracepoint markers are no longer at
13669 the same address or line numbers they used to be at last we
13670 checked. Losing your static tracepoints whenever you rebuild is
13671 undesirable. This function tries to resync/rematch gdb static
13672 tracepoints with the markers on the target, for static tracepoints
13673 that have not been set by marker id. Static tracepoint that have
13674 been set by marker id are reset by marker id in breakpoint_re_set.
13675 The heuristic is:
13676
13677 1) For a tracepoint set at a specific address, look for a marker at
13678 the old PC. If one is found there, assume to be the same marker.
13679 If the name / string id of the marker found is different from the
13680 previous known name, assume that means the user renamed the marker
13681 in the sources, and output a warning.
13682
13683 2) For a tracepoint set at a given line number, look for a marker
13684 at the new address of the old line number. If one is found there,
13685 assume to be the same marker. If the name / string id of the
13686 marker found is different from the previous known name, assume that
13687 means the user renamed the marker in the sources, and output a
13688 warning.
13689
13690 3) If a marker is no longer found at the same address or line, it
13691 may mean the marker no longer exists. But it may also just mean
13692 the code changed a bit. Maybe the user added a few lines of code
13693 that made the marker move up or down (in line number terms). Ask
13694 the target for info about the marker with the string id as we knew
13695 it. If found, update line number and address in the matching
13696 static tracepoint. This will get confused if there's more than one
13697 marker with the same ID (possible in UST, although unadvised
13698 precisely because it confuses tools). */
13699
13700 static struct symtab_and_line
13701 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13702 {
13703 struct tracepoint *tp = (struct tracepoint *) b;
13704 struct static_tracepoint_marker marker;
13705 CORE_ADDR pc;
13706
13707 pc = sal.pc;
13708 if (sal.line)
13709 find_line_pc (sal.symtab, sal.line, &pc);
13710
13711 if (target_static_tracepoint_marker_at (pc, &marker))
13712 {
13713 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13714 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13715 b->number,
13716 tp->static_trace_marker_id, marker.str_id);
13717
13718 xfree (tp->static_trace_marker_id);
13719 tp->static_trace_marker_id = xstrdup (marker.str_id);
13720 release_static_tracepoint_marker (&marker);
13721
13722 return sal;
13723 }
13724
13725 /* Old marker wasn't found on target at lineno. Try looking it up
13726 by string ID. */
13727 if (!sal.explicit_pc
13728 && sal.line != 0
13729 && sal.symtab != NULL
13730 && tp->static_trace_marker_id != NULL)
13731 {
13732 VEC(static_tracepoint_marker_p) *markers;
13733
13734 markers
13735 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13736
13737 if (!VEC_empty(static_tracepoint_marker_p, markers))
13738 {
13739 struct symbol *sym;
13740 struct static_tracepoint_marker *tpmarker;
13741 struct ui_out *uiout = current_uiout;
13742 struct explicit_location explicit_loc;
13743
13744 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13745
13746 xfree (tp->static_trace_marker_id);
13747 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13748
13749 warning (_("marker for static tracepoint %d (%s) not "
13750 "found at previous line number"),
13751 b->number, tp->static_trace_marker_id);
13752
13753 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13754 sym = find_pc_sect_function (tpmarker->address, NULL);
13755 uiout->text ("Now in ");
13756 if (sym)
13757 {
13758 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13759 uiout->text (" at ");
13760 }
13761 uiout->field_string ("file",
13762 symtab_to_filename_for_display (sal2.symtab));
13763 uiout->text (":");
13764
13765 if (uiout->is_mi_like_p ())
13766 {
13767 const char *fullname = symtab_to_fullname (sal2.symtab);
13768
13769 uiout->field_string ("fullname", fullname);
13770 }
13771
13772 uiout->field_int ("line", sal2.line);
13773 uiout->text ("\n");
13774
13775 b->loc->line_number = sal2.line;
13776 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13777
13778 b->location.reset (NULL);
13779 initialize_explicit_location (&explicit_loc);
13780 explicit_loc.source_filename
13781 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13782 explicit_loc.line_offset.offset = b->loc->line_number;
13783 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13784 b->location = new_explicit_location (&explicit_loc);
13785
13786 /* Might be nice to check if function changed, and warn if
13787 so. */
13788
13789 release_static_tracepoint_marker (tpmarker);
13790 }
13791 }
13792 return sal;
13793 }
13794
13795 /* Returns 1 iff locations A and B are sufficiently same that
13796 we don't need to report breakpoint as changed. */
13797
13798 static int
13799 locations_are_equal (struct bp_location *a, struct bp_location *b)
13800 {
13801 while (a && b)
13802 {
13803 if (a->address != b->address)
13804 return 0;
13805
13806 if (a->shlib_disabled != b->shlib_disabled)
13807 return 0;
13808
13809 if (a->enabled != b->enabled)
13810 return 0;
13811
13812 a = a->next;
13813 b = b->next;
13814 }
13815
13816 if ((a == NULL) != (b == NULL))
13817 return 0;
13818
13819 return 1;
13820 }
13821
13822 /* Split all locations of B that are bound to PSPACE out of B's
13823 location list to a separate list and return that list's head. If
13824 PSPACE is NULL, hoist out all locations of B. */
13825
13826 static struct bp_location *
13827 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13828 {
13829 struct bp_location head;
13830 struct bp_location *i = b->loc;
13831 struct bp_location **i_link = &b->loc;
13832 struct bp_location *hoisted = &head;
13833
13834 if (pspace == NULL)
13835 {
13836 i = b->loc;
13837 b->loc = NULL;
13838 return i;
13839 }
13840
13841 head.next = NULL;
13842
13843 while (i != NULL)
13844 {
13845 if (i->pspace == pspace)
13846 {
13847 *i_link = i->next;
13848 i->next = NULL;
13849 hoisted->next = i;
13850 hoisted = i;
13851 }
13852 else
13853 i_link = &i->next;
13854 i = *i_link;
13855 }
13856
13857 return head.next;
13858 }
13859
13860 /* Create new breakpoint locations for B (a hardware or software
13861 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13862 zero, then B is a ranged breakpoint. Only recreates locations for
13863 FILTER_PSPACE. Locations of other program spaces are left
13864 untouched. */
13865
13866 void
13867 update_breakpoint_locations (struct breakpoint *b,
13868 struct program_space *filter_pspace,
13869 gdb::array_view<const symtab_and_line> sals,
13870 gdb::array_view<const symtab_and_line> sals_end)
13871 {
13872 int i;
13873 struct bp_location *existing_locations;
13874
13875 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13876 {
13877 /* Ranged breakpoints have only one start location and one end
13878 location. */
13879 b->enable_state = bp_disabled;
13880 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13881 "multiple locations found\n"),
13882 b->number);
13883 return;
13884 }
13885
13886 /* If there's no new locations, and all existing locations are
13887 pending, don't do anything. This optimizes the common case where
13888 all locations are in the same shared library, that was unloaded.
13889 We'd like to retain the location, so that when the library is
13890 loaded again, we don't loose the enabled/disabled status of the
13891 individual locations. */
13892 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13893 return;
13894
13895 existing_locations = hoist_existing_locations (b, filter_pspace);
13896
13897 for (const auto &sal : sals)
13898 {
13899 struct bp_location *new_loc;
13900
13901 switch_to_program_space_and_thread (sal.pspace);
13902
13903 new_loc = add_location_to_breakpoint (b, &sal);
13904
13905 /* Reparse conditions, they might contain references to the
13906 old symtab. */
13907 if (b->cond_string != NULL)
13908 {
13909 const char *s;
13910
13911 s = b->cond_string;
13912 TRY
13913 {
13914 new_loc->cond = parse_exp_1 (&s, sal.pc,
13915 block_for_pc (sal.pc),
13916 0);
13917 }
13918 CATCH (e, RETURN_MASK_ERROR)
13919 {
13920 warning (_("failed to reevaluate condition "
13921 "for breakpoint %d: %s"),
13922 b->number, e.message);
13923 new_loc->enabled = 0;
13924 }
13925 END_CATCH
13926 }
13927
13928 if (!sals_end.empty ())
13929 {
13930 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13931
13932 new_loc->length = end - sals[0].pc + 1;
13933 }
13934 }
13935
13936 /* If possible, carry over 'disable' status from existing
13937 breakpoints. */
13938 {
13939 struct bp_location *e = existing_locations;
13940 /* If there are multiple breakpoints with the same function name,
13941 e.g. for inline functions, comparing function names won't work.
13942 Instead compare pc addresses; this is just a heuristic as things
13943 may have moved, but in practice it gives the correct answer
13944 often enough until a better solution is found. */
13945 int have_ambiguous_names = ambiguous_names_p (b->loc);
13946
13947 for (; e; e = e->next)
13948 {
13949 if (!e->enabled && e->function_name)
13950 {
13951 struct bp_location *l = b->loc;
13952 if (have_ambiguous_names)
13953 {
13954 for (; l; l = l->next)
13955 if (breakpoint_locations_match (e, l))
13956 {
13957 l->enabled = 0;
13958 break;
13959 }
13960 }
13961 else
13962 {
13963 for (; l; l = l->next)
13964 if (l->function_name
13965 && strcmp (e->function_name, l->function_name) == 0)
13966 {
13967 l->enabled = 0;
13968 break;
13969 }
13970 }
13971 }
13972 }
13973 }
13974
13975 if (!locations_are_equal (existing_locations, b->loc))
13976 observer_notify_breakpoint_modified (b);
13977 }
13978
13979 /* Find the SaL locations corresponding to the given LOCATION.
13980 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13981
13982 static std::vector<symtab_and_line>
13983 location_to_sals (struct breakpoint *b, struct event_location *location,
13984 struct program_space *search_pspace, int *found)
13985 {
13986 struct gdb_exception exception = exception_none;
13987
13988 gdb_assert (b->ops != NULL);
13989
13990 std::vector<symtab_and_line> sals;
13991
13992 TRY
13993 {
13994 sals = b->ops->decode_location (b, location, search_pspace);
13995 }
13996 CATCH (e, RETURN_MASK_ERROR)
13997 {
13998 int not_found_and_ok = 0;
13999
14000 exception = e;
14001
14002 /* For pending breakpoints, it's expected that parsing will
14003 fail until the right shared library is loaded. User has
14004 already told to create pending breakpoints and don't need
14005 extra messages. If breakpoint is in bp_shlib_disabled
14006 state, then user already saw the message about that
14007 breakpoint being disabled, and don't want to see more
14008 errors. */
14009 if (e.error == NOT_FOUND_ERROR
14010 && (b->condition_not_parsed
14011 || (b->loc != NULL
14012 && search_pspace != NULL
14013 && b->loc->pspace != search_pspace)
14014 || (b->loc && b->loc->shlib_disabled)
14015 || (b->loc && b->loc->pspace->executing_startup)
14016 || b->enable_state == bp_disabled))
14017 not_found_and_ok = 1;
14018
14019 if (!not_found_and_ok)
14020 {
14021 /* We surely don't want to warn about the same breakpoint
14022 10 times. One solution, implemented here, is disable
14023 the breakpoint on error. Another solution would be to
14024 have separate 'warning emitted' flag. Since this
14025 happens only when a binary has changed, I don't know
14026 which approach is better. */
14027 b->enable_state = bp_disabled;
14028 throw_exception (e);
14029 }
14030 }
14031 END_CATCH
14032
14033 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14034 {
14035 for (auto &sal : sals)
14036 resolve_sal_pc (&sal);
14037 if (b->condition_not_parsed && b->extra_string != NULL)
14038 {
14039 char *cond_string, *extra_string;
14040 int thread, task;
14041
14042 find_condition_and_thread (b->extra_string, sals[0].pc,
14043 &cond_string, &thread, &task,
14044 &extra_string);
14045 gdb_assert (b->cond_string == NULL);
14046 if (cond_string)
14047 b->cond_string = cond_string;
14048 b->thread = thread;
14049 b->task = task;
14050 if (extra_string)
14051 {
14052 xfree (b->extra_string);
14053 b->extra_string = extra_string;
14054 }
14055 b->condition_not_parsed = 0;
14056 }
14057
14058 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14059 sals[0] = update_static_tracepoint (b, sals[0]);
14060
14061 *found = 1;
14062 }
14063 else
14064 *found = 0;
14065
14066 return sals;
14067 }
14068
14069 /* The default re_set method, for typical hardware or software
14070 breakpoints. Reevaluate the breakpoint and recreate its
14071 locations. */
14072
14073 static void
14074 breakpoint_re_set_default (struct breakpoint *b)
14075 {
14076 struct program_space *filter_pspace = current_program_space;
14077 std::vector<symtab_and_line> expanded, expanded_end;
14078
14079 int found;
14080 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
14081 filter_pspace, &found);
14082 if (found)
14083 expanded = std::move (sals);
14084
14085 if (b->location_range_end != NULL)
14086 {
14087 std::vector<symtab_and_line> sals_end
14088 = location_to_sals (b, b->location_range_end.get (),
14089 filter_pspace, &found);
14090 if (found)
14091 expanded_end = std::move (sals_end);
14092 }
14093
14094 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14095 }
14096
14097 /* Default method for creating SALs from an address string. It basically
14098 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14099
14100 static void
14101 create_sals_from_location_default (const struct event_location *location,
14102 struct linespec_result *canonical,
14103 enum bptype type_wanted)
14104 {
14105 parse_breakpoint_sals (location, canonical);
14106 }
14107
14108 /* Call create_breakpoints_sal for the given arguments. This is the default
14109 function for the `create_breakpoints_sal' method of
14110 breakpoint_ops. */
14111
14112 static void
14113 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14114 struct linespec_result *canonical,
14115 gdb::unique_xmalloc_ptr<char> cond_string,
14116 gdb::unique_xmalloc_ptr<char> extra_string,
14117 enum bptype type_wanted,
14118 enum bpdisp disposition,
14119 int thread,
14120 int task, int ignore_count,
14121 const struct breakpoint_ops *ops,
14122 int from_tty, int enabled,
14123 int internal, unsigned flags)
14124 {
14125 create_breakpoints_sal (gdbarch, canonical,
14126 std::move (cond_string),
14127 std::move (extra_string),
14128 type_wanted, disposition,
14129 thread, task, ignore_count, ops, from_tty,
14130 enabled, internal, flags);
14131 }
14132
14133 /* Decode the line represented by S by calling decode_line_full. This is the
14134 default function for the `decode_location' method of breakpoint_ops. */
14135
14136 static std::vector<symtab_and_line>
14137 decode_location_default (struct breakpoint *b,
14138 const struct event_location *location,
14139 struct program_space *search_pspace)
14140 {
14141 struct linespec_result canonical;
14142
14143 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14144 (struct symtab *) NULL, 0,
14145 &canonical, multiple_symbols_all,
14146 b->filter);
14147
14148 /* We should get 0 or 1 resulting SALs. */
14149 gdb_assert (canonical.lsals.size () < 2);
14150
14151 if (!canonical.lsals.empty ())
14152 {
14153 const linespec_sals &lsal = canonical.lsals[0];
14154 return std::move (lsal.sals);
14155 }
14156 return {};
14157 }
14158
14159 /* Prepare the global context for a re-set of breakpoint B. */
14160
14161 static struct cleanup *
14162 prepare_re_set_context (struct breakpoint *b)
14163 {
14164 input_radix = b->input_radix;
14165 set_language (b->language);
14166
14167 return make_cleanup (null_cleanup, NULL);
14168 }
14169
14170 /* Reset a breakpoint given it's struct breakpoint * BINT.
14171 The value we return ends up being the return value from catch_errors.
14172 Unused in this case. */
14173
14174 static int
14175 breakpoint_re_set_one (void *bint)
14176 {
14177 /* Get past catch_errs. */
14178 struct breakpoint *b = (struct breakpoint *) bint;
14179 struct cleanup *cleanups;
14180
14181 cleanups = prepare_re_set_context (b);
14182 b->ops->re_set (b);
14183 do_cleanups (cleanups);
14184 return 0;
14185 }
14186
14187 /* Re-set breakpoint locations for the current program space.
14188 Locations bound to other program spaces are left untouched. */
14189
14190 void
14191 breakpoint_re_set (void)
14192 {
14193 struct breakpoint *b, *b_tmp;
14194 enum language save_language;
14195 int save_input_radix;
14196
14197 save_language = current_language->la_language;
14198 save_input_radix = input_radix;
14199
14200 {
14201 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14202
14203 /* Note: we must not try to insert locations until after all
14204 breakpoints have been re-set. Otherwise, e.g., when re-setting
14205 breakpoint 1, we'd insert the locations of breakpoint 2, which
14206 hadn't been re-set yet, and thus may have stale locations. */
14207
14208 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14209 {
14210 /* Format possible error msg. */
14211 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14212 b->number);
14213 struct cleanup *cleanups = make_cleanup (xfree, message);
14214 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14215 do_cleanups (cleanups);
14216 }
14217 set_language (save_language);
14218 input_radix = save_input_radix;
14219
14220 jit_breakpoint_re_set ();
14221 }
14222
14223 create_overlay_event_breakpoint ();
14224 create_longjmp_master_breakpoint ();
14225 create_std_terminate_master_breakpoint ();
14226 create_exception_master_breakpoint ();
14227
14228 /* Now we can insert. */
14229 update_global_location_list (UGLL_MAY_INSERT);
14230 }
14231 \f
14232 /* Reset the thread number of this breakpoint:
14233
14234 - If the breakpoint is for all threads, leave it as-is.
14235 - Else, reset it to the current thread for inferior_ptid. */
14236 void
14237 breakpoint_re_set_thread (struct breakpoint *b)
14238 {
14239 if (b->thread != -1)
14240 {
14241 if (in_thread_list (inferior_ptid))
14242 b->thread = ptid_to_global_thread_id (inferior_ptid);
14243
14244 /* We're being called after following a fork. The new fork is
14245 selected as current, and unless this was a vfork will have a
14246 different program space from the original thread. Reset that
14247 as well. */
14248 b->loc->pspace = current_program_space;
14249 }
14250 }
14251
14252 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14253 If from_tty is nonzero, it prints a message to that effect,
14254 which ends with a period (no newline). */
14255
14256 void
14257 set_ignore_count (int bptnum, int count, int from_tty)
14258 {
14259 struct breakpoint *b;
14260
14261 if (count < 0)
14262 count = 0;
14263
14264 ALL_BREAKPOINTS (b)
14265 if (b->number == bptnum)
14266 {
14267 if (is_tracepoint (b))
14268 {
14269 if (from_tty && count != 0)
14270 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14271 bptnum);
14272 return;
14273 }
14274
14275 b->ignore_count = count;
14276 if (from_tty)
14277 {
14278 if (count == 0)
14279 printf_filtered (_("Will stop next time "
14280 "breakpoint %d is reached."),
14281 bptnum);
14282 else if (count == 1)
14283 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14284 bptnum);
14285 else
14286 printf_filtered (_("Will ignore next %d "
14287 "crossings of breakpoint %d."),
14288 count, bptnum);
14289 }
14290 observer_notify_breakpoint_modified (b);
14291 return;
14292 }
14293
14294 error (_("No breakpoint number %d."), bptnum);
14295 }
14296
14297 /* Command to set ignore-count of breakpoint N to COUNT. */
14298
14299 static void
14300 ignore_command (char *args, int from_tty)
14301 {
14302 char *p = args;
14303 int num;
14304
14305 if (p == 0)
14306 error_no_arg (_("a breakpoint number"));
14307
14308 num = get_number (&p);
14309 if (num == 0)
14310 error (_("bad breakpoint number: '%s'"), args);
14311 if (*p == 0)
14312 error (_("Second argument (specified ignore-count) is missing."));
14313
14314 set_ignore_count (num,
14315 longest_to_int (value_as_long (parse_and_eval (p))),
14316 from_tty);
14317 if (from_tty)
14318 printf_filtered ("\n");
14319 }
14320 \f
14321 /* Call FUNCTION on each of the breakpoints
14322 whose numbers are given in ARGS. */
14323
14324 static void
14325 map_breakpoint_numbers (const char *args,
14326 gdb::function_view<void (breakpoint *)> function)
14327 {
14328 int num;
14329 struct breakpoint *b, *tmp;
14330
14331 if (args == 0 || *args == '\0')
14332 error_no_arg (_("one or more breakpoint numbers"));
14333
14334 number_or_range_parser parser (args);
14335
14336 while (!parser.finished ())
14337 {
14338 const char *p = parser.cur_tok ();
14339 bool match = false;
14340
14341 num = parser.get_number ();
14342 if (num == 0)
14343 {
14344 warning (_("bad breakpoint number at or near '%s'"), p);
14345 }
14346 else
14347 {
14348 ALL_BREAKPOINTS_SAFE (b, tmp)
14349 if (b->number == num)
14350 {
14351 match = true;
14352 function (b);
14353 break;
14354 }
14355 if (!match)
14356 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14357 }
14358 }
14359 }
14360
14361 static struct bp_location *
14362 find_location_by_number (const char *number)
14363 {
14364 const char *p1;
14365 int bp_num;
14366 int loc_num;
14367 struct breakpoint *b;
14368 struct bp_location *loc;
14369
14370 p1 = number;
14371 bp_num = get_number_trailer (&p1, '.');
14372 if (bp_num == 0 || p1[0] != '.')
14373 error (_("Bad breakpoint number '%s'"), number);
14374
14375 ALL_BREAKPOINTS (b)
14376 if (b->number == bp_num)
14377 {
14378 break;
14379 }
14380
14381 if (!b || b->number != bp_num)
14382 error (_("Bad breakpoint number '%s'"), number);
14383
14384 /* Skip the dot. */
14385 ++p1;
14386 const char *save = p1;
14387 loc_num = get_number (&p1);
14388 if (loc_num == 0)
14389 error (_("Bad breakpoint location number '%s'"), number);
14390
14391 --loc_num;
14392 loc = b->loc;
14393 for (;loc_num && loc; --loc_num, loc = loc->next)
14394 ;
14395 if (!loc)
14396 error (_("Bad breakpoint location number '%s'"), save);
14397
14398 return loc;
14399 }
14400
14401
14402 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14403 If from_tty is nonzero, it prints a message to that effect,
14404 which ends with a period (no newline). */
14405
14406 void
14407 disable_breakpoint (struct breakpoint *bpt)
14408 {
14409 /* Never disable a watchpoint scope breakpoint; we want to
14410 hit them when we leave scope so we can delete both the
14411 watchpoint and its scope breakpoint at that time. */
14412 if (bpt->type == bp_watchpoint_scope)
14413 return;
14414
14415 bpt->enable_state = bp_disabled;
14416
14417 /* Mark breakpoint locations modified. */
14418 mark_breakpoint_modified (bpt);
14419
14420 if (target_supports_enable_disable_tracepoint ()
14421 && current_trace_status ()->running && is_tracepoint (bpt))
14422 {
14423 struct bp_location *location;
14424
14425 for (location = bpt->loc; location; location = location->next)
14426 target_disable_tracepoint (location);
14427 }
14428
14429 update_global_location_list (UGLL_DONT_INSERT);
14430
14431 observer_notify_breakpoint_modified (bpt);
14432 }
14433
14434 static void
14435 disable_command (char *args, int from_tty)
14436 {
14437 if (args == 0)
14438 {
14439 struct breakpoint *bpt;
14440
14441 ALL_BREAKPOINTS (bpt)
14442 if (user_breakpoint_p (bpt))
14443 disable_breakpoint (bpt);
14444 }
14445 else
14446 {
14447 std::string num = extract_arg (&args);
14448
14449 while (!num.empty ())
14450 {
14451 if (num.find ('.') != std::string::npos)
14452 {
14453 struct bp_location *loc = find_location_by_number (num.c_str ());
14454
14455 if (loc)
14456 {
14457 if (loc->enabled)
14458 {
14459 loc->enabled = 0;
14460 mark_breakpoint_location_modified (loc);
14461 }
14462 if (target_supports_enable_disable_tracepoint ()
14463 && current_trace_status ()->running && loc->owner
14464 && is_tracepoint (loc->owner))
14465 target_disable_tracepoint (loc);
14466 }
14467 update_global_location_list (UGLL_DONT_INSERT);
14468 }
14469 else
14470 map_breakpoint_numbers
14471 (num.c_str (), [&] (breakpoint *b)
14472 {
14473 iterate_over_related_breakpoints (b, disable_breakpoint);
14474 });
14475 num = extract_arg (&args);
14476 }
14477 }
14478 }
14479
14480 static void
14481 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14482 int count)
14483 {
14484 int target_resources_ok;
14485
14486 if (bpt->type == bp_hardware_breakpoint)
14487 {
14488 int i;
14489 i = hw_breakpoint_used_count ();
14490 target_resources_ok =
14491 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14492 i + 1, 0);
14493 if (target_resources_ok == 0)
14494 error (_("No hardware breakpoint support in the target."));
14495 else if (target_resources_ok < 0)
14496 error (_("Hardware breakpoints used exceeds limit."));
14497 }
14498
14499 if (is_watchpoint (bpt))
14500 {
14501 /* Initialize it just to avoid a GCC false warning. */
14502 enum enable_state orig_enable_state = bp_disabled;
14503
14504 TRY
14505 {
14506 struct watchpoint *w = (struct watchpoint *) bpt;
14507
14508 orig_enable_state = bpt->enable_state;
14509 bpt->enable_state = bp_enabled;
14510 update_watchpoint (w, 1 /* reparse */);
14511 }
14512 CATCH (e, RETURN_MASK_ALL)
14513 {
14514 bpt->enable_state = orig_enable_state;
14515 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14516 bpt->number);
14517 return;
14518 }
14519 END_CATCH
14520 }
14521
14522 bpt->enable_state = bp_enabled;
14523
14524 /* Mark breakpoint locations modified. */
14525 mark_breakpoint_modified (bpt);
14526
14527 if (target_supports_enable_disable_tracepoint ()
14528 && current_trace_status ()->running && is_tracepoint (bpt))
14529 {
14530 struct bp_location *location;
14531
14532 for (location = bpt->loc; location; location = location->next)
14533 target_enable_tracepoint (location);
14534 }
14535
14536 bpt->disposition = disposition;
14537 bpt->enable_count = count;
14538 update_global_location_list (UGLL_MAY_INSERT);
14539
14540 observer_notify_breakpoint_modified (bpt);
14541 }
14542
14543
14544 void
14545 enable_breakpoint (struct breakpoint *bpt)
14546 {
14547 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14548 }
14549
14550 /* The enable command enables the specified breakpoints (or all defined
14551 breakpoints) so they once again become (or continue to be) effective
14552 in stopping the inferior. */
14553
14554 static void
14555 enable_command (char *args, int from_tty)
14556 {
14557 if (args == 0)
14558 {
14559 struct breakpoint *bpt;
14560
14561 ALL_BREAKPOINTS (bpt)
14562 if (user_breakpoint_p (bpt))
14563 enable_breakpoint (bpt);
14564 }
14565 else
14566 {
14567 std::string num = extract_arg (&args);
14568
14569 while (!num.empty ())
14570 {
14571 if (num.find ('.') != std::string::npos)
14572 {
14573 struct bp_location *loc = find_location_by_number (num.c_str ());
14574
14575 if (loc)
14576 {
14577 if (!loc->enabled)
14578 {
14579 loc->enabled = 1;
14580 mark_breakpoint_location_modified (loc);
14581 }
14582 if (target_supports_enable_disable_tracepoint ()
14583 && current_trace_status ()->running && loc->owner
14584 && is_tracepoint (loc->owner))
14585 target_enable_tracepoint (loc);
14586 }
14587 update_global_location_list (UGLL_MAY_INSERT);
14588 }
14589 else
14590 map_breakpoint_numbers
14591 (num.c_str (), [&] (breakpoint *b)
14592 {
14593 iterate_over_related_breakpoints (b, enable_breakpoint);
14594 });
14595 num = extract_arg (&args);
14596 }
14597 }
14598 }
14599
14600 static void
14601 enable_once_command (char *args, int from_tty)
14602 {
14603 map_breakpoint_numbers
14604 (args, [&] (breakpoint *b)
14605 {
14606 iterate_over_related_breakpoints
14607 (b, [&] (breakpoint *bpt)
14608 {
14609 enable_breakpoint_disp (bpt, disp_disable, 1);
14610 });
14611 });
14612 }
14613
14614 static void
14615 enable_count_command (char *args, int from_tty)
14616 {
14617 int count;
14618
14619 if (args == NULL)
14620 error_no_arg (_("hit count"));
14621
14622 count = get_number (&args);
14623
14624 map_breakpoint_numbers
14625 (args, [&] (breakpoint *b)
14626 {
14627 iterate_over_related_breakpoints
14628 (b, [&] (breakpoint *bpt)
14629 {
14630 enable_breakpoint_disp (bpt, disp_disable, count);
14631 });
14632 });
14633 }
14634
14635 static void
14636 enable_delete_command (char *args, int from_tty)
14637 {
14638 map_breakpoint_numbers
14639 (args, [&] (breakpoint *b)
14640 {
14641 iterate_over_related_breakpoints
14642 (b, [&] (breakpoint *bpt)
14643 {
14644 enable_breakpoint_disp (bpt, disp_del, 1);
14645 });
14646 });
14647 }
14648 \f
14649 static void
14650 set_breakpoint_cmd (char *args, int from_tty)
14651 {
14652 }
14653
14654 static void
14655 show_breakpoint_cmd (char *args, int from_tty)
14656 {
14657 }
14658
14659 /* Invalidate last known value of any hardware watchpoint if
14660 the memory which that value represents has been written to by
14661 GDB itself. */
14662
14663 static void
14664 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14665 CORE_ADDR addr, ssize_t len,
14666 const bfd_byte *data)
14667 {
14668 struct breakpoint *bp;
14669
14670 ALL_BREAKPOINTS (bp)
14671 if (bp->enable_state == bp_enabled
14672 && bp->type == bp_hardware_watchpoint)
14673 {
14674 struct watchpoint *wp = (struct watchpoint *) bp;
14675
14676 if (wp->val_valid && wp->val)
14677 {
14678 struct bp_location *loc;
14679
14680 for (loc = bp->loc; loc != NULL; loc = loc->next)
14681 if (loc->loc_type == bp_loc_hardware_watchpoint
14682 && loc->address + loc->length > addr
14683 && addr + len > loc->address)
14684 {
14685 value_free (wp->val);
14686 wp->val = NULL;
14687 wp->val_valid = 0;
14688 }
14689 }
14690 }
14691 }
14692
14693 /* Create and insert a breakpoint for software single step. */
14694
14695 void
14696 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14697 struct address_space *aspace,
14698 CORE_ADDR next_pc)
14699 {
14700 struct thread_info *tp = inferior_thread ();
14701 struct symtab_and_line sal;
14702 CORE_ADDR pc = next_pc;
14703
14704 if (tp->control.single_step_breakpoints == NULL)
14705 {
14706 tp->control.single_step_breakpoints
14707 = new_single_step_breakpoint (tp->global_num, gdbarch);
14708 }
14709
14710 sal = find_pc_line (pc, 0);
14711 sal.pc = pc;
14712 sal.section = find_pc_overlay (pc);
14713 sal.explicit_pc = 1;
14714 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14715
14716 update_global_location_list (UGLL_INSERT);
14717 }
14718
14719 /* Insert single step breakpoints according to the current state. */
14720
14721 int
14722 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14723 {
14724 struct regcache *regcache = get_current_regcache ();
14725 std::vector<CORE_ADDR> next_pcs;
14726
14727 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14728
14729 if (!next_pcs.empty ())
14730 {
14731 struct frame_info *frame = get_current_frame ();
14732 struct address_space *aspace = get_frame_address_space (frame);
14733
14734 for (CORE_ADDR pc : next_pcs)
14735 insert_single_step_breakpoint (gdbarch, aspace, pc);
14736
14737 return 1;
14738 }
14739 else
14740 return 0;
14741 }
14742
14743 /* See breakpoint.h. */
14744
14745 int
14746 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14747 struct address_space *aspace,
14748 CORE_ADDR pc)
14749 {
14750 struct bp_location *loc;
14751
14752 for (loc = bp->loc; loc != NULL; loc = loc->next)
14753 if (loc->inserted
14754 && breakpoint_location_address_match (loc, aspace, pc))
14755 return 1;
14756
14757 return 0;
14758 }
14759
14760 /* Check whether a software single-step breakpoint is inserted at
14761 PC. */
14762
14763 int
14764 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14765 CORE_ADDR pc)
14766 {
14767 struct breakpoint *bpt;
14768
14769 ALL_BREAKPOINTS (bpt)
14770 {
14771 if (bpt->type == bp_single_step
14772 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14773 return 1;
14774 }
14775 return 0;
14776 }
14777
14778 /* Tracepoint-specific operations. */
14779
14780 /* Set tracepoint count to NUM. */
14781 static void
14782 set_tracepoint_count (int num)
14783 {
14784 tracepoint_count = num;
14785 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14786 }
14787
14788 static void
14789 trace_command (char *arg, int from_tty)
14790 {
14791 struct breakpoint_ops *ops;
14792
14793 event_location_up location = string_to_event_location (&arg,
14794 current_language);
14795 if (location != NULL
14796 && event_location_type (location.get ()) == PROBE_LOCATION)
14797 ops = &tracepoint_probe_breakpoint_ops;
14798 else
14799 ops = &tracepoint_breakpoint_ops;
14800
14801 create_breakpoint (get_current_arch (),
14802 location.get (),
14803 NULL, 0, arg, 1 /* parse arg */,
14804 0 /* tempflag */,
14805 bp_tracepoint /* type_wanted */,
14806 0 /* Ignore count */,
14807 pending_break_support,
14808 ops,
14809 from_tty,
14810 1 /* enabled */,
14811 0 /* internal */, 0);
14812 }
14813
14814 static void
14815 ftrace_command (char *arg, int from_tty)
14816 {
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, int from_tty)
14836 {
14837 struct breakpoint_ops *ops;
14838 event_location_up location;
14839 struct cleanup *back_to;
14840
14841 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14842 or with a normal static tracepoint. */
14843 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14844 {
14845 ops = &strace_marker_breakpoint_ops;
14846 location = new_linespec_location (&arg);
14847 }
14848 else
14849 {
14850 ops = &tracepoint_breakpoint_ops;
14851 location = string_to_event_location (&arg, current_language);
14852 }
14853
14854 create_breakpoint (get_current_arch (),
14855 location.get (),
14856 NULL, 0, arg, 1 /* parse arg */,
14857 0 /* tempflag */,
14858 bp_static_tracepoint /* type_wanted */,
14859 0 /* Ignore count */,
14860 pending_break_support,
14861 ops,
14862 from_tty,
14863 1 /* enabled */,
14864 0 /* internal */, 0);
14865 }
14866
14867 /* Set up a fake reader function that gets command lines from a linked
14868 list that was acquired during tracepoint uploading. */
14869
14870 static struct uploaded_tp *this_utp;
14871 static int next_cmd;
14872
14873 static char *
14874 read_uploaded_action (void)
14875 {
14876 char *rslt;
14877
14878 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14879
14880 next_cmd++;
14881
14882 return rslt;
14883 }
14884
14885 /* Given information about a tracepoint as recorded on a target (which
14886 can be either a live system or a trace file), attempt to create an
14887 equivalent GDB tracepoint. This is not a reliable process, since
14888 the target does not necessarily have all the information used when
14889 the tracepoint was originally defined. */
14890
14891 struct tracepoint *
14892 create_tracepoint_from_upload (struct uploaded_tp *utp)
14893 {
14894 char *addr_str, small_buf[100];
14895 struct tracepoint *tp;
14896
14897 if (utp->at_string)
14898 addr_str = utp->at_string;
14899 else
14900 {
14901 /* In the absence of a source location, fall back to raw
14902 address. Since there is no way to confirm that the address
14903 means the same thing as when the trace was started, warn the
14904 user. */
14905 warning (_("Uploaded tracepoint %d has no "
14906 "source location, using raw address"),
14907 utp->number);
14908 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14909 addr_str = small_buf;
14910 }
14911
14912 /* There's not much we can do with a sequence of bytecodes. */
14913 if (utp->cond && !utp->cond_string)
14914 warning (_("Uploaded tracepoint %d condition "
14915 "has no source form, ignoring it"),
14916 utp->number);
14917
14918 event_location_up location = string_to_event_location (&addr_str,
14919 current_language);
14920 if (!create_breakpoint (get_current_arch (),
14921 location.get (),
14922 utp->cond_string, -1, addr_str,
14923 0 /* parse cond/thread */,
14924 0 /* tempflag */,
14925 utp->type /* type_wanted */,
14926 0 /* Ignore count */,
14927 pending_break_support,
14928 &tracepoint_breakpoint_ops,
14929 0 /* from_tty */,
14930 utp->enabled /* enabled */,
14931 0 /* internal */,
14932 CREATE_BREAKPOINT_FLAGS_INSERTED))
14933 return NULL;
14934
14935 /* Get the tracepoint we just created. */
14936 tp = get_tracepoint (tracepoint_count);
14937 gdb_assert (tp != NULL);
14938
14939 if (utp->pass > 0)
14940 {
14941 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14942 tp->number);
14943
14944 trace_pass_command (small_buf, 0);
14945 }
14946
14947 /* If we have uploaded versions of the original commands, set up a
14948 special-purpose "reader" function and call the usual command line
14949 reader, then pass the result to the breakpoint command-setting
14950 function. */
14951 if (!VEC_empty (char_ptr, utp->cmd_strings))
14952 {
14953 command_line_up cmd_list;
14954
14955 this_utp = utp;
14956 next_cmd = 0;
14957
14958 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14959
14960 breakpoint_set_commands (tp, std::move (cmd_list));
14961 }
14962 else if (!VEC_empty (char_ptr, utp->actions)
14963 || !VEC_empty (char_ptr, utp->step_actions))
14964 warning (_("Uploaded tracepoint %d actions "
14965 "have no source form, ignoring them"),
14966 utp->number);
14967
14968 /* Copy any status information that might be available. */
14969 tp->hit_count = utp->hit_count;
14970 tp->traceframe_usage = utp->traceframe_usage;
14971
14972 return tp;
14973 }
14974
14975 /* Print information on tracepoint number TPNUM_EXP, or all if
14976 omitted. */
14977
14978 static void
14979 info_tracepoints_command (char *args, int from_tty)
14980 {
14981 struct ui_out *uiout = current_uiout;
14982 int num_printed;
14983
14984 num_printed = breakpoint_1 (args, 0, is_tracepoint);
14985
14986 if (num_printed == 0)
14987 {
14988 if (args == NULL || *args == '\0')
14989 uiout->message ("No tracepoints.\n");
14990 else
14991 uiout->message ("No tracepoint matching '%s'.\n", args);
14992 }
14993
14994 default_collect_info ();
14995 }
14996
14997 /* The 'enable trace' command enables tracepoints.
14998 Not supported by all targets. */
14999 static void
15000 enable_trace_command (char *args, int from_tty)
15001 {
15002 enable_command (args, from_tty);
15003 }
15004
15005 /* The 'disable trace' command disables tracepoints.
15006 Not supported by all targets. */
15007 static void
15008 disable_trace_command (char *args, int from_tty)
15009 {
15010 disable_command (args, from_tty);
15011 }
15012
15013 /* Remove a tracepoint (or all if no argument). */
15014 static void
15015 delete_trace_command (char *arg, int from_tty)
15016 {
15017 struct breakpoint *b, *b_tmp;
15018
15019 dont_repeat ();
15020
15021 if (arg == 0)
15022 {
15023 int breaks_to_delete = 0;
15024
15025 /* Delete all breakpoints if no argument.
15026 Do not delete internal or call-dummy breakpoints, these
15027 have to be deleted with an explicit breakpoint number
15028 argument. */
15029 ALL_TRACEPOINTS (b)
15030 if (is_tracepoint (b) && user_breakpoint_p (b))
15031 {
15032 breaks_to_delete = 1;
15033 break;
15034 }
15035
15036 /* Ask user only if there are some breakpoints to delete. */
15037 if (!from_tty
15038 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15039 {
15040 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15041 if (is_tracepoint (b) && user_breakpoint_p (b))
15042 delete_breakpoint (b);
15043 }
15044 }
15045 else
15046 map_breakpoint_numbers
15047 (arg, [&] (breakpoint *b)
15048 {
15049 iterate_over_related_breakpoints (b, delete_breakpoint);
15050 });
15051 }
15052
15053 /* Helper function for trace_pass_command. */
15054
15055 static void
15056 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15057 {
15058 tp->pass_count = count;
15059 observer_notify_breakpoint_modified (tp);
15060 if (from_tty)
15061 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15062 tp->number, count);
15063 }
15064
15065 /* Set passcount for tracepoint.
15066
15067 First command argument is passcount, second is tracepoint number.
15068 If tracepoint number omitted, apply to most recently defined.
15069 Also accepts special argument "all". */
15070
15071 static void
15072 trace_pass_command (char *args, int from_tty)
15073 {
15074 struct tracepoint *t1;
15075 unsigned int count;
15076
15077 if (args == 0 || *args == 0)
15078 error (_("passcount command requires an "
15079 "argument (count + optional TP num)"));
15080
15081 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15082
15083 args = skip_spaces (args);
15084 if (*args && strncasecmp (args, "all", 3) == 0)
15085 {
15086 struct breakpoint *b;
15087
15088 args += 3; /* Skip special argument "all". */
15089 if (*args)
15090 error (_("Junk at end of arguments."));
15091
15092 ALL_TRACEPOINTS (b)
15093 {
15094 t1 = (struct tracepoint *) b;
15095 trace_pass_set_count (t1, count, from_tty);
15096 }
15097 }
15098 else if (*args == '\0')
15099 {
15100 t1 = get_tracepoint_by_number (&args, NULL);
15101 if (t1)
15102 trace_pass_set_count (t1, count, from_tty);
15103 }
15104 else
15105 {
15106 number_or_range_parser parser (args);
15107 while (!parser.finished ())
15108 {
15109 t1 = get_tracepoint_by_number (&args, &parser);
15110 if (t1)
15111 trace_pass_set_count (t1, count, from_tty);
15112 }
15113 }
15114 }
15115
15116 struct tracepoint *
15117 get_tracepoint (int num)
15118 {
15119 struct breakpoint *t;
15120
15121 ALL_TRACEPOINTS (t)
15122 if (t->number == num)
15123 return (struct tracepoint *) t;
15124
15125 return NULL;
15126 }
15127
15128 /* Find the tracepoint with the given target-side number (which may be
15129 different from the tracepoint number after disconnecting and
15130 reconnecting). */
15131
15132 struct tracepoint *
15133 get_tracepoint_by_number_on_target (int num)
15134 {
15135 struct breakpoint *b;
15136
15137 ALL_TRACEPOINTS (b)
15138 {
15139 struct tracepoint *t = (struct tracepoint *) b;
15140
15141 if (t->number_on_target == num)
15142 return t;
15143 }
15144
15145 return NULL;
15146 }
15147
15148 /* Utility: parse a tracepoint number and look it up in the list.
15149 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15150 If the argument is missing, the most recent tracepoint
15151 (tracepoint_count) is returned. */
15152
15153 struct tracepoint *
15154 get_tracepoint_by_number (char **arg,
15155 number_or_range_parser *parser)
15156 {
15157 struct breakpoint *t;
15158 int tpnum;
15159 char *instring = arg == NULL ? NULL : *arg;
15160
15161 if (parser != NULL)
15162 {
15163 gdb_assert (!parser->finished ());
15164 tpnum = parser->get_number ();
15165 }
15166 else if (arg == NULL || *arg == NULL || ! **arg)
15167 tpnum = tracepoint_count;
15168 else
15169 tpnum = get_number (arg);
15170
15171 if (tpnum <= 0)
15172 {
15173 if (instring && *instring)
15174 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15175 instring);
15176 else
15177 printf_filtered (_("No previous tracepoint\n"));
15178 return NULL;
15179 }
15180
15181 ALL_TRACEPOINTS (t)
15182 if (t->number == tpnum)
15183 {
15184 return (struct tracepoint *) t;
15185 }
15186
15187 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15188 return NULL;
15189 }
15190
15191 void
15192 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15193 {
15194 if (b->thread != -1)
15195 fprintf_unfiltered (fp, " thread %d", b->thread);
15196
15197 if (b->task != 0)
15198 fprintf_unfiltered (fp, " task %d", b->task);
15199
15200 fprintf_unfiltered (fp, "\n");
15201 }
15202
15203 /* Save information on user settable breakpoints (watchpoints, etc) to
15204 a new script file named FILENAME. If FILTER is non-NULL, call it
15205 on each breakpoint and only include the ones for which it returns
15206 non-zero. */
15207
15208 static void
15209 save_breakpoints (char *filename, int from_tty,
15210 int (*filter) (const struct breakpoint *))
15211 {
15212 struct breakpoint *tp;
15213 int any = 0;
15214 int extra_trace_bits = 0;
15215
15216 if (filename == 0 || *filename == 0)
15217 error (_("Argument required (file name in which to save)"));
15218
15219 /* See if we have anything to save. */
15220 ALL_BREAKPOINTS (tp)
15221 {
15222 /* Skip internal and momentary breakpoints. */
15223 if (!user_breakpoint_p (tp))
15224 continue;
15225
15226 /* If we have a filter, only save the breakpoints it accepts. */
15227 if (filter && !filter (tp))
15228 continue;
15229
15230 any = 1;
15231
15232 if (is_tracepoint (tp))
15233 {
15234 extra_trace_bits = 1;
15235
15236 /* We can stop searching. */
15237 break;
15238 }
15239 }
15240
15241 if (!any)
15242 {
15243 warning (_("Nothing to save."));
15244 return;
15245 }
15246
15247 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15248
15249 stdio_file fp;
15250
15251 if (!fp.open (expanded_filename.get (), "w"))
15252 error (_("Unable to open file '%s' for saving (%s)"),
15253 expanded_filename.get (), safe_strerror (errno));
15254
15255 if (extra_trace_bits)
15256 save_trace_state_variables (&fp);
15257
15258 ALL_BREAKPOINTS (tp)
15259 {
15260 /* Skip internal and momentary breakpoints. */
15261 if (!user_breakpoint_p (tp))
15262 continue;
15263
15264 /* If we have a filter, only save the breakpoints it accepts. */
15265 if (filter && !filter (tp))
15266 continue;
15267
15268 tp->ops->print_recreate (tp, &fp);
15269
15270 /* Note, we can't rely on tp->number for anything, as we can't
15271 assume the recreated breakpoint numbers will match. Use $bpnum
15272 instead. */
15273
15274 if (tp->cond_string)
15275 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15276
15277 if (tp->ignore_count)
15278 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15279
15280 if (tp->type != bp_dprintf && tp->commands)
15281 {
15282 fp.puts (" commands\n");
15283
15284 current_uiout->redirect (&fp);
15285 TRY
15286 {
15287 print_command_lines (current_uiout, tp->commands.get (), 2);
15288 }
15289 CATCH (ex, RETURN_MASK_ALL)
15290 {
15291 current_uiout->redirect (NULL);
15292 throw_exception (ex);
15293 }
15294 END_CATCH
15295
15296 current_uiout->redirect (NULL);
15297 fp.puts (" end\n");
15298 }
15299
15300 if (tp->enable_state == bp_disabled)
15301 fp.puts ("disable $bpnum\n");
15302
15303 /* If this is a multi-location breakpoint, check if the locations
15304 should be individually disabled. Watchpoint locations are
15305 special, and not user visible. */
15306 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15307 {
15308 struct bp_location *loc;
15309 int n = 1;
15310
15311 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15312 if (!loc->enabled)
15313 fp.printf ("disable $bpnum.%d\n", n);
15314 }
15315 }
15316
15317 if (extra_trace_bits && *default_collect)
15318 fp.printf ("set default-collect %s\n", default_collect);
15319
15320 if (from_tty)
15321 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15322 }
15323
15324 /* The `save breakpoints' command. */
15325
15326 static void
15327 save_breakpoints_command (char *args, int from_tty)
15328 {
15329 save_breakpoints (args, from_tty, NULL);
15330 }
15331
15332 /* The `save tracepoints' command. */
15333
15334 static void
15335 save_tracepoints_command (char *args, int from_tty)
15336 {
15337 save_breakpoints (args, from_tty, is_tracepoint);
15338 }
15339
15340 /* Create a vector of all tracepoints. */
15341
15342 VEC(breakpoint_p) *
15343 all_tracepoints (void)
15344 {
15345 VEC(breakpoint_p) *tp_vec = 0;
15346 struct breakpoint *tp;
15347
15348 ALL_TRACEPOINTS (tp)
15349 {
15350 VEC_safe_push (breakpoint_p, tp_vec, tp);
15351 }
15352
15353 return tp_vec;
15354 }
15355
15356 \f
15357 /* This help string is used to consolidate all the help string for specifying
15358 locations used by several commands. */
15359
15360 #define LOCATION_HELP_STRING \
15361 "Linespecs are colon-separated lists of location parameters, such as\n\
15362 source filename, function name, label name, and line number.\n\
15363 Example: To specify the start of a label named \"the_top\" in the\n\
15364 function \"fact\" in the file \"factorial.c\", use\n\
15365 \"factorial.c:fact:the_top\".\n\
15366 \n\
15367 Address locations begin with \"*\" and specify an exact address in the\n\
15368 program. Example: To specify the fourth byte past the start function\n\
15369 \"main\", use \"*main + 4\".\n\
15370 \n\
15371 Explicit locations are similar to linespecs but use an option/argument\n\
15372 syntax to specify location parameters.\n\
15373 Example: To specify the start of the label named \"the_top\" in the\n\
15374 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15375 -function fact -label the_top\".\n"
15376
15377 /* This help string is used for the break, hbreak, tbreak and thbreak
15378 commands. It is defined as a macro to prevent duplication.
15379 COMMAND should be a string constant containing the name of the
15380 command. */
15381
15382 #define BREAK_ARGS_HELP(command) \
15383 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15384 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15385 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15386 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15387 `-probe-dtrace' (for a DTrace probe).\n\
15388 LOCATION may be a linespec, address, or explicit location as described\n\
15389 below.\n\
15390 \n\
15391 With no LOCATION, uses current execution address of the selected\n\
15392 stack frame. This is useful for breaking on return to a stack frame.\n\
15393 \n\
15394 THREADNUM is the number from \"info threads\".\n\
15395 CONDITION is a boolean expression.\n\
15396 \n" LOCATION_HELP_STRING "\n\
15397 Multiple breakpoints at one place are permitted, and useful if their\n\
15398 conditions are different.\n\
15399 \n\
15400 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15401
15402 /* List of subcommands for "catch". */
15403 static struct cmd_list_element *catch_cmdlist;
15404
15405 /* List of subcommands for "tcatch". */
15406 static struct cmd_list_element *tcatch_cmdlist;
15407
15408 void
15409 add_catch_command (const char *name, const char *docstring,
15410 cmd_sfunc_ftype *sfunc,
15411 completer_ftype *completer,
15412 void *user_data_catch,
15413 void *user_data_tcatch)
15414 {
15415 struct cmd_list_element *command;
15416
15417 command = add_cmd (name, class_breakpoint, docstring,
15418 &catch_cmdlist);
15419 set_cmd_sfunc (command, sfunc);
15420 set_cmd_context (command, user_data_catch);
15421 set_cmd_completer (command, completer);
15422
15423 command = add_cmd (name, class_breakpoint, docstring,
15424 &tcatch_cmdlist);
15425 set_cmd_sfunc (command, sfunc);
15426 set_cmd_context (command, user_data_tcatch);
15427 set_cmd_completer (command, completer);
15428 }
15429
15430 static void
15431 save_command (char *arg, int from_tty)
15432 {
15433 printf_unfiltered (_("\"save\" must be followed by "
15434 "the name of a save subcommand.\n"));
15435 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15436 }
15437
15438 struct breakpoint *
15439 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15440 void *data)
15441 {
15442 struct breakpoint *b, *b_tmp;
15443
15444 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15445 {
15446 if ((*callback) (b, data))
15447 return b;
15448 }
15449
15450 return NULL;
15451 }
15452
15453 /* Zero if any of the breakpoint's locations could be a location where
15454 functions have been inlined, nonzero otherwise. */
15455
15456 static int
15457 is_non_inline_function (struct breakpoint *b)
15458 {
15459 /* The shared library event breakpoint is set on the address of a
15460 non-inline function. */
15461 if (b->type == bp_shlib_event)
15462 return 1;
15463
15464 return 0;
15465 }
15466
15467 /* Nonzero if the specified PC cannot be a location where functions
15468 have been inlined. */
15469
15470 int
15471 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15472 const struct target_waitstatus *ws)
15473 {
15474 struct breakpoint *b;
15475 struct bp_location *bl;
15476
15477 ALL_BREAKPOINTS (b)
15478 {
15479 if (!is_non_inline_function (b))
15480 continue;
15481
15482 for (bl = b->loc; bl != NULL; bl = bl->next)
15483 {
15484 if (!bl->shlib_disabled
15485 && bpstat_check_location (bl, aspace, pc, ws))
15486 return 1;
15487 }
15488 }
15489
15490 return 0;
15491 }
15492
15493 /* Remove any references to OBJFILE which is going to be freed. */
15494
15495 void
15496 breakpoint_free_objfile (struct objfile *objfile)
15497 {
15498 struct bp_location **locp, *loc;
15499
15500 ALL_BP_LOCATIONS (loc, locp)
15501 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15502 loc->symtab = NULL;
15503 }
15504
15505 void
15506 initialize_breakpoint_ops (void)
15507 {
15508 static int initialized = 0;
15509
15510 struct breakpoint_ops *ops;
15511
15512 if (initialized)
15513 return;
15514 initialized = 1;
15515
15516 /* The breakpoint_ops structure to be inherit by all kinds of
15517 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15518 internal and momentary breakpoints, etc.). */
15519 ops = &bkpt_base_breakpoint_ops;
15520 *ops = base_breakpoint_ops;
15521 ops->re_set = bkpt_re_set;
15522 ops->insert_location = bkpt_insert_location;
15523 ops->remove_location = bkpt_remove_location;
15524 ops->breakpoint_hit = bkpt_breakpoint_hit;
15525 ops->create_sals_from_location = bkpt_create_sals_from_location;
15526 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15527 ops->decode_location = bkpt_decode_location;
15528
15529 /* The breakpoint_ops structure to be used in regular breakpoints. */
15530 ops = &bkpt_breakpoint_ops;
15531 *ops = bkpt_base_breakpoint_ops;
15532 ops->re_set = bkpt_re_set;
15533 ops->resources_needed = bkpt_resources_needed;
15534 ops->print_it = bkpt_print_it;
15535 ops->print_mention = bkpt_print_mention;
15536 ops->print_recreate = bkpt_print_recreate;
15537
15538 /* Ranged breakpoints. */
15539 ops = &ranged_breakpoint_ops;
15540 *ops = bkpt_breakpoint_ops;
15541 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15542 ops->resources_needed = resources_needed_ranged_breakpoint;
15543 ops->print_it = print_it_ranged_breakpoint;
15544 ops->print_one = print_one_ranged_breakpoint;
15545 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15546 ops->print_mention = print_mention_ranged_breakpoint;
15547 ops->print_recreate = print_recreate_ranged_breakpoint;
15548
15549 /* Internal breakpoints. */
15550 ops = &internal_breakpoint_ops;
15551 *ops = bkpt_base_breakpoint_ops;
15552 ops->re_set = internal_bkpt_re_set;
15553 ops->check_status = internal_bkpt_check_status;
15554 ops->print_it = internal_bkpt_print_it;
15555 ops->print_mention = internal_bkpt_print_mention;
15556
15557 /* Momentary breakpoints. */
15558 ops = &momentary_breakpoint_ops;
15559 *ops = bkpt_base_breakpoint_ops;
15560 ops->re_set = momentary_bkpt_re_set;
15561 ops->check_status = momentary_bkpt_check_status;
15562 ops->print_it = momentary_bkpt_print_it;
15563 ops->print_mention = momentary_bkpt_print_mention;
15564
15565 /* Probe breakpoints. */
15566 ops = &bkpt_probe_breakpoint_ops;
15567 *ops = bkpt_breakpoint_ops;
15568 ops->insert_location = bkpt_probe_insert_location;
15569 ops->remove_location = bkpt_probe_remove_location;
15570 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15571 ops->decode_location = bkpt_probe_decode_location;
15572
15573 /* Watchpoints. */
15574 ops = &watchpoint_breakpoint_ops;
15575 *ops = base_breakpoint_ops;
15576 ops->re_set = re_set_watchpoint;
15577 ops->insert_location = insert_watchpoint;
15578 ops->remove_location = remove_watchpoint;
15579 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15580 ops->check_status = check_status_watchpoint;
15581 ops->resources_needed = resources_needed_watchpoint;
15582 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15583 ops->print_it = print_it_watchpoint;
15584 ops->print_mention = print_mention_watchpoint;
15585 ops->print_recreate = print_recreate_watchpoint;
15586 ops->explains_signal = explains_signal_watchpoint;
15587
15588 /* Masked watchpoints. */
15589 ops = &masked_watchpoint_breakpoint_ops;
15590 *ops = watchpoint_breakpoint_ops;
15591 ops->insert_location = insert_masked_watchpoint;
15592 ops->remove_location = remove_masked_watchpoint;
15593 ops->resources_needed = resources_needed_masked_watchpoint;
15594 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15595 ops->print_it = print_it_masked_watchpoint;
15596 ops->print_one_detail = print_one_detail_masked_watchpoint;
15597 ops->print_mention = print_mention_masked_watchpoint;
15598 ops->print_recreate = print_recreate_masked_watchpoint;
15599
15600 /* Tracepoints. */
15601 ops = &tracepoint_breakpoint_ops;
15602 *ops = base_breakpoint_ops;
15603 ops->re_set = tracepoint_re_set;
15604 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15605 ops->print_one_detail = tracepoint_print_one_detail;
15606 ops->print_mention = tracepoint_print_mention;
15607 ops->print_recreate = tracepoint_print_recreate;
15608 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15609 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15610 ops->decode_location = tracepoint_decode_location;
15611
15612 /* Probe tracepoints. */
15613 ops = &tracepoint_probe_breakpoint_ops;
15614 *ops = tracepoint_breakpoint_ops;
15615 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15616 ops->decode_location = tracepoint_probe_decode_location;
15617
15618 /* Static tracepoints with marker (`-m'). */
15619 ops = &strace_marker_breakpoint_ops;
15620 *ops = tracepoint_breakpoint_ops;
15621 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15622 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15623 ops->decode_location = strace_marker_decode_location;
15624
15625 /* Fork catchpoints. */
15626 ops = &catch_fork_breakpoint_ops;
15627 *ops = base_breakpoint_ops;
15628 ops->insert_location = insert_catch_fork;
15629 ops->remove_location = remove_catch_fork;
15630 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15631 ops->print_it = print_it_catch_fork;
15632 ops->print_one = print_one_catch_fork;
15633 ops->print_mention = print_mention_catch_fork;
15634 ops->print_recreate = print_recreate_catch_fork;
15635
15636 /* Vfork catchpoints. */
15637 ops = &catch_vfork_breakpoint_ops;
15638 *ops = base_breakpoint_ops;
15639 ops->insert_location = insert_catch_vfork;
15640 ops->remove_location = remove_catch_vfork;
15641 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15642 ops->print_it = print_it_catch_vfork;
15643 ops->print_one = print_one_catch_vfork;
15644 ops->print_mention = print_mention_catch_vfork;
15645 ops->print_recreate = print_recreate_catch_vfork;
15646
15647 /* Exec catchpoints. */
15648 ops = &catch_exec_breakpoint_ops;
15649 *ops = base_breakpoint_ops;
15650 ops->insert_location = insert_catch_exec;
15651 ops->remove_location = remove_catch_exec;
15652 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15653 ops->print_it = print_it_catch_exec;
15654 ops->print_one = print_one_catch_exec;
15655 ops->print_mention = print_mention_catch_exec;
15656 ops->print_recreate = print_recreate_catch_exec;
15657
15658 /* Solib-related catchpoints. */
15659 ops = &catch_solib_breakpoint_ops;
15660 *ops = base_breakpoint_ops;
15661 ops->insert_location = insert_catch_solib;
15662 ops->remove_location = remove_catch_solib;
15663 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15664 ops->check_status = check_status_catch_solib;
15665 ops->print_it = print_it_catch_solib;
15666 ops->print_one = print_one_catch_solib;
15667 ops->print_mention = print_mention_catch_solib;
15668 ops->print_recreate = print_recreate_catch_solib;
15669
15670 ops = &dprintf_breakpoint_ops;
15671 *ops = bkpt_base_breakpoint_ops;
15672 ops->re_set = dprintf_re_set;
15673 ops->resources_needed = bkpt_resources_needed;
15674 ops->print_it = bkpt_print_it;
15675 ops->print_mention = bkpt_print_mention;
15676 ops->print_recreate = dprintf_print_recreate;
15677 ops->after_condition_true = dprintf_after_condition_true;
15678 ops->breakpoint_hit = dprintf_breakpoint_hit;
15679 }
15680
15681 /* Chain containing all defined "enable breakpoint" subcommands. */
15682
15683 static struct cmd_list_element *enablebreaklist = NULL;
15684
15685 void
15686 _initialize_breakpoint (void)
15687 {
15688 struct cmd_list_element *c;
15689
15690 initialize_breakpoint_ops ();
15691
15692 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15693 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15694 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15695
15696 breakpoint_objfile_key
15697 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15698
15699 breakpoint_chain = 0;
15700 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15701 before a breakpoint is set. */
15702 breakpoint_count = 0;
15703
15704 tracepoint_count = 0;
15705
15706 add_com ("ignore", class_breakpoint, ignore_command, _("\
15707 Set ignore-count of breakpoint number N to COUNT.\n\
15708 Usage is `ignore N COUNT'."));
15709
15710 add_com ("commands", class_breakpoint, commands_command, _("\
15711 Set commands to be executed when the given breakpoints are hit.\n\
15712 Give a space-separated breakpoint list as argument after \"commands\".\n\
15713 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15714 (e.g. `5-7').\n\
15715 With no argument, the targeted breakpoint is the last one set.\n\
15716 The commands themselves follow starting on the next line.\n\
15717 Type a line containing \"end\" to indicate the end of them.\n\
15718 Give \"silent\" as the first line to make the breakpoint silent;\n\
15719 then no output is printed when it is hit, except what the commands print."));
15720
15721 c = add_com ("condition", class_breakpoint, condition_command, _("\
15722 Specify breakpoint number N to break only if COND is true.\n\
15723 Usage is `condition N COND', where N is an integer and COND is an\n\
15724 expression to be evaluated whenever breakpoint N is reached."));
15725 set_cmd_completer (c, condition_completer);
15726
15727 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15728 Set a temporary breakpoint.\n\
15729 Like \"break\" except the breakpoint is only temporary,\n\
15730 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15731 by using \"enable delete\" on the breakpoint number.\n\
15732 \n"
15733 BREAK_ARGS_HELP ("tbreak")));
15734 set_cmd_completer (c, location_completer);
15735
15736 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15737 Set a hardware assisted breakpoint.\n\
15738 Like \"break\" except the breakpoint requires hardware support,\n\
15739 some target hardware may not have this support.\n\
15740 \n"
15741 BREAK_ARGS_HELP ("hbreak")));
15742 set_cmd_completer (c, location_completer);
15743
15744 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15745 Set a temporary hardware assisted breakpoint.\n\
15746 Like \"hbreak\" except the breakpoint is only temporary,\n\
15747 so it will be deleted when hit.\n\
15748 \n"
15749 BREAK_ARGS_HELP ("thbreak")));
15750 set_cmd_completer (c, location_completer);
15751
15752 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15753 Enable some breakpoints.\n\
15754 Give breakpoint numbers (separated by spaces) as arguments.\n\
15755 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15756 This is used to cancel the effect of the \"disable\" command.\n\
15757 With a subcommand you can enable temporarily."),
15758 &enablelist, "enable ", 1, &cmdlist);
15759
15760 add_com_alias ("en", "enable", class_breakpoint, 1);
15761
15762 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15763 Enable some breakpoints.\n\
15764 Give breakpoint numbers (separated by spaces) as arguments.\n\
15765 This is used to cancel the effect of the \"disable\" command.\n\
15766 May be abbreviated to simply \"enable\".\n"),
15767 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15768
15769 add_cmd ("once", no_class, enable_once_command, _("\
15770 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15771 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15772 &enablebreaklist);
15773
15774 add_cmd ("delete", no_class, enable_delete_command, _("\
15775 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15776 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15777 &enablebreaklist);
15778
15779 add_cmd ("count", no_class, enable_count_command, _("\
15780 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15781 If a breakpoint is hit while enabled in this fashion,\n\
15782 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15783 &enablebreaklist);
15784
15785 add_cmd ("delete", no_class, enable_delete_command, _("\
15786 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15787 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15788 &enablelist);
15789
15790 add_cmd ("once", no_class, enable_once_command, _("\
15791 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15792 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15793 &enablelist);
15794
15795 add_cmd ("count", no_class, enable_count_command, _("\
15796 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15797 If a breakpoint is hit while enabled in this fashion,\n\
15798 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15799 &enablelist);
15800
15801 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15802 Disable some breakpoints.\n\
15803 Arguments are breakpoint numbers with spaces in between.\n\
15804 To disable all breakpoints, give no argument.\n\
15805 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15806 &disablelist, "disable ", 1, &cmdlist);
15807 add_com_alias ("dis", "disable", class_breakpoint, 1);
15808 add_com_alias ("disa", "disable", class_breakpoint, 1);
15809
15810 add_cmd ("breakpoints", class_alias, disable_command, _("\
15811 Disable some breakpoints.\n\
15812 Arguments are breakpoint numbers with spaces in between.\n\
15813 To disable all breakpoints, give no argument.\n\
15814 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15815 This command may be abbreviated \"disable\"."),
15816 &disablelist);
15817
15818 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15819 Delete some breakpoints or auto-display expressions.\n\
15820 Arguments are breakpoint numbers with spaces in between.\n\
15821 To delete all breakpoints, give no argument.\n\
15822 \n\
15823 Also a prefix command for deletion of other GDB objects.\n\
15824 The \"unset\" command is also an alias for \"delete\"."),
15825 &deletelist, "delete ", 1, &cmdlist);
15826 add_com_alias ("d", "delete", class_breakpoint, 1);
15827 add_com_alias ("del", "delete", class_breakpoint, 1);
15828
15829 add_cmd ("breakpoints", class_alias, delete_command, _("\
15830 Delete some breakpoints or auto-display expressions.\n\
15831 Arguments are breakpoint numbers with spaces in between.\n\
15832 To delete all breakpoints, give no argument.\n\
15833 This command may be abbreviated \"delete\"."),
15834 &deletelist);
15835
15836 add_com ("clear", class_breakpoint, clear_command, _("\
15837 Clear breakpoint at specified location.\n\
15838 Argument may be a linespec, explicit, or address location as described below.\n\
15839 \n\
15840 With no argument, clears all breakpoints in the line that the selected frame\n\
15841 is executing in.\n"
15842 "\n" LOCATION_HELP_STRING "\n\
15843 See also the \"delete\" command which clears breakpoints by number."));
15844 add_com_alias ("cl", "clear", class_breakpoint, 1);
15845
15846 c = add_com ("break", class_breakpoint, break_command, _("\
15847 Set breakpoint at specified location.\n"
15848 BREAK_ARGS_HELP ("break")));
15849 set_cmd_completer (c, location_completer);
15850
15851 add_com_alias ("b", "break", class_run, 1);
15852 add_com_alias ("br", "break", class_run, 1);
15853 add_com_alias ("bre", "break", class_run, 1);
15854 add_com_alias ("brea", "break", class_run, 1);
15855
15856 if (dbx_commands)
15857 {
15858 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15859 Break in function/address or break at a line in the current file."),
15860 &stoplist, "stop ", 1, &cmdlist);
15861 add_cmd ("in", class_breakpoint, stopin_command,
15862 _("Break in function or address."), &stoplist);
15863 add_cmd ("at", class_breakpoint, stopat_command,
15864 _("Break at a line in the current file."), &stoplist);
15865 add_com ("status", class_info, info_breakpoints_command, _("\
15866 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15867 The \"Type\" column indicates one of:\n\
15868 \tbreakpoint - normal breakpoint\n\
15869 \twatchpoint - watchpoint\n\
15870 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15871 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15872 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15873 address and file/line number respectively.\n\
15874 \n\
15875 Convenience variable \"$_\" and default examine address for \"x\"\n\
15876 are set to the address of the last breakpoint listed unless the command\n\
15877 is prefixed with \"server \".\n\n\
15878 Convenience variable \"$bpnum\" contains the number of the last\n\
15879 breakpoint set."));
15880 }
15881
15882 add_info ("breakpoints", info_breakpoints_command, _("\
15883 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15884 The \"Type\" column indicates one of:\n\
15885 \tbreakpoint - normal breakpoint\n\
15886 \twatchpoint - watchpoint\n\
15887 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15888 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15889 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15890 address and file/line number respectively.\n\
15891 \n\
15892 Convenience variable \"$_\" and default examine address for \"x\"\n\
15893 are set to the address of the last breakpoint listed unless the command\n\
15894 is prefixed with \"server \".\n\n\
15895 Convenience variable \"$bpnum\" contains the number of the last\n\
15896 breakpoint set."));
15897
15898 add_info_alias ("b", "breakpoints", 1);
15899
15900 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15901 Status of all breakpoints, or breakpoint number NUMBER.\n\
15902 The \"Type\" column indicates one of:\n\
15903 \tbreakpoint - normal breakpoint\n\
15904 \twatchpoint - watchpoint\n\
15905 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15906 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15907 \tuntil - internal breakpoint used by the \"until\" command\n\
15908 \tfinish - internal breakpoint used by the \"finish\" command\n\
15909 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15910 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15911 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15912 address and file/line number respectively.\n\
15913 \n\
15914 Convenience variable \"$_\" and default examine address for \"x\"\n\
15915 are set to the address of the last breakpoint listed unless the command\n\
15916 is prefixed with \"server \".\n\n\
15917 Convenience variable \"$bpnum\" contains the number of the last\n\
15918 breakpoint set."),
15919 &maintenanceinfolist);
15920
15921 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15922 Set catchpoints to catch events."),
15923 &catch_cmdlist, "catch ",
15924 0/*allow-unknown*/, &cmdlist);
15925
15926 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15927 Set temporary catchpoints to catch events."),
15928 &tcatch_cmdlist, "tcatch ",
15929 0/*allow-unknown*/, &cmdlist);
15930
15931 add_catch_command ("fork", _("Catch calls to fork."),
15932 catch_fork_command_1,
15933 NULL,
15934 (void *) (uintptr_t) catch_fork_permanent,
15935 (void *) (uintptr_t) catch_fork_temporary);
15936 add_catch_command ("vfork", _("Catch calls to vfork."),
15937 catch_fork_command_1,
15938 NULL,
15939 (void *) (uintptr_t) catch_vfork_permanent,
15940 (void *) (uintptr_t) catch_vfork_temporary);
15941 add_catch_command ("exec", _("Catch calls to exec."),
15942 catch_exec_command_1,
15943 NULL,
15944 CATCH_PERMANENT,
15945 CATCH_TEMPORARY);
15946 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15947 Usage: catch load [REGEX]\n\
15948 If REGEX is given, only stop for libraries matching the regular expression."),
15949 catch_load_command_1,
15950 NULL,
15951 CATCH_PERMANENT,
15952 CATCH_TEMPORARY);
15953 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15954 Usage: catch unload [REGEX]\n\
15955 If REGEX is given, only stop for libraries matching the regular expression."),
15956 catch_unload_command_1,
15957 NULL,
15958 CATCH_PERMANENT,
15959 CATCH_TEMPORARY);
15960
15961 c = add_com ("watch", class_breakpoint, watch_command, _("\
15962 Set a watchpoint for an expression.\n\
15963 Usage: watch [-l|-location] EXPRESSION\n\
15964 A watchpoint stops execution of your program whenever the value of\n\
15965 an expression changes.\n\
15966 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15967 the memory to which it refers."));
15968 set_cmd_completer (c, expression_completer);
15969
15970 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15971 Set a read watchpoint for an expression.\n\
15972 Usage: rwatch [-l|-location] EXPRESSION\n\
15973 A watchpoint stops execution of your program whenever the value of\n\
15974 an expression is read.\n\
15975 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15976 the memory to which it refers."));
15977 set_cmd_completer (c, expression_completer);
15978
15979 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15980 Set a watchpoint for an expression.\n\
15981 Usage: awatch [-l|-location] EXPRESSION\n\
15982 A watchpoint stops execution of your program whenever the value of\n\
15983 an expression is either read or written.\n\
15984 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15985 the memory to which it refers."));
15986 set_cmd_completer (c, expression_completer);
15987
15988 add_info ("watchpoints", info_watchpoints_command, _("\
15989 Status of specified watchpoints (all watchpoints if no argument)."));
15990
15991 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15992 respond to changes - contrary to the description. */
15993 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15994 &can_use_hw_watchpoints, _("\
15995 Set debugger's willingness to use watchpoint hardware."), _("\
15996 Show debugger's willingness to use watchpoint hardware."), _("\
15997 If zero, gdb will not use hardware for new watchpoints, even if\n\
15998 such is available. (However, any hardware watchpoints that were\n\
15999 created before setting this to nonzero, will continue to use watchpoint\n\
16000 hardware.)"),
16001 NULL,
16002 show_can_use_hw_watchpoints,
16003 &setlist, &showlist);
16004
16005 can_use_hw_watchpoints = 1;
16006
16007 /* Tracepoint manipulation commands. */
16008
16009 c = add_com ("trace", class_breakpoint, trace_command, _("\
16010 Set a tracepoint at specified location.\n\
16011 \n"
16012 BREAK_ARGS_HELP ("trace") "\n\
16013 Do \"help tracepoints\" for info on other tracepoint commands."));
16014 set_cmd_completer (c, location_completer);
16015
16016 add_com_alias ("tp", "trace", class_alias, 0);
16017 add_com_alias ("tr", "trace", class_alias, 1);
16018 add_com_alias ("tra", "trace", class_alias, 1);
16019 add_com_alias ("trac", "trace", class_alias, 1);
16020
16021 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16022 Set a fast tracepoint at specified location.\n\
16023 \n"
16024 BREAK_ARGS_HELP ("ftrace") "\n\
16025 Do \"help tracepoints\" for info on other tracepoint commands."));
16026 set_cmd_completer (c, location_completer);
16027
16028 c = add_com ("strace", class_breakpoint, strace_command, _("\
16029 Set a static tracepoint at location or marker.\n\
16030 \n\
16031 strace [LOCATION] [if CONDITION]\n\
16032 LOCATION may be a linespec, explicit, or address location (described below) \n\
16033 or -m MARKER_ID.\n\n\
16034 If a marker id is specified, probe the marker with that name. With\n\
16035 no LOCATION, uses current execution address of the selected stack frame.\n\
16036 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16037 This collects arbitrary user data passed in the probe point call to the\n\
16038 tracing library. You can inspect it when analyzing the trace buffer,\n\
16039 by printing the $_sdata variable like any other convenience variable.\n\
16040 \n\
16041 CONDITION is a boolean expression.\n\
16042 \n" LOCATION_HELP_STRING "\n\
16043 Multiple tracepoints at one place are permitted, and useful if their\n\
16044 conditions are different.\n\
16045 \n\
16046 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16047 Do \"help tracepoints\" for info on other tracepoint commands."));
16048 set_cmd_completer (c, location_completer);
16049
16050 add_info ("tracepoints", info_tracepoints_command, _("\
16051 Status of specified tracepoints (all tracepoints if no argument).\n\
16052 Convenience variable \"$tpnum\" contains the number of the\n\
16053 last tracepoint set."));
16054
16055 add_info_alias ("tp", "tracepoints", 1);
16056
16057 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16058 Delete specified tracepoints.\n\
16059 Arguments are tracepoint numbers, separated by spaces.\n\
16060 No argument means delete all tracepoints."),
16061 &deletelist);
16062 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16063
16064 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16065 Disable specified tracepoints.\n\
16066 Arguments are tracepoint numbers, separated by spaces.\n\
16067 No argument means disable all tracepoints."),
16068 &disablelist);
16069 deprecate_cmd (c, "disable");
16070
16071 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16072 Enable specified tracepoints.\n\
16073 Arguments are tracepoint numbers, separated by spaces.\n\
16074 No argument means enable all tracepoints."),
16075 &enablelist);
16076 deprecate_cmd (c, "enable");
16077
16078 add_com ("passcount", class_trace, trace_pass_command, _("\
16079 Set the passcount for a tracepoint.\n\
16080 The trace will end when the tracepoint has been passed 'count' times.\n\
16081 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16082 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16083
16084 add_prefix_cmd ("save", class_breakpoint, save_command,
16085 _("Save breakpoint definitions as a script."),
16086 &save_cmdlist, "save ",
16087 0/*allow-unknown*/, &cmdlist);
16088
16089 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16090 Save current breakpoint definitions as a script.\n\
16091 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16092 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16093 session to restore them."),
16094 &save_cmdlist);
16095 set_cmd_completer (c, filename_completer);
16096
16097 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16098 Save current tracepoint definitions as a script.\n\
16099 Use the 'source' command in another debug session to restore them."),
16100 &save_cmdlist);
16101 set_cmd_completer (c, filename_completer);
16102
16103 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16104 deprecate_cmd (c, "save tracepoints");
16105
16106 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16107 Breakpoint specific settings\n\
16108 Configure various breakpoint-specific variables such as\n\
16109 pending breakpoint behavior"),
16110 &breakpoint_set_cmdlist, "set breakpoint ",
16111 0/*allow-unknown*/, &setlist);
16112 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16113 Breakpoint specific settings\n\
16114 Configure various breakpoint-specific variables such as\n\
16115 pending breakpoint behavior"),
16116 &breakpoint_show_cmdlist, "show breakpoint ",
16117 0/*allow-unknown*/, &showlist);
16118
16119 add_setshow_auto_boolean_cmd ("pending", no_class,
16120 &pending_break_support, _("\
16121 Set debugger's behavior regarding pending breakpoints."), _("\
16122 Show debugger's behavior regarding pending breakpoints."), _("\
16123 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16124 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16125 an error. If auto, an unrecognized breakpoint location results in a\n\
16126 user-query to see if a pending breakpoint should be created."),
16127 NULL,
16128 show_pending_break_support,
16129 &breakpoint_set_cmdlist,
16130 &breakpoint_show_cmdlist);
16131
16132 pending_break_support = AUTO_BOOLEAN_AUTO;
16133
16134 add_setshow_boolean_cmd ("auto-hw", no_class,
16135 &automatic_hardware_breakpoints, _("\
16136 Set automatic usage of hardware breakpoints."), _("\
16137 Show automatic usage of hardware breakpoints."), _("\
16138 If set, the debugger will automatically use hardware breakpoints for\n\
16139 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16140 a warning will be emitted for such breakpoints."),
16141 NULL,
16142 show_automatic_hardware_breakpoints,
16143 &breakpoint_set_cmdlist,
16144 &breakpoint_show_cmdlist);
16145
16146 add_setshow_boolean_cmd ("always-inserted", class_support,
16147 &always_inserted_mode, _("\
16148 Set mode for inserting breakpoints."), _("\
16149 Show mode for inserting breakpoints."), _("\
16150 When this mode is on, breakpoints are inserted immediately as soon as\n\
16151 they're created, kept inserted even when execution stops, and removed\n\
16152 only when the user deletes them. When this mode is off (the default),\n\
16153 breakpoints are inserted only when execution continues, and removed\n\
16154 when execution stops."),
16155 NULL,
16156 &show_always_inserted_mode,
16157 &breakpoint_set_cmdlist,
16158 &breakpoint_show_cmdlist);
16159
16160 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16161 condition_evaluation_enums,
16162 &condition_evaluation_mode_1, _("\
16163 Set mode of breakpoint condition evaluation."), _("\
16164 Show mode of breakpoint condition evaluation."), _("\
16165 When this is set to \"host\", breakpoint conditions will be\n\
16166 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16167 breakpoint conditions will be downloaded to the target (if the target\n\
16168 supports such feature) and conditions will be evaluated on the target's side.\n\
16169 If this is set to \"auto\" (default), this will be automatically set to\n\
16170 \"target\" if it supports condition evaluation, otherwise it will\n\
16171 be set to \"gdb\""),
16172 &set_condition_evaluation_mode,
16173 &show_condition_evaluation_mode,
16174 &breakpoint_set_cmdlist,
16175 &breakpoint_show_cmdlist);
16176
16177 add_com ("break-range", class_breakpoint, break_range_command, _("\
16178 Set a breakpoint for an address range.\n\
16179 break-range START-LOCATION, END-LOCATION\n\
16180 where START-LOCATION and END-LOCATION can be one of the following:\n\
16181 LINENUM, for that line in the current file,\n\
16182 FILE:LINENUM, for that line in that file,\n\
16183 +OFFSET, for that number of lines after the current line\n\
16184 or the start of the range\n\
16185 FUNCTION, for the first line in that function,\n\
16186 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16187 *ADDRESS, for the instruction at that address.\n\
16188 \n\
16189 The breakpoint will stop execution of the inferior whenever it executes\n\
16190 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16191 range (including START-LOCATION and END-LOCATION)."));
16192
16193 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16194 Set a dynamic printf at specified location.\n\
16195 dprintf location,format string,arg1,arg2,...\n\
16196 location may be a linespec, explicit, or address location.\n"
16197 "\n" LOCATION_HELP_STRING));
16198 set_cmd_completer (c, location_completer);
16199
16200 add_setshow_enum_cmd ("dprintf-style", class_support,
16201 dprintf_style_enums, &dprintf_style, _("\
16202 Set the style of usage for dynamic printf."), _("\
16203 Show the style of usage for dynamic printf."), _("\
16204 This setting chooses how GDB will do a dynamic printf.\n\
16205 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16206 console, as with the \"printf\" command.\n\
16207 If the value is \"call\", the print is done by calling a function in your\n\
16208 program; by default printf(), but you can choose a different function or\n\
16209 output stream by setting dprintf-function and dprintf-channel."),
16210 update_dprintf_commands, NULL,
16211 &setlist, &showlist);
16212
16213 dprintf_function = xstrdup ("printf");
16214 add_setshow_string_cmd ("dprintf-function", class_support,
16215 &dprintf_function, _("\
16216 Set the function to use for dynamic printf"), _("\
16217 Show the function to use for dynamic printf"), NULL,
16218 update_dprintf_commands, NULL,
16219 &setlist, &showlist);
16220
16221 dprintf_channel = xstrdup ("");
16222 add_setshow_string_cmd ("dprintf-channel", class_support,
16223 &dprintf_channel, _("\
16224 Set the channel to use for dynamic printf"), _("\
16225 Show the channel to use for dynamic printf"), NULL,
16226 update_dprintf_commands, NULL,
16227 &setlist, &showlist);
16228
16229 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16230 &disconnected_dprintf, _("\
16231 Set whether dprintf continues after GDB disconnects."), _("\
16232 Show whether dprintf continues after GDB disconnects."), _("\
16233 Use this to let dprintf commands continue to hit and produce output\n\
16234 even if GDB disconnects or detaches from the target."),
16235 NULL,
16236 NULL,
16237 &setlist, &showlist);
16238
16239 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16240 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16241 (target agent only) This is useful for formatted output in user-defined commands."));
16242
16243 automatic_hardware_breakpoints = 1;
16244
16245 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16246 observer_attach_thread_exit (remove_threaded_breakpoints);
16247 }
This page took 0.390801 seconds and 3 git commands to generate.