Allocate breakpoint_objfile_data with new
[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 = 0;
3193
3194 /* SystemTap probe points for longjmp (if any). */
3195 VEC (probe_p) *longjmp_probes = NULL;
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 = 0;
3205
3206 /* SystemTap probe points for unwinding (if any). */
3207 VEC (probe_p) *exception_probes = NULL;
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 = new breakpoint_objfile_data ();
3236 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3237 }
3238 return bp_objfile_data;
3239 }
3240
3241 static void
3242 free_breakpoint_objfile_data (struct objfile *obj, void *data)
3243 {
3244 struct breakpoint_objfile_data *bp_objfile_data
3245 = (struct breakpoint_objfile_data *) data;
3246
3247 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3248 VEC_free (probe_p, bp_objfile_data->exception_probes);
3249
3250 delete bp_objfile_data;
3251 }
3252
3253 static void
3254 create_overlay_event_breakpoint (void)
3255 {
3256 struct objfile *objfile;
3257 const char *const func_name = "_ovly_debug_event";
3258
3259 ALL_OBJFILES (objfile)
3260 {
3261 struct breakpoint *b;
3262 struct breakpoint_objfile_data *bp_objfile_data;
3263 CORE_ADDR addr;
3264 struct explicit_location explicit_loc;
3265
3266 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3267
3268 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3269 continue;
3270
3271 if (bp_objfile_data->overlay_msym.minsym == NULL)
3272 {
3273 struct bound_minimal_symbol m;
3274
3275 m = lookup_minimal_symbol_text (func_name, objfile);
3276 if (m.minsym == NULL)
3277 {
3278 /* Avoid future lookups in this objfile. */
3279 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3280 continue;
3281 }
3282 bp_objfile_data->overlay_msym = m;
3283 }
3284
3285 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3286 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3287 bp_overlay_event,
3288 &internal_breakpoint_ops);
3289 initialize_explicit_location (&explicit_loc);
3290 explicit_loc.function_name = ASTRDUP (func_name);
3291 b->location = new_explicit_location (&explicit_loc);
3292
3293 if (overlay_debugging == ovly_auto)
3294 {
3295 b->enable_state = bp_enabled;
3296 overlay_events_enabled = 1;
3297 }
3298 else
3299 {
3300 b->enable_state = bp_disabled;
3301 overlay_events_enabled = 0;
3302 }
3303 }
3304 }
3305
3306 static void
3307 create_longjmp_master_breakpoint (void)
3308 {
3309 struct program_space *pspace;
3310
3311 scoped_restore_current_program_space restore_pspace;
3312
3313 ALL_PSPACES (pspace)
3314 {
3315 struct objfile *objfile;
3316
3317 set_current_program_space (pspace);
3318
3319 ALL_OBJFILES (objfile)
3320 {
3321 int i;
3322 struct gdbarch *gdbarch;
3323 struct breakpoint_objfile_data *bp_objfile_data;
3324
3325 gdbarch = get_objfile_arch (objfile);
3326
3327 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3328
3329 if (!bp_objfile_data->longjmp_searched)
3330 {
3331 VEC (probe_p) *ret;
3332
3333 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3334 if (ret != NULL)
3335 {
3336 /* We are only interested in checking one element. */
3337 struct probe *p = VEC_index (probe_p, ret, 0);
3338
3339 if (!can_evaluate_probe_arguments (p))
3340 {
3341 /* We cannot use the probe interface here, because it does
3342 not know how to evaluate arguments. */
3343 VEC_free (probe_p, ret);
3344 ret = NULL;
3345 }
3346 }
3347 bp_objfile_data->longjmp_probes = ret;
3348 bp_objfile_data->longjmp_searched = 1;
3349 }
3350
3351 if (bp_objfile_data->longjmp_probes != NULL)
3352 {
3353 int i;
3354 struct probe *probe;
3355 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3356
3357 for (i = 0;
3358 VEC_iterate (probe_p,
3359 bp_objfile_data->longjmp_probes,
3360 i, probe);
3361 ++i)
3362 {
3363 struct breakpoint *b;
3364
3365 b = create_internal_breakpoint (gdbarch,
3366 get_probe_address (probe,
3367 objfile),
3368 bp_longjmp_master,
3369 &internal_breakpoint_ops);
3370 b->location = new_probe_location ("-probe-stap libc:longjmp");
3371 b->enable_state = bp_disabled;
3372 }
3373
3374 continue;
3375 }
3376
3377 if (!gdbarch_get_longjmp_target_p (gdbarch))
3378 continue;
3379
3380 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3381 {
3382 struct breakpoint *b;
3383 const char *func_name;
3384 CORE_ADDR addr;
3385 struct explicit_location explicit_loc;
3386
3387 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3388 continue;
3389
3390 func_name = longjmp_names[i];
3391 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3392 {
3393 struct bound_minimal_symbol m;
3394
3395 m = lookup_minimal_symbol_text (func_name, objfile);
3396 if (m.minsym == NULL)
3397 {
3398 /* Prevent future lookups in this objfile. */
3399 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3400 continue;
3401 }
3402 bp_objfile_data->longjmp_msym[i] = m;
3403 }
3404
3405 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3406 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3407 &internal_breakpoint_ops);
3408 initialize_explicit_location (&explicit_loc);
3409 explicit_loc.function_name = ASTRDUP (func_name);
3410 b->location = new_explicit_location (&explicit_loc);
3411 b->enable_state = bp_disabled;
3412 }
3413 }
3414 }
3415 }
3416
3417 /* Create a master std::terminate breakpoint. */
3418 static void
3419 create_std_terminate_master_breakpoint (void)
3420 {
3421 struct program_space *pspace;
3422 const char *const func_name = "std::terminate()";
3423
3424 scoped_restore_current_program_space restore_pspace;
3425
3426 ALL_PSPACES (pspace)
3427 {
3428 struct objfile *objfile;
3429 CORE_ADDR addr;
3430
3431 set_current_program_space (pspace);
3432
3433 ALL_OBJFILES (objfile)
3434 {
3435 struct breakpoint *b;
3436 struct breakpoint_objfile_data *bp_objfile_data;
3437 struct explicit_location explicit_loc;
3438
3439 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3440
3441 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3442 continue;
3443
3444 if (bp_objfile_data->terminate_msym.minsym == NULL)
3445 {
3446 struct bound_minimal_symbol m;
3447
3448 m = lookup_minimal_symbol (func_name, NULL, objfile);
3449 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3450 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3451 {
3452 /* Prevent future lookups in this objfile. */
3453 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3454 continue;
3455 }
3456 bp_objfile_data->terminate_msym = m;
3457 }
3458
3459 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3460 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3461 bp_std_terminate_master,
3462 &internal_breakpoint_ops);
3463 initialize_explicit_location (&explicit_loc);
3464 explicit_loc.function_name = ASTRDUP (func_name);
3465 b->location = new_explicit_location (&explicit_loc);
3466 b->enable_state = bp_disabled;
3467 }
3468 }
3469 }
3470
3471 /* Install a master breakpoint on the unwinder's debug hook. */
3472
3473 static void
3474 create_exception_master_breakpoint (void)
3475 {
3476 struct objfile *objfile;
3477 const char *const func_name = "_Unwind_DebugHook";
3478
3479 ALL_OBJFILES (objfile)
3480 {
3481 struct breakpoint *b;
3482 struct gdbarch *gdbarch;
3483 struct breakpoint_objfile_data *bp_objfile_data;
3484 CORE_ADDR addr;
3485 struct explicit_location explicit_loc;
3486
3487 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3488
3489 /* We prefer the SystemTap probe point if it exists. */
3490 if (!bp_objfile_data->exception_searched)
3491 {
3492 VEC (probe_p) *ret;
3493
3494 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3495
3496 if (ret != NULL)
3497 {
3498 /* We are only interested in checking one element. */
3499 struct probe *p = VEC_index (probe_p, ret, 0);
3500
3501 if (!can_evaluate_probe_arguments (p))
3502 {
3503 /* We cannot use the probe interface here, because it does
3504 not know how to evaluate arguments. */
3505 VEC_free (probe_p, ret);
3506 ret = NULL;
3507 }
3508 }
3509 bp_objfile_data->exception_probes = ret;
3510 bp_objfile_data->exception_searched = 1;
3511 }
3512
3513 if (bp_objfile_data->exception_probes != NULL)
3514 {
3515 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3516 int i;
3517 struct probe *probe;
3518
3519 for (i = 0;
3520 VEC_iterate (probe_p,
3521 bp_objfile_data->exception_probes,
3522 i, probe);
3523 ++i)
3524 {
3525 struct breakpoint *b;
3526
3527 b = create_internal_breakpoint (gdbarch,
3528 get_probe_address (probe,
3529 objfile),
3530 bp_exception_master,
3531 &internal_breakpoint_ops);
3532 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3533 b->enable_state = bp_disabled;
3534 }
3535
3536 continue;
3537 }
3538
3539 /* Otherwise, try the hook function. */
3540
3541 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3542 continue;
3543
3544 gdbarch = get_objfile_arch (objfile);
3545
3546 if (bp_objfile_data->exception_msym.minsym == NULL)
3547 {
3548 struct bound_minimal_symbol debug_hook;
3549
3550 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3551 if (debug_hook.minsym == NULL)
3552 {
3553 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3554 continue;
3555 }
3556
3557 bp_objfile_data->exception_msym = debug_hook;
3558 }
3559
3560 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3561 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3562 &current_target);
3563 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3564 &internal_breakpoint_ops);
3565 initialize_explicit_location (&explicit_loc);
3566 explicit_loc.function_name = ASTRDUP (func_name);
3567 b->location = new_explicit_location (&explicit_loc);
3568 b->enable_state = bp_disabled;
3569 }
3570 }
3571
3572 /* Does B have a location spec? */
3573
3574 static int
3575 breakpoint_event_location_empty_p (const struct breakpoint *b)
3576 {
3577 return b->location != NULL && event_location_empty_p (b->location.get ());
3578 }
3579
3580 void
3581 update_breakpoints_after_exec (void)
3582 {
3583 struct breakpoint *b, *b_tmp;
3584 struct bp_location *bploc, **bplocp_tmp;
3585
3586 /* We're about to delete breakpoints from GDB's lists. If the
3587 INSERTED flag is true, GDB will try to lift the breakpoints by
3588 writing the breakpoints' "shadow contents" back into memory. The
3589 "shadow contents" are NOT valid after an exec, so GDB should not
3590 do that. Instead, the target is responsible from marking
3591 breakpoints out as soon as it detects an exec. We don't do that
3592 here instead, because there may be other attempts to delete
3593 breakpoints after detecting an exec and before reaching here. */
3594 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3595 if (bploc->pspace == current_program_space)
3596 gdb_assert (!bploc->inserted);
3597
3598 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3599 {
3600 if (b->pspace != current_program_space)
3601 continue;
3602
3603 /* Solib breakpoints must be explicitly reset after an exec(). */
3604 if (b->type == bp_shlib_event)
3605 {
3606 delete_breakpoint (b);
3607 continue;
3608 }
3609
3610 /* JIT breakpoints must be explicitly reset after an exec(). */
3611 if (b->type == bp_jit_event)
3612 {
3613 delete_breakpoint (b);
3614 continue;
3615 }
3616
3617 /* Thread event breakpoints must be set anew after an exec(),
3618 as must overlay event and longjmp master breakpoints. */
3619 if (b->type == bp_thread_event || b->type == bp_overlay_event
3620 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3621 || b->type == bp_exception_master)
3622 {
3623 delete_breakpoint (b);
3624 continue;
3625 }
3626
3627 /* Step-resume breakpoints are meaningless after an exec(). */
3628 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3629 {
3630 delete_breakpoint (b);
3631 continue;
3632 }
3633
3634 /* Just like single-step breakpoints. */
3635 if (b->type == bp_single_step)
3636 {
3637 delete_breakpoint (b);
3638 continue;
3639 }
3640
3641 /* Longjmp and longjmp-resume breakpoints are also meaningless
3642 after an exec. */
3643 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3644 || b->type == bp_longjmp_call_dummy
3645 || b->type == bp_exception || b->type == bp_exception_resume)
3646 {
3647 delete_breakpoint (b);
3648 continue;
3649 }
3650
3651 if (b->type == bp_catchpoint)
3652 {
3653 /* For now, none of the bp_catchpoint breakpoints need to
3654 do anything at this point. In the future, if some of
3655 the catchpoints need to something, we will need to add
3656 a new method, and call this method from here. */
3657 continue;
3658 }
3659
3660 /* bp_finish is a special case. The only way we ought to be able
3661 to see one of these when an exec() has happened, is if the user
3662 caught a vfork, and then said "finish". Ordinarily a finish just
3663 carries them to the call-site of the current callee, by setting
3664 a temporary bp there and resuming. But in this case, the finish
3665 will carry them entirely through the vfork & exec.
3666
3667 We don't want to allow a bp_finish to remain inserted now. But
3668 we can't safely delete it, 'cause finish_command has a handle to
3669 the bp on a bpstat, and will later want to delete it. There's a
3670 chance (and I've seen it happen) that if we delete the bp_finish
3671 here, that its storage will get reused by the time finish_command
3672 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3673 We really must allow finish_command to delete a bp_finish.
3674
3675 In the absence of a general solution for the "how do we know
3676 it's safe to delete something others may have handles to?"
3677 problem, what we'll do here is just uninsert the bp_finish, and
3678 let finish_command delete it.
3679
3680 (We know the bp_finish is "doomed" in the sense that it's
3681 momentary, and will be deleted as soon as finish_command sees
3682 the inferior stopped. So it doesn't matter that the bp's
3683 address is probably bogus in the new a.out, unlike e.g., the
3684 solib breakpoints.) */
3685
3686 if (b->type == bp_finish)
3687 {
3688 continue;
3689 }
3690
3691 /* Without a symbolic address, we have little hope of the
3692 pre-exec() address meaning the same thing in the post-exec()
3693 a.out. */
3694 if (breakpoint_event_location_empty_p (b))
3695 {
3696 delete_breakpoint (b);
3697 continue;
3698 }
3699 }
3700 }
3701
3702 int
3703 detach_breakpoints (ptid_t ptid)
3704 {
3705 struct bp_location *bl, **blp_tmp;
3706 int val = 0;
3707 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3708 struct inferior *inf = current_inferior ();
3709
3710 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3711 error (_("Cannot detach breakpoints of inferior_ptid"));
3712
3713 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3714 inferior_ptid = ptid;
3715 ALL_BP_LOCATIONS (bl, blp_tmp)
3716 {
3717 if (bl->pspace != inf->pspace)
3718 continue;
3719
3720 /* This function must physically remove breakpoints locations
3721 from the specified ptid, without modifying the breakpoint
3722 package's state. Locations of type bp_loc_other are only
3723 maintained at GDB side. So, there is no need to remove
3724 these bp_loc_other locations. Moreover, removing these
3725 would modify the breakpoint package's state. */
3726 if (bl->loc_type == bp_loc_other)
3727 continue;
3728
3729 if (bl->inserted)
3730 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3731 }
3732
3733 return val;
3734 }
3735
3736 /* Remove the breakpoint location BL from the current address space.
3737 Note that this is used to detach breakpoints from a child fork.
3738 When we get here, the child isn't in the inferior list, and neither
3739 do we have objects to represent its address space --- we should
3740 *not* look at bl->pspace->aspace here. */
3741
3742 static int
3743 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3744 {
3745 int val;
3746
3747 /* BL is never in moribund_locations by our callers. */
3748 gdb_assert (bl->owner != NULL);
3749
3750 /* The type of none suggests that owner is actually deleted.
3751 This should not ever happen. */
3752 gdb_assert (bl->owner->type != bp_none);
3753
3754 if (bl->loc_type == bp_loc_software_breakpoint
3755 || bl->loc_type == bp_loc_hardware_breakpoint)
3756 {
3757 /* "Normal" instruction breakpoint: either the standard
3758 trap-instruction bp (bp_breakpoint), or a
3759 bp_hardware_breakpoint. */
3760
3761 /* First check to see if we have to handle an overlay. */
3762 if (overlay_debugging == ovly_off
3763 || bl->section == NULL
3764 || !(section_is_overlay (bl->section)))
3765 {
3766 /* No overlay handling: just remove the breakpoint. */
3767
3768 /* If we're trying to uninsert a memory breakpoint that we
3769 know is set in a dynamic object that is marked
3770 shlib_disabled, then either the dynamic object was
3771 removed with "remove-symbol-file" or with
3772 "nosharedlibrary". In the former case, we don't know
3773 whether another dynamic object might have loaded over the
3774 breakpoint's address -- the user might well let us know
3775 about it next with add-symbol-file (the whole point of
3776 add-symbol-file is letting the user manually maintain a
3777 list of dynamically loaded objects). If we have the
3778 breakpoint's shadow memory, that is, this is a software
3779 breakpoint managed by GDB, check whether the breakpoint
3780 is still inserted in memory, to avoid overwriting wrong
3781 code with stale saved shadow contents. Note that HW
3782 breakpoints don't have shadow memory, as they're
3783 implemented using a mechanism that is not dependent on
3784 being able to modify the target's memory, and as such
3785 they should always be removed. */
3786 if (bl->shlib_disabled
3787 && bl->target_info.shadow_len != 0
3788 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3789 val = 0;
3790 else
3791 val = bl->owner->ops->remove_location (bl, reason);
3792 }
3793 else
3794 {
3795 /* This breakpoint is in an overlay section.
3796 Did we set a breakpoint at the LMA? */
3797 if (!overlay_events_enabled)
3798 {
3799 /* Yes -- overlay event support is not active, so we
3800 should have set a breakpoint at the LMA. Remove it.
3801 */
3802 /* Ignore any failures: if the LMA is in ROM, we will
3803 have already warned when we failed to insert it. */
3804 if (bl->loc_type == bp_loc_hardware_breakpoint)
3805 target_remove_hw_breakpoint (bl->gdbarch,
3806 &bl->overlay_target_info);
3807 else
3808 target_remove_breakpoint (bl->gdbarch,
3809 &bl->overlay_target_info,
3810 reason);
3811 }
3812 /* Did we set a breakpoint at the VMA?
3813 If so, we will have marked the breakpoint 'inserted'. */
3814 if (bl->inserted)
3815 {
3816 /* Yes -- remove it. Previously we did not bother to
3817 remove the breakpoint if the section had been
3818 unmapped, but let's not rely on that being safe. We
3819 don't know what the overlay manager might do. */
3820
3821 /* However, we should remove *software* breakpoints only
3822 if the section is still mapped, or else we overwrite
3823 wrong code with the saved shadow contents. */
3824 if (bl->loc_type == bp_loc_hardware_breakpoint
3825 || section_is_mapped (bl->section))
3826 val = bl->owner->ops->remove_location (bl, reason);
3827 else
3828 val = 0;
3829 }
3830 else
3831 {
3832 /* No -- not inserted, so no need to remove. No error. */
3833 val = 0;
3834 }
3835 }
3836
3837 /* In some cases, we might not be able to remove a breakpoint in
3838 a shared library that has already been removed, but we have
3839 not yet processed the shlib unload event. Similarly for an
3840 unloaded add-symbol-file object - the user might not yet have
3841 had the chance to remove-symbol-file it. shlib_disabled will
3842 be set if the library/object has already been removed, but
3843 the breakpoint hasn't been uninserted yet, e.g., after
3844 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3845 always-inserted mode. */
3846 if (val
3847 && (bl->loc_type == bp_loc_software_breakpoint
3848 && (bl->shlib_disabled
3849 || solib_name_from_address (bl->pspace, bl->address)
3850 || shared_objfile_contains_address_p (bl->pspace,
3851 bl->address))))
3852 val = 0;
3853
3854 if (val)
3855 return val;
3856 bl->inserted = (reason == DETACH_BREAKPOINT);
3857 }
3858 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3859 {
3860 gdb_assert (bl->owner->ops != NULL
3861 && bl->owner->ops->remove_location != NULL);
3862
3863 bl->inserted = (reason == DETACH_BREAKPOINT);
3864 bl->owner->ops->remove_location (bl, reason);
3865
3866 /* Failure to remove any of the hardware watchpoints comes here. */
3867 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3868 warning (_("Could not remove hardware watchpoint %d."),
3869 bl->owner->number);
3870 }
3871 else if (bl->owner->type == bp_catchpoint
3872 && breakpoint_enabled (bl->owner)
3873 && !bl->duplicate)
3874 {
3875 gdb_assert (bl->owner->ops != NULL
3876 && bl->owner->ops->remove_location != NULL);
3877
3878 val = bl->owner->ops->remove_location (bl, reason);
3879 if (val)
3880 return val;
3881
3882 bl->inserted = (reason == DETACH_BREAKPOINT);
3883 }
3884
3885 return 0;
3886 }
3887
3888 static int
3889 remove_breakpoint (struct bp_location *bl)
3890 {
3891 /* BL is never in moribund_locations by our callers. */
3892 gdb_assert (bl->owner != NULL);
3893
3894 /* The type of none suggests that owner is actually deleted.
3895 This should not ever happen. */
3896 gdb_assert (bl->owner->type != bp_none);
3897
3898 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3899
3900 switch_to_program_space_and_thread (bl->pspace);
3901
3902 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3903 }
3904
3905 /* Clear the "inserted" flag in all breakpoints. */
3906
3907 void
3908 mark_breakpoints_out (void)
3909 {
3910 struct bp_location *bl, **blp_tmp;
3911
3912 ALL_BP_LOCATIONS (bl, blp_tmp)
3913 if (bl->pspace == current_program_space)
3914 bl->inserted = 0;
3915 }
3916
3917 /* Clear the "inserted" flag in all breakpoints and delete any
3918 breakpoints which should go away between runs of the program.
3919
3920 Plus other such housekeeping that has to be done for breakpoints
3921 between runs.
3922
3923 Note: this function gets called at the end of a run (by
3924 generic_mourn_inferior) and when a run begins (by
3925 init_wait_for_inferior). */
3926
3927
3928
3929 void
3930 breakpoint_init_inferior (enum inf_context context)
3931 {
3932 struct breakpoint *b, *b_tmp;
3933 struct bp_location *bl;
3934 int ix;
3935 struct program_space *pspace = current_program_space;
3936
3937 /* If breakpoint locations are shared across processes, then there's
3938 nothing to do. */
3939 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3940 return;
3941
3942 mark_breakpoints_out ();
3943
3944 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3945 {
3946 if (b->loc && b->loc->pspace != pspace)
3947 continue;
3948
3949 switch (b->type)
3950 {
3951 case bp_call_dummy:
3952 case bp_longjmp_call_dummy:
3953
3954 /* If the call dummy breakpoint is at the entry point it will
3955 cause problems when the inferior is rerun, so we better get
3956 rid of it. */
3957
3958 case bp_watchpoint_scope:
3959
3960 /* Also get rid of scope breakpoints. */
3961
3962 case bp_shlib_event:
3963
3964 /* Also remove solib event breakpoints. Their addresses may
3965 have changed since the last time we ran the program.
3966 Actually we may now be debugging against different target;
3967 and so the solib backend that installed this breakpoint may
3968 not be used in by the target. E.g.,
3969
3970 (gdb) file prog-linux
3971 (gdb) run # native linux target
3972 ...
3973 (gdb) kill
3974 (gdb) file prog-win.exe
3975 (gdb) tar rem :9999 # remote Windows gdbserver.
3976 */
3977
3978 case bp_step_resume:
3979
3980 /* Also remove step-resume breakpoints. */
3981
3982 case bp_single_step:
3983
3984 /* Also remove single-step breakpoints. */
3985
3986 delete_breakpoint (b);
3987 break;
3988
3989 case bp_watchpoint:
3990 case bp_hardware_watchpoint:
3991 case bp_read_watchpoint:
3992 case bp_access_watchpoint:
3993 {
3994 struct watchpoint *w = (struct watchpoint *) b;
3995
3996 /* Likewise for watchpoints on local expressions. */
3997 if (w->exp_valid_block != NULL)
3998 delete_breakpoint (b);
3999 else
4000 {
4001 /* Get rid of existing locations, which are no longer
4002 valid. New ones will be created in
4003 update_watchpoint, when the inferior is restarted.
4004 The next update_global_location_list call will
4005 garbage collect them. */
4006 b->loc = NULL;
4007
4008 if (context == inf_starting)
4009 {
4010 /* Reset val field to force reread of starting value in
4011 insert_breakpoints. */
4012 if (w->val)
4013 value_free (w->val);
4014 w->val = NULL;
4015 w->val_valid = 0;
4016 }
4017 }
4018 }
4019 break;
4020 default:
4021 break;
4022 }
4023 }
4024
4025 /* Get rid of the moribund locations. */
4026 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4027 decref_bp_location (&bl);
4028 VEC_free (bp_location_p, moribund_locations);
4029 }
4030
4031 /* These functions concern about actual breakpoints inserted in the
4032 target --- to e.g. check if we need to do decr_pc adjustment or if
4033 we need to hop over the bkpt --- so we check for address space
4034 match, not program space. */
4035
4036 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4037 exists at PC. It returns ordinary_breakpoint_here if it's an
4038 ordinary breakpoint, or permanent_breakpoint_here if it's a
4039 permanent breakpoint.
4040 - When continuing from a location with an ordinary breakpoint, we
4041 actually single step once before calling insert_breakpoints.
4042 - When continuing from a location with a permanent breakpoint, we
4043 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4044 the target, to advance the PC past the breakpoint. */
4045
4046 enum breakpoint_here
4047 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4048 {
4049 struct bp_location *bl, **blp_tmp;
4050 int any_breakpoint_here = 0;
4051
4052 ALL_BP_LOCATIONS (bl, blp_tmp)
4053 {
4054 if (bl->loc_type != bp_loc_software_breakpoint
4055 && bl->loc_type != bp_loc_hardware_breakpoint)
4056 continue;
4057
4058 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4059 if ((breakpoint_enabled (bl->owner)
4060 || bl->permanent)
4061 && breakpoint_location_address_match (bl, aspace, pc))
4062 {
4063 if (overlay_debugging
4064 && section_is_overlay (bl->section)
4065 && !section_is_mapped (bl->section))
4066 continue; /* unmapped overlay -- can't be a match */
4067 else if (bl->permanent)
4068 return permanent_breakpoint_here;
4069 else
4070 any_breakpoint_here = 1;
4071 }
4072 }
4073
4074 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4075 }
4076
4077 /* See breakpoint.h. */
4078
4079 int
4080 breakpoint_in_range_p (const address_space *aspace,
4081 CORE_ADDR addr, ULONGEST len)
4082 {
4083 struct bp_location *bl, **blp_tmp;
4084
4085 ALL_BP_LOCATIONS (bl, blp_tmp)
4086 {
4087 if (bl->loc_type != bp_loc_software_breakpoint
4088 && bl->loc_type != bp_loc_hardware_breakpoint)
4089 continue;
4090
4091 if ((breakpoint_enabled (bl->owner)
4092 || bl->permanent)
4093 && breakpoint_location_address_range_overlap (bl, aspace,
4094 addr, len))
4095 {
4096 if (overlay_debugging
4097 && section_is_overlay (bl->section)
4098 && !section_is_mapped (bl->section))
4099 {
4100 /* Unmapped overlay -- can't be a match. */
4101 continue;
4102 }
4103
4104 return 1;
4105 }
4106 }
4107
4108 return 0;
4109 }
4110
4111 /* Return true if there's a moribund breakpoint at PC. */
4112
4113 int
4114 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4115 {
4116 struct bp_location *loc;
4117 int ix;
4118
4119 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4120 if (breakpoint_location_address_match (loc, aspace, pc))
4121 return 1;
4122
4123 return 0;
4124 }
4125
4126 /* Returns non-zero iff BL is inserted at PC, in address space
4127 ASPACE. */
4128
4129 static int
4130 bp_location_inserted_here_p (struct bp_location *bl,
4131 const address_space *aspace, CORE_ADDR pc)
4132 {
4133 if (bl->inserted
4134 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4135 aspace, pc))
4136 {
4137 if (overlay_debugging
4138 && section_is_overlay (bl->section)
4139 && !section_is_mapped (bl->section))
4140 return 0; /* unmapped overlay -- can't be a match */
4141 else
4142 return 1;
4143 }
4144 return 0;
4145 }
4146
4147 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4148
4149 int
4150 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4151 {
4152 struct bp_location **blp, **blp_tmp = NULL;
4153
4154 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4155 {
4156 struct bp_location *bl = *blp;
4157
4158 if (bl->loc_type != bp_loc_software_breakpoint
4159 && bl->loc_type != bp_loc_hardware_breakpoint)
4160 continue;
4161
4162 if (bp_location_inserted_here_p (bl, aspace, pc))
4163 return 1;
4164 }
4165 return 0;
4166 }
4167
4168 /* This function returns non-zero iff there is a software breakpoint
4169 inserted at PC. */
4170
4171 int
4172 software_breakpoint_inserted_here_p (const address_space *aspace,
4173 CORE_ADDR pc)
4174 {
4175 struct bp_location **blp, **blp_tmp = NULL;
4176
4177 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4178 {
4179 struct bp_location *bl = *blp;
4180
4181 if (bl->loc_type != bp_loc_software_breakpoint)
4182 continue;
4183
4184 if (bp_location_inserted_here_p (bl, aspace, pc))
4185 return 1;
4186 }
4187
4188 return 0;
4189 }
4190
4191 /* See breakpoint.h. */
4192
4193 int
4194 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4195 CORE_ADDR pc)
4196 {
4197 struct bp_location **blp, **blp_tmp = NULL;
4198
4199 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4200 {
4201 struct bp_location *bl = *blp;
4202
4203 if (bl->loc_type != bp_loc_hardware_breakpoint)
4204 continue;
4205
4206 if (bp_location_inserted_here_p (bl, aspace, pc))
4207 return 1;
4208 }
4209
4210 return 0;
4211 }
4212
4213 int
4214 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4215 CORE_ADDR addr, ULONGEST len)
4216 {
4217 struct breakpoint *bpt;
4218
4219 ALL_BREAKPOINTS (bpt)
4220 {
4221 struct bp_location *loc;
4222
4223 if (bpt->type != bp_hardware_watchpoint
4224 && bpt->type != bp_access_watchpoint)
4225 continue;
4226
4227 if (!breakpoint_enabled (bpt))
4228 continue;
4229
4230 for (loc = bpt->loc; loc; loc = loc->next)
4231 if (loc->pspace->aspace == aspace && loc->inserted)
4232 {
4233 CORE_ADDR l, h;
4234
4235 /* Check for intersection. */
4236 l = std::max<CORE_ADDR> (loc->address, addr);
4237 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4238 if (l < h)
4239 return 1;
4240 }
4241 }
4242 return 0;
4243 }
4244 \f
4245
4246 /* bpstat stuff. External routines' interfaces are documented
4247 in breakpoint.h. */
4248
4249 int
4250 is_catchpoint (struct breakpoint *ep)
4251 {
4252 return (ep->type == bp_catchpoint);
4253 }
4254
4255 /* Frees any storage that is part of a bpstat. Does not walk the
4256 'next' chain. */
4257
4258 bpstats::~bpstats ()
4259 {
4260 if (old_val != NULL)
4261 value_free (old_val);
4262 if (bp_location_at != NULL)
4263 decref_bp_location (&bp_location_at);
4264 }
4265
4266 /* Clear a bpstat so that it says we are not at any breakpoint.
4267 Also free any storage that is part of a bpstat. */
4268
4269 void
4270 bpstat_clear (bpstat *bsp)
4271 {
4272 bpstat p;
4273 bpstat q;
4274
4275 if (bsp == 0)
4276 return;
4277 p = *bsp;
4278 while (p != NULL)
4279 {
4280 q = p->next;
4281 delete p;
4282 p = q;
4283 }
4284 *bsp = NULL;
4285 }
4286
4287 bpstats::bpstats (const bpstats &other)
4288 : next (NULL),
4289 bp_location_at (other.bp_location_at),
4290 breakpoint_at (other.breakpoint_at),
4291 commands (other.commands),
4292 old_val (other.old_val),
4293 print (other.print),
4294 stop (other.stop),
4295 print_it (other.print_it)
4296 {
4297 if (old_val != NULL)
4298 {
4299 old_val = value_copy (old_val);
4300 release_value (old_val);
4301 }
4302 incref_bp_location (bp_location_at);
4303 }
4304
4305 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4306 is part of the bpstat is copied as well. */
4307
4308 bpstat
4309 bpstat_copy (bpstat bs)
4310 {
4311 bpstat p = NULL;
4312 bpstat tmp;
4313 bpstat retval = NULL;
4314
4315 if (bs == NULL)
4316 return bs;
4317
4318 for (; bs != NULL; bs = bs->next)
4319 {
4320 tmp = new bpstats (*bs);
4321
4322 if (p == NULL)
4323 /* This is the first thing in the chain. */
4324 retval = tmp;
4325 else
4326 p->next = tmp;
4327 p = tmp;
4328 }
4329 p->next = NULL;
4330 return retval;
4331 }
4332
4333 /* Find the bpstat associated with this breakpoint. */
4334
4335 bpstat
4336 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4337 {
4338 if (bsp == NULL)
4339 return NULL;
4340
4341 for (; bsp != NULL; bsp = bsp->next)
4342 {
4343 if (bsp->breakpoint_at == breakpoint)
4344 return bsp;
4345 }
4346 return NULL;
4347 }
4348
4349 /* See breakpoint.h. */
4350
4351 int
4352 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4353 {
4354 for (; bsp != NULL; bsp = bsp->next)
4355 {
4356 if (bsp->breakpoint_at == NULL)
4357 {
4358 /* A moribund location can never explain a signal other than
4359 GDB_SIGNAL_TRAP. */
4360 if (sig == GDB_SIGNAL_TRAP)
4361 return 1;
4362 }
4363 else
4364 {
4365 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4366 sig))
4367 return 1;
4368 }
4369 }
4370
4371 return 0;
4372 }
4373
4374 /* Put in *NUM the breakpoint number of the first breakpoint we are
4375 stopped at. *BSP upon return is a bpstat which points to the
4376 remaining breakpoints stopped at (but which is not guaranteed to be
4377 good for anything but further calls to bpstat_num).
4378
4379 Return 0 if passed a bpstat which does not indicate any breakpoints.
4380 Return -1 if stopped at a breakpoint that has been deleted since
4381 we set it.
4382 Return 1 otherwise. */
4383
4384 int
4385 bpstat_num (bpstat *bsp, int *num)
4386 {
4387 struct breakpoint *b;
4388
4389 if ((*bsp) == NULL)
4390 return 0; /* No more breakpoint values */
4391
4392 /* We assume we'll never have several bpstats that correspond to a
4393 single breakpoint -- otherwise, this function might return the
4394 same number more than once and this will look ugly. */
4395 b = (*bsp)->breakpoint_at;
4396 *bsp = (*bsp)->next;
4397 if (b == NULL)
4398 return -1; /* breakpoint that's been deleted since */
4399
4400 *num = b->number; /* We have its number */
4401 return 1;
4402 }
4403
4404 /* See breakpoint.h. */
4405
4406 void
4407 bpstat_clear_actions (void)
4408 {
4409 struct thread_info *tp;
4410 bpstat bs;
4411
4412 if (ptid_equal (inferior_ptid, null_ptid))
4413 return;
4414
4415 tp = find_thread_ptid (inferior_ptid);
4416 if (tp == NULL)
4417 return;
4418
4419 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4420 {
4421 bs->commands = NULL;
4422
4423 if (bs->old_val != NULL)
4424 {
4425 value_free (bs->old_val);
4426 bs->old_val = NULL;
4427 }
4428 }
4429 }
4430
4431 /* Called when a command is about to proceed the inferior. */
4432
4433 static void
4434 breakpoint_about_to_proceed (void)
4435 {
4436 if (!ptid_equal (inferior_ptid, null_ptid))
4437 {
4438 struct thread_info *tp = inferior_thread ();
4439
4440 /* Allow inferior function calls in breakpoint commands to not
4441 interrupt the command list. When the call finishes
4442 successfully, the inferior will be standing at the same
4443 breakpoint as if nothing happened. */
4444 if (tp->control.in_infcall)
4445 return;
4446 }
4447
4448 breakpoint_proceeded = 1;
4449 }
4450
4451 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4452 or its equivalent. */
4453
4454 static int
4455 command_line_is_silent (struct command_line *cmd)
4456 {
4457 return cmd && (strcmp ("silent", cmd->line) == 0);
4458 }
4459
4460 /* Execute all the commands associated with all the breakpoints at
4461 this location. Any of these commands could cause the process to
4462 proceed beyond this point, etc. We look out for such changes by
4463 checking the global "breakpoint_proceeded" after each command.
4464
4465 Returns true if a breakpoint command resumed the inferior. In that
4466 case, it is the caller's responsibility to recall it again with the
4467 bpstat of the current thread. */
4468
4469 static int
4470 bpstat_do_actions_1 (bpstat *bsp)
4471 {
4472 bpstat bs;
4473 int again = 0;
4474
4475 /* Avoid endless recursion if a `source' command is contained
4476 in bs->commands. */
4477 if (executing_breakpoint_commands)
4478 return 0;
4479
4480 scoped_restore save_executing
4481 = make_scoped_restore (&executing_breakpoint_commands, 1);
4482
4483 scoped_restore preventer = prevent_dont_repeat ();
4484
4485 /* This pointer will iterate over the list of bpstat's. */
4486 bs = *bsp;
4487
4488 breakpoint_proceeded = 0;
4489 for (; bs != NULL; bs = bs->next)
4490 {
4491 struct command_line *cmd = NULL;
4492
4493 /* Take ownership of the BSP's command tree, if it has one.
4494
4495 The command tree could legitimately contain commands like
4496 'step' and 'next', which call clear_proceed_status, which
4497 frees stop_bpstat's command tree. To make sure this doesn't
4498 free the tree we're executing out from under us, we need to
4499 take ownership of the tree ourselves. Since a given bpstat's
4500 commands are only executed once, we don't need to copy it; we
4501 can clear the pointer in the bpstat, and make sure we free
4502 the tree when we're done. */
4503 counted_command_line ccmd = bs->commands;
4504 bs->commands = NULL;
4505 if (ccmd != NULL)
4506 cmd = ccmd.get ();
4507 if (command_line_is_silent (cmd))
4508 {
4509 /* The action has been already done by bpstat_stop_status. */
4510 cmd = cmd->next;
4511 }
4512
4513 while (cmd != NULL)
4514 {
4515 execute_control_command (cmd);
4516
4517 if (breakpoint_proceeded)
4518 break;
4519 else
4520 cmd = cmd->next;
4521 }
4522
4523 if (breakpoint_proceeded)
4524 {
4525 if (current_ui->async)
4526 /* If we are in async mode, then the target might be still
4527 running, not stopped at any breakpoint, so nothing for
4528 us to do here -- just return to the event loop. */
4529 ;
4530 else
4531 /* In sync mode, when execute_control_command returns
4532 we're already standing on the next breakpoint.
4533 Breakpoint commands for that stop were not run, since
4534 execute_command does not run breakpoint commands --
4535 only command_line_handler does, but that one is not
4536 involved in execution of breakpoint commands. So, we
4537 can now execute breakpoint commands. It should be
4538 noted that making execute_command do bpstat actions is
4539 not an option -- in this case we'll have recursive
4540 invocation of bpstat for each breakpoint with a
4541 command, and can easily blow up GDB stack. Instead, we
4542 return true, which will trigger the caller to recall us
4543 with the new stop_bpstat. */
4544 again = 1;
4545 break;
4546 }
4547 }
4548 return again;
4549 }
4550
4551 void
4552 bpstat_do_actions (void)
4553 {
4554 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4555
4556 /* Do any commands attached to breakpoint we are stopped at. */
4557 while (!ptid_equal (inferior_ptid, null_ptid)
4558 && target_has_execution
4559 && !is_exited (inferior_ptid)
4560 && !is_executing (inferior_ptid))
4561 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4562 and only return when it is stopped at the next breakpoint, we
4563 keep doing breakpoint actions until it returns false to
4564 indicate the inferior was not resumed. */
4565 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4566 break;
4567
4568 discard_cleanups (cleanup_if_error);
4569 }
4570
4571 /* Print out the (old or new) value associated with a watchpoint. */
4572
4573 static void
4574 watchpoint_value_print (struct value *val, struct ui_file *stream)
4575 {
4576 if (val == NULL)
4577 fprintf_unfiltered (stream, _("<unreadable>"));
4578 else
4579 {
4580 struct value_print_options opts;
4581 get_user_print_options (&opts);
4582 value_print (val, stream, &opts);
4583 }
4584 }
4585
4586 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4587 debugging multiple threads. */
4588
4589 void
4590 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4591 {
4592 if (uiout->is_mi_like_p ())
4593 return;
4594
4595 uiout->text ("\n");
4596
4597 if (show_thread_that_caused_stop ())
4598 {
4599 const char *name;
4600 struct thread_info *thr = inferior_thread ();
4601
4602 uiout->text ("Thread ");
4603 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4604
4605 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4606 if (name != NULL)
4607 {
4608 uiout->text (" \"");
4609 uiout->field_fmt ("name", "%s", name);
4610 uiout->text ("\"");
4611 }
4612
4613 uiout->text (" hit ");
4614 }
4615 }
4616
4617 /* Generic routine for printing messages indicating why we
4618 stopped. The behavior of this function depends on the value
4619 'print_it' in the bpstat structure. Under some circumstances we
4620 may decide not to print anything here and delegate the task to
4621 normal_stop(). */
4622
4623 static enum print_stop_action
4624 print_bp_stop_message (bpstat bs)
4625 {
4626 switch (bs->print_it)
4627 {
4628 case print_it_noop:
4629 /* Nothing should be printed for this bpstat entry. */
4630 return PRINT_UNKNOWN;
4631 break;
4632
4633 case print_it_done:
4634 /* We still want to print the frame, but we already printed the
4635 relevant messages. */
4636 return PRINT_SRC_AND_LOC;
4637 break;
4638
4639 case print_it_normal:
4640 {
4641 struct breakpoint *b = bs->breakpoint_at;
4642
4643 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4644 which has since been deleted. */
4645 if (b == NULL)
4646 return PRINT_UNKNOWN;
4647
4648 /* Normal case. Call the breakpoint's print_it method. */
4649 return b->ops->print_it (bs);
4650 }
4651 break;
4652
4653 default:
4654 internal_error (__FILE__, __LINE__,
4655 _("print_bp_stop_message: unrecognized enum value"));
4656 break;
4657 }
4658 }
4659
4660 /* A helper function that prints a shared library stopped event. */
4661
4662 static void
4663 print_solib_event (int is_catchpoint)
4664 {
4665 int any_deleted
4666 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4667 int any_added
4668 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4669
4670 if (!is_catchpoint)
4671 {
4672 if (any_added || any_deleted)
4673 current_uiout->text (_("Stopped due to shared library event:\n"));
4674 else
4675 current_uiout->text (_("Stopped due to shared library event (no "
4676 "libraries added or removed)\n"));
4677 }
4678
4679 if (current_uiout->is_mi_like_p ())
4680 current_uiout->field_string ("reason",
4681 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4682
4683 if (any_deleted)
4684 {
4685 char *name;
4686 int ix;
4687
4688 current_uiout->text (_(" Inferior unloaded "));
4689 ui_out_emit_list list_emitter (current_uiout, "removed");
4690 for (ix = 0;
4691 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4692 ix, name);
4693 ++ix)
4694 {
4695 if (ix > 0)
4696 current_uiout->text (" ");
4697 current_uiout->field_string ("library", name);
4698 current_uiout->text ("\n");
4699 }
4700 }
4701
4702 if (any_added)
4703 {
4704 struct so_list *iter;
4705 int ix;
4706
4707 current_uiout->text (_(" Inferior loaded "));
4708 ui_out_emit_list list_emitter (current_uiout, "added");
4709 for (ix = 0;
4710 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4711 ix, iter);
4712 ++ix)
4713 {
4714 if (ix > 0)
4715 current_uiout->text (" ");
4716 current_uiout->field_string ("library", iter->so_name);
4717 current_uiout->text ("\n");
4718 }
4719 }
4720 }
4721
4722 /* Print a message indicating what happened. This is called from
4723 normal_stop(). The input to this routine is the head of the bpstat
4724 list - a list of the eventpoints that caused this stop. KIND is
4725 the target_waitkind for the stopping event. This
4726 routine calls the generic print routine for printing a message
4727 about reasons for stopping. This will print (for example) the
4728 "Breakpoint n," part of the output. The return value of this
4729 routine is one of:
4730
4731 PRINT_UNKNOWN: Means we printed nothing.
4732 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4733 code to print the location. An example is
4734 "Breakpoint 1, " which should be followed by
4735 the location.
4736 PRINT_SRC_ONLY: Means we printed something, but there is no need
4737 to also print the location part of the message.
4738 An example is the catch/throw messages, which
4739 don't require a location appended to the end.
4740 PRINT_NOTHING: We have done some printing and we don't need any
4741 further info to be printed. */
4742
4743 enum print_stop_action
4744 bpstat_print (bpstat bs, int kind)
4745 {
4746 enum print_stop_action val;
4747
4748 /* Maybe another breakpoint in the chain caused us to stop.
4749 (Currently all watchpoints go on the bpstat whether hit or not.
4750 That probably could (should) be changed, provided care is taken
4751 with respect to bpstat_explains_signal). */
4752 for (; bs; bs = bs->next)
4753 {
4754 val = print_bp_stop_message (bs);
4755 if (val == PRINT_SRC_ONLY
4756 || val == PRINT_SRC_AND_LOC
4757 || val == PRINT_NOTHING)
4758 return val;
4759 }
4760
4761 /* If we had hit a shared library event breakpoint,
4762 print_bp_stop_message would print out this message. If we hit an
4763 OS-level shared library event, do the same thing. */
4764 if (kind == TARGET_WAITKIND_LOADED)
4765 {
4766 print_solib_event (0);
4767 return PRINT_NOTHING;
4768 }
4769
4770 /* We reached the end of the chain, or we got a null BS to start
4771 with and nothing was printed. */
4772 return PRINT_UNKNOWN;
4773 }
4774
4775 /* Evaluate the boolean expression EXP and return the result. */
4776
4777 static bool
4778 breakpoint_cond_eval (expression *exp)
4779 {
4780 struct value *mark = value_mark ();
4781 bool res = value_true (evaluate_expression (exp));
4782
4783 value_free_to_mark (mark);
4784 return res;
4785 }
4786
4787 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4788
4789 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4790 : next (NULL),
4791 bp_location_at (bl),
4792 breakpoint_at (bl->owner),
4793 commands (NULL),
4794 old_val (NULL),
4795 print (0),
4796 stop (0),
4797 print_it (print_it_normal)
4798 {
4799 incref_bp_location (bl);
4800 **bs_link_pointer = this;
4801 *bs_link_pointer = &next;
4802 }
4803
4804 bpstats::bpstats ()
4805 : next (NULL),
4806 bp_location_at (NULL),
4807 breakpoint_at (NULL),
4808 commands (NULL),
4809 old_val (NULL),
4810 print (0),
4811 stop (0),
4812 print_it (print_it_normal)
4813 {
4814 }
4815 \f
4816 /* The target has stopped with waitstatus WS. Check if any hardware
4817 watchpoints have triggered, according to the target. */
4818
4819 int
4820 watchpoints_triggered (struct target_waitstatus *ws)
4821 {
4822 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4823 CORE_ADDR addr;
4824 struct breakpoint *b;
4825
4826 if (!stopped_by_watchpoint)
4827 {
4828 /* We were not stopped by a watchpoint. Mark all watchpoints
4829 as not triggered. */
4830 ALL_BREAKPOINTS (b)
4831 if (is_hardware_watchpoint (b))
4832 {
4833 struct watchpoint *w = (struct watchpoint *) b;
4834
4835 w->watchpoint_triggered = watch_triggered_no;
4836 }
4837
4838 return 0;
4839 }
4840
4841 if (!target_stopped_data_address (&current_target, &addr))
4842 {
4843 /* We were stopped by a watchpoint, but we don't know where.
4844 Mark all watchpoints as unknown. */
4845 ALL_BREAKPOINTS (b)
4846 if (is_hardware_watchpoint (b))
4847 {
4848 struct watchpoint *w = (struct watchpoint *) b;
4849
4850 w->watchpoint_triggered = watch_triggered_unknown;
4851 }
4852
4853 return 1;
4854 }
4855
4856 /* The target could report the data address. Mark watchpoints
4857 affected by this data address as triggered, and all others as not
4858 triggered. */
4859
4860 ALL_BREAKPOINTS (b)
4861 if (is_hardware_watchpoint (b))
4862 {
4863 struct watchpoint *w = (struct watchpoint *) b;
4864 struct bp_location *loc;
4865
4866 w->watchpoint_triggered = watch_triggered_no;
4867 for (loc = b->loc; loc; loc = loc->next)
4868 {
4869 if (is_masked_watchpoint (b))
4870 {
4871 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4872 CORE_ADDR start = loc->address & w->hw_wp_mask;
4873
4874 if (newaddr == start)
4875 {
4876 w->watchpoint_triggered = watch_triggered_yes;
4877 break;
4878 }
4879 }
4880 /* Exact match not required. Within range is sufficient. */
4881 else if (target_watchpoint_addr_within_range (&current_target,
4882 addr, loc->address,
4883 loc->length))
4884 {
4885 w->watchpoint_triggered = watch_triggered_yes;
4886 break;
4887 }
4888 }
4889 }
4890
4891 return 1;
4892 }
4893
4894 /* Possible return values for watchpoint_check. */
4895 enum wp_check_result
4896 {
4897 /* The watchpoint has been deleted. */
4898 WP_DELETED = 1,
4899
4900 /* The value has changed. */
4901 WP_VALUE_CHANGED = 2,
4902
4903 /* The value has not changed. */
4904 WP_VALUE_NOT_CHANGED = 3,
4905
4906 /* Ignore this watchpoint, no matter if the value changed or not. */
4907 WP_IGNORE = 4,
4908 };
4909
4910 #define BP_TEMPFLAG 1
4911 #define BP_HARDWAREFLAG 2
4912
4913 /* Evaluate watchpoint condition expression and check if its value
4914 changed. */
4915
4916 static wp_check_result
4917 watchpoint_check (bpstat bs)
4918 {
4919 struct watchpoint *b;
4920 struct frame_info *fr;
4921 int within_current_scope;
4922
4923 /* BS is built from an existing struct breakpoint. */
4924 gdb_assert (bs->breakpoint_at != NULL);
4925 b = (struct watchpoint *) bs->breakpoint_at;
4926
4927 /* If this is a local watchpoint, we only want to check if the
4928 watchpoint frame is in scope if the current thread is the thread
4929 that was used to create the watchpoint. */
4930 if (!watchpoint_in_thread_scope (b))
4931 return WP_IGNORE;
4932
4933 if (b->exp_valid_block == NULL)
4934 within_current_scope = 1;
4935 else
4936 {
4937 struct frame_info *frame = get_current_frame ();
4938 struct gdbarch *frame_arch = get_frame_arch (frame);
4939 CORE_ADDR frame_pc = get_frame_pc (frame);
4940
4941 /* stack_frame_destroyed_p() returns a non-zero value if we're
4942 still in the function but the stack frame has already been
4943 invalidated. Since we can't rely on the values of local
4944 variables after the stack has been destroyed, we are treating
4945 the watchpoint in that state as `not changed' without further
4946 checking. Don't mark watchpoints as changed if the current
4947 frame is in an epilogue - even if they are in some other
4948 frame, our view of the stack is likely to be wrong and
4949 frame_find_by_id could error out. */
4950 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4951 return WP_IGNORE;
4952
4953 fr = frame_find_by_id (b->watchpoint_frame);
4954 within_current_scope = (fr != NULL);
4955
4956 /* If we've gotten confused in the unwinder, we might have
4957 returned a frame that can't describe this variable. */
4958 if (within_current_scope)
4959 {
4960 struct symbol *function;
4961
4962 function = get_frame_function (fr);
4963 if (function == NULL
4964 || !contained_in (b->exp_valid_block,
4965 SYMBOL_BLOCK_VALUE (function)))
4966 within_current_scope = 0;
4967 }
4968
4969 if (within_current_scope)
4970 /* If we end up stopping, the current frame will get selected
4971 in normal_stop. So this call to select_frame won't affect
4972 the user. */
4973 select_frame (fr);
4974 }
4975
4976 if (within_current_scope)
4977 {
4978 /* We use value_{,free_to_}mark because it could be a *long*
4979 time before we return to the command level and call
4980 free_all_values. We can't call free_all_values because we
4981 might be in the middle of evaluating a function call. */
4982
4983 int pc = 0;
4984 struct value *mark;
4985 struct value *new_val;
4986
4987 if (is_masked_watchpoint (b))
4988 /* Since we don't know the exact trigger address (from
4989 stopped_data_address), just tell the user we've triggered
4990 a mask watchpoint. */
4991 return WP_VALUE_CHANGED;
4992
4993 mark = value_mark ();
4994 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4995
4996 if (b->val_bitsize != 0)
4997 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4998
4999 /* We use value_equal_contents instead of value_equal because
5000 the latter coerces an array to a pointer, thus comparing just
5001 the address of the array instead of its contents. This is
5002 not what we want. */
5003 if ((b->val != NULL) != (new_val != NULL)
5004 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5005 {
5006 if (new_val != NULL)
5007 {
5008 release_value (new_val);
5009 value_free_to_mark (mark);
5010 }
5011 bs->old_val = b->val;
5012 b->val = new_val;
5013 b->val_valid = 1;
5014 return WP_VALUE_CHANGED;
5015 }
5016 else
5017 {
5018 /* Nothing changed. */
5019 value_free_to_mark (mark);
5020 return WP_VALUE_NOT_CHANGED;
5021 }
5022 }
5023 else
5024 {
5025 /* This seems like the only logical thing to do because
5026 if we temporarily ignored the watchpoint, then when
5027 we reenter the block in which it is valid it contains
5028 garbage (in the case of a function, it may have two
5029 garbage values, one before and one after the prologue).
5030 So we can't even detect the first assignment to it and
5031 watch after that (since the garbage may or may not equal
5032 the first value assigned). */
5033 /* We print all the stop information in
5034 breakpoint_ops->print_it, but in this case, by the time we
5035 call breakpoint_ops->print_it this bp will be deleted
5036 already. So we have no choice but print the information
5037 here. */
5038
5039 SWITCH_THRU_ALL_UIS ()
5040 {
5041 struct ui_out *uiout = current_uiout;
5042
5043 if (uiout->is_mi_like_p ())
5044 uiout->field_string
5045 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5046 uiout->text ("\nWatchpoint ");
5047 uiout->field_int ("wpnum", b->number);
5048 uiout->text (" deleted because the program has left the block in\n"
5049 "which its expression is valid.\n");
5050 }
5051
5052 /* Make sure the watchpoint's commands aren't executed. */
5053 b->commands = NULL;
5054 watchpoint_del_at_next_stop (b);
5055
5056 return WP_DELETED;
5057 }
5058 }
5059
5060 /* Return true if it looks like target has stopped due to hitting
5061 breakpoint location BL. This function does not check if we should
5062 stop, only if BL explains the stop. */
5063
5064 static int
5065 bpstat_check_location (const struct bp_location *bl,
5066 const address_space *aspace, CORE_ADDR bp_addr,
5067 const struct target_waitstatus *ws)
5068 {
5069 struct breakpoint *b = bl->owner;
5070
5071 /* BL is from an existing breakpoint. */
5072 gdb_assert (b != NULL);
5073
5074 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5075 }
5076
5077 /* Determine if the watched values have actually changed, and we
5078 should stop. If not, set BS->stop to 0. */
5079
5080 static void
5081 bpstat_check_watchpoint (bpstat bs)
5082 {
5083 const struct bp_location *bl;
5084 struct watchpoint *b;
5085
5086 /* BS is built for existing struct breakpoint. */
5087 bl = bs->bp_location_at;
5088 gdb_assert (bl != NULL);
5089 b = (struct watchpoint *) bs->breakpoint_at;
5090 gdb_assert (b != NULL);
5091
5092 {
5093 int must_check_value = 0;
5094
5095 if (b->type == bp_watchpoint)
5096 /* For a software watchpoint, we must always check the
5097 watched value. */
5098 must_check_value = 1;
5099 else if (b->watchpoint_triggered == watch_triggered_yes)
5100 /* We have a hardware watchpoint (read, write, or access)
5101 and the target earlier reported an address watched by
5102 this watchpoint. */
5103 must_check_value = 1;
5104 else if (b->watchpoint_triggered == watch_triggered_unknown
5105 && b->type == bp_hardware_watchpoint)
5106 /* We were stopped by a hardware watchpoint, but the target could
5107 not report the data address. We must check the watchpoint's
5108 value. Access and read watchpoints are out of luck; without
5109 a data address, we can't figure it out. */
5110 must_check_value = 1;
5111
5112 if (must_check_value)
5113 {
5114 wp_check_result e;
5115
5116 TRY
5117 {
5118 e = watchpoint_check (bs);
5119 }
5120 CATCH (ex, RETURN_MASK_ALL)
5121 {
5122 exception_fprintf (gdb_stderr, ex,
5123 "Error evaluating expression "
5124 "for watchpoint %d\n",
5125 b->number);
5126
5127 SWITCH_THRU_ALL_UIS ()
5128 {
5129 printf_filtered (_("Watchpoint %d deleted.\n"),
5130 b->number);
5131 }
5132 watchpoint_del_at_next_stop (b);
5133 e = WP_DELETED;
5134 }
5135 END_CATCH
5136
5137 switch (e)
5138 {
5139 case WP_DELETED:
5140 /* We've already printed what needs to be printed. */
5141 bs->print_it = print_it_done;
5142 /* Stop. */
5143 break;
5144 case WP_IGNORE:
5145 bs->print_it = print_it_noop;
5146 bs->stop = 0;
5147 break;
5148 case WP_VALUE_CHANGED:
5149 if (b->type == bp_read_watchpoint)
5150 {
5151 /* There are two cases to consider here:
5152
5153 1. We're watching the triggered memory for reads.
5154 In that case, trust the target, and always report
5155 the watchpoint hit to the user. Even though
5156 reads don't cause value changes, the value may
5157 have changed since the last time it was read, and
5158 since we're not trapping writes, we will not see
5159 those, and as such we should ignore our notion of
5160 old value.
5161
5162 2. We're watching the triggered memory for both
5163 reads and writes. There are two ways this may
5164 happen:
5165
5166 2.1. This is a target that can't break on data
5167 reads only, but can break on accesses (reads or
5168 writes), such as e.g., x86. We detect this case
5169 at the time we try to insert read watchpoints.
5170
5171 2.2. Otherwise, the target supports read
5172 watchpoints, but, the user set an access or write
5173 watchpoint watching the same memory as this read
5174 watchpoint.
5175
5176 If we're watching memory writes as well as reads,
5177 ignore watchpoint hits when we find that the
5178 value hasn't changed, as reads don't cause
5179 changes. This still gives false positives when
5180 the program writes the same value to memory as
5181 what there was already in memory (we will confuse
5182 it for a read), but it's much better than
5183 nothing. */
5184
5185 int other_write_watchpoint = 0;
5186
5187 if (bl->watchpoint_type == hw_read)
5188 {
5189 struct breakpoint *other_b;
5190
5191 ALL_BREAKPOINTS (other_b)
5192 if (other_b->type == bp_hardware_watchpoint
5193 || other_b->type == bp_access_watchpoint)
5194 {
5195 struct watchpoint *other_w =
5196 (struct watchpoint *) other_b;
5197
5198 if (other_w->watchpoint_triggered
5199 == watch_triggered_yes)
5200 {
5201 other_write_watchpoint = 1;
5202 break;
5203 }
5204 }
5205 }
5206
5207 if (other_write_watchpoint
5208 || bl->watchpoint_type == hw_access)
5209 {
5210 /* We're watching the same memory for writes,
5211 and the value changed since the last time we
5212 updated it, so this trap must be for a write.
5213 Ignore it. */
5214 bs->print_it = print_it_noop;
5215 bs->stop = 0;
5216 }
5217 }
5218 break;
5219 case WP_VALUE_NOT_CHANGED:
5220 if (b->type == bp_hardware_watchpoint
5221 || b->type == bp_watchpoint)
5222 {
5223 /* Don't stop: write watchpoints shouldn't fire if
5224 the value hasn't changed. */
5225 bs->print_it = print_it_noop;
5226 bs->stop = 0;
5227 }
5228 /* Stop. */
5229 break;
5230 default:
5231 /* Can't happen. */
5232 break;
5233 }
5234 }
5235 else /* must_check_value == 0 */
5236 {
5237 /* This is a case where some watchpoint(s) triggered, but
5238 not at the address of this watchpoint, or else no
5239 watchpoint triggered after all. So don't print
5240 anything for this watchpoint. */
5241 bs->print_it = print_it_noop;
5242 bs->stop = 0;
5243 }
5244 }
5245 }
5246
5247 /* For breakpoints that are currently marked as telling gdb to stop,
5248 check conditions (condition proper, frame, thread and ignore count)
5249 of breakpoint referred to by BS. If we should not stop for this
5250 breakpoint, set BS->stop to 0. */
5251
5252 static void
5253 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5254 {
5255 const struct bp_location *bl;
5256 struct breakpoint *b;
5257 /* Assume stop. */
5258 bool condition_result = true;
5259 struct expression *cond;
5260
5261 gdb_assert (bs->stop);
5262
5263 /* BS is built for existing struct breakpoint. */
5264 bl = bs->bp_location_at;
5265 gdb_assert (bl != NULL);
5266 b = bs->breakpoint_at;
5267 gdb_assert (b != NULL);
5268
5269 /* Even if the target evaluated the condition on its end and notified GDB, we
5270 need to do so again since GDB does not know if we stopped due to a
5271 breakpoint or a single step breakpoint. */
5272
5273 if (frame_id_p (b->frame_id)
5274 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5275 {
5276 bs->stop = 0;
5277 return;
5278 }
5279
5280 /* If this is a thread/task-specific breakpoint, don't waste cpu
5281 evaluating the condition if this isn't the specified
5282 thread/task. */
5283 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5284 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5285
5286 {
5287 bs->stop = 0;
5288 return;
5289 }
5290
5291 /* Evaluate extension language breakpoints that have a "stop" method
5292 implemented. */
5293 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5294
5295 if (is_watchpoint (b))
5296 {
5297 struct watchpoint *w = (struct watchpoint *) b;
5298
5299 cond = w->cond_exp.get ();
5300 }
5301 else
5302 cond = bl->cond.get ();
5303
5304 if (cond && b->disposition != disp_del_at_next_stop)
5305 {
5306 int within_current_scope = 1;
5307 struct watchpoint * w;
5308
5309 /* We use value_mark and value_free_to_mark because it could
5310 be a long time before we return to the command level and
5311 call free_all_values. We can't call free_all_values
5312 because we might be in the middle of evaluating a
5313 function call. */
5314 struct value *mark = value_mark ();
5315
5316 if (is_watchpoint (b))
5317 w = (struct watchpoint *) b;
5318 else
5319 w = NULL;
5320
5321 /* Need to select the frame, with all that implies so that
5322 the conditions will have the right context. Because we
5323 use the frame, we will not see an inlined function's
5324 variables when we arrive at a breakpoint at the start
5325 of the inlined function; the current frame will be the
5326 call site. */
5327 if (w == NULL || w->cond_exp_valid_block == NULL)
5328 select_frame (get_current_frame ());
5329 else
5330 {
5331 struct frame_info *frame;
5332
5333 /* For local watchpoint expressions, which particular
5334 instance of a local is being watched matters, so we
5335 keep track of the frame to evaluate the expression
5336 in. To evaluate the condition however, it doesn't
5337 really matter which instantiation of the function
5338 where the condition makes sense triggers the
5339 watchpoint. This allows an expression like "watch
5340 global if q > 10" set in `func', catch writes to
5341 global on all threads that call `func', or catch
5342 writes on all recursive calls of `func' by a single
5343 thread. We simply always evaluate the condition in
5344 the innermost frame that's executing where it makes
5345 sense to evaluate the condition. It seems
5346 intuitive. */
5347 frame = block_innermost_frame (w->cond_exp_valid_block);
5348 if (frame != NULL)
5349 select_frame (frame);
5350 else
5351 within_current_scope = 0;
5352 }
5353 if (within_current_scope)
5354 {
5355 TRY
5356 {
5357 condition_result = breakpoint_cond_eval (cond);
5358 }
5359 CATCH (ex, RETURN_MASK_ALL)
5360 {
5361 exception_fprintf (gdb_stderr, ex,
5362 "Error in testing breakpoint condition:\n");
5363 }
5364 END_CATCH
5365 }
5366 else
5367 {
5368 warning (_("Watchpoint condition cannot be tested "
5369 "in the current scope"));
5370 /* If we failed to set the right context for this
5371 watchpoint, unconditionally report it. */
5372 }
5373 /* FIXME-someday, should give breakpoint #. */
5374 value_free_to_mark (mark);
5375 }
5376
5377 if (cond && !condition_result)
5378 {
5379 bs->stop = 0;
5380 }
5381 else if (b->ignore_count > 0)
5382 {
5383 b->ignore_count--;
5384 bs->stop = 0;
5385 /* Increase the hit count even though we don't stop. */
5386 ++(b->hit_count);
5387 observer_notify_breakpoint_modified (b);
5388 }
5389 }
5390
5391 /* Returns true if we need to track moribund locations of LOC's type
5392 on the current target. */
5393
5394 static int
5395 need_moribund_for_location_type (struct bp_location *loc)
5396 {
5397 return ((loc->loc_type == bp_loc_software_breakpoint
5398 && !target_supports_stopped_by_sw_breakpoint ())
5399 || (loc->loc_type == bp_loc_hardware_breakpoint
5400 && !target_supports_stopped_by_hw_breakpoint ()));
5401 }
5402
5403
5404 /* Get a bpstat associated with having just stopped at address
5405 BP_ADDR in thread PTID.
5406
5407 Determine whether we stopped at a breakpoint, etc, or whether we
5408 don't understand this stop. Result is a chain of bpstat's such
5409 that:
5410
5411 if we don't understand the stop, the result is a null pointer.
5412
5413 if we understand why we stopped, the result is not null.
5414
5415 Each element of the chain refers to a particular breakpoint or
5416 watchpoint at which we have stopped. (We may have stopped for
5417 several reasons concurrently.)
5418
5419 Each element of the chain has valid next, breakpoint_at,
5420 commands, FIXME??? fields. */
5421
5422 bpstat
5423 bpstat_stop_status (const address_space *aspace,
5424 CORE_ADDR bp_addr, ptid_t ptid,
5425 const struct target_waitstatus *ws)
5426 {
5427 struct breakpoint *b = NULL;
5428 struct bp_location *bl;
5429 struct bp_location *loc;
5430 /* First item of allocated bpstat's. */
5431 bpstat bs_head = NULL, *bs_link = &bs_head;
5432 /* Pointer to the last thing in the chain currently. */
5433 bpstat bs;
5434 int ix;
5435 int need_remove_insert;
5436 int removed_any;
5437
5438 /* First, build the bpstat chain with locations that explain a
5439 target stop, while being careful to not set the target running,
5440 as that may invalidate locations (in particular watchpoint
5441 locations are recreated). Resuming will happen here with
5442 breakpoint conditions or watchpoint expressions that include
5443 inferior function calls. */
5444
5445 ALL_BREAKPOINTS (b)
5446 {
5447 if (!breakpoint_enabled (b))
5448 continue;
5449
5450 for (bl = b->loc; bl != NULL; bl = bl->next)
5451 {
5452 /* For hardware watchpoints, we look only at the first
5453 location. The watchpoint_check function will work on the
5454 entire expression, not the individual locations. For
5455 read watchpoints, the watchpoints_triggered function has
5456 checked all locations already. */
5457 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5458 break;
5459
5460 if (!bl->enabled || bl->shlib_disabled)
5461 continue;
5462
5463 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5464 continue;
5465
5466 /* Come here if it's a watchpoint, or if the break address
5467 matches. */
5468
5469 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5470 explain stop. */
5471
5472 /* Assume we stop. Should we find a watchpoint that is not
5473 actually triggered, or if the condition of the breakpoint
5474 evaluates as false, we'll reset 'stop' to 0. */
5475 bs->stop = 1;
5476 bs->print = 1;
5477
5478 /* If this is a scope breakpoint, mark the associated
5479 watchpoint as triggered so that we will handle the
5480 out-of-scope event. We'll get to the watchpoint next
5481 iteration. */
5482 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5483 {
5484 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5485
5486 w->watchpoint_triggered = watch_triggered_yes;
5487 }
5488 }
5489 }
5490
5491 /* Check if a moribund breakpoint explains the stop. */
5492 if (!target_supports_stopped_by_sw_breakpoint ()
5493 || !target_supports_stopped_by_hw_breakpoint ())
5494 {
5495 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5496 {
5497 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5498 && need_moribund_for_location_type (loc))
5499 {
5500 bs = new bpstats (loc, &bs_link);
5501 /* For hits of moribund locations, we should just proceed. */
5502 bs->stop = 0;
5503 bs->print = 0;
5504 bs->print_it = print_it_noop;
5505 }
5506 }
5507 }
5508
5509 /* A bit of special processing for shlib breakpoints. We need to
5510 process solib loading here, so that the lists of loaded and
5511 unloaded libraries are correct before we handle "catch load" and
5512 "catch unload". */
5513 for (bs = bs_head; bs != NULL; bs = bs->next)
5514 {
5515 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5516 {
5517 handle_solib_event ();
5518 break;
5519 }
5520 }
5521
5522 /* Now go through the locations that caused the target to stop, and
5523 check whether we're interested in reporting this stop to higher
5524 layers, or whether we should resume the target transparently. */
5525
5526 removed_any = 0;
5527
5528 for (bs = bs_head; bs != NULL; bs = bs->next)
5529 {
5530 if (!bs->stop)
5531 continue;
5532
5533 b = bs->breakpoint_at;
5534 b->ops->check_status (bs);
5535 if (bs->stop)
5536 {
5537 bpstat_check_breakpoint_conditions (bs, ptid);
5538
5539 if (bs->stop)
5540 {
5541 ++(b->hit_count);
5542 observer_notify_breakpoint_modified (b);
5543
5544 /* We will stop here. */
5545 if (b->disposition == disp_disable)
5546 {
5547 --(b->enable_count);
5548 if (b->enable_count <= 0)
5549 b->enable_state = bp_disabled;
5550 removed_any = 1;
5551 }
5552 if (b->silent)
5553 bs->print = 0;
5554 bs->commands = b->commands;
5555 if (command_line_is_silent (bs->commands
5556 ? bs->commands.get () : NULL))
5557 bs->print = 0;
5558
5559 b->ops->after_condition_true (bs);
5560 }
5561
5562 }
5563
5564 /* Print nothing for this entry if we don't stop or don't
5565 print. */
5566 if (!bs->stop || !bs->print)
5567 bs->print_it = print_it_noop;
5568 }
5569
5570 /* If we aren't stopping, the value of some hardware watchpoint may
5571 not have changed, but the intermediate memory locations we are
5572 watching may have. Don't bother if we're stopping; this will get
5573 done later. */
5574 need_remove_insert = 0;
5575 if (! bpstat_causes_stop (bs_head))
5576 for (bs = bs_head; bs != NULL; bs = bs->next)
5577 if (!bs->stop
5578 && bs->breakpoint_at
5579 && is_hardware_watchpoint (bs->breakpoint_at))
5580 {
5581 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5582
5583 update_watchpoint (w, 0 /* don't reparse. */);
5584 need_remove_insert = 1;
5585 }
5586
5587 if (need_remove_insert)
5588 update_global_location_list (UGLL_MAY_INSERT);
5589 else if (removed_any)
5590 update_global_location_list (UGLL_DONT_INSERT);
5591
5592 return bs_head;
5593 }
5594
5595 static void
5596 handle_jit_event (void)
5597 {
5598 struct frame_info *frame;
5599 struct gdbarch *gdbarch;
5600
5601 if (debug_infrun)
5602 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5603
5604 /* Switch terminal for any messages produced by
5605 breakpoint_re_set. */
5606 target_terminal::ours_for_output ();
5607
5608 frame = get_current_frame ();
5609 gdbarch = get_frame_arch (frame);
5610
5611 jit_event_handler (gdbarch);
5612
5613 target_terminal::inferior ();
5614 }
5615
5616 /* Prepare WHAT final decision for infrun. */
5617
5618 /* Decide what infrun needs to do with this bpstat. */
5619
5620 struct bpstat_what
5621 bpstat_what (bpstat bs_head)
5622 {
5623 struct bpstat_what retval;
5624 bpstat bs;
5625
5626 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5627 retval.call_dummy = STOP_NONE;
5628 retval.is_longjmp = 0;
5629
5630 for (bs = bs_head; bs != NULL; bs = bs->next)
5631 {
5632 /* Extract this BS's action. After processing each BS, we check
5633 if its action overrides all we've seem so far. */
5634 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5635 enum bptype bptype;
5636
5637 if (bs->breakpoint_at == NULL)
5638 {
5639 /* I suspect this can happen if it was a momentary
5640 breakpoint which has since been deleted. */
5641 bptype = bp_none;
5642 }
5643 else
5644 bptype = bs->breakpoint_at->type;
5645
5646 switch (bptype)
5647 {
5648 case bp_none:
5649 break;
5650 case bp_breakpoint:
5651 case bp_hardware_breakpoint:
5652 case bp_single_step:
5653 case bp_until:
5654 case bp_finish:
5655 case bp_shlib_event:
5656 if (bs->stop)
5657 {
5658 if (bs->print)
5659 this_action = BPSTAT_WHAT_STOP_NOISY;
5660 else
5661 this_action = BPSTAT_WHAT_STOP_SILENT;
5662 }
5663 else
5664 this_action = BPSTAT_WHAT_SINGLE;
5665 break;
5666 case bp_watchpoint:
5667 case bp_hardware_watchpoint:
5668 case bp_read_watchpoint:
5669 case bp_access_watchpoint:
5670 if (bs->stop)
5671 {
5672 if (bs->print)
5673 this_action = BPSTAT_WHAT_STOP_NOISY;
5674 else
5675 this_action = BPSTAT_WHAT_STOP_SILENT;
5676 }
5677 else
5678 {
5679 /* There was a watchpoint, but we're not stopping.
5680 This requires no further action. */
5681 }
5682 break;
5683 case bp_longjmp:
5684 case bp_longjmp_call_dummy:
5685 case bp_exception:
5686 if (bs->stop)
5687 {
5688 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5689 retval.is_longjmp = bptype != bp_exception;
5690 }
5691 else
5692 this_action = BPSTAT_WHAT_SINGLE;
5693 break;
5694 case bp_longjmp_resume:
5695 case bp_exception_resume:
5696 if (bs->stop)
5697 {
5698 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5699 retval.is_longjmp = bptype == bp_longjmp_resume;
5700 }
5701 else
5702 this_action = BPSTAT_WHAT_SINGLE;
5703 break;
5704 case bp_step_resume:
5705 if (bs->stop)
5706 this_action = BPSTAT_WHAT_STEP_RESUME;
5707 else
5708 {
5709 /* It is for the wrong frame. */
5710 this_action = BPSTAT_WHAT_SINGLE;
5711 }
5712 break;
5713 case bp_hp_step_resume:
5714 if (bs->stop)
5715 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5716 else
5717 {
5718 /* It is for the wrong frame. */
5719 this_action = BPSTAT_WHAT_SINGLE;
5720 }
5721 break;
5722 case bp_watchpoint_scope:
5723 case bp_thread_event:
5724 case bp_overlay_event:
5725 case bp_longjmp_master:
5726 case bp_std_terminate_master:
5727 case bp_exception_master:
5728 this_action = BPSTAT_WHAT_SINGLE;
5729 break;
5730 case bp_catchpoint:
5731 if (bs->stop)
5732 {
5733 if (bs->print)
5734 this_action = BPSTAT_WHAT_STOP_NOISY;
5735 else
5736 this_action = BPSTAT_WHAT_STOP_SILENT;
5737 }
5738 else
5739 {
5740 /* There was a catchpoint, but we're not stopping.
5741 This requires no further action. */
5742 }
5743 break;
5744 case bp_jit_event:
5745 this_action = BPSTAT_WHAT_SINGLE;
5746 break;
5747 case bp_call_dummy:
5748 /* Make sure the action is stop (silent or noisy),
5749 so infrun.c pops the dummy frame. */
5750 retval.call_dummy = STOP_STACK_DUMMY;
5751 this_action = BPSTAT_WHAT_STOP_SILENT;
5752 break;
5753 case bp_std_terminate:
5754 /* Make sure the action is stop (silent or noisy),
5755 so infrun.c pops the dummy frame. */
5756 retval.call_dummy = STOP_STD_TERMINATE;
5757 this_action = BPSTAT_WHAT_STOP_SILENT;
5758 break;
5759 case bp_tracepoint:
5760 case bp_fast_tracepoint:
5761 case bp_static_tracepoint:
5762 /* Tracepoint hits should not be reported back to GDB, and
5763 if one got through somehow, it should have been filtered
5764 out already. */
5765 internal_error (__FILE__, __LINE__,
5766 _("bpstat_what: tracepoint encountered"));
5767 break;
5768 case bp_gnu_ifunc_resolver:
5769 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5770 this_action = BPSTAT_WHAT_SINGLE;
5771 break;
5772 case bp_gnu_ifunc_resolver_return:
5773 /* The breakpoint will be removed, execution will restart from the
5774 PC of the former breakpoint. */
5775 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5776 break;
5777
5778 case bp_dprintf:
5779 if (bs->stop)
5780 this_action = BPSTAT_WHAT_STOP_SILENT;
5781 else
5782 this_action = BPSTAT_WHAT_SINGLE;
5783 break;
5784
5785 default:
5786 internal_error (__FILE__, __LINE__,
5787 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5788 }
5789
5790 retval.main_action = std::max (retval.main_action, this_action);
5791 }
5792
5793 return retval;
5794 }
5795
5796 void
5797 bpstat_run_callbacks (bpstat bs_head)
5798 {
5799 bpstat bs;
5800
5801 for (bs = bs_head; bs != NULL; bs = bs->next)
5802 {
5803 struct breakpoint *b = bs->breakpoint_at;
5804
5805 if (b == NULL)
5806 continue;
5807 switch (b->type)
5808 {
5809 case bp_jit_event:
5810 handle_jit_event ();
5811 break;
5812 case bp_gnu_ifunc_resolver:
5813 gnu_ifunc_resolver_stop (b);
5814 break;
5815 case bp_gnu_ifunc_resolver_return:
5816 gnu_ifunc_resolver_return_stop (b);
5817 break;
5818 }
5819 }
5820 }
5821
5822 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5823 without hardware support). This isn't related to a specific bpstat,
5824 just to things like whether watchpoints are set. */
5825
5826 int
5827 bpstat_should_step (void)
5828 {
5829 struct breakpoint *b;
5830
5831 ALL_BREAKPOINTS (b)
5832 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5833 return 1;
5834 return 0;
5835 }
5836
5837 int
5838 bpstat_causes_stop (bpstat bs)
5839 {
5840 for (; bs != NULL; bs = bs->next)
5841 if (bs->stop)
5842 return 1;
5843
5844 return 0;
5845 }
5846
5847 \f
5848
5849 /* Compute a string of spaces suitable to indent the next line
5850 so it starts at the position corresponding to the table column
5851 named COL_NAME in the currently active table of UIOUT. */
5852
5853 static char *
5854 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5855 {
5856 static char wrap_indent[80];
5857 int i, total_width, width, align;
5858 const char *text;
5859
5860 total_width = 0;
5861 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5862 {
5863 if (strcmp (text, col_name) == 0)
5864 {
5865 gdb_assert (total_width < sizeof wrap_indent);
5866 memset (wrap_indent, ' ', total_width);
5867 wrap_indent[total_width] = 0;
5868
5869 return wrap_indent;
5870 }
5871
5872 total_width += width + 1;
5873 }
5874
5875 return NULL;
5876 }
5877
5878 /* Determine if the locations of this breakpoint will have their conditions
5879 evaluated by the target, host or a mix of both. Returns the following:
5880
5881 "host": Host evals condition.
5882 "host or target": Host or Target evals condition.
5883 "target": Target evals condition.
5884 */
5885
5886 static const char *
5887 bp_condition_evaluator (struct breakpoint *b)
5888 {
5889 struct bp_location *bl;
5890 char host_evals = 0;
5891 char target_evals = 0;
5892
5893 if (!b)
5894 return NULL;
5895
5896 if (!is_breakpoint (b))
5897 return NULL;
5898
5899 if (gdb_evaluates_breakpoint_condition_p ()
5900 || !target_supports_evaluation_of_breakpoint_conditions ())
5901 return condition_evaluation_host;
5902
5903 for (bl = b->loc; bl; bl = bl->next)
5904 {
5905 if (bl->cond_bytecode)
5906 target_evals++;
5907 else
5908 host_evals++;
5909 }
5910
5911 if (host_evals && target_evals)
5912 return condition_evaluation_both;
5913 else if (target_evals)
5914 return condition_evaluation_target;
5915 else
5916 return condition_evaluation_host;
5917 }
5918
5919 /* Determine the breakpoint location's condition evaluator. This is
5920 similar to bp_condition_evaluator, but for locations. */
5921
5922 static const char *
5923 bp_location_condition_evaluator (struct bp_location *bl)
5924 {
5925 if (bl && !is_breakpoint (bl->owner))
5926 return NULL;
5927
5928 if (gdb_evaluates_breakpoint_condition_p ()
5929 || !target_supports_evaluation_of_breakpoint_conditions ())
5930 return condition_evaluation_host;
5931
5932 if (bl && bl->cond_bytecode)
5933 return condition_evaluation_target;
5934 else
5935 return condition_evaluation_host;
5936 }
5937
5938 /* Print the LOC location out of the list of B->LOC locations. */
5939
5940 static void
5941 print_breakpoint_location (struct breakpoint *b,
5942 struct bp_location *loc)
5943 {
5944 struct ui_out *uiout = current_uiout;
5945
5946 scoped_restore_current_program_space restore_pspace;
5947
5948 if (loc != NULL && loc->shlib_disabled)
5949 loc = NULL;
5950
5951 if (loc != NULL)
5952 set_current_program_space (loc->pspace);
5953
5954 if (b->display_canonical)
5955 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5956 else if (loc && loc->symtab)
5957 {
5958 const struct symbol *sym = loc->symbol;
5959
5960 if (sym == NULL)
5961 sym = find_pc_sect_function (loc->address, loc->section);
5962
5963 if (sym)
5964 {
5965 uiout->text ("in ");
5966 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
5967 uiout->text (" ");
5968 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5969 uiout->text ("at ");
5970 }
5971 uiout->field_string ("file",
5972 symtab_to_filename_for_display (loc->symtab));
5973 uiout->text (":");
5974
5975 if (uiout->is_mi_like_p ())
5976 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5977
5978 uiout->field_int ("line", loc->line_number);
5979 }
5980 else if (loc)
5981 {
5982 string_file stb;
5983
5984 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5985 demangle, "");
5986 uiout->field_stream ("at", stb);
5987 }
5988 else
5989 {
5990 uiout->field_string ("pending",
5991 event_location_to_string (b->location.get ()));
5992 /* If extra_string is available, it could be holding a condition
5993 or dprintf arguments. In either case, make sure it is printed,
5994 too, but only for non-MI streams. */
5995 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5996 {
5997 if (b->type == bp_dprintf)
5998 uiout->text (",");
5999 else
6000 uiout->text (" ");
6001 uiout->text (b->extra_string);
6002 }
6003 }
6004
6005 if (loc && is_breakpoint (b)
6006 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6007 && bp_condition_evaluator (b) == condition_evaluation_both)
6008 {
6009 uiout->text (" (");
6010 uiout->field_string ("evaluated-by",
6011 bp_location_condition_evaluator (loc));
6012 uiout->text (")");
6013 }
6014 }
6015
6016 static const char *
6017 bptype_string (enum bptype type)
6018 {
6019 struct ep_type_description
6020 {
6021 enum bptype type;
6022 const char *description;
6023 };
6024 static struct ep_type_description bptypes[] =
6025 {
6026 {bp_none, "?deleted?"},
6027 {bp_breakpoint, "breakpoint"},
6028 {bp_hardware_breakpoint, "hw breakpoint"},
6029 {bp_single_step, "sw single-step"},
6030 {bp_until, "until"},
6031 {bp_finish, "finish"},
6032 {bp_watchpoint, "watchpoint"},
6033 {bp_hardware_watchpoint, "hw watchpoint"},
6034 {bp_read_watchpoint, "read watchpoint"},
6035 {bp_access_watchpoint, "acc watchpoint"},
6036 {bp_longjmp, "longjmp"},
6037 {bp_longjmp_resume, "longjmp resume"},
6038 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6039 {bp_exception, "exception"},
6040 {bp_exception_resume, "exception resume"},
6041 {bp_step_resume, "step resume"},
6042 {bp_hp_step_resume, "high-priority step resume"},
6043 {bp_watchpoint_scope, "watchpoint scope"},
6044 {bp_call_dummy, "call dummy"},
6045 {bp_std_terminate, "std::terminate"},
6046 {bp_shlib_event, "shlib events"},
6047 {bp_thread_event, "thread events"},
6048 {bp_overlay_event, "overlay events"},
6049 {bp_longjmp_master, "longjmp master"},
6050 {bp_std_terminate_master, "std::terminate master"},
6051 {bp_exception_master, "exception master"},
6052 {bp_catchpoint, "catchpoint"},
6053 {bp_tracepoint, "tracepoint"},
6054 {bp_fast_tracepoint, "fast tracepoint"},
6055 {bp_static_tracepoint, "static tracepoint"},
6056 {bp_dprintf, "dprintf"},
6057 {bp_jit_event, "jit events"},
6058 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6059 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6060 };
6061
6062 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6063 || ((int) type != bptypes[(int) type].type))
6064 internal_error (__FILE__, __LINE__,
6065 _("bptypes table does not describe type #%d."),
6066 (int) type);
6067
6068 return bptypes[(int) type].description;
6069 }
6070
6071 /* For MI, output a field named 'thread-groups' with a list as the value.
6072 For CLI, prefix the list with the string 'inf'. */
6073
6074 static void
6075 output_thread_groups (struct ui_out *uiout,
6076 const char *field_name,
6077 VEC(int) *inf_num,
6078 int mi_only)
6079 {
6080 int is_mi = uiout->is_mi_like_p ();
6081 int inf;
6082 int i;
6083
6084 /* For backward compatibility, don't display inferiors in CLI unless
6085 there are several. Always display them for MI. */
6086 if (!is_mi && mi_only)
6087 return;
6088
6089 ui_out_emit_list list_emitter (uiout, field_name);
6090
6091 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6092 {
6093 if (is_mi)
6094 {
6095 char mi_group[10];
6096
6097 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6098 uiout->field_string (NULL, mi_group);
6099 }
6100 else
6101 {
6102 if (i == 0)
6103 uiout->text (" inf ");
6104 else
6105 uiout->text (", ");
6106
6107 uiout->text (plongest (inf));
6108 }
6109 }
6110 }
6111
6112 /* Print B to gdb_stdout. */
6113
6114 static void
6115 print_one_breakpoint_location (struct breakpoint *b,
6116 struct bp_location *loc,
6117 int loc_number,
6118 struct bp_location **last_loc,
6119 int allflag)
6120 {
6121 struct command_line *l;
6122 static char bpenables[] = "nynny";
6123
6124 struct ui_out *uiout = current_uiout;
6125 int header_of_multiple = 0;
6126 int part_of_multiple = (loc != NULL);
6127 struct value_print_options opts;
6128
6129 get_user_print_options (&opts);
6130
6131 gdb_assert (!loc || loc_number != 0);
6132 /* See comment in print_one_breakpoint concerning treatment of
6133 breakpoints with single disabled location. */
6134 if (loc == NULL
6135 && (b->loc != NULL
6136 && (b->loc->next != NULL || !b->loc->enabled)))
6137 header_of_multiple = 1;
6138 if (loc == NULL)
6139 loc = b->loc;
6140
6141 annotate_record ();
6142
6143 /* 1 */
6144 annotate_field (0);
6145 if (part_of_multiple)
6146 {
6147 char *formatted;
6148 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6149 uiout->field_string ("number", formatted);
6150 xfree (formatted);
6151 }
6152 else
6153 {
6154 uiout->field_int ("number", b->number);
6155 }
6156
6157 /* 2 */
6158 annotate_field (1);
6159 if (part_of_multiple)
6160 uiout->field_skip ("type");
6161 else
6162 uiout->field_string ("type", bptype_string (b->type));
6163
6164 /* 3 */
6165 annotate_field (2);
6166 if (part_of_multiple)
6167 uiout->field_skip ("disp");
6168 else
6169 uiout->field_string ("disp", bpdisp_text (b->disposition));
6170
6171
6172 /* 4 */
6173 annotate_field (3);
6174 if (part_of_multiple)
6175 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6176 else
6177 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6178 uiout->spaces (2);
6179
6180
6181 /* 5 and 6 */
6182 if (b->ops != NULL && b->ops->print_one != NULL)
6183 {
6184 /* Although the print_one can possibly print all locations,
6185 calling it here is not likely to get any nice result. So,
6186 make sure there's just one location. */
6187 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6188 b->ops->print_one (b, last_loc);
6189 }
6190 else
6191 switch (b->type)
6192 {
6193 case bp_none:
6194 internal_error (__FILE__, __LINE__,
6195 _("print_one_breakpoint: bp_none encountered\n"));
6196 break;
6197
6198 case bp_watchpoint:
6199 case bp_hardware_watchpoint:
6200 case bp_read_watchpoint:
6201 case bp_access_watchpoint:
6202 {
6203 struct watchpoint *w = (struct watchpoint *) b;
6204
6205 /* Field 4, the address, is omitted (which makes the columns
6206 not line up too nicely with the headers, but the effect
6207 is relatively readable). */
6208 if (opts.addressprint)
6209 uiout->field_skip ("addr");
6210 annotate_field (5);
6211 uiout->field_string ("what", w->exp_string);
6212 }
6213 break;
6214
6215 case bp_breakpoint:
6216 case bp_hardware_breakpoint:
6217 case bp_single_step:
6218 case bp_until:
6219 case bp_finish:
6220 case bp_longjmp:
6221 case bp_longjmp_resume:
6222 case bp_longjmp_call_dummy:
6223 case bp_exception:
6224 case bp_exception_resume:
6225 case bp_step_resume:
6226 case bp_hp_step_resume:
6227 case bp_watchpoint_scope:
6228 case bp_call_dummy:
6229 case bp_std_terminate:
6230 case bp_shlib_event:
6231 case bp_thread_event:
6232 case bp_overlay_event:
6233 case bp_longjmp_master:
6234 case bp_std_terminate_master:
6235 case bp_exception_master:
6236 case bp_tracepoint:
6237 case bp_fast_tracepoint:
6238 case bp_static_tracepoint:
6239 case bp_dprintf:
6240 case bp_jit_event:
6241 case bp_gnu_ifunc_resolver:
6242 case bp_gnu_ifunc_resolver_return:
6243 if (opts.addressprint)
6244 {
6245 annotate_field (4);
6246 if (header_of_multiple)
6247 uiout->field_string ("addr", "<MULTIPLE>");
6248 else if (b->loc == NULL || loc->shlib_disabled)
6249 uiout->field_string ("addr", "<PENDING>");
6250 else
6251 uiout->field_core_addr ("addr",
6252 loc->gdbarch, loc->address);
6253 }
6254 annotate_field (5);
6255 if (!header_of_multiple)
6256 print_breakpoint_location (b, loc);
6257 if (b->loc)
6258 *last_loc = b->loc;
6259 break;
6260 }
6261
6262
6263 if (loc != NULL && !header_of_multiple)
6264 {
6265 struct inferior *inf;
6266 VEC(int) *inf_num = NULL;
6267 int mi_only = 1;
6268
6269 ALL_INFERIORS (inf)
6270 {
6271 if (inf->pspace == loc->pspace)
6272 VEC_safe_push (int, inf_num, inf->num);
6273 }
6274
6275 /* For backward compatibility, don't display inferiors in CLI unless
6276 there are several. Always display for MI. */
6277 if (allflag
6278 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6279 && (number_of_program_spaces () > 1
6280 || number_of_inferiors () > 1)
6281 /* LOC is for existing B, it cannot be in
6282 moribund_locations and thus having NULL OWNER. */
6283 && loc->owner->type != bp_catchpoint))
6284 mi_only = 0;
6285 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6286 VEC_free (int, inf_num);
6287 }
6288
6289 if (!part_of_multiple)
6290 {
6291 if (b->thread != -1)
6292 {
6293 /* FIXME: This seems to be redundant and lost here; see the
6294 "stop only in" line a little further down. */
6295 uiout->text (" thread ");
6296 uiout->field_int ("thread", b->thread);
6297 }
6298 else if (b->task != 0)
6299 {
6300 uiout->text (" task ");
6301 uiout->field_int ("task", b->task);
6302 }
6303 }
6304
6305 uiout->text ("\n");
6306
6307 if (!part_of_multiple)
6308 b->ops->print_one_detail (b, uiout);
6309
6310 if (part_of_multiple && frame_id_p (b->frame_id))
6311 {
6312 annotate_field (6);
6313 uiout->text ("\tstop only in stack frame at ");
6314 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6315 the frame ID. */
6316 uiout->field_core_addr ("frame",
6317 b->gdbarch, b->frame_id.stack_addr);
6318 uiout->text ("\n");
6319 }
6320
6321 if (!part_of_multiple && b->cond_string)
6322 {
6323 annotate_field (7);
6324 if (is_tracepoint (b))
6325 uiout->text ("\ttrace only if ");
6326 else
6327 uiout->text ("\tstop only if ");
6328 uiout->field_string ("cond", b->cond_string);
6329
6330 /* Print whether the target is doing the breakpoint's condition
6331 evaluation. If GDB is doing the evaluation, don't print anything. */
6332 if (is_breakpoint (b)
6333 && breakpoint_condition_evaluation_mode ()
6334 == condition_evaluation_target)
6335 {
6336 uiout->text (" (");
6337 uiout->field_string ("evaluated-by",
6338 bp_condition_evaluator (b));
6339 uiout->text (" evals)");
6340 }
6341 uiout->text ("\n");
6342 }
6343
6344 if (!part_of_multiple && b->thread != -1)
6345 {
6346 /* FIXME should make an annotation for this. */
6347 uiout->text ("\tstop only in thread ");
6348 if (uiout->is_mi_like_p ())
6349 uiout->field_int ("thread", b->thread);
6350 else
6351 {
6352 struct thread_info *thr = find_thread_global_id (b->thread);
6353
6354 uiout->field_string ("thread", print_thread_id (thr));
6355 }
6356 uiout->text ("\n");
6357 }
6358
6359 if (!part_of_multiple)
6360 {
6361 if (b->hit_count)
6362 {
6363 /* FIXME should make an annotation for this. */
6364 if (is_catchpoint (b))
6365 uiout->text ("\tcatchpoint");
6366 else if (is_tracepoint (b))
6367 uiout->text ("\ttracepoint");
6368 else
6369 uiout->text ("\tbreakpoint");
6370 uiout->text (" already hit ");
6371 uiout->field_int ("times", b->hit_count);
6372 if (b->hit_count == 1)
6373 uiout->text (" time\n");
6374 else
6375 uiout->text (" times\n");
6376 }
6377 else
6378 {
6379 /* Output the count also if it is zero, but only if this is mi. */
6380 if (uiout->is_mi_like_p ())
6381 uiout->field_int ("times", b->hit_count);
6382 }
6383 }
6384
6385 if (!part_of_multiple && b->ignore_count)
6386 {
6387 annotate_field (8);
6388 uiout->text ("\tignore next ");
6389 uiout->field_int ("ignore", b->ignore_count);
6390 uiout->text (" hits\n");
6391 }
6392
6393 /* Note that an enable count of 1 corresponds to "enable once"
6394 behavior, which is reported by the combination of enablement and
6395 disposition, so we don't need to mention it here. */
6396 if (!part_of_multiple && b->enable_count > 1)
6397 {
6398 annotate_field (8);
6399 uiout->text ("\tdisable after ");
6400 /* Tweak the wording to clarify that ignore and enable counts
6401 are distinct, and have additive effect. */
6402 if (b->ignore_count)
6403 uiout->text ("additional ");
6404 else
6405 uiout->text ("next ");
6406 uiout->field_int ("enable", b->enable_count);
6407 uiout->text (" hits\n");
6408 }
6409
6410 if (!part_of_multiple && is_tracepoint (b))
6411 {
6412 struct tracepoint *tp = (struct tracepoint *) b;
6413
6414 if (tp->traceframe_usage)
6415 {
6416 uiout->text ("\ttrace buffer usage ");
6417 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6418 uiout->text (" bytes\n");
6419 }
6420 }
6421
6422 l = b->commands ? b->commands.get () : NULL;
6423 if (!part_of_multiple && l)
6424 {
6425 annotate_field (9);
6426 ui_out_emit_tuple tuple_emitter (uiout, "script");
6427 print_command_lines (uiout, l, 4);
6428 }
6429
6430 if (is_tracepoint (b))
6431 {
6432 struct tracepoint *t = (struct tracepoint *) b;
6433
6434 if (!part_of_multiple && t->pass_count)
6435 {
6436 annotate_field (10);
6437 uiout->text ("\tpass count ");
6438 uiout->field_int ("pass", t->pass_count);
6439 uiout->text (" \n");
6440 }
6441
6442 /* Don't display it when tracepoint or tracepoint location is
6443 pending. */
6444 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6445 {
6446 annotate_field (11);
6447
6448 if (uiout->is_mi_like_p ())
6449 uiout->field_string ("installed",
6450 loc->inserted ? "y" : "n");
6451 else
6452 {
6453 if (loc->inserted)
6454 uiout->text ("\t");
6455 else
6456 uiout->text ("\tnot ");
6457 uiout->text ("installed on target\n");
6458 }
6459 }
6460 }
6461
6462 if (uiout->is_mi_like_p () && !part_of_multiple)
6463 {
6464 if (is_watchpoint (b))
6465 {
6466 struct watchpoint *w = (struct watchpoint *) b;
6467
6468 uiout->field_string ("original-location", w->exp_string);
6469 }
6470 else if (b->location != NULL
6471 && event_location_to_string (b->location.get ()) != NULL)
6472 uiout->field_string ("original-location",
6473 event_location_to_string (b->location.get ()));
6474 }
6475 }
6476
6477 static void
6478 print_one_breakpoint (struct breakpoint *b,
6479 struct bp_location **last_loc,
6480 int allflag)
6481 {
6482 struct ui_out *uiout = current_uiout;
6483
6484 {
6485 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6486
6487 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6488 }
6489
6490 /* If this breakpoint has custom print function,
6491 it's already printed. Otherwise, print individual
6492 locations, if any. */
6493 if (b->ops == NULL || b->ops->print_one == NULL)
6494 {
6495 /* If breakpoint has a single location that is disabled, we
6496 print it as if it had several locations, since otherwise it's
6497 hard to represent "breakpoint enabled, location disabled"
6498 situation.
6499
6500 Note that while hardware watchpoints have several locations
6501 internally, that's not a property exposed to user. */
6502 if (b->loc
6503 && !is_hardware_watchpoint (b)
6504 && (b->loc->next || !b->loc->enabled))
6505 {
6506 struct bp_location *loc;
6507 int n = 1;
6508
6509 for (loc = b->loc; loc; loc = loc->next, ++n)
6510 {
6511 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6512 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6513 }
6514 }
6515 }
6516 }
6517
6518 static int
6519 breakpoint_address_bits (struct breakpoint *b)
6520 {
6521 int print_address_bits = 0;
6522 struct bp_location *loc;
6523
6524 /* Software watchpoints that aren't watching memory don't have an
6525 address to print. */
6526 if (is_no_memory_software_watchpoint (b))
6527 return 0;
6528
6529 for (loc = b->loc; loc; loc = loc->next)
6530 {
6531 int addr_bit;
6532
6533 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6534 if (addr_bit > print_address_bits)
6535 print_address_bits = addr_bit;
6536 }
6537
6538 return print_address_bits;
6539 }
6540
6541 /* See breakpoint.h. */
6542
6543 void
6544 print_breakpoint (breakpoint *b)
6545 {
6546 struct bp_location *dummy_loc = NULL;
6547 print_one_breakpoint (b, &dummy_loc, 0);
6548 }
6549
6550 /* Return true if this breakpoint was set by the user, false if it is
6551 internal or momentary. */
6552
6553 int
6554 user_breakpoint_p (struct breakpoint *b)
6555 {
6556 return b->number > 0;
6557 }
6558
6559 /* See breakpoint.h. */
6560
6561 int
6562 pending_breakpoint_p (struct breakpoint *b)
6563 {
6564 return b->loc == NULL;
6565 }
6566
6567 /* Print information on user settable breakpoint (watchpoint, etc)
6568 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6569 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6570 FILTER is non-NULL, call it on each breakpoint and only include the
6571 ones for which it returns non-zero. Return the total number of
6572 breakpoints listed. */
6573
6574 static int
6575 breakpoint_1 (const char *args, int allflag,
6576 int (*filter) (const struct breakpoint *))
6577 {
6578 struct breakpoint *b;
6579 struct bp_location *last_loc = NULL;
6580 int nr_printable_breakpoints;
6581 struct value_print_options opts;
6582 int print_address_bits = 0;
6583 int print_type_col_width = 14;
6584 struct ui_out *uiout = current_uiout;
6585
6586 get_user_print_options (&opts);
6587
6588 /* Compute the number of rows in the table, as well as the size
6589 required for address fields. */
6590 nr_printable_breakpoints = 0;
6591 ALL_BREAKPOINTS (b)
6592 {
6593 /* If we have a filter, only list the breakpoints it accepts. */
6594 if (filter && !filter (b))
6595 continue;
6596
6597 /* If we have an "args" string, it is a list of breakpoints to
6598 accept. Skip the others. */
6599 if (args != NULL && *args != '\0')
6600 {
6601 if (allflag && parse_and_eval_long (args) != b->number)
6602 continue;
6603 if (!allflag && !number_is_in_list (args, b->number))
6604 continue;
6605 }
6606
6607 if (allflag || user_breakpoint_p (b))
6608 {
6609 int addr_bit, type_len;
6610
6611 addr_bit = breakpoint_address_bits (b);
6612 if (addr_bit > print_address_bits)
6613 print_address_bits = addr_bit;
6614
6615 type_len = strlen (bptype_string (b->type));
6616 if (type_len > print_type_col_width)
6617 print_type_col_width = type_len;
6618
6619 nr_printable_breakpoints++;
6620 }
6621 }
6622
6623 {
6624 ui_out_emit_table table_emitter (uiout,
6625 opts.addressprint ? 6 : 5,
6626 nr_printable_breakpoints,
6627 "BreakpointTable");
6628
6629 if (nr_printable_breakpoints > 0)
6630 annotate_breakpoints_headers ();
6631 if (nr_printable_breakpoints > 0)
6632 annotate_field (0);
6633 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6634 if (nr_printable_breakpoints > 0)
6635 annotate_field (1);
6636 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6637 if (nr_printable_breakpoints > 0)
6638 annotate_field (2);
6639 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6640 if (nr_printable_breakpoints > 0)
6641 annotate_field (3);
6642 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6643 if (opts.addressprint)
6644 {
6645 if (nr_printable_breakpoints > 0)
6646 annotate_field (4);
6647 if (print_address_bits <= 32)
6648 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6649 else
6650 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6651 }
6652 if (nr_printable_breakpoints > 0)
6653 annotate_field (5);
6654 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6655 uiout->table_body ();
6656 if (nr_printable_breakpoints > 0)
6657 annotate_breakpoints_table ();
6658
6659 ALL_BREAKPOINTS (b)
6660 {
6661 QUIT;
6662 /* If we have a filter, only list the breakpoints it accepts. */
6663 if (filter && !filter (b))
6664 continue;
6665
6666 /* If we have an "args" string, it is a list of breakpoints to
6667 accept. Skip the others. */
6668
6669 if (args != NULL && *args != '\0')
6670 {
6671 if (allflag) /* maintenance info breakpoint */
6672 {
6673 if (parse_and_eval_long (args) != b->number)
6674 continue;
6675 }
6676 else /* all others */
6677 {
6678 if (!number_is_in_list (args, b->number))
6679 continue;
6680 }
6681 }
6682 /* We only print out user settable breakpoints unless the
6683 allflag is set. */
6684 if (allflag || user_breakpoint_p (b))
6685 print_one_breakpoint (b, &last_loc, allflag);
6686 }
6687 }
6688
6689 if (nr_printable_breakpoints == 0)
6690 {
6691 /* If there's a filter, let the caller decide how to report
6692 empty list. */
6693 if (!filter)
6694 {
6695 if (args == NULL || *args == '\0')
6696 uiout->message ("No breakpoints or watchpoints.\n");
6697 else
6698 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6699 args);
6700 }
6701 }
6702 else
6703 {
6704 if (last_loc && !server_command)
6705 set_next_address (last_loc->gdbarch, last_loc->address);
6706 }
6707
6708 /* FIXME? Should this be moved up so that it is only called when
6709 there have been breakpoints? */
6710 annotate_breakpoints_table_end ();
6711
6712 return nr_printable_breakpoints;
6713 }
6714
6715 /* Display the value of default-collect in a way that is generally
6716 compatible with the breakpoint list. */
6717
6718 static void
6719 default_collect_info (void)
6720 {
6721 struct ui_out *uiout = current_uiout;
6722
6723 /* If it has no value (which is frequently the case), say nothing; a
6724 message like "No default-collect." gets in user's face when it's
6725 not wanted. */
6726 if (!*default_collect)
6727 return;
6728
6729 /* The following phrase lines up nicely with per-tracepoint collect
6730 actions. */
6731 uiout->text ("default collect ");
6732 uiout->field_string ("default-collect", default_collect);
6733 uiout->text (" \n");
6734 }
6735
6736 static void
6737 info_breakpoints_command (char *args, int from_tty)
6738 {
6739 breakpoint_1 (args, 0, NULL);
6740
6741 default_collect_info ();
6742 }
6743
6744 static void
6745 info_watchpoints_command (char *args, int from_tty)
6746 {
6747 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6748 struct ui_out *uiout = current_uiout;
6749
6750 if (num_printed == 0)
6751 {
6752 if (args == NULL || *args == '\0')
6753 uiout->message ("No watchpoints.\n");
6754 else
6755 uiout->message ("No watchpoint matching '%s'.\n", args);
6756 }
6757 }
6758
6759 static void
6760 maintenance_info_breakpoints (const char *args, int from_tty)
6761 {
6762 breakpoint_1 (args, 1, NULL);
6763
6764 default_collect_info ();
6765 }
6766
6767 static int
6768 breakpoint_has_pc (struct breakpoint *b,
6769 struct program_space *pspace,
6770 CORE_ADDR pc, struct obj_section *section)
6771 {
6772 struct bp_location *bl = b->loc;
6773
6774 for (; bl; bl = bl->next)
6775 {
6776 if (bl->pspace == pspace
6777 && bl->address == pc
6778 && (!overlay_debugging || bl->section == section))
6779 return 1;
6780 }
6781 return 0;
6782 }
6783
6784 /* Print a message describing any user-breakpoints set at PC. This
6785 concerns with logical breakpoints, so we match program spaces, not
6786 address spaces. */
6787
6788 static void
6789 describe_other_breakpoints (struct gdbarch *gdbarch,
6790 struct program_space *pspace, CORE_ADDR pc,
6791 struct obj_section *section, int thread)
6792 {
6793 int others = 0;
6794 struct breakpoint *b;
6795
6796 ALL_BREAKPOINTS (b)
6797 others += (user_breakpoint_p (b)
6798 && breakpoint_has_pc (b, pspace, pc, section));
6799 if (others > 0)
6800 {
6801 if (others == 1)
6802 printf_filtered (_("Note: breakpoint "));
6803 else /* if (others == ???) */
6804 printf_filtered (_("Note: breakpoints "));
6805 ALL_BREAKPOINTS (b)
6806 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6807 {
6808 others--;
6809 printf_filtered ("%d", b->number);
6810 if (b->thread == -1 && thread != -1)
6811 printf_filtered (" (all threads)");
6812 else if (b->thread != -1)
6813 printf_filtered (" (thread %d)", b->thread);
6814 printf_filtered ("%s%s ",
6815 ((b->enable_state == bp_disabled
6816 || b->enable_state == bp_call_disabled)
6817 ? " (disabled)"
6818 : ""),
6819 (others > 1) ? ","
6820 : ((others == 1) ? " and" : ""));
6821 }
6822 printf_filtered (_("also set at pc "));
6823 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6824 printf_filtered (".\n");
6825 }
6826 }
6827 \f
6828
6829 /* Return true iff it is meaningful to use the address member of
6830 BPT locations. For some breakpoint types, the locations' address members
6831 are irrelevant and it makes no sense to attempt to compare them to other
6832 addresses (or use them for any other purpose either).
6833
6834 More specifically, each of the following breakpoint types will
6835 always have a zero valued location address and we don't want to mark
6836 breakpoints of any of these types to be a duplicate of an actual
6837 breakpoint location at address zero:
6838
6839 bp_watchpoint
6840 bp_catchpoint
6841
6842 */
6843
6844 static int
6845 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6846 {
6847 enum bptype type = bpt->type;
6848
6849 return (type != bp_watchpoint && type != bp_catchpoint);
6850 }
6851
6852 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6853 true if LOC1 and LOC2 represent the same watchpoint location. */
6854
6855 static int
6856 watchpoint_locations_match (struct bp_location *loc1,
6857 struct bp_location *loc2)
6858 {
6859 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6860 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6861
6862 /* Both of them must exist. */
6863 gdb_assert (w1 != NULL);
6864 gdb_assert (w2 != NULL);
6865
6866 /* If the target can evaluate the condition expression in hardware,
6867 then we we need to insert both watchpoints even if they are at
6868 the same place. Otherwise the watchpoint will only trigger when
6869 the condition of whichever watchpoint was inserted evaluates to
6870 true, not giving a chance for GDB to check the condition of the
6871 other watchpoint. */
6872 if ((w1->cond_exp
6873 && target_can_accel_watchpoint_condition (loc1->address,
6874 loc1->length,
6875 loc1->watchpoint_type,
6876 w1->cond_exp.get ()))
6877 || (w2->cond_exp
6878 && target_can_accel_watchpoint_condition (loc2->address,
6879 loc2->length,
6880 loc2->watchpoint_type,
6881 w2->cond_exp.get ())))
6882 return 0;
6883
6884 /* Note that this checks the owner's type, not the location's. In
6885 case the target does not support read watchpoints, but does
6886 support access watchpoints, we'll have bp_read_watchpoint
6887 watchpoints with hw_access locations. Those should be considered
6888 duplicates of hw_read locations. The hw_read locations will
6889 become hw_access locations later. */
6890 return (loc1->owner->type == loc2->owner->type
6891 && loc1->pspace->aspace == loc2->pspace->aspace
6892 && loc1->address == loc2->address
6893 && loc1->length == loc2->length);
6894 }
6895
6896 /* See breakpoint.h. */
6897
6898 int
6899 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6900 const address_space *aspace2, CORE_ADDR addr2)
6901 {
6902 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6903 || aspace1 == aspace2)
6904 && addr1 == addr2);
6905 }
6906
6907 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6908 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6909 matches ASPACE2. On targets that have global breakpoints, the address
6910 space doesn't really matter. */
6911
6912 static int
6913 breakpoint_address_match_range (const address_space *aspace1,
6914 CORE_ADDR addr1,
6915 int len1, const address_space *aspace2,
6916 CORE_ADDR addr2)
6917 {
6918 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6919 || aspace1 == aspace2)
6920 && addr2 >= addr1 && addr2 < addr1 + len1);
6921 }
6922
6923 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6924 a ranged breakpoint. In most targets, a match happens only if ASPACE
6925 matches the breakpoint's address space. On targets that have global
6926 breakpoints, the address space doesn't really matter. */
6927
6928 static int
6929 breakpoint_location_address_match (struct bp_location *bl,
6930 const address_space *aspace,
6931 CORE_ADDR addr)
6932 {
6933 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6934 aspace, addr)
6935 || (bl->length
6936 && breakpoint_address_match_range (bl->pspace->aspace,
6937 bl->address, bl->length,
6938 aspace, addr)));
6939 }
6940
6941 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6942 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6943 match happens only if ASPACE matches the breakpoint's address
6944 space. On targets that have global breakpoints, the address space
6945 doesn't really matter. */
6946
6947 static int
6948 breakpoint_location_address_range_overlap (struct bp_location *bl,
6949 const address_space *aspace,
6950 CORE_ADDR addr, int len)
6951 {
6952 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6953 || bl->pspace->aspace == aspace)
6954 {
6955 int bl_len = bl->length != 0 ? bl->length : 1;
6956
6957 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6958 return 1;
6959 }
6960 return 0;
6961 }
6962
6963 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6964 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6965 true, otherwise returns false. */
6966
6967 static int
6968 tracepoint_locations_match (struct bp_location *loc1,
6969 struct bp_location *loc2)
6970 {
6971 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6972 /* Since tracepoint locations are never duplicated with others', tracepoint
6973 locations at the same address of different tracepoints are regarded as
6974 different locations. */
6975 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6976 else
6977 return 0;
6978 }
6979
6980 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6981 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6982 represent the same location. */
6983
6984 static int
6985 breakpoint_locations_match (struct bp_location *loc1,
6986 struct bp_location *loc2)
6987 {
6988 int hw_point1, hw_point2;
6989
6990 /* Both of them must not be in moribund_locations. */
6991 gdb_assert (loc1->owner != NULL);
6992 gdb_assert (loc2->owner != NULL);
6993
6994 hw_point1 = is_hardware_watchpoint (loc1->owner);
6995 hw_point2 = is_hardware_watchpoint (loc2->owner);
6996
6997 if (hw_point1 != hw_point2)
6998 return 0;
6999 else if (hw_point1)
7000 return watchpoint_locations_match (loc1, loc2);
7001 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7002 return tracepoint_locations_match (loc1, loc2);
7003 else
7004 /* We compare bp_location.length in order to cover ranged breakpoints. */
7005 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7006 loc2->pspace->aspace, loc2->address)
7007 && loc1->length == loc2->length);
7008 }
7009
7010 static void
7011 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7012 int bnum, int have_bnum)
7013 {
7014 /* The longest string possibly returned by hex_string_custom
7015 is 50 chars. These must be at least that big for safety. */
7016 char astr1[64];
7017 char astr2[64];
7018
7019 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7020 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7021 if (have_bnum)
7022 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7023 bnum, astr1, astr2);
7024 else
7025 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7026 }
7027
7028 /* Adjust a breakpoint's address to account for architectural
7029 constraints on breakpoint placement. Return the adjusted address.
7030 Note: Very few targets require this kind of adjustment. For most
7031 targets, this function is simply the identity function. */
7032
7033 static CORE_ADDR
7034 adjust_breakpoint_address (struct gdbarch *gdbarch,
7035 CORE_ADDR bpaddr, enum bptype bptype)
7036 {
7037 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7038 {
7039 /* Very few targets need any kind of breakpoint adjustment. */
7040 return bpaddr;
7041 }
7042 else if (bptype == bp_watchpoint
7043 || bptype == bp_hardware_watchpoint
7044 || bptype == bp_read_watchpoint
7045 || bptype == bp_access_watchpoint
7046 || bptype == bp_catchpoint)
7047 {
7048 /* Watchpoints and the various bp_catch_* eventpoints should not
7049 have their addresses modified. */
7050 return bpaddr;
7051 }
7052 else if (bptype == bp_single_step)
7053 {
7054 /* Single-step breakpoints should not have their addresses
7055 modified. If there's any architectural constrain that
7056 applies to this address, then it should have already been
7057 taken into account when the breakpoint was created in the
7058 first place. If we didn't do this, stepping through e.g.,
7059 Thumb-2 IT blocks would break. */
7060 return bpaddr;
7061 }
7062 else
7063 {
7064 CORE_ADDR adjusted_bpaddr;
7065
7066 /* Some targets have architectural constraints on the placement
7067 of breakpoint instructions. Obtain the adjusted address. */
7068 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7069
7070 /* An adjusted breakpoint address can significantly alter
7071 a user's expectations. Print a warning if an adjustment
7072 is required. */
7073 if (adjusted_bpaddr != bpaddr)
7074 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7075
7076 return adjusted_bpaddr;
7077 }
7078 }
7079
7080 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7081 {
7082 bp_location *loc = this;
7083
7084 gdb_assert (ops != NULL);
7085
7086 loc->ops = ops;
7087 loc->owner = owner;
7088 loc->cond_bytecode = NULL;
7089 loc->shlib_disabled = 0;
7090 loc->enabled = 1;
7091
7092 switch (owner->type)
7093 {
7094 case bp_breakpoint:
7095 case bp_single_step:
7096 case bp_until:
7097 case bp_finish:
7098 case bp_longjmp:
7099 case bp_longjmp_resume:
7100 case bp_longjmp_call_dummy:
7101 case bp_exception:
7102 case bp_exception_resume:
7103 case bp_step_resume:
7104 case bp_hp_step_resume:
7105 case bp_watchpoint_scope:
7106 case bp_call_dummy:
7107 case bp_std_terminate:
7108 case bp_shlib_event:
7109 case bp_thread_event:
7110 case bp_overlay_event:
7111 case bp_jit_event:
7112 case bp_longjmp_master:
7113 case bp_std_terminate_master:
7114 case bp_exception_master:
7115 case bp_gnu_ifunc_resolver:
7116 case bp_gnu_ifunc_resolver_return:
7117 case bp_dprintf:
7118 loc->loc_type = bp_loc_software_breakpoint;
7119 mark_breakpoint_location_modified (loc);
7120 break;
7121 case bp_hardware_breakpoint:
7122 loc->loc_type = bp_loc_hardware_breakpoint;
7123 mark_breakpoint_location_modified (loc);
7124 break;
7125 case bp_hardware_watchpoint:
7126 case bp_read_watchpoint:
7127 case bp_access_watchpoint:
7128 loc->loc_type = bp_loc_hardware_watchpoint;
7129 break;
7130 case bp_watchpoint:
7131 case bp_catchpoint:
7132 case bp_tracepoint:
7133 case bp_fast_tracepoint:
7134 case bp_static_tracepoint:
7135 loc->loc_type = bp_loc_other;
7136 break;
7137 default:
7138 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7139 }
7140
7141 loc->refc = 1;
7142 }
7143
7144 /* Allocate a struct bp_location. */
7145
7146 static struct bp_location *
7147 allocate_bp_location (struct breakpoint *bpt)
7148 {
7149 return bpt->ops->allocate_location (bpt);
7150 }
7151
7152 static void
7153 free_bp_location (struct bp_location *loc)
7154 {
7155 loc->ops->dtor (loc);
7156 delete loc;
7157 }
7158
7159 /* Increment reference count. */
7160
7161 static void
7162 incref_bp_location (struct bp_location *bl)
7163 {
7164 ++bl->refc;
7165 }
7166
7167 /* Decrement reference count. If the reference count reaches 0,
7168 destroy the bp_location. Sets *BLP to NULL. */
7169
7170 static void
7171 decref_bp_location (struct bp_location **blp)
7172 {
7173 gdb_assert ((*blp)->refc > 0);
7174
7175 if (--(*blp)->refc == 0)
7176 free_bp_location (*blp);
7177 *blp = NULL;
7178 }
7179
7180 /* Add breakpoint B at the end of the global breakpoint chain. */
7181
7182 static breakpoint *
7183 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7184 {
7185 struct breakpoint *b1;
7186 struct breakpoint *result = b.get ();
7187
7188 /* Add this breakpoint to the end of the chain so that a list of
7189 breakpoints will come out in order of increasing numbers. */
7190
7191 b1 = breakpoint_chain;
7192 if (b1 == 0)
7193 breakpoint_chain = b.release ();
7194 else
7195 {
7196 while (b1->next)
7197 b1 = b1->next;
7198 b1->next = b.release ();
7199 }
7200
7201 return result;
7202 }
7203
7204 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7205
7206 static void
7207 init_raw_breakpoint_without_location (struct breakpoint *b,
7208 struct gdbarch *gdbarch,
7209 enum bptype bptype,
7210 const struct breakpoint_ops *ops)
7211 {
7212 gdb_assert (ops != NULL);
7213
7214 b->ops = ops;
7215 b->type = bptype;
7216 b->gdbarch = gdbarch;
7217 b->language = current_language->la_language;
7218 b->input_radix = input_radix;
7219 b->related_breakpoint = b;
7220 }
7221
7222 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7223 that has type BPTYPE and has no locations as yet. */
7224
7225 static struct breakpoint *
7226 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7227 enum bptype bptype,
7228 const struct breakpoint_ops *ops)
7229 {
7230 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7231
7232 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7233 return add_to_breakpoint_chain (std::move (b));
7234 }
7235
7236 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7237 resolutions should be made as the user specified the location explicitly
7238 enough. */
7239
7240 static void
7241 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7242 {
7243 gdb_assert (loc->owner != NULL);
7244
7245 if (loc->owner->type == bp_breakpoint
7246 || loc->owner->type == bp_hardware_breakpoint
7247 || is_tracepoint (loc->owner))
7248 {
7249 int is_gnu_ifunc;
7250 const char *function_name;
7251 CORE_ADDR func_addr;
7252
7253 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7254 &func_addr, NULL, &is_gnu_ifunc);
7255
7256 if (is_gnu_ifunc && !explicit_loc)
7257 {
7258 struct breakpoint *b = loc->owner;
7259
7260 gdb_assert (loc->pspace == current_program_space);
7261 if (gnu_ifunc_resolve_name (function_name,
7262 &loc->requested_address))
7263 {
7264 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7265 loc->address = adjust_breakpoint_address (loc->gdbarch,
7266 loc->requested_address,
7267 b->type);
7268 }
7269 else if (b->type == bp_breakpoint && b->loc == loc
7270 && loc->next == NULL && b->related_breakpoint == b)
7271 {
7272 /* Create only the whole new breakpoint of this type but do not
7273 mess more complicated breakpoints with multiple locations. */
7274 b->type = bp_gnu_ifunc_resolver;
7275 /* Remember the resolver's address for use by the return
7276 breakpoint. */
7277 loc->related_address = func_addr;
7278 }
7279 }
7280
7281 if (function_name)
7282 loc->function_name = xstrdup (function_name);
7283 }
7284 }
7285
7286 /* Attempt to determine architecture of location identified by SAL. */
7287 struct gdbarch *
7288 get_sal_arch (struct symtab_and_line sal)
7289 {
7290 if (sal.section)
7291 return get_objfile_arch (sal.section->objfile);
7292 if (sal.symtab)
7293 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7294
7295 return NULL;
7296 }
7297
7298 /* Low level routine for partially initializing a breakpoint of type
7299 BPTYPE. The newly created breakpoint's address, section, source
7300 file name, and line number are provided by SAL.
7301
7302 It is expected that the caller will complete the initialization of
7303 the newly created breakpoint struct as well as output any status
7304 information regarding the creation of a new breakpoint. */
7305
7306 static void
7307 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7308 struct symtab_and_line sal, enum bptype bptype,
7309 const struct breakpoint_ops *ops)
7310 {
7311 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7312
7313 add_location_to_breakpoint (b, &sal);
7314
7315 if (bptype != bp_catchpoint)
7316 gdb_assert (sal.pspace != NULL);
7317
7318 /* Store the program space that was used to set the breakpoint,
7319 except for ordinary breakpoints, which are independent of the
7320 program space. */
7321 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7322 b->pspace = sal.pspace;
7323 }
7324
7325 /* set_raw_breakpoint is a low level routine for allocating and
7326 partially initializing a breakpoint of type BPTYPE. The newly
7327 created breakpoint's address, section, source file name, and line
7328 number are provided by SAL. The newly created and partially
7329 initialized breakpoint is added to the breakpoint chain and
7330 is also returned as the value of this function.
7331
7332 It is expected that the caller will complete the initialization of
7333 the newly created breakpoint struct as well as output any status
7334 information regarding the creation of a new breakpoint. In
7335 particular, set_raw_breakpoint does NOT set the breakpoint
7336 number! Care should be taken to not allow an error to occur
7337 prior to completing the initialization of the breakpoint. If this
7338 should happen, a bogus breakpoint will be left on the chain. */
7339
7340 struct breakpoint *
7341 set_raw_breakpoint (struct gdbarch *gdbarch,
7342 struct symtab_and_line sal, enum bptype bptype,
7343 const struct breakpoint_ops *ops)
7344 {
7345 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7346
7347 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7348 return add_to_breakpoint_chain (std::move (b));
7349 }
7350
7351 /* Call this routine when stepping and nexting to enable a breakpoint
7352 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7353 initiated the operation. */
7354
7355 void
7356 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7357 {
7358 struct breakpoint *b, *b_tmp;
7359 int thread = tp->global_num;
7360
7361 /* To avoid having to rescan all objfile symbols at every step,
7362 we maintain a list of continually-inserted but always disabled
7363 longjmp "master" breakpoints. Here, we simply create momentary
7364 clones of those and enable them for the requested thread. */
7365 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7366 if (b->pspace == current_program_space
7367 && (b->type == bp_longjmp_master
7368 || b->type == bp_exception_master))
7369 {
7370 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7371 struct breakpoint *clone;
7372
7373 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7374 after their removal. */
7375 clone = momentary_breakpoint_from_master (b, type,
7376 &momentary_breakpoint_ops, 1);
7377 clone->thread = thread;
7378 }
7379
7380 tp->initiating_frame = frame;
7381 }
7382
7383 /* Delete all longjmp breakpoints from THREAD. */
7384 void
7385 delete_longjmp_breakpoint (int thread)
7386 {
7387 struct breakpoint *b, *b_tmp;
7388
7389 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7390 if (b->type == bp_longjmp || b->type == bp_exception)
7391 {
7392 if (b->thread == thread)
7393 delete_breakpoint (b);
7394 }
7395 }
7396
7397 void
7398 delete_longjmp_breakpoint_at_next_stop (int thread)
7399 {
7400 struct breakpoint *b, *b_tmp;
7401
7402 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7403 if (b->type == bp_longjmp || b->type == bp_exception)
7404 {
7405 if (b->thread == thread)
7406 b->disposition = disp_del_at_next_stop;
7407 }
7408 }
7409
7410 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7411 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7412 pointer to any of them. Return NULL if this system cannot place longjmp
7413 breakpoints. */
7414
7415 struct breakpoint *
7416 set_longjmp_breakpoint_for_call_dummy (void)
7417 {
7418 struct breakpoint *b, *retval = NULL;
7419
7420 ALL_BREAKPOINTS (b)
7421 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7422 {
7423 struct breakpoint *new_b;
7424
7425 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7426 &momentary_breakpoint_ops,
7427 1);
7428 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7429
7430 /* Link NEW_B into the chain of RETVAL breakpoints. */
7431
7432 gdb_assert (new_b->related_breakpoint == new_b);
7433 if (retval == NULL)
7434 retval = new_b;
7435 new_b->related_breakpoint = retval;
7436 while (retval->related_breakpoint != new_b->related_breakpoint)
7437 retval = retval->related_breakpoint;
7438 retval->related_breakpoint = new_b;
7439 }
7440
7441 return retval;
7442 }
7443
7444 /* Verify all existing dummy frames and their associated breakpoints for
7445 TP. Remove those which can no longer be found in the current frame
7446 stack.
7447
7448 You should call this function only at places where it is safe to currently
7449 unwind the whole stack. Failed stack unwind would discard live dummy
7450 frames. */
7451
7452 void
7453 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7454 {
7455 struct breakpoint *b, *b_tmp;
7456
7457 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7458 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7459 {
7460 struct breakpoint *dummy_b = b->related_breakpoint;
7461
7462 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7463 dummy_b = dummy_b->related_breakpoint;
7464 if (dummy_b->type != bp_call_dummy
7465 || frame_find_by_id (dummy_b->frame_id) != NULL)
7466 continue;
7467
7468 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7469
7470 while (b->related_breakpoint != b)
7471 {
7472 if (b_tmp == b->related_breakpoint)
7473 b_tmp = b->related_breakpoint->next;
7474 delete_breakpoint (b->related_breakpoint);
7475 }
7476 delete_breakpoint (b);
7477 }
7478 }
7479
7480 void
7481 enable_overlay_breakpoints (void)
7482 {
7483 struct breakpoint *b;
7484
7485 ALL_BREAKPOINTS (b)
7486 if (b->type == bp_overlay_event)
7487 {
7488 b->enable_state = bp_enabled;
7489 update_global_location_list (UGLL_MAY_INSERT);
7490 overlay_events_enabled = 1;
7491 }
7492 }
7493
7494 void
7495 disable_overlay_breakpoints (void)
7496 {
7497 struct breakpoint *b;
7498
7499 ALL_BREAKPOINTS (b)
7500 if (b->type == bp_overlay_event)
7501 {
7502 b->enable_state = bp_disabled;
7503 update_global_location_list (UGLL_DONT_INSERT);
7504 overlay_events_enabled = 0;
7505 }
7506 }
7507
7508 /* Set an active std::terminate breakpoint for each std::terminate
7509 master breakpoint. */
7510 void
7511 set_std_terminate_breakpoint (void)
7512 {
7513 struct breakpoint *b, *b_tmp;
7514
7515 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7516 if (b->pspace == current_program_space
7517 && b->type == bp_std_terminate_master)
7518 {
7519 momentary_breakpoint_from_master (b, bp_std_terminate,
7520 &momentary_breakpoint_ops, 1);
7521 }
7522 }
7523
7524 /* Delete all the std::terminate breakpoints. */
7525 void
7526 delete_std_terminate_breakpoint (void)
7527 {
7528 struct breakpoint *b, *b_tmp;
7529
7530 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7531 if (b->type == bp_std_terminate)
7532 delete_breakpoint (b);
7533 }
7534
7535 struct breakpoint *
7536 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7537 {
7538 struct breakpoint *b;
7539
7540 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7541 &internal_breakpoint_ops);
7542
7543 b->enable_state = bp_enabled;
7544 /* location has to be used or breakpoint_re_set will delete me. */
7545 b->location = new_address_location (b->loc->address, NULL, 0);
7546
7547 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7548
7549 return b;
7550 }
7551
7552 struct lang_and_radix
7553 {
7554 enum language lang;
7555 int radix;
7556 };
7557
7558 /* Create a breakpoint for JIT code registration and unregistration. */
7559
7560 struct breakpoint *
7561 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7562 {
7563 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7564 &internal_breakpoint_ops);
7565 }
7566
7567 /* Remove JIT code registration and unregistration breakpoint(s). */
7568
7569 void
7570 remove_jit_event_breakpoints (void)
7571 {
7572 struct breakpoint *b, *b_tmp;
7573
7574 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7575 if (b->type == bp_jit_event
7576 && b->loc->pspace == current_program_space)
7577 delete_breakpoint (b);
7578 }
7579
7580 void
7581 remove_solib_event_breakpoints (void)
7582 {
7583 struct breakpoint *b, *b_tmp;
7584
7585 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7586 if (b->type == bp_shlib_event
7587 && b->loc->pspace == current_program_space)
7588 delete_breakpoint (b);
7589 }
7590
7591 /* See breakpoint.h. */
7592
7593 void
7594 remove_solib_event_breakpoints_at_next_stop (void)
7595 {
7596 struct breakpoint *b, *b_tmp;
7597
7598 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7599 if (b->type == bp_shlib_event
7600 && b->loc->pspace == current_program_space)
7601 b->disposition = disp_del_at_next_stop;
7602 }
7603
7604 /* Helper for create_solib_event_breakpoint /
7605 create_and_insert_solib_event_breakpoint. Allows specifying which
7606 INSERT_MODE to pass through to update_global_location_list. */
7607
7608 static struct breakpoint *
7609 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7610 enum ugll_insert_mode insert_mode)
7611 {
7612 struct breakpoint *b;
7613
7614 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7615 &internal_breakpoint_ops);
7616 update_global_location_list_nothrow (insert_mode);
7617 return b;
7618 }
7619
7620 struct breakpoint *
7621 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7622 {
7623 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7624 }
7625
7626 /* See breakpoint.h. */
7627
7628 struct breakpoint *
7629 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7630 {
7631 struct breakpoint *b;
7632
7633 /* Explicitly tell update_global_location_list to insert
7634 locations. */
7635 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7636 if (!b->loc->inserted)
7637 {
7638 delete_breakpoint (b);
7639 return NULL;
7640 }
7641 return b;
7642 }
7643
7644 /* Disable any breakpoints that are on code in shared libraries. Only
7645 apply to enabled breakpoints, disabled ones can just stay disabled. */
7646
7647 void
7648 disable_breakpoints_in_shlibs (void)
7649 {
7650 struct bp_location *loc, **locp_tmp;
7651
7652 ALL_BP_LOCATIONS (loc, locp_tmp)
7653 {
7654 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7655 struct breakpoint *b = loc->owner;
7656
7657 /* We apply the check to all breakpoints, including disabled for
7658 those with loc->duplicate set. This is so that when breakpoint
7659 becomes enabled, or the duplicate is removed, gdb will try to
7660 insert all breakpoints. If we don't set shlib_disabled here,
7661 we'll try to insert those breakpoints and fail. */
7662 if (((b->type == bp_breakpoint)
7663 || (b->type == bp_jit_event)
7664 || (b->type == bp_hardware_breakpoint)
7665 || (is_tracepoint (b)))
7666 && loc->pspace == current_program_space
7667 && !loc->shlib_disabled
7668 && solib_name_from_address (loc->pspace, loc->address)
7669 )
7670 {
7671 loc->shlib_disabled = 1;
7672 }
7673 }
7674 }
7675
7676 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7677 notification of unloaded_shlib. Only apply to enabled breakpoints,
7678 disabled ones can just stay disabled. */
7679
7680 static void
7681 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7682 {
7683 struct bp_location *loc, **locp_tmp;
7684 int disabled_shlib_breaks = 0;
7685
7686 ALL_BP_LOCATIONS (loc, locp_tmp)
7687 {
7688 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7689 struct breakpoint *b = loc->owner;
7690
7691 if (solib->pspace == loc->pspace
7692 && !loc->shlib_disabled
7693 && (((b->type == bp_breakpoint
7694 || b->type == bp_jit_event
7695 || b->type == bp_hardware_breakpoint)
7696 && (loc->loc_type == bp_loc_hardware_breakpoint
7697 || loc->loc_type == bp_loc_software_breakpoint))
7698 || is_tracepoint (b))
7699 && solib_contains_address_p (solib, loc->address))
7700 {
7701 loc->shlib_disabled = 1;
7702 /* At this point, we cannot rely on remove_breakpoint
7703 succeeding so we must mark the breakpoint as not inserted
7704 to prevent future errors occurring in remove_breakpoints. */
7705 loc->inserted = 0;
7706
7707 /* This may cause duplicate notifications for the same breakpoint. */
7708 observer_notify_breakpoint_modified (b);
7709
7710 if (!disabled_shlib_breaks)
7711 {
7712 target_terminal::ours_for_output ();
7713 warning (_("Temporarily disabling breakpoints "
7714 "for unloaded shared library \"%s\""),
7715 solib->so_name);
7716 }
7717 disabled_shlib_breaks = 1;
7718 }
7719 }
7720 }
7721
7722 /* Disable any breakpoints and tracepoints in OBJFILE upon
7723 notification of free_objfile. Only apply to enabled breakpoints,
7724 disabled ones can just stay disabled. */
7725
7726 static void
7727 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7728 {
7729 struct breakpoint *b;
7730
7731 if (objfile == NULL)
7732 return;
7733
7734 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7735 managed by the user with add-symbol-file/remove-symbol-file.
7736 Similarly to how breakpoints in shared libraries are handled in
7737 response to "nosharedlibrary", mark breakpoints in such modules
7738 shlib_disabled so they end up uninserted on the next global
7739 location list update. Shared libraries not loaded by the user
7740 aren't handled here -- they're already handled in
7741 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7742 solib_unloaded observer. We skip objfiles that are not
7743 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7744 main objfile). */
7745 if ((objfile->flags & OBJF_SHARED) == 0
7746 || (objfile->flags & OBJF_USERLOADED) == 0)
7747 return;
7748
7749 ALL_BREAKPOINTS (b)
7750 {
7751 struct bp_location *loc;
7752 int bp_modified = 0;
7753
7754 if (!is_breakpoint (b) && !is_tracepoint (b))
7755 continue;
7756
7757 for (loc = b->loc; loc != NULL; loc = loc->next)
7758 {
7759 CORE_ADDR loc_addr = loc->address;
7760
7761 if (loc->loc_type != bp_loc_hardware_breakpoint
7762 && loc->loc_type != bp_loc_software_breakpoint)
7763 continue;
7764
7765 if (loc->shlib_disabled != 0)
7766 continue;
7767
7768 if (objfile->pspace != loc->pspace)
7769 continue;
7770
7771 if (loc->loc_type != bp_loc_hardware_breakpoint
7772 && loc->loc_type != bp_loc_software_breakpoint)
7773 continue;
7774
7775 if (is_addr_in_objfile (loc_addr, objfile))
7776 {
7777 loc->shlib_disabled = 1;
7778 /* At this point, we don't know whether the object was
7779 unmapped from the inferior or not, so leave the
7780 inserted flag alone. We'll handle failure to
7781 uninsert quietly, in case the object was indeed
7782 unmapped. */
7783
7784 mark_breakpoint_location_modified (loc);
7785
7786 bp_modified = 1;
7787 }
7788 }
7789
7790 if (bp_modified)
7791 observer_notify_breakpoint_modified (b);
7792 }
7793 }
7794
7795 /* FORK & VFORK catchpoints. */
7796
7797 /* An instance of this type is used to represent a fork or vfork
7798 catchpoint. A breakpoint is really of this type iff its ops pointer points
7799 to CATCH_FORK_BREAKPOINT_OPS. */
7800
7801 struct fork_catchpoint : public breakpoint
7802 {
7803 /* Process id of a child process whose forking triggered this
7804 catchpoint. This field is only valid immediately after this
7805 catchpoint has triggered. */
7806 ptid_t forked_inferior_pid;
7807 };
7808
7809 /* Implement the "insert" breakpoint_ops method for fork
7810 catchpoints. */
7811
7812 static int
7813 insert_catch_fork (struct bp_location *bl)
7814 {
7815 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7816 }
7817
7818 /* Implement the "remove" breakpoint_ops method for fork
7819 catchpoints. */
7820
7821 static int
7822 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7823 {
7824 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7825 }
7826
7827 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7828 catchpoints. */
7829
7830 static int
7831 breakpoint_hit_catch_fork (const struct bp_location *bl,
7832 const address_space *aspace, CORE_ADDR bp_addr,
7833 const struct target_waitstatus *ws)
7834 {
7835 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7836
7837 if (ws->kind != TARGET_WAITKIND_FORKED)
7838 return 0;
7839
7840 c->forked_inferior_pid = ws->value.related_pid;
7841 return 1;
7842 }
7843
7844 /* Implement the "print_it" breakpoint_ops method for fork
7845 catchpoints. */
7846
7847 static enum print_stop_action
7848 print_it_catch_fork (bpstat bs)
7849 {
7850 struct ui_out *uiout = current_uiout;
7851 struct breakpoint *b = bs->breakpoint_at;
7852 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7853
7854 annotate_catchpoint (b->number);
7855 maybe_print_thread_hit_breakpoint (uiout);
7856 if (b->disposition == disp_del)
7857 uiout->text ("Temporary catchpoint ");
7858 else
7859 uiout->text ("Catchpoint ");
7860 if (uiout->is_mi_like_p ())
7861 {
7862 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7863 uiout->field_string ("disp", bpdisp_text (b->disposition));
7864 }
7865 uiout->field_int ("bkptno", b->number);
7866 uiout->text (" (forked process ");
7867 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7868 uiout->text ("), ");
7869 return PRINT_SRC_AND_LOC;
7870 }
7871
7872 /* Implement the "print_one" breakpoint_ops method for fork
7873 catchpoints. */
7874
7875 static void
7876 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7877 {
7878 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7879 struct value_print_options opts;
7880 struct ui_out *uiout = current_uiout;
7881
7882 get_user_print_options (&opts);
7883
7884 /* Field 4, the address, is omitted (which makes the columns not
7885 line up too nicely with the headers, but the effect is relatively
7886 readable). */
7887 if (opts.addressprint)
7888 uiout->field_skip ("addr");
7889 annotate_field (5);
7890 uiout->text ("fork");
7891 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7892 {
7893 uiout->text (", process ");
7894 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7895 uiout->spaces (1);
7896 }
7897
7898 if (uiout->is_mi_like_p ())
7899 uiout->field_string ("catch-type", "fork");
7900 }
7901
7902 /* Implement the "print_mention" breakpoint_ops method for fork
7903 catchpoints. */
7904
7905 static void
7906 print_mention_catch_fork (struct breakpoint *b)
7907 {
7908 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7909 }
7910
7911 /* Implement the "print_recreate" breakpoint_ops method for fork
7912 catchpoints. */
7913
7914 static void
7915 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7916 {
7917 fprintf_unfiltered (fp, "catch fork");
7918 print_recreate_thread (b, fp);
7919 }
7920
7921 /* The breakpoint_ops structure to be used in fork catchpoints. */
7922
7923 static struct breakpoint_ops catch_fork_breakpoint_ops;
7924
7925 /* Implement the "insert" breakpoint_ops method for vfork
7926 catchpoints. */
7927
7928 static int
7929 insert_catch_vfork (struct bp_location *bl)
7930 {
7931 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7932 }
7933
7934 /* Implement the "remove" breakpoint_ops method for vfork
7935 catchpoints. */
7936
7937 static int
7938 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7939 {
7940 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7941 }
7942
7943 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7944 catchpoints. */
7945
7946 static int
7947 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7948 const address_space *aspace, CORE_ADDR bp_addr,
7949 const struct target_waitstatus *ws)
7950 {
7951 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7952
7953 if (ws->kind != TARGET_WAITKIND_VFORKED)
7954 return 0;
7955
7956 c->forked_inferior_pid = ws->value.related_pid;
7957 return 1;
7958 }
7959
7960 /* Implement the "print_it" breakpoint_ops method for vfork
7961 catchpoints. */
7962
7963 static enum print_stop_action
7964 print_it_catch_vfork (bpstat bs)
7965 {
7966 struct ui_out *uiout = current_uiout;
7967 struct breakpoint *b = bs->breakpoint_at;
7968 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7969
7970 annotate_catchpoint (b->number);
7971 maybe_print_thread_hit_breakpoint (uiout);
7972 if (b->disposition == disp_del)
7973 uiout->text ("Temporary catchpoint ");
7974 else
7975 uiout->text ("Catchpoint ");
7976 if (uiout->is_mi_like_p ())
7977 {
7978 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7979 uiout->field_string ("disp", bpdisp_text (b->disposition));
7980 }
7981 uiout->field_int ("bkptno", b->number);
7982 uiout->text (" (vforked process ");
7983 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7984 uiout->text ("), ");
7985 return PRINT_SRC_AND_LOC;
7986 }
7987
7988 /* Implement the "print_one" breakpoint_ops method for vfork
7989 catchpoints. */
7990
7991 static void
7992 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7993 {
7994 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7995 struct value_print_options opts;
7996 struct ui_out *uiout = current_uiout;
7997
7998 get_user_print_options (&opts);
7999 /* Field 4, the address, is omitted (which makes the columns not
8000 line up too nicely with the headers, but the effect is relatively
8001 readable). */
8002 if (opts.addressprint)
8003 uiout->field_skip ("addr");
8004 annotate_field (5);
8005 uiout->text ("vfork");
8006 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8007 {
8008 uiout->text (", process ");
8009 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8010 uiout->spaces (1);
8011 }
8012
8013 if (uiout->is_mi_like_p ())
8014 uiout->field_string ("catch-type", "vfork");
8015 }
8016
8017 /* Implement the "print_mention" breakpoint_ops method for vfork
8018 catchpoints. */
8019
8020 static void
8021 print_mention_catch_vfork (struct breakpoint *b)
8022 {
8023 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8024 }
8025
8026 /* Implement the "print_recreate" breakpoint_ops method for vfork
8027 catchpoints. */
8028
8029 static void
8030 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8031 {
8032 fprintf_unfiltered (fp, "catch vfork");
8033 print_recreate_thread (b, fp);
8034 }
8035
8036 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8037
8038 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8039
8040 /* An instance of this type is used to represent an solib catchpoint.
8041 A breakpoint is really of this type iff its ops pointer points to
8042 CATCH_SOLIB_BREAKPOINT_OPS. */
8043
8044 struct solib_catchpoint : public breakpoint
8045 {
8046 ~solib_catchpoint () override;
8047
8048 /* True for "catch load", false for "catch unload". */
8049 unsigned char is_load;
8050
8051 /* Regular expression to match, if any. COMPILED is only valid when
8052 REGEX is non-NULL. */
8053 char *regex;
8054 std::unique_ptr<compiled_regex> compiled;
8055 };
8056
8057 solib_catchpoint::~solib_catchpoint ()
8058 {
8059 xfree (this->regex);
8060 }
8061
8062 static int
8063 insert_catch_solib (struct bp_location *ignore)
8064 {
8065 return 0;
8066 }
8067
8068 static int
8069 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8070 {
8071 return 0;
8072 }
8073
8074 static int
8075 breakpoint_hit_catch_solib (const struct bp_location *bl,
8076 const address_space *aspace,
8077 CORE_ADDR bp_addr,
8078 const struct target_waitstatus *ws)
8079 {
8080 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8081 struct breakpoint *other;
8082
8083 if (ws->kind == TARGET_WAITKIND_LOADED)
8084 return 1;
8085
8086 ALL_BREAKPOINTS (other)
8087 {
8088 struct bp_location *other_bl;
8089
8090 if (other == bl->owner)
8091 continue;
8092
8093 if (other->type != bp_shlib_event)
8094 continue;
8095
8096 if (self->pspace != NULL && other->pspace != self->pspace)
8097 continue;
8098
8099 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8100 {
8101 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8102 return 1;
8103 }
8104 }
8105
8106 return 0;
8107 }
8108
8109 static void
8110 check_status_catch_solib (struct bpstats *bs)
8111 {
8112 struct solib_catchpoint *self
8113 = (struct solib_catchpoint *) bs->breakpoint_at;
8114 int ix;
8115
8116 if (self->is_load)
8117 {
8118 struct so_list *iter;
8119
8120 for (ix = 0;
8121 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8122 ix, iter);
8123 ++ix)
8124 {
8125 if (!self->regex
8126 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8127 return;
8128 }
8129 }
8130 else
8131 {
8132 char *iter;
8133
8134 for (ix = 0;
8135 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8136 ix, iter);
8137 ++ix)
8138 {
8139 if (!self->regex
8140 || self->compiled->exec (iter, 0, NULL, 0) == 0)
8141 return;
8142 }
8143 }
8144
8145 bs->stop = 0;
8146 bs->print_it = print_it_noop;
8147 }
8148
8149 static enum print_stop_action
8150 print_it_catch_solib (bpstat bs)
8151 {
8152 struct breakpoint *b = bs->breakpoint_at;
8153 struct ui_out *uiout = current_uiout;
8154
8155 annotate_catchpoint (b->number);
8156 maybe_print_thread_hit_breakpoint (uiout);
8157 if (b->disposition == disp_del)
8158 uiout->text ("Temporary catchpoint ");
8159 else
8160 uiout->text ("Catchpoint ");
8161 uiout->field_int ("bkptno", b->number);
8162 uiout->text ("\n");
8163 if (uiout->is_mi_like_p ())
8164 uiout->field_string ("disp", bpdisp_text (b->disposition));
8165 print_solib_event (1);
8166 return PRINT_SRC_AND_LOC;
8167 }
8168
8169 static void
8170 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8171 {
8172 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8173 struct value_print_options opts;
8174 struct ui_out *uiout = current_uiout;
8175 char *msg;
8176
8177 get_user_print_options (&opts);
8178 /* Field 4, the address, is omitted (which makes the columns not
8179 line up too nicely with the headers, but the effect is relatively
8180 readable). */
8181 if (opts.addressprint)
8182 {
8183 annotate_field (4);
8184 uiout->field_skip ("addr");
8185 }
8186
8187 annotate_field (5);
8188 if (self->is_load)
8189 {
8190 if (self->regex)
8191 msg = xstrprintf (_("load of library matching %s"), self->regex);
8192 else
8193 msg = xstrdup (_("load of library"));
8194 }
8195 else
8196 {
8197 if (self->regex)
8198 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8199 else
8200 msg = xstrdup (_("unload of library"));
8201 }
8202 uiout->field_string ("what", msg);
8203 xfree (msg);
8204
8205 if (uiout->is_mi_like_p ())
8206 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8207 }
8208
8209 static void
8210 print_mention_catch_solib (struct breakpoint *b)
8211 {
8212 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8213
8214 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8215 self->is_load ? "load" : "unload");
8216 }
8217
8218 static void
8219 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8220 {
8221 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8222
8223 fprintf_unfiltered (fp, "%s %s",
8224 b->disposition == disp_del ? "tcatch" : "catch",
8225 self->is_load ? "load" : "unload");
8226 if (self->regex)
8227 fprintf_unfiltered (fp, " %s", self->regex);
8228 fprintf_unfiltered (fp, "\n");
8229 }
8230
8231 static struct breakpoint_ops catch_solib_breakpoint_ops;
8232
8233 /* Shared helper function (MI and CLI) for creating and installing
8234 a shared object event catchpoint. If IS_LOAD is non-zero then
8235 the events to be caught are load events, otherwise they are
8236 unload events. If IS_TEMP is non-zero the catchpoint is a
8237 temporary one. If ENABLED is non-zero the catchpoint is
8238 created in an enabled state. */
8239
8240 void
8241 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8242 {
8243 struct gdbarch *gdbarch = get_current_arch ();
8244
8245 if (!arg)
8246 arg = "";
8247 arg = skip_spaces (arg);
8248
8249 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8250
8251 if (*arg != '\0')
8252 {
8253 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8254 _("Invalid regexp")));
8255 c->regex = xstrdup (arg);
8256 }
8257
8258 c->is_load = is_load;
8259 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8260 &catch_solib_breakpoint_ops);
8261
8262 c->enable_state = enabled ? bp_enabled : bp_disabled;
8263
8264 install_breakpoint (0, std::move (c), 1);
8265 }
8266
8267 /* A helper function that does all the work for "catch load" and
8268 "catch unload". */
8269
8270 static void
8271 catch_load_or_unload (char *arg, int from_tty, int is_load,
8272 struct cmd_list_element *command)
8273 {
8274 int tempflag;
8275 const int enabled = 1;
8276
8277 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8278
8279 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8280 }
8281
8282 static void
8283 catch_load_command_1 (char *arg, int from_tty,
8284 struct cmd_list_element *command)
8285 {
8286 catch_load_or_unload (arg, from_tty, 1, command);
8287 }
8288
8289 static void
8290 catch_unload_command_1 (char *arg, int from_tty,
8291 struct cmd_list_element *command)
8292 {
8293 catch_load_or_unload (arg, from_tty, 0, command);
8294 }
8295
8296 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8297 is non-zero, then make the breakpoint temporary. If COND_STRING is
8298 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8299 the breakpoint_ops structure associated to the catchpoint. */
8300
8301 void
8302 init_catchpoint (struct breakpoint *b,
8303 struct gdbarch *gdbarch, int tempflag,
8304 const char *cond_string,
8305 const struct breakpoint_ops *ops)
8306 {
8307 symtab_and_line sal;
8308 sal.pspace = current_program_space;
8309
8310 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8311
8312 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8313 b->disposition = tempflag ? disp_del : disp_donttouch;
8314 }
8315
8316 void
8317 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8318 {
8319 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8320 set_breakpoint_number (internal, b);
8321 if (is_tracepoint (b))
8322 set_tracepoint_count (breakpoint_count);
8323 if (!internal)
8324 mention (b);
8325 observer_notify_breakpoint_created (b);
8326
8327 if (update_gll)
8328 update_global_location_list (UGLL_MAY_INSERT);
8329 }
8330
8331 static void
8332 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8333 int tempflag, const char *cond_string,
8334 const struct breakpoint_ops *ops)
8335 {
8336 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8337
8338 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8339
8340 c->forked_inferior_pid = null_ptid;
8341
8342 install_breakpoint (0, std::move (c), 1);
8343 }
8344
8345 /* Exec catchpoints. */
8346
8347 /* An instance of this type is used to represent an exec catchpoint.
8348 A breakpoint is really of this type iff its ops pointer points to
8349 CATCH_EXEC_BREAKPOINT_OPS. */
8350
8351 struct exec_catchpoint : public breakpoint
8352 {
8353 ~exec_catchpoint () override;
8354
8355 /* Filename of a program whose exec triggered this catchpoint.
8356 This field is only valid immediately after this catchpoint has
8357 triggered. */
8358 char *exec_pathname;
8359 };
8360
8361 /* Exec catchpoint destructor. */
8362
8363 exec_catchpoint::~exec_catchpoint ()
8364 {
8365 xfree (this->exec_pathname);
8366 }
8367
8368 static int
8369 insert_catch_exec (struct bp_location *bl)
8370 {
8371 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8372 }
8373
8374 static int
8375 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8376 {
8377 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8378 }
8379
8380 static int
8381 breakpoint_hit_catch_exec (const struct bp_location *bl,
8382 const address_space *aspace, CORE_ADDR bp_addr,
8383 const struct target_waitstatus *ws)
8384 {
8385 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8386
8387 if (ws->kind != TARGET_WAITKIND_EXECD)
8388 return 0;
8389
8390 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8391 return 1;
8392 }
8393
8394 static enum print_stop_action
8395 print_it_catch_exec (bpstat bs)
8396 {
8397 struct ui_out *uiout = current_uiout;
8398 struct breakpoint *b = bs->breakpoint_at;
8399 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8400
8401 annotate_catchpoint (b->number);
8402 maybe_print_thread_hit_breakpoint (uiout);
8403 if (b->disposition == disp_del)
8404 uiout->text ("Temporary catchpoint ");
8405 else
8406 uiout->text ("Catchpoint ");
8407 if (uiout->is_mi_like_p ())
8408 {
8409 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8410 uiout->field_string ("disp", bpdisp_text (b->disposition));
8411 }
8412 uiout->field_int ("bkptno", b->number);
8413 uiout->text (" (exec'd ");
8414 uiout->field_string ("new-exec", c->exec_pathname);
8415 uiout->text ("), ");
8416
8417 return PRINT_SRC_AND_LOC;
8418 }
8419
8420 static void
8421 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8422 {
8423 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8424 struct value_print_options opts;
8425 struct ui_out *uiout = current_uiout;
8426
8427 get_user_print_options (&opts);
8428
8429 /* Field 4, the address, is omitted (which makes the columns
8430 not line up too nicely with the headers, but the effect
8431 is relatively readable). */
8432 if (opts.addressprint)
8433 uiout->field_skip ("addr");
8434 annotate_field (5);
8435 uiout->text ("exec");
8436 if (c->exec_pathname != NULL)
8437 {
8438 uiout->text (", program \"");
8439 uiout->field_string ("what", c->exec_pathname);
8440 uiout->text ("\" ");
8441 }
8442
8443 if (uiout->is_mi_like_p ())
8444 uiout->field_string ("catch-type", "exec");
8445 }
8446
8447 static void
8448 print_mention_catch_exec (struct breakpoint *b)
8449 {
8450 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8451 }
8452
8453 /* Implement the "print_recreate" breakpoint_ops method for exec
8454 catchpoints. */
8455
8456 static void
8457 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8458 {
8459 fprintf_unfiltered (fp, "catch exec");
8460 print_recreate_thread (b, fp);
8461 }
8462
8463 static struct breakpoint_ops catch_exec_breakpoint_ops;
8464
8465 static int
8466 hw_breakpoint_used_count (void)
8467 {
8468 int i = 0;
8469 struct breakpoint *b;
8470 struct bp_location *bl;
8471
8472 ALL_BREAKPOINTS (b)
8473 {
8474 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8475 for (bl = b->loc; bl; bl = bl->next)
8476 {
8477 /* Special types of hardware breakpoints may use more than
8478 one register. */
8479 i += b->ops->resources_needed (bl);
8480 }
8481 }
8482
8483 return i;
8484 }
8485
8486 /* Returns the resources B would use if it were a hardware
8487 watchpoint. */
8488
8489 static int
8490 hw_watchpoint_use_count (struct breakpoint *b)
8491 {
8492 int i = 0;
8493 struct bp_location *bl;
8494
8495 if (!breakpoint_enabled (b))
8496 return 0;
8497
8498 for (bl = b->loc; bl; bl = bl->next)
8499 {
8500 /* Special types of hardware watchpoints may use more than
8501 one register. */
8502 i += b->ops->resources_needed (bl);
8503 }
8504
8505 return i;
8506 }
8507
8508 /* Returns the sum the used resources of all hardware watchpoints of
8509 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8510 the sum of the used resources of all hardware watchpoints of other
8511 types _not_ TYPE. */
8512
8513 static int
8514 hw_watchpoint_used_count_others (struct breakpoint *except,
8515 enum bptype type, int *other_type_used)
8516 {
8517 int i = 0;
8518 struct breakpoint *b;
8519
8520 *other_type_used = 0;
8521 ALL_BREAKPOINTS (b)
8522 {
8523 if (b == except)
8524 continue;
8525 if (!breakpoint_enabled (b))
8526 continue;
8527
8528 if (b->type == type)
8529 i += hw_watchpoint_use_count (b);
8530 else if (is_hardware_watchpoint (b))
8531 *other_type_used = 1;
8532 }
8533
8534 return i;
8535 }
8536
8537 void
8538 disable_watchpoints_before_interactive_call_start (void)
8539 {
8540 struct breakpoint *b;
8541
8542 ALL_BREAKPOINTS (b)
8543 {
8544 if (is_watchpoint (b) && breakpoint_enabled (b))
8545 {
8546 b->enable_state = bp_call_disabled;
8547 update_global_location_list (UGLL_DONT_INSERT);
8548 }
8549 }
8550 }
8551
8552 void
8553 enable_watchpoints_after_interactive_call_stop (void)
8554 {
8555 struct breakpoint *b;
8556
8557 ALL_BREAKPOINTS (b)
8558 {
8559 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8560 {
8561 b->enable_state = bp_enabled;
8562 update_global_location_list (UGLL_MAY_INSERT);
8563 }
8564 }
8565 }
8566
8567 void
8568 disable_breakpoints_before_startup (void)
8569 {
8570 current_program_space->executing_startup = 1;
8571 update_global_location_list (UGLL_DONT_INSERT);
8572 }
8573
8574 void
8575 enable_breakpoints_after_startup (void)
8576 {
8577 current_program_space->executing_startup = 0;
8578 breakpoint_re_set ();
8579 }
8580
8581 /* Create a new single-step breakpoint for thread THREAD, with no
8582 locations. */
8583
8584 static struct breakpoint *
8585 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8586 {
8587 std::unique_ptr<breakpoint> b (new breakpoint ());
8588
8589 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8590 &momentary_breakpoint_ops);
8591
8592 b->disposition = disp_donttouch;
8593 b->frame_id = null_frame_id;
8594
8595 b->thread = thread;
8596 gdb_assert (b->thread != 0);
8597
8598 return add_to_breakpoint_chain (std::move (b));
8599 }
8600
8601 /* Set a momentary breakpoint of type TYPE at address specified by
8602 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8603 frame. */
8604
8605 struct breakpoint *
8606 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8607 struct frame_id frame_id, enum bptype type)
8608 {
8609 struct breakpoint *b;
8610
8611 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8612 tail-called one. */
8613 gdb_assert (!frame_id_artificial_p (frame_id));
8614
8615 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8616 b->enable_state = bp_enabled;
8617 b->disposition = disp_donttouch;
8618 b->frame_id = frame_id;
8619
8620 /* If we're debugging a multi-threaded program, then we want
8621 momentary breakpoints to be active in only a single thread of
8622 control. */
8623 if (in_thread_list (inferior_ptid))
8624 b->thread = ptid_to_global_thread_id (inferior_ptid);
8625
8626 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8627
8628 return b;
8629 }
8630
8631 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8632 The new breakpoint will have type TYPE, use OPS as its
8633 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8634
8635 static struct breakpoint *
8636 momentary_breakpoint_from_master (struct breakpoint *orig,
8637 enum bptype type,
8638 const struct breakpoint_ops *ops,
8639 int loc_enabled)
8640 {
8641 struct breakpoint *copy;
8642
8643 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8644 copy->loc = allocate_bp_location (copy);
8645 set_breakpoint_location_function (copy->loc, 1);
8646
8647 copy->loc->gdbarch = orig->loc->gdbarch;
8648 copy->loc->requested_address = orig->loc->requested_address;
8649 copy->loc->address = orig->loc->address;
8650 copy->loc->section = orig->loc->section;
8651 copy->loc->pspace = orig->loc->pspace;
8652 copy->loc->probe = orig->loc->probe;
8653 copy->loc->line_number = orig->loc->line_number;
8654 copy->loc->symtab = orig->loc->symtab;
8655 copy->loc->enabled = loc_enabled;
8656 copy->frame_id = orig->frame_id;
8657 copy->thread = orig->thread;
8658 copy->pspace = orig->pspace;
8659
8660 copy->enable_state = bp_enabled;
8661 copy->disposition = disp_donttouch;
8662 copy->number = internal_breakpoint_number--;
8663
8664 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8665 return copy;
8666 }
8667
8668 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8669 ORIG is NULL. */
8670
8671 struct breakpoint *
8672 clone_momentary_breakpoint (struct breakpoint *orig)
8673 {
8674 /* If there's nothing to clone, then return nothing. */
8675 if (orig == NULL)
8676 return NULL;
8677
8678 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8679 }
8680
8681 struct breakpoint *
8682 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8683 enum bptype type)
8684 {
8685 struct symtab_and_line sal;
8686
8687 sal = find_pc_line (pc, 0);
8688 sal.pc = pc;
8689 sal.section = find_pc_overlay (pc);
8690 sal.explicit_pc = 1;
8691
8692 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8693 }
8694 \f
8695
8696 /* Tell the user we have just set a breakpoint B. */
8697
8698 static void
8699 mention (struct breakpoint *b)
8700 {
8701 b->ops->print_mention (b);
8702 if (current_uiout->is_mi_like_p ())
8703 return;
8704 printf_filtered ("\n");
8705 }
8706 \f
8707
8708 static int bp_loc_is_permanent (struct bp_location *loc);
8709
8710 static struct bp_location *
8711 add_location_to_breakpoint (struct breakpoint *b,
8712 const struct symtab_and_line *sal)
8713 {
8714 struct bp_location *loc, **tmp;
8715 CORE_ADDR adjusted_address;
8716 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8717
8718 if (loc_gdbarch == NULL)
8719 loc_gdbarch = b->gdbarch;
8720
8721 /* Adjust the breakpoint's address prior to allocating a location.
8722 Once we call allocate_bp_location(), that mostly uninitialized
8723 location will be placed on the location chain. Adjustment of the
8724 breakpoint may cause target_read_memory() to be called and we do
8725 not want its scan of the location chain to find a breakpoint and
8726 location that's only been partially initialized. */
8727 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8728 sal->pc, b->type);
8729
8730 /* Sort the locations by their ADDRESS. */
8731 loc = allocate_bp_location (b);
8732 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8733 tmp = &((*tmp)->next))
8734 ;
8735 loc->next = *tmp;
8736 *tmp = loc;
8737
8738 loc->requested_address = sal->pc;
8739 loc->address = adjusted_address;
8740 loc->pspace = sal->pspace;
8741 loc->probe.probe = sal->probe;
8742 loc->probe.objfile = sal->objfile;
8743 gdb_assert (loc->pspace != NULL);
8744 loc->section = sal->section;
8745 loc->gdbarch = loc_gdbarch;
8746 loc->line_number = sal->line;
8747 loc->symtab = sal->symtab;
8748 loc->symbol = sal->symbol;
8749
8750 set_breakpoint_location_function (loc,
8751 sal->explicit_pc || sal->explicit_line);
8752
8753 /* While by definition, permanent breakpoints are already present in the
8754 code, we don't mark the location as inserted. Normally one would expect
8755 that GDB could rely on that breakpoint instruction to stop the program,
8756 thus removing the need to insert its own breakpoint, except that executing
8757 the breakpoint instruction can kill the target instead of reporting a
8758 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8759 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8760 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8761 breakpoint be inserted normally results in QEMU knowing about the GDB
8762 breakpoint, and thus trap before the breakpoint instruction is executed.
8763 (If GDB later needs to continue execution past the permanent breakpoint,
8764 it manually increments the PC, thus avoiding executing the breakpoint
8765 instruction.) */
8766 if (bp_loc_is_permanent (loc))
8767 loc->permanent = 1;
8768
8769 return loc;
8770 }
8771 \f
8772
8773 /* See breakpoint.h. */
8774
8775 int
8776 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8777 {
8778 int len;
8779 CORE_ADDR addr;
8780 const gdb_byte *bpoint;
8781 gdb_byte *target_mem;
8782
8783 addr = address;
8784 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8785
8786 /* Software breakpoints unsupported? */
8787 if (bpoint == NULL)
8788 return 0;
8789
8790 target_mem = (gdb_byte *) alloca (len);
8791
8792 /* Enable the automatic memory restoration from breakpoints while
8793 we read the memory. Otherwise we could say about our temporary
8794 breakpoints they are permanent. */
8795 scoped_restore restore_memory
8796 = make_scoped_restore_show_memory_breakpoints (0);
8797
8798 if (target_read_memory (address, target_mem, len) == 0
8799 && memcmp (target_mem, bpoint, len) == 0)
8800 return 1;
8801
8802 return 0;
8803 }
8804
8805 /* Return 1 if LOC is pointing to a permanent breakpoint,
8806 return 0 otherwise. */
8807
8808 static int
8809 bp_loc_is_permanent (struct bp_location *loc)
8810 {
8811 gdb_assert (loc != NULL);
8812
8813 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8814 attempt to read from the addresses the locations of these breakpoint types
8815 point to. program_breakpoint_here_p, below, will attempt to read
8816 memory. */
8817 if (!breakpoint_address_is_meaningful (loc->owner))
8818 return 0;
8819
8820 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8821 switch_to_program_space_and_thread (loc->pspace);
8822 return program_breakpoint_here_p (loc->gdbarch, loc->address);
8823 }
8824
8825 /* Build a command list for the dprintf corresponding to the current
8826 settings of the dprintf style options. */
8827
8828 static void
8829 update_dprintf_command_list (struct breakpoint *b)
8830 {
8831 char *dprintf_args = b->extra_string;
8832 char *printf_line = NULL;
8833
8834 if (!dprintf_args)
8835 return;
8836
8837 dprintf_args = skip_spaces (dprintf_args);
8838
8839 /* Allow a comma, as it may have terminated a location, but don't
8840 insist on it. */
8841 if (*dprintf_args == ',')
8842 ++dprintf_args;
8843 dprintf_args = skip_spaces (dprintf_args);
8844
8845 if (*dprintf_args != '"')
8846 error (_("Bad format string, missing '\"'."));
8847
8848 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8849 printf_line = xstrprintf ("printf %s", dprintf_args);
8850 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8851 {
8852 if (!dprintf_function)
8853 error (_("No function supplied for dprintf call"));
8854
8855 if (dprintf_channel && strlen (dprintf_channel) > 0)
8856 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8857 dprintf_function,
8858 dprintf_channel,
8859 dprintf_args);
8860 else
8861 printf_line = xstrprintf ("call (void) %s (%s)",
8862 dprintf_function,
8863 dprintf_args);
8864 }
8865 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8866 {
8867 if (target_can_run_breakpoint_commands ())
8868 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8869 else
8870 {
8871 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8872 printf_line = xstrprintf ("printf %s", dprintf_args);
8873 }
8874 }
8875 else
8876 internal_error (__FILE__, __LINE__,
8877 _("Invalid dprintf style."));
8878
8879 gdb_assert (printf_line != NULL);
8880 /* Manufacture a printf sequence. */
8881 {
8882 struct command_line *printf_cmd_line = XNEW (struct command_line);
8883
8884 printf_cmd_line->control_type = simple_control;
8885 printf_cmd_line->body_count = 0;
8886 printf_cmd_line->body_list = NULL;
8887 printf_cmd_line->next = NULL;
8888 printf_cmd_line->line = printf_line;
8889
8890 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
8891 }
8892 }
8893
8894 /* Update all dprintf commands, making their command lists reflect
8895 current style settings. */
8896
8897 static void
8898 update_dprintf_commands (char *args, int from_tty,
8899 struct cmd_list_element *c)
8900 {
8901 struct breakpoint *b;
8902
8903 ALL_BREAKPOINTS (b)
8904 {
8905 if (b->type == bp_dprintf)
8906 update_dprintf_command_list (b);
8907 }
8908 }
8909
8910 /* Create a breakpoint with SAL as location. Use LOCATION
8911 as a description of the location, and COND_STRING
8912 as condition expression. If LOCATION is NULL then create an
8913 "address location" from the address in the SAL. */
8914
8915 static void
8916 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8917 gdb::array_view<const symtab_and_line> sals,
8918 event_location_up &&location,
8919 gdb::unique_xmalloc_ptr<char> filter,
8920 gdb::unique_xmalloc_ptr<char> cond_string,
8921 gdb::unique_xmalloc_ptr<char> extra_string,
8922 enum bptype type, enum bpdisp disposition,
8923 int thread, int task, int ignore_count,
8924 const struct breakpoint_ops *ops, int from_tty,
8925 int enabled, int internal, unsigned flags,
8926 int display_canonical)
8927 {
8928 int i;
8929
8930 if (type == bp_hardware_breakpoint)
8931 {
8932 int target_resources_ok;
8933
8934 i = hw_breakpoint_used_count ();
8935 target_resources_ok =
8936 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8937 i + 1, 0);
8938 if (target_resources_ok == 0)
8939 error (_("No hardware breakpoint support in the target."));
8940 else if (target_resources_ok < 0)
8941 error (_("Hardware breakpoints used exceeds limit."));
8942 }
8943
8944 gdb_assert (!sals.empty ());
8945
8946 for (const auto &sal : sals)
8947 {
8948 struct bp_location *loc;
8949
8950 if (from_tty)
8951 {
8952 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8953 if (!loc_gdbarch)
8954 loc_gdbarch = gdbarch;
8955
8956 describe_other_breakpoints (loc_gdbarch,
8957 sal.pspace, sal.pc, sal.section, thread);
8958 }
8959
8960 if (&sal == &sals[0])
8961 {
8962 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8963 b->thread = thread;
8964 b->task = task;
8965
8966 b->cond_string = cond_string.release ();
8967 b->extra_string = extra_string.release ();
8968 b->ignore_count = ignore_count;
8969 b->enable_state = enabled ? bp_enabled : bp_disabled;
8970 b->disposition = disposition;
8971
8972 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8973 b->loc->inserted = 1;
8974
8975 if (type == bp_static_tracepoint)
8976 {
8977 struct tracepoint *t = (struct tracepoint *) b;
8978 struct static_tracepoint_marker marker;
8979
8980 if (strace_marker_p (b))
8981 {
8982 /* We already know the marker exists, otherwise, we
8983 wouldn't see a sal for it. */
8984 const char *p
8985 = &event_location_to_string (b->location.get ())[3];
8986 const char *endp;
8987 char *marker_str;
8988
8989 p = skip_spaces (p);
8990
8991 endp = skip_to_space (p);
8992
8993 marker_str = savestring (p, endp - p);
8994 t->static_trace_marker_id = marker_str;
8995
8996 printf_filtered (_("Probed static tracepoint "
8997 "marker \"%s\"\n"),
8998 t->static_trace_marker_id);
8999 }
9000 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9001 {
9002 t->static_trace_marker_id = xstrdup (marker.str_id);
9003 release_static_tracepoint_marker (&marker);
9004
9005 printf_filtered (_("Probed static tracepoint "
9006 "marker \"%s\"\n"),
9007 t->static_trace_marker_id);
9008 }
9009 else
9010 warning (_("Couldn't determine the static "
9011 "tracepoint marker to probe"));
9012 }
9013
9014 loc = b->loc;
9015 }
9016 else
9017 {
9018 loc = add_location_to_breakpoint (b, &sal);
9019 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9020 loc->inserted = 1;
9021 }
9022
9023 if (b->cond_string)
9024 {
9025 const char *arg = b->cond_string;
9026
9027 loc->cond = parse_exp_1 (&arg, loc->address,
9028 block_for_pc (loc->address), 0);
9029 if (*arg)
9030 error (_("Garbage '%s' follows condition"), arg);
9031 }
9032
9033 /* Dynamic printf requires and uses additional arguments on the
9034 command line, otherwise it's an error. */
9035 if (type == bp_dprintf)
9036 {
9037 if (b->extra_string)
9038 update_dprintf_command_list (b);
9039 else
9040 error (_("Format string required"));
9041 }
9042 else if (b->extra_string)
9043 error (_("Garbage '%s' at end of command"), b->extra_string);
9044 }
9045
9046 b->display_canonical = display_canonical;
9047 if (location != NULL)
9048 b->location = std::move (location);
9049 else
9050 b->location = new_address_location (b->loc->address, NULL, 0);
9051 b->filter = filter.release ();
9052 }
9053
9054 static void
9055 create_breakpoint_sal (struct gdbarch *gdbarch,
9056 gdb::array_view<const symtab_and_line> sals,
9057 event_location_up &&location,
9058 gdb::unique_xmalloc_ptr<char> filter,
9059 gdb::unique_xmalloc_ptr<char> cond_string,
9060 gdb::unique_xmalloc_ptr<char> extra_string,
9061 enum bptype type, enum bpdisp disposition,
9062 int thread, int task, int ignore_count,
9063 const struct breakpoint_ops *ops, int from_tty,
9064 int enabled, int internal, unsigned flags,
9065 int display_canonical)
9066 {
9067 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9068
9069 init_breakpoint_sal (b.get (), gdbarch,
9070 sals, std::move (location),
9071 std::move (filter),
9072 std::move (cond_string),
9073 std::move (extra_string),
9074 type, disposition,
9075 thread, task, ignore_count,
9076 ops, from_tty,
9077 enabled, internal, flags,
9078 display_canonical);
9079
9080 install_breakpoint (internal, std::move (b), 0);
9081 }
9082
9083 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9084 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9085 value. COND_STRING, if not NULL, specified the condition to be
9086 used for all breakpoints. Essentially the only case where
9087 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9088 function. In that case, it's still not possible to specify
9089 separate conditions for different overloaded functions, so
9090 we take just a single condition string.
9091
9092 NOTE: If the function succeeds, the caller is expected to cleanup
9093 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9094 array contents). If the function fails (error() is called), the
9095 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9096 COND and SALS arrays and each of those arrays contents. */
9097
9098 static void
9099 create_breakpoints_sal (struct gdbarch *gdbarch,
9100 struct linespec_result *canonical,
9101 gdb::unique_xmalloc_ptr<char> cond_string,
9102 gdb::unique_xmalloc_ptr<char> extra_string,
9103 enum bptype type, enum bpdisp disposition,
9104 int thread, int task, int ignore_count,
9105 const struct breakpoint_ops *ops, int from_tty,
9106 int enabled, int internal, unsigned flags)
9107 {
9108 if (canonical->pre_expanded)
9109 gdb_assert (canonical->lsals.size () == 1);
9110
9111 for (const auto &lsal : canonical->lsals)
9112 {
9113 /* Note that 'location' can be NULL in the case of a plain
9114 'break', without arguments. */
9115 event_location_up location
9116 = (canonical->location != NULL
9117 ? copy_event_location (canonical->location.get ()) : NULL);
9118 gdb::unique_xmalloc_ptr<char> filter_string
9119 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9120
9121 create_breakpoint_sal (gdbarch, lsal.sals,
9122 std::move (location),
9123 std::move (filter_string),
9124 std::move (cond_string),
9125 std::move (extra_string),
9126 type, disposition,
9127 thread, task, ignore_count, ops,
9128 from_tty, enabled, internal, flags,
9129 canonical->special_display);
9130 }
9131 }
9132
9133 /* Parse LOCATION which is assumed to be a SAL specification possibly
9134 followed by conditionals. On return, SALS contains an array of SAL
9135 addresses found. LOCATION points to the end of the SAL (for
9136 linespec locations).
9137
9138 The array and the line spec strings are allocated on the heap, it is
9139 the caller's responsibility to free them. */
9140
9141 static void
9142 parse_breakpoint_sals (const struct event_location *location,
9143 struct linespec_result *canonical)
9144 {
9145 struct symtab_and_line cursal;
9146
9147 if (event_location_type (location) == LINESPEC_LOCATION)
9148 {
9149 const char *address = get_linespec_location (location);
9150
9151 if (address == NULL)
9152 {
9153 /* The last displayed codepoint, if it's valid, is our default
9154 breakpoint address. */
9155 if (last_displayed_sal_is_valid ())
9156 {
9157 /* Set sal's pspace, pc, symtab, and line to the values
9158 corresponding to the last call to print_frame_info.
9159 Be sure to reinitialize LINE with NOTCURRENT == 0
9160 as the breakpoint line number is inappropriate otherwise.
9161 find_pc_line would adjust PC, re-set it back. */
9162 symtab_and_line sal = get_last_displayed_sal ();
9163 CORE_ADDR pc = sal.pc;
9164
9165 sal = find_pc_line (pc, 0);
9166
9167 /* "break" without arguments is equivalent to "break *PC"
9168 where PC is the last displayed codepoint's address. So
9169 make sure to set sal.explicit_pc to prevent GDB from
9170 trying to expand the list of sals to include all other
9171 instances with the same symtab and line. */
9172 sal.pc = pc;
9173 sal.explicit_pc = 1;
9174
9175 struct linespec_sals lsal;
9176 lsal.sals = {sal};
9177 lsal.canonical = NULL;
9178
9179 canonical->lsals.push_back (std::move (lsal));
9180 return;
9181 }
9182 else
9183 error (_("No default breakpoint address now."));
9184 }
9185 }
9186
9187 /* Force almost all breakpoints to be in terms of the
9188 current_source_symtab (which is decode_line_1's default).
9189 This should produce the results we want almost all of the
9190 time while leaving default_breakpoint_* alone.
9191
9192 ObjC: However, don't match an Objective-C method name which
9193 may have a '+' or '-' succeeded by a '['. */
9194 cursal = get_current_source_symtab_and_line ();
9195 if (last_displayed_sal_is_valid ())
9196 {
9197 const char *address = NULL;
9198
9199 if (event_location_type (location) == LINESPEC_LOCATION)
9200 address = get_linespec_location (location);
9201
9202 if (!cursal.symtab
9203 || (address != NULL
9204 && strchr ("+-", address[0]) != NULL
9205 && address[1] != '['))
9206 {
9207 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9208 get_last_displayed_symtab (),
9209 get_last_displayed_line (),
9210 canonical, NULL, NULL);
9211 return;
9212 }
9213 }
9214
9215 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9216 cursal.symtab, cursal.line, canonical, NULL, NULL);
9217 }
9218
9219
9220 /* Convert each SAL into a real PC. Verify that the PC can be
9221 inserted as a breakpoint. If it can't throw an error. */
9222
9223 static void
9224 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9225 {
9226 for (auto &sal : sals)
9227 resolve_sal_pc (&sal);
9228 }
9229
9230 /* Fast tracepoints may have restrictions on valid locations. For
9231 instance, a fast tracepoint using a jump instead of a trap will
9232 likely have to overwrite more bytes than a trap would, and so can
9233 only be placed where the instruction is longer than the jump, or a
9234 multi-instruction sequence does not have a jump into the middle of
9235 it, etc. */
9236
9237 static void
9238 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9239 gdb::array_view<const symtab_and_line> sals)
9240 {
9241 int rslt;
9242 char *msg;
9243 struct cleanup *old_chain;
9244
9245 for (const auto &sal : sals)
9246 {
9247 struct gdbarch *sarch;
9248
9249 sarch = get_sal_arch (sal);
9250 /* We fall back to GDBARCH if there is no architecture
9251 associated with SAL. */
9252 if (sarch == NULL)
9253 sarch = gdbarch;
9254 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
9255 old_chain = make_cleanup (xfree, msg);
9256
9257 if (!rslt)
9258 error (_("May not have a fast tracepoint at %s%s"),
9259 paddress (sarch, sal.pc), (msg ? msg : ""));
9260
9261 do_cleanups (old_chain);
9262 }
9263 }
9264
9265 /* Given TOK, a string specification of condition and thread, as
9266 accepted by the 'break' command, extract the condition
9267 string and thread number and set *COND_STRING and *THREAD.
9268 PC identifies the context at which the condition should be parsed.
9269 If no condition is found, *COND_STRING is set to NULL.
9270 If no thread is found, *THREAD is set to -1. */
9271
9272 static void
9273 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9274 char **cond_string, int *thread, int *task,
9275 char **rest)
9276 {
9277 *cond_string = NULL;
9278 *thread = -1;
9279 *task = 0;
9280 *rest = NULL;
9281
9282 while (tok && *tok)
9283 {
9284 const char *end_tok;
9285 int toklen;
9286 const char *cond_start = NULL;
9287 const char *cond_end = NULL;
9288
9289 tok = skip_spaces (tok);
9290
9291 if ((*tok == '"' || *tok == ',') && rest)
9292 {
9293 *rest = savestring (tok, strlen (tok));
9294 return;
9295 }
9296
9297 end_tok = skip_to_space (tok);
9298
9299 toklen = end_tok - tok;
9300
9301 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9302 {
9303 tok = cond_start = end_tok + 1;
9304 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9305 cond_end = tok;
9306 *cond_string = savestring (cond_start, cond_end - cond_start);
9307 }
9308 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9309 {
9310 const char *tmptok;
9311 struct thread_info *thr;
9312
9313 tok = end_tok + 1;
9314 thr = parse_thread_id (tok, &tmptok);
9315 if (tok == tmptok)
9316 error (_("Junk after thread keyword."));
9317 *thread = thr->global_num;
9318 tok = tmptok;
9319 }
9320 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9321 {
9322 char *tmptok;
9323
9324 tok = end_tok + 1;
9325 *task = strtol (tok, &tmptok, 0);
9326 if (tok == tmptok)
9327 error (_("Junk after task keyword."));
9328 if (!valid_task_id (*task))
9329 error (_("Unknown task %d."), *task);
9330 tok = tmptok;
9331 }
9332 else if (rest)
9333 {
9334 *rest = savestring (tok, strlen (tok));
9335 return;
9336 }
9337 else
9338 error (_("Junk at end of arguments."));
9339 }
9340 }
9341
9342 /* Decode a static tracepoint marker spec. */
9343
9344 static std::vector<symtab_and_line>
9345 decode_static_tracepoint_spec (const char **arg_p)
9346 {
9347 VEC(static_tracepoint_marker_p) *markers = NULL;
9348 const char *p = &(*arg_p)[3];
9349 const char *endp;
9350 int i;
9351
9352 p = skip_spaces (p);
9353
9354 endp = skip_to_space (p);
9355
9356 std::string marker_str (p, endp - p);
9357
9358 markers = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9359 if (VEC_empty(static_tracepoint_marker_p, markers))
9360 error (_("No known static tracepoint marker named %s"),
9361 marker_str.c_str ());
9362
9363 std::vector<symtab_and_line> sals;
9364 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
9365
9366 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
9367 {
9368 struct static_tracepoint_marker *marker;
9369
9370 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9371
9372 symtab_and_line sal = find_pc_line (marker->address, 0);
9373 sal.pc = marker->address;
9374 sals.push_back (sal);
9375
9376 release_static_tracepoint_marker (marker);
9377 }
9378
9379 *arg_p = endp;
9380 return sals;
9381 }
9382
9383 /* See breakpoint.h. */
9384
9385 int
9386 create_breakpoint (struct gdbarch *gdbarch,
9387 const struct event_location *location,
9388 const char *cond_string,
9389 int thread, const char *extra_string,
9390 int parse_extra,
9391 int tempflag, enum bptype type_wanted,
9392 int ignore_count,
9393 enum auto_boolean pending_break_support,
9394 const struct breakpoint_ops *ops,
9395 int from_tty, int enabled, int internal,
9396 unsigned flags)
9397 {
9398 struct linespec_result canonical;
9399 struct cleanup *bkpt_chain = NULL;
9400 int pending = 0;
9401 int task = 0;
9402 int prev_bkpt_count = breakpoint_count;
9403
9404 gdb_assert (ops != NULL);
9405
9406 /* If extra_string isn't useful, set it to NULL. */
9407 if (extra_string != NULL && *extra_string == '\0')
9408 extra_string = NULL;
9409
9410 TRY
9411 {
9412 ops->create_sals_from_location (location, &canonical, type_wanted);
9413 }
9414 CATCH (e, RETURN_MASK_ERROR)
9415 {
9416 /* If caller is interested in rc value from parse, set
9417 value. */
9418 if (e.error == NOT_FOUND_ERROR)
9419 {
9420 /* If pending breakpoint support is turned off, throw
9421 error. */
9422
9423 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9424 throw_exception (e);
9425
9426 exception_print (gdb_stderr, e);
9427
9428 /* If pending breakpoint support is auto query and the user
9429 selects no, then simply return the error code. */
9430 if (pending_break_support == AUTO_BOOLEAN_AUTO
9431 && !nquery (_("Make %s pending on future shared library load? "),
9432 bptype_string (type_wanted)))
9433 return 0;
9434
9435 /* At this point, either the user was queried about setting
9436 a pending breakpoint and selected yes, or pending
9437 breakpoint behavior is on and thus a pending breakpoint
9438 is defaulted on behalf of the user. */
9439 pending = 1;
9440 }
9441 else
9442 throw_exception (e);
9443 }
9444 END_CATCH
9445
9446 if (!pending && canonical.lsals.empty ())
9447 return 0;
9448
9449 /* ----------------------------- SNIP -----------------------------
9450 Anything added to the cleanup chain beyond this point is assumed
9451 to be part of a breakpoint. If the breakpoint create succeeds
9452 then the memory is not reclaimed. */
9453 bkpt_chain = make_cleanup (null_cleanup, 0);
9454
9455 /* Resolve all line numbers to PC's and verify that the addresses
9456 are ok for the target. */
9457 if (!pending)
9458 {
9459 for (auto &lsal : canonical.lsals)
9460 breakpoint_sals_to_pc (lsal.sals);
9461 }
9462
9463 /* Fast tracepoints may have additional restrictions on location. */
9464 if (!pending && type_wanted == bp_fast_tracepoint)
9465 {
9466 for (const auto &lsal : canonical.lsals)
9467 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9468 }
9469
9470 /* Verify that condition can be parsed, before setting any
9471 breakpoints. Allocate a separate condition expression for each
9472 breakpoint. */
9473 if (!pending)
9474 {
9475 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9476 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9477
9478 if (parse_extra)
9479 {
9480 char *rest;
9481 char *cond;
9482
9483 const linespec_sals &lsal = canonical.lsals[0];
9484
9485 /* Here we only parse 'arg' to separate condition
9486 from thread number, so parsing in context of first
9487 sal is OK. When setting the breakpoint we'll
9488 re-parse it in context of each sal. */
9489
9490 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9491 &cond, &thread, &task, &rest);
9492 cond_string_copy.reset (cond);
9493 extra_string_copy.reset (rest);
9494 }
9495 else
9496 {
9497 if (type_wanted != bp_dprintf
9498 && extra_string != NULL && *extra_string != '\0')
9499 error (_("Garbage '%s' at end of location"), extra_string);
9500
9501 /* Create a private copy of condition string. */
9502 if (cond_string)
9503 cond_string_copy.reset (xstrdup (cond_string));
9504 /* Create a private copy of any extra string. */
9505 if (extra_string)
9506 extra_string_copy.reset (xstrdup (extra_string));
9507 }
9508
9509 ops->create_breakpoints_sal (gdbarch, &canonical,
9510 std::move (cond_string_copy),
9511 std::move (extra_string_copy),
9512 type_wanted,
9513 tempflag ? disp_del : disp_donttouch,
9514 thread, task, ignore_count, ops,
9515 from_tty, enabled, internal, flags);
9516 }
9517 else
9518 {
9519 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9520
9521 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9522 b->location = copy_event_location (location);
9523
9524 if (parse_extra)
9525 b->cond_string = NULL;
9526 else
9527 {
9528 /* Create a private copy of condition string. */
9529 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9530 b->thread = thread;
9531 }
9532
9533 /* Create a private copy of any extra string. */
9534 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9535 b->ignore_count = ignore_count;
9536 b->disposition = tempflag ? disp_del : disp_donttouch;
9537 b->condition_not_parsed = 1;
9538 b->enable_state = enabled ? bp_enabled : bp_disabled;
9539 if ((type_wanted != bp_breakpoint
9540 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9541 b->pspace = current_program_space;
9542
9543 install_breakpoint (internal, std::move (b), 0);
9544 }
9545
9546 if (canonical.lsals.size () > 1)
9547 {
9548 warning (_("Multiple breakpoints were set.\nUse the "
9549 "\"delete\" command to delete unwanted breakpoints."));
9550 prev_breakpoint_count = prev_bkpt_count;
9551 }
9552
9553 /* That's it. Discard the cleanups for data inserted into the
9554 breakpoint. */
9555 discard_cleanups (bkpt_chain);
9556
9557 /* error call may happen here - have BKPT_CHAIN already discarded. */
9558 update_global_location_list (UGLL_MAY_INSERT);
9559
9560 return 1;
9561 }
9562
9563 /* Set a breakpoint.
9564 ARG is a string describing breakpoint address,
9565 condition, and thread.
9566 FLAG specifies if a breakpoint is hardware on,
9567 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9568 and BP_TEMPFLAG. */
9569
9570 static void
9571 break_command_1 (const char *arg, int flag, int from_tty)
9572 {
9573 int tempflag = flag & BP_TEMPFLAG;
9574 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9575 ? bp_hardware_breakpoint
9576 : bp_breakpoint);
9577 struct breakpoint_ops *ops;
9578
9579 event_location_up location = string_to_event_location (&arg, current_language);
9580
9581 /* Matching breakpoints on probes. */
9582 if (location != NULL
9583 && event_location_type (location.get ()) == PROBE_LOCATION)
9584 ops = &bkpt_probe_breakpoint_ops;
9585 else
9586 ops = &bkpt_breakpoint_ops;
9587
9588 create_breakpoint (get_current_arch (),
9589 location.get (),
9590 NULL, 0, arg, 1 /* parse arg */,
9591 tempflag, type_wanted,
9592 0 /* Ignore count */,
9593 pending_break_support,
9594 ops,
9595 from_tty,
9596 1 /* enabled */,
9597 0 /* internal */,
9598 0);
9599 }
9600
9601 /* Helper function for break_command_1 and disassemble_command. */
9602
9603 void
9604 resolve_sal_pc (struct symtab_and_line *sal)
9605 {
9606 CORE_ADDR pc;
9607
9608 if (sal->pc == 0 && sal->symtab != NULL)
9609 {
9610 if (!find_line_pc (sal->symtab, sal->line, &pc))
9611 error (_("No line %d in file \"%s\"."),
9612 sal->line, symtab_to_filename_for_display (sal->symtab));
9613 sal->pc = pc;
9614
9615 /* If this SAL corresponds to a breakpoint inserted using a line
9616 number, then skip the function prologue if necessary. */
9617 if (sal->explicit_line)
9618 skip_prologue_sal (sal);
9619 }
9620
9621 if (sal->section == 0 && sal->symtab != NULL)
9622 {
9623 const struct blockvector *bv;
9624 const struct block *b;
9625 struct symbol *sym;
9626
9627 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9628 SYMTAB_COMPUNIT (sal->symtab));
9629 if (bv != NULL)
9630 {
9631 sym = block_linkage_function (b);
9632 if (sym != NULL)
9633 {
9634 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9635 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9636 sym);
9637 }
9638 else
9639 {
9640 /* It really is worthwhile to have the section, so we'll
9641 just have to look harder. This case can be executed
9642 if we have line numbers but no functions (as can
9643 happen in assembly source). */
9644
9645 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9646 switch_to_program_space_and_thread (sal->pspace);
9647
9648 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9649 if (msym.minsym)
9650 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9651 }
9652 }
9653 }
9654 }
9655
9656 void
9657 break_command (char *arg, int from_tty)
9658 {
9659 break_command_1 (arg, 0, from_tty);
9660 }
9661
9662 void
9663 tbreak_command (char *arg, int from_tty)
9664 {
9665 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9666 }
9667
9668 static void
9669 hbreak_command (char *arg, int from_tty)
9670 {
9671 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9672 }
9673
9674 static void
9675 thbreak_command (char *arg, int from_tty)
9676 {
9677 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9678 }
9679
9680 static void
9681 stop_command (char *arg, int from_tty)
9682 {
9683 printf_filtered (_("Specify the type of breakpoint to set.\n\
9684 Usage: stop in <function | address>\n\
9685 stop at <line>\n"));
9686 }
9687
9688 static void
9689 stopin_command (const char *arg, int from_tty)
9690 {
9691 int badInput = 0;
9692
9693 if (arg == (char *) NULL)
9694 badInput = 1;
9695 else if (*arg != '*')
9696 {
9697 const char *argptr = arg;
9698 int hasColon = 0;
9699
9700 /* Look for a ':'. If this is a line number specification, then
9701 say it is bad, otherwise, it should be an address or
9702 function/method name. */
9703 while (*argptr && !hasColon)
9704 {
9705 hasColon = (*argptr == ':');
9706 argptr++;
9707 }
9708
9709 if (hasColon)
9710 badInput = (*argptr != ':'); /* Not a class::method */
9711 else
9712 badInput = isdigit (*arg); /* a simple line number */
9713 }
9714
9715 if (badInput)
9716 printf_filtered (_("Usage: stop in <function | address>\n"));
9717 else
9718 break_command_1 (arg, 0, from_tty);
9719 }
9720
9721 static void
9722 stopat_command (const char *arg, int from_tty)
9723 {
9724 int badInput = 0;
9725
9726 if (arg == (char *) NULL || *arg == '*') /* no line number */
9727 badInput = 1;
9728 else
9729 {
9730 const char *argptr = arg;
9731 int hasColon = 0;
9732
9733 /* Look for a ':'. If there is a '::' then get out, otherwise
9734 it is probably a line number. */
9735 while (*argptr && !hasColon)
9736 {
9737 hasColon = (*argptr == ':');
9738 argptr++;
9739 }
9740
9741 if (hasColon)
9742 badInput = (*argptr == ':'); /* we have class::method */
9743 else
9744 badInput = !isdigit (*arg); /* not a line number */
9745 }
9746
9747 if (badInput)
9748 printf_filtered (_("Usage: stop at <line>\n"));
9749 else
9750 break_command_1 (arg, 0, from_tty);
9751 }
9752
9753 /* The dynamic printf command is mostly like a regular breakpoint, but
9754 with a prewired command list consisting of a single output command,
9755 built from extra arguments supplied on the dprintf command
9756 line. */
9757
9758 static void
9759 dprintf_command (char *arg_in, int from_tty)
9760 {
9761 const char *arg = arg_in;
9762 event_location_up location = string_to_event_location (&arg, current_language);
9763
9764 /* If non-NULL, ARG should have been advanced past the location;
9765 the next character must be ','. */
9766 if (arg != NULL)
9767 {
9768 if (arg[0] != ',' || arg[1] == '\0')
9769 error (_("Format string required"));
9770 else
9771 {
9772 /* Skip the comma. */
9773 ++arg;
9774 }
9775 }
9776
9777 create_breakpoint (get_current_arch (),
9778 location.get (),
9779 NULL, 0, arg, 1 /* parse arg */,
9780 0, bp_dprintf,
9781 0 /* Ignore count */,
9782 pending_break_support,
9783 &dprintf_breakpoint_ops,
9784 from_tty,
9785 1 /* enabled */,
9786 0 /* internal */,
9787 0);
9788 }
9789
9790 static void
9791 agent_printf_command (char *arg, int from_tty)
9792 {
9793 error (_("May only run agent-printf on the target"));
9794 }
9795
9796 /* Implement the "breakpoint_hit" breakpoint_ops method for
9797 ranged breakpoints. */
9798
9799 static int
9800 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9801 const address_space *aspace,
9802 CORE_ADDR bp_addr,
9803 const struct target_waitstatus *ws)
9804 {
9805 if (ws->kind != TARGET_WAITKIND_STOPPED
9806 || ws->value.sig != GDB_SIGNAL_TRAP)
9807 return 0;
9808
9809 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9810 bl->length, aspace, bp_addr);
9811 }
9812
9813 /* Implement the "resources_needed" breakpoint_ops method for
9814 ranged breakpoints. */
9815
9816 static int
9817 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9818 {
9819 return target_ranged_break_num_registers ();
9820 }
9821
9822 /* Implement the "print_it" breakpoint_ops method for
9823 ranged breakpoints. */
9824
9825 static enum print_stop_action
9826 print_it_ranged_breakpoint (bpstat bs)
9827 {
9828 struct breakpoint *b = bs->breakpoint_at;
9829 struct bp_location *bl = b->loc;
9830 struct ui_out *uiout = current_uiout;
9831
9832 gdb_assert (b->type == bp_hardware_breakpoint);
9833
9834 /* Ranged breakpoints have only one location. */
9835 gdb_assert (bl && bl->next == NULL);
9836
9837 annotate_breakpoint (b->number);
9838
9839 maybe_print_thread_hit_breakpoint (uiout);
9840
9841 if (b->disposition == disp_del)
9842 uiout->text ("Temporary ranged breakpoint ");
9843 else
9844 uiout->text ("Ranged breakpoint ");
9845 if (uiout->is_mi_like_p ())
9846 {
9847 uiout->field_string ("reason",
9848 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9849 uiout->field_string ("disp", bpdisp_text (b->disposition));
9850 }
9851 uiout->field_int ("bkptno", b->number);
9852 uiout->text (", ");
9853
9854 return PRINT_SRC_AND_LOC;
9855 }
9856
9857 /* Implement the "print_one" breakpoint_ops method for
9858 ranged breakpoints. */
9859
9860 static void
9861 print_one_ranged_breakpoint (struct breakpoint *b,
9862 struct bp_location **last_loc)
9863 {
9864 struct bp_location *bl = b->loc;
9865 struct value_print_options opts;
9866 struct ui_out *uiout = current_uiout;
9867
9868 /* Ranged breakpoints have only one location. */
9869 gdb_assert (bl && bl->next == NULL);
9870
9871 get_user_print_options (&opts);
9872
9873 if (opts.addressprint)
9874 /* We don't print the address range here, it will be printed later
9875 by print_one_detail_ranged_breakpoint. */
9876 uiout->field_skip ("addr");
9877 annotate_field (5);
9878 print_breakpoint_location (b, bl);
9879 *last_loc = bl;
9880 }
9881
9882 /* Implement the "print_one_detail" breakpoint_ops method for
9883 ranged breakpoints. */
9884
9885 static void
9886 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9887 struct ui_out *uiout)
9888 {
9889 CORE_ADDR address_start, address_end;
9890 struct bp_location *bl = b->loc;
9891 string_file stb;
9892
9893 gdb_assert (bl);
9894
9895 address_start = bl->address;
9896 address_end = address_start + bl->length - 1;
9897
9898 uiout->text ("\taddress range: ");
9899 stb.printf ("[%s, %s]",
9900 print_core_address (bl->gdbarch, address_start),
9901 print_core_address (bl->gdbarch, address_end));
9902 uiout->field_stream ("addr", stb);
9903 uiout->text ("\n");
9904 }
9905
9906 /* Implement the "print_mention" breakpoint_ops method for
9907 ranged breakpoints. */
9908
9909 static void
9910 print_mention_ranged_breakpoint (struct breakpoint *b)
9911 {
9912 struct bp_location *bl = b->loc;
9913 struct ui_out *uiout = current_uiout;
9914
9915 gdb_assert (bl);
9916 gdb_assert (b->type == bp_hardware_breakpoint);
9917
9918 if (uiout->is_mi_like_p ())
9919 return;
9920
9921 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9922 b->number, paddress (bl->gdbarch, bl->address),
9923 paddress (bl->gdbarch, bl->address + bl->length - 1));
9924 }
9925
9926 /* Implement the "print_recreate" breakpoint_ops method for
9927 ranged breakpoints. */
9928
9929 static void
9930 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9931 {
9932 fprintf_unfiltered (fp, "break-range %s, %s",
9933 event_location_to_string (b->location.get ()),
9934 event_location_to_string (b->location_range_end.get ()));
9935 print_recreate_thread (b, fp);
9936 }
9937
9938 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9939
9940 static struct breakpoint_ops ranged_breakpoint_ops;
9941
9942 /* Find the address where the end of the breakpoint range should be
9943 placed, given the SAL of the end of the range. This is so that if
9944 the user provides a line number, the end of the range is set to the
9945 last instruction of the given line. */
9946
9947 static CORE_ADDR
9948 find_breakpoint_range_end (struct symtab_and_line sal)
9949 {
9950 CORE_ADDR end;
9951
9952 /* If the user provided a PC value, use it. Otherwise,
9953 find the address of the end of the given location. */
9954 if (sal.explicit_pc)
9955 end = sal.pc;
9956 else
9957 {
9958 int ret;
9959 CORE_ADDR start;
9960
9961 ret = find_line_pc_range (sal, &start, &end);
9962 if (!ret)
9963 error (_("Could not find location of the end of the range."));
9964
9965 /* find_line_pc_range returns the start of the next line. */
9966 end--;
9967 }
9968
9969 return end;
9970 }
9971
9972 /* Implement the "break-range" CLI command. */
9973
9974 static void
9975 break_range_command (char *arg_in, int from_tty)
9976 {
9977 const char *arg = arg_in;
9978 const char *arg_start;
9979 struct linespec_result canonical_start, canonical_end;
9980 int bp_count, can_use_bp, length;
9981 CORE_ADDR end;
9982 struct breakpoint *b;
9983
9984 /* We don't support software ranged breakpoints. */
9985 if (target_ranged_break_num_registers () < 0)
9986 error (_("This target does not support hardware ranged breakpoints."));
9987
9988 bp_count = hw_breakpoint_used_count ();
9989 bp_count += target_ranged_break_num_registers ();
9990 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9991 bp_count, 0);
9992 if (can_use_bp < 0)
9993 error (_("Hardware breakpoints used exceeds limit."));
9994
9995 arg = skip_spaces (arg);
9996 if (arg == NULL || arg[0] == '\0')
9997 error(_("No address range specified."));
9998
9999 arg_start = arg;
10000 event_location_up start_location = string_to_event_location (&arg,
10001 current_language);
10002 parse_breakpoint_sals (start_location.get (), &canonical_start);
10003
10004 if (arg[0] != ',')
10005 error (_("Too few arguments."));
10006 else if (canonical_start.lsals.empty ())
10007 error (_("Could not find location of the beginning of the range."));
10008
10009 const linespec_sals &lsal_start = canonical_start.lsals[0];
10010
10011 if (canonical_start.lsals.size () > 1
10012 || lsal_start.sals.size () != 1)
10013 error (_("Cannot create a ranged breakpoint with multiple locations."));
10014
10015 const symtab_and_line &sal_start = lsal_start.sals[0];
10016 std::string addr_string_start (arg_start, arg - arg_start);
10017
10018 arg++; /* Skip the comma. */
10019 arg = skip_spaces (arg);
10020
10021 /* Parse the end location. */
10022
10023 arg_start = arg;
10024
10025 /* We call decode_line_full directly here instead of using
10026 parse_breakpoint_sals because we need to specify the start location's
10027 symtab and line as the default symtab and line for the end of the
10028 range. This makes it possible to have ranges like "foo.c:27, +14",
10029 where +14 means 14 lines from the start location. */
10030 event_location_up end_location = string_to_event_location (&arg,
10031 current_language);
10032 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10033 sal_start.symtab, sal_start.line,
10034 &canonical_end, NULL, NULL);
10035
10036 if (canonical_end.lsals.empty ())
10037 error (_("Could not find location of the end of the range."));
10038
10039 const linespec_sals &lsal_end = canonical_end.lsals[0];
10040 if (canonical_end.lsals.size () > 1
10041 || lsal_end.sals.size () != 1)
10042 error (_("Cannot create a ranged breakpoint with multiple locations."));
10043
10044 const symtab_and_line &sal_end = lsal_end.sals[0];
10045
10046 end = find_breakpoint_range_end (sal_end);
10047 if (sal_start.pc > end)
10048 error (_("Invalid address range, end precedes start."));
10049
10050 length = end - sal_start.pc + 1;
10051 if (length < 0)
10052 /* Length overflowed. */
10053 error (_("Address range too large."));
10054 else if (length == 1)
10055 {
10056 /* This range is simple enough to be handled by
10057 the `hbreak' command. */
10058 hbreak_command (&addr_string_start[0], 1);
10059
10060 return;
10061 }
10062
10063 /* Now set up the breakpoint. */
10064 b = set_raw_breakpoint (get_current_arch (), sal_start,
10065 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10066 set_breakpoint_count (breakpoint_count + 1);
10067 b->number = breakpoint_count;
10068 b->disposition = disp_donttouch;
10069 b->location = std::move (start_location);
10070 b->location_range_end = std::move (end_location);
10071 b->loc->length = length;
10072
10073 mention (b);
10074 observer_notify_breakpoint_created (b);
10075 update_global_location_list (UGLL_MAY_INSERT);
10076 }
10077
10078 /* Return non-zero if EXP is verified as constant. Returned zero
10079 means EXP is variable. Also the constant detection may fail for
10080 some constant expressions and in such case still falsely return
10081 zero. */
10082
10083 static int
10084 watchpoint_exp_is_const (const struct expression *exp)
10085 {
10086 int i = exp->nelts;
10087
10088 while (i > 0)
10089 {
10090 int oplenp, argsp;
10091
10092 /* We are only interested in the descriptor of each element. */
10093 operator_length (exp, i, &oplenp, &argsp);
10094 i -= oplenp;
10095
10096 switch (exp->elts[i].opcode)
10097 {
10098 case BINOP_ADD:
10099 case BINOP_SUB:
10100 case BINOP_MUL:
10101 case BINOP_DIV:
10102 case BINOP_REM:
10103 case BINOP_MOD:
10104 case BINOP_LSH:
10105 case BINOP_RSH:
10106 case BINOP_LOGICAL_AND:
10107 case BINOP_LOGICAL_OR:
10108 case BINOP_BITWISE_AND:
10109 case BINOP_BITWISE_IOR:
10110 case BINOP_BITWISE_XOR:
10111 case BINOP_EQUAL:
10112 case BINOP_NOTEQUAL:
10113 case BINOP_LESS:
10114 case BINOP_GTR:
10115 case BINOP_LEQ:
10116 case BINOP_GEQ:
10117 case BINOP_REPEAT:
10118 case BINOP_COMMA:
10119 case BINOP_EXP:
10120 case BINOP_MIN:
10121 case BINOP_MAX:
10122 case BINOP_INTDIV:
10123 case BINOP_CONCAT:
10124 case TERNOP_COND:
10125 case TERNOP_SLICE:
10126
10127 case OP_LONG:
10128 case OP_FLOAT:
10129 case OP_LAST:
10130 case OP_COMPLEX:
10131 case OP_STRING:
10132 case OP_ARRAY:
10133 case OP_TYPE:
10134 case OP_TYPEOF:
10135 case OP_DECLTYPE:
10136 case OP_TYPEID:
10137 case OP_NAME:
10138 case OP_OBJC_NSSTRING:
10139
10140 case UNOP_NEG:
10141 case UNOP_LOGICAL_NOT:
10142 case UNOP_COMPLEMENT:
10143 case UNOP_ADDR:
10144 case UNOP_HIGH:
10145 case UNOP_CAST:
10146
10147 case UNOP_CAST_TYPE:
10148 case UNOP_REINTERPRET_CAST:
10149 case UNOP_DYNAMIC_CAST:
10150 /* Unary, binary and ternary operators: We have to check
10151 their operands. If they are constant, then so is the
10152 result of that operation. For instance, if A and B are
10153 determined to be constants, then so is "A + B".
10154
10155 UNOP_IND is one exception to the rule above, because the
10156 value of *ADDR is not necessarily a constant, even when
10157 ADDR is. */
10158 break;
10159
10160 case OP_VAR_VALUE:
10161 /* Check whether the associated symbol is a constant.
10162
10163 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10164 possible that a buggy compiler could mark a variable as
10165 constant even when it is not, and TYPE_CONST would return
10166 true in this case, while SYMBOL_CLASS wouldn't.
10167
10168 We also have to check for function symbols because they
10169 are always constant. */
10170 {
10171 struct symbol *s = exp->elts[i + 2].symbol;
10172
10173 if (SYMBOL_CLASS (s) != LOC_BLOCK
10174 && SYMBOL_CLASS (s) != LOC_CONST
10175 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10176 return 0;
10177 break;
10178 }
10179
10180 /* The default action is to return 0 because we are using
10181 the optimistic approach here: If we don't know something,
10182 then it is not a constant. */
10183 default:
10184 return 0;
10185 }
10186 }
10187
10188 return 1;
10189 }
10190
10191 /* Watchpoint destructor. */
10192
10193 watchpoint::~watchpoint ()
10194 {
10195 xfree (this->exp_string);
10196 xfree (this->exp_string_reparse);
10197 value_free (this->val);
10198 }
10199
10200 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10201
10202 static void
10203 re_set_watchpoint (struct breakpoint *b)
10204 {
10205 struct watchpoint *w = (struct watchpoint *) b;
10206
10207 /* Watchpoint can be either on expression using entirely global
10208 variables, or it can be on local variables.
10209
10210 Watchpoints of the first kind are never auto-deleted, and even
10211 persist across program restarts. Since they can use variables
10212 from shared libraries, we need to reparse expression as libraries
10213 are loaded and unloaded.
10214
10215 Watchpoints on local variables can also change meaning as result
10216 of solib event. For example, if a watchpoint uses both a local
10217 and a global variables in expression, it's a local watchpoint,
10218 but unloading of a shared library will make the expression
10219 invalid. This is not a very common use case, but we still
10220 re-evaluate expression, to avoid surprises to the user.
10221
10222 Note that for local watchpoints, we re-evaluate it only if
10223 watchpoints frame id is still valid. If it's not, it means the
10224 watchpoint is out of scope and will be deleted soon. In fact,
10225 I'm not sure we'll ever be called in this case.
10226
10227 If a local watchpoint's frame id is still valid, then
10228 w->exp_valid_block is likewise valid, and we can safely use it.
10229
10230 Don't do anything about disabled watchpoints, since they will be
10231 reevaluated again when enabled. */
10232 update_watchpoint (w, 1 /* reparse */);
10233 }
10234
10235 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10236
10237 static int
10238 insert_watchpoint (struct bp_location *bl)
10239 {
10240 struct watchpoint *w = (struct watchpoint *) bl->owner;
10241 int length = w->exact ? 1 : bl->length;
10242
10243 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10244 w->cond_exp.get ());
10245 }
10246
10247 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10248
10249 static int
10250 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10251 {
10252 struct watchpoint *w = (struct watchpoint *) bl->owner;
10253 int length = w->exact ? 1 : bl->length;
10254
10255 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10256 w->cond_exp.get ());
10257 }
10258
10259 static int
10260 breakpoint_hit_watchpoint (const struct bp_location *bl,
10261 const address_space *aspace, CORE_ADDR bp_addr,
10262 const struct target_waitstatus *ws)
10263 {
10264 struct breakpoint *b = bl->owner;
10265 struct watchpoint *w = (struct watchpoint *) b;
10266
10267 /* Continuable hardware watchpoints are treated as non-existent if the
10268 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10269 some data address). Otherwise gdb won't stop on a break instruction
10270 in the code (not from a breakpoint) when a hardware watchpoint has
10271 been defined. Also skip watchpoints which we know did not trigger
10272 (did not match the data address). */
10273 if (is_hardware_watchpoint (b)
10274 && w->watchpoint_triggered == watch_triggered_no)
10275 return 0;
10276
10277 return 1;
10278 }
10279
10280 static void
10281 check_status_watchpoint (bpstat bs)
10282 {
10283 gdb_assert (is_watchpoint (bs->breakpoint_at));
10284
10285 bpstat_check_watchpoint (bs);
10286 }
10287
10288 /* Implement the "resources_needed" breakpoint_ops method for
10289 hardware watchpoints. */
10290
10291 static int
10292 resources_needed_watchpoint (const struct bp_location *bl)
10293 {
10294 struct watchpoint *w = (struct watchpoint *) bl->owner;
10295 int length = w->exact? 1 : bl->length;
10296
10297 return target_region_ok_for_hw_watchpoint (bl->address, length);
10298 }
10299
10300 /* Implement the "works_in_software_mode" breakpoint_ops method for
10301 hardware watchpoints. */
10302
10303 static int
10304 works_in_software_mode_watchpoint (const struct breakpoint *b)
10305 {
10306 /* Read and access watchpoints only work with hardware support. */
10307 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10308 }
10309
10310 static enum print_stop_action
10311 print_it_watchpoint (bpstat bs)
10312 {
10313 struct breakpoint *b;
10314 enum print_stop_action result;
10315 struct watchpoint *w;
10316 struct ui_out *uiout = current_uiout;
10317
10318 gdb_assert (bs->bp_location_at != NULL);
10319
10320 b = bs->breakpoint_at;
10321 w = (struct watchpoint *) b;
10322
10323 annotate_watchpoint (b->number);
10324 maybe_print_thread_hit_breakpoint (uiout);
10325
10326 string_file stb;
10327
10328 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10329 switch (b->type)
10330 {
10331 case bp_watchpoint:
10332 case bp_hardware_watchpoint:
10333 if (uiout->is_mi_like_p ())
10334 uiout->field_string
10335 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10336 mention (b);
10337 tuple_emitter.emplace (uiout, "value");
10338 uiout->text ("\nOld value = ");
10339 watchpoint_value_print (bs->old_val, &stb);
10340 uiout->field_stream ("old", stb);
10341 uiout->text ("\nNew value = ");
10342 watchpoint_value_print (w->val, &stb);
10343 uiout->field_stream ("new", stb);
10344 uiout->text ("\n");
10345 /* More than one watchpoint may have been triggered. */
10346 result = PRINT_UNKNOWN;
10347 break;
10348
10349 case bp_read_watchpoint:
10350 if (uiout->is_mi_like_p ())
10351 uiout->field_string
10352 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10353 mention (b);
10354 tuple_emitter.emplace (uiout, "value");
10355 uiout->text ("\nValue = ");
10356 watchpoint_value_print (w->val, &stb);
10357 uiout->field_stream ("value", stb);
10358 uiout->text ("\n");
10359 result = PRINT_UNKNOWN;
10360 break;
10361
10362 case bp_access_watchpoint:
10363 if (bs->old_val != NULL)
10364 {
10365 if (uiout->is_mi_like_p ())
10366 uiout->field_string
10367 ("reason",
10368 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10369 mention (b);
10370 tuple_emitter.emplace (uiout, "value");
10371 uiout->text ("\nOld value = ");
10372 watchpoint_value_print (bs->old_val, &stb);
10373 uiout->field_stream ("old", stb);
10374 uiout->text ("\nNew value = ");
10375 }
10376 else
10377 {
10378 mention (b);
10379 if (uiout->is_mi_like_p ())
10380 uiout->field_string
10381 ("reason",
10382 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10383 tuple_emitter.emplace (uiout, "value");
10384 uiout->text ("\nValue = ");
10385 }
10386 watchpoint_value_print (w->val, &stb);
10387 uiout->field_stream ("new", stb);
10388 uiout->text ("\n");
10389 result = PRINT_UNKNOWN;
10390 break;
10391 default:
10392 result = PRINT_UNKNOWN;
10393 }
10394
10395 return result;
10396 }
10397
10398 /* Implement the "print_mention" breakpoint_ops method for hardware
10399 watchpoints. */
10400
10401 static void
10402 print_mention_watchpoint (struct breakpoint *b)
10403 {
10404 struct watchpoint *w = (struct watchpoint *) b;
10405 struct ui_out *uiout = current_uiout;
10406 const char *tuple_name;
10407
10408 switch (b->type)
10409 {
10410 case bp_watchpoint:
10411 uiout->text ("Watchpoint ");
10412 tuple_name = "wpt";
10413 break;
10414 case bp_hardware_watchpoint:
10415 uiout->text ("Hardware watchpoint ");
10416 tuple_name = "wpt";
10417 break;
10418 case bp_read_watchpoint:
10419 uiout->text ("Hardware read watchpoint ");
10420 tuple_name = "hw-rwpt";
10421 break;
10422 case bp_access_watchpoint:
10423 uiout->text ("Hardware access (read/write) watchpoint ");
10424 tuple_name = "hw-awpt";
10425 break;
10426 default:
10427 internal_error (__FILE__, __LINE__,
10428 _("Invalid hardware watchpoint type."));
10429 }
10430
10431 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10432 uiout->field_int ("number", b->number);
10433 uiout->text (": ");
10434 uiout->field_string ("exp", w->exp_string);
10435 }
10436
10437 /* Implement the "print_recreate" breakpoint_ops method for
10438 watchpoints. */
10439
10440 static void
10441 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10442 {
10443 struct watchpoint *w = (struct watchpoint *) b;
10444
10445 switch (b->type)
10446 {
10447 case bp_watchpoint:
10448 case bp_hardware_watchpoint:
10449 fprintf_unfiltered (fp, "watch");
10450 break;
10451 case bp_read_watchpoint:
10452 fprintf_unfiltered (fp, "rwatch");
10453 break;
10454 case bp_access_watchpoint:
10455 fprintf_unfiltered (fp, "awatch");
10456 break;
10457 default:
10458 internal_error (__FILE__, __LINE__,
10459 _("Invalid watchpoint type."));
10460 }
10461
10462 fprintf_unfiltered (fp, " %s", w->exp_string);
10463 print_recreate_thread (b, fp);
10464 }
10465
10466 /* Implement the "explains_signal" breakpoint_ops method for
10467 watchpoints. */
10468
10469 static int
10470 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10471 {
10472 /* A software watchpoint cannot cause a signal other than
10473 GDB_SIGNAL_TRAP. */
10474 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10475 return 0;
10476
10477 return 1;
10478 }
10479
10480 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10481
10482 static struct breakpoint_ops watchpoint_breakpoint_ops;
10483
10484 /* Implement the "insert" breakpoint_ops method for
10485 masked hardware watchpoints. */
10486
10487 static int
10488 insert_masked_watchpoint (struct bp_location *bl)
10489 {
10490 struct watchpoint *w = (struct watchpoint *) bl->owner;
10491
10492 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10493 bl->watchpoint_type);
10494 }
10495
10496 /* Implement the "remove" breakpoint_ops method for
10497 masked hardware watchpoints. */
10498
10499 static int
10500 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10501 {
10502 struct watchpoint *w = (struct watchpoint *) bl->owner;
10503
10504 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10505 bl->watchpoint_type);
10506 }
10507
10508 /* Implement the "resources_needed" breakpoint_ops method for
10509 masked hardware watchpoints. */
10510
10511 static int
10512 resources_needed_masked_watchpoint (const struct bp_location *bl)
10513 {
10514 struct watchpoint *w = (struct watchpoint *) bl->owner;
10515
10516 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10517 }
10518
10519 /* Implement the "works_in_software_mode" breakpoint_ops method for
10520 masked hardware watchpoints. */
10521
10522 static int
10523 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10524 {
10525 return 0;
10526 }
10527
10528 /* Implement the "print_it" breakpoint_ops method for
10529 masked hardware watchpoints. */
10530
10531 static enum print_stop_action
10532 print_it_masked_watchpoint (bpstat bs)
10533 {
10534 struct breakpoint *b = bs->breakpoint_at;
10535 struct ui_out *uiout = current_uiout;
10536
10537 /* Masked watchpoints have only one location. */
10538 gdb_assert (b->loc && b->loc->next == NULL);
10539
10540 annotate_watchpoint (b->number);
10541 maybe_print_thread_hit_breakpoint (uiout);
10542
10543 switch (b->type)
10544 {
10545 case bp_hardware_watchpoint:
10546 if (uiout->is_mi_like_p ())
10547 uiout->field_string
10548 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10549 break;
10550
10551 case bp_read_watchpoint:
10552 if (uiout->is_mi_like_p ())
10553 uiout->field_string
10554 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10555 break;
10556
10557 case bp_access_watchpoint:
10558 if (uiout->is_mi_like_p ())
10559 uiout->field_string
10560 ("reason",
10561 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10562 break;
10563 default:
10564 internal_error (__FILE__, __LINE__,
10565 _("Invalid hardware watchpoint type."));
10566 }
10567
10568 mention (b);
10569 uiout->text (_("\n\
10570 Check the underlying instruction at PC for the memory\n\
10571 address and value which triggered this watchpoint.\n"));
10572 uiout->text ("\n");
10573
10574 /* More than one watchpoint may have been triggered. */
10575 return PRINT_UNKNOWN;
10576 }
10577
10578 /* Implement the "print_one_detail" breakpoint_ops method for
10579 masked hardware watchpoints. */
10580
10581 static void
10582 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10583 struct ui_out *uiout)
10584 {
10585 struct watchpoint *w = (struct watchpoint *) b;
10586
10587 /* Masked watchpoints have only one location. */
10588 gdb_assert (b->loc && b->loc->next == NULL);
10589
10590 uiout->text ("\tmask ");
10591 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10592 uiout->text ("\n");
10593 }
10594
10595 /* Implement the "print_mention" breakpoint_ops method for
10596 masked hardware watchpoints. */
10597
10598 static void
10599 print_mention_masked_watchpoint (struct breakpoint *b)
10600 {
10601 struct watchpoint *w = (struct watchpoint *) b;
10602 struct ui_out *uiout = current_uiout;
10603 const char *tuple_name;
10604
10605 switch (b->type)
10606 {
10607 case bp_hardware_watchpoint:
10608 uiout->text ("Masked hardware watchpoint ");
10609 tuple_name = "wpt";
10610 break;
10611 case bp_read_watchpoint:
10612 uiout->text ("Masked hardware read watchpoint ");
10613 tuple_name = "hw-rwpt";
10614 break;
10615 case bp_access_watchpoint:
10616 uiout->text ("Masked hardware access (read/write) watchpoint ");
10617 tuple_name = "hw-awpt";
10618 break;
10619 default:
10620 internal_error (__FILE__, __LINE__,
10621 _("Invalid hardware watchpoint type."));
10622 }
10623
10624 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10625 uiout->field_int ("number", b->number);
10626 uiout->text (": ");
10627 uiout->field_string ("exp", w->exp_string);
10628 }
10629
10630 /* Implement the "print_recreate" breakpoint_ops method for
10631 masked hardware watchpoints. */
10632
10633 static void
10634 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10635 {
10636 struct watchpoint *w = (struct watchpoint *) b;
10637 char tmp[40];
10638
10639 switch (b->type)
10640 {
10641 case bp_hardware_watchpoint:
10642 fprintf_unfiltered (fp, "watch");
10643 break;
10644 case bp_read_watchpoint:
10645 fprintf_unfiltered (fp, "rwatch");
10646 break;
10647 case bp_access_watchpoint:
10648 fprintf_unfiltered (fp, "awatch");
10649 break;
10650 default:
10651 internal_error (__FILE__, __LINE__,
10652 _("Invalid hardware watchpoint type."));
10653 }
10654
10655 sprintf_vma (tmp, w->hw_wp_mask);
10656 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10657 print_recreate_thread (b, fp);
10658 }
10659
10660 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10661
10662 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10663
10664 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10665
10666 static int
10667 is_masked_watchpoint (const struct breakpoint *b)
10668 {
10669 return b->ops == &masked_watchpoint_breakpoint_ops;
10670 }
10671
10672 /* accessflag: hw_write: watch write,
10673 hw_read: watch read,
10674 hw_access: watch access (read or write) */
10675 static void
10676 watch_command_1 (const char *arg, int accessflag, int from_tty,
10677 int just_location, int internal)
10678 {
10679 struct breakpoint *scope_breakpoint = NULL;
10680 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10681 struct value *val, *mark, *result;
10682 int saved_bitpos = 0, saved_bitsize = 0;
10683 const char *exp_start = NULL;
10684 const char *exp_end = NULL;
10685 const char *tok, *end_tok;
10686 int toklen = -1;
10687 const char *cond_start = NULL;
10688 const char *cond_end = NULL;
10689 enum bptype bp_type;
10690 int thread = -1;
10691 int pc = 0;
10692 /* Flag to indicate whether we are going to use masks for
10693 the hardware watchpoint. */
10694 int use_mask = 0;
10695 CORE_ADDR mask = 0;
10696
10697 /* Make sure that we actually have parameters to parse. */
10698 if (arg != NULL && arg[0] != '\0')
10699 {
10700 const char *value_start;
10701
10702 exp_end = arg + strlen (arg);
10703
10704 /* Look for "parameter value" pairs at the end
10705 of the arguments string. */
10706 for (tok = exp_end - 1; tok > arg; tok--)
10707 {
10708 /* Skip whitespace at the end of the argument list. */
10709 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10710 tok--;
10711
10712 /* Find the beginning of the last token.
10713 This is the value of the parameter. */
10714 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10715 tok--;
10716 value_start = tok + 1;
10717
10718 /* Skip whitespace. */
10719 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10720 tok--;
10721
10722 end_tok = tok;
10723
10724 /* Find the beginning of the second to last token.
10725 This is the parameter itself. */
10726 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10727 tok--;
10728 tok++;
10729 toklen = end_tok - tok + 1;
10730
10731 if (toklen == 6 && startswith (tok, "thread"))
10732 {
10733 struct thread_info *thr;
10734 /* At this point we've found a "thread" token, which means
10735 the user is trying to set a watchpoint that triggers
10736 only in a specific thread. */
10737 const char *endp;
10738
10739 if (thread != -1)
10740 error(_("You can specify only one thread."));
10741
10742 /* Extract the thread ID from the next token. */
10743 thr = parse_thread_id (value_start, &endp);
10744
10745 /* Check if the user provided a valid thread ID. */
10746 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10747 invalid_thread_id_error (value_start);
10748
10749 thread = thr->global_num;
10750 }
10751 else if (toklen == 4 && startswith (tok, "mask"))
10752 {
10753 /* We've found a "mask" token, which means the user wants to
10754 create a hardware watchpoint that is going to have the mask
10755 facility. */
10756 struct value *mask_value, *mark;
10757
10758 if (use_mask)
10759 error(_("You can specify only one mask."));
10760
10761 use_mask = just_location = 1;
10762
10763 mark = value_mark ();
10764 mask_value = parse_to_comma_and_eval (&value_start);
10765 mask = value_as_address (mask_value);
10766 value_free_to_mark (mark);
10767 }
10768 else
10769 /* We didn't recognize what we found. We should stop here. */
10770 break;
10771
10772 /* Truncate the string and get rid of the "parameter value" pair before
10773 the arguments string is parsed by the parse_exp_1 function. */
10774 exp_end = tok;
10775 }
10776 }
10777 else
10778 exp_end = arg;
10779
10780 /* Parse the rest of the arguments. From here on out, everything
10781 is in terms of a newly allocated string instead of the original
10782 ARG. */
10783 innermost_block = NULL;
10784 std::string expression (arg, exp_end - arg);
10785 exp_start = arg = expression.c_str ();
10786 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
10787 exp_end = arg;
10788 /* Remove trailing whitespace from the expression before saving it.
10789 This makes the eventual display of the expression string a bit
10790 prettier. */
10791 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10792 --exp_end;
10793
10794 /* Checking if the expression is not constant. */
10795 if (watchpoint_exp_is_const (exp.get ()))
10796 {
10797 int len;
10798
10799 len = exp_end - exp_start;
10800 while (len > 0 && isspace (exp_start[len - 1]))
10801 len--;
10802 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10803 }
10804
10805 exp_valid_block = innermost_block;
10806 mark = value_mark ();
10807 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
10808
10809 if (val != NULL && just_location)
10810 {
10811 saved_bitpos = value_bitpos (val);
10812 saved_bitsize = value_bitsize (val);
10813 }
10814
10815 if (just_location)
10816 {
10817 int ret;
10818
10819 exp_valid_block = NULL;
10820 val = value_addr (result);
10821 release_value (val);
10822 value_free_to_mark (mark);
10823
10824 if (use_mask)
10825 {
10826 ret = target_masked_watch_num_registers (value_as_address (val),
10827 mask);
10828 if (ret == -1)
10829 error (_("This target does not support masked watchpoints."));
10830 else if (ret == -2)
10831 error (_("Invalid mask or memory region."));
10832 }
10833 }
10834 else if (val != NULL)
10835 release_value (val);
10836
10837 tok = skip_spaces (arg);
10838 end_tok = skip_to_space (tok);
10839
10840 toklen = end_tok - tok;
10841 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10842 {
10843 innermost_block = NULL;
10844 tok = cond_start = end_tok + 1;
10845 parse_exp_1 (&tok, 0, 0, 0);
10846
10847 /* The watchpoint expression may not be local, but the condition
10848 may still be. E.g.: `watch global if local > 0'. */
10849 cond_exp_valid_block = innermost_block;
10850
10851 cond_end = tok;
10852 }
10853 if (*tok)
10854 error (_("Junk at end of command."));
10855
10856 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10857
10858 /* Save this because create_internal_breakpoint below invalidates
10859 'wp_frame'. */
10860 frame_id watchpoint_frame = get_frame_id (wp_frame);
10861
10862 /* If the expression is "local", then set up a "watchpoint scope"
10863 breakpoint at the point where we've left the scope of the watchpoint
10864 expression. Create the scope breakpoint before the watchpoint, so
10865 that we will encounter it first in bpstat_stop_status. */
10866 if (exp_valid_block != NULL && wp_frame != NULL)
10867 {
10868 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10869
10870 if (frame_id_p (caller_frame_id))
10871 {
10872 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10873 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10874
10875 scope_breakpoint
10876 = create_internal_breakpoint (caller_arch, caller_pc,
10877 bp_watchpoint_scope,
10878 &momentary_breakpoint_ops);
10879
10880 /* create_internal_breakpoint could invalidate WP_FRAME. */
10881 wp_frame = NULL;
10882
10883 scope_breakpoint->enable_state = bp_enabled;
10884
10885 /* Automatically delete the breakpoint when it hits. */
10886 scope_breakpoint->disposition = disp_del;
10887
10888 /* Only break in the proper frame (help with recursion). */
10889 scope_breakpoint->frame_id = caller_frame_id;
10890
10891 /* Set the address at which we will stop. */
10892 scope_breakpoint->loc->gdbarch = caller_arch;
10893 scope_breakpoint->loc->requested_address = caller_pc;
10894 scope_breakpoint->loc->address
10895 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10896 scope_breakpoint->loc->requested_address,
10897 scope_breakpoint->type);
10898 }
10899 }
10900
10901 /* Now set up the breakpoint. We create all watchpoints as hardware
10902 watchpoints here even if hardware watchpoints are turned off, a call
10903 to update_watchpoint later in this function will cause the type to
10904 drop back to bp_watchpoint (software watchpoint) if required. */
10905
10906 if (accessflag == hw_read)
10907 bp_type = bp_read_watchpoint;
10908 else if (accessflag == hw_access)
10909 bp_type = bp_access_watchpoint;
10910 else
10911 bp_type = bp_hardware_watchpoint;
10912
10913 std::unique_ptr<watchpoint> w (new watchpoint ());
10914
10915 if (use_mask)
10916 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10917 &masked_watchpoint_breakpoint_ops);
10918 else
10919 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10920 &watchpoint_breakpoint_ops);
10921 w->thread = thread;
10922 w->disposition = disp_donttouch;
10923 w->pspace = current_program_space;
10924 w->exp = std::move (exp);
10925 w->exp_valid_block = exp_valid_block;
10926 w->cond_exp_valid_block = cond_exp_valid_block;
10927 if (just_location)
10928 {
10929 struct type *t = value_type (val);
10930 CORE_ADDR addr = value_as_address (val);
10931
10932 w->exp_string_reparse
10933 = current_language->la_watch_location_expression (t, addr).release ();
10934
10935 w->exp_string = xstrprintf ("-location %.*s",
10936 (int) (exp_end - exp_start), exp_start);
10937 }
10938 else
10939 w->exp_string = savestring (exp_start, exp_end - exp_start);
10940
10941 if (use_mask)
10942 {
10943 w->hw_wp_mask = mask;
10944 }
10945 else
10946 {
10947 w->val = val;
10948 w->val_bitpos = saved_bitpos;
10949 w->val_bitsize = saved_bitsize;
10950 w->val_valid = 1;
10951 }
10952
10953 if (cond_start)
10954 w->cond_string = savestring (cond_start, cond_end - cond_start);
10955 else
10956 w->cond_string = 0;
10957
10958 if (frame_id_p (watchpoint_frame))
10959 {
10960 w->watchpoint_frame = watchpoint_frame;
10961 w->watchpoint_thread = inferior_ptid;
10962 }
10963 else
10964 {
10965 w->watchpoint_frame = null_frame_id;
10966 w->watchpoint_thread = null_ptid;
10967 }
10968
10969 if (scope_breakpoint != NULL)
10970 {
10971 /* The scope breakpoint is related to the watchpoint. We will
10972 need to act on them together. */
10973 w->related_breakpoint = scope_breakpoint;
10974 scope_breakpoint->related_breakpoint = w.get ();
10975 }
10976
10977 if (!just_location)
10978 value_free_to_mark (mark);
10979
10980 /* Finally update the new watchpoint. This creates the locations
10981 that should be inserted. */
10982 update_watchpoint (w.get (), 1);
10983
10984 install_breakpoint (internal, std::move (w), 1);
10985 }
10986
10987 /* Return count of debug registers needed to watch the given expression.
10988 If the watchpoint cannot be handled in hardware return zero. */
10989
10990 static int
10991 can_use_hardware_watchpoint (struct value *v)
10992 {
10993 int found_memory_cnt = 0;
10994 struct value *head = v;
10995
10996 /* Did the user specifically forbid us to use hardware watchpoints? */
10997 if (!can_use_hw_watchpoints)
10998 return 0;
10999
11000 /* Make sure that the value of the expression depends only upon
11001 memory contents, and values computed from them within GDB. If we
11002 find any register references or function calls, we can't use a
11003 hardware watchpoint.
11004
11005 The idea here is that evaluating an expression generates a series
11006 of values, one holding the value of every subexpression. (The
11007 expression a*b+c has five subexpressions: a, b, a*b, c, and
11008 a*b+c.) GDB's values hold almost enough information to establish
11009 the criteria given above --- they identify memory lvalues,
11010 register lvalues, computed values, etcetera. So we can evaluate
11011 the expression, and then scan the chain of values that leaves
11012 behind to decide whether we can detect any possible change to the
11013 expression's final value using only hardware watchpoints.
11014
11015 However, I don't think that the values returned by inferior
11016 function calls are special in any way. So this function may not
11017 notice that an expression involving an inferior function call
11018 can't be watched with hardware watchpoints. FIXME. */
11019 for (; v; v = value_next (v))
11020 {
11021 if (VALUE_LVAL (v) == lval_memory)
11022 {
11023 if (v != head && value_lazy (v))
11024 /* A lazy memory lvalue in the chain is one that GDB never
11025 needed to fetch; we either just used its address (e.g.,
11026 `a' in `a.b') or we never needed it at all (e.g., `a'
11027 in `a,b'). This doesn't apply to HEAD; if that is
11028 lazy then it was not readable, but watch it anyway. */
11029 ;
11030 else
11031 {
11032 /* Ahh, memory we actually used! Check if we can cover
11033 it with hardware watchpoints. */
11034 struct type *vtype = check_typedef (value_type (v));
11035
11036 /* We only watch structs and arrays if user asked for it
11037 explicitly, never if they just happen to appear in a
11038 middle of some value chain. */
11039 if (v == head
11040 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11041 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11042 {
11043 CORE_ADDR vaddr = value_address (v);
11044 int len;
11045 int num_regs;
11046
11047 len = (target_exact_watchpoints
11048 && is_scalar_type_recursive (vtype))?
11049 1 : TYPE_LENGTH (value_type (v));
11050
11051 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11052 if (!num_regs)
11053 return 0;
11054 else
11055 found_memory_cnt += num_regs;
11056 }
11057 }
11058 }
11059 else if (VALUE_LVAL (v) != not_lval
11060 && deprecated_value_modifiable (v) == 0)
11061 return 0; /* These are values from the history (e.g., $1). */
11062 else if (VALUE_LVAL (v) == lval_register)
11063 return 0; /* Cannot watch a register with a HW watchpoint. */
11064 }
11065
11066 /* The expression itself looks suitable for using a hardware
11067 watchpoint, but give the target machine a chance to reject it. */
11068 return found_memory_cnt;
11069 }
11070
11071 void
11072 watch_command_wrapper (const char *arg, int from_tty, int internal)
11073 {
11074 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11075 }
11076
11077 /* A helper function that looks for the "-location" argument and then
11078 calls watch_command_1. */
11079
11080 static void
11081 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11082 {
11083 int just_location = 0;
11084
11085 if (arg
11086 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11087 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11088 {
11089 arg = skip_spaces (arg);
11090 just_location = 1;
11091 }
11092
11093 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11094 }
11095
11096 static void
11097 watch_command (char *arg, int from_tty)
11098 {
11099 watch_maybe_just_location (arg, hw_write, from_tty);
11100 }
11101
11102 void
11103 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
11104 {
11105 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11106 }
11107
11108 static void
11109 rwatch_command (char *arg, int from_tty)
11110 {
11111 watch_maybe_just_location (arg, hw_read, from_tty);
11112 }
11113
11114 void
11115 awatch_command_wrapper (const char *arg, int from_tty, int internal)
11116 {
11117 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11118 }
11119
11120 static void
11121 awatch_command (char *arg, int from_tty)
11122 {
11123 watch_maybe_just_location (arg, hw_access, from_tty);
11124 }
11125 \f
11126
11127 /* Data for the FSM that manages the until(location)/advance commands
11128 in infcmd.c. Here because it uses the mechanisms of
11129 breakpoints. */
11130
11131 struct until_break_fsm
11132 {
11133 /* The base class. */
11134 struct thread_fsm thread_fsm;
11135
11136 /* The thread that as current when the command was executed. */
11137 int thread;
11138
11139 /* The breakpoint set at the destination location. */
11140 struct breakpoint *location_breakpoint;
11141
11142 /* Breakpoint set at the return address in the caller frame. May be
11143 NULL. */
11144 struct breakpoint *caller_breakpoint;
11145 };
11146
11147 static void until_break_fsm_clean_up (struct thread_fsm *self,
11148 struct thread_info *thread);
11149 static int until_break_fsm_should_stop (struct thread_fsm *self,
11150 struct thread_info *thread);
11151 static enum async_reply_reason
11152 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11153
11154 /* until_break_fsm's vtable. */
11155
11156 static struct thread_fsm_ops until_break_fsm_ops =
11157 {
11158 NULL, /* dtor */
11159 until_break_fsm_clean_up,
11160 until_break_fsm_should_stop,
11161 NULL, /* return_value */
11162 until_break_fsm_async_reply_reason,
11163 };
11164
11165 /* Allocate a new until_break_command_fsm. */
11166
11167 static struct until_break_fsm *
11168 new_until_break_fsm (struct interp *cmd_interp, int thread,
11169 struct breakpoint *location_breakpoint,
11170 struct breakpoint *caller_breakpoint)
11171 {
11172 struct until_break_fsm *sm;
11173
11174 sm = XCNEW (struct until_break_fsm);
11175 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11176
11177 sm->thread = thread;
11178 sm->location_breakpoint = location_breakpoint;
11179 sm->caller_breakpoint = caller_breakpoint;
11180
11181 return sm;
11182 }
11183
11184 /* Implementation of the 'should_stop' FSM method for the
11185 until(location)/advance commands. */
11186
11187 static int
11188 until_break_fsm_should_stop (struct thread_fsm *self,
11189 struct thread_info *tp)
11190 {
11191 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11192
11193 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11194 sm->location_breakpoint) != NULL
11195 || (sm->caller_breakpoint != NULL
11196 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11197 sm->caller_breakpoint) != NULL))
11198 thread_fsm_set_finished (self);
11199
11200 return 1;
11201 }
11202
11203 /* Implementation of the 'clean_up' FSM method for the
11204 until(location)/advance commands. */
11205
11206 static void
11207 until_break_fsm_clean_up (struct thread_fsm *self,
11208 struct thread_info *thread)
11209 {
11210 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11211
11212 /* Clean up our temporary breakpoints. */
11213 if (sm->location_breakpoint != NULL)
11214 {
11215 delete_breakpoint (sm->location_breakpoint);
11216 sm->location_breakpoint = NULL;
11217 }
11218 if (sm->caller_breakpoint != NULL)
11219 {
11220 delete_breakpoint (sm->caller_breakpoint);
11221 sm->caller_breakpoint = NULL;
11222 }
11223 delete_longjmp_breakpoint (sm->thread);
11224 }
11225
11226 /* Implementation of the 'async_reply_reason' FSM method for the
11227 until(location)/advance commands. */
11228
11229 static enum async_reply_reason
11230 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11231 {
11232 return EXEC_ASYNC_LOCATION_REACHED;
11233 }
11234
11235 void
11236 until_break_command (const char *arg, int from_tty, int anywhere)
11237 {
11238 struct frame_info *frame;
11239 struct gdbarch *frame_gdbarch;
11240 struct frame_id stack_frame_id;
11241 struct frame_id caller_frame_id;
11242 struct breakpoint *location_breakpoint;
11243 struct breakpoint *caller_breakpoint = NULL;
11244 struct cleanup *old_chain;
11245 int thread;
11246 struct thread_info *tp;
11247 struct until_break_fsm *sm;
11248
11249 clear_proceed_status (0);
11250
11251 /* Set a breakpoint where the user wants it and at return from
11252 this function. */
11253
11254 event_location_up location = string_to_event_location (&arg, current_language);
11255
11256 std::vector<symtab_and_line> sals
11257 = (last_displayed_sal_is_valid ()
11258 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11259 get_last_displayed_symtab (),
11260 get_last_displayed_line ())
11261 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11262 NULL, (struct symtab *) NULL, 0));
11263
11264 if (sals.size () != 1)
11265 error (_("Couldn't get information on specified line."));
11266
11267 symtab_and_line &sal = sals[0];
11268
11269 if (*arg)
11270 error (_("Junk at end of arguments."));
11271
11272 resolve_sal_pc (&sal);
11273
11274 tp = inferior_thread ();
11275 thread = tp->global_num;
11276
11277 old_chain = make_cleanup (null_cleanup, NULL);
11278
11279 /* Note linespec handling above invalidates the frame chain.
11280 Installing a breakpoint also invalidates the frame chain (as it
11281 may need to switch threads), so do any frame handling before
11282 that. */
11283
11284 frame = get_selected_frame (NULL);
11285 frame_gdbarch = get_frame_arch (frame);
11286 stack_frame_id = get_stack_frame_id (frame);
11287 caller_frame_id = frame_unwind_caller_id (frame);
11288
11289 /* Keep within the current frame, or in frames called by the current
11290 one. */
11291
11292 if (frame_id_p (caller_frame_id))
11293 {
11294 struct symtab_and_line sal2;
11295 struct gdbarch *caller_gdbarch;
11296
11297 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11298 sal2.pc = frame_unwind_caller_pc (frame);
11299 caller_gdbarch = frame_unwind_caller_arch (frame);
11300 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11301 sal2,
11302 caller_frame_id,
11303 bp_until);
11304 make_cleanup_delete_breakpoint (caller_breakpoint);
11305
11306 set_longjmp_breakpoint (tp, caller_frame_id);
11307 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11308 }
11309
11310 /* set_momentary_breakpoint could invalidate FRAME. */
11311 frame = NULL;
11312
11313 if (anywhere)
11314 /* If the user told us to continue until a specified location,
11315 we don't specify a frame at which we need to stop. */
11316 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11317 null_frame_id, bp_until);
11318 else
11319 /* Otherwise, specify the selected frame, because we want to stop
11320 only at the very same frame. */
11321 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11322 stack_frame_id, bp_until);
11323 make_cleanup_delete_breakpoint (location_breakpoint);
11324
11325 sm = new_until_break_fsm (command_interp (), tp->global_num,
11326 location_breakpoint, caller_breakpoint);
11327 tp->thread_fsm = &sm->thread_fsm;
11328
11329 discard_cleanups (old_chain);
11330
11331 proceed (-1, GDB_SIGNAL_DEFAULT);
11332 }
11333
11334 /* This function attempts to parse an optional "if <cond>" clause
11335 from the arg string. If one is not found, it returns NULL.
11336
11337 Else, it returns a pointer to the condition string. (It does not
11338 attempt to evaluate the string against a particular block.) And,
11339 it updates arg to point to the first character following the parsed
11340 if clause in the arg string. */
11341
11342 const char *
11343 ep_parse_optional_if_clause (const char **arg)
11344 {
11345 const char *cond_string;
11346
11347 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11348 return NULL;
11349
11350 /* Skip the "if" keyword. */
11351 (*arg) += 2;
11352
11353 /* Skip any extra leading whitespace, and record the start of the
11354 condition string. */
11355 *arg = skip_spaces (*arg);
11356 cond_string = *arg;
11357
11358 /* Assume that the condition occupies the remainder of the arg
11359 string. */
11360 (*arg) += strlen (cond_string);
11361
11362 return cond_string;
11363 }
11364
11365 /* Commands to deal with catching events, such as signals, exceptions,
11366 process start/exit, etc. */
11367
11368 typedef enum
11369 {
11370 catch_fork_temporary, catch_vfork_temporary,
11371 catch_fork_permanent, catch_vfork_permanent
11372 }
11373 catch_fork_kind;
11374
11375 static void
11376 catch_fork_command_1 (char *arg_entry, int from_tty,
11377 struct cmd_list_element *command)
11378 {
11379 const char *arg = arg_entry;
11380 struct gdbarch *gdbarch = get_current_arch ();
11381 const char *cond_string = NULL;
11382 catch_fork_kind fork_kind;
11383 int tempflag;
11384
11385 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11386 tempflag = (fork_kind == catch_fork_temporary
11387 || fork_kind == catch_vfork_temporary);
11388
11389 if (!arg)
11390 arg = "";
11391 arg = skip_spaces (arg);
11392
11393 /* The allowed syntax is:
11394 catch [v]fork
11395 catch [v]fork if <cond>
11396
11397 First, check if there's an if clause. */
11398 cond_string = ep_parse_optional_if_clause (&arg);
11399
11400 if ((*arg != '\0') && !isspace (*arg))
11401 error (_("Junk at end of arguments."));
11402
11403 /* If this target supports it, create a fork or vfork catchpoint
11404 and enable reporting of such events. */
11405 switch (fork_kind)
11406 {
11407 case catch_fork_temporary:
11408 case catch_fork_permanent:
11409 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11410 &catch_fork_breakpoint_ops);
11411 break;
11412 case catch_vfork_temporary:
11413 case catch_vfork_permanent:
11414 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11415 &catch_vfork_breakpoint_ops);
11416 break;
11417 default:
11418 error (_("unsupported or unknown fork kind; cannot catch it"));
11419 break;
11420 }
11421 }
11422
11423 static void
11424 catch_exec_command_1 (char *arg_entry, int from_tty,
11425 struct cmd_list_element *command)
11426 {
11427 const char *arg = arg_entry;
11428 struct gdbarch *gdbarch = get_current_arch ();
11429 int tempflag;
11430 const char *cond_string = NULL;
11431
11432 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11433
11434 if (!arg)
11435 arg = "";
11436 arg = skip_spaces (arg);
11437
11438 /* The allowed syntax is:
11439 catch exec
11440 catch exec if <cond>
11441
11442 First, check if there's an if clause. */
11443 cond_string = ep_parse_optional_if_clause (&arg);
11444
11445 if ((*arg != '\0') && !isspace (*arg))
11446 error (_("Junk at end of arguments."));
11447
11448 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11449 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11450 &catch_exec_breakpoint_ops);
11451 c->exec_pathname = NULL;
11452
11453 install_breakpoint (0, std::move (c), 1);
11454 }
11455
11456 void
11457 init_ada_exception_breakpoint (struct breakpoint *b,
11458 struct gdbarch *gdbarch,
11459 struct symtab_and_line sal,
11460 const char *addr_string,
11461 const struct breakpoint_ops *ops,
11462 int tempflag,
11463 int enabled,
11464 int from_tty)
11465 {
11466 if (from_tty)
11467 {
11468 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11469 if (!loc_gdbarch)
11470 loc_gdbarch = gdbarch;
11471
11472 describe_other_breakpoints (loc_gdbarch,
11473 sal.pspace, sal.pc, sal.section, -1);
11474 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11475 version for exception catchpoints, because two catchpoints
11476 used for different exception names will use the same address.
11477 In this case, a "breakpoint ... also set at..." warning is
11478 unproductive. Besides, the warning phrasing is also a bit
11479 inappropriate, we should use the word catchpoint, and tell
11480 the user what type of catchpoint it is. The above is good
11481 enough for now, though. */
11482 }
11483
11484 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11485
11486 b->enable_state = enabled ? bp_enabled : bp_disabled;
11487 b->disposition = tempflag ? disp_del : disp_donttouch;
11488 b->location = string_to_event_location (&addr_string,
11489 language_def (language_ada));
11490 b->language = language_ada;
11491 }
11492
11493 static void
11494 catch_command (const char *arg, int from_tty)
11495 {
11496 error (_("Catch requires an event name."));
11497 }
11498 \f
11499
11500 static void
11501 tcatch_command (const char *arg, int from_tty)
11502 {
11503 error (_("Catch requires an event name."));
11504 }
11505
11506 /* Compare two breakpoints and return a strcmp-like result. */
11507
11508 static int
11509 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11510 {
11511 uintptr_t ua = (uintptr_t) a;
11512 uintptr_t ub = (uintptr_t) b;
11513
11514 if (a->number < b->number)
11515 return -1;
11516 else if (a->number > b->number)
11517 return 1;
11518
11519 /* Now sort by address, in case we see, e..g, two breakpoints with
11520 the number 0. */
11521 if (ua < ub)
11522 return -1;
11523 return ua > ub ? 1 : 0;
11524 }
11525
11526 /* Delete breakpoints by address or line. */
11527
11528 static void
11529 clear_command (char *arg, int from_tty)
11530 {
11531 struct breakpoint *b;
11532 int default_match;
11533 int i;
11534
11535 std::vector<symtab_and_line> decoded_sals;
11536 symtab_and_line last_sal;
11537 gdb::array_view<symtab_and_line> sals;
11538 if (arg)
11539 {
11540 decoded_sals
11541 = decode_line_with_current_source (arg,
11542 (DECODE_LINE_FUNFIRSTLINE
11543 | DECODE_LINE_LIST_MODE));
11544 default_match = 0;
11545 sals = decoded_sals;
11546 }
11547 else
11548 {
11549 /* Set sal's line, symtab, pc, and pspace to the values
11550 corresponding to the last call to print_frame_info. If the
11551 codepoint is not valid, this will set all the fields to 0. */
11552 last_sal = get_last_displayed_sal ();
11553 if (last_sal.symtab == 0)
11554 error (_("No source file specified."));
11555
11556 default_match = 1;
11557 sals = last_sal;
11558 }
11559
11560 /* We don't call resolve_sal_pc here. That's not as bad as it
11561 seems, because all existing breakpoints typically have both
11562 file/line and pc set. So, if clear is given file/line, we can
11563 match this to existing breakpoint without obtaining pc at all.
11564
11565 We only support clearing given the address explicitly
11566 present in breakpoint table. Say, we've set breakpoint
11567 at file:line. There were several PC values for that file:line,
11568 due to optimization, all in one block.
11569
11570 We've picked one PC value. If "clear" is issued with another
11571 PC corresponding to the same file:line, the breakpoint won't
11572 be cleared. We probably can still clear the breakpoint, but
11573 since the other PC value is never presented to user, user
11574 can only find it by guessing, and it does not seem important
11575 to support that. */
11576
11577 /* For each line spec given, delete bps which correspond to it. Do
11578 it in two passes, solely to preserve the current behavior that
11579 from_tty is forced true if we delete more than one
11580 breakpoint. */
11581
11582 std::vector<struct breakpoint *> found;
11583 for (const auto &sal : sals)
11584 {
11585 const char *sal_fullname;
11586
11587 /* If exact pc given, clear bpts at that pc.
11588 If line given (pc == 0), clear all bpts on specified line.
11589 If defaulting, clear all bpts on default line
11590 or at default pc.
11591
11592 defaulting sal.pc != 0 tests to do
11593
11594 0 1 pc
11595 1 1 pc _and_ line
11596 0 0 line
11597 1 0 <can't happen> */
11598
11599 sal_fullname = (sal.symtab == NULL
11600 ? NULL : symtab_to_fullname (sal.symtab));
11601
11602 /* Find all matching breakpoints and add them to 'found'. */
11603 ALL_BREAKPOINTS (b)
11604 {
11605 int match = 0;
11606 /* Are we going to delete b? */
11607 if (b->type != bp_none && !is_watchpoint (b))
11608 {
11609 struct bp_location *loc = b->loc;
11610 for (; loc; loc = loc->next)
11611 {
11612 /* If the user specified file:line, don't allow a PC
11613 match. This matches historical gdb behavior. */
11614 int pc_match = (!sal.explicit_line
11615 && sal.pc
11616 && (loc->pspace == sal.pspace)
11617 && (loc->address == sal.pc)
11618 && (!section_is_overlay (loc->section)
11619 || loc->section == sal.section));
11620 int line_match = 0;
11621
11622 if ((default_match || sal.explicit_line)
11623 && loc->symtab != NULL
11624 && sal_fullname != NULL
11625 && sal.pspace == loc->pspace
11626 && loc->line_number == sal.line
11627 && filename_cmp (symtab_to_fullname (loc->symtab),
11628 sal_fullname) == 0)
11629 line_match = 1;
11630
11631 if (pc_match || line_match)
11632 {
11633 match = 1;
11634 break;
11635 }
11636 }
11637 }
11638
11639 if (match)
11640 found.push_back (b);
11641 }
11642 }
11643
11644 /* Now go thru the 'found' chain and delete them. */
11645 if (found.empty ())
11646 {
11647 if (arg)
11648 error (_("No breakpoint at %s."), arg);
11649 else
11650 error (_("No breakpoint at this line."));
11651 }
11652
11653 /* Remove duplicates from the vec. */
11654 std::sort (found.begin (), found.end (),
11655 [] (const breakpoint *a, const breakpoint *b)
11656 {
11657 return compare_breakpoints (a, b) < 0;
11658 });
11659 found.erase (std::unique (found.begin (), found.end (),
11660 [] (const breakpoint *a, const breakpoint *b)
11661 {
11662 return compare_breakpoints (a, b) == 0;
11663 }),
11664 found.end ());
11665
11666 if (found.size () > 1)
11667 from_tty = 1; /* Always report if deleted more than one. */
11668 if (from_tty)
11669 {
11670 if (found.size () == 1)
11671 printf_unfiltered (_("Deleted breakpoint "));
11672 else
11673 printf_unfiltered (_("Deleted breakpoints "));
11674 }
11675
11676 for (breakpoint *iter : found)
11677 {
11678 if (from_tty)
11679 printf_unfiltered ("%d ", iter->number);
11680 delete_breakpoint (iter);
11681 }
11682 if (from_tty)
11683 putchar_unfiltered ('\n');
11684 }
11685 \f
11686 /* Delete breakpoint in BS if they are `delete' breakpoints and
11687 all breakpoints that are marked for deletion, whether hit or not.
11688 This is called after any breakpoint is hit, or after errors. */
11689
11690 void
11691 breakpoint_auto_delete (bpstat bs)
11692 {
11693 struct breakpoint *b, *b_tmp;
11694
11695 for (; bs; bs = bs->next)
11696 if (bs->breakpoint_at
11697 && bs->breakpoint_at->disposition == disp_del
11698 && bs->stop)
11699 delete_breakpoint (bs->breakpoint_at);
11700
11701 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11702 {
11703 if (b->disposition == disp_del_at_next_stop)
11704 delete_breakpoint (b);
11705 }
11706 }
11707
11708 /* A comparison function for bp_location AP and BP being interfaced to
11709 qsort. Sort elements primarily by their ADDRESS (no matter what
11710 does breakpoint_address_is_meaningful say for its OWNER),
11711 secondarily by ordering first permanent elements and
11712 terciarily just ensuring the array is sorted stable way despite
11713 qsort being an unstable algorithm. */
11714
11715 static int
11716 bp_locations_compare (const void *ap, const void *bp)
11717 {
11718 const struct bp_location *a = *(const struct bp_location **) ap;
11719 const struct bp_location *b = *(const struct bp_location **) bp;
11720
11721 if (a->address != b->address)
11722 return (a->address > b->address) - (a->address < b->address);
11723
11724 /* Sort locations at the same address by their pspace number, keeping
11725 locations of the same inferior (in a multi-inferior environment)
11726 grouped. */
11727
11728 if (a->pspace->num != b->pspace->num)
11729 return ((a->pspace->num > b->pspace->num)
11730 - (a->pspace->num < b->pspace->num));
11731
11732 /* Sort permanent breakpoints first. */
11733 if (a->permanent != b->permanent)
11734 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11735
11736 /* Make the internal GDB representation stable across GDB runs
11737 where A and B memory inside GDB can differ. Breakpoint locations of
11738 the same type at the same address can be sorted in arbitrary order. */
11739
11740 if (a->owner->number != b->owner->number)
11741 return ((a->owner->number > b->owner->number)
11742 - (a->owner->number < b->owner->number));
11743
11744 return (a > b) - (a < b);
11745 }
11746
11747 /* Set bp_locations_placed_address_before_address_max and
11748 bp_locations_shadow_len_after_address_max according to the current
11749 content of the bp_locations array. */
11750
11751 static void
11752 bp_locations_target_extensions_update (void)
11753 {
11754 struct bp_location *bl, **blp_tmp;
11755
11756 bp_locations_placed_address_before_address_max = 0;
11757 bp_locations_shadow_len_after_address_max = 0;
11758
11759 ALL_BP_LOCATIONS (bl, blp_tmp)
11760 {
11761 CORE_ADDR start, end, addr;
11762
11763 if (!bp_location_has_shadow (bl))
11764 continue;
11765
11766 start = bl->target_info.placed_address;
11767 end = start + bl->target_info.shadow_len;
11768
11769 gdb_assert (bl->address >= start);
11770 addr = bl->address - start;
11771 if (addr > bp_locations_placed_address_before_address_max)
11772 bp_locations_placed_address_before_address_max = addr;
11773
11774 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11775
11776 gdb_assert (bl->address < end);
11777 addr = end - bl->address;
11778 if (addr > bp_locations_shadow_len_after_address_max)
11779 bp_locations_shadow_len_after_address_max = addr;
11780 }
11781 }
11782
11783 /* Download tracepoint locations if they haven't been. */
11784
11785 static void
11786 download_tracepoint_locations (void)
11787 {
11788 struct breakpoint *b;
11789 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11790
11791 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11792
11793 ALL_TRACEPOINTS (b)
11794 {
11795 struct bp_location *bl;
11796 struct tracepoint *t;
11797 int bp_location_downloaded = 0;
11798
11799 if ((b->type == bp_fast_tracepoint
11800 ? !may_insert_fast_tracepoints
11801 : !may_insert_tracepoints))
11802 continue;
11803
11804 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11805 {
11806 if (target_can_download_tracepoint ())
11807 can_download_tracepoint = TRIBOOL_TRUE;
11808 else
11809 can_download_tracepoint = TRIBOOL_FALSE;
11810 }
11811
11812 if (can_download_tracepoint == TRIBOOL_FALSE)
11813 break;
11814
11815 for (bl = b->loc; bl; bl = bl->next)
11816 {
11817 /* In tracepoint, locations are _never_ duplicated, so
11818 should_be_inserted is equivalent to
11819 unduplicated_should_be_inserted. */
11820 if (!should_be_inserted (bl) || bl->inserted)
11821 continue;
11822
11823 switch_to_program_space_and_thread (bl->pspace);
11824
11825 target_download_tracepoint (bl);
11826
11827 bl->inserted = 1;
11828 bp_location_downloaded = 1;
11829 }
11830 t = (struct tracepoint *) b;
11831 t->number_on_target = b->number;
11832 if (bp_location_downloaded)
11833 observer_notify_breakpoint_modified (b);
11834 }
11835 }
11836
11837 /* Swap the insertion/duplication state between two locations. */
11838
11839 static void
11840 swap_insertion (struct bp_location *left, struct bp_location *right)
11841 {
11842 const int left_inserted = left->inserted;
11843 const int left_duplicate = left->duplicate;
11844 const int left_needs_update = left->needs_update;
11845 const struct bp_target_info left_target_info = left->target_info;
11846
11847 /* Locations of tracepoints can never be duplicated. */
11848 if (is_tracepoint (left->owner))
11849 gdb_assert (!left->duplicate);
11850 if (is_tracepoint (right->owner))
11851 gdb_assert (!right->duplicate);
11852
11853 left->inserted = right->inserted;
11854 left->duplicate = right->duplicate;
11855 left->needs_update = right->needs_update;
11856 left->target_info = right->target_info;
11857 right->inserted = left_inserted;
11858 right->duplicate = left_duplicate;
11859 right->needs_update = left_needs_update;
11860 right->target_info = left_target_info;
11861 }
11862
11863 /* Force the re-insertion of the locations at ADDRESS. This is called
11864 once a new/deleted/modified duplicate location is found and we are evaluating
11865 conditions on the target's side. Such conditions need to be updated on
11866 the target. */
11867
11868 static void
11869 force_breakpoint_reinsertion (struct bp_location *bl)
11870 {
11871 struct bp_location **locp = NULL, **loc2p;
11872 struct bp_location *loc;
11873 CORE_ADDR address = 0;
11874 int pspace_num;
11875
11876 address = bl->address;
11877 pspace_num = bl->pspace->num;
11878
11879 /* This is only meaningful if the target is
11880 evaluating conditions and if the user has
11881 opted for condition evaluation on the target's
11882 side. */
11883 if (gdb_evaluates_breakpoint_condition_p ()
11884 || !target_supports_evaluation_of_breakpoint_conditions ())
11885 return;
11886
11887 /* Flag all breakpoint locations with this address and
11888 the same program space as the location
11889 as "its condition has changed". We need to
11890 update the conditions on the target's side. */
11891 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11892 {
11893 loc = *loc2p;
11894
11895 if (!is_breakpoint (loc->owner)
11896 || pspace_num != loc->pspace->num)
11897 continue;
11898
11899 /* Flag the location appropriately. We use a different state to
11900 let everyone know that we already updated the set of locations
11901 with addr bl->address and program space bl->pspace. This is so
11902 we don't have to keep calling these functions just to mark locations
11903 that have already been marked. */
11904 loc->condition_changed = condition_updated;
11905
11906 /* Free the agent expression bytecode as well. We will compute
11907 it later on. */
11908 loc->cond_bytecode.reset ();
11909 }
11910 }
11911 /* Called whether new breakpoints are created, or existing breakpoints
11912 deleted, to update the global location list and recompute which
11913 locations are duplicate of which.
11914
11915 The INSERT_MODE flag determines whether locations may not, may, or
11916 shall be inserted now. See 'enum ugll_insert_mode' for more
11917 info. */
11918
11919 static void
11920 update_global_location_list (enum ugll_insert_mode insert_mode)
11921 {
11922 struct breakpoint *b;
11923 struct bp_location **locp, *loc;
11924 /* Last breakpoint location address that was marked for update. */
11925 CORE_ADDR last_addr = 0;
11926 /* Last breakpoint location program space that was marked for update. */
11927 int last_pspace_num = -1;
11928
11929 /* Used in the duplicates detection below. When iterating over all
11930 bp_locations, points to the first bp_location of a given address.
11931 Breakpoints and watchpoints of different types are never
11932 duplicates of each other. Keep one pointer for each type of
11933 breakpoint/watchpoint, so we only need to loop over all locations
11934 once. */
11935 struct bp_location *bp_loc_first; /* breakpoint */
11936 struct bp_location *wp_loc_first; /* hardware watchpoint */
11937 struct bp_location *awp_loc_first; /* access watchpoint */
11938 struct bp_location *rwp_loc_first; /* read watchpoint */
11939
11940 /* Saved former bp_locations array which we compare against the newly
11941 built bp_locations from the current state of ALL_BREAKPOINTS. */
11942 struct bp_location **old_locp;
11943 unsigned old_locations_count;
11944 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11945
11946 old_locations_count = bp_locations_count;
11947 bp_locations = NULL;
11948 bp_locations_count = 0;
11949
11950 ALL_BREAKPOINTS (b)
11951 for (loc = b->loc; loc; loc = loc->next)
11952 bp_locations_count++;
11953
11954 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11955 locp = bp_locations;
11956 ALL_BREAKPOINTS (b)
11957 for (loc = b->loc; loc; loc = loc->next)
11958 *locp++ = loc;
11959 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11960 bp_locations_compare);
11961
11962 bp_locations_target_extensions_update ();
11963
11964 /* Identify bp_location instances that are no longer present in the
11965 new list, and therefore should be freed. Note that it's not
11966 necessary that those locations should be removed from inferior --
11967 if there's another location at the same address (previously
11968 marked as duplicate), we don't need to remove/insert the
11969 location.
11970
11971 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11972 and former bp_location array state respectively. */
11973
11974 locp = bp_locations;
11975 for (old_locp = old_locations.get ();
11976 old_locp < old_locations.get () + old_locations_count;
11977 old_locp++)
11978 {
11979 struct bp_location *old_loc = *old_locp;
11980 struct bp_location **loc2p;
11981
11982 /* Tells if 'old_loc' is found among the new locations. If
11983 not, we have to free it. */
11984 int found_object = 0;
11985 /* Tells if the location should remain inserted in the target. */
11986 int keep_in_target = 0;
11987 int removed = 0;
11988
11989 /* Skip LOCP entries which will definitely never be needed.
11990 Stop either at or being the one matching OLD_LOC. */
11991 while (locp < bp_locations + bp_locations_count
11992 && (*locp)->address < old_loc->address)
11993 locp++;
11994
11995 for (loc2p = locp;
11996 (loc2p < bp_locations + bp_locations_count
11997 && (*loc2p)->address == old_loc->address);
11998 loc2p++)
11999 {
12000 /* Check if this is a new/duplicated location or a duplicated
12001 location that had its condition modified. If so, we want to send
12002 its condition to the target if evaluation of conditions is taking
12003 place there. */
12004 if ((*loc2p)->condition_changed == condition_modified
12005 && (last_addr != old_loc->address
12006 || last_pspace_num != old_loc->pspace->num))
12007 {
12008 force_breakpoint_reinsertion (*loc2p);
12009 last_pspace_num = old_loc->pspace->num;
12010 }
12011
12012 if (*loc2p == old_loc)
12013 found_object = 1;
12014 }
12015
12016 /* We have already handled this address, update it so that we don't
12017 have to go through updates again. */
12018 last_addr = old_loc->address;
12019
12020 /* Target-side condition evaluation: Handle deleted locations. */
12021 if (!found_object)
12022 force_breakpoint_reinsertion (old_loc);
12023
12024 /* If this location is no longer present, and inserted, look if
12025 there's maybe a new location at the same address. If so,
12026 mark that one inserted, and don't remove this one. This is
12027 needed so that we don't have a time window where a breakpoint
12028 at certain location is not inserted. */
12029
12030 if (old_loc->inserted)
12031 {
12032 /* If the location is inserted now, we might have to remove
12033 it. */
12034
12035 if (found_object && should_be_inserted (old_loc))
12036 {
12037 /* The location is still present in the location list,
12038 and still should be inserted. Don't do anything. */
12039 keep_in_target = 1;
12040 }
12041 else
12042 {
12043 /* This location still exists, but it won't be kept in the
12044 target since it may have been disabled. We proceed to
12045 remove its target-side condition. */
12046
12047 /* The location is either no longer present, or got
12048 disabled. See if there's another location at the
12049 same address, in which case we don't need to remove
12050 this one from the target. */
12051
12052 /* OLD_LOC comes from existing struct breakpoint. */
12053 if (breakpoint_address_is_meaningful (old_loc->owner))
12054 {
12055 for (loc2p = locp;
12056 (loc2p < bp_locations + bp_locations_count
12057 && (*loc2p)->address == old_loc->address);
12058 loc2p++)
12059 {
12060 struct bp_location *loc2 = *loc2p;
12061
12062 if (breakpoint_locations_match (loc2, old_loc))
12063 {
12064 /* Read watchpoint locations are switched to
12065 access watchpoints, if the former are not
12066 supported, but the latter are. */
12067 if (is_hardware_watchpoint (old_loc->owner))
12068 {
12069 gdb_assert (is_hardware_watchpoint (loc2->owner));
12070 loc2->watchpoint_type = old_loc->watchpoint_type;
12071 }
12072
12073 /* loc2 is a duplicated location. We need to check
12074 if it should be inserted in case it will be
12075 unduplicated. */
12076 if (loc2 != old_loc
12077 && unduplicated_should_be_inserted (loc2))
12078 {
12079 swap_insertion (old_loc, loc2);
12080 keep_in_target = 1;
12081 break;
12082 }
12083 }
12084 }
12085 }
12086 }
12087
12088 if (!keep_in_target)
12089 {
12090 if (remove_breakpoint (old_loc))
12091 {
12092 /* This is just about all we can do. We could keep
12093 this location on the global list, and try to
12094 remove it next time, but there's no particular
12095 reason why we will succeed next time.
12096
12097 Note that at this point, old_loc->owner is still
12098 valid, as delete_breakpoint frees the breakpoint
12099 only after calling us. */
12100 printf_filtered (_("warning: Error removing "
12101 "breakpoint %d\n"),
12102 old_loc->owner->number);
12103 }
12104 removed = 1;
12105 }
12106 }
12107
12108 if (!found_object)
12109 {
12110 if (removed && target_is_non_stop_p ()
12111 && need_moribund_for_location_type (old_loc))
12112 {
12113 /* This location was removed from the target. In
12114 non-stop mode, a race condition is possible where
12115 we've removed a breakpoint, but stop events for that
12116 breakpoint are already queued and will arrive later.
12117 We apply an heuristic to be able to distinguish such
12118 SIGTRAPs from other random SIGTRAPs: we keep this
12119 breakpoint location for a bit, and will retire it
12120 after we see some number of events. The theory here
12121 is that reporting of events should, "on the average",
12122 be fair, so after a while we'll see events from all
12123 threads that have anything of interest, and no longer
12124 need to keep this breakpoint location around. We
12125 don't hold locations forever so to reduce chances of
12126 mistaking a non-breakpoint SIGTRAP for a breakpoint
12127 SIGTRAP.
12128
12129 The heuristic failing can be disastrous on
12130 decr_pc_after_break targets.
12131
12132 On decr_pc_after_break targets, like e.g., x86-linux,
12133 if we fail to recognize a late breakpoint SIGTRAP,
12134 because events_till_retirement has reached 0 too
12135 soon, we'll fail to do the PC adjustment, and report
12136 a random SIGTRAP to the user. When the user resumes
12137 the inferior, it will most likely immediately crash
12138 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12139 corrupted, because of being resumed e.g., in the
12140 middle of a multi-byte instruction, or skipped a
12141 one-byte instruction. This was actually seen happen
12142 on native x86-linux, and should be less rare on
12143 targets that do not support new thread events, like
12144 remote, due to the heuristic depending on
12145 thread_count.
12146
12147 Mistaking a random SIGTRAP for a breakpoint trap
12148 causes similar symptoms (PC adjustment applied when
12149 it shouldn't), but then again, playing with SIGTRAPs
12150 behind the debugger's back is asking for trouble.
12151
12152 Since hardware watchpoint traps are always
12153 distinguishable from other traps, so we don't need to
12154 apply keep hardware watchpoint moribund locations
12155 around. We simply always ignore hardware watchpoint
12156 traps we can no longer explain. */
12157
12158 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12159 old_loc->owner = NULL;
12160
12161 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12162 }
12163 else
12164 {
12165 old_loc->owner = NULL;
12166 decref_bp_location (&old_loc);
12167 }
12168 }
12169 }
12170
12171 /* Rescan breakpoints at the same address and section, marking the
12172 first one as "first" and any others as "duplicates". This is so
12173 that the bpt instruction is only inserted once. If we have a
12174 permanent breakpoint at the same place as BPT, make that one the
12175 official one, and the rest as duplicates. Permanent breakpoints
12176 are sorted first for the same address.
12177
12178 Do the same for hardware watchpoints, but also considering the
12179 watchpoint's type (regular/access/read) and length. */
12180
12181 bp_loc_first = NULL;
12182 wp_loc_first = NULL;
12183 awp_loc_first = NULL;
12184 rwp_loc_first = NULL;
12185 ALL_BP_LOCATIONS (loc, locp)
12186 {
12187 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12188 non-NULL. */
12189 struct bp_location **loc_first_p;
12190 b = loc->owner;
12191
12192 if (!unduplicated_should_be_inserted (loc)
12193 || !breakpoint_address_is_meaningful (b)
12194 /* Don't detect duplicate for tracepoint locations because they are
12195 never duplicated. See the comments in field `duplicate' of
12196 `struct bp_location'. */
12197 || is_tracepoint (b))
12198 {
12199 /* Clear the condition modification flag. */
12200 loc->condition_changed = condition_unchanged;
12201 continue;
12202 }
12203
12204 if (b->type == bp_hardware_watchpoint)
12205 loc_first_p = &wp_loc_first;
12206 else if (b->type == bp_read_watchpoint)
12207 loc_first_p = &rwp_loc_first;
12208 else if (b->type == bp_access_watchpoint)
12209 loc_first_p = &awp_loc_first;
12210 else
12211 loc_first_p = &bp_loc_first;
12212
12213 if (*loc_first_p == NULL
12214 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12215 || !breakpoint_locations_match (loc, *loc_first_p))
12216 {
12217 *loc_first_p = loc;
12218 loc->duplicate = 0;
12219
12220 if (is_breakpoint (loc->owner) && loc->condition_changed)
12221 {
12222 loc->needs_update = 1;
12223 /* Clear the condition modification flag. */
12224 loc->condition_changed = condition_unchanged;
12225 }
12226 continue;
12227 }
12228
12229
12230 /* This and the above ensure the invariant that the first location
12231 is not duplicated, and is the inserted one.
12232 All following are marked as duplicated, and are not inserted. */
12233 if (loc->inserted)
12234 swap_insertion (loc, *loc_first_p);
12235 loc->duplicate = 1;
12236
12237 /* Clear the condition modification flag. */
12238 loc->condition_changed = condition_unchanged;
12239 }
12240
12241 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12242 {
12243 if (insert_mode != UGLL_DONT_INSERT)
12244 insert_breakpoint_locations ();
12245 else
12246 {
12247 /* Even though the caller told us to not insert new
12248 locations, we may still need to update conditions on the
12249 target's side of breakpoints that were already inserted
12250 if the target is evaluating breakpoint conditions. We
12251 only update conditions for locations that are marked
12252 "needs_update". */
12253 update_inserted_breakpoint_locations ();
12254 }
12255 }
12256
12257 if (insert_mode != UGLL_DONT_INSERT)
12258 download_tracepoint_locations ();
12259 }
12260
12261 void
12262 breakpoint_retire_moribund (void)
12263 {
12264 struct bp_location *loc;
12265 int ix;
12266
12267 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12268 if (--(loc->events_till_retirement) == 0)
12269 {
12270 decref_bp_location (&loc);
12271 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12272 --ix;
12273 }
12274 }
12275
12276 static void
12277 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12278 {
12279
12280 TRY
12281 {
12282 update_global_location_list (insert_mode);
12283 }
12284 CATCH (e, RETURN_MASK_ERROR)
12285 {
12286 }
12287 END_CATCH
12288 }
12289
12290 /* Clear BKP from a BPS. */
12291
12292 static void
12293 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12294 {
12295 bpstat bs;
12296
12297 for (bs = bps; bs; bs = bs->next)
12298 if (bs->breakpoint_at == bpt)
12299 {
12300 bs->breakpoint_at = NULL;
12301 bs->old_val = NULL;
12302 /* bs->commands will be freed later. */
12303 }
12304 }
12305
12306 /* Callback for iterate_over_threads. */
12307 static int
12308 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12309 {
12310 struct breakpoint *bpt = (struct breakpoint *) data;
12311
12312 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12313 return 0;
12314 }
12315
12316 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12317 callbacks. */
12318
12319 static void
12320 say_where (struct breakpoint *b)
12321 {
12322 struct value_print_options opts;
12323
12324 get_user_print_options (&opts);
12325
12326 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12327 single string. */
12328 if (b->loc == NULL)
12329 {
12330 /* For pending locations, the output differs slightly based
12331 on b->extra_string. If this is non-NULL, it contains either
12332 a condition or dprintf arguments. */
12333 if (b->extra_string == NULL)
12334 {
12335 printf_filtered (_(" (%s) pending."),
12336 event_location_to_string (b->location.get ()));
12337 }
12338 else if (b->type == bp_dprintf)
12339 {
12340 printf_filtered (_(" (%s,%s) pending."),
12341 event_location_to_string (b->location.get ()),
12342 b->extra_string);
12343 }
12344 else
12345 {
12346 printf_filtered (_(" (%s %s) pending."),
12347 event_location_to_string (b->location.get ()),
12348 b->extra_string);
12349 }
12350 }
12351 else
12352 {
12353 if (opts.addressprint || b->loc->symtab == NULL)
12354 {
12355 printf_filtered (" at ");
12356 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12357 gdb_stdout);
12358 }
12359 if (b->loc->symtab != NULL)
12360 {
12361 /* If there is a single location, we can print the location
12362 more nicely. */
12363 if (b->loc->next == NULL)
12364 printf_filtered (": file %s, line %d.",
12365 symtab_to_filename_for_display (b->loc->symtab),
12366 b->loc->line_number);
12367 else
12368 /* This is not ideal, but each location may have a
12369 different file name, and this at least reflects the
12370 real situation somewhat. */
12371 printf_filtered (": %s.",
12372 event_location_to_string (b->location.get ()));
12373 }
12374
12375 if (b->loc->next)
12376 {
12377 struct bp_location *loc = b->loc;
12378 int n = 0;
12379 for (; loc; loc = loc->next)
12380 ++n;
12381 printf_filtered (" (%d locations)", n);
12382 }
12383 }
12384 }
12385
12386 /* Default bp_location_ops methods. */
12387
12388 static void
12389 bp_location_dtor (struct bp_location *self)
12390 {
12391 xfree (self->function_name);
12392 }
12393
12394 static const struct bp_location_ops bp_location_ops =
12395 {
12396 bp_location_dtor
12397 };
12398
12399 /* Destructor for the breakpoint base class. */
12400
12401 breakpoint::~breakpoint ()
12402 {
12403 xfree (this->cond_string);
12404 xfree (this->extra_string);
12405 xfree (this->filter);
12406 }
12407
12408 static struct bp_location *
12409 base_breakpoint_allocate_location (struct breakpoint *self)
12410 {
12411 return new bp_location (&bp_location_ops, self);
12412 }
12413
12414 static void
12415 base_breakpoint_re_set (struct breakpoint *b)
12416 {
12417 /* Nothing to re-set. */
12418 }
12419
12420 #define internal_error_pure_virtual_called() \
12421 gdb_assert_not_reached ("pure virtual function called")
12422
12423 static int
12424 base_breakpoint_insert_location (struct bp_location *bl)
12425 {
12426 internal_error_pure_virtual_called ();
12427 }
12428
12429 static int
12430 base_breakpoint_remove_location (struct bp_location *bl,
12431 enum remove_bp_reason reason)
12432 {
12433 internal_error_pure_virtual_called ();
12434 }
12435
12436 static int
12437 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12438 const address_space *aspace,
12439 CORE_ADDR bp_addr,
12440 const struct target_waitstatus *ws)
12441 {
12442 internal_error_pure_virtual_called ();
12443 }
12444
12445 static void
12446 base_breakpoint_check_status (bpstat bs)
12447 {
12448 /* Always stop. */
12449 }
12450
12451 /* A "works_in_software_mode" breakpoint_ops method that just internal
12452 errors. */
12453
12454 static int
12455 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12456 {
12457 internal_error_pure_virtual_called ();
12458 }
12459
12460 /* A "resources_needed" breakpoint_ops method that just internal
12461 errors. */
12462
12463 static int
12464 base_breakpoint_resources_needed (const struct bp_location *bl)
12465 {
12466 internal_error_pure_virtual_called ();
12467 }
12468
12469 static enum print_stop_action
12470 base_breakpoint_print_it (bpstat bs)
12471 {
12472 internal_error_pure_virtual_called ();
12473 }
12474
12475 static void
12476 base_breakpoint_print_one_detail (const struct breakpoint *self,
12477 struct ui_out *uiout)
12478 {
12479 /* nothing */
12480 }
12481
12482 static void
12483 base_breakpoint_print_mention (struct breakpoint *b)
12484 {
12485 internal_error_pure_virtual_called ();
12486 }
12487
12488 static void
12489 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12490 {
12491 internal_error_pure_virtual_called ();
12492 }
12493
12494 static void
12495 base_breakpoint_create_sals_from_location
12496 (const struct event_location *location,
12497 struct linespec_result *canonical,
12498 enum bptype type_wanted)
12499 {
12500 internal_error_pure_virtual_called ();
12501 }
12502
12503 static void
12504 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12505 struct linespec_result *c,
12506 gdb::unique_xmalloc_ptr<char> cond_string,
12507 gdb::unique_xmalloc_ptr<char> extra_string,
12508 enum bptype type_wanted,
12509 enum bpdisp disposition,
12510 int thread,
12511 int task, int ignore_count,
12512 const struct breakpoint_ops *o,
12513 int from_tty, int enabled,
12514 int internal, unsigned flags)
12515 {
12516 internal_error_pure_virtual_called ();
12517 }
12518
12519 static std::vector<symtab_and_line>
12520 base_breakpoint_decode_location (struct breakpoint *b,
12521 const struct event_location *location,
12522 struct program_space *search_pspace)
12523 {
12524 internal_error_pure_virtual_called ();
12525 }
12526
12527 /* The default 'explains_signal' method. */
12528
12529 static int
12530 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12531 {
12532 return 1;
12533 }
12534
12535 /* The default "after_condition_true" method. */
12536
12537 static void
12538 base_breakpoint_after_condition_true (struct bpstats *bs)
12539 {
12540 /* Nothing to do. */
12541 }
12542
12543 struct breakpoint_ops base_breakpoint_ops =
12544 {
12545 base_breakpoint_allocate_location,
12546 base_breakpoint_re_set,
12547 base_breakpoint_insert_location,
12548 base_breakpoint_remove_location,
12549 base_breakpoint_breakpoint_hit,
12550 base_breakpoint_check_status,
12551 base_breakpoint_resources_needed,
12552 base_breakpoint_works_in_software_mode,
12553 base_breakpoint_print_it,
12554 NULL,
12555 base_breakpoint_print_one_detail,
12556 base_breakpoint_print_mention,
12557 base_breakpoint_print_recreate,
12558 base_breakpoint_create_sals_from_location,
12559 base_breakpoint_create_breakpoints_sal,
12560 base_breakpoint_decode_location,
12561 base_breakpoint_explains_signal,
12562 base_breakpoint_after_condition_true,
12563 };
12564
12565 /* Default breakpoint_ops methods. */
12566
12567 static void
12568 bkpt_re_set (struct breakpoint *b)
12569 {
12570 /* FIXME: is this still reachable? */
12571 if (breakpoint_event_location_empty_p (b))
12572 {
12573 /* Anything without a location can't be re-set. */
12574 delete_breakpoint (b);
12575 return;
12576 }
12577
12578 breakpoint_re_set_default (b);
12579 }
12580
12581 static int
12582 bkpt_insert_location (struct bp_location *bl)
12583 {
12584 CORE_ADDR addr = bl->target_info.reqstd_address;
12585
12586 bl->target_info.kind = breakpoint_kind (bl, &addr);
12587 bl->target_info.placed_address = addr;
12588
12589 if (bl->loc_type == bp_loc_hardware_breakpoint)
12590 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12591 else
12592 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12593 }
12594
12595 static int
12596 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12597 {
12598 if (bl->loc_type == bp_loc_hardware_breakpoint)
12599 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12600 else
12601 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12602 }
12603
12604 static int
12605 bkpt_breakpoint_hit (const struct bp_location *bl,
12606 const address_space *aspace, CORE_ADDR bp_addr,
12607 const struct target_waitstatus *ws)
12608 {
12609 if (ws->kind != TARGET_WAITKIND_STOPPED
12610 || ws->value.sig != GDB_SIGNAL_TRAP)
12611 return 0;
12612
12613 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12614 aspace, bp_addr))
12615 return 0;
12616
12617 if (overlay_debugging /* unmapped overlay section */
12618 && section_is_overlay (bl->section)
12619 && !section_is_mapped (bl->section))
12620 return 0;
12621
12622 return 1;
12623 }
12624
12625 static int
12626 dprintf_breakpoint_hit (const struct bp_location *bl,
12627 const address_space *aspace, CORE_ADDR bp_addr,
12628 const struct target_waitstatus *ws)
12629 {
12630 if (dprintf_style == dprintf_style_agent
12631 && target_can_run_breakpoint_commands ())
12632 {
12633 /* An agent-style dprintf never causes a stop. If we see a trap
12634 for this address it must be for a breakpoint that happens to
12635 be set at the same address. */
12636 return 0;
12637 }
12638
12639 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12640 }
12641
12642 static int
12643 bkpt_resources_needed (const struct bp_location *bl)
12644 {
12645 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12646
12647 return 1;
12648 }
12649
12650 static enum print_stop_action
12651 bkpt_print_it (bpstat bs)
12652 {
12653 struct breakpoint *b;
12654 const struct bp_location *bl;
12655 int bp_temp;
12656 struct ui_out *uiout = current_uiout;
12657
12658 gdb_assert (bs->bp_location_at != NULL);
12659
12660 bl = bs->bp_location_at;
12661 b = bs->breakpoint_at;
12662
12663 bp_temp = b->disposition == disp_del;
12664 if (bl->address != bl->requested_address)
12665 breakpoint_adjustment_warning (bl->requested_address,
12666 bl->address,
12667 b->number, 1);
12668 annotate_breakpoint (b->number);
12669 maybe_print_thread_hit_breakpoint (uiout);
12670
12671 if (bp_temp)
12672 uiout->text ("Temporary breakpoint ");
12673 else
12674 uiout->text ("Breakpoint ");
12675 if (uiout->is_mi_like_p ())
12676 {
12677 uiout->field_string ("reason",
12678 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12679 uiout->field_string ("disp", bpdisp_text (b->disposition));
12680 }
12681 uiout->field_int ("bkptno", b->number);
12682 uiout->text (", ");
12683
12684 return PRINT_SRC_AND_LOC;
12685 }
12686
12687 static void
12688 bkpt_print_mention (struct breakpoint *b)
12689 {
12690 if (current_uiout->is_mi_like_p ())
12691 return;
12692
12693 switch (b->type)
12694 {
12695 case bp_breakpoint:
12696 case bp_gnu_ifunc_resolver:
12697 if (b->disposition == disp_del)
12698 printf_filtered (_("Temporary breakpoint"));
12699 else
12700 printf_filtered (_("Breakpoint"));
12701 printf_filtered (_(" %d"), b->number);
12702 if (b->type == bp_gnu_ifunc_resolver)
12703 printf_filtered (_(" at gnu-indirect-function resolver"));
12704 break;
12705 case bp_hardware_breakpoint:
12706 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12707 break;
12708 case bp_dprintf:
12709 printf_filtered (_("Dprintf %d"), b->number);
12710 break;
12711 }
12712
12713 say_where (b);
12714 }
12715
12716 static void
12717 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12718 {
12719 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12720 fprintf_unfiltered (fp, "tbreak");
12721 else if (tp->type == bp_breakpoint)
12722 fprintf_unfiltered (fp, "break");
12723 else if (tp->type == bp_hardware_breakpoint
12724 && tp->disposition == disp_del)
12725 fprintf_unfiltered (fp, "thbreak");
12726 else if (tp->type == bp_hardware_breakpoint)
12727 fprintf_unfiltered (fp, "hbreak");
12728 else
12729 internal_error (__FILE__, __LINE__,
12730 _("unhandled breakpoint type %d"), (int) tp->type);
12731
12732 fprintf_unfiltered (fp, " %s",
12733 event_location_to_string (tp->location.get ()));
12734
12735 /* Print out extra_string if this breakpoint is pending. It might
12736 contain, for example, conditions that were set by the user. */
12737 if (tp->loc == NULL && tp->extra_string != NULL)
12738 fprintf_unfiltered (fp, " %s", tp->extra_string);
12739
12740 print_recreate_thread (tp, fp);
12741 }
12742
12743 static void
12744 bkpt_create_sals_from_location (const struct event_location *location,
12745 struct linespec_result *canonical,
12746 enum bptype type_wanted)
12747 {
12748 create_sals_from_location_default (location, canonical, type_wanted);
12749 }
12750
12751 static void
12752 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12753 struct linespec_result *canonical,
12754 gdb::unique_xmalloc_ptr<char> cond_string,
12755 gdb::unique_xmalloc_ptr<char> extra_string,
12756 enum bptype type_wanted,
12757 enum bpdisp disposition,
12758 int thread,
12759 int task, int ignore_count,
12760 const struct breakpoint_ops *ops,
12761 int from_tty, int enabled,
12762 int internal, unsigned flags)
12763 {
12764 create_breakpoints_sal_default (gdbarch, canonical,
12765 std::move (cond_string),
12766 std::move (extra_string),
12767 type_wanted,
12768 disposition, thread, task,
12769 ignore_count, ops, from_tty,
12770 enabled, internal, flags);
12771 }
12772
12773 static std::vector<symtab_and_line>
12774 bkpt_decode_location (struct breakpoint *b,
12775 const struct event_location *location,
12776 struct program_space *search_pspace)
12777 {
12778 return decode_location_default (b, location, search_pspace);
12779 }
12780
12781 /* Virtual table for internal breakpoints. */
12782
12783 static void
12784 internal_bkpt_re_set (struct breakpoint *b)
12785 {
12786 switch (b->type)
12787 {
12788 /* Delete overlay event and longjmp master breakpoints; they
12789 will be reset later by breakpoint_re_set. */
12790 case bp_overlay_event:
12791 case bp_longjmp_master:
12792 case bp_std_terminate_master:
12793 case bp_exception_master:
12794 delete_breakpoint (b);
12795 break;
12796
12797 /* This breakpoint is special, it's set up when the inferior
12798 starts and we really don't want to touch it. */
12799 case bp_shlib_event:
12800
12801 /* Like bp_shlib_event, this breakpoint type is special. Once
12802 it is set up, we do not want to touch it. */
12803 case bp_thread_event:
12804 break;
12805 }
12806 }
12807
12808 static void
12809 internal_bkpt_check_status (bpstat bs)
12810 {
12811 if (bs->breakpoint_at->type == bp_shlib_event)
12812 {
12813 /* If requested, stop when the dynamic linker notifies GDB of
12814 events. This allows the user to get control and place
12815 breakpoints in initializer routines for dynamically loaded
12816 objects (among other things). */
12817 bs->stop = stop_on_solib_events;
12818 bs->print = stop_on_solib_events;
12819 }
12820 else
12821 bs->stop = 0;
12822 }
12823
12824 static enum print_stop_action
12825 internal_bkpt_print_it (bpstat bs)
12826 {
12827 struct breakpoint *b;
12828
12829 b = bs->breakpoint_at;
12830
12831 switch (b->type)
12832 {
12833 case bp_shlib_event:
12834 /* Did we stop because the user set the stop_on_solib_events
12835 variable? (If so, we report this as a generic, "Stopped due
12836 to shlib event" message.) */
12837 print_solib_event (0);
12838 break;
12839
12840 case bp_thread_event:
12841 /* Not sure how we will get here.
12842 GDB should not stop for these breakpoints. */
12843 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12844 break;
12845
12846 case bp_overlay_event:
12847 /* By analogy with the thread event, GDB should not stop for these. */
12848 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12849 break;
12850
12851 case bp_longjmp_master:
12852 /* These should never be enabled. */
12853 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12854 break;
12855
12856 case bp_std_terminate_master:
12857 /* These should never be enabled. */
12858 printf_filtered (_("std::terminate Master Breakpoint: "
12859 "gdb should not stop!\n"));
12860 break;
12861
12862 case bp_exception_master:
12863 /* These should never be enabled. */
12864 printf_filtered (_("Exception Master Breakpoint: "
12865 "gdb should not stop!\n"));
12866 break;
12867 }
12868
12869 return PRINT_NOTHING;
12870 }
12871
12872 static void
12873 internal_bkpt_print_mention (struct breakpoint *b)
12874 {
12875 /* Nothing to mention. These breakpoints are internal. */
12876 }
12877
12878 /* Virtual table for momentary breakpoints */
12879
12880 static void
12881 momentary_bkpt_re_set (struct breakpoint *b)
12882 {
12883 /* Keep temporary breakpoints, which can be encountered when we step
12884 over a dlopen call and solib_add is resetting the breakpoints.
12885 Otherwise these should have been blown away via the cleanup chain
12886 or by breakpoint_init_inferior when we rerun the executable. */
12887 }
12888
12889 static void
12890 momentary_bkpt_check_status (bpstat bs)
12891 {
12892 /* Nothing. The point of these breakpoints is causing a stop. */
12893 }
12894
12895 static enum print_stop_action
12896 momentary_bkpt_print_it (bpstat bs)
12897 {
12898 return PRINT_UNKNOWN;
12899 }
12900
12901 static void
12902 momentary_bkpt_print_mention (struct breakpoint *b)
12903 {
12904 /* Nothing to mention. These breakpoints are internal. */
12905 }
12906
12907 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12908
12909 It gets cleared already on the removal of the first one of such placed
12910 breakpoints. This is OK as they get all removed altogether. */
12911
12912 longjmp_breakpoint::~longjmp_breakpoint ()
12913 {
12914 thread_info *tp = find_thread_global_id (this->thread);
12915
12916 if (tp != NULL)
12917 tp->initiating_frame = null_frame_id;
12918 }
12919
12920 /* Specific methods for probe breakpoints. */
12921
12922 static int
12923 bkpt_probe_insert_location (struct bp_location *bl)
12924 {
12925 int v = bkpt_insert_location (bl);
12926
12927 if (v == 0)
12928 {
12929 /* The insertion was successful, now let's set the probe's semaphore
12930 if needed. */
12931 if (bl->probe.probe->pops->set_semaphore != NULL)
12932 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
12933 bl->probe.objfile,
12934 bl->gdbarch);
12935 }
12936
12937 return v;
12938 }
12939
12940 static int
12941 bkpt_probe_remove_location (struct bp_location *bl,
12942 enum remove_bp_reason reason)
12943 {
12944 /* Let's clear the semaphore before removing the location. */
12945 if (bl->probe.probe->pops->clear_semaphore != NULL)
12946 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
12947 bl->probe.objfile,
12948 bl->gdbarch);
12949
12950 return bkpt_remove_location (bl, reason);
12951 }
12952
12953 static void
12954 bkpt_probe_create_sals_from_location (const struct event_location *location,
12955 struct linespec_result *canonical,
12956 enum bptype type_wanted)
12957 {
12958 struct linespec_sals lsal;
12959
12960 lsal.sals = parse_probes (location, NULL, canonical);
12961 lsal.canonical
12962 = xstrdup (event_location_to_string (canonical->location.get ()));
12963 canonical->lsals.push_back (std::move (lsal));
12964 }
12965
12966 static std::vector<symtab_and_line>
12967 bkpt_probe_decode_location (struct breakpoint *b,
12968 const struct event_location *location,
12969 struct program_space *search_pspace)
12970 {
12971 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12972 if (sals.empty ())
12973 error (_("probe not found"));
12974 return sals;
12975 }
12976
12977 /* The breakpoint_ops structure to be used in tracepoints. */
12978
12979 static void
12980 tracepoint_re_set (struct breakpoint *b)
12981 {
12982 breakpoint_re_set_default (b);
12983 }
12984
12985 static int
12986 tracepoint_breakpoint_hit (const struct bp_location *bl,
12987 const address_space *aspace, CORE_ADDR bp_addr,
12988 const struct target_waitstatus *ws)
12989 {
12990 /* By definition, the inferior does not report stops at
12991 tracepoints. */
12992 return 0;
12993 }
12994
12995 static void
12996 tracepoint_print_one_detail (const struct breakpoint *self,
12997 struct ui_out *uiout)
12998 {
12999 struct tracepoint *tp = (struct tracepoint *) self;
13000 if (tp->static_trace_marker_id)
13001 {
13002 gdb_assert (self->type == bp_static_tracepoint);
13003
13004 uiout->text ("\tmarker id is ");
13005 uiout->field_string ("static-tracepoint-marker-string-id",
13006 tp->static_trace_marker_id);
13007 uiout->text ("\n");
13008 }
13009 }
13010
13011 static void
13012 tracepoint_print_mention (struct breakpoint *b)
13013 {
13014 if (current_uiout->is_mi_like_p ())
13015 return;
13016
13017 switch (b->type)
13018 {
13019 case bp_tracepoint:
13020 printf_filtered (_("Tracepoint"));
13021 printf_filtered (_(" %d"), b->number);
13022 break;
13023 case bp_fast_tracepoint:
13024 printf_filtered (_("Fast tracepoint"));
13025 printf_filtered (_(" %d"), b->number);
13026 break;
13027 case bp_static_tracepoint:
13028 printf_filtered (_("Static tracepoint"));
13029 printf_filtered (_(" %d"), b->number);
13030 break;
13031 default:
13032 internal_error (__FILE__, __LINE__,
13033 _("unhandled tracepoint type %d"), (int) b->type);
13034 }
13035
13036 say_where (b);
13037 }
13038
13039 static void
13040 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13041 {
13042 struct tracepoint *tp = (struct tracepoint *) self;
13043
13044 if (self->type == bp_fast_tracepoint)
13045 fprintf_unfiltered (fp, "ftrace");
13046 else if (self->type == bp_static_tracepoint)
13047 fprintf_unfiltered (fp, "strace");
13048 else if (self->type == bp_tracepoint)
13049 fprintf_unfiltered (fp, "trace");
13050 else
13051 internal_error (__FILE__, __LINE__,
13052 _("unhandled tracepoint type %d"), (int) self->type);
13053
13054 fprintf_unfiltered (fp, " %s",
13055 event_location_to_string (self->location.get ()));
13056 print_recreate_thread (self, fp);
13057
13058 if (tp->pass_count)
13059 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13060 }
13061
13062 static void
13063 tracepoint_create_sals_from_location (const struct event_location *location,
13064 struct linespec_result *canonical,
13065 enum bptype type_wanted)
13066 {
13067 create_sals_from_location_default (location, canonical, type_wanted);
13068 }
13069
13070 static void
13071 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13072 struct linespec_result *canonical,
13073 gdb::unique_xmalloc_ptr<char> cond_string,
13074 gdb::unique_xmalloc_ptr<char> extra_string,
13075 enum bptype type_wanted,
13076 enum bpdisp disposition,
13077 int thread,
13078 int task, int ignore_count,
13079 const struct breakpoint_ops *ops,
13080 int from_tty, int enabled,
13081 int internal, unsigned flags)
13082 {
13083 create_breakpoints_sal_default (gdbarch, canonical,
13084 std::move (cond_string),
13085 std::move (extra_string),
13086 type_wanted,
13087 disposition, thread, task,
13088 ignore_count, ops, from_tty,
13089 enabled, internal, flags);
13090 }
13091
13092 static std::vector<symtab_and_line>
13093 tracepoint_decode_location (struct breakpoint *b,
13094 const struct event_location *location,
13095 struct program_space *search_pspace)
13096 {
13097 return decode_location_default (b, location, search_pspace);
13098 }
13099
13100 struct breakpoint_ops tracepoint_breakpoint_ops;
13101
13102 /* The breakpoint_ops structure to be use on tracepoints placed in a
13103 static probe. */
13104
13105 static void
13106 tracepoint_probe_create_sals_from_location
13107 (const struct event_location *location,
13108 struct linespec_result *canonical,
13109 enum bptype type_wanted)
13110 {
13111 /* We use the same method for breakpoint on probes. */
13112 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13113 }
13114
13115 static std::vector<symtab_and_line>
13116 tracepoint_probe_decode_location (struct breakpoint *b,
13117 const struct event_location *location,
13118 struct program_space *search_pspace)
13119 {
13120 /* We use the same method for breakpoint on probes. */
13121 return bkpt_probe_decode_location (b, location, search_pspace);
13122 }
13123
13124 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13125
13126 /* Dprintf breakpoint_ops methods. */
13127
13128 static void
13129 dprintf_re_set (struct breakpoint *b)
13130 {
13131 breakpoint_re_set_default (b);
13132
13133 /* extra_string should never be non-NULL for dprintf. */
13134 gdb_assert (b->extra_string != NULL);
13135
13136 /* 1 - connect to target 1, that can run breakpoint commands.
13137 2 - create a dprintf, which resolves fine.
13138 3 - disconnect from target 1
13139 4 - connect to target 2, that can NOT run breakpoint commands.
13140
13141 After steps #3/#4, you'll want the dprintf command list to
13142 be updated, because target 1 and 2 may well return different
13143 answers for target_can_run_breakpoint_commands().
13144 Given absence of finer grained resetting, we get to do
13145 it all the time. */
13146 if (b->extra_string != NULL)
13147 update_dprintf_command_list (b);
13148 }
13149
13150 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13151
13152 static void
13153 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13154 {
13155 fprintf_unfiltered (fp, "dprintf %s,%s",
13156 event_location_to_string (tp->location.get ()),
13157 tp->extra_string);
13158 print_recreate_thread (tp, fp);
13159 }
13160
13161 /* Implement the "after_condition_true" breakpoint_ops method for
13162 dprintf.
13163
13164 dprintf's are implemented with regular commands in their command
13165 list, but we run the commands here instead of before presenting the
13166 stop to the user, as dprintf's don't actually cause a stop. This
13167 also makes it so that the commands of multiple dprintfs at the same
13168 address are all handled. */
13169
13170 static void
13171 dprintf_after_condition_true (struct bpstats *bs)
13172 {
13173 struct bpstats tmp_bs;
13174 struct bpstats *tmp_bs_p = &tmp_bs;
13175
13176 /* dprintf's never cause a stop. This wasn't set in the
13177 check_status hook instead because that would make the dprintf's
13178 condition not be evaluated. */
13179 bs->stop = 0;
13180
13181 /* Run the command list here. Take ownership of it instead of
13182 copying. We never want these commands to run later in
13183 bpstat_do_actions, if a breakpoint that causes a stop happens to
13184 be set at same address as this dprintf, or even if running the
13185 commands here throws. */
13186 tmp_bs.commands = bs->commands;
13187 bs->commands = NULL;
13188
13189 bpstat_do_actions_1 (&tmp_bs_p);
13190
13191 /* 'tmp_bs.commands' will usually be NULL by now, but
13192 bpstat_do_actions_1 may return early without processing the whole
13193 list. */
13194 }
13195
13196 /* The breakpoint_ops structure to be used on static tracepoints with
13197 markers (`-m'). */
13198
13199 static void
13200 strace_marker_create_sals_from_location (const struct event_location *location,
13201 struct linespec_result *canonical,
13202 enum bptype type_wanted)
13203 {
13204 struct linespec_sals lsal;
13205 const char *arg_start, *arg;
13206
13207 arg = arg_start = get_linespec_location (location);
13208 lsal.sals = decode_static_tracepoint_spec (&arg);
13209
13210 std::string str (arg_start, arg - arg_start);
13211 const char *ptr = str.c_str ();
13212 canonical->location = new_linespec_location (&ptr);
13213
13214 lsal.canonical
13215 = xstrdup (event_location_to_string (canonical->location.get ()));
13216 canonical->lsals.push_back (std::move (lsal));
13217 }
13218
13219 static void
13220 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13221 struct linespec_result *canonical,
13222 gdb::unique_xmalloc_ptr<char> cond_string,
13223 gdb::unique_xmalloc_ptr<char> extra_string,
13224 enum bptype type_wanted,
13225 enum bpdisp disposition,
13226 int thread,
13227 int task, int ignore_count,
13228 const struct breakpoint_ops *ops,
13229 int from_tty, int enabled,
13230 int internal, unsigned flags)
13231 {
13232 const linespec_sals &lsal = canonical->lsals[0];
13233
13234 /* If the user is creating a static tracepoint by marker id
13235 (strace -m MARKER_ID), then store the sals index, so that
13236 breakpoint_re_set can try to match up which of the newly
13237 found markers corresponds to this one, and, don't try to
13238 expand multiple locations for each sal, given than SALS
13239 already should contain all sals for MARKER_ID. */
13240
13241 for (size_t i = 0; i < lsal.sals.size (); i++)
13242 {
13243 event_location_up location
13244 = copy_event_location (canonical->location.get ());
13245
13246 std::unique_ptr<tracepoint> tp (new tracepoint ());
13247 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13248 std::move (location), NULL,
13249 std::move (cond_string),
13250 std::move (extra_string),
13251 type_wanted, disposition,
13252 thread, task, ignore_count, ops,
13253 from_tty, enabled, internal, flags,
13254 canonical->special_display);
13255 /* Given that its possible to have multiple markers with
13256 the same string id, if the user is creating a static
13257 tracepoint by marker id ("strace -m MARKER_ID"), then
13258 store the sals index, so that breakpoint_re_set can
13259 try to match up which of the newly found markers
13260 corresponds to this one */
13261 tp->static_trace_marker_id_idx = i;
13262
13263 install_breakpoint (internal, std::move (tp), 0);
13264 }
13265 }
13266
13267 static std::vector<symtab_and_line>
13268 strace_marker_decode_location (struct breakpoint *b,
13269 const struct event_location *location,
13270 struct program_space *search_pspace)
13271 {
13272 struct tracepoint *tp = (struct tracepoint *) b;
13273 const char *s = get_linespec_location (location);
13274
13275 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13276 if (sals.size () > tp->static_trace_marker_id_idx)
13277 {
13278 sals[0] = sals[tp->static_trace_marker_id_idx];
13279 sals.resize (1);
13280 return sals;
13281 }
13282 else
13283 error (_("marker %s not found"), tp->static_trace_marker_id);
13284 }
13285
13286 static struct breakpoint_ops strace_marker_breakpoint_ops;
13287
13288 static int
13289 strace_marker_p (struct breakpoint *b)
13290 {
13291 return b->ops == &strace_marker_breakpoint_ops;
13292 }
13293
13294 /* Delete a breakpoint and clean up all traces of it in the data
13295 structures. */
13296
13297 void
13298 delete_breakpoint (struct breakpoint *bpt)
13299 {
13300 struct breakpoint *b;
13301
13302 gdb_assert (bpt != NULL);
13303
13304 /* Has this bp already been deleted? This can happen because
13305 multiple lists can hold pointers to bp's. bpstat lists are
13306 especial culprits.
13307
13308 One example of this happening is a watchpoint's scope bp. When
13309 the scope bp triggers, we notice that the watchpoint is out of
13310 scope, and delete it. We also delete its scope bp. But the
13311 scope bp is marked "auto-deleting", and is already on a bpstat.
13312 That bpstat is then checked for auto-deleting bp's, which are
13313 deleted.
13314
13315 A real solution to this problem might involve reference counts in
13316 bp's, and/or giving them pointers back to their referencing
13317 bpstat's, and teaching delete_breakpoint to only free a bp's
13318 storage when no more references were extent. A cheaper bandaid
13319 was chosen. */
13320 if (bpt->type == bp_none)
13321 return;
13322
13323 /* At least avoid this stale reference until the reference counting
13324 of breakpoints gets resolved. */
13325 if (bpt->related_breakpoint != bpt)
13326 {
13327 struct breakpoint *related;
13328 struct watchpoint *w;
13329
13330 if (bpt->type == bp_watchpoint_scope)
13331 w = (struct watchpoint *) bpt->related_breakpoint;
13332 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13333 w = (struct watchpoint *) bpt;
13334 else
13335 w = NULL;
13336 if (w != NULL)
13337 watchpoint_del_at_next_stop (w);
13338
13339 /* Unlink bpt from the bpt->related_breakpoint ring. */
13340 for (related = bpt; related->related_breakpoint != bpt;
13341 related = related->related_breakpoint);
13342 related->related_breakpoint = bpt->related_breakpoint;
13343 bpt->related_breakpoint = bpt;
13344 }
13345
13346 /* watch_command_1 creates a watchpoint but only sets its number if
13347 update_watchpoint succeeds in creating its bp_locations. If there's
13348 a problem in that process, we'll be asked to delete the half-created
13349 watchpoint. In that case, don't announce the deletion. */
13350 if (bpt->number)
13351 observer_notify_breakpoint_deleted (bpt);
13352
13353 if (breakpoint_chain == bpt)
13354 breakpoint_chain = bpt->next;
13355
13356 ALL_BREAKPOINTS (b)
13357 if (b->next == bpt)
13358 {
13359 b->next = bpt->next;
13360 break;
13361 }
13362
13363 /* Be sure no bpstat's are pointing at the breakpoint after it's
13364 been freed. */
13365 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13366 in all threads for now. Note that we cannot just remove bpstats
13367 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13368 commands are associated with the bpstat; if we remove it here,
13369 then the later call to bpstat_do_actions (&stop_bpstat); in
13370 event-top.c won't do anything, and temporary breakpoints with
13371 commands won't work. */
13372
13373 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13374
13375 /* Now that breakpoint is removed from breakpoint list, update the
13376 global location list. This will remove locations that used to
13377 belong to this breakpoint. Do this before freeing the breakpoint
13378 itself, since remove_breakpoint looks at location's owner. It
13379 might be better design to have location completely
13380 self-contained, but it's not the case now. */
13381 update_global_location_list (UGLL_DONT_INSERT);
13382
13383 /* On the chance that someone will soon try again to delete this
13384 same bp, we mark it as deleted before freeing its storage. */
13385 bpt->type = bp_none;
13386 delete bpt;
13387 }
13388
13389 static void
13390 do_delete_breakpoint_cleanup (void *b)
13391 {
13392 delete_breakpoint ((struct breakpoint *) b);
13393 }
13394
13395 struct cleanup *
13396 make_cleanup_delete_breakpoint (struct breakpoint *b)
13397 {
13398 return make_cleanup (do_delete_breakpoint_cleanup, b);
13399 }
13400
13401 /* Iterator function to call a user-provided callback function once
13402 for each of B and its related breakpoints. */
13403
13404 static void
13405 iterate_over_related_breakpoints (struct breakpoint *b,
13406 gdb::function_view<void (breakpoint *)> function)
13407 {
13408 struct breakpoint *related;
13409
13410 related = b;
13411 do
13412 {
13413 struct breakpoint *next;
13414
13415 /* FUNCTION may delete RELATED. */
13416 next = related->related_breakpoint;
13417
13418 if (next == related)
13419 {
13420 /* RELATED is the last ring entry. */
13421 function (related);
13422
13423 /* FUNCTION may have deleted it, so we'd never reach back to
13424 B. There's nothing left to do anyway, so just break
13425 out. */
13426 break;
13427 }
13428 else
13429 function (related);
13430
13431 related = next;
13432 }
13433 while (related != b);
13434 }
13435
13436 static void
13437 delete_command (const char *arg, int from_tty)
13438 {
13439 struct breakpoint *b, *b_tmp;
13440
13441 dont_repeat ();
13442
13443 if (arg == 0)
13444 {
13445 int breaks_to_delete = 0;
13446
13447 /* Delete all breakpoints if no argument. Do not delete
13448 internal breakpoints, these have to be deleted with an
13449 explicit breakpoint number argument. */
13450 ALL_BREAKPOINTS (b)
13451 if (user_breakpoint_p (b))
13452 {
13453 breaks_to_delete = 1;
13454 break;
13455 }
13456
13457 /* Ask user only if there are some breakpoints to delete. */
13458 if (!from_tty
13459 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13460 {
13461 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13462 if (user_breakpoint_p (b))
13463 delete_breakpoint (b);
13464 }
13465 }
13466 else
13467 map_breakpoint_numbers
13468 (arg, [&] (breakpoint *b)
13469 {
13470 iterate_over_related_breakpoints (b, delete_breakpoint);
13471 });
13472 }
13473
13474 /* Return true if all locations of B bound to PSPACE are pending. If
13475 PSPACE is NULL, all locations of all program spaces are
13476 considered. */
13477
13478 static int
13479 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13480 {
13481 struct bp_location *loc;
13482
13483 for (loc = b->loc; loc != NULL; loc = loc->next)
13484 if ((pspace == NULL
13485 || loc->pspace == pspace)
13486 && !loc->shlib_disabled
13487 && !loc->pspace->executing_startup)
13488 return 0;
13489 return 1;
13490 }
13491
13492 /* Subroutine of update_breakpoint_locations to simplify it.
13493 Return non-zero if multiple fns in list LOC have the same name.
13494 Null names are ignored. */
13495
13496 static int
13497 ambiguous_names_p (struct bp_location *loc)
13498 {
13499 struct bp_location *l;
13500 htab_t htab = htab_create_alloc (13, htab_hash_string,
13501 (int (*) (const void *,
13502 const void *)) streq,
13503 NULL, xcalloc, xfree);
13504
13505 for (l = loc; l != NULL; l = l->next)
13506 {
13507 const char **slot;
13508 const char *name = l->function_name;
13509
13510 /* Allow for some names to be NULL, ignore them. */
13511 if (name == NULL)
13512 continue;
13513
13514 slot = (const char **) htab_find_slot (htab, (const void *) name,
13515 INSERT);
13516 /* NOTE: We can assume slot != NULL here because xcalloc never
13517 returns NULL. */
13518 if (*slot != NULL)
13519 {
13520 htab_delete (htab);
13521 return 1;
13522 }
13523 *slot = name;
13524 }
13525
13526 htab_delete (htab);
13527 return 0;
13528 }
13529
13530 /* When symbols change, it probably means the sources changed as well,
13531 and it might mean the static tracepoint markers are no longer at
13532 the same address or line numbers they used to be at last we
13533 checked. Losing your static tracepoints whenever you rebuild is
13534 undesirable. This function tries to resync/rematch gdb static
13535 tracepoints with the markers on the target, for static tracepoints
13536 that have not been set by marker id. Static tracepoint that have
13537 been set by marker id are reset by marker id in breakpoint_re_set.
13538 The heuristic is:
13539
13540 1) For a tracepoint set at a specific address, look for a marker at
13541 the old PC. If one is found there, assume to be the same marker.
13542 If the name / string id of the marker found is different from the
13543 previous known name, assume that means the user renamed the marker
13544 in the sources, and output a warning.
13545
13546 2) For a tracepoint set at a given line number, look for a marker
13547 at the new address of the old line number. If one is found there,
13548 assume to be the same marker. If the name / string id of the
13549 marker found is different from the previous known name, assume that
13550 means the user renamed the marker in the sources, and output a
13551 warning.
13552
13553 3) If a marker is no longer found at the same address or line, it
13554 may mean the marker no longer exists. But it may also just mean
13555 the code changed a bit. Maybe the user added a few lines of code
13556 that made the marker move up or down (in line number terms). Ask
13557 the target for info about the marker with the string id as we knew
13558 it. If found, update line number and address in the matching
13559 static tracepoint. This will get confused if there's more than one
13560 marker with the same ID (possible in UST, although unadvised
13561 precisely because it confuses tools). */
13562
13563 static struct symtab_and_line
13564 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13565 {
13566 struct tracepoint *tp = (struct tracepoint *) b;
13567 struct static_tracepoint_marker marker;
13568 CORE_ADDR pc;
13569
13570 pc = sal.pc;
13571 if (sal.line)
13572 find_line_pc (sal.symtab, sal.line, &pc);
13573
13574 if (target_static_tracepoint_marker_at (pc, &marker))
13575 {
13576 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13577 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13578 b->number,
13579 tp->static_trace_marker_id, marker.str_id);
13580
13581 xfree (tp->static_trace_marker_id);
13582 tp->static_trace_marker_id = xstrdup (marker.str_id);
13583 release_static_tracepoint_marker (&marker);
13584
13585 return sal;
13586 }
13587
13588 /* Old marker wasn't found on target at lineno. Try looking it up
13589 by string ID. */
13590 if (!sal.explicit_pc
13591 && sal.line != 0
13592 && sal.symtab != NULL
13593 && tp->static_trace_marker_id != NULL)
13594 {
13595 VEC(static_tracepoint_marker_p) *markers;
13596
13597 markers
13598 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13599
13600 if (!VEC_empty(static_tracepoint_marker_p, markers))
13601 {
13602 struct symbol *sym;
13603 struct static_tracepoint_marker *tpmarker;
13604 struct ui_out *uiout = current_uiout;
13605 struct explicit_location explicit_loc;
13606
13607 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13608
13609 xfree (tp->static_trace_marker_id);
13610 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13611
13612 warning (_("marker for static tracepoint %d (%s) not "
13613 "found at previous line number"),
13614 b->number, tp->static_trace_marker_id);
13615
13616 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13617 sym = find_pc_sect_function (tpmarker->address, NULL);
13618 uiout->text ("Now in ");
13619 if (sym)
13620 {
13621 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13622 uiout->text (" at ");
13623 }
13624 uiout->field_string ("file",
13625 symtab_to_filename_for_display (sal2.symtab));
13626 uiout->text (":");
13627
13628 if (uiout->is_mi_like_p ())
13629 {
13630 const char *fullname = symtab_to_fullname (sal2.symtab);
13631
13632 uiout->field_string ("fullname", fullname);
13633 }
13634
13635 uiout->field_int ("line", sal2.line);
13636 uiout->text ("\n");
13637
13638 b->loc->line_number = sal2.line;
13639 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13640
13641 b->location.reset (NULL);
13642 initialize_explicit_location (&explicit_loc);
13643 explicit_loc.source_filename
13644 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13645 explicit_loc.line_offset.offset = b->loc->line_number;
13646 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13647 b->location = new_explicit_location (&explicit_loc);
13648
13649 /* Might be nice to check if function changed, and warn if
13650 so. */
13651
13652 release_static_tracepoint_marker (tpmarker);
13653 }
13654 }
13655 return sal;
13656 }
13657
13658 /* Returns 1 iff locations A and B are sufficiently same that
13659 we don't need to report breakpoint as changed. */
13660
13661 static int
13662 locations_are_equal (struct bp_location *a, struct bp_location *b)
13663 {
13664 while (a && b)
13665 {
13666 if (a->address != b->address)
13667 return 0;
13668
13669 if (a->shlib_disabled != b->shlib_disabled)
13670 return 0;
13671
13672 if (a->enabled != b->enabled)
13673 return 0;
13674
13675 a = a->next;
13676 b = b->next;
13677 }
13678
13679 if ((a == NULL) != (b == NULL))
13680 return 0;
13681
13682 return 1;
13683 }
13684
13685 /* Split all locations of B that are bound to PSPACE out of B's
13686 location list to a separate list and return that list's head. If
13687 PSPACE is NULL, hoist out all locations of B. */
13688
13689 static struct bp_location *
13690 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13691 {
13692 struct bp_location head;
13693 struct bp_location *i = b->loc;
13694 struct bp_location **i_link = &b->loc;
13695 struct bp_location *hoisted = &head;
13696
13697 if (pspace == NULL)
13698 {
13699 i = b->loc;
13700 b->loc = NULL;
13701 return i;
13702 }
13703
13704 head.next = NULL;
13705
13706 while (i != NULL)
13707 {
13708 if (i->pspace == pspace)
13709 {
13710 *i_link = i->next;
13711 i->next = NULL;
13712 hoisted->next = i;
13713 hoisted = i;
13714 }
13715 else
13716 i_link = &i->next;
13717 i = *i_link;
13718 }
13719
13720 return head.next;
13721 }
13722
13723 /* Create new breakpoint locations for B (a hardware or software
13724 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13725 zero, then B is a ranged breakpoint. Only recreates locations for
13726 FILTER_PSPACE. Locations of other program spaces are left
13727 untouched. */
13728
13729 void
13730 update_breakpoint_locations (struct breakpoint *b,
13731 struct program_space *filter_pspace,
13732 gdb::array_view<const symtab_and_line> sals,
13733 gdb::array_view<const symtab_and_line> sals_end)
13734 {
13735 int i;
13736 struct bp_location *existing_locations;
13737
13738 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13739 {
13740 /* Ranged breakpoints have only one start location and one end
13741 location. */
13742 b->enable_state = bp_disabled;
13743 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13744 "multiple locations found\n"),
13745 b->number);
13746 return;
13747 }
13748
13749 /* If there's no new locations, and all existing locations are
13750 pending, don't do anything. This optimizes the common case where
13751 all locations are in the same shared library, that was unloaded.
13752 We'd like to retain the location, so that when the library is
13753 loaded again, we don't loose the enabled/disabled status of the
13754 individual locations. */
13755 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13756 return;
13757
13758 existing_locations = hoist_existing_locations (b, filter_pspace);
13759
13760 for (const auto &sal : sals)
13761 {
13762 struct bp_location *new_loc;
13763
13764 switch_to_program_space_and_thread (sal.pspace);
13765
13766 new_loc = add_location_to_breakpoint (b, &sal);
13767
13768 /* Reparse conditions, they might contain references to the
13769 old symtab. */
13770 if (b->cond_string != NULL)
13771 {
13772 const char *s;
13773
13774 s = b->cond_string;
13775 TRY
13776 {
13777 new_loc->cond = parse_exp_1 (&s, sal.pc,
13778 block_for_pc (sal.pc),
13779 0);
13780 }
13781 CATCH (e, RETURN_MASK_ERROR)
13782 {
13783 warning (_("failed to reevaluate condition "
13784 "for breakpoint %d: %s"),
13785 b->number, e.message);
13786 new_loc->enabled = 0;
13787 }
13788 END_CATCH
13789 }
13790
13791 if (!sals_end.empty ())
13792 {
13793 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13794
13795 new_loc->length = end - sals[0].pc + 1;
13796 }
13797 }
13798
13799 /* If possible, carry over 'disable' status from existing
13800 breakpoints. */
13801 {
13802 struct bp_location *e = existing_locations;
13803 /* If there are multiple breakpoints with the same function name,
13804 e.g. for inline functions, comparing function names won't work.
13805 Instead compare pc addresses; this is just a heuristic as things
13806 may have moved, but in practice it gives the correct answer
13807 often enough until a better solution is found. */
13808 int have_ambiguous_names = ambiguous_names_p (b->loc);
13809
13810 for (; e; e = e->next)
13811 {
13812 if (!e->enabled && e->function_name)
13813 {
13814 struct bp_location *l = b->loc;
13815 if (have_ambiguous_names)
13816 {
13817 for (; l; l = l->next)
13818 if (breakpoint_locations_match (e, l))
13819 {
13820 l->enabled = 0;
13821 break;
13822 }
13823 }
13824 else
13825 {
13826 for (; l; l = l->next)
13827 if (l->function_name
13828 && strcmp (e->function_name, l->function_name) == 0)
13829 {
13830 l->enabled = 0;
13831 break;
13832 }
13833 }
13834 }
13835 }
13836 }
13837
13838 if (!locations_are_equal (existing_locations, b->loc))
13839 observer_notify_breakpoint_modified (b);
13840 }
13841
13842 /* Find the SaL locations corresponding to the given LOCATION.
13843 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13844
13845 static std::vector<symtab_and_line>
13846 location_to_sals (struct breakpoint *b, struct event_location *location,
13847 struct program_space *search_pspace, int *found)
13848 {
13849 struct gdb_exception exception = exception_none;
13850
13851 gdb_assert (b->ops != NULL);
13852
13853 std::vector<symtab_and_line> sals;
13854
13855 TRY
13856 {
13857 sals = b->ops->decode_location (b, location, search_pspace);
13858 }
13859 CATCH (e, RETURN_MASK_ERROR)
13860 {
13861 int not_found_and_ok = 0;
13862
13863 exception = e;
13864
13865 /* For pending breakpoints, it's expected that parsing will
13866 fail until the right shared library is loaded. User has
13867 already told to create pending breakpoints and don't need
13868 extra messages. If breakpoint is in bp_shlib_disabled
13869 state, then user already saw the message about that
13870 breakpoint being disabled, and don't want to see more
13871 errors. */
13872 if (e.error == NOT_FOUND_ERROR
13873 && (b->condition_not_parsed
13874 || (b->loc != NULL
13875 && search_pspace != NULL
13876 && b->loc->pspace != search_pspace)
13877 || (b->loc && b->loc->shlib_disabled)
13878 || (b->loc && b->loc->pspace->executing_startup)
13879 || b->enable_state == bp_disabled))
13880 not_found_and_ok = 1;
13881
13882 if (!not_found_and_ok)
13883 {
13884 /* We surely don't want to warn about the same breakpoint
13885 10 times. One solution, implemented here, is disable
13886 the breakpoint on error. Another solution would be to
13887 have separate 'warning emitted' flag. Since this
13888 happens only when a binary has changed, I don't know
13889 which approach is better. */
13890 b->enable_state = bp_disabled;
13891 throw_exception (e);
13892 }
13893 }
13894 END_CATCH
13895
13896 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13897 {
13898 for (auto &sal : sals)
13899 resolve_sal_pc (&sal);
13900 if (b->condition_not_parsed && b->extra_string != NULL)
13901 {
13902 char *cond_string, *extra_string;
13903 int thread, task;
13904
13905 find_condition_and_thread (b->extra_string, sals[0].pc,
13906 &cond_string, &thread, &task,
13907 &extra_string);
13908 gdb_assert (b->cond_string == NULL);
13909 if (cond_string)
13910 b->cond_string = cond_string;
13911 b->thread = thread;
13912 b->task = task;
13913 if (extra_string)
13914 {
13915 xfree (b->extra_string);
13916 b->extra_string = extra_string;
13917 }
13918 b->condition_not_parsed = 0;
13919 }
13920
13921 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13922 sals[0] = update_static_tracepoint (b, sals[0]);
13923
13924 *found = 1;
13925 }
13926 else
13927 *found = 0;
13928
13929 return sals;
13930 }
13931
13932 /* The default re_set method, for typical hardware or software
13933 breakpoints. Reevaluate the breakpoint and recreate its
13934 locations. */
13935
13936 static void
13937 breakpoint_re_set_default (struct breakpoint *b)
13938 {
13939 struct program_space *filter_pspace = current_program_space;
13940 std::vector<symtab_and_line> expanded, expanded_end;
13941
13942 int found;
13943 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13944 filter_pspace, &found);
13945 if (found)
13946 expanded = std::move (sals);
13947
13948 if (b->location_range_end != NULL)
13949 {
13950 std::vector<symtab_and_line> sals_end
13951 = location_to_sals (b, b->location_range_end.get (),
13952 filter_pspace, &found);
13953 if (found)
13954 expanded_end = std::move (sals_end);
13955 }
13956
13957 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13958 }
13959
13960 /* Default method for creating SALs from an address string. It basically
13961 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13962
13963 static void
13964 create_sals_from_location_default (const struct event_location *location,
13965 struct linespec_result *canonical,
13966 enum bptype type_wanted)
13967 {
13968 parse_breakpoint_sals (location, canonical);
13969 }
13970
13971 /* Call create_breakpoints_sal for the given arguments. This is the default
13972 function for the `create_breakpoints_sal' method of
13973 breakpoint_ops. */
13974
13975 static void
13976 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13977 struct linespec_result *canonical,
13978 gdb::unique_xmalloc_ptr<char> cond_string,
13979 gdb::unique_xmalloc_ptr<char> extra_string,
13980 enum bptype type_wanted,
13981 enum bpdisp disposition,
13982 int thread,
13983 int task, int ignore_count,
13984 const struct breakpoint_ops *ops,
13985 int from_tty, int enabled,
13986 int internal, unsigned flags)
13987 {
13988 create_breakpoints_sal (gdbarch, canonical,
13989 std::move (cond_string),
13990 std::move (extra_string),
13991 type_wanted, disposition,
13992 thread, task, ignore_count, ops, from_tty,
13993 enabled, internal, flags);
13994 }
13995
13996 /* Decode the line represented by S by calling decode_line_full. This is the
13997 default function for the `decode_location' method of breakpoint_ops. */
13998
13999 static std::vector<symtab_and_line>
14000 decode_location_default (struct breakpoint *b,
14001 const struct event_location *location,
14002 struct program_space *search_pspace)
14003 {
14004 struct linespec_result canonical;
14005
14006 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14007 (struct symtab *) NULL, 0,
14008 &canonical, multiple_symbols_all,
14009 b->filter);
14010
14011 /* We should get 0 or 1 resulting SALs. */
14012 gdb_assert (canonical.lsals.size () < 2);
14013
14014 if (!canonical.lsals.empty ())
14015 {
14016 const linespec_sals &lsal = canonical.lsals[0];
14017 return std::move (lsal.sals);
14018 }
14019 return {};
14020 }
14021
14022 /* Reset a breakpoint. */
14023
14024 static void
14025 breakpoint_re_set_one (breakpoint *b)
14026 {
14027 input_radix = b->input_radix;
14028 set_language (b->language);
14029
14030 b->ops->re_set (b);
14031 }
14032
14033 /* Re-set breakpoint locations for the current program space.
14034 Locations bound to other program spaces are left untouched. */
14035
14036 void
14037 breakpoint_re_set (void)
14038 {
14039 struct breakpoint *b, *b_tmp;
14040
14041 {
14042 scoped_restore_current_language save_language;
14043 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
14044 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14045
14046 /* Note: we must not try to insert locations until after all
14047 breakpoints have been re-set. Otherwise, e.g., when re-setting
14048 breakpoint 1, we'd insert the locations of breakpoint 2, which
14049 hadn't been re-set yet, and thus may have stale locations. */
14050
14051 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14052 {
14053 TRY
14054 {
14055 breakpoint_re_set_one (b);
14056 }
14057 CATCH (ex, RETURN_MASK_ALL)
14058 {
14059 exception_fprintf (gdb_stderr, ex,
14060 "Error in re-setting breakpoint %d: ",
14061 b->number);
14062 }
14063 END_CATCH
14064 }
14065
14066 jit_breakpoint_re_set ();
14067 }
14068
14069 create_overlay_event_breakpoint ();
14070 create_longjmp_master_breakpoint ();
14071 create_std_terminate_master_breakpoint ();
14072 create_exception_master_breakpoint ();
14073
14074 /* Now we can insert. */
14075 update_global_location_list (UGLL_MAY_INSERT);
14076 }
14077 \f
14078 /* Reset the thread number of this breakpoint:
14079
14080 - If the breakpoint is for all threads, leave it as-is.
14081 - Else, reset it to the current thread for inferior_ptid. */
14082 void
14083 breakpoint_re_set_thread (struct breakpoint *b)
14084 {
14085 if (b->thread != -1)
14086 {
14087 if (in_thread_list (inferior_ptid))
14088 b->thread = ptid_to_global_thread_id (inferior_ptid);
14089
14090 /* We're being called after following a fork. The new fork is
14091 selected as current, and unless this was a vfork will have a
14092 different program space from the original thread. Reset that
14093 as well. */
14094 b->loc->pspace = current_program_space;
14095 }
14096 }
14097
14098 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14099 If from_tty is nonzero, it prints a message to that effect,
14100 which ends with a period (no newline). */
14101
14102 void
14103 set_ignore_count (int bptnum, int count, int from_tty)
14104 {
14105 struct breakpoint *b;
14106
14107 if (count < 0)
14108 count = 0;
14109
14110 ALL_BREAKPOINTS (b)
14111 if (b->number == bptnum)
14112 {
14113 if (is_tracepoint (b))
14114 {
14115 if (from_tty && count != 0)
14116 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14117 bptnum);
14118 return;
14119 }
14120
14121 b->ignore_count = count;
14122 if (from_tty)
14123 {
14124 if (count == 0)
14125 printf_filtered (_("Will stop next time "
14126 "breakpoint %d is reached."),
14127 bptnum);
14128 else if (count == 1)
14129 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14130 bptnum);
14131 else
14132 printf_filtered (_("Will ignore next %d "
14133 "crossings of breakpoint %d."),
14134 count, bptnum);
14135 }
14136 observer_notify_breakpoint_modified (b);
14137 return;
14138 }
14139
14140 error (_("No breakpoint number %d."), bptnum);
14141 }
14142
14143 /* Command to set ignore-count of breakpoint N to COUNT. */
14144
14145 static void
14146 ignore_command (char *args, int from_tty)
14147 {
14148 char *p = args;
14149 int num;
14150
14151 if (p == 0)
14152 error_no_arg (_("a breakpoint number"));
14153
14154 num = get_number (&p);
14155 if (num == 0)
14156 error (_("bad breakpoint number: '%s'"), args);
14157 if (*p == 0)
14158 error (_("Second argument (specified ignore-count) is missing."));
14159
14160 set_ignore_count (num,
14161 longest_to_int (value_as_long (parse_and_eval (p))),
14162 from_tty);
14163 if (from_tty)
14164 printf_filtered ("\n");
14165 }
14166 \f
14167 /* Call FUNCTION on each of the breakpoints
14168 whose numbers are given in ARGS. */
14169
14170 static void
14171 map_breakpoint_numbers (const char *args,
14172 gdb::function_view<void (breakpoint *)> function)
14173 {
14174 int num;
14175 struct breakpoint *b, *tmp;
14176
14177 if (args == 0 || *args == '\0')
14178 error_no_arg (_("one or more breakpoint numbers"));
14179
14180 number_or_range_parser parser (args);
14181
14182 while (!parser.finished ())
14183 {
14184 const char *p = parser.cur_tok ();
14185 bool match = false;
14186
14187 num = parser.get_number ();
14188 if (num == 0)
14189 {
14190 warning (_("bad breakpoint number at or near '%s'"), p);
14191 }
14192 else
14193 {
14194 ALL_BREAKPOINTS_SAFE (b, tmp)
14195 if (b->number == num)
14196 {
14197 match = true;
14198 function (b);
14199 break;
14200 }
14201 if (!match)
14202 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14203 }
14204 }
14205 }
14206
14207 static struct bp_location *
14208 find_location_by_number (const char *number)
14209 {
14210 const char *p1;
14211 int bp_num;
14212 int loc_num;
14213 struct breakpoint *b;
14214 struct bp_location *loc;
14215
14216 p1 = number;
14217 bp_num = get_number_trailer (&p1, '.');
14218 if (bp_num == 0 || p1[0] != '.')
14219 error (_("Bad breakpoint number '%s'"), number);
14220
14221 ALL_BREAKPOINTS (b)
14222 if (b->number == bp_num)
14223 {
14224 break;
14225 }
14226
14227 if (!b || b->number != bp_num)
14228 error (_("Bad breakpoint number '%s'"), number);
14229
14230 /* Skip the dot. */
14231 ++p1;
14232 const char *save = p1;
14233 loc_num = get_number (&p1);
14234 if (loc_num == 0)
14235 error (_("Bad breakpoint location number '%s'"), number);
14236
14237 --loc_num;
14238 loc = b->loc;
14239 for (;loc_num && loc; --loc_num, loc = loc->next)
14240 ;
14241 if (!loc)
14242 error (_("Bad breakpoint location number '%s'"), save);
14243
14244 return loc;
14245 }
14246
14247
14248 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14249 If from_tty is nonzero, it prints a message to that effect,
14250 which ends with a period (no newline). */
14251
14252 void
14253 disable_breakpoint (struct breakpoint *bpt)
14254 {
14255 /* Never disable a watchpoint scope breakpoint; we want to
14256 hit them when we leave scope so we can delete both the
14257 watchpoint and its scope breakpoint at that time. */
14258 if (bpt->type == bp_watchpoint_scope)
14259 return;
14260
14261 bpt->enable_state = bp_disabled;
14262
14263 /* Mark breakpoint locations modified. */
14264 mark_breakpoint_modified (bpt);
14265
14266 if (target_supports_enable_disable_tracepoint ()
14267 && current_trace_status ()->running && is_tracepoint (bpt))
14268 {
14269 struct bp_location *location;
14270
14271 for (location = bpt->loc; location; location = location->next)
14272 target_disable_tracepoint (location);
14273 }
14274
14275 update_global_location_list (UGLL_DONT_INSERT);
14276
14277 observer_notify_breakpoint_modified (bpt);
14278 }
14279
14280 static void
14281 disable_command (const char *args, int from_tty)
14282 {
14283 if (args == 0)
14284 {
14285 struct breakpoint *bpt;
14286
14287 ALL_BREAKPOINTS (bpt)
14288 if (user_breakpoint_p (bpt))
14289 disable_breakpoint (bpt);
14290 }
14291 else
14292 {
14293 std::string num = extract_arg (&args);
14294
14295 while (!num.empty ())
14296 {
14297 if (num.find ('.') != std::string::npos)
14298 {
14299 struct bp_location *loc = find_location_by_number (num.c_str ());
14300
14301 if (loc)
14302 {
14303 if (loc->enabled)
14304 {
14305 loc->enabled = 0;
14306 mark_breakpoint_location_modified (loc);
14307 }
14308 if (target_supports_enable_disable_tracepoint ()
14309 && current_trace_status ()->running && loc->owner
14310 && is_tracepoint (loc->owner))
14311 target_disable_tracepoint (loc);
14312 }
14313 update_global_location_list (UGLL_DONT_INSERT);
14314 }
14315 else
14316 map_breakpoint_numbers
14317 (num.c_str (), [&] (breakpoint *b)
14318 {
14319 iterate_over_related_breakpoints (b, disable_breakpoint);
14320 });
14321 num = extract_arg (&args);
14322 }
14323 }
14324 }
14325
14326 static void
14327 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14328 int count)
14329 {
14330 int target_resources_ok;
14331
14332 if (bpt->type == bp_hardware_breakpoint)
14333 {
14334 int i;
14335 i = hw_breakpoint_used_count ();
14336 target_resources_ok =
14337 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14338 i + 1, 0);
14339 if (target_resources_ok == 0)
14340 error (_("No hardware breakpoint support in the target."));
14341 else if (target_resources_ok < 0)
14342 error (_("Hardware breakpoints used exceeds limit."));
14343 }
14344
14345 if (is_watchpoint (bpt))
14346 {
14347 /* Initialize it just to avoid a GCC false warning. */
14348 enum enable_state orig_enable_state = bp_disabled;
14349
14350 TRY
14351 {
14352 struct watchpoint *w = (struct watchpoint *) bpt;
14353
14354 orig_enable_state = bpt->enable_state;
14355 bpt->enable_state = bp_enabled;
14356 update_watchpoint (w, 1 /* reparse */);
14357 }
14358 CATCH (e, RETURN_MASK_ALL)
14359 {
14360 bpt->enable_state = orig_enable_state;
14361 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14362 bpt->number);
14363 return;
14364 }
14365 END_CATCH
14366 }
14367
14368 bpt->enable_state = bp_enabled;
14369
14370 /* Mark breakpoint locations modified. */
14371 mark_breakpoint_modified (bpt);
14372
14373 if (target_supports_enable_disable_tracepoint ()
14374 && current_trace_status ()->running && is_tracepoint (bpt))
14375 {
14376 struct bp_location *location;
14377
14378 for (location = bpt->loc; location; location = location->next)
14379 target_enable_tracepoint (location);
14380 }
14381
14382 bpt->disposition = disposition;
14383 bpt->enable_count = count;
14384 update_global_location_list (UGLL_MAY_INSERT);
14385
14386 observer_notify_breakpoint_modified (bpt);
14387 }
14388
14389
14390 void
14391 enable_breakpoint (struct breakpoint *bpt)
14392 {
14393 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14394 }
14395
14396 /* The enable command enables the specified breakpoints (or all defined
14397 breakpoints) so they once again become (or continue to be) effective
14398 in stopping the inferior. */
14399
14400 static void
14401 enable_command (const char *args, int from_tty)
14402 {
14403 if (args == 0)
14404 {
14405 struct breakpoint *bpt;
14406
14407 ALL_BREAKPOINTS (bpt)
14408 if (user_breakpoint_p (bpt))
14409 enable_breakpoint (bpt);
14410 }
14411 else
14412 {
14413 std::string num = extract_arg (&args);
14414
14415 while (!num.empty ())
14416 {
14417 if (num.find ('.') != std::string::npos)
14418 {
14419 struct bp_location *loc = find_location_by_number (num.c_str ());
14420
14421 if (loc)
14422 {
14423 if (!loc->enabled)
14424 {
14425 loc->enabled = 1;
14426 mark_breakpoint_location_modified (loc);
14427 }
14428 if (target_supports_enable_disable_tracepoint ()
14429 && current_trace_status ()->running && loc->owner
14430 && is_tracepoint (loc->owner))
14431 target_enable_tracepoint (loc);
14432 }
14433 update_global_location_list (UGLL_MAY_INSERT);
14434 }
14435 else
14436 map_breakpoint_numbers
14437 (num.c_str (), [&] (breakpoint *b)
14438 {
14439 iterate_over_related_breakpoints (b, enable_breakpoint);
14440 });
14441 num = extract_arg (&args);
14442 }
14443 }
14444 }
14445
14446 static void
14447 enable_once_command (const char *args, int from_tty)
14448 {
14449 map_breakpoint_numbers
14450 (args, [&] (breakpoint *b)
14451 {
14452 iterate_over_related_breakpoints
14453 (b, [&] (breakpoint *bpt)
14454 {
14455 enable_breakpoint_disp (bpt, disp_disable, 1);
14456 });
14457 });
14458 }
14459
14460 static void
14461 enable_count_command (const char *args, int from_tty)
14462 {
14463 int count;
14464
14465 if (args == NULL)
14466 error_no_arg (_("hit count"));
14467
14468 count = get_number (&args);
14469
14470 map_breakpoint_numbers
14471 (args, [&] (breakpoint *b)
14472 {
14473 iterate_over_related_breakpoints
14474 (b, [&] (breakpoint *bpt)
14475 {
14476 enable_breakpoint_disp (bpt, disp_disable, count);
14477 });
14478 });
14479 }
14480
14481 static void
14482 enable_delete_command (const char *args, int from_tty)
14483 {
14484 map_breakpoint_numbers
14485 (args, [&] (breakpoint *b)
14486 {
14487 iterate_over_related_breakpoints
14488 (b, [&] (breakpoint *bpt)
14489 {
14490 enable_breakpoint_disp (bpt, disp_del, 1);
14491 });
14492 });
14493 }
14494 \f
14495 static void
14496 set_breakpoint_cmd (const char *args, int from_tty)
14497 {
14498 }
14499
14500 static void
14501 show_breakpoint_cmd (const char *args, int from_tty)
14502 {
14503 }
14504
14505 /* Invalidate last known value of any hardware watchpoint if
14506 the memory which that value represents has been written to by
14507 GDB itself. */
14508
14509 static void
14510 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14511 CORE_ADDR addr, ssize_t len,
14512 const bfd_byte *data)
14513 {
14514 struct breakpoint *bp;
14515
14516 ALL_BREAKPOINTS (bp)
14517 if (bp->enable_state == bp_enabled
14518 && bp->type == bp_hardware_watchpoint)
14519 {
14520 struct watchpoint *wp = (struct watchpoint *) bp;
14521
14522 if (wp->val_valid && wp->val)
14523 {
14524 struct bp_location *loc;
14525
14526 for (loc = bp->loc; loc != NULL; loc = loc->next)
14527 if (loc->loc_type == bp_loc_hardware_watchpoint
14528 && loc->address + loc->length > addr
14529 && addr + len > loc->address)
14530 {
14531 value_free (wp->val);
14532 wp->val = NULL;
14533 wp->val_valid = 0;
14534 }
14535 }
14536 }
14537 }
14538
14539 /* Create and insert a breakpoint for software single step. */
14540
14541 void
14542 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14543 const address_space *aspace,
14544 CORE_ADDR next_pc)
14545 {
14546 struct thread_info *tp = inferior_thread ();
14547 struct symtab_and_line sal;
14548 CORE_ADDR pc = next_pc;
14549
14550 if (tp->control.single_step_breakpoints == NULL)
14551 {
14552 tp->control.single_step_breakpoints
14553 = new_single_step_breakpoint (tp->global_num, gdbarch);
14554 }
14555
14556 sal = find_pc_line (pc, 0);
14557 sal.pc = pc;
14558 sal.section = find_pc_overlay (pc);
14559 sal.explicit_pc = 1;
14560 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14561
14562 update_global_location_list (UGLL_INSERT);
14563 }
14564
14565 /* Insert single step breakpoints according to the current state. */
14566
14567 int
14568 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14569 {
14570 struct regcache *regcache = get_current_regcache ();
14571 std::vector<CORE_ADDR> next_pcs;
14572
14573 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14574
14575 if (!next_pcs.empty ())
14576 {
14577 struct frame_info *frame = get_current_frame ();
14578 struct address_space *aspace = get_frame_address_space (frame);
14579
14580 for (CORE_ADDR pc : next_pcs)
14581 insert_single_step_breakpoint (gdbarch, aspace, pc);
14582
14583 return 1;
14584 }
14585 else
14586 return 0;
14587 }
14588
14589 /* See breakpoint.h. */
14590
14591 int
14592 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14593 const address_space *aspace,
14594 CORE_ADDR pc)
14595 {
14596 struct bp_location *loc;
14597
14598 for (loc = bp->loc; loc != NULL; loc = loc->next)
14599 if (loc->inserted
14600 && breakpoint_location_address_match (loc, aspace, pc))
14601 return 1;
14602
14603 return 0;
14604 }
14605
14606 /* Check whether a software single-step breakpoint is inserted at
14607 PC. */
14608
14609 int
14610 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14611 CORE_ADDR pc)
14612 {
14613 struct breakpoint *bpt;
14614
14615 ALL_BREAKPOINTS (bpt)
14616 {
14617 if (bpt->type == bp_single_step
14618 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14619 return 1;
14620 }
14621 return 0;
14622 }
14623
14624 /* Tracepoint-specific operations. */
14625
14626 /* Set tracepoint count to NUM. */
14627 static void
14628 set_tracepoint_count (int num)
14629 {
14630 tracepoint_count = num;
14631 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14632 }
14633
14634 static void
14635 trace_command (char *arg_in, int from_tty)
14636 {
14637 const char *arg = arg_in;
14638 struct breakpoint_ops *ops;
14639
14640 event_location_up location = string_to_event_location (&arg,
14641 current_language);
14642 if (location != NULL
14643 && event_location_type (location.get ()) == PROBE_LOCATION)
14644 ops = &tracepoint_probe_breakpoint_ops;
14645 else
14646 ops = &tracepoint_breakpoint_ops;
14647
14648 create_breakpoint (get_current_arch (),
14649 location.get (),
14650 NULL, 0, arg, 1 /* parse arg */,
14651 0 /* tempflag */,
14652 bp_tracepoint /* type_wanted */,
14653 0 /* Ignore count */,
14654 pending_break_support,
14655 ops,
14656 from_tty,
14657 1 /* enabled */,
14658 0 /* internal */, 0);
14659 }
14660
14661 static void
14662 ftrace_command (char *arg_in, int from_tty)
14663 {
14664 const char *arg = arg_in;
14665 event_location_up location = string_to_event_location (&arg,
14666 current_language);
14667 create_breakpoint (get_current_arch (),
14668 location.get (),
14669 NULL, 0, arg, 1 /* parse arg */,
14670 0 /* tempflag */,
14671 bp_fast_tracepoint /* type_wanted */,
14672 0 /* Ignore count */,
14673 pending_break_support,
14674 &tracepoint_breakpoint_ops,
14675 from_tty,
14676 1 /* enabled */,
14677 0 /* internal */, 0);
14678 }
14679
14680 /* strace command implementation. Creates a static tracepoint. */
14681
14682 static void
14683 strace_command (char *arg_in, int from_tty)
14684 {
14685 const char *arg = arg_in;
14686 struct breakpoint_ops *ops;
14687 event_location_up location;
14688
14689 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14690 or with a normal static tracepoint. */
14691 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14692 {
14693 ops = &strace_marker_breakpoint_ops;
14694 location = new_linespec_location (&arg);
14695 }
14696 else
14697 {
14698 ops = &tracepoint_breakpoint_ops;
14699 location = string_to_event_location (&arg, current_language);
14700 }
14701
14702 create_breakpoint (get_current_arch (),
14703 location.get (),
14704 NULL, 0, arg, 1 /* parse arg */,
14705 0 /* tempflag */,
14706 bp_static_tracepoint /* type_wanted */,
14707 0 /* Ignore count */,
14708 pending_break_support,
14709 ops,
14710 from_tty,
14711 1 /* enabled */,
14712 0 /* internal */, 0);
14713 }
14714
14715 /* Set up a fake reader function that gets command lines from a linked
14716 list that was acquired during tracepoint uploading. */
14717
14718 static struct uploaded_tp *this_utp;
14719 static int next_cmd;
14720
14721 static char *
14722 read_uploaded_action (void)
14723 {
14724 char *rslt;
14725
14726 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14727
14728 next_cmd++;
14729
14730 return rslt;
14731 }
14732
14733 /* Given information about a tracepoint as recorded on a target (which
14734 can be either a live system or a trace file), attempt to create an
14735 equivalent GDB tracepoint. This is not a reliable process, since
14736 the target does not necessarily have all the information used when
14737 the tracepoint was originally defined. */
14738
14739 struct tracepoint *
14740 create_tracepoint_from_upload (struct uploaded_tp *utp)
14741 {
14742 const char *addr_str;
14743 char small_buf[100];
14744 struct tracepoint *tp;
14745
14746 if (utp->at_string)
14747 addr_str = utp->at_string;
14748 else
14749 {
14750 /* In the absence of a source location, fall back to raw
14751 address. Since there is no way to confirm that the address
14752 means the same thing as when the trace was started, warn the
14753 user. */
14754 warning (_("Uploaded tracepoint %d has no "
14755 "source location, using raw address"),
14756 utp->number);
14757 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14758 addr_str = small_buf;
14759 }
14760
14761 /* There's not much we can do with a sequence of bytecodes. */
14762 if (utp->cond && !utp->cond_string)
14763 warning (_("Uploaded tracepoint %d condition "
14764 "has no source form, ignoring it"),
14765 utp->number);
14766
14767 event_location_up location = string_to_event_location (&addr_str,
14768 current_language);
14769 if (!create_breakpoint (get_current_arch (),
14770 location.get (),
14771 utp->cond_string, -1, addr_str,
14772 0 /* parse cond/thread */,
14773 0 /* tempflag */,
14774 utp->type /* type_wanted */,
14775 0 /* Ignore count */,
14776 pending_break_support,
14777 &tracepoint_breakpoint_ops,
14778 0 /* from_tty */,
14779 utp->enabled /* enabled */,
14780 0 /* internal */,
14781 CREATE_BREAKPOINT_FLAGS_INSERTED))
14782 return NULL;
14783
14784 /* Get the tracepoint we just created. */
14785 tp = get_tracepoint (tracepoint_count);
14786 gdb_assert (tp != NULL);
14787
14788 if (utp->pass > 0)
14789 {
14790 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14791 tp->number);
14792
14793 trace_pass_command (small_buf, 0);
14794 }
14795
14796 /* If we have uploaded versions of the original commands, set up a
14797 special-purpose "reader" function and call the usual command line
14798 reader, then pass the result to the breakpoint command-setting
14799 function. */
14800 if (!VEC_empty (char_ptr, utp->cmd_strings))
14801 {
14802 command_line_up cmd_list;
14803
14804 this_utp = utp;
14805 next_cmd = 0;
14806
14807 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14808
14809 breakpoint_set_commands (tp, std::move (cmd_list));
14810 }
14811 else if (!VEC_empty (char_ptr, utp->actions)
14812 || !VEC_empty (char_ptr, utp->step_actions))
14813 warning (_("Uploaded tracepoint %d actions "
14814 "have no source form, ignoring them"),
14815 utp->number);
14816
14817 /* Copy any status information that might be available. */
14818 tp->hit_count = utp->hit_count;
14819 tp->traceframe_usage = utp->traceframe_usage;
14820
14821 return tp;
14822 }
14823
14824 /* Print information on tracepoint number TPNUM_EXP, or all if
14825 omitted. */
14826
14827 static void
14828 info_tracepoints_command (char *args, int from_tty)
14829 {
14830 struct ui_out *uiout = current_uiout;
14831 int num_printed;
14832
14833 num_printed = breakpoint_1 (args, 0, is_tracepoint);
14834
14835 if (num_printed == 0)
14836 {
14837 if (args == NULL || *args == '\0')
14838 uiout->message ("No tracepoints.\n");
14839 else
14840 uiout->message ("No tracepoint matching '%s'.\n", args);
14841 }
14842
14843 default_collect_info ();
14844 }
14845
14846 /* The 'enable trace' command enables tracepoints.
14847 Not supported by all targets. */
14848 static void
14849 enable_trace_command (char *args, int from_tty)
14850 {
14851 enable_command (args, from_tty);
14852 }
14853
14854 /* The 'disable trace' command disables tracepoints.
14855 Not supported by all targets. */
14856 static void
14857 disable_trace_command (char *args, int from_tty)
14858 {
14859 disable_command (args, from_tty);
14860 }
14861
14862 /* Remove a tracepoint (or all if no argument). */
14863 static void
14864 delete_trace_command (const char *arg, int from_tty)
14865 {
14866 struct breakpoint *b, *b_tmp;
14867
14868 dont_repeat ();
14869
14870 if (arg == 0)
14871 {
14872 int breaks_to_delete = 0;
14873
14874 /* Delete all breakpoints if no argument.
14875 Do not delete internal or call-dummy breakpoints, these
14876 have to be deleted with an explicit breakpoint number
14877 argument. */
14878 ALL_TRACEPOINTS (b)
14879 if (is_tracepoint (b) && user_breakpoint_p (b))
14880 {
14881 breaks_to_delete = 1;
14882 break;
14883 }
14884
14885 /* Ask user only if there are some breakpoints to delete. */
14886 if (!from_tty
14887 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14888 {
14889 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14890 if (is_tracepoint (b) && user_breakpoint_p (b))
14891 delete_breakpoint (b);
14892 }
14893 }
14894 else
14895 map_breakpoint_numbers
14896 (arg, [&] (breakpoint *b)
14897 {
14898 iterate_over_related_breakpoints (b, delete_breakpoint);
14899 });
14900 }
14901
14902 /* Helper function for trace_pass_command. */
14903
14904 static void
14905 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14906 {
14907 tp->pass_count = count;
14908 observer_notify_breakpoint_modified (tp);
14909 if (from_tty)
14910 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14911 tp->number, count);
14912 }
14913
14914 /* Set passcount for tracepoint.
14915
14916 First command argument is passcount, second is tracepoint number.
14917 If tracepoint number omitted, apply to most recently defined.
14918 Also accepts special argument "all". */
14919
14920 static void
14921 trace_pass_command (char *args, int from_tty)
14922 {
14923 struct tracepoint *t1;
14924 unsigned int count;
14925
14926 if (args == 0 || *args == 0)
14927 error (_("passcount command requires an "
14928 "argument (count + optional TP num)"));
14929
14930 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
14931
14932 args = skip_spaces (args);
14933 if (*args && strncasecmp (args, "all", 3) == 0)
14934 {
14935 struct breakpoint *b;
14936
14937 args += 3; /* Skip special argument "all". */
14938 if (*args)
14939 error (_("Junk at end of arguments."));
14940
14941 ALL_TRACEPOINTS (b)
14942 {
14943 t1 = (struct tracepoint *) b;
14944 trace_pass_set_count (t1, count, from_tty);
14945 }
14946 }
14947 else if (*args == '\0')
14948 {
14949 t1 = get_tracepoint_by_number (&args, NULL);
14950 if (t1)
14951 trace_pass_set_count (t1, count, from_tty);
14952 }
14953 else
14954 {
14955 number_or_range_parser parser (args);
14956 while (!parser.finished ())
14957 {
14958 t1 = get_tracepoint_by_number (&args, &parser);
14959 if (t1)
14960 trace_pass_set_count (t1, count, from_tty);
14961 }
14962 }
14963 }
14964
14965 struct tracepoint *
14966 get_tracepoint (int num)
14967 {
14968 struct breakpoint *t;
14969
14970 ALL_TRACEPOINTS (t)
14971 if (t->number == num)
14972 return (struct tracepoint *) t;
14973
14974 return NULL;
14975 }
14976
14977 /* Find the tracepoint with the given target-side number (which may be
14978 different from the tracepoint number after disconnecting and
14979 reconnecting). */
14980
14981 struct tracepoint *
14982 get_tracepoint_by_number_on_target (int num)
14983 {
14984 struct breakpoint *b;
14985
14986 ALL_TRACEPOINTS (b)
14987 {
14988 struct tracepoint *t = (struct tracepoint *) b;
14989
14990 if (t->number_on_target == num)
14991 return t;
14992 }
14993
14994 return NULL;
14995 }
14996
14997 /* Utility: parse a tracepoint number and look it up in the list.
14998 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14999 If the argument is missing, the most recent tracepoint
15000 (tracepoint_count) is returned. */
15001
15002 struct tracepoint *
15003 get_tracepoint_by_number (char **arg,
15004 number_or_range_parser *parser)
15005 {
15006 struct breakpoint *t;
15007 int tpnum;
15008 char *instring = arg == NULL ? NULL : *arg;
15009
15010 if (parser != NULL)
15011 {
15012 gdb_assert (!parser->finished ());
15013 tpnum = parser->get_number ();
15014 }
15015 else if (arg == NULL || *arg == NULL || ! **arg)
15016 tpnum = tracepoint_count;
15017 else
15018 tpnum = get_number (arg);
15019
15020 if (tpnum <= 0)
15021 {
15022 if (instring && *instring)
15023 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15024 instring);
15025 else
15026 printf_filtered (_("No previous tracepoint\n"));
15027 return NULL;
15028 }
15029
15030 ALL_TRACEPOINTS (t)
15031 if (t->number == tpnum)
15032 {
15033 return (struct tracepoint *) t;
15034 }
15035
15036 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15037 return NULL;
15038 }
15039
15040 void
15041 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15042 {
15043 if (b->thread != -1)
15044 fprintf_unfiltered (fp, " thread %d", b->thread);
15045
15046 if (b->task != 0)
15047 fprintf_unfiltered (fp, " task %d", b->task);
15048
15049 fprintf_unfiltered (fp, "\n");
15050 }
15051
15052 /* Save information on user settable breakpoints (watchpoints, etc) to
15053 a new script file named FILENAME. If FILTER is non-NULL, call it
15054 on each breakpoint and only include the ones for which it returns
15055 non-zero. */
15056
15057 static void
15058 save_breakpoints (const char *filename, int from_tty,
15059 int (*filter) (const struct breakpoint *))
15060 {
15061 struct breakpoint *tp;
15062 int any = 0;
15063 int extra_trace_bits = 0;
15064
15065 if (filename == 0 || *filename == 0)
15066 error (_("Argument required (file name in which to save)"));
15067
15068 /* See if we have anything to save. */
15069 ALL_BREAKPOINTS (tp)
15070 {
15071 /* Skip internal and momentary breakpoints. */
15072 if (!user_breakpoint_p (tp))
15073 continue;
15074
15075 /* If we have a filter, only save the breakpoints it accepts. */
15076 if (filter && !filter (tp))
15077 continue;
15078
15079 any = 1;
15080
15081 if (is_tracepoint (tp))
15082 {
15083 extra_trace_bits = 1;
15084
15085 /* We can stop searching. */
15086 break;
15087 }
15088 }
15089
15090 if (!any)
15091 {
15092 warning (_("Nothing to save."));
15093 return;
15094 }
15095
15096 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15097
15098 stdio_file fp;
15099
15100 if (!fp.open (expanded_filename.get (), "w"))
15101 error (_("Unable to open file '%s' for saving (%s)"),
15102 expanded_filename.get (), safe_strerror (errno));
15103
15104 if (extra_trace_bits)
15105 save_trace_state_variables (&fp);
15106
15107 ALL_BREAKPOINTS (tp)
15108 {
15109 /* Skip internal and momentary breakpoints. */
15110 if (!user_breakpoint_p (tp))
15111 continue;
15112
15113 /* If we have a filter, only save the breakpoints it accepts. */
15114 if (filter && !filter (tp))
15115 continue;
15116
15117 tp->ops->print_recreate (tp, &fp);
15118
15119 /* Note, we can't rely on tp->number for anything, as we can't
15120 assume the recreated breakpoint numbers will match. Use $bpnum
15121 instead. */
15122
15123 if (tp->cond_string)
15124 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15125
15126 if (tp->ignore_count)
15127 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15128
15129 if (tp->type != bp_dprintf && tp->commands)
15130 {
15131 fp.puts (" commands\n");
15132
15133 current_uiout->redirect (&fp);
15134 TRY
15135 {
15136 print_command_lines (current_uiout, tp->commands.get (), 2);
15137 }
15138 CATCH (ex, RETURN_MASK_ALL)
15139 {
15140 current_uiout->redirect (NULL);
15141 throw_exception (ex);
15142 }
15143 END_CATCH
15144
15145 current_uiout->redirect (NULL);
15146 fp.puts (" end\n");
15147 }
15148
15149 if (tp->enable_state == bp_disabled)
15150 fp.puts ("disable $bpnum\n");
15151
15152 /* If this is a multi-location breakpoint, check if the locations
15153 should be individually disabled. Watchpoint locations are
15154 special, and not user visible. */
15155 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15156 {
15157 struct bp_location *loc;
15158 int n = 1;
15159
15160 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15161 if (!loc->enabled)
15162 fp.printf ("disable $bpnum.%d\n", n);
15163 }
15164 }
15165
15166 if (extra_trace_bits && *default_collect)
15167 fp.printf ("set default-collect %s\n", default_collect);
15168
15169 if (from_tty)
15170 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15171 }
15172
15173 /* The `save breakpoints' command. */
15174
15175 static void
15176 save_breakpoints_command (const char *args, int from_tty)
15177 {
15178 save_breakpoints (args, from_tty, NULL);
15179 }
15180
15181 /* The `save tracepoints' command. */
15182
15183 static void
15184 save_tracepoints_command (const char *args, int from_tty)
15185 {
15186 save_breakpoints (args, from_tty, is_tracepoint);
15187 }
15188
15189 /* Create a vector of all tracepoints. */
15190
15191 VEC(breakpoint_p) *
15192 all_tracepoints (void)
15193 {
15194 VEC(breakpoint_p) *tp_vec = 0;
15195 struct breakpoint *tp;
15196
15197 ALL_TRACEPOINTS (tp)
15198 {
15199 VEC_safe_push (breakpoint_p, tp_vec, tp);
15200 }
15201
15202 return tp_vec;
15203 }
15204
15205 \f
15206 /* This help string is used to consolidate all the help string for specifying
15207 locations used by several commands. */
15208
15209 #define LOCATION_HELP_STRING \
15210 "Linespecs are colon-separated lists of location parameters, such as\n\
15211 source filename, function name, label name, and line number.\n\
15212 Example: To specify the start of a label named \"the_top\" in the\n\
15213 function \"fact\" in the file \"factorial.c\", use\n\
15214 \"factorial.c:fact:the_top\".\n\
15215 \n\
15216 Address locations begin with \"*\" and specify an exact address in the\n\
15217 program. Example: To specify the fourth byte past the start function\n\
15218 \"main\", use \"*main + 4\".\n\
15219 \n\
15220 Explicit locations are similar to linespecs but use an option/argument\n\
15221 syntax to specify location parameters.\n\
15222 Example: To specify the start of the label named \"the_top\" in the\n\
15223 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15224 -function fact -label the_top\".\n"
15225
15226 /* This help string is used for the break, hbreak, tbreak and thbreak
15227 commands. It is defined as a macro to prevent duplication.
15228 COMMAND should be a string constant containing the name of the
15229 command. */
15230
15231 #define BREAK_ARGS_HELP(command) \
15232 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15233 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15234 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15235 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15236 `-probe-dtrace' (for a DTrace probe).\n\
15237 LOCATION may be a linespec, address, or explicit location as described\n\
15238 below.\n\
15239 \n\
15240 With no LOCATION, uses current execution address of the selected\n\
15241 stack frame. This is useful for breaking on return to a stack frame.\n\
15242 \n\
15243 THREADNUM is the number from \"info threads\".\n\
15244 CONDITION is a boolean expression.\n\
15245 \n" LOCATION_HELP_STRING "\n\
15246 Multiple breakpoints at one place are permitted, and useful if their\n\
15247 conditions are different.\n\
15248 \n\
15249 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15250
15251 /* List of subcommands for "catch". */
15252 static struct cmd_list_element *catch_cmdlist;
15253
15254 /* List of subcommands for "tcatch". */
15255 static struct cmd_list_element *tcatch_cmdlist;
15256
15257 void
15258 add_catch_command (const char *name, const char *docstring,
15259 cmd_sfunc_ftype *sfunc,
15260 completer_ftype *completer,
15261 void *user_data_catch,
15262 void *user_data_tcatch)
15263 {
15264 struct cmd_list_element *command;
15265
15266 command = add_cmd (name, class_breakpoint, docstring,
15267 &catch_cmdlist);
15268 set_cmd_sfunc (command, sfunc);
15269 set_cmd_context (command, user_data_catch);
15270 set_cmd_completer (command, completer);
15271
15272 command = add_cmd (name, class_breakpoint, docstring,
15273 &tcatch_cmdlist);
15274 set_cmd_sfunc (command, sfunc);
15275 set_cmd_context (command, user_data_tcatch);
15276 set_cmd_completer (command, completer);
15277 }
15278
15279 static void
15280 save_command (const char *arg, int from_tty)
15281 {
15282 printf_unfiltered (_("\"save\" must be followed by "
15283 "the name of a save subcommand.\n"));
15284 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15285 }
15286
15287 struct breakpoint *
15288 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15289 void *data)
15290 {
15291 struct breakpoint *b, *b_tmp;
15292
15293 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15294 {
15295 if ((*callback) (b, data))
15296 return b;
15297 }
15298
15299 return NULL;
15300 }
15301
15302 /* Zero if any of the breakpoint's locations could be a location where
15303 functions have been inlined, nonzero otherwise. */
15304
15305 static int
15306 is_non_inline_function (struct breakpoint *b)
15307 {
15308 /* The shared library event breakpoint is set on the address of a
15309 non-inline function. */
15310 if (b->type == bp_shlib_event)
15311 return 1;
15312
15313 return 0;
15314 }
15315
15316 /* Nonzero if the specified PC cannot be a location where functions
15317 have been inlined. */
15318
15319 int
15320 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15321 const struct target_waitstatus *ws)
15322 {
15323 struct breakpoint *b;
15324 struct bp_location *bl;
15325
15326 ALL_BREAKPOINTS (b)
15327 {
15328 if (!is_non_inline_function (b))
15329 continue;
15330
15331 for (bl = b->loc; bl != NULL; bl = bl->next)
15332 {
15333 if (!bl->shlib_disabled
15334 && bpstat_check_location (bl, aspace, pc, ws))
15335 return 1;
15336 }
15337 }
15338
15339 return 0;
15340 }
15341
15342 /* Remove any references to OBJFILE which is going to be freed. */
15343
15344 void
15345 breakpoint_free_objfile (struct objfile *objfile)
15346 {
15347 struct bp_location **locp, *loc;
15348
15349 ALL_BP_LOCATIONS (loc, locp)
15350 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15351 loc->symtab = NULL;
15352 }
15353
15354 void
15355 initialize_breakpoint_ops (void)
15356 {
15357 static int initialized = 0;
15358
15359 struct breakpoint_ops *ops;
15360
15361 if (initialized)
15362 return;
15363 initialized = 1;
15364
15365 /* The breakpoint_ops structure to be inherit by all kinds of
15366 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15367 internal and momentary breakpoints, etc.). */
15368 ops = &bkpt_base_breakpoint_ops;
15369 *ops = base_breakpoint_ops;
15370 ops->re_set = bkpt_re_set;
15371 ops->insert_location = bkpt_insert_location;
15372 ops->remove_location = bkpt_remove_location;
15373 ops->breakpoint_hit = bkpt_breakpoint_hit;
15374 ops->create_sals_from_location = bkpt_create_sals_from_location;
15375 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15376 ops->decode_location = bkpt_decode_location;
15377
15378 /* The breakpoint_ops structure to be used in regular breakpoints. */
15379 ops = &bkpt_breakpoint_ops;
15380 *ops = bkpt_base_breakpoint_ops;
15381 ops->re_set = bkpt_re_set;
15382 ops->resources_needed = bkpt_resources_needed;
15383 ops->print_it = bkpt_print_it;
15384 ops->print_mention = bkpt_print_mention;
15385 ops->print_recreate = bkpt_print_recreate;
15386
15387 /* Ranged breakpoints. */
15388 ops = &ranged_breakpoint_ops;
15389 *ops = bkpt_breakpoint_ops;
15390 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15391 ops->resources_needed = resources_needed_ranged_breakpoint;
15392 ops->print_it = print_it_ranged_breakpoint;
15393 ops->print_one = print_one_ranged_breakpoint;
15394 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15395 ops->print_mention = print_mention_ranged_breakpoint;
15396 ops->print_recreate = print_recreate_ranged_breakpoint;
15397
15398 /* Internal breakpoints. */
15399 ops = &internal_breakpoint_ops;
15400 *ops = bkpt_base_breakpoint_ops;
15401 ops->re_set = internal_bkpt_re_set;
15402 ops->check_status = internal_bkpt_check_status;
15403 ops->print_it = internal_bkpt_print_it;
15404 ops->print_mention = internal_bkpt_print_mention;
15405
15406 /* Momentary breakpoints. */
15407 ops = &momentary_breakpoint_ops;
15408 *ops = bkpt_base_breakpoint_ops;
15409 ops->re_set = momentary_bkpt_re_set;
15410 ops->check_status = momentary_bkpt_check_status;
15411 ops->print_it = momentary_bkpt_print_it;
15412 ops->print_mention = momentary_bkpt_print_mention;
15413
15414 /* Probe breakpoints. */
15415 ops = &bkpt_probe_breakpoint_ops;
15416 *ops = bkpt_breakpoint_ops;
15417 ops->insert_location = bkpt_probe_insert_location;
15418 ops->remove_location = bkpt_probe_remove_location;
15419 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15420 ops->decode_location = bkpt_probe_decode_location;
15421
15422 /* Watchpoints. */
15423 ops = &watchpoint_breakpoint_ops;
15424 *ops = base_breakpoint_ops;
15425 ops->re_set = re_set_watchpoint;
15426 ops->insert_location = insert_watchpoint;
15427 ops->remove_location = remove_watchpoint;
15428 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15429 ops->check_status = check_status_watchpoint;
15430 ops->resources_needed = resources_needed_watchpoint;
15431 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15432 ops->print_it = print_it_watchpoint;
15433 ops->print_mention = print_mention_watchpoint;
15434 ops->print_recreate = print_recreate_watchpoint;
15435 ops->explains_signal = explains_signal_watchpoint;
15436
15437 /* Masked watchpoints. */
15438 ops = &masked_watchpoint_breakpoint_ops;
15439 *ops = watchpoint_breakpoint_ops;
15440 ops->insert_location = insert_masked_watchpoint;
15441 ops->remove_location = remove_masked_watchpoint;
15442 ops->resources_needed = resources_needed_masked_watchpoint;
15443 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15444 ops->print_it = print_it_masked_watchpoint;
15445 ops->print_one_detail = print_one_detail_masked_watchpoint;
15446 ops->print_mention = print_mention_masked_watchpoint;
15447 ops->print_recreate = print_recreate_masked_watchpoint;
15448
15449 /* Tracepoints. */
15450 ops = &tracepoint_breakpoint_ops;
15451 *ops = base_breakpoint_ops;
15452 ops->re_set = tracepoint_re_set;
15453 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15454 ops->print_one_detail = tracepoint_print_one_detail;
15455 ops->print_mention = tracepoint_print_mention;
15456 ops->print_recreate = tracepoint_print_recreate;
15457 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15458 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15459 ops->decode_location = tracepoint_decode_location;
15460
15461 /* Probe tracepoints. */
15462 ops = &tracepoint_probe_breakpoint_ops;
15463 *ops = tracepoint_breakpoint_ops;
15464 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15465 ops->decode_location = tracepoint_probe_decode_location;
15466
15467 /* Static tracepoints with marker (`-m'). */
15468 ops = &strace_marker_breakpoint_ops;
15469 *ops = tracepoint_breakpoint_ops;
15470 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15471 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15472 ops->decode_location = strace_marker_decode_location;
15473
15474 /* Fork catchpoints. */
15475 ops = &catch_fork_breakpoint_ops;
15476 *ops = base_breakpoint_ops;
15477 ops->insert_location = insert_catch_fork;
15478 ops->remove_location = remove_catch_fork;
15479 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15480 ops->print_it = print_it_catch_fork;
15481 ops->print_one = print_one_catch_fork;
15482 ops->print_mention = print_mention_catch_fork;
15483 ops->print_recreate = print_recreate_catch_fork;
15484
15485 /* Vfork catchpoints. */
15486 ops = &catch_vfork_breakpoint_ops;
15487 *ops = base_breakpoint_ops;
15488 ops->insert_location = insert_catch_vfork;
15489 ops->remove_location = remove_catch_vfork;
15490 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15491 ops->print_it = print_it_catch_vfork;
15492 ops->print_one = print_one_catch_vfork;
15493 ops->print_mention = print_mention_catch_vfork;
15494 ops->print_recreate = print_recreate_catch_vfork;
15495
15496 /* Exec catchpoints. */
15497 ops = &catch_exec_breakpoint_ops;
15498 *ops = base_breakpoint_ops;
15499 ops->insert_location = insert_catch_exec;
15500 ops->remove_location = remove_catch_exec;
15501 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15502 ops->print_it = print_it_catch_exec;
15503 ops->print_one = print_one_catch_exec;
15504 ops->print_mention = print_mention_catch_exec;
15505 ops->print_recreate = print_recreate_catch_exec;
15506
15507 /* Solib-related catchpoints. */
15508 ops = &catch_solib_breakpoint_ops;
15509 *ops = base_breakpoint_ops;
15510 ops->insert_location = insert_catch_solib;
15511 ops->remove_location = remove_catch_solib;
15512 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15513 ops->check_status = check_status_catch_solib;
15514 ops->print_it = print_it_catch_solib;
15515 ops->print_one = print_one_catch_solib;
15516 ops->print_mention = print_mention_catch_solib;
15517 ops->print_recreate = print_recreate_catch_solib;
15518
15519 ops = &dprintf_breakpoint_ops;
15520 *ops = bkpt_base_breakpoint_ops;
15521 ops->re_set = dprintf_re_set;
15522 ops->resources_needed = bkpt_resources_needed;
15523 ops->print_it = bkpt_print_it;
15524 ops->print_mention = bkpt_print_mention;
15525 ops->print_recreate = dprintf_print_recreate;
15526 ops->after_condition_true = dprintf_after_condition_true;
15527 ops->breakpoint_hit = dprintf_breakpoint_hit;
15528 }
15529
15530 /* Chain containing all defined "enable breakpoint" subcommands. */
15531
15532 static struct cmd_list_element *enablebreaklist = NULL;
15533
15534 void
15535 _initialize_breakpoint (void)
15536 {
15537 struct cmd_list_element *c;
15538
15539 initialize_breakpoint_ops ();
15540
15541 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15542 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15543 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15544
15545 breakpoint_objfile_key
15546 = register_objfile_data_with_cleanup (NULL, free_breakpoint_objfile_data);
15547
15548 breakpoint_chain = 0;
15549 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15550 before a breakpoint is set. */
15551 breakpoint_count = 0;
15552
15553 tracepoint_count = 0;
15554
15555 add_com ("ignore", class_breakpoint, ignore_command, _("\
15556 Set ignore-count of breakpoint number N to COUNT.\n\
15557 Usage is `ignore N COUNT'."));
15558
15559 add_com ("commands", class_breakpoint, commands_command, _("\
15560 Set commands to be executed when the given breakpoints are hit.\n\
15561 Give a space-separated breakpoint list as argument after \"commands\".\n\
15562 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15563 (e.g. `5-7').\n\
15564 With no argument, the targeted breakpoint is the last one set.\n\
15565 The commands themselves follow starting on the next line.\n\
15566 Type a line containing \"end\" to indicate the end of them.\n\
15567 Give \"silent\" as the first line to make the breakpoint silent;\n\
15568 then no output is printed when it is hit, except what the commands print."));
15569
15570 c = add_com ("condition", class_breakpoint, condition_command, _("\
15571 Specify breakpoint number N to break only if COND is true.\n\
15572 Usage is `condition N COND', where N is an integer and COND is an\n\
15573 expression to be evaluated whenever breakpoint N is reached."));
15574 set_cmd_completer (c, condition_completer);
15575
15576 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15577 Set a temporary breakpoint.\n\
15578 Like \"break\" except the breakpoint is only temporary,\n\
15579 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15580 by using \"enable delete\" on the breakpoint number.\n\
15581 \n"
15582 BREAK_ARGS_HELP ("tbreak")));
15583 set_cmd_completer (c, location_completer);
15584
15585 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15586 Set a hardware assisted breakpoint.\n\
15587 Like \"break\" except the breakpoint requires hardware support,\n\
15588 some target hardware may not have this support.\n\
15589 \n"
15590 BREAK_ARGS_HELP ("hbreak")));
15591 set_cmd_completer (c, location_completer);
15592
15593 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15594 Set a temporary hardware assisted breakpoint.\n\
15595 Like \"hbreak\" except the breakpoint is only temporary,\n\
15596 so it will be deleted when hit.\n\
15597 \n"
15598 BREAK_ARGS_HELP ("thbreak")));
15599 set_cmd_completer (c, location_completer);
15600
15601 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15602 Enable some breakpoints.\n\
15603 Give breakpoint numbers (separated by spaces) as arguments.\n\
15604 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15605 This is used to cancel the effect of the \"disable\" command.\n\
15606 With a subcommand you can enable temporarily."),
15607 &enablelist, "enable ", 1, &cmdlist);
15608
15609 add_com_alias ("en", "enable", class_breakpoint, 1);
15610
15611 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15612 Enable some breakpoints.\n\
15613 Give breakpoint numbers (separated by spaces) as arguments.\n\
15614 This is used to cancel the effect of the \"disable\" command.\n\
15615 May be abbreviated to simply \"enable\".\n"),
15616 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15617
15618 add_cmd ("once", no_class, enable_once_command, _("\
15619 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15620 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15621 &enablebreaklist);
15622
15623 add_cmd ("delete", no_class, enable_delete_command, _("\
15624 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15625 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15626 &enablebreaklist);
15627
15628 add_cmd ("count", no_class, enable_count_command, _("\
15629 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15630 If a breakpoint is hit while enabled in this fashion,\n\
15631 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15632 &enablebreaklist);
15633
15634 add_cmd ("delete", no_class, enable_delete_command, _("\
15635 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15636 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15637 &enablelist);
15638
15639 add_cmd ("once", no_class, enable_once_command, _("\
15640 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15641 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15642 &enablelist);
15643
15644 add_cmd ("count", no_class, enable_count_command, _("\
15645 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15646 If a breakpoint is hit while enabled in this fashion,\n\
15647 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15648 &enablelist);
15649
15650 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15651 Disable some breakpoints.\n\
15652 Arguments are breakpoint numbers with spaces in between.\n\
15653 To disable all breakpoints, give no argument.\n\
15654 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15655 &disablelist, "disable ", 1, &cmdlist);
15656 add_com_alias ("dis", "disable", class_breakpoint, 1);
15657 add_com_alias ("disa", "disable", class_breakpoint, 1);
15658
15659 add_cmd ("breakpoints", class_alias, disable_command, _("\
15660 Disable some breakpoints.\n\
15661 Arguments are breakpoint numbers with spaces in between.\n\
15662 To disable all breakpoints, give no argument.\n\
15663 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15664 This command may be abbreviated \"disable\"."),
15665 &disablelist);
15666
15667 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15668 Delete some breakpoints or auto-display expressions.\n\
15669 Arguments are breakpoint numbers with spaces in between.\n\
15670 To delete all breakpoints, give no argument.\n\
15671 \n\
15672 Also a prefix command for deletion of other GDB objects.\n\
15673 The \"unset\" command is also an alias for \"delete\"."),
15674 &deletelist, "delete ", 1, &cmdlist);
15675 add_com_alias ("d", "delete", class_breakpoint, 1);
15676 add_com_alias ("del", "delete", class_breakpoint, 1);
15677
15678 add_cmd ("breakpoints", class_alias, delete_command, _("\
15679 Delete some breakpoints or auto-display expressions.\n\
15680 Arguments are breakpoint numbers with spaces in between.\n\
15681 To delete all breakpoints, give no argument.\n\
15682 This command may be abbreviated \"delete\"."),
15683 &deletelist);
15684
15685 add_com ("clear", class_breakpoint, clear_command, _("\
15686 Clear breakpoint at specified location.\n\
15687 Argument may be a linespec, explicit, or address location as described below.\n\
15688 \n\
15689 With no argument, clears all breakpoints in the line that the selected frame\n\
15690 is executing in.\n"
15691 "\n" LOCATION_HELP_STRING "\n\
15692 See also the \"delete\" command which clears breakpoints by number."));
15693 add_com_alias ("cl", "clear", class_breakpoint, 1);
15694
15695 c = add_com ("break", class_breakpoint, break_command, _("\
15696 Set breakpoint at specified location.\n"
15697 BREAK_ARGS_HELP ("break")));
15698 set_cmd_completer (c, location_completer);
15699
15700 add_com_alias ("b", "break", class_run, 1);
15701 add_com_alias ("br", "break", class_run, 1);
15702 add_com_alias ("bre", "break", class_run, 1);
15703 add_com_alias ("brea", "break", class_run, 1);
15704
15705 if (dbx_commands)
15706 {
15707 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15708 Break in function/address or break at a line in the current file."),
15709 &stoplist, "stop ", 1, &cmdlist);
15710 add_cmd ("in", class_breakpoint, stopin_command,
15711 _("Break in function or address."), &stoplist);
15712 add_cmd ("at", class_breakpoint, stopat_command,
15713 _("Break at a line in the current file."), &stoplist);
15714 add_com ("status", class_info, info_breakpoints_command, _("\
15715 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15716 The \"Type\" column indicates one of:\n\
15717 \tbreakpoint - normal breakpoint\n\
15718 \twatchpoint - watchpoint\n\
15719 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15720 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15721 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15722 address and file/line number respectively.\n\
15723 \n\
15724 Convenience variable \"$_\" and default examine address for \"x\"\n\
15725 are set to the address of the last breakpoint listed unless the command\n\
15726 is prefixed with \"server \".\n\n\
15727 Convenience variable \"$bpnum\" contains the number of the last\n\
15728 breakpoint set."));
15729 }
15730
15731 add_info ("breakpoints", info_breakpoints_command, _("\
15732 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15733 The \"Type\" column indicates one of:\n\
15734 \tbreakpoint - normal breakpoint\n\
15735 \twatchpoint - watchpoint\n\
15736 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15737 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15738 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15739 address and file/line number respectively.\n\
15740 \n\
15741 Convenience variable \"$_\" and default examine address for \"x\"\n\
15742 are set to the address of the last breakpoint listed unless the command\n\
15743 is prefixed with \"server \".\n\n\
15744 Convenience variable \"$bpnum\" contains the number of the last\n\
15745 breakpoint set."));
15746
15747 add_info_alias ("b", "breakpoints", 1);
15748
15749 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15750 Status of all breakpoints, or breakpoint number NUMBER.\n\
15751 The \"Type\" column indicates one of:\n\
15752 \tbreakpoint - normal breakpoint\n\
15753 \twatchpoint - watchpoint\n\
15754 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15755 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15756 \tuntil - internal breakpoint used by the \"until\" command\n\
15757 \tfinish - internal breakpoint used by the \"finish\" command\n\
15758 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15759 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15760 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15761 address and file/line number respectively.\n\
15762 \n\
15763 Convenience variable \"$_\" and default examine address for \"x\"\n\
15764 are set to the address of the last breakpoint listed unless the command\n\
15765 is prefixed with \"server \".\n\n\
15766 Convenience variable \"$bpnum\" contains the number of the last\n\
15767 breakpoint set."),
15768 &maintenanceinfolist);
15769
15770 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15771 Set catchpoints to catch events."),
15772 &catch_cmdlist, "catch ",
15773 0/*allow-unknown*/, &cmdlist);
15774
15775 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15776 Set temporary catchpoints to catch events."),
15777 &tcatch_cmdlist, "tcatch ",
15778 0/*allow-unknown*/, &cmdlist);
15779
15780 add_catch_command ("fork", _("Catch calls to fork."),
15781 catch_fork_command_1,
15782 NULL,
15783 (void *) (uintptr_t) catch_fork_permanent,
15784 (void *) (uintptr_t) catch_fork_temporary);
15785 add_catch_command ("vfork", _("Catch calls to vfork."),
15786 catch_fork_command_1,
15787 NULL,
15788 (void *) (uintptr_t) catch_vfork_permanent,
15789 (void *) (uintptr_t) catch_vfork_temporary);
15790 add_catch_command ("exec", _("Catch calls to exec."),
15791 catch_exec_command_1,
15792 NULL,
15793 CATCH_PERMANENT,
15794 CATCH_TEMPORARY);
15795 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15796 Usage: catch load [REGEX]\n\
15797 If REGEX is given, only stop for libraries matching the regular expression."),
15798 catch_load_command_1,
15799 NULL,
15800 CATCH_PERMANENT,
15801 CATCH_TEMPORARY);
15802 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15803 Usage: catch unload [REGEX]\n\
15804 If REGEX is given, only stop for libraries matching the regular expression."),
15805 catch_unload_command_1,
15806 NULL,
15807 CATCH_PERMANENT,
15808 CATCH_TEMPORARY);
15809
15810 c = add_com ("watch", class_breakpoint, watch_command, _("\
15811 Set a watchpoint for an expression.\n\
15812 Usage: watch [-l|-location] EXPRESSION\n\
15813 A watchpoint stops execution of your program whenever the value of\n\
15814 an expression changes.\n\
15815 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15816 the memory to which it refers."));
15817 set_cmd_completer (c, expression_completer);
15818
15819 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15820 Set a read watchpoint for an expression.\n\
15821 Usage: rwatch [-l|-location] EXPRESSION\n\
15822 A watchpoint stops execution of your program whenever the value of\n\
15823 an expression is read.\n\
15824 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15825 the memory to which it refers."));
15826 set_cmd_completer (c, expression_completer);
15827
15828 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15829 Set a watchpoint for an expression.\n\
15830 Usage: awatch [-l|-location] EXPRESSION\n\
15831 A watchpoint stops execution of your program whenever the value of\n\
15832 an expression is either read or written.\n\
15833 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15834 the memory to which it refers."));
15835 set_cmd_completer (c, expression_completer);
15836
15837 add_info ("watchpoints", info_watchpoints_command, _("\
15838 Status of specified watchpoints (all watchpoints if no argument)."));
15839
15840 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15841 respond to changes - contrary to the description. */
15842 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15843 &can_use_hw_watchpoints, _("\
15844 Set debugger's willingness to use watchpoint hardware."), _("\
15845 Show debugger's willingness to use watchpoint hardware."), _("\
15846 If zero, gdb will not use hardware for new watchpoints, even if\n\
15847 such is available. (However, any hardware watchpoints that were\n\
15848 created before setting this to nonzero, will continue to use watchpoint\n\
15849 hardware.)"),
15850 NULL,
15851 show_can_use_hw_watchpoints,
15852 &setlist, &showlist);
15853
15854 can_use_hw_watchpoints = 1;
15855
15856 /* Tracepoint manipulation commands. */
15857
15858 c = add_com ("trace", class_breakpoint, trace_command, _("\
15859 Set a tracepoint at specified location.\n\
15860 \n"
15861 BREAK_ARGS_HELP ("trace") "\n\
15862 Do \"help tracepoints\" for info on other tracepoint commands."));
15863 set_cmd_completer (c, location_completer);
15864
15865 add_com_alias ("tp", "trace", class_alias, 0);
15866 add_com_alias ("tr", "trace", class_alias, 1);
15867 add_com_alias ("tra", "trace", class_alias, 1);
15868 add_com_alias ("trac", "trace", class_alias, 1);
15869
15870 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15871 Set a fast tracepoint at specified location.\n\
15872 \n"
15873 BREAK_ARGS_HELP ("ftrace") "\n\
15874 Do \"help tracepoints\" for info on other tracepoint commands."));
15875 set_cmd_completer (c, location_completer);
15876
15877 c = add_com ("strace", class_breakpoint, strace_command, _("\
15878 Set a static tracepoint at location or marker.\n\
15879 \n\
15880 strace [LOCATION] [if CONDITION]\n\
15881 LOCATION may be a linespec, explicit, or address location (described below) \n\
15882 or -m MARKER_ID.\n\n\
15883 If a marker id is specified, probe the marker with that name. With\n\
15884 no LOCATION, uses current execution address of the selected stack frame.\n\
15885 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15886 This collects arbitrary user data passed in the probe point call to the\n\
15887 tracing library. You can inspect it when analyzing the trace buffer,\n\
15888 by printing the $_sdata variable like any other convenience variable.\n\
15889 \n\
15890 CONDITION is a boolean expression.\n\
15891 \n" LOCATION_HELP_STRING "\n\
15892 Multiple tracepoints at one place are permitted, and useful if their\n\
15893 conditions are different.\n\
15894 \n\
15895 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15896 Do \"help tracepoints\" for info on other tracepoint commands."));
15897 set_cmd_completer (c, location_completer);
15898
15899 add_info ("tracepoints", info_tracepoints_command, _("\
15900 Status of specified tracepoints (all tracepoints if no argument).\n\
15901 Convenience variable \"$tpnum\" contains the number of the\n\
15902 last tracepoint set."));
15903
15904 add_info_alias ("tp", "tracepoints", 1);
15905
15906 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15907 Delete specified tracepoints.\n\
15908 Arguments are tracepoint numbers, separated by spaces.\n\
15909 No argument means delete all tracepoints."),
15910 &deletelist);
15911 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15912
15913 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15914 Disable specified tracepoints.\n\
15915 Arguments are tracepoint numbers, separated by spaces.\n\
15916 No argument means disable all tracepoints."),
15917 &disablelist);
15918 deprecate_cmd (c, "disable");
15919
15920 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15921 Enable specified tracepoints.\n\
15922 Arguments are tracepoint numbers, separated by spaces.\n\
15923 No argument means enable all tracepoints."),
15924 &enablelist);
15925 deprecate_cmd (c, "enable");
15926
15927 add_com ("passcount", class_trace, trace_pass_command, _("\
15928 Set the passcount for a tracepoint.\n\
15929 The trace will end when the tracepoint has been passed 'count' times.\n\
15930 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15931 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15932
15933 add_prefix_cmd ("save", class_breakpoint, save_command,
15934 _("Save breakpoint definitions as a script."),
15935 &save_cmdlist, "save ",
15936 0/*allow-unknown*/, &cmdlist);
15937
15938 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15939 Save current breakpoint definitions as a script.\n\
15940 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15941 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15942 session to restore them."),
15943 &save_cmdlist);
15944 set_cmd_completer (c, filename_completer);
15945
15946 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15947 Save current tracepoint definitions as a script.\n\
15948 Use the 'source' command in another debug session to restore them."),
15949 &save_cmdlist);
15950 set_cmd_completer (c, filename_completer);
15951
15952 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15953 deprecate_cmd (c, "save tracepoints");
15954
15955 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
15956 Breakpoint specific settings\n\
15957 Configure various breakpoint-specific variables such as\n\
15958 pending breakpoint behavior"),
15959 &breakpoint_set_cmdlist, "set breakpoint ",
15960 0/*allow-unknown*/, &setlist);
15961 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
15962 Breakpoint specific settings\n\
15963 Configure various breakpoint-specific variables such as\n\
15964 pending breakpoint behavior"),
15965 &breakpoint_show_cmdlist, "show breakpoint ",
15966 0/*allow-unknown*/, &showlist);
15967
15968 add_setshow_auto_boolean_cmd ("pending", no_class,
15969 &pending_break_support, _("\
15970 Set debugger's behavior regarding pending breakpoints."), _("\
15971 Show debugger's behavior regarding pending breakpoints."), _("\
15972 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15973 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15974 an error. If auto, an unrecognized breakpoint location results in a\n\
15975 user-query to see if a pending breakpoint should be created."),
15976 NULL,
15977 show_pending_break_support,
15978 &breakpoint_set_cmdlist,
15979 &breakpoint_show_cmdlist);
15980
15981 pending_break_support = AUTO_BOOLEAN_AUTO;
15982
15983 add_setshow_boolean_cmd ("auto-hw", no_class,
15984 &automatic_hardware_breakpoints, _("\
15985 Set automatic usage of hardware breakpoints."), _("\
15986 Show automatic usage of hardware breakpoints."), _("\
15987 If set, the debugger will automatically use hardware breakpoints for\n\
15988 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15989 a warning will be emitted for such breakpoints."),
15990 NULL,
15991 show_automatic_hardware_breakpoints,
15992 &breakpoint_set_cmdlist,
15993 &breakpoint_show_cmdlist);
15994
15995 add_setshow_boolean_cmd ("always-inserted", class_support,
15996 &always_inserted_mode, _("\
15997 Set mode for inserting breakpoints."), _("\
15998 Show mode for inserting breakpoints."), _("\
15999 When this mode is on, breakpoints are inserted immediately as soon as\n\
16000 they're created, kept inserted even when execution stops, and removed\n\
16001 only when the user deletes them. When this mode is off (the default),\n\
16002 breakpoints are inserted only when execution continues, and removed\n\
16003 when execution stops."),
16004 NULL,
16005 &show_always_inserted_mode,
16006 &breakpoint_set_cmdlist,
16007 &breakpoint_show_cmdlist);
16008
16009 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16010 condition_evaluation_enums,
16011 &condition_evaluation_mode_1, _("\
16012 Set mode of breakpoint condition evaluation."), _("\
16013 Show mode of breakpoint condition evaluation."), _("\
16014 When this is set to \"host\", breakpoint conditions will be\n\
16015 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16016 breakpoint conditions will be downloaded to the target (if the target\n\
16017 supports such feature) and conditions will be evaluated on the target's side.\n\
16018 If this is set to \"auto\" (default), this will be automatically set to\n\
16019 \"target\" if it supports condition evaluation, otherwise it will\n\
16020 be set to \"gdb\""),
16021 &set_condition_evaluation_mode,
16022 &show_condition_evaluation_mode,
16023 &breakpoint_set_cmdlist,
16024 &breakpoint_show_cmdlist);
16025
16026 add_com ("break-range", class_breakpoint, break_range_command, _("\
16027 Set a breakpoint for an address range.\n\
16028 break-range START-LOCATION, END-LOCATION\n\
16029 where START-LOCATION and END-LOCATION can be one of the following:\n\
16030 LINENUM, for that line in the current file,\n\
16031 FILE:LINENUM, for that line in that file,\n\
16032 +OFFSET, for that number of lines after the current line\n\
16033 or the start of the range\n\
16034 FUNCTION, for the first line in that function,\n\
16035 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16036 *ADDRESS, for the instruction at that address.\n\
16037 \n\
16038 The breakpoint will stop execution of the inferior whenever it executes\n\
16039 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16040 range (including START-LOCATION and END-LOCATION)."));
16041
16042 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16043 Set a dynamic printf at specified location.\n\
16044 dprintf location,format string,arg1,arg2,...\n\
16045 location may be a linespec, explicit, or address location.\n"
16046 "\n" LOCATION_HELP_STRING));
16047 set_cmd_completer (c, location_completer);
16048
16049 add_setshow_enum_cmd ("dprintf-style", class_support,
16050 dprintf_style_enums, &dprintf_style, _("\
16051 Set the style of usage for dynamic printf."), _("\
16052 Show the style of usage for dynamic printf."), _("\
16053 This setting chooses how GDB will do a dynamic printf.\n\
16054 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16055 console, as with the \"printf\" command.\n\
16056 If the value is \"call\", the print is done by calling a function in your\n\
16057 program; by default printf(), but you can choose a different function or\n\
16058 output stream by setting dprintf-function and dprintf-channel."),
16059 update_dprintf_commands, NULL,
16060 &setlist, &showlist);
16061
16062 dprintf_function = xstrdup ("printf");
16063 add_setshow_string_cmd ("dprintf-function", class_support,
16064 &dprintf_function, _("\
16065 Set the function to use for dynamic printf"), _("\
16066 Show the function to use for dynamic printf"), NULL,
16067 update_dprintf_commands, NULL,
16068 &setlist, &showlist);
16069
16070 dprintf_channel = xstrdup ("");
16071 add_setshow_string_cmd ("dprintf-channel", class_support,
16072 &dprintf_channel, _("\
16073 Set the channel to use for dynamic printf"), _("\
16074 Show the channel to use for dynamic printf"), NULL,
16075 update_dprintf_commands, NULL,
16076 &setlist, &showlist);
16077
16078 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16079 &disconnected_dprintf, _("\
16080 Set whether dprintf continues after GDB disconnects."), _("\
16081 Show whether dprintf continues after GDB disconnects."), _("\
16082 Use this to let dprintf commands continue to hit and produce output\n\
16083 even if GDB disconnects or detaches from the target."),
16084 NULL,
16085 NULL,
16086 &setlist, &showlist);
16087
16088 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16089 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16090 (target agent only) This is useful for formatted output in user-defined commands."));
16091
16092 automatic_hardware_breakpoints = 1;
16093
16094 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16095 observer_attach_thread_exit (remove_threaded_breakpoints);
16096 }
This page took 0.335086 seconds and 5 git commands to generate.