Use SaL symbol name when reporting breakpoint locations
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observer.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "continuations.h"
63 #include "stack.h"
64 #include "skip.h"
65 #include "ax-gdb.h"
66 #include "dummy-frame.h"
67 #include "interps.h"
68 #include "format.h"
69 #include "thread-fsm.h"
70 #include "tid-parse.h"
71
72 /* readline include files */
73 #include "readline/readline.h"
74 #include "readline/history.h"
75
76 /* readline defines this. */
77 #undef savestring
78
79 #include "mi/mi-common.h"
80 #include "extension.h"
81 #include <algorithm>
82 #include "progspace-and-thread.h"
83 #include "common/array-view.h"
84 #include "common/gdb_optional.h"
85
86 /* Enums for exception-handling support. */
87 enum exception_event_kind
88 {
89 EX_EVENT_THROW,
90 EX_EVENT_RETHROW,
91 EX_EVENT_CATCH
92 };
93
94 /* Prototypes for local functions. */
95
96 static void map_breakpoint_numbers (const char *,
97 gdb::function_view<void (breakpoint *)>);
98
99 static void ignore_command (char *, int);
100
101 static void breakpoint_re_set_default (struct breakpoint *);
102
103 static void
104 create_sals_from_location_default (const struct event_location *location,
105 struct linespec_result *canonical,
106 enum bptype type_wanted);
107
108 static void create_breakpoints_sal_default (struct gdbarch *,
109 struct linespec_result *,
110 gdb::unique_xmalloc_ptr<char>,
111 gdb::unique_xmalloc_ptr<char>,
112 enum bptype,
113 enum bpdisp, int, int,
114 int,
115 const struct breakpoint_ops *,
116 int, int, int, unsigned);
117
118 static std::vector<symtab_and_line> decode_location_default
119 (struct breakpoint *b, const struct event_location *location,
120 struct program_space *search_pspace);
121
122 static void clear_command (char *, int);
123
124 static int can_use_hardware_watchpoint (struct value *);
125
126 static void mention (struct breakpoint *);
127
128 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
129 enum bptype,
130 const struct breakpoint_ops *);
131 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
132 const struct symtab_and_line *);
133
134 /* This function is used in gdbtk sources and thus can not be made
135 static. */
136 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
137 struct symtab_and_line,
138 enum bptype,
139 const struct breakpoint_ops *);
140
141 static struct breakpoint *
142 momentary_breakpoint_from_master (struct breakpoint *orig,
143 enum bptype type,
144 const struct breakpoint_ops *ops,
145 int loc_enabled);
146
147 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
148
149 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
150 CORE_ADDR bpaddr,
151 enum bptype bptype);
152
153 static void describe_other_breakpoints (struct gdbarch *,
154 struct program_space *, CORE_ADDR,
155 struct obj_section *, int);
156
157 static int watchpoint_locations_match (struct bp_location *loc1,
158 struct bp_location *loc2);
159
160 static int breakpoint_location_address_match (struct bp_location *bl,
161 const struct address_space *aspace,
162 CORE_ADDR addr);
163
164 static int breakpoint_location_address_range_overlap (struct bp_location *,
165 const address_space *,
166 CORE_ADDR, int);
167
168 static void info_breakpoints_command (char *, int);
169
170 static void info_watchpoints_command (char *, int);
171
172 static void commands_command (char *, int);
173
174 static void condition_command (char *, int);
175
176 static int remove_breakpoint (struct bp_location *);
177 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
178
179 static enum print_stop_action print_bp_stop_message (bpstat bs);
180
181 static int hw_breakpoint_used_count (void);
182
183 static int hw_watchpoint_use_count (struct breakpoint *);
184
185 static int hw_watchpoint_used_count_others (struct breakpoint *except,
186 enum bptype type,
187 int *other_type_used);
188
189 static void hbreak_command (char *, int);
190
191 static void thbreak_command (char *, int);
192
193 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
194 int count);
195
196 static void stop_command (char *arg, int from_tty);
197
198 static void free_bp_location (struct bp_location *loc);
199 static void incref_bp_location (struct bp_location *loc);
200 static void decref_bp_location (struct bp_location **loc);
201
202 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
203
204 /* update_global_location_list's modes of operation wrt to whether to
205 insert locations now. */
206 enum ugll_insert_mode
207 {
208 /* Don't insert any breakpoint locations into the inferior, only
209 remove already-inserted locations that no longer should be
210 inserted. Functions that delete a breakpoint or breakpoints
211 should specify this mode, so that deleting a breakpoint doesn't
212 have the side effect of inserting the locations of other
213 breakpoints that are marked not-inserted, but should_be_inserted
214 returns true on them.
215
216 This behavior is useful is situations close to tear-down -- e.g.,
217 after an exec, while the target still has execution, but
218 breakpoint shadows of the previous executable image should *NOT*
219 be restored to the new image; or before detaching, where the
220 target still has execution and wants to delete breakpoints from
221 GDB's lists, and all breakpoints had already been removed from
222 the inferior. */
223 UGLL_DONT_INSERT,
224
225 /* May insert breakpoints iff breakpoints_should_be_inserted_now
226 claims breakpoints should be inserted now. */
227 UGLL_MAY_INSERT,
228
229 /* Insert locations now, irrespective of
230 breakpoints_should_be_inserted_now. E.g., say all threads are
231 stopped right now, and the user did "continue". We need to
232 insert breakpoints _before_ resuming the target, but
233 UGLL_MAY_INSERT wouldn't insert them, because
234 breakpoints_should_be_inserted_now returns false at that point,
235 as no thread is running yet. */
236 UGLL_INSERT
237 };
238
239 static void update_global_location_list (enum ugll_insert_mode);
240
241 static void update_global_location_list_nothrow (enum ugll_insert_mode);
242
243 static int is_hardware_watchpoint (const struct breakpoint *bpt);
244
245 static void insert_breakpoint_locations (void);
246
247 static void info_tracepoints_command (char *, int);
248
249 static void enable_trace_command (char *, int);
250
251 static void disable_trace_command (char *, int);
252
253 static void trace_pass_command (char *, int);
254
255 static void set_tracepoint_count (int num);
256
257 static int is_masked_watchpoint (const struct breakpoint *b);
258
259 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
260
261 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
262 otherwise. */
263
264 static int strace_marker_p (struct breakpoint *b);
265
266 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
267 that are implemented on top of software or hardware breakpoints
268 (user breakpoints, internal and momentary breakpoints, etc.). */
269 static struct breakpoint_ops bkpt_base_breakpoint_ops;
270
271 /* Internal breakpoints class type. */
272 static struct breakpoint_ops internal_breakpoint_ops;
273
274 /* Momentary breakpoints class type. */
275 static struct breakpoint_ops momentary_breakpoint_ops;
276
277 /* The breakpoint_ops structure to be used in regular user created
278 breakpoints. */
279 struct breakpoint_ops bkpt_breakpoint_ops;
280
281 /* Breakpoints set on probes. */
282 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
283
284 /* Dynamic printf class type. */
285 struct breakpoint_ops dprintf_breakpoint_ops;
286
287 /* The style in which to perform a dynamic printf. This is a user
288 option because different output options have different tradeoffs;
289 if GDB does the printing, there is better error handling if there
290 is a problem with any of the arguments, but using an inferior
291 function lets you have special-purpose printers and sending of
292 output to the same place as compiled-in print functions. */
293
294 static const char dprintf_style_gdb[] = "gdb";
295 static const char dprintf_style_call[] = "call";
296 static const char dprintf_style_agent[] = "agent";
297 static const char *const dprintf_style_enums[] = {
298 dprintf_style_gdb,
299 dprintf_style_call,
300 dprintf_style_agent,
301 NULL
302 };
303 static const char *dprintf_style = dprintf_style_gdb;
304
305 /* The function to use for dynamic printf if the preferred style is to
306 call into the inferior. The value is simply a string that is
307 copied into the command, so it can be anything that GDB can
308 evaluate to a callable address, not necessarily a function name. */
309
310 static char *dprintf_function;
311
312 /* The channel to use for dynamic printf if the preferred style is to
313 call into the inferior; if a nonempty string, it will be passed to
314 the call as the first argument, with the format string as the
315 second. As with the dprintf function, this can be anything that
316 GDB knows how to evaluate, so in addition to common choices like
317 "stderr", this could be an app-specific expression like
318 "mystreams[curlogger]". */
319
320 static char *dprintf_channel;
321
322 /* True if dprintf commands should continue to operate even if GDB
323 has disconnected. */
324 static int disconnected_dprintf = 1;
325
326 struct command_line *
327 breakpoint_commands (struct breakpoint *b)
328 {
329 return b->commands ? b->commands.get () : NULL;
330 }
331
332 /* Flag indicating that a command has proceeded the inferior past the
333 current breakpoint. */
334
335 static int breakpoint_proceeded;
336
337 const char *
338 bpdisp_text (enum bpdisp disp)
339 {
340 /* NOTE: the following values are a part of MI protocol and
341 represent values of 'disp' field returned when inferior stops at
342 a breakpoint. */
343 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
344
345 return bpdisps[(int) disp];
346 }
347
348 /* Prototypes for exported functions. */
349 /* If FALSE, gdb will not use hardware support for watchpoints, even
350 if such is available. */
351 static int can_use_hw_watchpoints;
352
353 static void
354 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
355 struct cmd_list_element *c,
356 const char *value)
357 {
358 fprintf_filtered (file,
359 _("Debugger's willingness to use "
360 "watchpoint hardware is %s.\n"),
361 value);
362 }
363
364 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
365 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
366 for unrecognized breakpoint locations.
367 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
368 static enum auto_boolean pending_break_support;
369 static void
370 show_pending_break_support (struct ui_file *file, int from_tty,
371 struct cmd_list_element *c,
372 const char *value)
373 {
374 fprintf_filtered (file,
375 _("Debugger's behavior regarding "
376 "pending breakpoints is %s.\n"),
377 value);
378 }
379
380 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
381 set with "break" but falling in read-only memory.
382 If 0, gdb will warn about such breakpoints, but won't automatically
383 use hardware breakpoints. */
384 static int automatic_hardware_breakpoints;
385 static void
386 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
387 struct cmd_list_element *c,
388 const char *value)
389 {
390 fprintf_filtered (file,
391 _("Automatic usage of hardware breakpoints is %s.\n"),
392 value);
393 }
394
395 /* If on, GDB keeps breakpoints inserted even if the inferior is
396 stopped, and immediately inserts any new breakpoints as soon as
397 they're created. If off (default), GDB keeps breakpoints off of
398 the target as long as possible. That is, it delays inserting
399 breakpoints until the next resume, and removes them again when the
400 target fully stops. This is a bit safer in case GDB crashes while
401 processing user input. */
402 static int always_inserted_mode = 0;
403
404 static void
405 show_always_inserted_mode (struct ui_file *file, int from_tty,
406 struct cmd_list_element *c, const char *value)
407 {
408 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
409 value);
410 }
411
412 /* See breakpoint.h. */
413
414 int
415 breakpoints_should_be_inserted_now (void)
416 {
417 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
418 {
419 /* If breakpoints are global, they should be inserted even if no
420 thread under gdb's control is running, or even if there are
421 no threads under GDB's control yet. */
422 return 1;
423 }
424 else if (target_has_execution)
425 {
426 struct thread_info *tp;
427
428 if (always_inserted_mode)
429 {
430 /* The user wants breakpoints inserted even if all threads
431 are stopped. */
432 return 1;
433 }
434
435 if (threads_are_executing ())
436 return 1;
437
438 /* Don't remove breakpoints yet if, even though all threads are
439 stopped, we still have events to process. */
440 ALL_NON_EXITED_THREADS (tp)
441 if (tp->resumed
442 && tp->suspend.waitstatus_pending_p)
443 return 1;
444 }
445 return 0;
446 }
447
448 static const char condition_evaluation_both[] = "host or target";
449
450 /* Modes for breakpoint condition evaluation. */
451 static const char condition_evaluation_auto[] = "auto";
452 static const char condition_evaluation_host[] = "host";
453 static const char condition_evaluation_target[] = "target";
454 static const char *const condition_evaluation_enums[] = {
455 condition_evaluation_auto,
456 condition_evaluation_host,
457 condition_evaluation_target,
458 NULL
459 };
460
461 /* Global that holds the current mode for breakpoint condition evaluation. */
462 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
463
464 /* Global that we use to display information to the user (gets its value from
465 condition_evaluation_mode_1. */
466 static const char *condition_evaluation_mode = condition_evaluation_auto;
467
468 /* Translate a condition evaluation mode MODE into either "host"
469 or "target". This is used mostly to translate from "auto" to the
470 real setting that is being used. It returns the translated
471 evaluation mode. */
472
473 static const char *
474 translate_condition_evaluation_mode (const char *mode)
475 {
476 if (mode == condition_evaluation_auto)
477 {
478 if (target_supports_evaluation_of_breakpoint_conditions ())
479 return condition_evaluation_target;
480 else
481 return condition_evaluation_host;
482 }
483 else
484 return mode;
485 }
486
487 /* Discovers what condition_evaluation_auto translates to. */
488
489 static const char *
490 breakpoint_condition_evaluation_mode (void)
491 {
492 return translate_condition_evaluation_mode (condition_evaluation_mode);
493 }
494
495 /* Return true if GDB should evaluate breakpoint conditions or false
496 otherwise. */
497
498 static int
499 gdb_evaluates_breakpoint_condition_p (void)
500 {
501 const char *mode = breakpoint_condition_evaluation_mode ();
502
503 return (mode == condition_evaluation_host);
504 }
505
506 /* Are we executing breakpoint commands? */
507 static int executing_breakpoint_commands;
508
509 /* Are overlay event breakpoints enabled? */
510 static int overlay_events_enabled;
511
512 /* See description in breakpoint.h. */
513 int target_exact_watchpoints = 0;
514
515 /* Walk the following statement or block through all breakpoints.
516 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
517 current breakpoint. */
518
519 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
520
521 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
522 for (B = breakpoint_chain; \
523 B ? (TMP=B->next, 1): 0; \
524 B = TMP)
525
526 /* Similar iterator for the low-level breakpoints. SAFE variant is
527 not provided so update_global_location_list must not be called
528 while executing the block of ALL_BP_LOCATIONS. */
529
530 #define ALL_BP_LOCATIONS(B,BP_TMP) \
531 for (BP_TMP = bp_locations; \
532 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
533 BP_TMP++)
534
535 /* Iterates through locations with address ADDRESS for the currently selected
536 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
537 to where the loop should start from.
538 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
539 appropriate location to start with. */
540
541 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
542 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
543 BP_LOCP_TMP = BP_LOCP_START; \
544 BP_LOCP_START \
545 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
546 && (*BP_LOCP_TMP)->address == ADDRESS); \
547 BP_LOCP_TMP++)
548
549 /* Iterator for tracepoints only. */
550
551 #define ALL_TRACEPOINTS(B) \
552 for (B = breakpoint_chain; B; B = B->next) \
553 if (is_tracepoint (B))
554
555 /* Chains of all breakpoints defined. */
556
557 struct breakpoint *breakpoint_chain;
558
559 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
560
561 static struct bp_location **bp_locations;
562
563 /* Number of elements of BP_LOCATIONS. */
564
565 static unsigned bp_locations_count;
566
567 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
568 ADDRESS for the current elements of BP_LOCATIONS which get a valid
569 result from bp_location_has_shadow. You can use it for roughly
570 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
571 an address you need to read. */
572
573 static CORE_ADDR bp_locations_placed_address_before_address_max;
574
575 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
576 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
577 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
578 You can use it for roughly limiting the subrange of BP_LOCATIONS to
579 scan for shadow bytes for an address you need to read. */
580
581 static CORE_ADDR bp_locations_shadow_len_after_address_max;
582
583 /* The locations that no longer correspond to any breakpoint, unlinked
584 from the bp_locations array, but for which a hit may still be
585 reported by a target. */
586 VEC(bp_location_p) *moribund_locations = NULL;
587
588 /* Number of last breakpoint made. */
589
590 static int breakpoint_count;
591
592 /* The value of `breakpoint_count' before the last command that
593 created breakpoints. If the last (break-like) command created more
594 than one breakpoint, then the difference between BREAKPOINT_COUNT
595 and PREV_BREAKPOINT_COUNT is more than one. */
596 static int prev_breakpoint_count;
597
598 /* Number of last tracepoint made. */
599
600 static int tracepoint_count;
601
602 static struct cmd_list_element *breakpoint_set_cmdlist;
603 static struct cmd_list_element *breakpoint_show_cmdlist;
604 struct cmd_list_element *save_cmdlist;
605
606 /* See declaration at breakpoint.h. */
607
608 struct breakpoint *
609 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
610 void *user_data)
611 {
612 struct breakpoint *b = NULL;
613
614 ALL_BREAKPOINTS (b)
615 {
616 if (func (b, user_data) != 0)
617 break;
618 }
619
620 return b;
621 }
622
623 /* Return whether a breakpoint is an active enabled breakpoint. */
624 static int
625 breakpoint_enabled (struct breakpoint *b)
626 {
627 return (b->enable_state == bp_enabled);
628 }
629
630 /* Set breakpoint count to NUM. */
631
632 static void
633 set_breakpoint_count (int num)
634 {
635 prev_breakpoint_count = breakpoint_count;
636 breakpoint_count = num;
637 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
638 }
639
640 /* Used by `start_rbreak_breakpoints' below, to record the current
641 breakpoint count before "rbreak" creates any breakpoint. */
642 static int rbreak_start_breakpoint_count;
643
644 /* Called at the start an "rbreak" command to record the first
645 breakpoint made. */
646
647 void
648 start_rbreak_breakpoints (void)
649 {
650 rbreak_start_breakpoint_count = breakpoint_count;
651 }
652
653 /* Called at the end of an "rbreak" command to record the last
654 breakpoint made. */
655
656 void
657 end_rbreak_breakpoints (void)
658 {
659 prev_breakpoint_count = rbreak_start_breakpoint_count;
660 }
661
662 /* Used in run_command to zero the hit count when a new run starts. */
663
664 void
665 clear_breakpoint_hit_counts (void)
666 {
667 struct breakpoint *b;
668
669 ALL_BREAKPOINTS (b)
670 b->hit_count = 0;
671 }
672
673 \f
674 /* Return the breakpoint with the specified number, or NULL
675 if the number does not refer to an existing breakpoint. */
676
677 struct breakpoint *
678 get_breakpoint (int num)
679 {
680 struct breakpoint *b;
681
682 ALL_BREAKPOINTS (b)
683 if (b->number == num)
684 return b;
685
686 return NULL;
687 }
688
689 \f
690
691 /* Mark locations as "conditions have changed" in case the target supports
692 evaluating conditions on its side. */
693
694 static void
695 mark_breakpoint_modified (struct breakpoint *b)
696 {
697 struct bp_location *loc;
698
699 /* This is only meaningful if the target is
700 evaluating conditions and if the user has
701 opted for condition evaluation on the target's
702 side. */
703 if (gdb_evaluates_breakpoint_condition_p ()
704 || !target_supports_evaluation_of_breakpoint_conditions ())
705 return;
706
707 if (!is_breakpoint (b))
708 return;
709
710 for (loc = b->loc; loc; loc = loc->next)
711 loc->condition_changed = condition_modified;
712 }
713
714 /* Mark location as "conditions have changed" in case the target supports
715 evaluating conditions on its side. */
716
717 static void
718 mark_breakpoint_location_modified (struct bp_location *loc)
719 {
720 /* This is only meaningful if the target is
721 evaluating conditions and if the user has
722 opted for condition evaluation on the target's
723 side. */
724 if (gdb_evaluates_breakpoint_condition_p ()
725 || !target_supports_evaluation_of_breakpoint_conditions ())
726
727 return;
728
729 if (!is_breakpoint (loc->owner))
730 return;
731
732 loc->condition_changed = condition_modified;
733 }
734
735 /* Sets the condition-evaluation mode using the static global
736 condition_evaluation_mode. */
737
738 static void
739 set_condition_evaluation_mode (char *args, int from_tty,
740 struct cmd_list_element *c)
741 {
742 const char *old_mode, *new_mode;
743
744 if ((condition_evaluation_mode_1 == condition_evaluation_target)
745 && !target_supports_evaluation_of_breakpoint_conditions ())
746 {
747 condition_evaluation_mode_1 = condition_evaluation_mode;
748 warning (_("Target does not support breakpoint condition evaluation.\n"
749 "Using host evaluation mode instead."));
750 return;
751 }
752
753 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
754 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
755
756 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
757 settings was "auto". */
758 condition_evaluation_mode = condition_evaluation_mode_1;
759
760 /* Only update the mode if the user picked a different one. */
761 if (new_mode != old_mode)
762 {
763 struct bp_location *loc, **loc_tmp;
764 /* If the user switched to a different evaluation mode, we
765 need to synch the changes with the target as follows:
766
767 "host" -> "target": Send all (valid) conditions to the target.
768 "target" -> "host": Remove all the conditions from the target.
769 */
770
771 if (new_mode == condition_evaluation_target)
772 {
773 /* Mark everything modified and synch conditions with the
774 target. */
775 ALL_BP_LOCATIONS (loc, loc_tmp)
776 mark_breakpoint_location_modified (loc);
777 }
778 else
779 {
780 /* Manually mark non-duplicate locations to synch conditions
781 with the target. We do this to remove all the conditions the
782 target knows about. */
783 ALL_BP_LOCATIONS (loc, loc_tmp)
784 if (is_breakpoint (loc->owner) && loc->inserted)
785 loc->needs_update = 1;
786 }
787
788 /* Do the update. */
789 update_global_location_list (UGLL_MAY_INSERT);
790 }
791
792 return;
793 }
794
795 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
796 what "auto" is translating to. */
797
798 static void
799 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
800 struct cmd_list_element *c, const char *value)
801 {
802 if (condition_evaluation_mode == condition_evaluation_auto)
803 fprintf_filtered (file,
804 _("Breakpoint condition evaluation "
805 "mode is %s (currently %s).\n"),
806 value,
807 breakpoint_condition_evaluation_mode ());
808 else
809 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
810 value);
811 }
812
813 /* A comparison function for bp_location AP and BP that is used by
814 bsearch. This comparison function only cares about addresses, unlike
815 the more general bp_locations_compare function. */
816
817 static int
818 bp_locations_compare_addrs (const void *ap, const void *bp)
819 {
820 const struct bp_location *a = *(const struct bp_location **) ap;
821 const struct bp_location *b = *(const struct bp_location **) bp;
822
823 if (a->address == b->address)
824 return 0;
825 else
826 return ((a->address > b->address) - (a->address < b->address));
827 }
828
829 /* Helper function to skip all bp_locations with addresses
830 less than ADDRESS. It returns the first bp_location that
831 is greater than or equal to ADDRESS. If none is found, just
832 return NULL. */
833
834 static struct bp_location **
835 get_first_locp_gte_addr (CORE_ADDR address)
836 {
837 struct bp_location dummy_loc;
838 struct bp_location *dummy_locp = &dummy_loc;
839 struct bp_location **locp_found = NULL;
840
841 /* Initialize the dummy location's address field. */
842 dummy_loc.address = address;
843
844 /* Find a close match to the first location at ADDRESS. */
845 locp_found = ((struct bp_location **)
846 bsearch (&dummy_locp, bp_locations, bp_locations_count,
847 sizeof (struct bp_location **),
848 bp_locations_compare_addrs));
849
850 /* Nothing was found, nothing left to do. */
851 if (locp_found == NULL)
852 return NULL;
853
854 /* We may have found a location that is at ADDRESS but is not the first in the
855 location's list. Go backwards (if possible) and locate the first one. */
856 while ((locp_found - 1) >= bp_locations
857 && (*(locp_found - 1))->address == address)
858 locp_found--;
859
860 return locp_found;
861 }
862
863 void
864 set_breakpoint_condition (struct breakpoint *b, const char *exp,
865 int from_tty)
866 {
867 xfree (b->cond_string);
868 b->cond_string = NULL;
869
870 if (is_watchpoint (b))
871 {
872 struct watchpoint *w = (struct watchpoint *) b;
873
874 w->cond_exp.reset ();
875 }
876 else
877 {
878 struct bp_location *loc;
879
880 for (loc = b->loc; loc; loc = loc->next)
881 {
882 loc->cond.reset ();
883
884 /* No need to free the condition agent expression
885 bytecode (if we have one). We will handle this
886 when we go through update_global_location_list. */
887 }
888 }
889
890 if (*exp == 0)
891 {
892 if (from_tty)
893 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
894 }
895 else
896 {
897 const char *arg = exp;
898
899 /* I don't know if it matters whether this is the string the user
900 typed in or the decompiled expression. */
901 b->cond_string = xstrdup (arg);
902 b->condition_not_parsed = 0;
903
904 if (is_watchpoint (b))
905 {
906 struct watchpoint *w = (struct watchpoint *) b;
907
908 innermost_block = NULL;
909 arg = exp;
910 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
911 if (*arg)
912 error (_("Junk at end of expression"));
913 w->cond_exp_valid_block = innermost_block;
914 }
915 else
916 {
917 struct bp_location *loc;
918
919 for (loc = b->loc; loc; loc = loc->next)
920 {
921 arg = exp;
922 loc->cond =
923 parse_exp_1 (&arg, loc->address,
924 block_for_pc (loc->address), 0);
925 if (*arg)
926 error (_("Junk at end of expression"));
927 }
928 }
929 }
930 mark_breakpoint_modified (b);
931
932 observer_notify_breakpoint_modified (b);
933 }
934
935 /* Completion for the "condition" command. */
936
937 static void
938 condition_completer (struct cmd_list_element *cmd,
939 completion_tracker &tracker,
940 const char *text, const char *word)
941 {
942 const char *space;
943
944 text = skip_spaces (text);
945 space = skip_to_space (text);
946 if (*space == '\0')
947 {
948 int len;
949 struct breakpoint *b;
950 VEC (char_ptr) *result = NULL;
951
952 if (text[0] == '$')
953 {
954 /* We don't support completion of history indices. */
955 if (!isdigit (text[1]))
956 complete_internalvar (tracker, &text[1]);
957 return;
958 }
959
960 /* We're completing the breakpoint number. */
961 len = strlen (text);
962
963 ALL_BREAKPOINTS (b)
964 {
965 char number[50];
966
967 xsnprintf (number, sizeof (number), "%d", b->number);
968
969 if (strncmp (number, text, len) == 0)
970 {
971 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
972 tracker.add_completion (std::move (copy));
973 }
974 }
975
976 return;
977 }
978
979 /* We're completing the expression part. */
980 text = skip_spaces (space);
981 expression_completer (cmd, tracker, text, word);
982 }
983
984 /* condition N EXP -- set break condition of breakpoint N to EXP. */
985
986 static void
987 condition_command (char *arg, int from_tty)
988 {
989 struct breakpoint *b;
990 char *p;
991 int bnum;
992
993 if (arg == 0)
994 error_no_arg (_("breakpoint number"));
995
996 p = arg;
997 bnum = get_number (&p);
998 if (bnum == 0)
999 error (_("Bad breakpoint argument: '%s'"), arg);
1000
1001 ALL_BREAKPOINTS (b)
1002 if (b->number == bnum)
1003 {
1004 /* Check if this breakpoint has a "stop" method implemented in an
1005 extension language. This method and conditions entered into GDB
1006 from the CLI are mutually exclusive. */
1007 const struct extension_language_defn *extlang
1008 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1009
1010 if (extlang != NULL)
1011 {
1012 error (_("Only one stop condition allowed. There is currently"
1013 " a %s stop condition defined for this breakpoint."),
1014 ext_lang_capitalized_name (extlang));
1015 }
1016 set_breakpoint_condition (b, p, from_tty);
1017
1018 if (is_breakpoint (b))
1019 update_global_location_list (UGLL_MAY_INSERT);
1020
1021 return;
1022 }
1023
1024 error (_("No breakpoint number %d."), bnum);
1025 }
1026
1027 /* Check that COMMAND do not contain commands that are suitable
1028 only for tracepoints and not suitable for ordinary breakpoints.
1029 Throw if any such commands is found. */
1030
1031 static void
1032 check_no_tracepoint_commands (struct command_line *commands)
1033 {
1034 struct command_line *c;
1035
1036 for (c = commands; c; c = c->next)
1037 {
1038 int i;
1039
1040 if (c->control_type == while_stepping_control)
1041 error (_("The 'while-stepping' command can "
1042 "only be used for tracepoints"));
1043
1044 for (i = 0; i < c->body_count; ++i)
1045 check_no_tracepoint_commands ((c->body_list)[i]);
1046
1047 /* Not that command parsing removes leading whitespace and comment
1048 lines and also empty lines. So, we only need to check for
1049 command directly. */
1050 if (strstr (c->line, "collect ") == c->line)
1051 error (_("The 'collect' command can only be used for tracepoints"));
1052
1053 if (strstr (c->line, "teval ") == c->line)
1054 error (_("The 'teval' command can only be used for tracepoints"));
1055 }
1056 }
1057
1058 struct longjmp_breakpoint : public breakpoint
1059 {
1060 ~longjmp_breakpoint () override;
1061 };
1062
1063 /* Encapsulate tests for different types of tracepoints. */
1064
1065 static bool
1066 is_tracepoint_type (bptype type)
1067 {
1068 return (type == bp_tracepoint
1069 || type == bp_fast_tracepoint
1070 || type == bp_static_tracepoint);
1071 }
1072
1073 static bool
1074 is_longjmp_type (bptype type)
1075 {
1076 return type == bp_longjmp || type == bp_exception;
1077 }
1078
1079 int
1080 is_tracepoint (const struct breakpoint *b)
1081 {
1082 return is_tracepoint_type (b->type);
1083 }
1084
1085 /* Factory function to create an appropriate instance of breakpoint given
1086 TYPE. */
1087
1088 static std::unique_ptr<breakpoint>
1089 new_breakpoint_from_type (bptype type)
1090 {
1091 breakpoint *b;
1092
1093 if (is_tracepoint_type (type))
1094 b = new tracepoint ();
1095 else if (is_longjmp_type (type))
1096 b = new longjmp_breakpoint ();
1097 else
1098 b = new breakpoint ();
1099
1100 return std::unique_ptr<breakpoint> (b);
1101 }
1102
1103 /* A helper function that validates that COMMANDS are valid for a
1104 breakpoint. This function will throw an exception if a problem is
1105 found. */
1106
1107 static void
1108 validate_commands_for_breakpoint (struct breakpoint *b,
1109 struct command_line *commands)
1110 {
1111 if (is_tracepoint (b))
1112 {
1113 struct tracepoint *t = (struct tracepoint *) b;
1114 struct command_line *c;
1115 struct command_line *while_stepping = 0;
1116
1117 /* Reset the while-stepping step count. The previous commands
1118 might have included a while-stepping action, while the new
1119 ones might not. */
1120 t->step_count = 0;
1121
1122 /* We need to verify that each top-level element of commands is
1123 valid for tracepoints, that there's at most one
1124 while-stepping element, and that the while-stepping's body
1125 has valid tracing commands excluding nested while-stepping.
1126 We also need to validate the tracepoint action line in the
1127 context of the tracepoint --- validate_actionline actually
1128 has side effects, like setting the tracepoint's
1129 while-stepping STEP_COUNT, in addition to checking if the
1130 collect/teval actions parse and make sense in the
1131 tracepoint's context. */
1132 for (c = commands; c; c = c->next)
1133 {
1134 if (c->control_type == while_stepping_control)
1135 {
1136 if (b->type == bp_fast_tracepoint)
1137 error (_("The 'while-stepping' command "
1138 "cannot be used for fast tracepoint"));
1139 else if (b->type == bp_static_tracepoint)
1140 error (_("The 'while-stepping' command "
1141 "cannot be used for static tracepoint"));
1142
1143 if (while_stepping)
1144 error (_("The 'while-stepping' command "
1145 "can be used only once"));
1146 else
1147 while_stepping = c;
1148 }
1149
1150 validate_actionline (c->line, b);
1151 }
1152 if (while_stepping)
1153 {
1154 struct command_line *c2;
1155
1156 gdb_assert (while_stepping->body_count == 1);
1157 c2 = while_stepping->body_list[0];
1158 for (; c2; c2 = c2->next)
1159 {
1160 if (c2->control_type == while_stepping_control)
1161 error (_("The 'while-stepping' command cannot be nested"));
1162 }
1163 }
1164 }
1165 else
1166 {
1167 check_no_tracepoint_commands (commands);
1168 }
1169 }
1170
1171 /* Return a vector of all the static tracepoints set at ADDR. The
1172 caller is responsible for releasing the vector. */
1173
1174 VEC(breakpoint_p) *
1175 static_tracepoints_here (CORE_ADDR addr)
1176 {
1177 struct breakpoint *b;
1178 VEC(breakpoint_p) *found = 0;
1179 struct bp_location *loc;
1180
1181 ALL_BREAKPOINTS (b)
1182 if (b->type == bp_static_tracepoint)
1183 {
1184 for (loc = b->loc; loc; loc = loc->next)
1185 if (loc->address == addr)
1186 VEC_safe_push(breakpoint_p, found, b);
1187 }
1188
1189 return found;
1190 }
1191
1192 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1193 validate that only allowed commands are included. */
1194
1195 void
1196 breakpoint_set_commands (struct breakpoint *b,
1197 command_line_up &&commands)
1198 {
1199 validate_commands_for_breakpoint (b, commands.get ());
1200
1201 b->commands = std::move (commands);
1202 observer_notify_breakpoint_modified (b);
1203 }
1204
1205 /* Set the internal `silent' flag on the breakpoint. Note that this
1206 is not the same as the "silent" that may appear in the breakpoint's
1207 commands. */
1208
1209 void
1210 breakpoint_set_silent (struct breakpoint *b, int silent)
1211 {
1212 int old_silent = b->silent;
1213
1214 b->silent = silent;
1215 if (old_silent != silent)
1216 observer_notify_breakpoint_modified (b);
1217 }
1218
1219 /* Set the thread for this breakpoint. If THREAD is -1, make the
1220 breakpoint work for any thread. */
1221
1222 void
1223 breakpoint_set_thread (struct breakpoint *b, int thread)
1224 {
1225 int old_thread = b->thread;
1226
1227 b->thread = thread;
1228 if (old_thread != thread)
1229 observer_notify_breakpoint_modified (b);
1230 }
1231
1232 /* Set the task for this breakpoint. If TASK is 0, make the
1233 breakpoint work for any task. */
1234
1235 void
1236 breakpoint_set_task (struct breakpoint *b, int task)
1237 {
1238 int old_task = b->task;
1239
1240 b->task = task;
1241 if (old_task != task)
1242 observer_notify_breakpoint_modified (b);
1243 }
1244
1245 void
1246 check_tracepoint_command (char *line, void *closure)
1247 {
1248 struct breakpoint *b = (struct breakpoint *) closure;
1249
1250 validate_actionline (line, b);
1251 }
1252
1253 static void
1254 commands_command_1 (const char *arg, int from_tty,
1255 struct command_line *control)
1256 {
1257 counted_command_line cmd;
1258
1259 std::string new_arg;
1260
1261 if (arg == NULL || !*arg)
1262 {
1263 if (breakpoint_count - prev_breakpoint_count > 1)
1264 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1265 breakpoint_count);
1266 else if (breakpoint_count > 0)
1267 new_arg = string_printf ("%d", breakpoint_count);
1268 arg = new_arg.c_str ();
1269 }
1270
1271 map_breakpoint_numbers
1272 (arg, [&] (breakpoint *b)
1273 {
1274 if (cmd == NULL)
1275 {
1276 if (control != NULL)
1277 cmd = copy_command_lines (control->body_list[0]);
1278 else
1279 {
1280 std::string str
1281 = string_printf (_("Type commands for breakpoint(s) "
1282 "%s, one per line."),
1283 arg);
1284
1285 cmd = read_command_lines (&str[0],
1286 from_tty, 1,
1287 (is_tracepoint (b)
1288 ? check_tracepoint_command : 0),
1289 b);
1290 }
1291 }
1292
1293 /* If a breakpoint was on the list more than once, we don't need to
1294 do anything. */
1295 if (b->commands != cmd)
1296 {
1297 validate_commands_for_breakpoint (b, cmd.get ());
1298 b->commands = cmd;
1299 observer_notify_breakpoint_modified (b);
1300 }
1301 });
1302
1303 if (cmd == NULL)
1304 error (_("No breakpoints specified."));
1305 }
1306
1307 static void
1308 commands_command (char *arg, int from_tty)
1309 {
1310 commands_command_1 (arg, from_tty, NULL);
1311 }
1312
1313 /* Like commands_command, but instead of reading the commands from
1314 input stream, takes them from an already parsed command structure.
1315
1316 This is used by cli-script.c to DTRT with breakpoint commands
1317 that are part of if and while bodies. */
1318 enum command_control_type
1319 commands_from_control_command (const char *arg, struct command_line *cmd)
1320 {
1321 commands_command_1 (arg, 0, cmd);
1322 return simple_control;
1323 }
1324
1325 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1326
1327 static int
1328 bp_location_has_shadow (struct bp_location *bl)
1329 {
1330 if (bl->loc_type != bp_loc_software_breakpoint)
1331 return 0;
1332 if (!bl->inserted)
1333 return 0;
1334 if (bl->target_info.shadow_len == 0)
1335 /* BL isn't valid, or doesn't shadow memory. */
1336 return 0;
1337 return 1;
1338 }
1339
1340 /* Update BUF, which is LEN bytes read from the target address
1341 MEMADDR, by replacing a memory breakpoint with its shadowed
1342 contents.
1343
1344 If READBUF is not NULL, this buffer must not overlap with the of
1345 the breakpoint location's shadow_contents buffer. Otherwise, a
1346 failed assertion internal error will be raised. */
1347
1348 static void
1349 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1350 const gdb_byte *writebuf_org,
1351 ULONGEST memaddr, LONGEST len,
1352 struct bp_target_info *target_info,
1353 struct gdbarch *gdbarch)
1354 {
1355 /* Now do full processing of the found relevant range of elements. */
1356 CORE_ADDR bp_addr = 0;
1357 int bp_size = 0;
1358 int bptoffset = 0;
1359
1360 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1361 current_program_space->aspace, 0))
1362 {
1363 /* The breakpoint is inserted in a different address space. */
1364 return;
1365 }
1366
1367 /* Addresses and length of the part of the breakpoint that
1368 we need to copy. */
1369 bp_addr = target_info->placed_address;
1370 bp_size = target_info->shadow_len;
1371
1372 if (bp_addr + bp_size <= memaddr)
1373 {
1374 /* The breakpoint is entirely before the chunk of memory we are
1375 reading. */
1376 return;
1377 }
1378
1379 if (bp_addr >= memaddr + len)
1380 {
1381 /* The breakpoint is entirely after the chunk of memory we are
1382 reading. */
1383 return;
1384 }
1385
1386 /* Offset within shadow_contents. */
1387 if (bp_addr < memaddr)
1388 {
1389 /* Only copy the second part of the breakpoint. */
1390 bp_size -= memaddr - bp_addr;
1391 bptoffset = memaddr - bp_addr;
1392 bp_addr = memaddr;
1393 }
1394
1395 if (bp_addr + bp_size > memaddr + len)
1396 {
1397 /* Only copy the first part of the breakpoint. */
1398 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1399 }
1400
1401 if (readbuf != NULL)
1402 {
1403 /* Verify that the readbuf buffer does not overlap with the
1404 shadow_contents buffer. */
1405 gdb_assert (target_info->shadow_contents >= readbuf + len
1406 || readbuf >= (target_info->shadow_contents
1407 + target_info->shadow_len));
1408
1409 /* Update the read buffer with this inserted breakpoint's
1410 shadow. */
1411 memcpy (readbuf + bp_addr - memaddr,
1412 target_info->shadow_contents + bptoffset, bp_size);
1413 }
1414 else
1415 {
1416 const unsigned char *bp;
1417 CORE_ADDR addr = target_info->reqstd_address;
1418 int placed_size;
1419
1420 /* Update the shadow with what we want to write to memory. */
1421 memcpy (target_info->shadow_contents + bptoffset,
1422 writebuf_org + bp_addr - memaddr, bp_size);
1423
1424 /* Determine appropriate breakpoint contents and size for this
1425 address. */
1426 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1427
1428 /* Update the final write buffer with this inserted
1429 breakpoint's INSN. */
1430 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1431 }
1432 }
1433
1434 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1435 by replacing any memory breakpoints with their shadowed contents.
1436
1437 If READBUF is not NULL, this buffer must not overlap with any of
1438 the breakpoint location's shadow_contents buffers. Otherwise,
1439 a failed assertion internal error will be raised.
1440
1441 The range of shadowed area by each bp_location is:
1442 bl->address - bp_locations_placed_address_before_address_max
1443 up to bl->address + bp_locations_shadow_len_after_address_max
1444 The range we were requested to resolve shadows for is:
1445 memaddr ... memaddr + len
1446 Thus the safe cutoff boundaries for performance optimization are
1447 memaddr + len <= (bl->address
1448 - bp_locations_placed_address_before_address_max)
1449 and:
1450 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1451
1452 void
1453 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1454 const gdb_byte *writebuf_org,
1455 ULONGEST memaddr, LONGEST len)
1456 {
1457 /* Left boundary, right boundary and median element of our binary
1458 search. */
1459 unsigned bc_l, bc_r, bc;
1460
1461 /* Find BC_L which is a leftmost element which may affect BUF
1462 content. It is safe to report lower value but a failure to
1463 report higher one. */
1464
1465 bc_l = 0;
1466 bc_r = bp_locations_count;
1467 while (bc_l + 1 < bc_r)
1468 {
1469 struct bp_location *bl;
1470
1471 bc = (bc_l + bc_r) / 2;
1472 bl = bp_locations[bc];
1473
1474 /* Check first BL->ADDRESS will not overflow due to the added
1475 constant. Then advance the left boundary only if we are sure
1476 the BC element can in no way affect the BUF content (MEMADDR
1477 to MEMADDR + LEN range).
1478
1479 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1480 offset so that we cannot miss a breakpoint with its shadow
1481 range tail still reaching MEMADDR. */
1482
1483 if ((bl->address + bp_locations_shadow_len_after_address_max
1484 >= bl->address)
1485 && (bl->address + bp_locations_shadow_len_after_address_max
1486 <= memaddr))
1487 bc_l = bc;
1488 else
1489 bc_r = bc;
1490 }
1491
1492 /* Due to the binary search above, we need to make sure we pick the
1493 first location that's at BC_L's address. E.g., if there are
1494 multiple locations at the same address, BC_L may end up pointing
1495 at a duplicate location, and miss the "master"/"inserted"
1496 location. Say, given locations L1, L2 and L3 at addresses A and
1497 B:
1498
1499 L1@A, L2@A, L3@B, ...
1500
1501 BC_L could end up pointing at location L2, while the "master"
1502 location could be L1. Since the `loc->inserted' flag is only set
1503 on "master" locations, we'd forget to restore the shadow of L1
1504 and L2. */
1505 while (bc_l > 0
1506 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1507 bc_l--;
1508
1509 /* Now do full processing of the found relevant range of elements. */
1510
1511 for (bc = bc_l; bc < bp_locations_count; bc++)
1512 {
1513 struct bp_location *bl = bp_locations[bc];
1514
1515 /* bp_location array has BL->OWNER always non-NULL. */
1516 if (bl->owner->type == bp_none)
1517 warning (_("reading through apparently deleted breakpoint #%d?"),
1518 bl->owner->number);
1519
1520 /* Performance optimization: any further element can no longer affect BUF
1521 content. */
1522
1523 if (bl->address >= bp_locations_placed_address_before_address_max
1524 && memaddr + len <= (bl->address
1525 - bp_locations_placed_address_before_address_max))
1526 break;
1527
1528 if (!bp_location_has_shadow (bl))
1529 continue;
1530
1531 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1532 memaddr, len, &bl->target_info, bl->gdbarch);
1533 }
1534 }
1535
1536 \f
1537
1538 /* Return true if BPT is either a software breakpoint or a hardware
1539 breakpoint. */
1540
1541 int
1542 is_breakpoint (const struct breakpoint *bpt)
1543 {
1544 return (bpt->type == bp_breakpoint
1545 || bpt->type == bp_hardware_breakpoint
1546 || bpt->type == bp_dprintf);
1547 }
1548
1549 /* Return true if BPT is of any hardware watchpoint kind. */
1550
1551 static int
1552 is_hardware_watchpoint (const struct breakpoint *bpt)
1553 {
1554 return (bpt->type == bp_hardware_watchpoint
1555 || bpt->type == bp_read_watchpoint
1556 || bpt->type == bp_access_watchpoint);
1557 }
1558
1559 /* Return true if BPT is of any watchpoint kind, hardware or
1560 software. */
1561
1562 int
1563 is_watchpoint (const struct breakpoint *bpt)
1564 {
1565 return (is_hardware_watchpoint (bpt)
1566 || bpt->type == bp_watchpoint);
1567 }
1568
1569 /* Returns true if the current thread and its running state are safe
1570 to evaluate or update watchpoint B. Watchpoints on local
1571 expressions need to be evaluated in the context of the thread that
1572 was current when the watchpoint was created, and, that thread needs
1573 to be stopped to be able to select the correct frame context.
1574 Watchpoints on global expressions can be evaluated on any thread,
1575 and in any state. It is presently left to the target allowing
1576 memory accesses when threads are running. */
1577
1578 static int
1579 watchpoint_in_thread_scope (struct watchpoint *b)
1580 {
1581 return (b->pspace == current_program_space
1582 && (ptid_equal (b->watchpoint_thread, null_ptid)
1583 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1584 && !is_executing (inferior_ptid))));
1585 }
1586
1587 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1588 associated bp_watchpoint_scope breakpoint. */
1589
1590 static void
1591 watchpoint_del_at_next_stop (struct watchpoint *w)
1592 {
1593 if (w->related_breakpoint != w)
1594 {
1595 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1596 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1597 w->related_breakpoint->disposition = disp_del_at_next_stop;
1598 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1599 w->related_breakpoint = w;
1600 }
1601 w->disposition = disp_del_at_next_stop;
1602 }
1603
1604 /* Extract a bitfield value from value VAL using the bit parameters contained in
1605 watchpoint W. */
1606
1607 static struct value *
1608 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1609 {
1610 struct value *bit_val;
1611
1612 if (val == NULL)
1613 return NULL;
1614
1615 bit_val = allocate_value (value_type (val));
1616
1617 unpack_value_bitfield (bit_val,
1618 w->val_bitpos,
1619 w->val_bitsize,
1620 value_contents_for_printing (val),
1621 value_offset (val),
1622 val);
1623
1624 return bit_val;
1625 }
1626
1627 /* Allocate a dummy location and add it to B, which must be a software
1628 watchpoint. This is required because even if a software watchpoint
1629 is not watching any memory, bpstat_stop_status requires a location
1630 to be able to report stops. */
1631
1632 static void
1633 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1634 struct program_space *pspace)
1635 {
1636 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1637
1638 b->loc = allocate_bp_location (b);
1639 b->loc->pspace = pspace;
1640 b->loc->address = -1;
1641 b->loc->length = -1;
1642 }
1643
1644 /* Returns true if B is a software watchpoint that is not watching any
1645 memory (e.g., "watch $pc"). */
1646
1647 static int
1648 is_no_memory_software_watchpoint (struct breakpoint *b)
1649 {
1650 return (b->type == bp_watchpoint
1651 && b->loc != NULL
1652 && b->loc->next == NULL
1653 && b->loc->address == -1
1654 && b->loc->length == -1);
1655 }
1656
1657 /* Assuming that B is a watchpoint:
1658 - Reparse watchpoint expression, if REPARSE is non-zero
1659 - Evaluate expression and store the result in B->val
1660 - Evaluate the condition if there is one, and store the result
1661 in b->loc->cond.
1662 - Update the list of values that must be watched in B->loc.
1663
1664 If the watchpoint disposition is disp_del_at_next_stop, then do
1665 nothing. If this is local watchpoint that is out of scope, delete
1666 it.
1667
1668 Even with `set breakpoint always-inserted on' the watchpoints are
1669 removed + inserted on each stop here. Normal breakpoints must
1670 never be removed because they might be missed by a running thread
1671 when debugging in non-stop mode. On the other hand, hardware
1672 watchpoints (is_hardware_watchpoint; processed here) are specific
1673 to each LWP since they are stored in each LWP's hardware debug
1674 registers. Therefore, such LWP must be stopped first in order to
1675 be able to modify its hardware watchpoints.
1676
1677 Hardware watchpoints must be reset exactly once after being
1678 presented to the user. It cannot be done sooner, because it would
1679 reset the data used to present the watchpoint hit to the user. And
1680 it must not be done later because it could display the same single
1681 watchpoint hit during multiple GDB stops. Note that the latter is
1682 relevant only to the hardware watchpoint types bp_read_watchpoint
1683 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1684 not user-visible - its hit is suppressed if the memory content has
1685 not changed.
1686
1687 The following constraints influence the location where we can reset
1688 hardware watchpoints:
1689
1690 * target_stopped_by_watchpoint and target_stopped_data_address are
1691 called several times when GDB stops.
1692
1693 [linux]
1694 * Multiple hardware watchpoints can be hit at the same time,
1695 causing GDB to stop. GDB only presents one hardware watchpoint
1696 hit at a time as the reason for stopping, and all the other hits
1697 are presented later, one after the other, each time the user
1698 requests the execution to be resumed. Execution is not resumed
1699 for the threads still having pending hit event stored in
1700 LWP_INFO->STATUS. While the watchpoint is already removed from
1701 the inferior on the first stop the thread hit event is kept being
1702 reported from its cached value by linux_nat_stopped_data_address
1703 until the real thread resume happens after the watchpoint gets
1704 presented and thus its LWP_INFO->STATUS gets reset.
1705
1706 Therefore the hardware watchpoint hit can get safely reset on the
1707 watchpoint removal from inferior. */
1708
1709 static void
1710 update_watchpoint (struct watchpoint *b, int reparse)
1711 {
1712 int within_current_scope;
1713 struct frame_id saved_frame_id;
1714 int frame_saved;
1715
1716 /* If this is a local watchpoint, we only want to check if the
1717 watchpoint frame is in scope if the current thread is the thread
1718 that was used to create the watchpoint. */
1719 if (!watchpoint_in_thread_scope (b))
1720 return;
1721
1722 if (b->disposition == disp_del_at_next_stop)
1723 return;
1724
1725 frame_saved = 0;
1726
1727 /* Determine if the watchpoint is within scope. */
1728 if (b->exp_valid_block == NULL)
1729 within_current_scope = 1;
1730 else
1731 {
1732 struct frame_info *fi = get_current_frame ();
1733 struct gdbarch *frame_arch = get_frame_arch (fi);
1734 CORE_ADDR frame_pc = get_frame_pc (fi);
1735
1736 /* If we're at a point where the stack has been destroyed
1737 (e.g. in a function epilogue), unwinding may not work
1738 properly. Do not attempt to recreate locations at this
1739 point. See similar comments in watchpoint_check. */
1740 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1741 return;
1742
1743 /* Save the current frame's ID so we can restore it after
1744 evaluating the watchpoint expression on its own frame. */
1745 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1746 took a frame parameter, so that we didn't have to change the
1747 selected frame. */
1748 frame_saved = 1;
1749 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1750
1751 fi = frame_find_by_id (b->watchpoint_frame);
1752 within_current_scope = (fi != NULL);
1753 if (within_current_scope)
1754 select_frame (fi);
1755 }
1756
1757 /* We don't free locations. They are stored in the bp_location array
1758 and update_global_location_list will eventually delete them and
1759 remove breakpoints if needed. */
1760 b->loc = NULL;
1761
1762 if (within_current_scope && reparse)
1763 {
1764 const char *s;
1765
1766 b->exp.reset ();
1767 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1768 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1769 /* If the meaning of expression itself changed, the old value is
1770 no longer relevant. We don't want to report a watchpoint hit
1771 to the user when the old value and the new value may actually
1772 be completely different objects. */
1773 value_free (b->val);
1774 b->val = NULL;
1775 b->val_valid = 0;
1776
1777 /* Note that unlike with breakpoints, the watchpoint's condition
1778 expression is stored in the breakpoint object, not in the
1779 locations (re)created below. */
1780 if (b->cond_string != NULL)
1781 {
1782 b->cond_exp.reset ();
1783
1784 s = b->cond_string;
1785 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1786 }
1787 }
1788
1789 /* If we failed to parse the expression, for example because
1790 it refers to a global variable in a not-yet-loaded shared library,
1791 don't try to insert watchpoint. We don't automatically delete
1792 such watchpoint, though, since failure to parse expression
1793 is different from out-of-scope watchpoint. */
1794 if (!target_has_execution)
1795 {
1796 /* Without execution, memory can't change. No use to try and
1797 set watchpoint locations. The watchpoint will be reset when
1798 the target gains execution, through breakpoint_re_set. */
1799 if (!can_use_hw_watchpoints)
1800 {
1801 if (b->ops->works_in_software_mode (b))
1802 b->type = bp_watchpoint;
1803 else
1804 error (_("Can't set read/access watchpoint when "
1805 "hardware watchpoints are disabled."));
1806 }
1807 }
1808 else if (within_current_scope && b->exp)
1809 {
1810 int pc = 0;
1811 struct value *val_chain, *v, *result, *next;
1812 struct program_space *frame_pspace;
1813
1814 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1815
1816 /* Avoid setting b->val if it's already set. The meaning of
1817 b->val is 'the last value' user saw, and we should update
1818 it only if we reported that last value to user. As it
1819 happens, the code that reports it updates b->val directly.
1820 We don't keep track of the memory value for masked
1821 watchpoints. */
1822 if (!b->val_valid && !is_masked_watchpoint (b))
1823 {
1824 if (b->val_bitsize != 0)
1825 {
1826 v = extract_bitfield_from_watchpoint_value (b, v);
1827 if (v != NULL)
1828 release_value (v);
1829 }
1830 b->val = v;
1831 b->val_valid = 1;
1832 }
1833
1834 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1835
1836 /* Look at each value on the value chain. */
1837 for (v = val_chain; v; v = value_next (v))
1838 {
1839 /* If it's a memory location, and GDB actually needed
1840 its contents to evaluate the expression, then we
1841 must watch it. If the first value returned is
1842 still lazy, that means an error occurred reading it;
1843 watch it anyway in case it becomes readable. */
1844 if (VALUE_LVAL (v) == lval_memory
1845 && (v == val_chain || ! value_lazy (v)))
1846 {
1847 struct type *vtype = check_typedef (value_type (v));
1848
1849 /* We only watch structs and arrays if user asked
1850 for it explicitly, never if they just happen to
1851 appear in the middle of some value chain. */
1852 if (v == result
1853 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1854 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1855 {
1856 CORE_ADDR addr;
1857 enum target_hw_bp_type type;
1858 struct bp_location *loc, **tmp;
1859 int bitpos = 0, bitsize = 0;
1860
1861 if (value_bitsize (v) != 0)
1862 {
1863 /* Extract the bit parameters out from the bitfield
1864 sub-expression. */
1865 bitpos = value_bitpos (v);
1866 bitsize = value_bitsize (v);
1867 }
1868 else if (v == result && b->val_bitsize != 0)
1869 {
1870 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1871 lvalue whose bit parameters are saved in the fields
1872 VAL_BITPOS and VAL_BITSIZE. */
1873 bitpos = b->val_bitpos;
1874 bitsize = b->val_bitsize;
1875 }
1876
1877 addr = value_address (v);
1878 if (bitsize != 0)
1879 {
1880 /* Skip the bytes that don't contain the bitfield. */
1881 addr += bitpos / 8;
1882 }
1883
1884 type = hw_write;
1885 if (b->type == bp_read_watchpoint)
1886 type = hw_read;
1887 else if (b->type == bp_access_watchpoint)
1888 type = hw_access;
1889
1890 loc = allocate_bp_location (b);
1891 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1892 ;
1893 *tmp = loc;
1894 loc->gdbarch = get_type_arch (value_type (v));
1895
1896 loc->pspace = frame_pspace;
1897 loc->address = addr;
1898
1899 if (bitsize != 0)
1900 {
1901 /* Just cover the bytes that make up the bitfield. */
1902 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1903 }
1904 else
1905 loc->length = TYPE_LENGTH (value_type (v));
1906
1907 loc->watchpoint_type = type;
1908 }
1909 }
1910 }
1911
1912 /* Change the type of breakpoint between hardware assisted or
1913 an ordinary watchpoint depending on the hardware support
1914 and free hardware slots. REPARSE is set when the inferior
1915 is started. */
1916 if (reparse)
1917 {
1918 int reg_cnt;
1919 enum bp_loc_type loc_type;
1920 struct bp_location *bl;
1921
1922 reg_cnt = can_use_hardware_watchpoint (val_chain);
1923
1924 if (reg_cnt)
1925 {
1926 int i, target_resources_ok, other_type_used;
1927 enum bptype type;
1928
1929 /* Use an exact watchpoint when there's only one memory region to be
1930 watched, and only one debug register is needed to watch it. */
1931 b->exact = target_exact_watchpoints && reg_cnt == 1;
1932
1933 /* We need to determine how many resources are already
1934 used for all other hardware watchpoints plus this one
1935 to see if we still have enough resources to also fit
1936 this watchpoint in as well. */
1937
1938 /* If this is a software watchpoint, we try to turn it
1939 to a hardware one -- count resources as if B was of
1940 hardware watchpoint type. */
1941 type = b->type;
1942 if (type == bp_watchpoint)
1943 type = bp_hardware_watchpoint;
1944
1945 /* This watchpoint may or may not have been placed on
1946 the list yet at this point (it won't be in the list
1947 if we're trying to create it for the first time,
1948 through watch_command), so always account for it
1949 manually. */
1950
1951 /* Count resources used by all watchpoints except B. */
1952 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1953
1954 /* Add in the resources needed for B. */
1955 i += hw_watchpoint_use_count (b);
1956
1957 target_resources_ok
1958 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1959 if (target_resources_ok <= 0)
1960 {
1961 int sw_mode = b->ops->works_in_software_mode (b);
1962
1963 if (target_resources_ok == 0 && !sw_mode)
1964 error (_("Target does not support this type of "
1965 "hardware watchpoint."));
1966 else if (target_resources_ok < 0 && !sw_mode)
1967 error (_("There are not enough available hardware "
1968 "resources for this watchpoint."));
1969
1970 /* Downgrade to software watchpoint. */
1971 b->type = bp_watchpoint;
1972 }
1973 else
1974 {
1975 /* If this was a software watchpoint, we've just
1976 found we have enough resources to turn it to a
1977 hardware watchpoint. Otherwise, this is a
1978 nop. */
1979 b->type = type;
1980 }
1981 }
1982 else if (!b->ops->works_in_software_mode (b))
1983 {
1984 if (!can_use_hw_watchpoints)
1985 error (_("Can't set read/access watchpoint when "
1986 "hardware watchpoints are disabled."));
1987 else
1988 error (_("Expression cannot be implemented with "
1989 "read/access watchpoint."));
1990 }
1991 else
1992 b->type = bp_watchpoint;
1993
1994 loc_type = (b->type == bp_watchpoint? bp_loc_other
1995 : bp_loc_hardware_watchpoint);
1996 for (bl = b->loc; bl; bl = bl->next)
1997 bl->loc_type = loc_type;
1998 }
1999
2000 for (v = val_chain; v; v = next)
2001 {
2002 next = value_next (v);
2003 if (v != b->val)
2004 value_free (v);
2005 }
2006
2007 /* If a software watchpoint is not watching any memory, then the
2008 above left it without any location set up. But,
2009 bpstat_stop_status requires a location to be able to report
2010 stops, so make sure there's at least a dummy one. */
2011 if (b->type == bp_watchpoint && b->loc == NULL)
2012 software_watchpoint_add_no_memory_location (b, frame_pspace);
2013 }
2014 else if (!within_current_scope)
2015 {
2016 printf_filtered (_("\
2017 Watchpoint %d deleted because the program has left the block\n\
2018 in which its expression is valid.\n"),
2019 b->number);
2020 watchpoint_del_at_next_stop (b);
2021 }
2022
2023 /* Restore the selected frame. */
2024 if (frame_saved)
2025 select_frame (frame_find_by_id (saved_frame_id));
2026 }
2027
2028
2029 /* Returns 1 iff breakpoint location should be
2030 inserted in the inferior. We don't differentiate the type of BL's owner
2031 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2032 breakpoint_ops is not defined, because in insert_bp_location,
2033 tracepoint's insert_location will not be called. */
2034 static int
2035 should_be_inserted (struct bp_location *bl)
2036 {
2037 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2038 return 0;
2039
2040 if (bl->owner->disposition == disp_del_at_next_stop)
2041 return 0;
2042
2043 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2044 return 0;
2045
2046 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2047 return 0;
2048
2049 /* This is set for example, when we're attached to the parent of a
2050 vfork, and have detached from the child. The child is running
2051 free, and we expect it to do an exec or exit, at which point the
2052 OS makes the parent schedulable again (and the target reports
2053 that the vfork is done). Until the child is done with the shared
2054 memory region, do not insert breakpoints in the parent, otherwise
2055 the child could still trip on the parent's breakpoints. Since
2056 the parent is blocked anyway, it won't miss any breakpoint. */
2057 if (bl->pspace->breakpoints_not_allowed)
2058 return 0;
2059
2060 /* Don't insert a breakpoint if we're trying to step past its
2061 location, except if the breakpoint is a single-step breakpoint,
2062 and the breakpoint's thread is the thread which is stepping past
2063 a breakpoint. */
2064 if ((bl->loc_type == bp_loc_software_breakpoint
2065 || bl->loc_type == bp_loc_hardware_breakpoint)
2066 && stepping_past_instruction_at (bl->pspace->aspace,
2067 bl->address)
2068 /* The single-step breakpoint may be inserted at the location
2069 we're trying to step if the instruction branches to itself.
2070 However, the instruction won't be executed at all and it may
2071 break the semantics of the instruction, for example, the
2072 instruction is a conditional branch or updates some flags.
2073 We can't fix it unless GDB is able to emulate the instruction
2074 or switch to displaced stepping. */
2075 && !(bl->owner->type == bp_single_step
2076 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2077 {
2078 if (debug_infrun)
2079 {
2080 fprintf_unfiltered (gdb_stdlog,
2081 "infrun: skipping breakpoint: "
2082 "stepping past insn at: %s\n",
2083 paddress (bl->gdbarch, bl->address));
2084 }
2085 return 0;
2086 }
2087
2088 /* Don't insert watchpoints if we're trying to step past the
2089 instruction that triggered one. */
2090 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2091 && stepping_past_nonsteppable_watchpoint ())
2092 {
2093 if (debug_infrun)
2094 {
2095 fprintf_unfiltered (gdb_stdlog,
2096 "infrun: stepping past non-steppable watchpoint. "
2097 "skipping watchpoint at %s:%d\n",
2098 paddress (bl->gdbarch, bl->address),
2099 bl->length);
2100 }
2101 return 0;
2102 }
2103
2104 return 1;
2105 }
2106
2107 /* Same as should_be_inserted but does the check assuming
2108 that the location is not duplicated. */
2109
2110 static int
2111 unduplicated_should_be_inserted (struct bp_location *bl)
2112 {
2113 int result;
2114 const int save_duplicate = bl->duplicate;
2115
2116 bl->duplicate = 0;
2117 result = should_be_inserted (bl);
2118 bl->duplicate = save_duplicate;
2119 return result;
2120 }
2121
2122 /* Parses a conditional described by an expression COND into an
2123 agent expression bytecode suitable for evaluation
2124 by the bytecode interpreter. Return NULL if there was
2125 any error during parsing. */
2126
2127 static agent_expr_up
2128 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2129 {
2130 if (cond == NULL)
2131 return NULL;
2132
2133 agent_expr_up aexpr;
2134
2135 /* We don't want to stop processing, so catch any errors
2136 that may show up. */
2137 TRY
2138 {
2139 aexpr = gen_eval_for_expr (scope, cond);
2140 }
2141
2142 CATCH (ex, RETURN_MASK_ERROR)
2143 {
2144 /* If we got here, it means the condition could not be parsed to a valid
2145 bytecode expression and thus can't be evaluated on the target's side.
2146 It's no use iterating through the conditions. */
2147 }
2148 END_CATCH
2149
2150 /* We have a valid agent expression. */
2151 return aexpr;
2152 }
2153
2154 /* Based on location BL, create a list of breakpoint conditions to be
2155 passed on to the target. If we have duplicated locations with different
2156 conditions, we will add such conditions to the list. The idea is that the
2157 target will evaluate the list of conditions and will only notify GDB when
2158 one of them is true. */
2159
2160 static void
2161 build_target_condition_list (struct bp_location *bl)
2162 {
2163 struct bp_location **locp = NULL, **loc2p;
2164 int null_condition_or_parse_error = 0;
2165 int modified = bl->needs_update;
2166 struct bp_location *loc;
2167
2168 /* Release conditions left over from a previous insert. */
2169 bl->target_info.conditions.clear ();
2170
2171 /* This is only meaningful if the target is
2172 evaluating conditions and if the user has
2173 opted for condition evaluation on the target's
2174 side. */
2175 if (gdb_evaluates_breakpoint_condition_p ()
2176 || !target_supports_evaluation_of_breakpoint_conditions ())
2177 return;
2178
2179 /* Do a first pass to check for locations with no assigned
2180 conditions or conditions that fail to parse to a valid agent expression
2181 bytecode. If any of these happen, then it's no use to send conditions
2182 to the target since this location will always trigger and generate a
2183 response back to GDB. */
2184 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2185 {
2186 loc = (*loc2p);
2187 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2188 {
2189 if (modified)
2190 {
2191 /* Re-parse the conditions since something changed. In that
2192 case we already freed the condition bytecodes (see
2193 force_breakpoint_reinsertion). We just
2194 need to parse the condition to bytecodes again. */
2195 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2196 loc->cond.get ());
2197 }
2198
2199 /* If we have a NULL bytecode expression, it means something
2200 went wrong or we have a null condition expression. */
2201 if (!loc->cond_bytecode)
2202 {
2203 null_condition_or_parse_error = 1;
2204 break;
2205 }
2206 }
2207 }
2208
2209 /* If any of these happened, it means we will have to evaluate the conditions
2210 for the location's address on gdb's side. It is no use keeping bytecodes
2211 for all the other duplicate locations, thus we free all of them here.
2212
2213 This is so we have a finer control over which locations' conditions are
2214 being evaluated by GDB or the remote stub. */
2215 if (null_condition_or_parse_error)
2216 {
2217 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2218 {
2219 loc = (*loc2p);
2220 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2221 {
2222 /* Only go as far as the first NULL bytecode is
2223 located. */
2224 if (!loc->cond_bytecode)
2225 return;
2226
2227 loc->cond_bytecode.reset ();
2228 }
2229 }
2230 }
2231
2232 /* No NULL conditions or failed bytecode generation. Build a condition list
2233 for this location's address. */
2234 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2235 {
2236 loc = (*loc2p);
2237 if (loc->cond
2238 && is_breakpoint (loc->owner)
2239 && loc->pspace->num == bl->pspace->num
2240 && loc->owner->enable_state == bp_enabled
2241 && loc->enabled)
2242 {
2243 /* Add the condition to the vector. This will be used later
2244 to send the conditions to the target. */
2245 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2246 }
2247 }
2248
2249 return;
2250 }
2251
2252 /* Parses a command described by string CMD into an agent expression
2253 bytecode suitable for evaluation by the bytecode interpreter.
2254 Return NULL if there was any error during parsing. */
2255
2256 static agent_expr_up
2257 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2258 {
2259 struct cleanup *old_cleanups = 0;
2260 struct expression **argvec;
2261 const char *cmdrest;
2262 const char *format_start, *format_end;
2263 struct format_piece *fpieces;
2264 int nargs;
2265 struct gdbarch *gdbarch = get_current_arch ();
2266
2267 if (cmd == NULL)
2268 return NULL;
2269
2270 cmdrest = cmd;
2271
2272 if (*cmdrest == ',')
2273 ++cmdrest;
2274 cmdrest = skip_spaces (cmdrest);
2275
2276 if (*cmdrest++ != '"')
2277 error (_("No format string following the location"));
2278
2279 format_start = cmdrest;
2280
2281 fpieces = parse_format_string (&cmdrest);
2282
2283 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2284
2285 format_end = cmdrest;
2286
2287 if (*cmdrest++ != '"')
2288 error (_("Bad format string, non-terminated '\"'."));
2289
2290 cmdrest = skip_spaces (cmdrest);
2291
2292 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2293 error (_("Invalid argument syntax"));
2294
2295 if (*cmdrest == ',')
2296 cmdrest++;
2297 cmdrest = skip_spaces (cmdrest);
2298
2299 /* For each argument, make an expression. */
2300
2301 argvec = (struct expression **) alloca (strlen (cmd)
2302 * sizeof (struct expression *));
2303
2304 nargs = 0;
2305 while (*cmdrest != '\0')
2306 {
2307 const char *cmd1;
2308
2309 cmd1 = cmdrest;
2310 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2311 argvec[nargs++] = expr.release ();
2312 cmdrest = cmd1;
2313 if (*cmdrest == ',')
2314 ++cmdrest;
2315 }
2316
2317 agent_expr_up aexpr;
2318
2319 /* We don't want to stop processing, so catch any errors
2320 that may show up. */
2321 TRY
2322 {
2323 aexpr = gen_printf (scope, gdbarch, 0, 0,
2324 format_start, format_end - format_start,
2325 fpieces, nargs, argvec);
2326 }
2327 CATCH (ex, RETURN_MASK_ERROR)
2328 {
2329 /* If we got here, it means the command could not be parsed to a valid
2330 bytecode expression and thus can't be evaluated on the target's side.
2331 It's no use iterating through the other commands. */
2332 }
2333 END_CATCH
2334
2335 do_cleanups (old_cleanups);
2336
2337 /* We have a valid agent expression, return it. */
2338 return aexpr;
2339 }
2340
2341 /* Based on location BL, create a list of breakpoint commands to be
2342 passed on to the target. If we have duplicated locations with
2343 different commands, we will add any such to the list. */
2344
2345 static void
2346 build_target_command_list (struct bp_location *bl)
2347 {
2348 struct bp_location **locp = NULL, **loc2p;
2349 int null_command_or_parse_error = 0;
2350 int modified = bl->needs_update;
2351 struct bp_location *loc;
2352
2353 /* Clear commands left over from a previous insert. */
2354 bl->target_info.tcommands.clear ();
2355
2356 if (!target_can_run_breakpoint_commands ())
2357 return;
2358
2359 /* For now, limit to agent-style dprintf breakpoints. */
2360 if (dprintf_style != dprintf_style_agent)
2361 return;
2362
2363 /* For now, if we have any duplicate location that isn't a dprintf,
2364 don't install the target-side commands, as that would make the
2365 breakpoint not be reported to the core, and we'd lose
2366 control. */
2367 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2368 {
2369 loc = (*loc2p);
2370 if (is_breakpoint (loc->owner)
2371 && loc->pspace->num == bl->pspace->num
2372 && loc->owner->type != bp_dprintf)
2373 return;
2374 }
2375
2376 /* Do a first pass to check for locations with no assigned
2377 conditions or conditions that fail to parse to a valid agent expression
2378 bytecode. If any of these happen, then it's no use to send conditions
2379 to the target since this location will always trigger and generate a
2380 response back to GDB. */
2381 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2382 {
2383 loc = (*loc2p);
2384 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2385 {
2386 if (modified)
2387 {
2388 /* Re-parse the commands since something changed. In that
2389 case we already freed the command bytecodes (see
2390 force_breakpoint_reinsertion). We just
2391 need to parse the command to bytecodes again. */
2392 loc->cmd_bytecode
2393 = parse_cmd_to_aexpr (bl->address,
2394 loc->owner->extra_string);
2395 }
2396
2397 /* If we have a NULL bytecode expression, it means something
2398 went wrong or we have a null command expression. */
2399 if (!loc->cmd_bytecode)
2400 {
2401 null_command_or_parse_error = 1;
2402 break;
2403 }
2404 }
2405 }
2406
2407 /* If anything failed, then we're not doing target-side commands,
2408 and so clean up. */
2409 if (null_command_or_parse_error)
2410 {
2411 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2412 {
2413 loc = (*loc2p);
2414 if (is_breakpoint (loc->owner)
2415 && loc->pspace->num == bl->pspace->num)
2416 {
2417 /* Only go as far as the first NULL bytecode is
2418 located. */
2419 if (loc->cmd_bytecode == NULL)
2420 return;
2421
2422 loc->cmd_bytecode.reset ();
2423 }
2424 }
2425 }
2426
2427 /* No NULL commands or failed bytecode generation. Build a command list
2428 for this location's address. */
2429 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2430 {
2431 loc = (*loc2p);
2432 if (loc->owner->extra_string
2433 && is_breakpoint (loc->owner)
2434 && loc->pspace->num == bl->pspace->num
2435 && loc->owner->enable_state == bp_enabled
2436 && loc->enabled)
2437 {
2438 /* Add the command to the vector. This will be used later
2439 to send the commands to the target. */
2440 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2441 }
2442 }
2443
2444 bl->target_info.persist = 0;
2445 /* Maybe flag this location as persistent. */
2446 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2447 bl->target_info.persist = 1;
2448 }
2449
2450 /* Return the kind of breakpoint on address *ADDR. Get the kind
2451 of breakpoint according to ADDR except single-step breakpoint.
2452 Get the kind of single-step breakpoint according to the current
2453 registers state. */
2454
2455 static int
2456 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2457 {
2458 if (bl->owner->type == bp_single_step)
2459 {
2460 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2461 struct regcache *regcache;
2462
2463 regcache = get_thread_regcache (thr->ptid);
2464
2465 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2466 regcache, addr);
2467 }
2468 else
2469 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2470 }
2471
2472 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2473 location. Any error messages are printed to TMP_ERROR_STREAM; and
2474 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2475 Returns 0 for success, 1 if the bp_location type is not supported or
2476 -1 for failure.
2477
2478 NOTE drow/2003-09-09: This routine could be broken down to an
2479 object-style method for each breakpoint or catchpoint type. */
2480 static int
2481 insert_bp_location (struct bp_location *bl,
2482 struct ui_file *tmp_error_stream,
2483 int *disabled_breaks,
2484 int *hw_breakpoint_error,
2485 int *hw_bp_error_explained_already)
2486 {
2487 enum errors bp_err = GDB_NO_ERROR;
2488 const char *bp_err_message = NULL;
2489
2490 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2491 return 0;
2492
2493 /* Note we don't initialize bl->target_info, as that wipes out
2494 the breakpoint location's shadow_contents if the breakpoint
2495 is still inserted at that location. This in turn breaks
2496 target_read_memory which depends on these buffers when
2497 a memory read is requested at the breakpoint location:
2498 Once the target_info has been wiped, we fail to see that
2499 we have a breakpoint inserted at that address and thus
2500 read the breakpoint instead of returning the data saved in
2501 the breakpoint location's shadow contents. */
2502 bl->target_info.reqstd_address = bl->address;
2503 bl->target_info.placed_address_space = bl->pspace->aspace;
2504 bl->target_info.length = bl->length;
2505
2506 /* When working with target-side conditions, we must pass all the conditions
2507 for the same breakpoint address down to the target since GDB will not
2508 insert those locations. With a list of breakpoint conditions, the target
2509 can decide when to stop and notify GDB. */
2510
2511 if (is_breakpoint (bl->owner))
2512 {
2513 build_target_condition_list (bl);
2514 build_target_command_list (bl);
2515 /* Reset the modification marker. */
2516 bl->needs_update = 0;
2517 }
2518
2519 if (bl->loc_type == bp_loc_software_breakpoint
2520 || bl->loc_type == bp_loc_hardware_breakpoint)
2521 {
2522 if (bl->owner->type != bp_hardware_breakpoint)
2523 {
2524 /* If the explicitly specified breakpoint type
2525 is not hardware breakpoint, check the memory map to see
2526 if the breakpoint address is in read only memory or not.
2527
2528 Two important cases are:
2529 - location type is not hardware breakpoint, memory
2530 is readonly. We change the type of the location to
2531 hardware breakpoint.
2532 - location type is hardware breakpoint, memory is
2533 read-write. This means we've previously made the
2534 location hardware one, but then the memory map changed,
2535 so we undo.
2536
2537 When breakpoints are removed, remove_breakpoints will use
2538 location types we've just set here, the only possible
2539 problem is that memory map has changed during running
2540 program, but it's not going to work anyway with current
2541 gdb. */
2542 struct mem_region *mr
2543 = lookup_mem_region (bl->target_info.reqstd_address);
2544
2545 if (mr)
2546 {
2547 if (automatic_hardware_breakpoints)
2548 {
2549 enum bp_loc_type new_type;
2550
2551 if (mr->attrib.mode != MEM_RW)
2552 new_type = bp_loc_hardware_breakpoint;
2553 else
2554 new_type = bp_loc_software_breakpoint;
2555
2556 if (new_type != bl->loc_type)
2557 {
2558 static int said = 0;
2559
2560 bl->loc_type = new_type;
2561 if (!said)
2562 {
2563 fprintf_filtered (gdb_stdout,
2564 _("Note: automatically using "
2565 "hardware breakpoints for "
2566 "read-only addresses.\n"));
2567 said = 1;
2568 }
2569 }
2570 }
2571 else if (bl->loc_type == bp_loc_software_breakpoint
2572 && mr->attrib.mode != MEM_RW)
2573 {
2574 fprintf_unfiltered (tmp_error_stream,
2575 _("Cannot insert breakpoint %d.\n"
2576 "Cannot set software breakpoint "
2577 "at read-only address %s\n"),
2578 bl->owner->number,
2579 paddress (bl->gdbarch, bl->address));
2580 return 1;
2581 }
2582 }
2583 }
2584
2585 /* First check to see if we have to handle an overlay. */
2586 if (overlay_debugging == ovly_off
2587 || bl->section == NULL
2588 || !(section_is_overlay (bl->section)))
2589 {
2590 /* No overlay handling: just set the breakpoint. */
2591 TRY
2592 {
2593 int val;
2594
2595 val = bl->owner->ops->insert_location (bl);
2596 if (val)
2597 bp_err = GENERIC_ERROR;
2598 }
2599 CATCH (e, RETURN_MASK_ALL)
2600 {
2601 bp_err = e.error;
2602 bp_err_message = e.message;
2603 }
2604 END_CATCH
2605 }
2606 else
2607 {
2608 /* This breakpoint is in an overlay section.
2609 Shall we set a breakpoint at the LMA? */
2610 if (!overlay_events_enabled)
2611 {
2612 /* Yes -- overlay event support is not active,
2613 so we must try to set a breakpoint at the LMA.
2614 This will not work for a hardware breakpoint. */
2615 if (bl->loc_type == bp_loc_hardware_breakpoint)
2616 warning (_("hardware breakpoint %d not supported in overlay!"),
2617 bl->owner->number);
2618 else
2619 {
2620 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2621 bl->section);
2622 /* Set a software (trap) breakpoint at the LMA. */
2623 bl->overlay_target_info = bl->target_info;
2624 bl->overlay_target_info.reqstd_address = addr;
2625
2626 /* No overlay handling: just set the breakpoint. */
2627 TRY
2628 {
2629 int val;
2630
2631 bl->overlay_target_info.kind
2632 = breakpoint_kind (bl, &addr);
2633 bl->overlay_target_info.placed_address = addr;
2634 val = target_insert_breakpoint (bl->gdbarch,
2635 &bl->overlay_target_info);
2636 if (val)
2637 bp_err = GENERIC_ERROR;
2638 }
2639 CATCH (e, RETURN_MASK_ALL)
2640 {
2641 bp_err = e.error;
2642 bp_err_message = e.message;
2643 }
2644 END_CATCH
2645
2646 if (bp_err != GDB_NO_ERROR)
2647 fprintf_unfiltered (tmp_error_stream,
2648 "Overlay breakpoint %d "
2649 "failed: in ROM?\n",
2650 bl->owner->number);
2651 }
2652 }
2653 /* Shall we set a breakpoint at the VMA? */
2654 if (section_is_mapped (bl->section))
2655 {
2656 /* Yes. This overlay section is mapped into memory. */
2657 TRY
2658 {
2659 int val;
2660
2661 val = bl->owner->ops->insert_location (bl);
2662 if (val)
2663 bp_err = GENERIC_ERROR;
2664 }
2665 CATCH (e, RETURN_MASK_ALL)
2666 {
2667 bp_err = e.error;
2668 bp_err_message = e.message;
2669 }
2670 END_CATCH
2671 }
2672 else
2673 {
2674 /* No. This breakpoint will not be inserted.
2675 No error, but do not mark the bp as 'inserted'. */
2676 return 0;
2677 }
2678 }
2679
2680 if (bp_err != GDB_NO_ERROR)
2681 {
2682 /* Can't set the breakpoint. */
2683
2684 /* In some cases, we might not be able to insert a
2685 breakpoint in a shared library that has already been
2686 removed, but we have not yet processed the shlib unload
2687 event. Unfortunately, some targets that implement
2688 breakpoint insertion themselves can't tell why the
2689 breakpoint insertion failed (e.g., the remote target
2690 doesn't define error codes), so we must treat generic
2691 errors as memory errors. */
2692 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2693 && bl->loc_type == bp_loc_software_breakpoint
2694 && (solib_name_from_address (bl->pspace, bl->address)
2695 || shared_objfile_contains_address_p (bl->pspace,
2696 bl->address)))
2697 {
2698 /* See also: disable_breakpoints_in_shlibs. */
2699 bl->shlib_disabled = 1;
2700 observer_notify_breakpoint_modified (bl->owner);
2701 if (!*disabled_breaks)
2702 {
2703 fprintf_unfiltered (tmp_error_stream,
2704 "Cannot insert breakpoint %d.\n",
2705 bl->owner->number);
2706 fprintf_unfiltered (tmp_error_stream,
2707 "Temporarily disabling shared "
2708 "library breakpoints:\n");
2709 }
2710 *disabled_breaks = 1;
2711 fprintf_unfiltered (tmp_error_stream,
2712 "breakpoint #%d\n", bl->owner->number);
2713 return 0;
2714 }
2715 else
2716 {
2717 if (bl->loc_type == bp_loc_hardware_breakpoint)
2718 {
2719 *hw_breakpoint_error = 1;
2720 *hw_bp_error_explained_already = bp_err_message != NULL;
2721 fprintf_unfiltered (tmp_error_stream,
2722 "Cannot insert hardware breakpoint %d%s",
2723 bl->owner->number, bp_err_message ? ":" : ".\n");
2724 if (bp_err_message != NULL)
2725 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2726 }
2727 else
2728 {
2729 if (bp_err_message == NULL)
2730 {
2731 std::string message
2732 = memory_error_message (TARGET_XFER_E_IO,
2733 bl->gdbarch, bl->address);
2734
2735 fprintf_unfiltered (tmp_error_stream,
2736 "Cannot insert breakpoint %d.\n"
2737 "%s\n",
2738 bl->owner->number, message.c_str ());
2739 }
2740 else
2741 {
2742 fprintf_unfiltered (tmp_error_stream,
2743 "Cannot insert breakpoint %d: %s\n",
2744 bl->owner->number,
2745 bp_err_message);
2746 }
2747 }
2748 return 1;
2749
2750 }
2751 }
2752 else
2753 bl->inserted = 1;
2754
2755 return 0;
2756 }
2757
2758 else if (bl->loc_type == bp_loc_hardware_watchpoint
2759 /* NOTE drow/2003-09-08: This state only exists for removing
2760 watchpoints. It's not clear that it's necessary... */
2761 && bl->owner->disposition != disp_del_at_next_stop)
2762 {
2763 int val;
2764
2765 gdb_assert (bl->owner->ops != NULL
2766 && bl->owner->ops->insert_location != NULL);
2767
2768 val = bl->owner->ops->insert_location (bl);
2769
2770 /* If trying to set a read-watchpoint, and it turns out it's not
2771 supported, try emulating one with an access watchpoint. */
2772 if (val == 1 && bl->watchpoint_type == hw_read)
2773 {
2774 struct bp_location *loc, **loc_temp;
2775
2776 /* But don't try to insert it, if there's already another
2777 hw_access location that would be considered a duplicate
2778 of this one. */
2779 ALL_BP_LOCATIONS (loc, loc_temp)
2780 if (loc != bl
2781 && loc->watchpoint_type == hw_access
2782 && watchpoint_locations_match (bl, loc))
2783 {
2784 bl->duplicate = 1;
2785 bl->inserted = 1;
2786 bl->target_info = loc->target_info;
2787 bl->watchpoint_type = hw_access;
2788 val = 0;
2789 break;
2790 }
2791
2792 if (val == 1)
2793 {
2794 bl->watchpoint_type = hw_access;
2795 val = bl->owner->ops->insert_location (bl);
2796
2797 if (val)
2798 /* Back to the original value. */
2799 bl->watchpoint_type = hw_read;
2800 }
2801 }
2802
2803 bl->inserted = (val == 0);
2804 }
2805
2806 else if (bl->owner->type == bp_catchpoint)
2807 {
2808 int val;
2809
2810 gdb_assert (bl->owner->ops != NULL
2811 && bl->owner->ops->insert_location != NULL);
2812
2813 val = bl->owner->ops->insert_location (bl);
2814 if (val)
2815 {
2816 bl->owner->enable_state = bp_disabled;
2817
2818 if (val == 1)
2819 warning (_("\
2820 Error inserting catchpoint %d: Your system does not support this type\n\
2821 of catchpoint."), bl->owner->number);
2822 else
2823 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2824 }
2825
2826 bl->inserted = (val == 0);
2827
2828 /* We've already printed an error message if there was a problem
2829 inserting this catchpoint, and we've disabled the catchpoint,
2830 so just return success. */
2831 return 0;
2832 }
2833
2834 return 0;
2835 }
2836
2837 /* This function is called when program space PSPACE is about to be
2838 deleted. It takes care of updating breakpoints to not reference
2839 PSPACE anymore. */
2840
2841 void
2842 breakpoint_program_space_exit (struct program_space *pspace)
2843 {
2844 struct breakpoint *b, *b_temp;
2845 struct bp_location *loc, **loc_temp;
2846
2847 /* Remove any breakpoint that was set through this program space. */
2848 ALL_BREAKPOINTS_SAFE (b, b_temp)
2849 {
2850 if (b->pspace == pspace)
2851 delete_breakpoint (b);
2852 }
2853
2854 /* Breakpoints set through other program spaces could have locations
2855 bound to PSPACE as well. Remove those. */
2856 ALL_BP_LOCATIONS (loc, loc_temp)
2857 {
2858 struct bp_location *tmp;
2859
2860 if (loc->pspace == pspace)
2861 {
2862 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2863 if (loc->owner->loc == loc)
2864 loc->owner->loc = loc->next;
2865 else
2866 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2867 if (tmp->next == loc)
2868 {
2869 tmp->next = loc->next;
2870 break;
2871 }
2872 }
2873 }
2874
2875 /* Now update the global location list to permanently delete the
2876 removed locations above. */
2877 update_global_location_list (UGLL_DONT_INSERT);
2878 }
2879
2880 /* Make sure all breakpoints are inserted in inferior.
2881 Throws exception on any error.
2882 A breakpoint that is already inserted won't be inserted
2883 again, so calling this function twice is safe. */
2884 void
2885 insert_breakpoints (void)
2886 {
2887 struct breakpoint *bpt;
2888
2889 ALL_BREAKPOINTS (bpt)
2890 if (is_hardware_watchpoint (bpt))
2891 {
2892 struct watchpoint *w = (struct watchpoint *) bpt;
2893
2894 update_watchpoint (w, 0 /* don't reparse. */);
2895 }
2896
2897 /* Updating watchpoints creates new locations, so update the global
2898 location list. Explicitly tell ugll to insert locations and
2899 ignore breakpoints_always_inserted_mode. */
2900 update_global_location_list (UGLL_INSERT);
2901 }
2902
2903 /* Invoke CALLBACK for each of bp_location. */
2904
2905 void
2906 iterate_over_bp_locations (walk_bp_location_callback callback)
2907 {
2908 struct bp_location *loc, **loc_tmp;
2909
2910 ALL_BP_LOCATIONS (loc, loc_tmp)
2911 {
2912 callback (loc, NULL);
2913 }
2914 }
2915
2916 /* This is used when we need to synch breakpoint conditions between GDB and the
2917 target. It is the case with deleting and disabling of breakpoints when using
2918 always-inserted mode. */
2919
2920 static void
2921 update_inserted_breakpoint_locations (void)
2922 {
2923 struct bp_location *bl, **blp_tmp;
2924 int error_flag = 0;
2925 int val = 0;
2926 int disabled_breaks = 0;
2927 int hw_breakpoint_error = 0;
2928 int hw_bp_details_reported = 0;
2929
2930 string_file tmp_error_stream;
2931
2932 /* Explicitly mark the warning -- this will only be printed if
2933 there was an error. */
2934 tmp_error_stream.puts ("Warning:\n");
2935
2936 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2937
2938 ALL_BP_LOCATIONS (bl, blp_tmp)
2939 {
2940 /* We only want to update software breakpoints and hardware
2941 breakpoints. */
2942 if (!is_breakpoint (bl->owner))
2943 continue;
2944
2945 /* We only want to update locations that are already inserted
2946 and need updating. This is to avoid unwanted insertion during
2947 deletion of breakpoints. */
2948 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2949 continue;
2950
2951 switch_to_program_space_and_thread (bl->pspace);
2952
2953 /* For targets that support global breakpoints, there's no need
2954 to select an inferior to insert breakpoint to. In fact, even
2955 if we aren't attached to any process yet, we should still
2956 insert breakpoints. */
2957 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2958 && ptid_equal (inferior_ptid, null_ptid))
2959 continue;
2960
2961 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2962 &hw_breakpoint_error, &hw_bp_details_reported);
2963 if (val)
2964 error_flag = val;
2965 }
2966
2967 if (error_flag)
2968 {
2969 target_terminal::ours_for_output ();
2970 error_stream (tmp_error_stream);
2971 }
2972 }
2973
2974 /* Used when starting or continuing the program. */
2975
2976 static void
2977 insert_breakpoint_locations (void)
2978 {
2979 struct breakpoint *bpt;
2980 struct bp_location *bl, **blp_tmp;
2981 int error_flag = 0;
2982 int val = 0;
2983 int disabled_breaks = 0;
2984 int hw_breakpoint_error = 0;
2985 int hw_bp_error_explained_already = 0;
2986
2987 string_file tmp_error_stream;
2988
2989 /* Explicitly mark the warning -- this will only be printed if
2990 there was an error. */
2991 tmp_error_stream.puts ("Warning:\n");
2992
2993 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2994
2995 ALL_BP_LOCATIONS (bl, blp_tmp)
2996 {
2997 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2998 continue;
2999
3000 /* There is no point inserting thread-specific breakpoints if
3001 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3002 has BL->OWNER always non-NULL. */
3003 if (bl->owner->thread != -1
3004 && !valid_global_thread_id (bl->owner->thread))
3005 continue;
3006
3007 switch_to_program_space_and_thread (bl->pspace);
3008
3009 /* For targets that support global breakpoints, there's no need
3010 to select an inferior to insert breakpoint to. In fact, even
3011 if we aren't attached to any process yet, we should still
3012 insert breakpoints. */
3013 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3014 && ptid_equal (inferior_ptid, null_ptid))
3015 continue;
3016
3017 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3018 &hw_breakpoint_error, &hw_bp_error_explained_already);
3019 if (val)
3020 error_flag = val;
3021 }
3022
3023 /* If we failed to insert all locations of a watchpoint, remove
3024 them, as half-inserted watchpoint is of limited use. */
3025 ALL_BREAKPOINTS (bpt)
3026 {
3027 int some_failed = 0;
3028 struct bp_location *loc;
3029
3030 if (!is_hardware_watchpoint (bpt))
3031 continue;
3032
3033 if (!breakpoint_enabled (bpt))
3034 continue;
3035
3036 if (bpt->disposition == disp_del_at_next_stop)
3037 continue;
3038
3039 for (loc = bpt->loc; loc; loc = loc->next)
3040 if (!loc->inserted && should_be_inserted (loc))
3041 {
3042 some_failed = 1;
3043 break;
3044 }
3045 if (some_failed)
3046 {
3047 for (loc = bpt->loc; loc; loc = loc->next)
3048 if (loc->inserted)
3049 remove_breakpoint (loc);
3050
3051 hw_breakpoint_error = 1;
3052 tmp_error_stream.printf ("Could not insert "
3053 "hardware watchpoint %d.\n",
3054 bpt->number);
3055 error_flag = -1;
3056 }
3057 }
3058
3059 if (error_flag)
3060 {
3061 /* If a hardware breakpoint or watchpoint was inserted, add a
3062 message about possibly exhausted resources. */
3063 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3064 {
3065 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3066 You may have requested too many hardware breakpoints/watchpoints.\n");
3067 }
3068 target_terminal::ours_for_output ();
3069 error_stream (tmp_error_stream);
3070 }
3071 }
3072
3073 /* Used when the program stops.
3074 Returns zero if successful, or non-zero if there was a problem
3075 removing a breakpoint location. */
3076
3077 int
3078 remove_breakpoints (void)
3079 {
3080 struct bp_location *bl, **blp_tmp;
3081 int val = 0;
3082
3083 ALL_BP_LOCATIONS (bl, blp_tmp)
3084 {
3085 if (bl->inserted && !is_tracepoint (bl->owner))
3086 val |= remove_breakpoint (bl);
3087 }
3088 return val;
3089 }
3090
3091 /* When a thread exits, remove breakpoints that are related to
3092 that thread. */
3093
3094 static void
3095 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3096 {
3097 struct breakpoint *b, *b_tmp;
3098
3099 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3100 {
3101 if (b->thread == tp->global_num && user_breakpoint_p (b))
3102 {
3103 b->disposition = disp_del_at_next_stop;
3104
3105 printf_filtered (_("\
3106 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3107 b->number, print_thread_id (tp));
3108
3109 /* Hide it from the user. */
3110 b->number = 0;
3111 }
3112 }
3113 }
3114
3115 /* Remove breakpoints of process PID. */
3116
3117 int
3118 remove_breakpoints_pid (int pid)
3119 {
3120 struct bp_location *bl, **blp_tmp;
3121 int val;
3122 struct inferior *inf = find_inferior_pid (pid);
3123
3124 ALL_BP_LOCATIONS (bl, blp_tmp)
3125 {
3126 if (bl->pspace != inf->pspace)
3127 continue;
3128
3129 if (bl->inserted && !bl->target_info.persist)
3130 {
3131 val = remove_breakpoint (bl);
3132 if (val != 0)
3133 return val;
3134 }
3135 }
3136 return 0;
3137 }
3138
3139 static int internal_breakpoint_number = -1;
3140
3141 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3142 If INTERNAL is non-zero, the breakpoint number will be populated
3143 from internal_breakpoint_number and that variable decremented.
3144 Otherwise the breakpoint number will be populated from
3145 breakpoint_count and that value incremented. Internal breakpoints
3146 do not set the internal var bpnum. */
3147 static void
3148 set_breakpoint_number (int internal, struct breakpoint *b)
3149 {
3150 if (internal)
3151 b->number = internal_breakpoint_number--;
3152 else
3153 {
3154 set_breakpoint_count (breakpoint_count + 1);
3155 b->number = breakpoint_count;
3156 }
3157 }
3158
3159 static struct breakpoint *
3160 create_internal_breakpoint (struct gdbarch *gdbarch,
3161 CORE_ADDR address, enum bptype type,
3162 const struct breakpoint_ops *ops)
3163 {
3164 symtab_and_line sal;
3165 sal.pc = address;
3166 sal.section = find_pc_overlay (sal.pc);
3167 sal.pspace = current_program_space;
3168
3169 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3170 b->number = internal_breakpoint_number--;
3171 b->disposition = disp_donttouch;
3172
3173 return b;
3174 }
3175
3176 static const char *const longjmp_names[] =
3177 {
3178 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3179 };
3180 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3181
3182 /* Per-objfile data private to breakpoint.c. */
3183 struct breakpoint_objfile_data
3184 {
3185 /* Minimal symbol for "_ovly_debug_event" (if any). */
3186 struct bound_minimal_symbol overlay_msym;
3187
3188 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3189 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3190
3191 /* True if we have looked for longjmp probes. */
3192 int longjmp_searched;
3193
3194 /* SystemTap probe points for longjmp (if any). */
3195 VEC (probe_p) *longjmp_probes;
3196
3197 /* Minimal symbol for "std::terminate()" (if any). */
3198 struct bound_minimal_symbol terminate_msym;
3199
3200 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3201 struct bound_minimal_symbol exception_msym;
3202
3203 /* True if we have looked for exception probes. */
3204 int exception_searched;
3205
3206 /* SystemTap probe points for unwinding (if any). */
3207 VEC (probe_p) *exception_probes;
3208 };
3209
3210 static const struct objfile_data *breakpoint_objfile_key;
3211
3212 /* Minimal symbol not found sentinel. */
3213 static struct minimal_symbol msym_not_found;
3214
3215 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3216
3217 static int
3218 msym_not_found_p (const struct minimal_symbol *msym)
3219 {
3220 return msym == &msym_not_found;
3221 }
3222
3223 /* Return per-objfile data needed by breakpoint.c.
3224 Allocate the data if necessary. */
3225
3226 static struct breakpoint_objfile_data *
3227 get_breakpoint_objfile_data (struct objfile *objfile)
3228 {
3229 struct breakpoint_objfile_data *bp_objfile_data;
3230
3231 bp_objfile_data = ((struct breakpoint_objfile_data *)
3232 objfile_data (objfile, breakpoint_objfile_key));
3233 if (bp_objfile_data == NULL)
3234 {
3235 bp_objfile_data =
3236 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3237
3238 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3239 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3240 }
3241 return bp_objfile_data;
3242 }
3243
3244 static void
3245 free_breakpoint_probes (struct objfile *obj, void *data)
3246 {
3247 struct breakpoint_objfile_data *bp_objfile_data
3248 = (struct breakpoint_objfile_data *) data;
3249
3250 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3251 VEC_free (probe_p, bp_objfile_data->exception_probes);
3252 }
3253
3254 static void
3255 create_overlay_event_breakpoint (void)
3256 {
3257 struct objfile *objfile;
3258 const char *const func_name = "_ovly_debug_event";
3259
3260 ALL_OBJFILES (objfile)
3261 {
3262 struct breakpoint *b;
3263 struct breakpoint_objfile_data *bp_objfile_data;
3264 CORE_ADDR addr;
3265 struct explicit_location explicit_loc;
3266
3267 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3268
3269 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3270 continue;
3271
3272 if (bp_objfile_data->overlay_msym.minsym == NULL)
3273 {
3274 struct bound_minimal_symbol m;
3275
3276 m = lookup_minimal_symbol_text (func_name, objfile);
3277 if (m.minsym == NULL)
3278 {
3279 /* Avoid future lookups in this objfile. */
3280 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3281 continue;
3282 }
3283 bp_objfile_data->overlay_msym = m;
3284 }
3285
3286 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3287 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3288 bp_overlay_event,
3289 &internal_breakpoint_ops);
3290 initialize_explicit_location (&explicit_loc);
3291 explicit_loc.function_name = ASTRDUP (func_name);
3292 b->location = new_explicit_location (&explicit_loc);
3293
3294 if (overlay_debugging == ovly_auto)
3295 {
3296 b->enable_state = bp_enabled;
3297 overlay_events_enabled = 1;
3298 }
3299 else
3300 {
3301 b->enable_state = bp_disabled;
3302 overlay_events_enabled = 0;
3303 }
3304 }
3305 }
3306
3307 static void
3308 create_longjmp_master_breakpoint (void)
3309 {
3310 struct program_space *pspace;
3311
3312 scoped_restore_current_program_space restore_pspace;
3313
3314 ALL_PSPACES (pspace)
3315 {
3316 struct objfile *objfile;
3317
3318 set_current_program_space (pspace);
3319
3320 ALL_OBJFILES (objfile)
3321 {
3322 int i;
3323 struct gdbarch *gdbarch;
3324 struct breakpoint_objfile_data *bp_objfile_data;
3325
3326 gdbarch = get_objfile_arch (objfile);
3327
3328 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3329
3330 if (!bp_objfile_data->longjmp_searched)
3331 {
3332 VEC (probe_p) *ret;
3333
3334 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3335 if (ret != NULL)
3336 {
3337 /* We are only interested in checking one element. */
3338 struct probe *p = VEC_index (probe_p, ret, 0);
3339
3340 if (!can_evaluate_probe_arguments (p))
3341 {
3342 /* We cannot use the probe interface here, because it does
3343 not know how to evaluate arguments. */
3344 VEC_free (probe_p, ret);
3345 ret = NULL;
3346 }
3347 }
3348 bp_objfile_data->longjmp_probes = ret;
3349 bp_objfile_data->longjmp_searched = 1;
3350 }
3351
3352 if (bp_objfile_data->longjmp_probes != NULL)
3353 {
3354 int i;
3355 struct probe *probe;
3356 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3357
3358 for (i = 0;
3359 VEC_iterate (probe_p,
3360 bp_objfile_data->longjmp_probes,
3361 i, probe);
3362 ++i)
3363 {
3364 struct breakpoint *b;
3365
3366 b = create_internal_breakpoint (gdbarch,
3367 get_probe_address (probe,
3368 objfile),
3369 bp_longjmp_master,
3370 &internal_breakpoint_ops);
3371 b->location = new_probe_location ("-probe-stap libc:longjmp");
3372 b->enable_state = bp_disabled;
3373 }
3374
3375 continue;
3376 }
3377
3378 if (!gdbarch_get_longjmp_target_p (gdbarch))
3379 continue;
3380
3381 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3382 {
3383 struct breakpoint *b;
3384 const char *func_name;
3385 CORE_ADDR addr;
3386 struct explicit_location explicit_loc;
3387
3388 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3389 continue;
3390
3391 func_name = longjmp_names[i];
3392 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3393 {
3394 struct bound_minimal_symbol m;
3395
3396 m = lookup_minimal_symbol_text (func_name, objfile);
3397 if (m.minsym == NULL)
3398 {
3399 /* Prevent future lookups in this objfile. */
3400 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3401 continue;
3402 }
3403 bp_objfile_data->longjmp_msym[i] = m;
3404 }
3405
3406 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3407 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3408 &internal_breakpoint_ops);
3409 initialize_explicit_location (&explicit_loc);
3410 explicit_loc.function_name = ASTRDUP (func_name);
3411 b->location = new_explicit_location (&explicit_loc);
3412 b->enable_state = bp_disabled;
3413 }
3414 }
3415 }
3416 }
3417
3418 /* Create a master std::terminate breakpoint. */
3419 static void
3420 create_std_terminate_master_breakpoint (void)
3421 {
3422 struct program_space *pspace;
3423 const char *const func_name = "std::terminate()";
3424
3425 scoped_restore_current_program_space restore_pspace;
3426
3427 ALL_PSPACES (pspace)
3428 {
3429 struct objfile *objfile;
3430 CORE_ADDR addr;
3431
3432 set_current_program_space (pspace);
3433
3434 ALL_OBJFILES (objfile)
3435 {
3436 struct breakpoint *b;
3437 struct breakpoint_objfile_data *bp_objfile_data;
3438 struct explicit_location explicit_loc;
3439
3440 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3441
3442 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3443 continue;
3444
3445 if (bp_objfile_data->terminate_msym.minsym == NULL)
3446 {
3447 struct bound_minimal_symbol m;
3448
3449 m = lookup_minimal_symbol (func_name, NULL, objfile);
3450 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3451 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3452 {
3453 /* Prevent future lookups in this objfile. */
3454 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3455 continue;
3456 }
3457 bp_objfile_data->terminate_msym = m;
3458 }
3459
3460 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3461 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3462 bp_std_terminate_master,
3463 &internal_breakpoint_ops);
3464 initialize_explicit_location (&explicit_loc);
3465 explicit_loc.function_name = ASTRDUP (func_name);
3466 b->location = new_explicit_location (&explicit_loc);
3467 b->enable_state = bp_disabled;
3468 }
3469 }
3470 }
3471
3472 /* Install a master breakpoint on the unwinder's debug hook. */
3473
3474 static void
3475 create_exception_master_breakpoint (void)
3476 {
3477 struct objfile *objfile;
3478 const char *const func_name = "_Unwind_DebugHook";
3479
3480 ALL_OBJFILES (objfile)
3481 {
3482 struct breakpoint *b;
3483 struct gdbarch *gdbarch;
3484 struct breakpoint_objfile_data *bp_objfile_data;
3485 CORE_ADDR addr;
3486 struct explicit_location explicit_loc;
3487
3488 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3489
3490 /* We prefer the SystemTap probe point if it exists. */
3491 if (!bp_objfile_data->exception_searched)
3492 {
3493 VEC (probe_p) *ret;
3494
3495 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3496
3497 if (ret != NULL)
3498 {
3499 /* We are only interested in checking one element. */
3500 struct probe *p = VEC_index (probe_p, ret, 0);
3501
3502 if (!can_evaluate_probe_arguments (p))
3503 {
3504 /* We cannot use the probe interface here, because it does
3505 not know how to evaluate arguments. */
3506 VEC_free (probe_p, ret);
3507 ret = NULL;
3508 }
3509 }
3510 bp_objfile_data->exception_probes = ret;
3511 bp_objfile_data->exception_searched = 1;
3512 }
3513
3514 if (bp_objfile_data->exception_probes != NULL)
3515 {
3516 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3517 int i;
3518 struct probe *probe;
3519
3520 for (i = 0;
3521 VEC_iterate (probe_p,
3522 bp_objfile_data->exception_probes,
3523 i, probe);
3524 ++i)
3525 {
3526 struct breakpoint *b;
3527
3528 b = create_internal_breakpoint (gdbarch,
3529 get_probe_address (probe,
3530 objfile),
3531 bp_exception_master,
3532 &internal_breakpoint_ops);
3533 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3534 b->enable_state = bp_disabled;
3535 }
3536
3537 continue;
3538 }
3539
3540 /* Otherwise, try the hook function. */
3541
3542 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3543 continue;
3544
3545 gdbarch = get_objfile_arch (objfile);
3546
3547 if (bp_objfile_data->exception_msym.minsym == NULL)
3548 {
3549 struct bound_minimal_symbol debug_hook;
3550
3551 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3552 if (debug_hook.minsym == NULL)
3553 {
3554 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3555 continue;
3556 }
3557
3558 bp_objfile_data->exception_msym = debug_hook;
3559 }
3560
3561 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3562 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3563 &current_target);
3564 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3565 &internal_breakpoint_ops);
3566 initialize_explicit_location (&explicit_loc);
3567 explicit_loc.function_name = ASTRDUP (func_name);
3568 b->location = new_explicit_location (&explicit_loc);
3569 b->enable_state = bp_disabled;
3570 }
3571 }
3572
3573 /* Does B have a location spec? */
3574
3575 static int
3576 breakpoint_event_location_empty_p (const struct breakpoint *b)
3577 {
3578 return b->location != NULL && event_location_empty_p (b->location.get ());
3579 }
3580
3581 void
3582 update_breakpoints_after_exec (void)
3583 {
3584 struct breakpoint *b, *b_tmp;
3585 struct bp_location *bploc, **bplocp_tmp;
3586
3587 /* We're about to delete breakpoints from GDB's lists. If the
3588 INSERTED flag is true, GDB will try to lift the breakpoints by
3589 writing the breakpoints' "shadow contents" back into memory. The
3590 "shadow contents" are NOT valid after an exec, so GDB should not
3591 do that. Instead, the target is responsible from marking
3592 breakpoints out as soon as it detects an exec. We don't do that
3593 here instead, because there may be other attempts to delete
3594 breakpoints after detecting an exec and before reaching here. */
3595 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3596 if (bploc->pspace == current_program_space)
3597 gdb_assert (!bploc->inserted);
3598
3599 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3600 {
3601 if (b->pspace != current_program_space)
3602 continue;
3603
3604 /* Solib breakpoints must be explicitly reset after an exec(). */
3605 if (b->type == bp_shlib_event)
3606 {
3607 delete_breakpoint (b);
3608 continue;
3609 }
3610
3611 /* JIT breakpoints must be explicitly reset after an exec(). */
3612 if (b->type == bp_jit_event)
3613 {
3614 delete_breakpoint (b);
3615 continue;
3616 }
3617
3618 /* Thread event breakpoints must be set anew after an exec(),
3619 as must overlay event and longjmp master breakpoints. */
3620 if (b->type == bp_thread_event || b->type == bp_overlay_event
3621 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3622 || b->type == bp_exception_master)
3623 {
3624 delete_breakpoint (b);
3625 continue;
3626 }
3627
3628 /* Step-resume breakpoints are meaningless after an exec(). */
3629 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3630 {
3631 delete_breakpoint (b);
3632 continue;
3633 }
3634
3635 /* Just like single-step breakpoints. */
3636 if (b->type == bp_single_step)
3637 {
3638 delete_breakpoint (b);
3639 continue;
3640 }
3641
3642 /* Longjmp and longjmp-resume breakpoints are also meaningless
3643 after an exec. */
3644 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3645 || b->type == bp_longjmp_call_dummy
3646 || b->type == bp_exception || b->type == bp_exception_resume)
3647 {
3648 delete_breakpoint (b);
3649 continue;
3650 }
3651
3652 if (b->type == bp_catchpoint)
3653 {
3654 /* For now, none of the bp_catchpoint breakpoints need to
3655 do anything at this point. In the future, if some of
3656 the catchpoints need to something, we will need to add
3657 a new method, and call this method from here. */
3658 continue;
3659 }
3660
3661 /* bp_finish is a special case. The only way we ought to be able
3662 to see one of these when an exec() has happened, is if the user
3663 caught a vfork, and then said "finish". Ordinarily a finish just
3664 carries them to the call-site of the current callee, by setting
3665 a temporary bp there and resuming. But in this case, the finish
3666 will carry them entirely through the vfork & exec.
3667
3668 We don't want to allow a bp_finish to remain inserted now. But
3669 we can't safely delete it, 'cause finish_command has a handle to
3670 the bp on a bpstat, and will later want to delete it. There's a
3671 chance (and I've seen it happen) that if we delete the bp_finish
3672 here, that its storage will get reused by the time finish_command
3673 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3674 We really must allow finish_command to delete a bp_finish.
3675
3676 In the absence of a general solution for the "how do we know
3677 it's safe to delete something others may have handles to?"
3678 problem, what we'll do here is just uninsert the bp_finish, and
3679 let finish_command delete it.
3680
3681 (We know the bp_finish is "doomed" in the sense that it's
3682 momentary, and will be deleted as soon as finish_command sees
3683 the inferior stopped. So it doesn't matter that the bp's
3684 address is probably bogus in the new a.out, unlike e.g., the
3685 solib breakpoints.) */
3686
3687 if (b->type == bp_finish)
3688 {
3689 continue;
3690 }
3691
3692 /* Without a symbolic address, we have little hope of the
3693 pre-exec() address meaning the same thing in the post-exec()
3694 a.out. */
3695 if (breakpoint_event_location_empty_p (b))
3696 {
3697 delete_breakpoint (b);
3698 continue;
3699 }
3700 }
3701 }
3702
3703 int
3704 detach_breakpoints (ptid_t ptid)
3705 {
3706 struct bp_location *bl, **blp_tmp;
3707 int val = 0;
3708 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3709 struct inferior *inf = current_inferior ();
3710
3711 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3712 error (_("Cannot detach breakpoints of inferior_ptid"));
3713
3714 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3715 inferior_ptid = ptid;
3716 ALL_BP_LOCATIONS (bl, blp_tmp)
3717 {
3718 if (bl->pspace != inf->pspace)
3719 continue;
3720
3721 /* This function must physically remove breakpoints locations
3722 from the specified ptid, without modifying the breakpoint
3723 package's state. Locations of type bp_loc_other are only
3724 maintained at GDB side. So, there is no need to remove
3725 these bp_loc_other locations. Moreover, removing these
3726 would modify the breakpoint package's state. */
3727 if (bl->loc_type == bp_loc_other)
3728 continue;
3729
3730 if (bl->inserted)
3731 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3732 }
3733
3734 return val;
3735 }
3736
3737 /* Remove the breakpoint location BL from the current address space.
3738 Note that this is used to detach breakpoints from a child fork.
3739 When we get here, the child isn't in the inferior list, and neither
3740 do we have objects to represent its address space --- we should
3741 *not* look at bl->pspace->aspace here. */
3742
3743 static int
3744 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3745 {
3746 int val;
3747
3748 /* BL is never in moribund_locations by our callers. */
3749 gdb_assert (bl->owner != NULL);
3750
3751 /* The type of none suggests that owner is actually deleted.
3752 This should not ever happen. */
3753 gdb_assert (bl->owner->type != bp_none);
3754
3755 if (bl->loc_type == bp_loc_software_breakpoint
3756 || bl->loc_type == bp_loc_hardware_breakpoint)
3757 {
3758 /* "Normal" instruction breakpoint: either the standard
3759 trap-instruction bp (bp_breakpoint), or a
3760 bp_hardware_breakpoint. */
3761
3762 /* First check to see if we have to handle an overlay. */
3763 if (overlay_debugging == ovly_off
3764 || bl->section == NULL
3765 || !(section_is_overlay (bl->section)))
3766 {
3767 /* No overlay handling: just remove the breakpoint. */
3768
3769 /* If we're trying to uninsert a memory breakpoint that we
3770 know is set in a dynamic object that is marked
3771 shlib_disabled, then either the dynamic object was
3772 removed with "remove-symbol-file" or with
3773 "nosharedlibrary". In the former case, we don't know
3774 whether another dynamic object might have loaded over the
3775 breakpoint's address -- the user might well let us know
3776 about it next with add-symbol-file (the whole point of
3777 add-symbol-file is letting the user manually maintain a
3778 list of dynamically loaded objects). If we have the
3779 breakpoint's shadow memory, that is, this is a software
3780 breakpoint managed by GDB, check whether the breakpoint
3781 is still inserted in memory, to avoid overwriting wrong
3782 code with stale saved shadow contents. Note that HW
3783 breakpoints don't have shadow memory, as they're
3784 implemented using a mechanism that is not dependent on
3785 being able to modify the target's memory, and as such
3786 they should always be removed. */
3787 if (bl->shlib_disabled
3788 && bl->target_info.shadow_len != 0
3789 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3790 val = 0;
3791 else
3792 val = bl->owner->ops->remove_location (bl, reason);
3793 }
3794 else
3795 {
3796 /* This breakpoint is in an overlay section.
3797 Did we set a breakpoint at the LMA? */
3798 if (!overlay_events_enabled)
3799 {
3800 /* Yes -- overlay event support is not active, so we
3801 should have set a breakpoint at the LMA. Remove it.
3802 */
3803 /* Ignore any failures: if the LMA is in ROM, we will
3804 have already warned when we failed to insert it. */
3805 if (bl->loc_type == bp_loc_hardware_breakpoint)
3806 target_remove_hw_breakpoint (bl->gdbarch,
3807 &bl->overlay_target_info);
3808 else
3809 target_remove_breakpoint (bl->gdbarch,
3810 &bl->overlay_target_info,
3811 reason);
3812 }
3813 /* Did we set a breakpoint at the VMA?
3814 If so, we will have marked the breakpoint 'inserted'. */
3815 if (bl->inserted)
3816 {
3817 /* Yes -- remove it. Previously we did not bother to
3818 remove the breakpoint if the section had been
3819 unmapped, but let's not rely on that being safe. We
3820 don't know what the overlay manager might do. */
3821
3822 /* However, we should remove *software* breakpoints only
3823 if the section is still mapped, or else we overwrite
3824 wrong code with the saved shadow contents. */
3825 if (bl->loc_type == bp_loc_hardware_breakpoint
3826 || section_is_mapped (bl->section))
3827 val = bl->owner->ops->remove_location (bl, reason);
3828 else
3829 val = 0;
3830 }
3831 else
3832 {
3833 /* No -- not inserted, so no need to remove. No error. */
3834 val = 0;
3835 }
3836 }
3837
3838 /* In some cases, we might not be able to remove a breakpoint in
3839 a shared library that has already been removed, but we have
3840 not yet processed the shlib unload event. Similarly for an
3841 unloaded add-symbol-file object - the user might not yet have
3842 had the chance to remove-symbol-file it. shlib_disabled will
3843 be set if the library/object has already been removed, but
3844 the breakpoint hasn't been uninserted yet, e.g., after
3845 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3846 always-inserted mode. */
3847 if (val
3848 && (bl->loc_type == bp_loc_software_breakpoint
3849 && (bl->shlib_disabled
3850 || solib_name_from_address (bl->pspace, bl->address)
3851 || shared_objfile_contains_address_p (bl->pspace,
3852 bl->address))))
3853 val = 0;
3854
3855 if (val)
3856 return val;
3857 bl->inserted = (reason == DETACH_BREAKPOINT);
3858 }
3859 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3860 {
3861 gdb_assert (bl->owner->ops != NULL
3862 && bl->owner->ops->remove_location != NULL);
3863
3864 bl->inserted = (reason == DETACH_BREAKPOINT);
3865 bl->owner->ops->remove_location (bl, reason);
3866
3867 /* Failure to remove any of the hardware watchpoints comes here. */
3868 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3869 warning (_("Could not remove hardware watchpoint %d."),
3870 bl->owner->number);
3871 }
3872 else if (bl->owner->type == bp_catchpoint
3873 && breakpoint_enabled (bl->owner)
3874 && !bl->duplicate)
3875 {
3876 gdb_assert (bl->owner->ops != NULL
3877 && bl->owner->ops->remove_location != NULL);
3878
3879 val = bl->owner->ops->remove_location (bl, reason);
3880 if (val)
3881 return val;
3882
3883 bl->inserted = (reason == DETACH_BREAKPOINT);
3884 }
3885
3886 return 0;
3887 }
3888
3889 static int
3890 remove_breakpoint (struct bp_location *bl)
3891 {
3892 /* BL is never in moribund_locations by our callers. */
3893 gdb_assert (bl->owner != NULL);
3894
3895 /* The type of none suggests that owner is actually deleted.
3896 This should not ever happen. */
3897 gdb_assert (bl->owner->type != bp_none);
3898
3899 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3900
3901 switch_to_program_space_and_thread (bl->pspace);
3902
3903 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3904 }
3905
3906 /* Clear the "inserted" flag in all breakpoints. */
3907
3908 void
3909 mark_breakpoints_out (void)
3910 {
3911 struct bp_location *bl, **blp_tmp;
3912
3913 ALL_BP_LOCATIONS (bl, blp_tmp)
3914 if (bl->pspace == current_program_space)
3915 bl->inserted = 0;
3916 }
3917
3918 /* Clear the "inserted" flag in all breakpoints and delete any
3919 breakpoints which should go away between runs of the program.
3920
3921 Plus other such housekeeping that has to be done for breakpoints
3922 between runs.
3923
3924 Note: this function gets called at the end of a run (by
3925 generic_mourn_inferior) and when a run begins (by
3926 init_wait_for_inferior). */
3927
3928
3929
3930 void
3931 breakpoint_init_inferior (enum inf_context context)
3932 {
3933 struct breakpoint *b, *b_tmp;
3934 struct bp_location *bl;
3935 int ix;
3936 struct program_space *pspace = current_program_space;
3937
3938 /* If breakpoint locations are shared across processes, then there's
3939 nothing to do. */
3940 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3941 return;
3942
3943 mark_breakpoints_out ();
3944
3945 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3946 {
3947 if (b->loc && b->loc->pspace != pspace)
3948 continue;
3949
3950 switch (b->type)
3951 {
3952 case bp_call_dummy:
3953 case bp_longjmp_call_dummy:
3954
3955 /* If the call dummy breakpoint is at the entry point it will
3956 cause problems when the inferior is rerun, so we better get
3957 rid of it. */
3958
3959 case bp_watchpoint_scope:
3960
3961 /* Also get rid of scope breakpoints. */
3962
3963 case bp_shlib_event:
3964
3965 /* Also remove solib event breakpoints. Their addresses may
3966 have changed since the last time we ran the program.
3967 Actually we may now be debugging against different target;
3968 and so the solib backend that installed this breakpoint may
3969 not be used in by the target. E.g.,
3970
3971 (gdb) file prog-linux
3972 (gdb) run # native linux target
3973 ...
3974 (gdb) kill
3975 (gdb) file prog-win.exe
3976 (gdb) tar rem :9999 # remote Windows gdbserver.
3977 */
3978
3979 case bp_step_resume:
3980
3981 /* Also remove step-resume breakpoints. */
3982
3983 case bp_single_step:
3984
3985 /* Also remove single-step breakpoints. */
3986
3987 delete_breakpoint (b);
3988 break;
3989
3990 case bp_watchpoint:
3991 case bp_hardware_watchpoint:
3992 case bp_read_watchpoint:
3993 case bp_access_watchpoint:
3994 {
3995 struct watchpoint *w = (struct watchpoint *) b;
3996
3997 /* Likewise for watchpoints on local expressions. */
3998 if (w->exp_valid_block != NULL)
3999 delete_breakpoint (b);
4000 else
4001 {
4002 /* Get rid of existing locations, which are no longer
4003 valid. New ones will be created in
4004 update_watchpoint, when the inferior is restarted.
4005 The next update_global_location_list call will
4006 garbage collect them. */
4007 b->loc = NULL;
4008
4009 if (context == inf_starting)
4010 {
4011 /* Reset val field to force reread of starting value in
4012 insert_breakpoints. */
4013 if (w->val)
4014 value_free (w->val);
4015 w->val = NULL;
4016 w->val_valid = 0;
4017 }
4018 }
4019 }
4020 break;
4021 default:
4022 break;
4023 }
4024 }
4025
4026 /* Get rid of the moribund locations. */
4027 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4028 decref_bp_location (&bl);
4029 VEC_free (bp_location_p, moribund_locations);
4030 }
4031
4032 /* These functions concern about actual breakpoints inserted in the
4033 target --- to e.g. check if we need to do decr_pc adjustment or if
4034 we need to hop over the bkpt --- so we check for address space
4035 match, not program space. */
4036
4037 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4038 exists at PC. It returns ordinary_breakpoint_here if it's an
4039 ordinary breakpoint, or permanent_breakpoint_here if it's a
4040 permanent breakpoint.
4041 - When continuing from a location with an ordinary breakpoint, we
4042 actually single step once before calling insert_breakpoints.
4043 - When continuing from a location with a permanent breakpoint, we
4044 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4045 the target, to advance the PC past the breakpoint. */
4046
4047 enum breakpoint_here
4048 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4049 {
4050 struct bp_location *bl, **blp_tmp;
4051 int any_breakpoint_here = 0;
4052
4053 ALL_BP_LOCATIONS (bl, blp_tmp)
4054 {
4055 if (bl->loc_type != bp_loc_software_breakpoint
4056 && bl->loc_type != bp_loc_hardware_breakpoint)
4057 continue;
4058
4059 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4060 if ((breakpoint_enabled (bl->owner)
4061 || bl->permanent)
4062 && breakpoint_location_address_match (bl, aspace, pc))
4063 {
4064 if (overlay_debugging
4065 && section_is_overlay (bl->section)
4066 && !section_is_mapped (bl->section))
4067 continue; /* unmapped overlay -- can't be a match */
4068 else if (bl->permanent)
4069 return permanent_breakpoint_here;
4070 else
4071 any_breakpoint_here = 1;
4072 }
4073 }
4074
4075 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4076 }
4077
4078 /* See breakpoint.h. */
4079
4080 int
4081 breakpoint_in_range_p (const address_space *aspace,
4082 CORE_ADDR addr, ULONGEST len)
4083 {
4084 struct bp_location *bl, **blp_tmp;
4085
4086 ALL_BP_LOCATIONS (bl, blp_tmp)
4087 {
4088 if (bl->loc_type != bp_loc_software_breakpoint
4089 && bl->loc_type != bp_loc_hardware_breakpoint)
4090 continue;
4091
4092 if ((breakpoint_enabled (bl->owner)
4093 || bl->permanent)
4094 && breakpoint_location_address_range_overlap (bl, aspace,
4095 addr, len))
4096 {
4097 if (overlay_debugging
4098 && section_is_overlay (bl->section)
4099 && !section_is_mapped (bl->section))
4100 {
4101 /* Unmapped overlay -- can't be a match. */
4102 continue;
4103 }
4104
4105 return 1;
4106 }
4107 }
4108
4109 return 0;
4110 }
4111
4112 /* Return true if there's a moribund breakpoint at PC. */
4113
4114 int
4115 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4116 {
4117 struct bp_location *loc;
4118 int ix;
4119
4120 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4121 if (breakpoint_location_address_match (loc, aspace, pc))
4122 return 1;
4123
4124 return 0;
4125 }
4126
4127 /* Returns non-zero iff BL is inserted at PC, in address space
4128 ASPACE. */
4129
4130 static int
4131 bp_location_inserted_here_p (struct bp_location *bl,
4132 const address_space *aspace, CORE_ADDR pc)
4133 {
4134 if (bl->inserted
4135 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4136 aspace, pc))
4137 {
4138 if (overlay_debugging
4139 && section_is_overlay (bl->section)
4140 && !section_is_mapped (bl->section))
4141 return 0; /* unmapped overlay -- can't be a match */
4142 else
4143 return 1;
4144 }
4145 return 0;
4146 }
4147
4148 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4149
4150 int
4151 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4152 {
4153 struct bp_location **blp, **blp_tmp = NULL;
4154
4155 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4156 {
4157 struct bp_location *bl = *blp;
4158
4159 if (bl->loc_type != bp_loc_software_breakpoint
4160 && bl->loc_type != bp_loc_hardware_breakpoint)
4161 continue;
4162
4163 if (bp_location_inserted_here_p (bl, aspace, pc))
4164 return 1;
4165 }
4166 return 0;
4167 }
4168
4169 /* This function returns non-zero iff there is a software breakpoint
4170 inserted at PC. */
4171
4172 int
4173 software_breakpoint_inserted_here_p (const address_space *aspace,
4174 CORE_ADDR pc)
4175 {
4176 struct bp_location **blp, **blp_tmp = NULL;
4177
4178 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4179 {
4180 struct bp_location *bl = *blp;
4181
4182 if (bl->loc_type != bp_loc_software_breakpoint)
4183 continue;
4184
4185 if (bp_location_inserted_here_p (bl, aspace, pc))
4186 return 1;
4187 }
4188
4189 return 0;
4190 }
4191
4192 /* See breakpoint.h. */
4193
4194 int
4195 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4196 CORE_ADDR pc)
4197 {
4198 struct bp_location **blp, **blp_tmp = NULL;
4199
4200 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4201 {
4202 struct bp_location *bl = *blp;
4203
4204 if (bl->loc_type != bp_loc_hardware_breakpoint)
4205 continue;
4206
4207 if (bp_location_inserted_here_p (bl, aspace, pc))
4208 return 1;
4209 }
4210
4211 return 0;
4212 }
4213
4214 int
4215 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4216 CORE_ADDR addr, ULONGEST len)
4217 {
4218 struct breakpoint *bpt;
4219
4220 ALL_BREAKPOINTS (bpt)
4221 {
4222 struct bp_location *loc;
4223
4224 if (bpt->type != bp_hardware_watchpoint
4225 && bpt->type != bp_access_watchpoint)
4226 continue;
4227
4228 if (!breakpoint_enabled (bpt))
4229 continue;
4230
4231 for (loc = bpt->loc; loc; loc = loc->next)
4232 if (loc->pspace->aspace == aspace && loc->inserted)
4233 {
4234 CORE_ADDR l, h;
4235
4236 /* Check for intersection. */
4237 l = std::max<CORE_ADDR> (loc->address, addr);
4238 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4239 if (l < h)
4240 return 1;
4241 }
4242 }
4243 return 0;
4244 }
4245 \f
4246
4247 /* bpstat stuff. External routines' interfaces are documented
4248 in breakpoint.h. */
4249
4250 int
4251 is_catchpoint (struct breakpoint *ep)
4252 {
4253 return (ep->type == bp_catchpoint);
4254 }
4255
4256 /* Frees any storage that is part of a bpstat. Does not walk the
4257 'next' chain. */
4258
4259 bpstats::~bpstats ()
4260 {
4261 if (old_val != NULL)
4262 value_free (old_val);
4263 if (bp_location_at != NULL)
4264 decref_bp_location (&bp_location_at);
4265 }
4266
4267 /* Clear a bpstat so that it says we are not at any breakpoint.
4268 Also free any storage that is part of a bpstat. */
4269
4270 void
4271 bpstat_clear (bpstat *bsp)
4272 {
4273 bpstat p;
4274 bpstat q;
4275
4276 if (bsp == 0)
4277 return;
4278 p = *bsp;
4279 while (p != NULL)
4280 {
4281 q = p->next;
4282 delete p;
4283 p = q;
4284 }
4285 *bsp = NULL;
4286 }
4287
4288 bpstats::bpstats (const bpstats &other)
4289 : next (NULL),
4290 bp_location_at (other.bp_location_at),
4291 breakpoint_at (other.breakpoint_at),
4292 commands (other.commands),
4293 old_val (other.old_val),
4294 print (other.print),
4295 stop (other.stop),
4296 print_it (other.print_it)
4297 {
4298 if (old_val != NULL)
4299 {
4300 old_val = value_copy (old_val);
4301 release_value (old_val);
4302 }
4303 incref_bp_location (bp_location_at);
4304 }
4305
4306 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4307 is part of the bpstat is copied as well. */
4308
4309 bpstat
4310 bpstat_copy (bpstat bs)
4311 {
4312 bpstat p = NULL;
4313 bpstat tmp;
4314 bpstat retval = NULL;
4315
4316 if (bs == NULL)
4317 return bs;
4318
4319 for (; bs != NULL; bs = bs->next)
4320 {
4321 tmp = new bpstats (*bs);
4322
4323 if (p == NULL)
4324 /* This is the first thing in the chain. */
4325 retval = tmp;
4326 else
4327 p->next = tmp;
4328 p = tmp;
4329 }
4330 p->next = NULL;
4331 return retval;
4332 }
4333
4334 /* Find the bpstat associated with this breakpoint. */
4335
4336 bpstat
4337 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4338 {
4339 if (bsp == NULL)
4340 return NULL;
4341
4342 for (; bsp != NULL; bsp = bsp->next)
4343 {
4344 if (bsp->breakpoint_at == breakpoint)
4345 return bsp;
4346 }
4347 return NULL;
4348 }
4349
4350 /* See breakpoint.h. */
4351
4352 int
4353 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4354 {
4355 for (; bsp != NULL; bsp = bsp->next)
4356 {
4357 if (bsp->breakpoint_at == NULL)
4358 {
4359 /* A moribund location can never explain a signal other than
4360 GDB_SIGNAL_TRAP. */
4361 if (sig == GDB_SIGNAL_TRAP)
4362 return 1;
4363 }
4364 else
4365 {
4366 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4367 sig))
4368 return 1;
4369 }
4370 }
4371
4372 return 0;
4373 }
4374
4375 /* Put in *NUM the breakpoint number of the first breakpoint we are
4376 stopped at. *BSP upon return is a bpstat which points to the
4377 remaining breakpoints stopped at (but which is not guaranteed to be
4378 good for anything but further calls to bpstat_num).
4379
4380 Return 0 if passed a bpstat which does not indicate any breakpoints.
4381 Return -1 if stopped at a breakpoint that has been deleted since
4382 we set it.
4383 Return 1 otherwise. */
4384
4385 int
4386 bpstat_num (bpstat *bsp, int *num)
4387 {
4388 struct breakpoint *b;
4389
4390 if ((*bsp) == NULL)
4391 return 0; /* No more breakpoint values */
4392
4393 /* We assume we'll never have several bpstats that correspond to a
4394 single breakpoint -- otherwise, this function might return the
4395 same number more than once and this will look ugly. */
4396 b = (*bsp)->breakpoint_at;
4397 *bsp = (*bsp)->next;
4398 if (b == NULL)
4399 return -1; /* breakpoint that's been deleted since */
4400
4401 *num = b->number; /* We have its number */
4402 return 1;
4403 }
4404
4405 /* See breakpoint.h. */
4406
4407 void
4408 bpstat_clear_actions (void)
4409 {
4410 struct thread_info *tp;
4411 bpstat bs;
4412
4413 if (ptid_equal (inferior_ptid, null_ptid))
4414 return;
4415
4416 tp = find_thread_ptid (inferior_ptid);
4417 if (tp == NULL)
4418 return;
4419
4420 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4421 {
4422 bs->commands = NULL;
4423
4424 if (bs->old_val != NULL)
4425 {
4426 value_free (bs->old_val);
4427 bs->old_val = NULL;
4428 }
4429 }
4430 }
4431
4432 /* Called when a command is about to proceed the inferior. */
4433
4434 static void
4435 breakpoint_about_to_proceed (void)
4436 {
4437 if (!ptid_equal (inferior_ptid, null_ptid))
4438 {
4439 struct thread_info *tp = inferior_thread ();
4440
4441 /* Allow inferior function calls in breakpoint commands to not
4442 interrupt the command list. When the call finishes
4443 successfully, the inferior will be standing at the same
4444 breakpoint as if nothing happened. */
4445 if (tp->control.in_infcall)
4446 return;
4447 }
4448
4449 breakpoint_proceeded = 1;
4450 }
4451
4452 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4453 or its equivalent. */
4454
4455 static int
4456 command_line_is_silent (struct command_line *cmd)
4457 {
4458 return cmd && (strcmp ("silent", cmd->line) == 0);
4459 }
4460
4461 /* Execute all the commands associated with all the breakpoints at
4462 this location. Any of these commands could cause the process to
4463 proceed beyond this point, etc. We look out for such changes by
4464 checking the global "breakpoint_proceeded" after each command.
4465
4466 Returns true if a breakpoint command resumed the inferior. In that
4467 case, it is the caller's responsibility to recall it again with the
4468 bpstat of the current thread. */
4469
4470 static int
4471 bpstat_do_actions_1 (bpstat *bsp)
4472 {
4473 bpstat bs;
4474 int again = 0;
4475
4476 /* Avoid endless recursion if a `source' command is contained
4477 in bs->commands. */
4478 if (executing_breakpoint_commands)
4479 return 0;
4480
4481 scoped_restore save_executing
4482 = make_scoped_restore (&executing_breakpoint_commands, 1);
4483
4484 scoped_restore preventer = prevent_dont_repeat ();
4485
4486 /* This pointer will iterate over the list of bpstat's. */
4487 bs = *bsp;
4488
4489 breakpoint_proceeded = 0;
4490 for (; bs != NULL; bs = bs->next)
4491 {
4492 struct command_line *cmd = NULL;
4493
4494 /* Take ownership of the BSP's command tree, if it has one.
4495
4496 The command tree could legitimately contain commands like
4497 'step' and 'next', which call clear_proceed_status, which
4498 frees stop_bpstat's command tree. To make sure this doesn't
4499 free the tree we're executing out from under us, we need to
4500 take ownership of the tree ourselves. Since a given bpstat's
4501 commands are only executed once, we don't need to copy it; we
4502 can clear the pointer in the bpstat, and make sure we free
4503 the tree when we're done. */
4504 counted_command_line ccmd = bs->commands;
4505 bs->commands = NULL;
4506 if (ccmd != NULL)
4507 cmd = ccmd.get ();
4508 if (command_line_is_silent (cmd))
4509 {
4510 /* The action has been already done by bpstat_stop_status. */
4511 cmd = cmd->next;
4512 }
4513
4514 while (cmd != NULL)
4515 {
4516 execute_control_command (cmd);
4517
4518 if (breakpoint_proceeded)
4519 break;
4520 else
4521 cmd = cmd->next;
4522 }
4523
4524 if (breakpoint_proceeded)
4525 {
4526 if (current_ui->async)
4527 /* If we are in async mode, then the target might be still
4528 running, not stopped at any breakpoint, so nothing for
4529 us to do here -- just return to the event loop. */
4530 ;
4531 else
4532 /* In sync mode, when execute_control_command returns
4533 we're already standing on the next breakpoint.
4534 Breakpoint commands for that stop were not run, since
4535 execute_command does not run breakpoint commands --
4536 only command_line_handler does, but that one is not
4537 involved in execution of breakpoint commands. So, we
4538 can now execute breakpoint commands. It should be
4539 noted that making execute_command do bpstat actions is
4540 not an option -- in this case we'll have recursive
4541 invocation of bpstat for each breakpoint with a
4542 command, and can easily blow up GDB stack. Instead, we
4543 return true, which will trigger the caller to recall us
4544 with the new stop_bpstat. */
4545 again = 1;
4546 break;
4547 }
4548 }
4549 return again;
4550 }
4551
4552 void
4553 bpstat_do_actions (void)
4554 {
4555 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4556
4557 /* Do any commands attached to breakpoint we are stopped at. */
4558 while (!ptid_equal (inferior_ptid, null_ptid)
4559 && target_has_execution
4560 && !is_exited (inferior_ptid)
4561 && !is_executing (inferior_ptid))
4562 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4563 and only return when it is stopped at the next breakpoint, we
4564 keep doing breakpoint actions until it returns false to
4565 indicate the inferior was not resumed. */
4566 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4567 break;
4568
4569 discard_cleanups (cleanup_if_error);
4570 }
4571
4572 /* Print out the (old or new) value associated with a watchpoint. */
4573
4574 static void
4575 watchpoint_value_print (struct value *val, struct ui_file *stream)
4576 {
4577 if (val == NULL)
4578 fprintf_unfiltered (stream, _("<unreadable>"));
4579 else
4580 {
4581 struct value_print_options opts;
4582 get_user_print_options (&opts);
4583 value_print (val, stream, &opts);
4584 }
4585 }
4586
4587 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4588 debugging multiple threads. */
4589
4590 void
4591 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4592 {
4593 if (uiout->is_mi_like_p ())
4594 return;
4595
4596 uiout->text ("\n");
4597
4598 if (show_thread_that_caused_stop ())
4599 {
4600 const char *name;
4601 struct thread_info *thr = inferior_thread ();
4602
4603 uiout->text ("Thread ");
4604 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4605
4606 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4607 if (name != NULL)
4608 {
4609 uiout->text (" \"");
4610 uiout->field_fmt ("name", "%s", name);
4611 uiout->text ("\"");
4612 }
4613
4614 uiout->text (" hit ");
4615 }
4616 }
4617
4618 /* Generic routine for printing messages indicating why we
4619 stopped. The behavior of this function depends on the value
4620 'print_it' in the bpstat structure. Under some circumstances we
4621 may decide not to print anything here and delegate the task to
4622 normal_stop(). */
4623
4624 static enum print_stop_action
4625 print_bp_stop_message (bpstat bs)
4626 {
4627 switch (bs->print_it)
4628 {
4629 case print_it_noop:
4630 /* Nothing should be printed for this bpstat entry. */
4631 return PRINT_UNKNOWN;
4632 break;
4633
4634 case print_it_done:
4635 /* We still want to print the frame, but we already printed the
4636 relevant messages. */
4637 return PRINT_SRC_AND_LOC;
4638 break;
4639
4640 case print_it_normal:
4641 {
4642 struct breakpoint *b = bs->breakpoint_at;
4643
4644 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4645 which has since been deleted. */
4646 if (b == NULL)
4647 return PRINT_UNKNOWN;
4648
4649 /* Normal case. Call the breakpoint's print_it method. */
4650 return b->ops->print_it (bs);
4651 }
4652 break;
4653
4654 default:
4655 internal_error (__FILE__, __LINE__,
4656 _("print_bp_stop_message: unrecognized enum value"));
4657 break;
4658 }
4659 }
4660
4661 /* A helper function that prints a shared library stopped event. */
4662
4663 static void
4664 print_solib_event (int is_catchpoint)
4665 {
4666 int any_deleted
4667 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4668 int any_added
4669 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4670
4671 if (!is_catchpoint)
4672 {
4673 if (any_added || any_deleted)
4674 current_uiout->text (_("Stopped due to shared library event:\n"));
4675 else
4676 current_uiout->text (_("Stopped due to shared library event (no "
4677 "libraries added or removed)\n"));
4678 }
4679
4680 if (current_uiout->is_mi_like_p ())
4681 current_uiout->field_string ("reason",
4682 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4683
4684 if (any_deleted)
4685 {
4686 char *name;
4687 int ix;
4688
4689 current_uiout->text (_(" Inferior unloaded "));
4690 ui_out_emit_list list_emitter (current_uiout, "removed");
4691 for (ix = 0;
4692 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4693 ix, name);
4694 ++ix)
4695 {
4696 if (ix > 0)
4697 current_uiout->text (" ");
4698 current_uiout->field_string ("library", name);
4699 current_uiout->text ("\n");
4700 }
4701 }
4702
4703 if (any_added)
4704 {
4705 struct so_list *iter;
4706 int ix;
4707
4708 current_uiout->text (_(" Inferior loaded "));
4709 ui_out_emit_list list_emitter (current_uiout, "added");
4710 for (ix = 0;
4711 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4712 ix, iter);
4713 ++ix)
4714 {
4715 if (ix > 0)
4716 current_uiout->text (" ");
4717 current_uiout->field_string ("library", iter->so_name);
4718 current_uiout->text ("\n");
4719 }
4720 }
4721 }
4722
4723 /* Print a message indicating what happened. This is called from
4724 normal_stop(). The input to this routine is the head of the bpstat
4725 list - a list of the eventpoints that caused this stop. KIND is
4726 the target_waitkind for the stopping event. This
4727 routine calls the generic print routine for printing a message
4728 about reasons for stopping. This will print (for example) the
4729 "Breakpoint n," part of the output. The return value of this
4730 routine is one of:
4731
4732 PRINT_UNKNOWN: Means we printed nothing.
4733 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4734 code to print the location. An example is
4735 "Breakpoint 1, " which should be followed by
4736 the location.
4737 PRINT_SRC_ONLY: Means we printed something, but there is no need
4738 to also print the location part of the message.
4739 An example is the catch/throw messages, which
4740 don't require a location appended to the end.
4741 PRINT_NOTHING: We have done some printing and we don't need any
4742 further info to be printed. */
4743
4744 enum print_stop_action
4745 bpstat_print (bpstat bs, int kind)
4746 {
4747 enum print_stop_action val;
4748
4749 /* Maybe another breakpoint in the chain caused us to stop.
4750 (Currently all watchpoints go on the bpstat whether hit or not.
4751 That probably could (should) be changed, provided care is taken
4752 with respect to bpstat_explains_signal). */
4753 for (; bs; bs = bs->next)
4754 {
4755 val = print_bp_stop_message (bs);
4756 if (val == PRINT_SRC_ONLY
4757 || val == PRINT_SRC_AND_LOC
4758 || val == PRINT_NOTHING)
4759 return val;
4760 }
4761
4762 /* If we had hit a shared library event breakpoint,
4763 print_bp_stop_message would print out this message. If we hit an
4764 OS-level shared library event, do the same thing. */
4765 if (kind == TARGET_WAITKIND_LOADED)
4766 {
4767 print_solib_event (0);
4768 return PRINT_NOTHING;
4769 }
4770
4771 /* We reached the end of the chain, or we got a null BS to start
4772 with and nothing was printed. */
4773 return PRINT_UNKNOWN;
4774 }
4775
4776 /* Evaluate the boolean expression EXP and return the result. */
4777
4778 static bool
4779 breakpoint_cond_eval (expression *exp)
4780 {
4781 struct value *mark = value_mark ();
4782 bool res = value_true (evaluate_expression (exp));
4783
4784 value_free_to_mark (mark);
4785 return res;
4786 }
4787
4788 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4789
4790 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4791 : next (NULL),
4792 bp_location_at (bl),
4793 breakpoint_at (bl->owner),
4794 commands (NULL),
4795 old_val (NULL),
4796 print (0),
4797 stop (0),
4798 print_it (print_it_normal)
4799 {
4800 incref_bp_location (bl);
4801 **bs_link_pointer = this;
4802 *bs_link_pointer = &next;
4803 }
4804
4805 bpstats::bpstats ()
4806 : next (NULL),
4807 bp_location_at (NULL),
4808 breakpoint_at (NULL),
4809 commands (NULL),
4810 old_val (NULL),
4811 print (0),
4812 stop (0),
4813 print_it (print_it_normal)
4814 {
4815 }
4816 \f
4817 /* The target has stopped with waitstatus WS. Check if any hardware
4818 watchpoints have triggered, according to the target. */
4819
4820 int
4821 watchpoints_triggered (struct target_waitstatus *ws)
4822 {
4823 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4824 CORE_ADDR addr;
4825 struct breakpoint *b;
4826
4827 if (!stopped_by_watchpoint)
4828 {
4829 /* We were not stopped by a watchpoint. Mark all watchpoints
4830 as not triggered. */
4831 ALL_BREAKPOINTS (b)
4832 if (is_hardware_watchpoint (b))
4833 {
4834 struct watchpoint *w = (struct watchpoint *) b;
4835
4836 w->watchpoint_triggered = watch_triggered_no;
4837 }
4838
4839 return 0;
4840 }
4841
4842 if (!target_stopped_data_address (&current_target, &addr))
4843 {
4844 /* We were stopped by a watchpoint, but we don't know where.
4845 Mark all watchpoints as unknown. */
4846 ALL_BREAKPOINTS (b)
4847 if (is_hardware_watchpoint (b))
4848 {
4849 struct watchpoint *w = (struct watchpoint *) b;
4850
4851 w->watchpoint_triggered = watch_triggered_unknown;
4852 }
4853
4854 return 1;
4855 }
4856
4857 /* The target could report the data address. Mark watchpoints
4858 affected by this data address as triggered, and all others as not
4859 triggered. */
4860
4861 ALL_BREAKPOINTS (b)
4862 if (is_hardware_watchpoint (b))
4863 {
4864 struct watchpoint *w = (struct watchpoint *) b;
4865 struct bp_location *loc;
4866
4867 w->watchpoint_triggered = watch_triggered_no;
4868 for (loc = b->loc; loc; loc = loc->next)
4869 {
4870 if (is_masked_watchpoint (b))
4871 {
4872 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4873 CORE_ADDR start = loc->address & w->hw_wp_mask;
4874
4875 if (newaddr == start)
4876 {
4877 w->watchpoint_triggered = watch_triggered_yes;
4878 break;
4879 }
4880 }
4881 /* Exact match not required. Within range is sufficient. */
4882 else if (target_watchpoint_addr_within_range (&current_target,
4883 addr, loc->address,
4884 loc->length))
4885 {
4886 w->watchpoint_triggered = watch_triggered_yes;
4887 break;
4888 }
4889 }
4890 }
4891
4892 return 1;
4893 }
4894
4895 /* Possible return values for watchpoint_check. */
4896 enum wp_check_result
4897 {
4898 /* The watchpoint has been deleted. */
4899 WP_DELETED = 1,
4900
4901 /* The value has changed. */
4902 WP_VALUE_CHANGED = 2,
4903
4904 /* The value has not changed. */
4905 WP_VALUE_NOT_CHANGED = 3,
4906
4907 /* Ignore this watchpoint, no matter if the value changed or not. */
4908 WP_IGNORE = 4,
4909 };
4910
4911 #define BP_TEMPFLAG 1
4912 #define BP_HARDWAREFLAG 2
4913
4914 /* Evaluate watchpoint condition expression and check if its value
4915 changed. */
4916
4917 static wp_check_result
4918 watchpoint_check (bpstat bs)
4919 {
4920 struct watchpoint *b;
4921 struct frame_info *fr;
4922 int within_current_scope;
4923
4924 /* BS is built from an existing struct breakpoint. */
4925 gdb_assert (bs->breakpoint_at != NULL);
4926 b = (struct watchpoint *) bs->breakpoint_at;
4927
4928 /* If this is a local watchpoint, we only want to check if the
4929 watchpoint frame is in scope if the current thread is the thread
4930 that was used to create the watchpoint. */
4931 if (!watchpoint_in_thread_scope (b))
4932 return WP_IGNORE;
4933
4934 if (b->exp_valid_block == NULL)
4935 within_current_scope = 1;
4936 else
4937 {
4938 struct frame_info *frame = get_current_frame ();
4939 struct gdbarch *frame_arch = get_frame_arch (frame);
4940 CORE_ADDR frame_pc = get_frame_pc (frame);
4941
4942 /* stack_frame_destroyed_p() returns a non-zero value if we're
4943 still in the function but the stack frame has already been
4944 invalidated. Since we can't rely on the values of local
4945 variables after the stack has been destroyed, we are treating
4946 the watchpoint in that state as `not changed' without further
4947 checking. Don't mark watchpoints as changed if the current
4948 frame is in an epilogue - even if they are in some other
4949 frame, our view of the stack is likely to be wrong and
4950 frame_find_by_id could error out. */
4951 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4952 return WP_IGNORE;
4953
4954 fr = frame_find_by_id (b->watchpoint_frame);
4955 within_current_scope = (fr != NULL);
4956
4957 /* If we've gotten confused in the unwinder, we might have
4958 returned a frame that can't describe this variable. */
4959 if (within_current_scope)
4960 {
4961 struct symbol *function;
4962
4963 function = get_frame_function (fr);
4964 if (function == NULL
4965 || !contained_in (b->exp_valid_block,
4966 SYMBOL_BLOCK_VALUE (function)))
4967 within_current_scope = 0;
4968 }
4969
4970 if (within_current_scope)
4971 /* If we end up stopping, the current frame will get selected
4972 in normal_stop. So this call to select_frame won't affect
4973 the user. */
4974 select_frame (fr);
4975 }
4976
4977 if (within_current_scope)
4978 {
4979 /* We use value_{,free_to_}mark because it could be a *long*
4980 time before we return to the command level and call
4981 free_all_values. We can't call free_all_values because we
4982 might be in the middle of evaluating a function call. */
4983
4984 int pc = 0;
4985 struct value *mark;
4986 struct value *new_val;
4987
4988 if (is_masked_watchpoint (b))
4989 /* Since we don't know the exact trigger address (from
4990 stopped_data_address), just tell the user we've triggered
4991 a mask watchpoint. */
4992 return WP_VALUE_CHANGED;
4993
4994 mark = value_mark ();
4995 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4996
4997 if (b->val_bitsize != 0)
4998 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4999
5000 /* We use value_equal_contents instead of value_equal because
5001 the latter coerces an array to a pointer, thus comparing just
5002 the address of the array instead of its contents. This is
5003 not what we want. */
5004 if ((b->val != NULL) != (new_val != NULL)
5005 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5006 {
5007 if (new_val != NULL)
5008 {
5009 release_value (new_val);
5010 value_free_to_mark (mark);
5011 }
5012 bs->old_val = b->val;
5013 b->val = new_val;
5014 b->val_valid = 1;
5015 return WP_VALUE_CHANGED;
5016 }
5017 else
5018 {
5019 /* Nothing changed. */
5020 value_free_to_mark (mark);
5021 return WP_VALUE_NOT_CHANGED;
5022 }
5023 }
5024 else
5025 {
5026 /* This seems like the only logical thing to do because
5027 if we temporarily ignored the watchpoint, then when
5028 we reenter the block in which it is valid it contains
5029 garbage (in the case of a function, it may have two
5030 garbage values, one before and one after the prologue).
5031 So we can't even detect the first assignment to it and
5032 watch after that (since the garbage may or may not equal
5033 the first value assigned). */
5034 /* We print all the stop information in
5035 breakpoint_ops->print_it, but in this case, by the time we
5036 call breakpoint_ops->print_it this bp will be deleted
5037 already. So we have no choice but print the information
5038 here. */
5039
5040 SWITCH_THRU_ALL_UIS ()
5041 {
5042 struct ui_out *uiout = current_uiout;
5043
5044 if (uiout->is_mi_like_p ())
5045 uiout->field_string
5046 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5047 uiout->text ("\nWatchpoint ");
5048 uiout->field_int ("wpnum", b->number);
5049 uiout->text (" deleted because the program has left the block in\n"
5050 "which its expression is valid.\n");
5051 }
5052
5053 /* Make sure the watchpoint's commands aren't executed. */
5054 b->commands = NULL;
5055 watchpoint_del_at_next_stop (b);
5056
5057 return WP_DELETED;
5058 }
5059 }
5060
5061 /* Return true if it looks like target has stopped due to hitting
5062 breakpoint location BL. This function does not check if we should
5063 stop, only if BL explains the stop. */
5064
5065 static int
5066 bpstat_check_location (const struct bp_location *bl,
5067 const address_space *aspace, CORE_ADDR bp_addr,
5068 const struct target_waitstatus *ws)
5069 {
5070 struct breakpoint *b = bl->owner;
5071
5072 /* BL is from an existing breakpoint. */
5073 gdb_assert (b != NULL);
5074
5075 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5076 }
5077
5078 /* Determine if the watched values have actually changed, and we
5079 should stop. If not, set BS->stop to 0. */
5080
5081 static void
5082 bpstat_check_watchpoint (bpstat bs)
5083 {
5084 const struct bp_location *bl;
5085 struct watchpoint *b;
5086
5087 /* BS is built for existing struct breakpoint. */
5088 bl = bs->bp_location_at;
5089 gdb_assert (bl != NULL);
5090 b = (struct watchpoint *) bs->breakpoint_at;
5091 gdb_assert (b != NULL);
5092
5093 {
5094 int must_check_value = 0;
5095
5096 if (b->type == bp_watchpoint)
5097 /* For a software watchpoint, we must always check the
5098 watched value. */
5099 must_check_value = 1;
5100 else if (b->watchpoint_triggered == watch_triggered_yes)
5101 /* We have a hardware watchpoint (read, write, or access)
5102 and the target earlier reported an address watched by
5103 this watchpoint. */
5104 must_check_value = 1;
5105 else if (b->watchpoint_triggered == watch_triggered_unknown
5106 && b->type == bp_hardware_watchpoint)
5107 /* We were stopped by a hardware watchpoint, but the target could
5108 not report the data address. We must check the watchpoint's
5109 value. Access and read watchpoints are out of luck; without
5110 a data address, we can't figure it out. */
5111 must_check_value = 1;
5112
5113 if (must_check_value)
5114 {
5115 wp_check_result e;
5116
5117 TRY
5118 {
5119 e = watchpoint_check (bs);
5120 }
5121 CATCH (ex, RETURN_MASK_ALL)
5122 {
5123 exception_fprintf (gdb_stderr, ex,
5124 "Error evaluating expression "
5125 "for watchpoint %d\n",
5126 b->number);
5127
5128 SWITCH_THRU_ALL_UIS ()
5129 {
5130 printf_filtered (_("Watchpoint %d deleted.\n"),
5131 b->number);
5132 }
5133 watchpoint_del_at_next_stop (b);
5134 e = WP_DELETED;
5135 }
5136 END_CATCH
5137
5138 switch (e)
5139 {
5140 case WP_DELETED:
5141 /* We've already printed what needs to be printed. */
5142 bs->print_it = print_it_done;
5143 /* Stop. */
5144 break;
5145 case WP_IGNORE:
5146 bs->print_it = print_it_noop;
5147 bs->stop = 0;
5148 break;
5149 case WP_VALUE_CHANGED:
5150 if (b->type == bp_read_watchpoint)
5151 {
5152 /* There are two cases to consider here:
5153
5154 1. We're watching the triggered memory for reads.
5155 In that case, trust the target, and always report
5156 the watchpoint hit to the user. Even though
5157 reads don't cause value changes, the value may
5158 have changed since the last time it was read, and
5159 since we're not trapping writes, we will not see
5160 those, and as such we should ignore our notion of
5161 old value.
5162
5163 2. We're watching the triggered memory for both
5164 reads and writes. There are two ways this may
5165 happen:
5166
5167 2.1. This is a target that can't break on data
5168 reads only, but can break on accesses (reads or
5169 writes), such as e.g., x86. We detect this case
5170 at the time we try to insert read watchpoints.
5171
5172 2.2. Otherwise, the target supports read
5173 watchpoints, but, the user set an access or write
5174 watchpoint watching the same memory as this read
5175 watchpoint.
5176
5177 If we're watching memory writes as well as reads,
5178 ignore watchpoint hits when we find that the
5179 value hasn't changed, as reads don't cause
5180 changes. This still gives false positives when
5181 the program writes the same value to memory as
5182 what there was already in memory (we will confuse
5183 it for a read), but it's much better than
5184 nothing. */
5185
5186 int other_write_watchpoint = 0;
5187
5188 if (bl->watchpoint_type == hw_read)
5189 {
5190 struct breakpoint *other_b;
5191
5192 ALL_BREAKPOINTS (other_b)
5193 if (other_b->type == bp_hardware_watchpoint
5194 || other_b->type == bp_access_watchpoint)
5195 {
5196 struct watchpoint *other_w =
5197 (struct watchpoint *) other_b;
5198
5199 if (other_w->watchpoint_triggered
5200 == watch_triggered_yes)
5201 {
5202 other_write_watchpoint = 1;
5203 break;
5204 }
5205 }
5206 }
5207
5208 if (other_write_watchpoint
5209 || bl->watchpoint_type == hw_access)
5210 {
5211 /* We're watching the same memory for writes,
5212 and the value changed since the last time we
5213 updated it, so this trap must be for a write.
5214 Ignore it. */
5215 bs->print_it = print_it_noop;
5216 bs->stop = 0;
5217 }
5218 }
5219 break;
5220 case WP_VALUE_NOT_CHANGED:
5221 if (b->type == bp_hardware_watchpoint
5222 || b->type == bp_watchpoint)
5223 {
5224 /* Don't stop: write watchpoints shouldn't fire if
5225 the value hasn't changed. */
5226 bs->print_it = print_it_noop;
5227 bs->stop = 0;
5228 }
5229 /* Stop. */
5230 break;
5231 default:
5232 /* Can't happen. */
5233 break;
5234 }
5235 }
5236 else /* must_check_value == 0 */
5237 {
5238 /* This is a case where some watchpoint(s) triggered, but
5239 not at the address of this watchpoint, or else no
5240 watchpoint triggered after all. So don't print
5241 anything for this watchpoint. */
5242 bs->print_it = print_it_noop;
5243 bs->stop = 0;
5244 }
5245 }
5246 }
5247
5248 /* For breakpoints that are currently marked as telling gdb to stop,
5249 check conditions (condition proper, frame, thread and ignore count)
5250 of breakpoint referred to by BS. If we should not stop for this
5251 breakpoint, set BS->stop to 0. */
5252
5253 static void
5254 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5255 {
5256 const struct bp_location *bl;
5257 struct breakpoint *b;
5258 /* Assume stop. */
5259 bool condition_result = true;
5260 struct expression *cond;
5261
5262 gdb_assert (bs->stop);
5263
5264 /* BS is built for existing struct breakpoint. */
5265 bl = bs->bp_location_at;
5266 gdb_assert (bl != NULL);
5267 b = bs->breakpoint_at;
5268 gdb_assert (b != NULL);
5269
5270 /* Even if the target evaluated the condition on its end and notified GDB, we
5271 need to do so again since GDB does not know if we stopped due to a
5272 breakpoint or a single step breakpoint. */
5273
5274 if (frame_id_p (b->frame_id)
5275 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5276 {
5277 bs->stop = 0;
5278 return;
5279 }
5280
5281 /* If this is a thread/task-specific breakpoint, don't waste cpu
5282 evaluating the condition if this isn't the specified
5283 thread/task. */
5284 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5285 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5286
5287 {
5288 bs->stop = 0;
5289 return;
5290 }
5291
5292 /* Evaluate extension language breakpoints that have a "stop" method
5293 implemented. */
5294 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5295
5296 if (is_watchpoint (b))
5297 {
5298 struct watchpoint *w = (struct watchpoint *) b;
5299
5300 cond = w->cond_exp.get ();
5301 }
5302 else
5303 cond = bl->cond.get ();
5304
5305 if (cond && b->disposition != disp_del_at_next_stop)
5306 {
5307 int within_current_scope = 1;
5308 struct watchpoint * w;
5309
5310 /* We use value_mark and value_free_to_mark because it could
5311 be a long time before we return to the command level and
5312 call free_all_values. We can't call free_all_values
5313 because we might be in the middle of evaluating a
5314 function call. */
5315 struct value *mark = value_mark ();
5316
5317 if (is_watchpoint (b))
5318 w = (struct watchpoint *) b;
5319 else
5320 w = NULL;
5321
5322 /* Need to select the frame, with all that implies so that
5323 the conditions will have the right context. Because we
5324 use the frame, we will not see an inlined function's
5325 variables when we arrive at a breakpoint at the start
5326 of the inlined function; the current frame will be the
5327 call site. */
5328 if (w == NULL || w->cond_exp_valid_block == NULL)
5329 select_frame (get_current_frame ());
5330 else
5331 {
5332 struct frame_info *frame;
5333
5334 /* For local watchpoint expressions, which particular
5335 instance of a local is being watched matters, so we
5336 keep track of the frame to evaluate the expression
5337 in. To evaluate the condition however, it doesn't
5338 really matter which instantiation of the function
5339 where the condition makes sense triggers the
5340 watchpoint. This allows an expression like "watch
5341 global if q > 10" set in `func', catch writes to
5342 global on all threads that call `func', or catch
5343 writes on all recursive calls of `func' by a single
5344 thread. We simply always evaluate the condition in
5345 the innermost frame that's executing where it makes
5346 sense to evaluate the condition. It seems
5347 intuitive. */
5348 frame = block_innermost_frame (w->cond_exp_valid_block);
5349 if (frame != NULL)
5350 select_frame (frame);
5351 else
5352 within_current_scope = 0;
5353 }
5354 if (within_current_scope)
5355 {
5356 TRY
5357 {
5358 condition_result = breakpoint_cond_eval (cond);
5359 }
5360 CATCH (ex, RETURN_MASK_ALL)
5361 {
5362 exception_fprintf (gdb_stderr, ex,
5363 "Error in testing breakpoint condition:\n");
5364 }
5365 END_CATCH
5366 }
5367 else
5368 {
5369 warning (_("Watchpoint condition cannot be tested "
5370 "in the current scope"));
5371 /* If we failed to set the right context for this
5372 watchpoint, unconditionally report it. */
5373 }
5374 /* FIXME-someday, should give breakpoint #. */
5375 value_free_to_mark (mark);
5376 }
5377
5378 if (cond && !condition_result)
5379 {
5380 bs->stop = 0;
5381 }
5382 else if (b->ignore_count > 0)
5383 {
5384 b->ignore_count--;
5385 bs->stop = 0;
5386 /* Increase the hit count even though we don't stop. */
5387 ++(b->hit_count);
5388 observer_notify_breakpoint_modified (b);
5389 }
5390 }
5391
5392 /* Returns true if we need to track moribund locations of LOC's type
5393 on the current target. */
5394
5395 static int
5396 need_moribund_for_location_type (struct bp_location *loc)
5397 {
5398 return ((loc->loc_type == bp_loc_software_breakpoint
5399 && !target_supports_stopped_by_sw_breakpoint ())
5400 || (loc->loc_type == bp_loc_hardware_breakpoint
5401 && !target_supports_stopped_by_hw_breakpoint ()));
5402 }
5403
5404
5405 /* Get a bpstat associated with having just stopped at address
5406 BP_ADDR in thread PTID.
5407
5408 Determine whether we stopped at a breakpoint, etc, or whether we
5409 don't understand this stop. Result is a chain of bpstat's such
5410 that:
5411
5412 if we don't understand the stop, the result is a null pointer.
5413
5414 if we understand why we stopped, the result is not null.
5415
5416 Each element of the chain refers to a particular breakpoint or
5417 watchpoint at which we have stopped. (We may have stopped for
5418 several reasons concurrently.)
5419
5420 Each element of the chain has valid next, breakpoint_at,
5421 commands, FIXME??? fields. */
5422
5423 bpstat
5424 bpstat_stop_status (const address_space *aspace,
5425 CORE_ADDR bp_addr, ptid_t ptid,
5426 const struct target_waitstatus *ws)
5427 {
5428 struct breakpoint *b = NULL;
5429 struct bp_location *bl;
5430 struct bp_location *loc;
5431 /* First item of allocated bpstat's. */
5432 bpstat bs_head = NULL, *bs_link = &bs_head;
5433 /* Pointer to the last thing in the chain currently. */
5434 bpstat bs;
5435 int ix;
5436 int need_remove_insert;
5437 int removed_any;
5438
5439 /* First, build the bpstat chain with locations that explain a
5440 target stop, while being careful to not set the target running,
5441 as that may invalidate locations (in particular watchpoint
5442 locations are recreated). Resuming will happen here with
5443 breakpoint conditions or watchpoint expressions that include
5444 inferior function calls. */
5445
5446 ALL_BREAKPOINTS (b)
5447 {
5448 if (!breakpoint_enabled (b))
5449 continue;
5450
5451 for (bl = b->loc; bl != NULL; bl = bl->next)
5452 {
5453 /* For hardware watchpoints, we look only at the first
5454 location. The watchpoint_check function will work on the
5455 entire expression, not the individual locations. For
5456 read watchpoints, the watchpoints_triggered function has
5457 checked all locations already. */
5458 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5459 break;
5460
5461 if (!bl->enabled || bl->shlib_disabled)
5462 continue;
5463
5464 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5465 continue;
5466
5467 /* Come here if it's a watchpoint, or if the break address
5468 matches. */
5469
5470 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5471 explain stop. */
5472
5473 /* Assume we stop. Should we find a watchpoint that is not
5474 actually triggered, or if the condition of the breakpoint
5475 evaluates as false, we'll reset 'stop' to 0. */
5476 bs->stop = 1;
5477 bs->print = 1;
5478
5479 /* If this is a scope breakpoint, mark the associated
5480 watchpoint as triggered so that we will handle the
5481 out-of-scope event. We'll get to the watchpoint next
5482 iteration. */
5483 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5484 {
5485 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5486
5487 w->watchpoint_triggered = watch_triggered_yes;
5488 }
5489 }
5490 }
5491
5492 /* Check if a moribund breakpoint explains the stop. */
5493 if (!target_supports_stopped_by_sw_breakpoint ()
5494 || !target_supports_stopped_by_hw_breakpoint ())
5495 {
5496 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5497 {
5498 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5499 && need_moribund_for_location_type (loc))
5500 {
5501 bs = new bpstats (loc, &bs_link);
5502 /* For hits of moribund locations, we should just proceed. */
5503 bs->stop = 0;
5504 bs->print = 0;
5505 bs->print_it = print_it_noop;
5506 }
5507 }
5508 }
5509
5510 /* A bit of special processing for shlib breakpoints. We need to
5511 process solib loading here, so that the lists of loaded and
5512 unloaded libraries are correct before we handle "catch load" and
5513 "catch unload". */
5514 for (bs = bs_head; bs != NULL; bs = bs->next)
5515 {
5516 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5517 {
5518 handle_solib_event ();
5519 break;
5520 }
5521 }
5522
5523 /* Now go through the locations that caused the target to stop, and
5524 check whether we're interested in reporting this stop to higher
5525 layers, or whether we should resume the target transparently. */
5526
5527 removed_any = 0;
5528
5529 for (bs = bs_head; bs != NULL; bs = bs->next)
5530 {
5531 if (!bs->stop)
5532 continue;
5533
5534 b = bs->breakpoint_at;
5535 b->ops->check_status (bs);
5536 if (bs->stop)
5537 {
5538 bpstat_check_breakpoint_conditions (bs, ptid);
5539
5540 if (bs->stop)
5541 {
5542 ++(b->hit_count);
5543 observer_notify_breakpoint_modified (b);
5544
5545 /* We will stop here. */
5546 if (b->disposition == disp_disable)
5547 {
5548 --(b->enable_count);
5549 if (b->enable_count <= 0)
5550 b->enable_state = bp_disabled;
5551 removed_any = 1;
5552 }
5553 if (b->silent)
5554 bs->print = 0;
5555 bs->commands = b->commands;
5556 if (command_line_is_silent (bs->commands
5557 ? bs->commands.get () : NULL))
5558 bs->print = 0;
5559
5560 b->ops->after_condition_true (bs);
5561 }
5562
5563 }
5564
5565 /* Print nothing for this entry if we don't stop or don't
5566 print. */
5567 if (!bs->stop || !bs->print)
5568 bs->print_it = print_it_noop;
5569 }
5570
5571 /* If we aren't stopping, the value of some hardware watchpoint may
5572 not have changed, but the intermediate memory locations we are
5573 watching may have. Don't bother if we're stopping; this will get
5574 done later. */
5575 need_remove_insert = 0;
5576 if (! bpstat_causes_stop (bs_head))
5577 for (bs = bs_head; bs != NULL; bs = bs->next)
5578 if (!bs->stop
5579 && bs->breakpoint_at
5580 && is_hardware_watchpoint (bs->breakpoint_at))
5581 {
5582 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5583
5584 update_watchpoint (w, 0 /* don't reparse. */);
5585 need_remove_insert = 1;
5586 }
5587
5588 if (need_remove_insert)
5589 update_global_location_list (UGLL_MAY_INSERT);
5590 else if (removed_any)
5591 update_global_location_list (UGLL_DONT_INSERT);
5592
5593 return bs_head;
5594 }
5595
5596 static void
5597 handle_jit_event (void)
5598 {
5599 struct frame_info *frame;
5600 struct gdbarch *gdbarch;
5601
5602 if (debug_infrun)
5603 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5604
5605 /* Switch terminal for any messages produced by
5606 breakpoint_re_set. */
5607 target_terminal::ours_for_output ();
5608
5609 frame = get_current_frame ();
5610 gdbarch = get_frame_arch (frame);
5611
5612 jit_event_handler (gdbarch);
5613
5614 target_terminal::inferior ();
5615 }
5616
5617 /* Prepare WHAT final decision for infrun. */
5618
5619 /* Decide what infrun needs to do with this bpstat. */
5620
5621 struct bpstat_what
5622 bpstat_what (bpstat bs_head)
5623 {
5624 struct bpstat_what retval;
5625 bpstat bs;
5626
5627 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5628 retval.call_dummy = STOP_NONE;
5629 retval.is_longjmp = 0;
5630
5631 for (bs = bs_head; bs != NULL; bs = bs->next)
5632 {
5633 /* Extract this BS's action. After processing each BS, we check
5634 if its action overrides all we've seem so far. */
5635 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5636 enum bptype bptype;
5637
5638 if (bs->breakpoint_at == NULL)
5639 {
5640 /* I suspect this can happen if it was a momentary
5641 breakpoint which has since been deleted. */
5642 bptype = bp_none;
5643 }
5644 else
5645 bptype = bs->breakpoint_at->type;
5646
5647 switch (bptype)
5648 {
5649 case bp_none:
5650 break;
5651 case bp_breakpoint:
5652 case bp_hardware_breakpoint:
5653 case bp_single_step:
5654 case bp_until:
5655 case bp_finish:
5656 case bp_shlib_event:
5657 if (bs->stop)
5658 {
5659 if (bs->print)
5660 this_action = BPSTAT_WHAT_STOP_NOISY;
5661 else
5662 this_action = BPSTAT_WHAT_STOP_SILENT;
5663 }
5664 else
5665 this_action = BPSTAT_WHAT_SINGLE;
5666 break;
5667 case bp_watchpoint:
5668 case bp_hardware_watchpoint:
5669 case bp_read_watchpoint:
5670 case bp_access_watchpoint:
5671 if (bs->stop)
5672 {
5673 if (bs->print)
5674 this_action = BPSTAT_WHAT_STOP_NOISY;
5675 else
5676 this_action = BPSTAT_WHAT_STOP_SILENT;
5677 }
5678 else
5679 {
5680 /* There was a watchpoint, but we're not stopping.
5681 This requires no further action. */
5682 }
5683 break;
5684 case bp_longjmp:
5685 case bp_longjmp_call_dummy:
5686 case bp_exception:
5687 if (bs->stop)
5688 {
5689 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5690 retval.is_longjmp = bptype != bp_exception;
5691 }
5692 else
5693 this_action = BPSTAT_WHAT_SINGLE;
5694 break;
5695 case bp_longjmp_resume:
5696 case bp_exception_resume:
5697 if (bs->stop)
5698 {
5699 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5700 retval.is_longjmp = bptype == bp_longjmp_resume;
5701 }
5702 else
5703 this_action = BPSTAT_WHAT_SINGLE;
5704 break;
5705 case bp_step_resume:
5706 if (bs->stop)
5707 this_action = BPSTAT_WHAT_STEP_RESUME;
5708 else
5709 {
5710 /* It is for the wrong frame. */
5711 this_action = BPSTAT_WHAT_SINGLE;
5712 }
5713 break;
5714 case bp_hp_step_resume:
5715 if (bs->stop)
5716 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5717 else
5718 {
5719 /* It is for the wrong frame. */
5720 this_action = BPSTAT_WHAT_SINGLE;
5721 }
5722 break;
5723 case bp_watchpoint_scope:
5724 case bp_thread_event:
5725 case bp_overlay_event:
5726 case bp_longjmp_master:
5727 case bp_std_terminate_master:
5728 case bp_exception_master:
5729 this_action = BPSTAT_WHAT_SINGLE;
5730 break;
5731 case bp_catchpoint:
5732 if (bs->stop)
5733 {
5734 if (bs->print)
5735 this_action = BPSTAT_WHAT_STOP_NOISY;
5736 else
5737 this_action = BPSTAT_WHAT_STOP_SILENT;
5738 }
5739 else
5740 {
5741 /* There was a catchpoint, but we're not stopping.
5742 This requires no further action. */
5743 }
5744 break;
5745 case bp_jit_event:
5746 this_action = BPSTAT_WHAT_SINGLE;
5747 break;
5748 case bp_call_dummy:
5749 /* Make sure the action is stop (silent or noisy),
5750 so infrun.c pops the dummy frame. */
5751 retval.call_dummy = STOP_STACK_DUMMY;
5752 this_action = BPSTAT_WHAT_STOP_SILENT;
5753 break;
5754 case bp_std_terminate:
5755 /* Make sure the action is stop (silent or noisy),
5756 so infrun.c pops the dummy frame. */
5757 retval.call_dummy = STOP_STD_TERMINATE;
5758 this_action = BPSTAT_WHAT_STOP_SILENT;
5759 break;
5760 case bp_tracepoint:
5761 case bp_fast_tracepoint:
5762 case bp_static_tracepoint:
5763 /* Tracepoint hits should not be reported back to GDB, and
5764 if one got through somehow, it should have been filtered
5765 out already. */
5766 internal_error (__FILE__, __LINE__,
5767 _("bpstat_what: tracepoint encountered"));
5768 break;
5769 case bp_gnu_ifunc_resolver:
5770 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5771 this_action = BPSTAT_WHAT_SINGLE;
5772 break;
5773 case bp_gnu_ifunc_resolver_return:
5774 /* The breakpoint will be removed, execution will restart from the
5775 PC of the former breakpoint. */
5776 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5777 break;
5778
5779 case bp_dprintf:
5780 if (bs->stop)
5781 this_action = BPSTAT_WHAT_STOP_SILENT;
5782 else
5783 this_action = BPSTAT_WHAT_SINGLE;
5784 break;
5785
5786 default:
5787 internal_error (__FILE__, __LINE__,
5788 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5789 }
5790
5791 retval.main_action = std::max (retval.main_action, this_action);
5792 }
5793
5794 return retval;
5795 }
5796
5797 void
5798 bpstat_run_callbacks (bpstat bs_head)
5799 {
5800 bpstat bs;
5801
5802 for (bs = bs_head; bs != NULL; bs = bs->next)
5803 {
5804 struct breakpoint *b = bs->breakpoint_at;
5805
5806 if (b == NULL)
5807 continue;
5808 switch (b->type)
5809 {
5810 case bp_jit_event:
5811 handle_jit_event ();
5812 break;
5813 case bp_gnu_ifunc_resolver:
5814 gnu_ifunc_resolver_stop (b);
5815 break;
5816 case bp_gnu_ifunc_resolver_return:
5817 gnu_ifunc_resolver_return_stop (b);
5818 break;
5819 }
5820 }
5821 }
5822
5823 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5824 without hardware support). This isn't related to a specific bpstat,
5825 just to things like whether watchpoints are set. */
5826
5827 int
5828 bpstat_should_step (void)
5829 {
5830 struct breakpoint *b;
5831
5832 ALL_BREAKPOINTS (b)
5833 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5834 return 1;
5835 return 0;
5836 }
5837
5838 int
5839 bpstat_causes_stop (bpstat bs)
5840 {
5841 for (; bs != NULL; bs = bs->next)
5842 if (bs->stop)
5843 return 1;
5844
5845 return 0;
5846 }
5847
5848 \f
5849
5850 /* Compute a string of spaces suitable to indent the next line
5851 so it starts at the position corresponding to the table column
5852 named COL_NAME in the currently active table of UIOUT. */
5853
5854 static char *
5855 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5856 {
5857 static char wrap_indent[80];
5858 int i, total_width, width, align;
5859 const char *text;
5860
5861 total_width = 0;
5862 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5863 {
5864 if (strcmp (text, col_name) == 0)
5865 {
5866 gdb_assert (total_width < sizeof wrap_indent);
5867 memset (wrap_indent, ' ', total_width);
5868 wrap_indent[total_width] = 0;
5869
5870 return wrap_indent;
5871 }
5872
5873 total_width += width + 1;
5874 }
5875
5876 return NULL;
5877 }
5878
5879 /* Determine if the locations of this breakpoint will have their conditions
5880 evaluated by the target, host or a mix of both. Returns the following:
5881
5882 "host": Host evals condition.
5883 "host or target": Host or Target evals condition.
5884 "target": Target evals condition.
5885 */
5886
5887 static const char *
5888 bp_condition_evaluator (struct breakpoint *b)
5889 {
5890 struct bp_location *bl;
5891 char host_evals = 0;
5892 char target_evals = 0;
5893
5894 if (!b)
5895 return NULL;
5896
5897 if (!is_breakpoint (b))
5898 return NULL;
5899
5900 if (gdb_evaluates_breakpoint_condition_p ()
5901 || !target_supports_evaluation_of_breakpoint_conditions ())
5902 return condition_evaluation_host;
5903
5904 for (bl = b->loc; bl; bl = bl->next)
5905 {
5906 if (bl->cond_bytecode)
5907 target_evals++;
5908 else
5909 host_evals++;
5910 }
5911
5912 if (host_evals && target_evals)
5913 return condition_evaluation_both;
5914 else if (target_evals)
5915 return condition_evaluation_target;
5916 else
5917 return condition_evaluation_host;
5918 }
5919
5920 /* Determine the breakpoint location's condition evaluator. This is
5921 similar to bp_condition_evaluator, but for locations. */
5922
5923 static const char *
5924 bp_location_condition_evaluator (struct bp_location *bl)
5925 {
5926 if (bl && !is_breakpoint (bl->owner))
5927 return NULL;
5928
5929 if (gdb_evaluates_breakpoint_condition_p ()
5930 || !target_supports_evaluation_of_breakpoint_conditions ())
5931 return condition_evaluation_host;
5932
5933 if (bl && bl->cond_bytecode)
5934 return condition_evaluation_target;
5935 else
5936 return condition_evaluation_host;
5937 }
5938
5939 /* Print the LOC location out of the list of B->LOC locations. */
5940
5941 static void
5942 print_breakpoint_location (struct breakpoint *b,
5943 struct bp_location *loc)
5944 {
5945 struct ui_out *uiout = current_uiout;
5946
5947 scoped_restore_current_program_space restore_pspace;
5948
5949 if (loc != NULL && loc->shlib_disabled)
5950 loc = NULL;
5951
5952 if (loc != NULL)
5953 set_current_program_space (loc->pspace);
5954
5955 if (b->display_canonical)
5956 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5957 else if (loc && loc->symtab)
5958 {
5959 const struct symbol *sym = loc->symbol;
5960
5961 if (sym == NULL)
5962 sym = find_pc_sect_function (loc->address, loc->section);
5963
5964 if (sym)
5965 {
5966 uiout->text ("in ");
5967 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
5968 uiout->text (" ");
5969 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5970 uiout->text ("at ");
5971 }
5972 uiout->field_string ("file",
5973 symtab_to_filename_for_display (loc->symtab));
5974 uiout->text (":");
5975
5976 if (uiout->is_mi_like_p ())
5977 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5978
5979 uiout->field_int ("line", loc->line_number);
5980 }
5981 else if (loc)
5982 {
5983 string_file stb;
5984
5985 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5986 demangle, "");
5987 uiout->field_stream ("at", stb);
5988 }
5989 else
5990 {
5991 uiout->field_string ("pending",
5992 event_location_to_string (b->location.get ()));
5993 /* If extra_string is available, it could be holding a condition
5994 or dprintf arguments. In either case, make sure it is printed,
5995 too, but only for non-MI streams. */
5996 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5997 {
5998 if (b->type == bp_dprintf)
5999 uiout->text (",");
6000 else
6001 uiout->text (" ");
6002 uiout->text (b->extra_string);
6003 }
6004 }
6005
6006 if (loc && is_breakpoint (b)
6007 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6008 && bp_condition_evaluator (b) == condition_evaluation_both)
6009 {
6010 uiout->text (" (");
6011 uiout->field_string ("evaluated-by",
6012 bp_location_condition_evaluator (loc));
6013 uiout->text (")");
6014 }
6015 }
6016
6017 static const char *
6018 bptype_string (enum bptype type)
6019 {
6020 struct ep_type_description
6021 {
6022 enum bptype type;
6023 const char *description;
6024 };
6025 static struct ep_type_description bptypes[] =
6026 {
6027 {bp_none, "?deleted?"},
6028 {bp_breakpoint, "breakpoint"},
6029 {bp_hardware_breakpoint, "hw breakpoint"},
6030 {bp_single_step, "sw single-step"},
6031 {bp_until, "until"},
6032 {bp_finish, "finish"},
6033 {bp_watchpoint, "watchpoint"},
6034 {bp_hardware_watchpoint, "hw watchpoint"},
6035 {bp_read_watchpoint, "read watchpoint"},
6036 {bp_access_watchpoint, "acc watchpoint"},
6037 {bp_longjmp, "longjmp"},
6038 {bp_longjmp_resume, "longjmp resume"},
6039 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6040 {bp_exception, "exception"},
6041 {bp_exception_resume, "exception resume"},
6042 {bp_step_resume, "step resume"},
6043 {bp_hp_step_resume, "high-priority step resume"},
6044 {bp_watchpoint_scope, "watchpoint scope"},
6045 {bp_call_dummy, "call dummy"},
6046 {bp_std_terminate, "std::terminate"},
6047 {bp_shlib_event, "shlib events"},
6048 {bp_thread_event, "thread events"},
6049 {bp_overlay_event, "overlay events"},
6050 {bp_longjmp_master, "longjmp master"},
6051 {bp_std_terminate_master, "std::terminate master"},
6052 {bp_exception_master, "exception master"},
6053 {bp_catchpoint, "catchpoint"},
6054 {bp_tracepoint, "tracepoint"},
6055 {bp_fast_tracepoint, "fast tracepoint"},
6056 {bp_static_tracepoint, "static tracepoint"},
6057 {bp_dprintf, "dprintf"},
6058 {bp_jit_event, "jit events"},
6059 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6060 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6061 };
6062
6063 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6064 || ((int) type != bptypes[(int) type].type))
6065 internal_error (__FILE__, __LINE__,
6066 _("bptypes table does not describe type #%d."),
6067 (int) type);
6068
6069 return bptypes[(int) type].description;
6070 }
6071
6072 /* For MI, output a field named 'thread-groups' with a list as the value.
6073 For CLI, prefix the list with the string 'inf'. */
6074
6075 static void
6076 output_thread_groups (struct ui_out *uiout,
6077 const char *field_name,
6078 VEC(int) *inf_num,
6079 int mi_only)
6080 {
6081 int is_mi = uiout->is_mi_like_p ();
6082 int inf;
6083 int i;
6084
6085 /* For backward compatibility, don't display inferiors in CLI unless
6086 there are several. Always display them for MI. */
6087 if (!is_mi && mi_only)
6088 return;
6089
6090 ui_out_emit_list list_emitter (uiout, field_name);
6091
6092 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6093 {
6094 if (is_mi)
6095 {
6096 char mi_group[10];
6097
6098 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6099 uiout->field_string (NULL, mi_group);
6100 }
6101 else
6102 {
6103 if (i == 0)
6104 uiout->text (" inf ");
6105 else
6106 uiout->text (", ");
6107
6108 uiout->text (plongest (inf));
6109 }
6110 }
6111 }
6112
6113 /* Print B to gdb_stdout. */
6114
6115 static void
6116 print_one_breakpoint_location (struct breakpoint *b,
6117 struct bp_location *loc,
6118 int loc_number,
6119 struct bp_location **last_loc,
6120 int allflag)
6121 {
6122 struct command_line *l;
6123 static char bpenables[] = "nynny";
6124
6125 struct ui_out *uiout = current_uiout;
6126 int header_of_multiple = 0;
6127 int part_of_multiple = (loc != NULL);
6128 struct value_print_options opts;
6129
6130 get_user_print_options (&opts);
6131
6132 gdb_assert (!loc || loc_number != 0);
6133 /* See comment in print_one_breakpoint concerning treatment of
6134 breakpoints with single disabled location. */
6135 if (loc == NULL
6136 && (b->loc != NULL
6137 && (b->loc->next != NULL || !b->loc->enabled)))
6138 header_of_multiple = 1;
6139 if (loc == NULL)
6140 loc = b->loc;
6141
6142 annotate_record ();
6143
6144 /* 1 */
6145 annotate_field (0);
6146 if (part_of_multiple)
6147 {
6148 char *formatted;
6149 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6150 uiout->field_string ("number", formatted);
6151 xfree (formatted);
6152 }
6153 else
6154 {
6155 uiout->field_int ("number", b->number);
6156 }
6157
6158 /* 2 */
6159 annotate_field (1);
6160 if (part_of_multiple)
6161 uiout->field_skip ("type");
6162 else
6163 uiout->field_string ("type", bptype_string (b->type));
6164
6165 /* 3 */
6166 annotate_field (2);
6167 if (part_of_multiple)
6168 uiout->field_skip ("disp");
6169 else
6170 uiout->field_string ("disp", bpdisp_text (b->disposition));
6171
6172
6173 /* 4 */
6174 annotate_field (3);
6175 if (part_of_multiple)
6176 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6177 else
6178 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6179 uiout->spaces (2);
6180
6181
6182 /* 5 and 6 */
6183 if (b->ops != NULL && b->ops->print_one != NULL)
6184 {
6185 /* Although the print_one can possibly print all locations,
6186 calling it here is not likely to get any nice result. So,
6187 make sure there's just one location. */
6188 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6189 b->ops->print_one (b, last_loc);
6190 }
6191 else
6192 switch (b->type)
6193 {
6194 case bp_none:
6195 internal_error (__FILE__, __LINE__,
6196 _("print_one_breakpoint: bp_none encountered\n"));
6197 break;
6198
6199 case bp_watchpoint:
6200 case bp_hardware_watchpoint:
6201 case bp_read_watchpoint:
6202 case bp_access_watchpoint:
6203 {
6204 struct watchpoint *w = (struct watchpoint *) b;
6205
6206 /* Field 4, the address, is omitted (which makes the columns
6207 not line up too nicely with the headers, but the effect
6208 is relatively readable). */
6209 if (opts.addressprint)
6210 uiout->field_skip ("addr");
6211 annotate_field (5);
6212 uiout->field_string ("what", w->exp_string);
6213 }
6214 break;
6215
6216 case bp_breakpoint:
6217 case bp_hardware_breakpoint:
6218 case bp_single_step:
6219 case bp_until:
6220 case bp_finish:
6221 case bp_longjmp:
6222 case bp_longjmp_resume:
6223 case bp_longjmp_call_dummy:
6224 case bp_exception:
6225 case bp_exception_resume:
6226 case bp_step_resume:
6227 case bp_hp_step_resume:
6228 case bp_watchpoint_scope:
6229 case bp_call_dummy:
6230 case bp_std_terminate:
6231 case bp_shlib_event:
6232 case bp_thread_event:
6233 case bp_overlay_event:
6234 case bp_longjmp_master:
6235 case bp_std_terminate_master:
6236 case bp_exception_master:
6237 case bp_tracepoint:
6238 case bp_fast_tracepoint:
6239 case bp_static_tracepoint:
6240 case bp_dprintf:
6241 case bp_jit_event:
6242 case bp_gnu_ifunc_resolver:
6243 case bp_gnu_ifunc_resolver_return:
6244 if (opts.addressprint)
6245 {
6246 annotate_field (4);
6247 if (header_of_multiple)
6248 uiout->field_string ("addr", "<MULTIPLE>");
6249 else if (b->loc == NULL || loc->shlib_disabled)
6250 uiout->field_string ("addr", "<PENDING>");
6251 else
6252 uiout->field_core_addr ("addr",
6253 loc->gdbarch, loc->address);
6254 }
6255 annotate_field (5);
6256 if (!header_of_multiple)
6257 print_breakpoint_location (b, loc);
6258 if (b->loc)
6259 *last_loc = b->loc;
6260 break;
6261 }
6262
6263
6264 if (loc != NULL && !header_of_multiple)
6265 {
6266 struct inferior *inf;
6267 VEC(int) *inf_num = NULL;
6268 int mi_only = 1;
6269
6270 ALL_INFERIORS (inf)
6271 {
6272 if (inf->pspace == loc->pspace)
6273 VEC_safe_push (int, inf_num, inf->num);
6274 }
6275
6276 /* For backward compatibility, don't display inferiors in CLI unless
6277 there are several. Always display for MI. */
6278 if (allflag
6279 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6280 && (number_of_program_spaces () > 1
6281 || number_of_inferiors () > 1)
6282 /* LOC is for existing B, it cannot be in
6283 moribund_locations and thus having NULL OWNER. */
6284 && loc->owner->type != bp_catchpoint))
6285 mi_only = 0;
6286 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6287 VEC_free (int, inf_num);
6288 }
6289
6290 if (!part_of_multiple)
6291 {
6292 if (b->thread != -1)
6293 {
6294 /* FIXME: This seems to be redundant and lost here; see the
6295 "stop only in" line a little further down. */
6296 uiout->text (" thread ");
6297 uiout->field_int ("thread", b->thread);
6298 }
6299 else if (b->task != 0)
6300 {
6301 uiout->text (" task ");
6302 uiout->field_int ("task", b->task);
6303 }
6304 }
6305
6306 uiout->text ("\n");
6307
6308 if (!part_of_multiple)
6309 b->ops->print_one_detail (b, uiout);
6310
6311 if (part_of_multiple && frame_id_p (b->frame_id))
6312 {
6313 annotate_field (6);
6314 uiout->text ("\tstop only in stack frame at ");
6315 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6316 the frame ID. */
6317 uiout->field_core_addr ("frame",
6318 b->gdbarch, b->frame_id.stack_addr);
6319 uiout->text ("\n");
6320 }
6321
6322 if (!part_of_multiple && b->cond_string)
6323 {
6324 annotate_field (7);
6325 if (is_tracepoint (b))
6326 uiout->text ("\ttrace only if ");
6327 else
6328 uiout->text ("\tstop only if ");
6329 uiout->field_string ("cond", b->cond_string);
6330
6331 /* Print whether the target is doing the breakpoint's condition
6332 evaluation. If GDB is doing the evaluation, don't print anything. */
6333 if (is_breakpoint (b)
6334 && breakpoint_condition_evaluation_mode ()
6335 == condition_evaluation_target)
6336 {
6337 uiout->text (" (");
6338 uiout->field_string ("evaluated-by",
6339 bp_condition_evaluator (b));
6340 uiout->text (" evals)");
6341 }
6342 uiout->text ("\n");
6343 }
6344
6345 if (!part_of_multiple && b->thread != -1)
6346 {
6347 /* FIXME should make an annotation for this. */
6348 uiout->text ("\tstop only in thread ");
6349 if (uiout->is_mi_like_p ())
6350 uiout->field_int ("thread", b->thread);
6351 else
6352 {
6353 struct thread_info *thr = find_thread_global_id (b->thread);
6354
6355 uiout->field_string ("thread", print_thread_id (thr));
6356 }
6357 uiout->text ("\n");
6358 }
6359
6360 if (!part_of_multiple)
6361 {
6362 if (b->hit_count)
6363 {
6364 /* FIXME should make an annotation for this. */
6365 if (is_catchpoint (b))
6366 uiout->text ("\tcatchpoint");
6367 else if (is_tracepoint (b))
6368 uiout->text ("\ttracepoint");
6369 else
6370 uiout->text ("\tbreakpoint");
6371 uiout->text (" already hit ");
6372 uiout->field_int ("times", b->hit_count);
6373 if (b->hit_count == 1)
6374 uiout->text (" time\n");
6375 else
6376 uiout->text (" times\n");
6377 }
6378 else
6379 {
6380 /* Output the count also if it is zero, but only if this is mi. */
6381 if (uiout->is_mi_like_p ())
6382 uiout->field_int ("times", b->hit_count);
6383 }
6384 }
6385
6386 if (!part_of_multiple && b->ignore_count)
6387 {
6388 annotate_field (8);
6389 uiout->text ("\tignore next ");
6390 uiout->field_int ("ignore", b->ignore_count);
6391 uiout->text (" hits\n");
6392 }
6393
6394 /* Note that an enable count of 1 corresponds to "enable once"
6395 behavior, which is reported by the combination of enablement and
6396 disposition, so we don't need to mention it here. */
6397 if (!part_of_multiple && b->enable_count > 1)
6398 {
6399 annotate_field (8);
6400 uiout->text ("\tdisable after ");
6401 /* Tweak the wording to clarify that ignore and enable counts
6402 are distinct, and have additive effect. */
6403 if (b->ignore_count)
6404 uiout->text ("additional ");
6405 else
6406 uiout->text ("next ");
6407 uiout->field_int ("enable", b->enable_count);
6408 uiout->text (" hits\n");
6409 }
6410
6411 if (!part_of_multiple && is_tracepoint (b))
6412 {
6413 struct tracepoint *tp = (struct tracepoint *) b;
6414
6415 if (tp->traceframe_usage)
6416 {
6417 uiout->text ("\ttrace buffer usage ");
6418 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6419 uiout->text (" bytes\n");
6420 }
6421 }
6422
6423 l = b->commands ? b->commands.get () : NULL;
6424 if (!part_of_multiple && l)
6425 {
6426 annotate_field (9);
6427 ui_out_emit_tuple tuple_emitter (uiout, "script");
6428 print_command_lines (uiout, l, 4);
6429 }
6430
6431 if (is_tracepoint (b))
6432 {
6433 struct tracepoint *t = (struct tracepoint *) b;
6434
6435 if (!part_of_multiple && t->pass_count)
6436 {
6437 annotate_field (10);
6438 uiout->text ("\tpass count ");
6439 uiout->field_int ("pass", t->pass_count);
6440 uiout->text (" \n");
6441 }
6442
6443 /* Don't display it when tracepoint or tracepoint location is
6444 pending. */
6445 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6446 {
6447 annotate_field (11);
6448
6449 if (uiout->is_mi_like_p ())
6450 uiout->field_string ("installed",
6451 loc->inserted ? "y" : "n");
6452 else
6453 {
6454 if (loc->inserted)
6455 uiout->text ("\t");
6456 else
6457 uiout->text ("\tnot ");
6458 uiout->text ("installed on target\n");
6459 }
6460 }
6461 }
6462
6463 if (uiout->is_mi_like_p () && !part_of_multiple)
6464 {
6465 if (is_watchpoint (b))
6466 {
6467 struct watchpoint *w = (struct watchpoint *) b;
6468
6469 uiout->field_string ("original-location", w->exp_string);
6470 }
6471 else if (b->location != NULL
6472 && event_location_to_string (b->location.get ()) != NULL)
6473 uiout->field_string ("original-location",
6474 event_location_to_string (b->location.get ()));
6475 }
6476 }
6477
6478 static void
6479 print_one_breakpoint (struct breakpoint *b,
6480 struct bp_location **last_loc,
6481 int allflag)
6482 {
6483 struct ui_out *uiout = current_uiout;
6484
6485 {
6486 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6487
6488 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6489 }
6490
6491 /* If this breakpoint has custom print function,
6492 it's already printed. Otherwise, print individual
6493 locations, if any. */
6494 if (b->ops == NULL || b->ops->print_one == NULL)
6495 {
6496 /* If breakpoint has a single location that is disabled, we
6497 print it as if it had several locations, since otherwise it's
6498 hard to represent "breakpoint enabled, location disabled"
6499 situation.
6500
6501 Note that while hardware watchpoints have several locations
6502 internally, that's not a property exposed to user. */
6503 if (b->loc
6504 && !is_hardware_watchpoint (b)
6505 && (b->loc->next || !b->loc->enabled))
6506 {
6507 struct bp_location *loc;
6508 int n = 1;
6509
6510 for (loc = b->loc; loc; loc = loc->next, ++n)
6511 {
6512 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6513 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6514 }
6515 }
6516 }
6517 }
6518
6519 static int
6520 breakpoint_address_bits (struct breakpoint *b)
6521 {
6522 int print_address_bits = 0;
6523 struct bp_location *loc;
6524
6525 /* Software watchpoints that aren't watching memory don't have an
6526 address to print. */
6527 if (is_no_memory_software_watchpoint (b))
6528 return 0;
6529
6530 for (loc = b->loc; loc; loc = loc->next)
6531 {
6532 int addr_bit;
6533
6534 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6535 if (addr_bit > print_address_bits)
6536 print_address_bits = addr_bit;
6537 }
6538
6539 return print_address_bits;
6540 }
6541
6542 /* See breakpoint.h. */
6543
6544 void
6545 print_breakpoint (breakpoint *b)
6546 {
6547 struct bp_location *dummy_loc = NULL;
6548 print_one_breakpoint (b, &dummy_loc, 0);
6549 }
6550
6551 /* Return true if this breakpoint was set by the user, false if it is
6552 internal or momentary. */
6553
6554 int
6555 user_breakpoint_p (struct breakpoint *b)
6556 {
6557 return b->number > 0;
6558 }
6559
6560 /* See breakpoint.h. */
6561
6562 int
6563 pending_breakpoint_p (struct breakpoint *b)
6564 {
6565 return b->loc == NULL;
6566 }
6567
6568 /* Print information on user settable breakpoint (watchpoint, etc)
6569 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6570 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6571 FILTER is non-NULL, call it on each breakpoint and only include the
6572 ones for which it returns non-zero. Return the total number of
6573 breakpoints listed. */
6574
6575 static int
6576 breakpoint_1 (const char *args, int allflag,
6577 int (*filter) (const struct breakpoint *))
6578 {
6579 struct breakpoint *b;
6580 struct bp_location *last_loc = NULL;
6581 int nr_printable_breakpoints;
6582 struct value_print_options opts;
6583 int print_address_bits = 0;
6584 int print_type_col_width = 14;
6585 struct ui_out *uiout = current_uiout;
6586
6587 get_user_print_options (&opts);
6588
6589 /* Compute the number of rows in the table, as well as the size
6590 required for address fields. */
6591 nr_printable_breakpoints = 0;
6592 ALL_BREAKPOINTS (b)
6593 {
6594 /* If we have a filter, only list the breakpoints it accepts. */
6595 if (filter && !filter (b))
6596 continue;
6597
6598 /* If we have an "args" string, it is a list of breakpoints to
6599 accept. Skip the others. */
6600 if (args != NULL && *args != '\0')
6601 {
6602 if (allflag && parse_and_eval_long (args) != b->number)
6603 continue;
6604 if (!allflag && !number_is_in_list (args, b->number))
6605 continue;
6606 }
6607
6608 if (allflag || user_breakpoint_p (b))
6609 {
6610 int addr_bit, type_len;
6611
6612 addr_bit = breakpoint_address_bits (b);
6613 if (addr_bit > print_address_bits)
6614 print_address_bits = addr_bit;
6615
6616 type_len = strlen (bptype_string (b->type));
6617 if (type_len > print_type_col_width)
6618 print_type_col_width = type_len;
6619
6620 nr_printable_breakpoints++;
6621 }
6622 }
6623
6624 {
6625 ui_out_emit_table table_emitter (uiout,
6626 opts.addressprint ? 6 : 5,
6627 nr_printable_breakpoints,
6628 "BreakpointTable");
6629
6630 if (nr_printable_breakpoints > 0)
6631 annotate_breakpoints_headers ();
6632 if (nr_printable_breakpoints > 0)
6633 annotate_field (0);
6634 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6635 if (nr_printable_breakpoints > 0)
6636 annotate_field (1);
6637 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6638 if (nr_printable_breakpoints > 0)
6639 annotate_field (2);
6640 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6641 if (nr_printable_breakpoints > 0)
6642 annotate_field (3);
6643 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6644 if (opts.addressprint)
6645 {
6646 if (nr_printable_breakpoints > 0)
6647 annotate_field (4);
6648 if (print_address_bits <= 32)
6649 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6650 else
6651 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6652 }
6653 if (nr_printable_breakpoints > 0)
6654 annotate_field (5);
6655 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6656 uiout->table_body ();
6657 if (nr_printable_breakpoints > 0)
6658 annotate_breakpoints_table ();
6659
6660 ALL_BREAKPOINTS (b)
6661 {
6662 QUIT;
6663 /* If we have a filter, only list the breakpoints it accepts. */
6664 if (filter && !filter (b))
6665 continue;
6666
6667 /* If we have an "args" string, it is a list of breakpoints to
6668 accept. Skip the others. */
6669
6670 if (args != NULL && *args != '\0')
6671 {
6672 if (allflag) /* maintenance info breakpoint */
6673 {
6674 if (parse_and_eval_long (args) != b->number)
6675 continue;
6676 }
6677 else /* all others */
6678 {
6679 if (!number_is_in_list (args, b->number))
6680 continue;
6681 }
6682 }
6683 /* We only print out user settable breakpoints unless the
6684 allflag is set. */
6685 if (allflag || user_breakpoint_p (b))
6686 print_one_breakpoint (b, &last_loc, allflag);
6687 }
6688 }
6689
6690 if (nr_printable_breakpoints == 0)
6691 {
6692 /* If there's a filter, let the caller decide how to report
6693 empty list. */
6694 if (!filter)
6695 {
6696 if (args == NULL || *args == '\0')
6697 uiout->message ("No breakpoints or watchpoints.\n");
6698 else
6699 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6700 args);
6701 }
6702 }
6703 else
6704 {
6705 if (last_loc && !server_command)
6706 set_next_address (last_loc->gdbarch, last_loc->address);
6707 }
6708
6709 /* FIXME? Should this be moved up so that it is only called when
6710 there have been breakpoints? */
6711 annotate_breakpoints_table_end ();
6712
6713 return nr_printable_breakpoints;
6714 }
6715
6716 /* Display the value of default-collect in a way that is generally
6717 compatible with the breakpoint list. */
6718
6719 static void
6720 default_collect_info (void)
6721 {
6722 struct ui_out *uiout = current_uiout;
6723
6724 /* If it has no value (which is frequently the case), say nothing; a
6725 message like "No default-collect." gets in user's face when it's
6726 not wanted. */
6727 if (!*default_collect)
6728 return;
6729
6730 /* The following phrase lines up nicely with per-tracepoint collect
6731 actions. */
6732 uiout->text ("default collect ");
6733 uiout->field_string ("default-collect", default_collect);
6734 uiout->text (" \n");
6735 }
6736
6737 static void
6738 info_breakpoints_command (char *args, int from_tty)
6739 {
6740 breakpoint_1 (args, 0, NULL);
6741
6742 default_collect_info ();
6743 }
6744
6745 static void
6746 info_watchpoints_command (char *args, int from_tty)
6747 {
6748 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6749 struct ui_out *uiout = current_uiout;
6750
6751 if (num_printed == 0)
6752 {
6753 if (args == NULL || *args == '\0')
6754 uiout->message ("No watchpoints.\n");
6755 else
6756 uiout->message ("No watchpoint matching '%s'.\n", args);
6757 }
6758 }
6759
6760 static void
6761 maintenance_info_breakpoints (const char *args, int from_tty)
6762 {
6763 breakpoint_1 (args, 1, NULL);
6764
6765 default_collect_info ();
6766 }
6767
6768 static int
6769 breakpoint_has_pc (struct breakpoint *b,
6770 struct program_space *pspace,
6771 CORE_ADDR pc, struct obj_section *section)
6772 {
6773 struct bp_location *bl = b->loc;
6774
6775 for (; bl; bl = bl->next)
6776 {
6777 if (bl->pspace == pspace
6778 && bl->address == pc
6779 && (!overlay_debugging || bl->section == section))
6780 return 1;
6781 }
6782 return 0;
6783 }
6784
6785 /* Print a message describing any user-breakpoints set at PC. This
6786 concerns with logical breakpoints, so we match program spaces, not
6787 address spaces. */
6788
6789 static void
6790 describe_other_breakpoints (struct gdbarch *gdbarch,
6791 struct program_space *pspace, CORE_ADDR pc,
6792 struct obj_section *section, int thread)
6793 {
6794 int others = 0;
6795 struct breakpoint *b;
6796
6797 ALL_BREAKPOINTS (b)
6798 others += (user_breakpoint_p (b)
6799 && breakpoint_has_pc (b, pspace, pc, section));
6800 if (others > 0)
6801 {
6802 if (others == 1)
6803 printf_filtered (_("Note: breakpoint "));
6804 else /* if (others == ???) */
6805 printf_filtered (_("Note: breakpoints "));
6806 ALL_BREAKPOINTS (b)
6807 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6808 {
6809 others--;
6810 printf_filtered ("%d", b->number);
6811 if (b->thread == -1 && thread != -1)
6812 printf_filtered (" (all threads)");
6813 else if (b->thread != -1)
6814 printf_filtered (" (thread %d)", b->thread);
6815 printf_filtered ("%s%s ",
6816 ((b->enable_state == bp_disabled
6817 || b->enable_state == bp_call_disabled)
6818 ? " (disabled)"
6819 : ""),
6820 (others > 1) ? ","
6821 : ((others == 1) ? " and" : ""));
6822 }
6823 printf_filtered (_("also set at pc "));
6824 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6825 printf_filtered (".\n");
6826 }
6827 }
6828 \f
6829
6830 /* Return true iff it is meaningful to use the address member of
6831 BPT locations. For some breakpoint types, the locations' address members
6832 are irrelevant and it makes no sense to attempt to compare them to other
6833 addresses (or use them for any other purpose either).
6834
6835 More specifically, each of the following breakpoint types will
6836 always have a zero valued location address and we don't want to mark
6837 breakpoints of any of these types to be a duplicate of an actual
6838 breakpoint location at address zero:
6839
6840 bp_watchpoint
6841 bp_catchpoint
6842
6843 */
6844
6845 static int
6846 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6847 {
6848 enum bptype type = bpt->type;
6849
6850 return (type != bp_watchpoint && type != bp_catchpoint);
6851 }
6852
6853 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6854 true if LOC1 and LOC2 represent the same watchpoint location. */
6855
6856 static int
6857 watchpoint_locations_match (struct bp_location *loc1,
6858 struct bp_location *loc2)
6859 {
6860 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6861 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6862
6863 /* Both of them must exist. */
6864 gdb_assert (w1 != NULL);
6865 gdb_assert (w2 != NULL);
6866
6867 /* If the target can evaluate the condition expression in hardware,
6868 then we we need to insert both watchpoints even if they are at
6869 the same place. Otherwise the watchpoint will only trigger when
6870 the condition of whichever watchpoint was inserted evaluates to
6871 true, not giving a chance for GDB to check the condition of the
6872 other watchpoint. */
6873 if ((w1->cond_exp
6874 && target_can_accel_watchpoint_condition (loc1->address,
6875 loc1->length,
6876 loc1->watchpoint_type,
6877 w1->cond_exp.get ()))
6878 || (w2->cond_exp
6879 && target_can_accel_watchpoint_condition (loc2->address,
6880 loc2->length,
6881 loc2->watchpoint_type,
6882 w2->cond_exp.get ())))
6883 return 0;
6884
6885 /* Note that this checks the owner's type, not the location's. In
6886 case the target does not support read watchpoints, but does
6887 support access watchpoints, we'll have bp_read_watchpoint
6888 watchpoints with hw_access locations. Those should be considered
6889 duplicates of hw_read locations. The hw_read locations will
6890 become hw_access locations later. */
6891 return (loc1->owner->type == loc2->owner->type
6892 && loc1->pspace->aspace == loc2->pspace->aspace
6893 && loc1->address == loc2->address
6894 && loc1->length == loc2->length);
6895 }
6896
6897 /* See breakpoint.h. */
6898
6899 int
6900 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6901 const address_space *aspace2, CORE_ADDR addr2)
6902 {
6903 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6904 || aspace1 == aspace2)
6905 && addr1 == addr2);
6906 }
6907
6908 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6909 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6910 matches ASPACE2. On targets that have global breakpoints, the address
6911 space doesn't really matter. */
6912
6913 static int
6914 breakpoint_address_match_range (const address_space *aspace1,
6915 CORE_ADDR addr1,
6916 int len1, const address_space *aspace2,
6917 CORE_ADDR addr2)
6918 {
6919 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6920 || aspace1 == aspace2)
6921 && addr2 >= addr1 && addr2 < addr1 + len1);
6922 }
6923
6924 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6925 a ranged breakpoint. In most targets, a match happens only if ASPACE
6926 matches the breakpoint's address space. On targets that have global
6927 breakpoints, the address space doesn't really matter. */
6928
6929 static int
6930 breakpoint_location_address_match (struct bp_location *bl,
6931 const address_space *aspace,
6932 CORE_ADDR addr)
6933 {
6934 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6935 aspace, addr)
6936 || (bl->length
6937 && breakpoint_address_match_range (bl->pspace->aspace,
6938 bl->address, bl->length,
6939 aspace, addr)));
6940 }
6941
6942 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6943 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6944 match happens only if ASPACE matches the breakpoint's address
6945 space. On targets that have global breakpoints, the address space
6946 doesn't really matter. */
6947
6948 static int
6949 breakpoint_location_address_range_overlap (struct bp_location *bl,
6950 const address_space *aspace,
6951 CORE_ADDR addr, int len)
6952 {
6953 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6954 || bl->pspace->aspace == aspace)
6955 {
6956 int bl_len = bl->length != 0 ? bl->length : 1;
6957
6958 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6959 return 1;
6960 }
6961 return 0;
6962 }
6963
6964 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6965 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6966 true, otherwise returns false. */
6967
6968 static int
6969 tracepoint_locations_match (struct bp_location *loc1,
6970 struct bp_location *loc2)
6971 {
6972 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6973 /* Since tracepoint locations are never duplicated with others', tracepoint
6974 locations at the same address of different tracepoints are regarded as
6975 different locations. */
6976 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6977 else
6978 return 0;
6979 }
6980
6981 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6982 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6983 represent the same location. */
6984
6985 static int
6986 breakpoint_locations_match (struct bp_location *loc1,
6987 struct bp_location *loc2)
6988 {
6989 int hw_point1, hw_point2;
6990
6991 /* Both of them must not be in moribund_locations. */
6992 gdb_assert (loc1->owner != NULL);
6993 gdb_assert (loc2->owner != NULL);
6994
6995 hw_point1 = is_hardware_watchpoint (loc1->owner);
6996 hw_point2 = is_hardware_watchpoint (loc2->owner);
6997
6998 if (hw_point1 != hw_point2)
6999 return 0;
7000 else if (hw_point1)
7001 return watchpoint_locations_match (loc1, loc2);
7002 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7003 return tracepoint_locations_match (loc1, loc2);
7004 else
7005 /* We compare bp_location.length in order to cover ranged breakpoints. */
7006 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7007 loc2->pspace->aspace, loc2->address)
7008 && loc1->length == loc2->length);
7009 }
7010
7011 static void
7012 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7013 int bnum, int have_bnum)
7014 {
7015 /* The longest string possibly returned by hex_string_custom
7016 is 50 chars. These must be at least that big for safety. */
7017 char astr1[64];
7018 char astr2[64];
7019
7020 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7021 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7022 if (have_bnum)
7023 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7024 bnum, astr1, astr2);
7025 else
7026 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7027 }
7028
7029 /* Adjust a breakpoint's address to account for architectural
7030 constraints on breakpoint placement. Return the adjusted address.
7031 Note: Very few targets require this kind of adjustment. For most
7032 targets, this function is simply the identity function. */
7033
7034 static CORE_ADDR
7035 adjust_breakpoint_address (struct gdbarch *gdbarch,
7036 CORE_ADDR bpaddr, enum bptype bptype)
7037 {
7038 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7039 {
7040 /* Very few targets need any kind of breakpoint adjustment. */
7041 return bpaddr;
7042 }
7043 else if (bptype == bp_watchpoint
7044 || bptype == bp_hardware_watchpoint
7045 || bptype == bp_read_watchpoint
7046 || bptype == bp_access_watchpoint
7047 || bptype == bp_catchpoint)
7048 {
7049 /* Watchpoints and the various bp_catch_* eventpoints should not
7050 have their addresses modified. */
7051 return bpaddr;
7052 }
7053 else if (bptype == bp_single_step)
7054 {
7055 /* Single-step breakpoints should not have their addresses
7056 modified. If there's any architectural constrain that
7057 applies to this address, then it should have already been
7058 taken into account when the breakpoint was created in the
7059 first place. If we didn't do this, stepping through e.g.,
7060 Thumb-2 IT blocks would break. */
7061 return bpaddr;
7062 }
7063 else
7064 {
7065 CORE_ADDR adjusted_bpaddr;
7066
7067 /* Some targets have architectural constraints on the placement
7068 of breakpoint instructions. Obtain the adjusted address. */
7069 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7070
7071 /* An adjusted breakpoint address can significantly alter
7072 a user's expectations. Print a warning if an adjustment
7073 is required. */
7074 if (adjusted_bpaddr != bpaddr)
7075 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7076
7077 return adjusted_bpaddr;
7078 }
7079 }
7080
7081 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7082 {
7083 bp_location *loc = this;
7084
7085 gdb_assert (ops != NULL);
7086
7087 loc->ops = ops;
7088 loc->owner = owner;
7089 loc->cond_bytecode = NULL;
7090 loc->shlib_disabled = 0;
7091 loc->enabled = 1;
7092
7093 switch (owner->type)
7094 {
7095 case bp_breakpoint:
7096 case bp_single_step:
7097 case bp_until:
7098 case bp_finish:
7099 case bp_longjmp:
7100 case bp_longjmp_resume:
7101 case bp_longjmp_call_dummy:
7102 case bp_exception:
7103 case bp_exception_resume:
7104 case bp_step_resume:
7105 case bp_hp_step_resume:
7106 case bp_watchpoint_scope:
7107 case bp_call_dummy:
7108 case bp_std_terminate:
7109 case bp_shlib_event:
7110 case bp_thread_event:
7111 case bp_overlay_event:
7112 case bp_jit_event:
7113 case bp_longjmp_master:
7114 case bp_std_terminate_master:
7115 case bp_exception_master:
7116 case bp_gnu_ifunc_resolver:
7117 case bp_gnu_ifunc_resolver_return:
7118 case bp_dprintf:
7119 loc->loc_type = bp_loc_software_breakpoint;
7120 mark_breakpoint_location_modified (loc);
7121 break;
7122 case bp_hardware_breakpoint:
7123 loc->loc_type = bp_loc_hardware_breakpoint;
7124 mark_breakpoint_location_modified (loc);
7125 break;
7126 case bp_hardware_watchpoint:
7127 case bp_read_watchpoint:
7128 case bp_access_watchpoint:
7129 loc->loc_type = bp_loc_hardware_watchpoint;
7130 break;
7131 case bp_watchpoint:
7132 case bp_catchpoint:
7133 case bp_tracepoint:
7134 case bp_fast_tracepoint:
7135 case bp_static_tracepoint:
7136 loc->loc_type = bp_loc_other;
7137 break;
7138 default:
7139 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7140 }
7141
7142 loc->refc = 1;
7143 }
7144
7145 /* Allocate a struct bp_location. */
7146
7147 static struct bp_location *
7148 allocate_bp_location (struct breakpoint *bpt)
7149 {
7150 return bpt->ops->allocate_location (bpt);
7151 }
7152
7153 static void
7154 free_bp_location (struct bp_location *loc)
7155 {
7156 loc->ops->dtor (loc);
7157 delete loc;
7158 }
7159
7160 /* Increment reference count. */
7161
7162 static void
7163 incref_bp_location (struct bp_location *bl)
7164 {
7165 ++bl->refc;
7166 }
7167
7168 /* Decrement reference count. If the reference count reaches 0,
7169 destroy the bp_location. Sets *BLP to NULL. */
7170
7171 static void
7172 decref_bp_location (struct bp_location **blp)
7173 {
7174 gdb_assert ((*blp)->refc > 0);
7175
7176 if (--(*blp)->refc == 0)
7177 free_bp_location (*blp);
7178 *blp = NULL;
7179 }
7180
7181 /* Add breakpoint B at the end of the global breakpoint chain. */
7182
7183 static breakpoint *
7184 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7185 {
7186 struct breakpoint *b1;
7187 struct breakpoint *result = b.get ();
7188
7189 /* Add this breakpoint to the end of the chain so that a list of
7190 breakpoints will come out in order of increasing numbers. */
7191
7192 b1 = breakpoint_chain;
7193 if (b1 == 0)
7194 breakpoint_chain = b.release ();
7195 else
7196 {
7197 while (b1->next)
7198 b1 = b1->next;
7199 b1->next = b.release ();
7200 }
7201
7202 return result;
7203 }
7204
7205 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7206
7207 static void
7208 init_raw_breakpoint_without_location (struct breakpoint *b,
7209 struct gdbarch *gdbarch,
7210 enum bptype bptype,
7211 const struct breakpoint_ops *ops)
7212 {
7213 gdb_assert (ops != NULL);
7214
7215 b->ops = ops;
7216 b->type = bptype;
7217 b->gdbarch = gdbarch;
7218 b->language = current_language->la_language;
7219 b->input_radix = input_radix;
7220 b->related_breakpoint = b;
7221 }
7222
7223 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7224 that has type BPTYPE and has no locations as yet. */
7225
7226 static struct breakpoint *
7227 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7228 enum bptype bptype,
7229 const struct breakpoint_ops *ops)
7230 {
7231 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7232
7233 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7234 return add_to_breakpoint_chain (std::move (b));
7235 }
7236
7237 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7238 resolutions should be made as the user specified the location explicitly
7239 enough. */
7240
7241 static void
7242 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7243 {
7244 gdb_assert (loc->owner != NULL);
7245
7246 if (loc->owner->type == bp_breakpoint
7247 || loc->owner->type == bp_hardware_breakpoint
7248 || is_tracepoint (loc->owner))
7249 {
7250 int is_gnu_ifunc;
7251 const char *function_name;
7252 CORE_ADDR func_addr;
7253
7254 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7255 &func_addr, NULL, &is_gnu_ifunc);
7256
7257 if (is_gnu_ifunc && !explicit_loc)
7258 {
7259 struct breakpoint *b = loc->owner;
7260
7261 gdb_assert (loc->pspace == current_program_space);
7262 if (gnu_ifunc_resolve_name (function_name,
7263 &loc->requested_address))
7264 {
7265 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7266 loc->address = adjust_breakpoint_address (loc->gdbarch,
7267 loc->requested_address,
7268 b->type);
7269 }
7270 else if (b->type == bp_breakpoint && b->loc == loc
7271 && loc->next == NULL && b->related_breakpoint == b)
7272 {
7273 /* Create only the whole new breakpoint of this type but do not
7274 mess more complicated breakpoints with multiple locations. */
7275 b->type = bp_gnu_ifunc_resolver;
7276 /* Remember the resolver's address for use by the return
7277 breakpoint. */
7278 loc->related_address = func_addr;
7279 }
7280 }
7281
7282 if (function_name)
7283 loc->function_name = xstrdup (function_name);
7284 }
7285 }
7286
7287 /* Attempt to determine architecture of location identified by SAL. */
7288 struct gdbarch *
7289 get_sal_arch (struct symtab_and_line sal)
7290 {
7291 if (sal.section)
7292 return get_objfile_arch (sal.section->objfile);
7293 if (sal.symtab)
7294 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7295
7296 return NULL;
7297 }
7298
7299 /* Low level routine for partially initializing a breakpoint of type
7300 BPTYPE. The newly created breakpoint's address, section, source
7301 file name, and line number are provided by SAL.
7302
7303 It is expected that the caller will complete the initialization of
7304 the newly created breakpoint struct as well as output any status
7305 information regarding the creation of a new breakpoint. */
7306
7307 static void
7308 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7309 struct symtab_and_line sal, enum bptype bptype,
7310 const struct breakpoint_ops *ops)
7311 {
7312 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7313
7314 add_location_to_breakpoint (b, &sal);
7315
7316 if (bptype != bp_catchpoint)
7317 gdb_assert (sal.pspace != NULL);
7318
7319 /* Store the program space that was used to set the breakpoint,
7320 except for ordinary breakpoints, which are independent of the
7321 program space. */
7322 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7323 b->pspace = sal.pspace;
7324 }
7325
7326 /* set_raw_breakpoint is a low level routine for allocating and
7327 partially initializing a breakpoint of type BPTYPE. The newly
7328 created breakpoint's address, section, source file name, and line
7329 number are provided by SAL. The newly created and partially
7330 initialized breakpoint is added to the breakpoint chain and
7331 is also returned as the value of this function.
7332
7333 It is expected that the caller will complete the initialization of
7334 the newly created breakpoint struct as well as output any status
7335 information regarding the creation of a new breakpoint. In
7336 particular, set_raw_breakpoint does NOT set the breakpoint
7337 number! Care should be taken to not allow an error to occur
7338 prior to completing the initialization of the breakpoint. If this
7339 should happen, a bogus breakpoint will be left on the chain. */
7340
7341 struct breakpoint *
7342 set_raw_breakpoint (struct gdbarch *gdbarch,
7343 struct symtab_and_line sal, enum bptype bptype,
7344 const struct breakpoint_ops *ops)
7345 {
7346 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7347
7348 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7349 return add_to_breakpoint_chain (std::move (b));
7350 }
7351
7352 /* Call this routine when stepping and nexting to enable a breakpoint
7353 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7354 initiated the operation. */
7355
7356 void
7357 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7358 {
7359 struct breakpoint *b, *b_tmp;
7360 int thread = tp->global_num;
7361
7362 /* To avoid having to rescan all objfile symbols at every step,
7363 we maintain a list of continually-inserted but always disabled
7364 longjmp "master" breakpoints. Here, we simply create momentary
7365 clones of those and enable them for the requested thread. */
7366 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7367 if (b->pspace == current_program_space
7368 && (b->type == bp_longjmp_master
7369 || b->type == bp_exception_master))
7370 {
7371 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7372 struct breakpoint *clone;
7373
7374 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7375 after their removal. */
7376 clone = momentary_breakpoint_from_master (b, type,
7377 &momentary_breakpoint_ops, 1);
7378 clone->thread = thread;
7379 }
7380
7381 tp->initiating_frame = frame;
7382 }
7383
7384 /* Delete all longjmp breakpoints from THREAD. */
7385 void
7386 delete_longjmp_breakpoint (int thread)
7387 {
7388 struct breakpoint *b, *b_tmp;
7389
7390 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7391 if (b->type == bp_longjmp || b->type == bp_exception)
7392 {
7393 if (b->thread == thread)
7394 delete_breakpoint (b);
7395 }
7396 }
7397
7398 void
7399 delete_longjmp_breakpoint_at_next_stop (int thread)
7400 {
7401 struct breakpoint *b, *b_tmp;
7402
7403 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7404 if (b->type == bp_longjmp || b->type == bp_exception)
7405 {
7406 if (b->thread == thread)
7407 b->disposition = disp_del_at_next_stop;
7408 }
7409 }
7410
7411 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7412 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7413 pointer to any of them. Return NULL if this system cannot place longjmp
7414 breakpoints. */
7415
7416 struct breakpoint *
7417 set_longjmp_breakpoint_for_call_dummy (void)
7418 {
7419 struct breakpoint *b, *retval = NULL;
7420
7421 ALL_BREAKPOINTS (b)
7422 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7423 {
7424 struct breakpoint *new_b;
7425
7426 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7427 &momentary_breakpoint_ops,
7428 1);
7429 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7430
7431 /* Link NEW_B into the chain of RETVAL breakpoints. */
7432
7433 gdb_assert (new_b->related_breakpoint == new_b);
7434 if (retval == NULL)
7435 retval = new_b;
7436 new_b->related_breakpoint = retval;
7437 while (retval->related_breakpoint != new_b->related_breakpoint)
7438 retval = retval->related_breakpoint;
7439 retval->related_breakpoint = new_b;
7440 }
7441
7442 return retval;
7443 }
7444
7445 /* Verify all existing dummy frames and their associated breakpoints for
7446 TP. Remove those which can no longer be found in the current frame
7447 stack.
7448
7449 You should call this function only at places where it is safe to currently
7450 unwind the whole stack. Failed stack unwind would discard live dummy
7451 frames. */
7452
7453 void
7454 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7455 {
7456 struct breakpoint *b, *b_tmp;
7457
7458 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7459 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7460 {
7461 struct breakpoint *dummy_b = b->related_breakpoint;
7462
7463 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7464 dummy_b = dummy_b->related_breakpoint;
7465 if (dummy_b->type != bp_call_dummy
7466 || frame_find_by_id (dummy_b->frame_id) != NULL)
7467 continue;
7468
7469 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7470
7471 while (b->related_breakpoint != b)
7472 {
7473 if (b_tmp == b->related_breakpoint)
7474 b_tmp = b->related_breakpoint->next;
7475 delete_breakpoint (b->related_breakpoint);
7476 }
7477 delete_breakpoint (b);
7478 }
7479 }
7480
7481 void
7482 enable_overlay_breakpoints (void)
7483 {
7484 struct breakpoint *b;
7485
7486 ALL_BREAKPOINTS (b)
7487 if (b->type == bp_overlay_event)
7488 {
7489 b->enable_state = bp_enabled;
7490 update_global_location_list (UGLL_MAY_INSERT);
7491 overlay_events_enabled = 1;
7492 }
7493 }
7494
7495 void
7496 disable_overlay_breakpoints (void)
7497 {
7498 struct breakpoint *b;
7499
7500 ALL_BREAKPOINTS (b)
7501 if (b->type == bp_overlay_event)
7502 {
7503 b->enable_state = bp_disabled;
7504 update_global_location_list (UGLL_DONT_INSERT);
7505 overlay_events_enabled = 0;
7506 }
7507 }
7508
7509 /* Set an active std::terminate breakpoint for each std::terminate
7510 master breakpoint. */
7511 void
7512 set_std_terminate_breakpoint (void)
7513 {
7514 struct breakpoint *b, *b_tmp;
7515
7516 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7517 if (b->pspace == current_program_space
7518 && b->type == bp_std_terminate_master)
7519 {
7520 momentary_breakpoint_from_master (b, bp_std_terminate,
7521 &momentary_breakpoint_ops, 1);
7522 }
7523 }
7524
7525 /* Delete all the std::terminate breakpoints. */
7526 void
7527 delete_std_terminate_breakpoint (void)
7528 {
7529 struct breakpoint *b, *b_tmp;
7530
7531 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7532 if (b->type == bp_std_terminate)
7533 delete_breakpoint (b);
7534 }
7535
7536 struct breakpoint *
7537 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7538 {
7539 struct breakpoint *b;
7540
7541 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7542 &internal_breakpoint_ops);
7543
7544 b->enable_state = bp_enabled;
7545 /* location has to be used or breakpoint_re_set will delete me. */
7546 b->location = new_address_location (b->loc->address, NULL, 0);
7547
7548 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7549
7550 return b;
7551 }
7552
7553 struct lang_and_radix
7554 {
7555 enum language lang;
7556 int radix;
7557 };
7558
7559 /* Create a breakpoint for JIT code registration and unregistration. */
7560
7561 struct breakpoint *
7562 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7563 {
7564 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7565 &internal_breakpoint_ops);
7566 }
7567
7568 /* Remove JIT code registration and unregistration breakpoint(s). */
7569
7570 void
7571 remove_jit_event_breakpoints (void)
7572 {
7573 struct breakpoint *b, *b_tmp;
7574
7575 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7576 if (b->type == bp_jit_event
7577 && b->loc->pspace == current_program_space)
7578 delete_breakpoint (b);
7579 }
7580
7581 void
7582 remove_solib_event_breakpoints (void)
7583 {
7584 struct breakpoint *b, *b_tmp;
7585
7586 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7587 if (b->type == bp_shlib_event
7588 && b->loc->pspace == current_program_space)
7589 delete_breakpoint (b);
7590 }
7591
7592 /* See breakpoint.h. */
7593
7594 void
7595 remove_solib_event_breakpoints_at_next_stop (void)
7596 {
7597 struct breakpoint *b, *b_tmp;
7598
7599 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7600 if (b->type == bp_shlib_event
7601 && b->loc->pspace == current_program_space)
7602 b->disposition = disp_del_at_next_stop;
7603 }
7604
7605 /* Helper for create_solib_event_breakpoint /
7606 create_and_insert_solib_event_breakpoint. Allows specifying which
7607 INSERT_MODE to pass through to update_global_location_list. */
7608
7609 static struct breakpoint *
7610 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7611 enum ugll_insert_mode insert_mode)
7612 {
7613 struct breakpoint *b;
7614
7615 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7616 &internal_breakpoint_ops);
7617 update_global_location_list_nothrow (insert_mode);
7618 return b;
7619 }
7620
7621 struct breakpoint *
7622 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7623 {
7624 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7625 }
7626
7627 /* See breakpoint.h. */
7628
7629 struct breakpoint *
7630 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7631 {
7632 struct breakpoint *b;
7633
7634 /* Explicitly tell update_global_location_list to insert
7635 locations. */
7636 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7637 if (!b->loc->inserted)
7638 {
7639 delete_breakpoint (b);
7640 return NULL;
7641 }
7642 return b;
7643 }
7644
7645 /* Disable any breakpoints that are on code in shared libraries. Only
7646 apply to enabled breakpoints, disabled ones can just stay disabled. */
7647
7648 void
7649 disable_breakpoints_in_shlibs (void)
7650 {
7651 struct bp_location *loc, **locp_tmp;
7652
7653 ALL_BP_LOCATIONS (loc, locp_tmp)
7654 {
7655 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7656 struct breakpoint *b = loc->owner;
7657
7658 /* We apply the check to all breakpoints, including disabled for
7659 those with loc->duplicate set. This is so that when breakpoint
7660 becomes enabled, or the duplicate is removed, gdb will try to
7661 insert all breakpoints. If we don't set shlib_disabled here,
7662 we'll try to insert those breakpoints and fail. */
7663 if (((b->type == bp_breakpoint)
7664 || (b->type == bp_jit_event)
7665 || (b->type == bp_hardware_breakpoint)
7666 || (is_tracepoint (b)))
7667 && loc->pspace == current_program_space
7668 && !loc->shlib_disabled
7669 && solib_name_from_address (loc->pspace, loc->address)
7670 )
7671 {
7672 loc->shlib_disabled = 1;
7673 }
7674 }
7675 }
7676
7677 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7678 notification of unloaded_shlib. Only apply to enabled breakpoints,
7679 disabled ones can just stay disabled. */
7680
7681 static void
7682 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7683 {
7684 struct bp_location *loc, **locp_tmp;
7685 int disabled_shlib_breaks = 0;
7686
7687 ALL_BP_LOCATIONS (loc, locp_tmp)
7688 {
7689 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7690 struct breakpoint *b = loc->owner;
7691
7692 if (solib->pspace == loc->pspace
7693 && !loc->shlib_disabled
7694 && (((b->type == bp_breakpoint
7695 || b->type == bp_jit_event
7696 || b->type == bp_hardware_breakpoint)
7697 && (loc->loc_type == bp_loc_hardware_breakpoint
7698 || loc->loc_type == bp_loc_software_breakpoint))
7699 || is_tracepoint (b))
7700 && solib_contains_address_p (solib, loc->address))
7701 {
7702 loc->shlib_disabled = 1;
7703 /* At this point, we cannot rely on remove_breakpoint
7704 succeeding so we must mark the breakpoint as not inserted
7705 to prevent future errors occurring in remove_breakpoints. */
7706 loc->inserted = 0;
7707
7708 /* This may cause duplicate notifications for the same breakpoint. */
7709 observer_notify_breakpoint_modified (b);
7710
7711 if (!disabled_shlib_breaks)
7712 {
7713 target_terminal::ours_for_output ();
7714 warning (_("Temporarily disabling breakpoints "
7715 "for unloaded shared library \"%s\""),
7716 solib->so_name);
7717 }
7718 disabled_shlib_breaks = 1;
7719 }
7720 }
7721 }
7722
7723 /* Disable any breakpoints and tracepoints in OBJFILE upon
7724 notification of free_objfile. Only apply to enabled breakpoints,
7725 disabled ones can just stay disabled. */
7726
7727 static void
7728 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7729 {
7730 struct breakpoint *b;
7731
7732 if (objfile == NULL)
7733 return;
7734
7735 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7736 managed by the user with add-symbol-file/remove-symbol-file.
7737 Similarly to how breakpoints in shared libraries are handled in
7738 response to "nosharedlibrary", mark breakpoints in such modules
7739 shlib_disabled so they end up uninserted on the next global
7740 location list update. Shared libraries not loaded by the user
7741 aren't handled here -- they're already handled in
7742 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7743 solib_unloaded observer. We skip objfiles that are not
7744 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7745 main objfile). */
7746 if ((objfile->flags & OBJF_SHARED) == 0
7747 || (objfile->flags & OBJF_USERLOADED) == 0)
7748 return;
7749
7750 ALL_BREAKPOINTS (b)
7751 {
7752 struct bp_location *loc;
7753 int bp_modified = 0;
7754
7755 if (!is_breakpoint (b) && !is_tracepoint (b))
7756 continue;
7757
7758 for (loc = b->loc; loc != NULL; loc = loc->next)
7759 {
7760 CORE_ADDR loc_addr = loc->address;
7761
7762 if (loc->loc_type != bp_loc_hardware_breakpoint
7763 && loc->loc_type != bp_loc_software_breakpoint)
7764 continue;
7765
7766 if (loc->shlib_disabled != 0)
7767 continue;
7768
7769 if (objfile->pspace != loc->pspace)
7770 continue;
7771
7772 if (loc->loc_type != bp_loc_hardware_breakpoint
7773 && loc->loc_type != bp_loc_software_breakpoint)
7774 continue;
7775
7776 if (is_addr_in_objfile (loc_addr, objfile))
7777 {
7778 loc->shlib_disabled = 1;
7779 /* At this point, we don't know whether the object was
7780 unmapped from the inferior or not, so leave the
7781 inserted flag alone. We'll handle failure to
7782 uninsert quietly, in case the object was indeed
7783 unmapped. */
7784
7785 mark_breakpoint_location_modified (loc);
7786
7787 bp_modified = 1;
7788 }
7789 }
7790
7791 if (bp_modified)
7792 observer_notify_breakpoint_modified (b);
7793 }
7794 }
7795
7796 /* FORK & VFORK catchpoints. */
7797
7798 /* An instance of this type is used to represent a fork or vfork
7799 catchpoint. A breakpoint is really of this type iff its ops pointer points
7800 to CATCH_FORK_BREAKPOINT_OPS. */
7801
7802 struct fork_catchpoint : public breakpoint
7803 {
7804 /* Process id of a child process whose forking triggered this
7805 catchpoint. This field is only valid immediately after this
7806 catchpoint has triggered. */
7807 ptid_t forked_inferior_pid;
7808 };
7809
7810 /* Implement the "insert" breakpoint_ops method for fork
7811 catchpoints. */
7812
7813 static int
7814 insert_catch_fork (struct bp_location *bl)
7815 {
7816 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7817 }
7818
7819 /* Implement the "remove" breakpoint_ops method for fork
7820 catchpoints. */
7821
7822 static int
7823 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7824 {
7825 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7826 }
7827
7828 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7829 catchpoints. */
7830
7831 static int
7832 breakpoint_hit_catch_fork (const struct bp_location *bl,
7833 const address_space *aspace, CORE_ADDR bp_addr,
7834 const struct target_waitstatus *ws)
7835 {
7836 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7837
7838 if (ws->kind != TARGET_WAITKIND_FORKED)
7839 return 0;
7840
7841 c->forked_inferior_pid = ws->value.related_pid;
7842 return 1;
7843 }
7844
7845 /* Implement the "print_it" breakpoint_ops method for fork
7846 catchpoints. */
7847
7848 static enum print_stop_action
7849 print_it_catch_fork (bpstat bs)
7850 {
7851 struct ui_out *uiout = current_uiout;
7852 struct breakpoint *b = bs->breakpoint_at;
7853 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7854
7855 annotate_catchpoint (b->number);
7856 maybe_print_thread_hit_breakpoint (uiout);
7857 if (b->disposition == disp_del)
7858 uiout->text ("Temporary catchpoint ");
7859 else
7860 uiout->text ("Catchpoint ");
7861 if (uiout->is_mi_like_p ())
7862 {
7863 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7864 uiout->field_string ("disp", bpdisp_text (b->disposition));
7865 }
7866 uiout->field_int ("bkptno", b->number);
7867 uiout->text (" (forked process ");
7868 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7869 uiout->text ("), ");
7870 return PRINT_SRC_AND_LOC;
7871 }
7872
7873 /* Implement the "print_one" breakpoint_ops method for fork
7874 catchpoints. */
7875
7876 static void
7877 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7878 {
7879 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7880 struct value_print_options opts;
7881 struct ui_out *uiout = current_uiout;
7882
7883 get_user_print_options (&opts);
7884
7885 /* Field 4, the address, is omitted (which makes the columns not
7886 line up too nicely with the headers, but the effect is relatively
7887 readable). */
7888 if (opts.addressprint)
7889 uiout->field_skip ("addr");
7890 annotate_field (5);
7891 uiout->text ("fork");
7892 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7893 {
7894 uiout->text (", process ");
7895 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7896 uiout->spaces (1);
7897 }
7898
7899 if (uiout->is_mi_like_p ())
7900 uiout->field_string ("catch-type", "fork");
7901 }
7902
7903 /* Implement the "print_mention" breakpoint_ops method for fork
7904 catchpoints. */
7905
7906 static void
7907 print_mention_catch_fork (struct breakpoint *b)
7908 {
7909 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7910 }
7911
7912 /* Implement the "print_recreate" breakpoint_ops method for fork
7913 catchpoints. */
7914
7915 static void
7916 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7917 {
7918 fprintf_unfiltered (fp, "catch fork");
7919 print_recreate_thread (b, fp);
7920 }
7921
7922 /* The breakpoint_ops structure to be used in fork catchpoints. */
7923
7924 static struct breakpoint_ops catch_fork_breakpoint_ops;
7925
7926 /* Implement the "insert" breakpoint_ops method for vfork
7927 catchpoints. */
7928
7929 static int
7930 insert_catch_vfork (struct bp_location *bl)
7931 {
7932 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7933 }
7934
7935 /* Implement the "remove" breakpoint_ops method for vfork
7936 catchpoints. */
7937
7938 static int
7939 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7940 {
7941 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7942 }
7943
7944 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7945 catchpoints. */
7946
7947 static int
7948 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7949 const address_space *aspace, CORE_ADDR bp_addr,
7950 const struct target_waitstatus *ws)
7951 {
7952 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7953
7954 if (ws->kind != TARGET_WAITKIND_VFORKED)
7955 return 0;
7956
7957 c->forked_inferior_pid = ws->value.related_pid;
7958 return 1;
7959 }
7960
7961 /* Implement the "print_it" breakpoint_ops method for vfork
7962 catchpoints. */
7963
7964 static enum print_stop_action
7965 print_it_catch_vfork (bpstat bs)
7966 {
7967 struct ui_out *uiout = current_uiout;
7968 struct breakpoint *b = bs->breakpoint_at;
7969 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7970
7971 annotate_catchpoint (b->number);
7972 maybe_print_thread_hit_breakpoint (uiout);
7973 if (b->disposition == disp_del)
7974 uiout->text ("Temporary catchpoint ");
7975 else
7976 uiout->text ("Catchpoint ");
7977 if (uiout->is_mi_like_p ())
7978 {
7979 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7980 uiout->field_string ("disp", bpdisp_text (b->disposition));
7981 }
7982 uiout->field_int ("bkptno", b->number);
7983 uiout->text (" (vforked process ");
7984 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7985 uiout->text ("), ");
7986 return PRINT_SRC_AND_LOC;
7987 }
7988
7989 /* Implement the "print_one" breakpoint_ops method for vfork
7990 catchpoints. */
7991
7992 static void
7993 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7994 {
7995 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7996 struct value_print_options opts;
7997 struct ui_out *uiout = current_uiout;
7998
7999 get_user_print_options (&opts);
8000 /* Field 4, the address, is omitted (which makes the columns not
8001 line up too nicely with the headers, but the effect is relatively
8002 readable). */
8003 if (opts.addressprint)
8004 uiout->field_skip ("addr");
8005 annotate_field (5);
8006 uiout->text ("vfork");
8007 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8008 {
8009 uiout->text (", process ");
8010 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8011 uiout->spaces (1);
8012 }
8013
8014 if (uiout->is_mi_like_p ())
8015 uiout->field_string ("catch-type", "vfork");
8016 }
8017
8018 /* Implement the "print_mention" breakpoint_ops method for vfork
8019 catchpoints. */
8020
8021 static void
8022 print_mention_catch_vfork (struct breakpoint *b)
8023 {
8024 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8025 }
8026
8027 /* Implement the "print_recreate" breakpoint_ops method for vfork
8028 catchpoints. */
8029
8030 static void
8031 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8032 {
8033 fprintf_unfiltered (fp, "catch vfork");
8034 print_recreate_thread (b, fp);
8035 }
8036
8037 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8038
8039 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8040
8041 /* An instance of this type is used to represent an solib catchpoint.
8042 A breakpoint is really of this type iff its ops pointer points to
8043 CATCH_SOLIB_BREAKPOINT_OPS. */
8044
8045 struct solib_catchpoint : public breakpoint
8046 {
8047 ~solib_catchpoint () override;
8048
8049 /* True for "catch load", false for "catch unload". */
8050 unsigned char is_load;
8051
8052 /* Regular expression to match, if any. COMPILED is only valid when
8053 REGEX is non-NULL. */
8054 char *regex;
8055 std::unique_ptr<compiled_regex> compiled;
8056 };
8057
8058 solib_catchpoint::~solib_catchpoint ()
8059 {
8060 xfree (this->regex);
8061 }
8062
8063 static int
8064 insert_catch_solib (struct bp_location *ignore)
8065 {
8066 return 0;
8067 }
8068
8069 static int
8070 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8071 {
8072 return 0;
8073 }
8074
8075 static int
8076 breakpoint_hit_catch_solib (const struct bp_location *bl,
8077 const address_space *aspace,
8078 CORE_ADDR bp_addr,
8079 const struct target_waitstatus *ws)
8080 {
8081 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8082 struct breakpoint *other;
8083
8084 if (ws->kind == TARGET_WAITKIND_LOADED)
8085 return 1;
8086
8087 ALL_BREAKPOINTS (other)
8088 {
8089 struct bp_location *other_bl;
8090
8091 if (other == bl->owner)
8092 continue;
8093
8094 if (other->type != bp_shlib_event)
8095 continue;
8096
8097 if (self->pspace != NULL && other->pspace != self->pspace)
8098 continue;
8099
8100 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8101 {
8102 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8103 return 1;
8104 }
8105 }
8106
8107 return 0;
8108 }
8109
8110 static void
8111 check_status_catch_solib (struct bpstats *bs)
8112 {
8113 struct solib_catchpoint *self
8114 = (struct solib_catchpoint *) bs->breakpoint_at;
8115 int ix;
8116
8117 if (self->is_load)
8118 {
8119 struct so_list *iter;
8120
8121 for (ix = 0;
8122 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8123 ix, iter);
8124 ++ix)
8125 {
8126 if (!self->regex
8127 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8128 return;
8129 }
8130 }
8131 else
8132 {
8133 char *iter;
8134
8135 for (ix = 0;
8136 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8137 ix, iter);
8138 ++ix)
8139 {
8140 if (!self->regex
8141 || self->compiled->exec (iter, 0, NULL, 0) == 0)
8142 return;
8143 }
8144 }
8145
8146 bs->stop = 0;
8147 bs->print_it = print_it_noop;
8148 }
8149
8150 static enum print_stop_action
8151 print_it_catch_solib (bpstat bs)
8152 {
8153 struct breakpoint *b = bs->breakpoint_at;
8154 struct ui_out *uiout = current_uiout;
8155
8156 annotate_catchpoint (b->number);
8157 maybe_print_thread_hit_breakpoint (uiout);
8158 if (b->disposition == disp_del)
8159 uiout->text ("Temporary catchpoint ");
8160 else
8161 uiout->text ("Catchpoint ");
8162 uiout->field_int ("bkptno", b->number);
8163 uiout->text ("\n");
8164 if (uiout->is_mi_like_p ())
8165 uiout->field_string ("disp", bpdisp_text (b->disposition));
8166 print_solib_event (1);
8167 return PRINT_SRC_AND_LOC;
8168 }
8169
8170 static void
8171 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8172 {
8173 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8174 struct value_print_options opts;
8175 struct ui_out *uiout = current_uiout;
8176 char *msg;
8177
8178 get_user_print_options (&opts);
8179 /* Field 4, the address, is omitted (which makes the columns not
8180 line up too nicely with the headers, but the effect is relatively
8181 readable). */
8182 if (opts.addressprint)
8183 {
8184 annotate_field (4);
8185 uiout->field_skip ("addr");
8186 }
8187
8188 annotate_field (5);
8189 if (self->is_load)
8190 {
8191 if (self->regex)
8192 msg = xstrprintf (_("load of library matching %s"), self->regex);
8193 else
8194 msg = xstrdup (_("load of library"));
8195 }
8196 else
8197 {
8198 if (self->regex)
8199 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8200 else
8201 msg = xstrdup (_("unload of library"));
8202 }
8203 uiout->field_string ("what", msg);
8204 xfree (msg);
8205
8206 if (uiout->is_mi_like_p ())
8207 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8208 }
8209
8210 static void
8211 print_mention_catch_solib (struct breakpoint *b)
8212 {
8213 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8214
8215 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8216 self->is_load ? "load" : "unload");
8217 }
8218
8219 static void
8220 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8221 {
8222 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8223
8224 fprintf_unfiltered (fp, "%s %s",
8225 b->disposition == disp_del ? "tcatch" : "catch",
8226 self->is_load ? "load" : "unload");
8227 if (self->regex)
8228 fprintf_unfiltered (fp, " %s", self->regex);
8229 fprintf_unfiltered (fp, "\n");
8230 }
8231
8232 static struct breakpoint_ops catch_solib_breakpoint_ops;
8233
8234 /* Shared helper function (MI and CLI) for creating and installing
8235 a shared object event catchpoint. If IS_LOAD is non-zero then
8236 the events to be caught are load events, otherwise they are
8237 unload events. If IS_TEMP is non-zero the catchpoint is a
8238 temporary one. If ENABLED is non-zero the catchpoint is
8239 created in an enabled state. */
8240
8241 void
8242 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8243 {
8244 struct gdbarch *gdbarch = get_current_arch ();
8245
8246 if (!arg)
8247 arg = "";
8248 arg = skip_spaces (arg);
8249
8250 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8251
8252 if (*arg != '\0')
8253 {
8254 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8255 _("Invalid regexp")));
8256 c->regex = xstrdup (arg);
8257 }
8258
8259 c->is_load = is_load;
8260 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8261 &catch_solib_breakpoint_ops);
8262
8263 c->enable_state = enabled ? bp_enabled : bp_disabled;
8264
8265 install_breakpoint (0, std::move (c), 1);
8266 }
8267
8268 /* A helper function that does all the work for "catch load" and
8269 "catch unload". */
8270
8271 static void
8272 catch_load_or_unload (char *arg, int from_tty, int is_load,
8273 struct cmd_list_element *command)
8274 {
8275 int tempflag;
8276 const int enabled = 1;
8277
8278 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8279
8280 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8281 }
8282
8283 static void
8284 catch_load_command_1 (char *arg, int from_tty,
8285 struct cmd_list_element *command)
8286 {
8287 catch_load_or_unload (arg, from_tty, 1, command);
8288 }
8289
8290 static void
8291 catch_unload_command_1 (char *arg, int from_tty,
8292 struct cmd_list_element *command)
8293 {
8294 catch_load_or_unload (arg, from_tty, 0, command);
8295 }
8296
8297 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8298 is non-zero, then make the breakpoint temporary. If COND_STRING is
8299 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8300 the breakpoint_ops structure associated to the catchpoint. */
8301
8302 void
8303 init_catchpoint (struct breakpoint *b,
8304 struct gdbarch *gdbarch, int tempflag,
8305 const char *cond_string,
8306 const struct breakpoint_ops *ops)
8307 {
8308 symtab_and_line sal;
8309 sal.pspace = current_program_space;
8310
8311 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8312
8313 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8314 b->disposition = tempflag ? disp_del : disp_donttouch;
8315 }
8316
8317 void
8318 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8319 {
8320 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8321 set_breakpoint_number (internal, b);
8322 if (is_tracepoint (b))
8323 set_tracepoint_count (breakpoint_count);
8324 if (!internal)
8325 mention (b);
8326 observer_notify_breakpoint_created (b);
8327
8328 if (update_gll)
8329 update_global_location_list (UGLL_MAY_INSERT);
8330 }
8331
8332 static void
8333 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8334 int tempflag, const char *cond_string,
8335 const struct breakpoint_ops *ops)
8336 {
8337 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8338
8339 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8340
8341 c->forked_inferior_pid = null_ptid;
8342
8343 install_breakpoint (0, std::move (c), 1);
8344 }
8345
8346 /* Exec catchpoints. */
8347
8348 /* An instance of this type is used to represent an exec catchpoint.
8349 A breakpoint is really of this type iff its ops pointer points to
8350 CATCH_EXEC_BREAKPOINT_OPS. */
8351
8352 struct exec_catchpoint : public breakpoint
8353 {
8354 ~exec_catchpoint () override;
8355
8356 /* Filename of a program whose exec triggered this catchpoint.
8357 This field is only valid immediately after this catchpoint has
8358 triggered. */
8359 char *exec_pathname;
8360 };
8361
8362 /* Exec catchpoint destructor. */
8363
8364 exec_catchpoint::~exec_catchpoint ()
8365 {
8366 xfree (this->exec_pathname);
8367 }
8368
8369 static int
8370 insert_catch_exec (struct bp_location *bl)
8371 {
8372 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8373 }
8374
8375 static int
8376 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8377 {
8378 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8379 }
8380
8381 static int
8382 breakpoint_hit_catch_exec (const struct bp_location *bl,
8383 const address_space *aspace, CORE_ADDR bp_addr,
8384 const struct target_waitstatus *ws)
8385 {
8386 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8387
8388 if (ws->kind != TARGET_WAITKIND_EXECD)
8389 return 0;
8390
8391 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8392 return 1;
8393 }
8394
8395 static enum print_stop_action
8396 print_it_catch_exec (bpstat bs)
8397 {
8398 struct ui_out *uiout = current_uiout;
8399 struct breakpoint *b = bs->breakpoint_at;
8400 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8401
8402 annotate_catchpoint (b->number);
8403 maybe_print_thread_hit_breakpoint (uiout);
8404 if (b->disposition == disp_del)
8405 uiout->text ("Temporary catchpoint ");
8406 else
8407 uiout->text ("Catchpoint ");
8408 if (uiout->is_mi_like_p ())
8409 {
8410 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8411 uiout->field_string ("disp", bpdisp_text (b->disposition));
8412 }
8413 uiout->field_int ("bkptno", b->number);
8414 uiout->text (" (exec'd ");
8415 uiout->field_string ("new-exec", c->exec_pathname);
8416 uiout->text ("), ");
8417
8418 return PRINT_SRC_AND_LOC;
8419 }
8420
8421 static void
8422 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8423 {
8424 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8425 struct value_print_options opts;
8426 struct ui_out *uiout = current_uiout;
8427
8428 get_user_print_options (&opts);
8429
8430 /* Field 4, the address, is omitted (which makes the columns
8431 not line up too nicely with the headers, but the effect
8432 is relatively readable). */
8433 if (opts.addressprint)
8434 uiout->field_skip ("addr");
8435 annotate_field (5);
8436 uiout->text ("exec");
8437 if (c->exec_pathname != NULL)
8438 {
8439 uiout->text (", program \"");
8440 uiout->field_string ("what", c->exec_pathname);
8441 uiout->text ("\" ");
8442 }
8443
8444 if (uiout->is_mi_like_p ())
8445 uiout->field_string ("catch-type", "exec");
8446 }
8447
8448 static void
8449 print_mention_catch_exec (struct breakpoint *b)
8450 {
8451 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8452 }
8453
8454 /* Implement the "print_recreate" breakpoint_ops method for exec
8455 catchpoints. */
8456
8457 static void
8458 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8459 {
8460 fprintf_unfiltered (fp, "catch exec");
8461 print_recreate_thread (b, fp);
8462 }
8463
8464 static struct breakpoint_ops catch_exec_breakpoint_ops;
8465
8466 static int
8467 hw_breakpoint_used_count (void)
8468 {
8469 int i = 0;
8470 struct breakpoint *b;
8471 struct bp_location *bl;
8472
8473 ALL_BREAKPOINTS (b)
8474 {
8475 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8476 for (bl = b->loc; bl; bl = bl->next)
8477 {
8478 /* Special types of hardware breakpoints may use more than
8479 one register. */
8480 i += b->ops->resources_needed (bl);
8481 }
8482 }
8483
8484 return i;
8485 }
8486
8487 /* Returns the resources B would use if it were a hardware
8488 watchpoint. */
8489
8490 static int
8491 hw_watchpoint_use_count (struct breakpoint *b)
8492 {
8493 int i = 0;
8494 struct bp_location *bl;
8495
8496 if (!breakpoint_enabled (b))
8497 return 0;
8498
8499 for (bl = b->loc; bl; bl = bl->next)
8500 {
8501 /* Special types of hardware watchpoints may use more than
8502 one register. */
8503 i += b->ops->resources_needed (bl);
8504 }
8505
8506 return i;
8507 }
8508
8509 /* Returns the sum the used resources of all hardware watchpoints of
8510 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8511 the sum of the used resources of all hardware watchpoints of other
8512 types _not_ TYPE. */
8513
8514 static int
8515 hw_watchpoint_used_count_others (struct breakpoint *except,
8516 enum bptype type, int *other_type_used)
8517 {
8518 int i = 0;
8519 struct breakpoint *b;
8520
8521 *other_type_used = 0;
8522 ALL_BREAKPOINTS (b)
8523 {
8524 if (b == except)
8525 continue;
8526 if (!breakpoint_enabled (b))
8527 continue;
8528
8529 if (b->type == type)
8530 i += hw_watchpoint_use_count (b);
8531 else if (is_hardware_watchpoint (b))
8532 *other_type_used = 1;
8533 }
8534
8535 return i;
8536 }
8537
8538 void
8539 disable_watchpoints_before_interactive_call_start (void)
8540 {
8541 struct breakpoint *b;
8542
8543 ALL_BREAKPOINTS (b)
8544 {
8545 if (is_watchpoint (b) && breakpoint_enabled (b))
8546 {
8547 b->enable_state = bp_call_disabled;
8548 update_global_location_list (UGLL_DONT_INSERT);
8549 }
8550 }
8551 }
8552
8553 void
8554 enable_watchpoints_after_interactive_call_stop (void)
8555 {
8556 struct breakpoint *b;
8557
8558 ALL_BREAKPOINTS (b)
8559 {
8560 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8561 {
8562 b->enable_state = bp_enabled;
8563 update_global_location_list (UGLL_MAY_INSERT);
8564 }
8565 }
8566 }
8567
8568 void
8569 disable_breakpoints_before_startup (void)
8570 {
8571 current_program_space->executing_startup = 1;
8572 update_global_location_list (UGLL_DONT_INSERT);
8573 }
8574
8575 void
8576 enable_breakpoints_after_startup (void)
8577 {
8578 current_program_space->executing_startup = 0;
8579 breakpoint_re_set ();
8580 }
8581
8582 /* Create a new single-step breakpoint for thread THREAD, with no
8583 locations. */
8584
8585 static struct breakpoint *
8586 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8587 {
8588 std::unique_ptr<breakpoint> b (new breakpoint ());
8589
8590 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8591 &momentary_breakpoint_ops);
8592
8593 b->disposition = disp_donttouch;
8594 b->frame_id = null_frame_id;
8595
8596 b->thread = thread;
8597 gdb_assert (b->thread != 0);
8598
8599 return add_to_breakpoint_chain (std::move (b));
8600 }
8601
8602 /* Set a momentary breakpoint of type TYPE at address specified by
8603 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8604 frame. */
8605
8606 struct breakpoint *
8607 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8608 struct frame_id frame_id, enum bptype type)
8609 {
8610 struct breakpoint *b;
8611
8612 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8613 tail-called one. */
8614 gdb_assert (!frame_id_artificial_p (frame_id));
8615
8616 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8617 b->enable_state = bp_enabled;
8618 b->disposition = disp_donttouch;
8619 b->frame_id = frame_id;
8620
8621 /* If we're debugging a multi-threaded program, then we want
8622 momentary breakpoints to be active in only a single thread of
8623 control. */
8624 if (in_thread_list (inferior_ptid))
8625 b->thread = ptid_to_global_thread_id (inferior_ptid);
8626
8627 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8628
8629 return b;
8630 }
8631
8632 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8633 The new breakpoint will have type TYPE, use OPS as its
8634 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8635
8636 static struct breakpoint *
8637 momentary_breakpoint_from_master (struct breakpoint *orig,
8638 enum bptype type,
8639 const struct breakpoint_ops *ops,
8640 int loc_enabled)
8641 {
8642 struct breakpoint *copy;
8643
8644 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8645 copy->loc = allocate_bp_location (copy);
8646 set_breakpoint_location_function (copy->loc, 1);
8647
8648 copy->loc->gdbarch = orig->loc->gdbarch;
8649 copy->loc->requested_address = orig->loc->requested_address;
8650 copy->loc->address = orig->loc->address;
8651 copy->loc->section = orig->loc->section;
8652 copy->loc->pspace = orig->loc->pspace;
8653 copy->loc->probe = orig->loc->probe;
8654 copy->loc->line_number = orig->loc->line_number;
8655 copy->loc->symtab = orig->loc->symtab;
8656 copy->loc->enabled = loc_enabled;
8657 copy->frame_id = orig->frame_id;
8658 copy->thread = orig->thread;
8659 copy->pspace = orig->pspace;
8660
8661 copy->enable_state = bp_enabled;
8662 copy->disposition = disp_donttouch;
8663 copy->number = internal_breakpoint_number--;
8664
8665 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8666 return copy;
8667 }
8668
8669 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8670 ORIG is NULL. */
8671
8672 struct breakpoint *
8673 clone_momentary_breakpoint (struct breakpoint *orig)
8674 {
8675 /* If there's nothing to clone, then return nothing. */
8676 if (orig == NULL)
8677 return NULL;
8678
8679 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8680 }
8681
8682 struct breakpoint *
8683 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8684 enum bptype type)
8685 {
8686 struct symtab_and_line sal;
8687
8688 sal = find_pc_line (pc, 0);
8689 sal.pc = pc;
8690 sal.section = find_pc_overlay (pc);
8691 sal.explicit_pc = 1;
8692
8693 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8694 }
8695 \f
8696
8697 /* Tell the user we have just set a breakpoint B. */
8698
8699 static void
8700 mention (struct breakpoint *b)
8701 {
8702 b->ops->print_mention (b);
8703 if (current_uiout->is_mi_like_p ())
8704 return;
8705 printf_filtered ("\n");
8706 }
8707 \f
8708
8709 static int bp_loc_is_permanent (struct bp_location *loc);
8710
8711 static struct bp_location *
8712 add_location_to_breakpoint (struct breakpoint *b,
8713 const struct symtab_and_line *sal)
8714 {
8715 struct bp_location *loc, **tmp;
8716 CORE_ADDR adjusted_address;
8717 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8718
8719 if (loc_gdbarch == NULL)
8720 loc_gdbarch = b->gdbarch;
8721
8722 /* Adjust the breakpoint's address prior to allocating a location.
8723 Once we call allocate_bp_location(), that mostly uninitialized
8724 location will be placed on the location chain. Adjustment of the
8725 breakpoint may cause target_read_memory() to be called and we do
8726 not want its scan of the location chain to find a breakpoint and
8727 location that's only been partially initialized. */
8728 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8729 sal->pc, b->type);
8730
8731 /* Sort the locations by their ADDRESS. */
8732 loc = allocate_bp_location (b);
8733 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8734 tmp = &((*tmp)->next))
8735 ;
8736 loc->next = *tmp;
8737 *tmp = loc;
8738
8739 loc->requested_address = sal->pc;
8740 loc->address = adjusted_address;
8741 loc->pspace = sal->pspace;
8742 loc->probe.probe = sal->probe;
8743 loc->probe.objfile = sal->objfile;
8744 gdb_assert (loc->pspace != NULL);
8745 loc->section = sal->section;
8746 loc->gdbarch = loc_gdbarch;
8747 loc->line_number = sal->line;
8748 loc->symtab = sal->symtab;
8749 loc->symbol = sal->symbol;
8750
8751 set_breakpoint_location_function (loc,
8752 sal->explicit_pc || sal->explicit_line);
8753
8754 /* While by definition, permanent breakpoints are already present in the
8755 code, we don't mark the location as inserted. Normally one would expect
8756 that GDB could rely on that breakpoint instruction to stop the program,
8757 thus removing the need to insert its own breakpoint, except that executing
8758 the breakpoint instruction can kill the target instead of reporting a
8759 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8760 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8761 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8762 breakpoint be inserted normally results in QEMU knowing about the GDB
8763 breakpoint, and thus trap before the breakpoint instruction is executed.
8764 (If GDB later needs to continue execution past the permanent breakpoint,
8765 it manually increments the PC, thus avoiding executing the breakpoint
8766 instruction.) */
8767 if (bp_loc_is_permanent (loc))
8768 loc->permanent = 1;
8769
8770 return loc;
8771 }
8772 \f
8773
8774 /* See breakpoint.h. */
8775
8776 int
8777 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8778 {
8779 int len;
8780 CORE_ADDR addr;
8781 const gdb_byte *bpoint;
8782 gdb_byte *target_mem;
8783
8784 addr = address;
8785 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8786
8787 /* Software breakpoints unsupported? */
8788 if (bpoint == NULL)
8789 return 0;
8790
8791 target_mem = (gdb_byte *) alloca (len);
8792
8793 /* Enable the automatic memory restoration from breakpoints while
8794 we read the memory. Otherwise we could say about our temporary
8795 breakpoints they are permanent. */
8796 scoped_restore restore_memory
8797 = make_scoped_restore_show_memory_breakpoints (0);
8798
8799 if (target_read_memory (address, target_mem, len) == 0
8800 && memcmp (target_mem, bpoint, len) == 0)
8801 return 1;
8802
8803 return 0;
8804 }
8805
8806 /* Return 1 if LOC is pointing to a permanent breakpoint,
8807 return 0 otherwise. */
8808
8809 static int
8810 bp_loc_is_permanent (struct bp_location *loc)
8811 {
8812 gdb_assert (loc != NULL);
8813
8814 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8815 attempt to read from the addresses the locations of these breakpoint types
8816 point to. program_breakpoint_here_p, below, will attempt to read
8817 memory. */
8818 if (!breakpoint_address_is_meaningful (loc->owner))
8819 return 0;
8820
8821 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8822 switch_to_program_space_and_thread (loc->pspace);
8823 return program_breakpoint_here_p (loc->gdbarch, loc->address);
8824 }
8825
8826 /* Build a command list for the dprintf corresponding to the current
8827 settings of the dprintf style options. */
8828
8829 static void
8830 update_dprintf_command_list (struct breakpoint *b)
8831 {
8832 char *dprintf_args = b->extra_string;
8833 char *printf_line = NULL;
8834
8835 if (!dprintf_args)
8836 return;
8837
8838 dprintf_args = skip_spaces (dprintf_args);
8839
8840 /* Allow a comma, as it may have terminated a location, but don't
8841 insist on it. */
8842 if (*dprintf_args == ',')
8843 ++dprintf_args;
8844 dprintf_args = skip_spaces (dprintf_args);
8845
8846 if (*dprintf_args != '"')
8847 error (_("Bad format string, missing '\"'."));
8848
8849 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8850 printf_line = xstrprintf ("printf %s", dprintf_args);
8851 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8852 {
8853 if (!dprintf_function)
8854 error (_("No function supplied for dprintf call"));
8855
8856 if (dprintf_channel && strlen (dprintf_channel) > 0)
8857 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8858 dprintf_function,
8859 dprintf_channel,
8860 dprintf_args);
8861 else
8862 printf_line = xstrprintf ("call (void) %s (%s)",
8863 dprintf_function,
8864 dprintf_args);
8865 }
8866 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8867 {
8868 if (target_can_run_breakpoint_commands ())
8869 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8870 else
8871 {
8872 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8873 printf_line = xstrprintf ("printf %s", dprintf_args);
8874 }
8875 }
8876 else
8877 internal_error (__FILE__, __LINE__,
8878 _("Invalid dprintf style."));
8879
8880 gdb_assert (printf_line != NULL);
8881 /* Manufacture a printf sequence. */
8882 {
8883 struct command_line *printf_cmd_line = XNEW (struct command_line);
8884
8885 printf_cmd_line->control_type = simple_control;
8886 printf_cmd_line->body_count = 0;
8887 printf_cmd_line->body_list = NULL;
8888 printf_cmd_line->next = NULL;
8889 printf_cmd_line->line = printf_line;
8890
8891 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
8892 }
8893 }
8894
8895 /* Update all dprintf commands, making their command lists reflect
8896 current style settings. */
8897
8898 static void
8899 update_dprintf_commands (char *args, int from_tty,
8900 struct cmd_list_element *c)
8901 {
8902 struct breakpoint *b;
8903
8904 ALL_BREAKPOINTS (b)
8905 {
8906 if (b->type == bp_dprintf)
8907 update_dprintf_command_list (b);
8908 }
8909 }
8910
8911 /* Create a breakpoint with SAL as location. Use LOCATION
8912 as a description of the location, and COND_STRING
8913 as condition expression. If LOCATION is NULL then create an
8914 "address location" from the address in the SAL. */
8915
8916 static void
8917 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8918 gdb::array_view<const symtab_and_line> sals,
8919 event_location_up &&location,
8920 gdb::unique_xmalloc_ptr<char> filter,
8921 gdb::unique_xmalloc_ptr<char> cond_string,
8922 gdb::unique_xmalloc_ptr<char> extra_string,
8923 enum bptype type, enum bpdisp disposition,
8924 int thread, int task, int ignore_count,
8925 const struct breakpoint_ops *ops, int from_tty,
8926 int enabled, int internal, unsigned flags,
8927 int display_canonical)
8928 {
8929 int i;
8930
8931 if (type == bp_hardware_breakpoint)
8932 {
8933 int target_resources_ok;
8934
8935 i = hw_breakpoint_used_count ();
8936 target_resources_ok =
8937 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8938 i + 1, 0);
8939 if (target_resources_ok == 0)
8940 error (_("No hardware breakpoint support in the target."));
8941 else if (target_resources_ok < 0)
8942 error (_("Hardware breakpoints used exceeds limit."));
8943 }
8944
8945 gdb_assert (!sals.empty ());
8946
8947 for (const auto &sal : sals)
8948 {
8949 struct bp_location *loc;
8950
8951 if (from_tty)
8952 {
8953 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8954 if (!loc_gdbarch)
8955 loc_gdbarch = gdbarch;
8956
8957 describe_other_breakpoints (loc_gdbarch,
8958 sal.pspace, sal.pc, sal.section, thread);
8959 }
8960
8961 if (&sal == &sals[0])
8962 {
8963 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8964 b->thread = thread;
8965 b->task = task;
8966
8967 b->cond_string = cond_string.release ();
8968 b->extra_string = extra_string.release ();
8969 b->ignore_count = ignore_count;
8970 b->enable_state = enabled ? bp_enabled : bp_disabled;
8971 b->disposition = disposition;
8972
8973 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8974 b->loc->inserted = 1;
8975
8976 if (type == bp_static_tracepoint)
8977 {
8978 struct tracepoint *t = (struct tracepoint *) b;
8979 struct static_tracepoint_marker marker;
8980
8981 if (strace_marker_p (b))
8982 {
8983 /* We already know the marker exists, otherwise, we
8984 wouldn't see a sal for it. */
8985 const char *p
8986 = &event_location_to_string (b->location.get ())[3];
8987 const char *endp;
8988 char *marker_str;
8989
8990 p = skip_spaces (p);
8991
8992 endp = skip_to_space (p);
8993
8994 marker_str = savestring (p, endp - p);
8995 t->static_trace_marker_id = marker_str;
8996
8997 printf_filtered (_("Probed static tracepoint "
8998 "marker \"%s\"\n"),
8999 t->static_trace_marker_id);
9000 }
9001 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9002 {
9003 t->static_trace_marker_id = xstrdup (marker.str_id);
9004 release_static_tracepoint_marker (&marker);
9005
9006 printf_filtered (_("Probed static tracepoint "
9007 "marker \"%s\"\n"),
9008 t->static_trace_marker_id);
9009 }
9010 else
9011 warning (_("Couldn't determine the static "
9012 "tracepoint marker to probe"));
9013 }
9014
9015 loc = b->loc;
9016 }
9017 else
9018 {
9019 loc = add_location_to_breakpoint (b, &sal);
9020 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9021 loc->inserted = 1;
9022 }
9023
9024 if (b->cond_string)
9025 {
9026 const char *arg = b->cond_string;
9027
9028 loc->cond = parse_exp_1 (&arg, loc->address,
9029 block_for_pc (loc->address), 0);
9030 if (*arg)
9031 error (_("Garbage '%s' follows condition"), arg);
9032 }
9033
9034 /* Dynamic printf requires and uses additional arguments on the
9035 command line, otherwise it's an error. */
9036 if (type == bp_dprintf)
9037 {
9038 if (b->extra_string)
9039 update_dprintf_command_list (b);
9040 else
9041 error (_("Format string required"));
9042 }
9043 else if (b->extra_string)
9044 error (_("Garbage '%s' at end of command"), b->extra_string);
9045 }
9046
9047 b->display_canonical = display_canonical;
9048 if (location != NULL)
9049 b->location = std::move (location);
9050 else
9051 b->location = new_address_location (b->loc->address, NULL, 0);
9052 b->filter = filter.release ();
9053 }
9054
9055 static void
9056 create_breakpoint_sal (struct gdbarch *gdbarch,
9057 gdb::array_view<const symtab_and_line> sals,
9058 event_location_up &&location,
9059 gdb::unique_xmalloc_ptr<char> filter,
9060 gdb::unique_xmalloc_ptr<char> cond_string,
9061 gdb::unique_xmalloc_ptr<char> extra_string,
9062 enum bptype type, enum bpdisp disposition,
9063 int thread, int task, int ignore_count,
9064 const struct breakpoint_ops *ops, int from_tty,
9065 int enabled, int internal, unsigned flags,
9066 int display_canonical)
9067 {
9068 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9069
9070 init_breakpoint_sal (b.get (), gdbarch,
9071 sals, std::move (location),
9072 std::move (filter),
9073 std::move (cond_string),
9074 std::move (extra_string),
9075 type, disposition,
9076 thread, task, ignore_count,
9077 ops, from_tty,
9078 enabled, internal, flags,
9079 display_canonical);
9080
9081 install_breakpoint (internal, std::move (b), 0);
9082 }
9083
9084 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9085 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9086 value. COND_STRING, if not NULL, specified the condition to be
9087 used for all breakpoints. Essentially the only case where
9088 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9089 function. In that case, it's still not possible to specify
9090 separate conditions for different overloaded functions, so
9091 we take just a single condition string.
9092
9093 NOTE: If the function succeeds, the caller is expected to cleanup
9094 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9095 array contents). If the function fails (error() is called), the
9096 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9097 COND and SALS arrays and each of those arrays contents. */
9098
9099 static void
9100 create_breakpoints_sal (struct gdbarch *gdbarch,
9101 struct linespec_result *canonical,
9102 gdb::unique_xmalloc_ptr<char> cond_string,
9103 gdb::unique_xmalloc_ptr<char> extra_string,
9104 enum bptype type, enum bpdisp disposition,
9105 int thread, int task, int ignore_count,
9106 const struct breakpoint_ops *ops, int from_tty,
9107 int enabled, int internal, unsigned flags)
9108 {
9109 if (canonical->pre_expanded)
9110 gdb_assert (canonical->lsals.size () == 1);
9111
9112 for (const auto &lsal : canonical->lsals)
9113 {
9114 /* Note that 'location' can be NULL in the case of a plain
9115 'break', without arguments. */
9116 event_location_up location
9117 = (canonical->location != NULL
9118 ? copy_event_location (canonical->location.get ()) : NULL);
9119 gdb::unique_xmalloc_ptr<char> filter_string
9120 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9121
9122 create_breakpoint_sal (gdbarch, lsal.sals,
9123 std::move (location),
9124 std::move (filter_string),
9125 std::move (cond_string),
9126 std::move (extra_string),
9127 type, disposition,
9128 thread, task, ignore_count, ops,
9129 from_tty, enabled, internal, flags,
9130 canonical->special_display);
9131 }
9132 }
9133
9134 /* Parse LOCATION which is assumed to be a SAL specification possibly
9135 followed by conditionals. On return, SALS contains an array of SAL
9136 addresses found. LOCATION points to the end of the SAL (for
9137 linespec locations).
9138
9139 The array and the line spec strings are allocated on the heap, it is
9140 the caller's responsibility to free them. */
9141
9142 static void
9143 parse_breakpoint_sals (const struct event_location *location,
9144 struct linespec_result *canonical)
9145 {
9146 struct symtab_and_line cursal;
9147
9148 if (event_location_type (location) == LINESPEC_LOCATION)
9149 {
9150 const char *address = get_linespec_location (location);
9151
9152 if (address == NULL)
9153 {
9154 /* The last displayed codepoint, if it's valid, is our default
9155 breakpoint address. */
9156 if (last_displayed_sal_is_valid ())
9157 {
9158 /* Set sal's pspace, pc, symtab, and line to the values
9159 corresponding to the last call to print_frame_info.
9160 Be sure to reinitialize LINE with NOTCURRENT == 0
9161 as the breakpoint line number is inappropriate otherwise.
9162 find_pc_line would adjust PC, re-set it back. */
9163 symtab_and_line sal = get_last_displayed_sal ();
9164 CORE_ADDR pc = sal.pc;
9165
9166 sal = find_pc_line (pc, 0);
9167
9168 /* "break" without arguments is equivalent to "break *PC"
9169 where PC is the last displayed codepoint's address. So
9170 make sure to set sal.explicit_pc to prevent GDB from
9171 trying to expand the list of sals to include all other
9172 instances with the same symtab and line. */
9173 sal.pc = pc;
9174 sal.explicit_pc = 1;
9175
9176 struct linespec_sals lsal;
9177 lsal.sals = {sal};
9178 lsal.canonical = NULL;
9179
9180 canonical->lsals.push_back (std::move (lsal));
9181 return;
9182 }
9183 else
9184 error (_("No default breakpoint address now."));
9185 }
9186 }
9187
9188 /* Force almost all breakpoints to be in terms of the
9189 current_source_symtab (which is decode_line_1's default).
9190 This should produce the results we want almost all of the
9191 time while leaving default_breakpoint_* alone.
9192
9193 ObjC: However, don't match an Objective-C method name which
9194 may have a '+' or '-' succeeded by a '['. */
9195 cursal = get_current_source_symtab_and_line ();
9196 if (last_displayed_sal_is_valid ())
9197 {
9198 const char *address = NULL;
9199
9200 if (event_location_type (location) == LINESPEC_LOCATION)
9201 address = get_linespec_location (location);
9202
9203 if (!cursal.symtab
9204 || (address != NULL
9205 && strchr ("+-", address[0]) != NULL
9206 && address[1] != '['))
9207 {
9208 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9209 get_last_displayed_symtab (),
9210 get_last_displayed_line (),
9211 canonical, NULL, NULL);
9212 return;
9213 }
9214 }
9215
9216 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9217 cursal.symtab, cursal.line, canonical, NULL, NULL);
9218 }
9219
9220
9221 /* Convert each SAL into a real PC. Verify that the PC can be
9222 inserted as a breakpoint. If it can't throw an error. */
9223
9224 static void
9225 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9226 {
9227 for (auto &sal : sals)
9228 resolve_sal_pc (&sal);
9229 }
9230
9231 /* Fast tracepoints may have restrictions on valid locations. For
9232 instance, a fast tracepoint using a jump instead of a trap will
9233 likely have to overwrite more bytes than a trap would, and so can
9234 only be placed where the instruction is longer than the jump, or a
9235 multi-instruction sequence does not have a jump into the middle of
9236 it, etc. */
9237
9238 static void
9239 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9240 gdb::array_view<const symtab_and_line> sals)
9241 {
9242 int rslt;
9243 char *msg;
9244 struct cleanup *old_chain;
9245
9246 for (const auto &sal : sals)
9247 {
9248 struct gdbarch *sarch;
9249
9250 sarch = get_sal_arch (sal);
9251 /* We fall back to GDBARCH if there is no architecture
9252 associated with SAL. */
9253 if (sarch == NULL)
9254 sarch = gdbarch;
9255 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
9256 old_chain = make_cleanup (xfree, msg);
9257
9258 if (!rslt)
9259 error (_("May not have a fast tracepoint at %s%s"),
9260 paddress (sarch, sal.pc), (msg ? msg : ""));
9261
9262 do_cleanups (old_chain);
9263 }
9264 }
9265
9266 /* Given TOK, a string specification of condition and thread, as
9267 accepted by the 'break' command, extract the condition
9268 string and thread number and set *COND_STRING and *THREAD.
9269 PC identifies the context at which the condition should be parsed.
9270 If no condition is found, *COND_STRING is set to NULL.
9271 If no thread is found, *THREAD is set to -1. */
9272
9273 static void
9274 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9275 char **cond_string, int *thread, int *task,
9276 char **rest)
9277 {
9278 *cond_string = NULL;
9279 *thread = -1;
9280 *task = 0;
9281 *rest = NULL;
9282
9283 while (tok && *tok)
9284 {
9285 const char *end_tok;
9286 int toklen;
9287 const char *cond_start = NULL;
9288 const char *cond_end = NULL;
9289
9290 tok = skip_spaces (tok);
9291
9292 if ((*tok == '"' || *tok == ',') && rest)
9293 {
9294 *rest = savestring (tok, strlen (tok));
9295 return;
9296 }
9297
9298 end_tok = skip_to_space (tok);
9299
9300 toklen = end_tok - tok;
9301
9302 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9303 {
9304 tok = cond_start = end_tok + 1;
9305 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9306 cond_end = tok;
9307 *cond_string = savestring (cond_start, cond_end - cond_start);
9308 }
9309 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9310 {
9311 const char *tmptok;
9312 struct thread_info *thr;
9313
9314 tok = end_tok + 1;
9315 thr = parse_thread_id (tok, &tmptok);
9316 if (tok == tmptok)
9317 error (_("Junk after thread keyword."));
9318 *thread = thr->global_num;
9319 tok = tmptok;
9320 }
9321 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9322 {
9323 char *tmptok;
9324
9325 tok = end_tok + 1;
9326 *task = strtol (tok, &tmptok, 0);
9327 if (tok == tmptok)
9328 error (_("Junk after task keyword."));
9329 if (!valid_task_id (*task))
9330 error (_("Unknown task %d."), *task);
9331 tok = tmptok;
9332 }
9333 else if (rest)
9334 {
9335 *rest = savestring (tok, strlen (tok));
9336 return;
9337 }
9338 else
9339 error (_("Junk at end of arguments."));
9340 }
9341 }
9342
9343 /* Decode a static tracepoint marker spec. */
9344
9345 static std::vector<symtab_and_line>
9346 decode_static_tracepoint_spec (const char **arg_p)
9347 {
9348 VEC(static_tracepoint_marker_p) *markers = NULL;
9349 const char *p = &(*arg_p)[3];
9350 const char *endp;
9351 int i;
9352
9353 p = skip_spaces (p);
9354
9355 endp = skip_to_space (p);
9356
9357 std::string marker_str (p, endp - p);
9358
9359 markers = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9360 if (VEC_empty(static_tracepoint_marker_p, markers))
9361 error (_("No known static tracepoint marker named %s"),
9362 marker_str.c_str ());
9363
9364 std::vector<symtab_and_line> sals;
9365 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
9366
9367 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
9368 {
9369 struct static_tracepoint_marker *marker;
9370
9371 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9372
9373 symtab_and_line sal = find_pc_line (marker->address, 0);
9374 sal.pc = marker->address;
9375 sals.push_back (sal);
9376
9377 release_static_tracepoint_marker (marker);
9378 }
9379
9380 *arg_p = endp;
9381 return sals;
9382 }
9383
9384 /* See breakpoint.h. */
9385
9386 int
9387 create_breakpoint (struct gdbarch *gdbarch,
9388 const struct event_location *location,
9389 const char *cond_string,
9390 int thread, const char *extra_string,
9391 int parse_extra,
9392 int tempflag, enum bptype type_wanted,
9393 int ignore_count,
9394 enum auto_boolean pending_break_support,
9395 const struct breakpoint_ops *ops,
9396 int from_tty, int enabled, int internal,
9397 unsigned flags)
9398 {
9399 struct linespec_result canonical;
9400 struct cleanup *bkpt_chain = NULL;
9401 int pending = 0;
9402 int task = 0;
9403 int prev_bkpt_count = breakpoint_count;
9404
9405 gdb_assert (ops != NULL);
9406
9407 /* If extra_string isn't useful, set it to NULL. */
9408 if (extra_string != NULL && *extra_string == '\0')
9409 extra_string = NULL;
9410
9411 TRY
9412 {
9413 ops->create_sals_from_location (location, &canonical, type_wanted);
9414 }
9415 CATCH (e, RETURN_MASK_ERROR)
9416 {
9417 /* If caller is interested in rc value from parse, set
9418 value. */
9419 if (e.error == NOT_FOUND_ERROR)
9420 {
9421 /* If pending breakpoint support is turned off, throw
9422 error. */
9423
9424 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9425 throw_exception (e);
9426
9427 exception_print (gdb_stderr, e);
9428
9429 /* If pending breakpoint support is auto query and the user
9430 selects no, then simply return the error code. */
9431 if (pending_break_support == AUTO_BOOLEAN_AUTO
9432 && !nquery (_("Make %s pending on future shared library load? "),
9433 bptype_string (type_wanted)))
9434 return 0;
9435
9436 /* At this point, either the user was queried about setting
9437 a pending breakpoint and selected yes, or pending
9438 breakpoint behavior is on and thus a pending breakpoint
9439 is defaulted on behalf of the user. */
9440 pending = 1;
9441 }
9442 else
9443 throw_exception (e);
9444 }
9445 END_CATCH
9446
9447 if (!pending && canonical.lsals.empty ())
9448 return 0;
9449
9450 /* ----------------------------- SNIP -----------------------------
9451 Anything added to the cleanup chain beyond this point is assumed
9452 to be part of a breakpoint. If the breakpoint create succeeds
9453 then the memory is not reclaimed. */
9454 bkpt_chain = make_cleanup (null_cleanup, 0);
9455
9456 /* Resolve all line numbers to PC's and verify that the addresses
9457 are ok for the target. */
9458 if (!pending)
9459 {
9460 for (auto &lsal : canonical.lsals)
9461 breakpoint_sals_to_pc (lsal.sals);
9462 }
9463
9464 /* Fast tracepoints may have additional restrictions on location. */
9465 if (!pending && type_wanted == bp_fast_tracepoint)
9466 {
9467 for (const auto &lsal : canonical.lsals)
9468 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9469 }
9470
9471 /* Verify that condition can be parsed, before setting any
9472 breakpoints. Allocate a separate condition expression for each
9473 breakpoint. */
9474 if (!pending)
9475 {
9476 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9477 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9478
9479 if (parse_extra)
9480 {
9481 char *rest;
9482 char *cond;
9483
9484 const linespec_sals &lsal = canonical.lsals[0];
9485
9486 /* Here we only parse 'arg' to separate condition
9487 from thread number, so parsing in context of first
9488 sal is OK. When setting the breakpoint we'll
9489 re-parse it in context of each sal. */
9490
9491 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9492 &cond, &thread, &task, &rest);
9493 cond_string_copy.reset (cond);
9494 extra_string_copy.reset (rest);
9495 }
9496 else
9497 {
9498 if (type_wanted != bp_dprintf
9499 && extra_string != NULL && *extra_string != '\0')
9500 error (_("Garbage '%s' at end of location"), extra_string);
9501
9502 /* Create a private copy of condition string. */
9503 if (cond_string)
9504 cond_string_copy.reset (xstrdup (cond_string));
9505 /* Create a private copy of any extra string. */
9506 if (extra_string)
9507 extra_string_copy.reset (xstrdup (extra_string));
9508 }
9509
9510 ops->create_breakpoints_sal (gdbarch, &canonical,
9511 std::move (cond_string_copy),
9512 std::move (extra_string_copy),
9513 type_wanted,
9514 tempflag ? disp_del : disp_donttouch,
9515 thread, task, ignore_count, ops,
9516 from_tty, enabled, internal, flags);
9517 }
9518 else
9519 {
9520 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9521
9522 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9523 b->location = copy_event_location (location);
9524
9525 if (parse_extra)
9526 b->cond_string = NULL;
9527 else
9528 {
9529 /* Create a private copy of condition string. */
9530 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9531 b->thread = thread;
9532 }
9533
9534 /* Create a private copy of any extra string. */
9535 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9536 b->ignore_count = ignore_count;
9537 b->disposition = tempflag ? disp_del : disp_donttouch;
9538 b->condition_not_parsed = 1;
9539 b->enable_state = enabled ? bp_enabled : bp_disabled;
9540 if ((type_wanted != bp_breakpoint
9541 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9542 b->pspace = current_program_space;
9543
9544 install_breakpoint (internal, std::move (b), 0);
9545 }
9546
9547 if (canonical.lsals.size () > 1)
9548 {
9549 warning (_("Multiple breakpoints were set.\nUse the "
9550 "\"delete\" command to delete unwanted breakpoints."));
9551 prev_breakpoint_count = prev_bkpt_count;
9552 }
9553
9554 /* That's it. Discard the cleanups for data inserted into the
9555 breakpoint. */
9556 discard_cleanups (bkpt_chain);
9557
9558 /* error call may happen here - have BKPT_CHAIN already discarded. */
9559 update_global_location_list (UGLL_MAY_INSERT);
9560
9561 return 1;
9562 }
9563
9564 /* Set a breakpoint.
9565 ARG is a string describing breakpoint address,
9566 condition, and thread.
9567 FLAG specifies if a breakpoint is hardware on,
9568 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9569 and BP_TEMPFLAG. */
9570
9571 static void
9572 break_command_1 (const char *arg, int flag, int from_tty)
9573 {
9574 int tempflag = flag & BP_TEMPFLAG;
9575 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9576 ? bp_hardware_breakpoint
9577 : bp_breakpoint);
9578 struct breakpoint_ops *ops;
9579
9580 event_location_up location = string_to_event_location (&arg, current_language);
9581
9582 /* Matching breakpoints on probes. */
9583 if (location != NULL
9584 && event_location_type (location.get ()) == PROBE_LOCATION)
9585 ops = &bkpt_probe_breakpoint_ops;
9586 else
9587 ops = &bkpt_breakpoint_ops;
9588
9589 create_breakpoint (get_current_arch (),
9590 location.get (),
9591 NULL, 0, arg, 1 /* parse arg */,
9592 tempflag, type_wanted,
9593 0 /* Ignore count */,
9594 pending_break_support,
9595 ops,
9596 from_tty,
9597 1 /* enabled */,
9598 0 /* internal */,
9599 0);
9600 }
9601
9602 /* Helper function for break_command_1 and disassemble_command. */
9603
9604 void
9605 resolve_sal_pc (struct symtab_and_line *sal)
9606 {
9607 CORE_ADDR pc;
9608
9609 if (sal->pc == 0 && sal->symtab != NULL)
9610 {
9611 if (!find_line_pc (sal->symtab, sal->line, &pc))
9612 error (_("No line %d in file \"%s\"."),
9613 sal->line, symtab_to_filename_for_display (sal->symtab));
9614 sal->pc = pc;
9615
9616 /* If this SAL corresponds to a breakpoint inserted using a line
9617 number, then skip the function prologue if necessary. */
9618 if (sal->explicit_line)
9619 skip_prologue_sal (sal);
9620 }
9621
9622 if (sal->section == 0 && sal->symtab != NULL)
9623 {
9624 const struct blockvector *bv;
9625 const struct block *b;
9626 struct symbol *sym;
9627
9628 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9629 SYMTAB_COMPUNIT (sal->symtab));
9630 if (bv != NULL)
9631 {
9632 sym = block_linkage_function (b);
9633 if (sym != NULL)
9634 {
9635 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9636 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9637 sym);
9638 }
9639 else
9640 {
9641 /* It really is worthwhile to have the section, so we'll
9642 just have to look harder. This case can be executed
9643 if we have line numbers but no functions (as can
9644 happen in assembly source). */
9645
9646 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9647 switch_to_program_space_and_thread (sal->pspace);
9648
9649 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9650 if (msym.minsym)
9651 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9652 }
9653 }
9654 }
9655 }
9656
9657 void
9658 break_command (char *arg, int from_tty)
9659 {
9660 break_command_1 (arg, 0, from_tty);
9661 }
9662
9663 void
9664 tbreak_command (char *arg, int from_tty)
9665 {
9666 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9667 }
9668
9669 static void
9670 hbreak_command (char *arg, int from_tty)
9671 {
9672 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9673 }
9674
9675 static void
9676 thbreak_command (char *arg, int from_tty)
9677 {
9678 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9679 }
9680
9681 static void
9682 stop_command (char *arg, int from_tty)
9683 {
9684 printf_filtered (_("Specify the type of breakpoint to set.\n\
9685 Usage: stop in <function | address>\n\
9686 stop at <line>\n"));
9687 }
9688
9689 static void
9690 stopin_command (const char *arg, int from_tty)
9691 {
9692 int badInput = 0;
9693
9694 if (arg == (char *) NULL)
9695 badInput = 1;
9696 else if (*arg != '*')
9697 {
9698 const char *argptr = arg;
9699 int hasColon = 0;
9700
9701 /* Look for a ':'. If this is a line number specification, then
9702 say it is bad, otherwise, it should be an address or
9703 function/method name. */
9704 while (*argptr && !hasColon)
9705 {
9706 hasColon = (*argptr == ':');
9707 argptr++;
9708 }
9709
9710 if (hasColon)
9711 badInput = (*argptr != ':'); /* Not a class::method */
9712 else
9713 badInput = isdigit (*arg); /* a simple line number */
9714 }
9715
9716 if (badInput)
9717 printf_filtered (_("Usage: stop in <function | address>\n"));
9718 else
9719 break_command_1 (arg, 0, from_tty);
9720 }
9721
9722 static void
9723 stopat_command (const char *arg, int from_tty)
9724 {
9725 int badInput = 0;
9726
9727 if (arg == (char *) NULL || *arg == '*') /* no line number */
9728 badInput = 1;
9729 else
9730 {
9731 const char *argptr = arg;
9732 int hasColon = 0;
9733
9734 /* Look for a ':'. If there is a '::' then get out, otherwise
9735 it is probably a line number. */
9736 while (*argptr && !hasColon)
9737 {
9738 hasColon = (*argptr == ':');
9739 argptr++;
9740 }
9741
9742 if (hasColon)
9743 badInput = (*argptr == ':'); /* we have class::method */
9744 else
9745 badInput = !isdigit (*arg); /* not a line number */
9746 }
9747
9748 if (badInput)
9749 printf_filtered (_("Usage: stop at <line>\n"));
9750 else
9751 break_command_1 (arg, 0, from_tty);
9752 }
9753
9754 /* The dynamic printf command is mostly like a regular breakpoint, but
9755 with a prewired command list consisting of a single output command,
9756 built from extra arguments supplied on the dprintf command
9757 line. */
9758
9759 static void
9760 dprintf_command (char *arg_in, int from_tty)
9761 {
9762 const char *arg = arg_in;
9763 event_location_up location = string_to_event_location (&arg, current_language);
9764
9765 /* If non-NULL, ARG should have been advanced past the location;
9766 the next character must be ','. */
9767 if (arg != NULL)
9768 {
9769 if (arg[0] != ',' || arg[1] == '\0')
9770 error (_("Format string required"));
9771 else
9772 {
9773 /* Skip the comma. */
9774 ++arg;
9775 }
9776 }
9777
9778 create_breakpoint (get_current_arch (),
9779 location.get (),
9780 NULL, 0, arg, 1 /* parse arg */,
9781 0, bp_dprintf,
9782 0 /* Ignore count */,
9783 pending_break_support,
9784 &dprintf_breakpoint_ops,
9785 from_tty,
9786 1 /* enabled */,
9787 0 /* internal */,
9788 0);
9789 }
9790
9791 static void
9792 agent_printf_command (char *arg, int from_tty)
9793 {
9794 error (_("May only run agent-printf on the target"));
9795 }
9796
9797 /* Implement the "breakpoint_hit" breakpoint_ops method for
9798 ranged breakpoints. */
9799
9800 static int
9801 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9802 const address_space *aspace,
9803 CORE_ADDR bp_addr,
9804 const struct target_waitstatus *ws)
9805 {
9806 if (ws->kind != TARGET_WAITKIND_STOPPED
9807 || ws->value.sig != GDB_SIGNAL_TRAP)
9808 return 0;
9809
9810 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9811 bl->length, aspace, bp_addr);
9812 }
9813
9814 /* Implement the "resources_needed" breakpoint_ops method for
9815 ranged breakpoints. */
9816
9817 static int
9818 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9819 {
9820 return target_ranged_break_num_registers ();
9821 }
9822
9823 /* Implement the "print_it" breakpoint_ops method for
9824 ranged breakpoints. */
9825
9826 static enum print_stop_action
9827 print_it_ranged_breakpoint (bpstat bs)
9828 {
9829 struct breakpoint *b = bs->breakpoint_at;
9830 struct bp_location *bl = b->loc;
9831 struct ui_out *uiout = current_uiout;
9832
9833 gdb_assert (b->type == bp_hardware_breakpoint);
9834
9835 /* Ranged breakpoints have only one location. */
9836 gdb_assert (bl && bl->next == NULL);
9837
9838 annotate_breakpoint (b->number);
9839
9840 maybe_print_thread_hit_breakpoint (uiout);
9841
9842 if (b->disposition == disp_del)
9843 uiout->text ("Temporary ranged breakpoint ");
9844 else
9845 uiout->text ("Ranged breakpoint ");
9846 if (uiout->is_mi_like_p ())
9847 {
9848 uiout->field_string ("reason",
9849 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9850 uiout->field_string ("disp", bpdisp_text (b->disposition));
9851 }
9852 uiout->field_int ("bkptno", b->number);
9853 uiout->text (", ");
9854
9855 return PRINT_SRC_AND_LOC;
9856 }
9857
9858 /* Implement the "print_one" breakpoint_ops method for
9859 ranged breakpoints. */
9860
9861 static void
9862 print_one_ranged_breakpoint (struct breakpoint *b,
9863 struct bp_location **last_loc)
9864 {
9865 struct bp_location *bl = b->loc;
9866 struct value_print_options opts;
9867 struct ui_out *uiout = current_uiout;
9868
9869 /* Ranged breakpoints have only one location. */
9870 gdb_assert (bl && bl->next == NULL);
9871
9872 get_user_print_options (&opts);
9873
9874 if (opts.addressprint)
9875 /* We don't print the address range here, it will be printed later
9876 by print_one_detail_ranged_breakpoint. */
9877 uiout->field_skip ("addr");
9878 annotate_field (5);
9879 print_breakpoint_location (b, bl);
9880 *last_loc = bl;
9881 }
9882
9883 /* Implement the "print_one_detail" breakpoint_ops method for
9884 ranged breakpoints. */
9885
9886 static void
9887 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9888 struct ui_out *uiout)
9889 {
9890 CORE_ADDR address_start, address_end;
9891 struct bp_location *bl = b->loc;
9892 string_file stb;
9893
9894 gdb_assert (bl);
9895
9896 address_start = bl->address;
9897 address_end = address_start + bl->length - 1;
9898
9899 uiout->text ("\taddress range: ");
9900 stb.printf ("[%s, %s]",
9901 print_core_address (bl->gdbarch, address_start),
9902 print_core_address (bl->gdbarch, address_end));
9903 uiout->field_stream ("addr", stb);
9904 uiout->text ("\n");
9905 }
9906
9907 /* Implement the "print_mention" breakpoint_ops method for
9908 ranged breakpoints. */
9909
9910 static void
9911 print_mention_ranged_breakpoint (struct breakpoint *b)
9912 {
9913 struct bp_location *bl = b->loc;
9914 struct ui_out *uiout = current_uiout;
9915
9916 gdb_assert (bl);
9917 gdb_assert (b->type == bp_hardware_breakpoint);
9918
9919 if (uiout->is_mi_like_p ())
9920 return;
9921
9922 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9923 b->number, paddress (bl->gdbarch, bl->address),
9924 paddress (bl->gdbarch, bl->address + bl->length - 1));
9925 }
9926
9927 /* Implement the "print_recreate" breakpoint_ops method for
9928 ranged breakpoints. */
9929
9930 static void
9931 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9932 {
9933 fprintf_unfiltered (fp, "break-range %s, %s",
9934 event_location_to_string (b->location.get ()),
9935 event_location_to_string (b->location_range_end.get ()));
9936 print_recreate_thread (b, fp);
9937 }
9938
9939 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9940
9941 static struct breakpoint_ops ranged_breakpoint_ops;
9942
9943 /* Find the address where the end of the breakpoint range should be
9944 placed, given the SAL of the end of the range. This is so that if
9945 the user provides a line number, the end of the range is set to the
9946 last instruction of the given line. */
9947
9948 static CORE_ADDR
9949 find_breakpoint_range_end (struct symtab_and_line sal)
9950 {
9951 CORE_ADDR end;
9952
9953 /* If the user provided a PC value, use it. Otherwise,
9954 find the address of the end of the given location. */
9955 if (sal.explicit_pc)
9956 end = sal.pc;
9957 else
9958 {
9959 int ret;
9960 CORE_ADDR start;
9961
9962 ret = find_line_pc_range (sal, &start, &end);
9963 if (!ret)
9964 error (_("Could not find location of the end of the range."));
9965
9966 /* find_line_pc_range returns the start of the next line. */
9967 end--;
9968 }
9969
9970 return end;
9971 }
9972
9973 /* Implement the "break-range" CLI command. */
9974
9975 static void
9976 break_range_command (char *arg_in, int from_tty)
9977 {
9978 const char *arg = arg_in;
9979 const char *arg_start;
9980 struct linespec_result canonical_start, canonical_end;
9981 int bp_count, can_use_bp, length;
9982 CORE_ADDR end;
9983 struct breakpoint *b;
9984
9985 /* We don't support software ranged breakpoints. */
9986 if (target_ranged_break_num_registers () < 0)
9987 error (_("This target does not support hardware ranged breakpoints."));
9988
9989 bp_count = hw_breakpoint_used_count ();
9990 bp_count += target_ranged_break_num_registers ();
9991 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9992 bp_count, 0);
9993 if (can_use_bp < 0)
9994 error (_("Hardware breakpoints used exceeds limit."));
9995
9996 arg = skip_spaces (arg);
9997 if (arg == NULL || arg[0] == '\0')
9998 error(_("No address range specified."));
9999
10000 arg_start = arg;
10001 event_location_up start_location = string_to_event_location (&arg,
10002 current_language);
10003 parse_breakpoint_sals (start_location.get (), &canonical_start);
10004
10005 if (arg[0] != ',')
10006 error (_("Too few arguments."));
10007 else if (canonical_start.lsals.empty ())
10008 error (_("Could not find location of the beginning of the range."));
10009
10010 const linespec_sals &lsal_start = canonical_start.lsals[0];
10011
10012 if (canonical_start.lsals.size () > 1
10013 || lsal_start.sals.size () != 1)
10014 error (_("Cannot create a ranged breakpoint with multiple locations."));
10015
10016 const symtab_and_line &sal_start = lsal_start.sals[0];
10017 std::string addr_string_start (arg_start, arg - arg_start);
10018
10019 arg++; /* Skip the comma. */
10020 arg = skip_spaces (arg);
10021
10022 /* Parse the end location. */
10023
10024 arg_start = arg;
10025
10026 /* We call decode_line_full directly here instead of using
10027 parse_breakpoint_sals because we need to specify the start location's
10028 symtab and line as the default symtab and line for the end of the
10029 range. This makes it possible to have ranges like "foo.c:27, +14",
10030 where +14 means 14 lines from the start location. */
10031 event_location_up end_location = string_to_event_location (&arg,
10032 current_language);
10033 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10034 sal_start.symtab, sal_start.line,
10035 &canonical_end, NULL, NULL);
10036
10037 if (canonical_end.lsals.empty ())
10038 error (_("Could not find location of the end of the range."));
10039
10040 const linespec_sals &lsal_end = canonical_end.lsals[0];
10041 if (canonical_end.lsals.size () > 1
10042 || lsal_end.sals.size () != 1)
10043 error (_("Cannot create a ranged breakpoint with multiple locations."));
10044
10045 const symtab_and_line &sal_end = lsal_end.sals[0];
10046
10047 end = find_breakpoint_range_end (sal_end);
10048 if (sal_start.pc > end)
10049 error (_("Invalid address range, end precedes start."));
10050
10051 length = end - sal_start.pc + 1;
10052 if (length < 0)
10053 /* Length overflowed. */
10054 error (_("Address range too large."));
10055 else if (length == 1)
10056 {
10057 /* This range is simple enough to be handled by
10058 the `hbreak' command. */
10059 hbreak_command (&addr_string_start[0], 1);
10060
10061 return;
10062 }
10063
10064 /* Now set up the breakpoint. */
10065 b = set_raw_breakpoint (get_current_arch (), sal_start,
10066 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10067 set_breakpoint_count (breakpoint_count + 1);
10068 b->number = breakpoint_count;
10069 b->disposition = disp_donttouch;
10070 b->location = std::move (start_location);
10071 b->location_range_end = std::move (end_location);
10072 b->loc->length = length;
10073
10074 mention (b);
10075 observer_notify_breakpoint_created (b);
10076 update_global_location_list (UGLL_MAY_INSERT);
10077 }
10078
10079 /* Return non-zero if EXP is verified as constant. Returned zero
10080 means EXP is variable. Also the constant detection may fail for
10081 some constant expressions and in such case still falsely return
10082 zero. */
10083
10084 static int
10085 watchpoint_exp_is_const (const struct expression *exp)
10086 {
10087 int i = exp->nelts;
10088
10089 while (i > 0)
10090 {
10091 int oplenp, argsp;
10092
10093 /* We are only interested in the descriptor of each element. */
10094 operator_length (exp, i, &oplenp, &argsp);
10095 i -= oplenp;
10096
10097 switch (exp->elts[i].opcode)
10098 {
10099 case BINOP_ADD:
10100 case BINOP_SUB:
10101 case BINOP_MUL:
10102 case BINOP_DIV:
10103 case BINOP_REM:
10104 case BINOP_MOD:
10105 case BINOP_LSH:
10106 case BINOP_RSH:
10107 case BINOP_LOGICAL_AND:
10108 case BINOP_LOGICAL_OR:
10109 case BINOP_BITWISE_AND:
10110 case BINOP_BITWISE_IOR:
10111 case BINOP_BITWISE_XOR:
10112 case BINOP_EQUAL:
10113 case BINOP_NOTEQUAL:
10114 case BINOP_LESS:
10115 case BINOP_GTR:
10116 case BINOP_LEQ:
10117 case BINOP_GEQ:
10118 case BINOP_REPEAT:
10119 case BINOP_COMMA:
10120 case BINOP_EXP:
10121 case BINOP_MIN:
10122 case BINOP_MAX:
10123 case BINOP_INTDIV:
10124 case BINOP_CONCAT:
10125 case TERNOP_COND:
10126 case TERNOP_SLICE:
10127
10128 case OP_LONG:
10129 case OP_FLOAT:
10130 case OP_LAST:
10131 case OP_COMPLEX:
10132 case OP_STRING:
10133 case OP_ARRAY:
10134 case OP_TYPE:
10135 case OP_TYPEOF:
10136 case OP_DECLTYPE:
10137 case OP_TYPEID:
10138 case OP_NAME:
10139 case OP_OBJC_NSSTRING:
10140
10141 case UNOP_NEG:
10142 case UNOP_LOGICAL_NOT:
10143 case UNOP_COMPLEMENT:
10144 case UNOP_ADDR:
10145 case UNOP_HIGH:
10146 case UNOP_CAST:
10147
10148 case UNOP_CAST_TYPE:
10149 case UNOP_REINTERPRET_CAST:
10150 case UNOP_DYNAMIC_CAST:
10151 /* Unary, binary and ternary operators: We have to check
10152 their operands. If they are constant, then so is the
10153 result of that operation. For instance, if A and B are
10154 determined to be constants, then so is "A + B".
10155
10156 UNOP_IND is one exception to the rule above, because the
10157 value of *ADDR is not necessarily a constant, even when
10158 ADDR is. */
10159 break;
10160
10161 case OP_VAR_VALUE:
10162 /* Check whether the associated symbol is a constant.
10163
10164 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10165 possible that a buggy compiler could mark a variable as
10166 constant even when it is not, and TYPE_CONST would return
10167 true in this case, while SYMBOL_CLASS wouldn't.
10168
10169 We also have to check for function symbols because they
10170 are always constant. */
10171 {
10172 struct symbol *s = exp->elts[i + 2].symbol;
10173
10174 if (SYMBOL_CLASS (s) != LOC_BLOCK
10175 && SYMBOL_CLASS (s) != LOC_CONST
10176 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10177 return 0;
10178 break;
10179 }
10180
10181 /* The default action is to return 0 because we are using
10182 the optimistic approach here: If we don't know something,
10183 then it is not a constant. */
10184 default:
10185 return 0;
10186 }
10187 }
10188
10189 return 1;
10190 }
10191
10192 /* Watchpoint destructor. */
10193
10194 watchpoint::~watchpoint ()
10195 {
10196 xfree (this->exp_string);
10197 xfree (this->exp_string_reparse);
10198 value_free (this->val);
10199 }
10200
10201 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10202
10203 static void
10204 re_set_watchpoint (struct breakpoint *b)
10205 {
10206 struct watchpoint *w = (struct watchpoint *) b;
10207
10208 /* Watchpoint can be either on expression using entirely global
10209 variables, or it can be on local variables.
10210
10211 Watchpoints of the first kind are never auto-deleted, and even
10212 persist across program restarts. Since they can use variables
10213 from shared libraries, we need to reparse expression as libraries
10214 are loaded and unloaded.
10215
10216 Watchpoints on local variables can also change meaning as result
10217 of solib event. For example, if a watchpoint uses both a local
10218 and a global variables in expression, it's a local watchpoint,
10219 but unloading of a shared library will make the expression
10220 invalid. This is not a very common use case, but we still
10221 re-evaluate expression, to avoid surprises to the user.
10222
10223 Note that for local watchpoints, we re-evaluate it only if
10224 watchpoints frame id is still valid. If it's not, it means the
10225 watchpoint is out of scope and will be deleted soon. In fact,
10226 I'm not sure we'll ever be called in this case.
10227
10228 If a local watchpoint's frame id is still valid, then
10229 w->exp_valid_block is likewise valid, and we can safely use it.
10230
10231 Don't do anything about disabled watchpoints, since they will be
10232 reevaluated again when enabled. */
10233 update_watchpoint (w, 1 /* reparse */);
10234 }
10235
10236 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10237
10238 static int
10239 insert_watchpoint (struct bp_location *bl)
10240 {
10241 struct watchpoint *w = (struct watchpoint *) bl->owner;
10242 int length = w->exact ? 1 : bl->length;
10243
10244 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10245 w->cond_exp.get ());
10246 }
10247
10248 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10249
10250 static int
10251 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10252 {
10253 struct watchpoint *w = (struct watchpoint *) bl->owner;
10254 int length = w->exact ? 1 : bl->length;
10255
10256 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10257 w->cond_exp.get ());
10258 }
10259
10260 static int
10261 breakpoint_hit_watchpoint (const struct bp_location *bl,
10262 const address_space *aspace, CORE_ADDR bp_addr,
10263 const struct target_waitstatus *ws)
10264 {
10265 struct breakpoint *b = bl->owner;
10266 struct watchpoint *w = (struct watchpoint *) b;
10267
10268 /* Continuable hardware watchpoints are treated as non-existent if the
10269 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10270 some data address). Otherwise gdb won't stop on a break instruction
10271 in the code (not from a breakpoint) when a hardware watchpoint has
10272 been defined. Also skip watchpoints which we know did not trigger
10273 (did not match the data address). */
10274 if (is_hardware_watchpoint (b)
10275 && w->watchpoint_triggered == watch_triggered_no)
10276 return 0;
10277
10278 return 1;
10279 }
10280
10281 static void
10282 check_status_watchpoint (bpstat bs)
10283 {
10284 gdb_assert (is_watchpoint (bs->breakpoint_at));
10285
10286 bpstat_check_watchpoint (bs);
10287 }
10288
10289 /* Implement the "resources_needed" breakpoint_ops method for
10290 hardware watchpoints. */
10291
10292 static int
10293 resources_needed_watchpoint (const struct bp_location *bl)
10294 {
10295 struct watchpoint *w = (struct watchpoint *) bl->owner;
10296 int length = w->exact? 1 : bl->length;
10297
10298 return target_region_ok_for_hw_watchpoint (bl->address, length);
10299 }
10300
10301 /* Implement the "works_in_software_mode" breakpoint_ops method for
10302 hardware watchpoints. */
10303
10304 static int
10305 works_in_software_mode_watchpoint (const struct breakpoint *b)
10306 {
10307 /* Read and access watchpoints only work with hardware support. */
10308 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10309 }
10310
10311 static enum print_stop_action
10312 print_it_watchpoint (bpstat bs)
10313 {
10314 struct breakpoint *b;
10315 enum print_stop_action result;
10316 struct watchpoint *w;
10317 struct ui_out *uiout = current_uiout;
10318
10319 gdb_assert (bs->bp_location_at != NULL);
10320
10321 b = bs->breakpoint_at;
10322 w = (struct watchpoint *) b;
10323
10324 annotate_watchpoint (b->number);
10325 maybe_print_thread_hit_breakpoint (uiout);
10326
10327 string_file stb;
10328
10329 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10330 switch (b->type)
10331 {
10332 case bp_watchpoint:
10333 case bp_hardware_watchpoint:
10334 if (uiout->is_mi_like_p ())
10335 uiout->field_string
10336 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10337 mention (b);
10338 tuple_emitter.emplace (uiout, "value");
10339 uiout->text ("\nOld value = ");
10340 watchpoint_value_print (bs->old_val, &stb);
10341 uiout->field_stream ("old", stb);
10342 uiout->text ("\nNew value = ");
10343 watchpoint_value_print (w->val, &stb);
10344 uiout->field_stream ("new", stb);
10345 uiout->text ("\n");
10346 /* More than one watchpoint may have been triggered. */
10347 result = PRINT_UNKNOWN;
10348 break;
10349
10350 case bp_read_watchpoint:
10351 if (uiout->is_mi_like_p ())
10352 uiout->field_string
10353 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10354 mention (b);
10355 tuple_emitter.emplace (uiout, "value");
10356 uiout->text ("\nValue = ");
10357 watchpoint_value_print (w->val, &stb);
10358 uiout->field_stream ("value", stb);
10359 uiout->text ("\n");
10360 result = PRINT_UNKNOWN;
10361 break;
10362
10363 case bp_access_watchpoint:
10364 if (bs->old_val != NULL)
10365 {
10366 if (uiout->is_mi_like_p ())
10367 uiout->field_string
10368 ("reason",
10369 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10370 mention (b);
10371 tuple_emitter.emplace (uiout, "value");
10372 uiout->text ("\nOld value = ");
10373 watchpoint_value_print (bs->old_val, &stb);
10374 uiout->field_stream ("old", stb);
10375 uiout->text ("\nNew value = ");
10376 }
10377 else
10378 {
10379 mention (b);
10380 if (uiout->is_mi_like_p ())
10381 uiout->field_string
10382 ("reason",
10383 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10384 tuple_emitter.emplace (uiout, "value");
10385 uiout->text ("\nValue = ");
10386 }
10387 watchpoint_value_print (w->val, &stb);
10388 uiout->field_stream ("new", stb);
10389 uiout->text ("\n");
10390 result = PRINT_UNKNOWN;
10391 break;
10392 default:
10393 result = PRINT_UNKNOWN;
10394 }
10395
10396 return result;
10397 }
10398
10399 /* Implement the "print_mention" breakpoint_ops method for hardware
10400 watchpoints. */
10401
10402 static void
10403 print_mention_watchpoint (struct breakpoint *b)
10404 {
10405 struct watchpoint *w = (struct watchpoint *) b;
10406 struct ui_out *uiout = current_uiout;
10407 const char *tuple_name;
10408
10409 switch (b->type)
10410 {
10411 case bp_watchpoint:
10412 uiout->text ("Watchpoint ");
10413 tuple_name = "wpt";
10414 break;
10415 case bp_hardware_watchpoint:
10416 uiout->text ("Hardware watchpoint ");
10417 tuple_name = "wpt";
10418 break;
10419 case bp_read_watchpoint:
10420 uiout->text ("Hardware read watchpoint ");
10421 tuple_name = "hw-rwpt";
10422 break;
10423 case bp_access_watchpoint:
10424 uiout->text ("Hardware access (read/write) watchpoint ");
10425 tuple_name = "hw-awpt";
10426 break;
10427 default:
10428 internal_error (__FILE__, __LINE__,
10429 _("Invalid hardware watchpoint type."));
10430 }
10431
10432 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10433 uiout->field_int ("number", b->number);
10434 uiout->text (": ");
10435 uiout->field_string ("exp", w->exp_string);
10436 }
10437
10438 /* Implement the "print_recreate" breakpoint_ops method for
10439 watchpoints. */
10440
10441 static void
10442 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10443 {
10444 struct watchpoint *w = (struct watchpoint *) b;
10445
10446 switch (b->type)
10447 {
10448 case bp_watchpoint:
10449 case bp_hardware_watchpoint:
10450 fprintf_unfiltered (fp, "watch");
10451 break;
10452 case bp_read_watchpoint:
10453 fprintf_unfiltered (fp, "rwatch");
10454 break;
10455 case bp_access_watchpoint:
10456 fprintf_unfiltered (fp, "awatch");
10457 break;
10458 default:
10459 internal_error (__FILE__, __LINE__,
10460 _("Invalid watchpoint type."));
10461 }
10462
10463 fprintf_unfiltered (fp, " %s", w->exp_string);
10464 print_recreate_thread (b, fp);
10465 }
10466
10467 /* Implement the "explains_signal" breakpoint_ops method for
10468 watchpoints. */
10469
10470 static int
10471 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10472 {
10473 /* A software watchpoint cannot cause a signal other than
10474 GDB_SIGNAL_TRAP. */
10475 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10476 return 0;
10477
10478 return 1;
10479 }
10480
10481 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10482
10483 static struct breakpoint_ops watchpoint_breakpoint_ops;
10484
10485 /* Implement the "insert" breakpoint_ops method for
10486 masked hardware watchpoints. */
10487
10488 static int
10489 insert_masked_watchpoint (struct bp_location *bl)
10490 {
10491 struct watchpoint *w = (struct watchpoint *) bl->owner;
10492
10493 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10494 bl->watchpoint_type);
10495 }
10496
10497 /* Implement the "remove" breakpoint_ops method for
10498 masked hardware watchpoints. */
10499
10500 static int
10501 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10502 {
10503 struct watchpoint *w = (struct watchpoint *) bl->owner;
10504
10505 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10506 bl->watchpoint_type);
10507 }
10508
10509 /* Implement the "resources_needed" breakpoint_ops method for
10510 masked hardware watchpoints. */
10511
10512 static int
10513 resources_needed_masked_watchpoint (const struct bp_location *bl)
10514 {
10515 struct watchpoint *w = (struct watchpoint *) bl->owner;
10516
10517 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10518 }
10519
10520 /* Implement the "works_in_software_mode" breakpoint_ops method for
10521 masked hardware watchpoints. */
10522
10523 static int
10524 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10525 {
10526 return 0;
10527 }
10528
10529 /* Implement the "print_it" breakpoint_ops method for
10530 masked hardware watchpoints. */
10531
10532 static enum print_stop_action
10533 print_it_masked_watchpoint (bpstat bs)
10534 {
10535 struct breakpoint *b = bs->breakpoint_at;
10536 struct ui_out *uiout = current_uiout;
10537
10538 /* Masked watchpoints have only one location. */
10539 gdb_assert (b->loc && b->loc->next == NULL);
10540
10541 annotate_watchpoint (b->number);
10542 maybe_print_thread_hit_breakpoint (uiout);
10543
10544 switch (b->type)
10545 {
10546 case bp_hardware_watchpoint:
10547 if (uiout->is_mi_like_p ())
10548 uiout->field_string
10549 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10550 break;
10551
10552 case bp_read_watchpoint:
10553 if (uiout->is_mi_like_p ())
10554 uiout->field_string
10555 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10556 break;
10557
10558 case bp_access_watchpoint:
10559 if (uiout->is_mi_like_p ())
10560 uiout->field_string
10561 ("reason",
10562 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10563 break;
10564 default:
10565 internal_error (__FILE__, __LINE__,
10566 _("Invalid hardware watchpoint type."));
10567 }
10568
10569 mention (b);
10570 uiout->text (_("\n\
10571 Check the underlying instruction at PC for the memory\n\
10572 address and value which triggered this watchpoint.\n"));
10573 uiout->text ("\n");
10574
10575 /* More than one watchpoint may have been triggered. */
10576 return PRINT_UNKNOWN;
10577 }
10578
10579 /* Implement the "print_one_detail" breakpoint_ops method for
10580 masked hardware watchpoints. */
10581
10582 static void
10583 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10584 struct ui_out *uiout)
10585 {
10586 struct watchpoint *w = (struct watchpoint *) b;
10587
10588 /* Masked watchpoints have only one location. */
10589 gdb_assert (b->loc && b->loc->next == NULL);
10590
10591 uiout->text ("\tmask ");
10592 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10593 uiout->text ("\n");
10594 }
10595
10596 /* Implement the "print_mention" breakpoint_ops method for
10597 masked hardware watchpoints. */
10598
10599 static void
10600 print_mention_masked_watchpoint (struct breakpoint *b)
10601 {
10602 struct watchpoint *w = (struct watchpoint *) b;
10603 struct ui_out *uiout = current_uiout;
10604 const char *tuple_name;
10605
10606 switch (b->type)
10607 {
10608 case bp_hardware_watchpoint:
10609 uiout->text ("Masked hardware watchpoint ");
10610 tuple_name = "wpt";
10611 break;
10612 case bp_read_watchpoint:
10613 uiout->text ("Masked hardware read watchpoint ");
10614 tuple_name = "hw-rwpt";
10615 break;
10616 case bp_access_watchpoint:
10617 uiout->text ("Masked hardware access (read/write) watchpoint ");
10618 tuple_name = "hw-awpt";
10619 break;
10620 default:
10621 internal_error (__FILE__, __LINE__,
10622 _("Invalid hardware watchpoint type."));
10623 }
10624
10625 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10626 uiout->field_int ("number", b->number);
10627 uiout->text (": ");
10628 uiout->field_string ("exp", w->exp_string);
10629 }
10630
10631 /* Implement the "print_recreate" breakpoint_ops method for
10632 masked hardware watchpoints. */
10633
10634 static void
10635 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10636 {
10637 struct watchpoint *w = (struct watchpoint *) b;
10638 char tmp[40];
10639
10640 switch (b->type)
10641 {
10642 case bp_hardware_watchpoint:
10643 fprintf_unfiltered (fp, "watch");
10644 break;
10645 case bp_read_watchpoint:
10646 fprintf_unfiltered (fp, "rwatch");
10647 break;
10648 case bp_access_watchpoint:
10649 fprintf_unfiltered (fp, "awatch");
10650 break;
10651 default:
10652 internal_error (__FILE__, __LINE__,
10653 _("Invalid hardware watchpoint type."));
10654 }
10655
10656 sprintf_vma (tmp, w->hw_wp_mask);
10657 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10658 print_recreate_thread (b, fp);
10659 }
10660
10661 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10662
10663 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10664
10665 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10666
10667 static int
10668 is_masked_watchpoint (const struct breakpoint *b)
10669 {
10670 return b->ops == &masked_watchpoint_breakpoint_ops;
10671 }
10672
10673 /* accessflag: hw_write: watch write,
10674 hw_read: watch read,
10675 hw_access: watch access (read or write) */
10676 static void
10677 watch_command_1 (const char *arg, int accessflag, int from_tty,
10678 int just_location, int internal)
10679 {
10680 struct breakpoint *scope_breakpoint = NULL;
10681 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10682 struct value *val, *mark, *result;
10683 int saved_bitpos = 0, saved_bitsize = 0;
10684 const char *exp_start = NULL;
10685 const char *exp_end = NULL;
10686 const char *tok, *end_tok;
10687 int toklen = -1;
10688 const char *cond_start = NULL;
10689 const char *cond_end = NULL;
10690 enum bptype bp_type;
10691 int thread = -1;
10692 int pc = 0;
10693 /* Flag to indicate whether we are going to use masks for
10694 the hardware watchpoint. */
10695 int use_mask = 0;
10696 CORE_ADDR mask = 0;
10697
10698 /* Make sure that we actually have parameters to parse. */
10699 if (arg != NULL && arg[0] != '\0')
10700 {
10701 const char *value_start;
10702
10703 exp_end = arg + strlen (arg);
10704
10705 /* Look for "parameter value" pairs at the end
10706 of the arguments string. */
10707 for (tok = exp_end - 1; tok > arg; tok--)
10708 {
10709 /* Skip whitespace at the end of the argument list. */
10710 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10711 tok--;
10712
10713 /* Find the beginning of the last token.
10714 This is the value of the parameter. */
10715 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10716 tok--;
10717 value_start = tok + 1;
10718
10719 /* Skip whitespace. */
10720 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10721 tok--;
10722
10723 end_tok = tok;
10724
10725 /* Find the beginning of the second to last token.
10726 This is the parameter itself. */
10727 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10728 tok--;
10729 tok++;
10730 toklen = end_tok - tok + 1;
10731
10732 if (toklen == 6 && startswith (tok, "thread"))
10733 {
10734 struct thread_info *thr;
10735 /* At this point we've found a "thread" token, which means
10736 the user is trying to set a watchpoint that triggers
10737 only in a specific thread. */
10738 const char *endp;
10739
10740 if (thread != -1)
10741 error(_("You can specify only one thread."));
10742
10743 /* Extract the thread ID from the next token. */
10744 thr = parse_thread_id (value_start, &endp);
10745
10746 /* Check if the user provided a valid thread ID. */
10747 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10748 invalid_thread_id_error (value_start);
10749
10750 thread = thr->global_num;
10751 }
10752 else if (toklen == 4 && startswith (tok, "mask"))
10753 {
10754 /* We've found a "mask" token, which means the user wants to
10755 create a hardware watchpoint that is going to have the mask
10756 facility. */
10757 struct value *mask_value, *mark;
10758
10759 if (use_mask)
10760 error(_("You can specify only one mask."));
10761
10762 use_mask = just_location = 1;
10763
10764 mark = value_mark ();
10765 mask_value = parse_to_comma_and_eval (&value_start);
10766 mask = value_as_address (mask_value);
10767 value_free_to_mark (mark);
10768 }
10769 else
10770 /* We didn't recognize what we found. We should stop here. */
10771 break;
10772
10773 /* Truncate the string and get rid of the "parameter value" pair before
10774 the arguments string is parsed by the parse_exp_1 function. */
10775 exp_end = tok;
10776 }
10777 }
10778 else
10779 exp_end = arg;
10780
10781 /* Parse the rest of the arguments. From here on out, everything
10782 is in terms of a newly allocated string instead of the original
10783 ARG. */
10784 innermost_block = NULL;
10785 std::string expression (arg, exp_end - arg);
10786 exp_start = arg = expression.c_str ();
10787 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
10788 exp_end = arg;
10789 /* Remove trailing whitespace from the expression before saving it.
10790 This makes the eventual display of the expression string a bit
10791 prettier. */
10792 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10793 --exp_end;
10794
10795 /* Checking if the expression is not constant. */
10796 if (watchpoint_exp_is_const (exp.get ()))
10797 {
10798 int len;
10799
10800 len = exp_end - exp_start;
10801 while (len > 0 && isspace (exp_start[len - 1]))
10802 len--;
10803 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10804 }
10805
10806 exp_valid_block = innermost_block;
10807 mark = value_mark ();
10808 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
10809
10810 if (val != NULL && just_location)
10811 {
10812 saved_bitpos = value_bitpos (val);
10813 saved_bitsize = value_bitsize (val);
10814 }
10815
10816 if (just_location)
10817 {
10818 int ret;
10819
10820 exp_valid_block = NULL;
10821 val = value_addr (result);
10822 release_value (val);
10823 value_free_to_mark (mark);
10824
10825 if (use_mask)
10826 {
10827 ret = target_masked_watch_num_registers (value_as_address (val),
10828 mask);
10829 if (ret == -1)
10830 error (_("This target does not support masked watchpoints."));
10831 else if (ret == -2)
10832 error (_("Invalid mask or memory region."));
10833 }
10834 }
10835 else if (val != NULL)
10836 release_value (val);
10837
10838 tok = skip_spaces (arg);
10839 end_tok = skip_to_space (tok);
10840
10841 toklen = end_tok - tok;
10842 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10843 {
10844 innermost_block = NULL;
10845 tok = cond_start = end_tok + 1;
10846 parse_exp_1 (&tok, 0, 0, 0);
10847
10848 /* The watchpoint expression may not be local, but the condition
10849 may still be. E.g.: `watch global if local > 0'. */
10850 cond_exp_valid_block = innermost_block;
10851
10852 cond_end = tok;
10853 }
10854 if (*tok)
10855 error (_("Junk at end of command."));
10856
10857 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10858
10859 /* Save this because create_internal_breakpoint below invalidates
10860 'wp_frame'. */
10861 frame_id watchpoint_frame = get_frame_id (wp_frame);
10862
10863 /* If the expression is "local", then set up a "watchpoint scope"
10864 breakpoint at the point where we've left the scope of the watchpoint
10865 expression. Create the scope breakpoint before the watchpoint, so
10866 that we will encounter it first in bpstat_stop_status. */
10867 if (exp_valid_block != NULL && wp_frame != NULL)
10868 {
10869 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10870
10871 if (frame_id_p (caller_frame_id))
10872 {
10873 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10874 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10875
10876 scope_breakpoint
10877 = create_internal_breakpoint (caller_arch, caller_pc,
10878 bp_watchpoint_scope,
10879 &momentary_breakpoint_ops);
10880
10881 /* create_internal_breakpoint could invalidate WP_FRAME. */
10882 wp_frame = NULL;
10883
10884 scope_breakpoint->enable_state = bp_enabled;
10885
10886 /* Automatically delete the breakpoint when it hits. */
10887 scope_breakpoint->disposition = disp_del;
10888
10889 /* Only break in the proper frame (help with recursion). */
10890 scope_breakpoint->frame_id = caller_frame_id;
10891
10892 /* Set the address at which we will stop. */
10893 scope_breakpoint->loc->gdbarch = caller_arch;
10894 scope_breakpoint->loc->requested_address = caller_pc;
10895 scope_breakpoint->loc->address
10896 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10897 scope_breakpoint->loc->requested_address,
10898 scope_breakpoint->type);
10899 }
10900 }
10901
10902 /* Now set up the breakpoint. We create all watchpoints as hardware
10903 watchpoints here even if hardware watchpoints are turned off, a call
10904 to update_watchpoint later in this function will cause the type to
10905 drop back to bp_watchpoint (software watchpoint) if required. */
10906
10907 if (accessflag == hw_read)
10908 bp_type = bp_read_watchpoint;
10909 else if (accessflag == hw_access)
10910 bp_type = bp_access_watchpoint;
10911 else
10912 bp_type = bp_hardware_watchpoint;
10913
10914 std::unique_ptr<watchpoint> w (new watchpoint ());
10915
10916 if (use_mask)
10917 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10918 &masked_watchpoint_breakpoint_ops);
10919 else
10920 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10921 &watchpoint_breakpoint_ops);
10922 w->thread = thread;
10923 w->disposition = disp_donttouch;
10924 w->pspace = current_program_space;
10925 w->exp = std::move (exp);
10926 w->exp_valid_block = exp_valid_block;
10927 w->cond_exp_valid_block = cond_exp_valid_block;
10928 if (just_location)
10929 {
10930 struct type *t = value_type (val);
10931 CORE_ADDR addr = value_as_address (val);
10932
10933 w->exp_string_reparse
10934 = current_language->la_watch_location_expression (t, addr).release ();
10935
10936 w->exp_string = xstrprintf ("-location %.*s",
10937 (int) (exp_end - exp_start), exp_start);
10938 }
10939 else
10940 w->exp_string = savestring (exp_start, exp_end - exp_start);
10941
10942 if (use_mask)
10943 {
10944 w->hw_wp_mask = mask;
10945 }
10946 else
10947 {
10948 w->val = val;
10949 w->val_bitpos = saved_bitpos;
10950 w->val_bitsize = saved_bitsize;
10951 w->val_valid = 1;
10952 }
10953
10954 if (cond_start)
10955 w->cond_string = savestring (cond_start, cond_end - cond_start);
10956 else
10957 w->cond_string = 0;
10958
10959 if (frame_id_p (watchpoint_frame))
10960 {
10961 w->watchpoint_frame = watchpoint_frame;
10962 w->watchpoint_thread = inferior_ptid;
10963 }
10964 else
10965 {
10966 w->watchpoint_frame = null_frame_id;
10967 w->watchpoint_thread = null_ptid;
10968 }
10969
10970 if (scope_breakpoint != NULL)
10971 {
10972 /* The scope breakpoint is related to the watchpoint. We will
10973 need to act on them together. */
10974 w->related_breakpoint = scope_breakpoint;
10975 scope_breakpoint->related_breakpoint = w.get ();
10976 }
10977
10978 if (!just_location)
10979 value_free_to_mark (mark);
10980
10981 /* Finally update the new watchpoint. This creates the locations
10982 that should be inserted. */
10983 update_watchpoint (w.get (), 1);
10984
10985 install_breakpoint (internal, std::move (w), 1);
10986 }
10987
10988 /* Return count of debug registers needed to watch the given expression.
10989 If the watchpoint cannot be handled in hardware return zero. */
10990
10991 static int
10992 can_use_hardware_watchpoint (struct value *v)
10993 {
10994 int found_memory_cnt = 0;
10995 struct value *head = v;
10996
10997 /* Did the user specifically forbid us to use hardware watchpoints? */
10998 if (!can_use_hw_watchpoints)
10999 return 0;
11000
11001 /* Make sure that the value of the expression depends only upon
11002 memory contents, and values computed from them within GDB. If we
11003 find any register references or function calls, we can't use a
11004 hardware watchpoint.
11005
11006 The idea here is that evaluating an expression generates a series
11007 of values, one holding the value of every subexpression. (The
11008 expression a*b+c has five subexpressions: a, b, a*b, c, and
11009 a*b+c.) GDB's values hold almost enough information to establish
11010 the criteria given above --- they identify memory lvalues,
11011 register lvalues, computed values, etcetera. So we can evaluate
11012 the expression, and then scan the chain of values that leaves
11013 behind to decide whether we can detect any possible change to the
11014 expression's final value using only hardware watchpoints.
11015
11016 However, I don't think that the values returned by inferior
11017 function calls are special in any way. So this function may not
11018 notice that an expression involving an inferior function call
11019 can't be watched with hardware watchpoints. FIXME. */
11020 for (; v; v = value_next (v))
11021 {
11022 if (VALUE_LVAL (v) == lval_memory)
11023 {
11024 if (v != head && value_lazy (v))
11025 /* A lazy memory lvalue in the chain is one that GDB never
11026 needed to fetch; we either just used its address (e.g.,
11027 `a' in `a.b') or we never needed it at all (e.g., `a'
11028 in `a,b'). This doesn't apply to HEAD; if that is
11029 lazy then it was not readable, but watch it anyway. */
11030 ;
11031 else
11032 {
11033 /* Ahh, memory we actually used! Check if we can cover
11034 it with hardware watchpoints. */
11035 struct type *vtype = check_typedef (value_type (v));
11036
11037 /* We only watch structs and arrays if user asked for it
11038 explicitly, never if they just happen to appear in a
11039 middle of some value chain. */
11040 if (v == head
11041 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11042 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11043 {
11044 CORE_ADDR vaddr = value_address (v);
11045 int len;
11046 int num_regs;
11047
11048 len = (target_exact_watchpoints
11049 && is_scalar_type_recursive (vtype))?
11050 1 : TYPE_LENGTH (value_type (v));
11051
11052 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11053 if (!num_regs)
11054 return 0;
11055 else
11056 found_memory_cnt += num_regs;
11057 }
11058 }
11059 }
11060 else if (VALUE_LVAL (v) != not_lval
11061 && deprecated_value_modifiable (v) == 0)
11062 return 0; /* These are values from the history (e.g., $1). */
11063 else if (VALUE_LVAL (v) == lval_register)
11064 return 0; /* Cannot watch a register with a HW watchpoint. */
11065 }
11066
11067 /* The expression itself looks suitable for using a hardware
11068 watchpoint, but give the target machine a chance to reject it. */
11069 return found_memory_cnt;
11070 }
11071
11072 void
11073 watch_command_wrapper (const char *arg, int from_tty, int internal)
11074 {
11075 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11076 }
11077
11078 /* A helper function that looks for the "-location" argument and then
11079 calls watch_command_1. */
11080
11081 static void
11082 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11083 {
11084 int just_location = 0;
11085
11086 if (arg
11087 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11088 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11089 {
11090 arg = skip_spaces (arg);
11091 just_location = 1;
11092 }
11093
11094 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11095 }
11096
11097 static void
11098 watch_command (char *arg, int from_tty)
11099 {
11100 watch_maybe_just_location (arg, hw_write, from_tty);
11101 }
11102
11103 void
11104 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
11105 {
11106 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11107 }
11108
11109 static void
11110 rwatch_command (char *arg, int from_tty)
11111 {
11112 watch_maybe_just_location (arg, hw_read, from_tty);
11113 }
11114
11115 void
11116 awatch_command_wrapper (const char *arg, int from_tty, int internal)
11117 {
11118 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11119 }
11120
11121 static void
11122 awatch_command (char *arg, int from_tty)
11123 {
11124 watch_maybe_just_location (arg, hw_access, from_tty);
11125 }
11126 \f
11127
11128 /* Data for the FSM that manages the until(location)/advance commands
11129 in infcmd.c. Here because it uses the mechanisms of
11130 breakpoints. */
11131
11132 struct until_break_fsm
11133 {
11134 /* The base class. */
11135 struct thread_fsm thread_fsm;
11136
11137 /* The thread that as current when the command was executed. */
11138 int thread;
11139
11140 /* The breakpoint set at the destination location. */
11141 struct breakpoint *location_breakpoint;
11142
11143 /* Breakpoint set at the return address in the caller frame. May be
11144 NULL. */
11145 struct breakpoint *caller_breakpoint;
11146 };
11147
11148 static void until_break_fsm_clean_up (struct thread_fsm *self,
11149 struct thread_info *thread);
11150 static int until_break_fsm_should_stop (struct thread_fsm *self,
11151 struct thread_info *thread);
11152 static enum async_reply_reason
11153 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11154
11155 /* until_break_fsm's vtable. */
11156
11157 static struct thread_fsm_ops until_break_fsm_ops =
11158 {
11159 NULL, /* dtor */
11160 until_break_fsm_clean_up,
11161 until_break_fsm_should_stop,
11162 NULL, /* return_value */
11163 until_break_fsm_async_reply_reason,
11164 };
11165
11166 /* Allocate a new until_break_command_fsm. */
11167
11168 static struct until_break_fsm *
11169 new_until_break_fsm (struct interp *cmd_interp, int thread,
11170 struct breakpoint *location_breakpoint,
11171 struct breakpoint *caller_breakpoint)
11172 {
11173 struct until_break_fsm *sm;
11174
11175 sm = XCNEW (struct until_break_fsm);
11176 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11177
11178 sm->thread = thread;
11179 sm->location_breakpoint = location_breakpoint;
11180 sm->caller_breakpoint = caller_breakpoint;
11181
11182 return sm;
11183 }
11184
11185 /* Implementation of the 'should_stop' FSM method for the
11186 until(location)/advance commands. */
11187
11188 static int
11189 until_break_fsm_should_stop (struct thread_fsm *self,
11190 struct thread_info *tp)
11191 {
11192 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11193
11194 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11195 sm->location_breakpoint) != NULL
11196 || (sm->caller_breakpoint != NULL
11197 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11198 sm->caller_breakpoint) != NULL))
11199 thread_fsm_set_finished (self);
11200
11201 return 1;
11202 }
11203
11204 /* Implementation of the 'clean_up' FSM method for the
11205 until(location)/advance commands. */
11206
11207 static void
11208 until_break_fsm_clean_up (struct thread_fsm *self,
11209 struct thread_info *thread)
11210 {
11211 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11212
11213 /* Clean up our temporary breakpoints. */
11214 if (sm->location_breakpoint != NULL)
11215 {
11216 delete_breakpoint (sm->location_breakpoint);
11217 sm->location_breakpoint = NULL;
11218 }
11219 if (sm->caller_breakpoint != NULL)
11220 {
11221 delete_breakpoint (sm->caller_breakpoint);
11222 sm->caller_breakpoint = NULL;
11223 }
11224 delete_longjmp_breakpoint (sm->thread);
11225 }
11226
11227 /* Implementation of the 'async_reply_reason' FSM method for the
11228 until(location)/advance commands. */
11229
11230 static enum async_reply_reason
11231 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11232 {
11233 return EXEC_ASYNC_LOCATION_REACHED;
11234 }
11235
11236 void
11237 until_break_command (const char *arg, int from_tty, int anywhere)
11238 {
11239 struct frame_info *frame;
11240 struct gdbarch *frame_gdbarch;
11241 struct frame_id stack_frame_id;
11242 struct frame_id caller_frame_id;
11243 struct breakpoint *location_breakpoint;
11244 struct breakpoint *caller_breakpoint = NULL;
11245 struct cleanup *old_chain;
11246 int thread;
11247 struct thread_info *tp;
11248 struct until_break_fsm *sm;
11249
11250 clear_proceed_status (0);
11251
11252 /* Set a breakpoint where the user wants it and at return from
11253 this function. */
11254
11255 event_location_up location = string_to_event_location (&arg, current_language);
11256
11257 std::vector<symtab_and_line> sals
11258 = (last_displayed_sal_is_valid ()
11259 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11260 get_last_displayed_symtab (),
11261 get_last_displayed_line ())
11262 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11263 NULL, (struct symtab *) NULL, 0));
11264
11265 if (sals.size () != 1)
11266 error (_("Couldn't get information on specified line."));
11267
11268 symtab_and_line &sal = sals[0];
11269
11270 if (*arg)
11271 error (_("Junk at end of arguments."));
11272
11273 resolve_sal_pc (&sal);
11274
11275 tp = inferior_thread ();
11276 thread = tp->global_num;
11277
11278 old_chain = make_cleanup (null_cleanup, NULL);
11279
11280 /* Note linespec handling above invalidates the frame chain.
11281 Installing a breakpoint also invalidates the frame chain (as it
11282 may need to switch threads), so do any frame handling before
11283 that. */
11284
11285 frame = get_selected_frame (NULL);
11286 frame_gdbarch = get_frame_arch (frame);
11287 stack_frame_id = get_stack_frame_id (frame);
11288 caller_frame_id = frame_unwind_caller_id (frame);
11289
11290 /* Keep within the current frame, or in frames called by the current
11291 one. */
11292
11293 if (frame_id_p (caller_frame_id))
11294 {
11295 struct symtab_and_line sal2;
11296 struct gdbarch *caller_gdbarch;
11297
11298 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11299 sal2.pc = frame_unwind_caller_pc (frame);
11300 caller_gdbarch = frame_unwind_caller_arch (frame);
11301 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11302 sal2,
11303 caller_frame_id,
11304 bp_until);
11305 make_cleanup_delete_breakpoint (caller_breakpoint);
11306
11307 set_longjmp_breakpoint (tp, caller_frame_id);
11308 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11309 }
11310
11311 /* set_momentary_breakpoint could invalidate FRAME. */
11312 frame = NULL;
11313
11314 if (anywhere)
11315 /* If the user told us to continue until a specified location,
11316 we don't specify a frame at which we need to stop. */
11317 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11318 null_frame_id, bp_until);
11319 else
11320 /* Otherwise, specify the selected frame, because we want to stop
11321 only at the very same frame. */
11322 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11323 stack_frame_id, bp_until);
11324 make_cleanup_delete_breakpoint (location_breakpoint);
11325
11326 sm = new_until_break_fsm (command_interp (), tp->global_num,
11327 location_breakpoint, caller_breakpoint);
11328 tp->thread_fsm = &sm->thread_fsm;
11329
11330 discard_cleanups (old_chain);
11331
11332 proceed (-1, GDB_SIGNAL_DEFAULT);
11333 }
11334
11335 /* This function attempts to parse an optional "if <cond>" clause
11336 from the arg string. If one is not found, it returns NULL.
11337
11338 Else, it returns a pointer to the condition string. (It does not
11339 attempt to evaluate the string against a particular block.) And,
11340 it updates arg to point to the first character following the parsed
11341 if clause in the arg string. */
11342
11343 const char *
11344 ep_parse_optional_if_clause (const char **arg)
11345 {
11346 const char *cond_string;
11347
11348 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11349 return NULL;
11350
11351 /* Skip the "if" keyword. */
11352 (*arg) += 2;
11353
11354 /* Skip any extra leading whitespace, and record the start of the
11355 condition string. */
11356 *arg = skip_spaces (*arg);
11357 cond_string = *arg;
11358
11359 /* Assume that the condition occupies the remainder of the arg
11360 string. */
11361 (*arg) += strlen (cond_string);
11362
11363 return cond_string;
11364 }
11365
11366 /* Commands to deal with catching events, such as signals, exceptions,
11367 process start/exit, etc. */
11368
11369 typedef enum
11370 {
11371 catch_fork_temporary, catch_vfork_temporary,
11372 catch_fork_permanent, catch_vfork_permanent
11373 }
11374 catch_fork_kind;
11375
11376 static void
11377 catch_fork_command_1 (char *arg_entry, int from_tty,
11378 struct cmd_list_element *command)
11379 {
11380 const char *arg = arg_entry;
11381 struct gdbarch *gdbarch = get_current_arch ();
11382 const char *cond_string = NULL;
11383 catch_fork_kind fork_kind;
11384 int tempflag;
11385
11386 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11387 tempflag = (fork_kind == catch_fork_temporary
11388 || fork_kind == catch_vfork_temporary);
11389
11390 if (!arg)
11391 arg = "";
11392 arg = skip_spaces (arg);
11393
11394 /* The allowed syntax is:
11395 catch [v]fork
11396 catch [v]fork if <cond>
11397
11398 First, check if there's an if clause. */
11399 cond_string = ep_parse_optional_if_clause (&arg);
11400
11401 if ((*arg != '\0') && !isspace (*arg))
11402 error (_("Junk at end of arguments."));
11403
11404 /* If this target supports it, create a fork or vfork catchpoint
11405 and enable reporting of such events. */
11406 switch (fork_kind)
11407 {
11408 case catch_fork_temporary:
11409 case catch_fork_permanent:
11410 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11411 &catch_fork_breakpoint_ops);
11412 break;
11413 case catch_vfork_temporary:
11414 case catch_vfork_permanent:
11415 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11416 &catch_vfork_breakpoint_ops);
11417 break;
11418 default:
11419 error (_("unsupported or unknown fork kind; cannot catch it"));
11420 break;
11421 }
11422 }
11423
11424 static void
11425 catch_exec_command_1 (char *arg_entry, int from_tty,
11426 struct cmd_list_element *command)
11427 {
11428 const char *arg = arg_entry;
11429 struct gdbarch *gdbarch = get_current_arch ();
11430 int tempflag;
11431 const char *cond_string = NULL;
11432
11433 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11434
11435 if (!arg)
11436 arg = "";
11437 arg = skip_spaces (arg);
11438
11439 /* The allowed syntax is:
11440 catch exec
11441 catch exec if <cond>
11442
11443 First, check if there's an if clause. */
11444 cond_string = ep_parse_optional_if_clause (&arg);
11445
11446 if ((*arg != '\0') && !isspace (*arg))
11447 error (_("Junk at end of arguments."));
11448
11449 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11450 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11451 &catch_exec_breakpoint_ops);
11452 c->exec_pathname = NULL;
11453
11454 install_breakpoint (0, std::move (c), 1);
11455 }
11456
11457 void
11458 init_ada_exception_breakpoint (struct breakpoint *b,
11459 struct gdbarch *gdbarch,
11460 struct symtab_and_line sal,
11461 const char *addr_string,
11462 const struct breakpoint_ops *ops,
11463 int tempflag,
11464 int enabled,
11465 int from_tty)
11466 {
11467 if (from_tty)
11468 {
11469 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11470 if (!loc_gdbarch)
11471 loc_gdbarch = gdbarch;
11472
11473 describe_other_breakpoints (loc_gdbarch,
11474 sal.pspace, sal.pc, sal.section, -1);
11475 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11476 version for exception catchpoints, because two catchpoints
11477 used for different exception names will use the same address.
11478 In this case, a "breakpoint ... also set at..." warning is
11479 unproductive. Besides, the warning phrasing is also a bit
11480 inappropriate, we should use the word catchpoint, and tell
11481 the user what type of catchpoint it is. The above is good
11482 enough for now, though. */
11483 }
11484
11485 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11486
11487 b->enable_state = enabled ? bp_enabled : bp_disabled;
11488 b->disposition = tempflag ? disp_del : disp_donttouch;
11489 b->location = string_to_event_location (&addr_string,
11490 language_def (language_ada));
11491 b->language = language_ada;
11492 }
11493
11494 static void
11495 catch_command (const char *arg, int from_tty)
11496 {
11497 error (_("Catch requires an event name."));
11498 }
11499 \f
11500
11501 static void
11502 tcatch_command (const char *arg, int from_tty)
11503 {
11504 error (_("Catch requires an event name."));
11505 }
11506
11507 /* Compare two breakpoints and return a strcmp-like result. */
11508
11509 static int
11510 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11511 {
11512 uintptr_t ua = (uintptr_t) a;
11513 uintptr_t ub = (uintptr_t) b;
11514
11515 if (a->number < b->number)
11516 return -1;
11517 else if (a->number > b->number)
11518 return 1;
11519
11520 /* Now sort by address, in case we see, e..g, two breakpoints with
11521 the number 0. */
11522 if (ua < ub)
11523 return -1;
11524 return ua > ub ? 1 : 0;
11525 }
11526
11527 /* Delete breakpoints by address or line. */
11528
11529 static void
11530 clear_command (char *arg, int from_tty)
11531 {
11532 struct breakpoint *b;
11533 int default_match;
11534 int i;
11535
11536 std::vector<symtab_and_line> decoded_sals;
11537 symtab_and_line last_sal;
11538 gdb::array_view<symtab_and_line> sals;
11539 if (arg)
11540 {
11541 decoded_sals
11542 = decode_line_with_current_source (arg,
11543 (DECODE_LINE_FUNFIRSTLINE
11544 | DECODE_LINE_LIST_MODE));
11545 default_match = 0;
11546 sals = decoded_sals;
11547 }
11548 else
11549 {
11550 /* Set sal's line, symtab, pc, and pspace to the values
11551 corresponding to the last call to print_frame_info. If the
11552 codepoint is not valid, this will set all the fields to 0. */
11553 last_sal = get_last_displayed_sal ();
11554 if (last_sal.symtab == 0)
11555 error (_("No source file specified."));
11556
11557 default_match = 1;
11558 sals = last_sal;
11559 }
11560
11561 /* We don't call resolve_sal_pc here. That's not as bad as it
11562 seems, because all existing breakpoints typically have both
11563 file/line and pc set. So, if clear is given file/line, we can
11564 match this to existing breakpoint without obtaining pc at all.
11565
11566 We only support clearing given the address explicitly
11567 present in breakpoint table. Say, we've set breakpoint
11568 at file:line. There were several PC values for that file:line,
11569 due to optimization, all in one block.
11570
11571 We've picked one PC value. If "clear" is issued with another
11572 PC corresponding to the same file:line, the breakpoint won't
11573 be cleared. We probably can still clear the breakpoint, but
11574 since the other PC value is never presented to user, user
11575 can only find it by guessing, and it does not seem important
11576 to support that. */
11577
11578 /* For each line spec given, delete bps which correspond to it. Do
11579 it in two passes, solely to preserve the current behavior that
11580 from_tty is forced true if we delete more than one
11581 breakpoint. */
11582
11583 std::vector<struct breakpoint *> found;
11584 for (const auto &sal : sals)
11585 {
11586 const char *sal_fullname;
11587
11588 /* If exact pc given, clear bpts at that pc.
11589 If line given (pc == 0), clear all bpts on specified line.
11590 If defaulting, clear all bpts on default line
11591 or at default pc.
11592
11593 defaulting sal.pc != 0 tests to do
11594
11595 0 1 pc
11596 1 1 pc _and_ line
11597 0 0 line
11598 1 0 <can't happen> */
11599
11600 sal_fullname = (sal.symtab == NULL
11601 ? NULL : symtab_to_fullname (sal.symtab));
11602
11603 /* Find all matching breakpoints and add them to 'found'. */
11604 ALL_BREAKPOINTS (b)
11605 {
11606 int match = 0;
11607 /* Are we going to delete b? */
11608 if (b->type != bp_none && !is_watchpoint (b))
11609 {
11610 struct bp_location *loc = b->loc;
11611 for (; loc; loc = loc->next)
11612 {
11613 /* If the user specified file:line, don't allow a PC
11614 match. This matches historical gdb behavior. */
11615 int pc_match = (!sal.explicit_line
11616 && sal.pc
11617 && (loc->pspace == sal.pspace)
11618 && (loc->address == sal.pc)
11619 && (!section_is_overlay (loc->section)
11620 || loc->section == sal.section));
11621 int line_match = 0;
11622
11623 if ((default_match || sal.explicit_line)
11624 && loc->symtab != NULL
11625 && sal_fullname != NULL
11626 && sal.pspace == loc->pspace
11627 && loc->line_number == sal.line
11628 && filename_cmp (symtab_to_fullname (loc->symtab),
11629 sal_fullname) == 0)
11630 line_match = 1;
11631
11632 if (pc_match || line_match)
11633 {
11634 match = 1;
11635 break;
11636 }
11637 }
11638 }
11639
11640 if (match)
11641 found.push_back (b);
11642 }
11643 }
11644
11645 /* Now go thru the 'found' chain and delete them. */
11646 if (found.empty ())
11647 {
11648 if (arg)
11649 error (_("No breakpoint at %s."), arg);
11650 else
11651 error (_("No breakpoint at this line."));
11652 }
11653
11654 /* Remove duplicates from the vec. */
11655 std::sort (found.begin (), found.end (),
11656 [] (const breakpoint *a, const breakpoint *b)
11657 {
11658 return compare_breakpoints (a, b) < 0;
11659 });
11660 found.erase (std::unique (found.begin (), found.end (),
11661 [] (const breakpoint *a, const breakpoint *b)
11662 {
11663 return compare_breakpoints (a, b) == 0;
11664 }),
11665 found.end ());
11666
11667 if (found.size () > 1)
11668 from_tty = 1; /* Always report if deleted more than one. */
11669 if (from_tty)
11670 {
11671 if (found.size () == 1)
11672 printf_unfiltered (_("Deleted breakpoint "));
11673 else
11674 printf_unfiltered (_("Deleted breakpoints "));
11675 }
11676
11677 for (breakpoint *iter : found)
11678 {
11679 if (from_tty)
11680 printf_unfiltered ("%d ", iter->number);
11681 delete_breakpoint (iter);
11682 }
11683 if (from_tty)
11684 putchar_unfiltered ('\n');
11685 }
11686 \f
11687 /* Delete breakpoint in BS if they are `delete' breakpoints and
11688 all breakpoints that are marked for deletion, whether hit or not.
11689 This is called after any breakpoint is hit, or after errors. */
11690
11691 void
11692 breakpoint_auto_delete (bpstat bs)
11693 {
11694 struct breakpoint *b, *b_tmp;
11695
11696 for (; bs; bs = bs->next)
11697 if (bs->breakpoint_at
11698 && bs->breakpoint_at->disposition == disp_del
11699 && bs->stop)
11700 delete_breakpoint (bs->breakpoint_at);
11701
11702 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11703 {
11704 if (b->disposition == disp_del_at_next_stop)
11705 delete_breakpoint (b);
11706 }
11707 }
11708
11709 /* A comparison function for bp_location AP and BP being interfaced to
11710 qsort. Sort elements primarily by their ADDRESS (no matter what
11711 does breakpoint_address_is_meaningful say for its OWNER),
11712 secondarily by ordering first permanent elements and
11713 terciarily just ensuring the array is sorted stable way despite
11714 qsort being an unstable algorithm. */
11715
11716 static int
11717 bp_locations_compare (const void *ap, const void *bp)
11718 {
11719 const struct bp_location *a = *(const struct bp_location **) ap;
11720 const struct bp_location *b = *(const struct bp_location **) bp;
11721
11722 if (a->address != b->address)
11723 return (a->address > b->address) - (a->address < b->address);
11724
11725 /* Sort locations at the same address by their pspace number, keeping
11726 locations of the same inferior (in a multi-inferior environment)
11727 grouped. */
11728
11729 if (a->pspace->num != b->pspace->num)
11730 return ((a->pspace->num > b->pspace->num)
11731 - (a->pspace->num < b->pspace->num));
11732
11733 /* Sort permanent breakpoints first. */
11734 if (a->permanent != b->permanent)
11735 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11736
11737 /* Make the internal GDB representation stable across GDB runs
11738 where A and B memory inside GDB can differ. Breakpoint locations of
11739 the same type at the same address can be sorted in arbitrary order. */
11740
11741 if (a->owner->number != b->owner->number)
11742 return ((a->owner->number > b->owner->number)
11743 - (a->owner->number < b->owner->number));
11744
11745 return (a > b) - (a < b);
11746 }
11747
11748 /* Set bp_locations_placed_address_before_address_max and
11749 bp_locations_shadow_len_after_address_max according to the current
11750 content of the bp_locations array. */
11751
11752 static void
11753 bp_locations_target_extensions_update (void)
11754 {
11755 struct bp_location *bl, **blp_tmp;
11756
11757 bp_locations_placed_address_before_address_max = 0;
11758 bp_locations_shadow_len_after_address_max = 0;
11759
11760 ALL_BP_LOCATIONS (bl, blp_tmp)
11761 {
11762 CORE_ADDR start, end, addr;
11763
11764 if (!bp_location_has_shadow (bl))
11765 continue;
11766
11767 start = bl->target_info.placed_address;
11768 end = start + bl->target_info.shadow_len;
11769
11770 gdb_assert (bl->address >= start);
11771 addr = bl->address - start;
11772 if (addr > bp_locations_placed_address_before_address_max)
11773 bp_locations_placed_address_before_address_max = addr;
11774
11775 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11776
11777 gdb_assert (bl->address < end);
11778 addr = end - bl->address;
11779 if (addr > bp_locations_shadow_len_after_address_max)
11780 bp_locations_shadow_len_after_address_max = addr;
11781 }
11782 }
11783
11784 /* Download tracepoint locations if they haven't been. */
11785
11786 static void
11787 download_tracepoint_locations (void)
11788 {
11789 struct breakpoint *b;
11790 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11791
11792 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11793
11794 ALL_TRACEPOINTS (b)
11795 {
11796 struct bp_location *bl;
11797 struct tracepoint *t;
11798 int bp_location_downloaded = 0;
11799
11800 if ((b->type == bp_fast_tracepoint
11801 ? !may_insert_fast_tracepoints
11802 : !may_insert_tracepoints))
11803 continue;
11804
11805 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11806 {
11807 if (target_can_download_tracepoint ())
11808 can_download_tracepoint = TRIBOOL_TRUE;
11809 else
11810 can_download_tracepoint = TRIBOOL_FALSE;
11811 }
11812
11813 if (can_download_tracepoint == TRIBOOL_FALSE)
11814 break;
11815
11816 for (bl = b->loc; bl; bl = bl->next)
11817 {
11818 /* In tracepoint, locations are _never_ duplicated, so
11819 should_be_inserted is equivalent to
11820 unduplicated_should_be_inserted. */
11821 if (!should_be_inserted (bl) || bl->inserted)
11822 continue;
11823
11824 switch_to_program_space_and_thread (bl->pspace);
11825
11826 target_download_tracepoint (bl);
11827
11828 bl->inserted = 1;
11829 bp_location_downloaded = 1;
11830 }
11831 t = (struct tracepoint *) b;
11832 t->number_on_target = b->number;
11833 if (bp_location_downloaded)
11834 observer_notify_breakpoint_modified (b);
11835 }
11836 }
11837
11838 /* Swap the insertion/duplication state between two locations. */
11839
11840 static void
11841 swap_insertion (struct bp_location *left, struct bp_location *right)
11842 {
11843 const int left_inserted = left->inserted;
11844 const int left_duplicate = left->duplicate;
11845 const int left_needs_update = left->needs_update;
11846 const struct bp_target_info left_target_info = left->target_info;
11847
11848 /* Locations of tracepoints can never be duplicated. */
11849 if (is_tracepoint (left->owner))
11850 gdb_assert (!left->duplicate);
11851 if (is_tracepoint (right->owner))
11852 gdb_assert (!right->duplicate);
11853
11854 left->inserted = right->inserted;
11855 left->duplicate = right->duplicate;
11856 left->needs_update = right->needs_update;
11857 left->target_info = right->target_info;
11858 right->inserted = left_inserted;
11859 right->duplicate = left_duplicate;
11860 right->needs_update = left_needs_update;
11861 right->target_info = left_target_info;
11862 }
11863
11864 /* Force the re-insertion of the locations at ADDRESS. This is called
11865 once a new/deleted/modified duplicate location is found and we are evaluating
11866 conditions on the target's side. Such conditions need to be updated on
11867 the target. */
11868
11869 static void
11870 force_breakpoint_reinsertion (struct bp_location *bl)
11871 {
11872 struct bp_location **locp = NULL, **loc2p;
11873 struct bp_location *loc;
11874 CORE_ADDR address = 0;
11875 int pspace_num;
11876
11877 address = bl->address;
11878 pspace_num = bl->pspace->num;
11879
11880 /* This is only meaningful if the target is
11881 evaluating conditions and if the user has
11882 opted for condition evaluation on the target's
11883 side. */
11884 if (gdb_evaluates_breakpoint_condition_p ()
11885 || !target_supports_evaluation_of_breakpoint_conditions ())
11886 return;
11887
11888 /* Flag all breakpoint locations with this address and
11889 the same program space as the location
11890 as "its condition has changed". We need to
11891 update the conditions on the target's side. */
11892 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11893 {
11894 loc = *loc2p;
11895
11896 if (!is_breakpoint (loc->owner)
11897 || pspace_num != loc->pspace->num)
11898 continue;
11899
11900 /* Flag the location appropriately. We use a different state to
11901 let everyone know that we already updated the set of locations
11902 with addr bl->address and program space bl->pspace. This is so
11903 we don't have to keep calling these functions just to mark locations
11904 that have already been marked. */
11905 loc->condition_changed = condition_updated;
11906
11907 /* Free the agent expression bytecode as well. We will compute
11908 it later on. */
11909 loc->cond_bytecode.reset ();
11910 }
11911 }
11912 /* Called whether new breakpoints are created, or existing breakpoints
11913 deleted, to update the global location list and recompute which
11914 locations are duplicate of which.
11915
11916 The INSERT_MODE flag determines whether locations may not, may, or
11917 shall be inserted now. See 'enum ugll_insert_mode' for more
11918 info. */
11919
11920 static void
11921 update_global_location_list (enum ugll_insert_mode insert_mode)
11922 {
11923 struct breakpoint *b;
11924 struct bp_location **locp, *loc;
11925 /* Last breakpoint location address that was marked for update. */
11926 CORE_ADDR last_addr = 0;
11927 /* Last breakpoint location program space that was marked for update. */
11928 int last_pspace_num = -1;
11929
11930 /* Used in the duplicates detection below. When iterating over all
11931 bp_locations, points to the first bp_location of a given address.
11932 Breakpoints and watchpoints of different types are never
11933 duplicates of each other. Keep one pointer for each type of
11934 breakpoint/watchpoint, so we only need to loop over all locations
11935 once. */
11936 struct bp_location *bp_loc_first; /* breakpoint */
11937 struct bp_location *wp_loc_first; /* hardware watchpoint */
11938 struct bp_location *awp_loc_first; /* access watchpoint */
11939 struct bp_location *rwp_loc_first; /* read watchpoint */
11940
11941 /* Saved former bp_locations array which we compare against the newly
11942 built bp_locations from the current state of ALL_BREAKPOINTS. */
11943 struct bp_location **old_locp;
11944 unsigned old_locations_count;
11945 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11946
11947 old_locations_count = bp_locations_count;
11948 bp_locations = NULL;
11949 bp_locations_count = 0;
11950
11951 ALL_BREAKPOINTS (b)
11952 for (loc = b->loc; loc; loc = loc->next)
11953 bp_locations_count++;
11954
11955 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11956 locp = bp_locations;
11957 ALL_BREAKPOINTS (b)
11958 for (loc = b->loc; loc; loc = loc->next)
11959 *locp++ = loc;
11960 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11961 bp_locations_compare);
11962
11963 bp_locations_target_extensions_update ();
11964
11965 /* Identify bp_location instances that are no longer present in the
11966 new list, and therefore should be freed. Note that it's not
11967 necessary that those locations should be removed from inferior --
11968 if there's another location at the same address (previously
11969 marked as duplicate), we don't need to remove/insert the
11970 location.
11971
11972 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11973 and former bp_location array state respectively. */
11974
11975 locp = bp_locations;
11976 for (old_locp = old_locations.get ();
11977 old_locp < old_locations.get () + old_locations_count;
11978 old_locp++)
11979 {
11980 struct bp_location *old_loc = *old_locp;
11981 struct bp_location **loc2p;
11982
11983 /* Tells if 'old_loc' is found among the new locations. If
11984 not, we have to free it. */
11985 int found_object = 0;
11986 /* Tells if the location should remain inserted in the target. */
11987 int keep_in_target = 0;
11988 int removed = 0;
11989
11990 /* Skip LOCP entries which will definitely never be needed.
11991 Stop either at or being the one matching OLD_LOC. */
11992 while (locp < bp_locations + bp_locations_count
11993 && (*locp)->address < old_loc->address)
11994 locp++;
11995
11996 for (loc2p = locp;
11997 (loc2p < bp_locations + bp_locations_count
11998 && (*loc2p)->address == old_loc->address);
11999 loc2p++)
12000 {
12001 /* Check if this is a new/duplicated location or a duplicated
12002 location that had its condition modified. If so, we want to send
12003 its condition to the target if evaluation of conditions is taking
12004 place there. */
12005 if ((*loc2p)->condition_changed == condition_modified
12006 && (last_addr != old_loc->address
12007 || last_pspace_num != old_loc->pspace->num))
12008 {
12009 force_breakpoint_reinsertion (*loc2p);
12010 last_pspace_num = old_loc->pspace->num;
12011 }
12012
12013 if (*loc2p == old_loc)
12014 found_object = 1;
12015 }
12016
12017 /* We have already handled this address, update it so that we don't
12018 have to go through updates again. */
12019 last_addr = old_loc->address;
12020
12021 /* Target-side condition evaluation: Handle deleted locations. */
12022 if (!found_object)
12023 force_breakpoint_reinsertion (old_loc);
12024
12025 /* If this location is no longer present, and inserted, look if
12026 there's maybe a new location at the same address. If so,
12027 mark that one inserted, and don't remove this one. This is
12028 needed so that we don't have a time window where a breakpoint
12029 at certain location is not inserted. */
12030
12031 if (old_loc->inserted)
12032 {
12033 /* If the location is inserted now, we might have to remove
12034 it. */
12035
12036 if (found_object && should_be_inserted (old_loc))
12037 {
12038 /* The location is still present in the location list,
12039 and still should be inserted. Don't do anything. */
12040 keep_in_target = 1;
12041 }
12042 else
12043 {
12044 /* This location still exists, but it won't be kept in the
12045 target since it may have been disabled. We proceed to
12046 remove its target-side condition. */
12047
12048 /* The location is either no longer present, or got
12049 disabled. See if there's another location at the
12050 same address, in which case we don't need to remove
12051 this one from the target. */
12052
12053 /* OLD_LOC comes from existing struct breakpoint. */
12054 if (breakpoint_address_is_meaningful (old_loc->owner))
12055 {
12056 for (loc2p = locp;
12057 (loc2p < bp_locations + bp_locations_count
12058 && (*loc2p)->address == old_loc->address);
12059 loc2p++)
12060 {
12061 struct bp_location *loc2 = *loc2p;
12062
12063 if (breakpoint_locations_match (loc2, old_loc))
12064 {
12065 /* Read watchpoint locations are switched to
12066 access watchpoints, if the former are not
12067 supported, but the latter are. */
12068 if (is_hardware_watchpoint (old_loc->owner))
12069 {
12070 gdb_assert (is_hardware_watchpoint (loc2->owner));
12071 loc2->watchpoint_type = old_loc->watchpoint_type;
12072 }
12073
12074 /* loc2 is a duplicated location. We need to check
12075 if it should be inserted in case it will be
12076 unduplicated. */
12077 if (loc2 != old_loc
12078 && unduplicated_should_be_inserted (loc2))
12079 {
12080 swap_insertion (old_loc, loc2);
12081 keep_in_target = 1;
12082 break;
12083 }
12084 }
12085 }
12086 }
12087 }
12088
12089 if (!keep_in_target)
12090 {
12091 if (remove_breakpoint (old_loc))
12092 {
12093 /* This is just about all we can do. We could keep
12094 this location on the global list, and try to
12095 remove it next time, but there's no particular
12096 reason why we will succeed next time.
12097
12098 Note that at this point, old_loc->owner is still
12099 valid, as delete_breakpoint frees the breakpoint
12100 only after calling us. */
12101 printf_filtered (_("warning: Error removing "
12102 "breakpoint %d\n"),
12103 old_loc->owner->number);
12104 }
12105 removed = 1;
12106 }
12107 }
12108
12109 if (!found_object)
12110 {
12111 if (removed && target_is_non_stop_p ()
12112 && need_moribund_for_location_type (old_loc))
12113 {
12114 /* This location was removed from the target. In
12115 non-stop mode, a race condition is possible where
12116 we've removed a breakpoint, but stop events for that
12117 breakpoint are already queued and will arrive later.
12118 We apply an heuristic to be able to distinguish such
12119 SIGTRAPs from other random SIGTRAPs: we keep this
12120 breakpoint location for a bit, and will retire it
12121 after we see some number of events. The theory here
12122 is that reporting of events should, "on the average",
12123 be fair, so after a while we'll see events from all
12124 threads that have anything of interest, and no longer
12125 need to keep this breakpoint location around. We
12126 don't hold locations forever so to reduce chances of
12127 mistaking a non-breakpoint SIGTRAP for a breakpoint
12128 SIGTRAP.
12129
12130 The heuristic failing can be disastrous on
12131 decr_pc_after_break targets.
12132
12133 On decr_pc_after_break targets, like e.g., x86-linux,
12134 if we fail to recognize a late breakpoint SIGTRAP,
12135 because events_till_retirement has reached 0 too
12136 soon, we'll fail to do the PC adjustment, and report
12137 a random SIGTRAP to the user. When the user resumes
12138 the inferior, it will most likely immediately crash
12139 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12140 corrupted, because of being resumed e.g., in the
12141 middle of a multi-byte instruction, or skipped a
12142 one-byte instruction. This was actually seen happen
12143 on native x86-linux, and should be less rare on
12144 targets that do not support new thread events, like
12145 remote, due to the heuristic depending on
12146 thread_count.
12147
12148 Mistaking a random SIGTRAP for a breakpoint trap
12149 causes similar symptoms (PC adjustment applied when
12150 it shouldn't), but then again, playing with SIGTRAPs
12151 behind the debugger's back is asking for trouble.
12152
12153 Since hardware watchpoint traps are always
12154 distinguishable from other traps, so we don't need to
12155 apply keep hardware watchpoint moribund locations
12156 around. We simply always ignore hardware watchpoint
12157 traps we can no longer explain. */
12158
12159 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12160 old_loc->owner = NULL;
12161
12162 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12163 }
12164 else
12165 {
12166 old_loc->owner = NULL;
12167 decref_bp_location (&old_loc);
12168 }
12169 }
12170 }
12171
12172 /* Rescan breakpoints at the same address and section, marking the
12173 first one as "first" and any others as "duplicates". This is so
12174 that the bpt instruction is only inserted once. If we have a
12175 permanent breakpoint at the same place as BPT, make that one the
12176 official one, and the rest as duplicates. Permanent breakpoints
12177 are sorted first for the same address.
12178
12179 Do the same for hardware watchpoints, but also considering the
12180 watchpoint's type (regular/access/read) and length. */
12181
12182 bp_loc_first = NULL;
12183 wp_loc_first = NULL;
12184 awp_loc_first = NULL;
12185 rwp_loc_first = NULL;
12186 ALL_BP_LOCATIONS (loc, locp)
12187 {
12188 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12189 non-NULL. */
12190 struct bp_location **loc_first_p;
12191 b = loc->owner;
12192
12193 if (!unduplicated_should_be_inserted (loc)
12194 || !breakpoint_address_is_meaningful (b)
12195 /* Don't detect duplicate for tracepoint locations because they are
12196 never duplicated. See the comments in field `duplicate' of
12197 `struct bp_location'. */
12198 || is_tracepoint (b))
12199 {
12200 /* Clear the condition modification flag. */
12201 loc->condition_changed = condition_unchanged;
12202 continue;
12203 }
12204
12205 if (b->type == bp_hardware_watchpoint)
12206 loc_first_p = &wp_loc_first;
12207 else if (b->type == bp_read_watchpoint)
12208 loc_first_p = &rwp_loc_first;
12209 else if (b->type == bp_access_watchpoint)
12210 loc_first_p = &awp_loc_first;
12211 else
12212 loc_first_p = &bp_loc_first;
12213
12214 if (*loc_first_p == NULL
12215 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12216 || !breakpoint_locations_match (loc, *loc_first_p))
12217 {
12218 *loc_first_p = loc;
12219 loc->duplicate = 0;
12220
12221 if (is_breakpoint (loc->owner) && loc->condition_changed)
12222 {
12223 loc->needs_update = 1;
12224 /* Clear the condition modification flag. */
12225 loc->condition_changed = condition_unchanged;
12226 }
12227 continue;
12228 }
12229
12230
12231 /* This and the above ensure the invariant that the first location
12232 is not duplicated, and is the inserted one.
12233 All following are marked as duplicated, and are not inserted. */
12234 if (loc->inserted)
12235 swap_insertion (loc, *loc_first_p);
12236 loc->duplicate = 1;
12237
12238 /* Clear the condition modification flag. */
12239 loc->condition_changed = condition_unchanged;
12240 }
12241
12242 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12243 {
12244 if (insert_mode != UGLL_DONT_INSERT)
12245 insert_breakpoint_locations ();
12246 else
12247 {
12248 /* Even though the caller told us to not insert new
12249 locations, we may still need to update conditions on the
12250 target's side of breakpoints that were already inserted
12251 if the target is evaluating breakpoint conditions. We
12252 only update conditions for locations that are marked
12253 "needs_update". */
12254 update_inserted_breakpoint_locations ();
12255 }
12256 }
12257
12258 if (insert_mode != UGLL_DONT_INSERT)
12259 download_tracepoint_locations ();
12260 }
12261
12262 void
12263 breakpoint_retire_moribund (void)
12264 {
12265 struct bp_location *loc;
12266 int ix;
12267
12268 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12269 if (--(loc->events_till_retirement) == 0)
12270 {
12271 decref_bp_location (&loc);
12272 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12273 --ix;
12274 }
12275 }
12276
12277 static void
12278 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12279 {
12280
12281 TRY
12282 {
12283 update_global_location_list (insert_mode);
12284 }
12285 CATCH (e, RETURN_MASK_ERROR)
12286 {
12287 }
12288 END_CATCH
12289 }
12290
12291 /* Clear BKP from a BPS. */
12292
12293 static void
12294 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12295 {
12296 bpstat bs;
12297
12298 for (bs = bps; bs; bs = bs->next)
12299 if (bs->breakpoint_at == bpt)
12300 {
12301 bs->breakpoint_at = NULL;
12302 bs->old_val = NULL;
12303 /* bs->commands will be freed later. */
12304 }
12305 }
12306
12307 /* Callback for iterate_over_threads. */
12308 static int
12309 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12310 {
12311 struct breakpoint *bpt = (struct breakpoint *) data;
12312
12313 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12314 return 0;
12315 }
12316
12317 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12318 callbacks. */
12319
12320 static void
12321 say_where (struct breakpoint *b)
12322 {
12323 struct value_print_options opts;
12324
12325 get_user_print_options (&opts);
12326
12327 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12328 single string. */
12329 if (b->loc == NULL)
12330 {
12331 /* For pending locations, the output differs slightly based
12332 on b->extra_string. If this is non-NULL, it contains either
12333 a condition or dprintf arguments. */
12334 if (b->extra_string == NULL)
12335 {
12336 printf_filtered (_(" (%s) pending."),
12337 event_location_to_string (b->location.get ()));
12338 }
12339 else if (b->type == bp_dprintf)
12340 {
12341 printf_filtered (_(" (%s,%s) pending."),
12342 event_location_to_string (b->location.get ()),
12343 b->extra_string);
12344 }
12345 else
12346 {
12347 printf_filtered (_(" (%s %s) pending."),
12348 event_location_to_string (b->location.get ()),
12349 b->extra_string);
12350 }
12351 }
12352 else
12353 {
12354 if (opts.addressprint || b->loc->symtab == NULL)
12355 {
12356 printf_filtered (" at ");
12357 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12358 gdb_stdout);
12359 }
12360 if (b->loc->symtab != NULL)
12361 {
12362 /* If there is a single location, we can print the location
12363 more nicely. */
12364 if (b->loc->next == NULL)
12365 printf_filtered (": file %s, line %d.",
12366 symtab_to_filename_for_display (b->loc->symtab),
12367 b->loc->line_number);
12368 else
12369 /* This is not ideal, but each location may have a
12370 different file name, and this at least reflects the
12371 real situation somewhat. */
12372 printf_filtered (": %s.",
12373 event_location_to_string (b->location.get ()));
12374 }
12375
12376 if (b->loc->next)
12377 {
12378 struct bp_location *loc = b->loc;
12379 int n = 0;
12380 for (; loc; loc = loc->next)
12381 ++n;
12382 printf_filtered (" (%d locations)", n);
12383 }
12384 }
12385 }
12386
12387 /* Default bp_location_ops methods. */
12388
12389 static void
12390 bp_location_dtor (struct bp_location *self)
12391 {
12392 xfree (self->function_name);
12393 }
12394
12395 static const struct bp_location_ops bp_location_ops =
12396 {
12397 bp_location_dtor
12398 };
12399
12400 /* Destructor for the breakpoint base class. */
12401
12402 breakpoint::~breakpoint ()
12403 {
12404 xfree (this->cond_string);
12405 xfree (this->extra_string);
12406 xfree (this->filter);
12407 }
12408
12409 static struct bp_location *
12410 base_breakpoint_allocate_location (struct breakpoint *self)
12411 {
12412 return new bp_location (&bp_location_ops, self);
12413 }
12414
12415 static void
12416 base_breakpoint_re_set (struct breakpoint *b)
12417 {
12418 /* Nothing to re-set. */
12419 }
12420
12421 #define internal_error_pure_virtual_called() \
12422 gdb_assert_not_reached ("pure virtual function called")
12423
12424 static int
12425 base_breakpoint_insert_location (struct bp_location *bl)
12426 {
12427 internal_error_pure_virtual_called ();
12428 }
12429
12430 static int
12431 base_breakpoint_remove_location (struct bp_location *bl,
12432 enum remove_bp_reason reason)
12433 {
12434 internal_error_pure_virtual_called ();
12435 }
12436
12437 static int
12438 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12439 const address_space *aspace,
12440 CORE_ADDR bp_addr,
12441 const struct target_waitstatus *ws)
12442 {
12443 internal_error_pure_virtual_called ();
12444 }
12445
12446 static void
12447 base_breakpoint_check_status (bpstat bs)
12448 {
12449 /* Always stop. */
12450 }
12451
12452 /* A "works_in_software_mode" breakpoint_ops method that just internal
12453 errors. */
12454
12455 static int
12456 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12457 {
12458 internal_error_pure_virtual_called ();
12459 }
12460
12461 /* A "resources_needed" breakpoint_ops method that just internal
12462 errors. */
12463
12464 static int
12465 base_breakpoint_resources_needed (const struct bp_location *bl)
12466 {
12467 internal_error_pure_virtual_called ();
12468 }
12469
12470 static enum print_stop_action
12471 base_breakpoint_print_it (bpstat bs)
12472 {
12473 internal_error_pure_virtual_called ();
12474 }
12475
12476 static void
12477 base_breakpoint_print_one_detail (const struct breakpoint *self,
12478 struct ui_out *uiout)
12479 {
12480 /* nothing */
12481 }
12482
12483 static void
12484 base_breakpoint_print_mention (struct breakpoint *b)
12485 {
12486 internal_error_pure_virtual_called ();
12487 }
12488
12489 static void
12490 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12491 {
12492 internal_error_pure_virtual_called ();
12493 }
12494
12495 static void
12496 base_breakpoint_create_sals_from_location
12497 (const struct event_location *location,
12498 struct linespec_result *canonical,
12499 enum bptype type_wanted)
12500 {
12501 internal_error_pure_virtual_called ();
12502 }
12503
12504 static void
12505 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12506 struct linespec_result *c,
12507 gdb::unique_xmalloc_ptr<char> cond_string,
12508 gdb::unique_xmalloc_ptr<char> extra_string,
12509 enum bptype type_wanted,
12510 enum bpdisp disposition,
12511 int thread,
12512 int task, int ignore_count,
12513 const struct breakpoint_ops *o,
12514 int from_tty, int enabled,
12515 int internal, unsigned flags)
12516 {
12517 internal_error_pure_virtual_called ();
12518 }
12519
12520 static std::vector<symtab_and_line>
12521 base_breakpoint_decode_location (struct breakpoint *b,
12522 const struct event_location *location,
12523 struct program_space *search_pspace)
12524 {
12525 internal_error_pure_virtual_called ();
12526 }
12527
12528 /* The default 'explains_signal' method. */
12529
12530 static int
12531 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12532 {
12533 return 1;
12534 }
12535
12536 /* The default "after_condition_true" method. */
12537
12538 static void
12539 base_breakpoint_after_condition_true (struct bpstats *bs)
12540 {
12541 /* Nothing to do. */
12542 }
12543
12544 struct breakpoint_ops base_breakpoint_ops =
12545 {
12546 base_breakpoint_allocate_location,
12547 base_breakpoint_re_set,
12548 base_breakpoint_insert_location,
12549 base_breakpoint_remove_location,
12550 base_breakpoint_breakpoint_hit,
12551 base_breakpoint_check_status,
12552 base_breakpoint_resources_needed,
12553 base_breakpoint_works_in_software_mode,
12554 base_breakpoint_print_it,
12555 NULL,
12556 base_breakpoint_print_one_detail,
12557 base_breakpoint_print_mention,
12558 base_breakpoint_print_recreate,
12559 base_breakpoint_create_sals_from_location,
12560 base_breakpoint_create_breakpoints_sal,
12561 base_breakpoint_decode_location,
12562 base_breakpoint_explains_signal,
12563 base_breakpoint_after_condition_true,
12564 };
12565
12566 /* Default breakpoint_ops methods. */
12567
12568 static void
12569 bkpt_re_set (struct breakpoint *b)
12570 {
12571 /* FIXME: is this still reachable? */
12572 if (breakpoint_event_location_empty_p (b))
12573 {
12574 /* Anything without a location can't be re-set. */
12575 delete_breakpoint (b);
12576 return;
12577 }
12578
12579 breakpoint_re_set_default (b);
12580 }
12581
12582 static int
12583 bkpt_insert_location (struct bp_location *bl)
12584 {
12585 CORE_ADDR addr = bl->target_info.reqstd_address;
12586
12587 bl->target_info.kind = breakpoint_kind (bl, &addr);
12588 bl->target_info.placed_address = addr;
12589
12590 if (bl->loc_type == bp_loc_hardware_breakpoint)
12591 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12592 else
12593 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12594 }
12595
12596 static int
12597 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12598 {
12599 if (bl->loc_type == bp_loc_hardware_breakpoint)
12600 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12601 else
12602 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12603 }
12604
12605 static int
12606 bkpt_breakpoint_hit (const struct bp_location *bl,
12607 const address_space *aspace, CORE_ADDR bp_addr,
12608 const struct target_waitstatus *ws)
12609 {
12610 if (ws->kind != TARGET_WAITKIND_STOPPED
12611 || ws->value.sig != GDB_SIGNAL_TRAP)
12612 return 0;
12613
12614 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12615 aspace, bp_addr))
12616 return 0;
12617
12618 if (overlay_debugging /* unmapped overlay section */
12619 && section_is_overlay (bl->section)
12620 && !section_is_mapped (bl->section))
12621 return 0;
12622
12623 return 1;
12624 }
12625
12626 static int
12627 dprintf_breakpoint_hit (const struct bp_location *bl,
12628 const address_space *aspace, CORE_ADDR bp_addr,
12629 const struct target_waitstatus *ws)
12630 {
12631 if (dprintf_style == dprintf_style_agent
12632 && target_can_run_breakpoint_commands ())
12633 {
12634 /* An agent-style dprintf never causes a stop. If we see a trap
12635 for this address it must be for a breakpoint that happens to
12636 be set at the same address. */
12637 return 0;
12638 }
12639
12640 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12641 }
12642
12643 static int
12644 bkpt_resources_needed (const struct bp_location *bl)
12645 {
12646 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12647
12648 return 1;
12649 }
12650
12651 static enum print_stop_action
12652 bkpt_print_it (bpstat bs)
12653 {
12654 struct breakpoint *b;
12655 const struct bp_location *bl;
12656 int bp_temp;
12657 struct ui_out *uiout = current_uiout;
12658
12659 gdb_assert (bs->bp_location_at != NULL);
12660
12661 bl = bs->bp_location_at;
12662 b = bs->breakpoint_at;
12663
12664 bp_temp = b->disposition == disp_del;
12665 if (bl->address != bl->requested_address)
12666 breakpoint_adjustment_warning (bl->requested_address,
12667 bl->address,
12668 b->number, 1);
12669 annotate_breakpoint (b->number);
12670 maybe_print_thread_hit_breakpoint (uiout);
12671
12672 if (bp_temp)
12673 uiout->text ("Temporary breakpoint ");
12674 else
12675 uiout->text ("Breakpoint ");
12676 if (uiout->is_mi_like_p ())
12677 {
12678 uiout->field_string ("reason",
12679 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12680 uiout->field_string ("disp", bpdisp_text (b->disposition));
12681 }
12682 uiout->field_int ("bkptno", b->number);
12683 uiout->text (", ");
12684
12685 return PRINT_SRC_AND_LOC;
12686 }
12687
12688 static void
12689 bkpt_print_mention (struct breakpoint *b)
12690 {
12691 if (current_uiout->is_mi_like_p ())
12692 return;
12693
12694 switch (b->type)
12695 {
12696 case bp_breakpoint:
12697 case bp_gnu_ifunc_resolver:
12698 if (b->disposition == disp_del)
12699 printf_filtered (_("Temporary breakpoint"));
12700 else
12701 printf_filtered (_("Breakpoint"));
12702 printf_filtered (_(" %d"), b->number);
12703 if (b->type == bp_gnu_ifunc_resolver)
12704 printf_filtered (_(" at gnu-indirect-function resolver"));
12705 break;
12706 case bp_hardware_breakpoint:
12707 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12708 break;
12709 case bp_dprintf:
12710 printf_filtered (_("Dprintf %d"), b->number);
12711 break;
12712 }
12713
12714 say_where (b);
12715 }
12716
12717 static void
12718 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12719 {
12720 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12721 fprintf_unfiltered (fp, "tbreak");
12722 else if (tp->type == bp_breakpoint)
12723 fprintf_unfiltered (fp, "break");
12724 else if (tp->type == bp_hardware_breakpoint
12725 && tp->disposition == disp_del)
12726 fprintf_unfiltered (fp, "thbreak");
12727 else if (tp->type == bp_hardware_breakpoint)
12728 fprintf_unfiltered (fp, "hbreak");
12729 else
12730 internal_error (__FILE__, __LINE__,
12731 _("unhandled breakpoint type %d"), (int) tp->type);
12732
12733 fprintf_unfiltered (fp, " %s",
12734 event_location_to_string (tp->location.get ()));
12735
12736 /* Print out extra_string if this breakpoint is pending. It might
12737 contain, for example, conditions that were set by the user. */
12738 if (tp->loc == NULL && tp->extra_string != NULL)
12739 fprintf_unfiltered (fp, " %s", tp->extra_string);
12740
12741 print_recreate_thread (tp, fp);
12742 }
12743
12744 static void
12745 bkpt_create_sals_from_location (const struct event_location *location,
12746 struct linespec_result *canonical,
12747 enum bptype type_wanted)
12748 {
12749 create_sals_from_location_default (location, canonical, type_wanted);
12750 }
12751
12752 static void
12753 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12754 struct linespec_result *canonical,
12755 gdb::unique_xmalloc_ptr<char> cond_string,
12756 gdb::unique_xmalloc_ptr<char> extra_string,
12757 enum bptype type_wanted,
12758 enum bpdisp disposition,
12759 int thread,
12760 int task, int ignore_count,
12761 const struct breakpoint_ops *ops,
12762 int from_tty, int enabled,
12763 int internal, unsigned flags)
12764 {
12765 create_breakpoints_sal_default (gdbarch, canonical,
12766 std::move (cond_string),
12767 std::move (extra_string),
12768 type_wanted,
12769 disposition, thread, task,
12770 ignore_count, ops, from_tty,
12771 enabled, internal, flags);
12772 }
12773
12774 static std::vector<symtab_and_line>
12775 bkpt_decode_location (struct breakpoint *b,
12776 const struct event_location *location,
12777 struct program_space *search_pspace)
12778 {
12779 return decode_location_default (b, location, search_pspace);
12780 }
12781
12782 /* Virtual table for internal breakpoints. */
12783
12784 static void
12785 internal_bkpt_re_set (struct breakpoint *b)
12786 {
12787 switch (b->type)
12788 {
12789 /* Delete overlay event and longjmp master breakpoints; they
12790 will be reset later by breakpoint_re_set. */
12791 case bp_overlay_event:
12792 case bp_longjmp_master:
12793 case bp_std_terminate_master:
12794 case bp_exception_master:
12795 delete_breakpoint (b);
12796 break;
12797
12798 /* This breakpoint is special, it's set up when the inferior
12799 starts and we really don't want to touch it. */
12800 case bp_shlib_event:
12801
12802 /* Like bp_shlib_event, this breakpoint type is special. Once
12803 it is set up, we do not want to touch it. */
12804 case bp_thread_event:
12805 break;
12806 }
12807 }
12808
12809 static void
12810 internal_bkpt_check_status (bpstat bs)
12811 {
12812 if (bs->breakpoint_at->type == bp_shlib_event)
12813 {
12814 /* If requested, stop when the dynamic linker notifies GDB of
12815 events. This allows the user to get control and place
12816 breakpoints in initializer routines for dynamically loaded
12817 objects (among other things). */
12818 bs->stop = stop_on_solib_events;
12819 bs->print = stop_on_solib_events;
12820 }
12821 else
12822 bs->stop = 0;
12823 }
12824
12825 static enum print_stop_action
12826 internal_bkpt_print_it (bpstat bs)
12827 {
12828 struct breakpoint *b;
12829
12830 b = bs->breakpoint_at;
12831
12832 switch (b->type)
12833 {
12834 case bp_shlib_event:
12835 /* Did we stop because the user set the stop_on_solib_events
12836 variable? (If so, we report this as a generic, "Stopped due
12837 to shlib event" message.) */
12838 print_solib_event (0);
12839 break;
12840
12841 case bp_thread_event:
12842 /* Not sure how we will get here.
12843 GDB should not stop for these breakpoints. */
12844 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12845 break;
12846
12847 case bp_overlay_event:
12848 /* By analogy with the thread event, GDB should not stop for these. */
12849 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12850 break;
12851
12852 case bp_longjmp_master:
12853 /* These should never be enabled. */
12854 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12855 break;
12856
12857 case bp_std_terminate_master:
12858 /* These should never be enabled. */
12859 printf_filtered (_("std::terminate Master Breakpoint: "
12860 "gdb should not stop!\n"));
12861 break;
12862
12863 case bp_exception_master:
12864 /* These should never be enabled. */
12865 printf_filtered (_("Exception Master Breakpoint: "
12866 "gdb should not stop!\n"));
12867 break;
12868 }
12869
12870 return PRINT_NOTHING;
12871 }
12872
12873 static void
12874 internal_bkpt_print_mention (struct breakpoint *b)
12875 {
12876 /* Nothing to mention. These breakpoints are internal. */
12877 }
12878
12879 /* Virtual table for momentary breakpoints */
12880
12881 static void
12882 momentary_bkpt_re_set (struct breakpoint *b)
12883 {
12884 /* Keep temporary breakpoints, which can be encountered when we step
12885 over a dlopen call and solib_add is resetting the breakpoints.
12886 Otherwise these should have been blown away via the cleanup chain
12887 or by breakpoint_init_inferior when we rerun the executable. */
12888 }
12889
12890 static void
12891 momentary_bkpt_check_status (bpstat bs)
12892 {
12893 /* Nothing. The point of these breakpoints is causing a stop. */
12894 }
12895
12896 static enum print_stop_action
12897 momentary_bkpt_print_it (bpstat bs)
12898 {
12899 return PRINT_UNKNOWN;
12900 }
12901
12902 static void
12903 momentary_bkpt_print_mention (struct breakpoint *b)
12904 {
12905 /* Nothing to mention. These breakpoints are internal. */
12906 }
12907
12908 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12909
12910 It gets cleared already on the removal of the first one of such placed
12911 breakpoints. This is OK as they get all removed altogether. */
12912
12913 longjmp_breakpoint::~longjmp_breakpoint ()
12914 {
12915 thread_info *tp = find_thread_global_id (this->thread);
12916
12917 if (tp != NULL)
12918 tp->initiating_frame = null_frame_id;
12919 }
12920
12921 /* Specific methods for probe breakpoints. */
12922
12923 static int
12924 bkpt_probe_insert_location (struct bp_location *bl)
12925 {
12926 int v = bkpt_insert_location (bl);
12927
12928 if (v == 0)
12929 {
12930 /* The insertion was successful, now let's set the probe's semaphore
12931 if needed. */
12932 if (bl->probe.probe->pops->set_semaphore != NULL)
12933 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
12934 bl->probe.objfile,
12935 bl->gdbarch);
12936 }
12937
12938 return v;
12939 }
12940
12941 static int
12942 bkpt_probe_remove_location (struct bp_location *bl,
12943 enum remove_bp_reason reason)
12944 {
12945 /* Let's clear the semaphore before removing the location. */
12946 if (bl->probe.probe->pops->clear_semaphore != NULL)
12947 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
12948 bl->probe.objfile,
12949 bl->gdbarch);
12950
12951 return bkpt_remove_location (bl, reason);
12952 }
12953
12954 static void
12955 bkpt_probe_create_sals_from_location (const struct event_location *location,
12956 struct linespec_result *canonical,
12957 enum bptype type_wanted)
12958 {
12959 struct linespec_sals lsal;
12960
12961 lsal.sals = parse_probes (location, NULL, canonical);
12962 lsal.canonical
12963 = xstrdup (event_location_to_string (canonical->location.get ()));
12964 canonical->lsals.push_back (std::move (lsal));
12965 }
12966
12967 static std::vector<symtab_and_line>
12968 bkpt_probe_decode_location (struct breakpoint *b,
12969 const struct event_location *location,
12970 struct program_space *search_pspace)
12971 {
12972 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12973 if (sals.empty ())
12974 error (_("probe not found"));
12975 return sals;
12976 }
12977
12978 /* The breakpoint_ops structure to be used in tracepoints. */
12979
12980 static void
12981 tracepoint_re_set (struct breakpoint *b)
12982 {
12983 breakpoint_re_set_default (b);
12984 }
12985
12986 static int
12987 tracepoint_breakpoint_hit (const struct bp_location *bl,
12988 const address_space *aspace, CORE_ADDR bp_addr,
12989 const struct target_waitstatus *ws)
12990 {
12991 /* By definition, the inferior does not report stops at
12992 tracepoints. */
12993 return 0;
12994 }
12995
12996 static void
12997 tracepoint_print_one_detail (const struct breakpoint *self,
12998 struct ui_out *uiout)
12999 {
13000 struct tracepoint *tp = (struct tracepoint *) self;
13001 if (tp->static_trace_marker_id)
13002 {
13003 gdb_assert (self->type == bp_static_tracepoint);
13004
13005 uiout->text ("\tmarker id is ");
13006 uiout->field_string ("static-tracepoint-marker-string-id",
13007 tp->static_trace_marker_id);
13008 uiout->text ("\n");
13009 }
13010 }
13011
13012 static void
13013 tracepoint_print_mention (struct breakpoint *b)
13014 {
13015 if (current_uiout->is_mi_like_p ())
13016 return;
13017
13018 switch (b->type)
13019 {
13020 case bp_tracepoint:
13021 printf_filtered (_("Tracepoint"));
13022 printf_filtered (_(" %d"), b->number);
13023 break;
13024 case bp_fast_tracepoint:
13025 printf_filtered (_("Fast tracepoint"));
13026 printf_filtered (_(" %d"), b->number);
13027 break;
13028 case bp_static_tracepoint:
13029 printf_filtered (_("Static tracepoint"));
13030 printf_filtered (_(" %d"), b->number);
13031 break;
13032 default:
13033 internal_error (__FILE__, __LINE__,
13034 _("unhandled tracepoint type %d"), (int) b->type);
13035 }
13036
13037 say_where (b);
13038 }
13039
13040 static void
13041 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13042 {
13043 struct tracepoint *tp = (struct tracepoint *) self;
13044
13045 if (self->type == bp_fast_tracepoint)
13046 fprintf_unfiltered (fp, "ftrace");
13047 else if (self->type == bp_static_tracepoint)
13048 fprintf_unfiltered (fp, "strace");
13049 else if (self->type == bp_tracepoint)
13050 fprintf_unfiltered (fp, "trace");
13051 else
13052 internal_error (__FILE__, __LINE__,
13053 _("unhandled tracepoint type %d"), (int) self->type);
13054
13055 fprintf_unfiltered (fp, " %s",
13056 event_location_to_string (self->location.get ()));
13057 print_recreate_thread (self, fp);
13058
13059 if (tp->pass_count)
13060 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13061 }
13062
13063 static void
13064 tracepoint_create_sals_from_location (const struct event_location *location,
13065 struct linespec_result *canonical,
13066 enum bptype type_wanted)
13067 {
13068 create_sals_from_location_default (location, canonical, type_wanted);
13069 }
13070
13071 static void
13072 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13073 struct linespec_result *canonical,
13074 gdb::unique_xmalloc_ptr<char> cond_string,
13075 gdb::unique_xmalloc_ptr<char> extra_string,
13076 enum bptype type_wanted,
13077 enum bpdisp disposition,
13078 int thread,
13079 int task, int ignore_count,
13080 const struct breakpoint_ops *ops,
13081 int from_tty, int enabled,
13082 int internal, unsigned flags)
13083 {
13084 create_breakpoints_sal_default (gdbarch, canonical,
13085 std::move (cond_string),
13086 std::move (extra_string),
13087 type_wanted,
13088 disposition, thread, task,
13089 ignore_count, ops, from_tty,
13090 enabled, internal, flags);
13091 }
13092
13093 static std::vector<symtab_and_line>
13094 tracepoint_decode_location (struct breakpoint *b,
13095 const struct event_location *location,
13096 struct program_space *search_pspace)
13097 {
13098 return decode_location_default (b, location, search_pspace);
13099 }
13100
13101 struct breakpoint_ops tracepoint_breakpoint_ops;
13102
13103 /* The breakpoint_ops structure to be use on tracepoints placed in a
13104 static probe. */
13105
13106 static void
13107 tracepoint_probe_create_sals_from_location
13108 (const struct event_location *location,
13109 struct linespec_result *canonical,
13110 enum bptype type_wanted)
13111 {
13112 /* We use the same method for breakpoint on probes. */
13113 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13114 }
13115
13116 static std::vector<symtab_and_line>
13117 tracepoint_probe_decode_location (struct breakpoint *b,
13118 const struct event_location *location,
13119 struct program_space *search_pspace)
13120 {
13121 /* We use the same method for breakpoint on probes. */
13122 return bkpt_probe_decode_location (b, location, search_pspace);
13123 }
13124
13125 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13126
13127 /* Dprintf breakpoint_ops methods. */
13128
13129 static void
13130 dprintf_re_set (struct breakpoint *b)
13131 {
13132 breakpoint_re_set_default (b);
13133
13134 /* extra_string should never be non-NULL for dprintf. */
13135 gdb_assert (b->extra_string != NULL);
13136
13137 /* 1 - connect to target 1, that can run breakpoint commands.
13138 2 - create a dprintf, which resolves fine.
13139 3 - disconnect from target 1
13140 4 - connect to target 2, that can NOT run breakpoint commands.
13141
13142 After steps #3/#4, you'll want the dprintf command list to
13143 be updated, because target 1 and 2 may well return different
13144 answers for target_can_run_breakpoint_commands().
13145 Given absence of finer grained resetting, we get to do
13146 it all the time. */
13147 if (b->extra_string != NULL)
13148 update_dprintf_command_list (b);
13149 }
13150
13151 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13152
13153 static void
13154 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13155 {
13156 fprintf_unfiltered (fp, "dprintf %s,%s",
13157 event_location_to_string (tp->location.get ()),
13158 tp->extra_string);
13159 print_recreate_thread (tp, fp);
13160 }
13161
13162 /* Implement the "after_condition_true" breakpoint_ops method for
13163 dprintf.
13164
13165 dprintf's are implemented with regular commands in their command
13166 list, but we run the commands here instead of before presenting the
13167 stop to the user, as dprintf's don't actually cause a stop. This
13168 also makes it so that the commands of multiple dprintfs at the same
13169 address are all handled. */
13170
13171 static void
13172 dprintf_after_condition_true (struct bpstats *bs)
13173 {
13174 struct bpstats tmp_bs;
13175 struct bpstats *tmp_bs_p = &tmp_bs;
13176
13177 /* dprintf's never cause a stop. This wasn't set in the
13178 check_status hook instead because that would make the dprintf's
13179 condition not be evaluated. */
13180 bs->stop = 0;
13181
13182 /* Run the command list here. Take ownership of it instead of
13183 copying. We never want these commands to run later in
13184 bpstat_do_actions, if a breakpoint that causes a stop happens to
13185 be set at same address as this dprintf, or even if running the
13186 commands here throws. */
13187 tmp_bs.commands = bs->commands;
13188 bs->commands = NULL;
13189
13190 bpstat_do_actions_1 (&tmp_bs_p);
13191
13192 /* 'tmp_bs.commands' will usually be NULL by now, but
13193 bpstat_do_actions_1 may return early without processing the whole
13194 list. */
13195 }
13196
13197 /* The breakpoint_ops structure to be used on static tracepoints with
13198 markers (`-m'). */
13199
13200 static void
13201 strace_marker_create_sals_from_location (const struct event_location *location,
13202 struct linespec_result *canonical,
13203 enum bptype type_wanted)
13204 {
13205 struct linespec_sals lsal;
13206 const char *arg_start, *arg;
13207
13208 arg = arg_start = get_linespec_location (location);
13209 lsal.sals = decode_static_tracepoint_spec (&arg);
13210
13211 std::string str (arg_start, arg - arg_start);
13212 const char *ptr = str.c_str ();
13213 canonical->location = new_linespec_location (&ptr);
13214
13215 lsal.canonical
13216 = xstrdup (event_location_to_string (canonical->location.get ()));
13217 canonical->lsals.push_back (std::move (lsal));
13218 }
13219
13220 static void
13221 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13222 struct linespec_result *canonical,
13223 gdb::unique_xmalloc_ptr<char> cond_string,
13224 gdb::unique_xmalloc_ptr<char> extra_string,
13225 enum bptype type_wanted,
13226 enum bpdisp disposition,
13227 int thread,
13228 int task, int ignore_count,
13229 const struct breakpoint_ops *ops,
13230 int from_tty, int enabled,
13231 int internal, unsigned flags)
13232 {
13233 const linespec_sals &lsal = canonical->lsals[0];
13234
13235 /* If the user is creating a static tracepoint by marker id
13236 (strace -m MARKER_ID), then store the sals index, so that
13237 breakpoint_re_set can try to match up which of the newly
13238 found markers corresponds to this one, and, don't try to
13239 expand multiple locations for each sal, given than SALS
13240 already should contain all sals for MARKER_ID. */
13241
13242 for (size_t i = 0; i < lsal.sals.size (); i++)
13243 {
13244 event_location_up location
13245 = copy_event_location (canonical->location.get ());
13246
13247 std::unique_ptr<tracepoint> tp (new tracepoint ());
13248 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13249 std::move (location), NULL,
13250 std::move (cond_string),
13251 std::move (extra_string),
13252 type_wanted, disposition,
13253 thread, task, ignore_count, ops,
13254 from_tty, enabled, internal, flags,
13255 canonical->special_display);
13256 /* Given that its possible to have multiple markers with
13257 the same string id, if the user is creating a static
13258 tracepoint by marker id ("strace -m MARKER_ID"), then
13259 store the sals index, so that breakpoint_re_set can
13260 try to match up which of the newly found markers
13261 corresponds to this one */
13262 tp->static_trace_marker_id_idx = i;
13263
13264 install_breakpoint (internal, std::move (tp), 0);
13265 }
13266 }
13267
13268 static std::vector<symtab_and_line>
13269 strace_marker_decode_location (struct breakpoint *b,
13270 const struct event_location *location,
13271 struct program_space *search_pspace)
13272 {
13273 struct tracepoint *tp = (struct tracepoint *) b;
13274 const char *s = get_linespec_location (location);
13275
13276 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13277 if (sals.size () > tp->static_trace_marker_id_idx)
13278 {
13279 sals[0] = sals[tp->static_trace_marker_id_idx];
13280 sals.resize (1);
13281 return sals;
13282 }
13283 else
13284 error (_("marker %s not found"), tp->static_trace_marker_id);
13285 }
13286
13287 static struct breakpoint_ops strace_marker_breakpoint_ops;
13288
13289 static int
13290 strace_marker_p (struct breakpoint *b)
13291 {
13292 return b->ops == &strace_marker_breakpoint_ops;
13293 }
13294
13295 /* Delete a breakpoint and clean up all traces of it in the data
13296 structures. */
13297
13298 void
13299 delete_breakpoint (struct breakpoint *bpt)
13300 {
13301 struct breakpoint *b;
13302
13303 gdb_assert (bpt != NULL);
13304
13305 /* Has this bp already been deleted? This can happen because
13306 multiple lists can hold pointers to bp's. bpstat lists are
13307 especial culprits.
13308
13309 One example of this happening is a watchpoint's scope bp. When
13310 the scope bp triggers, we notice that the watchpoint is out of
13311 scope, and delete it. We also delete its scope bp. But the
13312 scope bp is marked "auto-deleting", and is already on a bpstat.
13313 That bpstat is then checked for auto-deleting bp's, which are
13314 deleted.
13315
13316 A real solution to this problem might involve reference counts in
13317 bp's, and/or giving them pointers back to their referencing
13318 bpstat's, and teaching delete_breakpoint to only free a bp's
13319 storage when no more references were extent. A cheaper bandaid
13320 was chosen. */
13321 if (bpt->type == bp_none)
13322 return;
13323
13324 /* At least avoid this stale reference until the reference counting
13325 of breakpoints gets resolved. */
13326 if (bpt->related_breakpoint != bpt)
13327 {
13328 struct breakpoint *related;
13329 struct watchpoint *w;
13330
13331 if (bpt->type == bp_watchpoint_scope)
13332 w = (struct watchpoint *) bpt->related_breakpoint;
13333 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13334 w = (struct watchpoint *) bpt;
13335 else
13336 w = NULL;
13337 if (w != NULL)
13338 watchpoint_del_at_next_stop (w);
13339
13340 /* Unlink bpt from the bpt->related_breakpoint ring. */
13341 for (related = bpt; related->related_breakpoint != bpt;
13342 related = related->related_breakpoint);
13343 related->related_breakpoint = bpt->related_breakpoint;
13344 bpt->related_breakpoint = bpt;
13345 }
13346
13347 /* watch_command_1 creates a watchpoint but only sets its number if
13348 update_watchpoint succeeds in creating its bp_locations. If there's
13349 a problem in that process, we'll be asked to delete the half-created
13350 watchpoint. In that case, don't announce the deletion. */
13351 if (bpt->number)
13352 observer_notify_breakpoint_deleted (bpt);
13353
13354 if (breakpoint_chain == bpt)
13355 breakpoint_chain = bpt->next;
13356
13357 ALL_BREAKPOINTS (b)
13358 if (b->next == bpt)
13359 {
13360 b->next = bpt->next;
13361 break;
13362 }
13363
13364 /* Be sure no bpstat's are pointing at the breakpoint after it's
13365 been freed. */
13366 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13367 in all threads for now. Note that we cannot just remove bpstats
13368 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13369 commands are associated with the bpstat; if we remove it here,
13370 then the later call to bpstat_do_actions (&stop_bpstat); in
13371 event-top.c won't do anything, and temporary breakpoints with
13372 commands won't work. */
13373
13374 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13375
13376 /* Now that breakpoint is removed from breakpoint list, update the
13377 global location list. This will remove locations that used to
13378 belong to this breakpoint. Do this before freeing the breakpoint
13379 itself, since remove_breakpoint looks at location's owner. It
13380 might be better design to have location completely
13381 self-contained, but it's not the case now. */
13382 update_global_location_list (UGLL_DONT_INSERT);
13383
13384 /* On the chance that someone will soon try again to delete this
13385 same bp, we mark it as deleted before freeing its storage. */
13386 bpt->type = bp_none;
13387 delete bpt;
13388 }
13389
13390 static void
13391 do_delete_breakpoint_cleanup (void *b)
13392 {
13393 delete_breakpoint ((struct breakpoint *) b);
13394 }
13395
13396 struct cleanup *
13397 make_cleanup_delete_breakpoint (struct breakpoint *b)
13398 {
13399 return make_cleanup (do_delete_breakpoint_cleanup, b);
13400 }
13401
13402 /* Iterator function to call a user-provided callback function once
13403 for each of B and its related breakpoints. */
13404
13405 static void
13406 iterate_over_related_breakpoints (struct breakpoint *b,
13407 gdb::function_view<void (breakpoint *)> function)
13408 {
13409 struct breakpoint *related;
13410
13411 related = b;
13412 do
13413 {
13414 struct breakpoint *next;
13415
13416 /* FUNCTION may delete RELATED. */
13417 next = related->related_breakpoint;
13418
13419 if (next == related)
13420 {
13421 /* RELATED is the last ring entry. */
13422 function (related);
13423
13424 /* FUNCTION may have deleted it, so we'd never reach back to
13425 B. There's nothing left to do anyway, so just break
13426 out. */
13427 break;
13428 }
13429 else
13430 function (related);
13431
13432 related = next;
13433 }
13434 while (related != b);
13435 }
13436
13437 static void
13438 delete_command (const char *arg, int from_tty)
13439 {
13440 struct breakpoint *b, *b_tmp;
13441
13442 dont_repeat ();
13443
13444 if (arg == 0)
13445 {
13446 int breaks_to_delete = 0;
13447
13448 /* Delete all breakpoints if no argument. Do not delete
13449 internal breakpoints, these have to be deleted with an
13450 explicit breakpoint number argument. */
13451 ALL_BREAKPOINTS (b)
13452 if (user_breakpoint_p (b))
13453 {
13454 breaks_to_delete = 1;
13455 break;
13456 }
13457
13458 /* Ask user only if there are some breakpoints to delete. */
13459 if (!from_tty
13460 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13461 {
13462 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13463 if (user_breakpoint_p (b))
13464 delete_breakpoint (b);
13465 }
13466 }
13467 else
13468 map_breakpoint_numbers
13469 (arg, [&] (breakpoint *b)
13470 {
13471 iterate_over_related_breakpoints (b, delete_breakpoint);
13472 });
13473 }
13474
13475 /* Return true if all locations of B bound to PSPACE are pending. If
13476 PSPACE is NULL, all locations of all program spaces are
13477 considered. */
13478
13479 static int
13480 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13481 {
13482 struct bp_location *loc;
13483
13484 for (loc = b->loc; loc != NULL; loc = loc->next)
13485 if ((pspace == NULL
13486 || loc->pspace == pspace)
13487 && !loc->shlib_disabled
13488 && !loc->pspace->executing_startup)
13489 return 0;
13490 return 1;
13491 }
13492
13493 /* Subroutine of update_breakpoint_locations to simplify it.
13494 Return non-zero if multiple fns in list LOC have the same name.
13495 Null names are ignored. */
13496
13497 static int
13498 ambiguous_names_p (struct bp_location *loc)
13499 {
13500 struct bp_location *l;
13501 htab_t htab = htab_create_alloc (13, htab_hash_string,
13502 (int (*) (const void *,
13503 const void *)) streq,
13504 NULL, xcalloc, xfree);
13505
13506 for (l = loc; l != NULL; l = l->next)
13507 {
13508 const char **slot;
13509 const char *name = l->function_name;
13510
13511 /* Allow for some names to be NULL, ignore them. */
13512 if (name == NULL)
13513 continue;
13514
13515 slot = (const char **) htab_find_slot (htab, (const void *) name,
13516 INSERT);
13517 /* NOTE: We can assume slot != NULL here because xcalloc never
13518 returns NULL. */
13519 if (*slot != NULL)
13520 {
13521 htab_delete (htab);
13522 return 1;
13523 }
13524 *slot = name;
13525 }
13526
13527 htab_delete (htab);
13528 return 0;
13529 }
13530
13531 /* When symbols change, it probably means the sources changed as well,
13532 and it might mean the static tracepoint markers are no longer at
13533 the same address or line numbers they used to be at last we
13534 checked. Losing your static tracepoints whenever you rebuild is
13535 undesirable. This function tries to resync/rematch gdb static
13536 tracepoints with the markers on the target, for static tracepoints
13537 that have not been set by marker id. Static tracepoint that have
13538 been set by marker id are reset by marker id in breakpoint_re_set.
13539 The heuristic is:
13540
13541 1) For a tracepoint set at a specific address, look for a marker at
13542 the old PC. If one is found there, assume to be the same marker.
13543 If the name / string id of the marker found is different from the
13544 previous known name, assume that means the user renamed the marker
13545 in the sources, and output a warning.
13546
13547 2) For a tracepoint set at a given line number, look for a marker
13548 at the new address of the old line number. If one is found there,
13549 assume to be the same marker. If the name / string id of the
13550 marker found is different from the previous known name, assume that
13551 means the user renamed the marker in the sources, and output a
13552 warning.
13553
13554 3) If a marker is no longer found at the same address or line, it
13555 may mean the marker no longer exists. But it may also just mean
13556 the code changed a bit. Maybe the user added a few lines of code
13557 that made the marker move up or down (in line number terms). Ask
13558 the target for info about the marker with the string id as we knew
13559 it. If found, update line number and address in the matching
13560 static tracepoint. This will get confused if there's more than one
13561 marker with the same ID (possible in UST, although unadvised
13562 precisely because it confuses tools). */
13563
13564 static struct symtab_and_line
13565 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13566 {
13567 struct tracepoint *tp = (struct tracepoint *) b;
13568 struct static_tracepoint_marker marker;
13569 CORE_ADDR pc;
13570
13571 pc = sal.pc;
13572 if (sal.line)
13573 find_line_pc (sal.symtab, sal.line, &pc);
13574
13575 if (target_static_tracepoint_marker_at (pc, &marker))
13576 {
13577 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13578 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13579 b->number,
13580 tp->static_trace_marker_id, marker.str_id);
13581
13582 xfree (tp->static_trace_marker_id);
13583 tp->static_trace_marker_id = xstrdup (marker.str_id);
13584 release_static_tracepoint_marker (&marker);
13585
13586 return sal;
13587 }
13588
13589 /* Old marker wasn't found on target at lineno. Try looking it up
13590 by string ID. */
13591 if (!sal.explicit_pc
13592 && sal.line != 0
13593 && sal.symtab != NULL
13594 && tp->static_trace_marker_id != NULL)
13595 {
13596 VEC(static_tracepoint_marker_p) *markers;
13597
13598 markers
13599 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13600
13601 if (!VEC_empty(static_tracepoint_marker_p, markers))
13602 {
13603 struct symbol *sym;
13604 struct static_tracepoint_marker *tpmarker;
13605 struct ui_out *uiout = current_uiout;
13606 struct explicit_location explicit_loc;
13607
13608 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13609
13610 xfree (tp->static_trace_marker_id);
13611 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13612
13613 warning (_("marker for static tracepoint %d (%s) not "
13614 "found at previous line number"),
13615 b->number, tp->static_trace_marker_id);
13616
13617 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13618 sym = find_pc_sect_function (tpmarker->address, NULL);
13619 uiout->text ("Now in ");
13620 if (sym)
13621 {
13622 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13623 uiout->text (" at ");
13624 }
13625 uiout->field_string ("file",
13626 symtab_to_filename_for_display (sal2.symtab));
13627 uiout->text (":");
13628
13629 if (uiout->is_mi_like_p ())
13630 {
13631 const char *fullname = symtab_to_fullname (sal2.symtab);
13632
13633 uiout->field_string ("fullname", fullname);
13634 }
13635
13636 uiout->field_int ("line", sal2.line);
13637 uiout->text ("\n");
13638
13639 b->loc->line_number = sal2.line;
13640 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13641
13642 b->location.reset (NULL);
13643 initialize_explicit_location (&explicit_loc);
13644 explicit_loc.source_filename
13645 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13646 explicit_loc.line_offset.offset = b->loc->line_number;
13647 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13648 b->location = new_explicit_location (&explicit_loc);
13649
13650 /* Might be nice to check if function changed, and warn if
13651 so. */
13652
13653 release_static_tracepoint_marker (tpmarker);
13654 }
13655 }
13656 return sal;
13657 }
13658
13659 /* Returns 1 iff locations A and B are sufficiently same that
13660 we don't need to report breakpoint as changed. */
13661
13662 static int
13663 locations_are_equal (struct bp_location *a, struct bp_location *b)
13664 {
13665 while (a && b)
13666 {
13667 if (a->address != b->address)
13668 return 0;
13669
13670 if (a->shlib_disabled != b->shlib_disabled)
13671 return 0;
13672
13673 if (a->enabled != b->enabled)
13674 return 0;
13675
13676 a = a->next;
13677 b = b->next;
13678 }
13679
13680 if ((a == NULL) != (b == NULL))
13681 return 0;
13682
13683 return 1;
13684 }
13685
13686 /* Split all locations of B that are bound to PSPACE out of B's
13687 location list to a separate list and return that list's head. If
13688 PSPACE is NULL, hoist out all locations of B. */
13689
13690 static struct bp_location *
13691 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13692 {
13693 struct bp_location head;
13694 struct bp_location *i = b->loc;
13695 struct bp_location **i_link = &b->loc;
13696 struct bp_location *hoisted = &head;
13697
13698 if (pspace == NULL)
13699 {
13700 i = b->loc;
13701 b->loc = NULL;
13702 return i;
13703 }
13704
13705 head.next = NULL;
13706
13707 while (i != NULL)
13708 {
13709 if (i->pspace == pspace)
13710 {
13711 *i_link = i->next;
13712 i->next = NULL;
13713 hoisted->next = i;
13714 hoisted = i;
13715 }
13716 else
13717 i_link = &i->next;
13718 i = *i_link;
13719 }
13720
13721 return head.next;
13722 }
13723
13724 /* Create new breakpoint locations for B (a hardware or software
13725 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13726 zero, then B is a ranged breakpoint. Only recreates locations for
13727 FILTER_PSPACE. Locations of other program spaces are left
13728 untouched. */
13729
13730 void
13731 update_breakpoint_locations (struct breakpoint *b,
13732 struct program_space *filter_pspace,
13733 gdb::array_view<const symtab_and_line> sals,
13734 gdb::array_view<const symtab_and_line> sals_end)
13735 {
13736 int i;
13737 struct bp_location *existing_locations;
13738
13739 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13740 {
13741 /* Ranged breakpoints have only one start location and one end
13742 location. */
13743 b->enable_state = bp_disabled;
13744 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13745 "multiple locations found\n"),
13746 b->number);
13747 return;
13748 }
13749
13750 /* If there's no new locations, and all existing locations are
13751 pending, don't do anything. This optimizes the common case where
13752 all locations are in the same shared library, that was unloaded.
13753 We'd like to retain the location, so that when the library is
13754 loaded again, we don't loose the enabled/disabled status of the
13755 individual locations. */
13756 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13757 return;
13758
13759 existing_locations = hoist_existing_locations (b, filter_pspace);
13760
13761 for (const auto &sal : sals)
13762 {
13763 struct bp_location *new_loc;
13764
13765 switch_to_program_space_and_thread (sal.pspace);
13766
13767 new_loc = add_location_to_breakpoint (b, &sal);
13768
13769 /* Reparse conditions, they might contain references to the
13770 old symtab. */
13771 if (b->cond_string != NULL)
13772 {
13773 const char *s;
13774
13775 s = b->cond_string;
13776 TRY
13777 {
13778 new_loc->cond = parse_exp_1 (&s, sal.pc,
13779 block_for_pc (sal.pc),
13780 0);
13781 }
13782 CATCH (e, RETURN_MASK_ERROR)
13783 {
13784 warning (_("failed to reevaluate condition "
13785 "for breakpoint %d: %s"),
13786 b->number, e.message);
13787 new_loc->enabled = 0;
13788 }
13789 END_CATCH
13790 }
13791
13792 if (!sals_end.empty ())
13793 {
13794 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13795
13796 new_loc->length = end - sals[0].pc + 1;
13797 }
13798 }
13799
13800 /* If possible, carry over 'disable' status from existing
13801 breakpoints. */
13802 {
13803 struct bp_location *e = existing_locations;
13804 /* If there are multiple breakpoints with the same function name,
13805 e.g. for inline functions, comparing function names won't work.
13806 Instead compare pc addresses; this is just a heuristic as things
13807 may have moved, but in practice it gives the correct answer
13808 often enough until a better solution is found. */
13809 int have_ambiguous_names = ambiguous_names_p (b->loc);
13810
13811 for (; e; e = e->next)
13812 {
13813 if (!e->enabled && e->function_name)
13814 {
13815 struct bp_location *l = b->loc;
13816 if (have_ambiguous_names)
13817 {
13818 for (; l; l = l->next)
13819 if (breakpoint_locations_match (e, l))
13820 {
13821 l->enabled = 0;
13822 break;
13823 }
13824 }
13825 else
13826 {
13827 for (; l; l = l->next)
13828 if (l->function_name
13829 && strcmp (e->function_name, l->function_name) == 0)
13830 {
13831 l->enabled = 0;
13832 break;
13833 }
13834 }
13835 }
13836 }
13837 }
13838
13839 if (!locations_are_equal (existing_locations, b->loc))
13840 observer_notify_breakpoint_modified (b);
13841 }
13842
13843 /* Find the SaL locations corresponding to the given LOCATION.
13844 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13845
13846 static std::vector<symtab_and_line>
13847 location_to_sals (struct breakpoint *b, struct event_location *location,
13848 struct program_space *search_pspace, int *found)
13849 {
13850 struct gdb_exception exception = exception_none;
13851
13852 gdb_assert (b->ops != NULL);
13853
13854 std::vector<symtab_and_line> sals;
13855
13856 TRY
13857 {
13858 sals = b->ops->decode_location (b, location, search_pspace);
13859 }
13860 CATCH (e, RETURN_MASK_ERROR)
13861 {
13862 int not_found_and_ok = 0;
13863
13864 exception = e;
13865
13866 /* For pending breakpoints, it's expected that parsing will
13867 fail until the right shared library is loaded. User has
13868 already told to create pending breakpoints and don't need
13869 extra messages. If breakpoint is in bp_shlib_disabled
13870 state, then user already saw the message about that
13871 breakpoint being disabled, and don't want to see more
13872 errors. */
13873 if (e.error == NOT_FOUND_ERROR
13874 && (b->condition_not_parsed
13875 || (b->loc != NULL
13876 && search_pspace != NULL
13877 && b->loc->pspace != search_pspace)
13878 || (b->loc && b->loc->shlib_disabled)
13879 || (b->loc && b->loc->pspace->executing_startup)
13880 || b->enable_state == bp_disabled))
13881 not_found_and_ok = 1;
13882
13883 if (!not_found_and_ok)
13884 {
13885 /* We surely don't want to warn about the same breakpoint
13886 10 times. One solution, implemented here, is disable
13887 the breakpoint on error. Another solution would be to
13888 have separate 'warning emitted' flag. Since this
13889 happens only when a binary has changed, I don't know
13890 which approach is better. */
13891 b->enable_state = bp_disabled;
13892 throw_exception (e);
13893 }
13894 }
13895 END_CATCH
13896
13897 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13898 {
13899 for (auto &sal : sals)
13900 resolve_sal_pc (&sal);
13901 if (b->condition_not_parsed && b->extra_string != NULL)
13902 {
13903 char *cond_string, *extra_string;
13904 int thread, task;
13905
13906 find_condition_and_thread (b->extra_string, sals[0].pc,
13907 &cond_string, &thread, &task,
13908 &extra_string);
13909 gdb_assert (b->cond_string == NULL);
13910 if (cond_string)
13911 b->cond_string = cond_string;
13912 b->thread = thread;
13913 b->task = task;
13914 if (extra_string)
13915 {
13916 xfree (b->extra_string);
13917 b->extra_string = extra_string;
13918 }
13919 b->condition_not_parsed = 0;
13920 }
13921
13922 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13923 sals[0] = update_static_tracepoint (b, sals[0]);
13924
13925 *found = 1;
13926 }
13927 else
13928 *found = 0;
13929
13930 return sals;
13931 }
13932
13933 /* The default re_set method, for typical hardware or software
13934 breakpoints. Reevaluate the breakpoint and recreate its
13935 locations. */
13936
13937 static void
13938 breakpoint_re_set_default (struct breakpoint *b)
13939 {
13940 struct program_space *filter_pspace = current_program_space;
13941 std::vector<symtab_and_line> expanded, expanded_end;
13942
13943 int found;
13944 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13945 filter_pspace, &found);
13946 if (found)
13947 expanded = std::move (sals);
13948
13949 if (b->location_range_end != NULL)
13950 {
13951 std::vector<symtab_and_line> sals_end
13952 = location_to_sals (b, b->location_range_end.get (),
13953 filter_pspace, &found);
13954 if (found)
13955 expanded_end = std::move (sals_end);
13956 }
13957
13958 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13959 }
13960
13961 /* Default method for creating SALs from an address string. It basically
13962 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13963
13964 static void
13965 create_sals_from_location_default (const struct event_location *location,
13966 struct linespec_result *canonical,
13967 enum bptype type_wanted)
13968 {
13969 parse_breakpoint_sals (location, canonical);
13970 }
13971
13972 /* Call create_breakpoints_sal for the given arguments. This is the default
13973 function for the `create_breakpoints_sal' method of
13974 breakpoint_ops. */
13975
13976 static void
13977 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13978 struct linespec_result *canonical,
13979 gdb::unique_xmalloc_ptr<char> cond_string,
13980 gdb::unique_xmalloc_ptr<char> extra_string,
13981 enum bptype type_wanted,
13982 enum bpdisp disposition,
13983 int thread,
13984 int task, int ignore_count,
13985 const struct breakpoint_ops *ops,
13986 int from_tty, int enabled,
13987 int internal, unsigned flags)
13988 {
13989 create_breakpoints_sal (gdbarch, canonical,
13990 std::move (cond_string),
13991 std::move (extra_string),
13992 type_wanted, disposition,
13993 thread, task, ignore_count, ops, from_tty,
13994 enabled, internal, flags);
13995 }
13996
13997 /* Decode the line represented by S by calling decode_line_full. This is the
13998 default function for the `decode_location' method of breakpoint_ops. */
13999
14000 static std::vector<symtab_and_line>
14001 decode_location_default (struct breakpoint *b,
14002 const struct event_location *location,
14003 struct program_space *search_pspace)
14004 {
14005 struct linespec_result canonical;
14006
14007 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14008 (struct symtab *) NULL, 0,
14009 &canonical, multiple_symbols_all,
14010 b->filter);
14011
14012 /* We should get 0 or 1 resulting SALs. */
14013 gdb_assert (canonical.lsals.size () < 2);
14014
14015 if (!canonical.lsals.empty ())
14016 {
14017 const linespec_sals &lsal = canonical.lsals[0];
14018 return std::move (lsal.sals);
14019 }
14020 return {};
14021 }
14022
14023 /* Reset a breakpoint. */
14024
14025 static void
14026 breakpoint_re_set_one (breakpoint *b)
14027 {
14028 input_radix = b->input_radix;
14029 set_language (b->language);
14030
14031 b->ops->re_set (b);
14032 }
14033
14034 /* Re-set breakpoint locations for the current program space.
14035 Locations bound to other program spaces are left untouched. */
14036
14037 void
14038 breakpoint_re_set (void)
14039 {
14040 struct breakpoint *b, *b_tmp;
14041
14042 {
14043 scoped_restore_current_language save_language;
14044 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
14045 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14046
14047 /* Note: we must not try to insert locations until after all
14048 breakpoints have been re-set. Otherwise, e.g., when re-setting
14049 breakpoint 1, we'd insert the locations of breakpoint 2, which
14050 hadn't been re-set yet, and thus may have stale locations. */
14051
14052 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14053 {
14054 TRY
14055 {
14056 breakpoint_re_set_one (b);
14057 }
14058 CATCH (ex, RETURN_MASK_ALL)
14059 {
14060 exception_fprintf (gdb_stderr, ex,
14061 "Error in re-setting breakpoint %d: ",
14062 b->number);
14063 }
14064 END_CATCH
14065 }
14066
14067 jit_breakpoint_re_set ();
14068 }
14069
14070 create_overlay_event_breakpoint ();
14071 create_longjmp_master_breakpoint ();
14072 create_std_terminate_master_breakpoint ();
14073 create_exception_master_breakpoint ();
14074
14075 /* Now we can insert. */
14076 update_global_location_list (UGLL_MAY_INSERT);
14077 }
14078 \f
14079 /* Reset the thread number of this breakpoint:
14080
14081 - If the breakpoint is for all threads, leave it as-is.
14082 - Else, reset it to the current thread for inferior_ptid. */
14083 void
14084 breakpoint_re_set_thread (struct breakpoint *b)
14085 {
14086 if (b->thread != -1)
14087 {
14088 if (in_thread_list (inferior_ptid))
14089 b->thread = ptid_to_global_thread_id (inferior_ptid);
14090
14091 /* We're being called after following a fork. The new fork is
14092 selected as current, and unless this was a vfork will have a
14093 different program space from the original thread. Reset that
14094 as well. */
14095 b->loc->pspace = current_program_space;
14096 }
14097 }
14098
14099 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14100 If from_tty is nonzero, it prints a message to that effect,
14101 which ends with a period (no newline). */
14102
14103 void
14104 set_ignore_count (int bptnum, int count, int from_tty)
14105 {
14106 struct breakpoint *b;
14107
14108 if (count < 0)
14109 count = 0;
14110
14111 ALL_BREAKPOINTS (b)
14112 if (b->number == bptnum)
14113 {
14114 if (is_tracepoint (b))
14115 {
14116 if (from_tty && count != 0)
14117 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14118 bptnum);
14119 return;
14120 }
14121
14122 b->ignore_count = count;
14123 if (from_tty)
14124 {
14125 if (count == 0)
14126 printf_filtered (_("Will stop next time "
14127 "breakpoint %d is reached."),
14128 bptnum);
14129 else if (count == 1)
14130 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14131 bptnum);
14132 else
14133 printf_filtered (_("Will ignore next %d "
14134 "crossings of breakpoint %d."),
14135 count, bptnum);
14136 }
14137 observer_notify_breakpoint_modified (b);
14138 return;
14139 }
14140
14141 error (_("No breakpoint number %d."), bptnum);
14142 }
14143
14144 /* Command to set ignore-count of breakpoint N to COUNT. */
14145
14146 static void
14147 ignore_command (char *args, int from_tty)
14148 {
14149 char *p = args;
14150 int num;
14151
14152 if (p == 0)
14153 error_no_arg (_("a breakpoint number"));
14154
14155 num = get_number (&p);
14156 if (num == 0)
14157 error (_("bad breakpoint number: '%s'"), args);
14158 if (*p == 0)
14159 error (_("Second argument (specified ignore-count) is missing."));
14160
14161 set_ignore_count (num,
14162 longest_to_int (value_as_long (parse_and_eval (p))),
14163 from_tty);
14164 if (from_tty)
14165 printf_filtered ("\n");
14166 }
14167 \f
14168 /* Call FUNCTION on each of the breakpoints
14169 whose numbers are given in ARGS. */
14170
14171 static void
14172 map_breakpoint_numbers (const char *args,
14173 gdb::function_view<void (breakpoint *)> function)
14174 {
14175 int num;
14176 struct breakpoint *b, *tmp;
14177
14178 if (args == 0 || *args == '\0')
14179 error_no_arg (_("one or more breakpoint numbers"));
14180
14181 number_or_range_parser parser (args);
14182
14183 while (!parser.finished ())
14184 {
14185 const char *p = parser.cur_tok ();
14186 bool match = false;
14187
14188 num = parser.get_number ();
14189 if (num == 0)
14190 {
14191 warning (_("bad breakpoint number at or near '%s'"), p);
14192 }
14193 else
14194 {
14195 ALL_BREAKPOINTS_SAFE (b, tmp)
14196 if (b->number == num)
14197 {
14198 match = true;
14199 function (b);
14200 break;
14201 }
14202 if (!match)
14203 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14204 }
14205 }
14206 }
14207
14208 static struct bp_location *
14209 find_location_by_number (const char *number)
14210 {
14211 const char *p1;
14212 int bp_num;
14213 int loc_num;
14214 struct breakpoint *b;
14215 struct bp_location *loc;
14216
14217 p1 = number;
14218 bp_num = get_number_trailer (&p1, '.');
14219 if (bp_num == 0 || p1[0] != '.')
14220 error (_("Bad breakpoint number '%s'"), number);
14221
14222 ALL_BREAKPOINTS (b)
14223 if (b->number == bp_num)
14224 {
14225 break;
14226 }
14227
14228 if (!b || b->number != bp_num)
14229 error (_("Bad breakpoint number '%s'"), number);
14230
14231 /* Skip the dot. */
14232 ++p1;
14233 const char *save = p1;
14234 loc_num = get_number (&p1);
14235 if (loc_num == 0)
14236 error (_("Bad breakpoint location number '%s'"), number);
14237
14238 --loc_num;
14239 loc = b->loc;
14240 for (;loc_num && loc; --loc_num, loc = loc->next)
14241 ;
14242 if (!loc)
14243 error (_("Bad breakpoint location number '%s'"), save);
14244
14245 return loc;
14246 }
14247
14248
14249 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14250 If from_tty is nonzero, it prints a message to that effect,
14251 which ends with a period (no newline). */
14252
14253 void
14254 disable_breakpoint (struct breakpoint *bpt)
14255 {
14256 /* Never disable a watchpoint scope breakpoint; we want to
14257 hit them when we leave scope so we can delete both the
14258 watchpoint and its scope breakpoint at that time. */
14259 if (bpt->type == bp_watchpoint_scope)
14260 return;
14261
14262 bpt->enable_state = bp_disabled;
14263
14264 /* Mark breakpoint locations modified. */
14265 mark_breakpoint_modified (bpt);
14266
14267 if (target_supports_enable_disable_tracepoint ()
14268 && current_trace_status ()->running && is_tracepoint (bpt))
14269 {
14270 struct bp_location *location;
14271
14272 for (location = bpt->loc; location; location = location->next)
14273 target_disable_tracepoint (location);
14274 }
14275
14276 update_global_location_list (UGLL_DONT_INSERT);
14277
14278 observer_notify_breakpoint_modified (bpt);
14279 }
14280
14281 static void
14282 disable_command (const char *args, int from_tty)
14283 {
14284 if (args == 0)
14285 {
14286 struct breakpoint *bpt;
14287
14288 ALL_BREAKPOINTS (bpt)
14289 if (user_breakpoint_p (bpt))
14290 disable_breakpoint (bpt);
14291 }
14292 else
14293 {
14294 std::string num = extract_arg (&args);
14295
14296 while (!num.empty ())
14297 {
14298 if (num.find ('.') != std::string::npos)
14299 {
14300 struct bp_location *loc = find_location_by_number (num.c_str ());
14301
14302 if (loc)
14303 {
14304 if (loc->enabled)
14305 {
14306 loc->enabled = 0;
14307 mark_breakpoint_location_modified (loc);
14308 }
14309 if (target_supports_enable_disable_tracepoint ()
14310 && current_trace_status ()->running && loc->owner
14311 && is_tracepoint (loc->owner))
14312 target_disable_tracepoint (loc);
14313 }
14314 update_global_location_list (UGLL_DONT_INSERT);
14315 }
14316 else
14317 map_breakpoint_numbers
14318 (num.c_str (), [&] (breakpoint *b)
14319 {
14320 iterate_over_related_breakpoints (b, disable_breakpoint);
14321 });
14322 num = extract_arg (&args);
14323 }
14324 }
14325 }
14326
14327 static void
14328 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14329 int count)
14330 {
14331 int target_resources_ok;
14332
14333 if (bpt->type == bp_hardware_breakpoint)
14334 {
14335 int i;
14336 i = hw_breakpoint_used_count ();
14337 target_resources_ok =
14338 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14339 i + 1, 0);
14340 if (target_resources_ok == 0)
14341 error (_("No hardware breakpoint support in the target."));
14342 else if (target_resources_ok < 0)
14343 error (_("Hardware breakpoints used exceeds limit."));
14344 }
14345
14346 if (is_watchpoint (bpt))
14347 {
14348 /* Initialize it just to avoid a GCC false warning. */
14349 enum enable_state orig_enable_state = bp_disabled;
14350
14351 TRY
14352 {
14353 struct watchpoint *w = (struct watchpoint *) bpt;
14354
14355 orig_enable_state = bpt->enable_state;
14356 bpt->enable_state = bp_enabled;
14357 update_watchpoint (w, 1 /* reparse */);
14358 }
14359 CATCH (e, RETURN_MASK_ALL)
14360 {
14361 bpt->enable_state = orig_enable_state;
14362 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14363 bpt->number);
14364 return;
14365 }
14366 END_CATCH
14367 }
14368
14369 bpt->enable_state = bp_enabled;
14370
14371 /* Mark breakpoint locations modified. */
14372 mark_breakpoint_modified (bpt);
14373
14374 if (target_supports_enable_disable_tracepoint ()
14375 && current_trace_status ()->running && is_tracepoint (bpt))
14376 {
14377 struct bp_location *location;
14378
14379 for (location = bpt->loc; location; location = location->next)
14380 target_enable_tracepoint (location);
14381 }
14382
14383 bpt->disposition = disposition;
14384 bpt->enable_count = count;
14385 update_global_location_list (UGLL_MAY_INSERT);
14386
14387 observer_notify_breakpoint_modified (bpt);
14388 }
14389
14390
14391 void
14392 enable_breakpoint (struct breakpoint *bpt)
14393 {
14394 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14395 }
14396
14397 /* The enable command enables the specified breakpoints (or all defined
14398 breakpoints) so they once again become (or continue to be) effective
14399 in stopping the inferior. */
14400
14401 static void
14402 enable_command (const char *args, int from_tty)
14403 {
14404 if (args == 0)
14405 {
14406 struct breakpoint *bpt;
14407
14408 ALL_BREAKPOINTS (bpt)
14409 if (user_breakpoint_p (bpt))
14410 enable_breakpoint (bpt);
14411 }
14412 else
14413 {
14414 std::string num = extract_arg (&args);
14415
14416 while (!num.empty ())
14417 {
14418 if (num.find ('.') != std::string::npos)
14419 {
14420 struct bp_location *loc = find_location_by_number (num.c_str ());
14421
14422 if (loc)
14423 {
14424 if (!loc->enabled)
14425 {
14426 loc->enabled = 1;
14427 mark_breakpoint_location_modified (loc);
14428 }
14429 if (target_supports_enable_disable_tracepoint ()
14430 && current_trace_status ()->running && loc->owner
14431 && is_tracepoint (loc->owner))
14432 target_enable_tracepoint (loc);
14433 }
14434 update_global_location_list (UGLL_MAY_INSERT);
14435 }
14436 else
14437 map_breakpoint_numbers
14438 (num.c_str (), [&] (breakpoint *b)
14439 {
14440 iterate_over_related_breakpoints (b, enable_breakpoint);
14441 });
14442 num = extract_arg (&args);
14443 }
14444 }
14445 }
14446
14447 static void
14448 enable_once_command (const char *args, int from_tty)
14449 {
14450 map_breakpoint_numbers
14451 (args, [&] (breakpoint *b)
14452 {
14453 iterate_over_related_breakpoints
14454 (b, [&] (breakpoint *bpt)
14455 {
14456 enable_breakpoint_disp (bpt, disp_disable, 1);
14457 });
14458 });
14459 }
14460
14461 static void
14462 enable_count_command (const char *args, int from_tty)
14463 {
14464 int count;
14465
14466 if (args == NULL)
14467 error_no_arg (_("hit count"));
14468
14469 count = get_number (&args);
14470
14471 map_breakpoint_numbers
14472 (args, [&] (breakpoint *b)
14473 {
14474 iterate_over_related_breakpoints
14475 (b, [&] (breakpoint *bpt)
14476 {
14477 enable_breakpoint_disp (bpt, disp_disable, count);
14478 });
14479 });
14480 }
14481
14482 static void
14483 enable_delete_command (const char *args, int from_tty)
14484 {
14485 map_breakpoint_numbers
14486 (args, [&] (breakpoint *b)
14487 {
14488 iterate_over_related_breakpoints
14489 (b, [&] (breakpoint *bpt)
14490 {
14491 enable_breakpoint_disp (bpt, disp_del, 1);
14492 });
14493 });
14494 }
14495 \f
14496 static void
14497 set_breakpoint_cmd (const char *args, int from_tty)
14498 {
14499 }
14500
14501 static void
14502 show_breakpoint_cmd (const char *args, int from_tty)
14503 {
14504 }
14505
14506 /* Invalidate last known value of any hardware watchpoint if
14507 the memory which that value represents has been written to by
14508 GDB itself. */
14509
14510 static void
14511 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14512 CORE_ADDR addr, ssize_t len,
14513 const bfd_byte *data)
14514 {
14515 struct breakpoint *bp;
14516
14517 ALL_BREAKPOINTS (bp)
14518 if (bp->enable_state == bp_enabled
14519 && bp->type == bp_hardware_watchpoint)
14520 {
14521 struct watchpoint *wp = (struct watchpoint *) bp;
14522
14523 if (wp->val_valid && wp->val)
14524 {
14525 struct bp_location *loc;
14526
14527 for (loc = bp->loc; loc != NULL; loc = loc->next)
14528 if (loc->loc_type == bp_loc_hardware_watchpoint
14529 && loc->address + loc->length > addr
14530 && addr + len > loc->address)
14531 {
14532 value_free (wp->val);
14533 wp->val = NULL;
14534 wp->val_valid = 0;
14535 }
14536 }
14537 }
14538 }
14539
14540 /* Create and insert a breakpoint for software single step. */
14541
14542 void
14543 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14544 const address_space *aspace,
14545 CORE_ADDR next_pc)
14546 {
14547 struct thread_info *tp = inferior_thread ();
14548 struct symtab_and_line sal;
14549 CORE_ADDR pc = next_pc;
14550
14551 if (tp->control.single_step_breakpoints == NULL)
14552 {
14553 tp->control.single_step_breakpoints
14554 = new_single_step_breakpoint (tp->global_num, gdbarch);
14555 }
14556
14557 sal = find_pc_line (pc, 0);
14558 sal.pc = pc;
14559 sal.section = find_pc_overlay (pc);
14560 sal.explicit_pc = 1;
14561 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14562
14563 update_global_location_list (UGLL_INSERT);
14564 }
14565
14566 /* Insert single step breakpoints according to the current state. */
14567
14568 int
14569 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14570 {
14571 struct regcache *regcache = get_current_regcache ();
14572 std::vector<CORE_ADDR> next_pcs;
14573
14574 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14575
14576 if (!next_pcs.empty ())
14577 {
14578 struct frame_info *frame = get_current_frame ();
14579 struct address_space *aspace = get_frame_address_space (frame);
14580
14581 for (CORE_ADDR pc : next_pcs)
14582 insert_single_step_breakpoint (gdbarch, aspace, pc);
14583
14584 return 1;
14585 }
14586 else
14587 return 0;
14588 }
14589
14590 /* See breakpoint.h. */
14591
14592 int
14593 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14594 const address_space *aspace,
14595 CORE_ADDR pc)
14596 {
14597 struct bp_location *loc;
14598
14599 for (loc = bp->loc; loc != NULL; loc = loc->next)
14600 if (loc->inserted
14601 && breakpoint_location_address_match (loc, aspace, pc))
14602 return 1;
14603
14604 return 0;
14605 }
14606
14607 /* Check whether a software single-step breakpoint is inserted at
14608 PC. */
14609
14610 int
14611 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14612 CORE_ADDR pc)
14613 {
14614 struct breakpoint *bpt;
14615
14616 ALL_BREAKPOINTS (bpt)
14617 {
14618 if (bpt->type == bp_single_step
14619 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14620 return 1;
14621 }
14622 return 0;
14623 }
14624
14625 /* Tracepoint-specific operations. */
14626
14627 /* Set tracepoint count to NUM. */
14628 static void
14629 set_tracepoint_count (int num)
14630 {
14631 tracepoint_count = num;
14632 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14633 }
14634
14635 static void
14636 trace_command (char *arg_in, int from_tty)
14637 {
14638 const char *arg = arg_in;
14639 struct breakpoint_ops *ops;
14640
14641 event_location_up location = string_to_event_location (&arg,
14642 current_language);
14643 if (location != NULL
14644 && event_location_type (location.get ()) == PROBE_LOCATION)
14645 ops = &tracepoint_probe_breakpoint_ops;
14646 else
14647 ops = &tracepoint_breakpoint_ops;
14648
14649 create_breakpoint (get_current_arch (),
14650 location.get (),
14651 NULL, 0, arg, 1 /* parse arg */,
14652 0 /* tempflag */,
14653 bp_tracepoint /* type_wanted */,
14654 0 /* Ignore count */,
14655 pending_break_support,
14656 ops,
14657 from_tty,
14658 1 /* enabled */,
14659 0 /* internal */, 0);
14660 }
14661
14662 static void
14663 ftrace_command (char *arg_in, int from_tty)
14664 {
14665 const char *arg = arg_in;
14666 event_location_up location = string_to_event_location (&arg,
14667 current_language);
14668 create_breakpoint (get_current_arch (),
14669 location.get (),
14670 NULL, 0, arg, 1 /* parse arg */,
14671 0 /* tempflag */,
14672 bp_fast_tracepoint /* type_wanted */,
14673 0 /* Ignore count */,
14674 pending_break_support,
14675 &tracepoint_breakpoint_ops,
14676 from_tty,
14677 1 /* enabled */,
14678 0 /* internal */, 0);
14679 }
14680
14681 /* strace command implementation. Creates a static tracepoint. */
14682
14683 static void
14684 strace_command (char *arg_in, int from_tty)
14685 {
14686 const char *arg = arg_in;
14687 struct breakpoint_ops *ops;
14688 event_location_up location;
14689
14690 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14691 or with a normal static tracepoint. */
14692 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14693 {
14694 ops = &strace_marker_breakpoint_ops;
14695 location = new_linespec_location (&arg);
14696 }
14697 else
14698 {
14699 ops = &tracepoint_breakpoint_ops;
14700 location = string_to_event_location (&arg, current_language);
14701 }
14702
14703 create_breakpoint (get_current_arch (),
14704 location.get (),
14705 NULL, 0, arg, 1 /* parse arg */,
14706 0 /* tempflag */,
14707 bp_static_tracepoint /* type_wanted */,
14708 0 /* Ignore count */,
14709 pending_break_support,
14710 ops,
14711 from_tty,
14712 1 /* enabled */,
14713 0 /* internal */, 0);
14714 }
14715
14716 /* Set up a fake reader function that gets command lines from a linked
14717 list that was acquired during tracepoint uploading. */
14718
14719 static struct uploaded_tp *this_utp;
14720 static int next_cmd;
14721
14722 static char *
14723 read_uploaded_action (void)
14724 {
14725 char *rslt;
14726
14727 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14728
14729 next_cmd++;
14730
14731 return rslt;
14732 }
14733
14734 /* Given information about a tracepoint as recorded on a target (which
14735 can be either a live system or a trace file), attempt to create an
14736 equivalent GDB tracepoint. This is not a reliable process, since
14737 the target does not necessarily have all the information used when
14738 the tracepoint was originally defined. */
14739
14740 struct tracepoint *
14741 create_tracepoint_from_upload (struct uploaded_tp *utp)
14742 {
14743 const char *addr_str;
14744 char small_buf[100];
14745 struct tracepoint *tp;
14746
14747 if (utp->at_string)
14748 addr_str = utp->at_string;
14749 else
14750 {
14751 /* In the absence of a source location, fall back to raw
14752 address. Since there is no way to confirm that the address
14753 means the same thing as when the trace was started, warn the
14754 user. */
14755 warning (_("Uploaded tracepoint %d has no "
14756 "source location, using raw address"),
14757 utp->number);
14758 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14759 addr_str = small_buf;
14760 }
14761
14762 /* There's not much we can do with a sequence of bytecodes. */
14763 if (utp->cond && !utp->cond_string)
14764 warning (_("Uploaded tracepoint %d condition "
14765 "has no source form, ignoring it"),
14766 utp->number);
14767
14768 event_location_up location = string_to_event_location (&addr_str,
14769 current_language);
14770 if (!create_breakpoint (get_current_arch (),
14771 location.get (),
14772 utp->cond_string, -1, addr_str,
14773 0 /* parse cond/thread */,
14774 0 /* tempflag */,
14775 utp->type /* type_wanted */,
14776 0 /* Ignore count */,
14777 pending_break_support,
14778 &tracepoint_breakpoint_ops,
14779 0 /* from_tty */,
14780 utp->enabled /* enabled */,
14781 0 /* internal */,
14782 CREATE_BREAKPOINT_FLAGS_INSERTED))
14783 return NULL;
14784
14785 /* Get the tracepoint we just created. */
14786 tp = get_tracepoint (tracepoint_count);
14787 gdb_assert (tp != NULL);
14788
14789 if (utp->pass > 0)
14790 {
14791 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14792 tp->number);
14793
14794 trace_pass_command (small_buf, 0);
14795 }
14796
14797 /* If we have uploaded versions of the original commands, set up a
14798 special-purpose "reader" function and call the usual command line
14799 reader, then pass the result to the breakpoint command-setting
14800 function. */
14801 if (!VEC_empty (char_ptr, utp->cmd_strings))
14802 {
14803 command_line_up cmd_list;
14804
14805 this_utp = utp;
14806 next_cmd = 0;
14807
14808 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14809
14810 breakpoint_set_commands (tp, std::move (cmd_list));
14811 }
14812 else if (!VEC_empty (char_ptr, utp->actions)
14813 || !VEC_empty (char_ptr, utp->step_actions))
14814 warning (_("Uploaded tracepoint %d actions "
14815 "have no source form, ignoring them"),
14816 utp->number);
14817
14818 /* Copy any status information that might be available. */
14819 tp->hit_count = utp->hit_count;
14820 tp->traceframe_usage = utp->traceframe_usage;
14821
14822 return tp;
14823 }
14824
14825 /* Print information on tracepoint number TPNUM_EXP, or all if
14826 omitted. */
14827
14828 static void
14829 info_tracepoints_command (char *args, int from_tty)
14830 {
14831 struct ui_out *uiout = current_uiout;
14832 int num_printed;
14833
14834 num_printed = breakpoint_1 (args, 0, is_tracepoint);
14835
14836 if (num_printed == 0)
14837 {
14838 if (args == NULL || *args == '\0')
14839 uiout->message ("No tracepoints.\n");
14840 else
14841 uiout->message ("No tracepoint matching '%s'.\n", args);
14842 }
14843
14844 default_collect_info ();
14845 }
14846
14847 /* The 'enable trace' command enables tracepoints.
14848 Not supported by all targets. */
14849 static void
14850 enable_trace_command (char *args, int from_tty)
14851 {
14852 enable_command (args, from_tty);
14853 }
14854
14855 /* The 'disable trace' command disables tracepoints.
14856 Not supported by all targets. */
14857 static void
14858 disable_trace_command (char *args, int from_tty)
14859 {
14860 disable_command (args, from_tty);
14861 }
14862
14863 /* Remove a tracepoint (or all if no argument). */
14864 static void
14865 delete_trace_command (const char *arg, int from_tty)
14866 {
14867 struct breakpoint *b, *b_tmp;
14868
14869 dont_repeat ();
14870
14871 if (arg == 0)
14872 {
14873 int breaks_to_delete = 0;
14874
14875 /* Delete all breakpoints if no argument.
14876 Do not delete internal or call-dummy breakpoints, these
14877 have to be deleted with an explicit breakpoint number
14878 argument. */
14879 ALL_TRACEPOINTS (b)
14880 if (is_tracepoint (b) && user_breakpoint_p (b))
14881 {
14882 breaks_to_delete = 1;
14883 break;
14884 }
14885
14886 /* Ask user only if there are some breakpoints to delete. */
14887 if (!from_tty
14888 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14889 {
14890 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14891 if (is_tracepoint (b) && user_breakpoint_p (b))
14892 delete_breakpoint (b);
14893 }
14894 }
14895 else
14896 map_breakpoint_numbers
14897 (arg, [&] (breakpoint *b)
14898 {
14899 iterate_over_related_breakpoints (b, delete_breakpoint);
14900 });
14901 }
14902
14903 /* Helper function for trace_pass_command. */
14904
14905 static void
14906 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14907 {
14908 tp->pass_count = count;
14909 observer_notify_breakpoint_modified (tp);
14910 if (from_tty)
14911 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14912 tp->number, count);
14913 }
14914
14915 /* Set passcount for tracepoint.
14916
14917 First command argument is passcount, second is tracepoint number.
14918 If tracepoint number omitted, apply to most recently defined.
14919 Also accepts special argument "all". */
14920
14921 static void
14922 trace_pass_command (char *args, int from_tty)
14923 {
14924 struct tracepoint *t1;
14925 unsigned int count;
14926
14927 if (args == 0 || *args == 0)
14928 error (_("passcount command requires an "
14929 "argument (count + optional TP num)"));
14930
14931 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
14932
14933 args = skip_spaces (args);
14934 if (*args && strncasecmp (args, "all", 3) == 0)
14935 {
14936 struct breakpoint *b;
14937
14938 args += 3; /* Skip special argument "all". */
14939 if (*args)
14940 error (_("Junk at end of arguments."));
14941
14942 ALL_TRACEPOINTS (b)
14943 {
14944 t1 = (struct tracepoint *) b;
14945 trace_pass_set_count (t1, count, from_tty);
14946 }
14947 }
14948 else if (*args == '\0')
14949 {
14950 t1 = get_tracepoint_by_number (&args, NULL);
14951 if (t1)
14952 trace_pass_set_count (t1, count, from_tty);
14953 }
14954 else
14955 {
14956 number_or_range_parser parser (args);
14957 while (!parser.finished ())
14958 {
14959 t1 = get_tracepoint_by_number (&args, &parser);
14960 if (t1)
14961 trace_pass_set_count (t1, count, from_tty);
14962 }
14963 }
14964 }
14965
14966 struct tracepoint *
14967 get_tracepoint (int num)
14968 {
14969 struct breakpoint *t;
14970
14971 ALL_TRACEPOINTS (t)
14972 if (t->number == num)
14973 return (struct tracepoint *) t;
14974
14975 return NULL;
14976 }
14977
14978 /* Find the tracepoint with the given target-side number (which may be
14979 different from the tracepoint number after disconnecting and
14980 reconnecting). */
14981
14982 struct tracepoint *
14983 get_tracepoint_by_number_on_target (int num)
14984 {
14985 struct breakpoint *b;
14986
14987 ALL_TRACEPOINTS (b)
14988 {
14989 struct tracepoint *t = (struct tracepoint *) b;
14990
14991 if (t->number_on_target == num)
14992 return t;
14993 }
14994
14995 return NULL;
14996 }
14997
14998 /* Utility: parse a tracepoint number and look it up in the list.
14999 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15000 If the argument is missing, the most recent tracepoint
15001 (tracepoint_count) is returned. */
15002
15003 struct tracepoint *
15004 get_tracepoint_by_number (char **arg,
15005 number_or_range_parser *parser)
15006 {
15007 struct breakpoint *t;
15008 int tpnum;
15009 char *instring = arg == NULL ? NULL : *arg;
15010
15011 if (parser != NULL)
15012 {
15013 gdb_assert (!parser->finished ());
15014 tpnum = parser->get_number ();
15015 }
15016 else if (arg == NULL || *arg == NULL || ! **arg)
15017 tpnum = tracepoint_count;
15018 else
15019 tpnum = get_number (arg);
15020
15021 if (tpnum <= 0)
15022 {
15023 if (instring && *instring)
15024 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15025 instring);
15026 else
15027 printf_filtered (_("No previous tracepoint\n"));
15028 return NULL;
15029 }
15030
15031 ALL_TRACEPOINTS (t)
15032 if (t->number == tpnum)
15033 {
15034 return (struct tracepoint *) t;
15035 }
15036
15037 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15038 return NULL;
15039 }
15040
15041 void
15042 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15043 {
15044 if (b->thread != -1)
15045 fprintf_unfiltered (fp, " thread %d", b->thread);
15046
15047 if (b->task != 0)
15048 fprintf_unfiltered (fp, " task %d", b->task);
15049
15050 fprintf_unfiltered (fp, "\n");
15051 }
15052
15053 /* Save information on user settable breakpoints (watchpoints, etc) to
15054 a new script file named FILENAME. If FILTER is non-NULL, call it
15055 on each breakpoint and only include the ones for which it returns
15056 non-zero. */
15057
15058 static void
15059 save_breakpoints (const char *filename, int from_tty,
15060 int (*filter) (const struct breakpoint *))
15061 {
15062 struct breakpoint *tp;
15063 int any = 0;
15064 int extra_trace_bits = 0;
15065
15066 if (filename == 0 || *filename == 0)
15067 error (_("Argument required (file name in which to save)"));
15068
15069 /* See if we have anything to save. */
15070 ALL_BREAKPOINTS (tp)
15071 {
15072 /* Skip internal and momentary breakpoints. */
15073 if (!user_breakpoint_p (tp))
15074 continue;
15075
15076 /* If we have a filter, only save the breakpoints it accepts. */
15077 if (filter && !filter (tp))
15078 continue;
15079
15080 any = 1;
15081
15082 if (is_tracepoint (tp))
15083 {
15084 extra_trace_bits = 1;
15085
15086 /* We can stop searching. */
15087 break;
15088 }
15089 }
15090
15091 if (!any)
15092 {
15093 warning (_("Nothing to save."));
15094 return;
15095 }
15096
15097 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15098
15099 stdio_file fp;
15100
15101 if (!fp.open (expanded_filename.get (), "w"))
15102 error (_("Unable to open file '%s' for saving (%s)"),
15103 expanded_filename.get (), safe_strerror (errno));
15104
15105 if (extra_trace_bits)
15106 save_trace_state_variables (&fp);
15107
15108 ALL_BREAKPOINTS (tp)
15109 {
15110 /* Skip internal and momentary breakpoints. */
15111 if (!user_breakpoint_p (tp))
15112 continue;
15113
15114 /* If we have a filter, only save the breakpoints it accepts. */
15115 if (filter && !filter (tp))
15116 continue;
15117
15118 tp->ops->print_recreate (tp, &fp);
15119
15120 /* Note, we can't rely on tp->number for anything, as we can't
15121 assume the recreated breakpoint numbers will match. Use $bpnum
15122 instead. */
15123
15124 if (tp->cond_string)
15125 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15126
15127 if (tp->ignore_count)
15128 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15129
15130 if (tp->type != bp_dprintf && tp->commands)
15131 {
15132 fp.puts (" commands\n");
15133
15134 current_uiout->redirect (&fp);
15135 TRY
15136 {
15137 print_command_lines (current_uiout, tp->commands.get (), 2);
15138 }
15139 CATCH (ex, RETURN_MASK_ALL)
15140 {
15141 current_uiout->redirect (NULL);
15142 throw_exception (ex);
15143 }
15144 END_CATCH
15145
15146 current_uiout->redirect (NULL);
15147 fp.puts (" end\n");
15148 }
15149
15150 if (tp->enable_state == bp_disabled)
15151 fp.puts ("disable $bpnum\n");
15152
15153 /* If this is a multi-location breakpoint, check if the locations
15154 should be individually disabled. Watchpoint locations are
15155 special, and not user visible. */
15156 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15157 {
15158 struct bp_location *loc;
15159 int n = 1;
15160
15161 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15162 if (!loc->enabled)
15163 fp.printf ("disable $bpnum.%d\n", n);
15164 }
15165 }
15166
15167 if (extra_trace_bits && *default_collect)
15168 fp.printf ("set default-collect %s\n", default_collect);
15169
15170 if (from_tty)
15171 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15172 }
15173
15174 /* The `save breakpoints' command. */
15175
15176 static void
15177 save_breakpoints_command (const char *args, int from_tty)
15178 {
15179 save_breakpoints (args, from_tty, NULL);
15180 }
15181
15182 /* The `save tracepoints' command. */
15183
15184 static void
15185 save_tracepoints_command (const char *args, int from_tty)
15186 {
15187 save_breakpoints (args, from_tty, is_tracepoint);
15188 }
15189
15190 /* Create a vector of all tracepoints. */
15191
15192 VEC(breakpoint_p) *
15193 all_tracepoints (void)
15194 {
15195 VEC(breakpoint_p) *tp_vec = 0;
15196 struct breakpoint *tp;
15197
15198 ALL_TRACEPOINTS (tp)
15199 {
15200 VEC_safe_push (breakpoint_p, tp_vec, tp);
15201 }
15202
15203 return tp_vec;
15204 }
15205
15206 \f
15207 /* This help string is used to consolidate all the help string for specifying
15208 locations used by several commands. */
15209
15210 #define LOCATION_HELP_STRING \
15211 "Linespecs are colon-separated lists of location parameters, such as\n\
15212 source filename, function name, label name, and line number.\n\
15213 Example: To specify the start of a label named \"the_top\" in the\n\
15214 function \"fact\" in the file \"factorial.c\", use\n\
15215 \"factorial.c:fact:the_top\".\n\
15216 \n\
15217 Address locations begin with \"*\" and specify an exact address in the\n\
15218 program. Example: To specify the fourth byte past the start function\n\
15219 \"main\", use \"*main + 4\".\n\
15220 \n\
15221 Explicit locations are similar to linespecs but use an option/argument\n\
15222 syntax to specify location parameters.\n\
15223 Example: To specify the start of the label named \"the_top\" in the\n\
15224 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15225 -function fact -label the_top\".\n"
15226
15227 /* This help string is used for the break, hbreak, tbreak and thbreak
15228 commands. It is defined as a macro to prevent duplication.
15229 COMMAND should be a string constant containing the name of the
15230 command. */
15231
15232 #define BREAK_ARGS_HELP(command) \
15233 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15234 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15235 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15236 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15237 `-probe-dtrace' (for a DTrace probe).\n\
15238 LOCATION may be a linespec, address, or explicit location as described\n\
15239 below.\n\
15240 \n\
15241 With no LOCATION, uses current execution address of the selected\n\
15242 stack frame. This is useful for breaking on return to a stack frame.\n\
15243 \n\
15244 THREADNUM is the number from \"info threads\".\n\
15245 CONDITION is a boolean expression.\n\
15246 \n" LOCATION_HELP_STRING "\n\
15247 Multiple breakpoints at one place are permitted, and useful if their\n\
15248 conditions are different.\n\
15249 \n\
15250 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15251
15252 /* List of subcommands for "catch". */
15253 static struct cmd_list_element *catch_cmdlist;
15254
15255 /* List of subcommands for "tcatch". */
15256 static struct cmd_list_element *tcatch_cmdlist;
15257
15258 void
15259 add_catch_command (const char *name, const char *docstring,
15260 cmd_sfunc_ftype *sfunc,
15261 completer_ftype *completer,
15262 void *user_data_catch,
15263 void *user_data_tcatch)
15264 {
15265 struct cmd_list_element *command;
15266
15267 command = add_cmd (name, class_breakpoint, docstring,
15268 &catch_cmdlist);
15269 set_cmd_sfunc (command, sfunc);
15270 set_cmd_context (command, user_data_catch);
15271 set_cmd_completer (command, completer);
15272
15273 command = add_cmd (name, class_breakpoint, docstring,
15274 &tcatch_cmdlist);
15275 set_cmd_sfunc (command, sfunc);
15276 set_cmd_context (command, user_data_tcatch);
15277 set_cmd_completer (command, completer);
15278 }
15279
15280 static void
15281 save_command (const char *arg, int from_tty)
15282 {
15283 printf_unfiltered (_("\"save\" must be followed by "
15284 "the name of a save subcommand.\n"));
15285 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15286 }
15287
15288 struct breakpoint *
15289 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15290 void *data)
15291 {
15292 struct breakpoint *b, *b_tmp;
15293
15294 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15295 {
15296 if ((*callback) (b, data))
15297 return b;
15298 }
15299
15300 return NULL;
15301 }
15302
15303 /* Zero if any of the breakpoint's locations could be a location where
15304 functions have been inlined, nonzero otherwise. */
15305
15306 static int
15307 is_non_inline_function (struct breakpoint *b)
15308 {
15309 /* The shared library event breakpoint is set on the address of a
15310 non-inline function. */
15311 if (b->type == bp_shlib_event)
15312 return 1;
15313
15314 return 0;
15315 }
15316
15317 /* Nonzero if the specified PC cannot be a location where functions
15318 have been inlined. */
15319
15320 int
15321 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15322 const struct target_waitstatus *ws)
15323 {
15324 struct breakpoint *b;
15325 struct bp_location *bl;
15326
15327 ALL_BREAKPOINTS (b)
15328 {
15329 if (!is_non_inline_function (b))
15330 continue;
15331
15332 for (bl = b->loc; bl != NULL; bl = bl->next)
15333 {
15334 if (!bl->shlib_disabled
15335 && bpstat_check_location (bl, aspace, pc, ws))
15336 return 1;
15337 }
15338 }
15339
15340 return 0;
15341 }
15342
15343 /* Remove any references to OBJFILE which is going to be freed. */
15344
15345 void
15346 breakpoint_free_objfile (struct objfile *objfile)
15347 {
15348 struct bp_location **locp, *loc;
15349
15350 ALL_BP_LOCATIONS (loc, locp)
15351 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15352 loc->symtab = NULL;
15353 }
15354
15355 void
15356 initialize_breakpoint_ops (void)
15357 {
15358 static int initialized = 0;
15359
15360 struct breakpoint_ops *ops;
15361
15362 if (initialized)
15363 return;
15364 initialized = 1;
15365
15366 /* The breakpoint_ops structure to be inherit by all kinds of
15367 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15368 internal and momentary breakpoints, etc.). */
15369 ops = &bkpt_base_breakpoint_ops;
15370 *ops = base_breakpoint_ops;
15371 ops->re_set = bkpt_re_set;
15372 ops->insert_location = bkpt_insert_location;
15373 ops->remove_location = bkpt_remove_location;
15374 ops->breakpoint_hit = bkpt_breakpoint_hit;
15375 ops->create_sals_from_location = bkpt_create_sals_from_location;
15376 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15377 ops->decode_location = bkpt_decode_location;
15378
15379 /* The breakpoint_ops structure to be used in regular breakpoints. */
15380 ops = &bkpt_breakpoint_ops;
15381 *ops = bkpt_base_breakpoint_ops;
15382 ops->re_set = bkpt_re_set;
15383 ops->resources_needed = bkpt_resources_needed;
15384 ops->print_it = bkpt_print_it;
15385 ops->print_mention = bkpt_print_mention;
15386 ops->print_recreate = bkpt_print_recreate;
15387
15388 /* Ranged breakpoints. */
15389 ops = &ranged_breakpoint_ops;
15390 *ops = bkpt_breakpoint_ops;
15391 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15392 ops->resources_needed = resources_needed_ranged_breakpoint;
15393 ops->print_it = print_it_ranged_breakpoint;
15394 ops->print_one = print_one_ranged_breakpoint;
15395 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15396 ops->print_mention = print_mention_ranged_breakpoint;
15397 ops->print_recreate = print_recreate_ranged_breakpoint;
15398
15399 /* Internal breakpoints. */
15400 ops = &internal_breakpoint_ops;
15401 *ops = bkpt_base_breakpoint_ops;
15402 ops->re_set = internal_bkpt_re_set;
15403 ops->check_status = internal_bkpt_check_status;
15404 ops->print_it = internal_bkpt_print_it;
15405 ops->print_mention = internal_bkpt_print_mention;
15406
15407 /* Momentary breakpoints. */
15408 ops = &momentary_breakpoint_ops;
15409 *ops = bkpt_base_breakpoint_ops;
15410 ops->re_set = momentary_bkpt_re_set;
15411 ops->check_status = momentary_bkpt_check_status;
15412 ops->print_it = momentary_bkpt_print_it;
15413 ops->print_mention = momentary_bkpt_print_mention;
15414
15415 /* Probe breakpoints. */
15416 ops = &bkpt_probe_breakpoint_ops;
15417 *ops = bkpt_breakpoint_ops;
15418 ops->insert_location = bkpt_probe_insert_location;
15419 ops->remove_location = bkpt_probe_remove_location;
15420 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15421 ops->decode_location = bkpt_probe_decode_location;
15422
15423 /* Watchpoints. */
15424 ops = &watchpoint_breakpoint_ops;
15425 *ops = base_breakpoint_ops;
15426 ops->re_set = re_set_watchpoint;
15427 ops->insert_location = insert_watchpoint;
15428 ops->remove_location = remove_watchpoint;
15429 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15430 ops->check_status = check_status_watchpoint;
15431 ops->resources_needed = resources_needed_watchpoint;
15432 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15433 ops->print_it = print_it_watchpoint;
15434 ops->print_mention = print_mention_watchpoint;
15435 ops->print_recreate = print_recreate_watchpoint;
15436 ops->explains_signal = explains_signal_watchpoint;
15437
15438 /* Masked watchpoints. */
15439 ops = &masked_watchpoint_breakpoint_ops;
15440 *ops = watchpoint_breakpoint_ops;
15441 ops->insert_location = insert_masked_watchpoint;
15442 ops->remove_location = remove_masked_watchpoint;
15443 ops->resources_needed = resources_needed_masked_watchpoint;
15444 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15445 ops->print_it = print_it_masked_watchpoint;
15446 ops->print_one_detail = print_one_detail_masked_watchpoint;
15447 ops->print_mention = print_mention_masked_watchpoint;
15448 ops->print_recreate = print_recreate_masked_watchpoint;
15449
15450 /* Tracepoints. */
15451 ops = &tracepoint_breakpoint_ops;
15452 *ops = base_breakpoint_ops;
15453 ops->re_set = tracepoint_re_set;
15454 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15455 ops->print_one_detail = tracepoint_print_one_detail;
15456 ops->print_mention = tracepoint_print_mention;
15457 ops->print_recreate = tracepoint_print_recreate;
15458 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15459 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15460 ops->decode_location = tracepoint_decode_location;
15461
15462 /* Probe tracepoints. */
15463 ops = &tracepoint_probe_breakpoint_ops;
15464 *ops = tracepoint_breakpoint_ops;
15465 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15466 ops->decode_location = tracepoint_probe_decode_location;
15467
15468 /* Static tracepoints with marker (`-m'). */
15469 ops = &strace_marker_breakpoint_ops;
15470 *ops = tracepoint_breakpoint_ops;
15471 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15472 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15473 ops->decode_location = strace_marker_decode_location;
15474
15475 /* Fork catchpoints. */
15476 ops = &catch_fork_breakpoint_ops;
15477 *ops = base_breakpoint_ops;
15478 ops->insert_location = insert_catch_fork;
15479 ops->remove_location = remove_catch_fork;
15480 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15481 ops->print_it = print_it_catch_fork;
15482 ops->print_one = print_one_catch_fork;
15483 ops->print_mention = print_mention_catch_fork;
15484 ops->print_recreate = print_recreate_catch_fork;
15485
15486 /* Vfork catchpoints. */
15487 ops = &catch_vfork_breakpoint_ops;
15488 *ops = base_breakpoint_ops;
15489 ops->insert_location = insert_catch_vfork;
15490 ops->remove_location = remove_catch_vfork;
15491 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15492 ops->print_it = print_it_catch_vfork;
15493 ops->print_one = print_one_catch_vfork;
15494 ops->print_mention = print_mention_catch_vfork;
15495 ops->print_recreate = print_recreate_catch_vfork;
15496
15497 /* Exec catchpoints. */
15498 ops = &catch_exec_breakpoint_ops;
15499 *ops = base_breakpoint_ops;
15500 ops->insert_location = insert_catch_exec;
15501 ops->remove_location = remove_catch_exec;
15502 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15503 ops->print_it = print_it_catch_exec;
15504 ops->print_one = print_one_catch_exec;
15505 ops->print_mention = print_mention_catch_exec;
15506 ops->print_recreate = print_recreate_catch_exec;
15507
15508 /* Solib-related catchpoints. */
15509 ops = &catch_solib_breakpoint_ops;
15510 *ops = base_breakpoint_ops;
15511 ops->insert_location = insert_catch_solib;
15512 ops->remove_location = remove_catch_solib;
15513 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15514 ops->check_status = check_status_catch_solib;
15515 ops->print_it = print_it_catch_solib;
15516 ops->print_one = print_one_catch_solib;
15517 ops->print_mention = print_mention_catch_solib;
15518 ops->print_recreate = print_recreate_catch_solib;
15519
15520 ops = &dprintf_breakpoint_ops;
15521 *ops = bkpt_base_breakpoint_ops;
15522 ops->re_set = dprintf_re_set;
15523 ops->resources_needed = bkpt_resources_needed;
15524 ops->print_it = bkpt_print_it;
15525 ops->print_mention = bkpt_print_mention;
15526 ops->print_recreate = dprintf_print_recreate;
15527 ops->after_condition_true = dprintf_after_condition_true;
15528 ops->breakpoint_hit = dprintf_breakpoint_hit;
15529 }
15530
15531 /* Chain containing all defined "enable breakpoint" subcommands. */
15532
15533 static struct cmd_list_element *enablebreaklist = NULL;
15534
15535 void
15536 _initialize_breakpoint (void)
15537 {
15538 struct cmd_list_element *c;
15539
15540 initialize_breakpoint_ops ();
15541
15542 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15543 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15544 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15545
15546 breakpoint_objfile_key
15547 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15548
15549 breakpoint_chain = 0;
15550 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15551 before a breakpoint is set. */
15552 breakpoint_count = 0;
15553
15554 tracepoint_count = 0;
15555
15556 add_com ("ignore", class_breakpoint, ignore_command, _("\
15557 Set ignore-count of breakpoint number N to COUNT.\n\
15558 Usage is `ignore N COUNT'."));
15559
15560 add_com ("commands", class_breakpoint, commands_command, _("\
15561 Set commands to be executed when the given breakpoints are hit.\n\
15562 Give a space-separated breakpoint list as argument after \"commands\".\n\
15563 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15564 (e.g. `5-7').\n\
15565 With no argument, the targeted breakpoint is the last one set.\n\
15566 The commands themselves follow starting on the next line.\n\
15567 Type a line containing \"end\" to indicate the end of them.\n\
15568 Give \"silent\" as the first line to make the breakpoint silent;\n\
15569 then no output is printed when it is hit, except what the commands print."));
15570
15571 c = add_com ("condition", class_breakpoint, condition_command, _("\
15572 Specify breakpoint number N to break only if COND is true.\n\
15573 Usage is `condition N COND', where N is an integer and COND is an\n\
15574 expression to be evaluated whenever breakpoint N is reached."));
15575 set_cmd_completer (c, condition_completer);
15576
15577 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15578 Set a temporary breakpoint.\n\
15579 Like \"break\" except the breakpoint is only temporary,\n\
15580 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15581 by using \"enable delete\" on the breakpoint number.\n\
15582 \n"
15583 BREAK_ARGS_HELP ("tbreak")));
15584 set_cmd_completer (c, location_completer);
15585
15586 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15587 Set a hardware assisted breakpoint.\n\
15588 Like \"break\" except the breakpoint requires hardware support,\n\
15589 some target hardware may not have this support.\n\
15590 \n"
15591 BREAK_ARGS_HELP ("hbreak")));
15592 set_cmd_completer (c, location_completer);
15593
15594 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15595 Set a temporary hardware assisted breakpoint.\n\
15596 Like \"hbreak\" except the breakpoint is only temporary,\n\
15597 so it will be deleted when hit.\n\
15598 \n"
15599 BREAK_ARGS_HELP ("thbreak")));
15600 set_cmd_completer (c, location_completer);
15601
15602 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15603 Enable some breakpoints.\n\
15604 Give breakpoint numbers (separated by spaces) as arguments.\n\
15605 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15606 This is used to cancel the effect of the \"disable\" command.\n\
15607 With a subcommand you can enable temporarily."),
15608 &enablelist, "enable ", 1, &cmdlist);
15609
15610 add_com_alias ("en", "enable", class_breakpoint, 1);
15611
15612 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15613 Enable some breakpoints.\n\
15614 Give breakpoint numbers (separated by spaces) as arguments.\n\
15615 This is used to cancel the effect of the \"disable\" command.\n\
15616 May be abbreviated to simply \"enable\".\n"),
15617 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15618
15619 add_cmd ("once", no_class, enable_once_command, _("\
15620 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15621 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15622 &enablebreaklist);
15623
15624 add_cmd ("delete", no_class, enable_delete_command, _("\
15625 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15626 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15627 &enablebreaklist);
15628
15629 add_cmd ("count", no_class, enable_count_command, _("\
15630 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15631 If a breakpoint is hit while enabled in this fashion,\n\
15632 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15633 &enablebreaklist);
15634
15635 add_cmd ("delete", no_class, enable_delete_command, _("\
15636 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15637 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15638 &enablelist);
15639
15640 add_cmd ("once", no_class, enable_once_command, _("\
15641 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15642 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15643 &enablelist);
15644
15645 add_cmd ("count", no_class, enable_count_command, _("\
15646 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15647 If a breakpoint is hit while enabled in this fashion,\n\
15648 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15649 &enablelist);
15650
15651 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15652 Disable some breakpoints.\n\
15653 Arguments are breakpoint numbers with spaces in between.\n\
15654 To disable all breakpoints, give no argument.\n\
15655 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15656 &disablelist, "disable ", 1, &cmdlist);
15657 add_com_alias ("dis", "disable", class_breakpoint, 1);
15658 add_com_alias ("disa", "disable", class_breakpoint, 1);
15659
15660 add_cmd ("breakpoints", class_alias, disable_command, _("\
15661 Disable some breakpoints.\n\
15662 Arguments are breakpoint numbers with spaces in between.\n\
15663 To disable all breakpoints, give no argument.\n\
15664 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15665 This command may be abbreviated \"disable\"."),
15666 &disablelist);
15667
15668 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15669 Delete some breakpoints or auto-display expressions.\n\
15670 Arguments are breakpoint numbers with spaces in between.\n\
15671 To delete all breakpoints, give no argument.\n\
15672 \n\
15673 Also a prefix command for deletion of other GDB objects.\n\
15674 The \"unset\" command is also an alias for \"delete\"."),
15675 &deletelist, "delete ", 1, &cmdlist);
15676 add_com_alias ("d", "delete", class_breakpoint, 1);
15677 add_com_alias ("del", "delete", class_breakpoint, 1);
15678
15679 add_cmd ("breakpoints", class_alias, delete_command, _("\
15680 Delete some breakpoints or auto-display expressions.\n\
15681 Arguments are breakpoint numbers with spaces in between.\n\
15682 To delete all breakpoints, give no argument.\n\
15683 This command may be abbreviated \"delete\"."),
15684 &deletelist);
15685
15686 add_com ("clear", class_breakpoint, clear_command, _("\
15687 Clear breakpoint at specified location.\n\
15688 Argument may be a linespec, explicit, or address location as described below.\n\
15689 \n\
15690 With no argument, clears all breakpoints in the line that the selected frame\n\
15691 is executing in.\n"
15692 "\n" LOCATION_HELP_STRING "\n\
15693 See also the \"delete\" command which clears breakpoints by number."));
15694 add_com_alias ("cl", "clear", class_breakpoint, 1);
15695
15696 c = add_com ("break", class_breakpoint, break_command, _("\
15697 Set breakpoint at specified location.\n"
15698 BREAK_ARGS_HELP ("break")));
15699 set_cmd_completer (c, location_completer);
15700
15701 add_com_alias ("b", "break", class_run, 1);
15702 add_com_alias ("br", "break", class_run, 1);
15703 add_com_alias ("bre", "break", class_run, 1);
15704 add_com_alias ("brea", "break", class_run, 1);
15705
15706 if (dbx_commands)
15707 {
15708 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15709 Break in function/address or break at a line in the current file."),
15710 &stoplist, "stop ", 1, &cmdlist);
15711 add_cmd ("in", class_breakpoint, stopin_command,
15712 _("Break in function or address."), &stoplist);
15713 add_cmd ("at", class_breakpoint, stopat_command,
15714 _("Break at a line in the current file."), &stoplist);
15715 add_com ("status", class_info, info_breakpoints_command, _("\
15716 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15717 The \"Type\" column indicates one of:\n\
15718 \tbreakpoint - normal breakpoint\n\
15719 \twatchpoint - watchpoint\n\
15720 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15721 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15722 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15723 address and file/line number respectively.\n\
15724 \n\
15725 Convenience variable \"$_\" and default examine address for \"x\"\n\
15726 are set to the address of the last breakpoint listed unless the command\n\
15727 is prefixed with \"server \".\n\n\
15728 Convenience variable \"$bpnum\" contains the number of the last\n\
15729 breakpoint set."));
15730 }
15731
15732 add_info ("breakpoints", info_breakpoints_command, _("\
15733 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15734 The \"Type\" column indicates one of:\n\
15735 \tbreakpoint - normal breakpoint\n\
15736 \twatchpoint - watchpoint\n\
15737 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15738 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15739 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15740 address and file/line number respectively.\n\
15741 \n\
15742 Convenience variable \"$_\" and default examine address for \"x\"\n\
15743 are set to the address of the last breakpoint listed unless the command\n\
15744 is prefixed with \"server \".\n\n\
15745 Convenience variable \"$bpnum\" contains the number of the last\n\
15746 breakpoint set."));
15747
15748 add_info_alias ("b", "breakpoints", 1);
15749
15750 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15751 Status of all breakpoints, or breakpoint number NUMBER.\n\
15752 The \"Type\" column indicates one of:\n\
15753 \tbreakpoint - normal breakpoint\n\
15754 \twatchpoint - watchpoint\n\
15755 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15756 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15757 \tuntil - internal breakpoint used by the \"until\" command\n\
15758 \tfinish - internal breakpoint used by the \"finish\" command\n\
15759 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15760 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15761 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15762 address and file/line number respectively.\n\
15763 \n\
15764 Convenience variable \"$_\" and default examine address for \"x\"\n\
15765 are set to the address of the last breakpoint listed unless the command\n\
15766 is prefixed with \"server \".\n\n\
15767 Convenience variable \"$bpnum\" contains the number of the last\n\
15768 breakpoint set."),
15769 &maintenanceinfolist);
15770
15771 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15772 Set catchpoints to catch events."),
15773 &catch_cmdlist, "catch ",
15774 0/*allow-unknown*/, &cmdlist);
15775
15776 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15777 Set temporary catchpoints to catch events."),
15778 &tcatch_cmdlist, "tcatch ",
15779 0/*allow-unknown*/, &cmdlist);
15780
15781 add_catch_command ("fork", _("Catch calls to fork."),
15782 catch_fork_command_1,
15783 NULL,
15784 (void *) (uintptr_t) catch_fork_permanent,
15785 (void *) (uintptr_t) catch_fork_temporary);
15786 add_catch_command ("vfork", _("Catch calls to vfork."),
15787 catch_fork_command_1,
15788 NULL,
15789 (void *) (uintptr_t) catch_vfork_permanent,
15790 (void *) (uintptr_t) catch_vfork_temporary);
15791 add_catch_command ("exec", _("Catch calls to exec."),
15792 catch_exec_command_1,
15793 NULL,
15794 CATCH_PERMANENT,
15795 CATCH_TEMPORARY);
15796 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15797 Usage: catch load [REGEX]\n\
15798 If REGEX is given, only stop for libraries matching the regular expression."),
15799 catch_load_command_1,
15800 NULL,
15801 CATCH_PERMANENT,
15802 CATCH_TEMPORARY);
15803 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15804 Usage: catch unload [REGEX]\n\
15805 If REGEX is given, only stop for libraries matching the regular expression."),
15806 catch_unload_command_1,
15807 NULL,
15808 CATCH_PERMANENT,
15809 CATCH_TEMPORARY);
15810
15811 c = add_com ("watch", class_breakpoint, watch_command, _("\
15812 Set a watchpoint for an expression.\n\
15813 Usage: watch [-l|-location] EXPRESSION\n\
15814 A watchpoint stops execution of your program whenever the value of\n\
15815 an expression changes.\n\
15816 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15817 the memory to which it refers."));
15818 set_cmd_completer (c, expression_completer);
15819
15820 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15821 Set a read watchpoint for an expression.\n\
15822 Usage: rwatch [-l|-location] EXPRESSION\n\
15823 A watchpoint stops execution of your program whenever the value of\n\
15824 an expression is read.\n\
15825 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15826 the memory to which it refers."));
15827 set_cmd_completer (c, expression_completer);
15828
15829 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15830 Set a watchpoint for an expression.\n\
15831 Usage: awatch [-l|-location] EXPRESSION\n\
15832 A watchpoint stops execution of your program whenever the value of\n\
15833 an expression is either read or written.\n\
15834 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15835 the memory to which it refers."));
15836 set_cmd_completer (c, expression_completer);
15837
15838 add_info ("watchpoints", info_watchpoints_command, _("\
15839 Status of specified watchpoints (all watchpoints if no argument)."));
15840
15841 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15842 respond to changes - contrary to the description. */
15843 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15844 &can_use_hw_watchpoints, _("\
15845 Set debugger's willingness to use watchpoint hardware."), _("\
15846 Show debugger's willingness to use watchpoint hardware."), _("\
15847 If zero, gdb will not use hardware for new watchpoints, even if\n\
15848 such is available. (However, any hardware watchpoints that were\n\
15849 created before setting this to nonzero, will continue to use watchpoint\n\
15850 hardware.)"),
15851 NULL,
15852 show_can_use_hw_watchpoints,
15853 &setlist, &showlist);
15854
15855 can_use_hw_watchpoints = 1;
15856
15857 /* Tracepoint manipulation commands. */
15858
15859 c = add_com ("trace", class_breakpoint, trace_command, _("\
15860 Set a tracepoint at specified location.\n\
15861 \n"
15862 BREAK_ARGS_HELP ("trace") "\n\
15863 Do \"help tracepoints\" for info on other tracepoint commands."));
15864 set_cmd_completer (c, location_completer);
15865
15866 add_com_alias ("tp", "trace", class_alias, 0);
15867 add_com_alias ("tr", "trace", class_alias, 1);
15868 add_com_alias ("tra", "trace", class_alias, 1);
15869 add_com_alias ("trac", "trace", class_alias, 1);
15870
15871 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15872 Set a fast tracepoint at specified location.\n\
15873 \n"
15874 BREAK_ARGS_HELP ("ftrace") "\n\
15875 Do \"help tracepoints\" for info on other tracepoint commands."));
15876 set_cmd_completer (c, location_completer);
15877
15878 c = add_com ("strace", class_breakpoint, strace_command, _("\
15879 Set a static tracepoint at location or marker.\n\
15880 \n\
15881 strace [LOCATION] [if CONDITION]\n\
15882 LOCATION may be a linespec, explicit, or address location (described below) \n\
15883 or -m MARKER_ID.\n\n\
15884 If a marker id is specified, probe the marker with that name. With\n\
15885 no LOCATION, uses current execution address of the selected stack frame.\n\
15886 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15887 This collects arbitrary user data passed in the probe point call to the\n\
15888 tracing library. You can inspect it when analyzing the trace buffer,\n\
15889 by printing the $_sdata variable like any other convenience variable.\n\
15890 \n\
15891 CONDITION is a boolean expression.\n\
15892 \n" LOCATION_HELP_STRING "\n\
15893 Multiple tracepoints at one place are permitted, and useful if their\n\
15894 conditions are different.\n\
15895 \n\
15896 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15897 Do \"help tracepoints\" for info on other tracepoint commands."));
15898 set_cmd_completer (c, location_completer);
15899
15900 add_info ("tracepoints", info_tracepoints_command, _("\
15901 Status of specified tracepoints (all tracepoints if no argument).\n\
15902 Convenience variable \"$tpnum\" contains the number of the\n\
15903 last tracepoint set."));
15904
15905 add_info_alias ("tp", "tracepoints", 1);
15906
15907 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15908 Delete specified tracepoints.\n\
15909 Arguments are tracepoint numbers, separated by spaces.\n\
15910 No argument means delete all tracepoints."),
15911 &deletelist);
15912 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15913
15914 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15915 Disable specified tracepoints.\n\
15916 Arguments are tracepoint numbers, separated by spaces.\n\
15917 No argument means disable all tracepoints."),
15918 &disablelist);
15919 deprecate_cmd (c, "disable");
15920
15921 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15922 Enable specified tracepoints.\n\
15923 Arguments are tracepoint numbers, separated by spaces.\n\
15924 No argument means enable all tracepoints."),
15925 &enablelist);
15926 deprecate_cmd (c, "enable");
15927
15928 add_com ("passcount", class_trace, trace_pass_command, _("\
15929 Set the passcount for a tracepoint.\n\
15930 The trace will end when the tracepoint has been passed 'count' times.\n\
15931 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15932 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15933
15934 add_prefix_cmd ("save", class_breakpoint, save_command,
15935 _("Save breakpoint definitions as a script."),
15936 &save_cmdlist, "save ",
15937 0/*allow-unknown*/, &cmdlist);
15938
15939 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15940 Save current breakpoint definitions as a script.\n\
15941 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15942 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15943 session to restore them."),
15944 &save_cmdlist);
15945 set_cmd_completer (c, filename_completer);
15946
15947 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15948 Save current tracepoint definitions as a script.\n\
15949 Use the 'source' command in another debug session to restore them."),
15950 &save_cmdlist);
15951 set_cmd_completer (c, filename_completer);
15952
15953 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15954 deprecate_cmd (c, "save tracepoints");
15955
15956 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
15957 Breakpoint specific settings\n\
15958 Configure various breakpoint-specific variables such as\n\
15959 pending breakpoint behavior"),
15960 &breakpoint_set_cmdlist, "set breakpoint ",
15961 0/*allow-unknown*/, &setlist);
15962 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
15963 Breakpoint specific settings\n\
15964 Configure various breakpoint-specific variables such as\n\
15965 pending breakpoint behavior"),
15966 &breakpoint_show_cmdlist, "show breakpoint ",
15967 0/*allow-unknown*/, &showlist);
15968
15969 add_setshow_auto_boolean_cmd ("pending", no_class,
15970 &pending_break_support, _("\
15971 Set debugger's behavior regarding pending breakpoints."), _("\
15972 Show debugger's behavior regarding pending breakpoints."), _("\
15973 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15974 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15975 an error. If auto, an unrecognized breakpoint location results in a\n\
15976 user-query to see if a pending breakpoint should be created."),
15977 NULL,
15978 show_pending_break_support,
15979 &breakpoint_set_cmdlist,
15980 &breakpoint_show_cmdlist);
15981
15982 pending_break_support = AUTO_BOOLEAN_AUTO;
15983
15984 add_setshow_boolean_cmd ("auto-hw", no_class,
15985 &automatic_hardware_breakpoints, _("\
15986 Set automatic usage of hardware breakpoints."), _("\
15987 Show automatic usage of hardware breakpoints."), _("\
15988 If set, the debugger will automatically use hardware breakpoints for\n\
15989 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15990 a warning will be emitted for such breakpoints."),
15991 NULL,
15992 show_automatic_hardware_breakpoints,
15993 &breakpoint_set_cmdlist,
15994 &breakpoint_show_cmdlist);
15995
15996 add_setshow_boolean_cmd ("always-inserted", class_support,
15997 &always_inserted_mode, _("\
15998 Set mode for inserting breakpoints."), _("\
15999 Show mode for inserting breakpoints."), _("\
16000 When this mode is on, breakpoints are inserted immediately as soon as\n\
16001 they're created, kept inserted even when execution stops, and removed\n\
16002 only when the user deletes them. When this mode is off (the default),\n\
16003 breakpoints are inserted only when execution continues, and removed\n\
16004 when execution stops."),
16005 NULL,
16006 &show_always_inserted_mode,
16007 &breakpoint_set_cmdlist,
16008 &breakpoint_show_cmdlist);
16009
16010 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16011 condition_evaluation_enums,
16012 &condition_evaluation_mode_1, _("\
16013 Set mode of breakpoint condition evaluation."), _("\
16014 Show mode of breakpoint condition evaluation."), _("\
16015 When this is set to \"host\", breakpoint conditions will be\n\
16016 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16017 breakpoint conditions will be downloaded to the target (if the target\n\
16018 supports such feature) and conditions will be evaluated on the target's side.\n\
16019 If this is set to \"auto\" (default), this will be automatically set to\n\
16020 \"target\" if it supports condition evaluation, otherwise it will\n\
16021 be set to \"gdb\""),
16022 &set_condition_evaluation_mode,
16023 &show_condition_evaluation_mode,
16024 &breakpoint_set_cmdlist,
16025 &breakpoint_show_cmdlist);
16026
16027 add_com ("break-range", class_breakpoint, break_range_command, _("\
16028 Set a breakpoint for an address range.\n\
16029 break-range START-LOCATION, END-LOCATION\n\
16030 where START-LOCATION and END-LOCATION can be one of the following:\n\
16031 LINENUM, for that line in the current file,\n\
16032 FILE:LINENUM, for that line in that file,\n\
16033 +OFFSET, for that number of lines after the current line\n\
16034 or the start of the range\n\
16035 FUNCTION, for the first line in that function,\n\
16036 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16037 *ADDRESS, for the instruction at that address.\n\
16038 \n\
16039 The breakpoint will stop execution of the inferior whenever it executes\n\
16040 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16041 range (including START-LOCATION and END-LOCATION)."));
16042
16043 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16044 Set a dynamic printf at specified location.\n\
16045 dprintf location,format string,arg1,arg2,...\n\
16046 location may be a linespec, explicit, or address location.\n"
16047 "\n" LOCATION_HELP_STRING));
16048 set_cmd_completer (c, location_completer);
16049
16050 add_setshow_enum_cmd ("dprintf-style", class_support,
16051 dprintf_style_enums, &dprintf_style, _("\
16052 Set the style of usage for dynamic printf."), _("\
16053 Show the style of usage for dynamic printf."), _("\
16054 This setting chooses how GDB will do a dynamic printf.\n\
16055 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16056 console, as with the \"printf\" command.\n\
16057 If the value is \"call\", the print is done by calling a function in your\n\
16058 program; by default printf(), but you can choose a different function or\n\
16059 output stream by setting dprintf-function and dprintf-channel."),
16060 update_dprintf_commands, NULL,
16061 &setlist, &showlist);
16062
16063 dprintf_function = xstrdup ("printf");
16064 add_setshow_string_cmd ("dprintf-function", class_support,
16065 &dprintf_function, _("\
16066 Set the function to use for dynamic printf"), _("\
16067 Show the function to use for dynamic printf"), NULL,
16068 update_dprintf_commands, NULL,
16069 &setlist, &showlist);
16070
16071 dprintf_channel = xstrdup ("");
16072 add_setshow_string_cmd ("dprintf-channel", class_support,
16073 &dprintf_channel, _("\
16074 Set the channel to use for dynamic printf"), _("\
16075 Show the channel to use for dynamic printf"), NULL,
16076 update_dprintf_commands, NULL,
16077 &setlist, &showlist);
16078
16079 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16080 &disconnected_dprintf, _("\
16081 Set whether dprintf continues after GDB disconnects."), _("\
16082 Show whether dprintf continues after GDB disconnects."), _("\
16083 Use this to let dprintf commands continue to hit and produce output\n\
16084 even if GDB disconnects or detaches from the target."),
16085 NULL,
16086 NULL,
16087 &setlist, &showlist);
16088
16089 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16090 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16091 (target agent only) This is useful for formatted output in user-defined commands."));
16092
16093 automatic_hardware_breakpoints = 1;
16094
16095 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16096 observer_attach_thread_exit (remove_threaded_breakpoints);
16097 }
This page took 0.374119 seconds and 5 git commands to generate.